gdb: add some additional debug output in remote.c
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
3666a048 3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
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;
835
52356b79
YQ
836 /* Adjust the partial die before generating a symbol for it. This
837 function may set the is_external flag or change the DIE's
838 name. */
839 void fixup (struct dwarf2_cu *cu);
840
48fbe735
YQ
841 /* Read a minimal amount of information into the minimal die
842 structure. */
843 const gdb_byte *read (const struct die_reader_specs *reader,
844 const struct abbrev_info &abbrev,
845 const gdb_byte *info_ptr);
846
7d00ffec
TT
847 /* Compute the name of this partial DIE. This memoizes the
848 result, so it is safe to call multiple times. */
849 const char *name (dwarf2_cu *cu);
850
72bf9492 851 /* Offset of this DIE. */
6f06d47b 852 const sect_offset sect_off;
72bf9492
DJ
853
854 /* DWARF-2 tag for this DIE. */
6f06d47b 855 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 856
72bf9492 857 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
858 const unsigned int has_children : 1;
859
72bf9492
DJ
860 unsigned int is_external : 1;
861 unsigned int is_declaration : 1;
862 unsigned int has_type : 1;
863 unsigned int has_specification : 1;
864 unsigned int has_pc_info : 1;
481860b3 865 unsigned int may_be_inlined : 1;
72bf9492 866
0c1b455e
TT
867 /* This DIE has been marked DW_AT_main_subprogram. */
868 unsigned int main_subprogram : 1;
869
72bf9492
DJ
870 /* Flag set if the SCOPE field of this structure has been
871 computed. */
872 unsigned int scope_set : 1;
873
fa4028e9
JB
874 /* Flag set if the DIE has a byte_size attribute. */
875 unsigned int has_byte_size : 1;
876
ff908ebf
AW
877 /* Flag set if the DIE has a DW_AT_const_value attribute. */
878 unsigned int has_const_value : 1;
879
98bfdba5
PA
880 /* Flag set if any of the DIE's children are template arguments. */
881 unsigned int has_template_arguments : 1;
882
52356b79 883 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
884 unsigned int fixup_called : 1;
885
36586728
TT
886 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
887 unsigned int is_dwz : 1;
888
889 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
890 unsigned int spec_is_dwz : 1;
891
7d00ffec
TT
892 unsigned int canonical_name : 1;
893
72bf9492 894 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 895 sometimes a default name for unnamed DIEs. */
7d00ffec 896 const char *raw_name = nullptr;
72bf9492 897
abc72ce4 898 /* The linkage name, if present. */
6f06d47b 899 const char *linkage_name = nullptr;
abc72ce4 900
72bf9492
DJ
901 /* The scope to prepend to our children. This is generally
902 allocated on the comp_unit_obstack, so will disappear
903 when this compilation unit leaves the cache. */
6f06d47b 904 const char *scope = nullptr;
72bf9492 905
95554aad
TT
906 /* Some data associated with the partial DIE. The tag determines
907 which field is live. */
908 union
909 {
910 /* The location description associated with this DIE, if any. */
911 struct dwarf_block *locdesc;
912 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 913 sect_offset sect_off;
6f06d47b 914 } d {};
72bf9492
DJ
915
916 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
917 CORE_ADDR lowpc = 0;
918 CORE_ADDR highpc = 0;
72bf9492 919
93311388 920 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 921 DW_AT_sibling, if any. */
48fbe735
YQ
922 /* NOTE: This member isn't strictly necessary, partial_die_info::read
923 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 924 const gdb_byte *sibling = nullptr;
72bf9492
DJ
925
926 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
927 DW_AT_specification (or DW_AT_abstract_origin or
928 DW_AT_extension). */
6f06d47b 929 sect_offset spec_offset {};
72bf9492
DJ
930
931 /* Pointers to this DIE's parent, first child, and next sibling,
932 if any. */
6f06d47b
YQ
933 struct partial_die_info *die_parent = nullptr;
934 struct partial_die_info *die_child = nullptr;
935 struct partial_die_info *die_sibling = nullptr;
936
937 friend struct partial_die_info *
938 dwarf2_cu::find_partial_die (sect_offset sect_off);
939
940 private:
941 /* Only need to do look up in dwarf2_cu::find_partial_die. */
942 partial_die_info (sect_offset sect_off)
943 : partial_die_info (sect_off, DW_TAG_padding, 0)
944 {
945 }
946
947 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
948 int has_children_)
949 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
950 {
951 is_external = 0;
952 is_declaration = 0;
953 has_type = 0;
954 has_specification = 0;
955 has_pc_info = 0;
956 may_be_inlined = 0;
957 main_subprogram = 0;
958 scope_set = 0;
959 has_byte_size = 0;
960 has_const_value = 0;
961 has_template_arguments = 0;
962 fixup_called = 0;
963 is_dwz = 0;
964 spec_is_dwz = 0;
7d00ffec 965 canonical_name = 0;
6f06d47b 966 }
c906108c
SS
967 };
968
c906108c
SS
969/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
970 but this would require a corresponding change in unpack_field_as_long
971 and friends. */
972static int bits_per_byte = 8;
973
9c6a1327
TT
974struct variant_part_builder;
975
976/* When reading a variant, we track a bit more information about the
977 field, and store it in an object of this type. */
2ddeaf8a
TT
978
979struct variant_field
980{
9c6a1327
TT
981 int first_field = -1;
982 int last_field = -1;
983
984 /* A variant can contain other variant parts. */
985 std::vector<variant_part_builder> variant_parts;
986
2ddeaf8a
TT
987 /* If we see a DW_TAG_variant, then this will be set if this is the
988 default branch. */
9c6a1327
TT
989 bool default_branch = false;
990 /* If we see a DW_AT_discr_value, then this will be the discriminant
991 value. */
992 ULONGEST discriminant_value = 0;
993 /* If we see a DW_AT_discr_list, then this is a pointer to the list
994 data. */
995 struct dwarf_block *discr_list_data = nullptr;
996};
997
998/* This represents a DW_TAG_variant_part. */
999
1000struct variant_part_builder
1001{
1002 /* The offset of the discriminant field. */
1003 sect_offset discriminant_offset {};
1004
1005 /* Variants that are direct children of this variant part. */
1006 std::vector<variant_field> variants;
1007
1008 /* True if we're currently reading a variant. */
1009 bool processing_variant = false;
2ddeaf8a
TT
1010};
1011
52059ffd
TT
1012struct nextfield
1013{
be2daae6
TT
1014 int accessibility = 0;
1015 int virtuality = 0;
9c6a1327
TT
1016 /* Variant parts need to find the discriminant, which is a DIE
1017 reference. We track the section offset of each field to make
1018 this link. */
1019 sect_offset offset;
be2daae6 1020 struct field field {};
52059ffd
TT
1021};
1022
1023struct fnfieldlist
1024{
be2daae6
TT
1025 const char *name = nullptr;
1026 std::vector<struct fn_field> fnfields;
52059ffd
TT
1027};
1028
c906108c
SS
1029/* The routines that read and process dies for a C struct or C++ class
1030 pass lists of data member fields and lists of member function fields
1031 in an instance of a field_info structure, as defined below. */
1032struct field_info
2de01bdb
SM
1033{
1034 /* List of data member and baseclasses fields. */
1035 std::vector<struct nextfield> fields;
1036 std::vector<struct nextfield> baseclasses;
1037
1038 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1039 bool non_public_fields = false;
2de01bdb
SM
1040
1041 /* Member function fieldlist array, contains name of possibly overloaded
1042 member function, number of overloaded member functions and a pointer
1043 to the head of the member function field chain. */
1044 std::vector<struct fnfieldlist> fnfieldlists;
1045
1046 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1047 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1048 std::vector<struct decl_field> typedef_field_list;
1049
1050 /* Nested types defined by this class and the number of elements in this
1051 list. */
1052 std::vector<struct decl_field> nested_types_list;
1053
1054 /* If non-null, this is the variant part we are currently
1055 reading. */
1056 variant_part_builder *current_variant_part = nullptr;
1057 /* This holds all the top-level variant parts attached to the type
1058 we're reading. */
1059 std::vector<variant_part_builder> variant_parts;
1060
1061 /* Return the total number of fields (including baseclasses). */
1062 int nfields () const
c5aa993b 1063 {
2de01bdb
SM
1064 return fields.size () + baseclasses.size ();
1065 }
1066};
c906108c 1067
ae038cb0
DJ
1068/* Loaded secondary compilation units are kept in memory until they
1069 have not been referenced for the processing of this many
1070 compilation units. Set this to zero to disable caching. Cache
1071 sizes of up to at least twenty will improve startup time for
1072 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1073static int dwarf_max_cache_age = 5;
920d2a44 1074static void
b4f54984
DE
1075show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1076 struct cmd_list_element *c, const char *value)
920d2a44 1077{
3e43a32a 1078 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1079 "DWARF compilation units is %s.\n"),
920d2a44
AC
1080 value);
1081}
4390d890 1082\f
c906108c
SS
1083/* local function prototypes */
1084
918dd910
JK
1085static void dwarf2_find_base_address (struct die_info *die,
1086 struct dwarf2_cu *cu);
1087
891813be 1088static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1089 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1090 const char *name);
0018ea6f 1091
f1902523
JK
1092static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1093 const gdb_byte *info_ptr,
3e225074 1094 struct die_info *type_unit_die);
f1902523 1095
976ca316 1096static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1097
72bf9492
DJ
1098static void scan_partial_symbols (struct partial_die_info *,
1099 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1100 int, struct dwarf2_cu *);
c906108c 1101
72bf9492
DJ
1102static void add_partial_symbol (struct partial_die_info *,
1103 struct dwarf2_cu *);
63d06c5c 1104
72bf9492
DJ
1105static void add_partial_namespace (struct partial_die_info *pdi,
1106 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1107 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1108
5d7cb8df 1109static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1110 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1111 struct dwarf2_cu *cu);
1112
72bf9492
DJ
1113static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1114 struct dwarf2_cu *cu);
91c24f0a 1115
bc30ff58
JB
1116static void add_partial_subprogram (struct partial_die_info *pdi,
1117 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1118 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1119
d521ce57 1120static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1121
dee91e82 1122static struct partial_die_info *load_partial_dies
d521ce57 1123 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1124
fb816e8b
TV
1125/* A pair of partial_die_info and compilation unit. */
1126struct cu_partial_die_info
1127{
1128 /* The compilation unit of the partial_die_info. */
1129 struct dwarf2_cu *cu;
1130 /* A partial_die_info. */
1131 struct partial_die_info *pdi;
122cf0f2
AB
1132
1133 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1134 : cu (cu),
1135 pdi (pdi)
405feb71 1136 { /* Nothing. */ }
122cf0f2
AB
1137
1138private:
1139 cu_partial_die_info () = delete;
fb816e8b
TV
1140};
1141
122cf0f2
AB
1142static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1143 struct dwarf2_cu *);
72bf9492 1144
d521ce57 1145static const gdb_byte *read_attribute (const struct die_reader_specs *,
4444f407
TT
1146 struct attribute *,
1147 const struct attr_abbrev *,
7a5f294d 1148 const gdb_byte *);
18a8505e
AT
1149
1150static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1151 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1152
1153static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1154
976ca316
SM
1155static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1156 dwarf2_section_info *, sect_offset);
f4dc4d17 1157
ed2dc618 1158static const char *read_indirect_string
976ca316 1159 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1160 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1161
ed2dc618 1162static const char *read_indirect_string_at_offset
976ca316 1163 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1164
d521ce57
TT
1165static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1166 const gdb_byte *,
3019eac3
DE
1167 unsigned int *);
1168
18a8505e
AT
1169static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1170 ULONGEST str_index);
1171
1172static const char *read_stub_str_index (struct dwarf2_cu *cu,
1173 ULONGEST str_index);
3019eac3 1174
e142c38c 1175static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1176
e142c38c
DJ
1177static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1178 struct dwarf2_cu *);
c906108c 1179
7d45c7c3 1180static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
dda83cd7 1181 struct dwarf2_cu *cu);
7d45c7c3 1182
a084a2a6
AT
1183static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1184
05cf31d1 1185static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
dda83cd7 1186 struct dwarf2_cu *cu);
05cf31d1 1187
e142c38c 1188static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1189
e142c38c 1190static struct die_info *die_specification (struct die_info *die,
f2f0e013 1191 struct dwarf2_cu **);
63d06c5c 1192
9c541725 1193static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1194 struct dwarf2_cu *cu);
debd256d 1195
f3f5162e 1196static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1197 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1198 CORE_ADDR, int decode_mapping);
c906108c 1199
804d2729
TT
1200static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1201 const char *);
c906108c 1202
a14ed312 1203static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1204 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1205
ff39bb5e 1206static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1207 struct dwarf2_cu *);
c906108c 1208
ff39bb5e 1209static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1210 struct type *type,
1211 const char *name,
1212 struct obstack *obstack,
12df843f 1213 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1214 const gdb_byte **bytes,
98bfdba5 1215 struct dwarf2_locexpr_baton **baton);
2df3850c 1216
57567375
TT
1217static struct type *read_subrange_index_type (struct die_info *die,
1218 struct dwarf2_cu *cu);
1219
e7c27a73 1220static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1221
b4ba55a1
JB
1222static int need_gnat_info (struct dwarf2_cu *);
1223
3e43a32a
MS
1224static struct type *die_descriptive_type (struct die_info *,
1225 struct dwarf2_cu *);
b4ba55a1
JB
1226
1227static void set_descriptive_type (struct type *, struct die_info *,
1228 struct dwarf2_cu *);
1229
e7c27a73
DJ
1230static struct type *die_containing_type (struct die_info *,
1231 struct dwarf2_cu *);
c906108c 1232
ff39bb5e 1233static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1234 struct dwarf2_cu *);
c906108c 1235
f792889a 1236static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1237
673bfd45
DE
1238static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1239
0d5cff50 1240static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1241
6e70227d 1242static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1243 const char *suffix, int physname,
1244 struct dwarf2_cu *cu);
63d06c5c 1245
e7c27a73 1246static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1247
348e048f
DE
1248static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1249
e7c27a73 1250static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1251
e7c27a73 1252static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1253
96408a79
SA
1254static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1255
71a3c369
TT
1256static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1257
41144253 1258/* Return the .debug_loclists section to use for cu. */
1259static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1260
d0ce17d8
CT
1261/* Return the .debug_rnglists section to use for cu. */
1262static struct dwarf2_section_info *cu_debug_rnglists_section
1263 (struct dwarf2_cu *cu, dwarf_tag tag);
1264
3a2b436a 1265/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1266 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1267enum pc_bounds_kind
1268{
e385593e 1269 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1270 PC_BOUNDS_NOT_PRESENT,
1271
e385593e
JK
1272 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1273 were present but they do not form a valid range of PC addresses. */
1274 PC_BOUNDS_INVALID,
1275
3a2b436a
JK
1276 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1277 PC_BOUNDS_RANGES,
1278
1279 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1280 PC_BOUNDS_HIGH_LOW,
1281};
1282
1283static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1284 CORE_ADDR *, CORE_ADDR *,
1285 struct dwarf2_cu *,
891813be 1286 dwarf2_psymtab *);
c906108c 1287
fae299cd
DC
1288static void get_scope_pc_bounds (struct die_info *,
1289 CORE_ADDR *, CORE_ADDR *,
1290 struct dwarf2_cu *);
1291
801e3a5b 1292static void dwarf2_record_block_ranges (struct die_info *, struct block *,
dda83cd7 1293 CORE_ADDR, struct dwarf2_cu *);
801e3a5b 1294
a14ed312 1295static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1296 struct dwarf2_cu *);
c906108c 1297
a14ed312 1298static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1299 struct type *, struct dwarf2_cu *);
c906108c 1300
a14ed312 1301static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1302 struct die_info *, struct type *,
e7c27a73 1303 struct dwarf2_cu *);
c906108c 1304
a14ed312 1305static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1306 struct type *,
1307 struct dwarf2_cu *);
c906108c 1308
134d01f1 1309static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1310
e7c27a73 1311static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1312
e7c27a73 1313static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1314
5d7cb8df
JK
1315static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1316
804d2729 1317static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1318
27aa8d6a
SW
1319static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1320
74921315
KS
1321static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1322
f55ee35c
JK
1323static struct type *read_module_type (struct die_info *die,
1324 struct dwarf2_cu *cu);
1325
38d518c9 1326static const char *namespace_name (struct die_info *die,
e142c38c 1327 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1328
134d01f1 1329static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1330
7d79de9a
TT
1331static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1332 bool * = nullptr);
c906108c 1333
6e70227d 1334static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1335 struct dwarf2_cu *);
1336
bf6af496 1337static struct die_info *read_die_and_siblings_1
d521ce57 1338 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1339 struct die_info *);
639d11d3 1340
dee91e82 1341static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1342 const gdb_byte *info_ptr,
1343 const gdb_byte **new_info_ptr,
639d11d3
DC
1344 struct die_info *parent);
1345
d521ce57
TT
1346static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1347 struct die_info **, const gdb_byte *,
3e225074 1348 int);
3019eac3 1349
d521ce57 1350static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1351 struct die_info **, const gdb_byte *);
93311388 1352
e7c27a73 1353static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1354
15d034d0 1355static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1356 struct objfile *);
71c25dea 1357
15d034d0 1358static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1359
15d034d0 1360static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1361 struct die_info *die,
1362 struct dwarf2_cu *cu);
1363
ca69b9e6
DE
1364static const char *dwarf2_physname (const char *name, struct die_info *die,
1365 struct dwarf2_cu *cu);
1366
e142c38c 1367static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1368 struct dwarf2_cu **);
9219021c 1369
d97bc12b
DE
1370static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1371
1372static void dump_die_for_error (struct die_info *);
1373
1374static void dump_die_1 (struct ui_file *, int level, int max_level,
1375 struct die_info *);
c906108c 1376
d97bc12b 1377/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1378
51545339 1379static void store_in_ref_table (struct die_info *,
10b3939b 1380 struct dwarf2_cu *);
c906108c 1381
348e048f 1382static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1383 const struct attribute *,
348e048f
DE
1384 struct dwarf2_cu **);
1385
10b3939b 1386static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1387 const struct attribute *,
f2f0e013 1388 struct dwarf2_cu **);
c906108c 1389
348e048f 1390static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1391 const struct attribute *,
348e048f
DE
1392 struct dwarf2_cu **);
1393
ac9ec31b
DE
1394static struct type *get_signatured_type (struct die_info *, ULONGEST,
1395 struct dwarf2_cu *);
1396
1397static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1398 const struct attribute *,
ac9ec31b
DE
1399 struct dwarf2_cu *);
1400
ab432490
SM
1401static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1402 dwarf2_per_objfile *per_objfile);
348e048f 1403
ab432490
SM
1404static void read_signatured_type (signatured_type *sig_type,
1405 dwarf2_per_objfile *per_objfile);
348e048f 1406
63e43d3a
PMR
1407static int attr_to_dynamic_prop (const struct attribute *attr,
1408 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1409 struct dynamic_prop *prop, struct type *type);
63e43d3a 1410
c906108c
SS
1411/* memory allocation interface */
1412
7b5a2f43 1413static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1414
b60c80d6 1415static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1416
43f3e411 1417static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1418
8cf6f0b1
TT
1419static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1420 struct dwarf2_loclist_baton *baton,
ff39bb5e 1421 const struct attribute *attr);
8cf6f0b1 1422
ff39bb5e 1423static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1424 struct symbol *sym,
f1e6e072
TT
1425 struct dwarf2_cu *cu,
1426 int is_block);
4c2df51b 1427
d521ce57
TT
1428static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1429 const gdb_byte *info_ptr,
7c32eebb 1430 const struct abbrev_info *abbrev);
4bb7a0a7 1431
72bf9492
DJ
1432static hashval_t partial_die_hash (const void *item);
1433
1434static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1435
ae038cb0 1436static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1437 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1438 dwarf2_per_objfile *per_objfile);
ae038cb0 1439
9816fde3 1440static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1441 struct die_info *comp_unit_die,
1442 enum language pretend_language);
93311388 1443
f792889a 1444static struct type *set_die_type (struct die_info *, struct type *,
57567375 1445 struct dwarf2_cu *, bool = false);
1c379e20 1446
976ca316 1447static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1448
ab432490
SM
1449static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1450 dwarf2_per_objfile *per_objfile,
4a636814 1451 dwarf2_cu *existing_cu,
ab432490
SM
1452 bool skip_partial,
1453 enum language pretend_language);
10b3939b 1454
8fc0b21d 1455static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1456 enum language pretend_language);
10b3939b 1457
8fc0b21d 1458static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1459 enum language pretend_language);
f4dc4d17 1460
b64f50a1 1461static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1462 dwarf2_per_cu_data *per_cu,
1463 dwarf2_per_objfile *per_objfile);
673bfd45 1464
f792889a 1465static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1466
120ce1b5
SM
1467static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1468 dwarf2_per_objfile *per_objfile,
95554aad
TT
1469 enum language pretend_language);
1470
976ca316 1471static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1472
b303c6f6
AB
1473/* Class, the destructor of which frees all allocated queue entries. This
1474 will only have work to do if an error was thrown while processing the
1475 dwarf. If no error was thrown then the queue entries should have all
1476 been processed, and freed, as we went along. */
1477
1478class dwarf2_queue_guard
1479{
1480public:
39856def
TT
1481 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1482 : m_per_objfile (per_objfile)
1483 {
08ac5771
SM
1484 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1485
1486 m_per_objfile->per_bfd->queue.emplace ();
39856def 1487 }
b303c6f6
AB
1488
1489 /* Free any entries remaining on the queue. There should only be
1490 entries left if we hit an error while processing the dwarf. */
1491 ~dwarf2_queue_guard ()
1492 {
08ac5771
SM
1493 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1494
1495 m_per_objfile->per_bfd->queue.reset ();
39856def 1496 }
b303c6f6 1497
39856def 1498 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1499
39856def
TT
1500private:
1501 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1502};
1503
39856def
TT
1504dwarf2_queue_item::~dwarf2_queue_item ()
1505{
1506 /* Anything still marked queued is likely to be in an
1507 inconsistent state, so discard it. */
1508 if (per_cu->queued)
1509 {
7188ed02 1510 per_objfile->remove_cu (per_cu);
39856def
TT
1511 per_cu->queued = 0;
1512 }
1513}
1514
473ab964
TT
1515/* See dwarf2/read.h. */
1516
1517void
1518dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1519{
1520 if (data->is_debug_types)
1521 delete static_cast<signatured_type *> (data);
1522 else
1523 delete data;
1524}
1525
d721ba37
PA
1526/* The return type of find_file_and_directory. Note, the enclosed
1527 string pointers are only valid while this object is valid. */
1528
1529struct file_and_directory
1530{
1531 /* The filename. This is never NULL. */
1532 const char *name;
1533
1534 /* The compilation directory. NULL if not known. If we needed to
1535 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1536 points directly to the DW_AT_comp_dir string attribute owned by
1537 the obstack that owns the DIE. */
1538 const char *comp_dir;
1539
1540 /* If we needed to build a new string for comp_dir, this is what
1541 owns the storage. */
1542 std::string comp_dir_storage;
1543};
1544
1545static file_and_directory find_file_and_directory (struct die_info *die,
1546 struct dwarf2_cu *cu);
9291a0cd 1547
298e9637 1548static htab_up allocate_signatured_type_table ();
1fd400ff 1549
298e9637 1550static htab_up allocate_dwo_unit_table ();
3019eac3 1551
57d63ce2 1552static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1553 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1554 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1555
976ca316 1556static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1557
3019eac3 1558static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1559 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1560 ULONGEST signature);
3019eac3
DE
1561
1562static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1563 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1564
1b555f17 1565static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1566
263db9a1
TT
1567/* A unique pointer to a dwo_file. */
1568
51ac9db5 1569typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1570
976ca316 1571static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1572
1b80a9fa 1573static void check_producer (struct dwarf2_cu *cu);
4390d890
DE
1574\f
1575/* Various complaints about symbol reading that don't abort the process. */
1576
4390d890
DE
1577static void
1578dwarf2_debug_line_missing_file_complaint (void)
1579{
b98664d3 1580 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1581}
1582
1583static void
1584dwarf2_debug_line_missing_end_sequence_complaint (void)
1585{
b98664d3 1586 complaint (_(".debug_line section has line "
4390d890
DE
1587 "program sequence without an end"));
1588}
1589
1590static void
1591dwarf2_complex_location_expr_complaint (void)
1592{
b98664d3 1593 complaint (_("location expression too complex"));
4390d890
DE
1594}
1595
1596static void
1597dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1598 int arg3)
1599{
b98664d3 1600 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1601 arg1, arg2, arg3);
1602}
1603
4390d890
DE
1604static void
1605dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1606{
b98664d3 1607 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1608 arg1, arg2);
1609}
527f3840
JK
1610
1611/* Hash function for line_header_hash. */
1612
1613static hashval_t
1614line_header_hash (const struct line_header *ofs)
1615{
9c541725 1616 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1617}
1618
1619/* Hash function for htab_create_alloc_ex for line_header_hash. */
1620
1621static hashval_t
1622line_header_hash_voidp (const void *item)
1623{
9a3c8263 1624 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1625
1626 return line_header_hash (ofs);
1627}
1628
1629/* Equality function for line_header_hash. */
1630
1631static int
1632line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1633{
9a3c8263
SM
1634 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1635 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1636
9c541725 1637 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1638 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1639}
1640
4390d890 1641\f
9291a0cd 1642
330cdd98
PA
1643/* See declaration. */
1644
5989a64e
SM
1645dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1646 bool can_copy_)
c3699833
SM
1647 : obfd (obfd),
1648 can_copy (can_copy_)
330cdd98
PA
1649{
1650 if (names == NULL)
1651 names = &dwarf2_elf_names;
1652
330cdd98
PA
1653 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1654 locate_sections (obfd, sec, *names);
1655}
1656
5989a64e 1657dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1658{
0d305d5c 1659 for (auto &per_cu : all_comp_units)
ae640021 1660 per_cu->imported_symtabs_free ();
fc8e7e75 1661
5989a64e 1662 /* Everything else should be on this->obstack. */
330cdd98
PA
1663}
1664
7188ed02 1665/* See read.h. */
330cdd98
PA
1666
1667void
7188ed02 1668dwarf2_per_objfile::remove_all_cus ()
330cdd98 1669{
08ac5771
SM
1670 gdb_assert (!this->per_bfd->queue.has_value ());
1671
7188ed02
SM
1672 for (auto pair : m_dwarf2_cus)
1673 delete pair.second;
330cdd98 1674
7188ed02 1675 m_dwarf2_cus.clear ();
330cdd98
PA
1676}
1677
11ed8cad
TT
1678/* A helper class that calls free_cached_comp_units on
1679 destruction. */
1680
1681class free_cached_comp_units
1682{
1683public:
1684
1685 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1686 : m_per_objfile (per_objfile)
1687 {
1688 }
1689
1690 ~free_cached_comp_units ()
1691 {
7188ed02 1692 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1693 }
1694
1695 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1696
1697private:
1698
1699 dwarf2_per_objfile *m_per_objfile;
1700};
1701
af758d11
SM
1702/* See read.h. */
1703
1704bool
1705dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1706{
1707 gdb_assert (per_cu->index < this->m_symtabs.size ());
1708
1709 return this->m_symtabs[per_cu->index] != nullptr;
1710}
1711
1712/* See read.h. */
1713
1714compunit_symtab *
1715dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1716{
1717 gdb_assert (per_cu->index < this->m_symtabs.size ());
1718
1719 return this->m_symtabs[per_cu->index];
1720}
1721
1722/* See read.h. */
1723
1724void
1725dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1726 compunit_symtab *symtab)
1727{
1728 gdb_assert (per_cu->index < this->m_symtabs.size ());
1729 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1730
1731 this->m_symtabs[per_cu->index] = symtab;
1732}
1733
c906108c 1734/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1735 information and return true if we have enough to do something.
1736 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1737 ELF names are used. CAN_COPY is true for formats where symbol
1738 interposition is possible and so symbol values must follow copy
1739 relocation rules. */
c906108c
SS
1740
1741int
251d32d9 1742dwarf2_has_info (struct objfile *objfile,
dda83cd7 1743 const struct dwarf2_debug_sections *names,
4b610737 1744 bool can_copy)
c906108c 1745{
97cbe998
SDJ
1746 if (objfile->flags & OBJF_READNEVER)
1747 return 0;
1748
976ca316 1749 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1750
976ca316 1751 if (per_objfile == NULL)
5989a64e 1752 {
17ee85fc
TT
1753 dwarf2_per_bfd *per_bfd;
1754
8a91fbdf
SM
1755 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1756 BFD doesn't require relocations.
1757
1758 We don't share with objfiles for which -readnow was requested,
1759 because it would complicate things when loading the same BFD with
1760 -readnow and then without -readnow. */
1761 if (!gdb_bfd_requires_relocations (objfile->obfd)
1762 && (objfile->flags & OBJF_READNOW) == 0)
17ee85fc
TT
1763 {
1764 /* See if one has been created for this BFD yet. */
1765 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1766
1767 if (per_bfd == nullptr)
1768 {
1769 /* No, create it now. */
1770 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1771 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1772 }
1773 }
1774 else
1775 {
1776 /* No sharing possible, create one specifically for this objfile. */
1777 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1778 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1779 }
5989a64e 1780
976ca316 1781 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1782 }
5bfd760d 1783
976ca316
SM
1784 return (!per_objfile->per_bfd->info.is_virtual
1785 && per_objfile->per_bfd->info.s.section != NULL
1786 && !per_objfile->per_bfd->abbrev.is_virtual
1787 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1788}
1789
330cdd98 1790/* See declaration. */
c906108c 1791
330cdd98 1792void
5989a64e
SM
1793dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1794 const dwarf2_debug_sections &names)
c906108c 1795{
fd361982 1796 flagword aflag = bfd_section_flags (sectp);
251d32d9 1797
dc7650b8
JK
1798 if ((aflag & SEC_HAS_CONTENTS) == 0)
1799 {
1800 }
950b7495
KS
1801 else if (elf_section_data (sectp)->this_hdr.sh_size
1802 > bfd_get_file_size (abfd))
1803 {
1804 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1805 warning (_("Discarding section %s which has a section size (%s"
1806 ") larger than the file size [in module %s]"),
1807 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1808 bfd_get_filename (abfd));
1809 }
fbedd546 1810 else if (names.info.matches (sectp->name))
c906108c 1811 {
330cdd98 1812 this->info.s.section = sectp;
fd361982 1813 this->info.size = bfd_section_size (sectp);
c906108c 1814 }
fbedd546 1815 else if (names.abbrev.matches (sectp->name))
c906108c 1816 {
330cdd98 1817 this->abbrev.s.section = sectp;
fd361982 1818 this->abbrev.size = bfd_section_size (sectp);
c906108c 1819 }
fbedd546 1820 else if (names.line.matches (sectp->name))
c906108c 1821 {
330cdd98 1822 this->line.s.section = sectp;
fd361982 1823 this->line.size = bfd_section_size (sectp);
c906108c 1824 }
fbedd546 1825 else if (names.loc.matches (sectp->name))
c906108c 1826 {
330cdd98 1827 this->loc.s.section = sectp;
fd361982 1828 this->loc.size = bfd_section_size (sectp);
c906108c 1829 }
fbedd546 1830 else if (names.loclists.matches (sectp->name))
43988095 1831 {
330cdd98 1832 this->loclists.s.section = sectp;
fd361982 1833 this->loclists.size = bfd_section_size (sectp);
43988095 1834 }
fbedd546 1835 else if (names.macinfo.matches (sectp->name))
c906108c 1836 {
330cdd98 1837 this->macinfo.s.section = sectp;
fd361982 1838 this->macinfo.size = bfd_section_size (sectp);
c906108c 1839 }
fbedd546 1840 else if (names.macro.matches (sectp->name))
cf2c3c16 1841 {
330cdd98 1842 this->macro.s.section = sectp;
fd361982 1843 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1844 }
fbedd546 1845 else if (names.str.matches (sectp->name))
c906108c 1846 {
330cdd98 1847 this->str.s.section = sectp;
fd361982 1848 this->str.size = bfd_section_size (sectp);
c906108c 1849 }
fbedd546 1850 else if (names.str_offsets.matches (sectp->name))
18a8505e
AT
1851 {
1852 this->str_offsets.s.section = sectp;
1853 this->str_offsets.size = bfd_section_size (sectp);
1854 }
fbedd546 1855 else if (names.line_str.matches (sectp->name))
43988095 1856 {
330cdd98 1857 this->line_str.s.section = sectp;
fd361982 1858 this->line_str.size = bfd_section_size (sectp);
43988095 1859 }
fbedd546 1860 else if (names.addr.matches (sectp->name))
3019eac3 1861 {
330cdd98 1862 this->addr.s.section = sectp;
fd361982 1863 this->addr.size = bfd_section_size (sectp);
3019eac3 1864 }
fbedd546 1865 else if (names.frame.matches (sectp->name))
b6af0555 1866 {
330cdd98 1867 this->frame.s.section = sectp;
fd361982 1868 this->frame.size = bfd_section_size (sectp);
b6af0555 1869 }
fbedd546 1870 else if (names.eh_frame.matches (sectp->name))
b6af0555 1871 {
330cdd98 1872 this->eh_frame.s.section = sectp;
fd361982 1873 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1874 }
fbedd546 1875 else if (names.ranges.matches (sectp->name))
af34e669 1876 {
330cdd98 1877 this->ranges.s.section = sectp;
fd361982 1878 this->ranges.size = bfd_section_size (sectp);
af34e669 1879 }
fbedd546 1880 else if (names.rnglists.matches (sectp->name))
43988095 1881 {
330cdd98 1882 this->rnglists.s.section = sectp;
fd361982 1883 this->rnglists.size = bfd_section_size (sectp);
43988095 1884 }
fbedd546 1885 else if (names.types.matches (sectp->name))
348e048f 1886 {
8b70b953
TT
1887 struct dwarf2_section_info type_section;
1888
1889 memset (&type_section, 0, sizeof (type_section));
049412e3 1890 type_section.s.section = sectp;
fd361982 1891 type_section.size = bfd_section_size (sectp);
8b70b953 1892
fd5866f6 1893 this->types.push_back (type_section);
348e048f 1894 }
fbedd546 1895 else if (names.gdb_index.matches (sectp->name))
9291a0cd 1896 {
330cdd98 1897 this->gdb_index.s.section = sectp;
fd361982 1898 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1899 }
fbedd546 1900 else if (names.debug_names.matches (sectp->name))
927aa2e7
JK
1901 {
1902 this->debug_names.s.section = sectp;
fd361982 1903 this->debug_names.size = bfd_section_size (sectp);
927aa2e7 1904 }
fbedd546 1905 else if (names.debug_aranges.matches (sectp->name))
927aa2e7
JK
1906 {
1907 this->debug_aranges.s.section = sectp;
fd361982 1908 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 1909 }
dce234bc 1910
fd361982
AM
1911 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1912 && bfd_section_vma (sectp) == 0)
330cdd98 1913 this->has_section_at_zero = true;
c906108c
SS
1914}
1915
dce234bc 1916/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1917 SECTION_NAME. */
af34e669 1918
dce234bc 1919void
3017a003 1920dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
1921 enum dwarf2_section_enum sect,
1922 asection **sectp, const gdb_byte **bufp,
1923 bfd_size_type *sizep)
dce234bc 1924{
976ca316 1925 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 1926 struct dwarf2_section_info *info;
a3b2a86b
TT
1927
1928 /* We may see an objfile without any DWARF, in which case we just
1929 return nothing. */
976ca316 1930 if (per_objfile == NULL)
a3b2a86b
TT
1931 {
1932 *sectp = NULL;
1933 *bufp = NULL;
1934 *sizep = 0;
1935 return;
1936 }
3017a003
TG
1937 switch (sect)
1938 {
1939 case DWARF2_DEBUG_FRAME:
976ca316 1940 info = &per_objfile->per_bfd->frame;
3017a003
TG
1941 break;
1942 case DWARF2_EH_FRAME:
976ca316 1943 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
1944 break;
1945 default:
1946 gdb_assert_not_reached ("unexpected section");
1947 }
dce234bc 1948
96b79293 1949 info->read (objfile);
dce234bc 1950
96b79293 1951 *sectp = info->get_bfd_section ();
dce234bc
PP
1952 *bufp = info->buffer;
1953 *sizep = info->size;
1954}
1955
9291a0cd 1956\f
39298a5d 1957/* DWARF quick_symbol_functions support. */
7b9f3c50
DE
1958
1959/* TUs can share .debug_line entries, and there can be a lot more TUs than
1960 unique line tables, so we maintain a separate table of all .debug_line
1961 derived entries to support the sharing.
1962 All the quick functions need is the list of file names. We discard the
1963 line_header when we're done and don't need to record it here. */
1964struct quick_file_names
1965{
094b34ac
DE
1966 /* The data used to construct the hash key. */
1967 struct stmt_list_hash hash;
7b9f3c50
DE
1968
1969 /* The number of entries in file_names, real_names. */
1970 unsigned int num_file_names;
1971
1972 /* The file names from the line table, after being run through
1973 file_full_name. */
1974 const char **file_names;
1975
1976 /* The file names from the line table after being run through
1977 gdb_realpath. These are computed lazily. */
1978 const char **real_names;
1979};
1980
1981/* When using the index (and thus not using psymtabs), each CU has an
1982 object of this type. This is used to hold information needed by
1983 the various "quick" methods. */
1984struct dwarf2_per_cu_quick_data
1985{
1986 /* The file table. This can be NULL if there was no file table
1987 or it's currently not read in.
5989a64e 1988 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
1989 struct quick_file_names *file_names;
1990
7b9f3c50
DE
1991 /* A temporary mark bit used when iterating over all CUs in
1992 expand_symtabs_matching. */
1993 unsigned int mark : 1;
1994
1995 /* True if we've tried to read the file table and found there isn't one.
1996 There will be no point in trying to read it again next time. */
1997 unsigned int no_file_data : 1;
1998};
1999
edc02ceb
TT
2000/* A subclass of psymbol_functions that arranges to read the DWARF
2001 partial symbols when needed. */
2002struct lazy_dwarf_reader : public psymbol_functions
2003{
2004 using psymbol_functions::psymbol_functions;
2005
2006 bool can_lazily_read_symbols () override
2007 {
2008 return true;
2009 }
2010
2011 void read_partial_symbols (struct objfile *objfile) override
2012 {
2013 if (dwarf2_has_info (objfile, nullptr))
2014 dwarf2_build_psymtabs (objfile, this);
2015 }
2016};
2017
2018static quick_symbol_functions_up
2019make_lazy_dwarf_reader ()
2020{
2021 return quick_symbol_functions_up (new lazy_dwarf_reader);
2022}
2023
39298a5d
TT
2024struct dwarf2_base_index_functions : public quick_symbol_functions
2025{
2026 bool has_symbols (struct objfile *objfile) override;
2027
2028 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2029
2030 void forget_cached_source_info (struct objfile *objfile) override;
2031
39298a5d
TT
2032 enum language lookup_global_symbol_language (struct objfile *objfile,
2033 const char *name,
2034 domain_enum domain,
2035 bool *symbol_found_p) override
2036 {
2037 *symbol_found_p = false;
2038 return language_unknown;
2039 }
2040
4829711b 2041 void print_stats (struct objfile *objfile, bool print_bcache) override;
39298a5d
TT
2042
2043 void expand_all_symtabs (struct objfile *objfile) override;
2044
39298a5d
TT
2045 struct compunit_symtab *find_pc_sect_compunit_symtab
2046 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2047 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2048
2049 struct compunit_symtab *find_compunit_symtab_by_address
2050 (struct objfile *objfile, CORE_ADDR address) override
2051 {
2052 return nullptr;
2053 }
2054
2055 void map_symbol_filenames (struct objfile *objfile,
f4655dee
TT
2056 gdb::function_view<symbol_filename_ftype> fun,
2057 bool need_fullname) override;
39298a5d
TT
2058};
2059
2060struct dwarf2_gdb_index : public dwarf2_base_index_functions
2061{
39298a5d
TT
2062 void dump (struct objfile *objfile) override;
2063
0b7b2c2a 2064 void expand_matching_symbols
39298a5d
TT
2065 (struct objfile *,
2066 const lookup_name_info &lookup_name,
2067 domain_enum domain,
2068 int global,
39298a5d
TT
2069 symbol_compare_ftype *ordered_compare) override;
2070
df35e626 2071 bool expand_symtabs_matching
39298a5d
TT
2072 (struct objfile *objfile,
2073 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2074 const lookup_name_info *lookup_name,
2075 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2076 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2077 block_search_flags search_flags,
3bfa51a7 2078 domain_enum domain,
39298a5d
TT
2079 enum search_domain kind) override;
2080};
2081
2082struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2083{
39298a5d
TT
2084 void dump (struct objfile *objfile) override;
2085
0b7b2c2a 2086 void expand_matching_symbols
39298a5d
TT
2087 (struct objfile *,
2088 const lookup_name_info &lookup_name,
2089 domain_enum domain,
2090 int global,
39298a5d
TT
2091 symbol_compare_ftype *ordered_compare) override;
2092
df35e626 2093 bool expand_symtabs_matching
39298a5d
TT
2094 (struct objfile *objfile,
2095 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2096 const lookup_name_info *lookup_name,
2097 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2098 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2099 block_search_flags search_flags,
3bfa51a7 2100 domain_enum domain,
39298a5d
TT
2101 enum search_domain kind) override;
2102};
2103
edc02ceb 2104static quick_symbol_functions_up
39298a5d
TT
2105make_dwarf_gdb_index ()
2106{
2107 return quick_symbol_functions_up (new dwarf2_gdb_index);
2108}
2109
edc02ceb 2110static quick_symbol_functions_up
39298a5d
TT
2111make_dwarf_debug_names ()
2112{
2113 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2114}
2115
094b34ac
DE
2116/* Utility hash function for a stmt_list_hash. */
2117
2118static hashval_t
2119hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2120{
2121 hashval_t v = 0;
2122
2123 if (stmt_list_hash->dwo_unit != NULL)
2124 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2125 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2126 return v;
2127}
2128
2129/* Utility equality function for a stmt_list_hash. */
2130
2131static int
2132eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2133 const struct stmt_list_hash *rhs)
2134{
2135 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2136 return 0;
2137 if (lhs->dwo_unit != NULL
2138 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2139 return 0;
2140
9c541725 2141 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2142}
2143
7b9f3c50
DE
2144/* Hash function for a quick_file_names. */
2145
2146static hashval_t
2147hash_file_name_entry (const void *e)
2148{
9a3c8263
SM
2149 const struct quick_file_names *file_data
2150 = (const struct quick_file_names *) e;
7b9f3c50 2151
094b34ac 2152 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2153}
2154
2155/* Equality function for a quick_file_names. */
2156
2157static int
2158eq_file_name_entry (const void *a, const void *b)
2159{
9a3c8263
SM
2160 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2161 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2162
094b34ac 2163 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2164}
2165
2166/* Delete function for a quick_file_names. */
2167
2168static void
2169delete_file_name_entry (void *e)
2170{
9a3c8263 2171 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2172 int i;
2173
2174 for (i = 0; i < file_data->num_file_names; ++i)
2175 {
2176 xfree ((void*) file_data->file_names[i]);
2177 if (file_data->real_names)
2178 xfree ((void*) file_data->real_names[i]);
2179 }
2180
45940949
TT
2181 /* The space for the struct itself lives on the obstack, so we don't
2182 free it here. */
7b9f3c50
DE
2183}
2184
2185/* Create a quick_file_names hash table. */
2186
5895093f 2187static htab_up
7b9f3c50
DE
2188create_quick_file_names_table (unsigned int nr_initial_entries)
2189{
5895093f
TT
2190 return htab_up (htab_create_alloc (nr_initial_entries,
2191 hash_file_name_entry, eq_file_name_entry,
2192 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2193}
9291a0cd 2194
ab432490
SM
2195/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2196 function is unrelated to symtabs, symtab would have to be created afterwards.
2197 You should call age_cached_comp_units after processing the CU. */
918dd910 2198
1b555f17 2199static dwarf2_cu *
ab432490
SM
2200load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2201 bool skip_partial)
918dd910 2202{
3019eac3 2203 if (per_cu->is_debug_types)
ab432490 2204 load_full_type_unit (per_cu, per_objfile);
918dd910 2205 else
4a636814
SM
2206 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2207 skip_partial, language_minimal);
918dd910 2208
7188ed02
SM
2209 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2210 if (cu == nullptr)
1b555f17 2211 return nullptr; /* Dummy CU. */
2dc860c0 2212
7188ed02 2213 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2214
7188ed02 2215 return cu;
918dd910
JK
2216}
2217
1350c3b4 2218/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2219
9291a0cd 2220static void
97a1449a 2221dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2222 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2223{
f4dc4d17
DE
2224 /* Skip type_unit_groups, reading the type units they contain
2225 is handled elsewhere. */
197400e8 2226 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2227 return;
2228
08ac5771
SM
2229 {
2230 /* The destructor of dwarf2_queue_guard frees any entries left on
2231 the queue. After this point we're guaranteed to leave this function
2232 with the dwarf queue empty. */
2233 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2234
08ac5771
SM
2235 if (!per_objfile->symtab_set_p (per_cu))
2236 {
2237 queue_comp_unit (per_cu, per_objfile, language_minimal);
2238 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2239
2240 /* If we just loaded a CU from a DWO, and we're working with an index
2241 that may badly handle TUs, load all the TUs in that DWO as well.
2242 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2243 if (!per_cu->is_debug_types
2244 && cu != NULL
2245 && cu->dwo_unit != NULL
2246 && per_objfile->per_bfd->index_table != NULL
2247 && per_objfile->per_bfd->index_table->version <= 7
2248 /* DWP files aren't supported yet. */
2249 && get_dwp_file (per_objfile) == NULL)
2250 queue_and_load_all_dwo_tus (cu);
2251 }
9291a0cd 2252
08ac5771
SM
2253 process_queue (per_objfile);
2254 }
9291a0cd
TT
2255
2256 /* Age the cache, releasing compilation units that have not
2257 been used recently. */
976ca316 2258 per_objfile->age_comp_units ();
9291a0cd
TT
2259}
2260
97a1449a
SM
2261/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2262 the per-objfile for which this symtab is instantiated.
2263
2264 Returns the resulting symbol table. */
2fdf6df6 2265
43f3e411 2266static struct compunit_symtab *
97a1449a 2267dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2268 dwarf2_per_objfile *per_objfile,
97a1449a 2269 bool skip_partial)
9291a0cd 2270{
976ca316 2271 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2272
976ca316 2273 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2274 {
976ca316 2275 free_cached_comp_units freer (per_objfile);
c83dd867 2276 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2277 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2278 process_cu_includes (per_objfile);
9291a0cd 2279 }
f194fefb 2280
976ca316 2281 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2282}
2283
d3473f0c
TT
2284/* See read.h. */
2285
473ab964 2286dwarf2_per_cu_data_up
5989a64e 2287dwarf2_per_bfd::allocate_per_cu ()
d3473f0c 2288{
473ab964 2289 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
1859c670 2290 result->per_bfd = this;
c96e8b04 2291 result->index = all_comp_units.size ();
d3473f0c
TT
2292 return result;
2293}
2294
2295/* See read.h. */
2296
46c6bcf6 2297signatured_type_up
9ea36493 2298dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
d3473f0c 2299{
9ea36493 2300 signatured_type_up result (new signatured_type (signature));
0d305d5c 2301 result->per_bfd = this;
c96e8b04 2302 result->index = all_comp_units.size ();
cc653233 2303 result->is_debug_types = true;
91eea9cc 2304 tu_stats.nr_tus++;
d3473f0c
TT
2305 return result;
2306}
2307
168c9250 2308/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2309 obstack, and constructed with the specified field values. */
4b514bc8 2310
473ab964 2311static dwarf2_per_cu_data_up
168c9250
SM
2312create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2313 struct dwarf2_section_info *section,
2314 int is_dwz,
2315 sect_offset sect_off, ULONGEST length)
4b514bc8 2316{
473ab964 2317 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2318 the_cu->sect_off = sect_off;
2319 the_cu->length = length;
4b514bc8 2320 the_cu->section = section;
168c9250 2321 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2322 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2323 the_cu->is_dwz = is_dwz;
2324 return the_cu;
2325}
2326
2ec9a5e0
TT
2327/* A helper for create_cus_from_index that handles a given list of
2328 CUs. */
2fdf6df6 2329
74a0d9f6 2330static void
168c9250 2331create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2332 const gdb_byte *cu_list, offset_type n_elements,
2333 struct dwarf2_section_info *section,
b76e467d 2334 int is_dwz)
9291a0cd 2335{
12359b5e 2336 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2337 {
74a0d9f6 2338 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2339
2340 sect_offset sect_off
2341 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2342 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2343 cu_list += 2 * 8;
2344
473ab964 2345 dwarf2_per_cu_data_up per_cu
168c9250
SM
2346 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2347 length);
0d305d5c 2348 per_bfd->all_comp_units.push_back (std::move (per_cu));
9291a0cd 2349 }
9291a0cd
TT
2350}
2351
2ec9a5e0 2352/* Read the CU list from the mapped index, and use it to create all
168c9250 2353 the CU objects for PER_BFD. */
2ec9a5e0 2354
74a0d9f6 2355static void
168c9250 2356create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2357 const gdb_byte *cu_list, offset_type cu_list_elements,
2358 const gdb_byte *dwz_list, offset_type dwz_elements)
2359{
168c9250
SM
2360 gdb_assert (per_bfd->all_comp_units.empty ());
2361 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2362
168c9250
SM
2363 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2364 &per_bfd->info, 0);
2ec9a5e0
TT
2365
2366 if (dwz_elements == 0)
74a0d9f6 2367 return;
2ec9a5e0 2368
168c9250
SM
2369 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2370 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2371 &dwz->info, 1);
2ec9a5e0
TT
2372}
2373
1fd400ff 2374/* Create the signatured type hash table from the index. */
673bfd45 2375
74a0d9f6 2376static void
12359b5e 2377create_signatured_type_table_from_index
168c9250
SM
2378 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2379 const gdb_byte *bytes, offset_type elements)
1fd400ff 2380{
298e9637 2381 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2382
12359b5e 2383 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2384 {
46c6bcf6 2385 signatured_type_up sig_type;
9c541725 2386 ULONGEST signature;
1fd400ff 2387 void **slot;
9c541725 2388 cu_offset type_offset_in_tu;
1fd400ff 2389
74a0d9f6 2390 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2391 sect_offset sect_off
2392 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2393 type_offset_in_tu
2394 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2395 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2396 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2397 bytes += 3 * 8;
2398
9ea36493 2399 sig_type = per_bfd->allocate_signatured_type (signature);
9c541725 2400 sig_type->type_offset_in_tu = type_offset_in_tu;
0d305d5c
TT
2401 sig_type->section = section;
2402 sig_type->sect_off = sect_off;
2403 sig_type->v.quick
168c9250 2404 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2405 struct dwarf2_per_cu_quick_data);
2406
0d305d5c
TT
2407 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2408 *slot = sig_type.get ();
1fd400ff 2409
473ab964 2410 per_bfd->all_comp_units.emplace_back (sig_type.release ());
1fd400ff
TT
2411 }
2412
168c9250 2413 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2414}
2415
927aa2e7
JK
2416/* Create the signatured type hash table from .debug_names. */
2417
2418static void
2419create_signatured_type_table_from_debug_names
976ca316 2420 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2421 const mapped_debug_names &map,
2422 struct dwarf2_section_info *section,
2423 struct dwarf2_section_info *abbrev_section)
2424{
976ca316 2425 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2426
96b79293
TT
2427 section->read (objfile);
2428 abbrev_section->read (objfile);
927aa2e7 2429
298e9637 2430 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2431
2432 for (uint32_t i = 0; i < map.tu_count; ++i)
2433 {
46c6bcf6 2434 signatured_type_up sig_type;
927aa2e7 2435 void **slot;
927aa2e7
JK
2436
2437 sect_offset sect_off
2438 = (sect_offset) (extract_unsigned_integer
2439 (map.tu_table_reordered + i * map.offset_size,
2440 map.offset_size,
2441 map.dwarf5_byte_order));
2442
2443 comp_unit_head cu_header;
976ca316 2444 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2445 abbrev_section,
927aa2e7
JK
2446 section->buffer + to_underlying (sect_off),
2447 rcuh_kind::TYPE);
2448
9ea36493
SM
2449 sig_type = per_objfile->per_bfd->allocate_signatured_type
2450 (cu_header.signature);
927aa2e7 2451 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
0d305d5c
TT
2452 sig_type->section = section;
2453 sig_type->sect_off = sect_off;
2454 sig_type->v.quick
976ca316 2455 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2456 struct dwarf2_per_cu_quick_data);
2457
0d305d5c
TT
2458 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2459 *slot = sig_type.get ();
927aa2e7 2460
473ab964 2461 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
927aa2e7
JK
2462 }
2463
976ca316 2464 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2465}
2466
9291a0cd 2467/* Read the address map data from the mapped index, and use it to
efd7398e 2468 populate the psymtabs_addrmap. */
2fdf6df6 2469
9291a0cd 2470static void
976ca316 2471create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2472 struct mapped_index *index)
9291a0cd 2473{
976ca316 2474 struct objfile *objfile = per_objfile->objfile;
efd7398e 2475 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
08feed99 2476 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2477 const gdb_byte *iter, *end;
9291a0cd 2478 struct addrmap *mutable_map;
9291a0cd
TT
2479 CORE_ADDR baseaddr;
2480
8268c778
PA
2481 auto_obstack temp_obstack;
2482
9291a0cd
TT
2483 mutable_map = addrmap_create_mutable (&temp_obstack);
2484
f00a2de2
PA
2485 iter = index->address_table.data ();
2486 end = iter + index->address_table.size ();
9291a0cd 2487
b3b3bada 2488 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2489
2490 while (iter < end)
2491 {
2492 ULONGEST hi, lo, cu_index;
2493 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2494 iter += 8;
2495 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2496 iter += 8;
2497 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2498 iter += 4;
f652bce2 2499
24a55014 2500 if (lo > hi)
f652bce2 2501 {
b98664d3 2502 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2503 hex_string (lo), hex_string (hi));
24a55014 2504 continue;
f652bce2 2505 }
24a55014 2506
efd7398e 2507 if (cu_index >= per_bfd->all_comp_units.size ())
f652bce2 2508 {
b98664d3 2509 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2510 (unsigned) cu_index);
24a55014 2511 continue;
f652bce2 2512 }
24a55014 2513
79748972
TT
2514 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2515 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2516 addrmap_set_empty (mutable_map, lo, hi - 1,
efd7398e 2517 per_bfd->get_cu (cu_index));
9291a0cd
TT
2518 }
2519
efd7398e
TT
2520 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2521 &per_bfd->obstack);
9291a0cd
TT
2522}
2523
927aa2e7 2524/* Read the address map data from DWARF-5 .debug_aranges, and use it to
efd7398e 2525 populate the psymtabs_addrmap. */
927aa2e7
JK
2526
2527static void
976ca316 2528create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2529 struct dwarf2_section_info *section)
2530{
976ca316 2531 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2532 bfd *abfd = objfile->obfd;
08feed99 2533 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2534 const CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e 2535 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
2536
2537 auto_obstack temp_obstack;
2538 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2539
2540 std::unordered_map<sect_offset,
2541 dwarf2_per_cu_data *,
2542 gdb::hash_enum<sect_offset>>
2543 debug_info_offset_to_per_cu;
0d305d5c 2544 for (const auto &per_cu : per_bfd->all_comp_units)
927aa2e7 2545 {
927aa2e7 2546 const auto insertpair
0d305d5c
TT
2547 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2548 per_cu.get ());
927aa2e7
JK
2549 if (!insertpair.second)
2550 {
2551 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2552 "debug_info_offset %s, ignoring .debug_aranges."),
2553 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2554 return;
2555 }
2556 }
2557
96b79293 2558 section->read (objfile);
927aa2e7
JK
2559
2560 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2561
2562 const gdb_byte *addr = section->buffer;
2563
2564 while (addr < section->buffer + section->size)
2565 {
2566 const gdb_byte *const entry_addr = addr;
2567 unsigned int bytes_read;
2568
2569 const LONGEST entry_length = read_initial_length (abfd, addr,
2570 &bytes_read);
2571 addr += bytes_read;
2572
2573 const gdb_byte *const entry_end = addr + entry_length;
2574 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2575 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2576 if (addr + entry_length > section->buffer + section->size)
2577 {
47e3f474 2578 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2579 "length %s exceeds section length %s, "
927aa2e7 2580 "ignoring .debug_aranges."),
47e3f474
TV
2581 objfile_name (objfile),
2582 plongest (entry_addr - section->buffer),
927aa2e7
JK
2583 plongest (bytes_read + entry_length),
2584 pulongest (section->size));
2585 return;
2586 }
2587
2588 /* The version number. */
2589 const uint16_t version = read_2_bytes (abfd, addr);
2590 addr += 2;
2591 if (version != 2)
2592 {
47e3f474 2593 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2594 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2595 objfile_name (objfile),
2596 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2597 return;
2598 }
2599
2600 const uint64_t debug_info_offset
2601 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2602 addr += offset_size;
2603 const auto per_cu_it
2604 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2605 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2606 {
47e3f474 2607 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2608 "debug_info_offset %s does not exists, "
2609 "ignoring .debug_aranges."),
47e3f474
TV
2610 objfile_name (objfile),
2611 plongest (entry_addr - section->buffer),
927aa2e7
JK
2612 pulongest (debug_info_offset));
2613 return;
2614 }
2615 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2616
2617 const uint8_t address_size = *addr++;
2618 if (address_size < 1 || address_size > 8)
2619 {
47e3f474 2620 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2621 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2622 objfile_name (objfile),
2623 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2624 return;
2625 }
2626
2627 const uint8_t segment_selector_size = *addr++;
2628 if (segment_selector_size != 0)
2629 {
47e3f474 2630 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2631 "segment_selector_size %u is not supported, "
2632 "ignoring .debug_aranges."),
47e3f474
TV
2633 objfile_name (objfile),
2634 plongest (entry_addr - section->buffer),
927aa2e7
JK
2635 segment_selector_size);
2636 return;
2637 }
2638
2639 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2640 size. It is undocumented by the DWARF standard but GCC does
2641 use it. */
927aa2e7
JK
2642 for (size_t padding = ((-(addr - section->buffer))
2643 & (2 * address_size - 1));
dda83cd7 2644 padding > 0; padding--)
927aa2e7
JK
2645 if (*addr++ != 0)
2646 {
47e3f474 2647 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2648 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2649 objfile_name (objfile),
2650 plongest (entry_addr - section->buffer));
927aa2e7
JK
2651 return;
2652 }
2653
2654 for (;;)
2655 {
2656 if (addr + 2 * address_size > entry_end)
2657 {
47e3f474 2658 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2659 "address list is not properly terminated, "
2660 "ignoring .debug_aranges."),
47e3f474
TV
2661 objfile_name (objfile),
2662 plongest (entry_addr - section->buffer));
927aa2e7
JK
2663 return;
2664 }
2665 ULONGEST start = extract_unsigned_integer (addr, address_size,
2666 dwarf5_byte_order);
2667 addr += address_size;
2668 ULONGEST length = extract_unsigned_integer (addr, address_size,
2669 dwarf5_byte_order);
2670 addr += address_size;
2671 if (start == 0 && length == 0)
2672 break;
efd7398e 2673 if (start == 0 && !per_bfd->has_section_at_zero)
927aa2e7
JK
2674 {
2675 /* Symbol was eliminated due to a COMDAT group. */
2676 continue;
2677 }
2678 ULONGEST end = start + length;
79748972
TT
2679 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2680 - baseaddr);
2681 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2682 - baseaddr);
927aa2e7
JK
2683 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2684 }
2685 }
2686
efd7398e
TT
2687 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2688 &per_bfd->obstack);
927aa2e7
JK
2689}
2690
4485a1c1
SM
2691/* A helper function that reads the .gdb_index from BUFFER and fills
2692 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2693 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2694 ok to use deprecated sections.
2695
2696 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2697 out parameters that are filled in with information about the CU and
2698 TU lists in the section.
2699
4485a1c1 2700 Returns true if all went well, false otherwise. */
2fdf6df6 2701
d33bc52e 2702static bool
3810f182 2703read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2704 bool deprecated_ok,
2705 gdb::array_view<const gdb_byte> buffer,
2706 struct mapped_index *map,
2707 const gdb_byte **cu_list,
2708 offset_type *cu_list_elements,
2709 const gdb_byte **types_list,
2710 offset_type *types_list_elements)
2711{
2712 const gdb_byte *addr = &buffer[0];
42c2c694 2713 offset_view metadata (buffer);
82430852 2714
9291a0cd 2715 /* Version check. */
42c2c694 2716 offset_type version = metadata[0];
987d643c 2717 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2718 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2719 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2720 indices. */
831adc1f 2721 if (version < 4)
481860b3
GB
2722 {
2723 static int warning_printed = 0;
2724 if (!warning_printed)
2725 {
2726 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2727 filename);
481860b3
GB
2728 warning_printed = 1;
2729 }
2730 return 0;
2731 }
2732 /* Index version 4 uses a different hash function than index version
2733 5 and later.
2734
2735 Versions earlier than 6 did not emit psymbols for inlined
2736 functions. Using these files will cause GDB not to be able to
2737 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2738 indices unless the user has done
2739 "set use-deprecated-index-sections on". */
2ec9a5e0 2740 if (version < 6 && !deprecated_ok)
481860b3
GB
2741 {
2742 static int warning_printed = 0;
2743 if (!warning_printed)
2744 {
e615022a
DE
2745 warning (_("\
2746Skipping deprecated .gdb_index section in %s.\n\
2747Do \"set use-deprecated-index-sections on\" before the file is read\n\
2748to use the section anyway."),
2ec9a5e0 2749 filename);
481860b3
GB
2750 warning_printed = 1;
2751 }
2752 return 0;
2753 }
796a7ff8 2754 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2755 of the TU (for symbols coming from TUs),
2756 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2757 Plus gold-generated indices can have duplicate entries for global symbols,
2758 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2759 These are just performance bugs, and we can't distinguish gdb-generated
2760 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2761
481860b3 2762 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2763 longer backward compatible. */
796a7ff8 2764 if (version > 8)
594e8718 2765 return 0;
9291a0cd 2766
559a7a62 2767 map->version = version;
9291a0cd 2768
42c2c694
TT
2769 int i = 1;
2770 *cu_list = addr + metadata[i];
2771 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
1fd400ff
TT
2772 ++i;
2773
42c2c694
TT
2774 *types_list = addr + metadata[i];
2775 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
987d643c 2776 ++i;
1fd400ff 2777
42c2c694
TT
2778 const gdb_byte *address_table = addr + metadata[i];
2779 const gdb_byte *address_table_end = addr + metadata[i + 1];
f00a2de2
PA
2780 map->address_table
2781 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2782 ++i;
2783
42c2c694
TT
2784 const gdb_byte *symbol_table = addr + metadata[i];
2785 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
f00a2de2 2786 map->symbol_table
42c2c694
TT
2787 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2788 symbol_table_end));
9291a0cd 2789
f00a2de2 2790 ++i;
42c2c694 2791 map->constant_pool = buffer.slice (metadata[i]);
1fd400ff 2792
2ec9a5e0
TT
2793 return 1;
2794}
2795
4485a1c1
SM
2796/* Callback types for dwarf2_read_gdb_index. */
2797
2798typedef gdb::function_view
5989a64e 2799 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
2800 get_gdb_index_contents_ftype;
2801typedef gdb::function_view
2802 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2803 get_gdb_index_contents_dwz_ftype;
2804
927aa2e7 2805/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2806 elements of all the CUs and return 1. Otherwise, return 0. */
2807
2808static int
4485a1c1 2809dwarf2_read_gdb_index
976ca316 2810 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
2811 get_gdb_index_contents_ftype get_gdb_index_contents,
2812 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 2813{
2ec9a5e0
TT
2814 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2815 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2816 struct dwz_file *dwz;
976ca316 2817 struct objfile *objfile = per_objfile->objfile;
f8c41851 2818 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 2819
4485a1c1 2820 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 2821 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
2822
2823 if (main_index_contents.empty ())
2824 return 0;
2825
3063847f 2826 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 2827 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
2828 use_deprecated_index_sections,
2829 main_index_contents, map.get (), &cu_list,
2830 &cu_list_elements, &types_list,
2831 &types_list_elements))
2ec9a5e0
TT
2832 return 0;
2833
0fefef59 2834 /* Don't use the index if it's empty. */
3063847f 2835 if (map->symbol_table.empty ())
0fefef59
DE
2836 return 0;
2837
2ec9a5e0
TT
2838 /* If there is a .dwz file, read it so we can get its CU list as
2839 well. */
f8c41851 2840 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 2841 if (dwz != NULL)
2ec9a5e0 2842 {
2ec9a5e0
TT
2843 struct mapped_index dwz_map;
2844 const gdb_byte *dwz_types_ignore;
2845 offset_type dwz_types_elements_ignore;
2846
4485a1c1
SM
2847 gdb::array_view<const gdb_byte> dwz_index_content
2848 = get_gdb_index_contents_dwz (objfile, dwz);
2849
2850 if (dwz_index_content.empty ())
2851 return 0;
2852
3810f182 2853 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 2854 1, dwz_index_content, &dwz_map,
4485a1c1
SM
2855 &dwz_list, &dwz_list_elements,
2856 &dwz_types_ignore,
2857 &dwz_types_elements_ignore))
2ec9a5e0
TT
2858 {
2859 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 2860 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
2861 return 0;
2862 }
2863 }
2864
f8c41851
SM
2865 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2866 dwz_list_elements);
1fd400ff 2867
8b70b953
TT
2868 if (types_list_elements)
2869 {
8b70b953
TT
2870 /* We can only handle a single .debug_types when we have an
2871 index. */
f8c41851 2872 if (per_bfd->types.size () != 1)
8b70b953
TT
2873 return 0;
2874
f8c41851 2875 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 2876
f8c41851 2877 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 2878 types_list_elements);
8b70b953 2879 }
9291a0cd 2880
976ca316 2881 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 2882
f8c41851
SM
2883 per_bfd->index_table = std::move (map);
2884 per_bfd->using_index = 1;
2885 per_bfd->quick_file_names_table =
2886 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2887
9291a0cd
TT
2888 return 1;
2889}
2890
dee91e82 2891/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2892
dee91e82
DE
2893static void
2894dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 2895 struct die_info *comp_unit_die)
9291a0cd 2896{
dee91e82 2897 struct dwarf2_cu *cu = reader->cu;
ed2dc618 2898 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 2899 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 2900 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 2901 struct attribute *attr;
7b9f3c50
DE
2902 void **slot;
2903 struct quick_file_names *qfn;
9291a0cd 2904
0186c6a7
DE
2905 gdb_assert (! this_cu->is_debug_types);
2906
07261596
TT
2907 /* Our callers never want to match partial units -- instead they
2908 will match the enclosing full CU. */
2909 if (comp_unit_die->tag == DW_TAG_partial_unit)
2910 {
2911 this_cu->v.quick->no_file_data = 1;
2912 return;
2913 }
2914
0186c6a7 2915 lh_cu = this_cu;
7b9f3c50 2916 slot = NULL;
dee91e82 2917
fff8551c 2918 line_header_up lh;
9c541725 2919 sect_offset line_offset {};
fff8551c 2920
dee91e82 2921 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 2922 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 2923 {
7b9f3c50
DE
2924 struct quick_file_names find_entry;
2925
d4df075e 2926 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
2927
2928 /* We may have already read in this line header (TU line header sharing).
2929 If we have we're done. */
094b34ac 2930 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 2931 find_entry.hash.line_sect_off = line_offset;
976ca316 2932 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
2933 &find_entry, INSERT);
2934 if (*slot != NULL)
2935 {
9a3c8263 2936 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 2937 return;
7b9f3c50
DE
2938 }
2939
3019eac3 2940 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2941 }
2942 if (lh == NULL)
2943 {
094b34ac 2944 lh_cu->v.quick->no_file_data = 1;
dee91e82 2945 return;
9291a0cd
TT
2946 }
2947
976ca316 2948 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 2949 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 2950 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
2951 gdb_assert (slot != NULL);
2952 *slot = qfn;
9291a0cd 2953
d721ba37 2954 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 2955
aa391654
TT
2956 int offset = 0;
2957 if (strcmp (fnd.name, "<unknown>") != 0)
2958 ++offset;
2959
7ba99d21 2960 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 2961 qfn->file_names =
976ca316 2962 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 2963 qfn->num_file_names);
aa391654
TT
2964 if (offset != 0)
2965 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 2966 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
2967 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
2968 fnd.comp_dir).release ();
7b9f3c50 2969 qfn->real_names = NULL;
9291a0cd 2970
094b34ac 2971 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2972}
2973
2974/* A helper for the "quick" functions which attempts to read the line
2975 table for THIS_CU. */
2976
2977static struct quick_file_names *
ab432490
SM
2978dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2979 dwarf2_per_objfile *per_objfile)
dee91e82 2980{
0186c6a7
DE
2981 /* This should never be called for TUs. */
2982 gdb_assert (! this_cu->is_debug_types);
2983 /* Nor type unit groups. */
197400e8 2984 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 2985
dee91e82
DE
2986 if (this_cu->v.quick->file_names != NULL)
2987 return this_cu->v.quick->file_names;
2988 /* If we know there is no line data, no point in looking again. */
2989 if (this_cu->v.quick->no_file_data)
2990 return NULL;
2991
ab432490 2992 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 2993 if (!reader.dummy_p)
eb5dd737 2994 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
2995
2996 if (this_cu->v.quick->no_file_data)
2997 return NULL;
2998 return this_cu->v.quick->file_names;
9291a0cd
TT
2999}
3000
3001/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3002 real path for a given file name from the line table. */
2fdf6df6 3003
9291a0cd 3004static const char *
976ca316 3005dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3006 struct quick_file_names *qfn, int index)
9291a0cd 3007{
7b9f3c50 3008 if (qfn->real_names == NULL)
976ca316 3009 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3010 qfn->num_file_names, const char *);
9291a0cd 3011
7b9f3c50 3012 if (qfn->real_names[index] == NULL)
14278e1f 3013 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3014
7b9f3c50 3015 return qfn->real_names[index];
9291a0cd
TT
3016}
3017
39298a5d
TT
3018struct symtab *
3019dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
9291a0cd 3020{
976ca316 3021 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
0d305d5c
TT
3022 dwarf2_per_cu_data *dwarf_cu
3023 = per_objfile->per_bfd->all_comp_units.back ().get ();
976ca316 3024 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3025
43f3e411
DE
3026 if (cust == NULL)
3027 return NULL;
ed2dc618 3028
43f3e411 3029 return compunit_primary_filetab (cust);
9291a0cd
TT
3030}
3031
7b9f3c50
DE
3032/* Traversal function for dw2_forget_cached_source_info. */
3033
3034static int
3035dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3036{
7b9f3c50 3037 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3038
7b9f3c50 3039 if (file_data->real_names)
9291a0cd 3040 {
7b9f3c50 3041 int i;
9291a0cd 3042
7b9f3c50 3043 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3044 {
7b9f3c50
DE
3045 xfree ((void*) file_data->real_names[i]);
3046 file_data->real_names[i] = NULL;
9291a0cd
TT
3047 }
3048 }
7b9f3c50
DE
3049
3050 return 1;
3051}
3052
39298a5d
TT
3053void
3054dwarf2_base_index_functions::forget_cached_source_info
3055 (struct objfile *objfile)
7b9f3c50 3056{
976ca316 3057 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3058
976ca316 3059 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3060 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3061}
3062
da51c347
DE
3063/* Struct used to manage iterating over all CUs looking for a symbol. */
3064
3065struct dw2_symtab_iterator
9291a0cd 3066{
ed2dc618 3067 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3068 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3069 /* If set, only look for symbols that match that block. Valid values are
3070 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3071 gdb::optional<block_enum> block_index;
da51c347
DE
3072 /* The kind of symbol we're looking for. */
3073 domain_enum domain;
3074 /* The list of CUs from the index entry of the symbol,
3075 or NULL if not found. */
42c2c694 3076 offset_view vec;
da51c347
DE
3077 /* The next element in VEC to look at. */
3078 int next;
3079 /* The number of elements in VEC, or zero if there is no match. */
3080 int length;
8943b874
DE
3081 /* Have we seen a global version of the symbol?
3082 If so we can ignore all further global instances.
3083 This is to work around gold/15646, inefficient gold-generated
3084 indices. */
3085 int global_seen;
da51c347 3086};
9291a0cd 3087
7089bd88 3088/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2fdf6df6 3089
9291a0cd 3090static void
7089bd88
TT
3091dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3092 dwarf2_per_objfile *per_objfile,
3093 gdb::optional<block_enum> block_index,
3094 domain_enum domain, offset_type namei)
da51c347 3095{
976ca316 3096 iter->per_objfile = per_objfile;
da51c347
DE
3097 iter->block_index = block_index;
3098 iter->domain = domain;
3099 iter->next = 0;
8943b874 3100 iter->global_seen = 0;
42c2c694 3101 iter->vec = {};
e5f3ece2 3102 iter->length = 0;
e5f3ece2
TV
3103
3104 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3105 /* index is NULL if OBJF_READNOW. */
3106 if (index == NULL)
3107 return;
3108
3109 gdb_assert (!index->symbol_name_slot_invalid (namei));
42c2c694 3110 offset_type vec_idx = index->symbol_vec_index (namei);
e5f3ece2 3111
42c2c694
TT
3112 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3113 iter->length = iter->vec[0];
da51c347
DE
3114}
3115
3116/* Return the next matching CU or NULL if there are no more. */
3117
3118static struct dwarf2_per_cu_data *
3119dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3120{
976ca316 3121 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3122
da51c347
DE
3123 for ( ; iter->next < iter->length; ++iter->next)
3124 {
42c2c694 3125 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
da51c347 3126 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3127 gdb_index_symbol_kind symbol_kind =
3128 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3129 /* Only check the symbol attributes if they're present.
3130 Indices prior to version 7 don't record them,
3131 and indices >= 7 may elide them for certain symbols
3132 (gold does this). */
3133 int attrs_valid =
976ca316 3134 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3135 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3136
3190f0c6 3137 /* Don't crash on bad data. */
91eea9cc 3138 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3190f0c6 3139 {
b98664d3 3140 complaint (_(".gdb_index entry has bad CU index"
976ca316 3141 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3142 continue;
3143 }
3144
91eea9cc 3145 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3190f0c6 3146
da51c347 3147 /* Skip if already read in. */
976ca316 3148 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3149 continue;
3150
8943b874
DE
3151 /* Check static vs global. */
3152 if (attrs_valid)
3153 {
2b79f376
SM
3154 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3155
3156 if (iter->block_index.has_value ())
3157 {
3158 bool want_static = *iter->block_index == STATIC_BLOCK;
3159
3160 if (is_static != want_static)
3161 continue;
3162 }
3163
8943b874 3164 /* Work around gold/15646. */
f030440d
TV
3165 if (!is_static
3166 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3167 {
3168 if (iter->global_seen)
3169 continue;
3170
3171 iter->global_seen = 1;
3172 }
8943b874 3173 }
da51c347
DE
3174
3175 /* Only check the symbol's kind if it has one. */
3176 if (attrs_valid)
3177 {
3178 switch (iter->domain)
3179 {
3180 case VAR_DOMAIN:
3181 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3182 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3183 /* Some types are also in VAR_DOMAIN. */
3184 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3185 continue;
3186 break;
3187 case STRUCT_DOMAIN:
3188 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3189 continue;
3190 break;
3191 case LABEL_DOMAIN:
3192 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3193 continue;
3194 break;
59c35742
AB
3195 case MODULE_DOMAIN:
3196 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3197 continue;
3198 break;
da51c347
DE
3199 default:
3200 break;
3201 }
3202 }
3203
3204 ++iter->next;
3205 return per_cu;
3206 }
3207
3208 return NULL;
3209}
3210
39298a5d 3211void
4829711b
TT
3212dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3213 bool print_bcache)
9291a0cd 3214{
4829711b
TT
3215 if (print_bcache)
3216 return;
3217
976ca316 3218 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
91eea9cc 3219 int total = per_objfile->per_bfd->all_comp_units.size ();
ed2dc618 3220 int count = 0;
9291a0cd 3221
ed2dc618 3222 for (int i = 0; i < total; ++i)
9291a0cd 3223 {
91eea9cc 3224 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
9291a0cd 3225
976ca316 3226 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3227 ++count;
3228 }
e4a48d9d 3229 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3230 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3231}
3232
779bd270
DE
3233/* This dumps minimal information about the index.
3234 It is called via "mt print objfiles".
3235 One use is to verify .gdb_index has been loaded by the
3236 gdb.dwarf2/gdb-index.exp testcase. */
3237
39298a5d
TT
3238void
3239dwarf2_gdb_index::dump (struct objfile *objfile)
9291a0cd 3240{
976ca316 3241 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3242
976ca316 3243 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3244 printf_filtered (".gdb_index:");
976ca316 3245 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3246 {
3247 printf_filtered (" version %d\n",
976ca316 3248 per_objfile->per_bfd->index_table->version);
779bd270
DE
3249 }
3250 else
3251 printf_filtered (" faked for \"readnow\"\n");
3252 printf_filtered ("\n");
9291a0cd
TT
3253}
3254
39298a5d
TT
3255void
3256dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
9291a0cd 3257{
976ca316 3258 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
91eea9cc 3259 int total_units = per_objfile->per_bfd->all_comp_units.size ();
9291a0cd 3260
ed2dc618 3261 for (int i = 0; i < total_units; ++i)
9291a0cd 3262 {
91eea9cc 3263 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
9291a0cd 3264
58f0c718
TT
3265 /* We don't want to directly expand a partial CU, because if we
3266 read it with the wrong language, then assertion failures can
3267 be triggered later on. See PR symtab/23010. So, tell
3268 dw2_instantiate_symtab to skip partial CUs -- any important
3269 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3270 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3271 }
3272}
3273
df35e626 3274static bool
9a0bacfb
TV
3275dw2_expand_symtabs_matching_symbol
3276 (mapped_index_base &index,
3277 const lookup_name_info &lookup_name_in,
3278 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3279 gdb::function_view<bool (offset_type)> match_callback,
3280 dwarf2_per_objfile *per_objfile);
9a0bacfb 3281
df35e626 3282static bool
9a0bacfb 3283dw2_expand_symtabs_matching_one
97a1449a
SM
3284 (dwarf2_per_cu_data *per_cu,
3285 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3286 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3287 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3288
af82f89d 3289void
0b7b2c2a 3290dwarf2_gdb_index::expand_matching_symbols
199b4314 3291 (struct objfile *objfile,
b054970d 3292 const lookup_name_info &name, domain_enum domain,
199b4314 3293 int global,
199b4314 3294 symbol_compare_ftype *ordered_compare)
9291a0cd 3295{
1aa98955 3296 /* Used for Ada. */
976ca316 3297 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3298
9a0bacfb
TV
3299 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3300
976ca316 3301 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3302 {
976ca316 3303 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3304
9a0bacfb
TV
3305 const char *match_name = name.ada ().lookup_name ().c_str ();
3306 auto matcher = [&] (const char *symname)
3307 {
3308 if (ordered_compare == nullptr)
3309 return true;
3310 return ordered_compare (symname, match_name) == 0;
3311 };
3312
b64f703b 3313 dw2_expand_symtabs_matching_symbol (index, name, matcher,
9a0bacfb
TV
3314 [&] (offset_type namei)
3315 {
3316 struct dw2_symtab_iterator iter;
3317 struct dwarf2_per_cu_data *per_cu;
3318
976ca316 3319 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3320 namei);
9a0bacfb 3321 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3322 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3323 nullptr);
9a0bacfb 3324 return true;
976ca316 3325 }, per_objfile);
9a0bacfb
TV
3326 }
3327 else
3328 {
3329 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3330 proceed assuming all symtabs have been read in. */
3331 }
9291a0cd
TT
3332}
3333
e1ef7d7a
PA
3334/* Starting from a search name, return the string that finds the upper
3335 bound of all strings that start with SEARCH_NAME in a sorted name
3336 list. Returns the empty string to indicate that the upper bound is
3337 the end of the list. */
3338
3339static std::string
3340make_sort_after_prefix_name (const char *search_name)
3341{
3342 /* When looking to complete "func", we find the upper bound of all
3343 symbols that start with "func" by looking for where we'd insert
3344 the closest string that would follow "func" in lexicographical
3345 order. Usually, that's "func"-with-last-character-incremented,
3346 i.e. "fund". Mind non-ASCII characters, though. Usually those
3347 will be UTF-8 multi-byte sequences, but we can't be certain.
3348 Especially mind the 0xff character, which is a valid character in
3349 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3350 rule out compilers allowing it in identifiers. Note that
3351 conveniently, strcmp/strcasecmp are specified to compare
3352 characters interpreted as unsigned char. So what we do is treat
3353 the whole string as a base 256 number composed of a sequence of
3354 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3355 to 0, and carries 1 to the following more-significant position.
3356 If the very first character in SEARCH_NAME ends up incremented
3357 and carries/overflows, then the upper bound is the end of the
3358 list. The string after the empty string is also the empty
3359 string.
3360
3361 Some examples of this operation:
3362
3363 SEARCH_NAME => "+1" RESULT
3364
3365 "abc" => "abd"
3366 "ab\xff" => "ac"
3367 "\xff" "a" "\xff" => "\xff" "b"
3368 "\xff" => ""
3369 "\xff\xff" => ""
3370 "" => ""
3371
3372 Then, with these symbols for example:
3373
3374 func
3375 func1
3376 fund
3377
3378 completing "func" looks for symbols between "func" and
3379 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3380 which finds "func" and "func1", but not "fund".
3381
3382 And with:
3383
3384 funcÿ (Latin1 'ÿ' [0xff])
3385 funcÿ1
3386 fund
3387
3388 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3389 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3390
3391 And with:
3392
3393 ÿÿ (Latin1 'ÿ' [0xff])
3394 ÿÿ1
3395
3396 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3397 the end of the list.
3398 */
3399 std::string after = search_name;
3400 while (!after.empty () && (unsigned char) after.back () == 0xff)
3401 after.pop_back ();
3402 if (!after.empty ())
3403 after.back () = (unsigned char) after.back () + 1;
3404 return after;
3405}
3406
5c58de74 3407/* See declaration. */
61d96d7e 3408
5c58de74
PA
3409std::pair<std::vector<name_component>::const_iterator,
3410 std::vector<name_component>::const_iterator>
44ed8f3e 3411mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3412 (const lookup_name_info &lookup_name_without_params, language lang,
3413 dwarf2_per_objfile *per_objfile) const
3f563c84 3414{
5c58de74
PA
3415 auto *name_cmp
3416 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3417
3b00ef10 3418 const char *lang_name
e0802d59 3419 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3420
3f563c84
PA
3421 /* Comparison function object for lower_bound that matches against a
3422 given symbol name. */
3423 auto lookup_compare_lower = [&] (const name_component &elem,
3424 const char *name)
3425 {
fcf23d5b 3426 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3427 const char *elem_name = elem_qualified + elem.name_offset;
3428 return name_cmp (elem_name, name) < 0;
3429 };
3430
3431 /* Comparison function object for upper_bound that matches against a
3432 given symbol name. */
3433 auto lookup_compare_upper = [&] (const char *name,
3434 const name_component &elem)
3435 {
fcf23d5b 3436 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3437 const char *elem_name = elem_qualified + elem.name_offset;
3438 return name_cmp (name, elem_name) < 0;
3439 };
3440
5c58de74
PA
3441 auto begin = this->name_components.begin ();
3442 auto end = this->name_components.end ();
3f563c84
PA
3443
3444 /* Find the lower bound. */
3445 auto lower = [&] ()
3446 {
3b00ef10 3447 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3448 return begin;
3449 else
3b00ef10 3450 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3451 } ();
3452
3453 /* Find the upper bound. */
3454 auto upper = [&] ()
3455 {
5c58de74 3456 if (lookup_name_without_params.completion_mode ())
3f563c84 3457 {
e1ef7d7a
PA
3458 /* In completion mode, we want UPPER to point past all
3459 symbols names that have the same prefix. I.e., with
3460 these symbols, and completing "func":
3461
3462 function << lower bound
3463 function1
3464 other_function << upper bound
3465
3466 We find the upper bound by looking for the insertion
3467 point of "func"-with-last-character-incremented,
3468 i.e. "fund". */
3b00ef10 3469 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3470 if (after.empty ())
3f563c84 3471 return end;
e6b2f5ef
PA
3472 return std::lower_bound (lower, end, after.c_str (),
3473 lookup_compare_lower);
3f563c84
PA
3474 }
3475 else
3b00ef10 3476 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3477 } ();
3478
5c58de74
PA
3479 return {lower, upper};
3480}
3481
3482/* See declaration. */
3483
3484void
fcf23d5b 3485mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3486{
3487 if (!this->name_components.empty ())
3488 return;
3489
3490 this->name_components_casing = case_sensitivity;
3491 auto *name_cmp
3492 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3493
3494 /* The code below only knows how to break apart components of C++
3495 symbol names (and other languages that use '::' as
3b00ef10 3496 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3497 auto count = this->symbol_name_count ();
3498 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3499 {
44ed8f3e 3500 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3501 continue;
3502
fcf23d5b 3503 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3504
3505 /* Add each name component to the name component table. */
3506 unsigned int previous_len = 0;
3b00ef10
TT
3507
3508 if (strstr (name, "::") != nullptr)
3509 {
3510 for (unsigned int current_len = cp_find_first_component (name);
3511 name[current_len] != '\0';
3512 current_len += cp_find_first_component (name + current_len))
3513 {
3514 gdb_assert (name[current_len] == ':');
3515 this->name_components.push_back ({previous_len, idx});
3516 /* Skip the '::'. */
3517 current_len += 2;
3518 previous_len = current_len;
3519 }
3520 }
3521 else
5c58de74 3522 {
3b00ef10
TT
3523 /* Handle the Ada encoded (aka mangled) form here. */
3524 for (const char *iter = strstr (name, "__");
3525 iter != nullptr;
3526 iter = strstr (iter, "__"))
3527 {
3528 this->name_components.push_back ({previous_len, idx});
3529 iter += 2;
3530 previous_len = iter - name;
3531 }
5c58de74 3532 }
3b00ef10 3533
5c58de74
PA
3534 this->name_components.push_back ({previous_len, idx});
3535 }
3536
3537 /* Sort name_components elements by name. */
3538 auto name_comp_compare = [&] (const name_component &left,
3539 const name_component &right)
3540 {
fcf23d5b
SM
3541 const char *left_qualified
3542 = this->symbol_name_at (left.idx, per_objfile);
3543 const char *right_qualified
3544 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
3545
3546 const char *left_name = left_qualified + left.name_offset;
3547 const char *right_name = right_qualified + right.name_offset;
3548
3549 return name_cmp (left_name, right_name) < 0;
3550 };
3551
3552 std::sort (this->name_components.begin (),
3553 this->name_components.end (),
3554 name_comp_compare);
3555}
3556
3557/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3558 mapped_index_base instead of the containing objfile. This is split
3559 to a separate function in order to be able to unit test the
3560 name_components matching using a mock mapped_index_base. For each
5c58de74 3561 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3562 symbol's index in the mapped_index_base symbol table. */
5c58de74 3563
df35e626 3564static bool
5c58de74 3565dw2_expand_symtabs_matching_symbol
44ed8f3e 3566 (mapped_index_base &index,
5c58de74
PA
3567 const lookup_name_info &lookup_name_in,
3568 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3569 gdb::function_view<bool (offset_type)> match_callback,
3570 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3571{
3572 lookup_name_info lookup_name_without_params
3573 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3574
3575 /* Build the symbol name component sorted vector, if we haven't
3576 yet. */
fcf23d5b 3577 index.build_name_components (per_objfile);
5c58de74 3578
3f563c84
PA
3579 /* The same symbol may appear more than once in the range though.
3580 E.g., if we're looking for symbols that complete "w", and we have
3581 a symbol named "w1::w2", we'll find the two name components for
3582 that same symbol in the range. To be sure we only call the
3583 callback once per symbol, we first collect the symbol name
3584 indexes that matched in a temporary vector and ignore
3585 duplicates. */
3586 std::vector<offset_type> matches;
3f563c84 3587
3b00ef10
TT
3588 struct name_and_matcher
3589 {
3590 symbol_name_matcher_ftype *matcher;
ecc6c606 3591 const char *name;
3b00ef10
TT
3592
3593 bool operator== (const name_and_matcher &other) const
3f563c84 3594 {
ecc6c606 3595 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3596 }
3597 };
3598
3599 /* A vector holding all the different symbol name matchers, for all
3600 languages. */
3601 std::vector<name_and_matcher> matchers;
3602
3603 for (int i = 0; i < nr_languages; i++)
3604 {
3605 enum language lang_e = (enum language) i;
3606
3607 const language_defn *lang = language_def (lang_e);
3608 symbol_name_matcher_ftype *name_matcher
c9debfb9 3609 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 3610
3b00ef10 3611 name_and_matcher key {
dda83cd7 3612 name_matcher,
3b00ef10
TT
3613 lookup_name_without_params.language_lookup_name (lang_e)
3614 };
3615
3616 /* Don't insert the same comparison routine more than once.
3617 Note that we do this linear walk. This is not a problem in
3618 practice because the number of supported languages is
3619 low. */
3620 if (std::find (matchers.begin (), matchers.end (), key)
3621 != matchers.end ())
9291a0cd 3622 continue;
3b00ef10
TT
3623 matchers.push_back (std::move (key));
3624
3625 auto bounds
3626 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 3627 lang_e, per_objfile);
3b00ef10
TT
3628
3629 /* Now for each symbol name in range, check to see if we have a name
3630 match, and if so, call the MATCH_CALLBACK callback. */
3631
3632 for (; bounds.first != bounds.second; ++bounds.first)
3633 {
fcf23d5b
SM
3634 const char *qualified
3635 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
3636
3637 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3638 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3639 continue;
9291a0cd 3640
3b00ef10
TT
3641 matches.push_back (bounds.first->idx);
3642 }
3f563c84
PA
3643 }
3644
3645 std::sort (matches.begin (), matches.end ());
3646
3647 /* Finally call the callback, once per match. */
3648 ULONGEST prev = -1;
df35e626 3649 bool result = true;
3f563c84
PA
3650 for (offset_type idx : matches)
3651 {
3652 if (prev != idx)
3653 {
3b00ef10 3654 if (!match_callback (idx))
df35e626
TT
3655 {
3656 result = false;
3657 break;
3658 }
3f563c84
PA
3659 prev = idx;
3660 }
3661 }
3662
3663 /* Above we use a type wider than idx's for 'prev', since 0 and
3664 (offset_type)-1 are both possible values. */
3665 static_assert (sizeof (prev) > sizeof (offset_type), "");
df35e626
TT
3666
3667 return result;
3f563c84
PA
3668}
3669
c62446b1
PA
3670#if GDB_SELF_TEST
3671
3672namespace selftests { namespace dw2_expand_symtabs_matching {
3673
a3c5fafd
PA
3674/* A mock .gdb_index/.debug_names-like name index table, enough to
3675 exercise dw2_expand_symtabs_matching_symbol, which works with the
3676 mapped_index_base interface. Builds an index from the symbol list
3677 passed as parameter to the constructor. */
3678class mock_mapped_index : public mapped_index_base
c62446b1
PA
3679{
3680public:
a3c5fafd
PA
3681 mock_mapped_index (gdb::array_view<const char *> symbols)
3682 : m_symbol_table (symbols)
c62446b1
PA
3683 {}
3684
a3c5fafd 3685 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 3686
a3c5fafd 3687 /* Return the number of names in the symbol table. */
632e107b 3688 size_t symbol_name_count () const override
c62446b1 3689 {
a3c5fafd 3690 return m_symbol_table.size ();
c62446b1
PA
3691 }
3692
a3c5fafd 3693 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
3694 const char *symbol_name_at
3695 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
3696 {
3697 return m_symbol_table[idx];
3698 }
c62446b1 3699
a3c5fafd
PA
3700private:
3701 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
3702};
3703
3704/* Convenience function that converts a NULL pointer to a "<null>"
3705 string, to pass to print routines. */
3706
3707static const char *
3708string_or_null (const char *str)
3709{
3710 return str != NULL ? str : "<null>";
3711}
3712
3713/* Check if a lookup_name_info built from
3714 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3715 index. EXPECTED_LIST is the list of expected matches, in expected
3716 matching order. If no match expected, then an empty list is
3717 specified. Returns true on success. On failure prints a warning
3718 indicating the file:line that failed, and returns false. */
3719
3720static bool
3721check_match (const char *file, int line,
3722 mock_mapped_index &mock_index,
3723 const char *name, symbol_name_match_type match_type,
3724 bool completion_mode,
fcf23d5b
SM
3725 std::initializer_list<const char *> expected_list,
3726 dwarf2_per_objfile *per_objfile)
c62446b1
PA
3727{
3728 lookup_name_info lookup_name (name, match_type, completion_mode);
3729
3730 bool matched = true;
3731
3732 auto mismatch = [&] (const char *expected_str,
3733 const char *got)
3734 {
3735 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3736 "expected=\"%s\", got=\"%s\"\n"),
3737 file, line,
3738 (match_type == symbol_name_match_type::FULL
3739 ? "FULL" : "WILD"),
3740 name, string_or_null (expected_str), string_or_null (got));
3741 matched = false;
3742 };
3743
3744 auto expected_it = expected_list.begin ();
3745 auto expected_end = expected_list.end ();
3746
a3c5fafd 3747 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
b64f703b 3748 nullptr,
c62446b1
PA
3749 [&] (offset_type idx)
3750 {
fcf23d5b 3751 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
3752 const char *expected_str
3753 = expected_it == expected_end ? NULL : *expected_it++;
3754
3755 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3756 mismatch (expected_str, matched_name);
3b00ef10 3757 return true;
fcf23d5b 3758 }, per_objfile);
c62446b1
PA
3759
3760 const char *expected_str
3761 = expected_it == expected_end ? NULL : *expected_it++;
3762 if (expected_str != NULL)
3763 mismatch (expected_str, NULL);
3764
3765 return matched;
3766}
3767
3768/* The symbols added to the mock mapped_index for testing (in
3769 canonical form). */
3770static const char *test_symbols[] = {
3771 "function",
3772 "std::bar",
3773 "std::zfunction",
3774 "std::zfunction2",
3775 "w1::w2",
3776 "ns::foo<char*>",
3777 "ns::foo<int>",
3778 "ns::foo<long>",
a20714ff
PA
3779 "ns2::tmpl<int>::foo2",
3780 "(anonymous namespace)::A::B::C",
c62446b1 3781
e1ef7d7a
PA
3782 /* These are used to check that the increment-last-char in the
3783 matching algorithm for completion doesn't match "t1_fund" when
3784 completing "t1_func". */
3785 "t1_func",
3786 "t1_func1",
3787 "t1_fund",
3788 "t1_fund1",
3789
3790 /* A UTF-8 name with multi-byte sequences to make sure that
3791 cp-name-parser understands this as a single identifier ("função"
3792 is "function" in PT). */
3793 u8"u8função",
3794
3795 /* \377 (0xff) is Latin1 'ÿ'. */
3796 "yfunc\377",
3797
3798 /* \377 (0xff) is Latin1 'ÿ'. */
3799 "\377",
3800 "\377\377123",
3801
c62446b1
PA
3802 /* A name with all sorts of complications. Starts with "z" to make
3803 it easier for the completion tests below. */
3804#define Z_SYM_NAME \
3805 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3806 "::tuple<(anonymous namespace)::ui*, " \
3807 "std::default_delete<(anonymous namespace)::ui>, void>"
3808
3809 Z_SYM_NAME
3810};
3811
a3c5fafd
PA
3812/* Returns true if the mapped_index_base::find_name_component_bounds
3813 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3814 in completion mode. */
5c58de74
PA
3815
3816static bool
a3c5fafd 3817check_find_bounds_finds (mapped_index_base &index,
5c58de74 3818 const char *search_name,
fcf23d5b
SM
3819 gdb::array_view<const char *> expected_syms,
3820 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3821{
3822 lookup_name_info lookup_name (search_name,
3823 symbol_name_match_type::FULL, true);
3824
3b00ef10 3825 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
3826 language_cplus,
3827 per_objfile);
5c58de74
PA
3828
3829 size_t distance = std::distance (bounds.first, bounds.second);
3830 if (distance != expected_syms.size ())
3831 return false;
3832
3833 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3834 {
3835 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 3836 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
3837 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3838 return false;
3839 }
3840
3841 return true;
3842}
3843
3844/* Test the lower-level mapped_index::find_name_component_bounds
3845 method. */
3846
c62446b1 3847static void
5c58de74
PA
3848test_mapped_index_find_name_component_bounds ()
3849{
3850 mock_mapped_index mock_index (test_symbols);
3851
fcf23d5b 3852 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
3853
3854 /* Test the lower-level mapped_index::find_name_component_bounds
3855 method in completion mode. */
3856 {
3857 static const char *expected_syms[] = {
3858 "t1_func",
3859 "t1_func1",
5c58de74
PA
3860 };
3861
fcf23d5b
SM
3862 SELF_CHECK (check_find_bounds_finds
3863 (mock_index, "t1_func", expected_syms,
3864 NULL /* per_objfile */));
5c58de74
PA
3865 }
3866
3867 /* Check that the increment-last-char in the name matching algorithm
3868 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3869 {
3870 static const char *expected_syms1[] = {
3871 "\377",
3872 "\377\377123",
3873 };
fcf23d5b
SM
3874 SELF_CHECK (check_find_bounds_finds
3875 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
3876
3877 static const char *expected_syms2[] = {
3878 "\377\377123",
3879 };
fcf23d5b
SM
3880 SELF_CHECK (check_find_bounds_finds
3881 (mock_index, "\377\377", expected_syms2,
3882 NULL /* per_objfile */));
5c58de74
PA
3883 }
3884}
3885
3886/* Test dw2_expand_symtabs_matching_symbol. */
3887
3888static void
3889test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
3890{
3891 mock_mapped_index mock_index (test_symbols);
3892
3893 /* We let all tests run until the end even if some fails, for debug
3894 convenience. */
3895 bool any_mismatch = false;
3896
3897 /* Create the expected symbols list (an initializer_list). Needed
3898 because lists have commas, and we need to pass them to CHECK,
3899 which is a macro. */
3900#define EXPECT(...) { __VA_ARGS__ }
3901
3902 /* Wrapper for check_match that passes down the current
3903 __FILE__/__LINE__. */
3904#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3905 any_mismatch |= !check_match (__FILE__, __LINE__, \
3906 mock_index, \
3907 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 3908 EXPECTED_LIST, NULL)
c62446b1
PA
3909
3910 /* Identity checks. */
3911 for (const char *sym : test_symbols)
3912 {
3913 /* Should be able to match all existing symbols. */
3914 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3915 EXPECT (sym));
3916
3917 /* Should be able to match all existing symbols with
3918 parameters. */
3919 std::string with_params = std::string (sym) + "(int)";
3920 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3921 EXPECT (sym));
3922
3923 /* Should be able to match all existing symbols with
3924 parameters and qualifiers. */
3925 with_params = std::string (sym) + " ( int ) const";
3926 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3927 EXPECT (sym));
3928
3929 /* This should really find sym, but cp-name-parser.y doesn't
3930 know about lvalue/rvalue qualifiers yet. */
3931 with_params = std::string (sym) + " ( int ) &&";
3932 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3933 {});
3934 }
3935
e1ef7d7a
PA
3936 /* Check that the name matching algorithm for completion doesn't get
3937 confused with Latin1 'ÿ' / 0xff. */
3938 {
3939 static const char str[] = "\377";
3940 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3941 EXPECT ("\377", "\377\377123"));
3942 }
3943
3944 /* Check that the increment-last-char in the matching algorithm for
3945 completion doesn't match "t1_fund" when completing "t1_func". */
3946 {
3947 static const char str[] = "t1_func";
3948 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3949 EXPECT ("t1_func", "t1_func1"));
3950 }
3951
c62446b1
PA
3952 /* Check that completion mode works at each prefix of the expected
3953 symbol name. */
3954 {
3955 static const char str[] = "function(int)";
3956 size_t len = strlen (str);
3957 std::string lookup;
3958
3959 for (size_t i = 1; i < len; i++)
3960 {
3961 lookup.assign (str, i);
3962 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3963 EXPECT ("function"));
3964 }
3965 }
3966
3967 /* While "w" is a prefix of both components, the match function
3968 should still only be called once. */
3969 {
3970 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3971 EXPECT ("w1::w2"));
a20714ff
PA
3972 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3973 EXPECT ("w1::w2"));
c62446b1
PA
3974 }
3975
3976 /* Same, with a "complicated" symbol. */
3977 {
3978 static const char str[] = Z_SYM_NAME;
3979 size_t len = strlen (str);
3980 std::string lookup;
3981
3982 for (size_t i = 1; i < len; i++)
3983 {
3984 lookup.assign (str, i);
3985 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3986 EXPECT (Z_SYM_NAME));
3987 }
3988 }
3989
3990 /* In FULL mode, an incomplete symbol doesn't match. */
3991 {
3992 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3993 {});
3994 }
3995
3996 /* A complete symbol with parameters matches any overload, since the
3997 index has no overload info. */
3998 {
3999 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4000 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4001 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4002 EXPECT ("std::zfunction", "std::zfunction2"));
4003 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4004 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4005 }
4006
4007 /* Check that whitespace is ignored appropriately. A symbol with a
4008 template argument list. */
4009 {
4010 static const char expected[] = "ns::foo<int>";
4011 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4012 EXPECT (expected));
a20714ff
PA
4013 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4014 EXPECT (expected));
c62446b1
PA
4015 }
4016
4017 /* Check that whitespace is ignored appropriately. A symbol with a
4018 template argument list that includes a pointer. */
4019 {
4020 static const char expected[] = "ns::foo<char*>";
4021 /* Try both completion and non-completion modes. */
4022 static const bool completion_mode[2] = {false, true};
4023 for (size_t i = 0; i < 2; i++)
4024 {
4025 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4026 completion_mode[i], EXPECT (expected));
a20714ff
PA
4027 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4028 completion_mode[i], EXPECT (expected));
c62446b1
PA
4029
4030 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4031 completion_mode[i], EXPECT (expected));
a20714ff
PA
4032 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4033 completion_mode[i], EXPECT (expected));
c62446b1
PA
4034 }
4035 }
4036
4037 {
4038 /* Check method qualifiers are ignored. */
4039 static const char expected[] = "ns::foo<char*>";
4040 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4041 symbol_name_match_type::FULL, true, EXPECT (expected));
4042 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4043 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4044 CHECK_MATCH ("foo < char * > ( int ) const",
4045 symbol_name_match_type::WILD, true, EXPECT (expected));
4046 CHECK_MATCH ("foo < char * > ( int ) &&",
4047 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4048 }
4049
4050 /* Test lookup names that don't match anything. */
4051 {
a20714ff
PA
4052 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4053 {});
4054
c62446b1
PA
4055 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4056 {});
4057 }
4058
a20714ff
PA
4059 /* Some wild matching tests, exercising "(anonymous namespace)",
4060 which should not be confused with a parameter list. */
4061 {
4062 static const char *syms[] = {
4063 "A::B::C",
4064 "B::C",
4065 "C",
4066 "A :: B :: C ( int )",
4067 "B :: C ( int )",
4068 "C ( int )",
4069 };
4070
4071 for (const char *s : syms)
4072 {
4073 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4074 EXPECT ("(anonymous namespace)::A::B::C"));
4075 }
4076 }
4077
4078 {
4079 static const char expected[] = "ns2::tmpl<int>::foo2";
4080 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4081 EXPECT (expected));
4082 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4083 EXPECT (expected));
4084 }
4085
c62446b1
PA
4086 SELF_CHECK (!any_mismatch);
4087
4088#undef EXPECT
4089#undef CHECK_MATCH
4090}
4091
5c58de74
PA
4092static void
4093run_test ()
4094{
4095 test_mapped_index_find_name_component_bounds ();
4096 test_dw2_expand_symtabs_matching_symbol ();
4097}
4098
c62446b1
PA
4099}} // namespace selftests::dw2_expand_symtabs_matching
4100
4101#endif /* GDB_SELF_TEST */
4102
4b514bc8
JK
4103/* If FILE_MATCHER is NULL or if PER_CU has
4104 dwarf2_per_cu_quick_data::MARK set (see
4105 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4106 EXPANSION_NOTIFY on it. */
4107
df35e626 4108static bool
4b514bc8 4109dw2_expand_symtabs_matching_one
97a1449a
SM
4110 (dwarf2_per_cu_data *per_cu,
4111 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4112 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4113 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4114{
4115 if (file_matcher == NULL || per_cu->v.quick->mark)
4116 {
af758d11 4117 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4118
97a1449a
SM
4119 compunit_symtab *symtab
4120 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4121 gdb_assert (symtab != nullptr);
4b514bc8 4122
af758d11 4123 if (expansion_notify != NULL && symtab_was_null)
df35e626 4124 return expansion_notify (symtab);
4b514bc8 4125 }
df35e626 4126 return true;
4b514bc8
JK
4127}
4128
3f563c84
PA
4129/* Helper for dw2_expand_matching symtabs. Called on each symbol
4130 matched, to expand corresponding CUs that were marked. IDX is the
4131 index of the symbol name that matched. */
4132
df35e626 4133static bool
3f563c84 4134dw2_expand_marked_cus
976ca316 4135 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4136 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4137 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4138 block_search_flags search_flags,
3f563c84
PA
4139 search_domain kind)
4140{
42c2c694 4141 offset_type vec_len, vec_idx;
3f563c84 4142 bool global_seen = false;
976ca316 4143 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4144
42c2c694
TT
4145 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4146 vec_len = vec[0];
61920122
PA
4147 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4148 {
42c2c694 4149 offset_type cu_index_and_attrs = vec[vec_idx + 1];
61920122
PA
4150 /* This value is only valid for index versions >= 7. */
4151 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4152 gdb_index_symbol_kind symbol_kind =
4153 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4154 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4155 /* Only check the symbol attributes if they're present.
4156 Indices prior to version 7 don't record them,
4157 and indices >= 7 may elide them for certain symbols
4158 (gold does this). */
4159 int attrs_valid =
4160 (index.version >= 7
4161 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4162
4163 /* Work around gold/15646. */
f030440d
TV
4164 if (attrs_valid
4165 && !is_static
4166 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4167 {
f030440d 4168 if (global_seen)
61920122 4169 continue;
f030440d
TV
4170
4171 global_seen = true;
61920122 4172 }
3190f0c6 4173
61920122
PA
4174 /* Only check the symbol's kind if it has one. */
4175 if (attrs_valid)
4176 {
03a8ea51
TT
4177 if (is_static)
4178 {
4179 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4180 continue;
4181 }
4182 else
4183 {
4184 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4185 continue;
4186 }
4187
61920122 4188 switch (kind)
8943b874 4189 {
61920122
PA
4190 case VARIABLES_DOMAIN:
4191 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4192 continue;
4193 break;
4194 case FUNCTIONS_DOMAIN:
4195 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4196 continue;
61920122
PA
4197 break;
4198 case TYPES_DOMAIN:
4199 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4200 continue;
4201 break;
59c35742
AB
4202 case MODULES_DOMAIN:
4203 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4204 continue;
4205 break;
61920122
PA
4206 default:
4207 break;
8943b874 4208 }
61920122 4209 }
8943b874 4210
61920122 4211 /* Don't crash on bad data. */
91eea9cc 4212 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
61920122 4213 {
b98664d3 4214 complaint (_(".gdb_index entry has bad CU index"
976ca316 4215 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4216 continue;
4217 }
4218
91eea9cc 4219 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
df35e626
TT
4220 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4221 expansion_notify))
4222 return false;
61920122 4223 }
df35e626
TT
4224
4225 return true;
61920122
PA
4226}
4227
4b514bc8
JK
4228/* If FILE_MATCHER is non-NULL, set all the
4229 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4230 that match FILE_MATCHER. */
4231
61920122 4232static void
4b514bc8 4233dw_expand_symtabs_matching_file_matcher
976ca316 4234 (dwarf2_per_objfile *per_objfile,
ed2dc618 4235 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4236{
4b514bc8 4237 if (file_matcher == NULL)
61920122
PA
4238 return;
4239
4b514bc8
JK
4240 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4241 htab_eq_pointer,
4242 NULL, xcalloc, xfree));
4243 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4244 htab_eq_pointer,
4245 NULL, xcalloc, xfree));
61920122 4246
4b514bc8
JK
4247 /* The rule is CUs specify all the files, including those used by
4248 any TU, so there's no need to scan TUs here. */
61920122 4249
0d305d5c 4250 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4251 {
927aa2e7
JK
4252 QUIT;
4253
91eea9cc
TT
4254 if (per_cu->is_debug_types)
4255 continue;
927aa2e7
JK
4256 per_cu->v.quick->mark = 0;
4257
4258 /* We only need to look at symtabs not already expanded. */
0d305d5c 4259 if (per_objfile->symtab_set_p (per_cu.get ()))
927aa2e7
JK
4260 continue;
4261
0d305d5c
TT
4262 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4263 per_objfile);
927aa2e7
JK
4264 if (file_data == NULL)
4265 continue;
4266
4267 if (htab_find (visited_not_found.get (), file_data) != NULL)
4268 continue;
4269 else if (htab_find (visited_found.get (), file_data) != NULL)
4270 {
4271 per_cu->v.quick->mark = 1;
4272 continue;
4273 }
4274
b76e467d 4275 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4276 {
4277 const char *this_real_name;
4278
4279 if (file_matcher (file_data->file_names[j], false))
4280 {
4281 per_cu->v.quick->mark = 1;
4282 break;
4283 }
4284
4285 /* Before we invoke realpath, which can get expensive when many
4286 files are involved, do a quick comparison of the basenames. */
4287 if (!basenames_may_differ
4288 && !file_matcher (lbasename (file_data->file_names[j]),
4289 true))
4290 continue;
4291
976ca316 4292 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4293 if (file_matcher (this_real_name, false))
4294 {
4295 per_cu->v.quick->mark = 1;
4296 break;
4297 }
4298 }
4299
b76e467d
SM
4300 void **slot = htab_find_slot (per_cu->v.quick->mark
4301 ? visited_found.get ()
4302 : visited_not_found.get (),
4303 file_data, INSERT);
927aa2e7
JK
4304 *slot = file_data;
4305 }
4306}
4307
df35e626 4308bool
af82f89d
TT
4309dwarf2_gdb_index::expand_symtabs_matching
4310 (struct objfile *objfile,
4311 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4312 const lookup_name_info *lookup_name,
4313 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4314 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4315 block_search_flags search_flags,
3bfa51a7 4316 domain_enum domain,
af82f89d 4317 enum search_domain kind)
927aa2e7 4318{
976ca316 4319 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4320
4321 /* index_table is NULL if OBJF_READNOW. */
976ca316 4322 if (!per_objfile->per_bfd->index_table)
df35e626 4323 return true;
927aa2e7 4324
976ca316 4325 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4326
c1a66c06
TV
4327 if (symbol_matcher == NULL && lookup_name == NULL)
4328 {
0d305d5c 4329 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4330 {
4331 QUIT;
4332
0d305d5c 4333 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
df35e626
TT
4334 file_matcher,
4335 expansion_notify))
4336 return false;
c1a66c06 4337 }
df35e626 4338 return true;
c1a66c06
TV
4339 }
4340
976ca316 4341 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4342
df35e626
TT
4343 bool result
4344 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4345 symbol_matcher,
4346 [&] (offset_type idx)
927aa2e7 4347 {
df35e626 4348 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
03a8ea51 4349 expansion_notify, search_flags, kind))
df35e626 4350 return false;
3b00ef10 4351 return true;
976ca316 4352 }, per_objfile);
df35e626
TT
4353
4354 return result;
927aa2e7
JK
4355}
4356
4357/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4358 symtab. */
4359
4360static struct compunit_symtab *
4361recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4362 CORE_ADDR pc)
4363{
4364 int i;
4365
4366 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4367 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4368 return cust;
4369
4370 if (cust->includes == NULL)
4371 return NULL;
4372
4373 for (i = 0; cust->includes[i]; ++i)
4374 {
4375 struct compunit_symtab *s = cust->includes[i];
4376
4377 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4378 if (s != NULL)
4379 return s;
4380 }
4381
4382 return NULL;
4383}
4384
39298a5d
TT
4385struct compunit_symtab *
4386dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4387 (struct objfile *objfile,
4388 struct bound_minimal_symbol msymbol,
4389 CORE_ADDR pc,
4390 struct obj_section *section,
4391 int warn_if_readin)
927aa2e7
JK
4392{
4393 struct dwarf2_per_cu_data *data;
4394 struct compunit_symtab *result;
4395
efd7398e
TT
4396 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4397 if (per_objfile->per_bfd->index_addrmap == nullptr)
927aa2e7
JK
4398 return NULL;
4399
b3b3bada 4400 CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e
TT
4401 data = ((struct dwarf2_per_cu_data *)
4402 addrmap_find (per_objfile->per_bfd->index_addrmap,
4403 pc - baseaddr));
927aa2e7
JK
4404 if (!data)
4405 return NULL;
4406
af758d11 4407 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4408 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4409 paddress (objfile->arch (), pc));
927aa2e7 4410
97a1449a
SM
4411 result = recursively_find_pc_sect_compunit_symtab
4412 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4413
927aa2e7
JK
4414 gdb_assert (result != NULL);
4415 return result;
4416}
4417
39298a5d 4418void
f4655dee
TT
4419dwarf2_base_index_functions::map_symbol_filenames
4420 (struct objfile *objfile,
4421 gdb::function_view<symbol_filename_ftype> fun,
4422 bool need_fullname)
927aa2e7 4423{
976ca316 4424 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4425
9fc29955
AB
4426 /* Use caches to ensure we only call FUN once for each filename. */
4427 filename_seen_cache filenames_cache;
4428 std::unordered_set<quick_file_names *> qfn_cache;
927aa2e7 4429
9fc29955
AB
4430 /* The rule is CUs specify all the files, including those used by any TU,
4431 so there's no need to scan TUs here. We can ignore file names coming
4432 from already-expanded CUs. It is possible that an expanded CU might
4433 reuse the file names data from a currently unexpanded CU, in this
4434 case we don't want to report the files from the unexpanded CU. */
927aa2e7 4435
0d305d5c 4436 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
9fc29955 4437 {
0d305d5c 4438 if (per_objfile->symtab_set_p (per_cu.get ()))
927aa2e7 4439 {
9fc29955
AB
4440 if (per_cu->v.quick->file_names != nullptr)
4441 qfn_cache.insert (per_cu->v.quick->file_names);
927aa2e7 4442 }
9fc29955 4443 }
927aa2e7 4444
0d305d5c 4445 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
9fc29955
AB
4446 {
4447 /* We only need to look at symtabs not already expanded. */
0d305d5c 4448 if (per_objfile->symtab_set_p (per_cu.get ()))
9fc29955 4449 continue;
927aa2e7 4450
0d305d5c
TT
4451 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4452 per_objfile);
9fc29955
AB
4453 if (file_data == nullptr
4454 || qfn_cache.find (file_data) != qfn_cache.end ())
4455 continue;
927aa2e7 4456
9fc29955
AB
4457 for (int j = 0; j < file_data->num_file_names; ++j)
4458 {
4459 const char *filename = file_data->file_names[j];
4460 filenames_cache.seen (filename);
927aa2e7
JK
4461 }
4462 }
4463
9fc29955 4464 filenames_cache.traverse ([&] (const char *filename)
927aa2e7
JK
4465 {
4466 gdb::unique_xmalloc_ptr<char> this_real_name;
4467
4468 if (need_fullname)
4469 this_real_name = gdb_realpath (filename);
f4655dee 4470 fun (filename, this_real_name.get ());
927aa2e7
JK
4471 });
4472}
4473
39298a5d
TT
4474bool
4475dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
927aa2e7 4476{
fae2120b 4477 return true;
927aa2e7
JK
4478}
4479
927aa2e7
JK
4480/* DWARF-5 debug_names reader. */
4481
4482/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4483static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4484
4485/* A helper function that reads the .debug_names section in SECTION
4486 and fills in MAP. FILENAME is the name of the file containing the
4487 section; it is used for error reporting.
4488
4489 Returns true if all went well, false otherwise. */
4490
4491static bool
4492read_debug_names_from_section (struct objfile *objfile,
4493 const char *filename,
4494 struct dwarf2_section_info *section,
4495 mapped_debug_names &map)
4496{
96b79293 4497 if (section->empty ())
927aa2e7
JK
4498 return false;
4499
4500 /* Older elfutils strip versions could keep the section in the main
4501 executable while splitting it for the separate debug info file. */
96b79293 4502 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4503 return false;
4504
96b79293 4505 section->read (objfile);
927aa2e7 4506
08feed99 4507 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4508
4509 const gdb_byte *addr = section->buffer;
4510
96b79293 4511 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4512
4513 unsigned int bytes_read;
4514 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4515 addr += bytes_read;
4516
4517 map.dwarf5_is_dwarf64 = bytes_read != 4;
4518 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4519 if (bytes_read + length != section->size)
4520 {
4521 /* There may be multiple per-CU indices. */
4522 warning (_("Section .debug_names in %s length %s does not match "
4523 "section length %s, ignoring .debug_names."),
4524 filename, plongest (bytes_read + length),
4525 pulongest (section->size));
4526 return false;
4527 }
4528
4529 /* The version number. */
4530 uint16_t version = read_2_bytes (abfd, addr);
4531 addr += 2;
4532 if (version != 5)
4533 {
4534 warning (_("Section .debug_names in %s has unsupported version %d, "
4535 "ignoring .debug_names."),
4536 filename, version);
4537 return false;
4538 }
4539
4540 /* Padding. */
4541 uint16_t padding = read_2_bytes (abfd, addr);
4542 addr += 2;
4543 if (padding != 0)
4544 {
4545 warning (_("Section .debug_names in %s has unsupported padding %d, "
4546 "ignoring .debug_names."),
4547 filename, padding);
4548 return false;
4549 }
4550
4551 /* comp_unit_count - The number of CUs in the CU list. */
4552 map.cu_count = read_4_bytes (abfd, addr);
4553 addr += 4;
4554
4555 /* local_type_unit_count - The number of TUs in the local TU
4556 list. */
4557 map.tu_count = read_4_bytes (abfd, addr);
4558 addr += 4;
4559
4560 /* foreign_type_unit_count - The number of TUs in the foreign TU
4561 list. */
4562 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4563 addr += 4;
4564 if (foreign_tu_count != 0)
4565 {
4566 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4567 "ignoring .debug_names."),
4568 filename, static_cast<unsigned long> (foreign_tu_count));
4569 return false;
4570 }
4571
4572 /* bucket_count - The number of hash buckets in the hash lookup
4573 table. */
4574 map.bucket_count = read_4_bytes (abfd, addr);
4575 addr += 4;
4576
4577 /* name_count - The number of unique names in the index. */
4578 map.name_count = read_4_bytes (abfd, addr);
4579 addr += 4;
4580
4581 /* abbrev_table_size - The size in bytes of the abbreviations
4582 table. */
4583 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4584 addr += 4;
4585
4586 /* augmentation_string_size - The size in bytes of the augmentation
4587 string. This value is rounded up to a multiple of 4. */
4588 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4589 addr += 4;
4590 map.augmentation_is_gdb = ((augmentation_string_size
4591 == sizeof (dwarf5_augmentation))
4592 && memcmp (addr, dwarf5_augmentation,
4593 sizeof (dwarf5_augmentation)) == 0);
4594 augmentation_string_size += (-augmentation_string_size) & 3;
4595 addr += augmentation_string_size;
4596
4597 /* List of CUs */
4598 map.cu_table_reordered = addr;
4599 addr += map.cu_count * map.offset_size;
4600
4601 /* List of Local TUs */
4602 map.tu_table_reordered = addr;
4603 addr += map.tu_count * map.offset_size;
4604
4605 /* Hash Lookup Table */
4606 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4607 addr += map.bucket_count * 4;
4608 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4609 addr += map.name_count * 4;
4610
4611 /* Name Table */
4612 map.name_table_string_offs_reordered = addr;
4613 addr += map.name_count * map.offset_size;
4614 map.name_table_entry_offs_reordered = addr;
4615 addr += map.name_count * map.offset_size;
4616
4617 const gdb_byte *abbrev_table_start = addr;
4618 for (;;)
4619 {
927aa2e7
JK
4620 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4621 addr += bytes_read;
4622 if (index_num == 0)
4623 break;
4624
4625 const auto insertpair
4626 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4627 if (!insertpair.second)
4628 {
4629 warning (_("Section .debug_names in %s has duplicate index %s, "
4630 "ignoring .debug_names."),
4631 filename, pulongest (index_num));
4632 return false;
4633 }
4634 mapped_debug_names::index_val &indexval = insertpair.first->second;
4635 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4636 addr += bytes_read;
4637
4638 for (;;)
4639 {
4640 mapped_debug_names::index_val::attr attr;
4641 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4642 addr += bytes_read;
4643 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4644 addr += bytes_read;
4645 if (attr.form == DW_FORM_implicit_const)
4646 {
4647 attr.implicit_const = read_signed_leb128 (abfd, addr,
4648 &bytes_read);
4649 addr += bytes_read;
4650 }
4651 if (attr.dw_idx == 0 && attr.form == 0)
4652 break;
4653 indexval.attr_vec.push_back (std::move (attr));
4654 }
4655 }
4656 if (addr != abbrev_table_start + abbrev_table_size)
4657 {
4658 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4659 "of size %s vs. written as %u, ignoring .debug_names."),
4660 filename, plongest (addr - abbrev_table_start),
4661 abbrev_table_size);
927aa2e7
JK
4662 return false;
4663 }
4664 map.entry_pool = addr;
4665
4666 return true;
4667}
4668
4669/* A helper for create_cus_from_debug_names that handles the MAP's CU
4670 list. */
4671
4672static void
168c9250 4673create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4674 const mapped_debug_names &map,
4675 dwarf2_section_info &section,
b76e467d 4676 bool is_dwz)
927aa2e7 4677{
3ee6bb11
TV
4678 if (!map.augmentation_is_gdb)
4679 {
fc9a13fb
TV
4680 for (uint32_t i = 0; i < map.cu_count; ++i)
4681 {
4682 sect_offset sect_off
4683 = (sect_offset) (extract_unsigned_integer
4684 (map.cu_table_reordered + i * map.offset_size,
4685 map.offset_size,
4686 map.dwarf5_byte_order));
4687 /* We don't know the length of the CU, because the CU list in a
4688 .debug_names index can be incomplete, so we can't use the start
4689 of the next CU as end of this CU. We create the CUs here with
4690 length 0, and in cutu_reader::cutu_reader we'll fill in the
4691 actual length. */
473ab964 4692 dwarf2_per_cu_data_up per_cu
fc9a13fb
TV
4693 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4694 sect_off, 0);
0d305d5c 4695 per_bfd->all_comp_units.push_back (std::move (per_cu));
fc9a13fb 4696 }
d3b54e63 4697 return;
3ee6bb11
TV
4698 }
4699
927aa2e7
JK
4700 sect_offset sect_off_prev;
4701 for (uint32_t i = 0; i <= map.cu_count; ++i)
4702 {
4703 sect_offset sect_off_next;
4704 if (i < map.cu_count)
4705 {
4706 sect_off_next
4707 = (sect_offset) (extract_unsigned_integer
4708 (map.cu_table_reordered + i * map.offset_size,
4709 map.offset_size,
4710 map.dwarf5_byte_order));
4711 }
4712 else
4713 sect_off_next = (sect_offset) section.size;
4714 if (i >= 1)
4715 {
4716 const ULONGEST length = sect_off_next - sect_off_prev;
473ab964 4717 dwarf2_per_cu_data_up per_cu
168c9250 4718 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 4719 sect_off_prev, length);
0d305d5c 4720 per_bfd->all_comp_units.push_back (std::move (per_cu));
927aa2e7
JK
4721 }
4722 sect_off_prev = sect_off_next;
4723 }
4724}
4725
4726/* Read the CU list from the mapped index, and use it to create all
ed2dc618 4727 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
4728
4729static void
168c9250 4730create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4731 const mapped_debug_names &map,
4732 const mapped_debug_names &dwz_map)
4733{
168c9250
SM
4734 gdb_assert (per_bfd->all_comp_units.empty ());
4735 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 4736
168c9250 4737 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 4738 false /* is_dwz */);
927aa2e7
JK
4739
4740 if (dwz_map.cu_count == 0)
4741 return;
4742
168c9250
SM
4743 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4744 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 4745 true /* is_dwz */);
927aa2e7
JK
4746}
4747
4748/* Read .debug_names. If everything went ok, initialize the "quick"
4749 elements of all the CUs and return true. Otherwise, return false. */
4750
4751static bool
976ca316 4752dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 4753{
fcf23d5b
SM
4754 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4755 mapped_debug_names dwz_map;
976ca316 4756 struct objfile *objfile = per_objfile->objfile;
f8c41851 4757 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
4758
4759 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
bab287cd 4760 &per_bfd->debug_names, *map))
927aa2e7
JK
4761 return false;
4762
4763 /* Don't use the index if it's empty. */
22ca247e 4764 if (map->name_count == 0)
927aa2e7
JK
4765 return false;
4766
4767 /* If there is a .dwz file, read it so we can get its CU list as
4768 well. */
f8c41851 4769 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
4770 if (dwz != NULL)
4771 {
4772 if (!read_debug_names_from_section (objfile,
00f93c44 4773 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
4774 &dwz->debug_names, dwz_map))
4775 {
4776 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 4777 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
4778 return false;
4779 }
4780 }
4781
f8c41851 4782 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 4783
22ca247e 4784 if (map->tu_count != 0)
927aa2e7
JK
4785 {
4786 /* We can only handle a single .debug_types when we have an
4787 index. */
f8c41851 4788 if (per_bfd->types.size () != 1)
927aa2e7
JK
4789 return false;
4790
f8c41851 4791 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
4792
4793 create_signatured_type_table_from_debug_names
f8c41851 4794 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
4795 }
4796
f8c41851 4797 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 4798
f8c41851
SM
4799 per_bfd->debug_names_table = std::move (map);
4800 per_bfd->using_index = 1;
4801 per_bfd->quick_file_names_table =
bab287cd 4802 create_quick_file_names_table (per_bfd->all_comp_units.size ());
927aa2e7
JK
4803
4804 return true;
4805}
4806
927aa2e7
JK
4807/* Type used to manage iterating over all CUs looking for a symbol for
4808 .debug_names. */
4809
4810class dw2_debug_names_iterator
4811{
4812public:
927aa2e7 4813 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4814 block_search_flags block_index,
2b79f376 4815 domain_enum domain,
fcf23d5b 4816 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 4817 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
4818 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4819 m_per_objfile (per_objfile)
927aa2e7
JK
4820 {}
4821
4822 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4823 search_domain search, uint32_t namei,
3bfa51a7
TT
4824 dwarf2_per_objfile *per_objfile,
4825 domain_enum domain = UNDEF_DOMAIN)
927aa2e7 4826 : m_map (map),
3bfa51a7 4827 m_domain (domain),
927aa2e7 4828 m_search (search),
fcf23d5b
SM
4829 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4830 m_per_objfile (per_objfile)
927aa2e7
JK
4831 {}
4832
3b00ef10 4833 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4834 block_search_flags block_index, domain_enum domain,
fcf23d5b 4835 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 4836 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
4837 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4838 m_per_objfile (per_objfile)
3b00ef10
TT
4839 {}
4840
927aa2e7
JK
4841 /* Return the next matching CU or NULL if there are no more. */
4842 dwarf2_per_cu_data *next ();
4843
4844private:
4845 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
4846 const char *name,
4847 dwarf2_per_objfile *per_objfile);
927aa2e7 4848 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
4849 uint32_t namei,
4850 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
4851
4852 /* The internalized form of .debug_names. */
4853 const mapped_debug_names &m_map;
4854
03a8ea51
TT
4855 /* Restrict the search to these blocks. */
4856 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4857 | SEARCH_STATIC_BLOCK);
927aa2e7
JK
4858
4859 /* The kind of symbol we're looking for. */
4860 const domain_enum m_domain = UNDEF_DOMAIN;
4861 const search_domain m_search = ALL_DOMAIN;
4862
4863 /* The list of CUs from the index entry of the symbol, or NULL if
4864 not found. */
4865 const gdb_byte *m_addr;
fcf23d5b
SM
4866
4867 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
4868};
4869
4870const char *
fcf23d5b 4871mapped_debug_names::namei_to_name
976ca316 4872 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
4873{
4874 const ULONGEST namei_string_offs
4875 = extract_unsigned_integer ((name_table_string_offs_reordered
4876 + namei * offset_size),
4877 offset_size,
4878 dwarf5_byte_order);
976ca316 4879 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
4880}
4881
4882/* Find a slot in .debug_names for the object named NAME. If NAME is
4883 found, return pointer to its pool data. If NAME cannot be found,
4884 return NULL. */
4885
4886const gdb_byte *
4887dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
4888 (const mapped_debug_names &map, const char *name,
4889 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
4890{
4891 int (*cmp) (const char *, const char *);
4892
54ee4252 4893 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
4894 if (current_language->la_language == language_cplus
4895 || current_language->la_language == language_fortran
4896 || current_language->la_language == language_d)
4897 {
4898 /* NAME is already canonical. Drop any qualifiers as
4899 .debug_names does not contain any. */
4900
4901 if (strchr (name, '(') != NULL)
4902 {
54ee4252 4903 without_params = cp_remove_params (name);
927aa2e7 4904 if (without_params != NULL)
54ee4252 4905 name = without_params.get ();
927aa2e7
JK
4906 }
4907 }
4908
4909 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4910
4911 const uint32_t full_hash = dwarf5_djb_hash (name);
4912 uint32_t namei
4913 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4914 (map.bucket_table_reordered
4915 + (full_hash % map.bucket_count)), 4,
4916 map.dwarf5_byte_order);
4917 if (namei == 0)
4918 return NULL;
4919 --namei;
4920 if (namei >= map.name_count)
4921 {
b98664d3 4922 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
4923 "[in module %s]"),
4924 namei, map.name_count,
fcf23d5b 4925 objfile_name (per_objfile->objfile));
927aa2e7
JK
4926 return NULL;
4927 }
4928
4929 for (;;)
4930 {
4931 const uint32_t namei_full_hash
4932 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4933 (map.hash_table_reordered + namei), 4,
4934 map.dwarf5_byte_order);
4935 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4936 return NULL;
4937
4938 if (full_hash == namei_full_hash)
4939 {
fcf23d5b 4940 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
4941
4942#if 0 /* An expensive sanity check. */
4943 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4944 {
b98664d3 4945 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
4946 "[in module %s]"),
4947 namei, objfile_name (dwarf2_per_objfile->objfile));
4948 return NULL;
4949 }
4950#endif
4951
4952 if (cmp (namei_string, name) == 0)
4953 {
4954 const ULONGEST namei_entry_offs
4955 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4956 + namei * map.offset_size),
4957 map.offset_size, map.dwarf5_byte_order);
4958 return map.entry_pool + namei_entry_offs;
4959 }
4960 }
4961
4962 ++namei;
4963 if (namei >= map.name_count)
4964 return NULL;
4965 }
4966}
4967
4968const gdb_byte *
4969dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 4970 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
4971{
4972 if (namei >= map.name_count)
4973 {
b98664d3 4974 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
4975 "[in module %s]"),
4976 namei, map.name_count,
fcf23d5b 4977 objfile_name (per_objfile->objfile));
927aa2e7
JK
4978 return NULL;
4979 }
4980
4981 const ULONGEST namei_entry_offs
4982 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4983 + namei * map.offset_size),
4984 map.offset_size, map.dwarf5_byte_order);
4985 return map.entry_pool + namei_entry_offs;
4986}
4987
4988/* See dw2_debug_names_iterator. */
4989
4990dwarf2_per_cu_data *
4991dw2_debug_names_iterator::next ()
4992{
4993 if (m_addr == NULL)
4994 return NULL;
4995
fcf23d5b
SM
4996 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4997 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 4998 bfd *const abfd = objfile->obfd;
927aa2e7
JK
4999
5000 again:
5001
5002 unsigned int bytes_read;
5003 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5004 m_addr += bytes_read;
5005 if (abbrev == 0)
5006 return NULL;
5007
5008 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5009 if (indexval_it == m_map.abbrev_map.cend ())
5010 {
b98664d3 5011 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5012 "[in module %s]"),
ed2dc618 5013 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5014 return NULL;
5015 }
5016 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5017 enum class symbol_linkage {
5018 unknown,
5019 static_,
5020 extern_,
23c13d42 5021 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5022 dwarf2_per_cu_data *per_cu = NULL;
5023 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5024 {
5025 ULONGEST ull;
5026 switch (attr.form)
5027 {
5028 case DW_FORM_implicit_const:
5029 ull = attr.implicit_const;
5030 break;
5031 case DW_FORM_flag_present:
5032 ull = 1;
5033 break;
5034 case DW_FORM_udata:
5035 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5036 m_addr += bytes_read;
5037 break;
6dc55ce9 5038 case DW_FORM_ref4:
5039 ull = read_4_bytes (abfd, m_addr);
5040 m_addr += 4;
5041 break;
5042 case DW_FORM_ref8:
5043 ull = read_8_bytes (abfd, m_addr);
5044 m_addr += 8;
5045 break;
5046 case DW_FORM_ref_sig8:
5047 ull = read_8_bytes (abfd, m_addr);
5048 m_addr += 8;
5049 break;
927aa2e7 5050 default:
b98664d3 5051 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5052 dwarf_form_name (attr.form),
ed2dc618 5053 objfile_name (objfile));
927aa2e7
JK
5054 return NULL;
5055 }
5056 switch (attr.dw_idx)
5057 {
5058 case DW_IDX_compile_unit:
5059 /* Don't crash on bad data. */
bab287cd 5060 if (ull >= per_bfd->all_comp_units.size ())
927aa2e7 5061 {
b98664d3 5062 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5063 " [in module %s]"),
5064 pulongest (ull),
fcf23d5b 5065 objfile_name (objfile));
927aa2e7
JK
5066 continue;
5067 }
91eea9cc 5068 per_cu = per_bfd->get_cu (ull);
927aa2e7 5069 break;
8af5c486
JK
5070 case DW_IDX_type_unit:
5071 /* Don't crash on bad data. */
91eea9cc 5072 if (ull >= per_bfd->tu_stats.nr_tus)
8af5c486 5073 {
b98664d3 5074 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5075 " [in module %s]"),
5076 pulongest (ull),
fcf23d5b 5077 objfile_name (objfile));
8af5c486
JK
5078 continue;
5079 }
91eea9cc 5080 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
8af5c486 5081 break;
6dc55ce9 5082 case DW_IDX_die_offset:
5083 /* In a per-CU index (as opposed to a per-module index), index
5084 entries without CU attribute implicitly refer to the single CU. */
5085 if (per_cu == NULL)
fcf23d5b 5086 per_cu = per_bfd->get_cu (0);
6dc55ce9 5087 break;
927aa2e7
JK
5088 case DW_IDX_GNU_internal:
5089 if (!m_map.augmentation_is_gdb)
5090 break;
23c13d42 5091 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5092 break;
5093 case DW_IDX_GNU_external:
5094 if (!m_map.augmentation_is_gdb)
5095 break;
23c13d42 5096 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5097 break;
5098 }
5099 }
5100
5101 /* Skip if already read in. */
fcf23d5b 5102 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5103 goto again;
5104
5105 /* Check static vs global. */
03a8ea51 5106 if (symbol_linkage_ != symbol_linkage::unknown)
927aa2e7 5107 {
03a8ea51
TT
5108 if (symbol_linkage_ == symbol_linkage::static_)
5109 {
5110 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5111 goto again;
5112 }
5113 else
5114 {
5115 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5116 goto again;
5117 }
927aa2e7
JK
5118 }
5119
5120 /* Match dw2_symtab_iter_next, symbol_kind
5121 and debug_names::psymbol_tag. */
5122 switch (m_domain)
5123 {
5124 case VAR_DOMAIN:
5125 switch (indexval.dwarf_tag)
5126 {
5127 case DW_TAG_variable:
5128 case DW_TAG_subprogram:
5129 /* Some types are also in VAR_DOMAIN. */
5130 case DW_TAG_typedef:
5131 case DW_TAG_structure_type:
5132 break;
5133 default:
5134 goto again;
5135 }
5136 break;
5137 case STRUCT_DOMAIN:
5138 switch (indexval.dwarf_tag)
5139 {
5140 case DW_TAG_typedef:
5141 case DW_TAG_structure_type:
5142 break;
5143 default:
5144 goto again;
5145 }
5146 break;
5147 case LABEL_DOMAIN:
5148 switch (indexval.dwarf_tag)
5149 {
5150 case 0:
5151 case DW_TAG_variable:
5152 break;
5153 default:
5154 goto again;
5155 }
5156 break;
59c35742
AB
5157 case MODULE_DOMAIN:
5158 switch (indexval.dwarf_tag)
5159 {
5160 case DW_TAG_module:
5161 break;
5162 default:
5163 goto again;
5164 }
5165 break;
927aa2e7
JK
5166 default:
5167 break;
5168 }
5169
5170 /* Match dw2_expand_symtabs_matching, symbol_kind and
5171 debug_names::psymbol_tag. */
5172 switch (m_search)
4b514bc8 5173 {
927aa2e7
JK
5174 case VARIABLES_DOMAIN:
5175 switch (indexval.dwarf_tag)
4b514bc8 5176 {
927aa2e7
JK
5177 case DW_TAG_variable:
5178 break;
5179 default:
5180 goto again;
4b514bc8 5181 }
927aa2e7
JK
5182 break;
5183 case FUNCTIONS_DOMAIN:
5184 switch (indexval.dwarf_tag)
4b514bc8 5185 {
927aa2e7
JK
5186 case DW_TAG_subprogram:
5187 break;
5188 default:
5189 goto again;
4b514bc8 5190 }
927aa2e7
JK
5191 break;
5192 case TYPES_DOMAIN:
5193 switch (indexval.dwarf_tag)
5194 {
5195 case DW_TAG_typedef:
5196 case DW_TAG_structure_type:
5197 break;
5198 default:
5199 goto again;
5200 }
5201 break;
59c35742
AB
5202 case MODULES_DOMAIN:
5203 switch (indexval.dwarf_tag)
5204 {
5205 case DW_TAG_module:
5206 break;
5207 default:
5208 goto again;
5209 }
927aa2e7
JK
5210 default:
5211 break;
4b514bc8 5212 }
927aa2e7
JK
5213
5214 return per_cu;
4b514bc8 5215}
61920122 5216
927aa2e7
JK
5217/* This dumps minimal information about .debug_names. It is called
5218 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5219 uses this to verify that .debug_names has been loaded. */
9291a0cd 5220
39298a5d
TT
5221void
5222dwarf2_debug_names_index::dump (struct objfile *objfile)
927aa2e7 5223{
976ca316 5224 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5225
976ca316 5226 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5227 printf_filtered (".debug_names:");
976ca316 5228 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5229 printf_filtered (" exists\n");
5230 else
5231 printf_filtered (" faked for \"readnow\"\n");
5232 printf_filtered ("\n");
9291a0cd
TT
5233}
5234
39298a5d 5235void
0b7b2c2a 5236dwarf2_debug_names_index::expand_matching_symbols
3b00ef10
TT
5237 (struct objfile *objfile,
5238 const lookup_name_info &name, domain_enum domain,
5239 int global,
3b00ef10
TT
5240 symbol_compare_ftype *ordered_compare)
5241{
976ca316 5242 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5243
5244 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5245 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5246 return;
5247
976ca316 5248 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
03a8ea51
TT
5249 const block_search_flags block_flags
5250 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
3b00ef10
TT
5251
5252 const char *match_name = name.ada ().lookup_name ().c_str ();
5253 auto matcher = [&] (const char *symname)
5254 {
5255 if (ordered_compare == nullptr)
5256 return true;
5257 return ordered_compare (symname, match_name) == 0;
5258 };
5259
b64f703b 5260 dw2_expand_symtabs_matching_symbol (map, name, matcher,
3b00ef10
TT
5261 [&] (offset_type namei)
5262 {
5263 /* The name was matched, now expand corresponding CUs that were
5264 marked. */
03a8ea51 5265 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
976ca316 5266 per_objfile);
3b00ef10
TT
5267
5268 struct dwarf2_per_cu_data *per_cu;
5269 while ((per_cu = iter.next ()) != NULL)
976ca316 5270 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5271 nullptr);
3b00ef10 5272 return true;
976ca316 5273 }, per_objfile);
3b00ef10
TT
5274}
5275
df35e626 5276bool
39298a5d 5277dwarf2_debug_names_index::expand_symtabs_matching
927aa2e7
JK
5278 (struct objfile *objfile,
5279 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5280 const lookup_name_info *lookup_name,
927aa2e7
JK
5281 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5282 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 5283 block_search_flags search_flags,
3bfa51a7 5284 domain_enum domain,
927aa2e7
JK
5285 enum search_domain kind)
5286{
976ca316 5287 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5288
927aa2e7 5289 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5290 if (!per_objfile->per_bfd->debug_names_table)
df35e626 5291 return true;
9291a0cd 5292
976ca316 5293 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5294
c1a66c06
TV
5295 if (symbol_matcher == NULL && lookup_name == NULL)
5296 {
0d305d5c 5297 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5298 {
5299 QUIT;
5300
0d305d5c 5301 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
df35e626
TT
5302 file_matcher,
5303 expansion_notify))
5304 return false;
c1a66c06 5305 }
df35e626 5306 return true;
c1a66c06
TV
5307 }
5308
976ca316 5309 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5310
df35e626
TT
5311 bool result
5312 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5313 symbol_matcher,
5314 [&] (offset_type namei)
927aa2e7 5315 {
927aa2e7
JK
5316 /* The name was matched, now expand corresponding CUs that were
5317 marked. */
3bfa51a7 5318 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
bbf2f4df 5319
927aa2e7
JK
5320 struct dwarf2_per_cu_data *per_cu;
5321 while ((per_cu = iter.next ()) != NULL)
df35e626
TT
5322 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5323 file_matcher,
5324 expansion_notify))
5325 return false;
3b00ef10 5326 return true;
976ca316 5327 }, per_objfile);
df35e626
TT
5328
5329 return result;
9291a0cd
TT
5330}
5331
4485a1c1 5332/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5333 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5334
5335template <typename T>
5336static gdb::array_view<const gdb_byte>
5337get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5338{
5339 dwarf2_section_info *section = &section_owner->gdb_index;
5340
96b79293 5341 if (section->empty ())
4485a1c1
SM
5342 return {};
5343
5344 /* Older elfutils strip versions could keep the section in the main
5345 executable while splitting it for the separate debug info file. */
96b79293 5346 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5347 return {};
5348
96b79293 5349 section->read (obj);
4485a1c1 5350
8bebfcda
PA
5351 /* dwarf2_section_info::size is a bfd_size_type, while
5352 gdb::array_view works with size_t. On 32-bit hosts, with
5353 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5354 is 32-bit. So we need an explicit narrowing conversion here.
5355 This is fine, because it's impossible to allocate or mmap an
5356 array/buffer larger than what size_t can represent. */
5357 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5358}
5359
87d6a7aa
SM
5360/* Lookup the index cache for the contents of the index associated to
5361 DWARF2_OBJ. */
5362
5363static gdb::array_view<const gdb_byte>
5989a64e 5364get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5365{
5366 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5367 if (build_id == nullptr)
5368 return {};
5369
5370 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5371 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5372}
5373
5374/* Same as the above, but for DWZ. */
5375
5376static gdb::array_view<const gdb_byte>
5377get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5378{
5379 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5380 if (build_id == nullptr)
5381 return {};
5382
5383 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5384}
5385
edc02ceb 5386/* See dwarf2/public.h. */
9291a0cd 5387
edc02ceb
TT
5388void
5389dwarf2_initialize_objfile (struct objfile *objfile)
9291a0cd 5390{
976ca316
SM
5391 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5392 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5393
850ed749
SM
5394 dwarf_read_debug_printf ("called");
5395
9291a0cd
TT
5396 /* If we're about to read full symbols, don't bother with the
5397 indices. In this case we also don't care if some other debug
5398 format is making psymtabs, because they are all about to be
5399 expanded anyway. */
5400 if ((objfile->flags & OBJF_READNOW))
5401 {
850ed749
SM
5402 dwarf_read_debug_printf ("readnow requested");
5403
17ee85fc
TT
5404 /* When using READNOW, the using_index flag (set below) indicates that
5405 PER_BFD was already initialized, when we loaded some other objfile. */
5406 if (per_bfd->using_index)
5407 {
850ed749 5408 dwarf_read_debug_printf ("using_index already set");
976ca316 5409 per_objfile->resize_symtabs ();
edc02ceb
TT
5410 objfile->qf.push_front (make_dwarf_gdb_index ());
5411 return;
17ee85fc
TT
5412 }
5413
5414 per_bfd->using_index = 1;
976ca316 5415 create_all_comp_units (per_objfile);
17ee85fc
TT
5416 per_bfd->quick_file_names_table
5417 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5418 per_objfile->resize_symtabs ();
9291a0cd 5419
91eea9cc 5420 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
9291a0cd 5421 {
91eea9cc 5422 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
9291a0cd 5423
17ee85fc 5424 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5425 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5426 }
5427
edc02ceb
TT
5428 /* Arrange for gdb to see the "quick" functions. However, these
5429 functions will be no-ops because we will have expanded all
5430 symtabs. */
5431 objfile->qf.push_front (make_dwarf_gdb_index ());
5432 return;
9291a0cd
TT
5433 }
5434
17ee85fc
TT
5435 /* Was a debug names index already read when we processed an objfile sharing
5436 PER_BFD? */
5437 if (per_bfd->debug_names_table != nullptr)
5438 {
850ed749 5439 dwarf_read_debug_printf ("re-using shared debug names table");
976ca316 5440 per_objfile->resize_symtabs ();
edc02ceb
TT
5441 objfile->qf.push_front (make_dwarf_debug_names ());
5442 return;
17ee85fc
TT
5443 }
5444
5445 /* Was a GDB index already read when we processed an objfile sharing
5446 PER_BFD? */
5447 if (per_bfd->index_table != nullptr)
5448 {
850ed749 5449 dwarf_read_debug_printf ("re-using shared index table");
976ca316 5450 per_objfile->resize_symtabs ();
edc02ceb
TT
5451 objfile->qf.push_front (make_dwarf_gdb_index ());
5452 return;
17ee85fc
TT
5453 }
5454
efb763a5
SM
5455 /* There might already be partial symtabs built for this BFD. This happens
5456 when loading the same binary twice with the index-cache enabled. If so,
5457 don't try to read an index. The objfile / per_objfile initialization will
5458 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5459 code path. */
5460 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
5461 {
5462 dwarf_read_debug_printf ("re-using shared partial symtabs");
edc02ceb
TT
5463 objfile->qf.push_front (make_lazy_dwarf_reader ());
5464 return;
850ed749 5465 }
efb763a5 5466
976ca316 5467 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 5468 {
850ed749 5469 dwarf_read_debug_printf ("found debug names");
976ca316 5470 per_objfile->resize_symtabs ();
edc02ceb
TT
5471 objfile->qf.push_front (make_dwarf_debug_names ());
5472 return;
3c0aa29a 5473 }
927aa2e7 5474
976ca316 5475 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 5476 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5477 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 5478 {
850ed749 5479 dwarf_read_debug_printf ("found gdb index from file");
976ca316 5480 per_objfile->resize_symtabs ();
edc02ceb
TT
5481 objfile->qf.push_front (make_dwarf_gdb_index ());
5482 return;
3c0aa29a 5483 }
9291a0cd 5484
87d6a7aa 5485 /* ... otherwise, try to find the index in the index cache. */
976ca316 5486 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
5487 get_gdb_index_contents_from_cache,
5488 get_gdb_index_contents_from_cache_dwz))
5489 {
850ed749 5490 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa 5491 global_index_cache.hit ();
976ca316 5492 per_objfile->resize_symtabs ();
edc02ceb
TT
5493 objfile->qf.push_front (make_dwarf_gdb_index ());
5494 return;
87d6a7aa
SM
5495 }
5496
5497 global_index_cache.miss ();
edc02ceb 5498 objfile->qf.push_front (make_lazy_dwarf_reader ());
9291a0cd
TT
5499}
5500
5501\f
5502
dce234bc
PP
5503/* Build a partial symbol table. */
5504
5505void
eb36a3eb 5506dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
dce234bc 5507{
976ca316
SM
5508 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5509 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
5510
5511 if (per_bfd->partial_symtabs != nullptr)
5512 {
5513 /* Partial symbols were already read, so now we can simply
5514 attach them. */
eb36a3eb
TT
5515 if (psf == nullptr)
5516 {
5517 psf = new psymbol_functions (per_bfd->partial_symtabs);
5518 objfile->qf.emplace_front (psf);
5519 }
5520 else
5521 psf->set_partial_symtabs (per_bfd->partial_symtabs);
976ca316 5522 per_objfile->resize_symtabs ();
17ee85fc
TT
5523 return;
5524 }
c9bf0622 5525
eb36a3eb
TT
5526 if (psf == nullptr)
5527 {
5528 psf = new psymbol_functions;
5529 objfile->qf.emplace_front (psf);
5530 }
5531 const std::shared_ptr<psymtab_storage> &partial_symtabs
5532 = psf->get_partial_symtabs ();
5533
51962708
TT
5534 /* Set the local reference to partial symtabs, so that we don't try
5535 to read them again if reading another objfile with the same BFD.
5536 If we can't in fact share, this won't make a difference anyway as
5537 the dwarf2_per_bfd object won't be shared. */
eb36a3eb 5538 per_bfd->partial_symtabs = partial_symtabs;
51962708 5539
a70b8144 5540 try
c9bf0622
TT
5541 {
5542 /* This isn't really ideal: all the data we allocate on the
5543 objfile's obstack is still uselessly kept around. However,
5544 freeing it seems unsafe. */
eb36a3eb 5545 psymtab_discarder psymtabs (partial_symtabs.get ());
976ca316 5546 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 5547 psymtabs.keep ();
87d6a7aa 5548
976ca316 5549 per_objfile->resize_symtabs ();
af758d11 5550
87d6a7aa 5551 /* (maybe) store an index in the cache. */
976ca316 5552 global_index_cache.store (per_objfile);
c9bf0622 5553 }
230d2906 5554 catch (const gdb_exception_error &except)
492d29ea
PA
5555 {
5556 exception_print (gdb_stderr, except);
5557 }
c906108c 5558}
c906108c 5559
3b80fe9b
DE
5560/* Find the base address of the compilation unit for range lists and
5561 location lists. It will normally be specified by DW_AT_low_pc.
5562 In DWARF-3 draft 4, the base address could be overridden by
5563 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5564 compilation units with discontinuous ranges. */
5565
5566static void
5567dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5568{
5569 struct attribute *attr;
5570
2b24b6e4 5571 cu->base_address.reset ();
3b80fe9b
DE
5572
5573 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5574 if (attr != nullptr)
95f982e5 5575 cu->base_address = attr->as_address ();
3b80fe9b
DE
5576 else
5577 {
5578 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5579 if (attr != nullptr)
95f982e5 5580 cu->base_address = attr->as_address ();
3b80fe9b
DE
5581 }
5582}
5583
36586728
TT
5584/* Helper function that returns the proper abbrev section for
5585 THIS_CU. */
5586
5587static struct dwarf2_section_info *
5588get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5589{
5590 struct dwarf2_section_info *abbrev;
c3699833 5591 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
5592
5593 if (this_cu->is_dwz)
a7308ce0 5594 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 5595 else
c3699833 5596 abbrev = &per_bfd->abbrev;
36586728
TT
5597
5598 return abbrev;
5599}
5600
f4dc4d17
DE
5601/* Fetch the abbreviation table offset from a comp or type unit header. */
5602
5603static sect_offset
976ca316 5604read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 5605 struct dwarf2_section_info *section,
9c541725 5606 sect_offset sect_off)
f4dc4d17 5607{
96b79293 5608 bfd *abfd = section->get_bfd_owner ();
d521ce57 5609 const gdb_byte *info_ptr;
ac298888 5610 unsigned int initial_length_size, offset_size;
43988095 5611 uint16_t version;
f4dc4d17 5612
976ca316 5613 section->read (per_objfile->objfile);
9c541725 5614 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5615 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5616 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5617 info_ptr += initial_length_size;
5618
5619 version = read_2_bytes (abfd, info_ptr);
5620 info_ptr += 2;
5621 if (version >= 5)
5622 {
5623 /* Skip unit type and address size. */
5624 info_ptr += 2;
5625 }
5626
24aa364d 5627 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5628}
5629
b83470bf
TT
5630/* A partial symtab that is used only for include files. */
5631struct dwarf2_include_psymtab : public partial_symtab
5632{
7b249e47
TT
5633 dwarf2_include_psymtab (const char *filename,
5634 psymtab_storage *partial_symtabs,
0072c873
SM
5635 objfile_per_bfd_storage *objfile_per_bfd)
5636 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
b83470bf
TT
5637 {
5638 }
5639
5640 void read_symtab (struct objfile *objfile) override
5641 {
194d088f
TV
5642 /* It's an include file, no symbols to read for it.
5643 Everything is in the includer symtab. */
5644
5645 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5646 expansion of the includer psymtab. We use the dependencies[0] field to
5647 model the includer. But if we go the regular route of calling
5648 expand_psymtab here, and having expand_psymtab call expand_dependencies
5649 to expand the includer, we'll only use expand_psymtab on the includer
5650 (making it a non-toplevel psymtab), while if we expand the includer via
5651 another path, we'll use read_symtab (making it a toplevel psymtab).
5652 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5653 psymtab, and trigger read_symtab on the includer here directly. */
5654 includer ()->read_symtab (objfile);
b83470bf
TT
5655 }
5656
5657 void expand_psymtab (struct objfile *objfile) override
5658 {
194d088f
TV
5659 /* This is not called by read_symtab, and should not be called by any
5660 expand_dependencies. */
5661 gdb_assert (false);
b83470bf
TT
5662 }
5663
5717c425 5664 bool readin_p (struct objfile *objfile) const override
b83470bf 5665 {
5717c425 5666 return includer ()->readin_p (objfile);
b83470bf
TT
5667 }
5668
5717c425 5669 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
5670 {
5671 return nullptr;
5672 }
5673
5674private:
194d088f
TV
5675 partial_symtab *includer () const
5676 {
5677 /* An include psymtab has exactly one dependency: the psymtab that
5678 includes it. */
5679 gdb_assert (this->number_of_dependencies == 1);
5680 return this->dependencies[0];
5681 }
b83470bf
TT
5682};
5683
aaa75496
JB
5684/* Allocate a new partial symtab for file named NAME and mark this new
5685 partial symtab as being an include of PST. */
5686
5687static void
84685904 5688dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
7b249e47
TT
5689 const char *name,
5690 dwarf2_psymtab *pst,
5691 psymtab_storage *partial_symtabs,
0072c873 5692 objfile_per_bfd_storage *objfile_per_bfd)
aaa75496 5693{
7b249e47 5694 dwarf2_include_psymtab *subpst
0072c873 5695 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
aaa75496 5696
fbd9ab74 5697 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 5698 subpst->dirname = pst->dirname;
fbd9ab74 5699
84685904 5700 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5701 subpst->dependencies[0] = pst;
5702 subpst->number_of_dependencies = 1;
aaa75496
JB
5703}
5704
5705/* Read the Line Number Program data and extract the list of files
5706 included by the source file represented by PST. Build an include
d85a05f0 5707 partial symtab for each of these included files. */
aaa75496
JB
5708
5709static void
5710dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5711 struct die_info *die,
891813be 5712 dwarf2_psymtab *pst)
aaa75496 5713{
fff8551c 5714 line_header_up lh;
d85a05f0 5715 struct attribute *attr;
aaa75496 5716
d85a05f0 5717 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
5718 if (attr != nullptr && attr->form_is_unsigned ())
5719 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
5720 if (lh == NULL)
5721 return; /* No linetable, so no includes. */
5722
79748972
TT
5723 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5724 that we pass in the raw text_low here; that is ok because we're
5725 only decoding the line table to make include partial symtabs, and
5726 so the addresses aren't really used. */
4ae976d1 5727 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5728 pst->raw_text_low (), 1);
aaa75496
JB
5729}
5730
348e048f 5731static hashval_t
52dc124a 5732hash_signatured_type (const void *item)
348e048f 5733{
9a3c8263
SM
5734 const struct signatured_type *sig_type
5735 = (const struct signatured_type *) item;
9a619af0 5736
348e048f 5737 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5738 return sig_type->signature;
348e048f
DE
5739}
5740
5741static int
52dc124a 5742eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5743{
9a3c8263
SM
5744 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5745 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5746
348e048f
DE
5747 return lhs->signature == rhs->signature;
5748}
5749
1fd400ff
TT
5750/* Allocate a hash table for signatured types. */
5751
b0b6a987 5752static htab_up
298e9637 5753allocate_signatured_type_table ()
1fd400ff 5754{
b0b6a987
TT
5755 return htab_up (htab_create_alloc (41,
5756 hash_signatured_type,
5757 eq_signatured_type,
5758 NULL, xcalloc, xfree));
1fd400ff
TT
5759}
5760
78d4d2c5 5761/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5762 and fill them into TYPES_HTAB. It will process only type units,
5763 therefore DW_UT_type. */
c88ee1f0 5764
78d4d2c5 5765static void
976ca316 5766create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 5767 struct dwo_file *dwo_file,
b0b6a987 5768 dwarf2_section_info *section, htab_up &types_htab,
43988095 5769 rcuh_kind section_kind)
348e048f 5770{
976ca316 5771 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 5772 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
5773 bfd *abfd;
5774 const gdb_byte *info_ptr, *end_ptr;
348e048f 5775
b8efb248 5776 abbrev_section = &dwo_file->sections.abbrev;
4bdcc0c1 5777
4800761a 5778 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
5779 section->get_name (),
5780 abbrev_section->get_file_name ());
09406207 5781
96b79293 5782 section->read (objfile);
78d4d2c5 5783 info_ptr = section->buffer;
348e048f 5784
78d4d2c5
JK
5785 if (info_ptr == NULL)
5786 return;
348e048f 5787
78d4d2c5
JK
5788 /* We can't set abfd until now because the section may be empty or
5789 not present, in which case the bfd is unknown. */
96b79293 5790 abfd = section->get_bfd_owner ();
348e048f 5791
c0ab21c2
TT
5792 /* We don't use cutu_reader here because we don't need to read
5793 any dies: the signature is in the header. */
3019eac3 5794
78d4d2c5
JK
5795 end_ptr = info_ptr + section->size;
5796 while (info_ptr < end_ptr)
5797 {
46c6bcf6 5798 signatured_type_up sig_type;
78d4d2c5
JK
5799 struct dwo_unit *dwo_tu;
5800 void **slot;
5801 const gdb_byte *ptr = info_ptr;
5802 struct comp_unit_head header;
5803 unsigned int length;
8b70b953 5804
9c541725 5805 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 5806
a49dd8dd
JK
5807 /* Initialize it due to a false compiler warning. */
5808 header.signature = -1;
9c541725 5809 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 5810
78d4d2c5
JK
5811 /* We need to read the type's signature in order to build the hash
5812 table, but we don't need anything else just yet. */
348e048f 5813
976ca316 5814 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 5815 abbrev_section, ptr, section_kind);
348e048f 5816
4057dfde 5817 length = header.get_length ();
6caca83c 5818
78d4d2c5
JK
5819 /* Skip dummy type units. */
5820 if (ptr >= info_ptr + length
43988095 5821 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
5822 || (header.unit_type != DW_UT_type
5823 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
5824 {
5825 info_ptr += length;
5826 continue;
5827 }
dee91e82 5828
78d4d2c5 5829 if (types_htab == NULL)
b8efb248 5830 types_htab = allocate_dwo_unit_table ();
8b70b953 5831
b8efb248
TT
5832 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5833 dwo_tu->dwo_file = dwo_file;
5834 dwo_tu->signature = header.signature;
5835 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5836 dwo_tu->section = section;
5837 dwo_tu->sect_off = sect_off;
5838 dwo_tu->length = length;
78d4d2c5 5839
b8efb248 5840 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
78d4d2c5
JK
5841 gdb_assert (slot != NULL);
5842 if (*slot != NULL)
b8efb248
TT
5843 complaint (_("debug type entry at offset %s is duplicate to"
5844 " the entry at offset %s, signature %s"),
5845 sect_offset_str (sect_off),
5846 sect_offset_str (dwo_tu->sect_off),
5847 hex_string (header.signature));
5848 *slot = dwo_tu;
3019eac3 5849
6f738b01
SM
5850 dwarf_read_debug_printf_v (" offset %s, signature %s",
5851 sect_offset_str (sect_off),
5852 hex_string (header.signature));
3019eac3 5853
78d4d2c5
JK
5854 info_ptr += length;
5855 }
5856}
3019eac3 5857
78d4d2c5
JK
5858/* Create the hash table of all entries in the .debug_types
5859 (or .debug_types.dwo) section(s).
b8efb248 5860 DWO_FILE is a pointer to the DWO file object.
b3c8eb43 5861
78d4d2c5 5862 The result is a pointer to the hash table or NULL if there are no types.
348e048f 5863
78d4d2c5 5864 Note: This function processes DWO files only, not DWP files. */
348e048f 5865
78d4d2c5 5866static void
976ca316 5867create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 5868 struct dwo_file *dwo_file,
fd5866f6 5869 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 5870 htab_up &types_htab)
78d4d2c5 5871{
fd5866f6 5872 for (dwarf2_section_info &section : type_sections)
976ca316
SM
5873 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5874 rcuh_kind::TYPE);
3019eac3
DE
5875}
5876
5989a64e 5877/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
5878 If SLOT is non-NULL, it is the entry to use in the hash table.
5879 Otherwise we find one. */
5880
5881static struct signatured_type *
976ca316 5882add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 5883{
91eea9cc
TT
5884 if (per_objfile->per_bfd->all_comp_units.size ()
5885 == per_objfile->per_bfd->all_comp_units.capacity ())
976ca316 5886 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 5887
46c6bcf6 5888 signatured_type_up sig_type_holder
9ea36493 5889 = per_objfile->per_bfd->allocate_signatured_type (sig);
0d305d5c 5890 signatured_type *sig_type = sig_type_holder.get ();
b2bdb8cf 5891
976ca316 5892 per_objfile->resize_symtabs ();
af758d11 5893
473ab964
TT
5894 per_objfile->per_bfd->all_comp_units.emplace_back
5895 (sig_type_holder.release ());
976ca316 5896 if (per_objfile->per_bfd->using_index)
6aa5f3a6 5897 {
0d305d5c 5898 sig_type->v.quick =
976ca316 5899 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
5900 struct dwarf2_per_cu_quick_data);
5901 }
5902
5903 if (slot == NULL)
5904 {
976ca316 5905 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
5906 sig_type, INSERT);
5907 }
5908 gdb_assert (*slot == NULL);
5909 *slot = sig_type;
5910 /* The rest of sig_type must be filled in by the caller. */
5911 return sig_type;
5912}
5913
a2ce51a0
DE
5914/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5915 Fill in SIG_ENTRY with DWO_ENTRY. */
5916
5917static void
976ca316 5918fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
5919 struct signatured_type *sig_entry,
5920 struct dwo_unit *dwo_entry)
5921{
976ca316 5922 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 5923
7ee85ab1 5924 /* Make sure we're not clobbering something we don't expect to. */
0d305d5c
TT
5925 gdb_assert (! sig_entry->queued);
5926 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
1859c670 5927 if (per_bfd->using_index)
6aa5f3a6 5928 {
0d305d5c
TT
5929 gdb_assert (sig_entry->v.quick != NULL);
5930 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6aa5f3a6
DE
5931 }
5932 else
0d305d5c 5933 gdb_assert (sig_entry->v.psymtab == NULL);
a2ce51a0 5934 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5935 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5936 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5937 gdb_assert (sig_entry->dwo_unit == NULL);
5938
0d305d5c
TT
5939 sig_entry->section = dwo_entry->section;
5940 sig_entry->sect_off = dwo_entry->sect_off;
5941 sig_entry->length = dwo_entry->length;
5942 sig_entry->reading_dwo_directly = 1;
5943 sig_entry->per_bfd = per_bfd;
a2ce51a0
DE
5944 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5945 sig_entry->dwo_unit = dwo_entry;
5946}
5947
5948/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5949 If we haven't read the TU yet, create the signatured_type data structure
5950 for a TU to be read in directly from a DWO file, bypassing the stub.
5951 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5952 using .gdb_index, then when reading a CU we want to stay in the DWO file
5953 containing that CU. Otherwise we could end up reading several other DWO
5954 files (due to comdat folding) to process the transitive closure of all the
5955 mentioned TUs, and that can be slow. The current DWO file will have every
5956 type signature that it needs.
a2ce51a0
DE
5957 We only do this for .gdb_index because in the psymtab case we already have
5958 to read all the DWOs to build the type unit groups. */
5959
5960static struct signatured_type *
5961lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5962{
976ca316 5963 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
5964 struct dwo_file *dwo_file;
5965 struct dwo_unit find_dwo_entry, *dwo_entry;
6aa5f3a6 5966 void **slot;
a2ce51a0 5967
976ca316 5968 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 5969
6aa5f3a6
DE
5970 /* If TU skeletons have been removed then we may not have read in any
5971 TUs yet. */
976ca316
SM
5972 if (per_objfile->per_bfd->signatured_types == NULL)
5973 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
5974
5975 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5976 Use the global signatured_types array to do our own comdat-folding
5977 of types. If this is the first time we're reading this TU, and
5978 the TU has an entry in .gdb_index, replace the recorded data from
5979 .gdb_index with this TU. */
a2ce51a0 5980
9ea36493 5981 signatured_type find_sig_entry (sig);
976ca316 5982 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 5983 &find_sig_entry, INSERT);
9ea36493 5984 signatured_type *sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5985
5986 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5987 read. Don't reassign the global entry to point to this DWO if that's
5988 the case. Also note that if the TU is already being read, it may not
5989 have come from a DWO, the program may be a mix of Fission-compiled
5990 code and non-Fission-compiled code. */
5991
5992 /* Have we already tried to read this TU?
5993 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5994 needn't exist in the global table yet). */
0d305d5c 5995 if (sig_entry != NULL && sig_entry->tu_read)
a2ce51a0
DE
5996 return sig_entry;
5997
6aa5f3a6
DE
5998 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5999 dwo_unit of the TU itself. */
6000 dwo_file = cu->dwo_unit->dwo_file;
6001
a2ce51a0
DE
6002 /* Ok, this is the first time we're reading this TU. */
6003 if (dwo_file->tus == NULL)
6004 return NULL;
6005 find_dwo_entry.signature = sig;
b0b6a987
TT
6006 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6007 &find_dwo_entry);
a2ce51a0
DE
6008 if (dwo_entry == NULL)
6009 return NULL;
6010
6aa5f3a6
DE
6011 /* If the global table doesn't have an entry for this TU, add one. */
6012 if (sig_entry == NULL)
976ca316 6013 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6014
976ca316 6015 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
0d305d5c 6016 sig_entry->tu_read = 1;
a2ce51a0
DE
6017 return sig_entry;
6018}
6019
a2ce51a0
DE
6020/* Subroutine of lookup_signatured_type.
6021 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6022 then try the DWP file. If the TU stub (skeleton) has been removed then
6023 it won't be in .gdb_index. */
a2ce51a0
DE
6024
6025static struct signatured_type *
6026lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6027{
976ca316
SM
6028 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6029 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0 6030 struct dwo_unit *dwo_entry;
6aa5f3a6 6031 void **slot;
a2ce51a0 6032
976ca316 6033 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6034 gdb_assert (dwp_file != NULL);
6035
6aa5f3a6
DE
6036 /* If TU skeletons have been removed then we may not have read in any
6037 TUs yet. */
976ca316
SM
6038 if (per_objfile->per_bfd->signatured_types == NULL)
6039 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6040
9ea36493 6041 signatured_type find_sig_entry (sig);
976ca316 6042 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6043 &find_sig_entry, INSERT);
9ea36493 6044 signatured_type *sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6045
6046 /* Have we already tried to read this TU?
6047 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6048 needn't exist in the global table yet). */
6049 if (sig_entry != NULL)
6050 return sig_entry;
6051
a2ce51a0
DE
6052 if (dwp_file->tus == NULL)
6053 return NULL;
976ca316
SM
6054 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6055 1 /* is_debug_types */);
a2ce51a0
DE
6056 if (dwo_entry == NULL)
6057 return NULL;
6058
976ca316
SM
6059 sig_entry = add_type_unit (per_objfile, sig, slot);
6060 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6061
a2ce51a0
DE
6062 return sig_entry;
6063}
6064
380bca97 6065/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6066 Returns NULL if signature SIG is not present in the table.
6067 It is up to the caller to complain about this. */
348e048f
DE
6068
6069static struct signatured_type *
a2ce51a0 6070lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6071{
976ca316 6072 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6073
976ca316 6074 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6075 {
6076 /* We're in a DWO/DWP file, and we're using .gdb_index.
6077 These cases require special processing. */
976ca316 6078 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6079 return lookup_dwo_signatured_type (cu, sig);
6080 else
6081 return lookup_dwp_signatured_type (cu, sig);
6082 }
6083 else
6084 {
976ca316 6085 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0 6086 return NULL;
9ea36493
SM
6087 signatured_type find_entry (sig);
6088 return ((struct signatured_type *)
6089 htab_find (per_objfile->per_bfd->signatured_types.get (),
6090 &find_entry));
a2ce51a0 6091 }
348e048f 6092}
18a8505e 6093
42e7ad6c 6094/* Low level DIE reading support. */
348e048f 6095
d85a05f0
DJ
6096/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6097
6098static void
6099init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6100 struct dwarf2_cu *cu,
3019eac3 6101 struct dwarf2_section_info *section,
685af9cd
TT
6102 struct dwo_file *dwo_file,
6103 struct abbrev_table *abbrev_table)
d85a05f0 6104{
fceca515 6105 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6106 reader->abfd = section->get_bfd_owner ();
d85a05f0 6107 reader->cu = cu;
3019eac3 6108 reader->dwo_file = dwo_file;
dee91e82
DE
6109 reader->die_section = section;
6110 reader->buffer = section->buffer;
f664829e 6111 reader->buffer_end = section->buffer + section->size;
685af9cd 6112 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6113}
6114
c0ab21c2 6115/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6116 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6117 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6118 already.
6119
6120 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6121 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6122 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6123 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6124 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6125 STUB_COMP_DIR may be non-NULL.
3e225074 6126 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6127 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6128 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6129 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6130 kept around for at least as long as *RESULT_READER.
6131
b0c7bfa9
DE
6132 The result is non-zero if a valid (non-dummy) DIE was found. */
6133
6134static int
4ab09049 6135read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6136 struct dwo_unit *dwo_unit,
b0c7bfa9 6137 struct die_info *stub_comp_unit_die,
a2ce51a0 6138 const char *stub_comp_dir,
b0c7bfa9 6139 struct die_reader_specs *result_reader,
d521ce57 6140 const gdb_byte **result_info_ptr,
b0c7bfa9 6141 struct die_info **result_comp_unit_die,
685af9cd 6142 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6143{
976ca316 6144 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6145 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6146 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6147 bfd *abfd;
d521ce57 6148 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6149 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6150 int i,num_extra_attrs;
6151 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6152 struct die_info *comp_unit_die;
6153
b0aeadb3
DE
6154 /* At most one of these may be provided. */
6155 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6156
b0c7bfa9
DE
6157 /* These attributes aren't processed until later:
6158 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6159 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6160 referenced later. However, these attributes are found in the stub
6161 which we won't have later. In order to not impose this complication
6162 on the rest of the code, we read them here and copy them to the
6163 DWO CU/TU die. */
b0c7bfa9
DE
6164
6165 stmt_list = NULL;
6166 low_pc = NULL;
6167 high_pc = NULL;
6168 ranges = NULL;
6169 comp_dir = NULL;
6170
6171 if (stub_comp_unit_die != NULL)
6172 {
6173 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6174 DWO file. */
4ab09049 6175 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6176 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6177 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6178 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6179 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6180 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6181
a39fdb41 6182 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6183
2b0c7f41
SM
6184 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6185 We need the value before we can process DW_AT_ranges values from the
6186 DWO. */
6187 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6188
6189 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6190 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6191 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6192 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6193 section. */
6194 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6195 }
a2ce51a0
DE
6196 else if (stub_comp_dir != NULL)
6197 {
6198 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6199 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6200 comp_dir->name = DW_AT_comp_dir;
6201 comp_dir->form = DW_FORM_string;
c6481205 6202 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6203 }
b0c7bfa9
DE
6204
6205 /* Set up for reading the DWO CU/TU. */
6206 cu->dwo_unit = dwo_unit;
685af9cd 6207 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6208 section->read (objfile);
6209 abfd = section->get_bfd_owner ();
9c541725
PA
6210 begin_info_ptr = info_ptr = (section->buffer
6211 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6212 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6213
4ab09049 6214 if (per_cu->is_debug_types)
b0c7bfa9 6215 {
4ab09049 6216 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6217
976ca316
SM
6218 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6219 section, dwo_abbrev_section,
43988095 6220 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6221 /* This is not an assert because it can be caused by bad debug info. */
43988095 6222 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6223 {
6224 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6225 " TU at offset %s [in module %s]"),
a2ce51a0 6226 hex_string (sig_type->signature),
43988095 6227 hex_string (cu->header.signature),
9d8780f0 6228 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6229 bfd_get_filename (abfd));
6230 }
9c541725 6231 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6232 /* For DWOs coming from DWP files, we don't know the CU length
6233 nor the type's offset in the TU until now. */
4057dfde 6234 dwo_unit->length = cu->header.get_length ();
9c541725 6235 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6236
6237 /* Establish the type offset that can be used to lookup the type.
6238 For DWO files, we don't know it until now. */
9c541725
PA
6239 sig_type->type_offset_in_section
6240 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6241 }
6242 else
6243 {
976ca316
SM
6244 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6245 section, dwo_abbrev_section,
43988095 6246 info_ptr, rcuh_kind::COMPILE);
9c541725 6247 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6248 /* For DWOs coming from DWP files, we don't know the CU length
6249 until now. */
4057dfde 6250 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6251 }
6252
606decb2 6253 dwo_abbrev_section->read (objfile);
685af9cd 6254 *result_dwo_abbrev_table
606decb2 6255 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6256 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6257 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6258
6259 /* Read in the die, but leave space to copy over the attributes
6260 from the stub. This has the benefit of simplifying the rest of
6261 the code - all the work to maintain the illusion of a single
6262 DW_TAG_{compile,type}_unit DIE is done here. */
6263 num_extra_attrs = ((stmt_list != NULL)
6264 + (low_pc != NULL)
6265 + (high_pc != NULL)
6266 + (ranges != NULL)
6267 + (comp_dir != NULL));
6268 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6269 num_extra_attrs);
b0c7bfa9
DE
6270
6271 /* Copy over the attributes from the stub to the DIE we just read in. */
6272 comp_unit_die = *result_comp_unit_die;
6273 i = comp_unit_die->num_attrs;
6274 if (stmt_list != NULL)
6275 comp_unit_die->attrs[i++] = *stmt_list;
6276 if (low_pc != NULL)
6277 comp_unit_die->attrs[i++] = *low_pc;
6278 if (high_pc != NULL)
6279 comp_unit_die->attrs[i++] = *high_pc;
6280 if (ranges != NULL)
6281 comp_unit_die->attrs[i++] = *ranges;
6282 if (comp_dir != NULL)
6283 comp_unit_die->attrs[i++] = *comp_dir;
6284 comp_unit_die->num_attrs += num_extra_attrs;
6285
b4f54984 6286 if (dwarf_die_debug)
bf6af496
DE
6287 {
6288 fprintf_unfiltered (gdb_stdlog,
6289 "Read die from %s@0x%x of %s:\n",
96b79293 6290 section->get_name (),
bf6af496
DE
6291 (unsigned) (begin_info_ptr - section->buffer),
6292 bfd_get_filename (abfd));
b4f54984 6293 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6294 }
6295
b0c7bfa9
DE
6296 /* Skip dummy compilation units. */
6297 if (info_ptr >= begin_info_ptr + dwo_unit->length
6298 || peek_abbrev_code (abfd, info_ptr) == 0)
6299 return 0;
6300
6301 *result_info_ptr = info_ptr;
6302 return 1;
6303}
6304
a084a2a6
AT
6305/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6306 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6307 signature is part of the header. */
6308static gdb::optional<ULONGEST>
6309lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6310{
6311 if (cu->header.version >= 5)
6312 return cu->header.signature;
6313 struct attribute *attr;
6314 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 6315 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 6316 return gdb::optional<ULONGEST> ();
529908cb 6317 return attr->as_unsigned ();
a084a2a6
AT
6318}
6319
c0ab21c2 6320/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6321 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6322 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6323
6324static struct dwo_unit *
4ab09049 6325lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6326{
4ab09049 6327 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6328 struct dwo_unit *dwo_unit;
c0ab21c2 6329 const char *comp_dir;
b0c7bfa9 6330
a2ce51a0
DE
6331 gdb_assert (cu != NULL);
6332
b0c7bfa9 6333 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6334 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6335 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6336
4ab09049
SM
6337 if (per_cu->is_debug_types)
6338 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6339 else
6340 {
a084a2a6 6341 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6342
a084a2a6 6343 if (!signature.has_value ())
b0c7bfa9
DE
6344 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6345 " [in module %s]"),
4ab09049
SM
6346 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6347
6348 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6349 }
6350
b0c7bfa9
DE
6351 return dwo_unit;
6352}
6353
c0ab21c2 6354/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6355 See it for a description of the parameters.
fcd3b13d 6356 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6357
c0ab21c2 6358void
9e021579
SM
6359cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6360 dwarf2_per_objfile *per_objfile,
2e671100 6361 dwarf2_cu *existing_cu)
a2ce51a0 6362{
a2ce51a0 6363 struct signatured_type *sig_type;
a2ce51a0
DE
6364
6365 /* Verify we can do the following downcast, and that we have the
6366 data we need. */
6367 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6368 sig_type = (struct signatured_type *) this_cu;
6369 gdb_assert (sig_type->dwo_unit != NULL);
6370
2e671100
SM
6371 dwarf2_cu *cu;
6372
6373 if (existing_cu != nullptr)
6aa5f3a6 6374 {
2e671100
SM
6375 cu = existing_cu;
6376 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6377 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6378 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6379 }
6380 else
6381 {
7188ed02 6382 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6383 in per_objfile yet. */
7188ed02 6384 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6385 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6386 cu = m_new_cu.get ();
6aa5f3a6
DE
6387 }
6388
6389 /* A future optimization, if needed, would be to use an existing
6390 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6391 could share abbrev tables. */
a2ce51a0 6392
2e671100 6393 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6394 NULL /* stub_comp_unit_die */,
6395 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6396 this, &info_ptr,
3e225074 6397 &comp_unit_die,
c0ab21c2 6398 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6399 {
6400 /* Dummy die. */
c0ab21c2 6401 dummy_p = true;
a2ce51a0 6402 }
a2ce51a0
DE
6403}
6404
fd820528 6405/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6406 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6407
f4dc4d17
DE
6408 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6409 Otherwise the table specified in the comp unit header is read in and used.
6410 This is an optimization for when we already have the abbrev table.
6411
2e671100
SM
6412 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6413 allocated. */
aaa75496 6414
ab432490 6415cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6416 dwarf2_per_objfile *per_objfile,
c0ab21c2 6417 struct abbrev_table *abbrev_table,
2e671100 6418 dwarf2_cu *existing_cu,
c0ab21c2
TT
6419 bool skip_partial)
6420 : die_reader_specs {},
6751ebae 6421 m_this_cu (this_cu)
c906108c 6422{
976ca316 6423 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6424 struct dwarf2_section_info *section = this_cu->section;
96b79293 6425 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 6426 const gdb_byte *begin_info_ptr;
dee91e82 6427 struct signatured_type *sig_type = NULL;
4bdcc0c1 6428 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6429 /* Non-zero if CU currently points to a DWO file and we need to
6430 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6431 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6432 int rereading_dwo_cu = 0;
c906108c 6433
b4f54984 6434 if (dwarf_die_debug)
9d8780f0 6435 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6436 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6437 sect_offset_str (this_cu->sect_off));
09406207 6438
a2ce51a0
DE
6439 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6440 file (instead of going through the stub), short-circuit all of this. */
6441 if (this_cu->reading_dwo_directly)
6442 {
6443 /* Narrow down the scope of possibilities to have to understand. */
6444 gdb_assert (this_cu->is_debug_types);
6445 gdb_assert (abbrev_table == NULL);
976ca316 6446 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
6447 return;
6448 }
6449
dee91e82 6450 /* This is cheap if the section is already read in. */
96b79293 6451 section->read (objfile);
dee91e82 6452
9c541725 6453 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6454
6455 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 6456
2e671100
SM
6457 dwarf2_cu *cu;
6458
6459 if (existing_cu != nullptr)
dee91e82 6460 {
2e671100 6461 cu = existing_cu;
42e7ad6c
DE
6462 /* If this CU is from a DWO file we need to start over, we need to
6463 refetch the attributes from the skeleton CU.
6464 This could be optimized by retrieving those attributes from when we
6465 were here the first time: the previous comp_unit_die was stored in
6466 comp_unit_obstack. But there's no data yet that we need this
6467 optimization. */
6468 if (cu->dwo_unit != NULL)
6469 rereading_dwo_cu = 1;
dee91e82
DE
6470 }
6471 else
6472 {
7188ed02 6473 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6474 in per_objfile yet. */
976ca316
SM
6475 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6476 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 6477 cu = m_new_cu.get ();
42e7ad6c 6478 }
dee91e82 6479
b0c7bfa9 6480 /* Get the header. */
9c541725 6481 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6482 {
6483 /* We already have the header, there's no need to read it in again. */
9c541725 6484 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6485 }
6486 else
6487 {
3019eac3 6488 if (this_cu->is_debug_types)
dee91e82 6489 {
976ca316
SM
6490 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6491 section, abbrev_section,
6492 info_ptr, rcuh_kind::TYPE);
dee91e82 6493
42e7ad6c
DE
6494 /* Since per_cu is the first member of struct signatured_type,
6495 we can go from a pointer to one to a pointer to the other. */
6496 sig_type = (struct signatured_type *) this_cu;
43988095 6497 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6498 gdb_assert (sig_type->type_offset_in_tu
6499 == cu->header.type_cu_offset_in_tu);
6500 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6501
42e7ad6c
DE
6502 /* LENGTH has not been set yet for type units if we're
6503 using .gdb_index. */
4057dfde 6504 this_cu->length = cu->header.get_length ();
3019eac3
DE
6505
6506 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6507 sig_type->type_offset_in_section =
6508 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6509
6510 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6511 }
6512 else
6513 {
976ca316
SM
6514 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6515 section, abbrev_section,
43988095
JK
6516 info_ptr,
6517 rcuh_kind::COMPILE);
dee91e82 6518
9c541725 6519 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
6520 if (this_cu->length == 0)
6521 this_cu->length = cu->header.get_length ();
6522 else
6523 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6524 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6525 }
6526 }
10b3939b 6527
6caca83c 6528 /* Skip dummy compilation units. */
dee91e82 6529 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6530 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6531 {
6532 dummy_p = true;
6533 return;
6534 }
6caca83c 6535
433df2d4
DE
6536 /* If we don't have them yet, read the abbrevs for this compilation unit.
6537 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6538 done. */
f4dc4d17 6539 if (abbrev_table != NULL)
685af9cd
TT
6540 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6541 else
f4dc4d17 6542 {
606decb2 6543 abbrev_section->read (objfile);
c0ab21c2 6544 m_abbrev_table_holder
606decb2 6545 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 6546 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6547 }
af703f96 6548
dee91e82 6549 /* Read the top level CU/TU die. */
c0ab21c2 6550 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6551 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6552
58f0c718 6553 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6554 {
6555 dummy_p = true;
6556 return;
6557 }
58f0c718 6558
b0c7bfa9 6559 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6560 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6561 table from the DWO file and pass the ownership over to us. It will be
6562 referenced from READER, so we must make sure to free it after we're done
6563 with READER.
6564
b0c7bfa9
DE
6565 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6566 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6567 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6568 if (dwo_name != nullptr)
3019eac3 6569 {
3019eac3 6570 struct dwo_unit *dwo_unit;
b0c7bfa9 6571 struct die_info *dwo_comp_unit_die;
3019eac3 6572
3e225074 6573 if (comp_unit_die->has_children)
6a506a2d 6574 {
b98664d3 6575 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6576 " has children (offset %s) [in module %s]"),
6577 sect_offset_str (this_cu->sect_off),
6578 bfd_get_filename (abfd));
6a506a2d 6579 }
4ab09049 6580 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 6581 if (dwo_unit != NULL)
3019eac3 6582 {
4ab09049 6583 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 6584 comp_unit_die, NULL,
c0ab21c2 6585 this, &info_ptr,
3e225074 6586 &dwo_comp_unit_die,
c0ab21c2 6587 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6588 {
6589 /* Dummy die. */
c0ab21c2 6590 dummy_p = true;
6a506a2d
DE
6591 return;
6592 }
6593 comp_unit_die = dwo_comp_unit_die;
6594 }
6595 else
6596 {
6597 /* Yikes, we couldn't find the rest of the DIE, we only have
6598 the stub. A complaint has already been logged. There's
6599 not much more we can do except pass on the stub DIE to
6600 die_reader_func. We don't want to throw an error on bad
6601 debug info. */
3019eac3
DE
6602 }
6603 }
c0ab21c2 6604}
3019eac3 6605
6751ebae
TT
6606void
6607cutu_reader::keep ()
c0ab21c2 6608{
b0c7bfa9 6609 /* Done, clean up. */
6751ebae
TT
6610 gdb_assert (!dummy_p);
6611 if (m_new_cu != NULL)
348e048f 6612 {
7188ed02 6613 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 6614 now. */
7188ed02
SM
6615 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6616 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 6617 }
dee91e82
DE
6618}
6619
18a8505e
AT
6620/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6621 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6622 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6623
6624 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6625 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6626
6627 We fill in THIS_CU->length.
6628
dee91e82 6629 THIS_CU->cu is always freed when done.
3019eac3 6630 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6631 to care whether it refers to the "main" CU or the DWO CU.
6632
6633 When parent_cu is passed, it is used to provide a default value for
6634 str_offsets_base and addr_base from the parent. */
dee91e82 6635
ab432490 6636cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6637 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
6638 struct dwarf2_cu *parent_cu,
6639 struct dwo_file *dwo_file)
6640 : die_reader_specs {},
6641 m_this_cu (this_cu)
dee91e82 6642{
976ca316 6643 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6644 struct dwarf2_section_info *section = this_cu->section;
96b79293 6645 bfd *abfd = section->get_bfd_owner ();
33e80786 6646 struct dwarf2_section_info *abbrev_section;
d521ce57 6647 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6648
b4f54984 6649 if (dwarf_die_debug)
9d8780f0 6650 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6651 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6652 sect_offset_str (this_cu->sect_off));
09406207 6653
976ca316 6654 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 6655
33e80786
DE
6656 abbrev_section = (dwo_file != NULL
6657 ? &dwo_file->sections.abbrev
6658 : get_abbrev_section_for_cu (this_cu));
6659
dee91e82 6660 /* This is cheap if the section is already read in. */
96b79293 6661 section->read (objfile);
dee91e82 6662
976ca316 6663 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 6664
9c541725 6665 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
6666 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6667 section, abbrev_section, info_ptr,
43988095
JK
6668 (this_cu->is_debug_types
6669 ? rcuh_kind::TYPE
6670 : rcuh_kind::COMPILE));
dee91e82 6671
18a8505e
AT
6672 if (parent_cu != nullptr)
6673 {
c0ab21c2
TT
6674 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6675 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 6676 }
4057dfde 6677 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
6678
6679 /* Skip dummy compilation units. */
6680 if (info_ptr >= begin_info_ptr + this_cu->length
6681 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6682 {
6683 dummy_p = true;
6684 return;
6685 }
72bf9492 6686
606decb2 6687 abbrev_section->read (objfile);
c0ab21c2 6688 m_abbrev_table_holder
606decb2 6689 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 6690
c0ab21c2
TT
6691 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6692 m_abbrev_table_holder.get ());
3e225074 6693 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
6694}
6695
0018ea6f
DE
6696\f
6697/* Type Unit Groups.
dee91e82 6698
0018ea6f
DE
6699 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6700 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6701 so that all types coming from the same compilation (.o file) are grouped
6702 together. A future step could be to put the types in the same symtab as
6703 the CU the types ultimately came from. */
ff013f42 6704
f4dc4d17
DE
6705static hashval_t
6706hash_type_unit_group (const void *item)
6707{
9a3c8263
SM
6708 const struct type_unit_group *tu_group
6709 = (const struct type_unit_group *) item;
f4dc4d17 6710
094b34ac 6711 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6712}
348e048f
DE
6713
6714static int
f4dc4d17 6715eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6716{
9a3c8263
SM
6717 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6718 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6719
094b34ac 6720 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6721}
348e048f 6722
f4dc4d17
DE
6723/* Allocate a hash table for type unit groups. */
6724
eaa5fa8b 6725static htab_up
298e9637 6726allocate_type_unit_groups_table ()
f4dc4d17 6727{
eaa5fa8b
TT
6728 return htab_up (htab_create_alloc (3,
6729 hash_type_unit_group,
6730 eq_type_unit_group,
ef5f598c 6731 htab_delete_entry<type_unit_group>,
0d305d5c 6732 xcalloc, xfree));
f4dc4d17 6733}
dee91e82 6734
f4dc4d17
DE
6735/* Type units that don't have DW_AT_stmt_list are grouped into their own
6736 partial symtabs. We combine several TUs per psymtab to not let the size
6737 of any one psymtab grow too big. */
6738#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6739#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6740
094b34ac 6741/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6742 Create the type_unit_group object used to hold one or more TUs. */
6743
0d305d5c 6744static std::unique_ptr<type_unit_group>
094b34ac 6745create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 6746{
976ca316
SM
6747 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6748 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 6749
0d305d5c
TT
6750 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6751 tu_group->per_bfd = per_bfd;
f4dc4d17 6752
1859c670 6753 if (per_bfd->using_index)
094b34ac 6754 {
0d305d5c
TT
6755 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6756 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6757 }
6758 else
6759 {
9c541725 6760 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 6761 dwarf2_psymtab *pst;
528e1572 6762 std::string name;
094b34ac
DE
6763
6764 /* Give the symtab a useful name for debug purposes. */
6765 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
6766 name = string_printf ("<type_units_%d>",
6767 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 6768 else
528e1572 6769 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 6770
0d305d5c
TT
6771 pst = create_partial_symtab (tu_group.get (), per_objfile,
6772 name.c_str ());
6d94535f 6773 pst->anonymous = true;
094b34ac 6774 }
f4dc4d17 6775
094b34ac 6776 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6777 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6778
6779 return tu_group;
6780}
6781
094b34ac
DE
6782/* Look up the type_unit_group for type unit CU, and create it if necessary.
6783 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6784
6785static struct type_unit_group *
ff39bb5e 6786get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 6787{
976ca316
SM
6788 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6789 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
6790 struct type_unit_group *tu_group;
6791 void **slot;
6792 unsigned int line_offset;
6793 struct type_unit_group type_unit_group_for_lookup;
6794
976ca316
SM
6795 if (per_objfile->per_bfd->type_unit_groups == NULL)
6796 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
6797
6798 /* Do we need to create a new group, or can we use an existing one? */
6799
529908cb 6800 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 6801 {
529908cb 6802 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
6803 ++tu_stats->nr_symtab_sharers;
6804 }
6805 else
6806 {
6807 /* Ugh, no stmt_list. Rare, but we have to handle it.
6808 We can do various things here like create one group per TU or
6809 spread them over multiple groups to split up the expansion work.
6810 To avoid worst case scenarios (too many groups or too large groups)
6811 we, umm, group them in bunches. */
6812 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6813 | (tu_stats->nr_stmt_less_type_units
6814 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6815 ++tu_stats->nr_stmt_less_type_units;
6816 }
6817
094b34ac 6818 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6819 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 6820 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17 6821 &type_unit_group_for_lookup, INSERT);
0d305d5c 6822 if (*slot == nullptr)
f4dc4d17 6823 {
9c541725 6824 sect_offset line_offset_struct = (sect_offset) line_offset;
0d305d5c
TT
6825 std::unique_ptr<type_unit_group> grp
6826 = create_type_unit_group (cu, line_offset_struct);
6827 *slot = grp.release ();
f4dc4d17
DE
6828 ++tu_stats->nr_symtabs;
6829 }
6830
0d305d5c
TT
6831 tu_group = (struct type_unit_group *) *slot;
6832 gdb_assert (tu_group != nullptr);
f4dc4d17
DE
6833 return tu_group;
6834}
0018ea6f
DE
6835\f
6836/* Partial symbol tables. */
6837
6838/* Create a psymtab named NAME and assign it to PER_CU.
6839
6840 The caller must fill in the following details:
6841 dirname, textlow, texthigh. */
6842
891813be 6843static dwarf2_psymtab *
7aa104c4
SM
6844create_partial_symtab (dwarf2_per_cu_data *per_cu,
6845 dwarf2_per_objfile *per_objfile,
6846 const char *name)
0018ea6f 6847{
0072c873
SM
6848 dwarf2_psymtab *pst
6849 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6850 per_objfile->objfile->per_bfd, per_cu);
0018ea6f 6851
6d94535f 6852 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
6853
6854 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
6855 per_cu->v.psymtab = pst;
6856
6857 return pst;
6858}
6859
c0ab21c2 6860/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
6861
6862static void
6863process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6864 const gdb_byte *info_ptr,
0018ea6f 6865 struct die_info *comp_unit_die,
c0ab21c2 6866 enum language pretend_language)
0018ea6f
DE
6867{
6868 struct dwarf2_cu *cu = reader->cu;
7aa104c4 6869 dwarf2_per_objfile *per_objfile = cu->per_objfile;
84685904 6870 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7aa104c4 6871 struct objfile *objfile = per_objfile->objfile;
08feed99 6872 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 6873 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6874 CORE_ADDR baseaddr;
6875 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 6876 dwarf2_psymtab *pst;
3a2b436a 6877 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6878 const char *filename;
0018ea6f 6879
0018ea6f
DE
6880 gdb_assert (! per_cu->is_debug_types);
6881
c0ab21c2 6882 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 6883
0018ea6f 6884 /* Allocate a new partial symbol table structure. */
2e927613
TV
6885 gdb::unique_xmalloc_ptr<char> debug_filename;
6886 static const char artificial[] = "<artificial>";
7d45c7c3
KB
6887 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6888 if (filename == NULL)
0018ea6f 6889 filename = "";
2e927613
TV
6890 else if (strcmp (filename, artificial) == 0)
6891 {
6892 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
6893 sect_offset_str (per_cu->sect_off),
6894 (char *) NULL));
2e927613
TV
6895 filename = debug_filename.get ();
6896 }
0018ea6f 6897
7aa104c4 6898 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
6899
6900 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6901 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 6902
b3b3bada 6903 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
6904
6905 dwarf2_find_base_address (comp_unit_die, cu);
6906
6907 /* Possibly set the default values of LOWPC and HIGHPC from
6908 `DW_AT_ranges'. */
3a2b436a
JK
6909 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6910 &best_highpc, cu, pst);
6911 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
6912 {
6913 CORE_ADDR low
6914 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
6915 - baseaddr);
6916 CORE_ADDR high
6917 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
6918 - baseaddr - 1);
6919 /* Store the contiguous range if it is not empty; it can be
6920 empty for CUs with no code. */
84685904 6921 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 6922 low, high, pst);
79748972 6923 }
0018ea6f
DE
6924
6925 /* Check if comp unit has_children.
6926 If so, read the rest of the partial symbols from this comp unit.
6927 If not, there's no more debug_info for this comp unit. */
3e225074 6928 if (comp_unit_die->has_children)
0018ea6f
DE
6929 {
6930 struct partial_die_info *first_die;
6931 CORE_ADDR lowpc, highpc;
6932
6933 lowpc = ((CORE_ADDR) -1);
6934 highpc = ((CORE_ADDR) 0);
6935
6936 first_die = load_partial_dies (reader, info_ptr, 1);
6937
6938 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6939 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6940
6941 /* If we didn't find a lowpc, set it to highpc to avoid
6942 complaints from `maint check'. */
6943 if (lowpc == ((CORE_ADDR) -1))
6944 lowpc = highpc;
6945
6946 /* If the compilation unit didn't have an explicit address range,
6947 then use the information extracted from its child dies. */
e385593e 6948 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6949 {
6950 best_lowpc = lowpc;
6951 best_highpc = highpc;
6952 }
6953 }
4ae976d1 6954 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
6955 best_lowpc + baseaddr)
6956 - baseaddr);
4ae976d1 6957 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
6958 best_highpc + baseaddr)
6959 - baseaddr);
0018ea6f 6960
ae7754b2 6961 pst->end ();
0018ea6f 6962
ae640021 6963 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
6964 {
6965 int i;
ae640021 6966 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
6967
6968 /* Fill in 'dependencies' here; we fill in 'users' in a
6969 post-pass. */
6970 pst->number_of_dependencies = len;
a9342b62 6971 pst->dependencies
84685904 6972 = per_bfd->partial_symtabs->allocate_dependencies (len);
ae640021
AB
6973 for (i = 0; i < len; ++i)
6974 {
6975 pst->dependencies[i]
6976 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
6977 }
0018ea6f 6978
ae640021 6979 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
6980 }
6981
6982 /* Get the list of files included in the current compilation unit,
6983 and build a psymtab for each of them. */
6984 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6985
6f738b01
SM
6986 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
6987 ", %d global, %d static syms",
6988 per_cu->is_debug_types ? "type" : "comp",
6989 sect_offset_str (per_cu->sect_off),
6990 paddress (gdbarch, pst->text_low (objfile)),
6991 paddress (gdbarch, pst->text_high (objfile)),
6992 (int) pst->global_psymbols.size (),
6993 (int) pst->static_psymbols.size ());
0018ea6f
DE
6994}
6995
6996/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6997 Process compilation unit THIS_CU for a psymtab. */
6998
6999static void
ab432490
SM
7000process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7001 dwarf2_per_objfile *per_objfile,
135f5437 7002 bool want_partial_unit,
b93601f3 7003 enum language pretend_language)
0018ea6f
DE
7004{
7005 /* If this compilation unit was already read in, free the
7006 cached copy in order to read it in again. This is
7007 necessary because we skipped some symbols when we first
7008 read in the compilation unit (see load_partial_dies).
7009 This problem could be avoided, but the benefit is unclear. */
7188ed02 7010 per_objfile->remove_cu (this_cu);
0018ea6f 7011
2e671100 7012 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7013
248f7165
TV
7014 if (reader.comp_unit_die == nullptr)
7015 return;
7016
58990295
TV
7017 switch (reader.comp_unit_die->tag)
7018 {
7019 case DW_TAG_compile_unit:
7020 this_cu->unit_type = DW_UT_compile;
7021 break;
7022 case DW_TAG_partial_unit:
7023 this_cu->unit_type = DW_UT_partial;
7024 break;
e77b0004
TV
7025 case DW_TAG_type_unit:
7026 this_cu->unit_type = DW_UT_type;
7027 break;
58990295 7028 default:
702cf3f5
AB
7029 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7030 dwarf_tag_name (reader.comp_unit_die->tag),
7031 sect_offset_str (reader.cu->per_cu->sect_off),
7032 objfile_name (per_objfile->objfile));
58990295
TV
7033 }
7034
c0ab21c2 7035 if (reader.dummy_p)
f1902523 7036 {
c0ab21c2 7037 /* Nothing. */
f1902523 7038 }
c0ab21c2 7039 else if (this_cu->is_debug_types)
3e225074
TT
7040 build_type_psymtabs_reader (&reader, reader.info_ptr,
7041 reader.comp_unit_die);
135f5437
TT
7042 else if (want_partial_unit
7043 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7044 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7045 reader.comp_unit_die,
c0ab21c2 7046 pretend_language);
0018ea6f 7047
7188ed02 7048 this_cu->lang = reader.cu->language;
58990295 7049
0018ea6f 7050 /* Age out any secondary CUs. */
7188ed02 7051 per_objfile->age_comp_units ();
0018ea6f 7052}
f4dc4d17
DE
7053
7054/* Reader function for build_type_psymtabs. */
7055
7056static void
7057build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7058 const gdb_byte *info_ptr,
3e225074 7059 struct die_info *type_unit_die)
f4dc4d17 7060{
976ca316 7061 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7062 struct dwarf2_cu *cu = reader->cu;
7063 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7064 struct signatured_type *sig_type;
f4dc4d17
DE
7065 struct type_unit_group *tu_group;
7066 struct attribute *attr;
7067 struct partial_die_info *first_die;
7068 CORE_ADDR lowpc, highpc;
891813be 7069 dwarf2_psymtab *pst;
f4dc4d17 7070
0186c6a7
DE
7071 gdb_assert (per_cu->is_debug_types);
7072 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7073
3e225074 7074 if (! type_unit_die->has_children)
f4dc4d17
DE
7075 return;
7076
052c8bb8 7077 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7078 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7079
df07e2c7 7080 if (tu_group->tus == nullptr)
a8b3b8e9 7081 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7082 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7083
7084 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7085 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7086 pst->anonymous = true;
f4dc4d17
DE
7087
7088 first_die = load_partial_dies (reader, info_ptr, 1);
7089
7090 lowpc = (CORE_ADDR) -1;
7091 highpc = (CORE_ADDR) 0;
7092 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7093
ae7754b2 7094 pst->end ();
f4dc4d17
DE
7095}
7096
73051182
DE
7097/* Struct used to sort TUs by their abbreviation table offset. */
7098
7099struct tu_abbrev_offset
7100{
b2bdb8cf
SM
7101 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7102 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7103 {}
7104
16e0020b 7105 /* This is used when sorting. */
5433e20e 7106 bool operator< (const tu_abbrev_offset &other) const
16e0020b
TT
7107 {
7108 return abbrev_offset < other.abbrev_offset;
7109 }
7110
b2bdb8cf 7111 signatured_type *sig_type;
73051182
DE
7112 sect_offset abbrev_offset;
7113};
7114
73051182 7115/* Efficiently read all the type units.
73051182
DE
7116
7117 The efficiency is because we sort TUs by the abbrev table they use and
7118 only read each abbrev table once. In one program there are 200K TUs
7119 sharing 8K abbrev tables.
7120
7121 The main purpose of this function is to support building the
5989a64e 7122 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7123 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7124 can collapse the search space by grouping them by stmt_list.
7125 The savings can be significant, in the same program from above the 200K TUs
7126 share 8K stmt_list tables.
7127
7128 FUNC is expected to call get_type_unit_group, which will create the
7129 struct type_unit_group if necessary and add it to
5989a64e 7130 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7131
7132static void
b8efb248 7133build_type_psymtabs (dwarf2_per_objfile *per_objfile)
73051182 7134{
976ca316 7135 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7136 abbrev_table_up abbrev_table;
73051182 7137 sect_offset abbrev_offset;
73051182
DE
7138
7139 /* It's up to the caller to not call us multiple times. */
976ca316 7140 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7141
91eea9cc 7142 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
73051182
DE
7143 return;
7144
7145 /* TUs typically share abbrev tables, and there can be way more TUs than
7146 abbrev tables. Sort by abbrev table to reduce the number of times we
7147 read each abbrev table in.
7148 Alternatives are to punt or to maintain a cache of abbrev tables.
7149 This is simpler and efficient enough for now.
7150
7151 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7152 symtab to use). Typically TUs with the same abbrev offset have the same
7153 stmt_list value too so in practice this should work well.
7154
7155 The basic algorithm here is:
7156
7157 sort TUs by abbrev table
7158 for each TU with same abbrev table:
7159 read abbrev table if first user
7160 read TU top level DIE
7161 [IWBN if DWO skeletons had DW_AT_stmt_list]
7162 call FUNC */
7163
6f738b01 7164 dwarf_read_debug_printf ("Building type unit groups ...");
73051182 7165
91eea9cc 7166 /* Sort in a separate table to maintain the order of all_comp_units
73051182 7167 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7168 std::vector<tu_abbrev_offset> sorted_by_abbrev;
91eea9cc 7169 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
b2bdb8cf 7170
91eea9cc
TT
7171 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7172 {
7173 if (cu->is_debug_types)
7174 {
7175 auto sig_type = static_cast<signatured_type *> (cu.get ());
7176 sorted_by_abbrev.emplace_back
7177 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7178 sig_type->sect_off));
7179 }
7180 }
73051182 7181
16e0020b 7182 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
73051182 7183
9c541725 7184 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7185
b2bdb8cf 7186 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7187 {
73051182
DE
7188 /* Switch to the next abbrev table if necessary. */
7189 if (abbrev_table == NULL
b2bdb8cf 7190 || tu.abbrev_offset != abbrev_offset)
73051182 7191 {
b2bdb8cf 7192 abbrev_offset = tu.abbrev_offset;
606decb2 7193 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7194 abbrev_table =
606decb2 7195 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7196 ++tu_stats->nr_uniq_abbrev_tables;
7197 }
7198
0d305d5c 7199 cutu_reader reader (tu.sig_type, per_objfile,
2e671100 7200 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7201 if (!reader.dummy_p)
7202 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7203 reader.comp_unit_die);
73051182 7204 }
6aa5f3a6 7205}
73051182 7206
6aa5f3a6
DE
7207/* Print collected type unit statistics. */
7208
7209static void
976ca316 7210print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7211{
976ca316 7212 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7213
6f738b01 7214 dwarf_read_debug_printf ("Type unit statistics:");
91eea9cc 7215 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
6f738b01
SM
7216 dwarf_read_debug_printf (" %d uniq abbrev tables",
7217 tu_stats->nr_uniq_abbrev_tables);
7218 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7219 tu_stats->nr_symtabs);
7220 dwarf_read_debug_printf (" %d symtab sharers",
7221 tu_stats->nr_symtab_sharers);
7222 dwarf_read_debug_printf (" %d type units without a stmt_list",
7223 tu_stats->nr_stmt_less_type_units);
7224 dwarf_read_debug_printf (" %d all_type_units reallocs",
7225 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7226}
7227
f4dc4d17
DE
7228/* Traversal function for build_type_psymtabs. */
7229
7230static int
7231build_type_psymtab_dependencies (void **slot, void *info)
7232{
976ca316 7233 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
84685904 7234 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 7235 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
0d305d5c 7236 dwarf2_psymtab *pst = tu_group->v.psymtab;
df07e2c7 7237 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7238 int i;
7239
7240 gdb_assert (len > 0);
0d305d5c 7241 gdb_assert (tu_group->type_unit_group_p ());
f4dc4d17
DE
7242
7243 pst->number_of_dependencies = len;
84685904 7244 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
df07e2c7 7245 for (i = 0; i < len; ++i)
f4dc4d17 7246 {
df07e2c7 7247 struct signatured_type *iter = tu_group->tus->at (i);
0d305d5c
TT
7248 gdb_assert (iter->is_debug_types);
7249 pst->dependencies[i] = iter->v.psymtab;
796a7ff8 7250 iter->type_unit_group = tu_group;
f4dc4d17
DE
7251 }
7252
df07e2c7
AB
7253 delete tu_group->tus;
7254 tu_group->tus = nullptr;
348e048f
DE
7255
7256 return 1;
7257}
7258
6aa5f3a6
DE
7259/* Traversal function for process_skeletonless_type_unit.
7260 Read a TU in a DWO file and build partial symbols for it. */
7261
7262static int
7263process_skeletonless_type_unit (void **slot, void *info)
7264{
7265 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7266 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7267
7268 /* If this TU doesn't exist in the global table, add it and read it in. */
7269
976ca316
SM
7270 if (per_objfile->per_bfd->signatured_types == NULL)
7271 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6 7272
9ea36493 7273 signatured_type find_entry (dwo_unit->signature);
976ca316 7274 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7275 &find_entry, INSERT);
6aa5f3a6
DE
7276 /* If we've already seen this type there's nothing to do. What's happening
7277 is we're doing our own version of comdat-folding here. */
7278 if (*slot != NULL)
7279 return 1;
7280
b8efb248 7281 /* This does the job that create_all_comp_units would have done for
6aa5f3a6 7282 this TU. */
9ea36493
SM
7283 signatured_type *entry
7284 = add_type_unit (per_objfile, dwo_unit->signature, slot);
976ca316 7285 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7286 *slot = entry;
7287
b8efb248 7288 /* This does the job that build_type_psymtabs would have done. */
0d305d5c 7289 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7290 if (!reader.dummy_p)
7291 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7292 reader.comp_unit_die);
6aa5f3a6
DE
7293
7294 return 1;
7295}
7296
7297/* Traversal function for process_skeletonless_type_units. */
7298
7299static int
7300process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7301{
7302 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7303
7304 if (dwo_file->tus != NULL)
b0b6a987
TT
7305 htab_traverse_noresize (dwo_file->tus.get (),
7306 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7307
7308 return 1;
7309}
7310
7311/* Scan all TUs of DWO files, verifying we've processed them.
7312 This is needed in case a TU was emitted without its skeleton.
7313 Note: This can't be done until we know what all the DWO files are. */
7314
7315static void
976ca316 7316process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7317{
7318 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7319 if (get_dwp_file (per_objfile) == NULL
7320 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7321 {
976ca316 7322 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7323 process_dwo_file_for_skeletonless_type_units,
976ca316 7324 per_objfile);
6aa5f3a6 7325 }
348e048f
DE
7326}
7327
ed2dc618 7328/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7329
7330static void
976ca316 7331set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7332{
0d305d5c 7333 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7334 {
891813be 7335 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7336
36586728
TT
7337 if (pst == NULL)
7338 continue;
7339
b76e467d 7340 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7341 {
7342 /* Set the 'user' field only if it is not already set. */
7343 if (pst->dependencies[j]->user == NULL)
7344 pst->dependencies[j]->user = pst;
7345 }
7346 }
7347}
7348
93311388
DE
7349/* Build the partial symbol table by doing a quick pass through the
7350 .debug_info and .debug_abbrev sections. */
72bf9492 7351
93311388 7352static void
976ca316 7353dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 7354{
976ca316 7355 struct objfile *objfile = per_objfile->objfile;
84685904 7356 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
93311388 7357
6f738b01
SM
7358 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7359 objfile_name (objfile));
45cfd468 7360
76935768 7361 scoped_restore restore_reading_psyms
bab287cd 7362 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
98bfdba5 7363
84685904 7364 per_bfd->info.read (objfile);
91c24f0a 7365
93311388
DE
7366 /* Any cached compilation units will be linked by the per-objfile
7367 read_in_chain. Make sure to free them when we're done. */
976ca316 7368 free_cached_comp_units freer (per_objfile);
72bf9492 7369
976ca316 7370 create_all_comp_units (per_objfile);
b8efb248 7371 build_type_psymtabs (per_objfile);
c906108c 7372
60606b2c
TT
7373 /* Create a temporary address map on a temporary obstack. We later
7374 copy this to the final obstack. */
8268c778 7375 auto_obstack temp_obstack;
791afaa2
TT
7376
7377 scoped_restore save_psymtabs_addrmap
84685904 7378 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
791afaa2 7379 addrmap_create_mutable (&temp_obstack));
72bf9492 7380
0d305d5c 7381 for (const auto &per_cu : per_bfd->all_comp_units)
3d5afab3
TV
7382 {
7383 if (per_cu->v.psymtab != NULL)
7384 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7385 continue;
0d305d5c 7386 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
ab432490 7387 language_minimal);
3d5afab3 7388 }
ff013f42 7389
6aa5f3a6 7390 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 7391 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
7392
7393 /* Now that all TUs have been processed we can fill in the dependencies. */
84685904 7394 if (per_bfd->type_unit_groups != NULL)
6aa5f3a6 7395 {
84685904 7396 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
976ca316 7397 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
7398 }
7399
6f738b01 7400 if (dwarf_read_debug > 0)
976ca316 7401 print_tu_stats (per_objfile);
6aa5f3a6 7402
976ca316 7403 set_partial_user (per_objfile);
95554aad 7404
84685904
TT
7405 per_bfd->partial_symtabs->psymtabs_addrmap
7406 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7407 per_bfd->partial_symtabs->obstack ());
791afaa2
TT
7408 /* At this point we want to keep the address map. */
7409 save_psymtabs_addrmap.release ();
ff013f42 7410
6f738b01
SM
7411 dwarf_read_debug_printf ("Done building psymtabs of %s",
7412 objfile_name (objfile));
ae038cb0
DJ
7413}
7414
dee91e82
DE
7415/* Load the partial DIEs for a secondary CU into memory.
7416 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7417
dee91e82 7418static void
ab432490 7419load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
7420 dwarf2_per_objfile *per_objfile,
7421 dwarf2_cu *existing_cu)
dee91e82 7422{
2e671100 7423 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
7424
7425 if (!reader.dummy_p)
7426 {
7427 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7428 language_minimal);
7429
7430 /* Check if comp unit has_children.
7431 If so, read the rest of the partial symbols from this comp unit.
7432 If not, there's no more debug_info for this comp unit. */
3e225074 7433 if (reader.comp_unit_die->has_children)
c0ab21c2 7434 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7435
7436 reader.keep ();
c0ab21c2 7437 }
ae038cb0
DJ
7438}
7439
ae038cb0 7440static void
976ca316 7441read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 7442 struct dwarf2_section_info *section,
f1902523 7443 struct dwarf2_section_info *abbrev_section,
b8efb248
TT
7444 unsigned int is_dwz,
7445 htab_up &types_htab,
7446 rcuh_kind section_kind)
ae038cb0 7447{
d521ce57 7448 const gdb_byte *info_ptr;
976ca316 7449 struct objfile *objfile = per_objfile->objfile;
be391dca 7450
6f738b01
SM
7451 dwarf_read_debug_printf ("Reading %s for %s",
7452 section->get_name (),
7453 section->get_file_name ());
bf6af496 7454
96b79293 7455 section->read (objfile);
ae038cb0 7456
36586728 7457 info_ptr = section->buffer;
6e70227d 7458
36586728 7459 while (info_ptr < section->buffer + section->size)
ae038cb0 7460 {
473ab964 7461 dwarf2_per_cu_data_up this_cu;
ae038cb0 7462
9c541725 7463 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7464
f1902523 7465 comp_unit_head cu_header;
976ca316 7466 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 7467 abbrev_section, info_ptr,
b8efb248 7468 section_kind);
ae038cb0
DJ
7469
7470 /* Save the compilation unit for later lookup. */
f1902523 7471 if (cu_header.unit_type != DW_UT_type)
976ca316 7472 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
7473 else
7474 {
b8efb248
TT
7475 if (types_htab == nullptr)
7476 types_htab = allocate_signatured_type_table ();
7477
9ea36493
SM
7478 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7479 (cu_header.signature);
b8efb248 7480 signatured_type *sig_ptr = sig_type.get ();
f1902523 7481 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
473ab964 7482 this_cu.reset (sig_type.release ());
b8efb248
TT
7483
7484 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7485 gdb_assert (slot != nullptr);
7486 if (*slot != nullptr)
7487 complaint (_("debug type entry at offset %s is duplicate to"
7488 " the entry at offset %s, signature %s"),
7489 sect_offset_str (sect_off),
7490 sect_offset_str (sig_ptr->sect_off),
7491 hex_string (sig_ptr->signature));
7492 *slot = sig_ptr;
f1902523 7493 }
9c541725 7494 this_cu->sect_off = sect_off;
f1902523 7495 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7496 this_cu->is_dwz = is_dwz;
8a0459fd 7497 this_cu->section = section;
ae038cb0 7498
ae038cb0 7499 info_ptr = info_ptr + this_cu->length;
0d305d5c 7500 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
ae038cb0 7501 }
36586728
TT
7502}
7503
7504/* Create a list of all compilation units in OBJFILE.
7505 This is only done for -readnow and building partial symtabs. */
7506
7507static void
976ca316 7508create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 7509{
b8efb248
TT
7510 htab_up types_htab;
7511
976ca316 7512 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
b8efb248
TT
7513 &per_objfile->per_bfd->abbrev, 0,
7514 types_htab, rcuh_kind::COMPILE);
7515 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7516 read_comp_units_from_section (per_objfile, &section,
7517 &per_objfile->per_bfd->abbrev, 0,
7518 types_htab, rcuh_kind::TYPE);
36586728 7519
976ca316 7520 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 7521 if (dwz != NULL)
b8efb248
TT
7522 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7523 types_htab, rcuh_kind::COMPILE);
7524
7525 per_objfile->per_bfd->signatured_types = std::move (types_htab);
c906108c
SS
7526}
7527
5734ee8b 7528/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7529 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7530 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7531 DW_AT_ranges). See the comments of add_partial_subprogram on how
7532 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7533
72bf9492
DJ
7534static void
7535scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7536 CORE_ADDR *highpc, int set_addrmap,
7537 struct dwarf2_cu *cu)
c906108c 7538{
72bf9492 7539 struct partial_die_info *pdi;
c906108c 7540
91c24f0a
DC
7541 /* Now, march along the PDI's, descending into ones which have
7542 interesting children but skipping the children of the other ones,
7543 until we reach the end of the compilation unit. */
c906108c 7544
72bf9492 7545 pdi = first_die;
91c24f0a 7546
72bf9492
DJ
7547 while (pdi != NULL)
7548 {
52356b79 7549 pdi->fixup (cu);
c906108c 7550
f55ee35c 7551 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7552 children, so we need to look at them. Ditto for anonymous
7553 enums. */
933c6fe4 7554
7d00ffec 7555 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7556 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7557 || pdi->tag == DW_TAG_imported_unit
7558 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7559 {
72bf9492 7560 switch (pdi->tag)
c906108c
SS
7561 {
7562 case DW_TAG_subprogram:
b1dc1806 7563 case DW_TAG_inlined_subroutine:
cdc07690 7564 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
7565 if (cu->language == language_cplus)
7566 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7567 set_addrmap, cu);
c906108c 7568 break;
72929c62 7569 case DW_TAG_constant:
c906108c
SS
7570 case DW_TAG_variable:
7571 case DW_TAG_typedef:
91c24f0a 7572 case DW_TAG_union_type:
317d2668
TV
7573 if (!pdi->is_declaration
7574 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 7575 {
72bf9492 7576 add_partial_symbol (pdi, cu);
63d06c5c
DC
7577 }
7578 break;
c906108c 7579 case DW_TAG_class_type:
680b30c7 7580 case DW_TAG_interface_type:
c906108c 7581 case DW_TAG_structure_type:
72bf9492 7582 if (!pdi->is_declaration)
c906108c 7583 {
72bf9492 7584 add_partial_symbol (pdi, cu);
c906108c 7585 }
b7fee5a3
KS
7586 if ((cu->language == language_rust
7587 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7588 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7589 set_addrmap, cu);
c906108c 7590 break;
91c24f0a 7591 case DW_TAG_enumeration_type:
72bf9492
DJ
7592 if (!pdi->is_declaration)
7593 add_partial_enumeration (pdi, cu);
c906108c
SS
7594 break;
7595 case DW_TAG_base_type:
dda83cd7 7596 case DW_TAG_subrange_type:
c906108c 7597 /* File scope base type definitions are added to the partial
dda83cd7 7598 symbol table. */
72bf9492 7599 add_partial_symbol (pdi, cu);
c906108c 7600 break;
d9fa45fe 7601 case DW_TAG_namespace:
cdc07690 7602 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7603 break;
5d7cb8df 7604 case DW_TAG_module:
59c35742
AB
7605 if (!pdi->is_declaration)
7606 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7607 break;
95554aad
TT
7608 case DW_TAG_imported_unit:
7609 {
7610 struct dwarf2_per_cu_data *per_cu;
7611
f4dc4d17
DE
7612 /* For now we don't handle imported units in type units. */
7613 if (cu->per_cu->is_debug_types)
7614 {
7615 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7616 " supported in type units [in module %s]"),
5e22e966 7617 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
7618 }
7619
e3b94546 7620 per_cu = dwarf2_find_containing_comp_unit
5e22e966 7621 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
7622
7623 /* Go read the partial unit, if needed. */
7624 if (per_cu->v.psymtab == NULL)
ab432490
SM
7625 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7626 cu->language);
95554aad 7627
ae640021 7628 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7629 }
7630 break;
74921315
KS
7631 case DW_TAG_imported_declaration:
7632 add_partial_symbol (pdi, cu);
7633 break;
c906108c
SS
7634 default:
7635 break;
7636 }
7637 }
7638
72bf9492
DJ
7639 /* If the die has a sibling, skip to the sibling. */
7640
7641 pdi = pdi->die_sibling;
7642 }
7643}
7644
7645/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7646
72bf9492 7647 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7648 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7649 Enumerators are an exception; they use the scope of their parent
7650 enumeration type, i.e. the name of the enumeration type is not
7651 prepended to the enumerator.
91c24f0a 7652
72bf9492
DJ
7653 There are two complexities. One is DW_AT_specification; in this
7654 case "parent" means the parent of the target of the specification,
7655 instead of the direct parent of the DIE. The other is compilers
7656 which do not emit DW_TAG_namespace; in this case we try to guess
7657 the fully qualified name of structure types from their members'
7658 linkage names. This must be done using the DIE's children rather
7659 than the children of any DW_AT_specification target. We only need
7660 to do this for structures at the top level, i.e. if the target of
7661 any DW_AT_specification (if any; otherwise the DIE itself) does not
7662 have a parent. */
7663
7664/* Compute the scope prefix associated with PDI's parent, in
7665 compilation unit CU. The result will be allocated on CU's
7666 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7667 field. NULL is returned if no prefix is necessary. */
15d034d0 7668static const char *
72bf9492
DJ
7669partial_die_parent_scope (struct partial_die_info *pdi,
7670 struct dwarf2_cu *cu)
7671{
15d034d0 7672 const char *grandparent_scope;
72bf9492 7673 struct partial_die_info *parent, *real_pdi;
91c24f0a 7674
72bf9492
DJ
7675 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7676 then this means the parent of the specification DIE. */
7677
7678 real_pdi = pdi;
72bf9492 7679 while (real_pdi->has_specification)
fb816e8b 7680 {
122cf0f2
AB
7681 auto res = find_partial_die (real_pdi->spec_offset,
7682 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
7683 real_pdi = res.pdi;
7684 cu = res.cu;
7685 }
72bf9492
DJ
7686
7687 parent = real_pdi->die_parent;
7688 if (parent == NULL)
7689 return NULL;
7690
7691 if (parent->scope_set)
7692 return parent->scope;
7693
52356b79 7694 parent->fixup (cu);
72bf9492 7695
10b3939b 7696 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7697
acebe513
UW
7698 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7699 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7700 Work around this problem here. */
7701 if (cu->language == language_cplus
6e70227d 7702 && parent->tag == DW_TAG_namespace
7d00ffec 7703 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
7704 && grandparent_scope == NULL)
7705 {
7706 parent->scope = NULL;
7707 parent->scope_set = 1;
7708 return NULL;
7709 }
7710
0a4b0913 7711 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
7712 if (pdi->tag == DW_TAG_enumerator)
7713 /* Enumerators should not get the name of the enumeration as a prefix. */
7714 parent->scope = grandparent_scope;
7715 else if (parent->tag == DW_TAG_namespace
f55ee35c 7716 || parent->tag == DW_TAG_module
72bf9492
DJ
7717 || parent->tag == DW_TAG_structure_type
7718 || parent->tag == DW_TAG_class_type
680b30c7 7719 || parent->tag == DW_TAG_interface_type
ceeb3d5a 7720 || parent->tag == DW_TAG_union_type
0a4b0913
AB
7721 || parent->tag == DW_TAG_enumeration_type
7722 || (cu->language == language_fortran
7723 && parent->tag == DW_TAG_subprogram
7724 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
7725 {
7726 if (grandparent_scope == NULL)
7d00ffec 7727 parent->scope = parent->name (cu);
72bf9492 7728 else
3e43a32a
MS
7729 parent->scope = typename_concat (&cu->comp_unit_obstack,
7730 grandparent_scope,
7d00ffec 7731 parent->name (cu), 0, cu);
72bf9492 7732 }
72bf9492
DJ
7733 else
7734 {
7735 /* FIXME drow/2004-04-01: What should we be doing with
7736 function-local names? For partial symbols, we should probably be
7737 ignoring them. */
fa9c3fa0
TT
7738 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7739 dwarf_tag_name (parent->tag),
7740 sect_offset_str (pdi->sect_off));
72bf9492 7741 parent->scope = grandparent_scope;
c906108c
SS
7742 }
7743
72bf9492
DJ
7744 parent->scope_set = 1;
7745 return parent->scope;
7746}
7747
7748/* Return the fully scoped name associated with PDI, from compilation unit
7749 CU. The result will be allocated with malloc. */
4568ecf9 7750
43816ebc 7751static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
7752partial_die_full_name (struct partial_die_info *pdi,
7753 struct dwarf2_cu *cu)
7754{
15d034d0 7755 const char *parent_scope;
72bf9492 7756
98bfdba5
PA
7757 /* If this is a template instantiation, we can not work out the
7758 template arguments from partial DIEs. So, unfortunately, we have
7759 to go through the full DIEs. At least any work we do building
7760 types here will be reused if full symbols are loaded later. */
7761 if (pdi->has_template_arguments)
7762 {
52356b79 7763 pdi->fixup (cu);
98bfdba5 7764
7d00ffec 7765 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
7766 {
7767 struct die_info *die;
7768 struct attribute attr;
7769 struct dwarf2_cu *ref_cu = cu;
7770
b64f50a1 7771 /* DW_FORM_ref_addr is using section offset. */
b4069958 7772 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7773 attr.form = DW_FORM_ref_addr;
9c541725 7774 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7775 die = follow_die_ref (NULL, &attr, &ref_cu);
7776
43816ebc 7777 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
7778 }
7779 }
7780
72bf9492
DJ
7781 parent_scope = partial_die_parent_scope (pdi, cu);
7782 if (parent_scope == NULL)
7783 return NULL;
7784 else
43816ebc 7785 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
7786 pdi->name (cu),
7787 0, cu));
c906108c
SS
7788}
7789
7790static void
72bf9492 7791add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7792{
976ca316
SM
7793 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7794 struct objfile *objfile = per_objfile->objfile;
08feed99 7795 struct gdbarch *gdbarch = objfile->arch ();
c906108c 7796 CORE_ADDR addr = 0;
15d034d0 7797 const char *actual_name = NULL;
e142c38c
DJ
7798 CORE_ADDR baseaddr;
7799
b3b3bada 7800 baseaddr = objfile->text_section_offset ();
c906108c 7801
43816ebc
TT
7802 gdb::unique_xmalloc_ptr<char> built_actual_name
7803 = partial_die_full_name (pdi, cu);
15d034d0 7804 if (built_actual_name != NULL)
43816ebc 7805 actual_name = built_actual_name.get ();
63d06c5c 7806
72bf9492 7807 if (actual_name == NULL)
7d00ffec 7808 actual_name = pdi->name (cu);
72bf9492 7809
76e288d1
TT
7810 partial_symbol psymbol;
7811 memset (&psymbol, 0, sizeof (psymbol));
7812 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 7813 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
7814
7815 /* The code below indicates that the psymbol should be installed by
7816 setting this. */
7817 gdb::optional<psymbol_placement> where;
7818
c906108c
SS
7819 switch (pdi->tag)
7820 {
b1dc1806 7821 case DW_TAG_inlined_subroutine:
c906108c 7822 case DW_TAG_subprogram:
79748972
TT
7823 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7824 - baseaddr);
0a4b0913
AB
7825 if (pdi->is_external
7826 || cu->language == language_ada
7827 || (cu->language == language_fortran
7828 && pdi->die_parent != NULL
7829 && pdi->die_parent->tag == DW_TAG_subprogram))
7830 {
dda83cd7
SM
7831 /* Normally, only "external" DIEs are part of the global scope.
7832 But in Ada and Fortran, we want to be able to access nested
7833 procedures globally. So all Ada and Fortran subprograms are
7834 stored in the global scope. */
76e288d1 7835 where = psymbol_placement::GLOBAL;
c906108c
SS
7836 }
7837 else
76e288d1
TT
7838 where = psymbol_placement::STATIC;
7839
7840 psymbol.domain = VAR_DOMAIN;
7841 psymbol.aclass = LOC_BLOCK;
a52d653e 7842 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 7843 psymbol.ginfo.value.address = addr;
0c1b455e
TT
7844
7845 if (pdi->main_subprogram && actual_name != NULL)
7846 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7847 break;
72929c62 7848 case DW_TAG_constant:
76e288d1
TT
7849 psymbol.domain = VAR_DOMAIN;
7850 psymbol.aclass = LOC_STATIC;
7851 where = (pdi->is_external
7852 ? psymbol_placement::GLOBAL
7853 : psymbol_placement::STATIC);
72929c62 7854 break;
c906108c 7855 case DW_TAG_variable:
95554aad
TT
7856 if (pdi->d.locdesc)
7857 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7858
95554aad 7859 if (pdi->d.locdesc
caac4577 7860 && addr == 0
976ca316 7861 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
7862 {
7863 /* A global or static variable may also have been stripped
7864 out by the linker if unused, in which case its address
7865 will be nullified; do not add such variables into partial
7866 symbol table then. */
7867 }
7868 else if (pdi->is_external)
c906108c
SS
7869 {
7870 /* Global Variable.
7871 Don't enter into the minimal symbol tables as there is
7872 a minimal symbol table entry from the ELF symbols already.
7873 Enter into partial symbol table if it has a location
7874 descriptor or a type.
7875 If the location descriptor is missing, new_symbol will create
7876 a LOC_UNRESOLVED symbol, the address of the variable will then
7877 be determined from the minimal symbol table whenever the variable
7878 is referenced.
7879 The address for the partial symbol table entry is not
7880 used by GDB, but it comes in handy for debugging partial symbol
7881 table building. */
7882
95554aad 7883 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
7884 {
7885 psymbol.domain = VAR_DOMAIN;
7886 psymbol.aclass = LOC_STATIC;
a52d653e 7887 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
7888 psymbol.ginfo.value.address = addr;
7889 where = psymbol_placement::GLOBAL;
7890 }
c906108c
SS
7891 }
7892 else
7893 {
ff908ebf
AW
7894 int has_loc = pdi->d.locdesc != NULL;
7895
7896 /* Static Variable. Skip symbols whose value we cannot know (those
7897 without location descriptors or constant values). */
7898 if (!has_loc && !pdi->has_const_value)
43816ebc 7899 return;
ff908ebf 7900
76e288d1
TT
7901 psymbol.domain = VAR_DOMAIN;
7902 psymbol.aclass = LOC_STATIC;
a52d653e 7903 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
7904 if (has_loc)
7905 psymbol.ginfo.value.address = addr;
7906 where = psymbol_placement::STATIC;
c906108c
SS
7907 }
7908 break;
d8f62e84 7909 case DW_TAG_array_type:
c906108c
SS
7910 case DW_TAG_typedef:
7911 case DW_TAG_base_type:
a02abb62 7912 case DW_TAG_subrange_type:
76e288d1
TT
7913 psymbol.domain = VAR_DOMAIN;
7914 psymbol.aclass = LOC_TYPEDEF;
7915 where = psymbol_placement::STATIC;
c906108c 7916 break;
74921315 7917 case DW_TAG_imported_declaration:
72bf9492 7918 case DW_TAG_namespace:
76e288d1
TT
7919 psymbol.domain = VAR_DOMAIN;
7920 psymbol.aclass = LOC_TYPEDEF;
7921 where = psymbol_placement::GLOBAL;
72bf9492 7922 break;
530e8392 7923 case DW_TAG_module:
a5fd13a9 7924 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
7925 available without any name. If so, we skip the module as it
7926 doesn't bring any value. */
a5fd13a9 7927 if (actual_name != nullptr)
76e288d1
TT
7928 {
7929 psymbol.domain = MODULE_DOMAIN;
7930 psymbol.aclass = LOC_TYPEDEF;
7931 where = psymbol_placement::GLOBAL;
7932 }
530e8392 7933 break;
c906108c 7934 case DW_TAG_class_type:
680b30c7 7935 case DW_TAG_interface_type:
c906108c
SS
7936 case DW_TAG_structure_type:
7937 case DW_TAG_union_type:
7938 case DW_TAG_enumeration_type:
fa4028e9 7939 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
7940 about "Structure, Union, and Class Type Entries": "An incomplete
7941 structure, union or class type is represented by a structure,
7942 union or class entry that does not have a byte size attribute
7943 and that has a DW_AT_declaration attribute." */
fa4028e9 7944 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 7945 return;
fa4028e9 7946
63d06c5c
DC
7947 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7948 static vs. global. */
76e288d1
TT
7949 psymbol.domain = STRUCT_DOMAIN;
7950 psymbol.aclass = LOC_TYPEDEF;
7951 where = (cu->language == language_cplus
7952 ? psymbol_placement::GLOBAL
7953 : psymbol_placement::STATIC);
c906108c
SS
7954 break;
7955 case DW_TAG_enumerator:
76e288d1
TT
7956 psymbol.domain = VAR_DOMAIN;
7957 psymbol.aclass = LOC_CONST;
7958 where = (cu->language == language_cplus
7959 ? psymbol_placement::GLOBAL
7960 : psymbol_placement::STATIC);
c906108c
SS
7961 break;
7962 default:
7963 break;
7964 }
76e288d1
TT
7965
7966 if (where.has_value ())
7967 {
f049a313
TT
7968 if (built_actual_name != nullptr)
7969 actual_name = objfile->intern (actual_name);
bcfe6157
TT
7970 if (pdi->linkage_name == nullptr || cu->language == language_ada)
7971 psymbol.ginfo.set_linkage_name (actual_name);
7972 else
7973 {
7974 psymbol.ginfo.set_demangled_name (actual_name,
7975 &objfile->objfile_obstack);
7976 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
7977 }
7b249e47
TT
7978 cu->per_cu->v.psymtab->add_psymbol
7979 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
7980 objfile);
76e288d1 7981 }
c906108c
SS
7982}
7983
5c4e30ca
DC
7984/* Read a partial die corresponding to a namespace; also, add a symbol
7985 corresponding to that namespace to the symbol table. NAMESPACE is
7986 the name of the enclosing namespace. */
91c24f0a 7987
72bf9492
DJ
7988static void
7989add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7990 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7991 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7992{
72bf9492 7993 /* Add a symbol for the namespace. */
e7c27a73 7994
72bf9492 7995 add_partial_symbol (pdi, cu);
5c4e30ca
DC
7996
7997 /* Now scan partial symbols in that namespace. */
7998
91c24f0a 7999 if (pdi->has_children)
cdc07690 8000 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8001}
8002
5d7cb8df
JK
8003/* Read a partial die corresponding to a Fortran module. */
8004
8005static void
8006add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8007 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8008{
530e8392
KB
8009 /* Add a symbol for the namespace. */
8010
8011 add_partial_symbol (pdi, cu);
8012
f55ee35c 8013 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8014
8015 if (pdi->has_children)
cdc07690 8016 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8017}
8018
b1dc1806
XR
8019/* Read a partial die corresponding to a subprogram or an inlined
8020 subprogram and create a partial symbol for that subprogram.
8021 When the CU language allows it, this routine also defines a partial
8022 symbol for each nested subprogram that this subprogram contains.
8023 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8024 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8025
cdc07690
YQ
8026 PDI may also be a lexical block, in which case we simply search
8027 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8028 Again, this is only performed when the CU language allows this
8029 type of definitions. */
8030
8031static void
8032add_partial_subprogram (struct partial_die_info *pdi,
8033 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8034 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8035{
b1dc1806 8036 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8037 {
8038 if (pdi->has_pc_info)
dda83cd7
SM
8039 {
8040 if (pdi->lowpc < *lowpc)
8041 *lowpc = pdi->lowpc;
8042 if (pdi->highpc > *highpc)
8043 *highpc = pdi->highpc;
cdc07690 8044 if (set_addrmap)
5734ee8b 8045 {
5e22e966 8046 struct objfile *objfile = cu->per_objfile->objfile;
84685904 8047 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 8048 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8049 CORE_ADDR baseaddr;
b926417a
TT
8050 CORE_ADDR this_highpc;
8051 CORE_ADDR this_lowpc;
5734ee8b 8052
b3b3bada 8053 baseaddr = objfile->text_section_offset ();
b926417a
TT
8054 this_lowpc
8055 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8056 pdi->lowpc + baseaddr)
8057 - baseaddr);
8058 this_highpc
8059 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8060 pdi->highpc + baseaddr)
8061 - baseaddr);
84685904 8062 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
b926417a 8063 this_lowpc, this_highpc - 1,
9291a0cd 8064 cu->per_cu->v.psymtab);
5734ee8b 8065 }
dda83cd7 8066 }
481860b3
GB
8067
8068 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8069 {
dda83cd7 8070 if (!pdi->is_declaration)
e8d05480
JB
8071 /* Ignore subprogram DIEs that do not have a name, they are
8072 illegal. Do not emit a complaint at this point, we will
8073 do so when we convert this psymtab into a symtab. */
7d00ffec 8074 if (pdi->name (cu))
e8d05480 8075 add_partial_symbol (pdi, cu);
dda83cd7 8076 }
bc30ff58 8077 }
6e70227d 8078
bc30ff58
JB
8079 if (! pdi->has_children)
8080 return;
8081
0a4b0913 8082 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8083 {
8084 pdi = pdi->die_child;
8085 while (pdi != NULL)
8086 {
52356b79 8087 pdi->fixup (cu);
bc30ff58 8088 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8089 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8090 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8091 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8092 pdi = pdi->die_sibling;
8093 }
8094 }
8095}
8096
91c24f0a
DC
8097/* Read a partial die corresponding to an enumeration type. */
8098
72bf9492
DJ
8099static void
8100add_partial_enumeration (struct partial_die_info *enum_pdi,
8101 struct dwarf2_cu *cu)
91c24f0a 8102{
72bf9492 8103 struct partial_die_info *pdi;
91c24f0a 8104
7d00ffec 8105 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8106 add_partial_symbol (enum_pdi, cu);
8107
8108 pdi = enum_pdi->die_child;
8109 while (pdi)
91c24f0a 8110 {
7d00ffec 8111 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8112 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8113 else
72bf9492
DJ
8114 add_partial_symbol (pdi, cu);
8115 pdi = pdi->die_sibling;
91c24f0a 8116 }
91c24f0a
DC
8117}
8118
6caca83c
CC
8119/* Return the initial uleb128 in the die at INFO_PTR. */
8120
8121static unsigned int
d521ce57 8122peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8123{
8124 unsigned int bytes_read;
8125
8126 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8127}
8128
685af9cd
TT
8129/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8130 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8131
4bb7a0a7
DJ
8132 Return the corresponding abbrev, or NULL if the number is zero (indicating
8133 an empty DIE). In either case *BYTES_READ will be set to the length of
8134 the initial number. */
8135
7c32eebb 8136static const struct abbrev_info *
685af9cd
TT
8137peek_die_abbrev (const die_reader_specs &reader,
8138 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8139{
685af9cd 8140 dwarf2_cu *cu = reader.cu;
0280fdcc 8141 bfd *abfd = reader.abfd;
685af9cd
TT
8142 unsigned int abbrev_number
8143 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8144
8145 if (abbrev_number == 0)
8146 return NULL;
8147
7c32eebb
TT
8148 const abbrev_info *abbrev
8149 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8150 if (!abbrev)
8151 {
422b9917 8152 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8153 " at offset %s [in module %s]"),
422b9917 8154 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8155 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8156 }
8157
8158 return abbrev;
8159}
8160
93311388
DE
8161/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8162 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8163 DIE. Any children of the skipped DIEs will also be skipped. */
8164
d521ce57
TT
8165static const gdb_byte *
8166skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8167{
4bb7a0a7
DJ
8168 while (1)
8169 {
685af9cd 8170 unsigned int bytes_read;
7c32eebb
TT
8171 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8172 &bytes_read);
685af9cd 8173
4bb7a0a7
DJ
8174 if (abbrev == NULL)
8175 return info_ptr + bytes_read;
8176 else
dee91e82 8177 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8178 }
8179}
8180
93311388
DE
8181/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8182 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8183 abbrev corresponding to that skipped uleb128 should be passed in
8184 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8185 children. */
8186
d521ce57
TT
8187static const gdb_byte *
8188skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8189 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8190{
8191 unsigned int bytes_read;
8192 struct attribute attr;
dee91e82
DE
8193 bfd *abfd = reader->abfd;
8194 struct dwarf2_cu *cu = reader->cu;
d521ce57 8195 const gdb_byte *buffer = reader->buffer;
f664829e 8196 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8197 unsigned int form, i;
8198
8199 for (i = 0; i < abbrev->num_attrs; i++)
8200 {
8201 /* The only abbrev we care about is DW_AT_sibling. */
8202 if (abbrev->attrs[i].name == DW_AT_sibling)
8203 {
7a5f294d 8204 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8205 if (attr.form == DW_FORM_ref_addr)
b98664d3 8206 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8207 else
b9502d3f 8208 {
0826b30a 8209 sect_offset off = attr.get_ref_die_offset ();
9c541725 8210 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8211
8212 if (sibling_ptr < info_ptr)
b98664d3 8213 complaint (_("DW_AT_sibling points backwards"));
22869d73 8214 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8215 reader->die_section->overflow_complaint ();
b9502d3f
WN
8216 else
8217 return sibling_ptr;
8218 }
4bb7a0a7
DJ
8219 }
8220
8221 /* If it isn't DW_AT_sibling, skip this attribute. */
8222 form = abbrev->attrs[i].form;
8223 skip_attribute:
8224 switch (form)
8225 {
4bb7a0a7 8226 case DW_FORM_ref_addr:
ae411497
TT
8227 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8228 and later it is offset sized. */
8229 if (cu->header.version == 2)
8230 info_ptr += cu->header.addr_size;
8231 else
8232 info_ptr += cu->header.offset_size;
8233 break;
36586728
TT
8234 case DW_FORM_GNU_ref_alt:
8235 info_ptr += cu->header.offset_size;
8236 break;
ae411497 8237 case DW_FORM_addr:
4bb7a0a7
DJ
8238 info_ptr += cu->header.addr_size;
8239 break;
8240 case DW_FORM_data1:
8241 case DW_FORM_ref1:
8242 case DW_FORM_flag:
8fe0f950 8243 case DW_FORM_strx1:
4bb7a0a7
DJ
8244 info_ptr += 1;
8245 break;
2dc7f7b3 8246 case DW_FORM_flag_present:
43988095 8247 case DW_FORM_implicit_const:
2dc7f7b3 8248 break;
4bb7a0a7
DJ
8249 case DW_FORM_data2:
8250 case DW_FORM_ref2:
8fe0f950 8251 case DW_FORM_strx2:
4bb7a0a7
DJ
8252 info_ptr += 2;
8253 break;
8fe0f950
AT
8254 case DW_FORM_strx3:
8255 info_ptr += 3;
8256 break;
4bb7a0a7
DJ
8257 case DW_FORM_data4:
8258 case DW_FORM_ref4:
8fe0f950 8259 case DW_FORM_strx4:
4bb7a0a7
DJ
8260 info_ptr += 4;
8261 break;
8262 case DW_FORM_data8:
8263 case DW_FORM_ref8:
55f1336d 8264 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8265 info_ptr += 8;
8266 break;
0224619f
JK
8267 case DW_FORM_data16:
8268 info_ptr += 16;
8269 break;
4bb7a0a7 8270 case DW_FORM_string:
9b1c24c8 8271 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8272 info_ptr += bytes_read;
8273 break;
2dc7f7b3 8274 case DW_FORM_sec_offset:
4bb7a0a7 8275 case DW_FORM_strp:
36586728 8276 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8277 info_ptr += cu->header.offset_size;
8278 break;
2dc7f7b3 8279 case DW_FORM_exprloc:
4bb7a0a7
DJ
8280 case DW_FORM_block:
8281 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8282 info_ptr += bytes_read;
8283 break;
8284 case DW_FORM_block1:
8285 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8286 break;
8287 case DW_FORM_block2:
8288 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8289 break;
8290 case DW_FORM_block4:
8291 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8292 break;
336d760d 8293 case DW_FORM_addrx:
cf532bd1 8294 case DW_FORM_strx:
4bb7a0a7
DJ
8295 case DW_FORM_sdata:
8296 case DW_FORM_udata:
8297 case DW_FORM_ref_udata:
3019eac3
DE
8298 case DW_FORM_GNU_addr_index:
8299 case DW_FORM_GNU_str_index:
18a8505e 8300 case DW_FORM_rnglistx:
41144253 8301 case DW_FORM_loclistx:
d521ce57 8302 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8303 break;
8304 case DW_FORM_indirect:
8305 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8306 info_ptr += bytes_read;
8307 /* We need to continue parsing from here, so just go back to
8308 the top. */
8309 goto skip_attribute;
8310
8311 default:
3e43a32a
MS
8312 error (_("Dwarf Error: Cannot handle %s "
8313 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8314 dwarf_form_name (form),
8315 bfd_get_filename (abfd));
8316 }
8317 }
8318
8319 if (abbrev->has_children)
dee91e82 8320 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8321 else
8322 return info_ptr;
8323}
8324
93311388 8325/* Locate ORIG_PDI's sibling.
dee91e82 8326 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8327
d521ce57 8328static const gdb_byte *
dee91e82
DE
8329locate_pdi_sibling (const struct die_reader_specs *reader,
8330 struct partial_die_info *orig_pdi,
d521ce57 8331 const gdb_byte *info_ptr)
91c24f0a
DC
8332{
8333 /* Do we know the sibling already? */
72bf9492 8334
91c24f0a
DC
8335 if (orig_pdi->sibling)
8336 return orig_pdi->sibling;
8337
8338 /* Are there any children to deal with? */
8339
8340 if (!orig_pdi->has_children)
8341 return info_ptr;
8342
4bb7a0a7 8343 /* Skip the children the long way. */
91c24f0a 8344
dee91e82 8345 return skip_children (reader, info_ptr);
91c24f0a
DC
8346}
8347
257e7a09 8348/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8349 not NULL. */
c906108c 8350
891813be
TT
8351void
8352dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8353{
976ca316 8354 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8355
976ca316 8356 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8357
077cbab2
TT
8358 /* If this psymtab is constructed from a debug-only objfile, the
8359 has_section_at_zero flag will not necessarily be correct. We
8360 can get the correct value for this flag by looking at the data
8361 associated with the (presumably stripped) associated objfile. */
8362 if (objfile->separate_debug_objfile_backlink)
c906108c 8363 {
976ca316 8364 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8365 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8366
976ca316
SM
8367 per_objfile->per_bfd->has_section_at_zero
8368 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8369 }
98bfdba5 8370
8566b89b 8371 expand_psymtab (objfile);
95554aad 8372
976ca316 8373 process_cu_includes (per_objfile);
c906108c 8374}
9cdd5dbd
DE
8375\f
8376/* Reading in full CUs. */
c906108c 8377
10b3939b
DJ
8378/* Add PER_CU to the queue. */
8379
8380static void
120ce1b5
SM
8381queue_comp_unit (dwarf2_per_cu_data *per_cu,
8382 dwarf2_per_objfile *per_objfile,
95554aad 8383 enum language pretend_language)
10b3939b 8384{
10b3939b 8385 per_cu->queued = 1;
08ac5771
SM
8386
8387 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8388 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8389}
8390
616c069a
SM
8391/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8392
89e63ee4
DE
8393 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8394 dependency.
69d751e3 8395
616c069a
SM
8396 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8397 DIEs, false otherwise.
8398
8399 Explanation: there is an invariant that if a CU is queued for expansion
8400 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8401 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8402 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8403 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8404 invariant is respected.
8405
8406 The caller is therefore not required to load the CU's DIEs (we return false)
8407 if:
8408
8409 - the CU is already expanded, and therefore does not get enqueued
8410 - the CU gets enqueued for expansion, but its DIEs are already loaded
8411
8412 Note that the caller should not use this function's return value as an
8413 indicator of whether the CU's DIEs are loaded right now, it should check
8414 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
8415
8416static int
89e63ee4 8417maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8418 dwarf2_per_cu_data *per_cu,
8419 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8420 enum language pretend_language)
8421{
8422 /* We may arrive here during partial symbol reading, if we need full
8423 DIEs to process an unusual case (e.g. template arguments). Do
8424 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8425 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 8426 {
7188ed02
SM
8427 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8428
8429 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
8430 return 1;
8431 return 0;
8432 }
8433
8434 /* Mark the dependence relation so that we don't flush PER_CU
8435 too early. */
89e63ee4 8436 if (dependent_cu != NULL)
347212b8 8437 dependent_cu->add_dependence (per_cu);
0907af0c
DE
8438
8439 /* If it's already on the queue, we have nothing to do. */
8440 if (per_cu->queued)
de53369b
SM
8441 {
8442 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8443 loaded. */
8444 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
8445
8446 /* If the CU is queued for expansion, it should not already be
8447 expanded. */
8448 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8449
8450 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
8451 return 0;
8452 }
0907af0c 8453
616c069a
SM
8454 bool queued = false;
8455 if (!per_objfile->symtab_set_p (per_cu))
8456 {
8457 /* Add it to the queue. */
8458 queue_comp_unit (per_cu, per_objfile, pretend_language);
8459 queued = true;
8460 }
8461
0907af0c
DE
8462 /* If the compilation unit is already loaded, just mark it as
8463 used. */
7188ed02
SM
8464 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8465 if (cu != nullptr)
616c069a 8466 cu->last_used = 0;
0907af0c 8467
616c069a
SM
8468 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8469 and the DIEs are not already loaded. */
8470 return queued && cu == nullptr;
0907af0c
DE
8471}
8472
10b3939b
DJ
8473/* Process the queue. */
8474
8475static void
976ca316 8476process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 8477{
6f738b01
SM
8478 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8479 objfile_name (per_objfile->objfile));
45cfd468 8480
03dd20cc
DJ
8481 /* The queue starts out with one item, but following a DIE reference
8482 may load a new CU, adding it to the end of the queue. */
08ac5771 8483 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 8484 {
08ac5771 8485 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 8486 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 8487
976ca316 8488 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 8489 {
976ca316 8490 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 8491
7188ed02
SM
8492 /* Skip dummy CUs. */
8493 if (cu != nullptr)
73be47f5 8494 {
7188ed02
SM
8495 unsigned int debug_print_threshold;
8496 char buf[100];
8497
8498 if (per_cu->is_debug_types)
8499 {
8500 struct signatured_type *sig_type =
8501 (struct signatured_type *) per_cu;
8502
8503 sprintf (buf, "TU %s at offset %s",
8504 hex_string (sig_type->signature),
8505 sect_offset_str (per_cu->sect_off));
8506 /* There can be 100s of TUs.
8507 Only print them in verbose mode. */
8508 debug_print_threshold = 2;
8509 }
8510 else
8511 {
8512 sprintf (buf, "CU at offset %s",
8513 sect_offset_str (per_cu->sect_off));
8514 debug_print_threshold = 1;
8515 }
247f5c4f 8516
7188ed02 8517 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8518 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 8519
7188ed02
SM
8520 if (per_cu->is_debug_types)
8521 process_full_type_unit (cu, item.pretend_language);
8522 else
8523 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 8524
7188ed02 8525 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8526 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 8527 }
f4dc4d17 8528 }
10b3939b 8529
7188ed02 8530 per_cu->queued = 0;
08ac5771 8531 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
8532 }
8533
6f738b01
SM
8534 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8535 objfile_name (per_objfile->objfile));
10b3939b
DJ
8536}
8537
10b3939b
DJ
8538/* Read in full symbols for PST, and anything it depends on. */
8539
8566b89b
TT
8540void
8541dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8542{
af758d11 8543 gdb_assert (!readin_p (objfile));
95554aad 8544
17ee85fc
TT
8545 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8546 free_cached_comp_units freer (per_objfile);
48993951 8547 expand_dependencies (objfile);
aaa75496 8548
97a1449a 8549 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 8550 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
8551}
8552
af758d11
SM
8553/* See psympriv.h. */
8554
8555bool
8556dwarf2_psymtab::readin_p (struct objfile *objfile) const
8557{
8558 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8559 return per_objfile->symtab_set_p (per_cu_data);
8560}
8561
8562/* See psympriv.h. */
8563
8564compunit_symtab *
8565dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8566{
8567 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8568 return per_objfile->get_symtab (per_cu_data);
8569}
8570
dee91e82
DE
8571/* Trivial hash function for die_info: the hash value of a DIE
8572 is its offset in .debug_info for this objfile. */
10b3939b 8573
dee91e82
DE
8574static hashval_t
8575die_hash (const void *item)
10b3939b 8576{
9a3c8263 8577 const struct die_info *die = (const struct die_info *) item;
6502dd73 8578
9c541725 8579 return to_underlying (die->sect_off);
dee91e82 8580}
63d06c5c 8581
dee91e82
DE
8582/* Trivial comparison function for die_info structures: two DIEs
8583 are equal if they have the same offset. */
98bfdba5 8584
dee91e82
DE
8585static int
8586die_eq (const void *item_lhs, const void *item_rhs)
8587{
9a3c8263
SM
8588 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8589 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8590
9c541725 8591 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8592}
c906108c 8593
4a636814
SM
8594/* Load the DIEs associated with PER_CU into memory.
8595
8596 In some cases, the caller, while reading partial symbols, will need to load
8597 the full symbols for the CU for some reason. It will already have a
8598 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8599 rather than creating a new one. */
c906108c 8600
dee91e82 8601static void
ab432490
SM
8602load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8603 dwarf2_per_objfile *per_objfile,
4a636814 8604 dwarf2_cu *existing_cu,
c0ab21c2
TT
8605 bool skip_partial,
8606 enum language pretend_language)
dee91e82 8607{
c0ab21c2
TT
8608 gdb_assert (! this_cu->is_debug_types);
8609
7188ed02 8610 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
8611 if (reader.dummy_p)
8612 return;
8613
8614 struct dwarf2_cu *cu = reader.cu;
8615 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8616
dee91e82
DE
8617 gdb_assert (cu->die_hash == NULL);
8618 cu->die_hash =
8619 htab_create_alloc_ex (cu->header.length / 12,
8620 die_hash,
8621 die_eq,
8622 NULL,
8623 &cu->comp_unit_obstack,
8624 hashtab_obstack_allocate,
8625 dummy_obstack_deallocate);
e142c38c 8626
3e225074 8627 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8628 reader.comp_unit_die->child
8629 = read_die_and_siblings (&reader, reader.info_ptr,
8630 &info_ptr, reader.comp_unit_die);
8631 cu->dies = reader.comp_unit_die;
dee91e82 8632 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8633
8634 /* We try not to read any attributes in this function, because not
9cdd5dbd 8635 all CUs needed for references have been loaded yet, and symbol
10b3939b 8636 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8637 or we won't be able to build types correctly.
8638 Similarly, if we do not read the producer, we can not apply
8639 producer-specific interpretation. */
c0ab21c2 8640 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8641
8642 reader.keep ();
10b3939b
DJ
8643}
8644
3da10d80
KS
8645/* Add a DIE to the delayed physname list. */
8646
8647static void
8648add_to_method_list (struct type *type, int fnfield_index, int index,
8649 const char *name, struct die_info *die,
8650 struct dwarf2_cu *cu)
8651{
8652 struct delayed_method_info mi;
8653 mi.type = type;
8654 mi.fnfield_index = fnfield_index;
8655 mi.index = index;
8656 mi.name = name;
8657 mi.die = die;
c89b44cd 8658 cu->method_list.push_back (mi);
3da10d80
KS
8659}
8660
3693fdb3
PA
8661/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8662 "const" / "volatile". If so, decrements LEN by the length of the
8663 modifier and return true. Otherwise return false. */
8664
8665template<size_t N>
8666static bool
8667check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8668{
8669 size_t mod_len = sizeof (mod) - 1;
8670 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8671 {
8672 len -= mod_len;
8673 return true;
8674 }
8675 return false;
8676}
8677
3da10d80
KS
8678/* Compute the physnames of any methods on the CU's method list.
8679
8680 The computation of method physnames is delayed in order to avoid the
8681 (bad) condition that one of the method's formal parameters is of an as yet
8682 incomplete type. */
8683
8684static void
8685compute_delayed_physnames (struct dwarf2_cu *cu)
8686{
3693fdb3 8687 /* Only C++ delays computing physnames. */
c89b44cd 8688 if (cu->method_list.empty ())
3693fdb3
PA
8689 return;
8690 gdb_assert (cu->language == language_cplus);
8691
52941706 8692 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8693 {
1d06ead6 8694 const char *physname;
3da10d80 8695 struct fn_fieldlist *fn_flp
c89b44cd
TT
8696 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8697 physname = dwarf2_physname (mi.name, mi.die, cu);
8698 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8699 = physname ? physname : "";
3693fdb3
PA
8700
8701 /* Since there's no tag to indicate whether a method is a
8702 const/volatile overload, extract that information out of the
8703 demangled name. */
8704 if (physname != NULL)
8705 {
8706 size_t len = strlen (physname);
8707
8708 while (1)
8709 {
8710 if (physname[len] == ')') /* shortcut */
8711 break;
8712 else if (check_modifier (physname, len, " const"))
c89b44cd 8713 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 8714 else if (check_modifier (physname, len, " volatile"))
c89b44cd 8715 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
8716 else
8717 break;
8718 }
8719 }
3da10d80 8720 }
c89b44cd
TT
8721
8722 /* The list is no longer needed. */
8723 cu->method_list.clear ();
3da10d80
KS
8724}
8725
a766d390
DE
8726/* Go objects should be embedded in a DW_TAG_module DIE,
8727 and it's not clear if/how imported objects will appear.
8728 To keep Go support simple until that's worked out,
8729 go back through what we've read and create something usable.
8730 We could do this while processing each DIE, and feels kinda cleaner,
8731 but that way is more invasive.
8732 This is to, for example, allow the user to type "p var" or "b main"
8733 without having to specify the package name, and allow lookups
8734 of module.object to work in contexts that use the expression
8735 parser. */
8736
8737static void
8738fixup_go_packaging (struct dwarf2_cu *cu)
8739{
421d1616 8740 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
8741 struct pending *list;
8742 int i;
8743
c24bdb02 8744 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
8745 list != NULL;
8746 list = list->next)
a766d390
DE
8747 {
8748 for (i = 0; i < list->nsyms; ++i)
8749 {
8750 struct symbol *sym = list->symbol[i];
8751
c1b5c1eb 8752 if (sym->language () == language_go
a766d390
DE
8753 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8754 {
421d1616
TT
8755 gdb::unique_xmalloc_ptr<char> this_package_name
8756 (go_symbol_package_name (sym));
a766d390
DE
8757
8758 if (this_package_name == NULL)
8759 continue;
8760 if (package_name == NULL)
421d1616 8761 package_name = std::move (this_package_name);
a766d390
DE
8762 else
8763 {
5e22e966 8764 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 8765 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 8766 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8767 (symbol_symtab (sym) != NULL
8768 ? symtab_to_filename_for_display
8769 (symbol_symtab (sym))
e3b94546 8770 : objfile_name (objfile)),
421d1616 8771 this_package_name.get (), package_name.get ());
a766d390
DE
8772 }
8773 }
8774 }
8775 }
8776
8777 if (package_name != NULL)
8778 {
5e22e966 8779 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 8780 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
8781 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8782 saved_package_name);
a766d390
DE
8783 struct symbol *sym;
8784
8c14c3a3 8785 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 8786 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 8787 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
8788 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8789 e.g., "main" finds the "main" module and not C's main(). */
8790 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8791 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8792 SYMBOL_TYPE (sym) = type;
8793
c24bdb02 8794 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
8795 }
8796}
8797
c9317f21
TT
8798/* Allocate a fully-qualified name consisting of the two parts on the
8799 obstack. */
8800
8801static const char *
8802rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8803{
8804 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8805}
8806
9c6a1327
TT
8807/* A helper that allocates a variant part to attach to a Rust enum
8808 type. OBSTACK is where the results should be allocated. TYPE is
8809 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
8810 discriminant. It must be the index of one of the fields of TYPE,
8811 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
8812 DEFAULT_INDEX is the index of the default field; or -1 if there is
8813 no default. RANGES is indexed by "effective" field number (the
8814 field index, but omitting the discriminant and default fields) and
8815 must hold the discriminant values used by the variants. Note that
8816 RANGES must have a lifetime at least as long as OBSTACK -- either
8817 already allocated on it, or static. */
c9317f21 8818
9c6a1327
TT
8819static void
8820alloc_rust_variant (struct obstack *obstack, struct type *type,
8821 int discriminant_index, int default_index,
8822 gdb::array_view<discriminant_range> ranges)
8823{
57d02173
TT
8824 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8825 gdb_assert (discriminant_index == -1
8826 || (discriminant_index >= 0
8827 && discriminant_index < type->num_fields ()));
c9317f21 8828 gdb_assert (default_index == -1
1f704f76 8829 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 8830
9c6a1327 8831 /* We have one variant for each non-discriminant field. */
57d02173
TT
8832 int n_variants = type->num_fields ();
8833 if (discriminant_index != -1)
8834 --n_variants;
c9317f21 8835
9c6a1327
TT
8836 variant *variants = new (obstack) variant[n_variants];
8837 int var_idx = 0;
8838 int range_idx = 0;
1f704f76 8839 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
8840 {
8841 if (i == discriminant_index)
8842 continue;
c9317f21 8843
9c6a1327
TT
8844 variants[var_idx].first_field = i;
8845 variants[var_idx].last_field = i + 1;
8846
8847 /* The default field does not need a range, but other fields do.
8848 We skipped the discriminant above. */
8849 if (i != default_index)
8850 {
8851 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8852 ++range_idx;
8853 }
c9317f21 8854
9c6a1327
TT
8855 ++var_idx;
8856 }
8857
8858 gdb_assert (range_idx == ranges.size ());
8859 gdb_assert (var_idx == n_variants);
8860
8861 variant_part *part = new (obstack) variant_part;
8862 part->discriminant_index = discriminant_index;
57d02173
TT
8863 /* If there is no discriminant, then whether it is signed is of no
8864 consequence. */
8865 part->is_unsigned
8866 = (discriminant_index == -1
8867 ? false
c6d940a9 8868 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
8869 part->variants = gdb::array_view<variant> (variants, n_variants);
8870
8871 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
8872 gdb::array_view<variant_part> *prop_value
8873 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 8874
9c6a1327 8875 struct dynamic_prop prop;
8c2e4e06 8876 prop.set_variant_parts (prop_value);
9c6a1327 8877
5c54719c 8878 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
8879}
8880
8881/* Some versions of rustc emitted enums in an unusual way.
8882
8883 Ordinary enums were emitted as unions. The first element of each
8884 structure in the union was named "RUST$ENUM$DISR". This element
8885 held the discriminant.
8886
8887 These versions of Rust also implemented the "non-zero"
8888 optimization. When the enum had two values, and one is empty and
8889 the other holds a pointer that cannot be zero, the pointer is used
8890 as the discriminant, with a zero value meaning the empty variant.
8891 Here, the union's first member is of the form
8892 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
8893 where the fieldnos are the indices of the fields that should be
8894 traversed in order to find the field (which may be several fields deep)
8895 and the variantname is the name of the variant of the case when the
8896 field is zero.
8897
8898 This function recognizes whether TYPE is of one of these forms,
8899 and, if so, smashes it to be a variant type. */
8900
8901static void
8902quirk_rust_enum (struct type *type, struct objfile *objfile)
8903{
78134374 8904 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
8905
8906 /* We don't need to deal with empty enums. */
1f704f76 8907 if (type->num_fields () == 0)
c9317f21
TT
8908 return;
8909
8910#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 8911 if (type->num_fields () == 1
c9317f21
TT
8912 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
8913 {
8914 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
8915
8916 /* Decode the field name to find the offset of the
8917 discriminant. */
8918 ULONGEST bit_offset = 0;
940da03e 8919 struct type *field_type = type->field (0).type ();
c9317f21
TT
8920 while (name[0] >= '0' && name[0] <= '9')
8921 {
8922 char *tail;
8923 unsigned long index = strtoul (name, &tail, 10);
8924 name = tail;
8925 if (*name != '$'
1f704f76 8926 || index >= field_type->num_fields ()
c9317f21
TT
8927 || (TYPE_FIELD_LOC_KIND (field_type, index)
8928 != FIELD_LOC_KIND_BITPOS))
8929 {
b98664d3 8930 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
8931 "[in module %s]"),
8932 TYPE_FIELD_NAME (type, 0),
8933 objfile_name (objfile));
8934 return;
8935 }
8936 ++name;
8937
8938 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 8939 field_type = field_type->field (index).type ();
c9317f21
TT
8940 }
8941
9c6a1327
TT
8942 /* Smash this type to be a structure type. We have to do this
8943 because the type has already been recorded. */
67607e24 8944 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 8945 type->set_num_fields (3);
9c6a1327 8946 /* Save the field we care about. */
ceacbf6e 8947 struct field saved_field = type->field (0);
3cabb6b0
SM
8948 type->set_fields
8949 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 8950
9c6a1327 8951 /* Put the discriminant at index 0. */
5d14b6e5 8952 type->field (0).set_type (field_type);
9c6a1327
TT
8953 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8954 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 8955 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
8956
8957 /* The order of fields doesn't really matter, so put the real
8958 field at index 1 and the data-less field at index 2. */
ceacbf6e 8959 type->field (1) = saved_field;
9c6a1327 8960 TYPE_FIELD_NAME (type, 1)
940da03e
SM
8961 = rust_last_path_segment (type->field (1).type ()->name ());
8962 type->field (1).type ()->set_name
7d93a1e0 8963 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 8964 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
8965
8966 const char *dataless_name
7d93a1e0 8967 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
8968 name);
8969 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8970 dataless_name);
5d14b6e5 8971 type->field (2).set_type (dataless_type);
c9317f21
TT
8972 /* NAME points into the original discriminant name, which
8973 already has the correct lifetime. */
9c6a1327 8974 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 8975 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 8976
9c6a1327
TT
8977 /* Indicate that this is a variant type. */
8978 static discriminant_range ranges[1] = { { 0, 0 } };
8979 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 8980 }
77c2dba3
TT
8981 /* A union with a single anonymous field is probably an old-style
8982 univariant enum. */
1f704f76 8983 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 8984 {
c9317f21
TT
8985 /* Smash this type to be a structure type. We have to do this
8986 because the type has already been recorded. */
67607e24 8987 type->set_code (TYPE_CODE_STRUCT);
c9317f21 8988
940da03e 8989 struct type *field_type = type->field (0).type ();
c9317f21 8990 const char *variant_name
7d93a1e0 8991 = rust_last_path_segment (field_type->name ());
9c6a1327 8992 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
8993 field_type->set_name
8994 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 8995 type->name (), variant_name));
57d02173
TT
8996
8997 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
8998 }
8999 else
9000 {
9001 struct type *disr_type = nullptr;
1f704f76 9002 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9003 {
940da03e 9004 disr_type = type->field (i).type ();
c9317f21 9005
78134374 9006 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9007 {
9008 /* All fields of a true enum will be structs. */
9009 return;
9010 }
1f704f76 9011 else if (disr_type->num_fields () == 0)
c9317f21
TT
9012 {
9013 /* Could be data-less variant, so keep going. */
a037790e 9014 disr_type = nullptr;
c9317f21
TT
9015 }
9016 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9017 "RUST$ENUM$DISR") != 0)
9018 {
9019 /* Not a Rust enum. */
9020 return;
9021 }
9022 else
9023 {
9024 /* Found one. */
9025 break;
9026 }
9027 }
9028
9029 /* If we got here without a discriminant, then it's probably
9030 just a union. */
9031 if (disr_type == nullptr)
9032 return;
9033
9034 /* Smash this type to be a structure type. We have to do this
9035 because the type has already been recorded. */
67607e24 9036 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9037
9c6a1327 9038 /* Make space for the discriminant field. */
ceacbf6e 9039 struct field *disr_field = &disr_type->field (0);
9c6a1327 9040 field *new_fields
1f704f76 9041 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9042 * sizeof (struct field)));
80fc5e77 9043 memcpy (new_fields + 1, type->fields (),
1f704f76 9044 type->num_fields () * sizeof (struct field));
3cabb6b0 9045 type->set_fields (new_fields);
1f704f76 9046 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9047
9048 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9049 type->field (0) = *disr_field;
9c6a1327
TT
9050 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9051 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9052
9053 /* We need a way to find the correct discriminant given a
9054 variant name. For convenience we build a map here. */
b6cdac4b 9055 struct type *enum_type = disr_field->type ();
c9317f21 9056 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9057 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9058 {
9059 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9060 {
9061 const char *name
9062 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9063 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9064 }
9065 }
9066
1f704f76 9067 int n_fields = type->num_fields ();
9c6a1327
TT
9068 /* We don't need a range entry for the discriminant, but we do
9069 need one for every other field, as there is no default
9070 variant. */
9071 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9072 discriminant_range,
9073 n_fields - 1);
c9317f21
TT
9074 /* Skip the discriminant here. */
9075 for (int i = 1; i < n_fields; ++i)
9076 {
9077 /* Find the final word in the name of this variant's type.
9078 That name can be used to look up the correct
9079 discriminant. */
9080 const char *variant_name
940da03e 9081 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9082
9083 auto iter = discriminant_map.find (variant_name);
9084 if (iter != discriminant_map.end ())
9c6a1327 9085 {
57d02173
TT
9086 ranges[i - 1].low = iter->second;
9087 ranges[i - 1].high = iter->second;
9c6a1327 9088 }
c9317f21 9089
57d02173
TT
9090 /* In Rust, each element should have the size of the
9091 enclosing enum. */
9092 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9093
bedda9ac 9094 /* Remove the discriminant field, if it exists. */
940da03e 9095 struct type *sub_type = type->field (i).type ();
1f704f76 9096 if (sub_type->num_fields () > 0)
bedda9ac 9097 {
5e33d5f4 9098 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9099 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9100 }
9c6a1327 9101 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9102 sub_type->set_name
9103 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9104 type->name (), variant_name));
c9317f21 9105 }
9c6a1327
TT
9106
9107 /* Indicate that this is a variant type. */
a1520ad8 9108 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9109 gdb::array_view<discriminant_range> (ranges,
9110 n_fields - 1));
c9317f21
TT
9111 }
9112}
9113
9114/* Rewrite some Rust unions to be structures with variants parts. */
9115
9116static void
9117rust_union_quirks (struct dwarf2_cu *cu)
9118{
9119 gdb_assert (cu->language == language_rust);
52941706 9120 for (type *type_ : cu->rust_unions)
5e22e966 9121 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9122 /* We don't need this any more. */
9123 cu->rust_unions.clear ();
c9317f21
TT
9124}
9125
8adb8487
TT
9126/* See read.h. */
9127
9128type_unit_group_unshareable *
9129dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9130{
9131 auto iter = this->m_type_units.find (tu_group);
9132 if (iter != this->m_type_units.end ())
9133 return iter->second.get ();
9134
9135 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9136 type_unit_group_unshareable *result = uniq.get ();
9137 this->m_type_units[tu_group] = std::move (uniq);
9138 return result;
9139}
9140
e286671b
TT
9141struct type *
9142dwarf2_per_objfile::get_type_for_signatured_type
9143 (signatured_type *sig_type) const
9144{
9145 auto iter = this->m_type_map.find (sig_type);
9146 if (iter == this->m_type_map.end ())
9147 return nullptr;
9148
9149 return iter->second;
9150}
9151
9152void dwarf2_per_objfile::set_type_for_signatured_type
9153 (signatured_type *sig_type, struct type *type)
9154{
9155 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9156
9157 this->m_type_map[sig_type] = type;
9158}
9159
95554aad
TT
9160/* A helper function for computing the list of all symbol tables
9161 included by PER_CU. */
9162
9163static void
4c39bc03 9164recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9165 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9166 dwarf2_per_cu_data *per_cu,
9167 dwarf2_per_objfile *per_objfile,
43f3e411 9168 struct compunit_symtab *immediate_parent)
95554aad 9169{
af758d11 9170 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9171 if (*slot != NULL)
9172 {
9173 /* This inclusion and its children have been processed. */
9174 return;
9175 }
9176
9177 *slot = per_cu;
af758d11 9178
95554aad 9179 /* Only add a CU if it has a symbol table. */
43182c09 9180 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9181 if (cust != NULL)
ec94af83
DE
9182 {
9183 /* If this is a type unit only add its symbol table if we haven't
9184 seen it yet (type unit per_cu's can share symtabs). */
9185 if (per_cu->is_debug_types)
9186 {
43f3e411 9187 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9188 if (*slot == NULL)
9189 {
43f3e411 9190 *slot = cust;
4c39bc03 9191 result->push_back (cust);
43f3e411
DE
9192 if (cust->user == NULL)
9193 cust->user = immediate_parent;
ec94af83
DE
9194 }
9195 }
9196 else
f9125b6c 9197 {
4c39bc03 9198 result->push_back (cust);
43f3e411
DE
9199 if (cust->user == NULL)
9200 cust->user = immediate_parent;
f9125b6c 9201 }
ec94af83 9202 }
95554aad 9203
ae640021
AB
9204 if (!per_cu->imported_symtabs_empty ())
9205 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9206 {
9207 recursively_compute_inclusions (result, all_children,
43182c09
SM
9208 all_type_symtabs, ptr, per_objfile,
9209 cust);
ae640021 9210 }
95554aad
TT
9211}
9212
43f3e411 9213/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9214 PER_CU. */
9215
9216static void
43182c09
SM
9217compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9218 dwarf2_per_objfile *per_objfile)
95554aad 9219{
f4dc4d17
DE
9220 gdb_assert (! per_cu->is_debug_types);
9221
ae640021 9222 if (!per_cu->imported_symtabs_empty ())
95554aad 9223 {
ae640021 9224 int len;
4c39bc03 9225 std::vector<compunit_symtab *> result_symtabs;
43182c09 9226 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9227
9228 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9229 if (cust == NULL)
95554aad
TT
9230 return;
9231
280a9412
TT
9232 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9233 htab_eq_pointer,
9234 NULL, xcalloc, xfree));
9235 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9236 htab_eq_pointer,
9237 NULL, xcalloc, xfree));
95554aad 9238
ae640021 9239 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9240 {
280a9412
TT
9241 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9242 all_type_symtabs.get (), ptr,
9243 per_objfile, cust);
ec94af83 9244 }
95554aad 9245
ec94af83 9246 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9247 len = result_symtabs.size ();
43f3e411 9248 cust->includes
f6e649dd 9249 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9250 struct compunit_symtab *, len + 1);
4c39bc03
TT
9251 memcpy (cust->includes, result_symtabs.data (),
9252 len * sizeof (compunit_symtab *));
43f3e411 9253 cust->includes[len] = NULL;
95554aad
TT
9254 }
9255}
9256
9257/* Compute the 'includes' field for the symtabs of all the CUs we just
9258 read. */
9259
9260static void
976ca316 9261process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9262{
976ca316 9263 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9264 {
9265 if (! iter->is_debug_types)
976ca316 9266 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9267 }
95554aad 9268
976ca316 9269 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9270}
9271
8fc0b21d 9272/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9273 already been loaded into memory. */
9274
9275static void
8fc0b21d 9276process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9277{
976ca316
SM
9278 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9279 struct objfile *objfile = per_objfile->objfile;
08feed99 9280 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9281 CORE_ADDR lowpc, highpc;
43f3e411 9282 struct compunit_symtab *cust;
10b3939b 9283 CORE_ADDR baseaddr;
4359dff1 9284 struct block *static_block;
3e29f34a 9285 CORE_ADDR addr;
10b3939b 9286
b3b3bada 9287 baseaddr = objfile->text_section_offset ();
10b3939b 9288
c89b44cd
TT
9289 /* Clear the list here in case something was left over. */
9290 cu->method_list.clear ();
10b3939b 9291
95554aad
TT
9292 cu->language = pretend_language;
9293 cu->language_defn = language_def (cu->language);
9294
1c47ec3e
TV
9295 dwarf2_find_base_address (cu->dies, cu);
9296
702cf3f5
AB
9297 /* Before we start reading the top-level DIE, ensure it has a valid tag
9298 type. */
9299 switch (cu->dies->tag)
9300 {
9301 case DW_TAG_compile_unit:
9302 case DW_TAG_partial_unit:
9303 case DW_TAG_type_unit:
9304 break;
9305 default:
9306 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9307 dwarf_tag_name (cu->dies->tag),
9308 sect_offset_str (cu->per_cu->sect_off),
9309 objfile_name (per_objfile->objfile));
9310 }
9311
c906108c 9312 /* Do line number decoding in read_file_scope () */
10b3939b 9313 process_die (cu->dies, cu);
c906108c 9314
a766d390
DE
9315 /* For now fudge the Go package. */
9316 if (cu->language == language_go)
9317 fixup_go_packaging (cu);
9318
5f48f8f3 9319 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9320 should be complete, and it should now be safe to compute all of the
9321 physnames. */
9322 compute_delayed_physnames (cu);
3da10d80 9323
c9317f21
TT
9324 if (cu->language == language_rust)
9325 rust_union_quirks (cu);
9326
fae299cd
DC
9327 /* Some compilers don't define a DW_AT_high_pc attribute for the
9328 compilation unit. If the DW_AT_high_pc is missing, synthesize
9329 it, by scanning the DIE's below the compilation unit. */
10b3939b 9330 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9331
3e29f34a 9332 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9333 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9334
9335 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9336 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9337 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9338 addrmap to help ensure it has an accurate map of pc values belonging to
9339 this comp unit. */
9340 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9341
c24bdb02 9342 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9343 SECT_OFF_TEXT (objfile),
9344 0);
c906108c 9345
43f3e411 9346 if (cust != NULL)
c906108c 9347 {
df15bd07 9348 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9349
8be455d7
JK
9350 /* Set symtab language to language from DW_AT_language. If the
9351 compilation is from a C file generated by language preprocessors, do
9352 not set the language if it was already deduced by start_subfile. */
43f3e411 9353 if (!(cu->language == language_c
40e3ad0e 9354 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9355 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9356
9357 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9358 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9359 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9360 there were bugs in prologue debug info, fixed later in GCC-4.5
9361 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9362
9363 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9364 needed, it would be wrong due to missing DW_AT_producer there.
9365
9366 Still one can confuse GDB by using non-standard GCC compilation
9367 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9368 */
ab260dad 9369 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9370 cust->locations_valid = 1;
e0d00bc7
JK
9371
9372 if (gcc_4_minor >= 5)
43f3e411 9373 cust->epilogue_unwind_valid = 1;
96408a79 9374
43f3e411 9375 cust->call_site_htab = cu->call_site_htab;
c906108c 9376 }
9291a0cd 9377
976ca316 9378 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9379
95554aad 9380 /* Push it for inclusion processing later. */
976ca316 9381 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9382
9383 /* Not needed any more. */
c24bdb02 9384 cu->reset_builder ();
f4dc4d17 9385}
45cfd468 9386
8fc0b21d 9387/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9388 already been loaded into memory. */
9389
9390static void
8fc0b21d 9391process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9392 enum language pretend_language)
9393{
976ca316
SM
9394 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9395 struct objfile *objfile = per_objfile->objfile;
43f3e411 9396 struct compunit_symtab *cust;
0186c6a7
DE
9397 struct signatured_type *sig_type;
9398
8fc0b21d
SM
9399 gdb_assert (cu->per_cu->is_debug_types);
9400 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9401
c89b44cd
TT
9402 /* Clear the list here in case something was left over. */
9403 cu->method_list.clear ();
f4dc4d17 9404
f4dc4d17
DE
9405 cu->language = pretend_language;
9406 cu->language_defn = language_def (cu->language);
9407
9408 /* The symbol tables are set up in read_type_unit_scope. */
9409 process_die (cu->dies, cu);
9410
9411 /* For now fudge the Go package. */
9412 if (cu->language == language_go)
9413 fixup_go_packaging (cu);
9414
5f48f8f3 9415 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9416 should be complete, and it should now be safe to compute all of the
9417 physnames. */
9418 compute_delayed_physnames (cu);
f4dc4d17 9419
c9317f21
TT
9420 if (cu->language == language_rust)
9421 rust_union_quirks (cu);
9422
f4dc4d17
DE
9423 /* TUs share symbol tables.
9424 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9425 of it with end_expandable_symtab. Otherwise, complete the addition of
9426 this TU's symbols to the existing symtab. */
8adb8487 9427 type_unit_group_unshareable *tug_unshare =
976ca316 9428 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9429 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9430 {
c24bdb02
KS
9431 buildsym_compunit *builder = cu->get_builder ();
9432 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9433 tug_unshare->compunit_symtab = cust;
f4dc4d17 9434
43f3e411 9435 if (cust != NULL)
f4dc4d17
DE
9436 {
9437 /* Set symtab language to language from DW_AT_language. If the
9438 compilation is from a C file generated by language preprocessors,
9439 do not set the language if it was already deduced by
9440 start_subfile. */
43f3e411
DE
9441 if (!(cu->language == language_c
9442 && COMPUNIT_FILETABS (cust)->language != language_c))
9443 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9444 }
9445 }
9446 else
9447 {
c24bdb02 9448 cu->get_builder ()->augment_type_symtab ();
8adb8487 9449 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9450 }
9451
976ca316 9452 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9453
9454 /* Not needed any more. */
c24bdb02 9455 cu->reset_builder ();
c906108c
SS
9456}
9457
95554aad
TT
9458/* Process an imported unit DIE. */
9459
9460static void
9461process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9462{
9463 struct attribute *attr;
9464
f4dc4d17
DE
9465 /* For now we don't handle imported units in type units. */
9466 if (cu->per_cu->is_debug_types)
9467 {
9468 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9469 " supported in type units [in module %s]"),
5e22e966 9470 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9471 }
9472
95554aad
TT
9473 attr = dwarf2_attr (die, DW_AT_import, cu);
9474 if (attr != NULL)
9475 {
0826b30a 9476 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9477 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9478 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9479 dwarf2_per_cu_data *per_cu
ab432490 9480 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9481
58990295
TV
9482 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9483 into another compilation unit, at root level. Regard this as a hint,
9484 and ignore it. */
9485 if (die->parent && die->parent->parent == NULL
9486 && per_cu->unit_type == DW_UT_compile
9487 && per_cu->lang == language_cplus)
9488 return;
9489
69d751e3 9490 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 9491 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
9492 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9493 false, cu->language);
95554aad 9494
ae640021 9495 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9496 }
9497}
9498
4c8aa72d
PA
9499/* RAII object that represents a process_die scope: i.e.,
9500 starts/finishes processing a DIE. */
9501class process_die_scope
adde2bff 9502{
4c8aa72d
PA
9503public:
9504 process_die_scope (die_info *die, dwarf2_cu *cu)
9505 : m_die (die), m_cu (cu)
9506 {
9507 /* We should only be processing DIEs not already in process. */
9508 gdb_assert (!m_die->in_process);
9509 m_die->in_process = true;
9510 }
8c3cb9fa 9511
4c8aa72d
PA
9512 ~process_die_scope ()
9513 {
9514 m_die->in_process = false;
9515
9516 /* If we're done processing the DIE for the CU that owns the line
9517 header, we don't need the line header anymore. */
9518 if (m_cu->line_header_die_owner == m_die)
9519 {
9520 delete m_cu->line_header;
9521 m_cu->line_header = NULL;
9522 m_cu->line_header_die_owner = NULL;
9523 }
9524 }
9525
9526private:
9527 die_info *m_die;
9528 dwarf2_cu *m_cu;
9529};
adde2bff 9530
c906108c
SS
9531/* Process a die and its children. */
9532
9533static void
e7c27a73 9534process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9535{
4c8aa72d 9536 process_die_scope scope (die, cu);
adde2bff 9537
c906108c
SS
9538 switch (die->tag)
9539 {
9540 case DW_TAG_padding:
9541 break;
9542 case DW_TAG_compile_unit:
95554aad 9543 case DW_TAG_partial_unit:
e7c27a73 9544 read_file_scope (die, cu);
c906108c 9545 break;
348e048f
DE
9546 case DW_TAG_type_unit:
9547 read_type_unit_scope (die, cu);
9548 break;
c906108c 9549 case DW_TAG_subprogram:
0a4b0913
AB
9550 /* Nested subprograms in Fortran get a prefix. */
9551 if (cu->language == language_fortran
9552 && die->parent != NULL
9553 && die->parent->tag == DW_TAG_subprogram)
9554 cu->processing_has_namespace_info = true;
9555 /* Fall through. */
c906108c 9556 case DW_TAG_inlined_subroutine:
edb3359d 9557 read_func_scope (die, cu);
c906108c
SS
9558 break;
9559 case DW_TAG_lexical_block:
14898363
L
9560 case DW_TAG_try_block:
9561 case DW_TAG_catch_block:
e7c27a73 9562 read_lexical_block_scope (die, cu);
c906108c 9563 break;
216f72a1 9564 case DW_TAG_call_site:
96408a79
SA
9565 case DW_TAG_GNU_call_site:
9566 read_call_site_scope (die, cu);
9567 break;
c906108c 9568 case DW_TAG_class_type:
680b30c7 9569 case DW_TAG_interface_type:
c906108c
SS
9570 case DW_TAG_structure_type:
9571 case DW_TAG_union_type:
134d01f1 9572 process_structure_scope (die, cu);
c906108c
SS
9573 break;
9574 case DW_TAG_enumeration_type:
134d01f1 9575 process_enumeration_scope (die, cu);
c906108c 9576 break;
134d01f1 9577
f792889a
DJ
9578 /* These dies have a type, but processing them does not create
9579 a symbol or recurse to process the children. Therefore we can
9580 read them on-demand through read_type_die. */
c906108c 9581 case DW_TAG_subroutine_type:
72019c9c 9582 case DW_TAG_set_type:
c906108c 9583 case DW_TAG_pointer_type:
c906108c 9584 case DW_TAG_ptr_to_member_type:
c906108c 9585 case DW_TAG_reference_type:
4297a3f0 9586 case DW_TAG_rvalue_reference_type:
c906108c 9587 case DW_TAG_string_type:
c906108c 9588 break;
134d01f1 9589
d8f62e84
TT
9590 case DW_TAG_array_type:
9591 /* We only need to handle this case for Ada -- in other
9592 languages, it's normal for the compiler to emit a typedef
9593 instead. */
9594 if (cu->language != language_ada)
9595 break;
9596 /* FALLTHROUGH */
c906108c 9597 case DW_TAG_base_type:
a02abb62 9598 case DW_TAG_subrange_type:
cb249c71 9599 case DW_TAG_typedef:
134d01f1 9600 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 9601 DW_AT_name. */
f792889a 9602 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9603 break;
c906108c 9604 case DW_TAG_common_block:
e7c27a73 9605 read_common_block (die, cu);
c906108c
SS
9606 break;
9607 case DW_TAG_common_inclusion:
9608 break;
d9fa45fe 9609 case DW_TAG_namespace:
9068261f 9610 cu->processing_has_namespace_info = true;
e7c27a73 9611 read_namespace (die, cu);
d9fa45fe 9612 break;
5d7cb8df 9613 case DW_TAG_module:
9068261f 9614 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9615 read_module (die, cu);
9616 break;
d9fa45fe 9617 case DW_TAG_imported_declaration:
9068261f 9618 cu->processing_has_namespace_info = true;
74921315
KS
9619 if (read_namespace_alias (die, cu))
9620 break;
86a73007
TT
9621 /* The declaration is not a global namespace alias. */
9622 /* Fall through. */
d9fa45fe 9623 case DW_TAG_imported_module:
9068261f 9624 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9625 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9626 || cu->language != language_fortran))
b98664d3 9627 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9628 dwarf_tag_name (die->tag));
9629 read_import_statement (die, cu);
d9fa45fe 9630 break;
95554aad
TT
9631
9632 case DW_TAG_imported_unit:
9633 process_imported_unit_die (die, cu);
9634 break;
9635
71a3c369
TT
9636 case DW_TAG_variable:
9637 read_variable (die, cu);
9638 break;
9639
c906108c 9640 default:
e7c27a73 9641 new_symbol (die, NULL, cu);
c906108c
SS
9642 break;
9643 }
9644}
ca69b9e6
DE
9645\f
9646/* DWARF name computation. */
c906108c 9647
94af9270
KS
9648/* A helper function for dwarf2_compute_name which determines whether DIE
9649 needs to have the name of the scope prepended to the name listed in the
9650 die. */
9651
9652static int
9653die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9654{
1c809c68
TT
9655 struct attribute *attr;
9656
94af9270
KS
9657 switch (die->tag)
9658 {
9659 case DW_TAG_namespace:
9660 case DW_TAG_typedef:
9661 case DW_TAG_class_type:
9662 case DW_TAG_interface_type:
9663 case DW_TAG_structure_type:
9664 case DW_TAG_union_type:
9665 case DW_TAG_enumeration_type:
9666 case DW_TAG_enumerator:
9667 case DW_TAG_subprogram:
08a76f8a 9668 case DW_TAG_inlined_subroutine:
94af9270 9669 case DW_TAG_member:
74921315 9670 case DW_TAG_imported_declaration:
94af9270
KS
9671 return 1;
9672
9673 case DW_TAG_variable:
c2b0a229 9674 case DW_TAG_constant:
94af9270
KS
9675 /* We only need to prefix "globally" visible variables. These include
9676 any variable marked with DW_AT_external or any variable that
9677 lives in a namespace. [Variables in anonymous namespaces
9678 require prefixing, but they are not DW_AT_external.] */
9679
9680 if (dwarf2_attr (die, DW_AT_specification, cu))
9681 {
9682 struct dwarf2_cu *spec_cu = cu;
9a619af0 9683
94af9270
KS
9684 return die_needs_namespace (die_specification (die, &spec_cu),
9685 spec_cu);
9686 }
9687
1c809c68 9688 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9689 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9690 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9691 return 0;
9692 /* A variable in a lexical block of some kind does not need a
9693 namespace, even though in C++ such variables may be external
9694 and have a mangled name. */
9695 if (die->parent->tag == DW_TAG_lexical_block
9696 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9697 || die->parent->tag == DW_TAG_catch_block
9698 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9699 return 0;
9700 return 1;
94af9270
KS
9701
9702 default:
9703 return 0;
9704 }
9705}
9706
73b9be8b
KS
9707/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9708 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9709 defined for the given DIE. */
9710
9711static struct attribute *
9712dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9713{
9714 struct attribute *attr;
9715
9716 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9717 if (attr == NULL)
9718 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9719
9720 return attr;
9721}
9722
9723/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9724 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9725 defined for the given DIE. */
9726
9727static const char *
9728dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9729{
9730 const char *linkage_name;
9731
9732 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9733 if (linkage_name == NULL)
9734 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9735
787de330
TT
9736 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9737 See https://github.com/rust-lang/rust/issues/32925. */
9738 if (cu->language == language_rust && linkage_name != NULL
9739 && strchr (linkage_name, '{') != NULL)
9740 linkage_name = NULL;
9741
73b9be8b
KS
9742 return linkage_name;
9743}
9744
94af9270 9745/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9746 compute the physname for the object, which include a method's:
9c37b5ae 9747 - formal parameters (C++),
a766d390 9748 - receiver type (Go),
a766d390
DE
9749
9750 The term "physname" is a bit confusing.
9751 For C++, for example, it is the demangled name.
9752 For Go, for example, it's the mangled name.
94af9270 9753
af6b7be1
JB
9754 For Ada, return the DIE's linkage name rather than the fully qualified
9755 name. PHYSNAME is ignored..
9756
5989a64e 9757 The result is allocated on the objfile->per_bfd's obstack and
45940949 9758 canonicalized. */
94af9270
KS
9759
9760static const char *
15d034d0
TT
9761dwarf2_compute_name (const char *name,
9762 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9763 int physname)
9764{
5e22e966 9765 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 9766
94af9270
KS
9767 if (name == NULL)
9768 name = dwarf2_name (die, cu);
9769
2ee7123e
DE
9770 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9771 but otherwise compute it by typename_concat inside GDB.
9772 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 9773 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 9774 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
9775 will set the demangled name to the result of dwarf2_full_name, and it is
9776 the demangled name that GDB uses if it exists. */
f55ee35c
JK
9777 if (cu->language == language_ada
9778 || (cu->language == language_fortran && physname))
9779 {
9780 /* For Ada unit, we prefer the linkage name over the name, as
9781 the former contains the exported name, which the user expects
9782 to be able to reference. Ideally, we want the user to be able
9783 to reference this entity using either natural or linkage name,
9784 but we haven't started looking at this enhancement yet. */
73b9be8b 9785 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 9786
2ee7123e
DE
9787 if (linkage_name != NULL)
9788 return linkage_name;
f55ee35c
JK
9789 }
9790
94af9270
KS
9791 /* These are the only languages we know how to qualify names in. */
9792 if (name != NULL
9c37b5ae 9793 && (cu->language == language_cplus
c44af4eb
TT
9794 || cu->language == language_fortran || cu->language == language_d
9795 || cu->language == language_rust))
94af9270
KS
9796 {
9797 if (die_needs_namespace (die, cu))
9798 {
0d5cff50 9799 const char *prefix;
34a68019 9800 const char *canonical_name = NULL;
94af9270 9801
d7e74731
PA
9802 string_file buf;
9803
94af9270 9804 prefix = determine_prefix (die, cu);
94af9270
KS
9805 if (*prefix != '\0')
9806 {
43816ebc
TT
9807 gdb::unique_xmalloc_ptr<char> prefixed_name
9808 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 9809
43816ebc 9810 buf.puts (prefixed_name.get ());
94af9270
KS
9811 }
9812 else
d7e74731 9813 buf.puts (name);
94af9270 9814
98bfdba5
PA
9815 /* Template parameters may be specified in the DIE's DW_AT_name, or
9816 as children with DW_TAG_template_type_param or
9817 DW_TAG_value_type_param. If the latter, add them to the name
9818 here. If the name already has template parameters, then
9819 skip this step; some versions of GCC emit both, and
9820 it is more efficient to use the pre-computed name.
9821
9822 Something to keep in mind about this process: it is very
9823 unlikely, or in some cases downright impossible, to produce
9824 something that will match the mangled name of a function.
9825 If the definition of the function has the same debug info,
9826 we should be able to match up with it anyway. But fallbacks
9827 using the minimal symbol, for instance to find a method
9828 implemented in a stripped copy of libstdc++, will not work.
9829 If we do not have debug info for the definition, we will have to
9830 match them up some other way.
9831
9832 When we do name matching there is a related problem with function
9833 templates; two instantiated function templates are allowed to
9834 differ only by their return types, which we do not add here. */
9835
9836 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9837 {
9838 struct attribute *attr;
9839 struct die_info *child;
9840 int first = 1;
2c75ccb2 9841 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
9842
9843 die->building_fullname = 1;
9844
9845 for (child = die->child; child != NULL; child = child->sibling)
9846 {
9847 struct type *type;
12df843f 9848 LONGEST value;
d521ce57 9849 const gdb_byte *bytes;
98bfdba5
PA
9850 struct dwarf2_locexpr_baton *baton;
9851 struct value *v;
9852
9853 if (child->tag != DW_TAG_template_type_param
9854 && child->tag != DW_TAG_template_value_param)
9855 continue;
9856
9857 if (first)
9858 {
d7e74731 9859 buf.puts ("<");
98bfdba5
PA
9860 first = 0;
9861 }
9862 else
d7e74731 9863 buf.puts (", ");
98bfdba5
PA
9864
9865 attr = dwarf2_attr (child, DW_AT_type, cu);
9866 if (attr == NULL)
9867 {
b98664d3 9868 complaint (_("template parameter missing DW_AT_type"));
d7e74731 9869 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
9870 continue;
9871 }
9872 type = die_type (child, cu);
9873
9874 if (child->tag == DW_TAG_template_type_param)
9875 {
2c75ccb2
AB
9876 cplus_lang->print_type (type, "", &buf, -1, 0,
9877 &type_print_raw_options);
98bfdba5
PA
9878 continue;
9879 }
9880
9881 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9882 if (attr == NULL)
9883 {
b98664d3 9884 complaint (_("template parameter missing "
3e43a32a 9885 "DW_AT_const_value"));
d7e74731 9886 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
9887 continue;
9888 }
9889
9890 dwarf2_const_value_attr (attr, type, name,
9891 &cu->comp_unit_obstack, cu,
9892 &value, &bytes, &baton);
9893
20ce4123 9894 if (type->has_no_signedness ())
98bfdba5
PA
9895 /* GDB prints characters as NUMBER 'CHAR'. If that's
9896 changed, this can use value_print instead. */
2c75ccb2 9897 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
9898 else
9899 {
9900 struct value_print_options opts;
9901
9902 if (baton != NULL)
9903 v = dwarf2_evaluate_loc_desc (type, NULL,
9904 baton->data,
9905 baton->size,
9f47c707
SM
9906 baton->per_cu,
9907 baton->per_objfile);
98bfdba5
PA
9908 else if (bytes != NULL)
9909 {
9910 v = allocate_value (type);
9911 memcpy (value_contents_writeable (v), bytes,
9912 TYPE_LENGTH (type));
9913 }
9914 else
9915 v = value_from_longest (type, value);
9916
3e43a32a
MS
9917 /* Specify decimal so that we do not depend on
9918 the radix. */
98bfdba5
PA
9919 get_formatted_print_options (&opts, 'd');
9920 opts.raw = 1;
d7e74731 9921 value_print (v, &buf, &opts);
98bfdba5 9922 release_value (v);
98bfdba5
PA
9923 }
9924 }
9925
9926 die->building_fullname = 0;
9927
9928 if (!first)
9929 {
9930 /* Close the argument list, with a space if necessary
9931 (nested templates). */
d7e74731
PA
9932 if (!buf.empty () && buf.string ().back () == '>')
9933 buf.puts (" >");
98bfdba5 9934 else
d7e74731 9935 buf.puts (">");
98bfdba5
PA
9936 }
9937 }
9938
9c37b5ae 9939 /* For C++ methods, append formal parameter type
94af9270 9940 information, if PHYSNAME. */
6e70227d 9941
94af9270 9942 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 9943 && cu->language == language_cplus)
94af9270
KS
9944 {
9945 struct type *type = read_type_die (die, cu);
9946
d7e74731 9947 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 9948 &type_print_raw_options);
94af9270 9949
9c37b5ae 9950 if (cu->language == language_cplus)
94af9270 9951 {
60430eff
DJ
9952 /* Assume that an artificial first parameter is
9953 "this", but do not crash if it is not. RealView
9954 marks unnamed (and thus unused) parameters as
9955 artificial; there is no way to differentiate
9956 the two cases. */
1f704f76 9957 if (type->num_fields () > 0
94af9270 9958 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
9959 && type->field (0).type ()->code () == TYPE_CODE_PTR
9960 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 9961 buf.puts (" const");
94af9270
KS
9962 }
9963 }
9964
d7e74731 9965 const std::string &intermediate_name = buf.string ();
94af9270
KS
9966
9967 if (cu->language == language_cplus)
34a68019 9968 canonical_name
322a8516 9969 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 9970 objfile);
34a68019
TT
9971
9972 /* If we only computed INTERMEDIATE_NAME, or if
9973 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 9974 intern it. */
322a8516 9975 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 9976 name = objfile->intern (intermediate_name);
34a68019
TT
9977 else
9978 name = canonical_name;
94af9270
KS
9979 }
9980 }
9981
9982 return name;
9983}
9984
0114d602
DJ
9985/* Return the fully qualified name of DIE, based on its DW_AT_name.
9986 If scope qualifiers are appropriate they will be added. The result
34a68019 9987 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
9988 not have a name. NAME may either be from a previous call to
9989 dwarf2_name or NULL.
9990
9c37b5ae 9991 The output string will be canonicalized (if C++). */
0114d602
DJ
9992
9993static const char *
15d034d0 9994dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 9995{
94af9270
KS
9996 return dwarf2_compute_name (name, die, cu, 0);
9997}
0114d602 9998
94af9270
KS
9999/* Construct a physname for the given DIE in CU. NAME may either be
10000 from a previous call to dwarf2_name or NULL. The result will be
10001 allocated on the objfile_objstack or NULL if the DIE does not have a
10002 name.
0114d602 10003
9c37b5ae 10004 The output string will be canonicalized (if C++). */
0114d602 10005
94af9270 10006static const char *
15d034d0 10007dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10008{
5e22e966 10009 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10010 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10011 int need_copy = 1;
10012
10013 /* In this case dwarf2_compute_name is just a shortcut not building anything
10014 on its own. */
10015 if (!die_needs_namespace (die, cu))
10016 return dwarf2_compute_name (name, die, cu, 1);
10017
906bb4c5
TT
10018 if (cu->language != language_rust)
10019 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10020
10021 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10022 has computed. */
791afaa2 10023 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10024 if (mangled != NULL)
900e11f9 10025 {
900e11f9 10026
d3355e4d 10027 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10028 {
10029 /* Do nothing (do not demangle the symbol name). */
10030 }
a766d390
DE
10031 else
10032 {
0eb876f5
JB
10033 /* Use DMGL_RET_DROP for C++ template functions to suppress
10034 their return type. It is easier for GDB users to search
10035 for such functions as `name(params)' than `long name(params)'.
10036 In such case the minimal symbol names do not match the full
10037 symbol names but for template functions there is never a need
10038 to look up their definition from their declaration so
10039 the only disadvantage remains the minimal symbol variant
10040 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10041 demangled.reset (gdb_demangle (mangled,
10042 (DMGL_PARAMS | DMGL_ANSI
10043 | DMGL_RET_DROP)));
a766d390 10044 }
900e11f9 10045 if (demangled)
791afaa2 10046 canon = demangled.get ();
900e11f9
JK
10047 else
10048 {
10049 canon = mangled;
10050 need_copy = 0;
10051 }
10052 }
10053
10054 if (canon == NULL || check_physname)
10055 {
10056 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10057
10058 if (canon != NULL && strcmp (physname, canon) != 0)
10059 {
10060 /* It may not mean a bug in GDB. The compiler could also
10061 compute DW_AT_linkage_name incorrectly. But in such case
10062 GDB would need to be bug-to-bug compatible. */
10063
b98664d3 10064 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10065 "(from linkage <%s>) - DIE at %s [in module %s]"),
10066 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10067 objfile_name (objfile));
900e11f9
JK
10068
10069 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10070 is available here - over computed PHYSNAME. It is safer
10071 against both buggy GDB and buggy compilers. */
10072
10073 retval = canon;
10074 }
10075 else
10076 {
10077 retval = physname;
10078 need_copy = 0;
10079 }
10080 }
10081 else
10082 retval = canon;
10083
10084 if (need_copy)
be1e3d3e 10085 retval = objfile->intern (retval);
900e11f9 10086
900e11f9 10087 return retval;
0114d602
DJ
10088}
10089
74921315
KS
10090/* Inspect DIE in CU for a namespace alias. If one exists, record
10091 a new symbol for it.
10092
10093 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10094
10095static int
10096read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10097{
10098 struct attribute *attr;
10099
10100 /* If the die does not have a name, this is not a namespace
10101 alias. */
10102 attr = dwarf2_attr (die, DW_AT_name, cu);
10103 if (attr != NULL)
10104 {
10105 int num;
10106 struct die_info *d = die;
10107 struct dwarf2_cu *imported_cu = cu;
10108
10109 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10110 keep inspecting DIEs until we hit the underlying import. */
10111#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10112 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10113 {
10114 attr = dwarf2_attr (d, DW_AT_import, cu);
10115 if (attr == NULL)
10116 break;
10117
10118 d = follow_die_ref (d, attr, &imported_cu);
10119 if (d->tag != DW_TAG_imported_declaration)
10120 break;
10121 }
10122
10123 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10124 {
b98664d3 10125 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10126 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10127 return 0;
10128 }
10129
10130 if (attr != NULL)
10131 {
10132 struct type *type;
0826b30a 10133 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10134
aa66c379 10135 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10136 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10137 {
10138 /* This declaration is a global namespace alias. Add
10139 a symbol for it whose type is the aliased namespace. */
10140 new_symbol (die, type, cu);
10141 return 1;
10142 }
10143 }
10144 }
10145
10146 return 0;
10147}
10148
22cee43f 10149/* Return the using directives repository (global or local?) to use in the
804d2729 10150 current context for CU.
22cee43f
PMR
10151
10152 For Ada, imported declarations can materialize renamings, which *may* be
10153 global. However it is impossible (for now?) in DWARF to distinguish
10154 "external" imported declarations and "static" ones. As all imported
10155 declarations seem to be static in all other languages, make them all CU-wide
10156 global only in Ada. */
10157
10158static struct using_direct **
804d2729 10159using_directives (struct dwarf2_cu *cu)
22cee43f 10160{
c24bdb02
KS
10161 if (cu->language == language_ada
10162 && cu->get_builder ()->outermost_context_p ())
10163 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10164 else
c24bdb02 10165 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10166}
10167
27aa8d6a
SW
10168/* Read the import statement specified by the given die and record it. */
10169
10170static void
10171read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10172{
5e22e966 10173 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10174 struct attribute *import_attr;
32019081 10175 struct die_info *imported_die, *child_die;
de4affc9 10176 struct dwarf2_cu *imported_cu;
27aa8d6a 10177 const char *imported_name;
794684b6 10178 const char *imported_name_prefix;
13387711
SW
10179 const char *canonical_name;
10180 const char *import_alias;
10181 const char *imported_declaration = NULL;
794684b6 10182 const char *import_prefix;
eb1e02fd 10183 std::vector<const char *> excludes;
13387711 10184
27aa8d6a
SW
10185 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10186 if (import_attr == NULL)
10187 {
b98664d3 10188 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10189 dwarf_tag_name (die->tag));
10190 return;
10191 }
10192
de4affc9
CC
10193 imported_cu = cu;
10194 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10195 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10196 if (imported_name == NULL)
10197 {
10198 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10199
dda83cd7
SM
10200 The import in the following code:
10201 namespace A
10202 {
10203 typedef int B;
10204 }
10205
10206 int main ()
10207 {
10208 using A::B;
10209 B b;
10210 return b;
10211 }
10212
10213 ...
10214 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10215 <52> DW_AT_decl_file : 1
10216 <53> DW_AT_decl_line : 6
10217 <54> DW_AT_import : <0x75>
10218 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10219 <59> DW_AT_name : B
10220 <5b> DW_AT_decl_file : 1
10221 <5c> DW_AT_decl_line : 2
10222 <5d> DW_AT_type : <0x6e>
10223 ...
10224 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10225 <76> DW_AT_byte_size : 4
10226 <77> DW_AT_encoding : 5 (signed)
10227
10228 imports the wrong die ( 0x75 instead of 0x58 ).
10229 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10230 return;
10231 }
10232
82856980
SW
10233 /* Figure out the local name after import. */
10234 import_alias = dwarf2_name (die, cu);
27aa8d6a 10235
794684b6
SW
10236 /* Figure out where the statement is being imported to. */
10237 import_prefix = determine_prefix (die, cu);
10238
10239 /* Figure out what the scope of the imported die is and prepend it
10240 to the name of the imported die. */
de4affc9 10241 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10242
f55ee35c
JK
10243 if (imported_die->tag != DW_TAG_namespace
10244 && imported_die->tag != DW_TAG_module)
794684b6 10245 {
13387711
SW
10246 imported_declaration = imported_name;
10247 canonical_name = imported_name_prefix;
794684b6 10248 }
13387711 10249 else if (strlen (imported_name_prefix) > 0)
12aaed36 10250 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10251 imported_name_prefix,
10252 (cu->language == language_d ? "." : "::"),
10253 imported_name, (char *) NULL);
13387711
SW
10254 else
10255 canonical_name = imported_name;
794684b6 10256
32019081
JK
10257 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10258 for (child_die = die->child; child_die && child_die->tag;
436c571c 10259 child_die = child_die->sibling)
32019081
JK
10260 {
10261 /* DWARF-4: A Fortran use statement with a “rename list” may be
10262 represented by an imported module entry with an import attribute
10263 referring to the module and owned entries corresponding to those
10264 entities that are renamed as part of being imported. */
10265
10266 if (child_die->tag != DW_TAG_imported_declaration)
10267 {
b98664d3 10268 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10269 "- DIE at %s [in module %s]"),
10270 sect_offset_str (child_die->sect_off),
10271 objfile_name (objfile));
32019081
JK
10272 continue;
10273 }
10274
10275 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10276 if (import_attr == NULL)
10277 {
b98664d3 10278 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10279 dwarf_tag_name (child_die->tag));
10280 continue;
10281 }
10282
10283 imported_cu = cu;
10284 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10285 &imported_cu);
10286 imported_name = dwarf2_name (imported_die, imported_cu);
10287 if (imported_name == NULL)
10288 {
b98664d3 10289 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10290 "imported name - DIE at %s [in module %s]"),
10291 sect_offset_str (child_die->sect_off),
10292 objfile_name (objfile));
32019081
JK
10293 continue;
10294 }
10295
eb1e02fd 10296 excludes.push_back (imported_name);
32019081
JK
10297
10298 process_die (child_die, cu);
10299 }
10300
804d2729 10301 add_using_directive (using_directives (cu),
22cee43f
PMR
10302 import_prefix,
10303 canonical_name,
10304 import_alias,
10305 imported_declaration,
10306 excludes,
10307 0,
10308 &objfile->objfile_obstack);
27aa8d6a
SW
10309}
10310
5230b05a
WT
10311/* ICC<14 does not output the required DW_AT_declaration on incomplete
10312 types, but gives them a size of zero. Starting with version 14,
10313 ICC is compatible with GCC. */
10314
9068261f 10315static bool
5230b05a
WT
10316producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10317{
10318 if (!cu->checked_producer)
10319 check_producer (cu);
10320
10321 return cu->producer_is_icc_lt_14;
10322}
10323
eb77c9df
AB
10324/* ICC generates a DW_AT_type for C void functions. This was observed on
10325 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10326 which says that void functions should not have a DW_AT_type. */
10327
10328static bool
10329producer_is_icc (struct dwarf2_cu *cu)
10330{
10331 if (!cu->checked_producer)
10332 check_producer (cu);
10333
10334 return cu->producer_is_icc;
10335}
10336
1b80a9fa
JK
10337/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10338 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10339 this, it was first present in GCC release 4.3.0. */
10340
9068261f 10341static bool
1b80a9fa
JK
10342producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10343{
10344 if (!cu->checked_producer)
10345 check_producer (cu);
10346
10347 return cu->producer_is_gcc_lt_4_3;
10348}
10349
d721ba37
PA
10350static file_and_directory
10351find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10352{
d721ba37
PA
10353 file_and_directory res;
10354
9291a0cd
TT
10355 /* Find the filename. Do not use dwarf2_name here, since the filename
10356 is not a source language identifier. */
d721ba37
PA
10357 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10358 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10359
d721ba37
PA
10360 if (res.comp_dir == NULL
10361 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10362 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10363 {
d721ba37
PA
10364 res.comp_dir_storage = ldirname (res.name);
10365 if (!res.comp_dir_storage.empty ())
10366 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10367 }
d721ba37 10368 if (res.comp_dir != NULL)
9291a0cd
TT
10369 {
10370 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10371 directory, get rid of it. */
d721ba37 10372 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10373
d721ba37
PA
10374 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10375 res.comp_dir = cp + 1;
9291a0cd
TT
10376 }
10377
d721ba37
PA
10378 if (res.name == NULL)
10379 res.name = "<unknown>";
10380
10381 return res;
9291a0cd
TT
10382}
10383
f4dc4d17
DE
10384/* Handle DW_AT_stmt_list for a compilation unit.
10385 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10386 COMP_DIR is the compilation directory. LOWPC is passed to
10387 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10388
10389static void
10390handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10391 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10392{
976ca316 10393 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10394 struct attribute *attr;
527f3840
JK
10395 struct line_header line_header_local;
10396 hashval_t line_header_local_hash;
527f3840
JK
10397 void **slot;
10398 int decode_mapping;
2ab95328 10399
f4dc4d17
DE
10400 gdb_assert (! cu->per_cu->is_debug_types);
10401
2ab95328 10402 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 10403 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
10404 return;
10405
d4df075e 10406 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
10407
10408 /* The line header hash table is only created if needed (it exists to
10409 prevent redundant reading of the line table for partial_units).
10410 If we're given a partial_unit, we'll need it. If we're given a
10411 compile_unit, then use the line header hash table if it's already
10412 created, but don't create one just yet. */
10413
976ca316 10414 if (per_objfile->line_header_hash == NULL
527f3840 10415 && die->tag == DW_TAG_partial_unit)
2ab95328 10416 {
976ca316 10417 per_objfile->line_header_hash
d15acc42
TT
10418 .reset (htab_create_alloc (127, line_header_hash_voidp,
10419 line_header_eq_voidp,
ef5f598c 10420 htab_delete_entry<line_header>,
d15acc42 10421 xcalloc, xfree));
527f3840 10422 }
2ab95328 10423
9c541725 10424 line_header_local.sect_off = line_offset;
527f3840
JK
10425 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10426 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10427 if (per_objfile->line_header_hash != NULL)
527f3840 10428 {
976ca316 10429 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10430 &line_header_local,
10431 line_header_local_hash, NO_INSERT);
10432
10433 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10434 is not present in *SLOT (since if there is something in *SLOT then
10435 it will be for a partial_unit). */
10436 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10437 {
527f3840 10438 gdb_assert (*slot != NULL);
9a3c8263 10439 cu->line_header = (struct line_header *) *slot;
527f3840 10440 return;
dee91e82 10441 }
2ab95328 10442 }
527f3840
JK
10443
10444 /* dwarf_decode_line_header does not yet provide sufficient information.
10445 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10446 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10447 if (lh == NULL)
527f3840 10448 return;
4c8aa72d
PA
10449
10450 cu->line_header = lh.release ();
10451 cu->line_header_die_owner = die;
527f3840 10452
976ca316 10453 if (per_objfile->line_header_hash == NULL)
527f3840
JK
10454 slot = NULL;
10455 else
10456 {
976ca316 10457 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10458 &line_header_local,
10459 line_header_local_hash, INSERT);
10460 gdb_assert (slot != NULL);
10461 }
10462 if (slot != NULL && *slot == NULL)
10463 {
10464 /* This newly decoded line number information unit will be owned
10465 by line_header_hash hash table. */
10466 *slot = cu->line_header;
4c8aa72d 10467 cu->line_header_die_owner = NULL;
527f3840
JK
10468 }
10469 else
10470 {
10471 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 10472 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
10473 line_header for this CU - it may happen at most once for each line
10474 number information unit. And if we're not using line_header_hash
10475 then this is what we want as well. */
10476 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10477 }
10478 decode_mapping = (die->tag != DW_TAG_partial_unit);
10479 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10480 decode_mapping);
fff8551c 10481
2ab95328
TT
10482}
10483
95554aad 10484/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10485
c906108c 10486static void
e7c27a73 10487read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10488{
976ca316
SM
10489 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10490 struct objfile *objfile = per_objfile->objfile;
08feed99 10491 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10492 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10493 CORE_ADDR highpc = ((CORE_ADDR) 0);
10494 struct attribute *attr;
c906108c 10495 struct die_info *child_die;
e142c38c 10496 CORE_ADDR baseaddr;
6e70227d 10497
380618d6 10498 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10499 baseaddr = objfile->text_section_offset ();
c906108c 10500
fae299cd 10501 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10502
10503 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10504 from finish_block. */
2acceee2 10505 if (lowpc == ((CORE_ADDR) -1))
c906108c 10506 lowpc = highpc;
3e29f34a 10507 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10508
d721ba37 10509 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10510
f4b8a18d
KW
10511 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10512 standardised yet. As a workaround for the language detection we fall
10513 back to the DW_AT_producer string. */
10514 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10515 cu->language = language_opencl;
10516
3019eac3
DE
10517 /* Similar hack for Go. */
10518 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10519 set_cu_language (DW_LANG_Go, cu);
10520
c24bdb02 10521 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10522
10523 /* Decode line number information if present. We do this before
10524 processing child DIEs, so that the line header table is available
10525 for DW_AT_decl_file. */
d721ba37 10526 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10527
10528 /* Process all dies in compilation unit. */
10529 if (die->child != NULL)
10530 {
10531 child_die = die->child;
10532 while (child_die && child_die->tag)
10533 {
10534 process_die (child_die, cu);
436c571c 10535 child_die = child_die->sibling;
3019eac3
DE
10536 }
10537 }
10538
10539 /* Decode macro information, if present. Dwarf 2 macro information
10540 refers to information in the line number info statement program
10541 header, so we can only read it if we've read the header
10542 successfully. */
0af92d60
JK
10543 attr = dwarf2_attr (die, DW_AT_macros, cu);
10544 if (attr == NULL)
10545 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 10546 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
10547 {
10548 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10549 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10550
529908cb 10551 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
10552 }
10553 else
10554 {
10555 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 10556 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 10557 {
529908cb 10558 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 10559
43f3e411 10560 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10561 }
10562 }
3019eac3
DE
10563}
10564
c24bdb02
KS
10565void
10566dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10567{
f4dc4d17
DE
10568 struct type_unit_group *tu_group;
10569 int first_time;
3019eac3 10570 struct attribute *attr;
9c541725 10571 unsigned int i;
0186c6a7 10572 struct signatured_type *sig_type;
3019eac3 10573
f4dc4d17 10574 gdb_assert (per_cu->is_debug_types);
0186c6a7 10575 sig_type = (struct signatured_type *) per_cu;
3019eac3 10576
c24bdb02 10577 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10578
f4dc4d17 10579 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10580 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10581 if (sig_type->type_unit_group == NULL)
c24bdb02 10582 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10583 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10584
10585 /* If we've already processed this stmt_list there's no real need to
10586 do it again, we could fake it and just recreate the part we need
10587 (file name,index -> symtab mapping). If data shows this optimization
10588 is useful we can do it then. */
8adb8487
TT
10589 type_unit_group_unshareable *tug_unshare
10590 = per_objfile->get_type_unit_group_unshareable (tu_group);
10591 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
10592
10593 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10594 debug info. */
fff8551c 10595 line_header_up lh;
d4df075e 10596 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 10597 {
d4df075e 10598 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 10599 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10600 }
10601 if (lh == NULL)
10602 {
10603 if (first_time)
c24bdb02 10604 start_symtab ("", NULL, 0);
f4dc4d17
DE
10605 else
10606 {
8adb8487 10607 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 10608 gdb_assert (m_builder == nullptr);
8adb8487 10609 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10610 m_builder.reset (new struct buildsym_compunit
10611 (COMPUNIT_OBJFILE (cust), "",
10612 COMPUNIT_DIRNAME (cust),
10613 compunit_language (cust),
10614 0, cust));
770479f2 10615 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10616 }
f4dc4d17 10617 return;
3019eac3
DE
10618 }
10619
c24bdb02
KS
10620 line_header = lh.release ();
10621 line_header_die_owner = die;
3019eac3 10622
f4dc4d17
DE
10623 if (first_time)
10624 {
c24bdb02 10625 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10626
1fd60fc0
DE
10627 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10628 still initializing it, and our caller (a few levels up)
10629 process_full_type_unit still needs to know if this is the first
10630 time. */
10631
8adb8487 10632 tug_unshare->symtabs
4ac93832
TT
10633 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10634 struct symtab *, line_header->file_names_size ());
3019eac3 10635
7ba99d21
AT
10636 auto &file_names = line_header->file_names ();
10637 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10638 {
7ba99d21 10639 file_entry &fe = file_names[i];
c24bdb02
KS
10640 dwarf2_start_subfile (this, fe.name,
10641 fe.include_dir (line_header));
10642 buildsym_compunit *b = get_builder ();
10643 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10644 {
4c8aa72d
PA
10645 /* NOTE: start_subfile will recognize when it's been
10646 passed a file it has already seen. So we can't
10647 assume there's a simple mapping from
10648 cu->line_header->file_names to subfiles, plus
10649 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10650 b->get_current_subfile ()->symtab
10651 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10652 }
10653
c24bdb02 10654 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 10655 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
10656 }
10657 }
10658 else
3019eac3 10659 {
c24bdb02 10660 gdb_assert (m_builder == nullptr);
8adb8487 10661 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10662 m_builder.reset (new struct buildsym_compunit
10663 (COMPUNIT_OBJFILE (cust), "",
10664 COMPUNIT_DIRNAME (cust),
10665 compunit_language (cust),
10666 0, cust));
770479f2 10667 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10668
7ba99d21
AT
10669 auto &file_names = line_header->file_names ();
10670 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10671 {
7ba99d21 10672 file_entry &fe = file_names[i];
8adb8487 10673 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 10674 }
3019eac3
DE
10675 }
10676
f4dc4d17
DE
10677 /* The main symtab is allocated last. Type units don't have DW_AT_name
10678 so they don't have a "real" (so to speak) symtab anyway.
10679 There is later code that will assign the main symtab to all symbols
10680 that don't have one. We need to handle the case of a symbol with a
10681 missing symtab (DW_AT_decl_file) anyway. */
10682}
3019eac3 10683
f4dc4d17
DE
10684/* Process DW_TAG_type_unit.
10685 For TUs we want to skip the first top level sibling if it's not the
10686 actual type being defined by this TU. In this case the first top
10687 level sibling is there to provide context only. */
3019eac3 10688
f4dc4d17
DE
10689static void
10690read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10691{
10692 struct die_info *child_die;
3019eac3 10693
f4dc4d17
DE
10694 prepare_one_comp_unit (cu, die, language_minimal);
10695
10696 /* Initialize (or reinitialize) the machinery for building symtabs.
10697 We do this before processing child DIEs, so that the line header table
10698 is available for DW_AT_decl_file. */
c24bdb02 10699 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10700
10701 if (die->child != NULL)
10702 {
10703 child_die = die->child;
10704 while (child_die && child_die->tag)
10705 {
10706 process_die (child_die, cu);
436c571c 10707 child_die = child_die->sibling;
f4dc4d17
DE
10708 }
10709 }
3019eac3
DE
10710}
10711\f
80626a55
DE
10712/* DWO/DWP files.
10713
10714 http://gcc.gnu.org/wiki/DebugFission
10715 http://gcc.gnu.org/wiki/DebugFissionDWP
10716
10717 To simplify handling of both DWO files ("object" files with the DWARF info)
10718 and DWP files (a file with the DWOs packaged up into one file), we treat
10719 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10720
10721static hashval_t
10722hash_dwo_file (const void *item)
10723{
9a3c8263 10724 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10725 hashval_t hash;
3019eac3 10726
a2ce51a0
DE
10727 hash = htab_hash_string (dwo_file->dwo_name);
10728 if (dwo_file->comp_dir != NULL)
10729 hash += htab_hash_string (dwo_file->comp_dir);
10730 return hash;
3019eac3
DE
10731}
10732
10733static int
10734eq_dwo_file (const void *item_lhs, const void *item_rhs)
10735{
9a3c8263
SM
10736 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10737 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10738
a2ce51a0
DE
10739 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10740 return 0;
10741 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10742 return lhs->comp_dir == rhs->comp_dir;
10743 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10744}
10745
10746/* Allocate a hash table for DWO files. */
10747
51ac9db5 10748static htab_up
298e9637 10749allocate_dwo_file_hash_table ()
3019eac3 10750{
bc68fb19
TT
10751 return htab_up (htab_create_alloc (41,
10752 hash_dwo_file,
10753 eq_dwo_file,
ef5f598c 10754 htab_delete_entry<dwo_file>,
bc68fb19 10755 xcalloc, xfree));
3019eac3
DE
10756}
10757
80626a55
DE
10758/* Lookup DWO file DWO_NAME. */
10759
10760static void **
976ca316 10761lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
10762 const char *dwo_name,
10763 const char *comp_dir)
80626a55
DE
10764{
10765 struct dwo_file find_entry;
10766 void **slot;
10767
976ca316
SM
10768 if (per_objfile->per_bfd->dwo_files == NULL)
10769 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 10770
0ac5b59e
DE
10771 find_entry.dwo_name = dwo_name;
10772 find_entry.comp_dir = comp_dir;
976ca316 10773 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 10774 INSERT);
80626a55
DE
10775
10776 return slot;
10777}
10778
3019eac3
DE
10779static hashval_t
10780hash_dwo_unit (const void *item)
10781{
9a3c8263 10782 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
10783
10784 /* This drops the top 32 bits of the id, but is ok for a hash. */
10785 return dwo_unit->signature;
10786}
10787
10788static int
10789eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10790{
9a3c8263
SM
10791 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10792 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
10793
10794 /* The signature is assumed to be unique within the DWO file.
10795 So while object file CU dwo_id's always have the value zero,
10796 that's OK, assuming each object file DWO file has only one CU,
10797 and that's the rule for now. */
10798 return lhs->signature == rhs->signature;
10799}
10800
10801/* Allocate a hash table for DWO CUs,TUs.
10802 There is one of these tables for each of CUs,TUs for each DWO file. */
10803
b0b6a987 10804static htab_up
298e9637 10805allocate_dwo_unit_table ()
3019eac3
DE
10806{
10807 /* Start out with a pretty small number.
10808 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
10809 return htab_up (htab_create_alloc (3,
10810 hash_dwo_unit,
10811 eq_dwo_unit,
10812 NULL, xcalloc, xfree));
3019eac3
DE
10813}
10814
19c3d4c9 10815/* die_reader_func for create_dwo_cu. */
3019eac3
DE
10816
10817static void
19c3d4c9
DE
10818create_dwo_cu_reader (const struct die_reader_specs *reader,
10819 const gdb_byte *info_ptr,
10820 struct die_info *comp_unit_die,
c0ab21c2
TT
10821 struct dwo_file *dwo_file,
10822 struct dwo_unit *dwo_unit)
3019eac3
DE
10823{
10824 struct dwarf2_cu *cu = reader->cu;
9c541725 10825 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 10826 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 10827
a084a2a6
AT
10828 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10829 if (!signature.has_value ())
3019eac3 10830 {
b98664d3 10831 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 10832 " its dwo_id [in module %s]"),
9d8780f0 10833 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
10834 return;
10835 }
10836
3019eac3 10837 dwo_unit->dwo_file = dwo_file;
a084a2a6 10838 dwo_unit->signature = *signature;
8a0459fd 10839 dwo_unit->section = section;
9c541725 10840 dwo_unit->sect_off = sect_off;
3019eac3
DE
10841 dwo_unit->length = cu->per_cu->length;
10842
6f738b01
SM
10843 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10844 sect_offset_str (sect_off),
10845 hex_string (dwo_unit->signature));
3019eac3
DE
10846}
10847
33c5cd75 10848/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 10849 Note: This function processes DWO files only, not DWP files. */
3019eac3 10850
33c5cd75 10851static void
976ca316 10852create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 10853 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 10854 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 10855{
976ca316
SM
10856 struct objfile *objfile = per_objfile->objfile;
10857 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 10858 const gdb_byte *info_ptr, *end_ptr;
3019eac3 10859
96b79293 10860 section.read (objfile);
33c5cd75 10861 info_ptr = section.buffer;
3019eac3
DE
10862
10863 if (info_ptr == NULL)
33c5cd75 10864 return;
3019eac3 10865
6f738b01
SM
10866 dwarf_read_debug_printf ("Reading %s for %s:",
10867 section.get_name (),
10868 section.get_file_name ());
3019eac3 10869
33c5cd75 10870 end_ptr = info_ptr + section.size;
3019eac3
DE
10871 while (info_ptr < end_ptr)
10872 {
10873 struct dwarf2_per_cu_data per_cu;
c0ab21c2 10874 struct dwo_unit read_unit {};
33c5cd75
DB
10875 struct dwo_unit *dwo_unit;
10876 void **slot;
10877 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 10878
1859c670 10879 per_cu.per_bfd = per_bfd;
3019eac3 10880 per_cu.is_debug_types = 0;
33c5cd75
DB
10881 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10882 per_cu.section = &section;
10883
976ca316 10884 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
10885 if (!reader.dummy_p)
10886 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 10887 &dwo_file, &read_unit);
33c5cd75
DB
10888 info_ptr += per_cu.length;
10889
10890 // If the unit could not be parsed, skip it.
c0ab21c2 10891 if (read_unit.dwo_file == NULL)
33c5cd75 10892 continue;
3019eac3 10893
33c5cd75 10894 if (cus_htab == NULL)
298e9637 10895 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 10896
1859c670 10897 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 10898 struct dwo_unit);
c0ab21c2 10899 *dwo_unit = read_unit;
b0b6a987 10900 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
10901 gdb_assert (slot != NULL);
10902 if (*slot != NULL)
19c3d4c9 10903 {
33c5cd75
DB
10904 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10905 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 10906
b98664d3 10907 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
10908 " the entry at offset %s, signature %s"),
10909 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 10910 hex_string (dwo_unit->signature));
19c3d4c9 10911 }
33c5cd75 10912 *slot = (void *)dwo_unit;
3019eac3 10913 }
3019eac3
DE
10914}
10915
80626a55
DE
10916/* DWP file .debug_{cu,tu}_index section format:
10917 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
10918 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10919
10920 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10921 officially standard DWP format was published with DWARF v5 and is called
10922 Version 5. There are no versions 3 or 4.
80626a55 10923
d2415c6c
DE
10924 DWP Version 1:
10925
80626a55
DE
10926 Both index sections have the same format, and serve to map a 64-bit
10927 signature to a set of section numbers. Each section begins with a header,
10928 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10929 indexes, and a pool of 32-bit section numbers. The index sections will be
10930 aligned at 8-byte boundaries in the file.
10931
d2415c6c
DE
10932 The index section header consists of:
10933
10934 V, 32 bit version number
10935 -, 32 bits unused
10936 N, 32 bit number of compilation units or type units in the index
10937 M, 32 bit number of slots in the hash table
80626a55 10938
d2415c6c 10939 Numbers are recorded using the byte order of the application binary.
80626a55 10940
d2415c6c
DE
10941 The hash table begins at offset 16 in the section, and consists of an array
10942 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10943 order of the application binary). Unused slots in the hash table are 0.
10944 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 10945
d2415c6c
DE
10946 The parallel table begins immediately after the hash table
10947 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10948 array of 32-bit indexes (using the byte order of the application binary),
10949 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10950 table contains a 32-bit index into the pool of section numbers. For unused
10951 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 10952
73869dc2
DE
10953 The pool of section numbers begins immediately following the hash table
10954 (at offset 16 + 12 * M from the beginning of the section). The pool of
10955 section numbers consists of an array of 32-bit words (using the byte order
10956 of the application binary). Each item in the array is indexed starting
10957 from 0. The hash table entry provides the index of the first section
10958 number in the set. Additional section numbers in the set follow, and the
10959 set is terminated by a 0 entry (section number 0 is not used in ELF).
10960
10961 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10962 section must be the first entry in the set, and the .debug_abbrev.dwo must
10963 be the second entry. Other members of the set may follow in any order.
10964
10965 ---
10966
d2854d8d 10967 DWP Versions 2 and 5:
73869dc2 10968
d2854d8d 10969 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
10970 and the entries in the index tables are now offsets into these sections.
10971 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10972 section.
10973
10974 Index Section Contents:
10975 Header
10976 Hash Table of Signatures dwp_hash_table.hash_table
10977 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
10978 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10979 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
10980
10981 The index section header consists of:
10982
10983 V, 32 bit version number
10984 L, 32 bit number of columns in the table of section offsets
10985 N, 32 bit number of compilation units or type units in the index
10986 M, 32 bit number of slots in the hash table
10987
10988 Numbers are recorded using the byte order of the application binary.
10989
10990 The hash table has the same format as version 1.
10991 The parallel table of indices has the same format as version 1,
10992 except that the entries are origin-1 indices into the table of sections
10993 offsets and the table of section sizes.
10994
10995 The table of offsets begins immediately following the parallel table
10996 (at offset 16 + 12 * M from the beginning of the section). The table is
10997 a two-dimensional array of 32-bit words (using the byte order of the
10998 application binary), with L columns and N+1 rows, in row-major order.
10999 Each row in the array is indexed starting from 0. The first row provides
11000 a key to the remaining rows: each column in this row provides an identifier
11001 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11002 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11003
11004 DW_SECT_INFO 1 .debug_info.dwo
11005 DW_SECT_TYPES 2 .debug_types.dwo
11006 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11007 DW_SECT_LINE 4 .debug_line.dwo
11008 DW_SECT_LOC 5 .debug_loc.dwo
11009 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11010 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11011 DW_SECT_MACRO 8 .debug_macro.dwo
11012
d2854d8d
CT
11013 The section identifiers for Version 5 are:
11014
11015 DW_SECT_INFO_V5 1 .debug_info.dwo
11016 DW_SECT_RESERVED_V5 2 --
11017 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11018 DW_SECT_LINE_V5 4 .debug_line.dwo
11019 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11020 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11021 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11022 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11023
73869dc2
DE
11024 The offsets provided by the CU and TU index sections are the base offsets
11025 for the contributions made by each CU or TU to the corresponding section
11026 in the package file. Each CU and TU header contains an abbrev_offset
11027 field, used to find the abbreviations table for that CU or TU within the
11028 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11029 be interpreted as relative to the base offset given in the index section.
11030 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11031 should be interpreted as relative to the base offset for .debug_line.dwo,
11032 and offsets into other debug sections obtained from DWARF attributes should
11033 also be interpreted as relative to the corresponding base offset.
11034
11035 The table of sizes begins immediately following the table of offsets.
11036 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11037 with L columns and N rows, in row-major order. Each row in the array is
11038 indexed starting from 1 (row 0 is shared by the two tables).
11039
11040 ---
11041
11042 Hash table lookup is handled the same in version 1 and 2:
11043
11044 We assume that N and M will not exceed 2^32 - 1.
11045 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11046
d2415c6c
DE
11047 Given a 64-bit compilation unit signature or a type signature S, an entry
11048 in the hash table is located as follows:
80626a55 11049
d2415c6c
DE
11050 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11051 the low-order k bits all set to 1.
80626a55 11052
d2415c6c 11053 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11054
d2415c6c
DE
11055 3) If the hash table entry at index H matches the signature, use that
11056 entry. If the hash table entry at index H is unused (all zeroes),
11057 terminate the search: the signature is not present in the table.
80626a55 11058
d2415c6c 11059 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11060
d2415c6c 11061 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11062 to stop at an unused slot or find the match. */
80626a55
DE
11063
11064/* Create a hash table to map DWO IDs to their CU/TU entry in
11065 .debug_{info,types}.dwo in DWP_FILE.
11066 Returns NULL if there isn't one.
11067 Note: This function processes DWP files only, not DWO files. */
11068
11069static struct dwp_hash_table *
976ca316 11070create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11071 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11072{
976ca316 11073 struct objfile *objfile = per_objfile->objfile;
400174b1 11074 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11075 const gdb_byte *index_ptr, *index_end;
80626a55 11076 struct dwarf2_section_info *index;
73869dc2 11077 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11078 struct dwp_hash_table *htab;
11079
11080 if (is_debug_types)
11081 index = &dwp_file->sections.tu_index;
11082 else
11083 index = &dwp_file->sections.cu_index;
11084
96b79293 11085 if (index->empty ())
80626a55 11086 return NULL;
96b79293 11087 index->read (objfile);
80626a55
DE
11088
11089 index_ptr = index->buffer;
11090 index_end = index_ptr + index->size;
11091
d2854d8d
CT
11092 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11093 For now it's safe to just read 4 bytes (particularly as it's difficult to
11094 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11095 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11096 index_ptr += 4;
d2854d8d 11097 if (version == 2 || version == 5)
73869dc2
DE
11098 nr_columns = read_4_bytes (dbfd, index_ptr);
11099 else
11100 nr_columns = 0;
11101 index_ptr += 4;
80626a55
DE
11102 nr_units = read_4_bytes (dbfd, index_ptr);
11103 index_ptr += 4;
11104 nr_slots = read_4_bytes (dbfd, index_ptr);
11105 index_ptr += 4;
11106
d2854d8d 11107 if (version != 1 && version != 2 && version != 5)
80626a55 11108 {
21aa081e 11109 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11110 " [in module %s]"),
21aa081e 11111 pulongest (version), dwp_file->name);
80626a55
DE
11112 }
11113 if (nr_slots != (nr_slots & -nr_slots))
11114 {
21aa081e 11115 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11116 " is not power of 2 [in module %s]"),
21aa081e 11117 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11118 }
11119
976ca316 11120 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11121 htab->version = version;
11122 htab->nr_columns = nr_columns;
80626a55
DE
11123 htab->nr_units = nr_units;
11124 htab->nr_slots = nr_slots;
11125 htab->hash_table = index_ptr;
11126 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11127
11128 /* Exit early if the table is empty. */
11129 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11130 || (version == 2 && nr_columns == 0)
11131 || (version == 5 && nr_columns == 0))
73869dc2
DE
11132 {
11133 /* All must be zero. */
11134 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11135 || (version == 2 && nr_columns != 0)
11136 || (version == 5 && nr_columns != 0))
73869dc2 11137 {
b98664d3 11138 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11139 " all zero [in modules %s]"),
11140 dwp_file->name);
11141 }
11142 return htab;
11143 }
11144
11145 if (version == 1)
11146 {
11147 htab->section_pool.v1.indices =
11148 htab->unit_table + sizeof (uint32_t) * nr_slots;
11149 /* It's harder to decide whether the section is too small in v1.
11150 V1 is deprecated anyway so we punt. */
11151 }
d2854d8d 11152 else if (version == 2)
73869dc2
DE
11153 {
11154 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11155 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11156 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11157 /* Reverse map for error checking. */
11158 int ids_seen[DW_SECT_MAX + 1];
11159 int i;
11160
11161 if (nr_columns < 2)
11162 {
11163 error (_("Dwarf Error: bad DWP hash table, too few columns"
11164 " in section table [in module %s]"),
11165 dwp_file->name);
11166 }
11167 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11168 {
11169 error (_("Dwarf Error: bad DWP hash table, too many columns"
11170 " in section table [in module %s]"),
11171 dwp_file->name);
11172 }
04fd5eed
GB
11173 memset (ids, 255, sizeof_ids);
11174 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11175 for (i = 0; i < nr_columns; ++i)
11176 {
11177 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11178
11179 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11180 {
11181 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11182 " in section table [in module %s]"),
11183 id, dwp_file->name);
11184 }
11185 if (ids_seen[id] != -1)
11186 {
11187 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11188 " id %d in section table [in module %s]"),
11189 id, dwp_file->name);
11190 }
11191 ids_seen[id] = i;
11192 ids[i] = id;
11193 }
11194 /* Must have exactly one info or types section. */
11195 if (((ids_seen[DW_SECT_INFO] != -1)
11196 + (ids_seen[DW_SECT_TYPES] != -1))
11197 != 1)
11198 {
11199 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11200 " DWO info/types section [in module %s]"),
11201 dwp_file->name);
11202 }
11203 /* Must have an abbrev section. */
11204 if (ids_seen[DW_SECT_ABBREV] == -1)
11205 {
11206 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11207 " section [in module %s]"),
11208 dwp_file->name);
11209 }
11210 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11211 htab->section_pool.v2.sizes =
11212 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11213 * nr_units * nr_columns);
11214 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11215 * nr_units * nr_columns))
11216 > index_end)
11217 {
11218 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11219 " [in module %s]"),
11220 dwp_file->name);
11221 }
11222 }
d2854d8d
CT
11223 else /* version == 5 */
11224 {
11225 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11226 int *ids = htab->section_pool.v5.section_ids;
11227 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11228 /* Reverse map for error checking. */
11229 int ids_seen[DW_SECT_MAX_V5 + 1];
11230
11231 if (nr_columns < 2)
11232 {
11233 error (_("Dwarf Error: bad DWP hash table, too few columns"
11234 " in section table [in module %s]"),
11235 dwp_file->name);
11236 }
11237 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11238 {
11239 error (_("Dwarf Error: bad DWP hash table, too many columns"
11240 " in section table [in module %s]"),
11241 dwp_file->name);
11242 }
11243 memset (ids, 255, sizeof_ids);
11244 memset (ids_seen, 255, sizeof (ids_seen));
11245 for (int i = 0; i < nr_columns; ++i)
11246 {
11247 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11248
11249 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11250 {
11251 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11252 " in section table [in module %s]"),
11253 id, dwp_file->name);
11254 }
11255 if (ids_seen[id] != -1)
11256 {
11257 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11258 " id %d in section table [in module %s]"),
11259 id, dwp_file->name);
11260 }
11261 ids_seen[id] = i;
11262 ids[i] = id;
11263 }
11264 /* Must have seen an info section. */
11265 if (ids_seen[DW_SECT_INFO_V5] == -1)
11266 {
11267 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11268 " DWO info/types section [in module %s]"),
11269 dwp_file->name);
11270 }
11271 /* Must have an abbrev section. */
11272 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11273 {
11274 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11275 " section [in module %s]"),
11276 dwp_file->name);
11277 }
11278 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11279 htab->section_pool.v5.sizes
11280 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11281 * nr_units * nr_columns);
11282 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11283 * nr_units * nr_columns))
11284 > index_end)
11285 {
11286 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11287 " [in module %s]"),
11288 dwp_file->name);
11289 }
11290 }
80626a55
DE
11291
11292 return htab;
11293}
11294
11295/* Update SECTIONS with the data from SECTP.
11296
5bb6e9dd
TT
11297 This function is like the other "locate" section routines, but in
11298 this context the sections to read comes from the DWP V1 hash table,
11299 not the full ELF section table.
80626a55
DE
11300
11301 The result is non-zero for success, or zero if an error was found. */
11302
11303static int
73869dc2
DE
11304locate_v1_virtual_dwo_sections (asection *sectp,
11305 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11306{
11307 const struct dwop_section_names *names = &dwop_section_names;
11308
fbedd546 11309 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
11310 {
11311 /* There can be only one. */
049412e3 11312 if (sections->abbrev.s.section != NULL)
80626a55 11313 return 0;
049412e3 11314 sections->abbrev.s.section = sectp;
fd361982 11315 sections->abbrev.size = bfd_section_size (sectp);
80626a55 11316 }
fbedd546
TT
11317 else if (names->info_dwo.matches (sectp->name)
11318 || names->types_dwo.matches (sectp->name))
80626a55
DE
11319 {
11320 /* There can be only one. */
049412e3 11321 if (sections->info_or_types.s.section != NULL)
80626a55 11322 return 0;
049412e3 11323 sections->info_or_types.s.section = sectp;
fd361982 11324 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 11325 }
fbedd546 11326 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
11327 {
11328 /* There can be only one. */
049412e3 11329 if (sections->line.s.section != NULL)
80626a55 11330 return 0;
049412e3 11331 sections->line.s.section = sectp;
fd361982 11332 sections->line.size = bfd_section_size (sectp);
80626a55 11333 }
fbedd546 11334 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
11335 {
11336 /* There can be only one. */
049412e3 11337 if (sections->loc.s.section != NULL)
80626a55 11338 return 0;
049412e3 11339 sections->loc.s.section = sectp;
fd361982 11340 sections->loc.size = bfd_section_size (sectp);
80626a55 11341 }
fbedd546 11342 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
11343 {
11344 /* There can be only one. */
049412e3 11345 if (sections->macinfo.s.section != NULL)
80626a55 11346 return 0;
049412e3 11347 sections->macinfo.s.section = sectp;
fd361982 11348 sections->macinfo.size = bfd_section_size (sectp);
80626a55 11349 }
fbedd546 11350 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
11351 {
11352 /* There can be only one. */
049412e3 11353 if (sections->macro.s.section != NULL)
80626a55 11354 return 0;
049412e3 11355 sections->macro.s.section = sectp;
fd361982 11356 sections->macro.size = bfd_section_size (sectp);
80626a55 11357 }
fbedd546 11358 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
11359 {
11360 /* There can be only one. */
049412e3 11361 if (sections->str_offsets.s.section != NULL)
80626a55 11362 return 0;
049412e3 11363 sections->str_offsets.s.section = sectp;
fd361982 11364 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11365 }
11366 else
11367 {
11368 /* No other kind of section is valid. */
11369 return 0;
11370 }
11371
11372 return 1;
11373}
11374
73869dc2
DE
11375/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11376 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11377 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11378 This is for DWP version 1 files. */
80626a55
DE
11379
11380static struct dwo_unit *
976ca316 11381create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11382 struct dwp_file *dwp_file,
73869dc2
DE
11383 uint32_t unit_index,
11384 const char *comp_dir,
11385 ULONGEST signature, int is_debug_types)
80626a55 11386{
73869dc2
DE
11387 const struct dwp_hash_table *dwp_htab =
11388 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11389 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11390 const char *kind = is_debug_types ? "TU" : "CU";
11391 struct dwo_file *dwo_file;
11392 struct dwo_unit *dwo_unit;
73869dc2 11393 struct virtual_v1_dwo_sections sections;
80626a55 11394 void **dwo_file_slot;
80626a55
DE
11395 int i;
11396
73869dc2
DE
11397 gdb_assert (dwp_file->version == 1);
11398
6f738b01
SM
11399 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11400 kind, pulongest (unit_index), hex_string (signature),
11401 dwp_file->name);
80626a55 11402
19ac8c2e 11403 /* Fetch the sections of this DWO unit.
80626a55
DE
11404 Put a limit on the number of sections we look for so that bad data
11405 doesn't cause us to loop forever. */
11406
73869dc2 11407#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11408 (1 /* .debug_info or .debug_types */ \
11409 + 1 /* .debug_abbrev */ \
11410 + 1 /* .debug_line */ \
11411 + 1 /* .debug_loc */ \
11412 + 1 /* .debug_str_offsets */ \
19ac8c2e 11413 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11414 + 1 /* trailing zero */)
11415
11416 memset (&sections, 0, sizeof (sections));
80626a55 11417
73869dc2 11418 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11419 {
11420 asection *sectp;
11421 uint32_t section_nr =
11422 read_4_bytes (dbfd,
73869dc2
DE
11423 dwp_htab->section_pool.v1.indices
11424 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11425
11426 if (section_nr == 0)
11427 break;
11428 if (section_nr >= dwp_file->num_sections)
11429 {
11430 error (_("Dwarf Error: bad DWP hash table, section number too large"
11431 " [in module %s]"),
11432 dwp_file->name);
11433 }
11434
11435 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11436 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11437 {
11438 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11439 " [in module %s]"),
11440 dwp_file->name);
11441 }
11442 }
11443
11444 if (i < 2
96b79293
TT
11445 || sections.info_or_types.empty ()
11446 || sections.abbrev.empty ())
80626a55
DE
11447 {
11448 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11449 " [in module %s]"),
11450 dwp_file->name);
11451 }
73869dc2 11452 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11453 {
11454 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11455 " [in module %s]"),
11456 dwp_file->name);
11457 }
11458
11459 /* It's easier for the rest of the code if we fake a struct dwo_file and
11460 have dwo_unit "live" in that. At least for now.
11461
11462 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11463 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11464 file, we can combine them back into a virtual DWO file to save space
11465 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11466 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11467
791afaa2
TT
11468 std::string virtual_dwo_name =
11469 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11470 sections.abbrev.get_id (),
11471 sections.line.get_id (),
11472 sections.loc.get_id (),
11473 sections.str_offsets.get_id ());
80626a55 11474 /* Can we use an existing virtual DWO file? */
976ca316 11475 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11476 comp_dir);
80626a55
DE
11477 /* Create one if necessary. */
11478 if (*dwo_file_slot == NULL)
11479 {
6f738b01
SM
11480 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11481 virtual_dwo_name.c_str ());
11482
51ac9db5 11483 dwo_file = new struct dwo_file;
976ca316 11484 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11485 dwo_file->comp_dir = comp_dir;
80626a55
DE
11486 dwo_file->sections.abbrev = sections.abbrev;
11487 dwo_file->sections.line = sections.line;
11488 dwo_file->sections.loc = sections.loc;
11489 dwo_file->sections.macinfo = sections.macinfo;
11490 dwo_file->sections.macro = sections.macro;
11491 dwo_file->sections.str_offsets = sections.str_offsets;
11492 /* The "str" section is global to the entire DWP file. */
11493 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11494 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11495 there's no need to record it in dwo_file.
11496 Also, we can't simply record type sections in dwo_file because
11497 we record a pointer into the vector in dwo_unit. As we collect more
11498 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11499 for it, invalidating all copies of pointers into the previous
11500 contents. */
80626a55
DE
11501 *dwo_file_slot = dwo_file;
11502 }
11503 else
11504 {
6f738b01
SM
11505 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11506 virtual_dwo_name.c_str ());
11507
9a3c8263 11508 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11509 }
80626a55 11510
976ca316 11511 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11512 dwo_unit->dwo_file = dwo_file;
11513 dwo_unit->signature = signature;
8d749320 11514 dwo_unit->section =
976ca316 11515 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11516 *dwo_unit->section = sections.info_or_types;
57d63ce2 11517 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11518
11519 return dwo_unit;
11520}
11521
d2854d8d
CT
11522/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11523 simplify them. Given a pointer to the containing section SECTION, and
11524 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11525 virtual section of just that piece. */
73869dc2
DE
11526
11527static struct dwarf2_section_info
d2854d8d
CT
11528create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11529 struct dwarf2_section_info *section,
11530 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
11531{
11532 struct dwarf2_section_info result;
11533 asection *sectp;
11534
11535 gdb_assert (section != NULL);
11536 gdb_assert (!section->is_virtual);
11537
11538 memset (&result, 0, sizeof (result));
11539 result.s.containing_section = section;
dc4ccb6f 11540 result.is_virtual = true;
73869dc2
DE
11541
11542 if (size == 0)
11543 return result;
11544
96b79293 11545 sectp = section->get_bfd_section ();
73869dc2
DE
11546
11547 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11548 bounds of the real section. This is a pretty-rare event, so just
11549 flag an error (easier) instead of a warning and trying to cope. */
11550 if (sectp == NULL
fd361982 11551 || offset + size > bfd_section_size (sectp))
73869dc2 11552 {
d2854d8d 11553 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 11554 " in section %s [in module %s]"),
fd361982 11555 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 11556 objfile_name (per_objfile->objfile));
73869dc2
DE
11557 }
11558
11559 result.virtual_offset = offset;
11560 result.size = size;
11561 return result;
11562}
11563
11564/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11565 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11566 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11567 This is for DWP version 2 files. */
11568
11569static struct dwo_unit *
976ca316 11570create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 11571 struct dwp_file *dwp_file,
73869dc2
DE
11572 uint32_t unit_index,
11573 const char *comp_dir,
11574 ULONGEST signature, int is_debug_types)
11575{
73869dc2
DE
11576 const struct dwp_hash_table *dwp_htab =
11577 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11578 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11579 const char *kind = is_debug_types ? "TU" : "CU";
11580 struct dwo_file *dwo_file;
11581 struct dwo_unit *dwo_unit;
d2854d8d 11582 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 11583 void **dwo_file_slot;
73869dc2
DE
11584 int i;
11585
11586 gdb_assert (dwp_file->version == 2);
11587
6f738b01
SM
11588 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11589 kind, pulongest (unit_index), hex_string (signature),
11590 dwp_file->name);
73869dc2
DE
11591
11592 /* Fetch the section offsets of this DWO unit. */
11593
11594 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11595
11596 for (i = 0; i < dwp_htab->nr_columns; ++i)
11597 {
11598 uint32_t offset = read_4_bytes (dbfd,
11599 dwp_htab->section_pool.v2.offsets
11600 + (((unit_index - 1) * dwp_htab->nr_columns
11601 + i)
11602 * sizeof (uint32_t)));
11603 uint32_t size = read_4_bytes (dbfd,
11604 dwp_htab->section_pool.v2.sizes
11605 + (((unit_index - 1) * dwp_htab->nr_columns
11606 + i)
11607 * sizeof (uint32_t)));
11608
11609 switch (dwp_htab->section_pool.v2.section_ids[i])
11610 {
11611 case DW_SECT_INFO:
11612 case DW_SECT_TYPES:
11613 sections.info_or_types_offset = offset;
11614 sections.info_or_types_size = size;
11615 break;
11616 case DW_SECT_ABBREV:
11617 sections.abbrev_offset = offset;
11618 sections.abbrev_size = size;
11619 break;
11620 case DW_SECT_LINE:
11621 sections.line_offset = offset;
11622 sections.line_size = size;
11623 break;
11624 case DW_SECT_LOC:
11625 sections.loc_offset = offset;
11626 sections.loc_size = size;
11627 break;
11628 case DW_SECT_STR_OFFSETS:
11629 sections.str_offsets_offset = offset;
11630 sections.str_offsets_size = size;
11631 break;
11632 case DW_SECT_MACINFO:
11633 sections.macinfo_offset = offset;
11634 sections.macinfo_size = size;
11635 break;
11636 case DW_SECT_MACRO:
11637 sections.macro_offset = offset;
11638 sections.macro_size = size;
11639 break;
11640 }
11641 }
11642
11643 /* It's easier for the rest of the code if we fake a struct dwo_file and
11644 have dwo_unit "live" in that. At least for now.
11645
11646 The DWP file can be made up of a random collection of CUs and TUs.
11647 However, for each CU + set of TUs that came from the same original DWO
11648 file, we can combine them back into a virtual DWO file to save space
11649 (fewer struct dwo_file objects to allocate). Remember that for really
11650 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11651
791afaa2
TT
11652 std::string virtual_dwo_name =
11653 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11654 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11655 (long) (sections.line_size ? sections.line_offset : 0),
11656 (long) (sections.loc_size ? sections.loc_offset : 0),
11657 (long) (sections.str_offsets_size
11658 ? sections.str_offsets_offset : 0));
73869dc2 11659 /* Can we use an existing virtual DWO file? */
976ca316 11660 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11661 comp_dir);
73869dc2
DE
11662 /* Create one if necessary. */
11663 if (*dwo_file_slot == NULL)
11664 {
6f738b01
SM
11665 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11666 virtual_dwo_name.c_str ());
11667
51ac9db5 11668 dwo_file = new struct dwo_file;
976ca316 11669 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
11670 dwo_file->comp_dir = comp_dir;
11671 dwo_file->sections.abbrev =
d2854d8d
CT
11672 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11673 sections.abbrev_offset,
11674 sections.abbrev_size);
73869dc2 11675 dwo_file->sections.line =
d2854d8d
CT
11676 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11677 sections.line_offset,
11678 sections.line_size);
73869dc2 11679 dwo_file->sections.loc =
d2854d8d
CT
11680 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11681 sections.loc_offset, sections.loc_size);
73869dc2 11682 dwo_file->sections.macinfo =
d2854d8d
CT
11683 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11684 sections.macinfo_offset,
11685 sections.macinfo_size);
73869dc2 11686 dwo_file->sections.macro =
d2854d8d
CT
11687 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11688 sections.macro_offset,
11689 sections.macro_size);
73869dc2 11690 dwo_file->sections.str_offsets =
d2854d8d
CT
11691 create_dwp_v2_or_v5_section (per_objfile,
11692 &dwp_file->sections.str_offsets,
11693 sections.str_offsets_offset,
11694 sections.str_offsets_size);
73869dc2
DE
11695 /* The "str" section is global to the entire DWP file. */
11696 dwo_file->sections.str = dwp_file->sections.str;
11697 /* The info or types section is assigned below to dwo_unit,
11698 there's no need to record it in dwo_file.
11699 Also, we can't simply record type sections in dwo_file because
11700 we record a pointer into the vector in dwo_unit. As we collect more
11701 types we'll grow the vector and eventually have to reallocate space
11702 for it, invalidating all copies of pointers into the previous
11703 contents. */
11704 *dwo_file_slot = dwo_file;
11705 }
11706 else
11707 {
6f738b01
SM
11708 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11709 virtual_dwo_name.c_str ());
11710
9a3c8263 11711 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11712 }
73869dc2 11713
976ca316 11714 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
11715 dwo_unit->dwo_file = dwo_file;
11716 dwo_unit->signature = signature;
8d749320 11717 dwo_unit->section =
976ca316 11718 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 11719 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 11720 (per_objfile,
d2854d8d
CT
11721 is_debug_types
11722 ? &dwp_file->sections.types
11723 : &dwp_file->sections.info,
11724 sections.info_or_types_offset,
11725 sections.info_or_types_size);
11726 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11727
11728 return dwo_unit;
11729}
11730
11731/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11732 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11733 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11734 This is for DWP version 5 files. */
11735
11736static struct dwo_unit *
11737create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11738 struct dwp_file *dwp_file,
11739 uint32_t unit_index,
11740 const char *comp_dir,
11741 ULONGEST signature, int is_debug_types)
11742{
11743 const struct dwp_hash_table *dwp_htab
11744 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11745 bfd *dbfd = dwp_file->dbfd.get ();
11746 const char *kind = is_debug_types ? "TU" : "CU";
11747 struct dwo_file *dwo_file;
11748 struct dwo_unit *dwo_unit;
11749 struct virtual_v2_or_v5_dwo_sections sections {};
11750 void **dwo_file_slot;
11751
11752 gdb_assert (dwp_file->version == 5);
11753
6f738b01
SM
11754 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11755 kind, pulongest (unit_index), hex_string (signature),
11756 dwp_file->name);
d2854d8d
CT
11757
11758 /* Fetch the section offsets of this DWO unit. */
11759
11760 /* memset (&sections, 0, sizeof (sections)); */
11761
11762 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11763 {
11764 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
11765 dwp_htab->section_pool.v5.offsets
11766 + (((unit_index - 1)
11767 * dwp_htab->nr_columns
11768 + i)
11769 * sizeof (uint32_t)));
d2854d8d 11770 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
11771 dwp_htab->section_pool.v5.sizes
11772 + (((unit_index - 1) * dwp_htab->nr_columns
11773 + i)
11774 * sizeof (uint32_t)));
d2854d8d
CT
11775
11776 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
11777 {
11778 case DW_SECT_ABBREV_V5:
11779 sections.abbrev_offset = offset;
11780 sections.abbrev_size = size;
11781 break;
11782 case DW_SECT_INFO_V5:
11783 sections.info_or_types_offset = offset;
11784 sections.info_or_types_size = size;
11785 break;
11786 case DW_SECT_LINE_V5:
11787 sections.line_offset = offset;
11788 sections.line_size = size;
11789 break;
11790 case DW_SECT_LOCLISTS_V5:
11791 sections.loclists_offset = offset;
11792 sections.loclists_size = size;
11793 break;
11794 case DW_SECT_MACRO_V5:
11795 sections.macro_offset = offset;
11796 sections.macro_size = size;
11797 break;
11798 case DW_SECT_RNGLISTS_V5:
11799 sections.rnglists_offset = offset;
11800 sections.rnglists_size = size;
11801 break;
11802 case DW_SECT_STR_OFFSETS_V5:
11803 sections.str_offsets_offset = offset;
11804 sections.str_offsets_size = size;
11805 break;
11806 case DW_SECT_RESERVED_V5:
11807 default:
11808 break;
11809 }
d2854d8d
CT
11810 }
11811
11812 /* It's easier for the rest of the code if we fake a struct dwo_file and
11813 have dwo_unit "live" in that. At least for now.
11814
11815 The DWP file can be made up of a random collection of CUs and TUs.
11816 However, for each CU + set of TUs that came from the same original DWO
11817 file, we can combine them back into a virtual DWO file to save space
11818 (fewer struct dwo_file objects to allocate). Remember that for really
11819 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11820
11821 std::string virtual_dwo_name =
11822 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
11823 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11824 (long) (sections.line_size ? sections.line_offset : 0),
11825 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11826 (long) (sections.str_offsets_size
11827 ? sections.str_offsets_offset : 0),
11828 (long) (sections.macro_size ? sections.macro_offset : 0),
11829 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
11830 /* Can we use an existing virtual DWO file? */
11831 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
11832 virtual_dwo_name.c_str (),
11833 comp_dir);
d2854d8d
CT
11834 /* Create one if necessary. */
11835 if (*dwo_file_slot == NULL)
11836 {
6f738b01
SM
11837 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11838 virtual_dwo_name.c_str ());
11839
d2854d8d
CT
11840 dwo_file = new struct dwo_file;
11841 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11842 dwo_file->comp_dir = comp_dir;
11843 dwo_file->sections.abbrev =
dda83cd7
SM
11844 create_dwp_v2_or_v5_section (per_objfile,
11845 &dwp_file->sections.abbrev,
11846 sections.abbrev_offset,
11847 sections.abbrev_size);
d2854d8d 11848 dwo_file->sections.line =
dda83cd7
SM
11849 create_dwp_v2_or_v5_section (per_objfile,
11850 &dwp_file->sections.line,
11851 sections.line_offset, sections.line_size);
d2854d8d 11852 dwo_file->sections.macro =
dda83cd7
SM
11853 create_dwp_v2_or_v5_section (per_objfile,
11854 &dwp_file->sections.macro,
11855 sections.macro_offset,
11856 sections.macro_size);
d2854d8d 11857 dwo_file->sections.loclists =
dda83cd7
SM
11858 create_dwp_v2_or_v5_section (per_objfile,
11859 &dwp_file->sections.loclists,
11860 sections.loclists_offset,
11861 sections.loclists_size);
d2854d8d 11862 dwo_file->sections.rnglists =
dda83cd7
SM
11863 create_dwp_v2_or_v5_section (per_objfile,
11864 &dwp_file->sections.rnglists,
11865 sections.rnglists_offset,
11866 sections.rnglists_size);
d2854d8d 11867 dwo_file->sections.str_offsets =
dda83cd7
SM
11868 create_dwp_v2_or_v5_section (per_objfile,
11869 &dwp_file->sections.str_offsets,
11870 sections.str_offsets_offset,
11871 sections.str_offsets_size);
d2854d8d
CT
11872 /* The "str" section is global to the entire DWP file. */
11873 dwo_file->sections.str = dwp_file->sections.str;
11874 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
11875 there's no need to record it in dwo_file.
11876 Also, we can't simply record type sections in dwo_file because
11877 we record a pointer into the vector in dwo_unit. As we collect more
11878 types we'll grow the vector and eventually have to reallocate space
11879 for it, invalidating all copies of pointers into the previous
11880 contents. */
d2854d8d
CT
11881 *dwo_file_slot = dwo_file;
11882 }
11883 else
11884 {
6f738b01
SM
11885 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11886 virtual_dwo_name.c_str ());
11887
d2854d8d
CT
11888 dwo_file = (struct dwo_file *) *dwo_file_slot;
11889 }
11890
11891 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11892 dwo_unit->dwo_file = dwo_file;
11893 dwo_unit->signature = signature;
11894 dwo_unit->section
11895 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11896 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
11897 &dwp_file->sections.info,
11898 sections.info_or_types_offset,
11899 sections.info_or_types_size);
73869dc2
DE
11900 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11901
11902 return dwo_unit;
11903}
11904
57d63ce2
DE
11905/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11906 Returns NULL if the signature isn't found. */
80626a55
DE
11907
11908static struct dwo_unit *
976ca316 11909lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 11910 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11911 ULONGEST signature, int is_debug_types)
80626a55 11912{
57d63ce2
DE
11913 const struct dwp_hash_table *dwp_htab =
11914 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11915 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11916 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11917 uint32_t hash = signature & mask;
11918 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11919 unsigned int i;
11920 void **slot;
870f88f7 11921 struct dwo_unit find_dwo_cu;
80626a55
DE
11922
11923 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11924 find_dwo_cu.signature = signature;
19ac8c2e 11925 slot = htab_find_slot (is_debug_types
48b490f2
TT
11926 ? dwp_file->loaded_tus.get ()
11927 : dwp_file->loaded_cus.get (),
19ac8c2e 11928 &find_dwo_cu, INSERT);
80626a55
DE
11929
11930 if (*slot != NULL)
9a3c8263 11931 return (struct dwo_unit *) *slot;
80626a55
DE
11932
11933 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11934 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11935 {
11936 ULONGEST signature_in_table;
11937
11938 signature_in_table =
57d63ce2 11939 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11940 if (signature_in_table == signature)
11941 {
57d63ce2
DE
11942 uint32_t unit_index =
11943 read_4_bytes (dbfd,
11944 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11945
73869dc2
DE
11946 if (dwp_file->version == 1)
11947 {
976ca316
SM
11948 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
11949 unit_index, comp_dir,
11950 signature, is_debug_types);
73869dc2 11951 }
d2854d8d 11952 else if (dwp_file->version == 2)
73869dc2 11953 {
976ca316
SM
11954 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
11955 unit_index, comp_dir,
11956 signature, is_debug_types);
73869dc2 11957 }
d2854d8d
CT
11958 else /* version == 5 */
11959 {
11960 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11961 unit_index, comp_dir,
11962 signature, is_debug_types);
11963 }
9a3c8263 11964 return (struct dwo_unit *) *slot;
80626a55
DE
11965 }
11966 if (signature_in_table == 0)
11967 return NULL;
11968 hash = (hash + hash2) & mask;
11969 }
11970
11971 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11972 " [in module %s]"),
11973 dwp_file->name);
11974}
11975
ab5088bf 11976/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11977 Open the file specified by FILE_NAME and hand it off to BFD for
11978 preliminary analysis. Return a newly initialized bfd *, which
11979 includes a canonicalized copy of FILE_NAME.
80626a55 11980 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11981 SEARCH_CWD is true if the current directory is to be searched.
11982 It will be searched before debug-file-directory.
13aaf454
DE
11983 If successful, the file is added to the bfd include table of the
11984 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11985 If unable to find/open the file, return NULL.
3019eac3
DE
11986 NOTE: This function is derived from symfile_bfd_open. */
11987
192b62ce 11988static gdb_bfd_ref_ptr
976ca316 11989try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 11990 const char *file_name, int is_dwp, int search_cwd)
3019eac3 11991{
24b9144d 11992 int desc;
9c02c129
DE
11993 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11994 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11995 to debug_file_directory. */
e0cc99a6 11996 const char *search_path;
9c02c129
DE
11997 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11998
e0cc99a6 11999 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12000 if (search_cwd)
12001 {
12002 if (*debug_file_directory != '\0')
e0cc99a6
TT
12003 {
12004 search_path_holder.reset (concat (".", dirname_separator_string,
12005 debug_file_directory,
12006 (char *) NULL));
12007 search_path = search_path_holder.get ();
12008 }
6ac97d4c 12009 else
e0cc99a6 12010 search_path = ".";
6ac97d4c 12011 }
9c02c129 12012 else
e0cc99a6 12013 search_path = debug_file_directory;
3019eac3 12014
56d467f4
CT
12015 /* Add the path for the executable binary to the list of search paths. */
12016 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12017 search_path_holder.reset (concat (objfile_dir.c_str (),
12018 dirname_separator_string,
12019 search_path, nullptr));
12020 search_path = search_path_holder.get ();
12021
24b9144d 12022 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12023 if (is_dwp)
12024 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12025
12026 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12027 desc = openp (search_path, flags, file_name,
3019eac3
DE
12028 O_RDONLY | O_BINARY, &absolute_name);
12029 if (desc < 0)
12030 return NULL;
12031
e0cc99a6
TT
12032 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12033 gnutarget, desc));
9c02c129
DE
12034 if (sym_bfd == NULL)
12035 return NULL;
192b62ce 12036 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12037
192b62ce
TT
12038 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12039 return NULL;
3019eac3 12040
13aaf454
DE
12041 /* Success. Record the bfd as having been included by the objfile's bfd.
12042 This is important because things like demangled_names_hash lives in the
12043 objfile's per_bfd space and may have references to things like symbol
12044 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12045 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12046
3019eac3
DE
12047 return sym_bfd;
12048}
12049
ab5088bf 12050/* Try to open DWO file FILE_NAME.
3019eac3
DE
12051 COMP_DIR is the DW_AT_comp_dir attribute.
12052 The result is the bfd handle of the file.
12053 If there is a problem finding or opening the file, return NULL.
12054 Upon success, the canonicalized path of the file is stored in the bfd,
12055 same as symfile_bfd_open. */
12056
192b62ce 12057static gdb_bfd_ref_ptr
976ca316 12058open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12059 const char *file_name, const char *comp_dir)
3019eac3 12060{
80626a55 12061 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12062 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12063 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12064
12065 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12066
12067 if (comp_dir != NULL)
12068 {
43816ebc
TT
12069 gdb::unique_xmalloc_ptr<char> path_to_try
12070 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12071
12072 /* NOTE: If comp_dir is a relative path, this will also try the
12073 search path, which seems useful. */
976ca316 12074 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12075 0 /*is_dwp*/,
192b62ce 12076 1 /*search_cwd*/));
3019eac3
DE
12077 if (abfd != NULL)
12078 return abfd;
12079 }
12080
12081 /* That didn't work, try debug-file-directory, which, despite its name,
12082 is a list of paths. */
12083
12084 if (*debug_file_directory == '\0')
12085 return NULL;
12086
976ca316 12087 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12088 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12089}
12090
80626a55
DE
12091/* This function is mapped across the sections and remembers the offset and
12092 size of each of the DWO debugging sections we are interested in. */
12093
12094static void
5bb6e9dd
TT
12095dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12096 dwo_sections *dwo_sections)
80626a55 12097{
80626a55
DE
12098 const struct dwop_section_names *names = &dwop_section_names;
12099
fbedd546 12100 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12101 {
049412e3 12102 dwo_sections->abbrev.s.section = sectp;
fd361982 12103 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12104 }
fbedd546 12105 else if (names->info_dwo.matches (sectp->name))
80626a55 12106 {
049412e3 12107 dwo_sections->info.s.section = sectp;
fd361982 12108 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12109 }
fbedd546 12110 else if (names->line_dwo.matches (sectp->name))
80626a55 12111 {
049412e3 12112 dwo_sections->line.s.section = sectp;
fd361982 12113 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12114 }
fbedd546 12115 else if (names->loc_dwo.matches (sectp->name))
80626a55 12116 {
049412e3 12117 dwo_sections->loc.s.section = sectp;
fd361982 12118 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12119 }
fbedd546 12120 else if (names->loclists_dwo.matches (sectp->name))
41144253 12121 {
12122 dwo_sections->loclists.s.section = sectp;
12123 dwo_sections->loclists.size = bfd_section_size (sectp);
12124 }
fbedd546 12125 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12126 {
049412e3 12127 dwo_sections->macinfo.s.section = sectp;
fd361982 12128 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12129 }
fbedd546 12130 else if (names->macro_dwo.matches (sectp->name))
80626a55 12131 {
049412e3 12132 dwo_sections->macro.s.section = sectp;
fd361982 12133 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12134 }
fbedd546 12135 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12136 {
12137 dwo_sections->rnglists.s.section = sectp;
12138 dwo_sections->rnglists.size = bfd_section_size (sectp);
12139 }
fbedd546 12140 else if (names->str_dwo.matches (sectp->name))
80626a55 12141 {
049412e3 12142 dwo_sections->str.s.section = sectp;
fd361982 12143 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12144 }
fbedd546 12145 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12146 {
049412e3 12147 dwo_sections->str_offsets.s.section = sectp;
fd361982 12148 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12149 }
fbedd546 12150 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12151 {
12152 struct dwarf2_section_info type_section;
12153
12154 memset (&type_section, 0, sizeof (type_section));
049412e3 12155 type_section.s.section = sectp;
fd361982 12156 type_section.size = bfd_section_size (sectp);
fd5866f6 12157 dwo_sections->types.push_back (type_section);
80626a55
DE
12158 }
12159}
12160
ab5088bf 12161/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12162 by PER_CU. This is for the non-DWP case.
80626a55 12163 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12164
12165static struct dwo_file *
4ab09049
SM
12166open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12167 const char *comp_dir)
3019eac3 12168{
976ca316 12169 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12170
976ca316 12171 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12172 if (dbfd == NULL)
12173 {
6f738b01
SM
12174 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12175
80626a55
DE
12176 return NULL;
12177 }
263db9a1 12178
51ac9db5 12179 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12180 dwo_file->dwo_name = dwo_name;
12181 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12182 dwo_file->dbfd = std::move (dbfd);
3019eac3 12183
5bb6e9dd
TT
12184 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12185 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12186 &dwo_file->sections);
3019eac3 12187
976ca316
SM
12188 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12189 dwo_file->cus);
3019eac3 12190
d2854d8d
CT
12191 if (cu->per_cu->dwarf_version < 5)
12192 {
12193 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12194 dwo_file->sections.types, dwo_file->tus);
12195 }
12196 else
12197 {
12198 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12199 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12200 rcuh_kind::COMPILE);
d2854d8d 12201 }
3019eac3 12202
6f738b01 12203 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12204
263db9a1 12205 return dwo_file.release ();
3019eac3
DE
12206}
12207
80626a55 12208/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12209 size of each of the DWP debugging sections common to version 1 and 2 that
12210 we are interested in. */
3019eac3 12211
80626a55 12212static void
73869dc2 12213dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12214 dwp_file *dwp_file)
3019eac3 12215{
80626a55
DE
12216 const struct dwop_section_names *names = &dwop_section_names;
12217 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12218
80626a55 12219 /* Record the ELF section number for later lookup: this is what the
73869dc2 12220 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12221 gdb_assert (elf_section_nr < dwp_file->num_sections);
12222 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12223
80626a55 12224 /* Look for specific sections that we need. */
fbedd546 12225 if (names->str_dwo.matches (sectp->name))
80626a55 12226 {
049412e3 12227 dwp_file->sections.str.s.section = sectp;
fd361982 12228 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12229 }
fbedd546 12230 else if (names->cu_index.matches (sectp->name))
80626a55 12231 {
049412e3 12232 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12233 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12234 }
fbedd546 12235 else if (names->tu_index.matches (sectp->name))
80626a55 12236 {
049412e3 12237 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12238 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12239 }
12240}
3019eac3 12241
73869dc2
DE
12242/* This function is mapped across the sections and remembers the offset and
12243 size of each of the DWP version 2 debugging sections that we are interested
12244 in. This is split into a separate function because we don't know if we
d2854d8d 12245 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12246
12247static void
12248dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12249{
9a3c8263 12250 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12251 const struct dwop_section_names *names = &dwop_section_names;
12252 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12253
12254 /* Record the ELF section number for later lookup: this is what the
12255 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12256 gdb_assert (elf_section_nr < dwp_file->num_sections);
12257 dwp_file->elf_sections[elf_section_nr] = sectp;
12258
12259 /* Look for specific sections that we need. */
fbedd546 12260 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12261 {
049412e3 12262 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12263 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12264 }
fbedd546 12265 else if (names->info_dwo.matches (sectp->name))
73869dc2 12266 {
049412e3 12267 dwp_file->sections.info.s.section = sectp;
fd361982 12268 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12269 }
fbedd546 12270 else if (names->line_dwo.matches (sectp->name))
73869dc2 12271 {
049412e3 12272 dwp_file->sections.line.s.section = sectp;
fd361982 12273 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 12274 }
fbedd546 12275 else if (names->loc_dwo.matches (sectp->name))
73869dc2 12276 {
049412e3 12277 dwp_file->sections.loc.s.section = sectp;
fd361982 12278 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 12279 }
fbedd546 12280 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 12281 {
049412e3 12282 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12283 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 12284 }
fbedd546 12285 else if (names->macro_dwo.matches (sectp->name))
73869dc2 12286 {
049412e3 12287 dwp_file->sections.macro.s.section = sectp;
fd361982 12288 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 12289 }
fbedd546 12290 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 12291 {
049412e3 12292 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12293 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 12294 }
fbedd546 12295 else if (names->types_dwo.matches (sectp->name))
73869dc2 12296 {
049412e3 12297 dwp_file->sections.types.s.section = sectp;
fd361982 12298 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12299 }
12300}
12301
d2854d8d
CT
12302/* This function is mapped across the sections and remembers the offset and
12303 size of each of the DWP version 5 debugging sections that we are interested
12304 in. This is split into a separate function because we don't know if we
12305 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12306
12307static void
12308dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12309{
12310 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12311 const struct dwop_section_names *names = &dwop_section_names;
12312 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12313
12314 /* Record the ELF section number for later lookup: this is what the
12315 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12316 gdb_assert (elf_section_nr < dwp_file->num_sections);
12317 dwp_file->elf_sections[elf_section_nr] = sectp;
12318
12319 /* Look for specific sections that we need. */
fbedd546 12320 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
12321 {
12322 dwp_file->sections.abbrev.s.section = sectp;
12323 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12324 }
fbedd546 12325 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
12326 {
12327 dwp_file->sections.info.s.section = sectp;
12328 dwp_file->sections.info.size = bfd_section_size (sectp);
12329 }
fbedd546 12330 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
12331 {
12332 dwp_file->sections.line.s.section = sectp;
12333 dwp_file->sections.line.size = bfd_section_size (sectp);
12334 }
fbedd546 12335 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
12336 {
12337 dwp_file->sections.loclists.s.section = sectp;
12338 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12339 }
fbedd546 12340 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
12341 {
12342 dwp_file->sections.macro.s.section = sectp;
12343 dwp_file->sections.macro.size = bfd_section_size (sectp);
12344 }
fbedd546 12345 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
12346 {
12347 dwp_file->sections.rnglists.s.section = sectp;
12348 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12349 }
fbedd546 12350 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
12351 {
12352 dwp_file->sections.str_offsets.s.section = sectp;
12353 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12354 }
12355}
12356
80626a55 12357/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12358
80626a55
DE
12359static hashval_t
12360hash_dwp_loaded_cutus (const void *item)
12361{
9a3c8263 12362 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12363
80626a55
DE
12364 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12365 return dwo_unit->signature;
3019eac3
DE
12366}
12367
80626a55 12368/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12369
80626a55
DE
12370static int
12371eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12372{
9a3c8263
SM
12373 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12374 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12375
80626a55
DE
12376 return dua->signature == dub->signature;
12377}
3019eac3 12378
80626a55 12379/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12380
48b490f2 12381static htab_up
298e9637 12382allocate_dwp_loaded_cutus_table ()
80626a55 12383{
48b490f2
TT
12384 return htab_up (htab_create_alloc (3,
12385 hash_dwp_loaded_cutus,
12386 eq_dwp_loaded_cutus,
12387 NULL, xcalloc, xfree));
80626a55 12388}
3019eac3 12389
ab5088bf
DE
12390/* Try to open DWP file FILE_NAME.
12391 The result is the bfd handle of the file.
12392 If there is a problem finding or opening the file, return NULL.
12393 Upon success, the canonicalized path of the file is stored in the bfd,
12394 same as symfile_bfd_open. */
12395
192b62ce 12396static gdb_bfd_ref_ptr
976ca316 12397open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12398{
976ca316 12399 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12400 1 /*is_dwp*/,
192b62ce 12401 1 /*search_cwd*/));
6ac97d4c
DE
12402 if (abfd != NULL)
12403 return abfd;
12404
12405 /* Work around upstream bug 15652.
12406 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12407 [Whether that's a "bug" is debatable, but it is getting in our way.]
12408 We have no real idea where the dwp file is, because gdb's realpath-ing
12409 of the executable's path may have discarded the needed info.
12410 [IWBN if the dwp file name was recorded in the executable, akin to
12411 .gnu_debuglink, but that doesn't exist yet.]
12412 Strip the directory from FILE_NAME and search again. */
12413 if (*debug_file_directory != '\0')
12414 {
12415 /* Don't implicitly search the current directory here.
12416 If the user wants to search "." to handle this case,
12417 it must be added to debug-file-directory. */
976ca316
SM
12418 return try_open_dwop_file (per_objfile, lbasename (file_name),
12419 1 /*is_dwp*/,
6ac97d4c
DE
12420 0 /*search_cwd*/);
12421 }
12422
12423 return NULL;
ab5088bf
DE
12424}
12425
80626a55
DE
12426/* Initialize the use of the DWP file for the current objfile.
12427 By convention the name of the DWP file is ${objfile}.dwp.
12428 The result is NULL if it can't be found. */
a766d390 12429
400174b1 12430static std::unique_ptr<struct dwp_file>
976ca316 12431open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12432{
976ca316 12433 struct objfile *objfile = per_objfile->objfile;
80626a55 12434
82bf32bc
JK
12435 /* Try to find first .dwp for the binary file before any symbolic links
12436 resolving. */
6c447423
DE
12437
12438 /* If the objfile is a debug file, find the name of the real binary
12439 file and get the name of dwp file from there. */
d721ba37 12440 std::string dwp_name;
6c447423
DE
12441 if (objfile->separate_debug_objfile_backlink != NULL)
12442 {
12443 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12444 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12445
d721ba37 12446 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12447 }
12448 else
d721ba37
PA
12449 dwp_name = objfile->original_name;
12450
12451 dwp_name += ".dwp";
80626a55 12452
976ca316 12453 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12454 if (dbfd == NULL
12455 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12456 {
12457 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12458 dwp_name = objfile_name (objfile);
12459 dwp_name += ".dwp";
976ca316 12460 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12461 }
12462
80626a55
DE
12463 if (dbfd == NULL)
12464 {
6f738b01
SM
12465 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12466
400174b1 12467 return std::unique_ptr<dwp_file> ();
3019eac3 12468 }
400174b1
TT
12469
12470 const char *name = bfd_get_filename (dbfd.get ());
12471 std::unique_ptr<struct dwp_file> dwp_file
12472 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12473
0a0f4c01 12474 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12475 dwp_file->elf_sections =
976ca316 12476 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12477 dwp_file->num_sections, asection *);
12478
5bb6e9dd
TT
12479 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12480 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12481 dwp_file.get ());
80626a55 12482
976ca316 12483 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12484
976ca316 12485 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12486
73869dc2 12487 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12488 if (dwp_file->cus && dwp_file->tus
12489 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12490 {
12491 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12492 pretty bizarre. We use pulongest here because that's the established
4d65956b 12493 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12494 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12495 " TU version %s [in DWP file %s]"),
12496 pulongest (dwp_file->cus->version),
d721ba37 12497 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12498 }
08302ed2
DE
12499
12500 if (dwp_file->cus)
12501 dwp_file->version = dwp_file->cus->version;
12502 else if (dwp_file->tus)
12503 dwp_file->version = dwp_file->tus->version;
12504 else
12505 dwp_file->version = 2;
73869dc2 12506
5bb6e9dd
TT
12507 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12508 {
12509 if (dwp_file->version == 2)
12510 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12511 dwp_file.get ());
12512 else
12513 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12514 dwp_file.get ());
12515 }
73869dc2 12516
298e9637
SM
12517 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12518 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12519
6f738b01
SM
12520 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12521 dwarf_read_debug_printf (" %s CUs, %s TUs",
12522 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12523 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12524
12525 return dwp_file;
3019eac3 12526}
c906108c 12527
ab5088bf
DE
12528/* Wrapper around open_and_init_dwp_file, only open it once. */
12529
12530static struct dwp_file *
976ca316 12531get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12532{
976ca316 12533 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12534 {
976ca316
SM
12535 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12536 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12537 }
976ca316 12538 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12539}
12540
80626a55
DE
12541/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12542 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12543 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12544 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12545 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12546
12547 This is called, for example, when wanting to read a variable with a
12548 complex location. Therefore we don't want to do file i/o for every call.
12549 Therefore we don't want to look for a DWO file on every call.
12550 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12551 then we check if we've already seen DWO_NAME, and only THEN do we check
12552 for a DWO file.
12553
1c658ad5 12554 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12555 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12556
3019eac3 12557static struct dwo_unit *
4ab09049 12558lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12559 ULONGEST signature, int is_debug_types)
3019eac3 12560{
976ca316
SM
12561 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12562 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12563 const char *kind = is_debug_types ? "TU" : "CU";
12564 void **dwo_file_slot;
3019eac3 12565 struct dwo_file *dwo_file;
80626a55 12566 struct dwp_file *dwp_file;
cb1df416 12567
6a506a2d
DE
12568 /* First see if there's a DWP file.
12569 If we have a DWP file but didn't find the DWO inside it, don't
12570 look for the original DWO file. It makes gdb behave differently
12571 depending on whether one is debugging in the build tree. */
cf2c3c16 12572
976ca316 12573 dwp_file = get_dwp_file (per_objfile);
80626a55 12574 if (dwp_file != NULL)
cf2c3c16 12575 {
80626a55
DE
12576 const struct dwp_hash_table *dwp_htab =
12577 is_debug_types ? dwp_file->tus : dwp_file->cus;
12578
12579 if (dwp_htab != NULL)
12580 {
12581 struct dwo_unit *dwo_cutu =
976ca316
SM
12582 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12583 is_debug_types);
80626a55
DE
12584
12585 if (dwo_cutu != NULL)
12586 {
6f738b01
SM
12587 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12588 kind, hex_string (signature),
12589 host_address_to_string (dwo_cutu));
12590
80626a55
DE
12591 return dwo_cutu;
12592 }
12593 }
12594 }
6a506a2d 12595 else
80626a55 12596 {
6a506a2d 12597 /* No DWP file, look for the DWO file. */
80626a55 12598
976ca316 12599 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 12600 if (*dwo_file_slot == NULL)
80626a55 12601 {
6a506a2d 12602 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12603 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12604 }
6a506a2d 12605 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12606 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12607
6a506a2d 12608 if (dwo_file != NULL)
19c3d4c9 12609 {
6a506a2d
DE
12610 struct dwo_unit *dwo_cutu = NULL;
12611
12612 if (is_debug_types && dwo_file->tus)
12613 {
12614 struct dwo_unit find_dwo_cutu;
12615
12616 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12617 find_dwo_cutu.signature = signature;
9a3c8263 12618 dwo_cutu
b0b6a987
TT
12619 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12620 &find_dwo_cutu);
6a506a2d 12621 }
33c5cd75 12622 else if (!is_debug_types && dwo_file->cus)
80626a55 12623 {
33c5cd75
DB
12624 struct dwo_unit find_dwo_cutu;
12625
12626 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12627 find_dwo_cutu.signature = signature;
b0b6a987 12628 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12629 &find_dwo_cutu);
6a506a2d
DE
12630 }
12631
12632 if (dwo_cutu != NULL)
12633 {
6f738b01
SM
12634 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12635 kind, dwo_name, hex_string (signature),
12636 host_address_to_string (dwo_cutu));
12637
6a506a2d 12638 return dwo_cutu;
80626a55
DE
12639 }
12640 }
2e276125 12641 }
9cdd5dbd 12642
80626a55
DE
12643 /* We didn't find it. This could mean a dwo_id mismatch, or
12644 someone deleted the DWO/DWP file, or the search path isn't set up
12645 correctly to find the file. */
12646
6f738b01
SM
12647 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12648 kind, dwo_name, hex_string (signature));
3019eac3 12649
6656a72d
DE
12650 /* This is a warning and not a complaint because it can be caused by
12651 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12652 {
12653 /* Print the name of the DWP file if we looked there, helps the user
12654 better diagnose the problem. */
791afaa2 12655 std::string dwp_text;
43942612
DE
12656
12657 if (dwp_file != NULL)
791afaa2
TT
12658 dwp_text = string_printf (" [in DWP file %s]",
12659 lbasename (dwp_file->name));
43942612 12660
9d8780f0 12661 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12662 " [in module %s]"),
4ab09049
SM
12663 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12664 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12665 }
3019eac3 12666 return NULL;
5fb290d7
DJ
12667}
12668
80626a55
DE
12669/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12670 See lookup_dwo_cutu_unit for details. */
12671
12672static struct dwo_unit *
4ab09049 12673lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12674 ULONGEST signature)
12675{
4ab09049
SM
12676 gdb_assert (!cu->per_cu->is_debug_types);
12677
12678 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12679}
12680
12681/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12682 See lookup_dwo_cutu_unit for details. */
12683
12684static struct dwo_unit *
4ab09049 12685lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12686{
4ab09049
SM
12687 gdb_assert (cu->per_cu->is_debug_types);
12688
12689 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12690
12691 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12692}
12693
89e63ee4
DE
12694/* Traversal function for queue_and_load_all_dwo_tus. */
12695
12696static int
12697queue_and_load_dwo_tu (void **slot, void *info)
12698{
12699 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12700 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12701 ULONGEST signature = dwo_unit->signature;
d460f660 12702 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12703
12704 if (sig_type != NULL)
12705 {
89e63ee4
DE
12706 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12707 a real dependency of PER_CU on SIG_TYPE. That is detected later
12708 while processing PER_CU. */
0d305d5c
TT
12709 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12710 cu->language))
12711 load_full_type_unit (sig_type, cu->per_objfile);
12712 cu->per_cu->imported_symtabs_push (sig_type);
89e63ee4
DE
12713 }
12714
12715 return 1;
12716}
12717
1b555f17 12718/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12719 The DWO may have the only definition of the type, though it may not be
12720 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12721 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12722
12723static void
1b555f17 12724queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12725{
12726 struct dwo_unit *dwo_unit;
12727 struct dwo_file *dwo_file;
12728
1b555f17
SM
12729 gdb_assert (cu != nullptr);
12730 gdb_assert (!cu->per_cu->is_debug_types);
12731 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12732
1b555f17 12733 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12734 gdb_assert (dwo_unit != NULL);
12735
12736 dwo_file = dwo_unit->dwo_file;
12737 if (dwo_file->tus != NULL)
1b555f17 12738 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
12739}
12740
3019eac3 12741/* Read in various DIEs. */
348e048f 12742
d389af10 12743/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12744 Inherit only the children of the DW_AT_abstract_origin DIE not being
12745 already referenced by DW_AT_abstract_origin from the children of the
12746 current DIE. */
d389af10
JK
12747
12748static void
12749inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12750{
12751 struct die_info *child_die;
791afaa2 12752 sect_offset *offsetp;
d389af10
JK
12753 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12754 struct die_info *origin_die;
12755 /* Iterator of the ORIGIN_DIE children. */
12756 struct die_info *origin_child_die;
d389af10 12757 struct attribute *attr;
cd02d79d
PA
12758 struct dwarf2_cu *origin_cu;
12759 struct pending **origin_previous_list_in_scope;
d389af10
JK
12760
12761 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12762 if (!attr)
12763 return;
12764
cd02d79d
PA
12765 /* Note that following die references may follow to a die in a
12766 different cu. */
12767
12768 origin_cu = cu;
12769 origin_die = follow_die_ref (die, attr, &origin_cu);
12770
12771 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12772 symbols in. */
12773 origin_previous_list_in_scope = origin_cu->list_in_scope;
12774 origin_cu->list_in_scope = cu->list_in_scope;
12775
edb3359d
DJ
12776 if (die->tag != origin_die->tag
12777 && !(die->tag == DW_TAG_inlined_subroutine
12778 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12779 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12780 sect_offset_str (die->sect_off),
12781 sect_offset_str (origin_die->sect_off));
d389af10 12782
34dc0f95
TBA
12783 /* Find if the concrete and abstract trees are structurally the
12784 same. This is a shallow traversal and it is not bullet-proof;
12785 the compiler can trick the debugger into believing that the trees
12786 are isomorphic, whereas they actually are not. However, the
12787 likelyhood of this happening is pretty low, and a full-fledged
12788 check would be an overkill. */
12789 bool are_isomorphic = true;
12790 die_info *concrete_child = die->child;
12791 die_info *abstract_child = origin_die->child;
12792 while (concrete_child != nullptr || abstract_child != nullptr)
12793 {
12794 if (concrete_child == nullptr
12795 || abstract_child == nullptr
12796 || concrete_child->tag != abstract_child->tag)
12797 {
12798 are_isomorphic = false;
12799 break;
12800 }
12801
12802 concrete_child = concrete_child->sibling;
12803 abstract_child = abstract_child->sibling;
12804 }
12805
12806 /* Walk the origin's children in parallel to the concrete children.
12807 This helps match an origin child in case the debug info misses
12808 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12809 origin tree may not have the same tree structure as the concrete
12810 DIE, though. */
12811 die_info *corresponding_abstract_child
12812 = are_isomorphic ? origin_die->child : nullptr;
12813
791afaa2 12814 std::vector<sect_offset> offsets;
d389af10 12815
3ea89b92
PMR
12816 for (child_die = die->child;
12817 child_die && child_die->tag;
436c571c 12818 child_die = child_die->sibling)
3ea89b92
PMR
12819 {
12820 struct die_info *child_origin_die;
12821 struct dwarf2_cu *child_origin_cu;
12822
12823 /* We are trying to process concrete instance entries:
216f72a1 12824 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12825 it's not relevant to our analysis here. i.e. detecting DIEs that are
12826 present in the abstract instance but not referenced in the concrete
12827 one. */
216f72a1 12828 if (child_die->tag == DW_TAG_call_site
dda83cd7 12829 || child_die->tag == DW_TAG_GNU_call_site)
34dc0f95
TBA
12830 {
12831 if (are_isomorphic)
12832 corresponding_abstract_child
12833 = corresponding_abstract_child->sibling;
12834 continue;
12835 }
3ea89b92 12836
c38f313d
DJ
12837 /* For each CHILD_DIE, find the corresponding child of
12838 ORIGIN_DIE. If there is more than one layer of
12839 DW_AT_abstract_origin, follow them all; there shouldn't be,
12840 but GCC versions at least through 4.4 generate this (GCC PR
12841 40573). */
3ea89b92
PMR
12842 child_origin_die = child_die;
12843 child_origin_cu = cu;
c38f313d
DJ
12844 while (1)
12845 {
cd02d79d
PA
12846 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12847 child_origin_cu);
c38f313d
DJ
12848 if (attr == NULL)
12849 break;
cd02d79d
PA
12850 child_origin_die = follow_die_ref (child_origin_die, attr,
12851 &child_origin_cu);
c38f313d
DJ
12852 }
12853
34dc0f95
TBA
12854 /* If missing DW_AT_abstract_origin, try the corresponding child
12855 of the origin. Clang emits such lexical scopes. */
12856 if (child_origin_die == child_die
12857 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12858 && are_isomorphic
12859 && child_die->tag == DW_TAG_lexical_block)
12860 child_origin_die = corresponding_abstract_child;
12861
d389af10
JK
12862 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12863 counterpart may exist. */
c38f313d 12864 if (child_origin_die != child_die)
d389af10 12865 {
edb3359d
DJ
12866 if (child_die->tag != child_origin_die->tag
12867 && !(child_die->tag == DW_TAG_inlined_subroutine
12868 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12869 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12870 "different tags"),
9d8780f0
SM
12871 sect_offset_str (child_die->sect_off),
12872 sect_offset_str (child_origin_die->sect_off));
c38f313d 12873 if (child_origin_die->parent != origin_die)
b98664d3 12874 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12875 "different parents"),
9d8780f0
SM
12876 sect_offset_str (child_die->sect_off),
12877 sect_offset_str (child_origin_die->sect_off));
c38f313d 12878 else
791afaa2 12879 offsets.push_back (child_origin_die->sect_off);
d389af10 12880 }
34dc0f95
TBA
12881
12882 if (are_isomorphic)
12883 corresponding_abstract_child = corresponding_abstract_child->sibling;
d389af10 12884 }
791afaa2
TT
12885 std::sort (offsets.begin (), offsets.end ());
12886 sect_offset *offsets_end = offsets.data () + offsets.size ();
12887 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12888 if (offsetp[-1] == *offsetp)
b98664d3 12889 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12890 "to DIE %s as their abstract origin"),
12891 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12892
791afaa2 12893 offsetp = offsets.data ();
d389af10
JK
12894 origin_child_die = origin_die->child;
12895 while (origin_child_die && origin_child_die->tag)
12896 {
12897 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12898 while (offsetp < offsets_end
9c541725 12899 && *offsetp < origin_child_die->sect_off)
d389af10 12900 offsetp++;
b64f50a1 12901 if (offsetp >= offsets_end
9c541725 12902 || *offsetp > origin_child_die->sect_off)
d389af10 12903 {
adde2bff
DE
12904 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12905 Check whether we're already processing ORIGIN_CHILD_DIE.
12906 This can happen with mutually referenced abstract_origins.
12907 PR 16581. */
12908 if (!origin_child_die->in_process)
12909 process_die (origin_child_die, origin_cu);
d389af10 12910 }
436c571c 12911 origin_child_die = origin_child_die->sibling;
d389af10 12912 }
cd02d79d 12913 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12914
12915 if (cu != origin_cu)
12916 compute_delayed_physnames (origin_cu);
d389af10
JK
12917}
12918
c906108c 12919static void
e7c27a73 12920read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12921{
5e22e966 12922 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 12923 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 12924 struct context_stack *newobj;
c906108c
SS
12925 CORE_ADDR lowpc;
12926 CORE_ADDR highpc;
12927 struct die_info *child_die;
edb3359d 12928 struct attribute *attr, *call_line, *call_file;
15d034d0 12929 const char *name;
e142c38c 12930 CORE_ADDR baseaddr;
801e3a5b 12931 struct block *block;
edb3359d 12932 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12933 std::vector<struct symbol *> template_args;
34eaf542 12934 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12935
12936 if (inlined_func)
12937 {
12938 /* If we do not have call site information, we can't show the
12939 caller of this inlined function. That's too confusing, so
12940 only use the scope for local variables. */
12941 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12942 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12943 if (call_line == NULL || call_file == NULL)
12944 {
12945 read_lexical_block_scope (die, cu);
12946 return;
12947 }
12948 }
c906108c 12949
b3b3bada 12950 baseaddr = objfile->text_section_offset ();
e142c38c 12951
94af9270 12952 name = dwarf2_name (die, cu);
c906108c 12953
e8d05480
JB
12954 /* Ignore functions with missing or empty names. These are actually
12955 illegal according to the DWARF standard. */
12956 if (name == NULL)
12957 {
b98664d3 12958 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12959 sect_offset_str (die->sect_off));
e8d05480
JB
12960 return;
12961 }
12962
12963 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12964 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12965 <= PC_BOUNDS_INVALID)
e8d05480 12966 {
ae4d0c03 12967 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 12968 if (attr == nullptr || !attr->as_boolean ())
b98664d3 12969 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12970 "for subprogram DIE at %s"),
12971 sect_offset_str (die->sect_off));
e8d05480
JB
12972 return;
12973 }
c906108c 12974
3e29f34a
MR
12975 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12976 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12977
34eaf542
TT
12978 /* If we have any template arguments, then we must allocate a
12979 different sort of symbol. */
436c571c 12980 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
12981 {
12982 if (child_die->tag == DW_TAG_template_type_param
12983 || child_die->tag == DW_TAG_template_value_param)
12984 {
8c14c3a3 12985 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 12986 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12987 break;
12988 }
12989 }
12990
702cf3f5 12991 gdb_assert (cu->get_builder () != nullptr);
c24bdb02 12992 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12993 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12994 (struct symbol *) templ_func);
4c2df51b 12995
81873cc8 12996 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12997 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12998 cu->language);
12999
4cecd739
DJ
13000 /* If there is a location expression for DW_AT_frame_base, record
13001 it. */
e142c38c 13002 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13003 if (attr != nullptr)
fe978cb0 13004 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13005
63e43d3a
PMR
13006 /* If there is a location for the static link, record it. */
13007 newobj->static_link = NULL;
13008 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13009 if (attr != nullptr)
63e43d3a 13010 {
224c3ddb
SM
13011 newobj->static_link
13012 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13013 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13014 cu->addr_type ());
63e43d3a
PMR
13015 }
13016
c24bdb02 13017 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13018
639d11d3 13019 if (die->child != NULL)
c906108c 13020 {
639d11d3 13021 child_die = die->child;
c906108c
SS
13022 while (child_die && child_die->tag)
13023 {
34eaf542
TT
13024 if (child_die->tag == DW_TAG_template_type_param
13025 || child_die->tag == DW_TAG_template_value_param)
13026 {
13027 struct symbol *arg = new_symbol (child_die, NULL, cu);
13028
f1078f66 13029 if (arg != NULL)
2f4732b0 13030 template_args.push_back (arg);
34eaf542
TT
13031 }
13032 else
13033 process_die (child_die, cu);
436c571c 13034 child_die = child_die->sibling;
c906108c
SS
13035 }
13036 }
13037
d389af10
JK
13038 inherit_abstract_dies (die, cu);
13039
4a811a97
UW
13040 /* If we have a DW_AT_specification, we might need to import using
13041 directives from the context of the specification DIE. See the
13042 comment in determine_prefix. */
13043 if (cu->language == language_cplus
13044 && dwarf2_attr (die, DW_AT_specification, cu))
13045 {
13046 struct dwarf2_cu *spec_cu = cu;
13047 struct die_info *spec_die = die_specification (die, &spec_cu);
13048
13049 while (spec_die)
13050 {
13051 child_die = spec_die->child;
13052 while (child_die && child_die->tag)
13053 {
13054 if (child_die->tag == DW_TAG_imported_module)
13055 process_die (child_die, spec_cu);
436c571c 13056 child_die = child_die->sibling;
4a811a97
UW
13057 }
13058
13059 /* In some cases, GCC generates specification DIEs that
13060 themselves contain DW_AT_specification attributes. */
13061 spec_die = die_specification (spec_die, &spec_cu);
13062 }
13063 }
13064
c24bdb02 13065 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13066 /* Make a block for the local symbols within. */
c24bdb02 13067 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13068 cstk.static_link, lowpc, highpc);
801e3a5b 13069
df8a16a1 13070 /* For C++, set the block's scope. */
45280282
IB
13071 if ((cu->language == language_cplus
13072 || cu->language == language_fortran
c44af4eb
TT
13073 || cu->language == language_d
13074 || cu->language == language_rust)
4d4ec4e5 13075 && cu->processing_has_namespace_info)
195a3f6c
TT
13076 block_set_scope (block, determine_prefix (die, cu),
13077 &objfile->objfile_obstack);
df8a16a1 13078
801e3a5b
JB
13079 /* If we have address ranges, record them. */
13080 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13081
a60f3166 13082 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13083
34eaf542 13084 /* Attach template arguments to function. */
2f4732b0 13085 if (!template_args.empty ())
34eaf542
TT
13086 {
13087 gdb_assert (templ_func != NULL);
13088
2f4732b0 13089 templ_func->n_template_arguments = template_args.size ();
34eaf542 13090 templ_func->template_arguments
dda83cd7 13091 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13092 templ_func->n_template_arguments);
34eaf542 13093 memcpy (templ_func->template_arguments,
2f4732b0 13094 template_args.data (),
34eaf542 13095 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13096
13097 /* Make sure that the symtab is set on the new symbols. Even
13098 though they don't appear in this symtab directly, other parts
13099 of gdb assume that symbols do, and this is reasonably
13100 true. */
8634679f 13101 for (symbol *sym : template_args)
3e1d3d8c 13102 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13103 }
13104
208d8187
JB
13105 /* In C++, we can have functions nested inside functions (e.g., when
13106 a function declares a class that has methods). This means that
13107 when we finish processing a function scope, we may need to go
13108 back to building a containing block's symbol lists. */
c24bdb02
KS
13109 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13110 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13111
921e78cf
JB
13112 /* If we've finished processing a top-level function, subsequent
13113 symbols go in the file symbol list. */
c24bdb02
KS
13114 if (cu->get_builder ()->outermost_context_p ())
13115 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13116}
13117
13118/* Process all the DIES contained within a lexical block scope. Start
13119 a new scope, process the dies, and then close the scope. */
13120
13121static void
e7c27a73 13122read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13123{
5e22e966 13124 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13125 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13126 CORE_ADDR lowpc, highpc;
13127 struct die_info *child_die;
e142c38c
DJ
13128 CORE_ADDR baseaddr;
13129
b3b3bada 13130 baseaddr = objfile->text_section_offset ();
c906108c
SS
13131
13132 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13133 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13134 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13135 be nasty. Might be easier to properly extend generic blocks to
af34e669 13136 describe ranges. */
e385593e
JK
13137 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13138 {
13139 case PC_BOUNDS_NOT_PRESENT:
13140 /* DW_TAG_lexical_block has no attributes, process its children as if
13141 there was no wrapping by that DW_TAG_lexical_block.
13142 GCC does no longer produces such DWARF since GCC r224161. */
13143 for (child_die = die->child;
13144 child_die != NULL && child_die->tag;
436c571c 13145 child_die = child_die->sibling)
4f7bc5ed
TT
13146 {
13147 /* We might already be processing this DIE. This can happen
13148 in an unusual circumstance -- where a subroutine A
13149 appears lexically in another subroutine B, but A actually
13150 inlines B. The recursion is broken here, rather than in
13151 inherit_abstract_dies, because it seems better to simply
13152 drop concrete children here. */
13153 if (!child_die->in_process)
13154 process_die (child_die, cu);
13155 }
e385593e
JK
13156 return;
13157 case PC_BOUNDS_INVALID:
13158 return;
13159 }
3e29f34a
MR
13160 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13161 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13162
c24bdb02 13163 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13164 if (die->child != NULL)
c906108c 13165 {
639d11d3 13166 child_die = die->child;
c906108c
SS
13167 while (child_die && child_die->tag)
13168 {
e7c27a73 13169 process_die (child_die, cu);
436c571c 13170 child_die = child_die->sibling;
c906108c
SS
13171 }
13172 }
3ea89b92 13173 inherit_abstract_dies (die, cu);
c24bdb02 13174 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13175
c24bdb02
KS
13176 if (*cu->get_builder ()->get_local_symbols () != NULL
13177 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13178 {
801e3a5b 13179 struct block *block
dda83cd7 13180 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13181 cstk.start_addr, highpc);
801e3a5b
JB
13182
13183 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13184 do here, means that recording a parent's ranges entails
13185 walking across all its children's ranges as they appear in
13186 the address map, which is quadratic behavior.
13187
13188 It would be nicer to record the parent's ranges before
13189 traversing its children, simply overriding whatever you find
13190 there. But since we don't even decide whether to create a
13191 block until after we've traversed its children, that's hard
13192 to do. */
801e3a5b 13193 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13194 }
c24bdb02
KS
13195 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13196 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13197}
13198
216f72a1 13199/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13200
13201static void
13202read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13203{
5e22e966 13204 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13205 struct objfile *objfile = per_objfile->objfile;
08feed99 13206 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13207 CORE_ADDR pc, baseaddr;
13208 struct attribute *attr;
13209 struct call_site *call_site, call_site_local;
13210 void **slot;
13211 int nparams;
13212 struct die_info *child_die;
13213
b3b3bada 13214 baseaddr = objfile->text_section_offset ();
96408a79 13215
216f72a1
JK
13216 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13217 if (attr == NULL)
13218 {
13219 /* This was a pre-DWARF-5 GNU extension alias
13220 for DW_AT_call_return_pc. */
13221 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13222 }
96408a79
SA
13223 if (!attr)
13224 {
b98664d3 13225 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13226 "DIE %s [in module %s]"),
13227 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13228 return;
13229 }
95f982e5 13230 pc = attr->as_address () + baseaddr;
3e29f34a 13231 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13232
13233 if (cu->call_site_htab == NULL)
13234 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13235 NULL, &objfile->objfile_obstack,
13236 hashtab_obstack_allocate, NULL);
13237 call_site_local.pc = pc;
13238 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13239 if (*slot != NULL)
13240 {
b98664d3 13241 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13242 "DIE %s [in module %s]"),
13243 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13244 objfile_name (objfile));
96408a79
SA
13245 return;
13246 }
13247
13248 /* Count parameters at the caller. */
13249
13250 nparams = 0;
13251 for (child_die = die->child; child_die && child_die->tag;
436c571c 13252 child_die = child_die->sibling)
96408a79 13253 {
216f72a1 13254 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13255 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13256 {
b98664d3 13257 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13258 "DW_TAG_call_site child DIE %s [in module %s]"),
13259 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13260 objfile_name (objfile));
96408a79
SA
13261 continue;
13262 }
13263
13264 nparams++;
13265 }
13266
224c3ddb
SM
13267 call_site
13268 = ((struct call_site *)
13269 obstack_alloc (&objfile->objfile_obstack,
13270 sizeof (*call_site)
13271 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13272 *slot = call_site;
13273 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13274 call_site->pc = pc;
13275
216f72a1
JK
13276 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13277 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13278 {
13279 struct die_info *func_die;
13280
13281 /* Skip also over DW_TAG_inlined_subroutine. */
13282 for (func_die = die->parent;
13283 func_die && func_die->tag != DW_TAG_subprogram
13284 && func_die->tag != DW_TAG_subroutine_type;
13285 func_die = func_die->parent);
13286
216f72a1
JK
13287 /* DW_AT_call_all_calls is a superset
13288 of DW_AT_call_all_tail_calls. */
96408a79 13289 if (func_die
dda83cd7
SM
13290 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13291 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13292 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13293 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13294 {
13295 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13296 not complete. But keep CALL_SITE for look ups via call_site_htab,
13297 both the initial caller containing the real return address PC and
13298 the final callee containing the current PC of a chain of tail
13299 calls do not need to have the tail call list complete. But any
13300 function candidate for a virtual tail call frame searched via
13301 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13302 determined unambiguously. */
13303 }
13304 else
13305 {
13306 struct type *func_type = NULL;
13307
13308 if (func_die)
13309 func_type = get_die_type (func_die, cu);
13310 if (func_type != NULL)
13311 {
78134374 13312 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13313
13314 /* Enlist this call site to the function. */
13315 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13316 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13317 }
13318 else
b98664d3 13319 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13320 "DIE %s [in module %s]"),
13321 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13322 }
13323 }
13324
216f72a1
JK
13325 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13326 if (attr == NULL)
13327 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13328 if (attr == NULL)
13329 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13330 if (attr == NULL)
216f72a1
JK
13331 {
13332 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13333 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13334 }
96408a79 13335 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13336 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13337 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13338 else if (attr->form_is_block ())
96408a79
SA
13339 {
13340 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13341 struct dwarf_block *block = attr->as_block ();
96408a79 13342
8d749320 13343 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13344 dlbaton->data = block->data;
13345 dlbaton->size = block->size;
a50264ba 13346 dlbaton->per_objfile = per_objfile;
96408a79
SA
13347 dlbaton->per_cu = cu->per_cu;
13348
13349 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13350 }
cd6c91b4 13351 else if (attr->form_is_ref ())
96408a79 13352 {
96408a79
SA
13353 struct dwarf2_cu *target_cu = cu;
13354 struct die_info *target_die;
13355
ac9ec31b 13356 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13357 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13358 if (die_is_declaration (target_die, target_cu))
13359 {
7d45c7c3 13360 const char *target_physname;
9112db09
JK
13361
13362 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13363 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13364 if (target_physname == NULL)
9112db09 13365 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13366 if (target_physname == NULL)
b98664d3 13367 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13368 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 13369 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13370 else
7d455152 13371 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13372 }
13373 else
13374 {
13375 CORE_ADDR lowpc;
13376
13377 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13378 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13379 <= PC_BOUNDS_INVALID)
b98664d3 13380 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13381 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 13382 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13383 else
3e29f34a
MR
13384 {
13385 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13386 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13387 }
96408a79
SA
13388 }
13389 }
13390 else
b98664d3 13391 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13392 "block nor reference, for DIE %s [in module %s]"),
13393 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13394
13395 call_site->per_cu = cu->per_cu;
9f47c707 13396 call_site->per_objfile = per_objfile;
96408a79
SA
13397
13398 for (child_die = die->child;
13399 child_die && child_die->tag;
436c571c 13400 child_die = child_die->sibling)
96408a79 13401 {
96408a79 13402 struct call_site_parameter *parameter;
1788b2d3 13403 struct attribute *loc, *origin;
96408a79 13404
216f72a1 13405 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13406 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13407 {
13408 /* Already printed the complaint above. */
13409 continue;
13410 }
13411
13412 gdb_assert (call_site->parameter_count < nparams);
13413 parameter = &call_site->parameter[call_site->parameter_count];
13414
1788b2d3
JK
13415 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13416 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13417 register is contained in DW_AT_call_value. */
96408a79 13418
24c5c679 13419 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13420 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13421 if (origin == NULL)
13422 {
13423 /* This was a pre-DWARF-5 GNU extension alias
13424 for DW_AT_call_parameter. */
13425 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13426 }
cd6c91b4 13427 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13428 {
1788b2d3 13429 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13430
0826b30a 13431 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13432 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13433 {
13434 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13435 binding can be done only inside one CU. Such referenced DIE
13436 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13437 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13438 "DW_TAG_call_site child DIE %s [in module %s]"),
13439 sect_offset_str (child_die->sect_off),
9c541725 13440 objfile_name (objfile));
d76b7dbc
JK
13441 continue;
13442 }
9c541725
PA
13443 parameter->u.param_cu_off
13444 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13445 }
4fc6c0d5 13446 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13447 {
b98664d3 13448 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13449 "DW_TAG_call_site child DIE %s [in module %s]"),
13450 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13451 continue;
13452 }
24c5c679 13453 else
96408a79 13454 {
9d2246fc
TT
13455 struct dwarf_block *block = loc->as_block ();
13456
24c5c679 13457 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 13458 (block->data, &block->data[block->size]);
24c5c679
JK
13459 if (parameter->u.dwarf_reg != -1)
13460 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
13461 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13462 &block->data[block->size],
24c5c679
JK
13463 &parameter->u.fb_offset))
13464 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13465 else
13466 {
b98664d3 13467 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13468 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13469 "DW_TAG_call_site child DIE %s "
24c5c679 13470 "[in module %s]"),
9d8780f0 13471 sect_offset_str (child_die->sect_off),
9c541725 13472 objfile_name (objfile));
24c5c679
JK
13473 continue;
13474 }
96408a79
SA
13475 }
13476
216f72a1
JK
13477 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13478 if (attr == NULL)
13479 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13480 if (attr == NULL || !attr->form_is_block ())
96408a79 13481 {
b98664d3 13482 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13483 "DW_TAG_call_site child DIE %s [in module %s]"),
13484 sect_offset_str (child_die->sect_off),
9c541725 13485 objfile_name (objfile));
96408a79
SA
13486 continue;
13487 }
9d2246fc
TT
13488
13489 struct dwarf_block *block = attr->as_block ();
13490 parameter->value = block->data;
13491 parameter->value_size = block->size;
96408a79
SA
13492
13493 /* Parameters are not pre-cleared by memset above. */
13494 parameter->data_value = NULL;
13495 parameter->data_value_size = 0;
13496 call_site->parameter_count++;
13497
216f72a1
JK
13498 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13499 if (attr == NULL)
13500 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13501 if (attr != nullptr)
96408a79 13502 {
4fc6c0d5 13503 if (!attr->form_is_block ())
b98664d3 13504 complaint (_("No DW_FORM_block* DW_AT_call_data_value 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));
96408a79
SA
13508 else
13509 {
9d2246fc
TT
13510 block = attr->as_block ();
13511 parameter->data_value = block->data;
13512 parameter->data_value_size = block->size;
96408a79
SA
13513 }
13514 }
13515 }
13516}
13517
71a3c369
TT
13518/* Helper function for read_variable. If DIE represents a virtual
13519 table, then return the type of the concrete object that is
13520 associated with the virtual table. Otherwise, return NULL. */
13521
13522static struct type *
13523rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13524{
13525 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13526 if (attr == NULL)
13527 return NULL;
13528
13529 /* Find the type DIE. */
13530 struct die_info *type_die = NULL;
13531 struct dwarf2_cu *type_cu = cu;
13532
cd6c91b4 13533 if (attr->form_is_ref ())
71a3c369
TT
13534 type_die = follow_die_ref (die, attr, &type_cu);
13535 if (type_die == NULL)
13536 return NULL;
13537
13538 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13539 return NULL;
13540 return die_containing_type (type_die, type_cu);
13541}
13542
13543/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13544
13545static void
13546read_variable (struct die_info *die, struct dwarf2_cu *cu)
13547{
13548 struct rust_vtable_symbol *storage = NULL;
13549
13550 if (cu->language == language_rust)
13551 {
13552 struct type *containing_type = rust_containing_type (die, cu);
13553
13554 if (containing_type != NULL)
13555 {
5e22e966 13556 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13557
8c14c3a3 13558 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13559 storage->concrete_type = containing_type;
cf724bc9 13560 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13561 }
13562 }
13563
e4a62c65
TV
13564 struct symbol *res = new_symbol (die, NULL, cu, storage);
13565 struct attribute *abstract_origin
13566 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13567 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13568 if (res == NULL && loc && abstract_origin)
13569 {
13570 /* We have a variable without a name, but with a location and an abstract
13571 origin. This may be a concrete instance of an abstract variable
13572 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13573 later. */
13574 struct dwarf2_cu *origin_cu = cu;
13575 struct die_info *origin_die
13576 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13577 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13578 per_objfile->per_bfd->abstract_to_concrete
13579 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13580 }
71a3c369
TT
13581}
13582
43988095
JK
13583/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13584 reading .debug_rnglists.
13585 Callback's type should be:
13586 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13587 Return true if the attributes are present and valid, otherwise,
13588 return false. */
13589
13590template <typename Callback>
13591static bool
13592dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 13593 dwarf_tag tag, Callback &&callback)
43988095 13594{
976ca316
SM
13595 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13596 struct objfile *objfile = per_objfile->objfile;
43988095 13597 bfd *obfd = objfile->obfd;
43988095 13598 /* Base address selection entry. */
2b24b6e4 13599 gdb::optional<CORE_ADDR> base;
43988095 13600 const gdb_byte *buffer;
43988095
JK
13601 CORE_ADDR baseaddr;
13602 bool overflow = false;
d0ce17d8
CT
13603 ULONGEST addr_index;
13604 struct dwarf2_section_info *rnglists_section;
43988095 13605
43988095 13606 base = cu->base_address;
d0ce17d8
CT
13607 rnglists_section = cu_debug_rnglists_section (cu, tag);
13608 rnglists_section->read (objfile);
43988095 13609
d0ce17d8 13610 if (offset >= rnglists_section->size)
43988095 13611 {
b98664d3 13612 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13613 offset);
13614 return false;
13615 }
d0ce17d8 13616 buffer = rnglists_section->buffer + offset;
43988095 13617
b3b3bada 13618 baseaddr = objfile->text_section_offset ();
43988095
JK
13619
13620 while (1)
13621 {
7814882a
JK
13622 /* Initialize it due to a false compiler warning. */
13623 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
13624 const gdb_byte *buf_end = (rnglists_section->buffer
13625 + rnglists_section->size);
43988095
JK
13626 unsigned int bytes_read;
13627
13628 if (buffer == buf_end)
13629 {
13630 overflow = true;
13631 break;
13632 }
13633 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13634 switch (rlet)
13635 {
13636 case DW_RLE_end_of_list:
13637 break;
13638 case DW_RLE_base_address:
13639 if (buffer + cu->header.addr_size > buf_end)
13640 {
13641 overflow = true;
13642 break;
13643 }
c8a7a66f 13644 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13645 buffer += bytes_read;
13646 break;
dda83cd7
SM
13647 case DW_RLE_base_addressx:
13648 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13649 buffer += bytes_read;
13650 base = read_addr_index (cu, addr_index);
13651 break;
43988095
JK
13652 case DW_RLE_start_length:
13653 if (buffer + cu->header.addr_size > buf_end)
13654 {
13655 overflow = true;
13656 break;
13657 }
c8a7a66f
TT
13658 range_beginning = cu->header.read_address (obfd, buffer,
13659 &bytes_read);
43988095
JK
13660 buffer += bytes_read;
13661 range_end = (range_beginning
13662 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13663 buffer += bytes_read;
13664 if (buffer > buf_end)
13665 {
13666 overflow = true;
13667 break;
13668 }
13669 break;
d0ce17d8 13670 case DW_RLE_startx_length:
dda83cd7
SM
13671 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13672 buffer += bytes_read;
13673 range_beginning = read_addr_index (cu, addr_index);
13674 if (buffer > buf_end)
13675 {
13676 overflow = true;
13677 break;
13678 }
13679 range_end = (range_beginning
13680 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13681 buffer += bytes_read;
13682 break;
43988095
JK
13683 case DW_RLE_offset_pair:
13684 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13685 buffer += bytes_read;
13686 if (buffer > buf_end)
13687 {
13688 overflow = true;
13689 break;
13690 }
13691 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13692 buffer += bytes_read;
13693 if (buffer > buf_end)
13694 {
13695 overflow = true;
13696 break;
13697 }
13698 break;
13699 case DW_RLE_start_end:
13700 if (buffer + 2 * cu->header.addr_size > buf_end)
13701 {
13702 overflow = true;
13703 break;
13704 }
c8a7a66f
TT
13705 range_beginning = cu->header.read_address (obfd, buffer,
13706 &bytes_read);
43988095 13707 buffer += bytes_read;
c8a7a66f 13708 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13709 buffer += bytes_read;
13710 break;
d0ce17d8 13711 case DW_RLE_startx_endx:
dda83cd7
SM
13712 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13713 buffer += bytes_read;
13714 range_beginning = read_addr_index (cu, addr_index);
13715 if (buffer > buf_end)
13716 {
13717 overflow = true;
13718 break;
13719 }
13720 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13721 buffer += bytes_read;
13722 range_end = read_addr_index (cu, addr_index);
13723 break;
43988095 13724 default:
b98664d3 13725 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13726 return false;
13727 }
13728 if (rlet == DW_RLE_end_of_list || overflow)
13729 break;
13730 if (rlet == DW_RLE_base_address)
13731 continue;
13732
43988095
JK
13733 if (range_beginning > range_end)
13734 {
13735 /* Inverted range entries are invalid. */
b98664d3 13736 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13737 return false;
13738 }
13739
13740 /* Empty range entries have no effect. */
13741 if (range_beginning == range_end)
13742 continue;
13743
d0ce17d8
CT
13744 /* Only DW_RLE_offset_pair needs the base address added. */
13745 if (rlet == DW_RLE_offset_pair)
13746 {
13747 if (!base.has_value ())
13748 {
13749 /* We have no valid base address for the DW_RLE_offset_pair. */
13750 complaint (_("Invalid .debug_rnglists data (no base address for "
13751 "DW_RLE_offset_pair)"));
13752 return false;
13753 }
13754
13755 range_beginning += *base;
13756 range_end += *base;
13757 }
43988095
JK
13758
13759 /* A not-uncommon case of bad debug info.
13760 Don't pollute the addrmap with bad data. */
13761 if (range_beginning + baseaddr == 0
976ca316 13762 && !per_objfile->per_bfd->has_section_at_zero)
43988095 13763 {
b98664d3 13764 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13765 " [in module %s]"), objfile_name (objfile));
13766 continue;
13767 }
13768
13769 callback (range_beginning, range_end);
13770 }
13771
13772 if (overflow)
13773 {
b98664d3 13774 complaint (_("Offset %d is not terminated "
43988095
JK
13775 "for DW_AT_ranges attribute"),
13776 offset);
13777 return false;
13778 }
13779
13780 return true;
13781}
13782
13783/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13784 Callback's type should be:
13785 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13786 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13787
43988095 13788template <typename Callback>
43039443 13789static int
d0ce17d8 13790dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 13791 Callback &&callback)
43039443 13792{
5e22e966
SM
13793 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13794 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13795 struct comp_unit_head *cu_header = &cu->header;
13796 bfd *obfd = objfile->obfd;
13797 unsigned int addr_size = cu_header->addr_size;
13798 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13799 /* Base address selection entry. */
2b24b6e4 13800 gdb::optional<CORE_ADDR> base;
43039443 13801 unsigned int dummy;
d521ce57 13802 const gdb_byte *buffer;
ff013f42 13803 CORE_ADDR baseaddr;
43039443 13804
43988095 13805 if (cu_header->version >= 5)
d0ce17d8 13806 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 13807
d00adf39 13808 base = cu->base_address;
43039443 13809
5e22e966
SM
13810 per_objfile->per_bfd->ranges.read (objfile);
13811 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13812 {
b98664d3 13813 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13814 offset);
13815 return 0;
13816 }
5e22e966 13817 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13818
b3b3bada 13819 baseaddr = objfile->text_section_offset ();
ff013f42 13820
43039443
JK
13821 while (1)
13822 {
13823 CORE_ADDR range_beginning, range_end;
13824
c8a7a66f 13825 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13826 buffer += addr_size;
c8a7a66f 13827 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13828 buffer += addr_size;
13829 offset += 2 * addr_size;
13830
13831 /* An end of list marker is a pair of zero addresses. */
13832 if (range_beginning == 0 && range_end == 0)
13833 /* Found the end of list entry. */
13834 break;
13835
13836 /* Each base address selection entry is a pair of 2 values.
13837 The first is the largest possible address, the second is
13838 the base address. Check for a base address here. */
13839 if ((range_beginning & mask) == mask)
13840 {
28d2bfb9
AB
13841 /* If we found the largest possible address, then we already
13842 have the base address in range_end. */
13843 base = range_end;
43039443
JK
13844 continue;
13845 }
13846
2b24b6e4 13847 if (!base.has_value ())
43039443
JK
13848 {
13849 /* We have no valid base address for the ranges
13850 data. */
b98664d3 13851 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13852 return 0;
13853 }
13854
9277c30c
UW
13855 if (range_beginning > range_end)
13856 {
13857 /* Inverted range entries are invalid. */
b98664d3 13858 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13859 return 0;
13860 }
13861
13862 /* Empty range entries have no effect. */
13863 if (range_beginning == range_end)
13864 continue;
13865
2b24b6e4
TT
13866 range_beginning += *base;
13867 range_end += *base;
43039443 13868
01093045
DE
13869 /* A not-uncommon case of bad debug info.
13870 Don't pollute the addrmap with bad data. */
13871 if (range_beginning + baseaddr == 0
5e22e966 13872 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13873 {
b98664d3 13874 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13875 " [in module %s]"), objfile_name (objfile));
01093045
DE
13876 continue;
13877 }
13878
5f46c5a5
JK
13879 callback (range_beginning, range_end);
13880 }
13881
13882 return 1;
13883}
13884
13885/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13886 Return 1 if the attributes are present and valid, otherwise, return 0.
efd7398e 13887 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
5f46c5a5
JK
13888
13889static int
13890dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13891 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 13892 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 13893{
5e22e966 13894 struct objfile *objfile = cu->per_objfile->objfile;
84685904 13895 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 13896 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13897 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13898 int low_set = 0;
13899 CORE_ADDR low = 0;
13900 CORE_ADDR high = 0;
13901 int retval;
13902
d0ce17d8 13903 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
13904 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13905 {
9277c30c 13906 if (ranges_pst != NULL)
3e29f34a
MR
13907 {
13908 CORE_ADDR lowpc;
13909 CORE_ADDR highpc;
13910
79748972
TT
13911 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13912 range_beginning + baseaddr)
13913 - baseaddr);
13914 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13915 range_end + baseaddr)
13916 - baseaddr);
84685904 13917 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 13918 lowpc, highpc - 1, ranges_pst);
3e29f34a 13919 }
ff013f42 13920
43039443
JK
13921 /* FIXME: This is recording everything as a low-high
13922 segment of consecutive addresses. We should have a
13923 data structure for discontiguous block ranges
13924 instead. */
13925 if (! low_set)
13926 {
13927 low = range_beginning;
13928 high = range_end;
13929 low_set = 1;
13930 }
13931 else
13932 {
13933 if (range_beginning < low)
13934 low = range_beginning;
13935 if (range_end > high)
13936 high = range_end;
13937 }
5f46c5a5
JK
13938 });
13939 if (!retval)
13940 return 0;
43039443
JK
13941
13942 if (! low_set)
13943 /* If the first entry is an end-of-list marker, the range
13944 describes an empty scope, i.e. no instructions. */
13945 return 0;
13946
13947 if (low_return)
13948 *low_return = low;
13949 if (high_return)
13950 *high_return = high;
13951 return 1;
13952}
13953
3a2b436a
JK
13954/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13955 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13956 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13957
3a2b436a 13958static enum pc_bounds_kind
af34e669 13959dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13960 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13961 dwarf2_psymtab *pst)
c906108c 13962{
976ca316 13963 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 13964 struct attribute *attr;
91da1414 13965 struct attribute *attr_high;
af34e669
DJ
13966 CORE_ADDR low = 0;
13967 CORE_ADDR high = 0;
e385593e 13968 enum pc_bounds_kind ret;
c906108c 13969
91da1414
MW
13970 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13971 if (attr_high)
af34e669 13972 {
e142c38c 13973 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13974 if (attr != nullptr)
dda83cd7 13975 {
95f982e5
TT
13976 low = attr->as_address ();
13977 high = attr_high->as_address ();
cd6c91b4 13978 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13979 high += low;
91da1414 13980 }
af34e669
DJ
13981 else
13982 /* Found high w/o low attribute. */
e385593e 13983 return PC_BOUNDS_INVALID;
af34e669
DJ
13984
13985 /* Found consecutive range of addresses. */
3a2b436a 13986 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13987 }
c906108c 13988 else
af34e669 13989 {
e142c38c 13990 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 13991 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 13992 {
2b0c7f41
SM
13993 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13994 on DWARF version). */
13995 ULONGEST ranges_offset = attr->as_unsigned ();
13996
13997 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13998 this value. */
13999 if (die->tag != DW_TAG_compile_unit)
14000 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14001
af34e669 14002 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14003 .debug_ranges section. */
d0ce17d8
CT
14004 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14005 die->tag))
e385593e 14006 return PC_BOUNDS_INVALID;
43039443 14007 /* Found discontinuous range of addresses. */
3a2b436a 14008 ret = PC_BOUNDS_RANGES;
af34e669 14009 }
e385593e
JK
14010 else
14011 return PC_BOUNDS_NOT_PRESENT;
af34e669 14012 }
c906108c 14013
48fbe735 14014 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14015 if (high <= low)
e385593e 14016 return PC_BOUNDS_INVALID;
c906108c
SS
14017
14018 /* When using the GNU linker, .gnu.linkonce. sections are used to
14019 eliminate duplicate copies of functions and vtables and such.
14020 The linker will arbitrarily choose one and discard the others.
14021 The AT_*_pc values for such functions refer to local labels in
14022 these sections. If the section from that file was discarded, the
14023 labels are not in the output, so the relocs get a value of 0.
14024 If this is a discarded function, mark the pc bounds as invalid,
14025 so that GDB will ignore it. */
976ca316 14026 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14027 return PC_BOUNDS_INVALID;
c906108c
SS
14028
14029 *lowpc = low;
96408a79
SA
14030 if (highpc)
14031 *highpc = high;
af34e669 14032 return ret;
c906108c
SS
14033}
14034
b084d499
JB
14035/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14036 its low and high PC addresses. Do nothing if these addresses could not
14037 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14038 and HIGHPC to the high address if greater than HIGHPC. */
14039
14040static void
14041dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14042 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14043 struct dwarf2_cu *cu)
b084d499
JB
14044{
14045 CORE_ADDR low, high;
14046 struct die_info *child = die->child;
14047
e385593e 14048 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14049 {
325fac50
PA
14050 *lowpc = std::min (*lowpc, low);
14051 *highpc = std::max (*highpc, high);
b084d499
JB
14052 }
14053
14054 /* If the language does not allow nested subprograms (either inside
14055 subprograms or lexical blocks), we're done. */
14056 if (cu->language != language_ada)
14057 return;
6e70227d 14058
b084d499
JB
14059 /* Check all the children of the given DIE. If it contains nested
14060 subprograms, then check their pc bounds. Likewise, we need to
14061 check lexical blocks as well, as they may also contain subprogram
14062 definitions. */
14063 while (child && child->tag)
14064 {
14065 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14066 || child->tag == DW_TAG_lexical_block)
14067 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14068 child = child->sibling;
b084d499
JB
14069 }
14070}
14071
fae299cd
DC
14072/* Get the low and high pc's represented by the scope DIE, and store
14073 them in *LOWPC and *HIGHPC. If the correct values can't be
14074 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14075
14076static void
14077get_scope_pc_bounds (struct die_info *die,
14078 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14079 struct dwarf2_cu *cu)
14080{
14081 CORE_ADDR best_low = (CORE_ADDR) -1;
14082 CORE_ADDR best_high = (CORE_ADDR) 0;
14083 CORE_ADDR current_low, current_high;
14084
3a2b436a 14085 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14086 >= PC_BOUNDS_RANGES)
fae299cd
DC
14087 {
14088 best_low = current_low;
14089 best_high = current_high;
14090 }
14091 else
14092 {
14093 struct die_info *child = die->child;
14094
14095 while (child && child->tag)
14096 {
14097 switch (child->tag) {
14098 case DW_TAG_subprogram:
dda83cd7 14099 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14100 break;
14101 case DW_TAG_namespace:
f55ee35c 14102 case DW_TAG_module:
fae299cd
DC
14103 /* FIXME: carlton/2004-01-16: Should we do this for
14104 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14105 that current GCC's always emit the DIEs corresponding
14106 to definitions of methods of classes as children of a
14107 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14108 the DIEs giving the declarations, which could be
14109 anywhere). But I don't see any reason why the
14110 standards says that they have to be there. */
14111 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14112
14113 if (current_low != ((CORE_ADDR) -1))
14114 {
325fac50
PA
14115 best_low = std::min (best_low, current_low);
14116 best_high = std::max (best_high, current_high);
fae299cd
DC
14117 }
14118 break;
14119 default:
0963b4bd 14120 /* Ignore. */
fae299cd
DC
14121 break;
14122 }
14123
436c571c 14124 child = child->sibling;
fae299cd
DC
14125 }
14126 }
14127
14128 *lowpc = best_low;
14129 *highpc = best_high;
14130}
14131
801e3a5b
JB
14132/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14133 in DIE. */
380bca97 14134
801e3a5b
JB
14135static void
14136dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14137 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14138{
5e22e966 14139 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14140 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14141 struct attribute *attr;
91da1414 14142 struct attribute *attr_high;
801e3a5b 14143
91da1414
MW
14144 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14145 if (attr_high)
801e3a5b 14146 {
801e3a5b 14147 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14148 if (attr != nullptr)
dda83cd7 14149 {
95f982e5
TT
14150 CORE_ADDR low = attr->as_address ();
14151 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14152
cd6c91b4 14153 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14154 high += low;
9a619af0 14155
3e29f34a
MR
14156 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14157 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14158 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14159 }
801e3a5b
JB
14160 }
14161
14162 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14163 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14164 {
2b0c7f41
SM
14165 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14166 on DWARF version). */
14167 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14168
2b0c7f41
SM
14169 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14170 this value. */
14171 if (die->tag != DW_TAG_compile_unit)
14172 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14173
2d5f09ec 14174 std::vector<blockrange> blockvec;
2b0c7f41 14175 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14176 [&] (CORE_ADDR start, CORE_ADDR end)
14177 {
58fdfd2c
JK
14178 start += baseaddr;
14179 end += baseaddr;
5f46c5a5
JK
14180 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14181 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14182 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14183 blockvec.emplace_back (start, end);
5f46c5a5 14184 });
2d5f09ec
KB
14185
14186 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14187 }
14188}
14189
685b1105
JK
14190/* Check whether the producer field indicates either of GCC < 4.6, or the
14191 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14192
685b1105
JK
14193static void
14194check_producer (struct dwarf2_cu *cu)
60d5a603 14195{
38360086 14196 int major, minor;
60d5a603
JK
14197
14198 if (cu->producer == NULL)
14199 {
14200 /* For unknown compilers expect their behavior is DWARF version
14201 compliant.
14202
14203 GCC started to support .debug_types sections by -gdwarf-4 since
14204 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14205 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14206 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14207 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14208 }
b1ffba5a 14209 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14210 {
38360086
MW
14211 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14212 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14213 }
5230b05a 14214 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14215 {
14216 cu->producer_is_icc = true;
14217 cu->producer_is_icc_lt_14 = major < 14;
14218 }
c258c396
JD
14219 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14220 cu->producer_is_codewarrior = true;
685b1105
JK
14221 else
14222 {
14223 /* For other non-GCC compilers, expect their behavior is DWARF version
14224 compliant. */
60d5a603
JK
14225 }
14226
9068261f 14227 cu->checked_producer = true;
685b1105 14228}
ba919b58 14229
685b1105
JK
14230/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14231 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14232 during 4.6.0 experimental. */
14233
9068261f 14234static bool
685b1105
JK
14235producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14236{
14237 if (!cu->checked_producer)
14238 check_producer (cu);
14239
14240 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14241}
14242
c258c396
JD
14243
14244/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14245 with incorrect is_stmt attributes. */
14246
14247static bool
14248producer_is_codewarrior (struct dwarf2_cu *cu)
14249{
14250 if (!cu->checked_producer)
14251 check_producer (cu);
14252
14253 return cu->producer_is_codewarrior;
14254}
14255
bf23a268
TT
14256/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14257 If that attribute is not available, return the appropriate
14258 default. */
60d5a603
JK
14259
14260static enum dwarf_access_attribute
bf23a268 14261dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14262{
bf23a268
TT
14263 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14264 if (attr != nullptr)
14265 {
14266 LONGEST value = attr->constant_value (-1);
14267 if (value == DW_ACCESS_public
14268 || value == DW_ACCESS_protected
14269 || value == DW_ACCESS_private)
14270 return (dwarf_access_attribute) value;
14271 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14272 plongest (value));
14273 }
14274
60d5a603
JK
14275 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14276 {
14277 /* The default DWARF 2 accessibility for members is public, the default
14278 accessibility for inheritance is private. */
14279
14280 if (die->tag != DW_TAG_inheritance)
14281 return DW_ACCESS_public;
14282 else
14283 return DW_ACCESS_private;
14284 }
14285 else
14286 {
14287 /* DWARF 3+ defines the default accessibility a different way. The same
14288 rules apply now for DW_TAG_inheritance as for the members and it only
14289 depends on the container kind. */
14290
14291 if (die->parent->tag == DW_TAG_class_type)
14292 return DW_ACCESS_private;
14293 else
14294 return DW_ACCESS_public;
14295 }
14296}
14297
74ac6d43
TT
14298/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14299 offset. If the attribute was not found return 0, otherwise return
14300 1. If it was found but could not properly be handled, set *OFFSET
14301 to 0. */
14302
14303static int
14304handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14305 LONGEST *offset)
14306{
14307 struct attribute *attr;
14308
14309 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14310 if (attr != NULL)
14311 {
14312 *offset = 0;
14313
14314 /* Note that we do not check for a section offset first here.
14315 This is because DW_AT_data_member_location is new in DWARF 4,
14316 so if we see it, we can assume that a constant form is really
14317 a constant and not a section offset. */
cd6c91b4 14318 if (attr->form_is_constant ())
0826b30a 14319 *offset = attr->constant_value (0);
cd6c91b4 14320 else if (attr->form_is_section_offset ())
74ac6d43 14321 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14322 else if (attr->form_is_block ())
9d2246fc 14323 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14324 else
14325 dwarf2_complex_location_expr_complaint ();
14326
14327 return 1;
14328 }
14329
14330 return 0;
14331}
14332
7d79de9a
TT
14333/* Look for DW_AT_data_member_location and store the results in FIELD. */
14334
14335static void
14336handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14337 struct field *field)
14338{
14339 struct attribute *attr;
14340
14341 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14342 if (attr != NULL)
14343 {
14344 if (attr->form_is_constant ())
14345 {
14346 LONGEST offset = attr->constant_value (0);
14347 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14348 }
14349 else if (attr->form_is_section_offset ())
14350 dwarf2_complex_location_expr_complaint ();
14351 else if (attr->form_is_block ())
14352 {
14353 bool handled;
9d2246fc 14354 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14355 if (handled)
14356 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14357 else
14358 {
5e22e966
SM
14359 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14360 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14361 struct dwarf2_locexpr_baton *dlbaton
14362 = XOBNEW (&objfile->objfile_obstack,
14363 struct dwarf2_locexpr_baton);
9d2246fc
TT
14364 dlbaton->data = attr->as_block ()->data;
14365 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14366 /* When using this baton, we want to compute the address
14367 of the field, not the value. This is why
14368 is_reference is set to false here. */
14369 dlbaton->is_reference = false;
5e22e966 14370 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14371 dlbaton->per_cu = cu->per_cu;
14372
14373 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14374 }
14375 }
14376 else
14377 dwarf2_complex_location_expr_complaint ();
14378 }
14379}
14380
c906108c
SS
14381/* Add an aggregate field to the field list. */
14382
14383static void
107d2387 14384dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14385 struct dwarf2_cu *cu)
6e70227d 14386{
5e22e966 14387 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14388 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14389 struct nextfield *new_field;
14390 struct attribute *attr;
14391 struct field *fp;
15d034d0 14392 const char *fieldname = "";
c906108c 14393
7d0ccb61
DJ
14394 if (die->tag == DW_TAG_inheritance)
14395 {
be2daae6
TT
14396 fip->baseclasses.emplace_back ();
14397 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14398 }
14399 else
14400 {
be2daae6
TT
14401 fip->fields.emplace_back ();
14402 new_field = &fip->fields.back ();
7d0ccb61 14403 }
be2daae6 14404
9c6a1327
TT
14405 new_field->offset = die->sect_off;
14406
bf23a268 14407 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 14408 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 14409 fip->non_public_fields = true;
60d5a603 14410
e142c38c 14411 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14412 if (attr != nullptr)
23dca5c3 14413 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
14414 else
14415 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14416
14417 fp = &new_field->field;
a9a9bd0f 14418
e142c38c 14419 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14420 {
a9a9bd0f 14421 /* Data member other than a C++ static data member. */
6e70227d 14422
c906108c 14423 /* Get type of field. */
5d14b6e5 14424 fp->set_type (die_type (die, cu));
c906108c 14425
d6a843b5 14426 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14427
c906108c 14428 /* Get bit size of field (zero if none). */
e142c38c 14429 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14430 if (attr != nullptr)
c906108c 14431 {
529908cb 14432 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
14433 }
14434 else
14435 {
14436 FIELD_BITSIZE (*fp) = 0;
14437 }
14438
14439 /* Get bit offset of field. */
7d79de9a 14440 handle_data_member_location (die, cu, fp);
e142c38c 14441 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 14442 if (attr != nullptr && attr->form_is_constant ())
c906108c 14443 {
d5a22e77 14444 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14445 {
14446 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
14447 additional bit offset from the MSB of the containing
14448 anonymous object to the MSB of the field. We don't
14449 have to do anything special since we don't need to
14450 know the size of the anonymous object. */
529908cb 14451 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 14452 + attr->constant_value (0)));
c906108c
SS
14453 }
14454 else
14455 {
14456 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
14457 MSB of the anonymous object, subtract off the number of
14458 bits from the MSB of the field to the MSB of the
14459 object, and then subtract off the number of bits of
14460 the field itself. The result is the bit offset of
14461 the LSB of the field. */
c906108c 14462 int anonymous_size;
7c184d33 14463 int bit_offset = attr->constant_value (0);
c906108c 14464
e142c38c 14465 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 14466 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
14467 {
14468 /* The size of the anonymous object containing
14469 the bit field is explicit, so use the
14470 indicated size (in bytes). */
7c184d33 14471 anonymous_size = attr->constant_value (0);
c906108c
SS
14472 }
14473 else
14474 {
14475 /* The size of the anonymous object containing
14476 the bit field must be inferred from the type
14477 attribute of the data member containing the
14478 bit field. */
5d14b6e5 14479 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14480 }
f41f5e61
PA
14481 SET_FIELD_BITPOS (*fp,
14482 (FIELD_BITPOS (*fp)
14483 + anonymous_size * bits_per_byte
14484 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14485 }
14486 }
da5b30da
AA
14487 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14488 if (attr != NULL)
14489 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14490 + attr->constant_value (0)));
c906108c
SS
14491
14492 /* Get name of field. */
39cbfefa
DJ
14493 fieldname = dwarf2_name (die, cu);
14494 if (fieldname == NULL)
14495 fieldname = "";
d8151005
DJ
14496
14497 /* The name is already allocated along with this objfile, so we don't
14498 need to duplicate it for the type. */
14499 fp->name = fieldname;
c906108c
SS
14500
14501 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 14502 pointer or virtual base class pointer) to private. */
e142c38c 14503 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14504 {
d48cc9dd 14505 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 14506 new_field->accessibility = DW_ACCESS_private;
264fc0e2 14507 fip->non_public_fields = true;
c906108c
SS
14508 }
14509 }
a9a9bd0f 14510 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14511 {
a9a9bd0f
DC
14512 /* C++ static member. */
14513
14514 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14515 is a declaration, but all versions of G++ as of this writing
14516 (so through at least 3.2.1) incorrectly generate
14517 DW_TAG_variable tags. */
6e70227d 14518
ff355380 14519 const char *physname;
c906108c 14520
a9a9bd0f 14521 /* Get name of field. */
39cbfefa
DJ
14522 fieldname = dwarf2_name (die, cu);
14523 if (fieldname == NULL)
c906108c
SS
14524 return;
14525
254e6b9e 14526 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14527 if (attr
14528 /* Only create a symbol if this is an external value.
14529 new_symbol checks this and puts the value in the global symbol
14530 table, which we want. If it is not external, new_symbol
14531 will try to put the value in cu->list_in_scope which is wrong. */
14532 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14533 {
14534 /* A static const member, not much different than an enum as far as
14535 we're concerned, except that we can support more types. */
14536 new_symbol (die, NULL, cu);
14537 }
14538
2df3850c 14539 /* Get physical name. */
ff355380 14540 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14541
d8151005
DJ
14542 /* The name is already allocated along with this objfile, so we don't
14543 need to duplicate it for the type. */
14544 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 14545 fp->set_type (die_type (die, cu));
d8151005 14546 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14547 }
14548 else if (die->tag == DW_TAG_inheritance)
14549 {
74ac6d43 14550 /* C++ base class field. */
7d79de9a 14551 handle_data_member_location (die, cu, fp);
c906108c 14552 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
14553 fp->set_type (die_type (die, cu));
14554 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 14555 }
2ddeaf8a
TT
14556 else
14557 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14558}
14559
883fd55a
KS
14560/* Can the type given by DIE define another type? */
14561
14562static bool
14563type_can_define_types (const struct die_info *die)
14564{
14565 switch (die->tag)
14566 {
14567 case DW_TAG_typedef:
14568 case DW_TAG_class_type:
14569 case DW_TAG_structure_type:
14570 case DW_TAG_union_type:
14571 case DW_TAG_enumeration_type:
14572 return true;
14573
14574 default:
14575 return false;
14576 }
14577}
14578
14579/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14580
14581static void
883fd55a
KS
14582dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14583 struct dwarf2_cu *cu)
6e70227d 14584{
be2daae6
TT
14585 struct decl_field fp;
14586 memset (&fp, 0, sizeof (fp));
98751a41 14587
883fd55a 14588 gdb_assert (type_can_define_types (die));
98751a41 14589
883fd55a 14590 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14591 fp.name = dwarf2_name (die, cu);
14592 fp.type = read_type_die (die, cu);
98751a41 14593
c191a687 14594 /* Save accessibility. */
bf23a268 14595 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
14596 switch (accessibility)
14597 {
14598 case DW_ACCESS_public:
14599 /* The assumed value if neither private nor protected. */
14600 break;
14601 case DW_ACCESS_private:
be2daae6 14602 fp.is_private = 1;
c191a687
KS
14603 break;
14604 case DW_ACCESS_protected:
be2daae6 14605 fp.is_protected = 1;
c191a687 14606 break;
c191a687
KS
14607 }
14608
883fd55a 14609 if (die->tag == DW_TAG_typedef)
be2daae6 14610 fip->typedef_field_list.push_back (fp);
883fd55a 14611 else
be2daae6 14612 fip->nested_types_list.push_back (fp);
98751a41
JK
14613}
14614
9c6a1327
TT
14615/* A convenience typedef that's used when finding the discriminant
14616 field for a variant part. */
1b95cdb7
SM
14617typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14618 offset_map_type;
9c6a1327
TT
14619
14620/* Compute the discriminant range for a given variant. OBSTACK is
14621 where the results will be stored. VARIANT is the variant to
14622 process. IS_UNSIGNED indicates whether the discriminant is signed
14623 or unsigned. */
14624
14625static const gdb::array_view<discriminant_range>
14626convert_variant_range (struct obstack *obstack, const variant_field &variant,
14627 bool is_unsigned)
14628{
14629 std::vector<discriminant_range> ranges;
14630
14631 if (variant.default_branch)
14632 return {};
14633
14634 if (variant.discr_list_data == nullptr)
14635 {
14636 discriminant_range r
14637 = {variant.discriminant_value, variant.discriminant_value};
14638 ranges.push_back (r);
14639 }
14640 else
14641 {
14642 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14643 variant.discr_list_data->size);
14644 while (!data.empty ())
14645 {
14646 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14647 {
14648 complaint (_("invalid discriminant marker: %d"), data[0]);
14649 break;
14650 }
14651 bool is_range = data[0] == DW_DSC_range;
14652 data = data.slice (1);
14653
14654 ULONGEST low, high;
14655 unsigned int bytes_read;
14656
14657 if (data.empty ())
14658 {
14659 complaint (_("DW_AT_discr_list missing low value"));
14660 break;
14661 }
14662 if (is_unsigned)
14663 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14664 else
14665 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14666 &bytes_read);
14667 data = data.slice (bytes_read);
14668
14669 if (is_range)
14670 {
14671 if (data.empty ())
14672 {
14673 complaint (_("DW_AT_discr_list missing high value"));
14674 break;
14675 }
14676 if (is_unsigned)
14677 high = read_unsigned_leb128 (nullptr, data.data (),
14678 &bytes_read);
14679 else
14680 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14681 &bytes_read);
14682 data = data.slice (bytes_read);
14683 }
14684 else
14685 high = low;
14686
14687 ranges.push_back ({ low, high });
14688 }
14689 }
14690
14691 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14692 ranges.size ());
14693 std::copy (ranges.begin (), ranges.end (), result);
14694 return gdb::array_view<discriminant_range> (result, ranges.size ());
14695}
14696
14697static const gdb::array_view<variant_part> create_variant_parts
14698 (struct obstack *obstack,
14699 const offset_map_type &offset_map,
14700 struct field_info *fi,
14701 const std::vector<variant_part_builder> &variant_parts);
14702
14703/* Fill in a "struct variant" for a given variant field. RESULT is
14704 the variant to fill in. OBSTACK is where any needed allocations
14705 will be done. OFFSET_MAP holds the mapping from section offsets to
14706 fields for the type. FI describes the fields of the type we're
14707 processing. FIELD is the variant field we're converting. */
14708
14709static void
14710create_one_variant (variant &result, struct obstack *obstack,
14711 const offset_map_type &offset_map,
14712 struct field_info *fi, const variant_field &field)
14713{
14714 result.discriminants = convert_variant_range (obstack, field, false);
14715 result.first_field = field.first_field + fi->baseclasses.size ();
14716 result.last_field = field.last_field + fi->baseclasses.size ();
14717 result.parts = create_variant_parts (obstack, offset_map, fi,
14718 field.variant_parts);
14719}
14720
14721/* Fill in a "struct variant_part" for a given variant part. RESULT
14722 is the variant part to fill in. OBSTACK is where any needed
14723 allocations will be done. OFFSET_MAP holds the mapping from
14724 section offsets to fields for the type. FI describes the fields of
14725 the type we're processing. BUILDER is the variant part to be
14726 converted. */
14727
14728static void
14729create_one_variant_part (variant_part &result,
14730 struct obstack *obstack,
14731 const offset_map_type &offset_map,
14732 struct field_info *fi,
14733 const variant_part_builder &builder)
14734{
14735 auto iter = offset_map.find (builder.discriminant_offset);
14736 if (iter == offset_map.end ())
14737 {
14738 result.discriminant_index = -1;
14739 /* Doesn't matter. */
14740 result.is_unsigned = false;
14741 }
14742 else
14743 {
14744 result.discriminant_index = iter->second;
14745 result.is_unsigned
c6d940a9 14746 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
14747 }
14748
14749 size_t n = builder.variants.size ();
14750 variant *output = new (obstack) variant[n];
14751 for (size_t i = 0; i < n; ++i)
14752 create_one_variant (output[i], obstack, offset_map, fi,
14753 builder.variants[i]);
14754
14755 result.variants = gdb::array_view<variant> (output, n);
14756}
14757
14758/* Create a vector of variant parts that can be attached to a type.
14759 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14760 holds the mapping from section offsets to fields for the type. FI
14761 describes the fields of the type we're processing. VARIANT_PARTS
14762 is the vector to convert. */
14763
14764static const gdb::array_view<variant_part>
14765create_variant_parts (struct obstack *obstack,
14766 const offset_map_type &offset_map,
14767 struct field_info *fi,
14768 const std::vector<variant_part_builder> &variant_parts)
14769{
14770 if (variant_parts.empty ())
14771 return {};
14772
14773 size_t n = variant_parts.size ();
14774 variant_part *result = new (obstack) variant_part[n];
14775 for (size_t i = 0; i < n; ++i)
14776 create_one_variant_part (result[i], obstack, offset_map, fi,
14777 variant_parts[i]);
14778
14779 return gdb::array_view<variant_part> (result, n);
14780}
14781
14782/* Compute the variant part vector for FIP, attaching it to TYPE when
14783 done. */
14784
14785static void
14786add_variant_property (struct field_info *fip, struct type *type,
14787 struct dwarf2_cu *cu)
14788{
14789 /* Map section offsets of fields to their field index. Note the
14790 field index here does not take the number of baseclasses into
14791 account. */
14792 offset_map_type offset_map;
14793 for (int i = 0; i < fip->fields.size (); ++i)
14794 offset_map[fip->fields[i].offset] = i;
14795
5e22e966 14796 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14797 gdb::array_view<variant_part> parts
14798 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14799 fip->variant_parts);
14800
14801 struct dynamic_prop prop;
8c2e4e06
SM
14802 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14803 obstack_copy (&objfile->objfile_obstack, &parts,
14804 sizeof (parts)));
9c6a1327 14805
5c54719c 14806 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14807}
14808
c906108c
SS
14809/* Create the vector of fields, and attach it to the type. */
14810
14811static void
fba45db2 14812dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14813 struct dwarf2_cu *cu)
c906108c 14814{
317f7127 14815 int nfields = fip->nfields ();
c906108c
SS
14816
14817 /* Record the field count, allocate space for the array of fields,
14818 and create blank accessibility bitfields if necessary. */
5e33d5f4 14819 type->set_num_fields (nfields);
3cabb6b0
SM
14820 type->set_fields
14821 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14822
b4ba55a1 14823 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14824 {
14825 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14826
14827 TYPE_FIELD_PRIVATE_BITS (type) =
14828 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14829 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14830
14831 TYPE_FIELD_PROTECTED_BITS (type) =
14832 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14833 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14834
774b6a14
TT
14835 TYPE_FIELD_IGNORE_BITS (type) =
14836 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14837 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14838 }
14839
14840 /* If the type has baseclasses, allocate and clear a bit vector for
14841 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14842 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14843 {
be2daae6 14844 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14845 unsigned char *pointer;
c906108c
SS
14846
14847 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14848 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14849 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14850 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14851 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14852 }
14853
9c6a1327
TT
14854 if (!fip->variant_parts.empty ())
14855 add_variant_property (fip, type, cu);
2ddeaf8a 14856
be2daae6
TT
14857 /* Copy the saved-up fields into the field vector. */
14858 for (int i = 0; i < nfields; ++i)
c906108c 14859 {
be2daae6
TT
14860 struct nextfield &field
14861 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14862 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14863
ceacbf6e 14864 type->field (i) = field.field;
be2daae6 14865 switch (field.accessibility)
c906108c 14866 {
c5aa993b 14867 case DW_ACCESS_private:
b4ba55a1 14868 if (cu->language != language_ada)
be2daae6 14869 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14870 break;
c906108c 14871
c5aa993b 14872 case DW_ACCESS_protected:
b4ba55a1 14873 if (cu->language != language_ada)
be2daae6 14874 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14875 break;
c906108c 14876
c5aa993b
JM
14877 case DW_ACCESS_public:
14878 break;
c906108c 14879
c5aa993b
JM
14880 default:
14881 /* Unknown accessibility. Complain and treat it as public. */
14882 {
b98664d3 14883 complaint (_("unsupported accessibility %d"),
be2daae6 14884 field.accessibility);
c5aa993b
JM
14885 }
14886 break;
c906108c 14887 }
be2daae6 14888 if (i < fip->baseclasses.size ())
c906108c 14889 {
be2daae6 14890 switch (field.virtuality)
c906108c 14891 {
c5aa993b
JM
14892 case DW_VIRTUALITY_virtual:
14893 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14894 if (cu->language == language_ada)
a73c6dcd 14895 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14896 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14897 break;
c906108c
SS
14898 }
14899 }
c906108c
SS
14900 }
14901}
14902
7d27a96d
TT
14903/* Return true if this member function is a constructor, false
14904 otherwise. */
14905
14906static int
14907dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14908{
14909 const char *fieldname;
fe978cb0 14910 const char *type_name;
7d27a96d
TT
14911 int len;
14912
14913 if (die->parent == NULL)
14914 return 0;
14915
14916 if (die->parent->tag != DW_TAG_structure_type
14917 && die->parent->tag != DW_TAG_union_type
14918 && die->parent->tag != DW_TAG_class_type)
14919 return 0;
14920
14921 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14922 type_name = dwarf2_name (die->parent, cu);
14923 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14924 return 0;
14925
14926 len = strlen (fieldname);
fe978cb0
PA
14927 return (strncmp (fieldname, type_name, len) == 0
14928 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14929}
14930
c906108c
SS
14931/* Add a member function to the proper fieldlist. */
14932
14933static void
107d2387 14934dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14935 struct type *type, struct dwarf2_cu *cu)
c906108c 14936{
5e22e966 14937 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 14938 struct attribute *attr;
c906108c 14939 int i;
be2daae6 14940 struct fnfieldlist *flp = nullptr;
c906108c 14941 struct fn_field *fnp;
15d034d0 14942 const char *fieldname;
f792889a 14943 struct type *this_type;
c906108c 14944
b4ba55a1 14945 if (cu->language == language_ada)
a73c6dcd 14946 error (_("unexpected member function in Ada type"));
b4ba55a1 14947
2df3850c 14948 /* Get name of member function. */
39cbfefa
DJ
14949 fieldname = dwarf2_name (die, cu);
14950 if (fieldname == NULL)
2df3850c 14951 return;
c906108c 14952
c906108c 14953 /* Look up member function name in fieldlist. */
be2daae6 14954 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14955 {
27bfe10e 14956 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14957 {
14958 flp = &fip->fnfieldlists[i];
14959 break;
14960 }
c906108c
SS
14961 }
14962
be2daae6
TT
14963 /* Create a new fnfieldlist if necessary. */
14964 if (flp == nullptr)
c906108c 14965 {
be2daae6
TT
14966 fip->fnfieldlists.emplace_back ();
14967 flp = &fip->fnfieldlists.back ();
c906108c 14968 flp->name = fieldname;
be2daae6 14969 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14970 }
14971
be2daae6
TT
14972 /* Create a new member function field and add it to the vector of
14973 fnfieldlists. */
14974 flp->fnfields.emplace_back ();
14975 fnp = &flp->fnfields.back ();
3da10d80
KS
14976
14977 /* Delay processing of the physname until later. */
9c37b5ae 14978 if (cu->language == language_cplus)
be2daae6
TT
14979 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14980 die, cu);
3da10d80
KS
14981 else
14982 {
1d06ead6 14983 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14984 fnp->physname = physname ? physname : "";
14985 }
14986
c906108c 14987 fnp->type = alloc_type (objfile);
f792889a 14988 this_type = read_type_die (die, cu);
78134374 14989 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 14990 {
1f704f76 14991 int nparams = this_type->num_fields ();
c906108c 14992
f792889a 14993 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14994 of the method itself (TYPE_CODE_METHOD). */
14995 smash_to_method_type (fnp->type, type,
f792889a 14996 TYPE_TARGET_TYPE (this_type),
80fc5e77 14997 this_type->fields (),
1f704f76 14998 this_type->num_fields (),
a409645d 14999 this_type->has_varargs ());
c906108c
SS
15000
15001 /* Handle static member functions.
dda83cd7
SM
15002 Dwarf2 has no clean way to discern C++ static and non-static
15003 member functions. G++ helps GDB by marking the first
15004 parameter for non-static member functions (which is the this
15005 pointer) as artificial. We obtain this information from
15006 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15007 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15008 fnp->voffset = VOFFSET_STATIC;
15009 }
15010 else
b98664d3 15011 complaint (_("member function type missing for '%s'"),
3da10d80 15012 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15013
15014 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15015 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15016 fnp->fcontext = die_containing_type (die, cu);
c906108c 15017
3e43a32a
MS
15018 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15019 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15020
15021 /* Get accessibility. */
bf23a268 15022 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15023 switch (accessibility)
c906108c 15024 {
60d5a603
JK
15025 case DW_ACCESS_private:
15026 fnp->is_private = 1;
15027 break;
15028 case DW_ACCESS_protected:
15029 fnp->is_protected = 1;
15030 break;
c906108c
SS
15031 }
15032
b02dede2 15033 /* Check for artificial methods. */
e142c38c 15034 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15035 if (attr && attr->as_boolean ())
b02dede2
DJ
15036 fnp->is_artificial = 1;
15037
e35000a7
TBA
15038 /* Check for defaulted methods. */
15039 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15040 if (attr != nullptr)
15041 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15042
15043 /* Check for deleted methods. */
15044 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15045 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15046 fnp->is_deleted = 1;
15047
7d27a96d
TT
15048 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15049
0d564a31 15050 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15051 function. For older versions of GCC, this is an offset in the
15052 appropriate virtual table, as specified by DW_AT_containing_type.
15053 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15054 to the object address. */
15055
e142c38c 15056 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15057 if (attr != nullptr)
8e19ed76 15058 {
9d2246fc 15059 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15060 {
9d2246fc
TT
15061 struct dwarf_block *block = attr->as_block ();
15062
15063 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15064 {
15065 /* Old-style GCC. */
9d2246fc 15066 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15067 }
9d2246fc
TT
15068 else if (block->data[0] == DW_OP_deref
15069 || (block->size > 1
15070 && block->data[0] == DW_OP_deref_size
15071 && block->data[1] == cu->header.addr_size))
aec5aa8b 15072 {
9d2246fc 15073 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15074 if ((fnp->voffset % cu->header.addr_size) != 0)
15075 dwarf2_complex_location_expr_complaint ();
15076 else
15077 fnp->voffset /= cu->header.addr_size;
15078 fnp->voffset += 2;
15079 }
15080 else
15081 dwarf2_complex_location_expr_complaint ();
15082
15083 if (!fnp->fcontext)
7e993ebf
KS
15084 {
15085 /* If there is no `this' field and no DW_AT_containing_type,
15086 we cannot actually find a base class context for the
15087 vtable! */
1f704f76 15088 if (this_type->num_fields () == 0
7e993ebf
KS
15089 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15090 {
b98664d3 15091 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15092 "function \"%s\" (offset %s)"),
15093 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15094 }
15095 else
15096 {
15097 fnp->fcontext
940da03e 15098 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15099 }
15100 }
aec5aa8b 15101 }
cd6c91b4 15102 else if (attr->form_is_section_offset ())
dda83cd7 15103 {
4d3c2250 15104 dwarf2_complex_location_expr_complaint ();
dda83cd7 15105 }
8e19ed76 15106 else
dda83cd7 15107 {
4d3c2250
KB
15108 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15109 fieldname);
dda83cd7 15110 }
0d564a31 15111 }
d48cc9dd
DJ
15112 else
15113 {
15114 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15115 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15116 {
15117 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15118 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15119 "but the vtable offset is not specified"),
9d8780f0 15120 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15121 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15122 TYPE_CPLUS_DYNAMIC (type) = 1;
15123 }
15124 }
c906108c
SS
15125}
15126
15127/* Create the vector of member function fields, and attach it to the type. */
15128
15129static void
fba45db2 15130dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15131 struct dwarf2_cu *cu)
c906108c 15132{
b4ba55a1 15133 if (cu->language == language_ada)
a73c6dcd 15134 error (_("unexpected member functions in Ada type"));
b4ba55a1 15135
c906108c
SS
15136 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15137 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15138 TYPE_ALLOC (type,
15139 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15140
be2daae6 15141 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15142 {
be2daae6 15143 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15144 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15145
be2daae6
TT
15146 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15147 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15148 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15149 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15150
15151 for (int k = 0; k < nf.fnfields.size (); ++k)
15152 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15153 }
15154
be2daae6 15155 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15156}
15157
1168df01
JB
15158/* Returns non-zero if NAME is the name of a vtable member in CU's
15159 language, zero otherwise. */
15160static int
15161is_vtable_name (const char *name, struct dwarf2_cu *cu)
15162{
15163 static const char vptr[] = "_vptr";
15164
9c37b5ae
TT
15165 /* Look for the C++ form of the vtable. */
15166 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15167 return 1;
15168
15169 return 0;
15170}
15171
c0dd20ea 15172/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15173 functions, with the ABI-specified layout. If TYPE describes
15174 such a structure, smash it into a member function type.
61049d3b
DJ
15175
15176 GCC shouldn't do this; it should just output pointer to member DIEs.
15177 This is GCC PR debug/28767. */
c0dd20ea 15178
0b92b5bb
TT
15179static void
15180quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15181{
09e2d7c7 15182 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15183
15184 /* Check for a structure with no name and two children. */
1f704f76 15185 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15186 return;
c0dd20ea
DJ
15187
15188 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15189 if (TYPE_FIELD_NAME (type, 0) == NULL
15190 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15191 || TYPE_FIELD_NAME (type, 1) == NULL
15192 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15193 return;
c0dd20ea
DJ
15194
15195 /* Find the type of the method. */
940da03e 15196 pfn_type = type->field (0).type ();
c0dd20ea 15197 if (pfn_type == NULL
78134374
SM
15198 || pfn_type->code () != TYPE_CODE_PTR
15199 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15200 return;
c0dd20ea
DJ
15201
15202 /* Look for the "this" argument. */
15203 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15204 if (pfn_type->num_fields () == 0
940da03e
SM
15205 /* || pfn_type->field (0).type () == NULL */
15206 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15207 return;
c0dd20ea 15208
940da03e 15209 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15210 new_type = alloc_type (objfile);
09e2d7c7 15211 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15212 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15213 pfn_type->has_varargs ());
0b92b5bb 15214 smash_to_methodptr_type (type, new_type);
c0dd20ea 15215}
1168df01 15216
e26624c6
TT
15217/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15218 requires rewriting, then copy it and return the updated copy.
15219 Otherwise return nullptr. */
15220
15221static struct type *
15222rewrite_array_type (struct type *type)
15223{
15224 if (type->code () != TYPE_CODE_ARRAY)
15225 return nullptr;
15226
15227 struct type *index_type = type->index_type ();
15228 range_bounds *current_bounds = index_type->bounds ();
15229
15230 /* Handle multi-dimensional arrays. */
15231 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15232 if (new_target == nullptr)
15233 {
15234 /* Maybe we don't need to rewrite this array. */
15235 if (current_bounds->low.kind () == PROP_CONST
15236 && current_bounds->high.kind () == PROP_CONST)
15237 return nullptr;
15238 }
15239
15240 /* Either the target type was rewritten, or the bounds have to be
15241 updated. Either way we want to copy the type and update
15242 everything. */
15243 struct type *copy = copy_type (type);
15244 int nfields = copy->num_fields ();
15245 field *new_fields
15246 = ((struct field *) TYPE_ZALLOC (copy,
15247 nfields * sizeof (struct field)));
15248 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15249 copy->set_fields (new_fields);
15250 if (new_target != nullptr)
15251 TYPE_TARGET_TYPE (copy) = new_target;
15252
15253 struct type *index_copy = copy_type (index_type);
15254 range_bounds *bounds
15255 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15256 sizeof (range_bounds));
15257 *bounds = *current_bounds;
15258 bounds->low.set_const_val (1);
15259 bounds->high.set_const_val (0);
15260 index_copy->set_bounds (bounds);
15261 copy->set_index_type (index_copy);
15262
15263 return copy;
15264}
15265
57567375
TT
15266/* While some versions of GCC will generate complicated DWARF for an
15267 array (see quirk_ada_thick_pointer), more recent versions were
15268 modified to emit an explicit thick pointer structure. However, in
15269 this case, the array still has DWARF expressions for its ranges,
15270 and these must be ignored. */
15271
15272static void
15273quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15274 struct type *type)
15275{
15276 gdb_assert (cu->language == language_ada);
15277
15278 /* Check for a structure with two children. */
15279 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15280 return;
15281
15282 /* Check for P_ARRAY and P_BOUNDS members. */
15283 if (TYPE_FIELD_NAME (type, 0) == NULL
15284 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15285 || TYPE_FIELD_NAME (type, 1) == NULL
15286 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15287 return;
15288
15289 /* Make sure we're looking at a pointer to an array. */
15290 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15291 return;
57567375 15292
e26624c6
TT
15293 /* The Ada code already knows how to handle these types, so all that
15294 we need to do is turn the bounds into static bounds. However, we
15295 don't want to rewrite existing array or index types in-place,
15296 because those may be referenced in other contexts where this
15297 rewriting is undesirable. */
15298 struct type *new_ary_type
15299 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15300 if (new_ary_type != nullptr)
15301 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15302}
15303
2b4424c3
TT
15304/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15305 appropriate error checking and issuing complaints if there is a
15306 problem. */
15307
15308static ULONGEST
15309get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15310{
15311 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15312
15313 if (attr == nullptr)
15314 return 0;
15315
cd6c91b4 15316 if (!attr->form_is_constant ())
2b4424c3 15317 {
b98664d3 15318 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15319 " - DIE at %s [in module %s]"),
15320 sect_offset_str (die->sect_off),
5e22e966 15321 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15322 return 0;
15323 }
15324
529908cb
TT
15325 LONGEST val = attr->constant_value (0);
15326 if (val < 0)
2b4424c3 15327 {
529908cb
TT
15328 complaint (_("DW_AT_alignment value must not be negative"
15329 " - DIE at %s [in module %s]"),
15330 sect_offset_str (die->sect_off),
15331 objfile_name (cu->per_objfile->objfile));
15332 return 0;
2b4424c3 15333 }
529908cb 15334 ULONGEST align = val;
2b4424c3
TT
15335
15336 if (align == 0)
15337 {
b98664d3 15338 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15339 " - DIE at %s [in module %s]"),
15340 sect_offset_str (die->sect_off),
5e22e966 15341 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15342 return 0;
15343 }
15344 if ((align & (align - 1)) != 0)
15345 {
b98664d3 15346 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15347 " - DIE at %s [in module %s]"),
15348 sect_offset_str (die->sect_off),
5e22e966 15349 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15350 return 0;
15351 }
15352
15353 return align;
15354}
15355
15356/* If the DIE has a DW_AT_alignment attribute, use its value to set
15357 the alignment for TYPE. */
15358
15359static void
15360maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15361 struct type *type)
15362{
15363 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15364 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15365 " - DIE at %s [in module %s]"),
15366 sect_offset_str (die->sect_off),
5e22e966 15367 objfile_name (cu->per_objfile->objfile));
2b4424c3 15368}
685b1105 15369
e35000a7
TBA
15370/* Check if the given VALUE is a valid enum dwarf_calling_convention
15371 constant for a type, according to DWARF5 spec, Table 5.5. */
15372
15373static bool
15374is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15375{
15376 switch (value)
15377 {
15378 case DW_CC_normal:
15379 case DW_CC_pass_by_reference:
15380 case DW_CC_pass_by_value:
15381 return true;
15382
15383 default:
15384 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15385 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15386 return false;
15387 }
15388}
15389
d0922fcf
TBA
15390/* Check if the given VALUE is a valid enum dwarf_calling_convention
15391 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15392 also according to GNU-specific values (see include/dwarf2.h). */
15393
15394static bool
15395is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15396{
15397 switch (value)
15398 {
15399 case DW_CC_normal:
15400 case DW_CC_program:
15401 case DW_CC_nocall:
15402 return true;
15403
15404 case DW_CC_GNU_renesas_sh:
15405 case DW_CC_GNU_borland_fastcall_i386:
15406 case DW_CC_GDB_IBM_OpenCL:
15407 return true;
15408
15409 default:
15410 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15411 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15412 return false;
15413 }
15414}
15415
c906108c 15416/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15417 (definition) to create a type for the structure or union. Fill in
15418 the type's name and general properties; the members will not be
83655187
DE
15419 processed until process_structure_scope. A symbol table entry for
15420 the type will also not be done until process_structure_scope (assuming
15421 the type has a name).
c906108c 15422
c767944b
DJ
15423 NOTE: we need to call these functions regardless of whether or not the
15424 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15425 structure or union. This gets the type entered into our set of
83655187 15426 user defined types. */
c906108c 15427
f792889a 15428static struct type *
134d01f1 15429read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15430{
5e22e966 15431 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15432 struct type *type;
15433 struct attribute *attr;
15d034d0 15434 const char *name;
c906108c 15435
348e048f
DE
15436 /* If the definition of this type lives in .debug_types, read that type.
15437 Don't follow DW_AT_specification though, that will take us back up
15438 the chain and we want to go down. */
052c8bb8 15439 attr = die->attr (DW_AT_signature);
435d3d88 15440 if (attr != nullptr)
348e048f 15441 {
ac9ec31b 15442 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15443
ac9ec31b 15444 /* The type's CU may not be the same as CU.
02142a6c 15445 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15446 return set_die_type (die, type, cu);
15447 }
15448
c0dd20ea 15449 type = alloc_type (objfile);
c906108c 15450 INIT_CPLUS_SPECIFIC (type);
93311388 15451
39cbfefa
DJ
15452 name = dwarf2_name (die, cu);
15453 if (name != NULL)
c906108c 15454 {
987504bb 15455 if (cu->language == language_cplus
c44af4eb
TT
15456 || cu->language == language_d
15457 || cu->language == language_rust)
63d06c5c 15458 {
15d034d0 15459 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15460
15461 /* dwarf2_full_name might have already finished building the DIE's
15462 type. If so, there is no need to continue. */
15463 if (get_die_type (die, cu) != NULL)
15464 return get_die_type (die, cu);
15465
d0e39ea2 15466 type->set_name (full_name);
63d06c5c
DC
15467 }
15468 else
15469 {
d8151005
DJ
15470 /* The name is already allocated along with this objfile, so
15471 we don't need to duplicate it for the type. */
d0e39ea2 15472 type->set_name (name);
63d06c5c 15473 }
c906108c
SS
15474 }
15475
15476 if (die->tag == DW_TAG_structure_type)
15477 {
67607e24 15478 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15479 }
15480 else if (die->tag == DW_TAG_union_type)
15481 {
67607e24 15482 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15483 }
15484 else
15485 {
67607e24 15486 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15487 }
15488
0cc2414c 15489 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
aa70e35c 15490 type->set_is_declared_class (true);
0cc2414c 15491
e35000a7
TBA
15492 /* Store the calling convention in the type if it's available in
15493 the die. Otherwise the calling convention remains set to
15494 the default value DW_CC_normal. */
15495 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15496 if (attr != nullptr
529908cb 15497 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
15498 {
15499 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15500 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 15501 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
15502 }
15503
e142c38c 15504 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15505 if (attr != nullptr)
c906108c 15506 {
cd6c91b4 15507 if (attr->form_is_constant ())
dda83cd7 15508 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
15509 else
15510 {
f8e89861 15511 struct dynamic_prop prop;
293e7e51 15512 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15513 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 15514 TYPE_LENGTH (type) = 0;
155bfbd3 15515 }
c906108c
SS
15516 }
15517 else
15518 {
15519 TYPE_LENGTH (type) = 0;
15520 }
15521
2b4424c3
TT
15522 maybe_set_alignment (cu, die, type);
15523
5230b05a 15524 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15525 {
5230b05a
WT
15526 /* ICC<14 does not output the required DW_AT_declaration on
15527 incomplete types, but gives them a size of zero. */
b4b73759 15528 type->set_is_stub (true);
685b1105
JK
15529 }
15530 else
9baccff6 15531 type->set_stub_is_supported (true);
685b1105 15532
dc718098 15533 if (die_is_declaration (die, cu))
b4b73759 15534 type->set_is_stub (true);
a6c727b2
DJ
15535 else if (attr == NULL && die->child == NULL
15536 && producer_is_realview (cu->producer))
15537 /* RealView does not output the required DW_AT_declaration
15538 on incomplete types. */
b4b73759 15539 type->set_is_stub (true);
dc718098 15540
c906108c
SS
15541 /* We need to add the type field to the die immediately so we don't
15542 infinitely recurse when dealing with pointers to the structure
0963b4bd 15543 type within the structure itself. */
1c379e20 15544 set_die_type (die, type, cu);
c906108c 15545
7e314c57
JK
15546 /* set_die_type should be already done. */
15547 set_descriptive_type (type, die, cu);
15548
c767944b
DJ
15549 return type;
15550}
15551
9c6a1327
TT
15552static void handle_struct_member_die
15553 (struct die_info *child_die,
15554 struct type *type,
15555 struct field_info *fi,
15556 std::vector<struct symbol *> *template_args,
15557 struct dwarf2_cu *cu);
15558
15559/* A helper for handle_struct_member_die that handles
15560 DW_TAG_variant_part. */
15561
15562static void
15563handle_variant_part (struct die_info *die, struct type *type,
15564 struct field_info *fi,
15565 std::vector<struct symbol *> *template_args,
15566 struct dwarf2_cu *cu)
15567{
15568 variant_part_builder *new_part;
15569 if (fi->current_variant_part == nullptr)
15570 {
15571 fi->variant_parts.emplace_back ();
15572 new_part = &fi->variant_parts.back ();
15573 }
15574 else if (!fi->current_variant_part->processing_variant)
15575 {
15576 complaint (_("nested DW_TAG_variant_part seen "
15577 "- DIE at %s [in module %s]"),
15578 sect_offset_str (die->sect_off),
5e22e966 15579 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15580 return;
15581 }
15582 else
15583 {
15584 variant_field &current = fi->current_variant_part->variants.back ();
15585 current.variant_parts.emplace_back ();
15586 new_part = &current.variant_parts.back ();
15587 }
15588
15589 /* When we recurse, we want callees to add to this new variant
15590 part. */
15591 scoped_restore save_current_variant_part
15592 = make_scoped_restore (&fi->current_variant_part, new_part);
15593
15594 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15595 if (discr == NULL)
15596 {
15597 /* It's a univariant form, an extension we support. */
15598 }
15599 else if (discr->form_is_ref ())
15600 {
15601 struct dwarf2_cu *target_cu = cu;
15602 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15603
15604 new_part->discriminant_offset = target_die->sect_off;
15605 }
15606 else
15607 {
15608 complaint (_("DW_AT_discr does not have DIE reference form"
15609 " - DIE at %s [in module %s]"),
15610 sect_offset_str (die->sect_off),
5e22e966 15611 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15612 }
15613
15614 for (die_info *child_die = die->child;
15615 child_die != NULL;
15616 child_die = child_die->sibling)
15617 handle_struct_member_die (child_die, type, fi, template_args, cu);
15618}
15619
15620/* A helper for handle_struct_member_die that handles
15621 DW_TAG_variant. */
15622
15623static void
15624handle_variant (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 if (fi->current_variant_part == nullptr)
15630 {
15631 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15632 "- DIE at %s [in module %s]"),
15633 sect_offset_str (die->sect_off),
5e22e966 15634 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15635 return;
15636 }
15637 if (fi->current_variant_part->processing_variant)
15638 {
15639 complaint (_("nested DW_TAG_variant seen "
15640 "- DIE at %s [in module %s]"),
15641 sect_offset_str (die->sect_off),
5e22e966 15642 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15643 return;
15644 }
15645
15646 scoped_restore save_processing_variant
15647 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15648 true);
15649
15650 fi->current_variant_part->variants.emplace_back ();
15651 variant_field &variant = fi->current_variant_part->variants.back ();
15652 variant.first_field = fi->fields.size ();
15653
15654 /* In a variant we want to get the discriminant and also add a
15655 field for our sole member child. */
15656 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 15657 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
15658 {
15659 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 15660 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
15661 variant.default_branch = true;
15662 else
9d2246fc 15663 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
15664 }
15665 else
cae21f8e 15666 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
15667
15668 for (die_info *variant_child = die->child;
15669 variant_child != NULL;
15670 variant_child = variant_child->sibling)
15671 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15672
15673 variant.last_field = fi->fields.size ();
15674}
15675
2ddeaf8a
TT
15676/* A helper for process_structure_scope that handles a single member
15677 DIE. */
15678
15679static void
15680handle_struct_member_die (struct die_info *child_die, struct type *type,
15681 struct field_info *fi,
15682 std::vector<struct symbol *> *template_args,
15683 struct dwarf2_cu *cu)
15684{
15685 if (child_die->tag == DW_TAG_member
9c6a1327 15686 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15687 {
15688 /* NOTE: carlton/2002-11-05: A C++ static data member
15689 should be a DW_TAG_member that is a declaration, but
15690 all versions of G++ as of this writing (so through at
15691 least 3.2.1) incorrectly generate DW_TAG_variable
15692 tags for them instead. */
15693 dwarf2_add_field (fi, child_die, cu);
15694 }
15695 else if (child_die->tag == DW_TAG_subprogram)
15696 {
15697 /* Rust doesn't have member functions in the C++ sense.
15698 However, it does emit ordinary functions as children
15699 of a struct DIE. */
15700 if (cu->language == language_rust)
15701 read_func_scope (child_die, cu);
15702 else
15703 {
15704 /* C++ member function. */
15705 dwarf2_add_member_fn (fi, child_die, type, cu);
15706 }
15707 }
15708 else if (child_die->tag == DW_TAG_inheritance)
15709 {
15710 /* C++ base class field. */
15711 dwarf2_add_field (fi, child_die, cu);
15712 }
15713 else if (type_can_define_types (child_die))
15714 dwarf2_add_type_defn (fi, child_die, cu);
15715 else if (child_die->tag == DW_TAG_template_type_param
15716 || child_die->tag == DW_TAG_template_value_param)
15717 {
15718 struct symbol *arg = new_symbol (child_die, NULL, cu);
15719
15720 if (arg != NULL)
15721 template_args->push_back (arg);
15722 }
9c6a1327
TT
15723 else if (child_die->tag == DW_TAG_variant_part)
15724 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15725 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15726 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15727}
15728
c767944b
DJ
15729/* Finish creating a structure or union type, including filling in
15730 its members and creating a symbol for it. */
15731
15732static void
15733process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15734{
5e22e966 15735 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15736 struct die_info *child_die;
c767944b
DJ
15737 struct type *type;
15738
15739 type = get_die_type (die, cu);
15740 if (type == NULL)
15741 type = read_structure_type (die, cu);
15742
3e1d3d8c 15743 bool has_template_parameters = false;
e142c38c 15744 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15745 {
15746 struct field_info fi;
2f4732b0 15747 std::vector<struct symbol *> template_args;
c906108c 15748
639d11d3 15749 child_die = die->child;
c906108c
SS
15750
15751 while (child_die && child_die->tag)
15752 {
2ddeaf8a 15753 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15754 child_die = child_die->sibling;
c906108c
SS
15755 }
15756
34eaf542 15757 /* Attach template arguments to type. */
2f4732b0 15758 if (!template_args.empty ())
34eaf542 15759 {
3e1d3d8c 15760 has_template_parameters = true;
34eaf542 15761 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15762 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15763 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15764 = XOBNEWVEC (&objfile->objfile_obstack,
15765 struct symbol *,
15766 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15767 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15768 template_args.data (),
34eaf542
TT
15769 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15770 * sizeof (struct symbol *)));
34eaf542
TT
15771 }
15772
c906108c 15773 /* Attach fields and member functions to the type. */
317f7127 15774 if (fi.nfields () > 0)
e7c27a73 15775 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15776 if (!fi.fnfieldlists.empty ())
c906108c 15777 {
e7c27a73 15778 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15779
c5aa993b 15780 /* Get the type which refers to the base class (possibly this
c906108c 15781 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15782 class from the DW_AT_containing_type attribute. This use of
15783 DW_AT_containing_type is a GNU extension. */
c906108c 15784
e142c38c 15785 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15786 {
e7c27a73 15787 struct type *t = die_containing_type (die, cu);
c906108c 15788
ae6ae975 15789 set_type_vptr_basetype (type, t);
c906108c
SS
15790 if (type == t)
15791 {
c906108c
SS
15792 int i;
15793
15794 /* Our own class provides vtbl ptr. */
1f704f76 15795 for (i = t->num_fields () - 1;
c906108c
SS
15796 i >= TYPE_N_BASECLASSES (t);
15797 --i)
15798 {
0d5cff50 15799 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15800
dda83cd7 15801 if (is_vtable_name (fieldname, cu))
c906108c 15802 {
ae6ae975 15803 set_type_vptr_fieldno (type, i);
c906108c
SS
15804 break;
15805 }
15806 }
15807
15808 /* Complain if virtual function table field not found. */
15809 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15810 complaint (_("virtual function table pointer "
3e43a32a 15811 "not found when defining class '%s'"),
7d93a1e0 15812 type->name () ? type->name () : "");
c906108c
SS
15813 }
15814 else
15815 {
ae6ae975 15816 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15817 }
15818 }
f6235d4c 15819 else if (cu->producer
61012eef 15820 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15821 {
15822 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
15823 of the containing type, but the vtable pointer is
15824 always named __vfp. */
f6235d4c
EZ
15825
15826 int i;
15827
1f704f76 15828 for (i = type->num_fields () - 1;
f6235d4c
EZ
15829 i >= TYPE_N_BASECLASSES (type);
15830 --i)
15831 {
15832 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15833 {
ae6ae975
DE
15834 set_type_vptr_fieldno (type, i);
15835 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15836 break;
15837 }
15838 }
15839 }
c906108c 15840 }
98751a41
JK
15841
15842 /* Copy fi.typedef_field_list linked list elements content into the
15843 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15844 if (!fi.typedef_field_list.empty ())
98751a41 15845 {
be2daae6 15846 int count = fi.typedef_field_list.size ();
98751a41 15847
a0d7a4ff 15848 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15849 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15850 = ((struct decl_field *)
be2daae6
TT
15851 TYPE_ALLOC (type,
15852 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15853 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15854
be2daae6
TT
15855 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15856 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15857 }
c767944b 15858
883fd55a
KS
15859 /* Copy fi.nested_types_list linked list elements content into the
15860 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15861 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15862 {
be2daae6 15863 int count = fi.nested_types_list.size ();
883fd55a
KS
15864
15865 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15866 TYPE_NESTED_TYPES_ARRAY (type)
15867 = ((struct decl_field *)
be2daae6
TT
15868 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15869 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15870
be2daae6
TT
15871 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15872 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15873 }
c906108c 15874 }
63d06c5c 15875
bb5ed363 15876 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15877 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15878 cu->rust_unions.push_back (type);
57567375
TT
15879 else if (cu->language == language_ada)
15880 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 15881
90aeadfc
DC
15882 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15883 snapshots) has been known to create a die giving a declaration
15884 for a class that has, as a child, a die giving a definition for a
15885 nested class. So we have to process our children even if the
15886 current die is a declaration. Normally, of course, a declaration
15887 won't have any children at all. */
134d01f1 15888
ca040673
DE
15889 child_die = die->child;
15890
90aeadfc
DC
15891 while (child_die != NULL && child_die->tag)
15892 {
15893 if (child_die->tag == DW_TAG_member
15894 || child_die->tag == DW_TAG_variable
34eaf542
TT
15895 || child_die->tag == DW_TAG_inheritance
15896 || child_die->tag == DW_TAG_template_value_param
15897 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15898 {
90aeadfc 15899 /* Do nothing. */
134d01f1 15900 }
90aeadfc
DC
15901 else
15902 process_die (child_die, cu);
134d01f1 15903
436c571c 15904 child_die = child_die->sibling;
134d01f1
DJ
15905 }
15906
fa4028e9
JB
15907 /* Do not consider external references. According to the DWARF standard,
15908 these DIEs are identified by the fact that they have no byte_size
15909 attribute, and a declaration attribute. */
15910 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15911 || !die_is_declaration (die, cu)
15912 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15913 {
15914 struct symbol *sym = new_symbol (die, type, cu);
15915
15916 if (has_template_parameters)
15917 {
a776957c
TT
15918 struct symtab *symtab;
15919 if (sym != nullptr)
15920 symtab = symbol_symtab (sym);
15921 else if (cu->line_header != nullptr)
15922 {
15923 /* Any related symtab will do. */
15924 symtab
7ba99d21 15925 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15926 }
15927 else
15928 {
15929 symtab = nullptr;
15930 complaint (_("could not find suitable "
15931 "symtab for template parameter"
15932 " - DIE at %s [in module %s]"),
15933 sect_offset_str (die->sect_off),
15934 objfile_name (objfile));
15935 }
15936
15937 if (symtab != nullptr)
15938 {
15939 /* Make sure that the symtab is set on the new symbols.
15940 Even though they don't appear in this symtab directly,
15941 other parts of gdb assume that symbols do, and this is
15942 reasonably true. */
15943 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15944 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15945 }
3e1d3d8c
TT
15946 }
15947 }
134d01f1
DJ
15948}
15949
ed6acedd
TT
15950/* Assuming DIE is an enumeration type, and TYPE is its associated
15951 type, update TYPE using some information only available in DIE's
15952 children. In particular, the fields are computed. */
55426c9d
JB
15953
15954static void
15955update_enumeration_type_from_children (struct die_info *die,
15956 struct type *type,
15957 struct dwarf2_cu *cu)
15958{
60f7655a 15959 struct die_info *child_die;
55426c9d
JB
15960 int unsigned_enum = 1;
15961 int flag_enum = 1;
55426c9d 15962
8268c778 15963 auto_obstack obstack;
ed6acedd 15964 std::vector<struct field> fields;
55426c9d 15965
60f7655a
DE
15966 for (child_die = die->child;
15967 child_die != NULL && child_die->tag;
436c571c 15968 child_die = child_die->sibling)
55426c9d
JB
15969 {
15970 struct attribute *attr;
15971 LONGEST value;
15972 const gdb_byte *bytes;
15973 struct dwarf2_locexpr_baton *baton;
15974 const char *name;
60f7655a 15975
55426c9d
JB
15976 if (child_die->tag != DW_TAG_enumerator)
15977 continue;
15978
15979 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15980 if (attr == NULL)
15981 continue;
15982
15983 name = dwarf2_name (child_die, cu);
15984 if (name == NULL)
15985 name = "<anonymous enumerator>";
15986
15987 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15988 &value, &bytes, &baton);
15989 if (value < 0)
15990 {
15991 unsigned_enum = 0;
15992 flag_enum = 0;
15993 }
55426c9d 15994 else
edd45eb0
SM
15995 {
15996 if (count_one_bits_ll (value) >= 2)
15997 flag_enum = 0;
edd45eb0 15998 }
55426c9d 15999
ed6acedd
TT
16000 fields.emplace_back ();
16001 struct field &field = fields.back ();
16002 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16003 SET_FIELD_ENUMVAL (field, value);
16004 }
16005
16006 if (!fields.empty ())
16007 {
5e33d5f4 16008 type->set_num_fields (fields.size ());
3cabb6b0
SM
16009 type->set_fields
16010 ((struct field *)
16011 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16012 memcpy (type->fields (), fields.data (),
ed6acedd 16013 sizeof (struct field) * fields.size ());
55426c9d
JB
16014 }
16015
16016 if (unsigned_enum)
653223d3
SM
16017 type->set_is_unsigned (true);
16018
55426c9d 16019 if (flag_enum)
9902b327 16020 type->set_is_flag_enum (true);
55426c9d
JB
16021}
16022
134d01f1
DJ
16023/* Given a DW_AT_enumeration_type die, set its type. We do not
16024 complete the type's fields yet, or create any symbols. */
c906108c 16025
f792889a 16026static struct type *
134d01f1 16027read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16028{
5e22e966 16029 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16030 struct type *type;
c906108c 16031 struct attribute *attr;
0114d602 16032 const char *name;
134d01f1 16033
348e048f
DE
16034 /* If the definition of this type lives in .debug_types, read that type.
16035 Don't follow DW_AT_specification though, that will take us back up
16036 the chain and we want to go down. */
052c8bb8 16037 attr = die->attr (DW_AT_signature);
435d3d88 16038 if (attr != nullptr)
348e048f 16039 {
ac9ec31b 16040 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16041
ac9ec31b 16042 /* The type's CU may not be the same as CU.
02142a6c 16043 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16044 return set_die_type (die, type, cu);
16045 }
16046
c906108c
SS
16047 type = alloc_type (objfile);
16048
67607e24 16049 type->set_code (TYPE_CODE_ENUM);
94af9270 16050 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16051 if (name != NULL)
d0e39ea2 16052 type->set_name (name);
c906108c 16053
0626fc76
TT
16054 attr = dwarf2_attr (die, DW_AT_type, cu);
16055 if (attr != NULL)
16056 {
16057 struct type *underlying_type = die_type (die, cu);
16058
16059 TYPE_TARGET_TYPE (type) = underlying_type;
16060 }
16061
e142c38c 16062 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16063 if (attr != nullptr)
c906108c 16064 {
529908cb 16065 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16066 }
16067 else
16068 {
16069 TYPE_LENGTH (type) = 0;
16070 }
16071
2b4424c3
TT
16072 maybe_set_alignment (cu, die, type);
16073
137033e9
JB
16074 /* The enumeration DIE can be incomplete. In Ada, any type can be
16075 declared as private in the package spec, and then defined only
16076 inside the package body. Such types are known as Taft Amendment
16077 Types. When another package uses such a type, an incomplete DIE
16078 may be generated by the compiler. */
02eb380e 16079 if (die_is_declaration (die, cu))
b4b73759 16080 type->set_is_stub (true);
02eb380e 16081
0626fc76
TT
16082 /* If this type has an underlying type that is not a stub, then we
16083 may use its attributes. We always use the "unsigned" attribute
16084 in this situation, because ordinarily we guess whether the type
16085 is unsigned -- but the guess can be wrong and the underlying type
16086 can tell us the reality. However, we defer to a local size
16087 attribute if one exists, because this lets the compiler override
16088 the underlying type if needed. */
e46d3488 16089 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16090 {
9e7c9a03
HD
16091 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16092 underlying_type = check_typedef (underlying_type);
653223d3
SM
16093
16094 type->set_is_unsigned (underlying_type->is_unsigned ());
16095
0626fc76 16096 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16097 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16098
2b4424c3 16099 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16100 && TYPE_RAW_ALIGN (underlying_type) != 0)
16101 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16102 }
16103
aa70e35c 16104 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
3d567982 16105
ed6acedd
TT
16106 set_die_type (die, type, cu);
16107
16108 /* Finish the creation of this type by using the enum's children.
16109 Note that, as usual, this must come after set_die_type to avoid
16110 infinite recursion when trying to compute the names of the
16111 enumerators. */
16112 update_enumeration_type_from_children (die, type, cu);
16113
16114 return type;
134d01f1
DJ
16115}
16116
16117/* Given a pointer to a die which begins an enumeration, process all
16118 the dies that define the members of the enumeration, and create the
16119 symbol for the enumeration type.
16120
16121 NOTE: We reverse the order of the element list. */
16122
16123static void
16124process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16125{
f792889a 16126 struct type *this_type;
134d01f1 16127
f792889a
DJ
16128 this_type = get_die_type (die, cu);
16129 if (this_type == NULL)
16130 this_type = read_enumeration_type (die, cu);
9dc481d3 16131
639d11d3 16132 if (die->child != NULL)
c906108c 16133 {
9dc481d3 16134 struct die_info *child_die;
15d034d0 16135 const char *name;
9dc481d3 16136
639d11d3 16137 child_die = die->child;
c906108c
SS
16138 while (child_die && child_die->tag)
16139 {
16140 if (child_die->tag != DW_TAG_enumerator)
16141 {
e7c27a73 16142 process_die (child_die, cu);
c906108c
SS
16143 }
16144 else
16145 {
39cbfefa
DJ
16146 name = dwarf2_name (child_die, cu);
16147 if (name)
ed6acedd 16148 new_symbol (child_die, this_type, cu);
c906108c
SS
16149 }
16150
436c571c 16151 child_die = child_die->sibling;
c906108c 16152 }
c906108c 16153 }
134d01f1 16154
6c83ed52
TT
16155 /* If we are reading an enum from a .debug_types unit, and the enum
16156 is a declaration, and the enum is not the signatured type in the
16157 unit, then we do not want to add a symbol for it. Adding a
16158 symbol would in some cases obscure the true definition of the
16159 enum, giving users an incomplete type when the definition is
16160 actually available. Note that we do not want to do this for all
16161 enums which are just declarations, because C++0x allows forward
16162 enum declarations. */
3019eac3 16163 if (cu->per_cu->is_debug_types
6c83ed52
TT
16164 && die_is_declaration (die, cu))
16165 {
52dc124a 16166 struct signatured_type *sig_type;
6c83ed52 16167
c0f78cd4 16168 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16169 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16170 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16171 return;
16172 }
16173
f792889a 16174 new_symbol (die, this_type, cu);
c906108c
SS
16175}
16176
57567375
TT
16177/* Helper function for quirk_ada_thick_pointer that examines a bounds
16178 expression for an index type and finds the corresponding field
16179 offset in the hidden "P_BOUNDS" structure. Returns true on success
16180 and updates *FIELD, false if it fails to recognize an
16181 expression. */
16182
16183static bool
16184recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16185 int *bounds_offset, struct field *field,
16186 struct dwarf2_cu *cu)
16187{
16188 struct attribute *attr = dwarf2_attr (die, name, cu);
16189 if (attr == nullptr || !attr->form_is_block ())
16190 return false;
16191
16192 const struct dwarf_block *block = attr->as_block ();
16193 const gdb_byte *start = block->data;
16194 const gdb_byte *end = block->data + block->size;
16195
16196 /* The expression to recognize generally looks like:
16197
16198 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16199 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16200
16201 However, the second "plus_uconst" may be missing:
16202
16203 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16204 DW_OP_deref_size: 4)
16205
16206 This happens when the field is at the start of the structure.
16207
16208 Also, the final deref may not be sized:
16209
16210 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16211 DW_OP_deref)
16212
16213 This happens when the size of the index type happens to be the
16214 same as the architecture's word size. This can occur with or
16215 without the second plus_uconst. */
16216
16217 if (end - start < 2)
16218 return false;
16219 if (*start++ != DW_OP_push_object_address)
16220 return false;
16221 if (*start++ != DW_OP_plus_uconst)
16222 return false;
16223
16224 uint64_t this_bound_off;
16225 start = gdb_read_uleb128 (start, end, &this_bound_off);
16226 if (start == nullptr || (int) this_bound_off != this_bound_off)
16227 return false;
16228 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16229 is consistent among all bounds. */
16230 if (*bounds_offset == -1)
16231 *bounds_offset = this_bound_off;
16232 else if (*bounds_offset != this_bound_off)
16233 return false;
16234
16235 if (start == end || *start++ != DW_OP_deref)
16236 return false;
16237
16238 int offset = 0;
16239 if (start ==end)
16240 return false;
16241 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16242 {
16243 /* This means an offset of 0. */
16244 }
16245 else if (*start++ != DW_OP_plus_uconst)
16246 return false;
16247 else
16248 {
16249 /* The size is the parameter to DW_OP_plus_uconst. */
16250 uint64_t val;
16251 start = gdb_read_uleb128 (start, end, &val);
16252 if (start == nullptr)
16253 return false;
16254 if ((int) val != val)
16255 return false;
16256 offset = val;
16257 }
16258
16259 if (start == end)
16260 return false;
16261
16262 uint64_t size;
16263 if (*start == DW_OP_deref_size)
16264 {
16265 start = gdb_read_uleb128 (start + 1, end, &size);
16266 if (start == nullptr)
16267 return false;
16268 }
16269 else if (*start == DW_OP_deref)
16270 {
16271 size = cu->header.addr_size;
16272 ++start;
16273 }
16274 else
16275 return false;
16276
16277 SET_FIELD_BITPOS (*field, 8 * offset);
16278 if (size != TYPE_LENGTH (field->type ()))
16279 FIELD_BITSIZE (*field) = 8 * size;
16280
16281 return true;
16282}
16283
16284/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16285 some kinds of Ada arrays:
16286
16287 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16288 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16289 <11e0> DW_AT_data_location: 2 byte block: 97 6
16290 (DW_OP_push_object_address; DW_OP_deref)
16291 <11e3> DW_AT_type : <0x1173>
16292 <11e7> DW_AT_sibling : <0x1201>
16293 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16294 <11ec> DW_AT_type : <0x1206>
16295 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16296 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16297 DW_OP_deref_size: 4)
16298 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16299 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16300 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16301
16302 This actually represents a "thick pointer", which is a structure
16303 with two elements: one that is a pointer to the array data, and one
16304 that is a pointer to another structure; this second structure holds
16305 the array bounds.
16306
16307 This returns a new type on success, or nullptr if this didn't
16308 recognize the type. */
16309
16310static struct type *
16311quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16312 struct type *type)
16313{
16314 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16315 /* So far we've only seen this with block form. */
16316 if (attr == nullptr || !attr->form_is_block ())
16317 return nullptr;
16318
16319 /* Note that this will fail if the structure layout is changed by
16320 the compiler. However, we have no good way to recognize some
16321 other layout, because we don't know what expression the compiler
16322 might choose to emit should this happen. */
16323 struct dwarf_block *blk = attr->as_block ();
16324 if (blk->size != 2
16325 || blk->data[0] != DW_OP_push_object_address
16326 || blk->data[1] != DW_OP_deref)
16327 return nullptr;
16328
16329 int bounds_offset = -1;
16330 int max_align = -1;
16331 std::vector<struct field> range_fields;
16332 for (struct die_info *child_die = die->child;
16333 child_die;
16334 child_die = child_die->sibling)
16335 {
16336 if (child_die->tag == DW_TAG_subrange_type)
16337 {
16338 struct type *underlying = read_subrange_index_type (child_die, cu);
16339
16340 int this_align = type_align (underlying);
16341 if (this_align > max_align)
16342 max_align = this_align;
16343
16344 range_fields.emplace_back ();
16345 range_fields.emplace_back ();
16346
16347 struct field &lower = range_fields[range_fields.size () - 2];
16348 struct field &upper = range_fields[range_fields.size () - 1];
16349
16350 lower.set_type (underlying);
16351 FIELD_ARTIFICIAL (lower) = 1;
16352
16353 upper.set_type (underlying);
16354 FIELD_ARTIFICIAL (upper) = 1;
16355
16356 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16357 &bounds_offset, &lower, cu)
16358 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16359 &bounds_offset, &upper, cu))
16360 return nullptr;
16361 }
16362 }
16363
16364 /* This shouldn't really happen, but double-check that we found
16365 where the bounds are stored. */
16366 if (bounds_offset == -1)
16367 return nullptr;
16368
16369 struct objfile *objfile = cu->per_objfile->objfile;
16370 for (int i = 0; i < range_fields.size (); i += 2)
16371 {
16372 char name[20];
16373
16374 /* Set the name of each field in the bounds. */
16375 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16376 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16377 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16378 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16379 }
16380
16381 struct type *bounds = alloc_type (objfile);
16382 bounds->set_code (TYPE_CODE_STRUCT);
16383
16384 bounds->set_num_fields (range_fields.size ());
16385 bounds->set_fields
16386 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16387 * sizeof (struct field))));
16388 memcpy (bounds->fields (), range_fields.data (),
16389 bounds->num_fields () * sizeof (struct field));
16390
16391 int last_fieldno = range_fields.size () - 1;
16392 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16393 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16394 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16395
16396 /* Rewrite the existing array type in place. Specifically, we
16397 remove any dynamic properties we might have read, and we replace
16398 the index types. */
16399 struct type *iter = type;
16400 for (int i = 0; i < range_fields.size (); i += 2)
16401 {
16402 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16403 iter->main_type->dyn_prop_list = nullptr;
16404 iter->set_index_type
16405 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16406 iter = TYPE_TARGET_TYPE (iter);
16407 }
16408
16409 struct type *result = alloc_type (objfile);
16410 result->set_code (TYPE_CODE_STRUCT);
16411
16412 result->set_num_fields (2);
16413 result->set_fields
16414 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16415 * sizeof (struct field))));
16416
16417 /* The names are chosen to coincide with what the compiler does with
16418 -fgnat-encodings=all, which the Ada code in gdb already
16419 understands. */
16420 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16421 result->field (0).set_type (lookup_pointer_type (type));
16422
16423 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16424 result->field (1).set_type (lookup_pointer_type (bounds));
16425 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16426
16427 result->set_name (type->name ());
16428 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16429 + TYPE_LENGTH (result->field (1).type ()));
16430
16431 return result;
16432}
16433
c906108c
SS
16434/* Extract all information from a DW_TAG_array_type DIE and put it in
16435 the DIE's type field. For now, this only handles one dimensional
16436 arrays. */
16437
f792889a 16438static struct type *
e7c27a73 16439read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16440{
5e22e966 16441 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16442 struct die_info *child_die;
7e314c57 16443 struct type *type;
c906108c 16444 struct type *element_type, *range_type, *index_type;
c906108c 16445 struct attribute *attr;
15d034d0 16446 const char *name;
a405673c 16447 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16448 unsigned int bit_stride = 0;
c906108c 16449
e7c27a73 16450 element_type = die_type (die, cu);
c906108c 16451
7e314c57
JK
16452 /* The die_type call above may have already set the type for this DIE. */
16453 type = get_die_type (die, cu);
16454 if (type)
16455 return type;
16456
dc53a7ad
JB
16457 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16458 if (attr != NULL)
a405673c
JB
16459 {
16460 int stride_ok;
293e7e51 16461 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16462
16463 byte_stride_prop
16464 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16465 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16466 prop_type);
a405673c
JB
16467 if (!stride_ok)
16468 {
b98664d3 16469 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16470 " - DIE at %s [in module %s]"),
16471 sect_offset_str (die->sect_off),
5e22e966 16472 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16473 /* Ignore this attribute. We will likely not be able to print
16474 arrays of this type correctly, but there is little we can do
16475 to help if we cannot read the attribute's value. */
16476 byte_stride_prop = NULL;
16477 }
16478 }
dc53a7ad
JB
16479
16480 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16481 if (attr != NULL)
529908cb 16482 bit_stride = attr->constant_value (0);
dc53a7ad 16483
c906108c
SS
16484 /* Irix 6.2 native cc creates array types without children for
16485 arrays with unspecified length. */
639d11d3 16486 if (die->child == NULL)
c906108c 16487 {
46bf5051 16488 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16489 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16490 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16491 byte_stride_prop, bit_stride);
f792889a 16492 return set_die_type (die, type, cu);
c906108c
SS
16493 }
16494
791afaa2 16495 std::vector<struct type *> range_types;
639d11d3 16496 child_die = die->child;
c906108c
SS
16497 while (child_die && child_die->tag)
16498 {
16499 if (child_die->tag == DW_TAG_subrange_type)
16500 {
f792889a 16501 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16502
dda83cd7
SM
16503 if (child_type != NULL)
16504 {
0963b4bd 16505 /* The range type was succesfully read. Save it for the
dda83cd7 16506 array type creation. */
791afaa2 16507 range_types.push_back (child_type);
dda83cd7 16508 }
c906108c 16509 }
436c571c 16510 child_die = child_die->sibling;
c906108c
SS
16511 }
16512
cf2b2075
TV
16513 if (range_types.empty ())
16514 {
16515 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16516 sect_offset_str (die->sect_off),
16517 objfile_name (cu->per_objfile->objfile));
16518 return NULL;
16519 }
16520
c906108c
SS
16521 /* Dwarf2 dimensions are output from left to right, create the
16522 necessary array types in backwards order. */
7ca2d3a3 16523
c906108c 16524 type = element_type;
7ca2d3a3
DL
16525
16526 if (read_array_order (die, cu) == DW_ORD_col_major)
16527 {
16528 int i = 0;
9a619af0 16529
791afaa2 16530 while (i < range_types.size ())
10f6a3ad
TT
16531 {
16532 type = create_array_type_with_stride (NULL, type, range_types[i++],
16533 byte_stride_prop, bit_stride);
16534 bit_stride = 0;
16535 byte_stride_prop = nullptr;
16536 }
7ca2d3a3
DL
16537 }
16538 else
16539 {
791afaa2 16540 size_t ndim = range_types.size ();
7ca2d3a3 16541 while (ndim-- > 0)
10f6a3ad
TT
16542 {
16543 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16544 byte_stride_prop, bit_stride);
16545 bit_stride = 0;
16546 byte_stride_prop = nullptr;
16547 }
7ca2d3a3 16548 }
c906108c 16549
cf2b2075
TV
16550 gdb_assert (type != element_type);
16551
f5f8a009
EZ
16552 /* Understand Dwarf2 support for vector types (like they occur on
16553 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16554 array type. This is not part of the Dwarf2/3 standard yet, but a
16555 custom vendor extension. The main difference between a regular
16556 array and the vector variant is that vectors are passed by value
16557 to functions. */
e142c38c 16558 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16559 if (attr != nullptr)
ea37ba09 16560 make_vector_type (type);
f5f8a009 16561
dbc98a8b
KW
16562 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16563 implementation may choose to implement triple vectors using this
16564 attribute. */
16565 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 16566 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 16567 {
529908cb
TT
16568 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16569 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 16570 else
b98664d3 16571 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16572 "than the total size of elements"));
dbc98a8b
KW
16573 }
16574
39cbfefa
DJ
16575 name = dwarf2_name (die, cu);
16576 if (name)
d0e39ea2 16577 type->set_name (name);
6e70227d 16578
2b4424c3
TT
16579 maybe_set_alignment (cu, die, type);
16580
57567375
TT
16581 struct type *replacement_type = nullptr;
16582 if (cu->language == language_ada)
16583 {
16584 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16585 if (replacement_type != nullptr)
16586 type = replacement_type;
16587 }
16588
0963b4bd 16589 /* Install the type in the die. */
57567375 16590 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
16591
16592 /* set_die_type should be already done. */
b4ba55a1
JB
16593 set_descriptive_type (type, die, cu);
16594
7e314c57 16595 return type;
c906108c
SS
16596}
16597
7ca2d3a3 16598static enum dwarf_array_dim_ordering
6e70227d 16599read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16600{
16601 struct attribute *attr;
16602
16603 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16604
435d3d88 16605 if (attr != nullptr)
1bc397c5
TT
16606 {
16607 LONGEST val = attr->constant_value (-1);
16608 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16609 return (enum dwarf_array_dim_ordering) val;
16610 }
7ca2d3a3 16611
0963b4bd
MS
16612 /* GNU F77 is a special case, as at 08/2004 array type info is the
16613 opposite order to the dwarf2 specification, but data is still
16614 laid out as per normal fortran.
7ca2d3a3 16615
0963b4bd
MS
16616 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16617 version checking. */
7ca2d3a3 16618
905e0470
PM
16619 if (cu->language == language_fortran
16620 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16621 {
16622 return DW_ORD_row_major;
16623 }
16624
3a3440fb 16625 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
16626 {
16627 case array_column_major:
16628 return DW_ORD_col_major;
16629 case array_row_major:
16630 default:
16631 return DW_ORD_row_major;
16632 };
16633}
16634
72019c9c 16635/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16636 the DIE's type field. */
72019c9c 16637
f792889a 16638static struct type *
72019c9c
GM
16639read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16640{
7e314c57
JK
16641 struct type *domain_type, *set_type;
16642 struct attribute *attr;
f792889a 16643
7e314c57
JK
16644 domain_type = die_type (die, cu);
16645
16646 /* The die_type call above may have already set the type for this DIE. */
16647 set_type = get_die_type (die, cu);
16648 if (set_type)
16649 return set_type;
16650
16651 set_type = create_set_type (NULL, domain_type);
16652
16653 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
16654 if (attr != nullptr && attr->form_is_unsigned ())
16655 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 16656
2b4424c3
TT
16657 maybe_set_alignment (cu, die, set_type);
16658
f792889a 16659 return set_die_type (die, set_type, cu);
72019c9c 16660}
7ca2d3a3 16661
0971de02
TT
16662/* A helper for read_common_block that creates a locexpr baton.
16663 SYM is the symbol which we are marking as computed.
16664 COMMON_DIE is the DIE for the common block.
16665 COMMON_LOC is the location expression attribute for the common
16666 block itself.
16667 MEMBER_LOC is the location expression attribute for the particular
16668 member of the common block that we are processing.
16669 CU is the CU from which the above come. */
16670
16671static void
16672mark_common_block_symbol_computed (struct symbol *sym,
16673 struct die_info *common_die,
16674 struct attribute *common_loc,
16675 struct attribute *member_loc,
16676 struct dwarf2_cu *cu)
16677{
5e22e966 16678 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16679 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16680 struct dwarf2_locexpr_baton *baton;
16681 gdb_byte *ptr;
16682 unsigned int cu_off;
08feed99 16683 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16684 LONGEST offset = 0;
16685
16686 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16687 gdb_assert (common_loc->form_is_block ());
16688 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16689 || member_loc->form_is_constant ());
0971de02 16690
8d749320 16691 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16692 baton->per_objfile = per_objfile;
0971de02
TT
16693 baton->per_cu = cu->per_cu;
16694 gdb_assert (baton->per_cu);
16695
16696 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16697
cd6c91b4 16698 if (member_loc->form_is_constant ())
0971de02 16699 {
0826b30a 16700 offset = member_loc->constant_value (0);
0971de02
TT
16701 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16702 }
16703 else
9d2246fc 16704 baton->size += member_loc->as_block ()->size;
0971de02 16705
224c3ddb 16706 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16707 baton->data = ptr;
16708
16709 *ptr++ = DW_OP_call4;
9c541725 16710 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16711 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16712 ptr += 4;
16713
cd6c91b4 16714 if (member_loc->form_is_constant ())
0971de02
TT
16715 {
16716 *ptr++ = DW_OP_addr;
16717 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16718 ptr += cu->header.addr_size;
16719 }
16720 else
16721 {
16722 /* We have to copy the data here, because DW_OP_call4 will only
16723 use a DW_AT_location attribute. */
9d2246fc
TT
16724 struct dwarf_block *block = member_loc->as_block ();
16725 memcpy (ptr, block->data, block->size);
16726 ptr += block->size;
0971de02
TT
16727 }
16728
16729 *ptr++ = DW_OP_plus;
16730 gdb_assert (ptr - baton->data == baton->size);
16731
0971de02 16732 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16733 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16734}
16735
4357ac6c
TT
16736/* Create appropriate locally-scoped variables for all the
16737 DW_TAG_common_block entries. Also create a struct common_block
16738 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16739 is used to separate the common blocks name namespace from regular
4357ac6c 16740 variable names. */
c906108c
SS
16741
16742static void
e7c27a73 16743read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16744{
0971de02
TT
16745 struct attribute *attr;
16746
16747 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16748 if (attr != nullptr)
0971de02
TT
16749 {
16750 /* Support the .debug_loc offsets. */
4fc6c0d5 16751 if (attr->form_is_block ())
dda83cd7 16752 {
0971de02 16753 /* Ok. */
dda83cd7 16754 }
cd6c91b4 16755 else if (attr->form_is_section_offset ())
dda83cd7 16756 {
0971de02
TT
16757 dwarf2_complex_location_expr_complaint ();
16758 attr = NULL;
dda83cd7 16759 }
0971de02 16760 else
dda83cd7 16761 {
0971de02
TT
16762 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16763 "common block member");
16764 attr = NULL;
dda83cd7 16765 }
0971de02
TT
16766 }
16767
639d11d3 16768 if (die->child != NULL)
c906108c 16769 {
5e22e966 16770 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16771 struct die_info *child_die;
16772 size_t n_entries = 0, size;
16773 struct common_block *common_block;
16774 struct symbol *sym;
74ac6d43 16775
4357ac6c
TT
16776 for (child_die = die->child;
16777 child_die && child_die->tag;
436c571c 16778 child_die = child_die->sibling)
4357ac6c
TT
16779 ++n_entries;
16780
16781 size = (sizeof (struct common_block)
16782 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16783 common_block
16784 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16785 size);
4357ac6c
TT
16786 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16787 common_block->n_entries = 0;
16788
16789 for (child_die = die->child;
16790 child_die && child_die->tag;
436c571c 16791 child_die = child_die->sibling)
4357ac6c
TT
16792 {
16793 /* Create the symbol in the DW_TAG_common_block block in the current
16794 symbol scope. */
e7c27a73 16795 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16796 if (sym != NULL)
16797 {
16798 struct attribute *member_loc;
16799
16800 common_block->contents[common_block->n_entries++] = sym;
16801
16802 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16803 cu);
16804 if (member_loc)
16805 {
16806 /* GDB has handled this for a long time, but it is
16807 not specified by DWARF. It seems to have been
16808 emitted by gfortran at least as recently as:
16809 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16810 complaint (_("Variable in common block has "
0971de02 16811 "DW_AT_data_member_location "
9d8780f0
SM
16812 "- DIE at %s [in module %s]"),
16813 sect_offset_str (child_die->sect_off),
518817b3 16814 objfile_name (objfile));
0971de02 16815
cd6c91b4 16816 if (member_loc->form_is_section_offset ())
0971de02 16817 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16818 else if (member_loc->form_is_constant ()
4fc6c0d5 16819 || member_loc->form_is_block ())
0971de02 16820 {
435d3d88 16821 if (attr != nullptr)
0971de02
TT
16822 mark_common_block_symbol_computed (sym, die, attr,
16823 member_loc, cu);
16824 }
16825 else
16826 dwarf2_complex_location_expr_complaint ();
16827 }
16828 }
c906108c 16829 }
4357ac6c
TT
16830
16831 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16832 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16833 }
16834}
16835
0114d602 16836/* Create a type for a C++ namespace. */
d9fa45fe 16837
0114d602
DJ
16838static struct type *
16839read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16840{
5e22e966 16841 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16842 const char *previous_prefix, *name;
9219021c 16843 int is_anonymous;
0114d602
DJ
16844 struct type *type;
16845
16846 /* For extensions, reuse the type of the original namespace. */
16847 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16848 {
16849 struct die_info *ext_die;
16850 struct dwarf2_cu *ext_cu = cu;
9a619af0 16851
0114d602
DJ
16852 ext_die = dwarf2_extension (die, &ext_cu);
16853 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16854
16855 /* EXT_CU may not be the same as CU.
02142a6c 16856 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16857 return set_die_type (die, type, cu);
16858 }
9219021c 16859
e142c38c 16860 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16861
16862 /* Now build the name of the current namespace. */
16863
0114d602
DJ
16864 previous_prefix = determine_prefix (die, cu);
16865 if (previous_prefix[0] != '\0')
16866 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16867 previous_prefix, name, 0, cu);
0114d602
DJ
16868
16869 /* Create the type. */
19f392bc 16870 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16871
60531b24 16872 return set_die_type (die, type, cu);
0114d602
DJ
16873}
16874
22cee43f 16875/* Read a namespace scope. */
0114d602
DJ
16876
16877static void
16878read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16879{
5e22e966 16880 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16881 int is_anonymous;
9219021c 16882
5c4e30ca
DC
16883 /* Add a symbol associated to this if we haven't seen the namespace
16884 before. Also, add a using directive if it's an anonymous
16885 namespace. */
9219021c 16886
f2f0e013 16887 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16888 {
16889 struct type *type;
16890
0114d602 16891 type = read_type_die (die, cu);
e7c27a73 16892 new_symbol (die, type, cu);
5c4e30ca 16893
e8e80198 16894 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16895 if (is_anonymous)
0114d602
DJ
16896 {
16897 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16898
eb1e02fd 16899 std::vector<const char *> excludes;
804d2729 16900 add_using_directive (using_directives (cu),
7d93a1e0 16901 previous_prefix, type->name (), NULL,
eb1e02fd 16902 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16903 }
5c4e30ca 16904 }
9219021c 16905
639d11d3 16906 if (die->child != NULL)
d9fa45fe 16907 {
639d11d3 16908 struct die_info *child_die = die->child;
6e70227d 16909
d9fa45fe
DC
16910 while (child_die && child_die->tag)
16911 {
e7c27a73 16912 process_die (child_die, cu);
436c571c 16913 child_die = child_die->sibling;
d9fa45fe
DC
16914 }
16915 }
38d518c9
EZ
16916}
16917
f55ee35c
JK
16918/* Read a Fortran module as type. This DIE can be only a declaration used for
16919 imported module. Still we need that type as local Fortran "use ... only"
16920 declaration imports depend on the created type in determine_prefix. */
16921
16922static struct type *
16923read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16924{
5e22e966 16925 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16926 const char *module_name;
f55ee35c
JK
16927 struct type *type;
16928
16929 module_name = dwarf2_name (die, cu);
19f392bc 16930 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16931
f55ee35c
JK
16932 return set_die_type (die, type, cu);
16933}
16934
5d7cb8df
JK
16935/* Read a Fortran module. */
16936
16937static void
16938read_module (struct die_info *die, struct dwarf2_cu *cu)
16939{
16940 struct die_info *child_die = die->child;
530e8392
KB
16941 struct type *type;
16942
16943 type = read_type_die (die, cu);
16944 new_symbol (die, type, cu);
5d7cb8df 16945
5d7cb8df
JK
16946 while (child_die && child_die->tag)
16947 {
16948 process_die (child_die, cu);
436c571c 16949 child_die = child_die->sibling;
5d7cb8df
JK
16950 }
16951}
16952
38d518c9
EZ
16953/* Return the name of the namespace represented by DIE. Set
16954 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16955 namespace. */
16956
16957static const char *
e142c38c 16958namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16959{
16960 struct die_info *current_die;
16961 const char *name = NULL;
16962
16963 /* Loop through the extensions until we find a name. */
16964
16965 for (current_die = die;
16966 current_die != NULL;
f2f0e013 16967 current_die = dwarf2_extension (die, &cu))
38d518c9 16968 {
96553a0c
DE
16969 /* We don't use dwarf2_name here so that we can detect the absence
16970 of a name -> anonymous namespace. */
7d45c7c3 16971 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16972
38d518c9
EZ
16973 if (name != NULL)
16974 break;
16975 }
16976
16977 /* Is it an anonymous namespace? */
16978
16979 *is_anonymous = (name == NULL);
16980 if (*is_anonymous)
2b1dbab0 16981 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16982
16983 return name;
d9fa45fe
DC
16984}
16985
c906108c
SS
16986/* Extract all information from a DW_TAG_pointer_type DIE and add to
16987 the user defined type vector. */
16988
f792889a 16989static struct type *
e7c27a73 16990read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16991{
5e22e966 16992 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16993 struct comp_unit_head *cu_header = &cu->header;
c906108c 16994 struct type *type;
8b2dbe47
KB
16995 struct attribute *attr_byte_size;
16996 struct attribute *attr_address_class;
16997 int byte_size, addr_class;
7e314c57
JK
16998 struct type *target_type;
16999
17000 target_type = die_type (die, cu);
c906108c 17001
7e314c57
JK
17002 /* The die_type call above may have already set the type for this DIE. */
17003 type = get_die_type (die, cu);
17004 if (type)
17005 return type;
17006
17007 type = lookup_pointer_type (target_type);
8b2dbe47 17008
e142c38c 17009 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17010 if (attr_byte_size)
529908cb 17011 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17012 else
8b2dbe47
KB
17013 byte_size = cu_header->addr_size;
17014
e142c38c 17015 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17016 if (attr_address_class)
529908cb 17017 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17018 else
17019 addr_class = DW_ADDR_none;
17020
2b4424c3
TT
17021 ULONGEST alignment = get_alignment (cu, die);
17022
17023 /* If the pointer size, alignment, or address class is different
17024 than the default, create a type variant marked as such and set
17025 the length accordingly. */
17026 if (TYPE_LENGTH (type) != byte_size
17027 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17028 && alignment != TYPE_RAW_ALIGN (type))
17029 || addr_class != DW_ADDR_none)
c906108c 17030 {
5e2b427d 17031 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17032 {
314ad88d
PA
17033 type_instance_flags type_flags
17034 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17035 addr_class);
876cecd0
TT
17036 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17037 == 0);
8b2dbe47
KB
17038 type = make_type_with_address_space (type, type_flags);
17039 }
17040 else if (TYPE_LENGTH (type) != byte_size)
17041 {
b98664d3 17042 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17043 }
2b4424c3
TT
17044 else if (TYPE_RAW_ALIGN (type) != alignment)
17045 {
b98664d3 17046 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17047 " - DIE at %s [in module %s]"),
17048 sect_offset_str (die->sect_off),
5e22e966 17049 objfile_name (cu->per_objfile->objfile));
2b4424c3 17050 }
6e70227d 17051 else
9a619af0
MS
17052 {
17053 /* Should we also complain about unhandled address classes? */
17054 }
c906108c 17055 }
8b2dbe47
KB
17056
17057 TYPE_LENGTH (type) = byte_size;
2b4424c3 17058 set_type_align (type, alignment);
f792889a 17059 return set_die_type (die, type, cu);
c906108c
SS
17060}
17061
17062/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17063 the user defined type vector. */
17064
f792889a 17065static struct type *
e7c27a73 17066read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17067{
17068 struct type *type;
17069 struct type *to_type;
17070 struct type *domain;
17071
e7c27a73
DJ
17072 to_type = die_type (die, cu);
17073 domain = die_containing_type (die, cu);
0d5de010 17074
7e314c57
JK
17075 /* The calls above may have already set the type for this DIE. */
17076 type = get_die_type (die, cu);
17077 if (type)
17078 return type;
17079
78134374 17080 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17081 type = lookup_methodptr_type (to_type);
78134374 17082 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17083 {
5e22e966 17084 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17085
17086 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17087 to_type->fields (), to_type->num_fields (),
a409645d 17088 to_type->has_varargs ());
7078baeb
TT
17089 type = lookup_methodptr_type (new_type);
17090 }
0d5de010
DJ
17091 else
17092 type = lookup_memberptr_type (to_type, domain);
c906108c 17093
f792889a 17094 return set_die_type (die, type, cu);
c906108c
SS
17095}
17096
4297a3f0 17097/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17098 the user defined type vector. */
17099
f792889a 17100static struct type *
4297a3f0 17101read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17102 enum type_code refcode)
c906108c 17103{
e7c27a73 17104 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17105 struct type *type, *target_type;
c906108c
SS
17106 struct attribute *attr;
17107
4297a3f0
AV
17108 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17109
7e314c57
JK
17110 target_type = die_type (die, cu);
17111
17112 /* The die_type call above may have already set the type for this DIE. */
17113 type = get_die_type (die, cu);
17114 if (type)
17115 return type;
17116
4297a3f0 17117 type = lookup_reference_type (target_type, refcode);
e142c38c 17118 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17119 if (attr != nullptr)
c906108c 17120 {
529908cb 17121 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17122 }
17123 else
17124 {
107d2387 17125 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17126 }
2b4424c3 17127 maybe_set_alignment (cu, die, type);
f792889a 17128 return set_die_type (die, type, cu);
c906108c
SS
17129}
17130
cf363f18
MW
17131/* Add the given cv-qualifiers to the element type of the array. GCC
17132 outputs DWARF type qualifiers that apply to an array, not the
17133 element type. But GDB relies on the array element type to carry
17134 the cv-qualifiers. This mimics section 6.7.3 of the C99
17135 specification. */
17136
17137static struct type *
17138add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17139 struct type *base_type, int cnst, int voltl)
17140{
17141 struct type *el_type, *inner_array;
17142
17143 base_type = copy_type (base_type);
17144 inner_array = base_type;
17145
78134374 17146 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17147 {
17148 TYPE_TARGET_TYPE (inner_array) =
17149 copy_type (TYPE_TARGET_TYPE (inner_array));
17150 inner_array = TYPE_TARGET_TYPE (inner_array);
17151 }
17152
17153 el_type = TYPE_TARGET_TYPE (inner_array);
17154 cnst |= TYPE_CONST (el_type);
17155 voltl |= TYPE_VOLATILE (el_type);
17156 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17157
17158 return set_die_type (die, base_type, cu);
17159}
17160
f792889a 17161static struct type *
e7c27a73 17162read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17163{
f792889a 17164 struct type *base_type, *cv_type;
c906108c 17165
e7c27a73 17166 base_type = die_type (die, cu);
7e314c57
JK
17167
17168 /* The die_type call above may have already set the type for this DIE. */
17169 cv_type = get_die_type (die, cu);
17170 if (cv_type)
17171 return cv_type;
17172
2f608a3a
KW
17173 /* In case the const qualifier is applied to an array type, the element type
17174 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17175 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17176 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17177
f792889a
DJ
17178 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17179 return set_die_type (die, cv_type, cu);
c906108c
SS
17180}
17181
f792889a 17182static struct type *
e7c27a73 17183read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17184{
f792889a 17185 struct type *base_type, *cv_type;
c906108c 17186
e7c27a73 17187 base_type = die_type (die, cu);
7e314c57
JK
17188
17189 /* The die_type call above may have already set the type for this DIE. */
17190 cv_type = get_die_type (die, cu);
17191 if (cv_type)
17192 return cv_type;
17193
cf363f18
MW
17194 /* In case the volatile qualifier is applied to an array type, the
17195 element type is so qualified, not the array type (section 6.7.3
17196 of C99). */
78134374 17197 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17198 return add_array_cv_type (die, cu, base_type, 0, 1);
17199
f792889a
DJ
17200 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17201 return set_die_type (die, cv_type, cu);
c906108c
SS
17202}
17203
06d66ee9
TT
17204/* Handle DW_TAG_restrict_type. */
17205
17206static struct type *
17207read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17208{
17209 struct type *base_type, *cv_type;
17210
17211 base_type = die_type (die, cu);
17212
17213 /* The die_type call above may have already set the type for this DIE. */
17214 cv_type = get_die_type (die, cu);
17215 if (cv_type)
17216 return cv_type;
17217
17218 cv_type = make_restrict_type (base_type);
17219 return set_die_type (die, cv_type, cu);
17220}
17221
a2c2acaf
MW
17222/* Handle DW_TAG_atomic_type. */
17223
17224static struct type *
17225read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17226{
17227 struct type *base_type, *cv_type;
17228
17229 base_type = die_type (die, cu);
17230
17231 /* The die_type call above may have already set the type for this DIE. */
17232 cv_type = get_die_type (die, cu);
17233 if (cv_type)
17234 return cv_type;
17235
17236 cv_type = make_atomic_type (base_type);
17237 return set_die_type (die, cv_type, cu);
17238}
17239
c906108c
SS
17240/* Extract all information from a DW_TAG_string_type DIE and add to
17241 the user defined type vector. It isn't really a user defined type,
17242 but it behaves like one, with other DIE's using an AT_user_def_type
17243 attribute to reference it. */
17244
f792889a 17245static struct type *
e7c27a73 17246read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17247{
5e22e966 17248 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17249 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17250 struct type *type, *range_type, *index_type, *char_type;
17251 struct attribute *attr;
216a7e6b
AB
17252 struct dynamic_prop prop;
17253 bool length_is_constant = true;
17254 LONGEST length;
17255
17256 /* There are a couple of places where bit sizes might be made use of
17257 when parsing a DW_TAG_string_type, however, no producer that we know
17258 of make use of these. Handling bit sizes that are a multiple of the
17259 byte size is easy enough, but what about other bit sizes? Lets deal
17260 with that problem when we have to. Warn about these attributes being
17261 unsupported, then parse the type and ignore them like we always
17262 have. */
17263 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17264 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17265 {
17266 static bool warning_printed = false;
17267 if (!warning_printed)
17268 {
17269 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17270 "currently supported on DW_TAG_string_type."));
17271 warning_printed = true;
17272 }
17273 }
c906108c 17274
e142c38c 17275 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17276 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17277 {
17278 /* The string length describes the location at which the length of
17279 the string can be found. The size of the length field can be
17280 specified with one of the attributes below. */
17281 struct type *prop_type;
17282 struct attribute *len
17283 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17284 if (len == nullptr)
17285 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17286 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17287 {
17288 /* Pass 0 as the default as we know this attribute is constant
17289 and the default value will not be returned. */
0826b30a 17290 LONGEST sz = len->constant_value (0);
293e7e51 17291 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17292 }
17293 else
17294 {
17295 /* If the size is not specified then we assume it is the size of
17296 an address on this target. */
293e7e51 17297 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17298 }
17299
17300 /* Convert the attribute into a dynamic property. */
17301 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17302 length = 1;
17303 else
17304 length_is_constant = false;
17305 }
17306 else if (attr != nullptr)
17307 {
17308 /* This DW_AT_string_length just contains the length with no
17309 indirection. There's no need to create a dynamic property in this
17310 case. Pass 0 for the default value as we know it will not be
17311 returned in this case. */
0826b30a 17312 length = attr->constant_value (0);
216a7e6b
AB
17313 }
17314 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17315 {
216a7e6b 17316 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17317 length = attr->constant_value (1);
c906108c
SS
17318 }
17319 else
17320 {
216a7e6b
AB
17321 /* Use 1 as a fallback length if we have nothing else. */
17322 length = 1;
c906108c 17323 }
6ccb9162 17324
46bf5051 17325 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17326 if (length_is_constant)
17327 range_type = create_static_range_type (NULL, index_type, 1, length);
17328 else
17329 {
17330 struct dynamic_prop low_bound;
17331
8c2e4e06 17332 low_bound.set_const_val (1);
216a7e6b
AB
17333 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17334 }
3b7538c0
UW
17335 char_type = language_string_char_type (cu->language_defn, gdbarch);
17336 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17337
f792889a 17338 return set_die_type (die, type, cu);
c906108c
SS
17339}
17340
4d804846
JB
17341/* Assuming that DIE corresponds to a function, returns nonzero
17342 if the function is prototyped. */
17343
17344static int
17345prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17346{
17347 struct attribute *attr;
17348
17349 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17350 if (attr && attr->as_boolean ())
4d804846
JB
17351 return 1;
17352
17353 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17354 is only meaningful for C, but the concept also extends to other
4d804846
JB
17355 languages that allow unprototyped functions (Eg: Objective C).
17356 For all other languages, assume that functions are always
17357 prototyped. */
17358 if (cu->language != language_c
17359 && cu->language != language_objc
17360 && cu->language != language_opencl)
17361 return 1;
17362
17363 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17364 prototyped and unprototyped functions; default to prototyped,
17365 since that is more common in modern code (and RealView warns
17366 about unprototyped functions). */
17367 if (producer_is_realview (cu->producer))
17368 return 1;
17369
17370 return 0;
17371}
17372
c906108c
SS
17373/* Handle DIES due to C code like:
17374
17375 struct foo
c5aa993b
JM
17376 {
17377 int (*funcp)(int a, long l);
17378 int b;
17379 };
c906108c 17380
0963b4bd 17381 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17382
f792889a 17383static struct type *
e7c27a73 17384read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17385{
5e22e966 17386 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17387 struct type *type; /* Type that this function returns. */
17388 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17389 struct attribute *attr;
17390
e7c27a73 17391 type = die_type (die, cu);
7e314c57
JK
17392
17393 /* The die_type call above may have already set the type for this DIE. */
17394 ftype = get_die_type (die, cu);
17395 if (ftype)
17396 return ftype;
17397
0c8b41f1 17398 ftype = lookup_function_type (type);
c906108c 17399
4d804846 17400 if (prototyped_function_p (die, cu))
27e69b7a 17401 ftype->set_is_prototyped (true);
c906108c 17402
c055b101
CV
17403 /* Store the calling convention in the type if it's available in
17404 the subroutine die. Otherwise set the calling convention to
17405 the default value DW_CC_normal. */
17406 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 17407 if (attr != nullptr
529908cb 17408 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 17409 TYPE_CALLING_CONVENTION (ftype)
529908cb 17410 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
17411 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17412 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17413 else
17414 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17415
743649fd
MW
17416 /* Record whether the function returns normally to its caller or not
17417 if the DWARF producer set that information. */
17418 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 17419 if (attr && attr->as_boolean ())
743649fd
MW
17420 TYPE_NO_RETURN (ftype) = 1;
17421
76c10ea2
GM
17422 /* We need to add the subroutine type to the die immediately so
17423 we don't infinitely recurse when dealing with parameters
0963b4bd 17424 declared as the same subroutine type. */
76c10ea2 17425 set_die_type (die, ftype, cu);
6e70227d 17426
639d11d3 17427 if (die->child != NULL)
c906108c 17428 {
bb5ed363 17429 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17430 struct die_info *child_die;
8072405b 17431 int nparams, iparams;
c906108c
SS
17432
17433 /* Count the number of parameters.
dda83cd7
SM
17434 FIXME: GDB currently ignores vararg functions, but knows about
17435 vararg member functions. */
8072405b 17436 nparams = 0;
639d11d3 17437 child_die = die->child;
c906108c
SS
17438 while (child_die && child_die->tag)
17439 {
17440 if (child_die->tag == DW_TAG_formal_parameter)
17441 nparams++;
17442 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
17443 ftype->set_has_varargs (true);
17444
436c571c 17445 child_die = child_die->sibling;
c906108c
SS
17446 }
17447
17448 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17449 ftype->set_num_fields (nparams);
3cabb6b0
SM
17450 ftype->set_fields
17451 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17452
8072405b
JK
17453 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17454 even if we error out during the parameters reading below. */
17455 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17456 ftype->field (iparams).set_type (void_type);
8072405b
JK
17457
17458 iparams = 0;
639d11d3 17459 child_die = die->child;
c906108c
SS
17460 while (child_die && child_die->tag)
17461 {
17462 if (child_die->tag == DW_TAG_formal_parameter)
17463 {
3ce3b1ba
PA
17464 struct type *arg_type;
17465
17466 /* DWARF version 2 has no clean way to discern C++
17467 static and non-static member functions. G++ helps
17468 GDB by marking the first parameter for non-static
17469 member functions (which is the this pointer) as
17470 artificial. We pass this information to
17471 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17472
17473 DWARF version 3 added DW_AT_object_pointer, which GCC
17474 4.5 does not yet generate. */
e142c38c 17475 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17476 if (attr != nullptr)
c45bc3f8 17477 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 17478 else
9c37b5ae 17479 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17480 arg_type = die_type (child_die, cu);
17481
17482 /* RealView does not mark THIS as const, which the testsuite
17483 expects. GCC marks THIS as const in method definitions,
17484 but not in the class specifications (GCC PR 43053). */
17485 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17486 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17487 {
17488 int is_this = 0;
17489 struct dwarf2_cu *arg_cu = cu;
17490 const char *name = dwarf2_name (child_die, cu);
17491
17492 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17493 if (attr != nullptr)
3ce3b1ba
PA
17494 {
17495 /* If the compiler emits this, use it. */
17496 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17497 is_this = 1;
17498 }
17499 else if (name && strcmp (name, "this") == 0)
17500 /* Function definitions will have the argument names. */
17501 is_this = 1;
17502 else if (name == NULL && iparams == 0)
17503 /* Declarations may not have the names, so like
17504 elsewhere in GDB, assume an artificial first
17505 argument is "this". */
17506 is_this = 1;
17507
17508 if (is_this)
17509 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17510 arg_type, 0);
17511 }
17512
5d14b6e5 17513 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17514 iparams++;
17515 }
436c571c 17516 child_die = child_die->sibling;
c906108c
SS
17517 }
17518 }
17519
76c10ea2 17520 return ftype;
c906108c
SS
17521}
17522
f792889a 17523static struct type *
e7c27a73 17524read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17525{
5e22e966 17526 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17527 const char *name = NULL;
3c8e0968 17528 struct type *this_type, *target_type;
c906108c 17529
94af9270 17530 name = dwarf2_full_name (NULL, die, cu);
19f392bc 17531 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 17532 this_type->set_target_is_stub (true);
f792889a 17533 set_die_type (die, this_type, cu);
3c8e0968
DE
17534 target_type = die_type (die, cu);
17535 if (target_type != this_type)
17536 TYPE_TARGET_TYPE (this_type) = target_type;
17537 else
17538 {
17539 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17540 spec and cause infinite loops in GDB. */
b98664d3 17541 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17542 "- DIE at %s [in module %s]"),
17543 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17544 TYPE_TARGET_TYPE (this_type) = NULL;
17545 }
e4003a34
TV
17546 if (name == NULL)
17547 {
17548 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17549 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17550 Handle these by just returning the target type, rather than
17551 constructing an anonymous typedef type and trying to handle this
17552 elsewhere. */
17553 set_die_type (die, target_type, cu);
17554 return target_type;
17555 }
f792889a 17556 return this_type;
c906108c
SS
17557}
17558
a625a8c9
TT
17559/* Helper for get_dwarf2_rational_constant that computes the value of
17560 a given gmp_mpz given an attribute. */
17561
17562static void
17563get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17564{
17565 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17566 location expression that pushes an implicit value. */
17567 if (attr->form == DW_FORM_exprloc)
17568 {
17569 dwarf_block *blk = attr->as_block ();
17570 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17571 {
17572 uint64_t len;
17573 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17574 blk->data + blk->size,
17575 &len);
17576 if (ptr - blk->data + len <= blk->size)
17577 {
17578 mpz_import (value->val, len,
17579 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17580 1, 0, 0, ptr);
17581 return;
17582 }
17583 }
17584
17585 /* On failure set it to 1. */
17586 *value = gdb_mpz (1);
17587 }
17588 else if (attr->form_is_block ())
17589 {
17590 dwarf_block *blk = attr->as_block ();
17591 mpz_import (value->val, blk->size,
17592 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17593 1, 0, 0, blk->data);
17594 }
17595 else
17596 *value = gdb_mpz (attr->constant_value (1));
17597}
17598
09584414
JB
17599/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17600 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17601
17602 If the numerator and/or numerator attribute is missing,
17603 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17604 untouched. */
17605
17606static void
17607get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 17608 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
17609{
17610 struct attribute *num_attr, *denom_attr;
17611
17612 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17613 if (num_attr == nullptr)
17614 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17615 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17616
17617 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17618 if (denom_attr == nullptr)
17619 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17620 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17621
17622 if (num_attr == nullptr || denom_attr == nullptr)
17623 return;
17624
a625a8c9
TT
17625 get_mpz (cu, numerator, num_attr);
17626 get_mpz (cu, denominator, denom_attr);
09584414
JB
17627}
17628
17629/* Same as get_dwarf2_rational_constant, but extracting an unsigned
17630 rational constant, rather than a signed one.
17631
17632 If the rational constant has a negative value, a complaint
17633 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17634
17635static void
17636get_dwarf2_unsigned_rational_constant (struct die_info *die,
17637 struct dwarf2_cu *cu,
5cde1d82
TT
17638 gdb_mpz *numerator,
17639 gdb_mpz *denominator)
09584414 17640{
5cde1d82
TT
17641 gdb_mpz num (1);
17642 gdb_mpz denom (1);
09584414
JB
17643
17644 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 17645 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 17646 {
5cde1d82
TT
17647 mpz_neg (num.val, num.val);
17648 mpz_neg (denom.val, denom.val);
09584414 17649 }
5cde1d82 17650 else if (mpz_sgn (num.val) == -1)
09584414
JB
17651 {
17652 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17653 " in DIE at %s"),
17654 sect_offset_str (die->sect_off));
17655 return;
17656 }
5cde1d82 17657 else if (mpz_sgn (denom.val) == -1)
09584414
JB
17658 {
17659 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17660 " in DIE at %s"),
17661 sect_offset_str (die->sect_off));
17662 return;
17663 }
17664
5cde1d82
TT
17665 *numerator = std::move (num);
17666 *denominator = std::move (denom);
09584414
JB
17667}
17668
bbcdf9ab
TT
17669/* Assuming that ENCODING is a string whose contents starting at the
17670 K'th character is "_nn" where "nn" is a decimal number, scan that
17671 number and set RESULT to the value. K is updated to point to the
17672 character immediately following the number.
17673
17674 If the string does not conform to the format described above, false
17675 is returned, and K may or may not be changed. */
17676
17677static bool
17678ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17679{
17680 /* The next character should be an underscore ('_') followed
17681 by a digit. */
17682 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17683 return false;
17684
17685 /* Skip the underscore. */
17686 k++;
17687 int start = k;
17688
17689 /* Determine the number of digits for our number. */
17690 while (isdigit (encoding[k]))
17691 k++;
17692 if (k == start)
17693 return false;
17694
17695 std::string copy (&encoding[start], k - start);
17696 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17697 return false;
17698
17699 return true;
17700}
17701
17702/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17703 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17704 DENOM, update OFFSET, and return true on success. Return false on
17705 failure. */
17706
17707static bool
17708ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17709 gdb_mpz *num, gdb_mpz *denom)
17710{
17711 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17712 return false;
17713 return ada_get_gnat_encoded_number (encoding, offset, denom);
17714}
17715
09584414 17716/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
17717 of the corresponding TYPE by setting its type-specific data. CU is
17718 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17719 encodings. It is nullptr if the GNAT encoding should be
17720 ignored. */
09584414
JB
17721
17722static void
bbcdf9ab
TT
17723finish_fixed_point_type (struct type *type, const char *suffix,
17724 struct die_info *die, struct dwarf2_cu *cu)
09584414 17725{
09584414
JB
17726 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17727 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17728
bbcdf9ab
TT
17729 /* If GNAT encodings are preferred, don't examine the
17730 attributes. */
17731 struct attribute *attr = nullptr;
17732 if (suffix == nullptr)
17733 {
17734 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17735 if (attr == nullptr)
17736 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17737 if (attr == nullptr)
17738 attr = dwarf2_attr (die, DW_AT_small, cu);
17739 }
09584414 17740
5cde1d82
TT
17741 /* Numerator and denominator of our fixed-point type's scaling factor.
17742 The default is a scaling factor of 1, which we use as a fallback
17743 when we are not able to decode it (problem with the debugging info,
17744 unsupported forms, bug in GDB, etc...). Using that as the default
17745 allows us to at least print the unscaled value, which might still
17746 be useful to a user. */
17747 gdb_mpz scale_num (1);
17748 gdb_mpz scale_denom (1);
17749
09584414
JB
17750 if (attr == nullptr)
17751 {
bbcdf9ab
TT
17752 int offset = 0;
17753 if (suffix != nullptr
17754 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17755 &scale_denom)
17756 /* The number might be encoded as _nn_dd_nn_dd, where the
17757 second ratio is the 'small value. In this situation, we
17758 want the second value. */
17759 && (suffix[offset] != '_'
17760 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17761 &scale_denom)))
17762 {
17763 /* Found it. */
17764 }
17765 else
17766 {
17767 /* Scaling factor not found. Assume a scaling factor of 1,
17768 and hope for the best. At least the user will be able to
17769 see the encoded value. */
17770 scale_num = 1;
17771 scale_denom = 1;
17772 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17773 sect_offset_str (die->sect_off));
17774 }
09584414
JB
17775 }
17776 else if (attr->name == DW_AT_binary_scale)
17777 {
17778 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 17779 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 17780
5cde1d82 17781 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
17782 }
17783 else if (attr->name == DW_AT_decimal_scale)
17784 {
17785 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 17786 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 17787
5cde1d82 17788 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
17789 }
17790 else if (attr->name == DW_AT_small)
17791 {
17792 struct die_info *scale_die;
17793 struct dwarf2_cu *scale_cu = cu;
17794
17795 scale_die = follow_die_ref (die, attr, &scale_cu);
17796 if (scale_die->tag == DW_TAG_constant)
17797 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17798 &scale_num, &scale_denom);
17799 else
17800 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17801 " (DIE at %s)"),
17802 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17803 }
17804 else
17805 {
17806 complaint (_("unsupported scale attribute %s for fixed-point type"
17807 " (DIE at %s)"),
17808 dwarf_attr_name (attr->name),
17809 sect_offset_str (die->sect_off));
17810 }
17811
2a12c336 17812 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
17813 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17814 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
17815 mpq_canonicalize (scaling_factor.val);
17816}
17817
bbcdf9ab
TT
17818/* The gnat-encoding suffix for fixed point. */
17819
17820#define GNAT_FIXED_POINT_SUFFIX "___XF_"
17821
17822/* If NAME encodes an Ada fixed-point type, return a pointer to the
17823 "XF" suffix of the name. The text after this is what encodes the
17824 'small and 'delta information. Otherwise, return nullptr. */
17825
17826static const char *
17827gnat_encoded_fixed_point_type_info (const char *name)
17828{
17829 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17830}
17831
9b790ce7
UW
17832/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17833 (which may be different from NAME) to the architecture back-end to allow
17834 it to guess the correct format if necessary. */
17835
17836static struct type *
17837dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17838 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17839{
08feed99 17840 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17841 const struct floatformat **format;
17842 struct type *type;
17843
17844 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17845 if (format)
103a685e 17846 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17847 else
77b7c781 17848 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17849
17850 return type;
17851}
17852
eb77c9df
AB
17853/* Allocate an integer type of size BITS and name NAME. */
17854
17855static struct type *
17856dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17857 int bits, int unsigned_p, const char *name)
17858{
17859 struct type *type;
17860
17861 /* Versions of Intel's C Compiler generate an integer type called "void"
17862 instead of using DW_TAG_unspecified_type. This has been seen on
17863 at least versions 14, 17, and 18. */
35ee2dc2
AB
17864 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17865 && strcmp (name, "void") == 0)
eb77c9df
AB
17866 type = objfile_type (objfile)->builtin_void;
17867 else
17868 type = init_integer_type (objfile, bits, unsigned_p, name);
17869
17870 return type;
17871}
17872
09584414
JB
17873/* Return true if DIE has a DW_AT_small attribute whose value is
17874 a constant rational, where both the numerator and denominator
17875 are equal to zero.
17876
17877 CU is the DIE's Compilation Unit. */
17878
17879static bool
17880has_zero_over_zero_small_attribute (struct die_info *die,
17881 struct dwarf2_cu *cu)
17882{
17883 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
17884 if (attr == nullptr)
17885 return false;
17886
17887 struct dwarf2_cu *scale_cu = cu;
17888 struct die_info *scale_die
17889 = follow_die_ref (die, attr, &scale_cu);
17890
17891 if (scale_die->tag != DW_TAG_constant)
17892 return false;
17893
5cde1d82 17894 gdb_mpz num (1), denom (1);
09584414 17895 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 17896 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
17897}
17898
8bdc1658
AB
17899/* Initialise and return a floating point type of size BITS suitable for
17900 use as a component of a complex number. The NAME_HINT is passed through
17901 when initialising the floating point type and is the name of the complex
17902 type.
17903
17904 As DWARF doesn't currently provide an explicit name for the components
17905 of a complex number, but it can be helpful to have these components
17906 named, we try to select a suitable name based on the size of the
17907 component. */
17908static struct type *
17909dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17910 struct objfile *objfile,
103a685e
TT
17911 int bits, const char *name_hint,
17912 enum bfd_endian byte_order)
8bdc1658 17913{
08feed99 17914 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17915 struct type *tt = nullptr;
17916
35add35e
AB
17917 /* Try to find a suitable floating point builtin type of size BITS.
17918 We're going to use the name of this type as the name for the complex
17919 target type that we are about to create. */
1db455a7 17920 switch (cu->language)
8bdc1658 17921 {
1db455a7
AB
17922 case language_fortran:
17923 switch (bits)
17924 {
17925 case 32:
17926 tt = builtin_f_type (gdbarch)->builtin_real;
17927 break;
17928 case 64:
17929 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17930 break;
17931 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17932 case 128:
17933 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17934 break;
17935 }
8bdc1658 17936 break;
1db455a7
AB
17937 default:
17938 switch (bits)
17939 {
17940 case 32:
17941 tt = builtin_type (gdbarch)->builtin_float;
17942 break;
17943 case 64:
17944 tt = builtin_type (gdbarch)->builtin_double;
17945 break;
17946 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17947 case 128:
17948 tt = builtin_type (gdbarch)->builtin_long_double;
17949 break;
17950 }
8bdc1658
AB
17951 break;
17952 }
17953
35add35e
AB
17954 /* If the type we found doesn't match the size we were looking for, then
17955 pretend we didn't find a type at all, the complex target type we
17956 create will then be nameless. */
a12e5744 17957 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17958 tt = nullptr;
17959
7d93a1e0 17960 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17961 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17962}
17963
c906108c
SS
17964/* Find a representation of a given base type and install
17965 it in the TYPE field of the die. */
17966
f792889a 17967static struct type *
e7c27a73 17968read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17969{
5e22e966 17970 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17971 struct type *type;
17972 struct attribute *attr;
19f392bc 17973 int encoding = 0, bits = 0;
15d034d0 17974 const char *name;
34877895 17975 gdbarch *arch;
c906108c 17976
e142c38c 17977 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
17978 if (attr != nullptr && attr->form_is_constant ())
17979 encoding = attr->constant_value (0);
e142c38c 17980 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17981 if (attr != nullptr)
529908cb 17982 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 17983 name = dwarf2_name (die, cu);
6ccb9162 17984 if (!name)
34877895 17985 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17986
08feed99 17987 arch = objfile->arch ();
103a685e
TT
17988 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17989
34877895 17990 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 17991 if (attr != nullptr && attr->form_is_constant ())
103a685e 17992 {
529908cb 17993 int endianity = attr->constant_value (0);
103a685e
TT
17994
17995 switch (endianity)
17996 {
17997 case DW_END_big:
17998 byte_order = BFD_ENDIAN_BIG;
17999 break;
18000 case DW_END_little:
18001 byte_order = BFD_ENDIAN_LITTLE;
18002 break;
18003 default:
18004 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18005 break;
18006 }
18007 }
6ccb9162 18008
09584414
JB
18009 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18010 && cu->language == language_ada
18011 && has_zero_over_zero_small_attribute (die, cu))
18012 {
18013 /* brobecker/2018-02-24: This is a fixed point type for which
18014 the scaling factor is represented as fraction whose value
18015 does not make sense (zero divided by zero), so we should
18016 normally never see these. However, there is a small category
18017 of fixed point types for which GNAT is unable to provide
18018 the scaling factor via the standard DWARF mechanisms, and
18019 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18020 This is likely what this DIE is about. */
09584414
JB
18021 encoding = (encoding == DW_ATE_signed_fixed
18022 ? DW_ATE_signed
18023 : DW_ATE_unsigned);
18024 }
18025
bbcdf9ab
TT
18026 /* With GNAT encodings, fixed-point information will be encoded in
18027 the type name. Note that this can also occur with the above
18028 zero-over-zero case, which is why this is a separate "if" rather
18029 than an "else if". */
18030 const char *gnat_encoding_suffix = nullptr;
18031 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18032 && cu->language == language_ada
18033 && name != nullptr)
18034 {
18035 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18036 if (gnat_encoding_suffix != nullptr)
18037 {
18038 gdb_assert (startswith (gnat_encoding_suffix,
18039 GNAT_FIXED_POINT_SUFFIX));
18040 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18041 name, gnat_encoding_suffix - name);
18042 /* Use -1 here so that SUFFIX points at the "_" after the
18043 "XF". */
18044 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18045
18046 encoding = (encoding == DW_ATE_signed
18047 ? DW_ATE_signed_fixed
18048 : DW_ATE_unsigned_fixed);
18049 }
18050 }
18051
6ccb9162 18052 switch (encoding)
c906108c 18053 {
6ccb9162
UW
18054 case DW_ATE_address:
18055 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18056 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18057 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18058 break;
18059 case DW_ATE_boolean:
19f392bc 18060 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18061 break;
18062 case DW_ATE_complex_float:
103a685e
TT
18063 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18064 byte_order);
78134374 18065 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18066 {
18067 if (name == nullptr)
18068 {
18069 struct obstack *obstack
5e22e966 18070 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18071 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18072 nullptr);
18073 }
18074 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18075 }
18076 else
18077 type = init_complex_type (name, type);
6ccb9162
UW
18078 break;
18079 case DW_ATE_decimal_float:
19f392bc 18080 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18081 break;
18082 case DW_ATE_float:
103a685e 18083 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18084 break;
18085 case DW_ATE_signed:
eb77c9df 18086 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18087 break;
18088 case DW_ATE_unsigned:
3b2b8fea
TT
18089 if (cu->language == language_fortran
18090 && name
61012eef 18091 && startswith (name, "character("))
19f392bc
UW
18092 type = init_character_type (objfile, bits, 1, name);
18093 else
eb77c9df 18094 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18095 break;
18096 case DW_ATE_signed_char:
6e70227d 18097 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18098 || cu->language == language_pascal
18099 || cu->language == language_fortran)
19f392bc
UW
18100 type = init_character_type (objfile, bits, 0, name);
18101 else
eb77c9df 18102 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18103 break;
18104 case DW_ATE_unsigned_char:
868a0084 18105 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18106 || cu->language == language_pascal
c44af4eb
TT
18107 || cu->language == language_fortran
18108 || cu->language == language_rust)
19f392bc
UW
18109 type = init_character_type (objfile, bits, 1, name);
18110 else
eb77c9df 18111 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18112 break;
75079b2b 18113 case DW_ATE_UTF:
53e710ac 18114 {
53e710ac
PA
18115 if (bits == 16)
18116 type = builtin_type (arch)->builtin_char16;
18117 else if (bits == 32)
18118 type = builtin_type (arch)->builtin_char32;
18119 else
18120 {
b98664d3 18121 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18122 bits);
eb77c9df 18123 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18124 }
18125 return set_die_type (die, type, cu);
18126 }
75079b2b 18127 break;
09584414
JB
18128 case DW_ATE_signed_fixed:
18129 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18130 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18131 break;
18132 case DW_ATE_unsigned_fixed:
18133 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18134 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18135 break;
75079b2b 18136
6ccb9162 18137 default:
b98664d3 18138 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18139 dwarf_type_encoding_name (encoding));
77b7c781 18140 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18141 break;
c906108c 18142 }
6ccb9162 18143
0114d602 18144 if (name && strcmp (name, "char") == 0)
15152a54 18145 type->set_has_no_signedness (true);
0114d602 18146
2b4424c3
TT
18147 maybe_set_alignment (cu, die, type);
18148
db558e34 18149 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18150
20a5fcbd
TT
18151 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18152 {
18153 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18154 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18155 {
529908cb 18156 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18157 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18158 /* Only use the attributes if they make sense together. */
18159 if (attr == nullptr
529908cb
TT
18160 || (attr->as_unsigned () + real_bit_size
18161 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18162 {
18163 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18164 = real_bit_size;
18165 if (attr != nullptr)
18166 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18167 = attr->as_unsigned ();
20a5fcbd
TT
18168 }
18169 }
18170 }
18171
f792889a 18172 return set_die_type (die, type, cu);
c906108c
SS
18173}
18174
80180f79
SA
18175/* Parse dwarf attribute if it's a block, reference or constant and put the
18176 resulting value of the attribute into struct bound_prop.
18177 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18178
18179static int
18180attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18181 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18182 struct type *default_type)
80180f79
SA
18183{
18184 struct dwarf2_property_baton *baton;
5e22e966 18185 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18186 struct objfile *objfile = per_objfile->objfile;
18187 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18188
9a49df9d
AB
18189 gdb_assert (default_type != NULL);
18190
80180f79
SA
18191 if (attr == NULL || prop == NULL)
18192 return 0;
18193
4fc6c0d5 18194 if (attr->form_is_block ())
80180f79 18195 {
8d749320 18196 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18197 baton->property_type = default_type;
80180f79 18198 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18199 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18200
18201 struct dwarf_block *block = attr->as_block ();
18202 baton->locexpr.size = block->size;
18203 baton->locexpr.data = block->data;
216a7e6b
AB
18204 switch (attr->name)
18205 {
18206 case DW_AT_string_length:
18207 baton->locexpr.is_reference = true;
18208 break;
18209 default:
18210 baton->locexpr.is_reference = false;
18211 break;
18212 }
8c2e4e06
SM
18213
18214 prop->set_locexpr (baton);
18215 gdb_assert (prop->baton () != NULL);
80180f79 18216 }
cd6c91b4 18217 else if (attr->form_is_ref ())
80180f79
SA
18218 {
18219 struct dwarf2_cu *target_cu = cu;
18220 struct die_info *target_die;
18221 struct attribute *target_attr;
18222
18223 target_die = follow_die_ref (die, attr, &target_cu);
18224 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18225 if (target_attr == NULL)
18226 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18227 target_cu);
80180f79
SA
18228 if (target_attr == NULL)
18229 return 0;
18230
df25ebbd 18231 switch (target_attr->name)
80180f79 18232 {
df25ebbd 18233 case DW_AT_location:
cd6c91b4 18234 if (target_attr->form_is_section_offset ())
df25ebbd 18235 {
8d749320 18236 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18237 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18238 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18239 prop->set_loclist (baton);
18240 gdb_assert (prop->baton () != NULL);
df25ebbd 18241 }
4fc6c0d5 18242 else if (target_attr->form_is_block ())
df25ebbd 18243 {
8d749320 18244 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18245 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18246 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18247 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18248 struct dwarf_block *block = target_attr->as_block ();
18249 baton->locexpr.size = block->size;
18250 baton->locexpr.data = block->data;
9a49df9d 18251 baton->locexpr.is_reference = true;
8c2e4e06
SM
18252 prop->set_locexpr (baton);
18253 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18254 }
18255 else
18256 {
18257 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18258 "dynamic property");
18259 return 0;
18260 }
18261 break;
18262 case DW_AT_data_member_location:
18263 {
18264 LONGEST offset;
18265
18266 if (!handle_data_member_location (target_die, target_cu,
18267 &offset))
18268 return 0;
18269
8d749320 18270 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18271 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18272 target_cu);
df25ebbd
JB
18273 baton->offset_info.offset = offset;
18274 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18275 prop->set_addr_offset (baton);
df25ebbd
JB
18276 break;
18277 }
80180f79
SA
18278 }
18279 }
cd6c91b4 18280 else if (attr->form_is_constant ())
8c2e4e06 18281 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18282 else
18283 {
18284 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18285 dwarf2_name (die, cu));
18286 return 0;
18287 }
18288
18289 return 1;
18290}
18291
09ba997f 18292/* See read.h. */
9a49df9d 18293
09ba997f 18294struct type *
293e7e51 18295dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18296{
9a49df9d
AB
18297 struct type *int_type;
18298
18299 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18300#define TRY_TYPE(F) \
18301 int_type = (unsigned_p \
18302 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18303 : objfile_type (objfile)->builtin_ ## F); \
18304 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18305 return int_type
18306
18307 TRY_TYPE (char);
18308 TRY_TYPE (short);
18309 TRY_TYPE (int);
18310 TRY_TYPE (long);
18311 TRY_TYPE (long_long);
18312
18313#undef TRY_TYPE
18314
18315 gdb_assert_not_reached ("unable to find suitable integer type");
18316}
18317
b86352cf
AB
18318/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18319 present (which is valid) then compute the default type based on the
18320 compilation units address size. */
18321
18322static struct type *
18323read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18324{
18325 struct type *index_type = die_type (die, cu);
18326
18327 /* Dwarf-2 specifications explicitly allows to create subrange types
18328 without specifying a base type.
18329 In that case, the base type must be set to the type of
18330 the lower bound, upper bound or count, in that order, if any of these
18331 three attributes references an object that has a type.
18332 If no base type is found, the Dwarf-2 specifications say that
18333 a signed integer type of size equal to the size of an address should
18334 be used.
18335 For the following C code: `extern char gdb_int [];'
18336 GCC produces an empty range DIE.
18337 FIXME: muller/2010-05-28: Possible references to object for low bound,
18338 high bound or count are not yet handled by this code. */
78134374 18339 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18340 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18341
18342 return index_type;
18343}
18344
a02abb62
JB
18345/* Read the given DW_AT_subrange DIE. */
18346
f792889a 18347static struct type *
a02abb62
JB
18348read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18349{
4c9ad8c2 18350 struct type *base_type, *orig_base_type;
a02abb62
JB
18351 struct type *range_type;
18352 struct attribute *attr;
729efb13 18353 struct dynamic_prop low, high;
4fae6e18 18354 int low_default_is_valid;
c451ebe5 18355 int high_bound_is_count = 0;
15d034d0 18356 const char *name;
d359392f 18357 ULONGEST negative_mask;
e77813c8 18358
b86352cf
AB
18359 orig_base_type = read_subrange_index_type (die, cu);
18360
4c9ad8c2
TT
18361 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18362 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18363 creating the range type, but we use the result of check_typedef
18364 when examining properties of the type. */
18365 base_type = check_typedef (orig_base_type);
a02abb62 18366
7e314c57
JK
18367 /* The die_type call above may have already set the type for this DIE. */
18368 range_type = get_die_type (die, cu);
18369 if (range_type)
18370 return range_type;
18371
8c2e4e06 18372 high.set_const_val (0);
729efb13 18373
4fae6e18
JK
18374 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18375 omitting DW_AT_lower_bound. */
18376 switch (cu->language)
6e70227d 18377 {
4fae6e18
JK
18378 case language_c:
18379 case language_cplus:
8c2e4e06 18380 low.set_const_val (0);
4fae6e18
JK
18381 low_default_is_valid = 1;
18382 break;
18383 case language_fortran:
8c2e4e06 18384 low.set_const_val (1);
4fae6e18
JK
18385 low_default_is_valid = 1;
18386 break;
18387 case language_d:
4fae6e18 18388 case language_objc:
c44af4eb 18389 case language_rust:
8c2e4e06 18390 low.set_const_val (0);
4fae6e18
JK
18391 low_default_is_valid = (cu->header.version >= 4);
18392 break;
18393 case language_ada:
18394 case language_m2:
18395 case language_pascal:
8c2e4e06 18396 low.set_const_val (1);
4fae6e18
JK
18397 low_default_is_valid = (cu->header.version >= 4);
18398 break;
18399 default:
8c2e4e06 18400 low.set_const_val (0);
4fae6e18
JK
18401 low_default_is_valid = 0;
18402 break;
a02abb62
JB
18403 }
18404
e142c38c 18405 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18406 if (attr != nullptr)
9a49df9d 18407 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18408 else if (!low_default_is_valid)
b98664d3 18409 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18410 "- DIE at %s [in module %s]"),
18411 sect_offset_str (die->sect_off),
5e22e966 18412 objfile_name (cu->per_objfile->objfile));
a02abb62 18413
506f5c41
TV
18414 struct attribute *attr_ub, *attr_count;
18415 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18416 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18417 {
506f5c41 18418 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18419 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18420 {
c451ebe5 18421 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18422 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18423 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18424 else
18425 high_bound_is_count = 1;
c2ff108b 18426 }
506f5c41
TV
18427 else
18428 {
18429 if (attr_ub != NULL)
18430 complaint (_("Unresolved DW_AT_upper_bound "
18431 "- DIE at %s [in module %s]"),
18432 sect_offset_str (die->sect_off),
5e22e966 18433 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18434 if (attr_count != NULL)
18435 complaint (_("Unresolved DW_AT_count "
18436 "- DIE at %s [in module %s]"),
18437 sect_offset_str (die->sect_off),
5e22e966 18438 objfile_name (cu->per_objfile->objfile));
506f5c41 18439 }
e77813c8 18440 }
a02abb62 18441
4e962e74
TT
18442 LONGEST bias = 0;
18443 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18444 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18445 bias = bias_attr->constant_value (0);
4e962e74 18446
dbb9c2b1
JB
18447 /* Normally, the DWARF producers are expected to use a signed
18448 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18449 But this is unfortunately not always the case, as witnessed
18450 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18451 is used instead. To work around that ambiguity, we treat
18452 the bounds as signed, and thus sign-extend their values, when
18453 the base type is signed. */
6e70227d 18454 negative_mask =
d359392f 18455 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18456 if (low.kind () == PROP_CONST
c6d940a9 18457 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18458 low.set_const_val (low.const_val () | negative_mask);
18459 if (high.kind () == PROP_CONST
c6d940a9 18460 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18461 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18462
5bbd8269
AB
18463 /* Check for bit and byte strides. */
18464 struct dynamic_prop byte_stride_prop;
18465 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18466 if (attr_byte_stride != nullptr)
18467 {
293e7e51 18468 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18469 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18470 prop_type);
18471 }
18472
18473 struct dynamic_prop bit_stride_prop;
18474 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18475 if (attr_bit_stride != nullptr)
18476 {
18477 /* It only makes sense to have either a bit or byte stride. */
18478 if (attr_byte_stride != nullptr)
18479 {
18480 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18481 "- DIE at %s [in module %s]"),
18482 sect_offset_str (die->sect_off),
5e22e966 18483 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
18484 attr_bit_stride = nullptr;
18485 }
18486 else
18487 {
293e7e51 18488 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18489 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18490 prop_type);
18491 }
18492 }
18493
18494 if (attr_byte_stride != nullptr
18495 || attr_bit_stride != nullptr)
18496 {
18497 bool byte_stride_p = (attr_byte_stride != nullptr);
18498 struct dynamic_prop *stride
18499 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18500
18501 range_type
18502 = create_range_type_with_stride (NULL, orig_base_type, &low,
18503 &high, bias, stride, byte_stride_p);
18504 }
18505 else
18506 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18507
c451ebe5 18508 if (high_bound_is_count)
599088e3 18509 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 18510
c2ff108b
JK
18511 /* Ada expects an empty array on no boundary attributes. */
18512 if (attr == NULL && cu->language != language_ada)
8c2e4e06 18513 range_type->bounds ()->high.set_undefined ();
c2ff108b 18514
39cbfefa
DJ
18515 name = dwarf2_name (die, cu);
18516 if (name)
d0e39ea2 18517 range_type->set_name (name);
6e70227d 18518
e142c38c 18519 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18520 if (attr != nullptr)
529908cb 18521 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 18522
2b4424c3
TT
18523 maybe_set_alignment (cu, die, range_type);
18524
7e314c57
JK
18525 set_die_type (die, range_type, cu);
18526
18527 /* set_die_type should be already done. */
b4ba55a1
JB
18528 set_descriptive_type (range_type, die, cu);
18529
7e314c57 18530 return range_type;
a02abb62 18531}
6e70227d 18532
f792889a 18533static struct type *
81a17f79
JB
18534read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18535{
18536 struct type *type;
81a17f79 18537
5e22e966 18538 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 18539 type->set_name (dwarf2_name (die, cu));
81a17f79 18540
74a2f8ff 18541 /* In Ada, an unspecified type is typically used when the description
85102364 18542 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18543 such a type, we treat it as a stub, and try to resolve it later on,
18544 when needed. */
18545 if (cu->language == language_ada)
b4b73759 18546 type->set_is_stub (true);
74a2f8ff 18547
f792889a 18548 return set_die_type (die, type, cu);
81a17f79 18549}
a02abb62 18550
639d11d3
DC
18551/* Read a single die and all its descendents. Set the die's sibling
18552 field to NULL; set other fields in the die correctly, and set all
18553 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18554 location of the info_ptr after reading all of those dies. PARENT
18555 is the parent of the die in question. */
18556
18557static struct die_info *
dee91e82 18558read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18559 const gdb_byte *info_ptr,
18560 const gdb_byte **new_info_ptr,
dee91e82 18561 struct die_info *parent)
639d11d3
DC
18562{
18563 struct die_info *die;
d521ce57 18564 const gdb_byte *cur_ptr;
639d11d3 18565
3e225074 18566 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18567 if (die == NULL)
18568 {
18569 *new_info_ptr = cur_ptr;
18570 return NULL;
18571 }
93311388 18572 store_in_ref_table (die, reader->cu);
639d11d3 18573
3e225074 18574 if (die->has_children)
bf6af496 18575 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18576 else
18577 {
18578 die->child = NULL;
18579 *new_info_ptr = cur_ptr;
18580 }
18581
18582 die->sibling = NULL;
18583 die->parent = parent;
18584 return die;
18585}
18586
18587/* Read a die, all of its descendents, and all of its siblings; set
18588 all of the fields of all of the dies correctly. Arguments are as
18589 in read_die_and_children. */
18590
18591static struct die_info *
bf6af496 18592read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18593 const gdb_byte *info_ptr,
18594 const gdb_byte **new_info_ptr,
bf6af496 18595 struct die_info *parent)
639d11d3
DC
18596{
18597 struct die_info *first_die, *last_sibling;
d521ce57 18598 const gdb_byte *cur_ptr;
639d11d3 18599
c906108c 18600 cur_ptr = info_ptr;
639d11d3
DC
18601 first_die = last_sibling = NULL;
18602
18603 while (1)
c906108c 18604 {
639d11d3 18605 struct die_info *die
dee91e82 18606 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18607
1d325ec1 18608 if (die == NULL)
c906108c 18609 {
639d11d3
DC
18610 *new_info_ptr = cur_ptr;
18611 return first_die;
c906108c 18612 }
1d325ec1
DJ
18613
18614 if (!first_die)
18615 first_die = die;
c906108c 18616 else
1d325ec1
DJ
18617 last_sibling->sibling = die;
18618
18619 last_sibling = die;
c906108c 18620 }
c906108c
SS
18621}
18622
bf6af496
DE
18623/* Read a die, all of its descendents, and all of its siblings; set
18624 all of the fields of all of the dies correctly. Arguments are as
18625 in read_die_and_children.
18626 This the main entry point for reading a DIE and all its children. */
18627
18628static struct die_info *
18629read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18630 const gdb_byte *info_ptr,
18631 const gdb_byte **new_info_ptr,
bf6af496
DE
18632 struct die_info *parent)
18633{
18634 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18635 new_info_ptr, parent);
18636
b4f54984 18637 if (dwarf_die_debug)
bf6af496
DE
18638 {
18639 fprintf_unfiltered (gdb_stdlog,
18640 "Read die from %s@0x%x of %s:\n",
96b79293 18641 reader->die_section->get_name (),
bf6af496
DE
18642 (unsigned) (info_ptr - reader->die_section->buffer),
18643 bfd_get_filename (reader->abfd));
b4f54984 18644 dump_die (die, dwarf_die_debug);
bf6af496
DE
18645 }
18646
18647 return die;
18648}
18649
3019eac3
DE
18650/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18651 attributes.
18652 The caller is responsible for filling in the extra attributes
18653 and updating (*DIEP)->num_attrs.
18654 Set DIEP to point to a newly allocated die with its information,
3e225074 18655 except for its child, sibling, and parent fields. */
93311388 18656
d521ce57 18657static const gdb_byte *
3019eac3 18658read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18659 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18660 int num_extra_attrs)
93311388 18661{
b64f50a1 18662 unsigned int abbrev_number, bytes_read, i;
7c32eebb 18663 const struct abbrev_info *abbrev;
93311388
DE
18664 struct die_info *die;
18665 struct dwarf2_cu *cu = reader->cu;
18666 bfd *abfd = reader->abfd;
18667
9c541725 18668 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18669 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18670 info_ptr += bytes_read;
18671 if (!abbrev_number)
18672 {
18673 *diep = NULL;
93311388
DE
18674 return info_ptr;
18675 }
18676
685af9cd 18677 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18678 if (!abbrev)
348e048f
DE
18679 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18680 abbrev_number,
18681 bfd_get_filename (abfd));
18682
3019eac3 18683 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18684 die->sect_off = sect_off;
93311388
DE
18685 die->tag = abbrev->tag;
18686 die->abbrev = abbrev_number;
3e225074 18687 die->has_children = abbrev->has_children;
93311388 18688
3019eac3
DE
18689 /* Make the result usable.
18690 The caller needs to update num_attrs after adding the extra
18691 attributes. */
93311388
DE
18692 die->num_attrs = abbrev->num_attrs;
18693
7a5f294d 18694 bool any_need_reprocess = false;
93311388 18695 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 18696 {
7a5f294d
TT
18697 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18698 info_ptr);
18699 if (die->attrs[i].requires_reprocessing_p ())
18700 any_need_reprocess = true;
18a8505e
AT
18701 }
18702
052c8bb8 18703 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
18704 if (attr != nullptr && attr->form_is_unsigned ())
18705 cu->str_offsets_base = attr->as_unsigned ();
93311388 18706
41144253 18707 attr = die->attr (DW_AT_loclists_base);
18708 if (attr != nullptr)
529908cb 18709 cu->loclist_base = attr->as_unsigned ();
41144253 18710
a39fdb41 18711 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18712 if (maybe_addr_base.has_value ())
18713 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
18714
18715 attr = die->attr (DW_AT_rnglists_base);
18716 if (attr != nullptr)
2b0c7f41 18717 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 18718
7a5f294d
TT
18719 if (any_need_reprocess)
18720 {
18721 for (i = 0; i < abbrev->num_attrs; ++i)
18722 {
18723 if (die->attrs[i].requires_reprocessing_p ())
18724 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18725 }
18726 }
93311388 18727 *diep = die;
93311388
DE
18728 return info_ptr;
18729}
18730
3019eac3
DE
18731/* Read a die and all its attributes.
18732 Set DIEP to point to a newly allocated die with its information,
3e225074 18733 except for its child, sibling, and parent fields. */
3019eac3 18734
d521ce57 18735static const gdb_byte *
3019eac3 18736read_full_die (const struct die_reader_specs *reader,
3e225074 18737 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18738{
d521ce57 18739 const gdb_byte *result;
bf6af496 18740
3e225074 18741 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18742
b4f54984 18743 if (dwarf_die_debug)
bf6af496
DE
18744 {
18745 fprintf_unfiltered (gdb_stdlog,
18746 "Read die from %s@0x%x of %s:\n",
96b79293 18747 reader->die_section->get_name (),
bf6af496
DE
18748 (unsigned) (info_ptr - reader->die_section->buffer),
18749 bfd_get_filename (reader->abfd));
b4f54984 18750 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18751 }
18752
18753 return result;
3019eac3 18754}
433df2d4 18755\f
c906108c 18756
72bf9492
DJ
18757/* Returns nonzero if TAG represents a type that we might generate a partial
18758 symbol for. */
18759
18760static int
d8f62e84 18761is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
18762{
18763 switch (tag)
18764 {
18765#if 0
18766 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
18767 that we don't at present. Note that normally this does not
18768 matter, mainly because C compilers don't give names to these
18769 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
18770 case DW_TAG_file_type:
18771 case DW_TAG_ptr_to_member_type:
18772 case DW_TAG_set_type:
18773 case DW_TAG_string_type:
18774 case DW_TAG_subroutine_type:
18775#endif
d8f62e84
TT
18776
18777 /* GNAT may emit an array with a name, but no typedef, so we
18778 need to make a symbol in this case. */
18779 case DW_TAG_array_type:
18780 return lang == language_ada;
18781
72bf9492
DJ
18782 case DW_TAG_base_type:
18783 case DW_TAG_class_type:
680b30c7 18784 case DW_TAG_interface_type:
72bf9492
DJ
18785 case DW_TAG_enumeration_type:
18786 case DW_TAG_structure_type:
18787 case DW_TAG_subrange_type:
18788 case DW_TAG_typedef:
18789 case DW_TAG_union_type:
18790 return 1;
18791 default:
18792 return 0;
18793 }
18794}
18795
18796/* Load all DIEs that are interesting for partial symbols into memory. */
18797
18798static struct partial_die_info *
dee91e82 18799load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18800 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18801{
dee91e82 18802 struct dwarf2_cu *cu = reader->cu;
5e22e966 18803 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18804 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18805 unsigned int bytes_read;
5afb4e99 18806 unsigned int load_all = 0;
72bf9492
DJ
18807 int nesting_level = 1;
18808
18809 parent_die = NULL;
18810 last_die = NULL;
18811
7adf1e79 18812 gdb_assert (cu->per_cu != NULL);
6dcd1193 18813 if (cu->load_all_dies)
5afb4e99
DJ
18814 load_all = 1;
18815
72bf9492
DJ
18816 cu->partial_dies
18817 = htab_create_alloc_ex (cu->header.length / 12,
18818 partial_die_hash,
18819 partial_die_eq,
18820 NULL,
18821 &cu->comp_unit_obstack,
18822 hashtab_obstack_allocate,
18823 dummy_obstack_deallocate);
18824
72bf9492
DJ
18825 while (1)
18826 {
7c32eebb
TT
18827 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18828 &bytes_read);
72bf9492
DJ
18829
18830 /* A NULL abbrev means the end of a series of children. */
18831 if (abbrev == NULL)
18832 {
18833 if (--nesting_level == 0)
cd9983dd
YQ
18834 return first_die;
18835
72bf9492
DJ
18836 info_ptr += bytes_read;
18837 last_die = parent_die;
18838 parent_die = parent_die->die_parent;
18839 continue;
18840 }
18841
98bfdba5
PA
18842 /* Check for template arguments. We never save these; if
18843 they're seen, we just mark the parent, and go on our way. */
18844 if (parent_die != NULL
18845 && cu->language == language_cplus
18846 && (abbrev->tag == DW_TAG_template_type_param
18847 || abbrev->tag == DW_TAG_template_value_param))
18848 {
18849 parent_die->has_template_arguments = 1;
18850
18851 if (!load_all)
18852 {
18853 /* We don't need a partial DIE for the template argument. */
dee91e82 18854 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18855 continue;
18856 }
18857 }
18858
0d99eb77 18859 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18860 Skip their other children. */
18861 if (!load_all
18862 && cu->language == language_cplus
18863 && parent_die != NULL
f9b5d5ea
TV
18864 && parent_die->tag == DW_TAG_subprogram
18865 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 18866 {
dee91e82 18867 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18868 continue;
18869 }
18870
5afb4e99
DJ
18871 /* Check whether this DIE is interesting enough to save. Normally
18872 we would not be interested in members here, but there may be
18873 later variables referencing them via DW_AT_specification (for
18874 static members). */
18875 if (!load_all
d8f62e84 18876 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 18877 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18878 && abbrev->tag != DW_TAG_enumerator
18879 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18880 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18881 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18882 && abbrev->tag != DW_TAG_variable
5afb4e99 18883 && abbrev->tag != DW_TAG_namespace
f55ee35c 18884 && abbrev->tag != DW_TAG_module
95554aad 18885 && abbrev->tag != DW_TAG_member
74921315
KS
18886 && abbrev->tag != DW_TAG_imported_unit
18887 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18888 {
18889 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18890 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18891 continue;
18892 }
18893
6f06d47b
YQ
18894 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18895 abbrev);
cd9983dd 18896
48fbe735 18897 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18898
18899 /* This two-pass algorithm for processing partial symbols has a
18900 high cost in cache pressure. Thus, handle some simple cases
18901 here which cover the majority of C partial symbols. DIEs
18902 which neither have specification tags in them, nor could have
18903 specification tags elsewhere pointing at them, can simply be
18904 processed and discarded.
18905
18906 This segment is also optional; scan_partial_symbols and
18907 add_partial_symbol will handle these DIEs if we chain
18908 them in normally. When compilers which do not emit large
18909 quantities of duplicate debug information are more common,
18910 this code can probably be removed. */
18911
18912 /* Any complete simple types at the top level (pretty much all
18913 of them, for a language without namespaces), can be processed
18914 directly. */
18915 if (parent_die == NULL
cd9983dd
YQ
18916 && pdi.has_specification == 0
18917 && pdi.is_declaration == 0
18918 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18919 || pdi.tag == DW_TAG_base_type
d8f62e84 18920 || pdi.tag == DW_TAG_array_type
cd9983dd 18921 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18922 {
7d00ffec 18923 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
18924 add_partial_symbol (&pdi, cu);
18925
cd9983dd 18926 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18927 continue;
18928 }
18929
d8228535
JK
18930 /* The exception for DW_TAG_typedef with has_children above is
18931 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18932 type_name_or_error will error on such types later.
d8228535
JK
18933
18934 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18935 it could not find the child DIEs referenced later, this is checked
18936 above. In correct DWARF DW_TAG_typedef should have no children. */
18937
cd9983dd 18938 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18939 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18940 "- DIE at %s [in module %s]"),
cd9983dd 18941 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18942
72bf9492
DJ
18943 /* If we're at the second level, and we're an enumerator, and
18944 our parent has no specification (meaning possibly lives in a
18945 namespace elsewhere), then we can add the partial symbol now
18946 instead of queueing it. */
cd9983dd 18947 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18948 && parent_die != NULL
18949 && parent_die->die_parent == NULL
18950 && parent_die->tag == DW_TAG_enumeration_type
18951 && parent_die->has_specification == 0)
18952 {
7d00ffec 18953 if (pdi.raw_name == NULL)
b98664d3 18954 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18955 else if (building_psymtab)
f0fbb768 18956 add_partial_symbol (&pdi, cu);
72bf9492 18957
cd9983dd 18958 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18959 continue;
18960 }
18961
cd9983dd 18962 struct partial_die_info *part_die
6f06d47b 18963 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18964
72bf9492
DJ
18965 /* We'll save this DIE so link it in. */
18966 part_die->die_parent = parent_die;
18967 part_die->die_sibling = NULL;
18968 part_die->die_child = NULL;
18969
18970 if (last_die && last_die == parent_die)
18971 last_die->die_child = part_die;
18972 else if (last_die)
18973 last_die->die_sibling = part_die;
18974
18975 last_die = part_die;
18976
18977 if (first_die == NULL)
18978 first_die = part_die;
18979
18980 /* Maybe add the DIE to the hash table. Not all DIEs that we
18981 find interesting need to be in the hash table, because we
18982 also have the parent/sibling/child chains; only those that we
18983 might refer to by offset later during partial symbol reading.
18984
18985 For now this means things that might have be the target of a
18986 DW_AT_specification, DW_AT_abstract_origin, or
18987 DW_AT_extension. DW_AT_extension will refer only to
18988 namespaces; DW_AT_abstract_origin refers to functions (and
18989 many things under the function DIE, but we do not recurse
18990 into function DIEs during partial symbol reading) and
18991 possibly variables as well; DW_AT_specification refers to
18992 declarations. Declarations ought to have the DW_AT_declaration
18993 flag. It happens that GCC forgets to put it in sometimes, but
18994 only for functions, not for types.
18995
18996 Adding more things than necessary to the hash table is harmless
18997 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18998 wasted time in find_partial_die, when we reread the compilation
18999 unit with load_all_dies set. */
72bf9492 19000
5afb4e99 19001 if (load_all
72929c62 19002 || abbrev->tag == DW_TAG_constant
5afb4e99 19003 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19004 || abbrev->tag == DW_TAG_variable
19005 || abbrev->tag == DW_TAG_namespace
19006 || part_die->is_declaration)
19007 {
19008 void **slot;
19009
19010 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19011 to_underlying (part_die->sect_off),
19012 INSERT);
72bf9492
DJ
19013 *slot = part_die;
19014 }
19015
72bf9492 19016 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19017 we have no reason to follow the children of structures; for other
98bfdba5
PA
19018 languages we have to, so that we can get at method physnames
19019 to infer fully qualified class names, for DW_AT_specification,
19020 and for C++ template arguments. For C++, we also look one level
19021 inside functions to find template arguments (if the name of the
19022 function does not already contain the template arguments).
bc30ff58 19023
0a4b0913
AB
19024 For Ada and Fortran, we need to scan the children of subprograms
19025 and lexical blocks as well because these languages allow the
19026 definition of nested entities that could be interesting for the
19027 debugger, such as nested subprograms for instance. */
72bf9492 19028 if (last_die->has_children
5afb4e99
DJ
19029 && (load_all
19030 || last_die->tag == DW_TAG_namespace
f55ee35c 19031 || last_die->tag == DW_TAG_module
72bf9492 19032 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19033 || (cu->language == language_cplus
19034 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19035 && (last_die->raw_name == NULL
19036 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19037 || (cu->language != language_c
19038 && (last_die->tag == DW_TAG_class_type
680b30c7 19039 || last_die->tag == DW_TAG_interface_type
72bf9492 19040 || last_die->tag == DW_TAG_structure_type
bc30ff58 19041 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19042 || ((cu->language == language_ada
19043 || cu->language == language_fortran)
bc30ff58
JB
19044 && (last_die->tag == DW_TAG_subprogram
19045 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19046 {
19047 nesting_level++;
19048 parent_die = last_die;
19049 continue;
19050 }
19051
19052 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19053 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19054
19055 /* Back to the top, do it again. */
19056 }
19057}
19058
6f06d47b 19059partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19060 const struct abbrev_info *abbrev)
6f06d47b
YQ
19061 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19062{
19063}
19064
7d00ffec
TT
19065/* See class definition. */
19066
19067const char *
19068partial_die_info::name (dwarf2_cu *cu)
19069{
19070 if (!canonical_name && raw_name != nullptr)
19071 {
19072 struct objfile *objfile = cu->per_objfile->objfile;
19073 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19074 canonical_name = 1;
19075 }
19076
19077 return raw_name;
19078}
19079
35cc7ed7
YQ
19080/* Read a minimal amount of information into the minimal die structure.
19081 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19082
48fbe735
YQ
19083const gdb_byte *
19084partial_die_info::read (const struct die_reader_specs *reader,
19085 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19086{
dee91e82 19087 struct dwarf2_cu *cu = reader->cu;
976ca316 19088 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19089 unsigned int i;
c5aa993b 19090 int has_low_pc_attr = 0;
c906108c 19091 int has_high_pc_attr = 0;
91da1414 19092 int high_pc_relative = 0;
c906108c 19093
fd0a254f 19094 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19095 {
e7da7f8f 19096 attribute attr;
7a5f294d 19097 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19098 /* String and address offsets that need to do the reprocessing have
dda83cd7 19099 already been read at this point, so there is no need to wait until
18a8505e 19100 the loop terminates to do the reprocessing. */
7a5f294d 19101 if (attr.requires_reprocessing_p ())
d0ce17d8 19102 read_attribute_reprocess (reader, &attr, tag);
c906108c 19103 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19104 partial symbol table. */
c906108c
SS
19105 switch (attr.name)
19106 {
19107 case DW_AT_name:
48fbe735 19108 switch (tag)
71c25dea
TT
19109 {
19110 case DW_TAG_compile_unit:
95554aad 19111 case DW_TAG_partial_unit:
348e048f 19112 case DW_TAG_type_unit:
71c25dea
TT
19113 /* Compilation units have a DW_AT_name that is a filename, not
19114 a source language identifier. */
19115 case DW_TAG_enumeration_type:
19116 case DW_TAG_enumerator:
19117 /* These tags always have simple identifiers already; no need
19118 to canonicalize them. */
7d00ffec 19119 canonical_name = 1;
2c830f54 19120 raw_name = attr.as_string ();
71c25dea
TT
19121 break;
19122 default:
7d00ffec 19123 canonical_name = 0;
2c830f54 19124 raw_name = attr.as_string ();
71c25dea
TT
19125 break;
19126 }
c906108c 19127 break;
31ef98ae 19128 case DW_AT_linkage_name:
c906108c 19129 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19130 /* Note that both forms of linkage name might appear. We
19131 assume they will be the same, and we only store the last
19132 one we see. */
95f982e5 19133 linkage_name = attr.as_string ();
c906108c
SS
19134 break;
19135 case DW_AT_low_pc:
19136 has_low_pc_attr = 1;
95f982e5 19137 lowpc = attr.as_address ();
c906108c
SS
19138 break;
19139 case DW_AT_high_pc:
19140 has_high_pc_attr = 1;
95f982e5 19141 highpc = attr.as_address ();
cd6c91b4 19142 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19143 high_pc_relative = 1;
c906108c
SS
19144 break;
19145 case DW_AT_location:
dda83cd7
SM
19146 /* Support the .debug_loc offsets. */
19147 if (attr.form_is_block ())
19148 {
9d2246fc 19149 d.locdesc = attr.as_block ();
dda83cd7
SM
19150 }
19151 else if (attr.form_is_section_offset ())
19152 {
4d3c2250 19153 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19154 }
19155 else
19156 {
4d3c2250
KB
19157 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19158 "partial symbol information");
dda83cd7 19159 }
c906108c 19160 break;
c906108c 19161 case DW_AT_external:
c45bc3f8 19162 is_external = attr.as_boolean ();
c906108c
SS
19163 break;
19164 case DW_AT_declaration:
c45bc3f8 19165 is_declaration = attr.as_boolean ();
c906108c
SS
19166 break;
19167 case DW_AT_type:
48fbe735 19168 has_type = 1;
c906108c
SS
19169 break;
19170 case DW_AT_abstract_origin:
19171 case DW_AT_specification:
72bf9492 19172 case DW_AT_extension:
48fbe735 19173 has_specification = 1;
0826b30a 19174 spec_offset = attr.get_ref_die_offset ();
48fbe735 19175 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19176 || cu->per_cu->is_dwz);
c906108c
SS
19177 break;
19178 case DW_AT_sibling:
19179 /* Ignore absolute siblings, they might point outside of
19180 the current compile unit. */
19181 if (attr.form == DW_FORM_ref_addr)
b98664d3 19182 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19183 else
b9502d3f 19184 {
48fbe735 19185 const gdb_byte *buffer = reader->buffer;
0826b30a 19186 sect_offset off = attr.get_ref_die_offset ();
9c541725 19187 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19188
19189 if (sibling_ptr < info_ptr)
b98664d3 19190 complaint (_("DW_AT_sibling points backwards"));
22869d73 19191 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19192 reader->die_section->overflow_complaint ();
b9502d3f 19193 else
48fbe735 19194 sibling = sibling_ptr;
b9502d3f 19195 }
c906108c 19196 break;
dda83cd7
SM
19197 case DW_AT_byte_size:
19198 has_byte_size = 1;
19199 break;
19200 case DW_AT_const_value:
19201 has_const_value = 1;
19202 break;
68511cec
CES
19203 case DW_AT_calling_convention:
19204 /* DWARF doesn't provide a way to identify a program's source-level
19205 entry point. DW_AT_calling_convention attributes are only meant
19206 to describe functions' calling conventions.
19207
19208 However, because it's a necessary piece of information in
0c1b455e
TT
19209 Fortran, and before DWARF 4 DW_CC_program was the only
19210 piece of debugging information whose definition refers to
19211 a 'main program' at all, several compilers marked Fortran
19212 main programs with DW_CC_program --- even when those
19213 functions use the standard calling conventions.
19214
19215 Although DWARF now specifies a way to provide this
19216 information, we support this practice for backward
19217 compatibility. */
529908cb 19218 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19219 && cu->language == language_fortran)
48fbe735 19220 main_subprogram = 1;
68511cec 19221 break;
481860b3 19222 case DW_AT_inline:
529908cb
TT
19223 {
19224 LONGEST value = attr.constant_value (-1);
19225 if (value == DW_INL_inlined
19226 || value == DW_INL_declared_inlined)
19227 may_be_inlined = 1;
19228 }
481860b3 19229 break;
95554aad
TT
19230
19231 case DW_AT_import:
48fbe735 19232 if (tag == DW_TAG_imported_unit)
36586728 19233 {
0826b30a 19234 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19235 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19236 || cu->per_cu->is_dwz);
19237 }
95554aad
TT
19238 break;
19239
0c1b455e 19240 case DW_AT_main_subprogram:
c45bc3f8 19241 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19242 break;
19243
05caa1d2
TT
19244 case DW_AT_ranges:
19245 {
2b0c7f41
SM
19246 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19247 on DWARF version). */
19248 ULONGEST ranges_offset = attr.as_unsigned ();
19249
19250 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19251 this value. */
19252 if (tag != DW_TAG_compile_unit)
19253 ranges_offset += cu->gnu_ranges_base;
19254
05caa1d2 19255 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19256 nullptr, tag))
05caa1d2
TT
19257 has_pc_info = 1;
19258 }
19259 break;
19260
c906108c
SS
19261 default:
19262 break;
19263 }
19264 }
19265
10d06d82
TT
19266 /* For Ada, if both the name and the linkage name appear, we prefer
19267 the latter. This lets "catch exception" work better, regardless
19268 of the order in which the name and linkage name were emitted.
19269 Really, though, this is just a workaround for the fact that gdb
19270 doesn't store both the name and the linkage name. */
19271 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19272 raw_name = linkage_name;
10d06d82 19273
91da1414 19274 if (high_pc_relative)
48fbe735 19275 highpc += lowpc;
91da1414 19276
9373cf26
JK
19277 if (has_low_pc_attr && has_high_pc_attr)
19278 {
19279 /* When using the GNU linker, .gnu.linkonce. sections are used to
19280 eliminate duplicate copies of functions and vtables and such.
19281 The linker will arbitrarily choose one and discard the others.
19282 The AT_*_pc values for such functions refer to local labels in
19283 these sections. If the section from that file was discarded, the
19284 labels are not in the output, so the relocs get a value of 0.
19285 If this is a discarded function, mark the pc bounds as invalid,
19286 so that GDB will ignore it. */
976ca316 19287 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19288 {
976ca316 19289 struct objfile *objfile = per_objfile->objfile;
08feed99 19290 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19291
b98664d3 19292 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19293 "for DIE at %s [in module %s]"),
48fbe735
YQ
19294 paddress (gdbarch, lowpc),
19295 sect_offset_str (sect_off),
9d8780f0 19296 objfile_name (objfile));
9373cf26
JK
19297 }
19298 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19299 else if (lowpc >= highpc)
9373cf26 19300 {
976ca316 19301 struct objfile *objfile = per_objfile->objfile;
08feed99 19302 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19303
b98664d3 19304 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19305 "for DIE at %s [in module %s]"),
48fbe735
YQ
19306 paddress (gdbarch, lowpc),
19307 paddress (gdbarch, highpc),
19308 sect_offset_str (sect_off),
9c541725 19309 objfile_name (objfile));
9373cf26
JK
19310 }
19311 else
48fbe735 19312 has_pc_info = 1;
9373cf26 19313 }
85cbf3d3 19314
c906108c
SS
19315 return info_ptr;
19316}
19317
72bf9492
DJ
19318/* Find a cached partial DIE at OFFSET in CU. */
19319
d590ff25
YQ
19320struct partial_die_info *
19321dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19322{
19323 struct partial_die_info *lookup_die = NULL;
6f06d47b 19324 struct partial_die_info part_die (sect_off);
72bf9492 19325
9a3c8263 19326 lookup_die = ((struct partial_die_info *)
d590ff25 19327 htab_find_with_hash (partial_dies, &part_die,
9c541725 19328 to_underlying (sect_off)));
72bf9492 19329
72bf9492
DJ
19330 return lookup_die;
19331}
19332
348e048f
DE
19333/* Find a partial DIE at OFFSET, which may or may not be in CU,
19334 except in the case of .debug_types DIEs which do not reference
19335 outside their CU (they do however referencing other types via
55f1336d 19336 DW_FORM_ref_sig8). */
72bf9492 19337
122cf0f2 19338static const struct cu_partial_die_info
9c541725 19339find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19340{
976ca316
SM
19341 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19342 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19343 struct partial_die_info *pd = NULL;
72bf9492 19344
36586728 19345 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19346 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19347 {
d590ff25 19348 pd = cu->find_partial_die (sect_off);
5afb4e99 19349 if (pd != NULL)
fb816e8b 19350 return { cu, pd };
0d99eb77
DE
19351 /* We missed recording what we needed.
19352 Load all dies and try again. */
5afb4e99 19353 }
0d99eb77
DE
19354 else
19355 {
19356 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19357 if (cu->per_cu->is_debug_types)
0d99eb77 19358 {
9d8780f0
SM
19359 error (_("Dwarf Error: Type Unit at offset %s contains"
19360 " external reference to offset %s [in module %s].\n"),
19361 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19362 bfd_get_filename (objfile->obfd));
19363 }
7188ed02
SM
19364 dwarf2_per_cu_data *per_cu
19365 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19366 per_objfile);
72bf9492 19367
976ca316 19368 cu = per_objfile->get_cu (per_cu);
7188ed02 19369 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19370 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19371
976ca316 19372 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19373
19374 cu->last_used = 0;
19375 pd = cu->find_partial_die (sect_off);
0d99eb77 19376 }
5afb4e99 19377
dee91e82
DE
19378 /* If we didn't find it, and not all dies have been loaded,
19379 load them all and try again. */
19380
6dcd1193 19381 if (pd == NULL && cu->load_all_dies == 0)
5afb4e99 19382 {
6dcd1193 19383 cu->load_all_dies = 1;
fd820528
DE
19384
19385 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19386 THIS_CU->cu may already be in use. So we can't just free it and
19387 replace its DIEs with the ones we read in. Instead, we leave those
19388 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19389 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19390 set. */
976ca316 19391 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19392
7188ed02 19393 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19394 }
19395
19396 if (pd == NULL)
2152b4fd 19397 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
9d8780f0 19398 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19399 return { cu, pd };
72bf9492
DJ
19400}
19401
abc72ce4
DE
19402/* See if we can figure out if the class lives in a namespace. We do
19403 this by looking for a member function; its demangled name will
19404 contain namespace info, if there is any. */
19405
19406static void
19407guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19408 struct dwarf2_cu *cu)
19409{
19410 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19411 what template types look like, because the demangler
19412 frequently doesn't give the same name as the debug info. We
19413 could fix this by only using the demangled name to get the
19414 prefix (but see comment in read_structure_type). */
19415
19416 struct partial_die_info *real_pdi;
19417 struct partial_die_info *child_pdi;
19418
19419 /* If this DIE (this DIE's specification, if any) has a parent, then
19420 we should not do this. We'll prepend the parent's fully qualified
19421 name when we create the partial symbol. */
19422
19423 real_pdi = struct_pdi;
19424 while (real_pdi->has_specification)
fb816e8b 19425 {
122cf0f2
AB
19426 auto res = find_partial_die (real_pdi->spec_offset,
19427 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19428 real_pdi = res.pdi;
19429 cu = res.cu;
19430 }
abc72ce4
DE
19431
19432 if (real_pdi->die_parent != NULL)
19433 return;
19434
19435 for (child_pdi = struct_pdi->die_child;
19436 child_pdi != NULL;
19437 child_pdi = child_pdi->die_sibling)
19438 {
19439 if (child_pdi->tag == DW_TAG_subprogram
19440 && child_pdi->linkage_name != NULL)
19441 {
43816ebc 19442 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19443 (cu->language_defn->class_name_from_physname
19444 (child_pdi->linkage_name));
abc72ce4
DE
19445 if (actual_class_name != NULL)
19446 {
5e22e966 19447 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19448 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19449 struct_pdi->canonical_name = 1;
abc72ce4
DE
19450 }
19451 break;
19452 }
19453 }
19454}
19455
25c11aca
TV
19456/* Return true if a DIE with TAG may have the DW_AT_const_value
19457 attribute. */
19458
19459static bool
19460can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19461{
19462 switch (tag)
19463 {
19464 case DW_TAG_constant:
19465 case DW_TAG_enumerator:
19466 case DW_TAG_formal_parameter:
19467 case DW_TAG_template_value_param:
19468 case DW_TAG_variable:
19469 return true;
19470 }
19471
19472 return false;
19473}
19474
52356b79
YQ
19475void
19476partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19477{
abc72ce4
DE
19478 /* Once we've fixed up a die, there's no point in doing so again.
19479 This also avoids a memory leak if we were to call
19480 guess_partial_die_structure_name multiple times. */
52356b79 19481 if (fixup_called)
abc72ce4
DE
19482 return;
19483
72bf9492
DJ
19484 /* If we found a reference attribute and the DIE has no name, try
19485 to find a name in the referred to DIE. */
19486
7d00ffec 19487 if (raw_name == NULL && has_specification)
72bf9492
DJ
19488 {
19489 struct partial_die_info *spec_die;
72bf9492 19490
122cf0f2 19491 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19492 spec_die = res.pdi;
19493 cu = res.cu;
72bf9492 19494
52356b79 19495 spec_die->fixup (cu);
72bf9492 19496
7d00ffec 19497 if (spec_die->raw_name)
72bf9492 19498 {
7d00ffec
TT
19499 raw_name = spec_die->raw_name;
19500 canonical_name = spec_die->canonical_name;
72bf9492
DJ
19501
19502 /* Copy DW_AT_external attribute if it is set. */
19503 if (spec_die->is_external)
52356b79 19504 is_external = spec_die->is_external;
72bf9492
DJ
19505 }
19506 }
19507
25c11aca
TV
19508 if (!has_const_value && has_specification
19509 && can_have_DW_AT_const_value_p (tag))
19510 {
19511 struct partial_die_info *spec_die;
19512
19513 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19514 spec_die = res.pdi;
19515 cu = res.cu;
19516
19517 spec_die->fixup (cu);
19518
19519 if (spec_die->has_const_value)
19520 {
19521 /* Copy DW_AT_const_value attribute if it is set. */
19522 has_const_value = spec_die->has_const_value;
19523 }
19524 }
19525
72bf9492 19526 /* Set default names for some unnamed DIEs. */
72bf9492 19527
7d00ffec
TT
19528 if (raw_name == NULL && tag == DW_TAG_namespace)
19529 {
19530 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19531 canonical_name = 1;
19532 }
72bf9492 19533
abc72ce4
DE
19534 /* If there is no parent die to provide a namespace, and there are
19535 children, see if we can determine the namespace from their linkage
122d1940 19536 name. */
abc72ce4 19537 if (cu->language == language_cplus
5e22e966 19538 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
19539 && die_parent == NULL
19540 && has_children
19541 && (tag == DW_TAG_class_type
19542 || tag == DW_TAG_structure_type
19543 || tag == DW_TAG_union_type))
19544 guess_partial_die_structure_name (this, cu);
abc72ce4 19545
53832f31
TT
19546 /* GCC might emit a nameless struct or union that has a linkage
19547 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 19548 if (raw_name == NULL
52356b79
YQ
19549 && (tag == DW_TAG_class_type
19550 || tag == DW_TAG_interface_type
19551 || tag == DW_TAG_structure_type
19552 || tag == DW_TAG_union_type)
19553 && linkage_name != NULL)
53832f31 19554 {
43816ebc
TT
19555 gdb::unique_xmalloc_ptr<char> demangled
19556 (gdb_demangle (linkage_name, DMGL_TYPES));
19557 if (demangled != nullptr)
53832f31 19558 {
96408a79
SA
19559 const char *base;
19560
19561 /* Strip any leading namespaces/classes, keep only the base name.
19562 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19563 base = strrchr (demangled.get (), ':');
19564 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19565 base++;
19566 else
43816ebc 19567 base = demangled.get ();
96408a79 19568
5e22e966 19569 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19570 raw_name = objfile->intern (base);
19571 canonical_name = 1;
53832f31
TT
19572 }
19573 }
19574
52356b79 19575 fixup_called = 1;
72bf9492
DJ
19576}
19577
d0ce17d8 19578/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
19579 contents from the given SECTION in the HEADER.
19580
19581 HEADER_OFFSET is the offset of the header in the section. */
41144253 19582static void
d0ce17d8 19583read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
19584 struct dwarf2_section_info *section,
19585 sect_offset header_offset)
41144253 19586{
19587 unsigned int bytes_read;
19588 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
19589 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19590
41144253 19591 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19592 info_ptr += bytes_read;
a1c40103 19593
41144253 19594 header->version = read_2_bytes (abfd, info_ptr);
19595 info_ptr += 2;
a1c40103 19596
41144253 19597 header->addr_size = read_1_byte (abfd, info_ptr);
19598 info_ptr += 1;
a1c40103 19599
41144253 19600 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19601 info_ptr += 1;
a1c40103 19602
41144253 19603 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19604}
19605
19606/* Return the DW_AT_loclists_base value for the CU. */
19607static ULONGEST
19608lookup_loclist_base (struct dwarf2_cu *cu)
19609{
19610 /* For the .dwo unit, the loclist_base points to the first offset following
19611 the header. The header consists of the following entities-
19612 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19613 bit format)
19614 2. version (2 bytes)
19615 3. address size (1 byte)
19616 4. segment selector size (1 byte)
19617 5. offset entry count (4 bytes)
19618 These sizes are derived as per the DWARFv5 standard. */
19619 if (cu->dwo_unit != nullptr)
19620 {
19621 if (cu->header.initial_length_size == 4)
19622 return LOCLIST_HEADER_SIZE32;
19623 return LOCLIST_HEADER_SIZE64;
19624 }
19625 return cu->loclist_base;
19626}
19627
19628/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19629 array of offsets in the .debug_loclists section. */
e57933dc
SM
19630
19631static sect_offset
41144253 19632read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19633{
976ca316
SM
19634 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19635 struct objfile *objfile = per_objfile->objfile;
41144253 19636 bfd *abfd = objfile->obfd;
a1c40103
SM
19637 ULONGEST loclist_header_size =
19638 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19639 : LOCLIST_HEADER_SIZE64);
41144253 19640 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
19641
19642 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19643 ULONGEST start_offset =
19644 loclist_base + loclist_index * cu->header.offset_size;
19645
a1c40103 19646 /* Get loclists section. */
41144253 19647 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19648
a1c40103 19649 /* Read the loclists section content. */
41144253 19650 section->read (objfile);
19651 if (section->buffer == NULL)
a0c1eeba
SM
19652 error (_("DW_FORM_loclistx used without .debug_loclists "
19653 "section [in module %s]"), objfile_name (objfile));
19654
a1c40103
SM
19655 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19656 so if loclist_base is smaller than the header size, we have a problem. */
19657 if (loclist_base < loclist_header_size)
19658 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19659 objfile_name (objfile));
19660
19661 /* Read the header of the loclists contribution. */
d0ce17d8 19662 struct loclists_rnglists_header header;
a1c40103
SM
19663 read_loclists_rnglists_header (&header, section,
19664 (sect_offset) (loclist_base - loclist_header_size));
19665
19666 /* Verify the loclist index is valid. */
41144253 19667 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
19668 error (_("DW_FORM_loclistx pointing outside of "
19669 ".debug_loclists offset array [in module %s]"),
19670 objfile_name (objfile));
19671
05787bad
SM
19672 /* Validate that reading won't go beyond the end of the section. */
19673 if (start_offset + cu->header.offset_size > section->size)
19674 error (_("Reading DW_FORM_loclistx index beyond end of"
19675 ".debug_loclists section [in module %s]"),
19676 objfile_name (objfile));
19677
19678 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 19679
19680 if (cu->header.offset_size == 4)
e57933dc 19681 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 19682 else
e57933dc 19683 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 19684}
19685
d0ce17d8
CT
19686/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19687 array of offsets in the .debug_rnglists section. */
e57933dc
SM
19688
19689static sect_offset
d0ce17d8
CT
19690read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19691 dwarf_tag tag)
19692{
19693 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19694 struct objfile *objfile = dwarf2_per_objfile->objfile;
19695 bfd *abfd = objfile->obfd;
19696 ULONGEST rnglist_header_size =
19697 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19698 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
19699
19700 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19701 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19702 doesn't apply. */
d0ce17d8 19703 ULONGEST rnglist_base =
2b0c7f41 19704 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
19705
19706 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
19707 ULONGEST start_offset =
19708 rnglist_base + rnglist_index * cu->header.offset_size;
19709
19710 /* Get rnglists section. */
19711 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19712
19713 /* Read the rnglists section content. */
19714 section->read (objfile);
19715 if (section->buffer == nullptr)
19716 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19717 "[in module %s]"),
19718 objfile_name (objfile));
19719
a1c40103
SM
19720 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19721 so if rnglist_base is smaller than the header size, we have a problem. */
19722 if (rnglist_base < rnglist_header_size)
19723 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19724 objfile_name (objfile));
19725
19726 /* Read the header of the rnglists contribution. */
d0ce17d8 19727 struct loclists_rnglists_header header;
a1c40103
SM
19728 read_loclists_rnglists_header (&header, section,
19729 (sect_offset) (rnglist_base - rnglist_header_size));
19730
19731 /* Verify the rnglist index is valid. */
d0ce17d8
CT
19732 if (rnglist_index >= header.offset_entry_count)
19733 error (_("DW_FORM_rnglistx index pointing outside of "
19734 ".debug_rnglists offset array [in module %s]"),
19735 objfile_name (objfile));
19736
d0ce17d8 19737 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 19738 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
19739 error (_("Reading DW_FORM_rnglistx index beyond end of"
19740 ".debug_rnglists section [in module %s]"),
19741 objfile_name (objfile));
19742
19743 const gdb_byte *info_ptr = section->buffer + start_offset;
19744
19745 if (cu->header.offset_size == 4)
e57933dc 19746 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 19747 else
e57933dc 19748 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
19749}
19750
18a8505e
AT
19751/* Process the attributes that had to be skipped in the first round. These
19752 attributes are the ones that need str_offsets_base or addr_base attributes.
19753 They could not have been processed in the first round, because at the time
19754 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19755static void
19756read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 19757 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
19758{
19759 struct dwarf2_cu *cu = reader->cu;
19760 switch (attr->form)
19761 {
19762 case DW_FORM_addrx:
19763 case DW_FORM_GNU_addr_index:
36d378cf
TT
19764 attr->set_address (read_addr_index (cu,
19765 attr->as_unsigned_reprocess ()));
dda83cd7 19766 break;
41144253 19767 case DW_FORM_loclistx:
b1829e1b 19768 {
e57933dc 19769 sect_offset loclists_sect_off
b1829e1b
SM
19770 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19771
e57933dc 19772 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
19773 }
19774 break;
d0ce17d8 19775 case DW_FORM_rnglistx:
b1829e1b 19776 {
e57933dc 19777 sect_offset rnglists_sect_off
b1829e1b
SM
19778 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19779
e57933dc 19780 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 19781 }
dda83cd7 19782 break;
18a8505e
AT
19783 case DW_FORM_strx:
19784 case DW_FORM_strx1:
19785 case DW_FORM_strx2:
19786 case DW_FORM_strx3:
19787 case DW_FORM_strx4:
19788 case DW_FORM_GNU_str_index:
19789 {
fe56917a 19790 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 19791 gdb_assert (!attr->canonical_string_p ());
18a8505e 19792 if (reader->dwo_file != NULL)
c6481205
TT
19793 attr->set_string_noncanonical (read_dwo_str_index (reader,
19794 str_index));
18a8505e 19795 else
c6481205
TT
19796 attr->set_string_noncanonical (read_stub_str_index (cu,
19797 str_index));
18a8505e
AT
19798 break;
19799 }
19800 default:
19801 gdb_assert_not_reached (_("Unexpected DWARF form."));
19802 }
19803}
19804
a8329558 19805/* Read an attribute value described by an attribute form. */
c906108c 19806
d521ce57 19807static const gdb_byte *
dee91e82
DE
19808read_attribute_value (const struct die_reader_specs *reader,
19809 struct attribute *attr, unsigned form,
7a5f294d 19810 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19811{
dee91e82 19812 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
19813 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19814 struct objfile *objfile = per_objfile->objfile;
dee91e82 19815 bfd *abfd = reader->abfd;
e7c27a73 19816 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19817 unsigned int bytes_read;
19818 struct dwarf_block *blk;
19819
aead7601 19820 attr->form = (enum dwarf_form) form;
a8329558 19821 switch (form)
c906108c 19822 {
c906108c 19823 case DW_FORM_ref_addr:
7c290a04
TT
19824 if (cu_header->version == 2)
19825 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 19826 &bytes_read));
ae411497 19827 else
7c290a04 19828 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19829 &bytes_read));
ae411497
TT
19830 info_ptr += bytes_read;
19831 break;
36586728 19832 case DW_FORM_GNU_ref_alt:
7c290a04 19833 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19834 &bytes_read));
36586728
TT
19835 info_ptr += bytes_read;
19836 break;
ae411497 19837 case DW_FORM_addr:
08feed99
TT
19838 {
19839 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 19840 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
19841 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19842 attr->set_address (addr);
08feed99
TT
19843 info_ptr += bytes_read;
19844 }
c906108c
SS
19845 break;
19846 case DW_FORM_block2:
7b5a2f43 19847 blk = dwarf_alloc_block (cu);
c906108c
SS
19848 blk->size = read_2_bytes (abfd, info_ptr);
19849 info_ptr += 2;
19850 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19851 info_ptr += blk->size;
9d2246fc 19852 attr->set_block (blk);
c906108c
SS
19853 break;
19854 case DW_FORM_block4:
7b5a2f43 19855 blk = dwarf_alloc_block (cu);
c906108c
SS
19856 blk->size = read_4_bytes (abfd, info_ptr);
19857 info_ptr += 4;
19858 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19859 info_ptr += blk->size;
9d2246fc 19860 attr->set_block (blk);
c906108c
SS
19861 break;
19862 case DW_FORM_data2:
414ad644 19863 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
19864 info_ptr += 2;
19865 break;
19866 case DW_FORM_data4:
414ad644 19867 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
19868 info_ptr += 4;
19869 break;
19870 case DW_FORM_data8:
414ad644 19871 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
19872 info_ptr += 8;
19873 break;
0224619f
JK
19874 case DW_FORM_data16:
19875 blk = dwarf_alloc_block (cu);
19876 blk->size = 16;
19877 blk->data = read_n_bytes (abfd, info_ptr, 16);
19878 info_ptr += 16;
9d2246fc 19879 attr->set_block (blk);
0224619f 19880 break;
2dc7f7b3 19881 case DW_FORM_sec_offset:
7c290a04 19882 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19883 &bytes_read));
2dc7f7b3
TT
19884 info_ptr += bytes_read;
19885 break;
41144253 19886 case DW_FORM_loclistx:
19887 {
7a5f294d
TT
19888 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19889 &bytes_read));
19890 info_ptr += bytes_read;
41144253 19891 }
19892 break;
c906108c 19893 case DW_FORM_string:
c6481205
TT
19894 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19895 &bytes_read));
c906108c
SS
19896 info_ptr += bytes_read;
19897 break;
4bdf3d34 19898 case DW_FORM_strp:
36586728
TT
19899 if (!cu->per_cu->is_dwz)
19900 {
c6481205
TT
19901 attr->set_string_noncanonical
19902 (read_indirect_string (per_objfile,
19903 abfd, info_ptr, cu_header,
19904 &bytes_read));
36586728
TT
19905 info_ptr += bytes_read;
19906 break;
19907 }
19908 /* FALLTHROUGH */
43988095
JK
19909 case DW_FORM_line_strp:
19910 if (!cu->per_cu->is_dwz)
19911 {
c6481205
TT
19912 attr->set_string_noncanonical
19913 (per_objfile->read_line_string (info_ptr, cu_header,
19914 &bytes_read));
43988095
JK
19915 info_ptr += bytes_read;
19916 break;
19917 }
19918 /* FALLTHROUGH */
36586728
TT
19919 case DW_FORM_GNU_strp_alt:
19920 {
a7308ce0 19921 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
19922 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19923 &bytes_read);
36586728 19924
c6481205
TT
19925 attr->set_string_noncanonical
19926 (dwz->read_string (objfile, str_offset));
36586728
TT
19927 info_ptr += bytes_read;
19928 }
4bdf3d34 19929 break;
2dc7f7b3 19930 case DW_FORM_exprloc:
c906108c 19931 case DW_FORM_block:
7b5a2f43 19932 blk = dwarf_alloc_block (cu);
c906108c
SS
19933 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19934 info_ptr += bytes_read;
19935 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19936 info_ptr += blk->size;
9d2246fc 19937 attr->set_block (blk);
c906108c
SS
19938 break;
19939 case DW_FORM_block1:
7b5a2f43 19940 blk = dwarf_alloc_block (cu);
c906108c
SS
19941 blk->size = read_1_byte (abfd, info_ptr);
19942 info_ptr += 1;
19943 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19944 info_ptr += blk->size;
9d2246fc 19945 attr->set_block (blk);
c906108c
SS
19946 break;
19947 case DW_FORM_data1:
c906108c 19948 case DW_FORM_flag:
414ad644 19949 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
19950 info_ptr += 1;
19951 break;
2dc7f7b3 19952 case DW_FORM_flag_present:
414ad644 19953 attr->set_unsigned (1);
2dc7f7b3 19954 break;
c906108c 19955 case DW_FORM_sdata:
1bc397c5 19956 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19957 info_ptr += bytes_read;
19958 break;
18a8505e 19959 case DW_FORM_rnglistx:
7a5f294d
TT
19960 {
19961 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19962 &bytes_read));
19963 info_ptr += bytes_read;
19964 }
19965 break;
d0ce17d8 19966 case DW_FORM_udata:
414ad644 19967 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19968 info_ptr += bytes_read;
19969 break;
19970 case DW_FORM_ref1:
7c290a04 19971 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 19972 + read_1_byte (abfd, info_ptr)));
c906108c
SS
19973 info_ptr += 1;
19974 break;
19975 case DW_FORM_ref2:
7c290a04 19976 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 19977 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
19978 info_ptr += 2;
19979 break;
19980 case DW_FORM_ref4:
7c290a04 19981 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 19982 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
19983 info_ptr += 4;
19984 break;
613e1657 19985 case DW_FORM_ref8:
7c290a04 19986 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 19987 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
19988 info_ptr += 8;
19989 break;
55f1336d 19990 case DW_FORM_ref_sig8:
630ed6b9 19991 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
19992 info_ptr += 8;
19993 break;
c906108c 19994 case DW_FORM_ref_udata:
7c290a04 19995 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
19996 + read_unsigned_leb128 (abfd, info_ptr,
19997 &bytes_read)));
c906108c
SS
19998 info_ptr += bytes_read;
19999 break;
c906108c 20000 case DW_FORM_indirect:
a8329558
KW
20001 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20002 info_ptr += bytes_read;
43988095
JK
20003 if (form == DW_FORM_implicit_const)
20004 {
20005 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20006 info_ptr += bytes_read;
20007 }
20008 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20009 info_ptr);
43988095
JK
20010 break;
20011 case DW_FORM_implicit_const:
1bc397c5 20012 attr->set_signed (implicit_const);
a8329558 20013 break;
336d760d 20014 case DW_FORM_addrx:
3019eac3 20015 case DW_FORM_GNU_addr_index:
fe56917a
TT
20016 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20017 &bytes_read));
3019eac3
DE
20018 info_ptr += bytes_read;
20019 break;
cf532bd1 20020 case DW_FORM_strx:
15f18d14
AT
20021 case DW_FORM_strx1:
20022 case DW_FORM_strx2:
20023 case DW_FORM_strx3:
20024 case DW_FORM_strx4:
3019eac3 20025 case DW_FORM_GNU_str_index:
3019eac3 20026 {
15f18d14
AT
20027 ULONGEST str_index;
20028 if (form == DW_FORM_strx1)
20029 {
20030 str_index = read_1_byte (abfd, info_ptr);
20031 info_ptr += 1;
20032 }
20033 else if (form == DW_FORM_strx2)
20034 {
20035 str_index = read_2_bytes (abfd, info_ptr);
20036 info_ptr += 2;
20037 }
20038 else if (form == DW_FORM_strx3)
20039 {
20040 str_index = read_3_bytes (abfd, info_ptr);
20041 info_ptr += 3;
20042 }
20043 else if (form == DW_FORM_strx4)
20044 {
20045 str_index = read_4_bytes (abfd, info_ptr);
20046 info_ptr += 4;
20047 }
20048 else
20049 {
20050 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20051 info_ptr += bytes_read;
20052 }
fe56917a 20053 attr->set_unsigned_reprocess (str_index);
7a5f294d 20054 }
3019eac3 20055 break;
c906108c 20056 default:
8a3fe4f8 20057 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20058 dwarf_form_name (form),
20059 bfd_get_filename (abfd));
c906108c 20060 }
28e94949 20061
36586728 20062 /* Super hack. */
cd6c91b4 20063 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20064 attr->form = DW_FORM_GNU_ref_alt;
20065
28e94949
JB
20066 /* We have seen instances where the compiler tried to emit a byte
20067 size attribute of -1 which ended up being encoded as an unsigned
20068 0xffffffff. Although 0xffffffff is technically a valid size value,
20069 an object of this size seems pretty unlikely so we can relatively
20070 safely treat these cases as if the size attribute was invalid and
20071 treat them as zero by default. */
20072 if (attr->name == DW_AT_byte_size
20073 && form == DW_FORM_data4
529908cb 20074 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20075 {
20076 complaint
dda83cd7
SM
20077 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20078 hex_string (attr->as_unsigned ()));
414ad644 20079 attr->set_unsigned (0);
01c66ae6 20080 }
28e94949 20081
c906108c
SS
20082 return info_ptr;
20083}
20084
a8329558
KW
20085/* Read an attribute described by an abbreviated attribute. */
20086
d521ce57 20087static const gdb_byte *
dee91e82 20088read_attribute (const struct die_reader_specs *reader,
4444f407 20089 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20090 const gdb_byte *info_ptr)
a8329558
KW
20091{
20092 attr->name = abbrev->name;
c6481205 20093 attr->string_is_canonical = 0;
fe56917a 20094 attr->requires_reprocessing = 0;
43988095 20095 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20096 abbrev->implicit_const, info_ptr);
a8329558
KW
20097}
20098
43988095
JK
20099/* Return pointer to string at .debug_str offset STR_OFFSET. */
20100
20101static const char *
976ca316 20102read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20103 LONGEST str_offset)
43988095 20104{
976ca316
SM
20105 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20106 str_offset, "DW_FORM_strp");
c906108c
SS
20107}
20108
43988095
JK
20109/* Return pointer to string at .debug_str offset as read from BUF.
20110 BUF is assumed to be in a compilation unit described by CU_HEADER.
20111 Return *BYTES_READ_PTR count of bytes read from BUF. */
20112
d521ce57 20113static const char *
976ca316 20114read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20115 const gdb_byte *buf,
cf2c3c16
TT
20116 const struct comp_unit_head *cu_header,
20117 unsigned int *bytes_read_ptr)
20118{
8266302d 20119 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20120
976ca316 20121 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20122}
20123
86c0bb4c 20124/* See read.h. */
43988095 20125
86c0bb4c
TT
20126const char *
20127dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20128 const struct comp_unit_head *cu_header,
20129 unsigned int *bytes_read_ptr)
43988095 20130{
86c0bb4c 20131 bfd *abfd = objfile->obfd;
8266302d 20132 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20133
5989a64e 20134 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20135}
20136
3019eac3 20137/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20138 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20139 ADDR_SIZE is the size of addresses from the CU header. */
20140
20141static CORE_ADDR
976ca316
SM
20142read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20143 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20144{
976ca316 20145 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20146 bfd *abfd = objfile->obfd;
20147 const gdb_byte *info_ptr;
18a8505e 20148 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20149
976ca316
SM
20150 per_objfile->per_bfd->addr.read (objfile);
20151 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20152 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20153 objfile_name (objfile));
18a8505e 20154 if (addr_base_or_zero + addr_index * addr_size
976ca316 20155 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20156 error (_("DW_FORM_addr_index pointing outside of "
20157 ".debug_addr section [in module %s]"),
4262abfb 20158 objfile_name (objfile));
976ca316
SM
20159 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20160 + addr_index * addr_size);
3019eac3
DE
20161 if (addr_size == 4)
20162 return bfd_get_32 (abfd, info_ptr);
20163 else
20164 return bfd_get_64 (abfd, info_ptr);
20165}
20166
20167/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20168
20169static CORE_ADDR
20170read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20171{
5e22e966 20172 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20173 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20174}
20175
20176/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20177
20178static CORE_ADDR
d521ce57 20179read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20180 unsigned int *bytes_read)
20181{
5e22e966 20182 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20183 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20184
20185 return read_addr_index (cu, addr_index);
20186}
20187
450a1bfc 20188/* See read.h. */
3019eac3
DE
20189
20190CORE_ADDR
82ca3f51 20191dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20192 dwarf2_per_objfile *per_objfile,
82ca3f51 20193 unsigned int addr_index)
3019eac3 20194{
976ca316 20195 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20196 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20197 int addr_size;
20198
3019eac3
DE
20199 /* We need addr_base and addr_size.
20200 If we don't have PER_CU->cu, we have to get it.
20201 Nasty, but the alternative is storing the needed info in PER_CU,
20202 which at this point doesn't seem justified: it's not clear how frequently
20203 it would get used and it would increase the size of every PER_CU.
20204 Entry points like dwarf2_per_cu_addr_size do a similar thing
20205 so we're not in uncharted territory here.
20206 Alas we need to be a bit more complicated as addr_base is contained
20207 in the DIE.
20208
20209 We don't need to read the entire CU(/TU).
20210 We just need the header and top level die.
a1b64ce1 20211
3019eac3 20212 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20213 For now we skip this optimization. */
3019eac3
DE
20214
20215 if (cu != NULL)
20216 {
20217 addr_base = cu->addr_base;
20218 addr_size = cu->header.addr_size;
20219 }
20220 else
20221 {
976ca316 20222 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20223 addr_base = reader.cu->addr_base;
20224 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20225 }
20226
976ca316 20227 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20228}
20229
18a8505e
AT
20230/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20231 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20232 DWO file. */
3019eac3 20233
d521ce57 20234static const char *
18a8505e
AT
20235read_str_index (struct dwarf2_cu *cu,
20236 struct dwarf2_section_info *str_section,
20237 struct dwarf2_section_info *str_offsets_section,
20238 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20239{
976ca316
SM
20240 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20241 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20242 const char *objf_name = objfile_name (objfile);
3019eac3 20243 bfd *abfd = objfile->obfd;
d521ce57 20244 const gdb_byte *info_ptr;
3019eac3 20245 ULONGEST str_offset;
cf532bd1 20246 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20247
96b79293
TT
20248 str_section->read (objfile);
20249 str_offsets_section->read (objfile);
73869dc2 20250 if (str_section->buffer == NULL)
18a8505e 20251 error (_("%s used without %s section"
9d8780f0 20252 " in CU at offset %s [in module %s]"),
96b79293 20253 form_name, str_section->get_name (),
dda83cd7 20254 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20255 if (str_offsets_section->buffer == NULL)
18a8505e 20256 error (_("%s used without %s section"
9d8780f0 20257 " in CU at offset %s [in module %s]"),
96b79293 20258 form_name, str_section->get_name (),
dda83cd7 20259 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20260 info_ptr = (str_offsets_section->buffer
18a8505e 20261 + str_offsets_base
3019eac3
DE
20262 + str_index * cu->header.offset_size);
20263 if (cu->header.offset_size == 4)
20264 str_offset = bfd_get_32 (abfd, info_ptr);
20265 else
20266 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20267 if (str_offset >= str_section->size)
57d63ce2 20268 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20269 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20270 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20271 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20272}
20273
18a8505e
AT
20274/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20275
20276static const char *
20277read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20278{
20279 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20280 ? reader->cu->header.addr_size : 0;
20281 return read_str_index (reader->cu,
20282 &reader->dwo_file->sections.str,
20283 &reader->dwo_file->sections.str_offsets,
20284 str_offsets_base, str_index);
20285}
20286
20287/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20288
20289static const char *
20290read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20291{
5e22e966 20292 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20293 const char *objf_name = objfile_name (objfile);
20294 static const char form_name[] = "DW_FORM_GNU_str_index";
20295 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20296
20297 if (!cu->str_offsets_base.has_value ())
20298 error (_("%s used in Fission stub without %s"
20299 " in CU at offset 0x%lx [in module %s]"),
20300 form_name, str_offsets_attr_name,
20301 (long) cu->header.offset_size, objf_name);
20302
20303 return read_str_index (cu,
5e22e966
SM
20304 &cu->per_objfile->per_bfd->str,
20305 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20306 *cu->str_offsets_base, str_index);
20307}
20308
3019eac3
DE
20309/* Return the length of an LEB128 number in BUF. */
20310
20311static int
20312leb128_size (const gdb_byte *buf)
20313{
20314 const gdb_byte *begin = buf;
20315 gdb_byte byte;
20316
20317 while (1)
20318 {
20319 byte = *buf++;
20320 if ((byte & 128) == 0)
20321 return buf - begin;
20322 }
20323}
20324
c906108c 20325static void
e142c38c 20326set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20327{
20328 switch (lang)
20329 {
20330 case DW_LANG_C89:
76bee0cc 20331 case DW_LANG_C99:
0cfd832f 20332 case DW_LANG_C11:
c906108c 20333 case DW_LANG_C:
d1be3247 20334 case DW_LANG_UPC:
e142c38c 20335 cu->language = language_c;
c906108c 20336 break;
9c37b5ae 20337 case DW_LANG_Java:
c906108c 20338 case DW_LANG_C_plus_plus:
0cfd832f
MW
20339 case DW_LANG_C_plus_plus_11:
20340 case DW_LANG_C_plus_plus_14:
e142c38c 20341 cu->language = language_cplus;
c906108c 20342 break;
6aecb9c2
JB
20343 case DW_LANG_D:
20344 cu->language = language_d;
20345 break;
c906108c
SS
20346 case DW_LANG_Fortran77:
20347 case DW_LANG_Fortran90:
b21b22e0 20348 case DW_LANG_Fortran95:
f7de9aab
MW
20349 case DW_LANG_Fortran03:
20350 case DW_LANG_Fortran08:
e142c38c 20351 cu->language = language_fortran;
c906108c 20352 break;
a766d390
DE
20353 case DW_LANG_Go:
20354 cu->language = language_go;
20355 break;
c906108c 20356 case DW_LANG_Mips_Assembler:
e142c38c 20357 cu->language = language_asm;
c906108c
SS
20358 break;
20359 case DW_LANG_Ada83:
8aaf0b47 20360 case DW_LANG_Ada95:
bc5f45f8
JB
20361 cu->language = language_ada;
20362 break;
72019c9c
GM
20363 case DW_LANG_Modula2:
20364 cu->language = language_m2;
20365 break;
fe8e67fd
PM
20366 case DW_LANG_Pascal83:
20367 cu->language = language_pascal;
20368 break;
22566fbd
DJ
20369 case DW_LANG_ObjC:
20370 cu->language = language_objc;
20371 break;
c44af4eb
TT
20372 case DW_LANG_Rust:
20373 case DW_LANG_Rust_old:
20374 cu->language = language_rust;
20375 break;
c906108c
SS
20376 case DW_LANG_Cobol74:
20377 case DW_LANG_Cobol85:
c906108c 20378 default:
e142c38c 20379 cu->language = language_minimal;
c906108c
SS
20380 break;
20381 }
e142c38c 20382 cu->language_defn = language_def (cu->language);
c906108c
SS
20383}
20384
20385/* Return the named attribute or NULL if not there. */
20386
20387static struct attribute *
e142c38c 20388dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20389{
a48e046c 20390 for (;;)
c906108c 20391 {
a48e046c
TT
20392 unsigned int i;
20393 struct attribute *spec = NULL;
20394
20395 for (i = 0; i < die->num_attrs; ++i)
20396 {
20397 if (die->attrs[i].name == name)
20398 return &die->attrs[i];
20399 if (die->attrs[i].name == DW_AT_specification
20400 || die->attrs[i].name == DW_AT_abstract_origin)
20401 spec = &die->attrs[i];
20402 }
20403
20404 if (!spec)
20405 break;
c906108c 20406
f2f0e013 20407 die = follow_die_ref (die, spec, &cu);
f2f0e013 20408 }
c5aa993b 20409
c906108c
SS
20410 return NULL;
20411}
20412
7d45c7c3
KB
20413/* Return the string associated with a string-typed attribute, or NULL if it
20414 is either not found or is of an incorrect type. */
20415
20416static const char *
20417dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20418{
20419 struct attribute *attr;
20420 const char *str = NULL;
20421
20422 attr = dwarf2_attr (die, name, cu);
20423
20424 if (attr != NULL)
20425 {
95f982e5 20426 str = attr->as_string ();
e61108c9 20427 if (str == nullptr)
dda83cd7 20428 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20429 "DIE at %s in module %s"),
20430 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20431 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20432 }
20433
20434 return str;
20435}
20436
a084a2a6 20437/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20438 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20439static const char *
20440dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20441{
20442 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20443 if (dwo_name == nullptr)
20444 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20445 return dwo_name;
20446}
20447
05cf31d1
JB
20448/* Return non-zero iff the attribute NAME is defined for the given DIE,
20449 and holds a non-zero value. This function should only be used for
2dc7f7b3 20450 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20451
20452static int
20453dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20454{
20455 struct attribute *attr = dwarf2_attr (die, name, cu);
20456
c45bc3f8 20457 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
20458}
20459
3ca72b44 20460static int
e142c38c 20461die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20462{
05cf31d1
JB
20463 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20464 which value is non-zero. However, we have to be careful with
20465 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20466 (via dwarf2_flag_true_p) follows this attribute. So we may
20467 end up accidently finding a declaration attribute that belongs
20468 to a different DIE referenced by the specification attribute,
20469 even though the given DIE does not have a declaration attribute. */
20470 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20471 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20472}
20473
63d06c5c 20474/* Return the die giving the specification for DIE, if there is
f2f0e013 20475 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20476 containing the return value on output. If there is no
20477 specification, but there is an abstract origin, that is
20478 returned. */
63d06c5c
DC
20479
20480static struct die_info *
f2f0e013 20481die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20482{
f2f0e013
DJ
20483 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20484 *spec_cu);
63d06c5c 20485
edb3359d
DJ
20486 if (spec_attr == NULL)
20487 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20488
63d06c5c
DC
20489 if (spec_attr == NULL)
20490 return NULL;
20491 else
f2f0e013 20492 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20493}
c906108c 20494
83769d0b 20495/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20496
20497static struct dwarf2_section_info *
20498get_debug_line_section (struct dwarf2_cu *cu)
20499{
20500 struct dwarf2_section_info *section;
976ca316 20501 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
20502
20503 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20504 DWO file. */
20505 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20506 section = &cu->dwo_unit->dwo_file->sections.line;
20507 else if (cu->per_cu->is_dwz)
20508 {
a7308ce0 20509 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
20510
20511 section = &dwz->line;
20512 }
20513 else
976ca316 20514 section = &per_objfile->per_bfd->line;
36586728
TT
20515
20516 return section;
20517}
20518
debd256d 20519/* Read the statement program header starting at OFFSET in
3019eac3 20520 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20521 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20522 Returns NULL if there is a problem reading the header, e.g., if it
20523 has a version we don't understand.
debd256d
JB
20524
20525 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20526 the returned object point into the dwarf line section buffer,
20527 and must not be freed. */
ae2de4f8 20528
fff8551c 20529static line_header_up
9c541725 20530dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20531{
3019eac3 20532 struct dwarf2_section_info *section;
976ca316 20533 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 20534
36586728 20535 section = get_debug_line_section (cu);
976ca316 20536 section->read (per_objfile->objfile);
3019eac3 20537 if (section->buffer == NULL)
debd256d 20538 {
3019eac3 20539 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20540 complaint (_("missing .debug_line.dwo section"));
3019eac3 20541 else
b98664d3 20542 complaint (_("missing .debug_line section"));
debd256d
JB
20543 return 0;
20544 }
20545
0df7ad3a 20546 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 20547 per_objfile, section, &cu->header);
debd256d 20548}
c906108c 20549
c6da4cef 20550/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20551 Return the file name of the psymtab for the given file_entry.
c6da4cef 20552 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20553 If space for the result is malloc'd, *NAME_HOLDER will be set.
20554 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20555
d521ce57 20556static const char *
7ba99d21 20557psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20558 const dwarf2_psymtab *pst,
c89b44cd
TT
20559 const char *comp_dir,
20560 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20561{
d521ce57
TT
20562 const char *include_name = fe.name;
20563 const char *include_name_to_compare = include_name;
72b9f47f 20564 const char *pst_filename;
c6da4cef
DE
20565 int file_is_pst;
20566
8c43009f 20567 const char *dir_name = fe.include_dir (lh);
c6da4cef 20568
c89b44cd 20569 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20570 if (!IS_ABSOLUTE_PATH (include_name)
20571 && (dir_name != NULL || comp_dir != NULL))
20572 {
20573 /* Avoid creating a duplicate psymtab for PST.
20574 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20575 Before we do the comparison, however, we need to account
20576 for DIR_NAME and COMP_DIR.
20577 First prepend dir_name (if non-NULL). If we still don't
20578 have an absolute path prepend comp_dir (if non-NULL).
20579 However, the directory we record in the include-file's
20580 psymtab does not contain COMP_DIR (to match the
20581 corresponding symtab(s)).
20582
20583 Example:
20584
20585 bash$ cd /tmp
20586 bash$ gcc -g ./hello.c
20587 include_name = "hello.c"
20588 dir_name = "."
20589 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20590 DW_AT_name = "./hello.c"
20591
20592 */
c6da4cef
DE
20593
20594 if (dir_name != NULL)
20595 {
c89b44cd
TT
20596 name_holder->reset (concat (dir_name, SLASH_STRING,
20597 include_name, (char *) NULL));
20598 include_name = name_holder->get ();
c6da4cef 20599 include_name_to_compare = include_name;
c6da4cef
DE
20600 }
20601 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20602 {
c89b44cd
TT
20603 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20604 include_name, (char *) NULL));
20605 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20606 }
20607 }
20608
20609 pst_filename = pst->filename;
c89b44cd 20610 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20611 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20612 {
c89b44cd
TT
20613 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20614 pst_filename, (char *) NULL));
20615 pst_filename = copied_name.get ();
c6da4cef
DE
20616 }
20617
1e3fad37 20618 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20619
c6da4cef
DE
20620 if (file_is_pst)
20621 return NULL;
20622 return include_name;
20623}
20624
d9b3de22
DE
20625/* State machine to track the state of the line number program. */
20626
6f77053d 20627class lnp_state_machine
d9b3de22 20628{
6f77053d
PA
20629public:
20630 /* Initialize a machine state for the start of a line number
20631 program. */
804d2729
TT
20632 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20633 bool record_lines_p);
6f77053d 20634
8c43009f
PA
20635 file_entry *current_file ()
20636 {
20637 /* lh->file_names is 0-based, but the file name numbers in the
20638 statement program are 1-based. */
6f77053d
PA
20639 return m_line_header->file_name_at (m_file);
20640 }
20641
20642 /* Record the line in the state machine. END_SEQUENCE is true if
20643 we're processing the end of a sequence. */
20644 void record_line (bool end_sequence);
20645
a8caed5d 20646 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 20647 nop-out rest of the lines in this sequence. */
6f77053d
PA
20648 void check_line_address (struct dwarf2_cu *cu,
20649 const gdb_byte *line_ptr,
7ab6656f 20650 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20651
20652 void handle_set_discriminator (unsigned int discriminator)
20653 {
20654 m_discriminator = discriminator;
20655 m_line_has_non_zero_discriminator |= discriminator != 0;
20656 }
20657
20658 /* Handle DW_LNE_set_address. */
20659 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20660 {
20661 m_op_index = 0;
20662 address += baseaddr;
20663 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20664 }
20665
20666 /* Handle DW_LNS_advance_pc. */
20667 void handle_advance_pc (CORE_ADDR adjust);
20668
20669 /* Handle a special opcode. */
20670 void handle_special_opcode (unsigned char op_code);
20671
20672 /* Handle DW_LNS_advance_line. */
20673 void handle_advance_line (int line_delta)
20674 {
20675 advance_line (line_delta);
20676 }
20677
20678 /* Handle DW_LNS_set_file. */
20679 void handle_set_file (file_name_index file);
20680
20681 /* Handle DW_LNS_negate_stmt. */
20682 void handle_negate_stmt ()
20683 {
20684 m_is_stmt = !m_is_stmt;
20685 }
20686
20687 /* Handle DW_LNS_const_add_pc. */
20688 void handle_const_add_pc ();
20689
20690 /* Handle DW_LNS_fixed_advance_pc. */
20691 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20692 {
20693 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20694 m_op_index = 0;
20695 }
20696
20697 /* Handle DW_LNS_copy. */
20698 void handle_copy ()
20699 {
20700 record_line (false);
20701 m_discriminator = 0;
20702 }
20703
20704 /* Handle DW_LNE_end_sequence. */
20705 void handle_end_sequence ()
20706 {
804d2729 20707 m_currently_recording_lines = true;
6f77053d
PA
20708 }
20709
20710private:
20711 /* Advance the line by LINE_DELTA. */
20712 void advance_line (int line_delta)
20713 {
20714 m_line += line_delta;
20715
20716 if (line_delta != 0)
20717 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20718 }
20719
804d2729
TT
20720 struct dwarf2_cu *m_cu;
20721
6f77053d
PA
20722 gdbarch *m_gdbarch;
20723
20724 /* True if we're recording lines.
20725 Otherwise we're building partial symtabs and are just interested in
20726 finding include files mentioned by the line number program. */
20727 bool m_record_lines_p;
20728
8c43009f 20729 /* The line number header. */
6f77053d 20730 line_header *m_line_header;
8c43009f 20731
6f77053d
PA
20732 /* These are part of the standard DWARF line number state machine,
20733 and initialized according to the DWARF spec. */
d9b3de22 20734
6f77053d 20735 unsigned char m_op_index = 0;
7ba99d21
AT
20736 /* The line table index of the current file. */
20737 file_name_index m_file = 1;
6f77053d
PA
20738 unsigned int m_line = 1;
20739
20740 /* These are initialized in the constructor. */
20741
20742 CORE_ADDR m_address;
20743 bool m_is_stmt;
20744 unsigned int m_discriminator;
d9b3de22
DE
20745
20746 /* Additional bits of state we need to track. */
20747
20748 /* The last file that we called dwarf2_start_subfile for.
20749 This is only used for TLLs. */
6f77053d 20750 unsigned int m_last_file = 0;
d9b3de22 20751 /* The last file a line number was recorded for. */
6f77053d 20752 struct subfile *m_last_subfile = NULL;
d9b3de22 20753
1313c56e
AB
20754 /* The address of the last line entry. */
20755 CORE_ADDR m_last_address;
20756
20757 /* Set to true when a previous line at the same address (using
20758 m_last_address) had m_is_stmt true. This is reset to false when a
20759 line entry at a new address (m_address different to m_last_address) is
20760 processed. */
20761 bool m_stmt_at_address = false;
20762
804d2729
TT
20763 /* When true, record the lines we decode. */
20764 bool m_currently_recording_lines = false;
d9b3de22
DE
20765
20766 /* The last line number that was recorded, used to coalesce
20767 consecutive entries for the same line. This can happen, for
20768 example, when discriminators are present. PR 17276. */
6f77053d
PA
20769 unsigned int m_last_line = 0;
20770 bool m_line_has_non_zero_discriminator = false;
8c43009f 20771};
d9b3de22 20772
6f77053d
PA
20773void
20774lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20775{
20776 CORE_ADDR addr_adj = (((m_op_index + adjust)
20777 / m_line_header->maximum_ops_per_instruction)
20778 * m_line_header->minimum_instruction_length);
20779 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20780 m_op_index = ((m_op_index + adjust)
20781 % m_line_header->maximum_ops_per_instruction);
20782}
d9b3de22 20783
6f77053d
PA
20784void
20785lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20786{
6f77053d 20787 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20788 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20789 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20790 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20791 / m_line_header->maximum_ops_per_instruction)
20792 * m_line_header->minimum_instruction_length);
20793 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20794 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20795 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20796
258bf0ee 20797 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20798 advance_line (line_delta);
20799 record_line (false);
20800 m_discriminator = 0;
20801}
d9b3de22 20802
6f77053d
PA
20803void
20804lnp_state_machine::handle_set_file (file_name_index file)
20805{
20806 m_file = file;
20807
20808 const file_entry *fe = current_file ();
20809 if (fe == NULL)
20810 dwarf2_debug_line_missing_file_complaint ();
20811 else if (m_record_lines_p)
20812 {
20813 const char *dir = fe->include_dir (m_line_header);
20814
c24bdb02 20815 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20816 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20817 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20818 }
20819}
20820
20821void
20822lnp_state_machine::handle_const_add_pc ()
20823{
20824 CORE_ADDR adjust
20825 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20826
20827 CORE_ADDR addr_adj
20828 = (((m_op_index + adjust)
20829 / m_line_header->maximum_ops_per_instruction)
20830 * m_line_header->minimum_instruction_length);
20831
20832 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20833 m_op_index = ((m_op_index + adjust)
20834 % m_line_header->maximum_ops_per_instruction);
20835}
d9b3de22 20836
a05a36a5
DE
20837/* Return non-zero if we should add LINE to the line number table.
20838 LINE is the line to add, LAST_LINE is the last line that was added,
20839 LAST_SUBFILE is the subfile for LAST_LINE.
20840 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20841 had a non-zero discriminator.
20842
20843 We have to be careful in the presence of discriminators.
20844 E.g., for this line:
20845
20846 for (i = 0; i < 100000; i++);
20847
20848 clang can emit four line number entries for that one line,
20849 each with a different discriminator.
20850 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20851
20852 However, we want gdb to coalesce all four entries into one.
20853 Otherwise the user could stepi into the middle of the line and
20854 gdb would get confused about whether the pc really was in the
20855 middle of the line.
20856
20857 Things are further complicated by the fact that two consecutive
20858 line number entries for the same line is a heuristic used by gcc
20859 to denote the end of the prologue. So we can't just discard duplicate
20860 entries, we have to be selective about it. The heuristic we use is
20861 that we only collapse consecutive entries for the same line if at least
20862 one of those entries has a non-zero discriminator. PR 17276.
20863
20864 Note: Addresses in the line number state machine can never go backwards
20865 within one sequence, thus this coalescing is ok. */
20866
20867static int
804d2729
TT
20868dwarf_record_line_p (struct dwarf2_cu *cu,
20869 unsigned int line, unsigned int last_line,
a05a36a5
DE
20870 int line_has_non_zero_discriminator,
20871 struct subfile *last_subfile)
20872{
c24bdb02 20873 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20874 return 1;
20875 if (line != last_line)
20876 return 1;
20877 /* Same line for the same file that we've seen already.
20878 As a last check, for pr 17276, only record the line if the line
20879 has never had a non-zero discriminator. */
20880 if (!line_has_non_zero_discriminator)
20881 return 1;
20882 return 0;
20883}
20884
804d2729
TT
20885/* Use the CU's builder to record line number LINE beginning at
20886 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20887
20888static void
d9b3de22 20889dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20890 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20891 struct dwarf2_cu *cu)
252a6764
DE
20892{
20893 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20894
27e0867f
DE
20895 if (dwarf_line_debug)
20896 {
20897 fprintf_unfiltered (gdb_stdlog,
20898 "Recording line %u, file %s, address %s\n",
20899 line, lbasename (subfile->name),
20900 paddress (gdbarch, address));
20901 }
20902
804d2729 20903 if (cu != nullptr)
8c95582d 20904 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20905}
20906
20907/* Subroutine of dwarf_decode_lines_1 to simplify it.
20908 Mark the end of a set of line number records.
d9b3de22 20909 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20910 If SUBFILE is NULL the request is ignored. */
20911
20912static void
20913dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20914 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20915{
27e0867f
DE
20916 if (subfile == NULL)
20917 return;
20918
20919 if (dwarf_line_debug)
20920 {
20921 fprintf_unfiltered (gdb_stdlog,
20922 "Finishing current line, file %s, address %s\n",
20923 lbasename (subfile->name),
20924 paddress (gdbarch, address));
20925 }
20926
8c95582d 20927 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20928}
20929
6f77053d
PA
20930void
20931lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20932{
d9b3de22
DE
20933 if (dwarf_line_debug)
20934 {
20935 fprintf_unfiltered (gdb_stdlog,
20936 "Processing actual line %u: file %u,"
94a72be7 20937 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20938 m_line, m_file,
6f77053d 20939 paddress (m_gdbarch, m_address),
94a72be7
AB
20940 m_is_stmt, m_discriminator,
20941 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20942 }
20943
6f77053d 20944 file_entry *fe = current_file ();
8c43009f
PA
20945
20946 if (fe == NULL)
d9b3de22
DE
20947 dwarf2_debug_line_missing_file_complaint ();
20948 /* For now we ignore lines not starting on an instruction boundary.
20949 But not when processing end_sequence for compatibility with the
20950 previous version of the code. */
6f77053d 20951 else if (m_op_index == 0 || end_sequence)
d9b3de22 20952 {
8c43009f 20953 fe->included_p = 1;
8c95582d 20954 if (m_record_lines_p)
d9b3de22 20955 {
1313c56e
AB
20956 /* When we switch files we insert an end maker in the first file,
20957 switch to the second file and add a new line entry. The
20958 problem is that the end marker inserted in the first file will
20959 discard any previous line entries at the same address. If the
20960 line entries in the first file are marked as is-stmt, while
20961 the new line in the second file is non-stmt, then this means
20962 the end marker will discard is-stmt lines so we can have a
20963 non-stmt line. This means that there are less addresses at
20964 which the user can insert a breakpoint.
20965
20966 To improve this we track the last address in m_last_address,
20967 and whether we have seen an is-stmt at this address. Then
20968 when switching files, if we have seen a stmt at the current
20969 address, and we are switching to create a non-stmt line, then
20970 discard the new line. */
20971 bool file_changed
20972 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20973 bool ignore_this_line
dda83cd7
SM
20974 = ((file_changed && !end_sequence && m_last_address == m_address
20975 && !m_is_stmt && m_stmt_at_address)
20976 || (!end_sequence && m_line == 0));
1313c56e
AB
20977
20978 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 20979 {
804d2729
TT
20980 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20981 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20982 }
20983
1313c56e 20984 if (!end_sequence && !ignore_this_line)
d9b3de22 20985 {
8c95582d
AB
20986 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20987
804d2729 20988 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20989 m_line_has_non_zero_discriminator,
20990 m_last_subfile))
d9b3de22 20991 {
c24bdb02 20992 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20993 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20994 builder->get_current_subfile (),
8c95582d 20995 m_line, m_address, is_stmt,
804d2729 20996 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20997 }
c24bdb02 20998 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20999 m_last_line = m_line;
d9b3de22
DE
21000 }
21001 }
21002 }
1313c56e
AB
21003
21004 /* Track whether we have seen any m_is_stmt true at m_address in case we
21005 have multiple line table entries all at m_address. */
21006 if (m_last_address != m_address)
21007 {
21008 m_stmt_at_address = false;
21009 m_last_address = m_address;
21010 }
21011 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21012}
21013
804d2729
TT
21014lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21015 line_header *lh, bool record_lines_p)
d9b3de22 21016{
804d2729 21017 m_cu = cu;
6f77053d
PA
21018 m_gdbarch = arch;
21019 m_record_lines_p = record_lines_p;
21020 m_line_header = lh;
d9b3de22 21021
804d2729 21022 m_currently_recording_lines = true;
d9b3de22 21023
d9b3de22
DE
21024 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21025 was a line entry for it so that the backend has a chance to adjust it
21026 and also record it in case it needs it. This is currently used by MIPS
21027 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21028 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21029 m_is_stmt = lh->default_is_stmt;
21030 m_discriminator = 0;
1313c56e
AB
21031
21032 m_last_address = m_address;
21033 m_stmt_at_address = false;
252a6764
DE
21034}
21035
6f77053d
PA
21036void
21037lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21038 const gdb_byte *line_ptr,
7ab6656f 21039 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21040{
a8caed5d
FS
21041 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21042 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21043 located at 0x0. In this case, additionally check that if
21044 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21045
a8caed5d
FS
21046 if ((address == 0 && address < unrelocated_lowpc)
21047 || address == (CORE_ADDR) -1)
924c2928
DE
21048 {
21049 /* This line table is for a function which has been
21050 GCd by the linker. Ignore it. PR gdb/12528 */
21051
5e22e966 21052 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21053 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21054
b98664d3 21055 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21056 line_offset, objfile_name (objfile));
804d2729
TT
21057 m_currently_recording_lines = false;
21058 /* Note: m_currently_recording_lines is left as false until we see
21059 DW_LNE_end_sequence. */
924c2928
DE
21060 }
21061}
21062
f3f5162e 21063/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21064 Process the line number information in LH.
21065 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21066 program in order to set included_p for every referenced header. */
debd256d 21067
c906108c 21068static void
43f3e411
DE
21069dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21070 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21071{
d521ce57
TT
21072 const gdb_byte *line_ptr, *extended_end;
21073 const gdb_byte *line_end;
a8c50c1f 21074 unsigned int bytes_read, extended_len;
699ca60a 21075 unsigned char op_code, extended_op;
e142c38c 21076 CORE_ADDR baseaddr;
5e22e966 21077 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21078 bfd *abfd = objfile->obfd;
08feed99 21079 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21080 /* True if we're recording line info (as opposed to building partial
21081 symtabs and just interested in finding include files mentioned by
21082 the line number program). */
21083 bool record_lines_p = !decode_for_pst_p;
e142c38c 21084
b3b3bada 21085 baseaddr = objfile->text_section_offset ();
c906108c 21086
debd256d
JB
21087 line_ptr = lh->statement_program_start;
21088 line_end = lh->statement_program_end;
c906108c
SS
21089
21090 /* Read the statement sequences until there's nothing left. */
21091 while (line_ptr < line_end)
21092 {
6f77053d
PA
21093 /* The DWARF line number program state machine. Reset the state
21094 machine at the start of each sequence. */
804d2729 21095 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21096 bool end_sequence = false;
d9b3de22 21097
8c43009f 21098 if (record_lines_p)
c906108c 21099 {
8c43009f
PA
21100 /* Start a subfile for the current file of the state
21101 machine. */
21102 const file_entry *fe = state_machine.current_file ();
21103
21104 if (fe != NULL)
804d2729 21105 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21106 }
21107
a738430d 21108 /* Decode the table. */
d9b3de22 21109 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21110 {
21111 op_code = read_1_byte (abfd, line_ptr);
21112 line_ptr += 1;
9aa1fe7e 21113
debd256d 21114 if (op_code >= lh->opcode_base)
6e70227d 21115 {
8e07a239 21116 /* Special opcode. */
6f77053d 21117 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21118 }
21119 else switch (op_code)
c906108c
SS
21120 {
21121 case DW_LNS_extended_op:
3e43a32a
MS
21122 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21123 &bytes_read);
473b7be6 21124 line_ptr += bytes_read;
a8c50c1f 21125 extended_end = line_ptr + extended_len;
c906108c
SS
21126 extended_op = read_1_byte (abfd, line_ptr);
21127 line_ptr += 1;
8f34b746
TV
21128 if (DW_LNE_lo_user <= extended_op
21129 && extended_op <= DW_LNE_hi_user)
21130 {
21131 /* Vendor extension, ignore. */
21132 line_ptr = extended_end;
21133 break;
21134 }
c906108c
SS
21135 switch (extended_op)
21136 {
21137 case DW_LNE_end_sequence:
6f77053d
PA
21138 state_machine.handle_end_sequence ();
21139 end_sequence = true;
c906108c
SS
21140 break;
21141 case DW_LNE_set_address:
d9b3de22
DE
21142 {
21143 CORE_ADDR address
c8a7a66f 21144 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21145 line_ptr += bytes_read;
6f77053d
PA
21146
21147 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21148 lowpc - baseaddr, address);
6f77053d 21149 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21150 }
c906108c
SS
21151 break;
21152 case DW_LNE_define_file:
dda83cd7
SM
21153 {
21154 const char *cur_file;
ecfb656c
PA
21155 unsigned int mod_time, length;
21156 dir_index dindex;
6e70227d 21157
dda83cd7 21158 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21159 &bytes_read);
dda83cd7
SM
21160 line_ptr += bytes_read;
21161 dindex = (dir_index)
21162 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21163 line_ptr += bytes_read;
21164 mod_time =
21165 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21166 line_ptr += bytes_read;
21167 length =
21168 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21169 line_ptr += bytes_read;
21170 lh->add_file_name (cur_file, dindex, mod_time, length);
21171 }
c906108c 21172 break;
d0c6ba3d 21173 case DW_LNE_set_discriminator:
6f77053d
PA
21174 {
21175 /* The discriminator is not interesting to the
21176 debugger; just ignore it. We still need to
21177 check its value though:
21178 if there are consecutive entries for the same
21179 (non-prologue) line we want to coalesce them.
21180 PR 17276. */
21181 unsigned int discr
21182 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21183 line_ptr += bytes_read;
21184
21185 state_machine.handle_set_discriminator (discr);
21186 }
d0c6ba3d 21187 break;
c906108c 21188 default:
b98664d3 21189 complaint (_("mangled .debug_line section"));
debd256d 21190 return;
c906108c 21191 }
a8c50c1f
DJ
21192 /* Make sure that we parsed the extended op correctly. If e.g.
21193 we expected a different address size than the producer used,
21194 we may have read the wrong number of bytes. */
21195 if (line_ptr != extended_end)
21196 {
b98664d3 21197 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21198 return;
21199 }
c906108c
SS
21200 break;
21201 case DW_LNS_copy:
6f77053d 21202 state_machine.handle_copy ();
c906108c
SS
21203 break;
21204 case DW_LNS_advance_pc:
2dc7f7b3
TT
21205 {
21206 CORE_ADDR adjust
21207 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21208 line_ptr += bytes_read;
6f77053d
PA
21209
21210 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21211 }
c906108c
SS
21212 break;
21213 case DW_LNS_advance_line:
a05a36a5
DE
21214 {
21215 int line_delta
21216 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21217 line_ptr += bytes_read;
6f77053d
PA
21218
21219 state_machine.handle_advance_line (line_delta);
a05a36a5 21220 }
c906108c
SS
21221 break;
21222 case DW_LNS_set_file:
d9b3de22 21223 {
6f77053d 21224 file_name_index file
ecfb656c
PA
21225 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21226 &bytes_read);
d9b3de22 21227 line_ptr += bytes_read;
8c43009f 21228
6f77053d 21229 state_machine.handle_set_file (file);
d9b3de22 21230 }
c906108c
SS
21231 break;
21232 case DW_LNS_set_column:
0ad93d4f 21233 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21234 line_ptr += bytes_read;
21235 break;
21236 case DW_LNS_negate_stmt:
6f77053d 21237 state_machine.handle_negate_stmt ();
c906108c
SS
21238 break;
21239 case DW_LNS_set_basic_block:
c906108c 21240 break;
c2c6d25f
JM
21241 /* Add to the address register of the state machine the
21242 address increment value corresponding to special opcode
a738430d
MK
21243 255. I.e., this value is scaled by the minimum
21244 instruction length since special opcode 255 would have
b021a221 21245 scaled the increment. */
c906108c 21246 case DW_LNS_const_add_pc:
6f77053d 21247 state_machine.handle_const_add_pc ();
c906108c
SS
21248 break;
21249 case DW_LNS_fixed_advance_pc:
3e29f34a 21250 {
6f77053d 21251 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21252 line_ptr += 2;
6f77053d
PA
21253
21254 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21255 }
c906108c 21256 break;
9aa1fe7e 21257 default:
a738430d
MK
21258 {
21259 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21260 int i;
a738430d 21261
debd256d 21262 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21263 {
21264 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21265 line_ptr += bytes_read;
21266 }
21267 }
c906108c
SS
21268 }
21269 }
d9b3de22
DE
21270
21271 if (!end_sequence)
21272 dwarf2_debug_line_missing_end_sequence_complaint ();
21273
21274 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21275 in which case we still finish recording the last line). */
6f77053d 21276 state_machine.record_line (true);
c906108c 21277 }
f3f5162e
DE
21278}
21279
21280/* Decode the Line Number Program (LNP) for the given line_header
21281 structure and CU. The actual information extracted and the type
21282 of structures created from the LNP depends on the value of PST.
21283
21284 1. If PST is NULL, then this procedure uses the data from the program
21285 to create all necessary symbol tables, and their linetables.
21286
21287 2. If PST is not NULL, this procedure reads the program to determine
21288 the list of files included by the unit represented by PST, and
21289 builds all the associated partial symbol tables.
21290
21291 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21292 It is used for relative paths in the line table.
21293 NOTE: When processing partial symtabs (pst != NULL),
21294 comp_dir == pst->dirname.
21295
21296 NOTE: It is important that psymtabs have the same file name (via strcmp)
21297 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21298 symtab we don't use it in the name of the psymtabs we create.
21299 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21300 A good testcase for this is mb-inline.exp.
21301
527f3840
JK
21302 LOWPC is the lowest address in CU (or 0 if not known).
21303
21304 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21305 for its PC<->lines mapping information. Otherwise only the filename
21306 table is read in. */
f3f5162e
DE
21307
21308static void
21309dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21310 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21311 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21312{
5e22e966 21313 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21314 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21315
527f3840
JK
21316 if (decode_mapping)
21317 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21318
21319 if (decode_for_pst_p)
21320 {
aaa75496 21321 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21322 create the psymtab of each included file. */
7ba99d21 21323 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21324 if (file_entry.included_p == 1)
21325 {
c89b44cd 21326 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21327 const char *include_name =
7ba99d21
AT
21328 psymtab_include_file_name (lh, file_entry, pst,
21329 comp_dir, &name_holder);
c6da4cef 21330 if (include_name != NULL)
7b249e47
TT
21331 dwarf2_create_include_psymtab
21332 (cu->per_objfile->per_bfd, include_name, pst,
21333 cu->per_objfile->per_bfd->partial_symtabs.get (),
0072c873 21334 objfile->per_bfd);
dda83cd7 21335 }
aaa75496 21336 }
cb1df416
DJ
21337 else
21338 {
21339 /* Make sure a symtab is created for every file, even files
21340 which contain only variables (i.e. no code with associated
21341 line numbers). */
c24bdb02
KS
21342 buildsym_compunit *builder = cu->get_builder ();
21343 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21344
7ba99d21 21345 for (auto &fe : lh->file_names ())
cb1df416 21346 {
804d2729 21347 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21348 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21349 {
c24bdb02 21350 builder->get_current_subfile ()->symtab
804d2729 21351 = allocate_symtab (cust,
c24bdb02 21352 builder->get_current_subfile ()->name);
43f3e411 21353 }
c24bdb02 21354 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21355 }
21356 }
c906108c
SS
21357}
21358
21359/* Start a subfile for DWARF. FILENAME is the name of the file and
21360 DIRNAME the name of the source directory which contains FILENAME
4d663531 21361 or NULL if not known.
c906108c
SS
21362 This routine tries to keep line numbers from identical absolute and
21363 relative file names in a common subfile.
21364
21365 Using the `list' example from the GDB testsuite, which resides in
21366 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21367 of /srcdir/list0.c yields the following debugging information for list0.c:
21368
c5aa993b 21369 DW_AT_name: /srcdir/list0.c
4d663531 21370 DW_AT_comp_dir: /compdir
357e46e7 21371 files.files[0].name: list0.h
c5aa993b 21372 files.files[0].dir: /srcdir
357e46e7 21373 files.files[1].name: list0.c
c5aa993b 21374 files.files[1].dir: /srcdir
c906108c
SS
21375
21376 The line number information for list0.c has to end up in a single
4f1520fb
FR
21377 subfile, so that `break /srcdir/list0.c:1' works as expected.
21378 start_subfile will ensure that this happens provided that we pass the
21379 concatenation of files.files[1].dir and files.files[1].name as the
21380 subfile's name. */
c906108c
SS
21381
21382static void
804d2729
TT
21383dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21384 const char *dirname)
c906108c 21385{
43816ebc 21386 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21387
4d663531 21388 /* In order not to lose the line information directory,
4f1520fb
FR
21389 we concatenate it to the filename when it makes sense.
21390 Note that the Dwarf3 standard says (speaking of filenames in line
21391 information): ``The directory index is ignored for file names
21392 that represent full path names''. Thus ignoring dirname in the
21393 `else' branch below isn't an issue. */
c906108c 21394
d5166ae1 21395 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21396 {
43816ebc
TT
21397 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21398 filename = copy.get ();
d521ce57 21399 }
c906108c 21400
c24bdb02 21401 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21402}
21403
4c2df51b
DJ
21404static void
21405var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21406 struct dwarf2_cu *cu)
4c2df51b 21407{
5e22e966 21408 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21409 struct comp_unit_head *cu_header = &cu->header;
21410
4c2df51b
DJ
21411 /* NOTE drow/2003-01-30: There used to be a comment and some special
21412 code here to turn a symbol with DW_AT_external and a
21413 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21414 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21415 with some versions of binutils) where shared libraries could have
21416 relocations against symbols in their debug information - the
21417 minimal symbol would have the right address, but the debug info
21418 would not. It's no longer necessary, because we will explicitly
21419 apply relocations when we read in the debug information now. */
21420
21421 /* A DW_AT_location attribute with no contents indicates that a
21422 variable has been optimized away. */
9d2246fc 21423 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21424 {
f1e6e072 21425 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21426 return;
21427 }
21428
21429 /* Handle one degenerate form of location expression specially, to
21430 preserve GDB's previous behavior when section offsets are
336d760d
AT
21431 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21432 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21433
9d2246fc
TT
21434 if (attr->form_is_block ())
21435 {
21436 struct dwarf_block *block = attr->as_block ();
21437
21438 if ((block->data[0] == DW_OP_addr
21439 && block->size == 1 + cu_header->addr_size)
21440 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 21441 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
21442 && (block->size
21443 == 1 + leb128_size (&block->data[1]))))
21444 {
21445 unsigned int dummy;
21446
21447 if (block->data[0] == DW_OP_addr)
21448 SET_SYMBOL_VALUE_ADDRESS
21449 (sym, cu->header.read_address (objfile->obfd,
21450 block->data + 1,
38583298 21451 &dummy));
9d2246fc
TT
21452 else
21453 SET_SYMBOL_VALUE_ADDRESS
21454 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21455 &dummy));
21456 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21457 fixup_symbol_section (sym, objfile);
21458 SET_SYMBOL_VALUE_ADDRESS
21459 (sym,
21460 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 21461 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
21462 return;
21463 }
4c2df51b
DJ
21464 }
21465
21466 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21467 expression evaluator, and use LOC_COMPUTED only when necessary
21468 (i.e. when the value of a register or memory location is
21469 referenced, or a thread-local block, etc.). Then again, it might
21470 not be worthwhile. I'm assuming that it isn't unless performance
21471 or memory numbers show me otherwise. */
21472
f1e6e072 21473 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21474
f1e6e072 21475 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21476 cu->has_loclist = true;
4c2df51b
DJ
21477}
21478
c906108c
SS
21479/* Given a pointer to a DWARF information entry, figure out if we need
21480 to make a symbol table entry for it, and if so, create a new entry
21481 and return a pointer to it.
21482 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21483 used the passed type.
21484 If SPACE is not NULL, use it to hold the new symbol. If it is
21485 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21486
21487static struct symbol *
5e2db402
TT
21488new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21489 struct symbol *space)
c906108c 21490{
976ca316
SM
21491 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21492 struct objfile *objfile = per_objfile->objfile;
08feed99 21493 struct gdbarch *gdbarch = objfile->arch ();
c906108c 21494 struct symbol *sym = NULL;
15d034d0 21495 const char *name;
c906108c
SS
21496 struct attribute *attr = NULL;
21497 struct attribute *attr2 = NULL;
e142c38c 21498 CORE_ADDR baseaddr;
e37fd15a
SW
21499 struct pending **list_to_add = NULL;
21500
edb3359d 21501 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21502
b3b3bada 21503 baseaddr = objfile->text_section_offset ();
c906108c 21504
94af9270 21505 name = dwarf2_name (die, cu);
c906108c
SS
21506 if (name)
21507 {
34eaf542 21508 int suppress_add = 0;
94af9270 21509
34eaf542
TT
21510 if (space)
21511 sym = space;
21512 else
8c14c3a3 21513 sym = new (&objfile->objfile_obstack) symbol;
c906108c 21514 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21515
21516 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21517 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
21518 /* Fortran does not have mangling standard and the mangling does differ
21519 between gfortran, iFort etc. */
bcfe6157
TT
21520 const char *physname
21521 = (cu->language == language_fortran
21522 ? dwarf2_full_name (name, die, cu)
21523 : dwarf2_physname (name, die, cu));
21524 const char *linkagename = dw2_linkage_name (die, cu);
21525
21526 if (linkagename == nullptr || cu->language == language_ada)
21527 sym->set_linkage_name (physname);
21528 else
21529 {
21530 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21531 sym->set_linkage_name (linkagename);
21532 }
f55ee35c 21533
c906108c 21534 /* Default assumptions.
dda83cd7 21535 Use the passed type or decode it from the die. */
176620f1 21536 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21537 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21538 if (type != NULL)
21539 SYMBOL_TYPE (sym) = type;
21540 else
e7c27a73 21541 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21542 attr = dwarf2_attr (die,
21543 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21544 cu);
435d3d88 21545 if (attr != nullptr)
529908cb 21546 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 21547
edb3359d
DJ
21548 attr = dwarf2_attr (die,
21549 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21550 cu);
aa659cfa 21551 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 21552 {
529908cb 21553 file_name_index file_index
aa659cfa 21554 = (file_name_index) attr->as_nonnegative ();
8c43009f 21555 struct file_entry *fe;
9a619af0 21556
ecfb656c
PA
21557 if (cu->line_header != NULL)
21558 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21559 else
21560 fe = NULL;
21561
21562 if (fe == NULL)
b98664d3 21563 complaint (_("file index out of range"));
8c43009f
PA
21564 else
21565 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21566 }
21567
c906108c
SS
21568 switch (die->tag)
21569 {
21570 case DW_TAG_label:
e142c38c 21571 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21572 if (attr != nullptr)
3e29f34a
MR
21573 {
21574 CORE_ADDR addr;
21575
95f982e5 21576 addr = attr->as_address ();
3e29f34a 21577 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21578 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 21579 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 21580 }
8f5c6526
TV
21581 else
21582 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
21583 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21584 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 21585 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21586 break;
21587 case DW_TAG_subprogram:
21588 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21589 finish_block. */
f1e6e072 21590 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21591 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 21592 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
21593 || cu->language == language_ada
21594 || cu->language == language_fortran)
c906108c 21595 {
dda83cd7
SM
21596 /* Subprograms marked external are stored as a global symbol.
21597 Ada and Fortran subprograms, whether marked external or
21598 not, are always stored as a global symbol, because we want
21599 to be able to access them globally. For instance, we want
21600 to be able to break on a nested subprogram without having
21601 to specify the context. */
c24bdb02 21602 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21603 }
21604 else
21605 {
e37fd15a 21606 list_to_add = cu->list_in_scope;
c906108c
SS
21607 }
21608 break;
edb3359d
DJ
21609 case DW_TAG_inlined_subroutine:
21610 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21611 finish_block. */
f1e6e072 21612 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21613 SYMBOL_INLINED (sym) = 1;
481860b3 21614 list_to_add = cu->list_in_scope;
edb3359d 21615 break;
34eaf542
TT
21616 case DW_TAG_template_value_param:
21617 suppress_add = 1;
21618 /* Fall through. */
72929c62 21619 case DW_TAG_constant:
c906108c 21620 case DW_TAG_variable:
254e6b9e 21621 case DW_TAG_member:
0963b4bd
MS
21622 /* Compilation with minimal debug info may result in
21623 variables with missing type entries. Change the
21624 misleading `void' type to something sensible. */
78134374 21625 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 21626 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21627
e142c38c 21628 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21629 /* In the case of DW_TAG_member, we should only be called for
21630 static const members. */
21631 if (die->tag == DW_TAG_member)
21632 {
3863f96c
DE
21633 /* dwarf2_add_field uses die_is_declaration,
21634 so we do the same. */
254e6b9e
DE
21635 gdb_assert (die_is_declaration (die, cu));
21636 gdb_assert (attr);
21637 }
435d3d88 21638 if (attr != nullptr)
c906108c 21639 {
e7c27a73 21640 dwarf2_const_value (attr, sym, cu);
e142c38c 21641 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21642 if (!suppress_add)
34eaf542 21643 {
c45bc3f8 21644 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 21645 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21646 else
e37fd15a 21647 list_to_add = cu->list_in_scope;
34eaf542 21648 }
c906108c
SS
21649 break;
21650 }
e142c38c 21651 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21652 if (attr != nullptr)
c906108c 21653 {
e7c27a73 21654 var_decode_location (attr, sym, cu);
e142c38c 21655 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21656
21657 /* Fortran explicitly imports any global symbols to the local
21658 scope by DW_TAG_common_block. */
21659 if (cu->language == language_fortran && die->parent
21660 && die->parent->tag == DW_TAG_common_block)
21661 attr2 = NULL;
21662
caac4577
JG
21663 if (SYMBOL_CLASS (sym) == LOC_STATIC
21664 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 21665 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
21666 {
21667 /* When a static variable is eliminated by the linker,
21668 the corresponding debug information is not stripped
21669 out, but the variable address is set to null;
21670 do not add such variables into symbol table. */
21671 }
c45bc3f8 21672 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 21673 {
4b610737
TT
21674 if (SYMBOL_CLASS (sym) == LOC_STATIC
21675 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 21676 && per_objfile->per_bfd->can_copy)
4b610737
TT
21677 {
21678 /* A global static variable might be subject to
21679 copy relocation. We first check for a local
21680 minsym, though, because maybe the symbol was
21681 marked hidden, in which case this would not
21682 apply. */
21683 bound_minimal_symbol found
21684 = (lookup_minimal_symbol_linkage
987012b8 21685 (sym->linkage_name (), objfile));
4b610737
TT
21686 if (found.minsym != nullptr)
21687 sym->maybe_copied = 1;
21688 }
f55ee35c 21689
1c809c68
TT
21690 /* A variable with DW_AT_external is never static,
21691 but it may be block-scoped. */
804d2729 21692 list_to_add
c24bdb02
KS
21693 = ((cu->list_in_scope
21694 == cu->get_builder ()->get_file_symbols ())
21695 ? cu->get_builder ()->get_global_symbols ()
804d2729 21696 : cu->list_in_scope);
1c809c68 21697 }
c906108c 21698 else
e37fd15a 21699 list_to_add = cu->list_in_scope;
c906108c
SS
21700 }
21701 else
21702 {
21703 /* We do not know the address of this symbol.
dda83cd7
SM
21704 If it is an external symbol and we have type information
21705 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21706 The address of the variable will then be determined from
21707 the minimal symbol table whenever the variable is
21708 referenced. */
e142c38c 21709 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21710
21711 /* Fortran explicitly imports any global symbols to the local
21712 scope by DW_TAG_common_block. */
21713 if (cu->language == language_fortran && die->parent
21714 && die->parent->tag == DW_TAG_common_block)
21715 {
21716 /* SYMBOL_CLASS doesn't matter here because
21717 read_common_block is going to reset it. */
21718 if (!suppress_add)
21719 list_to_add = cu->list_in_scope;
21720 }
c45bc3f8 21721 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 21722 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21723 {
0fe7935b
DJ
21724 /* A variable with DW_AT_external is never static, but it
21725 may be block-scoped. */
804d2729 21726 list_to_add
c24bdb02
KS
21727 = ((cu->list_in_scope
21728 == cu->get_builder ()->get_file_symbols ())
21729 ? cu->get_builder ()->get_global_symbols ()
804d2729 21730 : cu->list_in_scope);
0fe7935b 21731
f1e6e072 21732 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21733 }
442ddf59
JK
21734 else if (!die_is_declaration (die, cu))
21735 {
21736 /* Use the default LOC_OPTIMIZED_OUT class. */
21737 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21738 if (!suppress_add)
21739 list_to_add = cu->list_in_scope;
442ddf59 21740 }
c906108c
SS
21741 }
21742 break;
21743 case DW_TAG_formal_parameter:
a60f3166 21744 {
3db19b2d
TV
21745 /* If we are inside a function, mark this as an argument. If
21746 not, we might be looking at an argument to an inlined function
21747 when we do not have enough information to show inlined frames;
21748 pretend it's a local variable in that case so that the user can
21749 still see it. */
21750 struct context_stack *curr
21751 = cu->get_builder ()->get_current_context_stack ();
21752 if (curr != nullptr && curr->name != nullptr)
21753 SYMBOL_IS_ARGUMENT (sym) = 1;
a60f3166 21754 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21755 if (attr != nullptr)
a60f3166
TT
21756 {
21757 var_decode_location (attr, sym, cu);
21758 }
21759 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21760 if (attr != nullptr)
a60f3166
TT
21761 {
21762 dwarf2_const_value (attr, sym, cu);
21763 }
f346a30d 21764
a60f3166
TT
21765 list_to_add = cu->list_in_scope;
21766 }
c906108c
SS
21767 break;
21768 case DW_TAG_unspecified_parameters:
21769 /* From varargs functions; gdb doesn't seem to have any
21770 interest in this information, so just ignore it for now.
21771 (FIXME?) */
21772 break;
34eaf542
TT
21773 case DW_TAG_template_type_param:
21774 suppress_add = 1;
21775 /* Fall through. */
c906108c 21776 case DW_TAG_class_type:
680b30c7 21777 case DW_TAG_interface_type:
c906108c
SS
21778 case DW_TAG_structure_type:
21779 case DW_TAG_union_type:
72019c9c 21780 case DW_TAG_set_type:
c906108c 21781 case DW_TAG_enumeration_type:
f1e6e072 21782 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21783 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21784
63d06c5c 21785 {
9c37b5ae 21786 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21787 really ever be static objects: otherwise, if you try
21788 to, say, break of a class's method and you're in a file
21789 which doesn't mention that class, it won't work unless
21790 the check for all static symbols in lookup_symbol_aux
21791 saves you. See the OtherFileClass tests in
21792 gdb.c++/namespace.exp. */
21793
e37fd15a 21794 if (!suppress_add)
34eaf542 21795 {
c24bdb02 21796 buildsym_compunit *builder = cu->get_builder ();
804d2729 21797 list_to_add
c24bdb02 21798 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21799 && cu->language == language_cplus
c24bdb02 21800 ? builder->get_global_symbols ()
804d2729 21801 : cu->list_in_scope);
63d06c5c 21802
64382290 21803 /* The semantics of C++ state that "struct foo {
9c37b5ae 21804 ... }" also defines a typedef for "foo". */
64382290 21805 if (cu->language == language_cplus
45280282 21806 || cu->language == language_ada
c44af4eb
TT
21807 || cu->language == language_d
21808 || cu->language == language_rust)
64382290
TT
21809 {
21810 /* The symbol's name is already allocated along
21811 with this objfile, so we don't need to
21812 duplicate it for the type. */
7d93a1e0 21813 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21814 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21815 }
63d06c5c
DC
21816 }
21817 }
c906108c
SS
21818 break;
21819 case DW_TAG_typedef:
f1e6e072 21820 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21821 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21822 list_to_add = cu->list_in_scope;
63d06c5c 21823 break;
d8f62e84 21824 case DW_TAG_array_type:
c906108c 21825 case DW_TAG_base_type:
dda83cd7 21826 case DW_TAG_subrange_type:
f1e6e072 21827 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21828 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21829 list_to_add = cu->list_in_scope;
c906108c
SS
21830 break;
21831 case DW_TAG_enumerator:
e142c38c 21832 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21833 if (attr != nullptr)
c906108c 21834 {
e7c27a73 21835 dwarf2_const_value (attr, sym, cu);
c906108c 21836 }
63d06c5c
DC
21837 {
21838 /* NOTE: carlton/2003-11-10: See comment above in the
21839 DW_TAG_class_type, etc. block. */
21840
804d2729 21841 list_to_add
c24bdb02 21842 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21843 && cu->language == language_cplus
c24bdb02 21844 ? cu->get_builder ()->get_global_symbols ()
804d2729 21845 : cu->list_in_scope);
63d06c5c 21846 }
c906108c 21847 break;
74921315 21848 case DW_TAG_imported_declaration:
5c4e30ca 21849 case DW_TAG_namespace:
f1e6e072 21850 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21851 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21852 break;
530e8392
KB
21853 case DW_TAG_module:
21854 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21855 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21856 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21857 break;
4357ac6c 21858 case DW_TAG_common_block:
f1e6e072 21859 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21860 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21861 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21862 break;
c906108c
SS
21863 default:
21864 /* Not a tag we recognize. Hopefully we aren't processing
21865 trash data, but since we must specifically ignore things
21866 we don't recognize, there is nothing else we should do at
0963b4bd 21867 this point. */
b98664d3 21868 complaint (_("unsupported tag: '%s'"),
4d3c2250 21869 dwarf_tag_name (die->tag));
c906108c
SS
21870 break;
21871 }
df8a16a1 21872
e37fd15a
SW
21873 if (suppress_add)
21874 {
21875 sym->hash_next = objfile->template_symbols;
21876 objfile->template_symbols = sym;
21877 list_to_add = NULL;
21878 }
21879
21880 if (list_to_add != NULL)
d3cb6808 21881 add_symbol_to_list (sym, list_to_add);
e37fd15a 21882
df8a16a1
DJ
21883 /* For the benefit of old versions of GCC, check for anonymous
21884 namespaces based on the demangled name. */
4d4ec4e5 21885 if (!cu->processing_has_namespace_info
94af9270 21886 && cu->language == language_cplus)
c24bdb02 21887 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21888 }
21889 return (sym);
21890}
21891
98bfdba5
PA
21892/* Given an attr with a DW_FORM_dataN value in host byte order,
21893 zero-extend it as appropriate for the symbol's type. The DWARF
21894 standard (v4) is not entirely clear about the meaning of using
21895 DW_FORM_dataN for a constant with a signed type, where the type is
21896 wider than the data. The conclusion of a discussion on the DWARF
21897 list was that this is unspecified. We choose to always zero-extend
21898 because that is the interpretation long in use by GCC. */
c906108c 21899
98bfdba5 21900static gdb_byte *
ff39bb5e 21901dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21902 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21903{
5e22e966 21904 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21905 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21906 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 21907 LONGEST l = attr->constant_value (0);
98bfdba5
PA
21908
21909 if (bits < sizeof (*value) * 8)
21910 {
21911 l &= ((LONGEST) 1 << bits) - 1;
21912 *value = l;
21913 }
21914 else if (bits == sizeof (*value) * 8)
21915 *value = l;
21916 else
21917 {
224c3ddb 21918 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21919 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21920 return bytes;
21921 }
21922
21923 return NULL;
21924}
21925
21926/* Read a constant value from an attribute. Either set *VALUE, or if
21927 the value does not fit in *VALUE, set *BYTES - either already
21928 allocated on the objfile obstack, or newly allocated on OBSTACK,
21929 or, set *BATON, if we translated the constant to a location
21930 expression. */
21931
21932static void
ff39bb5e 21933dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21934 const char *name, struct obstack *obstack,
21935 struct dwarf2_cu *cu,
d521ce57 21936 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21937 struct dwarf2_locexpr_baton **baton)
21938{
5e22e966 21939 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21940 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21941 struct comp_unit_head *cu_header = &cu->header;
c906108c 21942 struct dwarf_block *blk;
98bfdba5
PA
21943 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21944 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21945
21946 *value = 0;
21947 *bytes = NULL;
21948 *baton = NULL;
c906108c
SS
21949
21950 switch (attr->form)
21951 {
21952 case DW_FORM_addr:
336d760d 21953 case DW_FORM_addrx:
3019eac3 21954 case DW_FORM_GNU_addr_index:
ac56253d 21955 {
ac56253d
TT
21956 gdb_byte *data;
21957
98bfdba5
PA
21958 if (TYPE_LENGTH (type) != cu_header->addr_size)
21959 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21960 cu_header->addr_size,
98bfdba5 21961 TYPE_LENGTH (type));
ac56253d
TT
21962 /* Symbols of this form are reasonably rare, so we just
21963 piggyback on the existing location code rather than writing
21964 a new implementation of symbol_computed_ops. */
8d749320 21965 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21966 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21967 (*baton)->per_cu = cu->per_cu;
21968 gdb_assert ((*baton)->per_cu);
ac56253d 21969
98bfdba5 21970 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21971 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21972 (*baton)->data = data;
ac56253d
TT
21973
21974 data[0] = DW_OP_addr;
21975 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 21976 byte_order, attr->as_address ());
ac56253d 21977 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21978 }
c906108c 21979 break;
4ac36638 21980 case DW_FORM_string:
93b5768b 21981 case DW_FORM_strp:
cf532bd1 21982 case DW_FORM_strx:
3019eac3 21983 case DW_FORM_GNU_str_index:
36586728 21984 case DW_FORM_GNU_strp_alt:
c6481205 21985 /* The string is already allocated on the objfile obstack, point
98bfdba5 21986 directly to it. */
2c830f54 21987 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 21988 break;
c906108c
SS
21989 case DW_FORM_block1:
21990 case DW_FORM_block2:
21991 case DW_FORM_block4:
21992 case DW_FORM_block:
2dc7f7b3 21993 case DW_FORM_exprloc:
0224619f 21994 case DW_FORM_data16:
9d2246fc 21995 blk = attr->as_block ();
98bfdba5
PA
21996 if (TYPE_LENGTH (type) != blk->size)
21997 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21998 TYPE_LENGTH (type));
21999 *bytes = blk->data;
c906108c 22000 break;
2df3850c
JM
22001
22002 /* The DW_AT_const_value attributes are supposed to carry the
22003 symbol's value "represented as it would be on the target
22004 architecture." By the time we get here, it's already been
22005 converted to host endianness, so we just need to sign- or
22006 zero-extend it as appropriate. */
22007 case DW_FORM_data1:
3aef2284 22008 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22009 break;
c906108c 22010 case DW_FORM_data2:
3aef2284 22011 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22012 break;
c906108c 22013 case DW_FORM_data4:
3aef2284 22014 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22015 break;
c906108c 22016 case DW_FORM_data8:
3aef2284 22017 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22018 break;
22019
c906108c 22020 case DW_FORM_sdata:
663c44ac 22021 case DW_FORM_implicit_const:
1bc397c5 22022 *value = attr->as_signed ();
2df3850c
JM
22023 break;
22024
c906108c 22025 case DW_FORM_udata:
529908cb 22026 *value = attr->as_unsigned ();
c906108c 22027 break;
2df3850c 22028
c906108c 22029 default:
b98664d3 22030 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22031 dwarf_form_name (attr->form));
98bfdba5 22032 *value = 0;
c906108c
SS
22033 break;
22034 }
22035}
22036
2df3850c 22037
98bfdba5
PA
22038/* Copy constant value from an attribute to a symbol. */
22039
2df3850c 22040static void
ff39bb5e 22041dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22042 struct dwarf2_cu *cu)
2df3850c 22043{
5e22e966 22044 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22045 LONGEST value;
d521ce57 22046 const gdb_byte *bytes;
98bfdba5 22047 struct dwarf2_locexpr_baton *baton;
2df3850c 22048
98bfdba5 22049 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22050 sym->print_name (),
98bfdba5
PA
22051 &objfile->objfile_obstack, cu,
22052 &value, &bytes, &baton);
2df3850c 22053
98bfdba5
PA
22054 if (baton != NULL)
22055 {
98bfdba5 22056 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22057 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22058 }
22059 else if (bytes != NULL)
22060 {
22061 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22062 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22063 }
22064 else
22065 {
22066 SYMBOL_VALUE (sym) = value;
f1e6e072 22067 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22068 }
2df3850c
JM
22069}
22070
c906108c
SS
22071/* Return the type of the die in question using its DW_AT_type attribute. */
22072
22073static struct type *
e7c27a73 22074die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22075{
c906108c 22076 struct attribute *type_attr;
c906108c 22077
e142c38c 22078 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22079 if (!type_attr)
22080 {
5e22e966 22081 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22082 /* A missing DW_AT_type represents a void type. */
518817b3 22083 return objfile_type (objfile)->builtin_void;
c906108c 22084 }
348e048f 22085
673bfd45 22086 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22087}
22088
b4ba55a1
JB
22089/* True iff CU's producer generates GNAT Ada auxiliary information
22090 that allows to find parallel types through that information instead
22091 of having to do expensive parallel lookups by type name. */
22092
22093static int
22094need_gnat_info (struct dwarf2_cu *cu)
22095{
de4cb04a
JB
22096 /* Assume that the Ada compiler was GNAT, which always produces
22097 the auxiliary information. */
22098 return (cu->language == language_ada);
b4ba55a1
JB
22099}
22100
b4ba55a1
JB
22101/* Return the auxiliary type of the die in question using its
22102 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22103 attribute is not present. */
22104
22105static struct type *
22106die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22107{
b4ba55a1 22108 struct attribute *type_attr;
b4ba55a1
JB
22109
22110 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22111 if (!type_attr)
22112 return NULL;
22113
673bfd45 22114 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22115}
22116
22117/* If DIE has a descriptive_type attribute, then set the TYPE's
22118 descriptive type accordingly. */
22119
22120static void
22121set_descriptive_type (struct type *type, struct die_info *die,
22122 struct dwarf2_cu *cu)
22123{
22124 struct type *descriptive_type = die_descriptive_type (die, cu);
22125
22126 if (descriptive_type)
22127 {
22128 ALLOCATE_GNAT_AUX_TYPE (type);
22129 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22130 }
22131}
22132
c906108c
SS
22133/* Return the containing type of the die in question using its
22134 DW_AT_containing_type attribute. */
22135
22136static struct type *
e7c27a73 22137die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22138{
c906108c 22139 struct attribute *type_attr;
5e22e966 22140 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22141
e142c38c 22142 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22143 if (!type_attr)
22144 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22145 "[in module %s]"), objfile_name (objfile));
33ac96f0 22146
673bfd45 22147 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22148}
22149
ac9ec31b
DE
22150/* Return an error marker type to use for the ill formed type in DIE/CU. */
22151
22152static struct type *
22153build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22154{
976ca316
SM
22155 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22156 struct objfile *objfile = per_objfile->objfile;
528e1572 22157 char *saved;
ac9ec31b 22158
528e1572
SM
22159 std::string message
22160 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22161 objfile_name (objfile),
22162 sect_offset_str (cu->header.sect_off),
22163 sect_offset_str (die->sect_off));
efba19b0 22164 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22165
19f392bc 22166 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22167}
22168
673bfd45 22169/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22170 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22171 DW_AT_containing_type.
673bfd45
DE
22172 If there is no type substitute an error marker. */
22173
c906108c 22174static struct type *
ff39bb5e 22175lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22176 struct dwarf2_cu *cu)
c906108c 22177{
976ca316
SM
22178 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22179 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22180 struct type *this_type;
22181
ac9ec31b
DE
22182 gdb_assert (attr->name == DW_AT_type
22183 || attr->name == DW_AT_GNAT_descriptive_type
22184 || attr->name == DW_AT_containing_type);
22185
673bfd45
DE
22186 /* First see if we have it cached. */
22187
36586728
TT
22188 if (attr->form == DW_FORM_GNU_ref_alt)
22189 {
22190 struct dwarf2_per_cu_data *per_cu;
0826b30a 22191 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22192
976ca316
SM
22193 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22194 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22195 }
cd6c91b4 22196 else if (attr->form_is_ref ())
673bfd45 22197 {
0826b30a 22198 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22199
976ca316 22200 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22201 }
55f1336d 22202 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22203 {
630ed6b9 22204 ULONGEST signature = attr->as_signature ();
673bfd45 22205
ac9ec31b 22206 return get_signatured_type (die, signature, cu);
673bfd45
DE
22207 }
22208 else
22209 {
b98664d3 22210 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22211 " at %s [in module %s]"),
22212 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22213 objfile_name (objfile));
ac9ec31b 22214 return build_error_marker_type (cu, die);
673bfd45
DE
22215 }
22216
22217 /* If not cached we need to read it in. */
22218
22219 if (this_type == NULL)
22220 {
ac9ec31b 22221 struct die_info *type_die = NULL;
673bfd45
DE
22222 struct dwarf2_cu *type_cu = cu;
22223
cd6c91b4 22224 if (attr->form_is_ref ())
ac9ec31b
DE
22225 type_die = follow_die_ref (die, attr, &type_cu);
22226 if (type_die == NULL)
22227 return build_error_marker_type (cu, die);
22228 /* If we find the type now, it's probably because the type came
3019eac3
DE
22229 from an inter-CU reference and the type's CU got expanded before
22230 ours. */
ac9ec31b 22231 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22232 }
22233
22234 /* If we still don't have a type use an error marker. */
22235
22236 if (this_type == NULL)
ac9ec31b 22237 return build_error_marker_type (cu, die);
673bfd45 22238
f792889a 22239 return this_type;
c906108c
SS
22240}
22241
673bfd45
DE
22242/* Return the type in DIE, CU.
22243 Returns NULL for invalid types.
22244
02142a6c 22245 This first does a lookup in die_type_hash,
673bfd45
DE
22246 and only reads the die in if necessary.
22247
22248 NOTE: This can be called when reading in partial or full symbols. */
22249
f792889a 22250static struct type *
e7c27a73 22251read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22252{
f792889a
DJ
22253 struct type *this_type;
22254
22255 this_type = get_die_type (die, cu);
22256 if (this_type)
22257 return this_type;
22258
673bfd45
DE
22259 return read_type_die_1 (die, cu);
22260}
22261
22262/* Read the type in DIE, CU.
22263 Returns NULL for invalid types. */
22264
22265static struct type *
22266read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22267{
22268 struct type *this_type = NULL;
22269
c906108c
SS
22270 switch (die->tag)
22271 {
22272 case DW_TAG_class_type:
680b30c7 22273 case DW_TAG_interface_type:
c906108c
SS
22274 case DW_TAG_structure_type:
22275 case DW_TAG_union_type:
f792889a 22276 this_type = read_structure_type (die, cu);
c906108c
SS
22277 break;
22278 case DW_TAG_enumeration_type:
f792889a 22279 this_type = read_enumeration_type (die, cu);
c906108c
SS
22280 break;
22281 case DW_TAG_subprogram:
22282 case DW_TAG_subroutine_type:
edb3359d 22283 case DW_TAG_inlined_subroutine:
f792889a 22284 this_type = read_subroutine_type (die, cu);
c906108c
SS
22285 break;
22286 case DW_TAG_array_type:
f792889a 22287 this_type = read_array_type (die, cu);
c906108c 22288 break;
72019c9c 22289 case DW_TAG_set_type:
f792889a 22290 this_type = read_set_type (die, cu);
72019c9c 22291 break;
c906108c 22292 case DW_TAG_pointer_type:
f792889a 22293 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22294 break;
22295 case DW_TAG_ptr_to_member_type:
f792889a 22296 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22297 break;
22298 case DW_TAG_reference_type:
4297a3f0
AV
22299 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22300 break;
22301 case DW_TAG_rvalue_reference_type:
22302 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22303 break;
22304 case DW_TAG_const_type:
f792889a 22305 this_type = read_tag_const_type (die, cu);
c906108c
SS
22306 break;
22307 case DW_TAG_volatile_type:
f792889a 22308 this_type = read_tag_volatile_type (die, cu);
c906108c 22309 break;
06d66ee9
TT
22310 case DW_TAG_restrict_type:
22311 this_type = read_tag_restrict_type (die, cu);
22312 break;
c906108c 22313 case DW_TAG_string_type:
f792889a 22314 this_type = read_tag_string_type (die, cu);
c906108c
SS
22315 break;
22316 case DW_TAG_typedef:
f792889a 22317 this_type = read_typedef (die, cu);
c906108c 22318 break;
a02abb62 22319 case DW_TAG_subrange_type:
f792889a 22320 this_type = read_subrange_type (die, cu);
a02abb62 22321 break;
c906108c 22322 case DW_TAG_base_type:
f792889a 22323 this_type = read_base_type (die, cu);
c906108c 22324 break;
81a17f79 22325 case DW_TAG_unspecified_type:
f792889a 22326 this_type = read_unspecified_type (die, cu);
81a17f79 22327 break;
0114d602
DJ
22328 case DW_TAG_namespace:
22329 this_type = read_namespace_type (die, cu);
22330 break;
f55ee35c
JK
22331 case DW_TAG_module:
22332 this_type = read_module_type (die, cu);
22333 break;
a2c2acaf
MW
22334 case DW_TAG_atomic_type:
22335 this_type = read_tag_atomic_type (die, cu);
22336 break;
c906108c 22337 default:
b98664d3 22338 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22339 dwarf_tag_name (die->tag));
c906108c
SS
22340 break;
22341 }
63d06c5c 22342
f792889a 22343 return this_type;
63d06c5c
DC
22344}
22345
abc72ce4
DE
22346/* See if we can figure out if the class lives in a namespace. We do
22347 this by looking for a member function; its demangled name will
22348 contain namespace info, if there is any.
22349 Return the computed name or NULL.
22350 Space for the result is allocated on the objfile's obstack.
22351 This is the full-die version of guess_partial_die_structure_name.
22352 In this case we know DIE has no useful parent. */
22353
43816ebc 22354static const char *
abc72ce4
DE
22355guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22356{
22357 struct die_info *spec_die;
22358 struct dwarf2_cu *spec_cu;
22359 struct die_info *child;
5e22e966 22360 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22361
22362 spec_cu = cu;
22363 spec_die = die_specification (die, &spec_cu);
22364 if (spec_die != NULL)
22365 {
22366 die = spec_die;
22367 cu = spec_cu;
22368 }
22369
22370 for (child = die->child;
22371 child != NULL;
22372 child = child->sibling)
22373 {
22374 if (child->tag == DW_TAG_subprogram)
22375 {
73b9be8b 22376 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22377
7d45c7c3 22378 if (linkage_name != NULL)
abc72ce4 22379 {
43816ebc 22380 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22381 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22382 const char *name = NULL;
abc72ce4
DE
22383
22384 if (actual_name != NULL)
22385 {
15d034d0 22386 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22387
22388 if (die_name != NULL
43816ebc 22389 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22390 {
22391 /* Strip off the class name from the full name.
22392 We want the prefix. */
22393 int die_name_len = strlen (die_name);
43816ebc
TT
22394 int actual_name_len = strlen (actual_name.get ());
22395 const char *ptr = actual_name.get ();
abc72ce4
DE
22396
22397 /* Test for '::' as a sanity check. */
22398 if (actual_name_len > die_name_len + 2
43816ebc 22399 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22400 name = obstack_strndup (
e3b94546 22401 &objfile->per_bfd->storage_obstack,
43816ebc 22402 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22403 }
22404 }
abc72ce4
DE
22405 return name;
22406 }
22407 }
22408 }
22409
22410 return NULL;
22411}
22412
96408a79
SA
22413/* GCC might emit a nameless typedef that has a linkage name. Determine the
22414 prefix part in such case. See
22415 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22416
a121b7c1 22417static const char *
96408a79
SA
22418anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22419{
22420 struct attribute *attr;
e6a959d6 22421 const char *base;
96408a79
SA
22422
22423 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22424 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22425 return NULL;
22426
7d45c7c3 22427 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22428 return NULL;
22429
73b9be8b 22430 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22431 const char *attr_name = attr->as_string ();
22432 if (attr == NULL || attr_name == NULL)
96408a79
SA
22433 return NULL;
22434
22435 /* dwarf2_name had to be already called. */
3b64bf15 22436 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22437
22438 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22439 base = strrchr (attr_name, ':');
22440 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22441 return "";
22442
5e22e966 22443 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22444 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22445 attr_name,
22446 &base[-1] - attr_name);
96408a79
SA
22447}
22448
fdde2d81 22449/* Return the name of the namespace/class that DIE is defined within,
0114d602 22450 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22451
0114d602
DJ
22452 For example, if we're within the method foo() in the following
22453 code:
22454
22455 namespace N {
22456 class C {
22457 void foo () {
22458 }
22459 };
22460 }
22461
22462 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22463
0d5cff50 22464static const char *
e142c38c 22465determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22466{
976ca316 22467 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
22468 struct die_info *parent, *spec_die;
22469 struct dwarf2_cu *spec_cu;
22470 struct type *parent_type;
a121b7c1 22471 const char *retval;
63d06c5c 22472
9c37b5ae 22473 if (cu->language != language_cplus
c44af4eb
TT
22474 && cu->language != language_fortran && cu->language != language_d
22475 && cu->language != language_rust)
0114d602
DJ
22476 return "";
22477
96408a79
SA
22478 retval = anonymous_struct_prefix (die, cu);
22479 if (retval)
22480 return retval;
22481
0114d602
DJ
22482 /* We have to be careful in the presence of DW_AT_specification.
22483 For example, with GCC 3.4, given the code
22484
22485 namespace N {
22486 void foo() {
22487 // Definition of N::foo.
22488 }
22489 }
22490
22491 then we'll have a tree of DIEs like this:
22492
22493 1: DW_TAG_compile_unit
22494 2: DW_TAG_namespace // N
22495 3: DW_TAG_subprogram // declaration of N::foo
22496 4: DW_TAG_subprogram // definition of N::foo
22497 DW_AT_specification // refers to die #3
22498
22499 Thus, when processing die #4, we have to pretend that we're in
22500 the context of its DW_AT_specification, namely the contex of die
22501 #3. */
22502 spec_cu = cu;
22503 spec_die = die_specification (die, &spec_cu);
22504 if (spec_die == NULL)
22505 parent = die->parent;
22506 else
63d06c5c 22507 {
0114d602
DJ
22508 parent = spec_die->parent;
22509 cu = spec_cu;
63d06c5c 22510 }
0114d602
DJ
22511
22512 if (parent == NULL)
22513 return "";
98bfdba5
PA
22514 else if (parent->building_fullname)
22515 {
22516 const char *name;
22517 const char *parent_name;
22518
22519 /* It has been seen on RealView 2.2 built binaries,
22520 DW_TAG_template_type_param types actually _defined_ as
22521 children of the parent class:
22522
22523 enum E {};
22524 template class <class Enum> Class{};
22525 Class<enum E> class_e;
22526
dda83cd7
SM
22527 1: DW_TAG_class_type (Class)
22528 2: DW_TAG_enumeration_type (E)
22529 3: DW_TAG_enumerator (enum1:0)
22530 3: DW_TAG_enumerator (enum2:1)
22531 ...
22532 2: DW_TAG_template_type_param
22533 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
22534
22535 Besides being broken debug info, it can put GDB into an
22536 infinite loop. Consider:
22537
22538 When we're building the full name for Class<E>, we'll start
22539 at Class, and go look over its template type parameters,
22540 finding E. We'll then try to build the full name of E, and
22541 reach here. We're now trying to build the full name of E,
22542 and look over the parent DIE for containing scope. In the
22543 broken case, if we followed the parent DIE of E, we'd again
22544 find Class, and once again go look at its template type
22545 arguments, etc., etc. Simply don't consider such parent die
22546 as source-level parent of this die (it can't be, the language
22547 doesn't allow it), and break the loop here. */
22548 name = dwarf2_name (die, cu);
22549 parent_name = dwarf2_name (parent, cu);
b98664d3 22550 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22551 name ? name : "<unknown>",
22552 parent_name ? parent_name : "<unknown>");
22553 return "";
22554 }
63d06c5c 22555 else
0114d602
DJ
22556 switch (parent->tag)
22557 {
63d06c5c 22558 case DW_TAG_namespace:
0114d602 22559 parent_type = read_type_die (parent, cu);
acebe513
UW
22560 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22561 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22562 Work around this problem here. */
22563 if (cu->language == language_cplus
7d93a1e0 22564 && strcmp (parent_type->name (), "::") == 0)
acebe513 22565 return "";
0114d602 22566 /* We give a name to even anonymous namespaces. */
7d93a1e0 22567 return parent_type->name ();
63d06c5c 22568 case DW_TAG_class_type:
680b30c7 22569 case DW_TAG_interface_type:
63d06c5c 22570 case DW_TAG_structure_type:
0114d602 22571 case DW_TAG_union_type:
f55ee35c 22572 case DW_TAG_module:
0114d602 22573 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
22574 if (parent_type->name () != NULL)
22575 return parent_type->name ();
0114d602
DJ
22576 else
22577 /* An anonymous structure is only allowed non-static data
22578 members; no typedefs, no member functions, et cetera.
22579 So it does not need a prefix. */
22580 return "";
abc72ce4 22581 case DW_TAG_compile_unit:
95554aad 22582 case DW_TAG_partial_unit:
abc72ce4
DE
22583 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22584 if (cu->language == language_cplus
976ca316 22585 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
22586 && die->child != NULL
22587 && (die->tag == DW_TAG_class_type
22588 || die->tag == DW_TAG_structure_type
22589 || die->tag == DW_TAG_union_type))
22590 {
43816ebc 22591 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22592 if (name != NULL)
22593 return name;
22594 }
22595 return "";
0a4b0913
AB
22596 case DW_TAG_subprogram:
22597 /* Nested subroutines in Fortran get a prefix with the name
22598 of the parent's subroutine. */
22599 if (cu->language == language_fortran)
22600 {
22601 if ((die->tag == DW_TAG_subprogram)
22602 && (dwarf2_name (parent, cu) != NULL))
22603 return dwarf2_name (parent, cu);
22604 }
22605 return determine_prefix (parent, cu);
3d567982
TT
22606 case DW_TAG_enumeration_type:
22607 parent_type = read_type_die (parent, cu);
3bc440a2 22608 if (parent_type->is_declared_class ())
3d567982 22609 {
7d93a1e0
SM
22610 if (parent_type->name () != NULL)
22611 return parent_type->name ();
3d567982
TT
22612 return "";
22613 }
22614 /* Fall through. */
63d06c5c 22615 default:
8176b9b8 22616 return determine_prefix (parent, cu);
63d06c5c 22617 }
63d06c5c
DC
22618}
22619
3e43a32a
MS
22620/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22621 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22622 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22623 an obconcat, otherwise allocate storage for the result. The CU argument is
22624 used to determine the language and hence, the appropriate separator. */
987504bb 22625
f55ee35c 22626#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22627
22628static char *
f55ee35c 22629typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 22630 int physname, struct dwarf2_cu *cu)
63d06c5c 22631{
f55ee35c 22632 const char *lead = "";
5c315b68 22633 const char *sep;
63d06c5c 22634
3e43a32a
MS
22635 if (suffix == NULL || suffix[0] == '\0'
22636 || prefix == NULL || prefix[0] == '\0')
987504bb 22637 sep = "";
45280282
IB
22638 else if (cu->language == language_d)
22639 {
22640 /* For D, the 'main' function could be defined in any module, but it
22641 should never be prefixed. */
22642 if (strcmp (suffix, "D main") == 0)
22643 {
22644 prefix = "";
22645 sep = "";
22646 }
22647 else
22648 sep = ".";
22649 }
f55ee35c
JK
22650 else if (cu->language == language_fortran && physname)
22651 {
22652 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22653 DW_AT_MIPS_linkage_name is preferred and used instead. */
22654
22655 lead = "__";
22656 sep = "_MOD_";
22657 }
987504bb
JJ
22658 else
22659 sep = "::";
63d06c5c 22660
6dd47d34
DE
22661 if (prefix == NULL)
22662 prefix = "";
22663 if (suffix == NULL)
22664 suffix = "";
22665
987504bb
JJ
22666 if (obs == NULL)
22667 {
3e43a32a 22668 char *retval
224c3ddb
SM
22669 = ((char *)
22670 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22671
f55ee35c
JK
22672 strcpy (retval, lead);
22673 strcat (retval, prefix);
6dd47d34
DE
22674 strcat (retval, sep);
22675 strcat (retval, suffix);
63d06c5c
DC
22676 return retval;
22677 }
987504bb
JJ
22678 else
22679 {
22680 /* We have an obstack. */
f55ee35c 22681 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22682 }
63d06c5c
DC
22683}
22684
71c25dea
TT
22685/* Get name of a die, return NULL if not found. */
22686
15d034d0
TT
22687static const char *
22688dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 22689 struct objfile *objfile)
71c25dea
TT
22690{
22691 if (name && cu->language == language_cplus)
22692 {
596dc4ad
TT
22693 gdb::unique_xmalloc_ptr<char> canon_name
22694 = cp_canonicalize_string (name);
71c25dea 22695
596dc4ad
TT
22696 if (canon_name != nullptr)
22697 name = objfile->intern (canon_name.get ());
71c25dea
TT
22698 }
22699
22700 return name;
c906108c
SS
22701}
22702
96553a0c
DE
22703/* Get name of a die, return NULL if not found.
22704 Anonymous namespaces are converted to their magic string. */
9219021c 22705
15d034d0 22706static const char *
e142c38c 22707dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22708{
22709 struct attribute *attr;
5e22e966 22710 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 22711
e142c38c 22712 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
22713 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22714 if (attr_name == nullptr
96553a0c 22715 && die->tag != DW_TAG_namespace
53832f31
TT
22716 && die->tag != DW_TAG_class_type
22717 && die->tag != DW_TAG_interface_type
22718 && die->tag != DW_TAG_structure_type
22719 && die->tag != DW_TAG_union_type)
71c25dea
TT
22720 return NULL;
22721
22722 switch (die->tag)
22723 {
22724 case DW_TAG_compile_unit:
95554aad 22725 case DW_TAG_partial_unit:
71c25dea
TT
22726 /* Compilation units have a DW_AT_name that is a filename, not
22727 a source language identifier. */
22728 case DW_TAG_enumeration_type:
22729 case DW_TAG_enumerator:
22730 /* These tags always have simple identifiers already; no need
22731 to canonicalize them. */
2c830f54 22732 return attr_name;
907af001 22733
96553a0c 22734 case DW_TAG_namespace:
2c830f54
TT
22735 if (attr_name != nullptr)
22736 return attr_name;
96553a0c
DE
22737 return CP_ANONYMOUS_NAMESPACE_STR;
22738
907af001
UW
22739 case DW_TAG_class_type:
22740 case DW_TAG_interface_type:
22741 case DW_TAG_structure_type:
22742 case DW_TAG_union_type:
22743 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22744 structures or unions. These were of the form "._%d" in GCC 4.1,
22745 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22746 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
22747 if (attr_name != nullptr
22748 && (startswith (attr_name, "._")
22749 || startswith (attr_name, "<anonymous")))
907af001 22750 return NULL;
53832f31
TT
22751
22752 /* GCC might emit a nameless typedef that has a linkage name. See
22753 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 22754 if (!attr || attr_name == NULL)
53832f31 22755 {
73b9be8b 22756 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 22757 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 22758 if (attr == NULL || attr_name == NULL)
53832f31
TT
22759 return NULL;
22760
2c830f54 22761 /* Avoid demangling attr_name the second time on a second
df5c6c50 22762 call for the same DIE. */
3b64bf15 22763 if (!attr->canonical_string_p ())
53832f31 22764 {
43816ebc 22765 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 22766 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
22767 if (demangled == nullptr)
22768 return nullptr;
43816ebc 22769
c6481205 22770 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 22771 attr_name = attr->as_string ();
53832f31 22772 }
67430cd0 22773
2c830f54
TT
22774 /* Strip any leading namespaces/classes, keep only the
22775 base name. DW_AT_name for named DIEs does not
22776 contain the prefixes. */
22777 const char *base = strrchr (attr_name, ':');
22778 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
22779 return &base[1];
22780 else
2c830f54 22781 return attr_name;
53832f31 22782 }
907af001
UW
22783 break;
22784
71c25dea 22785 default:
907af001
UW
22786 break;
22787 }
22788
3b64bf15 22789 if (!attr->canonical_string_p ())
c6481205
TT
22790 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22791 objfile));
2c830f54 22792 return attr->as_string ();
9219021c
DC
22793}
22794
22795/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22796 is none. *EXT_CU is the CU containing DIE on input, and the CU
22797 containing the return value on output. */
9219021c
DC
22798
22799static struct die_info *
f2f0e013 22800dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22801{
22802 struct attribute *attr;
9219021c 22803
f2f0e013 22804 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22805 if (attr == NULL)
22806 return NULL;
22807
f2f0e013 22808 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22809}
22810
f9aca02d 22811static void
d97bc12b 22812dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22813{
22814 unsigned int i;
22815
d97bc12b 22816 print_spaces (indent, f);
9d8780f0 22817 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22818 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22819 sect_offset_str (die->sect_off));
d97bc12b
DE
22820
22821 if (die->parent != NULL)
22822 {
22823 print_spaces (indent, f);
9d8780f0
SM
22824 fprintf_unfiltered (f, " parent at offset: %s\n",
22825 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22826 }
22827
22828 print_spaces (indent, f);
22829 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22830 dwarf_bool_name (die->child != NULL));
c906108c 22831
d97bc12b
DE
22832 print_spaces (indent, f);
22833 fprintf_unfiltered (f, " attributes:\n");
22834
c906108c
SS
22835 for (i = 0; i < die->num_attrs; ++i)
22836 {
d97bc12b
DE
22837 print_spaces (indent, f);
22838 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22839 dwarf_attr_name (die->attrs[i].name),
22840 dwarf_form_name (die->attrs[i].form));
d97bc12b 22841
c906108c
SS
22842 switch (die->attrs[i].form)
22843 {
c906108c 22844 case DW_FORM_addr:
336d760d 22845 case DW_FORM_addrx:
3019eac3 22846 case DW_FORM_GNU_addr_index:
d97bc12b 22847 fprintf_unfiltered (f, "address: ");
36d378cf 22848 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
22849 break;
22850 case DW_FORM_block2:
22851 case DW_FORM_block4:
22852 case DW_FORM_block:
22853 case DW_FORM_block1:
56eb65bd 22854 fprintf_unfiltered (f, "block: size %s",
9d2246fc 22855 pulongest (die->attrs[i].as_block ()->size));
c906108c 22856 break;
2dc7f7b3 22857 case DW_FORM_exprloc:
56eb65bd 22858 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 22859 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 22860 break;
0224619f
JK
22861 case DW_FORM_data16:
22862 fprintf_unfiltered (f, "constant of 16 bytes");
22863 break;
4568ecf9
DE
22864 case DW_FORM_ref_addr:
22865 fprintf_unfiltered (f, "ref address: ");
529908cb 22866 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 22867 break;
36586728
TT
22868 case DW_FORM_GNU_ref_alt:
22869 fprintf_unfiltered (f, "alt ref address: ");
529908cb 22870 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 22871 break;
10b3939b
DJ
22872 case DW_FORM_ref1:
22873 case DW_FORM_ref2:
22874 case DW_FORM_ref4:
4568ecf9
DE
22875 case DW_FORM_ref8:
22876 case DW_FORM_ref_udata:
d97bc12b 22877 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 22878 (long) (die->attrs[i].as_unsigned ()));
10b3939b 22879 break;
c906108c
SS
22880 case DW_FORM_data1:
22881 case DW_FORM_data2:
22882 case DW_FORM_data4:
ce5d95e1 22883 case DW_FORM_data8:
c906108c 22884 case DW_FORM_udata:
43bbcdc2 22885 fprintf_unfiltered (f, "constant: %s",
529908cb 22886 pulongest (die->attrs[i].as_unsigned ()));
c906108c 22887 break;
2dc7f7b3
TT
22888 case DW_FORM_sec_offset:
22889 fprintf_unfiltered (f, "section offset: %s",
529908cb 22890 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 22891 break;
55f1336d 22892 case DW_FORM_ref_sig8:
ac9ec31b 22893 fprintf_unfiltered (f, "signature: %s",
630ed6b9 22894 hex_string (die->attrs[i].as_signature ()));
348e048f 22895 break;
c906108c 22896 case DW_FORM_string:
4bdf3d34 22897 case DW_FORM_strp:
43988095 22898 case DW_FORM_line_strp:
cf532bd1 22899 case DW_FORM_strx:
3019eac3 22900 case DW_FORM_GNU_str_index:
36586728 22901 case DW_FORM_GNU_strp_alt:
8285870a 22902 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
22903 die->attrs[i].as_string ()
22904 ? die->attrs[i].as_string () : "",
22905 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
22906 break;
22907 case DW_FORM_flag:
c45bc3f8 22908 if (die->attrs[i].as_boolean ())
d97bc12b 22909 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22910 else
d97bc12b 22911 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22912 break;
2dc7f7b3
TT
22913 case DW_FORM_flag_present:
22914 fprintf_unfiltered (f, "flag: TRUE");
22915 break;
a8329558 22916 case DW_FORM_indirect:
0963b4bd
MS
22917 /* The reader will have reduced the indirect form to
22918 the "base form" so this form should not occur. */
5f48f8f3 22919 fprintf_unfiltered (f,
3e43a32a 22920 "unexpected attribute form: DW_FORM_indirect");
a8329558 22921 break;
1bc397c5 22922 case DW_FORM_sdata:
663c44ac
JK
22923 case DW_FORM_implicit_const:
22924 fprintf_unfiltered (f, "constant: %s",
1bc397c5 22925 plongest (die->attrs[i].as_signed ()));
663c44ac 22926 break;
c906108c 22927 default:
d97bc12b 22928 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22929 die->attrs[i].form);
d97bc12b 22930 break;
c906108c 22931 }
d97bc12b 22932 fprintf_unfiltered (f, "\n");
c906108c
SS
22933 }
22934}
22935
f9aca02d 22936static void
d97bc12b 22937dump_die_for_error (struct die_info *die)
c906108c 22938{
d97bc12b
DE
22939 dump_die_shallow (gdb_stderr, 0, die);
22940}
22941
22942static void
22943dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22944{
22945 int indent = level * 4;
22946
22947 gdb_assert (die != NULL);
22948
22949 if (level >= max_level)
22950 return;
22951
22952 dump_die_shallow (f, indent, die);
22953
22954 if (die->child != NULL)
c906108c 22955 {
d97bc12b
DE
22956 print_spaces (indent, f);
22957 fprintf_unfiltered (f, " Children:");
22958 if (level + 1 < max_level)
22959 {
22960 fprintf_unfiltered (f, "\n");
22961 dump_die_1 (f, level + 1, max_level, die->child);
22962 }
22963 else
22964 {
3e43a32a
MS
22965 fprintf_unfiltered (f,
22966 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22967 }
22968 }
22969
22970 if (die->sibling != NULL && level > 0)
22971 {
22972 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22973 }
22974}
22975
d97bc12b
DE
22976/* This is called from the pdie macro in gdbinit.in.
22977 It's not static so gcc will keep a copy callable from gdb. */
22978
22979void
22980dump_die (struct die_info *die, int max_level)
22981{
22982 dump_die_1 (gdb_stdlog, 0, max_level, die);
22983}
22984
f9aca02d 22985static void
51545339 22986store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22987{
51545339 22988 void **slot;
c906108c 22989
9c541725
PA
22990 slot = htab_find_slot_with_hash (cu->die_hash, die,
22991 to_underlying (die->sect_off),
b64f50a1 22992 INSERT);
51545339
DJ
22993
22994 *slot = die;
c906108c
SS
22995}
22996
348e048f
DE
22997/* Follow reference or signature attribute ATTR of SRC_DIE.
22998 On entry *REF_CU is the CU of SRC_DIE.
22999 On exit *REF_CU is the CU of the result. */
23000
23001static struct die_info *
ff39bb5e 23002follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23003 struct dwarf2_cu **ref_cu)
23004{
23005 struct die_info *die;
23006
cd6c91b4 23007 if (attr->form_is_ref ())
348e048f 23008 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23009 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23010 die = follow_die_sig (src_die, attr, ref_cu);
23011 else
23012 {
23013 dump_die_for_error (src_die);
23014 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23015 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23016 }
23017
23018 return die;
03dd20cc
DJ
23019}
23020
5c631832 23021/* Follow reference OFFSET.
673bfd45
DE
23022 On entry *REF_CU is the CU of the source die referencing OFFSET.
23023 On exit *REF_CU is the CU of the result.
23024 Returns NULL if OFFSET is invalid. */
f504f079 23025
f9aca02d 23026static struct die_info *
9c541725 23027follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23028 struct dwarf2_cu **ref_cu)
c906108c 23029{
10b3939b 23030 struct die_info temp_die;
f2f0e013 23031 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23032 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23033
348e048f
DE
23034 gdb_assert (cu->per_cu != NULL);
23035
98bfdba5
PA
23036 target_cu = cu;
23037
17e593e9
SM
23038 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23039 "source CU contains target offset: %d",
23040 sect_offset_str (cu->per_cu->sect_off),
23041 sect_offset_str (sect_off),
23042 cu->header.offset_in_cu_p (sect_off));
23043
3019eac3 23044 if (cu->per_cu->is_debug_types)
348e048f
DE
23045 {
23046 /* .debug_types CUs cannot reference anything outside their CU.
23047 If they need to, they have to reference a signatured type via
55f1336d 23048 DW_FORM_ref_sig8. */
4057dfde 23049 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23050 return NULL;
348e048f 23051 }
36586728 23052 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23053 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23054 {
23055 struct dwarf2_per_cu_data *per_cu;
9a619af0 23056
9c541725 23057 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23058 per_objfile);
03dd20cc 23059
17e593e9
SM
23060 dwarf_read_debug_printf_v ("target CU offset: %s, "
23061 "target CU DIEs loaded: %d",
23062 sect_offset_str (per_cu->sect_off),
23063 per_objfile->get_cu (per_cu) != nullptr);
23064
616c069a
SM
23065 /* If necessary, add it to the queue and load its DIEs.
23066
23067 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23068 it doesn't mean they are currently loaded. Since we require them
23069 to be loaded, we must check for ourselves. */
23070 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23071 || per_objfile->get_cu (per_cu) == nullptr)
4a636814
SM
23072 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23073 false, cu->language);
03dd20cc 23074
976ca316 23075 target_cu = per_objfile->get_cu (per_cu);
616c069a 23076 gdb_assert (target_cu != nullptr);
10b3939b 23077 }
98bfdba5
PA
23078 else if (cu->dies == NULL)
23079 {
23080 /* We're loading full DIEs during partial symbol reading. */
976ca316 23081 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23082 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23083 language_minimal);
98bfdba5 23084 }
c906108c 23085
f2f0e013 23086 *ref_cu = target_cu;
9c541725 23087 temp_die.sect_off = sect_off;
c24bdb02 23088
3db19b2d
TV
23089 if (target_cu != cu)
23090 target_cu->ancestor = cu;
23091
9a3c8263 23092 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23093 &temp_die,
23094 to_underlying (sect_off));
5c631832 23095}
10b3939b 23096
5c631832
JK
23097/* Follow reference attribute ATTR of SRC_DIE.
23098 On entry *REF_CU is the CU of SRC_DIE.
23099 On exit *REF_CU is the CU of the result. */
23100
23101static struct die_info *
ff39bb5e 23102follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23103 struct dwarf2_cu **ref_cu)
23104{
0826b30a 23105 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23106 struct dwarf2_cu *cu = *ref_cu;
23107 struct die_info *die;
23108
9c541725 23109 die = follow_die_offset (sect_off,
36586728
TT
23110 (attr->form == DW_FORM_GNU_ref_alt
23111 || cu->per_cu->is_dwz),
23112 ref_cu);
5c631832 23113 if (!die)
9d8780f0
SM
23114 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23115 "at %s [in module %s]"),
23116 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23117 objfile_name (cu->per_objfile->objfile));
348e048f 23118
5c631832
JK
23119 return die;
23120}
23121
d4c9a4f8 23122/* See read.h. */
5c631832
JK
23123
23124struct dwarf2_locexpr_baton
9c541725 23125dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23126 dwarf2_per_cu_data *per_cu,
976ca316 23127 dwarf2_per_objfile *per_objfile,
041d9819
SM
23128 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23129 bool resolve_abstract_p)
5c631832 23130{
5c631832
JK
23131 struct die_info *die;
23132 struct attribute *attr;
23133 struct dwarf2_locexpr_baton retval;
976ca316 23134 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23135
976ca316 23136 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23137 if (cu == nullptr)
976ca316 23138 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23139
23140 if (cu == nullptr)
cc12ce38
DE
23141 {
23142 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23143 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23144 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23145 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23146 }
918dd910 23147
9c541725 23148 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23149 if (!die)
9d8780f0
SM
23150 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23151 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23152
23153 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23154 if (!attr && resolve_abstract_p
976ca316
SM
23155 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23156 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23157 {
041d9819 23158 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23159 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23160 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23161
3360b6e7 23162 for (const auto &cand_off
976ca316 23163 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23164 {
3360b6e7
TV
23165 struct dwarf2_cu *cand_cu = cu;
23166 struct die_info *cand
23167 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23168 if (!cand
23169 || !cand->parent
e4a62c65
TV
23170 || cand->parent->tag != DW_TAG_subprogram)
23171 continue;
23172
23173 CORE_ADDR pc_low, pc_high;
23174 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23175 if (pc_low == ((CORE_ADDR) -1))
23176 continue;
23177 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23178 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23179 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23180 continue;
23181
23182 die = cand;
23183 attr = dwarf2_attr (die, DW_AT_location, cu);
23184 break;
23185 }
23186 }
23187
5c631832
JK
23188 if (!attr)
23189 {
e103e986
JK
23190 /* DWARF: "If there is no such attribute, then there is no effect.".
23191 DATA is ignored if SIZE is 0. */
5c631832 23192
e103e986 23193 retval.data = NULL;
5c631832
JK
23194 retval.size = 0;
23195 }
cd6c91b4 23196 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23197 {
23198 struct dwarf2_loclist_baton loclist_baton;
041d9819 23199 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23200 size_t size;
23201
23202 fill_in_loclist_baton (cu, &loclist_baton, attr);
23203
23204 retval.data = dwarf2_find_location_expression (&loclist_baton,
23205 &size, pc);
23206 retval.size = size;
23207 }
5c631832
JK
23208 else
23209 {
4fc6c0d5 23210 if (!attr->form_is_block ())
9d8780f0 23211 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23212 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23213 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23214
9d2246fc
TT
23215 struct dwarf_block *block = attr->as_block ();
23216 retval.data = block->data;
23217 retval.size = block->size;
5c631832 23218 }
976ca316 23219 retval.per_objfile = per_objfile;
5c631832 23220 retval.per_cu = cu->per_cu;
918dd910 23221
976ca316 23222 per_objfile->age_comp_units ();
918dd910 23223
5c631832 23224 return retval;
348e048f
DE
23225}
23226
d4c9a4f8 23227/* See read.h. */
8b9737bf
TT
23228
23229struct dwarf2_locexpr_baton
23230dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23231 dwarf2_per_cu_data *per_cu,
14095eb3 23232 dwarf2_per_objfile *per_objfile,
041d9819 23233 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23234{
9c541725 23235 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23236
14095eb3 23237 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23238 get_frame_pc);
8b9737bf
TT
23239}
23240
b6807d98
TT
23241/* Write a constant of a given type as target-ordered bytes into
23242 OBSTACK. */
23243
23244static const gdb_byte *
23245write_constant_as_bytes (struct obstack *obstack,
23246 enum bfd_endian byte_order,
23247 struct type *type,
23248 ULONGEST value,
23249 LONGEST *len)
23250{
23251 gdb_byte *result;
23252
23253 *len = TYPE_LENGTH (type);
224c3ddb 23254 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23255 store_unsigned_integer (result, *len, byte_order, value);
23256
23257 return result;
23258}
23259
d4c9a4f8 23260/* See read.h. */
b6807d98
TT
23261
23262const gdb_byte *
9c541725 23263dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23264 dwarf2_per_cu_data *per_cu,
14095eb3 23265 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23266 obstack *obstack,
b6807d98
TT
23267 LONGEST *len)
23268{
b6807d98
TT
23269 struct die_info *die;
23270 struct attribute *attr;
23271 const gdb_byte *result = NULL;
23272 struct type *type;
23273 LONGEST value;
23274 enum bfd_endian byte_order;
14095eb3 23275 struct objfile *objfile = per_objfile->objfile;
b6807d98 23276
7188ed02 23277 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23278 if (cu == nullptr)
23279 cu = load_cu (per_cu, per_objfile, false);
23280
23281 if (cu == nullptr)
cc12ce38
DE
23282 {
23283 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23284 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23285 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23286 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23287 }
b6807d98 23288
9c541725 23289 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23290 if (!die)
9d8780f0
SM
23291 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23292 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23293
23294 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23295 if (attr == NULL)
23296 return NULL;
23297
e3b94546 23298 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23299 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23300
23301 switch (attr->form)
23302 {
23303 case DW_FORM_addr:
336d760d 23304 case DW_FORM_addrx:
b6807d98
TT
23305 case DW_FORM_GNU_addr_index:
23306 {
23307 gdb_byte *tem;
23308
23309 *len = cu->header.addr_size;
224c3ddb 23310 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23311 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23312 result = tem;
23313 }
23314 break;
23315 case DW_FORM_string:
23316 case DW_FORM_strp:
cf532bd1 23317 case DW_FORM_strx:
b6807d98
TT
23318 case DW_FORM_GNU_str_index:
23319 case DW_FORM_GNU_strp_alt:
c6481205 23320 /* The string is already allocated on the objfile obstack, point
b6807d98 23321 directly to it. */
2c830f54
TT
23322 {
23323 const char *attr_name = attr->as_string ();
23324 result = (const gdb_byte *) attr_name;
23325 *len = strlen (attr_name);
23326 }
b6807d98
TT
23327 break;
23328 case DW_FORM_block1:
23329 case DW_FORM_block2:
23330 case DW_FORM_block4:
23331 case DW_FORM_block:
23332 case DW_FORM_exprloc:
0224619f 23333 case DW_FORM_data16:
9d2246fc
TT
23334 {
23335 struct dwarf_block *block = attr->as_block ();
23336 result = block->data;
23337 *len = block->size;
23338 }
b6807d98
TT
23339 break;
23340
23341 /* The DW_AT_const_value attributes are supposed to carry the
23342 symbol's value "represented as it would be on the target
23343 architecture." By the time we get here, it's already been
23344 converted to host endianness, so we just need to sign- or
23345 zero-extend it as appropriate. */
23346 case DW_FORM_data1:
23347 type = die_type (die, cu);
23348 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23349 if (result == NULL)
23350 result = write_constant_as_bytes (obstack, byte_order,
23351 type, value, len);
23352 break;
23353 case DW_FORM_data2:
23354 type = die_type (die, cu);
23355 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23356 if (result == NULL)
23357 result = write_constant_as_bytes (obstack, byte_order,
23358 type, value, len);
23359 break;
23360 case DW_FORM_data4:
23361 type = die_type (die, cu);
23362 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23363 if (result == NULL)
23364 result = write_constant_as_bytes (obstack, byte_order,
23365 type, value, len);
23366 break;
23367 case DW_FORM_data8:
23368 type = die_type (die, cu);
23369 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23370 if (result == NULL)
23371 result = write_constant_as_bytes (obstack, byte_order,
23372 type, value, len);
23373 break;
23374
23375 case DW_FORM_sdata:
663c44ac 23376 case DW_FORM_implicit_const:
b6807d98
TT
23377 type = die_type (die, cu);
23378 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23379 type, attr->as_signed (), len);
b6807d98
TT
23380 break;
23381
23382 case DW_FORM_udata:
23383 type = die_type (die, cu);
23384 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23385 type, attr->as_unsigned (), len);
b6807d98
TT
23386 break;
23387
23388 default:
b98664d3 23389 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23390 dwarf_form_name (attr->form));
23391 break;
23392 }
23393
23394 return result;
23395}
23396
d4c9a4f8 23397/* See read.h. */
7942e96e
AA
23398
23399struct type *
9c541725 23400dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
23401 dwarf2_per_cu_data *per_cu,
23402 dwarf2_per_objfile *per_objfile)
7942e96e 23403{
7942e96e
AA
23404 struct die_info *die;
23405
7188ed02 23406 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23407 if (cu == nullptr)
23408 cu = load_cu (per_cu, per_objfile, false);
23409
23410 if (cu == nullptr)
23411 return nullptr;
7942e96e 23412
9c541725 23413 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23414 if (!die)
23415 return NULL;
23416
23417 return die_type (die, cu);
23418}
23419
8cb5117c 23420/* See read.h. */
8a9b8146
TT
23421
23422struct type *
b64f50a1 23423dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23424 dwarf2_per_cu_data *per_cu,
23425 dwarf2_per_objfile *per_objfile)
8a9b8146 23426{
9c541725 23427 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23428 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23429}
23430
ac9ec31b 23431/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23432 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23433 On exit *REF_CU is the CU of the result.
23434 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23435
23436static struct die_info *
ac9ec31b
DE
23437follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23438 struct dwarf2_cu **ref_cu)
348e048f 23439{
348e048f 23440 struct die_info temp_die;
3db19b2d 23441 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 23442 struct die_info *die;
976ca316 23443 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23444
348e048f 23445
ac9ec31b
DE
23446 /* While it might be nice to assert sig_type->type == NULL here,
23447 we can get here for DW_AT_imported_declaration where we need
23448 the DIE not the type. */
348e048f 23449
616c069a 23450 /* If necessary, add it to the queue and load its DIEs.
348e048f 23451
616c069a
SM
23452 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23453 it doesn't mean they are currently loaded. Since we require them
23454 to be loaded, we must check for ourselves. */
0d305d5c 23455 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
616c069a 23456 language_minimal)
0d305d5c 23457 || per_objfile->get_cu (sig_type) == nullptr)
976ca316 23458 read_signatured_type (sig_type, per_objfile);
348e048f 23459
0d305d5c 23460 sig_cu = per_objfile->get_cu (sig_type);
69d751e3 23461 gdb_assert (sig_cu != NULL);
9c541725
PA
23462 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23463 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23464 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23465 to_underlying (temp_die.sect_off));
348e048f
DE
23466 if (die)
23467 {
796a7ff8
DE
23468 /* For .gdb_index version 7 keep track of included TUs.
23469 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
23470 if (per_objfile->per_bfd->index_table != NULL
23471 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 23472 {
ae640021 23473 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23474 }
23475
348e048f 23476 *ref_cu = sig_cu;
3db19b2d
TV
23477 if (sig_cu != cu)
23478 sig_cu->ancestor = cu;
c24bdb02 23479
348e048f
DE
23480 return die;
23481 }
23482
ac9ec31b
DE
23483 return NULL;
23484}
23485
23486/* Follow signatured type referenced by ATTR in SRC_DIE.
23487 On entry *REF_CU is the CU of SRC_DIE.
23488 On exit *REF_CU is the CU of the result.
23489 The result is the DIE of the type.
23490 If the referenced type cannot be found an error is thrown. */
23491
23492static struct die_info *
ff39bb5e 23493follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23494 struct dwarf2_cu **ref_cu)
23495{
630ed6b9 23496 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
23497 struct signatured_type *sig_type;
23498 struct die_info *die;
23499
23500 gdb_assert (attr->form == DW_FORM_ref_sig8);
23501
a2ce51a0 23502 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23503 /* sig_type will be NULL if the signatured type is missing from
23504 the debug info. */
23505 if (sig_type == NULL)
23506 {
23507 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
23508 " from DIE at %s [in module %s]"),
23509 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23510 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23511 }
23512
23513 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23514 if (die == NULL)
23515 {
23516 dump_die_for_error (src_die);
23517 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23518 " from DIE at %s [in module %s]"),
23519 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23520 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23521 }
23522
23523 return die;
23524}
23525
23526/* Get the type specified by SIGNATURE referenced in DIE/CU,
23527 reading in and processing the type unit if necessary. */
23528
23529static struct type *
23530get_signatured_type (struct die_info *die, ULONGEST signature,
23531 struct dwarf2_cu *cu)
23532{
976ca316 23533 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
23534 struct signatured_type *sig_type;
23535 struct dwarf2_cu *type_cu;
23536 struct die_info *type_die;
23537 struct type *type;
23538
a2ce51a0 23539 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23540 /* sig_type will be NULL if the signatured type is missing from
23541 the debug info. */
23542 if (sig_type == NULL)
23543 {
b98664d3 23544 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23545 " from DIE at %s [in module %s]"),
23546 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23547 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23548 return build_error_marker_type (cu, die);
23549 }
23550
23551 /* If we already know the type we're done. */
976ca316 23552 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
23553 if (type != nullptr)
23554 return type;
ac9ec31b
DE
23555
23556 type_cu = cu;
23557 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23558 if (type_die != NULL)
23559 {
23560 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23561 is created. This is important, for example, because for c++ classes
23562 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23563 type = read_type_die (type_die, type_cu);
23564 if (type == NULL)
23565 {
b98664d3 23566 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23567 " referenced from DIE at %s [in module %s]"),
23568 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23569 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23570 type = build_error_marker_type (cu, die);
23571 }
23572 }
23573 else
23574 {
b98664d3 23575 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23576 " from DIE at %s [in module %s]"),
23577 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23578 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23579 type = build_error_marker_type (cu, die);
23580 }
e286671b 23581
976ca316 23582 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
23583
23584 return type;
23585}
23586
23587/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23588 reading in and processing the type unit if necessary. */
23589
23590static struct type *
ff39bb5e 23591get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23592 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23593{
23594 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 23595 if (attr->form_is_ref ())
ac9ec31b
DE
23596 {
23597 struct dwarf2_cu *type_cu = cu;
23598 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23599
23600 return read_type_die (type_die, type_cu);
23601 }
23602 else if (attr->form == DW_FORM_ref_sig8)
23603 {
630ed6b9 23604 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
23605 }
23606 else
23607 {
976ca316 23608 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23609
b98664d3 23610 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23611 " at %s [in module %s]"),
23612 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 23613 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23614 return build_error_marker_type (cu, die);
23615 }
348e048f
DE
23616}
23617
e5fe5e75 23618/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23619
23620static void
ab432490
SM
23621load_full_type_unit (dwarf2_per_cu_data *per_cu,
23622 dwarf2_per_objfile *per_objfile)
348e048f 23623{
52dc124a 23624 struct signatured_type *sig_type;
348e048f 23625
f4dc4d17 23626 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 23627 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 23628
6721b2ec
DE
23629 /* We have the per_cu, but we need the signatured_type.
23630 Fortunately this is an easy translation. */
23631 gdb_assert (per_cu->is_debug_types);
23632 sig_type = (struct signatured_type *) per_cu;
348e048f 23633
7188ed02 23634 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 23635
ab432490 23636 read_signatured_type (sig_type, per_objfile);
348e048f 23637
7188ed02 23638 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
23639}
23640
3019eac3
DE
23641/* Read in a signatured type and build its CU and DIEs.
23642 If the type is a stub for the real type in a DWO file,
23643 read in the real type from the DWO file as well. */
dee91e82
DE
23644
23645static void
ab432490
SM
23646read_signatured_type (signatured_type *sig_type,
23647 dwarf2_per_objfile *per_objfile)
dee91e82 23648{
0d305d5c
TT
23649 gdb_assert (sig_type->is_debug_types);
23650 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
348e048f 23651
0d305d5c 23652 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
23653
23654 if (!reader.dummy_p)
23655 {
23656 struct dwarf2_cu *cu = reader.cu;
23657 const gdb_byte *info_ptr = reader.info_ptr;
23658
23659 gdb_assert (cu->die_hash == NULL);
23660 cu->die_hash =
23661 htab_create_alloc_ex (cu->header.length / 12,
23662 die_hash,
23663 die_eq,
23664 NULL,
23665 &cu->comp_unit_obstack,
23666 hashtab_obstack_allocate,
23667 dummy_obstack_deallocate);
23668
3e225074 23669 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
23670 reader.comp_unit_die->child
23671 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23672 reader.comp_unit_die);
23673 cu->dies = reader.comp_unit_die;
23674 /* comp_unit_die is not stored in die_hash, no need. */
23675
23676 /* We try not to read any attributes in this function, because
23677 not all CUs needed for references have been loaded yet, and
23678 symbol table processing isn't initialized. But we have to
23679 set the CU language, or we won't be able to build types
23680 correctly. Similarly, if we do not read the producer, we can
23681 not apply producer-specific interpretation. */
23682 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
23683
23684 reader.keep ();
c0ab21c2
TT
23685 }
23686
0d305d5c 23687 sig_type->tu_read = 1;
c906108c
SS
23688}
23689
c906108c
SS
23690/* Decode simple location descriptions.
23691 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
23692 the location and return the value. If COMPUTED is non-null, it is
23693 set to true to indicate that decoding was successful, and false
23694 otherwise. If COMPUTED is null, then this function may emit a
23695 complaint. */
c906108c
SS
23696
23697static CORE_ADDR
7d79de9a 23698decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 23699{
5e22e966 23700 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
23701 size_t i;
23702 size_t size = blk->size;
d521ce57 23703 const gdb_byte *data = blk->data;
21ae7a4d
JK
23704 CORE_ADDR stack[64];
23705 int stacki;
23706 unsigned int bytes_read, unsnd;
23707 gdb_byte op;
c906108c 23708
7d79de9a
TT
23709 if (computed != nullptr)
23710 *computed = false;
23711
21ae7a4d
JK
23712 i = 0;
23713 stacki = 0;
23714 stack[stacki] = 0;
23715 stack[++stacki] = 0;
23716
23717 while (i < size)
23718 {
23719 op = data[i++];
23720 switch (op)
23721 {
23722 case DW_OP_lit0:
23723 case DW_OP_lit1:
23724 case DW_OP_lit2:
23725 case DW_OP_lit3:
23726 case DW_OP_lit4:
23727 case DW_OP_lit5:
23728 case DW_OP_lit6:
23729 case DW_OP_lit7:
23730 case DW_OP_lit8:
23731 case DW_OP_lit9:
23732 case DW_OP_lit10:
23733 case DW_OP_lit11:
23734 case DW_OP_lit12:
23735 case DW_OP_lit13:
23736 case DW_OP_lit14:
23737 case DW_OP_lit15:
23738 case DW_OP_lit16:
23739 case DW_OP_lit17:
23740 case DW_OP_lit18:
23741 case DW_OP_lit19:
23742 case DW_OP_lit20:
23743 case DW_OP_lit21:
23744 case DW_OP_lit22:
23745 case DW_OP_lit23:
23746 case DW_OP_lit24:
23747 case DW_OP_lit25:
23748 case DW_OP_lit26:
23749 case DW_OP_lit27:
23750 case DW_OP_lit28:
23751 case DW_OP_lit29:
23752 case DW_OP_lit30:
23753 case DW_OP_lit31:
23754 stack[++stacki] = op - DW_OP_lit0;
23755 break;
f1bea926 23756
21ae7a4d
JK
23757 case DW_OP_reg0:
23758 case DW_OP_reg1:
23759 case DW_OP_reg2:
23760 case DW_OP_reg3:
23761 case DW_OP_reg4:
23762 case DW_OP_reg5:
23763 case DW_OP_reg6:
23764 case DW_OP_reg7:
23765 case DW_OP_reg8:
23766 case DW_OP_reg9:
23767 case DW_OP_reg10:
23768 case DW_OP_reg11:
23769 case DW_OP_reg12:
23770 case DW_OP_reg13:
23771 case DW_OP_reg14:
23772 case DW_OP_reg15:
23773 case DW_OP_reg16:
23774 case DW_OP_reg17:
23775 case DW_OP_reg18:
23776 case DW_OP_reg19:
23777 case DW_OP_reg20:
23778 case DW_OP_reg21:
23779 case DW_OP_reg22:
23780 case DW_OP_reg23:
23781 case DW_OP_reg24:
23782 case DW_OP_reg25:
23783 case DW_OP_reg26:
23784 case DW_OP_reg27:
23785 case DW_OP_reg28:
23786 case DW_OP_reg29:
23787 case DW_OP_reg30:
23788 case DW_OP_reg31:
23789 stack[++stacki] = op - DW_OP_reg0;
23790 if (i < size)
7d79de9a
TT
23791 {
23792 if (computed == nullptr)
23793 dwarf2_complex_location_expr_complaint ();
23794 else
23795 return 0;
23796 }
21ae7a4d 23797 break;
c906108c 23798
21ae7a4d
JK
23799 case DW_OP_regx:
23800 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23801 i += bytes_read;
23802 stack[++stacki] = unsnd;
23803 if (i < size)
7d79de9a
TT
23804 {
23805 if (computed == nullptr)
23806 dwarf2_complex_location_expr_complaint ();
23807 else
23808 return 0;
23809 }
21ae7a4d 23810 break;
c906108c 23811
21ae7a4d 23812 case DW_OP_addr:
c8a7a66f
TT
23813 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23814 &bytes_read);
21ae7a4d
JK
23815 i += bytes_read;
23816 break;
d53d4ac5 23817
21ae7a4d
JK
23818 case DW_OP_const1u:
23819 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23820 i += 1;
23821 break;
23822
23823 case DW_OP_const1s:
23824 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23825 i += 1;
23826 break;
23827
23828 case DW_OP_const2u:
23829 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23830 i += 2;
23831 break;
23832
23833 case DW_OP_const2s:
23834 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23835 i += 2;
23836 break;
d53d4ac5 23837
21ae7a4d
JK
23838 case DW_OP_const4u:
23839 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23840 i += 4;
23841 break;
23842
23843 case DW_OP_const4s:
23844 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23845 i += 4;
23846 break;
23847
585861ea
JK
23848 case DW_OP_const8u:
23849 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23850 i += 8;
23851 break;
23852
21ae7a4d
JK
23853 case DW_OP_constu:
23854 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23855 &bytes_read);
23856 i += bytes_read;
23857 break;
23858
23859 case DW_OP_consts:
23860 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23861 i += bytes_read;
23862 break;
23863
23864 case DW_OP_dup:
23865 stack[stacki + 1] = stack[stacki];
23866 stacki++;
23867 break;
23868
23869 case DW_OP_plus:
23870 stack[stacki - 1] += stack[stacki];
23871 stacki--;
23872 break;
23873
23874 case DW_OP_plus_uconst:
23875 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23876 &bytes_read);
23877 i += bytes_read;
23878 break;
23879
23880 case DW_OP_minus:
23881 stack[stacki - 1] -= stack[stacki];
23882 stacki--;
23883 break;
23884
23885 case DW_OP_deref:
23886 /* If we're not the last op, then we definitely can't encode
23887 this using GDB's address_class enum. This is valid for partial
23888 global symbols, although the variable's address will be bogus
23889 in the psymtab. */
23890 if (i < size)
7d79de9a
TT
23891 {
23892 if (computed == nullptr)
23893 dwarf2_complex_location_expr_complaint ();
23894 else
23895 return 0;
23896 }
21ae7a4d
JK
23897 break;
23898
dda83cd7 23899 case DW_OP_GNU_push_tls_address:
4aa4e28b 23900 case DW_OP_form_tls_address:
21ae7a4d
JK
23901 /* The top of the stack has the offset from the beginning
23902 of the thread control block at which the variable is located. */
23903 /* Nothing should follow this operator, so the top of stack would
23904 be returned. */
23905 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23906 address will be bogus in the psymtab. Make it always at least
23907 non-zero to not look as a variable garbage collected by linker
23908 which have DW_OP_addr 0. */
21ae7a4d 23909 if (i < size)
7d79de9a
TT
23910 {
23911 if (computed == nullptr)
23912 dwarf2_complex_location_expr_complaint ();
23913 else
23914 return 0;
23915 }
585861ea 23916 stack[stacki]++;
dda83cd7 23917 break;
21ae7a4d
JK
23918
23919 case DW_OP_GNU_uninit:
7d79de9a
TT
23920 if (computed != nullptr)
23921 return 0;
21ae7a4d
JK
23922 break;
23923
336d760d 23924 case DW_OP_addrx:
3019eac3 23925 case DW_OP_GNU_addr_index:
49f6c839 23926 case DW_OP_GNU_const_index:
3019eac3
DE
23927 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23928 &bytes_read);
23929 i += bytes_read;
23930 break;
23931
21ae7a4d 23932 default:
7d79de9a
TT
23933 if (computed == nullptr)
23934 {
23935 const char *name = get_DW_OP_name (op);
21ae7a4d 23936
7d79de9a
TT
23937 if (name)
23938 complaint (_("unsupported stack op: '%s'"),
23939 name);
23940 else
23941 complaint (_("unsupported stack op: '%02x'"),
23942 op);
23943 }
21ae7a4d
JK
23944
23945 return (stack[stacki]);
d53d4ac5 23946 }
3c6e0cb3 23947
21ae7a4d 23948 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 23949 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
23950 if (stacki >= ARRAY_SIZE (stack) - 1)
23951 {
7d79de9a
TT
23952 if (computed == nullptr)
23953 complaint (_("location description stack overflow"));
21ae7a4d
JK
23954 return 0;
23955 }
23956
23957 if (stacki <= 0)
23958 {
7d79de9a
TT
23959 if (computed == nullptr)
23960 complaint (_("location description stack underflow"));
21ae7a4d
JK
23961 return 0;
23962 }
23963 }
7d79de9a
TT
23964
23965 if (computed != nullptr)
23966 *computed = true;
21ae7a4d 23967 return (stack[stacki]);
c906108c
SS
23968}
23969
23970/* memory allocation interface */
23971
c906108c 23972static struct dwarf_block *
7b5a2f43 23973dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23974{
8d749320 23975 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23976}
23977
c906108c 23978static struct die_info *
b60c80d6 23979dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23980{
23981 struct die_info *die;
b60c80d6
DJ
23982 size_t size = sizeof (struct die_info);
23983
23984 if (num_attrs > 1)
23985 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23986
b60c80d6 23987 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23988 memset (die, 0, sizeof (struct die_info));
23989 return (die);
23990}
2e276125
JB
23991
23992\f
a036ba48 23993
c90ec28a 23994/* Macro support. */
cf2c3c16 23995
9eac9650
TT
23996/* An overload of dwarf_decode_macros that finds the correct section
23997 and ensures it is read in before calling the other overload. */
23998
23999static void
24000dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24001 int section_is_gnu)
24002{
976ca316
SM
24003 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24004 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24005 const struct line_header *lh = cu->line_header;
9eac9650
TT
24006 unsigned int offset_size = cu->header.offset_size;
24007 struct dwarf2_section_info *section;
24008 const char *section_name;
24009
24010 if (cu->dwo_unit != nullptr)
24011 {
24012 if (section_is_gnu)
24013 {
24014 section = &cu->dwo_unit->dwo_file->sections.macro;
24015 section_name = ".debug_macro.dwo";
24016 }
24017 else
24018 {
24019 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24020 section_name = ".debug_macinfo.dwo";
24021 }
24022 }
24023 else
24024 {
24025 if (section_is_gnu)
24026 {
976ca316 24027 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24028 section_name = ".debug_macro";
24029 }
24030 else
24031 {
976ca316 24032 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24033 section_name = ".debug_macinfo";
24034 }
24035 }
24036
24037 section->read (objfile);
24038 if (section->buffer == nullptr)
24039 {
24040 complaint (_("missing %s section"), section_name);
24041 return;
24042 }
24043
24044 buildsym_compunit *builder = cu->get_builder ();
24045
048fde1e 24046 struct dwarf2_section_info *str_offsets_section;
24047 struct dwarf2_section_info *str_section;
24048 ULONGEST str_offsets_base;
24049
24050 if (cu->dwo_unit != nullptr)
24051 {
24052 str_offsets_section = &cu->dwo_unit->dwo_file
24053 ->sections.str_offsets;
24054 str_section = &cu->dwo_unit->dwo_file->sections.str;
24055 str_offsets_base = cu->header.addr_size;
24056 }
24057 else
24058 {
24059 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24060 str_section = &per_objfile->per_bfd->str;
24061 str_offsets_base = *cu->str_offsets_base;
24062 }
24063
976ca316 24064 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24065 offset_size, offset, str_section, str_offsets_section,
24066 str_offsets_base, section_is_gnu);
9eac9650
TT
24067}
24068
3019eac3
DE
24069/* Return the .debug_loc section to use for CU.
24070 For DWO files use .debug_loc.dwo. */
24071
24072static struct dwarf2_section_info *
24073cu_debug_loc_section (struct dwarf2_cu *cu)
24074{
976ca316 24075 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24076
3019eac3 24077 if (cu->dwo_unit)
43988095
JK
24078 {
24079 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24080
43988095
JK
24081 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24082 }
976ca316
SM
24083 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24084 : &per_objfile->per_bfd->loc);
3019eac3
DE
24085}
24086
d0ce17d8
CT
24087/* Return the .debug_rnglists section to use for CU. */
24088static struct dwarf2_section_info *
24089cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24090{
24091 if (cu->header.version < 5)
24092 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24093 cu->header.version);
24094 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24095
24096 /* Make sure we read the .debug_rnglists section from the file that
24097 contains the DW_AT_ranges attribute we are reading. Normally that
24098 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24099 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24100 program. */
24101 if (cu->dwo_unit != nullptr
24102 && tag != DW_TAG_compile_unit
24103 && tag != DW_TAG_skeleton_unit)
24104 {
24105 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24106
24107 if (sections->rnglists.size > 0)
24108 return &sections->rnglists;
24109 else
24110 error (_(".debug_rnglists section is missing from .dwo file."));
24111 }
24112 return &dwarf2_per_objfile->per_bfd->rnglists;
24113}
24114
8cf6f0b1
TT
24115/* A helper function that fills in a dwarf2_loclist_baton. */
24116
24117static void
24118fill_in_loclist_baton (struct dwarf2_cu *cu,
24119 struct dwarf2_loclist_baton *baton,
ff39bb5e 24120 const struct attribute *attr)
8cf6f0b1 24121{
976ca316 24122 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24123 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24124
976ca316 24125 section->read (per_objfile->objfile);
8cf6f0b1 24126
976ca316 24127 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24128 baton->per_cu = cu->per_cu;
24129 gdb_assert (baton->per_cu);
24130 /* We don't know how long the location list is, but make sure we
24131 don't run off the edge of the section. */
d4df075e
TT
24132 baton->size = section->size - attr->as_unsigned ();
24133 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24134 if (cu->base_address.has_value ())
24135 baton->base_address = *cu->base_address;
24136 else
24137 baton->base_address = 0;
f664829e 24138 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24139}
24140
4c2df51b 24141static void
ff39bb5e 24142dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24143 struct dwarf2_cu *cu, int is_block)
4c2df51b 24144{
976ca316
SM
24145 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24146 struct objfile *objfile = per_objfile->objfile;
3019eac3 24147 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24148
cd6c91b4 24149 if (attr->form_is_section_offset ()
3019eac3 24150 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24151 the section. If so, fall through to the complaint in the
24152 other branch. */
d4df075e 24153 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24154 {
0d53c4c4 24155 struct dwarf2_loclist_baton *baton;
4c2df51b 24156
8d749320 24157 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24158
8cf6f0b1 24159 fill_in_loclist_baton (cu, baton, attr);
be391dca 24160
2b24b6e4 24161 if (!cu->base_address.has_value ())
b98664d3 24162 complaint (_("Location list used without "
3e43a32a 24163 "specifying the CU base address."));
4c2df51b 24164
f1e6e072
TT
24165 SYMBOL_ACLASS_INDEX (sym) = (is_block
24166 ? dwarf2_loclist_block_index
24167 : dwarf2_loclist_index);
0d53c4c4
DJ
24168 SYMBOL_LOCATION_BATON (sym) = baton;
24169 }
24170 else
24171 {
24172 struct dwarf2_locexpr_baton *baton;
24173
8d749320 24174 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24175 baton->per_objfile = per_objfile;
ae0d2f24
UW
24176 baton->per_cu = cu->per_cu;
24177 gdb_assert (baton->per_cu);
0d53c4c4 24178
4fc6c0d5 24179 if (attr->form_is_block ())
0d53c4c4
DJ
24180 {
24181 /* Note that we're just copying the block's data pointer
24182 here, not the actual data. We're still pointing into the
6502dd73
DJ
24183 info_buffer for SYM's objfile; right now we never release
24184 that buffer, but when we do clean up properly this may
24185 need to change. */
9d2246fc
TT
24186 struct dwarf_block *block = attr->as_block ();
24187 baton->size = block->size;
24188 baton->data = block->data;
0d53c4c4
DJ
24189 }
24190 else
24191 {
24192 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24193 sym->natural_name ());
0d53c4c4 24194 baton->size = 0;
0d53c4c4 24195 }
6e70227d 24196
f1e6e072
TT
24197 SYMBOL_ACLASS_INDEX (sym) = (is_block
24198 ? dwarf2_locexpr_block_index
24199 : dwarf2_locexpr_index);
0d53c4c4
DJ
24200 SYMBOL_LOCATION_BATON (sym) = baton;
24201 }
4c2df51b 24202}
6502dd73 24203
2e6a9f79 24204/* See read.h. */
96408a79 24205
2e6a9f79
SM
24206const comp_unit_head *
24207dwarf2_per_cu_data::get_header () const
96408a79 24208{
2e6a9f79
SM
24209 if (!m_header_read_in)
24210 {
24211 const gdb_byte *info_ptr
24212 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24213
2e6a9f79 24214 memset (&m_header, 0, sizeof (m_header));
96408a79 24215
2e6a9f79
SM
24216 read_comp_unit_head (&m_header, info_ptr, this->section,
24217 rcuh_kind::COMPILE);
a9f172c6
TT
24218
24219 m_header_read_in = true;
2e6a9f79 24220 }
96408a79 24221
2e6a9f79 24222 return &m_header;
96408a79
SA
24223}
24224
09ba997f 24225/* See read.h. */
ae0d2f24 24226
98714339 24227int
09ba997f 24228dwarf2_per_cu_data::addr_size () const
ae0d2f24 24229{
2e6a9f79 24230 return this->get_header ()->addr_size;
ae0d2f24
UW
24231}
24232
09ba997f 24233/* See read.h. */
9eae7c52
TT
24234
24235int
09ba997f 24236dwarf2_per_cu_data::offset_size () const
9eae7c52 24237{
2e6a9f79 24238 return this->get_header ()->offset_size;
96408a79
SA
24239}
24240
09ba997f 24241/* See read.h. */
96408a79
SA
24242
24243int
09ba997f 24244dwarf2_per_cu_data::ref_addr_size () const
96408a79 24245{
2e6a9f79 24246 const comp_unit_head *header = this->get_header ();
96408a79 24247
2e6a9f79
SM
24248 if (header->version == 2)
24249 return header->addr_size;
96408a79 24250 else
2e6a9f79 24251 return header->offset_size;
181cebd4
JK
24252}
24253
22b6cd70
TT
24254/* A helper function for dwarf2_find_containing_comp_unit that returns
24255 the index of the result, and that searches a vector. It will
24256 return a result even if the offset in question does not actually
24257 occur in any CU. This is separate so that it can be unit
24258 tested. */
ae038cb0 24259
22b6cd70
TT
24260static int
24261dwarf2_find_containing_comp_unit
24262 (sect_offset sect_off,
24263 unsigned int offset_in_dwz,
473ab964 24264 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
ae038cb0 24265{
ae038cb0
DJ
24266 int low, high;
24267
ae038cb0 24268 low = 0;
22b6cd70 24269 high = all_comp_units.size () - 1;
ae038cb0
DJ
24270 while (high > low)
24271 {
36586728 24272 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24273 int mid = low + (high - low) / 2;
9a619af0 24274
0d305d5c 24275 mid_cu = all_comp_units[mid].get ();
36586728 24276 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24277 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24278 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24279 high = mid;
24280 else
24281 low = mid + 1;
24282 }
24283 gdb_assert (low == high);
22b6cd70
TT
24284 return low;
24285}
24286
24287/* Locate the .debug_info compilation unit from CU's objfile which contains
24288 the DIE at OFFSET. Raises an error on failure. */
24289
24290static struct dwarf2_per_cu_data *
24291dwarf2_find_containing_comp_unit (sect_offset sect_off,
24292 unsigned int offset_in_dwz,
976ca316 24293 dwarf2_per_objfile *per_objfile)
22b6cd70 24294{
976ca316
SM
24295 int low = dwarf2_find_containing_comp_unit
24296 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
0d305d5c
TT
24297 dwarf2_per_cu_data *this_cu
24298 = per_objfile->per_bfd->all_comp_units[low].get ();
22b6cd70 24299
45b8ae0c 24300 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24301 {
36586728 24302 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24303 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24304 "offset %s [in module %s]"),
24305 sect_offset_str (sect_off),
976ca316 24306 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24307
976ca316 24308 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24309 <= sect_off);
0d305d5c 24310 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
ae038cb0
DJ
24311 }
24312 else
24313 {
976ca316 24314 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24315 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24316 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24317 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24318 return this_cu;
24319 }
24320}
24321
22b6cd70
TT
24322#if GDB_SELF_TEST
24323
24324namespace selftests {
24325namespace find_containing_comp_unit {
24326
24327static void
24328run_test ()
24329{
473ab964 24330 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
0d305d5c 24331 dwarf2_per_cu_data *one_ptr = one.get ();
473ab964 24332 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
0d305d5c 24333 dwarf2_per_cu_data *two_ptr = two.get ();
473ab964 24334 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
0d305d5c 24335 dwarf2_per_cu_data *three_ptr = three.get ();
473ab964 24336 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
0d305d5c
TT
24337 dwarf2_per_cu_data *four_ptr = four.get ();
24338
24339 one->length = 5;
24340 two->sect_off = sect_offset (one->length);
24341 two->length = 7;
24342
24343 three->length = 5;
24344 three->is_dwz = 1;
24345 four->sect_off = sect_offset (three->length);
24346 four->length = 7;
24347 four->is_dwz = 1;
24348
473ab964 24349 std::vector<dwarf2_per_cu_data_up> units;
0d305d5c
TT
24350 units.push_back (std::move (one));
24351 units.push_back (std::move (two));
24352 units.push_back (std::move (three));
24353 units.push_back (std::move (four));
22b6cd70
TT
24354
24355 int result;
24356
24357 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
0d305d5c 24358 SELF_CHECK (units[result].get () == one_ptr);
22b6cd70 24359 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
0d305d5c 24360 SELF_CHECK (units[result].get () == one_ptr);
22b6cd70 24361 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
0d305d5c 24362 SELF_CHECK (units[result].get () == two_ptr);
22b6cd70
TT
24363
24364 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
0d305d5c 24365 SELF_CHECK (units[result].get () == three_ptr);
22b6cd70 24366 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
0d305d5c 24367 SELF_CHECK (units[result].get () == three_ptr);
22b6cd70 24368 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
0d305d5c 24369 SELF_CHECK (units[result].get () == four_ptr);
22b6cd70
TT
24370}
24371
24372}
24373}
24374
24375#endif /* GDB_SELF_TEST */
24376
9816fde3
JK
24377/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24378
24379static void
95554aad
TT
24380prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24381 enum language pretend_language)
9816fde3
JK
24382{
24383 struct attribute *attr;
24384
24385 /* Set the language we're debugging. */
24386 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24387 if (attr != nullptr)
529908cb 24388 set_cu_language (attr->constant_value (0), cu);
9816fde3 24389 else
9cded63f 24390 {
95554aad 24391 cu->language = pretend_language;
9cded63f
TT
24392 cu->language_defn = language_def (cu->language);
24393 }
dee91e82 24394
7d45c7c3 24395 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24396}
24397
7188ed02 24398/* See read.h. */
ae038cb0 24399
7188ed02
SM
24400dwarf2_cu *
24401dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24402{
7188ed02
SM
24403 auto it = m_dwarf2_cus.find (per_cu);
24404 if (it == m_dwarf2_cus.end ())
24405 return nullptr;
ae038cb0 24406
7188ed02
SM
24407 return it->second;
24408}
24409
24410/* See read.h. */
24411
24412void
24413dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24414{
24415 gdb_assert (this->get_cu (per_cu) == nullptr);
24416
24417 m_dwarf2_cus[per_cu] = cu;
24418}
24419
24420/* See read.h. */
24421
24422void
24423dwarf2_per_objfile::age_comp_units ()
24424{
17e593e9
SM
24425 dwarf_read_debug_printf_v ("running");
24426
08ac5771
SM
24427 /* This is not expected to be called in the middle of CU expansion. There is
24428 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24429 loaded in memory. Calling age_comp_units while the queue is in use could
24430 make us free the DIEs for a CU that is in the queue and therefore break
24431 that invariant. */
24432 gdb_assert (!this->per_bfd->queue.has_value ());
24433
7188ed02
SM
24434 /* Start by clearing all marks. */
24435 for (auto pair : m_dwarf2_cus)
347212b8 24436 pair.second->clear_mark ();
7188ed02
SM
24437
24438 /* Traverse all CUs, mark them and their dependencies if used recently
24439 enough. */
24440 for (auto pair : m_dwarf2_cus)
ae038cb0 24441 {
7188ed02
SM
24442 dwarf2_cu *cu = pair.second;
24443
24444 cu->last_used++;
24445 if (cu->last_used <= dwarf_max_cache_age)
347212b8 24446 cu->mark ();
ae038cb0
DJ
24447 }
24448
7188ed02
SM
24449 /* Delete all CUs still not marked. */
24450 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 24451 {
7188ed02 24452 dwarf2_cu *cu = it->second;
ae038cb0 24453
347212b8 24454 if (!cu->is_marked ())
ae038cb0 24455 {
17e593e9
SM
24456 dwarf_read_debug_printf_v ("deleting old CU %s",
24457 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
24458 delete cu;
24459 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
24460 }
24461 else
7188ed02 24462 it++;
ae038cb0
DJ
24463 }
24464}
24465
7188ed02 24466/* See read.h. */
ae038cb0 24467
7188ed02
SM
24468void
24469dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24470{
7188ed02
SM
24471 auto it = m_dwarf2_cus.find (per_cu);
24472 if (it == m_dwarf2_cus.end ())
24473 return;
ae038cb0 24474
7188ed02 24475 delete it->second;
ae038cb0 24476
7188ed02
SM
24477 m_dwarf2_cus.erase (it);
24478}
ae038cb0 24479
7188ed02
SM
24480dwarf2_per_objfile::~dwarf2_per_objfile ()
24481{
24482 remove_all_cus ();
ae038cb0
DJ
24483}
24484
dee91e82
DE
24485/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24486 We store these in a hash table separate from the DIEs, and preserve them
24487 when the DIEs are flushed out of cache.
24488
24489 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24490 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24491 or the type may come from a DWO file. Furthermore, while it's more logical
24492 to use per_cu->section+offset, with Fission the section with the data is in
24493 the DWO file but we don't know that section at the point we need it.
24494 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24495 because we can enter the lookup routine, get_die_type_at_offset, from
24496 outside this file, and thus won't necessarily have PER_CU->cu.
24497 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24498
dee91e82 24499struct dwarf2_per_cu_offset_and_type
1c379e20 24500{
dee91e82 24501 const struct dwarf2_per_cu_data *per_cu;
9c541725 24502 sect_offset sect_off;
1c379e20
DJ
24503 struct type *type;
24504};
24505
dee91e82 24506/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24507
24508static hashval_t
dee91e82 24509per_cu_offset_and_type_hash (const void *item)
1c379e20 24510{
9a3c8263
SM
24511 const struct dwarf2_per_cu_offset_and_type *ofs
24512 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24513
9c541725 24514 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24515}
24516
dee91e82 24517/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24518
24519static int
dee91e82 24520per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24521{
9a3c8263
SM
24522 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24523 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24524 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24525 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24526
dee91e82 24527 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24528 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24529}
24530
24531/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24532 table if necessary. For convenience, return TYPE.
24533
24534 The DIEs reading must have careful ordering to:
85102364 24535 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24536 reading current DIE.
24537 * Not trying to dereference contents of still incompletely read in types
24538 while reading in other DIEs.
24539 * Enable referencing still incompletely read in types just by a pointer to
24540 the type without accessing its fields.
24541
24542 Therefore caller should follow these rules:
24543 * Try to fetch any prerequisite types we may need to build this DIE type
24544 before building the type and calling set_die_type.
e71ec853 24545 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24546 possible before fetching more types to complete the current type.
24547 * Make the type as complete as possible before fetching more types. */
1c379e20 24548
f792889a 24549static struct type *
57567375
TT
24550set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24551 bool skip_data_location)
1c379e20 24552{
976ca316 24553 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 24554 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 24555 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
24556 struct attribute *attr;
24557 struct dynamic_prop prop;
1c379e20 24558
b4ba55a1
JB
24559 /* For Ada types, make sure that the gnat-specific data is always
24560 initialized (if not already set). There are a few types where
24561 we should not be doing so, because the type-specific area is
24562 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24563 where the type-specific area is used to store the floatformat).
24564 But this is not a problem, because the gnat-specific information
24565 is actually not needed for these types. */
24566 if (need_gnat_info (cu)
78134374
SM
24567 && type->code () != TYPE_CODE_FUNC
24568 && type->code () != TYPE_CODE_FLT
24569 && type->code () != TYPE_CODE_METHODPTR
24570 && type->code () != TYPE_CODE_MEMBERPTR
24571 && type->code () != TYPE_CODE_METHOD
09584414 24572 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
24573 && !HAVE_GNAT_AUX_INFO (type))
24574 INIT_GNAT_SPECIFIC (type);
24575
3f2f83dd
KB
24576 /* Read DW_AT_allocated and set in type. */
24577 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 24578 if (attr != NULL)
3f2f83dd 24579 {
293e7e51 24580 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24581 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24582 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 24583 }
3f2f83dd
KB
24584
24585 /* Read DW_AT_associated and set in type. */
24586 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 24587 if (attr != NULL)
3f2f83dd 24588 {
293e7e51 24589 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24590 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24591 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 24592 }
3f2f83dd 24593
3cdcd0ce 24594 /* Read DW_AT_data_location and set in type. */
57567375
TT
24595 if (!skip_data_location)
24596 {
24597 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24598 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24599 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24600 }
3cdcd0ce 24601
976ca316
SM
24602 if (per_objfile->die_type_hash == NULL)
24603 per_objfile->die_type_hash
0335378b
TT
24604 = htab_up (htab_create_alloc (127,
24605 per_cu_offset_and_type_hash,
24606 per_cu_offset_and_type_eq,
24607 NULL, xcalloc, xfree));
1c379e20 24608
dee91e82 24609 ofs.per_cu = cu->per_cu;
9c541725 24610 ofs.sect_off = die->sect_off;
1c379e20 24611 ofs.type = type;
dee91e82 24612 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 24613 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24614 if (*slot)
b98664d3 24615 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24616 sect_offset_str (die->sect_off));
8d749320
SM
24617 *slot = XOBNEW (&objfile->objfile_obstack,
24618 struct dwarf2_per_cu_offset_and_type);
1c379e20 24619 **slot = ofs;
f792889a 24620 return type;
1c379e20
DJ
24621}
24622
9c541725 24623/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24624 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24625
24626static struct type *
9c541725 24627get_die_type_at_offset (sect_offset sect_off,
aa66c379 24628 dwarf2_per_cu_data *per_cu,
976ca316 24629 dwarf2_per_objfile *per_objfile)
1c379e20 24630{
dee91e82 24631 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 24632
976ca316 24633 if (per_objfile->die_type_hash == NULL)
f792889a 24634 return NULL;
1c379e20 24635
dee91e82 24636 ofs.per_cu = per_cu;
9c541725 24637 ofs.sect_off = sect_off;
9a3c8263 24638 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 24639 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24640 if (slot)
24641 return slot->type;
24642 else
24643 return NULL;
24644}
24645
02142a6c 24646/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24647 or return NULL if DIE does not have a saved type. */
24648
24649static struct type *
24650get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24651{
aa66c379 24652 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
24653}
24654
72bf9492
DJ
24655/* Trivial hash function for partial_die_info: the hash value of a DIE
24656 is its offset in .debug_info for this objfile. */
24657
24658static hashval_t
24659partial_die_hash (const void *item)
24660{
9a3c8263
SM
24661 const struct partial_die_info *part_die
24662 = (const struct partial_die_info *) item;
9a619af0 24663
9c541725 24664 return to_underlying (part_die->sect_off);
72bf9492
DJ
24665}
24666
24667/* Trivial comparison function for partial_die_info structures: two DIEs
24668 are equal if they have the same offset. */
24669
24670static int
24671partial_die_eq (const void *item_lhs, const void *item_rhs)
24672{
9a3c8263
SM
24673 const struct partial_die_info *part_die_lhs
24674 = (const struct partial_die_info *) item_lhs;
24675 const struct partial_die_info *part_die_rhs
24676 = (const struct partial_die_info *) item_rhs;
9a619af0 24677
9c541725 24678 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24679}
24680
3c3bb058
AB
24681struct cmd_list_element *set_dwarf_cmdlist;
24682struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 24683
9291a0cd 24684static void
cd4fb1b2
SM
24685show_check_physname (struct ui_file *file, int from_tty,
24686 struct cmd_list_element *c, const char *value)
9291a0cd 24687{
cd4fb1b2
SM
24688 fprintf_filtered (file,
24689 _("Whether to check \"physname\" is %s.\n"),
24690 value);
9291a0cd
TT
24691}
24692
6c265988 24693void _initialize_dwarf2_read ();
cd4fb1b2 24694void
6c265988 24695_initialize_dwarf2_read ()
9291a0cd 24696{
0743fc83 24697 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 24698Set DWARF specific variables.\n\
590042fc 24699Configure DWARF variables such as the cache size."),
2f822da5 24700 &set_dwarf_cmdlist,
0743fc83 24701 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24702
0743fc83 24703 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
24704Show DWARF specific variables.\n\
24705Show DWARF variables such as the cache size."),
2f822da5 24706 &show_dwarf_cmdlist,
0743fc83 24707 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24708
cd4fb1b2
SM
24709 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24710 &dwarf_max_cache_age, _("\
24711Set the upper bound on the age of cached DWARF compilation units."), _("\
24712Show the upper bound on the age of cached DWARF compilation units."), _("\
24713A higher limit means that cached compilation units will be stored\n\
24714in memory longer, and more total memory will be used. Zero disables\n\
24715caching, which can slow down startup."),
24716 NULL,
24717 show_dwarf_max_cache_age,
24718 &set_dwarf_cmdlist,
24719 &show_dwarf_cmdlist);
156942c7 24720
cd4fb1b2
SM
24721 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24722Set debugging of the DWARF reader."), _("\
24723Show debugging of the DWARF reader."), _("\
24724When enabled (non-zero), debugging messages are printed during DWARF\n\
24725reading and symtab expansion. A value of 1 (one) provides basic\n\
24726information. A value greater than 1 provides more verbose information."),
24727 NULL,
24728 NULL,
24729 &setdebuglist, &showdebuglist);
9291a0cd 24730
3a706c17 24731 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
cd4fb1b2
SM
24732Set debugging of the DWARF DIE reader."), _("\
24733Show debugging of the DWARF DIE reader."), _("\
3a706c17 24734When enabled (non-zero), DIEs are dumped after they are read in.\n\
cd4fb1b2 24735The value is the maximum depth to print."),
3a706c17
SM
24736 NULL,
24737 NULL,
24738 &setdebuglist, &showdebuglist);
9291a0cd 24739
cd4fb1b2
SM
24740 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24741Set debugging of the dwarf line reader."), _("\
24742Show debugging of the dwarf line reader."), _("\
24743When enabled (non-zero), line number entries are dumped as they are read in.\n\
24744A value of 1 (one) provides basic information.\n\
24745A value greater than 1 provides more verbose information."),
24746 NULL,
24747 NULL,
24748 &setdebuglist, &showdebuglist);
437afbb8 24749
cd4fb1b2
SM
24750 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24751Set cross-checking of \"physname\" code against demangler."), _("\
24752Show cross-checking of \"physname\" code against demangler."), _("\
24753When enabled, GDB's internal \"physname\" code is checked against\n\
24754the demangler."),
24755 NULL, show_check_physname,
24756 &setdebuglist, &showdebuglist);
900e11f9 24757
e615022a
DE
24758 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24759 no_class, &use_deprecated_index_sections, _("\
24760Set whether to use deprecated gdb_index sections."), _("\
24761Show whether to use deprecated gdb_index sections."), _("\
24762When enabled, deprecated .gdb_index sections are used anyway.\n\
24763Normally they are ignored either because of a missing feature or\n\
24764performance issue.\n\
24765Warning: This option must be enabled before gdb reads the file."),
24766 NULL,
24767 NULL,
24768 &setlist, &showlist);
24769
f1e6e072
TT
24770 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24771 &dwarf2_locexpr_funcs);
24772 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24773 &dwarf2_loclist_funcs);
24774
24775 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24776 &dwarf2_block_frame_base_locexpr_funcs);
24777 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24778 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24779
24780#if GDB_SELF_TEST
24781 selftests::register_test ("dw2_expand_symtabs_matching",
24782 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24783 selftests::register_test ("dwarf2_find_containing_comp_unit",
24784 selftests::find_containing_comp_unit::run_test);
c62446b1 24785#endif
6502dd73 24786}
This page took 6.45125 seconds and 4 git commands to generate.