gdb: change dwarf_die_debug to bool
[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
749369c4
SM
110/* When true, dump DIEs after they are read in. */
111static bool dwarf_die_debug = false;
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;
d3473f0c
TT
2291 result->index = m_num_psymtabs++;
2292 return result;
2293}
2294
2295/* See read.h. */
2296
0d305d5c 2297std::unique_ptr<signatured_type>
5989a64e 2298dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c 2299{
0d305d5c
TT
2300 std::unique_ptr<signatured_type> result (new signatured_type);
2301 result->per_bfd = this;
2302 result->index = m_num_psymtabs++;
91eea9cc 2303 tu_stats.nr_tus++;
d3473f0c
TT
2304 return result;
2305}
2306
168c9250 2307/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2308 obstack, and constructed with the specified field values. */
4b514bc8 2309
473ab964 2310static dwarf2_per_cu_data_up
168c9250
SM
2311create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2312 struct dwarf2_section_info *section,
2313 int is_dwz,
2314 sect_offset sect_off, ULONGEST length)
4b514bc8 2315{
473ab964 2316 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2317 the_cu->sect_off = sect_off;
2318 the_cu->length = length;
4b514bc8 2319 the_cu->section = section;
168c9250 2320 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2321 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2322 the_cu->is_dwz = is_dwz;
2323 return the_cu;
2324}
2325
2ec9a5e0
TT
2326/* A helper for create_cus_from_index that handles a given list of
2327 CUs. */
2fdf6df6 2328
74a0d9f6 2329static void
168c9250 2330create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2331 const gdb_byte *cu_list, offset_type n_elements,
2332 struct dwarf2_section_info *section,
b76e467d 2333 int is_dwz)
9291a0cd 2334{
12359b5e 2335 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2336 {
74a0d9f6 2337 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2338
2339 sect_offset sect_off
2340 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2341 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2342 cu_list += 2 * 8;
2343
473ab964 2344 dwarf2_per_cu_data_up per_cu
168c9250
SM
2345 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2346 length);
0d305d5c 2347 per_bfd->all_comp_units.push_back (std::move (per_cu));
9291a0cd 2348 }
9291a0cd
TT
2349}
2350
2ec9a5e0 2351/* Read the CU list from the mapped index, and use it to create all
168c9250 2352 the CU objects for PER_BFD. */
2ec9a5e0 2353
74a0d9f6 2354static void
168c9250 2355create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2356 const gdb_byte *cu_list, offset_type cu_list_elements,
2357 const gdb_byte *dwz_list, offset_type dwz_elements)
2358{
168c9250
SM
2359 gdb_assert (per_bfd->all_comp_units.empty ());
2360 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2361
168c9250
SM
2362 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2363 &per_bfd->info, 0);
2ec9a5e0
TT
2364
2365 if (dwz_elements == 0)
74a0d9f6 2366 return;
2ec9a5e0 2367
168c9250
SM
2368 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2369 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2370 &dwz->info, 1);
2ec9a5e0
TT
2371}
2372
1fd400ff 2373/* Create the signatured type hash table from the index. */
673bfd45 2374
74a0d9f6 2375static void
12359b5e 2376create_signatured_type_table_from_index
168c9250
SM
2377 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2378 const gdb_byte *bytes, offset_type elements)
1fd400ff 2379{
298e9637 2380 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2381
12359b5e 2382 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2383 {
0d305d5c 2384 std::unique_ptr<signatured_type> sig_type;
9c541725 2385 ULONGEST signature;
1fd400ff 2386 void **slot;
9c541725 2387 cu_offset type_offset_in_tu;
1fd400ff 2388
74a0d9f6 2389 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2390 sect_offset sect_off
2391 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2392 type_offset_in_tu
2393 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2394 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2395 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2396 bytes += 3 * 8;
2397
168c9250 2398 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2399 sig_type->signature = signature;
9c541725 2400 sig_type->type_offset_in_tu = type_offset_in_tu;
0d305d5c
TT
2401 sig_type->is_debug_types = 1;
2402 sig_type->section = section;
2403 sig_type->sect_off = sect_off;
2404 sig_type->v.quick
168c9250 2405 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2406 struct dwarf2_per_cu_quick_data);
2407
0d305d5c
TT
2408 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2409 *slot = sig_type.get ();
1fd400ff 2410
473ab964 2411 per_bfd->all_comp_units.emplace_back (sig_type.release ());
1fd400ff
TT
2412 }
2413
168c9250 2414 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2415}
2416
927aa2e7
JK
2417/* Create the signatured type hash table from .debug_names. */
2418
2419static void
2420create_signatured_type_table_from_debug_names
976ca316 2421 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2422 const mapped_debug_names &map,
2423 struct dwarf2_section_info *section,
2424 struct dwarf2_section_info *abbrev_section)
2425{
976ca316 2426 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2427
96b79293
TT
2428 section->read (objfile);
2429 abbrev_section->read (objfile);
927aa2e7 2430
298e9637 2431 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2432
2433 for (uint32_t i = 0; i < map.tu_count; ++i)
2434 {
0d305d5c 2435 std::unique_ptr<signatured_type> sig_type;
927aa2e7 2436 void **slot;
927aa2e7
JK
2437
2438 sect_offset sect_off
2439 = (sect_offset) (extract_unsigned_integer
2440 (map.tu_table_reordered + i * map.offset_size,
2441 map.offset_size,
2442 map.dwarf5_byte_order));
2443
2444 comp_unit_head cu_header;
976ca316 2445 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2446 abbrev_section,
927aa2e7
JK
2447 section->buffer + to_underlying (sect_off),
2448 rcuh_kind::TYPE);
2449
976ca316 2450 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2451 sig_type->signature = cu_header.signature;
2452 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
0d305d5c
TT
2453 sig_type->is_debug_types = 1;
2454 sig_type->section = section;
2455 sig_type->sect_off = sect_off;
2456 sig_type->v.quick
976ca316 2457 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2458 struct dwarf2_per_cu_quick_data);
2459
0d305d5c
TT
2460 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2461 *slot = sig_type.get ();
927aa2e7 2462
473ab964 2463 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
927aa2e7
JK
2464 }
2465
976ca316 2466 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2467}
2468
9291a0cd 2469/* Read the address map data from the mapped index, and use it to
efd7398e 2470 populate the psymtabs_addrmap. */
2fdf6df6 2471
9291a0cd 2472static void
976ca316 2473create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2474 struct mapped_index *index)
9291a0cd 2475{
976ca316 2476 struct objfile *objfile = per_objfile->objfile;
efd7398e 2477 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
08feed99 2478 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2479 const gdb_byte *iter, *end;
9291a0cd 2480 struct addrmap *mutable_map;
9291a0cd
TT
2481 CORE_ADDR baseaddr;
2482
8268c778
PA
2483 auto_obstack temp_obstack;
2484
9291a0cd
TT
2485 mutable_map = addrmap_create_mutable (&temp_obstack);
2486
f00a2de2
PA
2487 iter = index->address_table.data ();
2488 end = iter + index->address_table.size ();
9291a0cd 2489
b3b3bada 2490 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2491
2492 while (iter < end)
2493 {
2494 ULONGEST hi, lo, cu_index;
2495 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2496 iter += 8;
2497 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2498 iter += 8;
2499 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2500 iter += 4;
f652bce2 2501
24a55014 2502 if (lo > hi)
f652bce2 2503 {
b98664d3 2504 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2505 hex_string (lo), hex_string (hi));
24a55014 2506 continue;
f652bce2 2507 }
24a55014 2508
efd7398e 2509 if (cu_index >= per_bfd->all_comp_units.size ())
f652bce2 2510 {
b98664d3 2511 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2512 (unsigned) cu_index);
24a55014 2513 continue;
f652bce2 2514 }
24a55014 2515
79748972
TT
2516 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2517 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2518 addrmap_set_empty (mutable_map, lo, hi - 1,
efd7398e 2519 per_bfd->get_cu (cu_index));
9291a0cd
TT
2520 }
2521
efd7398e
TT
2522 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2523 &per_bfd->obstack);
9291a0cd
TT
2524}
2525
927aa2e7 2526/* Read the address map data from DWARF-5 .debug_aranges, and use it to
efd7398e 2527 populate the psymtabs_addrmap. */
927aa2e7
JK
2528
2529static void
976ca316 2530create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2531 struct dwarf2_section_info *section)
2532{
976ca316 2533 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2534 bfd *abfd = objfile->obfd;
08feed99 2535 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2536 const CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e 2537 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
2538
2539 auto_obstack temp_obstack;
2540 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2541
2542 std::unordered_map<sect_offset,
2543 dwarf2_per_cu_data *,
2544 gdb::hash_enum<sect_offset>>
2545 debug_info_offset_to_per_cu;
0d305d5c 2546 for (const auto &per_cu : per_bfd->all_comp_units)
927aa2e7 2547 {
927aa2e7 2548 const auto insertpair
0d305d5c
TT
2549 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2550 per_cu.get ());
927aa2e7
JK
2551 if (!insertpair.second)
2552 {
2553 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2554 "debug_info_offset %s, ignoring .debug_aranges."),
2555 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2556 return;
2557 }
2558 }
2559
96b79293 2560 section->read (objfile);
927aa2e7
JK
2561
2562 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2563
2564 const gdb_byte *addr = section->buffer;
2565
2566 while (addr < section->buffer + section->size)
2567 {
2568 const gdb_byte *const entry_addr = addr;
2569 unsigned int bytes_read;
2570
2571 const LONGEST entry_length = read_initial_length (abfd, addr,
2572 &bytes_read);
2573 addr += bytes_read;
2574
2575 const gdb_byte *const entry_end = addr + entry_length;
2576 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2577 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2578 if (addr + entry_length > section->buffer + section->size)
2579 {
47e3f474 2580 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2581 "length %s exceeds section length %s, "
927aa2e7 2582 "ignoring .debug_aranges."),
47e3f474
TV
2583 objfile_name (objfile),
2584 plongest (entry_addr - section->buffer),
927aa2e7
JK
2585 plongest (bytes_read + entry_length),
2586 pulongest (section->size));
2587 return;
2588 }
2589
2590 /* The version number. */
2591 const uint16_t version = read_2_bytes (abfd, addr);
2592 addr += 2;
2593 if (version != 2)
2594 {
47e3f474 2595 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2596 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2597 objfile_name (objfile),
2598 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2599 return;
2600 }
2601
2602 const uint64_t debug_info_offset
2603 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2604 addr += offset_size;
2605 const auto per_cu_it
2606 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2607 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2608 {
47e3f474 2609 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2610 "debug_info_offset %s does not exists, "
2611 "ignoring .debug_aranges."),
47e3f474
TV
2612 objfile_name (objfile),
2613 plongest (entry_addr - section->buffer),
927aa2e7
JK
2614 pulongest (debug_info_offset));
2615 return;
2616 }
2617 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2618
2619 const uint8_t address_size = *addr++;
2620 if (address_size < 1 || address_size > 8)
2621 {
47e3f474 2622 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2623 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2624 objfile_name (objfile),
2625 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2626 return;
2627 }
2628
2629 const uint8_t segment_selector_size = *addr++;
2630 if (segment_selector_size != 0)
2631 {
47e3f474 2632 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2633 "segment_selector_size %u is not supported, "
2634 "ignoring .debug_aranges."),
47e3f474
TV
2635 objfile_name (objfile),
2636 plongest (entry_addr - section->buffer),
927aa2e7
JK
2637 segment_selector_size);
2638 return;
2639 }
2640
2641 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2642 size. It is undocumented by the DWARF standard but GCC does
2643 use it. */
927aa2e7
JK
2644 for (size_t padding = ((-(addr - section->buffer))
2645 & (2 * address_size - 1));
dda83cd7 2646 padding > 0; padding--)
927aa2e7
JK
2647 if (*addr++ != 0)
2648 {
47e3f474 2649 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2650 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2651 objfile_name (objfile),
2652 plongest (entry_addr - section->buffer));
927aa2e7
JK
2653 return;
2654 }
2655
2656 for (;;)
2657 {
2658 if (addr + 2 * address_size > entry_end)
2659 {
47e3f474 2660 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2661 "address list is not properly terminated, "
2662 "ignoring .debug_aranges."),
47e3f474
TV
2663 objfile_name (objfile),
2664 plongest (entry_addr - section->buffer));
927aa2e7
JK
2665 return;
2666 }
2667 ULONGEST start = extract_unsigned_integer (addr, address_size,
2668 dwarf5_byte_order);
2669 addr += address_size;
2670 ULONGEST length = extract_unsigned_integer (addr, address_size,
2671 dwarf5_byte_order);
2672 addr += address_size;
2673 if (start == 0 && length == 0)
2674 break;
efd7398e 2675 if (start == 0 && !per_bfd->has_section_at_zero)
927aa2e7
JK
2676 {
2677 /* Symbol was eliminated due to a COMDAT group. */
2678 continue;
2679 }
2680 ULONGEST end = start + length;
79748972
TT
2681 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2682 - baseaddr);
2683 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2684 - baseaddr);
927aa2e7
JK
2685 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2686 }
2687 }
2688
efd7398e
TT
2689 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2690 &per_bfd->obstack);
927aa2e7
JK
2691}
2692
4485a1c1
SM
2693/* A helper function that reads the .gdb_index from BUFFER and fills
2694 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2695 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2696 ok to use deprecated sections.
2697
2698 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2699 out parameters that are filled in with information about the CU and
2700 TU lists in the section.
2701
4485a1c1 2702 Returns true if all went well, false otherwise. */
2fdf6df6 2703
d33bc52e 2704static bool
3810f182 2705read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2706 bool deprecated_ok,
2707 gdb::array_view<const gdb_byte> buffer,
2708 struct mapped_index *map,
2709 const gdb_byte **cu_list,
2710 offset_type *cu_list_elements,
2711 const gdb_byte **types_list,
2712 offset_type *types_list_elements)
2713{
2714 const gdb_byte *addr = &buffer[0];
42c2c694 2715 offset_view metadata (buffer);
82430852 2716
9291a0cd 2717 /* Version check. */
42c2c694 2718 offset_type version = metadata[0];
987d643c 2719 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2720 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2721 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2722 indices. */
831adc1f 2723 if (version < 4)
481860b3
GB
2724 {
2725 static int warning_printed = 0;
2726 if (!warning_printed)
2727 {
2728 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2729 filename);
481860b3
GB
2730 warning_printed = 1;
2731 }
2732 return 0;
2733 }
2734 /* Index version 4 uses a different hash function than index version
2735 5 and later.
2736
2737 Versions earlier than 6 did not emit psymbols for inlined
2738 functions. Using these files will cause GDB not to be able to
2739 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2740 indices unless the user has done
2741 "set use-deprecated-index-sections on". */
2ec9a5e0 2742 if (version < 6 && !deprecated_ok)
481860b3
GB
2743 {
2744 static int warning_printed = 0;
2745 if (!warning_printed)
2746 {
e615022a
DE
2747 warning (_("\
2748Skipping deprecated .gdb_index section in %s.\n\
2749Do \"set use-deprecated-index-sections on\" before the file is read\n\
2750to use the section anyway."),
2ec9a5e0 2751 filename);
481860b3
GB
2752 warning_printed = 1;
2753 }
2754 return 0;
2755 }
796a7ff8 2756 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2757 of the TU (for symbols coming from TUs),
2758 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2759 Plus gold-generated indices can have duplicate entries for global symbols,
2760 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2761 These are just performance bugs, and we can't distinguish gdb-generated
2762 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2763
481860b3 2764 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2765 longer backward compatible. */
796a7ff8 2766 if (version > 8)
594e8718 2767 return 0;
9291a0cd 2768
559a7a62 2769 map->version = version;
9291a0cd 2770
42c2c694
TT
2771 int i = 1;
2772 *cu_list = addr + metadata[i];
2773 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
1fd400ff
TT
2774 ++i;
2775
42c2c694
TT
2776 *types_list = addr + metadata[i];
2777 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
987d643c 2778 ++i;
1fd400ff 2779
42c2c694
TT
2780 const gdb_byte *address_table = addr + metadata[i];
2781 const gdb_byte *address_table_end = addr + metadata[i + 1];
f00a2de2
PA
2782 map->address_table
2783 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2784 ++i;
2785
42c2c694
TT
2786 const gdb_byte *symbol_table = addr + metadata[i];
2787 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
f00a2de2 2788 map->symbol_table
42c2c694
TT
2789 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2790 symbol_table_end));
9291a0cd 2791
f00a2de2 2792 ++i;
42c2c694 2793 map->constant_pool = buffer.slice (metadata[i]);
1fd400ff 2794
2ec9a5e0
TT
2795 return 1;
2796}
2797
4485a1c1
SM
2798/* Callback types for dwarf2_read_gdb_index. */
2799
2800typedef gdb::function_view
5989a64e 2801 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
2802 get_gdb_index_contents_ftype;
2803typedef gdb::function_view
2804 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2805 get_gdb_index_contents_dwz_ftype;
2806
927aa2e7 2807/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2808 elements of all the CUs and return 1. Otherwise, return 0. */
2809
2810static int
4485a1c1 2811dwarf2_read_gdb_index
976ca316 2812 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
2813 get_gdb_index_contents_ftype get_gdb_index_contents,
2814 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 2815{
2ec9a5e0
TT
2816 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2817 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2818 struct dwz_file *dwz;
976ca316 2819 struct objfile *objfile = per_objfile->objfile;
f8c41851 2820 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 2821
4485a1c1 2822 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 2823 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
2824
2825 if (main_index_contents.empty ())
2826 return 0;
2827
3063847f 2828 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 2829 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
2830 use_deprecated_index_sections,
2831 main_index_contents, map.get (), &cu_list,
2832 &cu_list_elements, &types_list,
2833 &types_list_elements))
2ec9a5e0
TT
2834 return 0;
2835
0fefef59 2836 /* Don't use the index if it's empty. */
3063847f 2837 if (map->symbol_table.empty ())
0fefef59
DE
2838 return 0;
2839
2ec9a5e0
TT
2840 /* If there is a .dwz file, read it so we can get its CU list as
2841 well. */
f8c41851 2842 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 2843 if (dwz != NULL)
2ec9a5e0 2844 {
2ec9a5e0
TT
2845 struct mapped_index dwz_map;
2846 const gdb_byte *dwz_types_ignore;
2847 offset_type dwz_types_elements_ignore;
2848
4485a1c1
SM
2849 gdb::array_view<const gdb_byte> dwz_index_content
2850 = get_gdb_index_contents_dwz (objfile, dwz);
2851
2852 if (dwz_index_content.empty ())
2853 return 0;
2854
3810f182 2855 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 2856 1, dwz_index_content, &dwz_map,
4485a1c1
SM
2857 &dwz_list, &dwz_list_elements,
2858 &dwz_types_ignore,
2859 &dwz_types_elements_ignore))
2ec9a5e0
TT
2860 {
2861 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 2862 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
2863 return 0;
2864 }
2865 }
2866
f8c41851
SM
2867 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2868 dwz_list_elements);
1fd400ff 2869
8b70b953
TT
2870 if (types_list_elements)
2871 {
8b70b953
TT
2872 /* We can only handle a single .debug_types when we have an
2873 index. */
f8c41851 2874 if (per_bfd->types.size () != 1)
8b70b953
TT
2875 return 0;
2876
f8c41851 2877 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 2878
f8c41851 2879 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 2880 types_list_elements);
8b70b953 2881 }
9291a0cd 2882
976ca316 2883 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 2884
f8c41851
SM
2885 per_bfd->index_table = std::move (map);
2886 per_bfd->using_index = 1;
2887 per_bfd->quick_file_names_table =
2888 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2889
9291a0cd
TT
2890 return 1;
2891}
2892
dee91e82 2893/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2894
dee91e82
DE
2895static void
2896dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 2897 struct die_info *comp_unit_die)
9291a0cd 2898{
dee91e82 2899 struct dwarf2_cu *cu = reader->cu;
ed2dc618 2900 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 2901 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 2902 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 2903 struct attribute *attr;
7b9f3c50
DE
2904 void **slot;
2905 struct quick_file_names *qfn;
9291a0cd 2906
0186c6a7
DE
2907 gdb_assert (! this_cu->is_debug_types);
2908
07261596
TT
2909 /* Our callers never want to match partial units -- instead they
2910 will match the enclosing full CU. */
2911 if (comp_unit_die->tag == DW_TAG_partial_unit)
2912 {
2913 this_cu->v.quick->no_file_data = 1;
2914 return;
2915 }
2916
0186c6a7 2917 lh_cu = this_cu;
7b9f3c50 2918 slot = NULL;
dee91e82 2919
fff8551c 2920 line_header_up lh;
9c541725 2921 sect_offset line_offset {};
fff8551c 2922
dee91e82 2923 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 2924 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 2925 {
7b9f3c50
DE
2926 struct quick_file_names find_entry;
2927
d4df075e 2928 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
2929
2930 /* We may have already read in this line header (TU line header sharing).
2931 If we have we're done. */
094b34ac 2932 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 2933 find_entry.hash.line_sect_off = line_offset;
976ca316 2934 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
2935 &find_entry, INSERT);
2936 if (*slot != NULL)
2937 {
9a3c8263 2938 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 2939 return;
7b9f3c50
DE
2940 }
2941
3019eac3 2942 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2943 }
2944 if (lh == NULL)
2945 {
094b34ac 2946 lh_cu->v.quick->no_file_data = 1;
dee91e82 2947 return;
9291a0cd
TT
2948 }
2949
976ca316 2950 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 2951 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 2952 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
2953 gdb_assert (slot != NULL);
2954 *slot = qfn;
9291a0cd 2955
d721ba37 2956 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 2957
aa391654
TT
2958 int offset = 0;
2959 if (strcmp (fnd.name, "<unknown>") != 0)
2960 ++offset;
2961
7ba99d21 2962 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 2963 qfn->file_names =
976ca316 2964 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 2965 qfn->num_file_names);
aa391654
TT
2966 if (offset != 0)
2967 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 2968 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
2969 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
2970 fnd.comp_dir).release ();
7b9f3c50 2971 qfn->real_names = NULL;
9291a0cd 2972
094b34ac 2973 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2974}
2975
2976/* A helper for the "quick" functions which attempts to read the line
2977 table for THIS_CU. */
2978
2979static struct quick_file_names *
ab432490
SM
2980dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2981 dwarf2_per_objfile *per_objfile)
dee91e82 2982{
0186c6a7
DE
2983 /* This should never be called for TUs. */
2984 gdb_assert (! this_cu->is_debug_types);
2985 /* Nor type unit groups. */
197400e8 2986 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 2987
dee91e82
DE
2988 if (this_cu->v.quick->file_names != NULL)
2989 return this_cu->v.quick->file_names;
2990 /* If we know there is no line data, no point in looking again. */
2991 if (this_cu->v.quick->no_file_data)
2992 return NULL;
2993
ab432490 2994 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 2995 if (!reader.dummy_p)
eb5dd737 2996 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
2997
2998 if (this_cu->v.quick->no_file_data)
2999 return NULL;
3000 return this_cu->v.quick->file_names;
9291a0cd
TT
3001}
3002
3003/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3004 real path for a given file name from the line table. */
2fdf6df6 3005
9291a0cd 3006static const char *
976ca316 3007dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3008 struct quick_file_names *qfn, int index)
9291a0cd 3009{
7b9f3c50 3010 if (qfn->real_names == NULL)
976ca316 3011 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3012 qfn->num_file_names, const char *);
9291a0cd 3013
7b9f3c50 3014 if (qfn->real_names[index] == NULL)
14278e1f 3015 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3016
7b9f3c50 3017 return qfn->real_names[index];
9291a0cd
TT
3018}
3019
39298a5d
TT
3020struct symtab *
3021dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
9291a0cd 3022{
976ca316 3023 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
0d305d5c
TT
3024 dwarf2_per_cu_data *dwarf_cu
3025 = per_objfile->per_bfd->all_comp_units.back ().get ();
976ca316 3026 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3027
43f3e411
DE
3028 if (cust == NULL)
3029 return NULL;
ed2dc618 3030
43f3e411 3031 return compunit_primary_filetab (cust);
9291a0cd
TT
3032}
3033
7b9f3c50
DE
3034/* Traversal function for dw2_forget_cached_source_info. */
3035
3036static int
3037dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3038{
7b9f3c50 3039 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3040
7b9f3c50 3041 if (file_data->real_names)
9291a0cd 3042 {
7b9f3c50 3043 int i;
9291a0cd 3044
7b9f3c50 3045 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3046 {
7b9f3c50
DE
3047 xfree ((void*) file_data->real_names[i]);
3048 file_data->real_names[i] = NULL;
9291a0cd
TT
3049 }
3050 }
7b9f3c50
DE
3051
3052 return 1;
3053}
3054
39298a5d
TT
3055void
3056dwarf2_base_index_functions::forget_cached_source_info
3057 (struct objfile *objfile)
7b9f3c50 3058{
976ca316 3059 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3060
976ca316 3061 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3062 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3063}
3064
da51c347
DE
3065/* Struct used to manage iterating over all CUs looking for a symbol. */
3066
3067struct dw2_symtab_iterator
9291a0cd 3068{
ed2dc618 3069 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3070 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3071 /* If set, only look for symbols that match that block. Valid values are
3072 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3073 gdb::optional<block_enum> block_index;
da51c347
DE
3074 /* The kind of symbol we're looking for. */
3075 domain_enum domain;
3076 /* The list of CUs from the index entry of the symbol,
3077 or NULL if not found. */
42c2c694 3078 offset_view vec;
da51c347
DE
3079 /* The next element in VEC to look at. */
3080 int next;
3081 /* The number of elements in VEC, or zero if there is no match. */
3082 int length;
8943b874
DE
3083 /* Have we seen a global version of the symbol?
3084 If so we can ignore all further global instances.
3085 This is to work around gold/15646, inefficient gold-generated
3086 indices. */
3087 int global_seen;
da51c347 3088};
9291a0cd 3089
7089bd88 3090/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2fdf6df6 3091
9291a0cd 3092static void
7089bd88
TT
3093dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3094 dwarf2_per_objfile *per_objfile,
3095 gdb::optional<block_enum> block_index,
3096 domain_enum domain, offset_type namei)
da51c347 3097{
976ca316 3098 iter->per_objfile = per_objfile;
da51c347
DE
3099 iter->block_index = block_index;
3100 iter->domain = domain;
3101 iter->next = 0;
8943b874 3102 iter->global_seen = 0;
42c2c694 3103 iter->vec = {};
e5f3ece2 3104 iter->length = 0;
e5f3ece2
TV
3105
3106 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3107 /* index is NULL if OBJF_READNOW. */
3108 if (index == NULL)
3109 return;
3110
3111 gdb_assert (!index->symbol_name_slot_invalid (namei));
42c2c694 3112 offset_type vec_idx = index->symbol_vec_index (namei);
e5f3ece2 3113
42c2c694
TT
3114 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3115 iter->length = iter->vec[0];
da51c347
DE
3116}
3117
3118/* Return the next matching CU or NULL if there are no more. */
3119
3120static struct dwarf2_per_cu_data *
3121dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3122{
976ca316 3123 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3124
da51c347
DE
3125 for ( ; iter->next < iter->length; ++iter->next)
3126 {
42c2c694 3127 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
da51c347 3128 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3129 gdb_index_symbol_kind symbol_kind =
3130 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3131 /* Only check the symbol attributes if they're present.
3132 Indices prior to version 7 don't record them,
3133 and indices >= 7 may elide them for certain symbols
3134 (gold does this). */
3135 int attrs_valid =
976ca316 3136 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3137 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3138
3190f0c6 3139 /* Don't crash on bad data. */
91eea9cc 3140 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3190f0c6 3141 {
b98664d3 3142 complaint (_(".gdb_index entry has bad CU index"
976ca316 3143 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3144 continue;
3145 }
3146
91eea9cc 3147 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3190f0c6 3148
da51c347 3149 /* Skip if already read in. */
976ca316 3150 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3151 continue;
3152
8943b874
DE
3153 /* Check static vs global. */
3154 if (attrs_valid)
3155 {
2b79f376
SM
3156 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3157
3158 if (iter->block_index.has_value ())
3159 {
3160 bool want_static = *iter->block_index == STATIC_BLOCK;
3161
3162 if (is_static != want_static)
3163 continue;
3164 }
3165
8943b874 3166 /* Work around gold/15646. */
f030440d
TV
3167 if (!is_static
3168 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3169 {
3170 if (iter->global_seen)
3171 continue;
3172
3173 iter->global_seen = 1;
3174 }
8943b874 3175 }
da51c347
DE
3176
3177 /* Only check the symbol's kind if it has one. */
3178 if (attrs_valid)
3179 {
3180 switch (iter->domain)
3181 {
3182 case VAR_DOMAIN:
3183 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3184 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3185 /* Some types are also in VAR_DOMAIN. */
3186 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3187 continue;
3188 break;
3189 case STRUCT_DOMAIN:
3190 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3191 continue;
3192 break;
3193 case LABEL_DOMAIN:
3194 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3195 continue;
3196 break;
59c35742
AB
3197 case MODULE_DOMAIN:
3198 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3199 continue;
3200 break;
da51c347
DE
3201 default:
3202 break;
3203 }
3204 }
3205
3206 ++iter->next;
3207 return per_cu;
3208 }
3209
3210 return NULL;
3211}
3212
39298a5d 3213void
4829711b
TT
3214dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3215 bool print_bcache)
9291a0cd 3216{
4829711b
TT
3217 if (print_bcache)
3218 return;
3219
976ca316 3220 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
91eea9cc 3221 int total = per_objfile->per_bfd->all_comp_units.size ();
ed2dc618 3222 int count = 0;
9291a0cd 3223
ed2dc618 3224 for (int i = 0; i < total; ++i)
9291a0cd 3225 {
91eea9cc 3226 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
9291a0cd 3227
976ca316 3228 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3229 ++count;
3230 }
e4a48d9d 3231 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3232 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3233}
3234
779bd270
DE
3235/* This dumps minimal information about the index.
3236 It is called via "mt print objfiles".
3237 One use is to verify .gdb_index has been loaded by the
3238 gdb.dwarf2/gdb-index.exp testcase. */
3239
39298a5d
TT
3240void
3241dwarf2_gdb_index::dump (struct objfile *objfile)
9291a0cd 3242{
976ca316 3243 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3244
976ca316 3245 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3246 printf_filtered (".gdb_index:");
976ca316 3247 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3248 {
3249 printf_filtered (" version %d\n",
976ca316 3250 per_objfile->per_bfd->index_table->version);
779bd270
DE
3251 }
3252 else
3253 printf_filtered (" faked for \"readnow\"\n");
3254 printf_filtered ("\n");
9291a0cd
TT
3255}
3256
39298a5d
TT
3257void
3258dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
9291a0cd 3259{
976ca316 3260 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
91eea9cc 3261 int total_units = per_objfile->per_bfd->all_comp_units.size ();
9291a0cd 3262
ed2dc618 3263 for (int i = 0; i < total_units; ++i)
9291a0cd 3264 {
91eea9cc 3265 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
9291a0cd 3266
58f0c718
TT
3267 /* We don't want to directly expand a partial CU, because if we
3268 read it with the wrong language, then assertion failures can
3269 be triggered later on. See PR symtab/23010. So, tell
3270 dw2_instantiate_symtab to skip partial CUs -- any important
3271 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3272 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3273 }
3274}
3275
df35e626 3276static bool
9a0bacfb
TV
3277dw2_expand_symtabs_matching_symbol
3278 (mapped_index_base &index,
3279 const lookup_name_info &lookup_name_in,
3280 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3281 gdb::function_view<bool (offset_type)> match_callback,
3282 dwarf2_per_objfile *per_objfile);
9a0bacfb 3283
df35e626 3284static bool
9a0bacfb 3285dw2_expand_symtabs_matching_one
97a1449a
SM
3286 (dwarf2_per_cu_data *per_cu,
3287 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3288 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3289 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3290
af82f89d 3291void
0b7b2c2a 3292dwarf2_gdb_index::expand_matching_symbols
199b4314 3293 (struct objfile *objfile,
b054970d 3294 const lookup_name_info &name, domain_enum domain,
199b4314 3295 int global,
199b4314 3296 symbol_compare_ftype *ordered_compare)
9291a0cd 3297{
1aa98955 3298 /* Used for Ada. */
976ca316 3299 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3300
9a0bacfb
TV
3301 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3302
976ca316 3303 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3304 {
976ca316 3305 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3306
9a0bacfb
TV
3307 const char *match_name = name.ada ().lookup_name ().c_str ();
3308 auto matcher = [&] (const char *symname)
3309 {
3310 if (ordered_compare == nullptr)
3311 return true;
3312 return ordered_compare (symname, match_name) == 0;
3313 };
3314
b64f703b 3315 dw2_expand_symtabs_matching_symbol (index, name, matcher,
9a0bacfb
TV
3316 [&] (offset_type namei)
3317 {
3318 struct dw2_symtab_iterator iter;
3319 struct dwarf2_per_cu_data *per_cu;
3320
976ca316 3321 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3322 namei);
9a0bacfb 3323 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3324 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3325 nullptr);
9a0bacfb 3326 return true;
976ca316 3327 }, per_objfile);
9a0bacfb
TV
3328 }
3329 else
3330 {
3331 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3332 proceed assuming all symtabs have been read in. */
3333 }
9291a0cd
TT
3334}
3335
e1ef7d7a
PA
3336/* Starting from a search name, return the string that finds the upper
3337 bound of all strings that start with SEARCH_NAME in a sorted name
3338 list. Returns the empty string to indicate that the upper bound is
3339 the end of the list. */
3340
3341static std::string
3342make_sort_after_prefix_name (const char *search_name)
3343{
3344 /* When looking to complete "func", we find the upper bound of all
3345 symbols that start with "func" by looking for where we'd insert
3346 the closest string that would follow "func" in lexicographical
3347 order. Usually, that's "func"-with-last-character-incremented,
3348 i.e. "fund". Mind non-ASCII characters, though. Usually those
3349 will be UTF-8 multi-byte sequences, but we can't be certain.
3350 Especially mind the 0xff character, which is a valid character in
3351 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3352 rule out compilers allowing it in identifiers. Note that
3353 conveniently, strcmp/strcasecmp are specified to compare
3354 characters interpreted as unsigned char. So what we do is treat
3355 the whole string as a base 256 number composed of a sequence of
3356 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3357 to 0, and carries 1 to the following more-significant position.
3358 If the very first character in SEARCH_NAME ends up incremented
3359 and carries/overflows, then the upper bound is the end of the
3360 list. The string after the empty string is also the empty
3361 string.
3362
3363 Some examples of this operation:
3364
3365 SEARCH_NAME => "+1" RESULT
3366
3367 "abc" => "abd"
3368 "ab\xff" => "ac"
3369 "\xff" "a" "\xff" => "\xff" "b"
3370 "\xff" => ""
3371 "\xff\xff" => ""
3372 "" => ""
3373
3374 Then, with these symbols for example:
3375
3376 func
3377 func1
3378 fund
3379
3380 completing "func" looks for symbols between "func" and
3381 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3382 which finds "func" and "func1", but not "fund".
3383
3384 And with:
3385
3386 funcÿ (Latin1 'ÿ' [0xff])
3387 funcÿ1
3388 fund
3389
3390 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3391 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3392
3393 And with:
3394
3395 ÿÿ (Latin1 'ÿ' [0xff])
3396 ÿÿ1
3397
3398 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3399 the end of the list.
3400 */
3401 std::string after = search_name;
3402 while (!after.empty () && (unsigned char) after.back () == 0xff)
3403 after.pop_back ();
3404 if (!after.empty ())
3405 after.back () = (unsigned char) after.back () + 1;
3406 return after;
3407}
3408
5c58de74 3409/* See declaration. */
61d96d7e 3410
5c58de74
PA
3411std::pair<std::vector<name_component>::const_iterator,
3412 std::vector<name_component>::const_iterator>
44ed8f3e 3413mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3414 (const lookup_name_info &lookup_name_without_params, language lang,
3415 dwarf2_per_objfile *per_objfile) const
3f563c84 3416{
5c58de74
PA
3417 auto *name_cmp
3418 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3419
3b00ef10 3420 const char *lang_name
e0802d59 3421 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3422
3f563c84
PA
3423 /* Comparison function object for lower_bound that matches against a
3424 given symbol name. */
3425 auto lookup_compare_lower = [&] (const name_component &elem,
3426 const char *name)
3427 {
fcf23d5b 3428 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3429 const char *elem_name = elem_qualified + elem.name_offset;
3430 return name_cmp (elem_name, name) < 0;
3431 };
3432
3433 /* Comparison function object for upper_bound that matches against a
3434 given symbol name. */
3435 auto lookup_compare_upper = [&] (const char *name,
3436 const name_component &elem)
3437 {
fcf23d5b 3438 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3439 const char *elem_name = elem_qualified + elem.name_offset;
3440 return name_cmp (name, elem_name) < 0;
3441 };
3442
5c58de74
PA
3443 auto begin = this->name_components.begin ();
3444 auto end = this->name_components.end ();
3f563c84
PA
3445
3446 /* Find the lower bound. */
3447 auto lower = [&] ()
3448 {
3b00ef10 3449 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3450 return begin;
3451 else
3b00ef10 3452 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3453 } ();
3454
3455 /* Find the upper bound. */
3456 auto upper = [&] ()
3457 {
5c58de74 3458 if (lookup_name_without_params.completion_mode ())
3f563c84 3459 {
e1ef7d7a
PA
3460 /* In completion mode, we want UPPER to point past all
3461 symbols names that have the same prefix. I.e., with
3462 these symbols, and completing "func":
3463
3464 function << lower bound
3465 function1
3466 other_function << upper bound
3467
3468 We find the upper bound by looking for the insertion
3469 point of "func"-with-last-character-incremented,
3470 i.e. "fund". */
3b00ef10 3471 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3472 if (after.empty ())
3f563c84 3473 return end;
e6b2f5ef
PA
3474 return std::lower_bound (lower, end, after.c_str (),
3475 lookup_compare_lower);
3f563c84
PA
3476 }
3477 else
3b00ef10 3478 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3479 } ();
3480
5c58de74
PA
3481 return {lower, upper};
3482}
3483
3484/* See declaration. */
3485
3486void
fcf23d5b 3487mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3488{
3489 if (!this->name_components.empty ())
3490 return;
3491
3492 this->name_components_casing = case_sensitivity;
3493 auto *name_cmp
3494 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3495
3496 /* The code below only knows how to break apart components of C++
3497 symbol names (and other languages that use '::' as
3b00ef10 3498 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3499 auto count = this->symbol_name_count ();
3500 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3501 {
44ed8f3e 3502 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3503 continue;
3504
fcf23d5b 3505 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3506
3507 /* Add each name component to the name component table. */
3508 unsigned int previous_len = 0;
3b00ef10
TT
3509
3510 if (strstr (name, "::") != nullptr)
3511 {
3512 for (unsigned int current_len = cp_find_first_component (name);
3513 name[current_len] != '\0';
3514 current_len += cp_find_first_component (name + current_len))
3515 {
3516 gdb_assert (name[current_len] == ':');
3517 this->name_components.push_back ({previous_len, idx});
3518 /* Skip the '::'. */
3519 current_len += 2;
3520 previous_len = current_len;
3521 }
3522 }
3523 else
5c58de74 3524 {
3b00ef10
TT
3525 /* Handle the Ada encoded (aka mangled) form here. */
3526 for (const char *iter = strstr (name, "__");
3527 iter != nullptr;
3528 iter = strstr (iter, "__"))
3529 {
3530 this->name_components.push_back ({previous_len, idx});
3531 iter += 2;
3532 previous_len = iter - name;
3533 }
5c58de74 3534 }
3b00ef10 3535
5c58de74
PA
3536 this->name_components.push_back ({previous_len, idx});
3537 }
3538
3539 /* Sort name_components elements by name. */
3540 auto name_comp_compare = [&] (const name_component &left,
3541 const name_component &right)
3542 {
fcf23d5b
SM
3543 const char *left_qualified
3544 = this->symbol_name_at (left.idx, per_objfile);
3545 const char *right_qualified
3546 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
3547
3548 const char *left_name = left_qualified + left.name_offset;
3549 const char *right_name = right_qualified + right.name_offset;
3550
3551 return name_cmp (left_name, right_name) < 0;
3552 };
3553
3554 std::sort (this->name_components.begin (),
3555 this->name_components.end (),
3556 name_comp_compare);
3557}
3558
3559/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3560 mapped_index_base instead of the containing objfile. This is split
3561 to a separate function in order to be able to unit test the
3562 name_components matching using a mock mapped_index_base. For each
5c58de74 3563 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3564 symbol's index in the mapped_index_base symbol table. */
5c58de74 3565
df35e626 3566static bool
5c58de74 3567dw2_expand_symtabs_matching_symbol
44ed8f3e 3568 (mapped_index_base &index,
5c58de74
PA
3569 const lookup_name_info &lookup_name_in,
3570 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3571 gdb::function_view<bool (offset_type)> match_callback,
3572 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3573{
3574 lookup_name_info lookup_name_without_params
3575 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3576
3577 /* Build the symbol name component sorted vector, if we haven't
3578 yet. */
fcf23d5b 3579 index.build_name_components (per_objfile);
5c58de74 3580
3f563c84
PA
3581 /* The same symbol may appear more than once in the range though.
3582 E.g., if we're looking for symbols that complete "w", and we have
3583 a symbol named "w1::w2", we'll find the two name components for
3584 that same symbol in the range. To be sure we only call the
3585 callback once per symbol, we first collect the symbol name
3586 indexes that matched in a temporary vector and ignore
3587 duplicates. */
3588 std::vector<offset_type> matches;
3f563c84 3589
3b00ef10
TT
3590 struct name_and_matcher
3591 {
3592 symbol_name_matcher_ftype *matcher;
ecc6c606 3593 const char *name;
3b00ef10
TT
3594
3595 bool operator== (const name_and_matcher &other) const
3f563c84 3596 {
ecc6c606 3597 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3598 }
3599 };
3600
3601 /* A vector holding all the different symbol name matchers, for all
3602 languages. */
3603 std::vector<name_and_matcher> matchers;
3604
3605 for (int i = 0; i < nr_languages; i++)
3606 {
3607 enum language lang_e = (enum language) i;
3608
3609 const language_defn *lang = language_def (lang_e);
3610 symbol_name_matcher_ftype *name_matcher
c9debfb9 3611 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 3612
3b00ef10 3613 name_and_matcher key {
dda83cd7 3614 name_matcher,
3b00ef10
TT
3615 lookup_name_without_params.language_lookup_name (lang_e)
3616 };
3617
3618 /* Don't insert the same comparison routine more than once.
3619 Note that we do this linear walk. This is not a problem in
3620 practice because the number of supported languages is
3621 low. */
3622 if (std::find (matchers.begin (), matchers.end (), key)
3623 != matchers.end ())
9291a0cd 3624 continue;
3b00ef10
TT
3625 matchers.push_back (std::move (key));
3626
3627 auto bounds
3628 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 3629 lang_e, per_objfile);
3b00ef10
TT
3630
3631 /* Now for each symbol name in range, check to see if we have a name
3632 match, and if so, call the MATCH_CALLBACK callback. */
3633
3634 for (; bounds.first != bounds.second; ++bounds.first)
3635 {
fcf23d5b
SM
3636 const char *qualified
3637 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
3638
3639 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3640 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3641 continue;
9291a0cd 3642
3b00ef10
TT
3643 matches.push_back (bounds.first->idx);
3644 }
3f563c84
PA
3645 }
3646
3647 std::sort (matches.begin (), matches.end ());
3648
3649 /* Finally call the callback, once per match. */
3650 ULONGEST prev = -1;
df35e626 3651 bool result = true;
3f563c84
PA
3652 for (offset_type idx : matches)
3653 {
3654 if (prev != idx)
3655 {
3b00ef10 3656 if (!match_callback (idx))
df35e626
TT
3657 {
3658 result = false;
3659 break;
3660 }
3f563c84
PA
3661 prev = idx;
3662 }
3663 }
3664
3665 /* Above we use a type wider than idx's for 'prev', since 0 and
3666 (offset_type)-1 are both possible values. */
3667 static_assert (sizeof (prev) > sizeof (offset_type), "");
df35e626
TT
3668
3669 return result;
3f563c84
PA
3670}
3671
c62446b1
PA
3672#if GDB_SELF_TEST
3673
3674namespace selftests { namespace dw2_expand_symtabs_matching {
3675
a3c5fafd
PA
3676/* A mock .gdb_index/.debug_names-like name index table, enough to
3677 exercise dw2_expand_symtabs_matching_symbol, which works with the
3678 mapped_index_base interface. Builds an index from the symbol list
3679 passed as parameter to the constructor. */
3680class mock_mapped_index : public mapped_index_base
c62446b1
PA
3681{
3682public:
a3c5fafd
PA
3683 mock_mapped_index (gdb::array_view<const char *> symbols)
3684 : m_symbol_table (symbols)
c62446b1
PA
3685 {}
3686
a3c5fafd 3687 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 3688
a3c5fafd 3689 /* Return the number of names in the symbol table. */
632e107b 3690 size_t symbol_name_count () const override
c62446b1 3691 {
a3c5fafd 3692 return m_symbol_table.size ();
c62446b1
PA
3693 }
3694
a3c5fafd 3695 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
3696 const char *symbol_name_at
3697 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
3698 {
3699 return m_symbol_table[idx];
3700 }
c62446b1 3701
a3c5fafd
PA
3702private:
3703 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
3704};
3705
3706/* Convenience function that converts a NULL pointer to a "<null>"
3707 string, to pass to print routines. */
3708
3709static const char *
3710string_or_null (const char *str)
3711{
3712 return str != NULL ? str : "<null>";
3713}
3714
3715/* Check if a lookup_name_info built from
3716 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3717 index. EXPECTED_LIST is the list of expected matches, in expected
3718 matching order. If no match expected, then an empty list is
3719 specified. Returns true on success. On failure prints a warning
3720 indicating the file:line that failed, and returns false. */
3721
3722static bool
3723check_match (const char *file, int line,
3724 mock_mapped_index &mock_index,
3725 const char *name, symbol_name_match_type match_type,
3726 bool completion_mode,
fcf23d5b
SM
3727 std::initializer_list<const char *> expected_list,
3728 dwarf2_per_objfile *per_objfile)
c62446b1
PA
3729{
3730 lookup_name_info lookup_name (name, match_type, completion_mode);
3731
3732 bool matched = true;
3733
3734 auto mismatch = [&] (const char *expected_str,
3735 const char *got)
3736 {
3737 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3738 "expected=\"%s\", got=\"%s\"\n"),
3739 file, line,
3740 (match_type == symbol_name_match_type::FULL
3741 ? "FULL" : "WILD"),
3742 name, string_or_null (expected_str), string_or_null (got));
3743 matched = false;
3744 };
3745
3746 auto expected_it = expected_list.begin ();
3747 auto expected_end = expected_list.end ();
3748
a3c5fafd 3749 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
b64f703b 3750 nullptr,
c62446b1
PA
3751 [&] (offset_type idx)
3752 {
fcf23d5b 3753 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
3754 const char *expected_str
3755 = expected_it == expected_end ? NULL : *expected_it++;
3756
3757 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3758 mismatch (expected_str, matched_name);
3b00ef10 3759 return true;
fcf23d5b 3760 }, per_objfile);
c62446b1
PA
3761
3762 const char *expected_str
3763 = expected_it == expected_end ? NULL : *expected_it++;
3764 if (expected_str != NULL)
3765 mismatch (expected_str, NULL);
3766
3767 return matched;
3768}
3769
3770/* The symbols added to the mock mapped_index for testing (in
3771 canonical form). */
3772static const char *test_symbols[] = {
3773 "function",
3774 "std::bar",
3775 "std::zfunction",
3776 "std::zfunction2",
3777 "w1::w2",
3778 "ns::foo<char*>",
3779 "ns::foo<int>",
3780 "ns::foo<long>",
a20714ff
PA
3781 "ns2::tmpl<int>::foo2",
3782 "(anonymous namespace)::A::B::C",
c62446b1 3783
e1ef7d7a
PA
3784 /* These are used to check that the increment-last-char in the
3785 matching algorithm for completion doesn't match "t1_fund" when
3786 completing "t1_func". */
3787 "t1_func",
3788 "t1_func1",
3789 "t1_fund",
3790 "t1_fund1",
3791
3792 /* A UTF-8 name with multi-byte sequences to make sure that
3793 cp-name-parser understands this as a single identifier ("função"
3794 is "function" in PT). */
3795 u8"u8função",
3796
3797 /* \377 (0xff) is Latin1 'ÿ'. */
3798 "yfunc\377",
3799
3800 /* \377 (0xff) is Latin1 'ÿ'. */
3801 "\377",
3802 "\377\377123",
3803
c62446b1
PA
3804 /* A name with all sorts of complications. Starts with "z" to make
3805 it easier for the completion tests below. */
3806#define Z_SYM_NAME \
3807 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3808 "::tuple<(anonymous namespace)::ui*, " \
3809 "std::default_delete<(anonymous namespace)::ui>, void>"
3810
3811 Z_SYM_NAME
3812};
3813
a3c5fafd
PA
3814/* Returns true if the mapped_index_base::find_name_component_bounds
3815 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3816 in completion mode. */
5c58de74
PA
3817
3818static bool
a3c5fafd 3819check_find_bounds_finds (mapped_index_base &index,
5c58de74 3820 const char *search_name,
fcf23d5b
SM
3821 gdb::array_view<const char *> expected_syms,
3822 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3823{
3824 lookup_name_info lookup_name (search_name,
3825 symbol_name_match_type::FULL, true);
3826
3b00ef10 3827 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
3828 language_cplus,
3829 per_objfile);
5c58de74
PA
3830
3831 size_t distance = std::distance (bounds.first, bounds.second);
3832 if (distance != expected_syms.size ())
3833 return false;
3834
3835 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3836 {
3837 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 3838 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
3839 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3840 return false;
3841 }
3842
3843 return true;
3844}
3845
3846/* Test the lower-level mapped_index::find_name_component_bounds
3847 method. */
3848
c62446b1 3849static void
5c58de74
PA
3850test_mapped_index_find_name_component_bounds ()
3851{
3852 mock_mapped_index mock_index (test_symbols);
3853
fcf23d5b 3854 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
3855
3856 /* Test the lower-level mapped_index::find_name_component_bounds
3857 method in completion mode. */
3858 {
3859 static const char *expected_syms[] = {
3860 "t1_func",
3861 "t1_func1",
5c58de74
PA
3862 };
3863
fcf23d5b
SM
3864 SELF_CHECK (check_find_bounds_finds
3865 (mock_index, "t1_func", expected_syms,
3866 NULL /* per_objfile */));
5c58de74
PA
3867 }
3868
3869 /* Check that the increment-last-char in the name matching algorithm
3870 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3871 {
3872 static const char *expected_syms1[] = {
3873 "\377",
3874 "\377\377123",
3875 };
fcf23d5b
SM
3876 SELF_CHECK (check_find_bounds_finds
3877 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
3878
3879 static const char *expected_syms2[] = {
3880 "\377\377123",
3881 };
fcf23d5b
SM
3882 SELF_CHECK (check_find_bounds_finds
3883 (mock_index, "\377\377", expected_syms2,
3884 NULL /* per_objfile */));
5c58de74
PA
3885 }
3886}
3887
3888/* Test dw2_expand_symtabs_matching_symbol. */
3889
3890static void
3891test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
3892{
3893 mock_mapped_index mock_index (test_symbols);
3894
3895 /* We let all tests run until the end even if some fails, for debug
3896 convenience. */
3897 bool any_mismatch = false;
3898
3899 /* Create the expected symbols list (an initializer_list). Needed
3900 because lists have commas, and we need to pass them to CHECK,
3901 which is a macro. */
3902#define EXPECT(...) { __VA_ARGS__ }
3903
3904 /* Wrapper for check_match that passes down the current
3905 __FILE__/__LINE__. */
3906#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3907 any_mismatch |= !check_match (__FILE__, __LINE__, \
3908 mock_index, \
3909 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 3910 EXPECTED_LIST, NULL)
c62446b1
PA
3911
3912 /* Identity checks. */
3913 for (const char *sym : test_symbols)
3914 {
3915 /* Should be able to match all existing symbols. */
3916 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3917 EXPECT (sym));
3918
3919 /* Should be able to match all existing symbols with
3920 parameters. */
3921 std::string with_params = std::string (sym) + "(int)";
3922 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3923 EXPECT (sym));
3924
3925 /* Should be able to match all existing symbols with
3926 parameters and qualifiers. */
3927 with_params = std::string (sym) + " ( int ) const";
3928 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3929 EXPECT (sym));
3930
3931 /* This should really find sym, but cp-name-parser.y doesn't
3932 know about lvalue/rvalue qualifiers yet. */
3933 with_params = std::string (sym) + " ( int ) &&";
3934 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3935 {});
3936 }
3937
e1ef7d7a
PA
3938 /* Check that the name matching algorithm for completion doesn't get
3939 confused with Latin1 'ÿ' / 0xff. */
3940 {
3941 static const char str[] = "\377";
3942 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3943 EXPECT ("\377", "\377\377123"));
3944 }
3945
3946 /* Check that the increment-last-char in the matching algorithm for
3947 completion doesn't match "t1_fund" when completing "t1_func". */
3948 {
3949 static const char str[] = "t1_func";
3950 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3951 EXPECT ("t1_func", "t1_func1"));
3952 }
3953
c62446b1
PA
3954 /* Check that completion mode works at each prefix of the expected
3955 symbol name. */
3956 {
3957 static const char str[] = "function(int)";
3958 size_t len = strlen (str);
3959 std::string lookup;
3960
3961 for (size_t i = 1; i < len; i++)
3962 {
3963 lookup.assign (str, i);
3964 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3965 EXPECT ("function"));
3966 }
3967 }
3968
3969 /* While "w" is a prefix of both components, the match function
3970 should still only be called once. */
3971 {
3972 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3973 EXPECT ("w1::w2"));
a20714ff
PA
3974 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3975 EXPECT ("w1::w2"));
c62446b1
PA
3976 }
3977
3978 /* Same, with a "complicated" symbol. */
3979 {
3980 static const char str[] = Z_SYM_NAME;
3981 size_t len = strlen (str);
3982 std::string lookup;
3983
3984 for (size_t i = 1; i < len; i++)
3985 {
3986 lookup.assign (str, i);
3987 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3988 EXPECT (Z_SYM_NAME));
3989 }
3990 }
3991
3992 /* In FULL mode, an incomplete symbol doesn't match. */
3993 {
3994 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3995 {});
3996 }
3997
3998 /* A complete symbol with parameters matches any overload, since the
3999 index has no overload info. */
4000 {
4001 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4002 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4003 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4004 EXPECT ("std::zfunction", "std::zfunction2"));
4005 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4006 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4007 }
4008
4009 /* Check that whitespace is ignored appropriately. A symbol with a
4010 template argument list. */
4011 {
4012 static const char expected[] = "ns::foo<int>";
4013 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4014 EXPECT (expected));
a20714ff
PA
4015 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4016 EXPECT (expected));
c62446b1
PA
4017 }
4018
4019 /* Check that whitespace is ignored appropriately. A symbol with a
4020 template argument list that includes a pointer. */
4021 {
4022 static const char expected[] = "ns::foo<char*>";
4023 /* Try both completion and non-completion modes. */
4024 static const bool completion_mode[2] = {false, true};
4025 for (size_t i = 0; i < 2; i++)
4026 {
4027 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4028 completion_mode[i], EXPECT (expected));
a20714ff
PA
4029 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4030 completion_mode[i], EXPECT (expected));
c62446b1
PA
4031
4032 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4033 completion_mode[i], EXPECT (expected));
a20714ff
PA
4034 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4035 completion_mode[i], EXPECT (expected));
c62446b1
PA
4036 }
4037 }
4038
4039 {
4040 /* Check method qualifiers are ignored. */
4041 static const char expected[] = "ns::foo<char*>";
4042 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4043 symbol_name_match_type::FULL, true, EXPECT (expected));
4044 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4045 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4046 CHECK_MATCH ("foo < char * > ( int ) const",
4047 symbol_name_match_type::WILD, true, EXPECT (expected));
4048 CHECK_MATCH ("foo < char * > ( int ) &&",
4049 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4050 }
4051
4052 /* Test lookup names that don't match anything. */
4053 {
a20714ff
PA
4054 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4055 {});
4056
c62446b1
PA
4057 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4058 {});
4059 }
4060
a20714ff
PA
4061 /* Some wild matching tests, exercising "(anonymous namespace)",
4062 which should not be confused with a parameter list. */
4063 {
4064 static const char *syms[] = {
4065 "A::B::C",
4066 "B::C",
4067 "C",
4068 "A :: B :: C ( int )",
4069 "B :: C ( int )",
4070 "C ( int )",
4071 };
4072
4073 for (const char *s : syms)
4074 {
4075 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4076 EXPECT ("(anonymous namespace)::A::B::C"));
4077 }
4078 }
4079
4080 {
4081 static const char expected[] = "ns2::tmpl<int>::foo2";
4082 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4083 EXPECT (expected));
4084 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4085 EXPECT (expected));
4086 }
4087
c62446b1
PA
4088 SELF_CHECK (!any_mismatch);
4089
4090#undef EXPECT
4091#undef CHECK_MATCH
4092}
4093
5c58de74
PA
4094static void
4095run_test ()
4096{
4097 test_mapped_index_find_name_component_bounds ();
4098 test_dw2_expand_symtabs_matching_symbol ();
4099}
4100
c62446b1
PA
4101}} // namespace selftests::dw2_expand_symtabs_matching
4102
4103#endif /* GDB_SELF_TEST */
4104
4b514bc8
JK
4105/* If FILE_MATCHER is NULL or if PER_CU has
4106 dwarf2_per_cu_quick_data::MARK set (see
4107 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4108 EXPANSION_NOTIFY on it. */
4109
df35e626 4110static bool
4b514bc8 4111dw2_expand_symtabs_matching_one
97a1449a
SM
4112 (dwarf2_per_cu_data *per_cu,
4113 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4114 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4115 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4116{
4117 if (file_matcher == NULL || per_cu->v.quick->mark)
4118 {
af758d11 4119 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4120
97a1449a
SM
4121 compunit_symtab *symtab
4122 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4123 gdb_assert (symtab != nullptr);
4b514bc8 4124
af758d11 4125 if (expansion_notify != NULL && symtab_was_null)
df35e626 4126 return expansion_notify (symtab);
4b514bc8 4127 }
df35e626 4128 return true;
4b514bc8
JK
4129}
4130
3f563c84
PA
4131/* Helper for dw2_expand_matching symtabs. Called on each symbol
4132 matched, to expand corresponding CUs that were marked. IDX is the
4133 index of the symbol name that matched. */
4134
df35e626 4135static bool
3f563c84 4136dw2_expand_marked_cus
976ca316 4137 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4138 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4139 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4140 block_search_flags search_flags,
3f563c84
PA
4141 search_domain kind)
4142{
42c2c694 4143 offset_type vec_len, vec_idx;
3f563c84 4144 bool global_seen = false;
976ca316 4145 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4146
42c2c694
TT
4147 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4148 vec_len = vec[0];
61920122
PA
4149 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4150 {
42c2c694 4151 offset_type cu_index_and_attrs = vec[vec_idx + 1];
61920122
PA
4152 /* This value is only valid for index versions >= 7. */
4153 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4154 gdb_index_symbol_kind symbol_kind =
4155 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4156 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4157 /* Only check the symbol attributes if they're present.
4158 Indices prior to version 7 don't record them,
4159 and indices >= 7 may elide them for certain symbols
4160 (gold does this). */
4161 int attrs_valid =
4162 (index.version >= 7
4163 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4164
4165 /* Work around gold/15646. */
f030440d
TV
4166 if (attrs_valid
4167 && !is_static
4168 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4169 {
f030440d 4170 if (global_seen)
61920122 4171 continue;
f030440d
TV
4172
4173 global_seen = true;
61920122 4174 }
3190f0c6 4175
61920122
PA
4176 /* Only check the symbol's kind if it has one. */
4177 if (attrs_valid)
4178 {
03a8ea51
TT
4179 if (is_static)
4180 {
4181 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4182 continue;
4183 }
4184 else
4185 {
4186 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4187 continue;
4188 }
4189
61920122 4190 switch (kind)
8943b874 4191 {
61920122
PA
4192 case VARIABLES_DOMAIN:
4193 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4194 continue;
4195 break;
4196 case FUNCTIONS_DOMAIN:
4197 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4198 continue;
61920122
PA
4199 break;
4200 case TYPES_DOMAIN:
4201 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4202 continue;
4203 break;
59c35742
AB
4204 case MODULES_DOMAIN:
4205 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4206 continue;
4207 break;
61920122
PA
4208 default:
4209 break;
8943b874 4210 }
61920122 4211 }
8943b874 4212
61920122 4213 /* Don't crash on bad data. */
91eea9cc 4214 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
61920122 4215 {
b98664d3 4216 complaint (_(".gdb_index entry has bad CU index"
976ca316 4217 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4218 continue;
4219 }
4220
91eea9cc 4221 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
df35e626
TT
4222 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4223 expansion_notify))
4224 return false;
61920122 4225 }
df35e626
TT
4226
4227 return true;
61920122
PA
4228}
4229
4b514bc8
JK
4230/* If FILE_MATCHER is non-NULL, set all the
4231 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4232 that match FILE_MATCHER. */
4233
61920122 4234static void
4b514bc8 4235dw_expand_symtabs_matching_file_matcher
976ca316 4236 (dwarf2_per_objfile *per_objfile,
ed2dc618 4237 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4238{
4b514bc8 4239 if (file_matcher == NULL)
61920122
PA
4240 return;
4241
4b514bc8
JK
4242 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4243 htab_eq_pointer,
4244 NULL, xcalloc, xfree));
4245 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4246 htab_eq_pointer,
4247 NULL, xcalloc, xfree));
61920122 4248
4b514bc8
JK
4249 /* The rule is CUs specify all the files, including those used by
4250 any TU, so there's no need to scan TUs here. */
61920122 4251
0d305d5c 4252 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4253 {
927aa2e7
JK
4254 QUIT;
4255
91eea9cc
TT
4256 if (per_cu->is_debug_types)
4257 continue;
927aa2e7
JK
4258 per_cu->v.quick->mark = 0;
4259
4260 /* We only need to look at symtabs not already expanded. */
0d305d5c 4261 if (per_objfile->symtab_set_p (per_cu.get ()))
927aa2e7
JK
4262 continue;
4263
0d305d5c
TT
4264 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4265 per_objfile);
927aa2e7
JK
4266 if (file_data == NULL)
4267 continue;
4268
4269 if (htab_find (visited_not_found.get (), file_data) != NULL)
4270 continue;
4271 else if (htab_find (visited_found.get (), file_data) != NULL)
4272 {
4273 per_cu->v.quick->mark = 1;
4274 continue;
4275 }
4276
b76e467d 4277 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4278 {
4279 const char *this_real_name;
4280
4281 if (file_matcher (file_data->file_names[j], false))
4282 {
4283 per_cu->v.quick->mark = 1;
4284 break;
4285 }
4286
4287 /* Before we invoke realpath, which can get expensive when many
4288 files are involved, do a quick comparison of the basenames. */
4289 if (!basenames_may_differ
4290 && !file_matcher (lbasename (file_data->file_names[j]),
4291 true))
4292 continue;
4293
976ca316 4294 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4295 if (file_matcher (this_real_name, false))
4296 {
4297 per_cu->v.quick->mark = 1;
4298 break;
4299 }
4300 }
4301
b76e467d
SM
4302 void **slot = htab_find_slot (per_cu->v.quick->mark
4303 ? visited_found.get ()
4304 : visited_not_found.get (),
4305 file_data, INSERT);
927aa2e7
JK
4306 *slot = file_data;
4307 }
4308}
4309
df35e626 4310bool
af82f89d
TT
4311dwarf2_gdb_index::expand_symtabs_matching
4312 (struct objfile *objfile,
4313 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4314 const lookup_name_info *lookup_name,
4315 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4316 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4317 block_search_flags search_flags,
3bfa51a7 4318 domain_enum domain,
af82f89d 4319 enum search_domain kind)
927aa2e7 4320{
976ca316 4321 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4322
4323 /* index_table is NULL if OBJF_READNOW. */
976ca316 4324 if (!per_objfile->per_bfd->index_table)
df35e626 4325 return true;
927aa2e7 4326
976ca316 4327 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4328
c1a66c06
TV
4329 if (symbol_matcher == NULL && lookup_name == NULL)
4330 {
0d305d5c 4331 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4332 {
4333 QUIT;
4334
0d305d5c 4335 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
df35e626
TT
4336 file_matcher,
4337 expansion_notify))
4338 return false;
c1a66c06 4339 }
df35e626 4340 return true;
c1a66c06
TV
4341 }
4342
976ca316 4343 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4344
df35e626
TT
4345 bool result
4346 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4347 symbol_matcher,
4348 [&] (offset_type idx)
927aa2e7 4349 {
df35e626 4350 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
03a8ea51 4351 expansion_notify, search_flags, kind))
df35e626 4352 return false;
3b00ef10 4353 return true;
976ca316 4354 }, per_objfile);
df35e626
TT
4355
4356 return result;
927aa2e7
JK
4357}
4358
4359/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4360 symtab. */
4361
4362static struct compunit_symtab *
4363recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4364 CORE_ADDR pc)
4365{
4366 int i;
4367
4368 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4369 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4370 return cust;
4371
4372 if (cust->includes == NULL)
4373 return NULL;
4374
4375 for (i = 0; cust->includes[i]; ++i)
4376 {
4377 struct compunit_symtab *s = cust->includes[i];
4378
4379 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4380 if (s != NULL)
4381 return s;
4382 }
4383
4384 return NULL;
4385}
4386
39298a5d
TT
4387struct compunit_symtab *
4388dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4389 (struct objfile *objfile,
4390 struct bound_minimal_symbol msymbol,
4391 CORE_ADDR pc,
4392 struct obj_section *section,
4393 int warn_if_readin)
927aa2e7
JK
4394{
4395 struct dwarf2_per_cu_data *data;
4396 struct compunit_symtab *result;
4397
efd7398e
TT
4398 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4399 if (per_objfile->per_bfd->index_addrmap == nullptr)
927aa2e7
JK
4400 return NULL;
4401
b3b3bada 4402 CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e
TT
4403 data = ((struct dwarf2_per_cu_data *)
4404 addrmap_find (per_objfile->per_bfd->index_addrmap,
4405 pc - baseaddr));
927aa2e7
JK
4406 if (!data)
4407 return NULL;
4408
af758d11 4409 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4410 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4411 paddress (objfile->arch (), pc));
927aa2e7 4412
97a1449a
SM
4413 result = recursively_find_pc_sect_compunit_symtab
4414 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4415
927aa2e7
JK
4416 gdb_assert (result != NULL);
4417 return result;
4418}
4419
39298a5d 4420void
f4655dee
TT
4421dwarf2_base_index_functions::map_symbol_filenames
4422 (struct objfile *objfile,
4423 gdb::function_view<symbol_filename_ftype> fun,
4424 bool need_fullname)
927aa2e7 4425{
976ca316 4426 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4427
9fc29955
AB
4428 /* Use caches to ensure we only call FUN once for each filename. */
4429 filename_seen_cache filenames_cache;
4430 std::unordered_set<quick_file_names *> qfn_cache;
927aa2e7 4431
9fc29955
AB
4432 /* The rule is CUs specify all the files, including those used by any TU,
4433 so there's no need to scan TUs here. We can ignore file names coming
4434 from already-expanded CUs. It is possible that an expanded CU might
4435 reuse the file names data from a currently unexpanded CU, in this
4436 case we don't want to report the files from the unexpanded CU. */
927aa2e7 4437
0d305d5c 4438 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
9fc29955 4439 {
0d305d5c 4440 if (per_objfile->symtab_set_p (per_cu.get ()))
927aa2e7 4441 {
9fc29955
AB
4442 if (per_cu->v.quick->file_names != nullptr)
4443 qfn_cache.insert (per_cu->v.quick->file_names);
927aa2e7 4444 }
9fc29955 4445 }
927aa2e7 4446
0d305d5c 4447 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
9fc29955
AB
4448 {
4449 /* We only need to look at symtabs not already expanded. */
0d305d5c 4450 if (per_objfile->symtab_set_p (per_cu.get ()))
9fc29955 4451 continue;
927aa2e7 4452
0d305d5c
TT
4453 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4454 per_objfile);
9fc29955
AB
4455 if (file_data == nullptr
4456 || qfn_cache.find (file_data) != qfn_cache.end ())
4457 continue;
927aa2e7 4458
9fc29955
AB
4459 for (int j = 0; j < file_data->num_file_names; ++j)
4460 {
4461 const char *filename = file_data->file_names[j];
4462 filenames_cache.seen (filename);
927aa2e7
JK
4463 }
4464 }
4465
9fc29955 4466 filenames_cache.traverse ([&] (const char *filename)
927aa2e7
JK
4467 {
4468 gdb::unique_xmalloc_ptr<char> this_real_name;
4469
4470 if (need_fullname)
4471 this_real_name = gdb_realpath (filename);
f4655dee 4472 fun (filename, this_real_name.get ());
927aa2e7
JK
4473 });
4474}
4475
39298a5d
TT
4476bool
4477dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
927aa2e7 4478{
fae2120b 4479 return true;
927aa2e7
JK
4480}
4481
927aa2e7
JK
4482/* DWARF-5 debug_names reader. */
4483
4484/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4485static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4486
4487/* A helper function that reads the .debug_names section in SECTION
4488 and fills in MAP. FILENAME is the name of the file containing the
4489 section; it is used for error reporting.
4490
4491 Returns true if all went well, false otherwise. */
4492
4493static bool
4494read_debug_names_from_section (struct objfile *objfile,
4495 const char *filename,
4496 struct dwarf2_section_info *section,
4497 mapped_debug_names &map)
4498{
96b79293 4499 if (section->empty ())
927aa2e7
JK
4500 return false;
4501
4502 /* Older elfutils strip versions could keep the section in the main
4503 executable while splitting it for the separate debug info file. */
96b79293 4504 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4505 return false;
4506
96b79293 4507 section->read (objfile);
927aa2e7 4508
08feed99 4509 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4510
4511 const gdb_byte *addr = section->buffer;
4512
96b79293 4513 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4514
4515 unsigned int bytes_read;
4516 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4517 addr += bytes_read;
4518
4519 map.dwarf5_is_dwarf64 = bytes_read != 4;
4520 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4521 if (bytes_read + length != section->size)
4522 {
4523 /* There may be multiple per-CU indices. */
4524 warning (_("Section .debug_names in %s length %s does not match "
4525 "section length %s, ignoring .debug_names."),
4526 filename, plongest (bytes_read + length),
4527 pulongest (section->size));
4528 return false;
4529 }
4530
4531 /* The version number. */
4532 uint16_t version = read_2_bytes (abfd, addr);
4533 addr += 2;
4534 if (version != 5)
4535 {
4536 warning (_("Section .debug_names in %s has unsupported version %d, "
4537 "ignoring .debug_names."),
4538 filename, version);
4539 return false;
4540 }
4541
4542 /* Padding. */
4543 uint16_t padding = read_2_bytes (abfd, addr);
4544 addr += 2;
4545 if (padding != 0)
4546 {
4547 warning (_("Section .debug_names in %s has unsupported padding %d, "
4548 "ignoring .debug_names."),
4549 filename, padding);
4550 return false;
4551 }
4552
4553 /* comp_unit_count - The number of CUs in the CU list. */
4554 map.cu_count = read_4_bytes (abfd, addr);
4555 addr += 4;
4556
4557 /* local_type_unit_count - The number of TUs in the local TU
4558 list. */
4559 map.tu_count = read_4_bytes (abfd, addr);
4560 addr += 4;
4561
4562 /* foreign_type_unit_count - The number of TUs in the foreign TU
4563 list. */
4564 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4565 addr += 4;
4566 if (foreign_tu_count != 0)
4567 {
4568 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4569 "ignoring .debug_names."),
4570 filename, static_cast<unsigned long> (foreign_tu_count));
4571 return false;
4572 }
4573
4574 /* bucket_count - The number of hash buckets in the hash lookup
4575 table. */
4576 map.bucket_count = read_4_bytes (abfd, addr);
4577 addr += 4;
4578
4579 /* name_count - The number of unique names in the index. */
4580 map.name_count = read_4_bytes (abfd, addr);
4581 addr += 4;
4582
4583 /* abbrev_table_size - The size in bytes of the abbreviations
4584 table. */
4585 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4586 addr += 4;
4587
4588 /* augmentation_string_size - The size in bytes of the augmentation
4589 string. This value is rounded up to a multiple of 4. */
4590 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4591 addr += 4;
4592 map.augmentation_is_gdb = ((augmentation_string_size
4593 == sizeof (dwarf5_augmentation))
4594 && memcmp (addr, dwarf5_augmentation,
4595 sizeof (dwarf5_augmentation)) == 0);
4596 augmentation_string_size += (-augmentation_string_size) & 3;
4597 addr += augmentation_string_size;
4598
4599 /* List of CUs */
4600 map.cu_table_reordered = addr;
4601 addr += map.cu_count * map.offset_size;
4602
4603 /* List of Local TUs */
4604 map.tu_table_reordered = addr;
4605 addr += map.tu_count * map.offset_size;
4606
4607 /* Hash Lookup Table */
4608 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4609 addr += map.bucket_count * 4;
4610 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4611 addr += map.name_count * 4;
4612
4613 /* Name Table */
4614 map.name_table_string_offs_reordered = addr;
4615 addr += map.name_count * map.offset_size;
4616 map.name_table_entry_offs_reordered = addr;
4617 addr += map.name_count * map.offset_size;
4618
4619 const gdb_byte *abbrev_table_start = addr;
4620 for (;;)
4621 {
927aa2e7
JK
4622 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4623 addr += bytes_read;
4624 if (index_num == 0)
4625 break;
4626
4627 const auto insertpair
4628 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4629 if (!insertpair.second)
4630 {
4631 warning (_("Section .debug_names in %s has duplicate index %s, "
4632 "ignoring .debug_names."),
4633 filename, pulongest (index_num));
4634 return false;
4635 }
4636 mapped_debug_names::index_val &indexval = insertpair.first->second;
4637 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4638 addr += bytes_read;
4639
4640 for (;;)
4641 {
4642 mapped_debug_names::index_val::attr attr;
4643 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4644 addr += bytes_read;
4645 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4646 addr += bytes_read;
4647 if (attr.form == DW_FORM_implicit_const)
4648 {
4649 attr.implicit_const = read_signed_leb128 (abfd, addr,
4650 &bytes_read);
4651 addr += bytes_read;
4652 }
4653 if (attr.dw_idx == 0 && attr.form == 0)
4654 break;
4655 indexval.attr_vec.push_back (std::move (attr));
4656 }
4657 }
4658 if (addr != abbrev_table_start + abbrev_table_size)
4659 {
4660 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4661 "of size %s vs. written as %u, ignoring .debug_names."),
4662 filename, plongest (addr - abbrev_table_start),
4663 abbrev_table_size);
927aa2e7
JK
4664 return false;
4665 }
4666 map.entry_pool = addr;
4667
4668 return true;
4669}
4670
4671/* A helper for create_cus_from_debug_names that handles the MAP's CU
4672 list. */
4673
4674static void
168c9250 4675create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4676 const mapped_debug_names &map,
4677 dwarf2_section_info &section,
b76e467d 4678 bool is_dwz)
927aa2e7 4679{
3ee6bb11
TV
4680 if (!map.augmentation_is_gdb)
4681 {
fc9a13fb
TV
4682 for (uint32_t i = 0; i < map.cu_count; ++i)
4683 {
4684 sect_offset sect_off
4685 = (sect_offset) (extract_unsigned_integer
4686 (map.cu_table_reordered + i * map.offset_size,
4687 map.offset_size,
4688 map.dwarf5_byte_order));
4689 /* We don't know the length of the CU, because the CU list in a
4690 .debug_names index can be incomplete, so we can't use the start
4691 of the next CU as end of this CU. We create the CUs here with
4692 length 0, and in cutu_reader::cutu_reader we'll fill in the
4693 actual length. */
473ab964 4694 dwarf2_per_cu_data_up per_cu
fc9a13fb
TV
4695 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4696 sect_off, 0);
0d305d5c 4697 per_bfd->all_comp_units.push_back (std::move (per_cu));
fc9a13fb 4698 }
d3b54e63 4699 return;
3ee6bb11
TV
4700 }
4701
927aa2e7
JK
4702 sect_offset sect_off_prev;
4703 for (uint32_t i = 0; i <= map.cu_count; ++i)
4704 {
4705 sect_offset sect_off_next;
4706 if (i < map.cu_count)
4707 {
4708 sect_off_next
4709 = (sect_offset) (extract_unsigned_integer
4710 (map.cu_table_reordered + i * map.offset_size,
4711 map.offset_size,
4712 map.dwarf5_byte_order));
4713 }
4714 else
4715 sect_off_next = (sect_offset) section.size;
4716 if (i >= 1)
4717 {
4718 const ULONGEST length = sect_off_next - sect_off_prev;
473ab964 4719 dwarf2_per_cu_data_up per_cu
168c9250 4720 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 4721 sect_off_prev, length);
0d305d5c 4722 per_bfd->all_comp_units.push_back (std::move (per_cu));
927aa2e7
JK
4723 }
4724 sect_off_prev = sect_off_next;
4725 }
4726}
4727
4728/* Read the CU list from the mapped index, and use it to create all
ed2dc618 4729 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
4730
4731static void
168c9250 4732create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4733 const mapped_debug_names &map,
4734 const mapped_debug_names &dwz_map)
4735{
168c9250
SM
4736 gdb_assert (per_bfd->all_comp_units.empty ());
4737 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 4738
168c9250 4739 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 4740 false /* is_dwz */);
927aa2e7
JK
4741
4742 if (dwz_map.cu_count == 0)
4743 return;
4744
168c9250
SM
4745 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4746 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 4747 true /* is_dwz */);
927aa2e7
JK
4748}
4749
4750/* Read .debug_names. If everything went ok, initialize the "quick"
4751 elements of all the CUs and return true. Otherwise, return false. */
4752
4753static bool
976ca316 4754dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 4755{
fcf23d5b
SM
4756 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4757 mapped_debug_names dwz_map;
976ca316 4758 struct objfile *objfile = per_objfile->objfile;
f8c41851 4759 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
4760
4761 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
bab287cd 4762 &per_bfd->debug_names, *map))
927aa2e7
JK
4763 return false;
4764
4765 /* Don't use the index if it's empty. */
22ca247e 4766 if (map->name_count == 0)
927aa2e7
JK
4767 return false;
4768
4769 /* If there is a .dwz file, read it so we can get its CU list as
4770 well. */
f8c41851 4771 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
4772 if (dwz != NULL)
4773 {
4774 if (!read_debug_names_from_section (objfile,
00f93c44 4775 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
4776 &dwz->debug_names, dwz_map))
4777 {
4778 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 4779 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
4780 return false;
4781 }
4782 }
4783
f8c41851 4784 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 4785
22ca247e 4786 if (map->tu_count != 0)
927aa2e7
JK
4787 {
4788 /* We can only handle a single .debug_types when we have an
4789 index. */
f8c41851 4790 if (per_bfd->types.size () != 1)
927aa2e7
JK
4791 return false;
4792
f8c41851 4793 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
4794
4795 create_signatured_type_table_from_debug_names
f8c41851 4796 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
4797 }
4798
f8c41851 4799 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 4800
f8c41851
SM
4801 per_bfd->debug_names_table = std::move (map);
4802 per_bfd->using_index = 1;
4803 per_bfd->quick_file_names_table =
bab287cd 4804 create_quick_file_names_table (per_bfd->all_comp_units.size ());
927aa2e7
JK
4805
4806 return true;
4807}
4808
927aa2e7
JK
4809/* Type used to manage iterating over all CUs looking for a symbol for
4810 .debug_names. */
4811
4812class dw2_debug_names_iterator
4813{
4814public:
927aa2e7 4815 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4816 block_search_flags block_index,
2b79f376 4817 domain_enum domain,
fcf23d5b 4818 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 4819 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
4820 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4821 m_per_objfile (per_objfile)
927aa2e7
JK
4822 {}
4823
4824 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4825 search_domain search, uint32_t namei,
3bfa51a7
TT
4826 dwarf2_per_objfile *per_objfile,
4827 domain_enum domain = UNDEF_DOMAIN)
927aa2e7 4828 : m_map (map),
3bfa51a7 4829 m_domain (domain),
927aa2e7 4830 m_search (search),
fcf23d5b
SM
4831 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4832 m_per_objfile (per_objfile)
927aa2e7
JK
4833 {}
4834
3b00ef10 4835 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4836 block_search_flags block_index, domain_enum domain,
fcf23d5b 4837 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 4838 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
4839 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4840 m_per_objfile (per_objfile)
3b00ef10
TT
4841 {}
4842
927aa2e7
JK
4843 /* Return the next matching CU or NULL if there are no more. */
4844 dwarf2_per_cu_data *next ();
4845
4846private:
4847 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
4848 const char *name,
4849 dwarf2_per_objfile *per_objfile);
927aa2e7 4850 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
4851 uint32_t namei,
4852 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
4853
4854 /* The internalized form of .debug_names. */
4855 const mapped_debug_names &m_map;
4856
03a8ea51
TT
4857 /* Restrict the search to these blocks. */
4858 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4859 | SEARCH_STATIC_BLOCK);
927aa2e7
JK
4860
4861 /* The kind of symbol we're looking for. */
4862 const domain_enum m_domain = UNDEF_DOMAIN;
4863 const search_domain m_search = ALL_DOMAIN;
4864
4865 /* The list of CUs from the index entry of the symbol, or NULL if
4866 not found. */
4867 const gdb_byte *m_addr;
fcf23d5b
SM
4868
4869 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
4870};
4871
4872const char *
fcf23d5b 4873mapped_debug_names::namei_to_name
976ca316 4874 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
4875{
4876 const ULONGEST namei_string_offs
4877 = extract_unsigned_integer ((name_table_string_offs_reordered
4878 + namei * offset_size),
4879 offset_size,
4880 dwarf5_byte_order);
976ca316 4881 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
4882}
4883
4884/* Find a slot in .debug_names for the object named NAME. If NAME is
4885 found, return pointer to its pool data. If NAME cannot be found,
4886 return NULL. */
4887
4888const gdb_byte *
4889dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
4890 (const mapped_debug_names &map, const char *name,
4891 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
4892{
4893 int (*cmp) (const char *, const char *);
4894
54ee4252 4895 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
4896 if (current_language->la_language == language_cplus
4897 || current_language->la_language == language_fortran
4898 || current_language->la_language == language_d)
4899 {
4900 /* NAME is already canonical. Drop any qualifiers as
4901 .debug_names does not contain any. */
4902
4903 if (strchr (name, '(') != NULL)
4904 {
54ee4252 4905 without_params = cp_remove_params (name);
927aa2e7 4906 if (without_params != NULL)
54ee4252 4907 name = without_params.get ();
927aa2e7
JK
4908 }
4909 }
4910
4911 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4912
4913 const uint32_t full_hash = dwarf5_djb_hash (name);
4914 uint32_t namei
4915 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4916 (map.bucket_table_reordered
4917 + (full_hash % map.bucket_count)), 4,
4918 map.dwarf5_byte_order);
4919 if (namei == 0)
4920 return NULL;
4921 --namei;
4922 if (namei >= map.name_count)
4923 {
b98664d3 4924 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
4925 "[in module %s]"),
4926 namei, map.name_count,
fcf23d5b 4927 objfile_name (per_objfile->objfile));
927aa2e7
JK
4928 return NULL;
4929 }
4930
4931 for (;;)
4932 {
4933 const uint32_t namei_full_hash
4934 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4935 (map.hash_table_reordered + namei), 4,
4936 map.dwarf5_byte_order);
4937 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4938 return NULL;
4939
4940 if (full_hash == namei_full_hash)
4941 {
fcf23d5b 4942 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
4943
4944#if 0 /* An expensive sanity check. */
4945 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4946 {
b98664d3 4947 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
4948 "[in module %s]"),
4949 namei, objfile_name (dwarf2_per_objfile->objfile));
4950 return NULL;
4951 }
4952#endif
4953
4954 if (cmp (namei_string, name) == 0)
4955 {
4956 const ULONGEST namei_entry_offs
4957 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4958 + namei * map.offset_size),
4959 map.offset_size, map.dwarf5_byte_order);
4960 return map.entry_pool + namei_entry_offs;
4961 }
4962 }
4963
4964 ++namei;
4965 if (namei >= map.name_count)
4966 return NULL;
4967 }
4968}
4969
4970const gdb_byte *
4971dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 4972 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
4973{
4974 if (namei >= map.name_count)
4975 {
b98664d3 4976 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
4977 "[in module %s]"),
4978 namei, map.name_count,
fcf23d5b 4979 objfile_name (per_objfile->objfile));
927aa2e7
JK
4980 return NULL;
4981 }
4982
4983 const ULONGEST namei_entry_offs
4984 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4985 + namei * map.offset_size),
4986 map.offset_size, map.dwarf5_byte_order);
4987 return map.entry_pool + namei_entry_offs;
4988}
4989
4990/* See dw2_debug_names_iterator. */
4991
4992dwarf2_per_cu_data *
4993dw2_debug_names_iterator::next ()
4994{
4995 if (m_addr == NULL)
4996 return NULL;
4997
fcf23d5b
SM
4998 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4999 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5000 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5001
5002 again:
5003
5004 unsigned int bytes_read;
5005 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5006 m_addr += bytes_read;
5007 if (abbrev == 0)
5008 return NULL;
5009
5010 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5011 if (indexval_it == m_map.abbrev_map.cend ())
5012 {
b98664d3 5013 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5014 "[in module %s]"),
ed2dc618 5015 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5016 return NULL;
5017 }
5018 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5019 enum class symbol_linkage {
5020 unknown,
5021 static_,
5022 extern_,
23c13d42 5023 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5024 dwarf2_per_cu_data *per_cu = NULL;
5025 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5026 {
5027 ULONGEST ull;
5028 switch (attr.form)
5029 {
5030 case DW_FORM_implicit_const:
5031 ull = attr.implicit_const;
5032 break;
5033 case DW_FORM_flag_present:
5034 ull = 1;
5035 break;
5036 case DW_FORM_udata:
5037 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5038 m_addr += bytes_read;
5039 break;
6dc55ce9 5040 case DW_FORM_ref4:
5041 ull = read_4_bytes (abfd, m_addr);
5042 m_addr += 4;
5043 break;
5044 case DW_FORM_ref8:
5045 ull = read_8_bytes (abfd, m_addr);
5046 m_addr += 8;
5047 break;
5048 case DW_FORM_ref_sig8:
5049 ull = read_8_bytes (abfd, m_addr);
5050 m_addr += 8;
5051 break;
927aa2e7 5052 default:
b98664d3 5053 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5054 dwarf_form_name (attr.form),
ed2dc618 5055 objfile_name (objfile));
927aa2e7
JK
5056 return NULL;
5057 }
5058 switch (attr.dw_idx)
5059 {
5060 case DW_IDX_compile_unit:
5061 /* Don't crash on bad data. */
bab287cd 5062 if (ull >= per_bfd->all_comp_units.size ())
927aa2e7 5063 {
b98664d3 5064 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5065 " [in module %s]"),
5066 pulongest (ull),
fcf23d5b 5067 objfile_name (objfile));
927aa2e7
JK
5068 continue;
5069 }
91eea9cc 5070 per_cu = per_bfd->get_cu (ull);
927aa2e7 5071 break;
8af5c486
JK
5072 case DW_IDX_type_unit:
5073 /* Don't crash on bad data. */
91eea9cc 5074 if (ull >= per_bfd->tu_stats.nr_tus)
8af5c486 5075 {
b98664d3 5076 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5077 " [in module %s]"),
5078 pulongest (ull),
fcf23d5b 5079 objfile_name (objfile));
8af5c486
JK
5080 continue;
5081 }
91eea9cc 5082 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
8af5c486 5083 break;
6dc55ce9 5084 case DW_IDX_die_offset:
5085 /* In a per-CU index (as opposed to a per-module index), index
5086 entries without CU attribute implicitly refer to the single CU. */
5087 if (per_cu == NULL)
fcf23d5b 5088 per_cu = per_bfd->get_cu (0);
6dc55ce9 5089 break;
927aa2e7
JK
5090 case DW_IDX_GNU_internal:
5091 if (!m_map.augmentation_is_gdb)
5092 break;
23c13d42 5093 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5094 break;
5095 case DW_IDX_GNU_external:
5096 if (!m_map.augmentation_is_gdb)
5097 break;
23c13d42 5098 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5099 break;
5100 }
5101 }
5102
5103 /* Skip if already read in. */
fcf23d5b 5104 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5105 goto again;
5106
5107 /* Check static vs global. */
03a8ea51 5108 if (symbol_linkage_ != symbol_linkage::unknown)
927aa2e7 5109 {
03a8ea51
TT
5110 if (symbol_linkage_ == symbol_linkage::static_)
5111 {
5112 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5113 goto again;
5114 }
5115 else
5116 {
5117 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5118 goto again;
5119 }
927aa2e7
JK
5120 }
5121
5122 /* Match dw2_symtab_iter_next, symbol_kind
5123 and debug_names::psymbol_tag. */
5124 switch (m_domain)
5125 {
5126 case VAR_DOMAIN:
5127 switch (indexval.dwarf_tag)
5128 {
5129 case DW_TAG_variable:
5130 case DW_TAG_subprogram:
5131 /* Some types are also in VAR_DOMAIN. */
5132 case DW_TAG_typedef:
5133 case DW_TAG_structure_type:
5134 break;
5135 default:
5136 goto again;
5137 }
5138 break;
5139 case STRUCT_DOMAIN:
5140 switch (indexval.dwarf_tag)
5141 {
5142 case DW_TAG_typedef:
5143 case DW_TAG_structure_type:
5144 break;
5145 default:
5146 goto again;
5147 }
5148 break;
5149 case LABEL_DOMAIN:
5150 switch (indexval.dwarf_tag)
5151 {
5152 case 0:
5153 case DW_TAG_variable:
5154 break;
5155 default:
5156 goto again;
5157 }
5158 break;
59c35742
AB
5159 case MODULE_DOMAIN:
5160 switch (indexval.dwarf_tag)
5161 {
5162 case DW_TAG_module:
5163 break;
5164 default:
5165 goto again;
5166 }
5167 break;
927aa2e7
JK
5168 default:
5169 break;
5170 }
5171
5172 /* Match dw2_expand_symtabs_matching, symbol_kind and
5173 debug_names::psymbol_tag. */
5174 switch (m_search)
4b514bc8 5175 {
927aa2e7
JK
5176 case VARIABLES_DOMAIN:
5177 switch (indexval.dwarf_tag)
4b514bc8 5178 {
927aa2e7
JK
5179 case DW_TAG_variable:
5180 break;
5181 default:
5182 goto again;
4b514bc8 5183 }
927aa2e7
JK
5184 break;
5185 case FUNCTIONS_DOMAIN:
5186 switch (indexval.dwarf_tag)
4b514bc8 5187 {
927aa2e7
JK
5188 case DW_TAG_subprogram:
5189 break;
5190 default:
5191 goto again;
4b514bc8 5192 }
927aa2e7
JK
5193 break;
5194 case TYPES_DOMAIN:
5195 switch (indexval.dwarf_tag)
5196 {
5197 case DW_TAG_typedef:
5198 case DW_TAG_structure_type:
5199 break;
5200 default:
5201 goto again;
5202 }
5203 break;
59c35742
AB
5204 case MODULES_DOMAIN:
5205 switch (indexval.dwarf_tag)
5206 {
5207 case DW_TAG_module:
5208 break;
5209 default:
5210 goto again;
5211 }
927aa2e7
JK
5212 default:
5213 break;
4b514bc8 5214 }
927aa2e7
JK
5215
5216 return per_cu;
4b514bc8 5217}
61920122 5218
927aa2e7
JK
5219/* This dumps minimal information about .debug_names. It is called
5220 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5221 uses this to verify that .debug_names has been loaded. */
9291a0cd 5222
39298a5d
TT
5223void
5224dwarf2_debug_names_index::dump (struct objfile *objfile)
927aa2e7 5225{
976ca316 5226 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5227
976ca316 5228 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5229 printf_filtered (".debug_names:");
976ca316 5230 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5231 printf_filtered (" exists\n");
5232 else
5233 printf_filtered (" faked for \"readnow\"\n");
5234 printf_filtered ("\n");
9291a0cd
TT
5235}
5236
39298a5d 5237void
0b7b2c2a 5238dwarf2_debug_names_index::expand_matching_symbols
3b00ef10
TT
5239 (struct objfile *objfile,
5240 const lookup_name_info &name, domain_enum domain,
5241 int global,
3b00ef10
TT
5242 symbol_compare_ftype *ordered_compare)
5243{
976ca316 5244 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5245
5246 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5247 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5248 return;
5249
976ca316 5250 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
03a8ea51
TT
5251 const block_search_flags block_flags
5252 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
3b00ef10
TT
5253
5254 const char *match_name = name.ada ().lookup_name ().c_str ();
5255 auto matcher = [&] (const char *symname)
5256 {
5257 if (ordered_compare == nullptr)
5258 return true;
5259 return ordered_compare (symname, match_name) == 0;
5260 };
5261
b64f703b 5262 dw2_expand_symtabs_matching_symbol (map, name, matcher,
3b00ef10
TT
5263 [&] (offset_type namei)
5264 {
5265 /* The name was matched, now expand corresponding CUs that were
5266 marked. */
03a8ea51 5267 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
976ca316 5268 per_objfile);
3b00ef10
TT
5269
5270 struct dwarf2_per_cu_data *per_cu;
5271 while ((per_cu = iter.next ()) != NULL)
976ca316 5272 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5273 nullptr);
3b00ef10 5274 return true;
976ca316 5275 }, per_objfile);
3b00ef10
TT
5276}
5277
df35e626 5278bool
39298a5d 5279dwarf2_debug_names_index::expand_symtabs_matching
927aa2e7
JK
5280 (struct objfile *objfile,
5281 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5282 const lookup_name_info *lookup_name,
927aa2e7
JK
5283 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5284 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 5285 block_search_flags search_flags,
3bfa51a7 5286 domain_enum domain,
927aa2e7
JK
5287 enum search_domain kind)
5288{
976ca316 5289 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5290
927aa2e7 5291 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5292 if (!per_objfile->per_bfd->debug_names_table)
df35e626 5293 return true;
9291a0cd 5294
976ca316 5295 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5296
c1a66c06
TV
5297 if (symbol_matcher == NULL && lookup_name == NULL)
5298 {
0d305d5c 5299 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5300 {
5301 QUIT;
5302
0d305d5c 5303 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
df35e626
TT
5304 file_matcher,
5305 expansion_notify))
5306 return false;
c1a66c06 5307 }
df35e626 5308 return true;
c1a66c06
TV
5309 }
5310
976ca316 5311 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5312
df35e626
TT
5313 bool result
5314 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5315 symbol_matcher,
5316 [&] (offset_type namei)
927aa2e7 5317 {
927aa2e7
JK
5318 /* The name was matched, now expand corresponding CUs that were
5319 marked. */
3bfa51a7 5320 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
bbf2f4df 5321
927aa2e7
JK
5322 struct dwarf2_per_cu_data *per_cu;
5323 while ((per_cu = iter.next ()) != NULL)
df35e626
TT
5324 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5325 file_matcher,
5326 expansion_notify))
5327 return false;
3b00ef10 5328 return true;
976ca316 5329 }, per_objfile);
df35e626
TT
5330
5331 return result;
9291a0cd
TT
5332}
5333
4485a1c1 5334/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5335 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5336
5337template <typename T>
5338static gdb::array_view<const gdb_byte>
5339get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5340{
5341 dwarf2_section_info *section = &section_owner->gdb_index;
5342
96b79293 5343 if (section->empty ())
4485a1c1
SM
5344 return {};
5345
5346 /* Older elfutils strip versions could keep the section in the main
5347 executable while splitting it for the separate debug info file. */
96b79293 5348 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5349 return {};
5350
96b79293 5351 section->read (obj);
4485a1c1 5352
8bebfcda
PA
5353 /* dwarf2_section_info::size is a bfd_size_type, while
5354 gdb::array_view works with size_t. On 32-bit hosts, with
5355 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5356 is 32-bit. So we need an explicit narrowing conversion here.
5357 This is fine, because it's impossible to allocate or mmap an
5358 array/buffer larger than what size_t can represent. */
5359 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5360}
5361
87d6a7aa
SM
5362/* Lookup the index cache for the contents of the index associated to
5363 DWARF2_OBJ. */
5364
5365static gdb::array_view<const gdb_byte>
5989a64e 5366get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5367{
5368 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5369 if (build_id == nullptr)
5370 return {};
5371
5372 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5373 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5374}
5375
5376/* Same as the above, but for DWZ. */
5377
5378static gdb::array_view<const gdb_byte>
5379get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5380{
5381 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5382 if (build_id == nullptr)
5383 return {};
5384
5385 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5386}
5387
edc02ceb 5388/* See dwarf2/public.h. */
9291a0cd 5389
edc02ceb
TT
5390void
5391dwarf2_initialize_objfile (struct objfile *objfile)
9291a0cd 5392{
976ca316
SM
5393 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5394 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5395
850ed749
SM
5396 dwarf_read_debug_printf ("called");
5397
9291a0cd
TT
5398 /* If we're about to read full symbols, don't bother with the
5399 indices. In this case we also don't care if some other debug
5400 format is making psymtabs, because they are all about to be
5401 expanded anyway. */
5402 if ((objfile->flags & OBJF_READNOW))
5403 {
850ed749
SM
5404 dwarf_read_debug_printf ("readnow requested");
5405
17ee85fc
TT
5406 /* When using READNOW, the using_index flag (set below) indicates that
5407 PER_BFD was already initialized, when we loaded some other objfile. */
5408 if (per_bfd->using_index)
5409 {
850ed749 5410 dwarf_read_debug_printf ("using_index already set");
976ca316 5411 per_objfile->resize_symtabs ();
edc02ceb
TT
5412 objfile->qf.push_front (make_dwarf_gdb_index ());
5413 return;
17ee85fc
TT
5414 }
5415
5416 per_bfd->using_index = 1;
976ca316 5417 create_all_comp_units (per_objfile);
17ee85fc
TT
5418 per_bfd->quick_file_names_table
5419 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5420 per_objfile->resize_symtabs ();
9291a0cd 5421
91eea9cc 5422 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
9291a0cd 5423 {
91eea9cc 5424 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
9291a0cd 5425
17ee85fc 5426 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5427 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5428 }
5429
edc02ceb
TT
5430 /* Arrange for gdb to see the "quick" functions. However, these
5431 functions will be no-ops because we will have expanded all
5432 symtabs. */
5433 objfile->qf.push_front (make_dwarf_gdb_index ());
5434 return;
9291a0cd
TT
5435 }
5436
17ee85fc
TT
5437 /* Was a debug names index already read when we processed an objfile sharing
5438 PER_BFD? */
5439 if (per_bfd->debug_names_table != nullptr)
5440 {
850ed749 5441 dwarf_read_debug_printf ("re-using shared debug names table");
976ca316 5442 per_objfile->resize_symtabs ();
edc02ceb
TT
5443 objfile->qf.push_front (make_dwarf_debug_names ());
5444 return;
17ee85fc
TT
5445 }
5446
5447 /* Was a GDB index already read when we processed an objfile sharing
5448 PER_BFD? */
5449 if (per_bfd->index_table != nullptr)
5450 {
850ed749 5451 dwarf_read_debug_printf ("re-using shared index table");
976ca316 5452 per_objfile->resize_symtabs ();
edc02ceb
TT
5453 objfile->qf.push_front (make_dwarf_gdb_index ());
5454 return;
17ee85fc
TT
5455 }
5456
efb763a5
SM
5457 /* There might already be partial symtabs built for this BFD. This happens
5458 when loading the same binary twice with the index-cache enabled. If so,
5459 don't try to read an index. The objfile / per_objfile initialization will
5460 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5461 code path. */
5462 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
5463 {
5464 dwarf_read_debug_printf ("re-using shared partial symtabs");
edc02ceb
TT
5465 objfile->qf.push_front (make_lazy_dwarf_reader ());
5466 return;
850ed749 5467 }
efb763a5 5468
976ca316 5469 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 5470 {
850ed749 5471 dwarf_read_debug_printf ("found debug names");
976ca316 5472 per_objfile->resize_symtabs ();
edc02ceb
TT
5473 objfile->qf.push_front (make_dwarf_debug_names ());
5474 return;
3c0aa29a 5475 }
927aa2e7 5476
976ca316 5477 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 5478 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5479 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 5480 {
850ed749 5481 dwarf_read_debug_printf ("found gdb index from file");
976ca316 5482 per_objfile->resize_symtabs ();
edc02ceb
TT
5483 objfile->qf.push_front (make_dwarf_gdb_index ());
5484 return;
3c0aa29a 5485 }
9291a0cd 5486
87d6a7aa 5487 /* ... otherwise, try to find the index in the index cache. */
976ca316 5488 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
5489 get_gdb_index_contents_from_cache,
5490 get_gdb_index_contents_from_cache_dwz))
5491 {
850ed749 5492 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa 5493 global_index_cache.hit ();
976ca316 5494 per_objfile->resize_symtabs ();
edc02ceb
TT
5495 objfile->qf.push_front (make_dwarf_gdb_index ());
5496 return;
87d6a7aa
SM
5497 }
5498
5499 global_index_cache.miss ();
edc02ceb 5500 objfile->qf.push_front (make_lazy_dwarf_reader ());
9291a0cd
TT
5501}
5502
5503\f
5504
dce234bc
PP
5505/* Build a partial symbol table. */
5506
5507void
eb36a3eb 5508dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
dce234bc 5509{
976ca316
SM
5510 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5511 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
5512
5513 if (per_bfd->partial_symtabs != nullptr)
5514 {
5515 /* Partial symbols were already read, so now we can simply
5516 attach them. */
eb36a3eb
TT
5517 if (psf == nullptr)
5518 {
5519 psf = new psymbol_functions (per_bfd->partial_symtabs);
5520 objfile->qf.emplace_front (psf);
5521 }
5522 else
5523 psf->set_partial_symtabs (per_bfd->partial_symtabs);
976ca316 5524 per_objfile->resize_symtabs ();
17ee85fc
TT
5525 return;
5526 }
c9bf0622 5527
eb36a3eb
TT
5528 if (psf == nullptr)
5529 {
5530 psf = new psymbol_functions;
5531 objfile->qf.emplace_front (psf);
5532 }
5533 const std::shared_ptr<psymtab_storage> &partial_symtabs
5534 = psf->get_partial_symtabs ();
5535
51962708
TT
5536 /* Set the local reference to partial symtabs, so that we don't try
5537 to read them again if reading another objfile with the same BFD.
5538 If we can't in fact share, this won't make a difference anyway as
5539 the dwarf2_per_bfd object won't be shared. */
eb36a3eb 5540 per_bfd->partial_symtabs = partial_symtabs;
51962708 5541
a70b8144 5542 try
c9bf0622
TT
5543 {
5544 /* This isn't really ideal: all the data we allocate on the
5545 objfile's obstack is still uselessly kept around. However,
5546 freeing it seems unsafe. */
eb36a3eb 5547 psymtab_discarder psymtabs (partial_symtabs.get ());
976ca316 5548 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 5549 psymtabs.keep ();
87d6a7aa 5550
976ca316 5551 per_objfile->resize_symtabs ();
af758d11 5552
87d6a7aa 5553 /* (maybe) store an index in the cache. */
976ca316 5554 global_index_cache.store (per_objfile);
c9bf0622 5555 }
230d2906 5556 catch (const gdb_exception_error &except)
492d29ea
PA
5557 {
5558 exception_print (gdb_stderr, except);
5559 }
c906108c 5560}
c906108c 5561
3b80fe9b
DE
5562/* Find the base address of the compilation unit for range lists and
5563 location lists. It will normally be specified by DW_AT_low_pc.
5564 In DWARF-3 draft 4, the base address could be overridden by
5565 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5566 compilation units with discontinuous ranges. */
5567
5568static void
5569dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5570{
5571 struct attribute *attr;
5572
2b24b6e4 5573 cu->base_address.reset ();
3b80fe9b
DE
5574
5575 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5576 if (attr != nullptr)
95f982e5 5577 cu->base_address = attr->as_address ();
3b80fe9b
DE
5578 else
5579 {
5580 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5581 if (attr != nullptr)
95f982e5 5582 cu->base_address = attr->as_address ();
3b80fe9b
DE
5583 }
5584}
5585
36586728
TT
5586/* Helper function that returns the proper abbrev section for
5587 THIS_CU. */
5588
5589static struct dwarf2_section_info *
5590get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5591{
5592 struct dwarf2_section_info *abbrev;
c3699833 5593 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
5594
5595 if (this_cu->is_dwz)
a7308ce0 5596 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 5597 else
c3699833 5598 abbrev = &per_bfd->abbrev;
36586728
TT
5599
5600 return abbrev;
5601}
5602
f4dc4d17
DE
5603/* Fetch the abbreviation table offset from a comp or type unit header. */
5604
5605static sect_offset
976ca316 5606read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 5607 struct dwarf2_section_info *section,
9c541725 5608 sect_offset sect_off)
f4dc4d17 5609{
96b79293 5610 bfd *abfd = section->get_bfd_owner ();
d521ce57 5611 const gdb_byte *info_ptr;
ac298888 5612 unsigned int initial_length_size, offset_size;
43988095 5613 uint16_t version;
f4dc4d17 5614
976ca316 5615 section->read (per_objfile->objfile);
9c541725 5616 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5617 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5618 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5619 info_ptr += initial_length_size;
5620
5621 version = read_2_bytes (abfd, info_ptr);
5622 info_ptr += 2;
5623 if (version >= 5)
5624 {
5625 /* Skip unit type and address size. */
5626 info_ptr += 2;
5627 }
5628
24aa364d 5629 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5630}
5631
b83470bf
TT
5632/* A partial symtab that is used only for include files. */
5633struct dwarf2_include_psymtab : public partial_symtab
5634{
7b249e47
TT
5635 dwarf2_include_psymtab (const char *filename,
5636 psymtab_storage *partial_symtabs,
0072c873
SM
5637 objfile_per_bfd_storage *objfile_per_bfd)
5638 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
b83470bf
TT
5639 {
5640 }
5641
5642 void read_symtab (struct objfile *objfile) override
5643 {
194d088f
TV
5644 /* It's an include file, no symbols to read for it.
5645 Everything is in the includer symtab. */
5646
5647 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5648 expansion of the includer psymtab. We use the dependencies[0] field to
5649 model the includer. But if we go the regular route of calling
5650 expand_psymtab here, and having expand_psymtab call expand_dependencies
5651 to expand the includer, we'll only use expand_psymtab on the includer
5652 (making it a non-toplevel psymtab), while if we expand the includer via
5653 another path, we'll use read_symtab (making it a toplevel psymtab).
5654 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5655 psymtab, and trigger read_symtab on the includer here directly. */
5656 includer ()->read_symtab (objfile);
b83470bf
TT
5657 }
5658
5659 void expand_psymtab (struct objfile *objfile) override
5660 {
194d088f
TV
5661 /* This is not called by read_symtab, and should not be called by any
5662 expand_dependencies. */
5663 gdb_assert (false);
b83470bf
TT
5664 }
5665
5717c425 5666 bool readin_p (struct objfile *objfile) const override
b83470bf 5667 {
5717c425 5668 return includer ()->readin_p (objfile);
b83470bf
TT
5669 }
5670
5717c425 5671 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
5672 {
5673 return nullptr;
5674 }
5675
5676private:
194d088f
TV
5677 partial_symtab *includer () const
5678 {
5679 /* An include psymtab has exactly one dependency: the psymtab that
5680 includes it. */
5681 gdb_assert (this->number_of_dependencies == 1);
5682 return this->dependencies[0];
5683 }
b83470bf
TT
5684};
5685
aaa75496
JB
5686/* Allocate a new partial symtab for file named NAME and mark this new
5687 partial symtab as being an include of PST. */
5688
5689static void
84685904 5690dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
7b249e47
TT
5691 const char *name,
5692 dwarf2_psymtab *pst,
5693 psymtab_storage *partial_symtabs,
0072c873 5694 objfile_per_bfd_storage *objfile_per_bfd)
aaa75496 5695{
7b249e47 5696 dwarf2_include_psymtab *subpst
0072c873 5697 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
aaa75496 5698
fbd9ab74 5699 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 5700 subpst->dirname = pst->dirname;
fbd9ab74 5701
84685904 5702 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5703 subpst->dependencies[0] = pst;
5704 subpst->number_of_dependencies = 1;
aaa75496
JB
5705}
5706
5707/* Read the Line Number Program data and extract the list of files
5708 included by the source file represented by PST. Build an include
d85a05f0 5709 partial symtab for each of these included files. */
aaa75496
JB
5710
5711static void
5712dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5713 struct die_info *die,
891813be 5714 dwarf2_psymtab *pst)
aaa75496 5715{
fff8551c 5716 line_header_up lh;
d85a05f0 5717 struct attribute *attr;
aaa75496 5718
d85a05f0 5719 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
5720 if (attr != nullptr && attr->form_is_unsigned ())
5721 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
5722 if (lh == NULL)
5723 return; /* No linetable, so no includes. */
5724
79748972
TT
5725 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5726 that we pass in the raw text_low here; that is ok because we're
5727 only decoding the line table to make include partial symtabs, and
5728 so the addresses aren't really used. */
4ae976d1 5729 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5730 pst->raw_text_low (), 1);
aaa75496
JB
5731}
5732
348e048f 5733static hashval_t
52dc124a 5734hash_signatured_type (const void *item)
348e048f 5735{
9a3c8263
SM
5736 const struct signatured_type *sig_type
5737 = (const struct signatured_type *) item;
9a619af0 5738
348e048f 5739 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5740 return sig_type->signature;
348e048f
DE
5741}
5742
5743static int
52dc124a 5744eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5745{
9a3c8263
SM
5746 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5747 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5748
348e048f
DE
5749 return lhs->signature == rhs->signature;
5750}
5751
1fd400ff
TT
5752/* Allocate a hash table for signatured types. */
5753
b0b6a987 5754static htab_up
298e9637 5755allocate_signatured_type_table ()
1fd400ff 5756{
b0b6a987
TT
5757 return htab_up (htab_create_alloc (41,
5758 hash_signatured_type,
5759 eq_signatured_type,
5760 NULL, xcalloc, xfree));
1fd400ff
TT
5761}
5762
78d4d2c5 5763/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5764 and fill them into TYPES_HTAB. It will process only type units,
5765 therefore DW_UT_type. */
c88ee1f0 5766
78d4d2c5 5767static void
976ca316 5768create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 5769 struct dwo_file *dwo_file,
b0b6a987 5770 dwarf2_section_info *section, htab_up &types_htab,
43988095 5771 rcuh_kind section_kind)
348e048f 5772{
976ca316 5773 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 5774 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
5775 bfd *abfd;
5776 const gdb_byte *info_ptr, *end_ptr;
348e048f 5777
b8efb248 5778 abbrev_section = &dwo_file->sections.abbrev;
4bdcc0c1 5779
4800761a 5780 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
5781 section->get_name (),
5782 abbrev_section->get_file_name ());
09406207 5783
96b79293 5784 section->read (objfile);
78d4d2c5 5785 info_ptr = section->buffer;
348e048f 5786
78d4d2c5
JK
5787 if (info_ptr == NULL)
5788 return;
348e048f 5789
78d4d2c5
JK
5790 /* We can't set abfd until now because the section may be empty or
5791 not present, in which case the bfd is unknown. */
96b79293 5792 abfd = section->get_bfd_owner ();
348e048f 5793
c0ab21c2
TT
5794 /* We don't use cutu_reader here because we don't need to read
5795 any dies: the signature is in the header. */
3019eac3 5796
78d4d2c5
JK
5797 end_ptr = info_ptr + section->size;
5798 while (info_ptr < end_ptr)
5799 {
0d305d5c 5800 std::unique_ptr<signatured_type> sig_type;
78d4d2c5
JK
5801 struct dwo_unit *dwo_tu;
5802 void **slot;
5803 const gdb_byte *ptr = info_ptr;
5804 struct comp_unit_head header;
5805 unsigned int length;
8b70b953 5806
9c541725 5807 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 5808
a49dd8dd
JK
5809 /* Initialize it due to a false compiler warning. */
5810 header.signature = -1;
9c541725 5811 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 5812
78d4d2c5
JK
5813 /* We need to read the type's signature in order to build the hash
5814 table, but we don't need anything else just yet. */
348e048f 5815
976ca316 5816 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 5817 abbrev_section, ptr, section_kind);
348e048f 5818
4057dfde 5819 length = header.get_length ();
6caca83c 5820
78d4d2c5
JK
5821 /* Skip dummy type units. */
5822 if (ptr >= info_ptr + length
43988095 5823 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
5824 || (header.unit_type != DW_UT_type
5825 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
5826 {
5827 info_ptr += length;
5828 continue;
5829 }
dee91e82 5830
78d4d2c5 5831 if (types_htab == NULL)
b8efb248 5832 types_htab = allocate_dwo_unit_table ();
8b70b953 5833
b8efb248
TT
5834 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5835 dwo_tu->dwo_file = dwo_file;
5836 dwo_tu->signature = header.signature;
5837 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5838 dwo_tu->section = section;
5839 dwo_tu->sect_off = sect_off;
5840 dwo_tu->length = length;
78d4d2c5 5841
b8efb248 5842 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
78d4d2c5
JK
5843 gdb_assert (slot != NULL);
5844 if (*slot != NULL)
b8efb248
TT
5845 complaint (_("debug type entry at offset %s is duplicate to"
5846 " the entry at offset %s, signature %s"),
5847 sect_offset_str (sect_off),
5848 sect_offset_str (dwo_tu->sect_off),
5849 hex_string (header.signature));
5850 *slot = dwo_tu;
3019eac3 5851
6f738b01
SM
5852 dwarf_read_debug_printf_v (" offset %s, signature %s",
5853 sect_offset_str (sect_off),
5854 hex_string (header.signature));
3019eac3 5855
78d4d2c5
JK
5856 info_ptr += length;
5857 }
5858}
3019eac3 5859
78d4d2c5
JK
5860/* Create the hash table of all entries in the .debug_types
5861 (or .debug_types.dwo) section(s).
b8efb248 5862 DWO_FILE is a pointer to the DWO file object.
b3c8eb43 5863
78d4d2c5 5864 The result is a pointer to the hash table or NULL if there are no types.
348e048f 5865
78d4d2c5 5866 Note: This function processes DWO files only, not DWP files. */
348e048f 5867
78d4d2c5 5868static void
976ca316 5869create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 5870 struct dwo_file *dwo_file,
fd5866f6 5871 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 5872 htab_up &types_htab)
78d4d2c5 5873{
fd5866f6 5874 for (dwarf2_section_info &section : type_sections)
976ca316
SM
5875 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5876 rcuh_kind::TYPE);
3019eac3
DE
5877}
5878
5989a64e 5879/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
5880 If SLOT is non-NULL, it is the entry to use in the hash table.
5881 Otherwise we find one. */
5882
5883static struct signatured_type *
976ca316 5884add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 5885{
91eea9cc
TT
5886 if (per_objfile->per_bfd->all_comp_units.size ()
5887 == per_objfile->per_bfd->all_comp_units.capacity ())
976ca316 5888 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 5889
0d305d5c
TT
5890 std::unique_ptr<signatured_type> sig_type_holder
5891 = per_objfile->per_bfd->allocate_signatured_type ();
5892 signatured_type *sig_type = sig_type_holder.get ();
b2bdb8cf 5893
976ca316 5894 per_objfile->resize_symtabs ();
af758d11 5895
473ab964
TT
5896 per_objfile->per_bfd->all_comp_units.emplace_back
5897 (sig_type_holder.release ());
6aa5f3a6 5898 sig_type->signature = sig;
0d305d5c 5899 sig_type->is_debug_types = 1;
976ca316 5900 if (per_objfile->per_bfd->using_index)
6aa5f3a6 5901 {
0d305d5c 5902 sig_type->v.quick =
976ca316 5903 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
5904 struct dwarf2_per_cu_quick_data);
5905 }
5906
5907 if (slot == NULL)
5908 {
976ca316 5909 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
5910 sig_type, INSERT);
5911 }
5912 gdb_assert (*slot == NULL);
5913 *slot = sig_type;
5914 /* The rest of sig_type must be filled in by the caller. */
5915 return sig_type;
5916}
5917
a2ce51a0
DE
5918/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5919 Fill in SIG_ENTRY with DWO_ENTRY. */
5920
5921static void
976ca316 5922fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
5923 struct signatured_type *sig_entry,
5924 struct dwo_unit *dwo_entry)
5925{
976ca316 5926 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 5927
7ee85ab1 5928 /* Make sure we're not clobbering something we don't expect to. */
0d305d5c
TT
5929 gdb_assert (! sig_entry->queued);
5930 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
1859c670 5931 if (per_bfd->using_index)
6aa5f3a6 5932 {
0d305d5c
TT
5933 gdb_assert (sig_entry->v.quick != NULL);
5934 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6aa5f3a6
DE
5935 }
5936 else
0d305d5c 5937 gdb_assert (sig_entry->v.psymtab == NULL);
a2ce51a0 5938 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5939 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5940 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5941 gdb_assert (sig_entry->dwo_unit == NULL);
5942
0d305d5c
TT
5943 sig_entry->section = dwo_entry->section;
5944 sig_entry->sect_off = dwo_entry->sect_off;
5945 sig_entry->length = dwo_entry->length;
5946 sig_entry->reading_dwo_directly = 1;
5947 sig_entry->per_bfd = per_bfd;
a2ce51a0
DE
5948 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5949 sig_entry->dwo_unit = dwo_entry;
5950}
5951
5952/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5953 If we haven't read the TU yet, create the signatured_type data structure
5954 for a TU to be read in directly from a DWO file, bypassing the stub.
5955 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5956 using .gdb_index, then when reading a CU we want to stay in the DWO file
5957 containing that CU. Otherwise we could end up reading several other DWO
5958 files (due to comdat folding) to process the transitive closure of all the
5959 mentioned TUs, and that can be slow. The current DWO file will have every
5960 type signature that it needs.
a2ce51a0
DE
5961 We only do this for .gdb_index because in the psymtab case we already have
5962 to read all the DWOs to build the type unit groups. */
5963
5964static struct signatured_type *
5965lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5966{
976ca316 5967 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
5968 struct dwo_file *dwo_file;
5969 struct dwo_unit find_dwo_entry, *dwo_entry;
5970 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5971 void **slot;
a2ce51a0 5972
976ca316 5973 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 5974
6aa5f3a6
DE
5975 /* If TU skeletons have been removed then we may not have read in any
5976 TUs yet. */
976ca316
SM
5977 if (per_objfile->per_bfd->signatured_types == NULL)
5978 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
5979
5980 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5981 Use the global signatured_types array to do our own comdat-folding
5982 of types. If this is the first time we're reading this TU, and
5983 the TU has an entry in .gdb_index, replace the recorded data from
5984 .gdb_index with this TU. */
a2ce51a0 5985
a2ce51a0 5986 find_sig_entry.signature = sig;
976ca316 5987 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 5988 &find_sig_entry, INSERT);
9a3c8263 5989 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5990
5991 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5992 read. Don't reassign the global entry to point to this DWO if that's
5993 the case. Also note that if the TU is already being read, it may not
5994 have come from a DWO, the program may be a mix of Fission-compiled
5995 code and non-Fission-compiled code. */
5996
5997 /* Have we already tried to read this TU?
5998 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5999 needn't exist in the global table yet). */
0d305d5c 6000 if (sig_entry != NULL && sig_entry->tu_read)
a2ce51a0
DE
6001 return sig_entry;
6002
6aa5f3a6
DE
6003 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6004 dwo_unit of the TU itself. */
6005 dwo_file = cu->dwo_unit->dwo_file;
6006
a2ce51a0
DE
6007 /* Ok, this is the first time we're reading this TU. */
6008 if (dwo_file->tus == NULL)
6009 return NULL;
6010 find_dwo_entry.signature = sig;
b0b6a987
TT
6011 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6012 &find_dwo_entry);
a2ce51a0
DE
6013 if (dwo_entry == NULL)
6014 return NULL;
6015
6aa5f3a6
DE
6016 /* If the global table doesn't have an entry for this TU, add one. */
6017 if (sig_entry == NULL)
976ca316 6018 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6019
976ca316 6020 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
0d305d5c 6021 sig_entry->tu_read = 1;
a2ce51a0
DE
6022 return sig_entry;
6023}
6024
a2ce51a0
DE
6025/* Subroutine of lookup_signatured_type.
6026 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6027 then try the DWP file. If the TU stub (skeleton) has been removed then
6028 it won't be in .gdb_index. */
a2ce51a0
DE
6029
6030static struct signatured_type *
6031lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6032{
976ca316
SM
6033 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6034 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6035 struct dwo_unit *dwo_entry;
6036 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6037 void **slot;
a2ce51a0 6038
976ca316 6039 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6040 gdb_assert (dwp_file != NULL);
6041
6aa5f3a6
DE
6042 /* If TU skeletons have been removed then we may not have read in any
6043 TUs yet. */
976ca316
SM
6044 if (per_objfile->per_bfd->signatured_types == NULL)
6045 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6046
6aa5f3a6 6047 find_sig_entry.signature = sig;
976ca316 6048 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6049 &find_sig_entry, INSERT);
9a3c8263 6050 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6051
6052 /* Have we already tried to read this TU?
6053 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6054 needn't exist in the global table yet). */
6055 if (sig_entry != NULL)
6056 return sig_entry;
6057
a2ce51a0
DE
6058 if (dwp_file->tus == NULL)
6059 return NULL;
976ca316
SM
6060 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6061 1 /* is_debug_types */);
a2ce51a0
DE
6062 if (dwo_entry == NULL)
6063 return NULL;
6064
976ca316
SM
6065 sig_entry = add_type_unit (per_objfile, sig, slot);
6066 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6067
a2ce51a0
DE
6068 return sig_entry;
6069}
6070
380bca97 6071/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6072 Returns NULL if signature SIG is not present in the table.
6073 It is up to the caller to complain about this. */
348e048f
DE
6074
6075static struct signatured_type *
a2ce51a0 6076lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6077{
976ca316 6078 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6079
976ca316 6080 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6081 {
6082 /* We're in a DWO/DWP file, and we're using .gdb_index.
6083 These cases require special processing. */
976ca316 6084 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6085 return lookup_dwo_signatured_type (cu, sig);
6086 else
6087 return lookup_dwp_signatured_type (cu, sig);
6088 }
6089 else
6090 {
6091 struct signatured_type find_entry, *entry;
348e048f 6092
976ca316 6093 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6094 return NULL;
6095 find_entry.signature = sig;
9a3c8263 6096 entry = ((struct signatured_type *)
976ca316 6097 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6098 &find_entry));
a2ce51a0
DE
6099 return entry;
6100 }
348e048f 6101}
18a8505e 6102
42e7ad6c 6103/* Low level DIE reading support. */
348e048f 6104
d85a05f0
DJ
6105/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6106
6107static void
6108init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6109 struct dwarf2_cu *cu,
3019eac3 6110 struct dwarf2_section_info *section,
685af9cd
TT
6111 struct dwo_file *dwo_file,
6112 struct abbrev_table *abbrev_table)
d85a05f0 6113{
fceca515 6114 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6115 reader->abfd = section->get_bfd_owner ();
d85a05f0 6116 reader->cu = cu;
3019eac3 6117 reader->dwo_file = dwo_file;
dee91e82
DE
6118 reader->die_section = section;
6119 reader->buffer = section->buffer;
f664829e 6120 reader->buffer_end = section->buffer + section->size;
685af9cd 6121 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6122}
6123
c0ab21c2 6124/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6125 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6126 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6127 already.
6128
6129 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6130 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6131 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6132 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6133 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6134 STUB_COMP_DIR may be non-NULL.
3e225074 6135 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6136 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6137 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6138 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6139 kept around for at least as long as *RESULT_READER.
6140
b0c7bfa9
DE
6141 The result is non-zero if a valid (non-dummy) DIE was found. */
6142
6143static int
4ab09049 6144read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6145 struct dwo_unit *dwo_unit,
b0c7bfa9 6146 struct die_info *stub_comp_unit_die,
a2ce51a0 6147 const char *stub_comp_dir,
b0c7bfa9 6148 struct die_reader_specs *result_reader,
d521ce57 6149 const gdb_byte **result_info_ptr,
b0c7bfa9 6150 struct die_info **result_comp_unit_die,
685af9cd 6151 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6152{
976ca316 6153 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6154 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6155 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6156 bfd *abfd;
d521ce57 6157 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6158 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6159 int i,num_extra_attrs;
6160 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6161 struct die_info *comp_unit_die;
6162
b0aeadb3
DE
6163 /* At most one of these may be provided. */
6164 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6165
b0c7bfa9
DE
6166 /* These attributes aren't processed until later:
6167 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6168 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6169 referenced later. However, these attributes are found in the stub
6170 which we won't have later. In order to not impose this complication
6171 on the rest of the code, we read them here and copy them to the
6172 DWO CU/TU die. */
b0c7bfa9
DE
6173
6174 stmt_list = NULL;
6175 low_pc = NULL;
6176 high_pc = NULL;
6177 ranges = NULL;
6178 comp_dir = NULL;
6179
6180 if (stub_comp_unit_die != NULL)
6181 {
6182 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6183 DWO file. */
4ab09049 6184 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6185 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6186 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6187 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6188 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6189 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6190
a39fdb41 6191 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6192
2b0c7f41
SM
6193 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6194 We need the value before we can process DW_AT_ranges values from the
6195 DWO. */
6196 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6197
6198 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6199 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6200 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6201 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6202 section. */
6203 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6204 }
a2ce51a0
DE
6205 else if (stub_comp_dir != NULL)
6206 {
6207 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6208 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6209 comp_dir->name = DW_AT_comp_dir;
6210 comp_dir->form = DW_FORM_string;
c6481205 6211 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6212 }
b0c7bfa9
DE
6213
6214 /* Set up for reading the DWO CU/TU. */
6215 cu->dwo_unit = dwo_unit;
685af9cd 6216 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6217 section->read (objfile);
6218 abfd = section->get_bfd_owner ();
9c541725
PA
6219 begin_info_ptr = info_ptr = (section->buffer
6220 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6221 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6222
4ab09049 6223 if (per_cu->is_debug_types)
b0c7bfa9 6224 {
4ab09049 6225 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6226
976ca316
SM
6227 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6228 section, dwo_abbrev_section,
43988095 6229 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6230 /* This is not an assert because it can be caused by bad debug info. */
43988095 6231 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6232 {
6233 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6234 " TU at offset %s [in module %s]"),
a2ce51a0 6235 hex_string (sig_type->signature),
43988095 6236 hex_string (cu->header.signature),
9d8780f0 6237 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6238 bfd_get_filename (abfd));
6239 }
9c541725 6240 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6241 /* For DWOs coming from DWP files, we don't know the CU length
6242 nor the type's offset in the TU until now. */
4057dfde 6243 dwo_unit->length = cu->header.get_length ();
9c541725 6244 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6245
6246 /* Establish the type offset that can be used to lookup the type.
6247 For DWO files, we don't know it until now. */
9c541725
PA
6248 sig_type->type_offset_in_section
6249 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6250 }
6251 else
6252 {
976ca316
SM
6253 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6254 section, dwo_abbrev_section,
43988095 6255 info_ptr, rcuh_kind::COMPILE);
9c541725 6256 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6257 /* For DWOs coming from DWP files, we don't know the CU length
6258 until now. */
4057dfde 6259 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6260 }
6261
606decb2 6262 dwo_abbrev_section->read (objfile);
685af9cd 6263 *result_dwo_abbrev_table
606decb2 6264 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6265 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6266 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6267
6268 /* Read in the die, but leave space to copy over the attributes
6269 from the stub. This has the benefit of simplifying the rest of
6270 the code - all the work to maintain the illusion of a single
6271 DW_TAG_{compile,type}_unit DIE is done here. */
6272 num_extra_attrs = ((stmt_list != NULL)
6273 + (low_pc != NULL)
6274 + (high_pc != NULL)
6275 + (ranges != NULL)
6276 + (comp_dir != NULL));
6277 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6278 num_extra_attrs);
b0c7bfa9
DE
6279
6280 /* Copy over the attributes from the stub to the DIE we just read in. */
6281 comp_unit_die = *result_comp_unit_die;
6282 i = comp_unit_die->num_attrs;
6283 if (stmt_list != NULL)
6284 comp_unit_die->attrs[i++] = *stmt_list;
6285 if (low_pc != NULL)
6286 comp_unit_die->attrs[i++] = *low_pc;
6287 if (high_pc != NULL)
6288 comp_unit_die->attrs[i++] = *high_pc;
6289 if (ranges != NULL)
6290 comp_unit_die->attrs[i++] = *ranges;
6291 if (comp_dir != NULL)
6292 comp_unit_die->attrs[i++] = *comp_dir;
6293 comp_unit_die->num_attrs += num_extra_attrs;
6294
b4f54984 6295 if (dwarf_die_debug)
bf6af496
DE
6296 {
6297 fprintf_unfiltered (gdb_stdlog,
6298 "Read die from %s@0x%x of %s:\n",
96b79293 6299 section->get_name (),
bf6af496
DE
6300 (unsigned) (begin_info_ptr - section->buffer),
6301 bfd_get_filename (abfd));
b4f54984 6302 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6303 }
6304
b0c7bfa9
DE
6305 /* Skip dummy compilation units. */
6306 if (info_ptr >= begin_info_ptr + dwo_unit->length
6307 || peek_abbrev_code (abfd, info_ptr) == 0)
6308 return 0;
6309
6310 *result_info_ptr = info_ptr;
6311 return 1;
6312}
6313
a084a2a6
AT
6314/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6315 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6316 signature is part of the header. */
6317static gdb::optional<ULONGEST>
6318lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6319{
6320 if (cu->header.version >= 5)
6321 return cu->header.signature;
6322 struct attribute *attr;
6323 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 6324 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 6325 return gdb::optional<ULONGEST> ();
529908cb 6326 return attr->as_unsigned ();
a084a2a6
AT
6327}
6328
c0ab21c2 6329/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6330 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6331 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6332
6333static struct dwo_unit *
4ab09049 6334lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6335{
4ab09049 6336 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6337 struct dwo_unit *dwo_unit;
c0ab21c2 6338 const char *comp_dir;
b0c7bfa9 6339
a2ce51a0
DE
6340 gdb_assert (cu != NULL);
6341
b0c7bfa9 6342 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6343 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6344 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6345
4ab09049
SM
6346 if (per_cu->is_debug_types)
6347 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6348 else
6349 {
a084a2a6 6350 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6351
a084a2a6 6352 if (!signature.has_value ())
b0c7bfa9
DE
6353 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6354 " [in module %s]"),
4ab09049
SM
6355 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6356
6357 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6358 }
6359
b0c7bfa9
DE
6360 return dwo_unit;
6361}
6362
c0ab21c2 6363/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6364 See it for a description of the parameters.
fcd3b13d 6365 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6366
c0ab21c2 6367void
9e021579
SM
6368cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6369 dwarf2_per_objfile *per_objfile,
2e671100 6370 dwarf2_cu *existing_cu)
a2ce51a0 6371{
a2ce51a0 6372 struct signatured_type *sig_type;
a2ce51a0
DE
6373
6374 /* Verify we can do the following downcast, and that we have the
6375 data we need. */
6376 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6377 sig_type = (struct signatured_type *) this_cu;
6378 gdb_assert (sig_type->dwo_unit != NULL);
6379
2e671100
SM
6380 dwarf2_cu *cu;
6381
6382 if (existing_cu != nullptr)
6aa5f3a6 6383 {
2e671100
SM
6384 cu = existing_cu;
6385 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6386 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6387 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6388 }
6389 else
6390 {
7188ed02 6391 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6392 in per_objfile yet. */
7188ed02 6393 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6394 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6395 cu = m_new_cu.get ();
6aa5f3a6
DE
6396 }
6397
6398 /* A future optimization, if needed, would be to use an existing
6399 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6400 could share abbrev tables. */
a2ce51a0 6401
2e671100 6402 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6403 NULL /* stub_comp_unit_die */,
6404 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6405 this, &info_ptr,
3e225074 6406 &comp_unit_die,
c0ab21c2 6407 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6408 {
6409 /* Dummy die. */
c0ab21c2 6410 dummy_p = true;
a2ce51a0 6411 }
a2ce51a0
DE
6412}
6413
fd820528 6414/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6415 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6416
f4dc4d17
DE
6417 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6418 Otherwise the table specified in the comp unit header is read in and used.
6419 This is an optimization for when we already have the abbrev table.
6420
2e671100
SM
6421 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6422 allocated. */
aaa75496 6423
ab432490 6424cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6425 dwarf2_per_objfile *per_objfile,
c0ab21c2 6426 struct abbrev_table *abbrev_table,
2e671100 6427 dwarf2_cu *existing_cu,
c0ab21c2
TT
6428 bool skip_partial)
6429 : die_reader_specs {},
6751ebae 6430 m_this_cu (this_cu)
c906108c 6431{
976ca316 6432 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6433 struct dwarf2_section_info *section = this_cu->section;
96b79293 6434 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 6435 const gdb_byte *begin_info_ptr;
dee91e82 6436 struct signatured_type *sig_type = NULL;
4bdcc0c1 6437 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6438 /* Non-zero if CU currently points to a DWO file and we need to
6439 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6440 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6441 int rereading_dwo_cu = 0;
c906108c 6442
b4f54984 6443 if (dwarf_die_debug)
9d8780f0 6444 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6445 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6446 sect_offset_str (this_cu->sect_off));
09406207 6447
a2ce51a0
DE
6448 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6449 file (instead of going through the stub), short-circuit all of this. */
6450 if (this_cu->reading_dwo_directly)
6451 {
6452 /* Narrow down the scope of possibilities to have to understand. */
6453 gdb_assert (this_cu->is_debug_types);
6454 gdb_assert (abbrev_table == NULL);
976ca316 6455 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
6456 return;
6457 }
6458
dee91e82 6459 /* This is cheap if the section is already read in. */
96b79293 6460 section->read (objfile);
dee91e82 6461
9c541725 6462 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6463
6464 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 6465
2e671100
SM
6466 dwarf2_cu *cu;
6467
6468 if (existing_cu != nullptr)
dee91e82 6469 {
2e671100 6470 cu = existing_cu;
42e7ad6c
DE
6471 /* If this CU is from a DWO file we need to start over, we need to
6472 refetch the attributes from the skeleton CU.
6473 This could be optimized by retrieving those attributes from when we
6474 were here the first time: the previous comp_unit_die was stored in
6475 comp_unit_obstack. But there's no data yet that we need this
6476 optimization. */
6477 if (cu->dwo_unit != NULL)
6478 rereading_dwo_cu = 1;
dee91e82
DE
6479 }
6480 else
6481 {
7188ed02 6482 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6483 in per_objfile yet. */
976ca316
SM
6484 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6485 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 6486 cu = m_new_cu.get ();
42e7ad6c 6487 }
dee91e82 6488
b0c7bfa9 6489 /* Get the header. */
9c541725 6490 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6491 {
6492 /* We already have the header, there's no need to read it in again. */
9c541725 6493 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6494 }
6495 else
6496 {
3019eac3 6497 if (this_cu->is_debug_types)
dee91e82 6498 {
976ca316
SM
6499 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6500 section, abbrev_section,
6501 info_ptr, rcuh_kind::TYPE);
dee91e82 6502
42e7ad6c
DE
6503 /* Since per_cu is the first member of struct signatured_type,
6504 we can go from a pointer to one to a pointer to the other. */
6505 sig_type = (struct signatured_type *) this_cu;
43988095 6506 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6507 gdb_assert (sig_type->type_offset_in_tu
6508 == cu->header.type_cu_offset_in_tu);
6509 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6510
42e7ad6c
DE
6511 /* LENGTH has not been set yet for type units if we're
6512 using .gdb_index. */
4057dfde 6513 this_cu->length = cu->header.get_length ();
3019eac3
DE
6514
6515 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6516 sig_type->type_offset_in_section =
6517 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6518
6519 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6520 }
6521 else
6522 {
976ca316
SM
6523 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6524 section, abbrev_section,
43988095
JK
6525 info_ptr,
6526 rcuh_kind::COMPILE);
dee91e82 6527
9c541725 6528 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
6529 if (this_cu->length == 0)
6530 this_cu->length = cu->header.get_length ();
6531 else
6532 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6533 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6534 }
6535 }
10b3939b 6536
6caca83c 6537 /* Skip dummy compilation units. */
dee91e82 6538 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6539 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6540 {
6541 dummy_p = true;
6542 return;
6543 }
6caca83c 6544
433df2d4
DE
6545 /* If we don't have them yet, read the abbrevs for this compilation unit.
6546 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6547 done. */
f4dc4d17 6548 if (abbrev_table != NULL)
685af9cd
TT
6549 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6550 else
f4dc4d17 6551 {
606decb2 6552 abbrev_section->read (objfile);
c0ab21c2 6553 m_abbrev_table_holder
606decb2 6554 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 6555 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6556 }
af703f96 6557
dee91e82 6558 /* Read the top level CU/TU die. */
c0ab21c2 6559 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6560 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6561
58f0c718 6562 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6563 {
6564 dummy_p = true;
6565 return;
6566 }
58f0c718 6567
b0c7bfa9 6568 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6569 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6570 table from the DWO file and pass the ownership over to us. It will be
6571 referenced from READER, so we must make sure to free it after we're done
6572 with READER.
6573
b0c7bfa9
DE
6574 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6575 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6576 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6577 if (dwo_name != nullptr)
3019eac3 6578 {
3019eac3 6579 struct dwo_unit *dwo_unit;
b0c7bfa9 6580 struct die_info *dwo_comp_unit_die;
3019eac3 6581
3e225074 6582 if (comp_unit_die->has_children)
6a506a2d 6583 {
b98664d3 6584 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6585 " has children (offset %s) [in module %s]"),
6586 sect_offset_str (this_cu->sect_off),
6587 bfd_get_filename (abfd));
6a506a2d 6588 }
4ab09049 6589 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 6590 if (dwo_unit != NULL)
3019eac3 6591 {
4ab09049 6592 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 6593 comp_unit_die, NULL,
c0ab21c2 6594 this, &info_ptr,
3e225074 6595 &dwo_comp_unit_die,
c0ab21c2 6596 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6597 {
6598 /* Dummy die. */
c0ab21c2 6599 dummy_p = true;
6a506a2d
DE
6600 return;
6601 }
6602 comp_unit_die = dwo_comp_unit_die;
6603 }
6604 else
6605 {
6606 /* Yikes, we couldn't find the rest of the DIE, we only have
6607 the stub. A complaint has already been logged. There's
6608 not much more we can do except pass on the stub DIE to
6609 die_reader_func. We don't want to throw an error on bad
6610 debug info. */
3019eac3
DE
6611 }
6612 }
c0ab21c2 6613}
3019eac3 6614
6751ebae
TT
6615void
6616cutu_reader::keep ()
c0ab21c2 6617{
b0c7bfa9 6618 /* Done, clean up. */
6751ebae
TT
6619 gdb_assert (!dummy_p);
6620 if (m_new_cu != NULL)
348e048f 6621 {
7188ed02 6622 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 6623 now. */
7188ed02
SM
6624 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6625 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 6626 }
dee91e82
DE
6627}
6628
18a8505e
AT
6629/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6630 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6631 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6632
6633 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6634 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6635
6636 We fill in THIS_CU->length.
6637
dee91e82 6638 THIS_CU->cu is always freed when done.
3019eac3 6639 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6640 to care whether it refers to the "main" CU or the DWO CU.
6641
6642 When parent_cu is passed, it is used to provide a default value for
6643 str_offsets_base and addr_base from the parent. */
dee91e82 6644
ab432490 6645cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6646 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
6647 struct dwarf2_cu *parent_cu,
6648 struct dwo_file *dwo_file)
6649 : die_reader_specs {},
6650 m_this_cu (this_cu)
dee91e82 6651{
976ca316 6652 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6653 struct dwarf2_section_info *section = this_cu->section;
96b79293 6654 bfd *abfd = section->get_bfd_owner ();
33e80786 6655 struct dwarf2_section_info *abbrev_section;
d521ce57 6656 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6657
b4f54984 6658 if (dwarf_die_debug)
9d8780f0 6659 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6660 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6661 sect_offset_str (this_cu->sect_off));
09406207 6662
976ca316 6663 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 6664
33e80786
DE
6665 abbrev_section = (dwo_file != NULL
6666 ? &dwo_file->sections.abbrev
6667 : get_abbrev_section_for_cu (this_cu));
6668
dee91e82 6669 /* This is cheap if the section is already read in. */
96b79293 6670 section->read (objfile);
dee91e82 6671
976ca316 6672 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 6673
9c541725 6674 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
6675 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6676 section, abbrev_section, info_ptr,
43988095
JK
6677 (this_cu->is_debug_types
6678 ? rcuh_kind::TYPE
6679 : rcuh_kind::COMPILE));
dee91e82 6680
18a8505e
AT
6681 if (parent_cu != nullptr)
6682 {
c0ab21c2
TT
6683 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6684 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 6685 }
4057dfde 6686 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
6687
6688 /* Skip dummy compilation units. */
6689 if (info_ptr >= begin_info_ptr + this_cu->length
6690 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6691 {
6692 dummy_p = true;
6693 return;
6694 }
72bf9492 6695
606decb2 6696 abbrev_section->read (objfile);
c0ab21c2 6697 m_abbrev_table_holder
606decb2 6698 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 6699
c0ab21c2
TT
6700 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6701 m_abbrev_table_holder.get ());
3e225074 6702 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
6703}
6704
0018ea6f
DE
6705\f
6706/* Type Unit Groups.
dee91e82 6707
0018ea6f
DE
6708 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6709 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6710 so that all types coming from the same compilation (.o file) are grouped
6711 together. A future step could be to put the types in the same symtab as
6712 the CU the types ultimately came from. */
ff013f42 6713
f4dc4d17
DE
6714static hashval_t
6715hash_type_unit_group (const void *item)
6716{
9a3c8263
SM
6717 const struct type_unit_group *tu_group
6718 = (const struct type_unit_group *) item;
f4dc4d17 6719
094b34ac 6720 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6721}
348e048f
DE
6722
6723static int
f4dc4d17 6724eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6725{
9a3c8263
SM
6726 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6727 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6728
094b34ac 6729 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6730}
348e048f 6731
f4dc4d17
DE
6732/* Allocate a hash table for type unit groups. */
6733
eaa5fa8b 6734static htab_up
298e9637 6735allocate_type_unit_groups_table ()
f4dc4d17 6736{
eaa5fa8b
TT
6737 return htab_up (htab_create_alloc (3,
6738 hash_type_unit_group,
6739 eq_type_unit_group,
ef5f598c 6740 htab_delete_entry<type_unit_group>,
0d305d5c 6741 xcalloc, xfree));
f4dc4d17 6742}
dee91e82 6743
f4dc4d17
DE
6744/* Type units that don't have DW_AT_stmt_list are grouped into their own
6745 partial symtabs. We combine several TUs per psymtab to not let the size
6746 of any one psymtab grow too big. */
6747#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6748#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6749
094b34ac 6750/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6751 Create the type_unit_group object used to hold one or more TUs. */
6752
0d305d5c 6753static std::unique_ptr<type_unit_group>
094b34ac 6754create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 6755{
976ca316
SM
6756 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6757 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 6758
0d305d5c
TT
6759 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6760 tu_group->per_bfd = per_bfd;
f4dc4d17 6761
1859c670 6762 if (per_bfd->using_index)
094b34ac 6763 {
0d305d5c
TT
6764 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6765 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6766 }
6767 else
6768 {
9c541725 6769 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 6770 dwarf2_psymtab *pst;
528e1572 6771 std::string name;
094b34ac
DE
6772
6773 /* Give the symtab a useful name for debug purposes. */
6774 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
6775 name = string_printf ("<type_units_%d>",
6776 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 6777 else
528e1572 6778 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 6779
0d305d5c
TT
6780 pst = create_partial_symtab (tu_group.get (), per_objfile,
6781 name.c_str ());
6d94535f 6782 pst->anonymous = true;
094b34ac 6783 }
f4dc4d17 6784
094b34ac 6785 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6786 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6787
6788 return tu_group;
6789}
6790
094b34ac
DE
6791/* Look up the type_unit_group for type unit CU, and create it if necessary.
6792 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6793
6794static struct type_unit_group *
ff39bb5e 6795get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 6796{
976ca316
SM
6797 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6798 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
6799 struct type_unit_group *tu_group;
6800 void **slot;
6801 unsigned int line_offset;
6802 struct type_unit_group type_unit_group_for_lookup;
6803
976ca316
SM
6804 if (per_objfile->per_bfd->type_unit_groups == NULL)
6805 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
6806
6807 /* Do we need to create a new group, or can we use an existing one? */
6808
529908cb 6809 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 6810 {
529908cb 6811 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
6812 ++tu_stats->nr_symtab_sharers;
6813 }
6814 else
6815 {
6816 /* Ugh, no stmt_list. Rare, but we have to handle it.
6817 We can do various things here like create one group per TU or
6818 spread them over multiple groups to split up the expansion work.
6819 To avoid worst case scenarios (too many groups or too large groups)
6820 we, umm, group them in bunches. */
6821 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6822 | (tu_stats->nr_stmt_less_type_units
6823 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6824 ++tu_stats->nr_stmt_less_type_units;
6825 }
6826
094b34ac 6827 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6828 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 6829 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17 6830 &type_unit_group_for_lookup, INSERT);
0d305d5c 6831 if (*slot == nullptr)
f4dc4d17 6832 {
9c541725 6833 sect_offset line_offset_struct = (sect_offset) line_offset;
0d305d5c
TT
6834 std::unique_ptr<type_unit_group> grp
6835 = create_type_unit_group (cu, line_offset_struct);
6836 *slot = grp.release ();
f4dc4d17
DE
6837 ++tu_stats->nr_symtabs;
6838 }
6839
0d305d5c
TT
6840 tu_group = (struct type_unit_group *) *slot;
6841 gdb_assert (tu_group != nullptr);
f4dc4d17
DE
6842 return tu_group;
6843}
0018ea6f
DE
6844\f
6845/* Partial symbol tables. */
6846
6847/* Create a psymtab named NAME and assign it to PER_CU.
6848
6849 The caller must fill in the following details:
6850 dirname, textlow, texthigh. */
6851
891813be 6852static dwarf2_psymtab *
7aa104c4
SM
6853create_partial_symtab (dwarf2_per_cu_data *per_cu,
6854 dwarf2_per_objfile *per_objfile,
6855 const char *name)
0018ea6f 6856{
0072c873
SM
6857 dwarf2_psymtab *pst
6858 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6859 per_objfile->objfile->per_bfd, per_cu);
0018ea6f 6860
6d94535f 6861 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
6862
6863 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
6864 per_cu->v.psymtab = pst;
6865
6866 return pst;
6867}
6868
c0ab21c2 6869/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
6870
6871static void
6872process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6873 const gdb_byte *info_ptr,
0018ea6f 6874 struct die_info *comp_unit_die,
c0ab21c2 6875 enum language pretend_language)
0018ea6f
DE
6876{
6877 struct dwarf2_cu *cu = reader->cu;
7aa104c4 6878 dwarf2_per_objfile *per_objfile = cu->per_objfile;
84685904 6879 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7aa104c4 6880 struct objfile *objfile = per_objfile->objfile;
08feed99 6881 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 6882 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6883 CORE_ADDR baseaddr;
6884 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 6885 dwarf2_psymtab *pst;
3a2b436a 6886 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6887 const char *filename;
0018ea6f 6888
0018ea6f
DE
6889 gdb_assert (! per_cu->is_debug_types);
6890
c0ab21c2 6891 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 6892
0018ea6f 6893 /* Allocate a new partial symbol table structure. */
2e927613
TV
6894 gdb::unique_xmalloc_ptr<char> debug_filename;
6895 static const char artificial[] = "<artificial>";
7d45c7c3
KB
6896 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6897 if (filename == NULL)
0018ea6f 6898 filename = "";
2e927613
TV
6899 else if (strcmp (filename, artificial) == 0)
6900 {
6901 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
6902 sect_offset_str (per_cu->sect_off),
6903 (char *) NULL));
2e927613
TV
6904 filename = debug_filename.get ();
6905 }
0018ea6f 6906
7aa104c4 6907 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
6908
6909 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6910 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 6911
b3b3bada 6912 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
6913
6914 dwarf2_find_base_address (comp_unit_die, cu);
6915
6916 /* Possibly set the default values of LOWPC and HIGHPC from
6917 `DW_AT_ranges'. */
3a2b436a
JK
6918 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6919 &best_highpc, cu, pst);
6920 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
6921 {
6922 CORE_ADDR low
6923 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
6924 - baseaddr);
6925 CORE_ADDR high
6926 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
6927 - baseaddr - 1);
6928 /* Store the contiguous range if it is not empty; it can be
6929 empty for CUs with no code. */
84685904 6930 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 6931 low, high, pst);
79748972 6932 }
0018ea6f
DE
6933
6934 /* Check if comp unit has_children.
6935 If so, read the rest of the partial symbols from this comp unit.
6936 If not, there's no more debug_info for this comp unit. */
3e225074 6937 if (comp_unit_die->has_children)
0018ea6f
DE
6938 {
6939 struct partial_die_info *first_die;
6940 CORE_ADDR lowpc, highpc;
6941
6942 lowpc = ((CORE_ADDR) -1);
6943 highpc = ((CORE_ADDR) 0);
6944
6945 first_die = load_partial_dies (reader, info_ptr, 1);
6946
6947 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6948 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6949
6950 /* If we didn't find a lowpc, set it to highpc to avoid
6951 complaints from `maint check'. */
6952 if (lowpc == ((CORE_ADDR) -1))
6953 lowpc = highpc;
6954
6955 /* If the compilation unit didn't have an explicit address range,
6956 then use the information extracted from its child dies. */
e385593e 6957 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6958 {
6959 best_lowpc = lowpc;
6960 best_highpc = highpc;
6961 }
6962 }
4ae976d1 6963 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
6964 best_lowpc + baseaddr)
6965 - baseaddr);
4ae976d1 6966 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
6967 best_highpc + baseaddr)
6968 - baseaddr);
0018ea6f 6969
ae7754b2 6970 pst->end ();
0018ea6f 6971
ae640021 6972 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
6973 {
6974 int i;
ae640021 6975 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
6976
6977 /* Fill in 'dependencies' here; we fill in 'users' in a
6978 post-pass. */
6979 pst->number_of_dependencies = len;
a9342b62 6980 pst->dependencies
84685904 6981 = per_bfd->partial_symtabs->allocate_dependencies (len);
ae640021
AB
6982 for (i = 0; i < len; ++i)
6983 {
6984 pst->dependencies[i]
6985 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
6986 }
0018ea6f 6987
ae640021 6988 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
6989 }
6990
6991 /* Get the list of files included in the current compilation unit,
6992 and build a psymtab for each of them. */
6993 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6994
6f738b01
SM
6995 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
6996 ", %d global, %d static syms",
6997 per_cu->is_debug_types ? "type" : "comp",
6998 sect_offset_str (per_cu->sect_off),
6999 paddress (gdbarch, pst->text_low (objfile)),
7000 paddress (gdbarch, pst->text_high (objfile)),
7001 (int) pst->global_psymbols.size (),
7002 (int) pst->static_psymbols.size ());
0018ea6f
DE
7003}
7004
7005/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7006 Process compilation unit THIS_CU for a psymtab. */
7007
7008static void
ab432490
SM
7009process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7010 dwarf2_per_objfile *per_objfile,
135f5437 7011 bool want_partial_unit,
b93601f3 7012 enum language pretend_language)
0018ea6f
DE
7013{
7014 /* If this compilation unit was already read in, free the
7015 cached copy in order to read it in again. This is
7016 necessary because we skipped some symbols when we first
7017 read in the compilation unit (see load_partial_dies).
7018 This problem could be avoided, but the benefit is unclear. */
7188ed02 7019 per_objfile->remove_cu (this_cu);
0018ea6f 7020
2e671100 7021 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7022
58990295
TV
7023 switch (reader.comp_unit_die->tag)
7024 {
7025 case DW_TAG_compile_unit:
7026 this_cu->unit_type = DW_UT_compile;
7027 break;
7028 case DW_TAG_partial_unit:
7029 this_cu->unit_type = DW_UT_partial;
7030 break;
e77b0004
TV
7031 case DW_TAG_type_unit:
7032 this_cu->unit_type = DW_UT_type;
7033 break;
58990295 7034 default:
702cf3f5
AB
7035 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7036 dwarf_tag_name (reader.comp_unit_die->tag),
7037 sect_offset_str (reader.cu->per_cu->sect_off),
7038 objfile_name (per_objfile->objfile));
58990295
TV
7039 }
7040
c0ab21c2 7041 if (reader.dummy_p)
f1902523 7042 {
c0ab21c2 7043 /* Nothing. */
f1902523 7044 }
c0ab21c2 7045 else if (this_cu->is_debug_types)
3e225074
TT
7046 build_type_psymtabs_reader (&reader, reader.info_ptr,
7047 reader.comp_unit_die);
135f5437
TT
7048 else if (want_partial_unit
7049 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7050 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7051 reader.comp_unit_die,
c0ab21c2 7052 pretend_language);
0018ea6f 7053
7188ed02 7054 this_cu->lang = reader.cu->language;
58990295 7055
0018ea6f 7056 /* Age out any secondary CUs. */
7188ed02 7057 per_objfile->age_comp_units ();
0018ea6f 7058}
f4dc4d17
DE
7059
7060/* Reader function for build_type_psymtabs. */
7061
7062static void
7063build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7064 const gdb_byte *info_ptr,
3e225074 7065 struct die_info *type_unit_die)
f4dc4d17 7066{
976ca316 7067 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7068 struct dwarf2_cu *cu = reader->cu;
7069 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7070 struct signatured_type *sig_type;
f4dc4d17
DE
7071 struct type_unit_group *tu_group;
7072 struct attribute *attr;
7073 struct partial_die_info *first_die;
7074 CORE_ADDR lowpc, highpc;
891813be 7075 dwarf2_psymtab *pst;
f4dc4d17 7076
0186c6a7
DE
7077 gdb_assert (per_cu->is_debug_types);
7078 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7079
3e225074 7080 if (! type_unit_die->has_children)
f4dc4d17
DE
7081 return;
7082
052c8bb8 7083 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7084 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7085
df07e2c7 7086 if (tu_group->tus == nullptr)
a8b3b8e9 7087 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7088 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7089
7090 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7091 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7092 pst->anonymous = true;
f4dc4d17
DE
7093
7094 first_die = load_partial_dies (reader, info_ptr, 1);
7095
7096 lowpc = (CORE_ADDR) -1;
7097 highpc = (CORE_ADDR) 0;
7098 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7099
ae7754b2 7100 pst->end ();
f4dc4d17
DE
7101}
7102
73051182
DE
7103/* Struct used to sort TUs by their abbreviation table offset. */
7104
7105struct tu_abbrev_offset
7106{
b2bdb8cf
SM
7107 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7108 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7109 {}
7110
16e0020b 7111 /* This is used when sorting. */
5433e20e 7112 bool operator< (const tu_abbrev_offset &other) const
16e0020b
TT
7113 {
7114 return abbrev_offset < other.abbrev_offset;
7115 }
7116
b2bdb8cf 7117 signatured_type *sig_type;
73051182
DE
7118 sect_offset abbrev_offset;
7119};
7120
73051182 7121/* Efficiently read all the type units.
73051182
DE
7122
7123 The efficiency is because we sort TUs by the abbrev table they use and
7124 only read each abbrev table once. In one program there are 200K TUs
7125 sharing 8K abbrev tables.
7126
7127 The main purpose of this function is to support building the
5989a64e 7128 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7129 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7130 can collapse the search space by grouping them by stmt_list.
7131 The savings can be significant, in the same program from above the 200K TUs
7132 share 8K stmt_list tables.
7133
7134 FUNC is expected to call get_type_unit_group, which will create the
7135 struct type_unit_group if necessary and add it to
5989a64e 7136 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7137
7138static void
b8efb248 7139build_type_psymtabs (dwarf2_per_objfile *per_objfile)
73051182 7140{
976ca316 7141 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7142 abbrev_table_up abbrev_table;
73051182 7143 sect_offset abbrev_offset;
73051182
DE
7144
7145 /* It's up to the caller to not call us multiple times. */
976ca316 7146 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7147
91eea9cc 7148 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
73051182
DE
7149 return;
7150
7151 /* TUs typically share abbrev tables, and there can be way more TUs than
7152 abbrev tables. Sort by abbrev table to reduce the number of times we
7153 read each abbrev table in.
7154 Alternatives are to punt or to maintain a cache of abbrev tables.
7155 This is simpler and efficient enough for now.
7156
7157 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7158 symtab to use). Typically TUs with the same abbrev offset have the same
7159 stmt_list value too so in practice this should work well.
7160
7161 The basic algorithm here is:
7162
7163 sort TUs by abbrev table
7164 for each TU with same abbrev table:
7165 read abbrev table if first user
7166 read TU top level DIE
7167 [IWBN if DWO skeletons had DW_AT_stmt_list]
7168 call FUNC */
7169
6f738b01 7170 dwarf_read_debug_printf ("Building type unit groups ...");
73051182 7171
91eea9cc 7172 /* Sort in a separate table to maintain the order of all_comp_units
73051182 7173 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7174 std::vector<tu_abbrev_offset> sorted_by_abbrev;
91eea9cc 7175 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
b2bdb8cf 7176
91eea9cc
TT
7177 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7178 {
7179 if (cu->is_debug_types)
7180 {
7181 auto sig_type = static_cast<signatured_type *> (cu.get ());
7182 sorted_by_abbrev.emplace_back
7183 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7184 sig_type->sect_off));
7185 }
7186 }
73051182 7187
16e0020b 7188 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
73051182 7189
9c541725 7190 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7191
b2bdb8cf 7192 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7193 {
73051182
DE
7194 /* Switch to the next abbrev table if necessary. */
7195 if (abbrev_table == NULL
b2bdb8cf 7196 || tu.abbrev_offset != abbrev_offset)
73051182 7197 {
b2bdb8cf 7198 abbrev_offset = tu.abbrev_offset;
606decb2 7199 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7200 abbrev_table =
606decb2 7201 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7202 ++tu_stats->nr_uniq_abbrev_tables;
7203 }
7204
0d305d5c 7205 cutu_reader reader (tu.sig_type, per_objfile,
2e671100 7206 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7207 if (!reader.dummy_p)
7208 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7209 reader.comp_unit_die);
73051182 7210 }
6aa5f3a6 7211}
73051182 7212
6aa5f3a6
DE
7213/* Print collected type unit statistics. */
7214
7215static void
976ca316 7216print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7217{
976ca316 7218 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7219
6f738b01 7220 dwarf_read_debug_printf ("Type unit statistics:");
91eea9cc 7221 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
6f738b01
SM
7222 dwarf_read_debug_printf (" %d uniq abbrev tables",
7223 tu_stats->nr_uniq_abbrev_tables);
7224 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7225 tu_stats->nr_symtabs);
7226 dwarf_read_debug_printf (" %d symtab sharers",
7227 tu_stats->nr_symtab_sharers);
7228 dwarf_read_debug_printf (" %d type units without a stmt_list",
7229 tu_stats->nr_stmt_less_type_units);
7230 dwarf_read_debug_printf (" %d all_type_units reallocs",
7231 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7232}
7233
f4dc4d17
DE
7234/* Traversal function for build_type_psymtabs. */
7235
7236static int
7237build_type_psymtab_dependencies (void **slot, void *info)
7238{
976ca316 7239 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
84685904 7240 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 7241 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
0d305d5c 7242 dwarf2_psymtab *pst = tu_group->v.psymtab;
df07e2c7 7243 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7244 int i;
7245
7246 gdb_assert (len > 0);
0d305d5c 7247 gdb_assert (tu_group->type_unit_group_p ());
f4dc4d17
DE
7248
7249 pst->number_of_dependencies = len;
84685904 7250 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
df07e2c7 7251 for (i = 0; i < len; ++i)
f4dc4d17 7252 {
df07e2c7 7253 struct signatured_type *iter = tu_group->tus->at (i);
0d305d5c
TT
7254 gdb_assert (iter->is_debug_types);
7255 pst->dependencies[i] = iter->v.psymtab;
796a7ff8 7256 iter->type_unit_group = tu_group;
f4dc4d17
DE
7257 }
7258
df07e2c7
AB
7259 delete tu_group->tus;
7260 tu_group->tus = nullptr;
348e048f
DE
7261
7262 return 1;
7263}
7264
6aa5f3a6
DE
7265/* Traversal function for process_skeletonless_type_unit.
7266 Read a TU in a DWO file and build partial symbols for it. */
7267
7268static int
7269process_skeletonless_type_unit (void **slot, void *info)
7270{
7271 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7272 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7273 struct signatured_type find_entry, *entry;
7274
7275 /* If this TU doesn't exist in the global table, add it and read it in. */
7276
976ca316
SM
7277 if (per_objfile->per_bfd->signatured_types == NULL)
7278 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7279
7280 find_entry.signature = dwo_unit->signature;
976ca316 7281 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7282 &find_entry, INSERT);
6aa5f3a6
DE
7283 /* If we've already seen this type there's nothing to do. What's happening
7284 is we're doing our own version of comdat-folding here. */
7285 if (*slot != NULL)
7286 return 1;
7287
b8efb248 7288 /* This does the job that create_all_comp_units would have done for
6aa5f3a6 7289 this TU. */
976ca316
SM
7290 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7291 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7292 *slot = entry;
7293
b8efb248 7294 /* This does the job that build_type_psymtabs would have done. */
0d305d5c 7295 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7296 if (!reader.dummy_p)
7297 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7298 reader.comp_unit_die);
6aa5f3a6
DE
7299
7300 return 1;
7301}
7302
7303/* Traversal function for process_skeletonless_type_units. */
7304
7305static int
7306process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7307{
7308 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7309
7310 if (dwo_file->tus != NULL)
b0b6a987
TT
7311 htab_traverse_noresize (dwo_file->tus.get (),
7312 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7313
7314 return 1;
7315}
7316
7317/* Scan all TUs of DWO files, verifying we've processed them.
7318 This is needed in case a TU was emitted without its skeleton.
7319 Note: This can't be done until we know what all the DWO files are. */
7320
7321static void
976ca316 7322process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7323{
7324 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7325 if (get_dwp_file (per_objfile) == NULL
7326 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7327 {
976ca316 7328 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7329 process_dwo_file_for_skeletonless_type_units,
976ca316 7330 per_objfile);
6aa5f3a6 7331 }
348e048f
DE
7332}
7333
ed2dc618 7334/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7335
7336static void
976ca316 7337set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7338{
0d305d5c 7339 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7340 {
891813be 7341 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7342
36586728
TT
7343 if (pst == NULL)
7344 continue;
7345
b76e467d 7346 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7347 {
7348 /* Set the 'user' field only if it is not already set. */
7349 if (pst->dependencies[j]->user == NULL)
7350 pst->dependencies[j]->user = pst;
7351 }
7352 }
7353}
7354
93311388
DE
7355/* Build the partial symbol table by doing a quick pass through the
7356 .debug_info and .debug_abbrev sections. */
72bf9492 7357
93311388 7358static void
976ca316 7359dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 7360{
976ca316 7361 struct objfile *objfile = per_objfile->objfile;
84685904 7362 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
93311388 7363
6f738b01
SM
7364 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7365 objfile_name (objfile));
45cfd468 7366
76935768 7367 scoped_restore restore_reading_psyms
bab287cd 7368 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
98bfdba5 7369
84685904 7370 per_bfd->info.read (objfile);
91c24f0a 7371
93311388
DE
7372 /* Any cached compilation units will be linked by the per-objfile
7373 read_in_chain. Make sure to free them when we're done. */
976ca316 7374 free_cached_comp_units freer (per_objfile);
72bf9492 7375
976ca316 7376 create_all_comp_units (per_objfile);
b8efb248 7377 build_type_psymtabs (per_objfile);
c906108c 7378
60606b2c
TT
7379 /* Create a temporary address map on a temporary obstack. We later
7380 copy this to the final obstack. */
8268c778 7381 auto_obstack temp_obstack;
791afaa2
TT
7382
7383 scoped_restore save_psymtabs_addrmap
84685904 7384 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
791afaa2 7385 addrmap_create_mutable (&temp_obstack));
72bf9492 7386
0d305d5c 7387 for (const auto &per_cu : per_bfd->all_comp_units)
3d5afab3
TV
7388 {
7389 if (per_cu->v.psymtab != NULL)
7390 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7391 continue;
0d305d5c 7392 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
ab432490 7393 language_minimal);
3d5afab3 7394 }
ff013f42 7395
6aa5f3a6 7396 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 7397 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
7398
7399 /* Now that all TUs have been processed we can fill in the dependencies. */
84685904 7400 if (per_bfd->type_unit_groups != NULL)
6aa5f3a6 7401 {
84685904 7402 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
976ca316 7403 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
7404 }
7405
6f738b01 7406 if (dwarf_read_debug > 0)
976ca316 7407 print_tu_stats (per_objfile);
6aa5f3a6 7408
976ca316 7409 set_partial_user (per_objfile);
95554aad 7410
84685904
TT
7411 per_bfd->partial_symtabs->psymtabs_addrmap
7412 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7413 per_bfd->partial_symtabs->obstack ());
791afaa2
TT
7414 /* At this point we want to keep the address map. */
7415 save_psymtabs_addrmap.release ();
ff013f42 7416
6f738b01
SM
7417 dwarf_read_debug_printf ("Done building psymtabs of %s",
7418 objfile_name (objfile));
ae038cb0
DJ
7419}
7420
dee91e82
DE
7421/* Load the partial DIEs for a secondary CU into memory.
7422 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7423
dee91e82 7424static void
ab432490 7425load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
7426 dwarf2_per_objfile *per_objfile,
7427 dwarf2_cu *existing_cu)
dee91e82 7428{
2e671100 7429 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
7430
7431 if (!reader.dummy_p)
7432 {
7433 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7434 language_minimal);
7435
7436 /* Check if comp unit has_children.
7437 If so, read the rest of the partial symbols from this comp unit.
7438 If not, there's no more debug_info for this comp unit. */
3e225074 7439 if (reader.comp_unit_die->has_children)
c0ab21c2 7440 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7441
7442 reader.keep ();
c0ab21c2 7443 }
ae038cb0
DJ
7444}
7445
ae038cb0 7446static void
976ca316 7447read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 7448 struct dwarf2_section_info *section,
f1902523 7449 struct dwarf2_section_info *abbrev_section,
b8efb248
TT
7450 unsigned int is_dwz,
7451 htab_up &types_htab,
7452 rcuh_kind section_kind)
ae038cb0 7453{
d521ce57 7454 const gdb_byte *info_ptr;
976ca316 7455 struct objfile *objfile = per_objfile->objfile;
be391dca 7456
6f738b01
SM
7457 dwarf_read_debug_printf ("Reading %s for %s",
7458 section->get_name (),
7459 section->get_file_name ());
bf6af496 7460
96b79293 7461 section->read (objfile);
ae038cb0 7462
36586728 7463 info_ptr = section->buffer;
6e70227d 7464
36586728 7465 while (info_ptr < section->buffer + section->size)
ae038cb0 7466 {
473ab964 7467 dwarf2_per_cu_data_up this_cu;
ae038cb0 7468
9c541725 7469 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7470
f1902523 7471 comp_unit_head cu_header;
976ca316 7472 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 7473 abbrev_section, info_ptr,
b8efb248 7474 section_kind);
ae038cb0
DJ
7475
7476 /* Save the compilation unit for later lookup. */
f1902523 7477 if (cu_header.unit_type != DW_UT_type)
976ca316 7478 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
7479 else
7480 {
b8efb248
TT
7481 if (types_htab == nullptr)
7482 types_htab = allocate_signatured_type_table ();
7483
976ca316 7484 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b8efb248 7485 signatured_type *sig_ptr = sig_type.get ();
f1902523
JK
7486 sig_type->signature = cu_header.signature;
7487 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
473ab964 7488 this_cu.reset (sig_type.release ());
b8efb248
TT
7489
7490 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7491 gdb_assert (slot != nullptr);
7492 if (*slot != nullptr)
7493 complaint (_("debug type entry at offset %s is duplicate to"
7494 " the entry at offset %s, signature %s"),
7495 sect_offset_str (sect_off),
7496 sect_offset_str (sig_ptr->sect_off),
7497 hex_string (sig_ptr->signature));
7498 *slot = sig_ptr;
f1902523
JK
7499 }
7500 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7501 this_cu->sect_off = sect_off;
f1902523 7502 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7503 this_cu->is_dwz = is_dwz;
8a0459fd 7504 this_cu->section = section;
ae038cb0 7505
ae038cb0 7506 info_ptr = info_ptr + this_cu->length;
0d305d5c 7507 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
ae038cb0 7508 }
36586728
TT
7509}
7510
7511/* Create a list of all compilation units in OBJFILE.
7512 This is only done for -readnow and building partial symtabs. */
7513
7514static void
976ca316 7515create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 7516{
b8efb248
TT
7517 htab_up types_htab;
7518
976ca316 7519 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
b8efb248
TT
7520 &per_objfile->per_bfd->abbrev, 0,
7521 types_htab, rcuh_kind::COMPILE);
7522 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7523 read_comp_units_from_section (per_objfile, &section,
7524 &per_objfile->per_bfd->abbrev, 0,
7525 types_htab, rcuh_kind::TYPE);
36586728 7526
976ca316 7527 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 7528 if (dwz != NULL)
b8efb248
TT
7529 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7530 types_htab, rcuh_kind::COMPILE);
7531
7532 per_objfile->per_bfd->signatured_types = std::move (types_htab);
c906108c
SS
7533}
7534
5734ee8b 7535/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7536 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7537 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7538 DW_AT_ranges). See the comments of add_partial_subprogram on how
7539 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7540
72bf9492
DJ
7541static void
7542scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7543 CORE_ADDR *highpc, int set_addrmap,
7544 struct dwarf2_cu *cu)
c906108c 7545{
72bf9492 7546 struct partial_die_info *pdi;
c906108c 7547
91c24f0a
DC
7548 /* Now, march along the PDI's, descending into ones which have
7549 interesting children but skipping the children of the other ones,
7550 until we reach the end of the compilation unit. */
c906108c 7551
72bf9492 7552 pdi = first_die;
91c24f0a 7553
72bf9492
DJ
7554 while (pdi != NULL)
7555 {
52356b79 7556 pdi->fixup (cu);
c906108c 7557
f55ee35c 7558 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7559 children, so we need to look at them. Ditto for anonymous
7560 enums. */
933c6fe4 7561
7d00ffec 7562 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7563 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7564 || pdi->tag == DW_TAG_imported_unit
7565 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7566 {
72bf9492 7567 switch (pdi->tag)
c906108c
SS
7568 {
7569 case DW_TAG_subprogram:
b1dc1806 7570 case DW_TAG_inlined_subroutine:
cdc07690 7571 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
7572 if (cu->language == language_cplus)
7573 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7574 set_addrmap, cu);
c906108c 7575 break;
72929c62 7576 case DW_TAG_constant:
c906108c
SS
7577 case DW_TAG_variable:
7578 case DW_TAG_typedef:
91c24f0a 7579 case DW_TAG_union_type:
317d2668
TV
7580 if (!pdi->is_declaration
7581 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 7582 {
72bf9492 7583 add_partial_symbol (pdi, cu);
63d06c5c
DC
7584 }
7585 break;
c906108c 7586 case DW_TAG_class_type:
680b30c7 7587 case DW_TAG_interface_type:
c906108c 7588 case DW_TAG_structure_type:
72bf9492 7589 if (!pdi->is_declaration)
c906108c 7590 {
72bf9492 7591 add_partial_symbol (pdi, cu);
c906108c 7592 }
b7fee5a3
KS
7593 if ((cu->language == language_rust
7594 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7595 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7596 set_addrmap, cu);
c906108c 7597 break;
91c24f0a 7598 case DW_TAG_enumeration_type:
72bf9492
DJ
7599 if (!pdi->is_declaration)
7600 add_partial_enumeration (pdi, cu);
c906108c
SS
7601 break;
7602 case DW_TAG_base_type:
dda83cd7 7603 case DW_TAG_subrange_type:
c906108c 7604 /* File scope base type definitions are added to the partial
dda83cd7 7605 symbol table. */
72bf9492 7606 add_partial_symbol (pdi, cu);
c906108c 7607 break;
d9fa45fe 7608 case DW_TAG_namespace:
cdc07690 7609 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7610 break;
5d7cb8df 7611 case DW_TAG_module:
59c35742
AB
7612 if (!pdi->is_declaration)
7613 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7614 break;
95554aad
TT
7615 case DW_TAG_imported_unit:
7616 {
7617 struct dwarf2_per_cu_data *per_cu;
7618
f4dc4d17
DE
7619 /* For now we don't handle imported units in type units. */
7620 if (cu->per_cu->is_debug_types)
7621 {
7622 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7623 " supported in type units [in module %s]"),
5e22e966 7624 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
7625 }
7626
e3b94546 7627 per_cu = dwarf2_find_containing_comp_unit
5e22e966 7628 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
7629
7630 /* Go read the partial unit, if needed. */
7631 if (per_cu->v.psymtab == NULL)
ab432490
SM
7632 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7633 cu->language);
95554aad 7634
ae640021 7635 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7636 }
7637 break;
74921315
KS
7638 case DW_TAG_imported_declaration:
7639 add_partial_symbol (pdi, cu);
7640 break;
c906108c
SS
7641 default:
7642 break;
7643 }
7644 }
7645
72bf9492
DJ
7646 /* If the die has a sibling, skip to the sibling. */
7647
7648 pdi = pdi->die_sibling;
7649 }
7650}
7651
7652/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7653
72bf9492 7654 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7655 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7656 Enumerators are an exception; they use the scope of their parent
7657 enumeration type, i.e. the name of the enumeration type is not
7658 prepended to the enumerator.
91c24f0a 7659
72bf9492
DJ
7660 There are two complexities. One is DW_AT_specification; in this
7661 case "parent" means the parent of the target of the specification,
7662 instead of the direct parent of the DIE. The other is compilers
7663 which do not emit DW_TAG_namespace; in this case we try to guess
7664 the fully qualified name of structure types from their members'
7665 linkage names. This must be done using the DIE's children rather
7666 than the children of any DW_AT_specification target. We only need
7667 to do this for structures at the top level, i.e. if the target of
7668 any DW_AT_specification (if any; otherwise the DIE itself) does not
7669 have a parent. */
7670
7671/* Compute the scope prefix associated with PDI's parent, in
7672 compilation unit CU. The result will be allocated on CU's
7673 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7674 field. NULL is returned if no prefix is necessary. */
15d034d0 7675static const char *
72bf9492
DJ
7676partial_die_parent_scope (struct partial_die_info *pdi,
7677 struct dwarf2_cu *cu)
7678{
15d034d0 7679 const char *grandparent_scope;
72bf9492 7680 struct partial_die_info *parent, *real_pdi;
91c24f0a 7681
72bf9492
DJ
7682 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7683 then this means the parent of the specification DIE. */
7684
7685 real_pdi = pdi;
72bf9492 7686 while (real_pdi->has_specification)
fb816e8b 7687 {
122cf0f2
AB
7688 auto res = find_partial_die (real_pdi->spec_offset,
7689 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
7690 real_pdi = res.pdi;
7691 cu = res.cu;
7692 }
72bf9492
DJ
7693
7694 parent = real_pdi->die_parent;
7695 if (parent == NULL)
7696 return NULL;
7697
7698 if (parent->scope_set)
7699 return parent->scope;
7700
52356b79 7701 parent->fixup (cu);
72bf9492 7702
10b3939b 7703 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7704
acebe513
UW
7705 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7706 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7707 Work around this problem here. */
7708 if (cu->language == language_cplus
6e70227d 7709 && parent->tag == DW_TAG_namespace
7d00ffec 7710 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
7711 && grandparent_scope == NULL)
7712 {
7713 parent->scope = NULL;
7714 parent->scope_set = 1;
7715 return NULL;
7716 }
7717
0a4b0913 7718 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
7719 if (pdi->tag == DW_TAG_enumerator)
7720 /* Enumerators should not get the name of the enumeration as a prefix. */
7721 parent->scope = grandparent_scope;
7722 else if (parent->tag == DW_TAG_namespace
f55ee35c 7723 || parent->tag == DW_TAG_module
72bf9492
DJ
7724 || parent->tag == DW_TAG_structure_type
7725 || parent->tag == DW_TAG_class_type
680b30c7 7726 || parent->tag == DW_TAG_interface_type
ceeb3d5a 7727 || parent->tag == DW_TAG_union_type
0a4b0913
AB
7728 || parent->tag == DW_TAG_enumeration_type
7729 || (cu->language == language_fortran
7730 && parent->tag == DW_TAG_subprogram
7731 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
7732 {
7733 if (grandparent_scope == NULL)
7d00ffec 7734 parent->scope = parent->name (cu);
72bf9492 7735 else
3e43a32a
MS
7736 parent->scope = typename_concat (&cu->comp_unit_obstack,
7737 grandparent_scope,
7d00ffec 7738 parent->name (cu), 0, cu);
72bf9492 7739 }
72bf9492
DJ
7740 else
7741 {
7742 /* FIXME drow/2004-04-01: What should we be doing with
7743 function-local names? For partial symbols, we should probably be
7744 ignoring them. */
fa9c3fa0
TT
7745 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7746 dwarf_tag_name (parent->tag),
7747 sect_offset_str (pdi->sect_off));
72bf9492 7748 parent->scope = grandparent_scope;
c906108c
SS
7749 }
7750
72bf9492
DJ
7751 parent->scope_set = 1;
7752 return parent->scope;
7753}
7754
7755/* Return the fully scoped name associated with PDI, from compilation unit
7756 CU. The result will be allocated with malloc. */
4568ecf9 7757
43816ebc 7758static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
7759partial_die_full_name (struct partial_die_info *pdi,
7760 struct dwarf2_cu *cu)
7761{
15d034d0 7762 const char *parent_scope;
72bf9492 7763
98bfdba5
PA
7764 /* If this is a template instantiation, we can not work out the
7765 template arguments from partial DIEs. So, unfortunately, we have
7766 to go through the full DIEs. At least any work we do building
7767 types here will be reused if full symbols are loaded later. */
7768 if (pdi->has_template_arguments)
7769 {
52356b79 7770 pdi->fixup (cu);
98bfdba5 7771
7d00ffec 7772 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
7773 {
7774 struct die_info *die;
7775 struct attribute attr;
7776 struct dwarf2_cu *ref_cu = cu;
7777
b64f50a1 7778 /* DW_FORM_ref_addr is using section offset. */
b4069958 7779 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7780 attr.form = DW_FORM_ref_addr;
9c541725 7781 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7782 die = follow_die_ref (NULL, &attr, &ref_cu);
7783
43816ebc 7784 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
7785 }
7786 }
7787
72bf9492
DJ
7788 parent_scope = partial_die_parent_scope (pdi, cu);
7789 if (parent_scope == NULL)
7790 return NULL;
7791 else
43816ebc 7792 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
7793 pdi->name (cu),
7794 0, cu));
c906108c
SS
7795}
7796
7797static void
72bf9492 7798add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7799{
976ca316
SM
7800 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7801 struct objfile *objfile = per_objfile->objfile;
08feed99 7802 struct gdbarch *gdbarch = objfile->arch ();
c906108c 7803 CORE_ADDR addr = 0;
15d034d0 7804 const char *actual_name = NULL;
e142c38c
DJ
7805 CORE_ADDR baseaddr;
7806
b3b3bada 7807 baseaddr = objfile->text_section_offset ();
c906108c 7808
43816ebc
TT
7809 gdb::unique_xmalloc_ptr<char> built_actual_name
7810 = partial_die_full_name (pdi, cu);
15d034d0 7811 if (built_actual_name != NULL)
43816ebc 7812 actual_name = built_actual_name.get ();
63d06c5c 7813
72bf9492 7814 if (actual_name == NULL)
7d00ffec 7815 actual_name = pdi->name (cu);
72bf9492 7816
76e288d1
TT
7817 partial_symbol psymbol;
7818 memset (&psymbol, 0, sizeof (psymbol));
7819 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 7820 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
7821
7822 /* The code below indicates that the psymbol should be installed by
7823 setting this. */
7824 gdb::optional<psymbol_placement> where;
7825
c906108c
SS
7826 switch (pdi->tag)
7827 {
b1dc1806 7828 case DW_TAG_inlined_subroutine:
c906108c 7829 case DW_TAG_subprogram:
79748972
TT
7830 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7831 - baseaddr);
0a4b0913
AB
7832 if (pdi->is_external
7833 || cu->language == language_ada
7834 || (cu->language == language_fortran
7835 && pdi->die_parent != NULL
7836 && pdi->die_parent->tag == DW_TAG_subprogram))
7837 {
dda83cd7
SM
7838 /* Normally, only "external" DIEs are part of the global scope.
7839 But in Ada and Fortran, we want to be able to access nested
7840 procedures globally. So all Ada and Fortran subprograms are
7841 stored in the global scope. */
76e288d1 7842 where = psymbol_placement::GLOBAL;
c906108c
SS
7843 }
7844 else
76e288d1
TT
7845 where = psymbol_placement::STATIC;
7846
7847 psymbol.domain = VAR_DOMAIN;
7848 psymbol.aclass = LOC_BLOCK;
a52d653e 7849 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 7850 psymbol.ginfo.value.address = addr;
0c1b455e
TT
7851
7852 if (pdi->main_subprogram && actual_name != NULL)
7853 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7854 break;
72929c62 7855 case DW_TAG_constant:
76e288d1
TT
7856 psymbol.domain = VAR_DOMAIN;
7857 psymbol.aclass = LOC_STATIC;
7858 where = (pdi->is_external
7859 ? psymbol_placement::GLOBAL
7860 : psymbol_placement::STATIC);
72929c62 7861 break;
c906108c 7862 case DW_TAG_variable:
95554aad
TT
7863 if (pdi->d.locdesc)
7864 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7865
95554aad 7866 if (pdi->d.locdesc
caac4577 7867 && addr == 0
976ca316 7868 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
7869 {
7870 /* A global or static variable may also have been stripped
7871 out by the linker if unused, in which case its address
7872 will be nullified; do not add such variables into partial
7873 symbol table then. */
7874 }
7875 else if (pdi->is_external)
c906108c
SS
7876 {
7877 /* Global Variable.
7878 Don't enter into the minimal symbol tables as there is
7879 a minimal symbol table entry from the ELF symbols already.
7880 Enter into partial symbol table if it has a location
7881 descriptor or a type.
7882 If the location descriptor is missing, new_symbol will create
7883 a LOC_UNRESOLVED symbol, the address of the variable will then
7884 be determined from the minimal symbol table whenever the variable
7885 is referenced.
7886 The address for the partial symbol table entry is not
7887 used by GDB, but it comes in handy for debugging partial symbol
7888 table building. */
7889
95554aad 7890 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
7891 {
7892 psymbol.domain = VAR_DOMAIN;
7893 psymbol.aclass = LOC_STATIC;
a52d653e 7894 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
7895 psymbol.ginfo.value.address = addr;
7896 where = psymbol_placement::GLOBAL;
7897 }
c906108c
SS
7898 }
7899 else
7900 {
ff908ebf
AW
7901 int has_loc = pdi->d.locdesc != NULL;
7902
7903 /* Static Variable. Skip symbols whose value we cannot know (those
7904 without location descriptors or constant values). */
7905 if (!has_loc && !pdi->has_const_value)
43816ebc 7906 return;
ff908ebf 7907
76e288d1
TT
7908 psymbol.domain = VAR_DOMAIN;
7909 psymbol.aclass = LOC_STATIC;
a52d653e 7910 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
7911 if (has_loc)
7912 psymbol.ginfo.value.address = addr;
7913 where = psymbol_placement::STATIC;
c906108c
SS
7914 }
7915 break;
d8f62e84 7916 case DW_TAG_array_type:
c906108c
SS
7917 case DW_TAG_typedef:
7918 case DW_TAG_base_type:
a02abb62 7919 case DW_TAG_subrange_type:
76e288d1
TT
7920 psymbol.domain = VAR_DOMAIN;
7921 psymbol.aclass = LOC_TYPEDEF;
7922 where = psymbol_placement::STATIC;
c906108c 7923 break;
74921315 7924 case DW_TAG_imported_declaration:
72bf9492 7925 case DW_TAG_namespace:
76e288d1
TT
7926 psymbol.domain = VAR_DOMAIN;
7927 psymbol.aclass = LOC_TYPEDEF;
7928 where = psymbol_placement::GLOBAL;
72bf9492 7929 break;
530e8392 7930 case DW_TAG_module:
a5fd13a9 7931 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
7932 available without any name. If so, we skip the module as it
7933 doesn't bring any value. */
a5fd13a9 7934 if (actual_name != nullptr)
76e288d1
TT
7935 {
7936 psymbol.domain = MODULE_DOMAIN;
7937 psymbol.aclass = LOC_TYPEDEF;
7938 where = psymbol_placement::GLOBAL;
7939 }
530e8392 7940 break;
c906108c 7941 case DW_TAG_class_type:
680b30c7 7942 case DW_TAG_interface_type:
c906108c
SS
7943 case DW_TAG_structure_type:
7944 case DW_TAG_union_type:
7945 case DW_TAG_enumeration_type:
fa4028e9 7946 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
7947 about "Structure, Union, and Class Type Entries": "An incomplete
7948 structure, union or class type is represented by a structure,
7949 union or class entry that does not have a byte size attribute
7950 and that has a DW_AT_declaration attribute." */
fa4028e9 7951 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 7952 return;
fa4028e9 7953
63d06c5c
DC
7954 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7955 static vs. global. */
76e288d1
TT
7956 psymbol.domain = STRUCT_DOMAIN;
7957 psymbol.aclass = LOC_TYPEDEF;
7958 where = (cu->language == language_cplus
7959 ? psymbol_placement::GLOBAL
7960 : psymbol_placement::STATIC);
c906108c
SS
7961 break;
7962 case DW_TAG_enumerator:
76e288d1
TT
7963 psymbol.domain = VAR_DOMAIN;
7964 psymbol.aclass = LOC_CONST;
7965 where = (cu->language == language_cplus
7966 ? psymbol_placement::GLOBAL
7967 : psymbol_placement::STATIC);
c906108c
SS
7968 break;
7969 default:
7970 break;
7971 }
76e288d1
TT
7972
7973 if (where.has_value ())
7974 {
f049a313
TT
7975 if (built_actual_name != nullptr)
7976 actual_name = objfile->intern (actual_name);
bcfe6157
TT
7977 if (pdi->linkage_name == nullptr || cu->language == language_ada)
7978 psymbol.ginfo.set_linkage_name (actual_name);
7979 else
7980 {
7981 psymbol.ginfo.set_demangled_name (actual_name,
7982 &objfile->objfile_obstack);
7983 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
7984 }
7b249e47
TT
7985 cu->per_cu->v.psymtab->add_psymbol
7986 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
7987 objfile);
76e288d1 7988 }
c906108c
SS
7989}
7990
5c4e30ca
DC
7991/* Read a partial die corresponding to a namespace; also, add a symbol
7992 corresponding to that namespace to the symbol table. NAMESPACE is
7993 the name of the enclosing namespace. */
91c24f0a 7994
72bf9492
DJ
7995static void
7996add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 7997 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 7998 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 7999{
72bf9492 8000 /* Add a symbol for the namespace. */
e7c27a73 8001
72bf9492 8002 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8003
8004 /* Now scan partial symbols in that namespace. */
8005
91c24f0a 8006 if (pdi->has_children)
cdc07690 8007 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8008}
8009
5d7cb8df
JK
8010/* Read a partial die corresponding to a Fortran module. */
8011
8012static void
8013add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8014 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8015{
530e8392
KB
8016 /* Add a symbol for the namespace. */
8017
8018 add_partial_symbol (pdi, cu);
8019
f55ee35c 8020 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8021
8022 if (pdi->has_children)
cdc07690 8023 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8024}
8025
b1dc1806
XR
8026/* Read a partial die corresponding to a subprogram or an inlined
8027 subprogram and create a partial symbol for that subprogram.
8028 When the CU language allows it, this routine also defines a partial
8029 symbol for each nested subprogram that this subprogram contains.
8030 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8031 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8032
cdc07690
YQ
8033 PDI may also be a lexical block, in which case we simply search
8034 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8035 Again, this is only performed when the CU language allows this
8036 type of definitions. */
8037
8038static void
8039add_partial_subprogram (struct partial_die_info *pdi,
8040 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8041 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8042{
b1dc1806 8043 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8044 {
8045 if (pdi->has_pc_info)
dda83cd7
SM
8046 {
8047 if (pdi->lowpc < *lowpc)
8048 *lowpc = pdi->lowpc;
8049 if (pdi->highpc > *highpc)
8050 *highpc = pdi->highpc;
cdc07690 8051 if (set_addrmap)
5734ee8b 8052 {
5e22e966 8053 struct objfile *objfile = cu->per_objfile->objfile;
84685904 8054 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 8055 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8056 CORE_ADDR baseaddr;
b926417a
TT
8057 CORE_ADDR this_highpc;
8058 CORE_ADDR this_lowpc;
5734ee8b 8059
b3b3bada 8060 baseaddr = objfile->text_section_offset ();
b926417a
TT
8061 this_lowpc
8062 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8063 pdi->lowpc + baseaddr)
8064 - baseaddr);
8065 this_highpc
8066 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8067 pdi->highpc + baseaddr)
8068 - baseaddr);
84685904 8069 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
b926417a 8070 this_lowpc, this_highpc - 1,
9291a0cd 8071 cu->per_cu->v.psymtab);
5734ee8b 8072 }
dda83cd7 8073 }
481860b3
GB
8074
8075 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8076 {
dda83cd7 8077 if (!pdi->is_declaration)
e8d05480
JB
8078 /* Ignore subprogram DIEs that do not have a name, they are
8079 illegal. Do not emit a complaint at this point, we will
8080 do so when we convert this psymtab into a symtab. */
7d00ffec 8081 if (pdi->name (cu))
e8d05480 8082 add_partial_symbol (pdi, cu);
dda83cd7 8083 }
bc30ff58 8084 }
6e70227d 8085
bc30ff58
JB
8086 if (! pdi->has_children)
8087 return;
8088
0a4b0913 8089 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8090 {
8091 pdi = pdi->die_child;
8092 while (pdi != NULL)
8093 {
52356b79 8094 pdi->fixup (cu);
bc30ff58 8095 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8096 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8097 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8098 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8099 pdi = pdi->die_sibling;
8100 }
8101 }
8102}
8103
91c24f0a
DC
8104/* Read a partial die corresponding to an enumeration type. */
8105
72bf9492
DJ
8106static void
8107add_partial_enumeration (struct partial_die_info *enum_pdi,
8108 struct dwarf2_cu *cu)
91c24f0a 8109{
72bf9492 8110 struct partial_die_info *pdi;
91c24f0a 8111
7d00ffec 8112 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8113 add_partial_symbol (enum_pdi, cu);
8114
8115 pdi = enum_pdi->die_child;
8116 while (pdi)
91c24f0a 8117 {
7d00ffec 8118 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8119 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8120 else
72bf9492
DJ
8121 add_partial_symbol (pdi, cu);
8122 pdi = pdi->die_sibling;
91c24f0a 8123 }
91c24f0a
DC
8124}
8125
6caca83c
CC
8126/* Return the initial uleb128 in the die at INFO_PTR. */
8127
8128static unsigned int
d521ce57 8129peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8130{
8131 unsigned int bytes_read;
8132
8133 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8134}
8135
685af9cd
TT
8136/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8137 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8138
4bb7a0a7
DJ
8139 Return the corresponding abbrev, or NULL if the number is zero (indicating
8140 an empty DIE). In either case *BYTES_READ will be set to the length of
8141 the initial number. */
8142
7c32eebb 8143static const struct abbrev_info *
685af9cd
TT
8144peek_die_abbrev (const die_reader_specs &reader,
8145 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8146{
685af9cd 8147 dwarf2_cu *cu = reader.cu;
0280fdcc 8148 bfd *abfd = reader.abfd;
685af9cd
TT
8149 unsigned int abbrev_number
8150 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8151
8152 if (abbrev_number == 0)
8153 return NULL;
8154
7c32eebb
TT
8155 const abbrev_info *abbrev
8156 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8157 if (!abbrev)
8158 {
422b9917 8159 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8160 " at offset %s [in module %s]"),
422b9917 8161 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8162 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8163 }
8164
8165 return abbrev;
8166}
8167
93311388
DE
8168/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8169 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8170 DIE. Any children of the skipped DIEs will also be skipped. */
8171
d521ce57
TT
8172static const gdb_byte *
8173skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8174{
4bb7a0a7
DJ
8175 while (1)
8176 {
685af9cd 8177 unsigned int bytes_read;
7c32eebb
TT
8178 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8179 &bytes_read);
685af9cd 8180
4bb7a0a7
DJ
8181 if (abbrev == NULL)
8182 return info_ptr + bytes_read;
8183 else
dee91e82 8184 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8185 }
8186}
8187
93311388
DE
8188/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8189 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8190 abbrev corresponding to that skipped uleb128 should be passed in
8191 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8192 children. */
8193
d521ce57
TT
8194static const gdb_byte *
8195skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8196 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8197{
8198 unsigned int bytes_read;
8199 struct attribute attr;
dee91e82
DE
8200 bfd *abfd = reader->abfd;
8201 struct dwarf2_cu *cu = reader->cu;
d521ce57 8202 const gdb_byte *buffer = reader->buffer;
f664829e 8203 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8204 unsigned int form, i;
8205
8206 for (i = 0; i < abbrev->num_attrs; i++)
8207 {
8208 /* The only abbrev we care about is DW_AT_sibling. */
8209 if (abbrev->attrs[i].name == DW_AT_sibling)
8210 {
7a5f294d 8211 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8212 if (attr.form == DW_FORM_ref_addr)
b98664d3 8213 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8214 else
b9502d3f 8215 {
0826b30a 8216 sect_offset off = attr.get_ref_die_offset ();
9c541725 8217 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8218
8219 if (sibling_ptr < info_ptr)
b98664d3 8220 complaint (_("DW_AT_sibling points backwards"));
22869d73 8221 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8222 reader->die_section->overflow_complaint ();
b9502d3f
WN
8223 else
8224 return sibling_ptr;
8225 }
4bb7a0a7
DJ
8226 }
8227
8228 /* If it isn't DW_AT_sibling, skip this attribute. */
8229 form = abbrev->attrs[i].form;
8230 skip_attribute:
8231 switch (form)
8232 {
4bb7a0a7 8233 case DW_FORM_ref_addr:
ae411497
TT
8234 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8235 and later it is offset sized. */
8236 if (cu->header.version == 2)
8237 info_ptr += cu->header.addr_size;
8238 else
8239 info_ptr += cu->header.offset_size;
8240 break;
36586728
TT
8241 case DW_FORM_GNU_ref_alt:
8242 info_ptr += cu->header.offset_size;
8243 break;
ae411497 8244 case DW_FORM_addr:
4bb7a0a7
DJ
8245 info_ptr += cu->header.addr_size;
8246 break;
8247 case DW_FORM_data1:
8248 case DW_FORM_ref1:
8249 case DW_FORM_flag:
8fe0f950 8250 case DW_FORM_strx1:
4bb7a0a7
DJ
8251 info_ptr += 1;
8252 break;
2dc7f7b3 8253 case DW_FORM_flag_present:
43988095 8254 case DW_FORM_implicit_const:
2dc7f7b3 8255 break;
4bb7a0a7
DJ
8256 case DW_FORM_data2:
8257 case DW_FORM_ref2:
8fe0f950 8258 case DW_FORM_strx2:
4bb7a0a7
DJ
8259 info_ptr += 2;
8260 break;
8fe0f950
AT
8261 case DW_FORM_strx3:
8262 info_ptr += 3;
8263 break;
4bb7a0a7
DJ
8264 case DW_FORM_data4:
8265 case DW_FORM_ref4:
8fe0f950 8266 case DW_FORM_strx4:
4bb7a0a7
DJ
8267 info_ptr += 4;
8268 break;
8269 case DW_FORM_data8:
8270 case DW_FORM_ref8:
55f1336d 8271 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8272 info_ptr += 8;
8273 break;
0224619f
JK
8274 case DW_FORM_data16:
8275 info_ptr += 16;
8276 break;
4bb7a0a7 8277 case DW_FORM_string:
9b1c24c8 8278 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8279 info_ptr += bytes_read;
8280 break;
2dc7f7b3 8281 case DW_FORM_sec_offset:
4bb7a0a7 8282 case DW_FORM_strp:
36586728 8283 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8284 info_ptr += cu->header.offset_size;
8285 break;
2dc7f7b3 8286 case DW_FORM_exprloc:
4bb7a0a7
DJ
8287 case DW_FORM_block:
8288 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8289 info_ptr += bytes_read;
8290 break;
8291 case DW_FORM_block1:
8292 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8293 break;
8294 case DW_FORM_block2:
8295 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8296 break;
8297 case DW_FORM_block4:
8298 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8299 break;
336d760d 8300 case DW_FORM_addrx:
cf532bd1 8301 case DW_FORM_strx:
4bb7a0a7
DJ
8302 case DW_FORM_sdata:
8303 case DW_FORM_udata:
8304 case DW_FORM_ref_udata:
3019eac3
DE
8305 case DW_FORM_GNU_addr_index:
8306 case DW_FORM_GNU_str_index:
18a8505e 8307 case DW_FORM_rnglistx:
41144253 8308 case DW_FORM_loclistx:
d521ce57 8309 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8310 break;
8311 case DW_FORM_indirect:
8312 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8313 info_ptr += bytes_read;
8314 /* We need to continue parsing from here, so just go back to
8315 the top. */
8316 goto skip_attribute;
8317
8318 default:
3e43a32a
MS
8319 error (_("Dwarf Error: Cannot handle %s "
8320 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8321 dwarf_form_name (form),
8322 bfd_get_filename (abfd));
8323 }
8324 }
8325
8326 if (abbrev->has_children)
dee91e82 8327 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8328 else
8329 return info_ptr;
8330}
8331
93311388 8332/* Locate ORIG_PDI's sibling.
dee91e82 8333 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8334
d521ce57 8335static const gdb_byte *
dee91e82
DE
8336locate_pdi_sibling (const struct die_reader_specs *reader,
8337 struct partial_die_info *orig_pdi,
d521ce57 8338 const gdb_byte *info_ptr)
91c24f0a
DC
8339{
8340 /* Do we know the sibling already? */
72bf9492 8341
91c24f0a
DC
8342 if (orig_pdi->sibling)
8343 return orig_pdi->sibling;
8344
8345 /* Are there any children to deal with? */
8346
8347 if (!orig_pdi->has_children)
8348 return info_ptr;
8349
4bb7a0a7 8350 /* Skip the children the long way. */
91c24f0a 8351
dee91e82 8352 return skip_children (reader, info_ptr);
91c24f0a
DC
8353}
8354
257e7a09 8355/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8356 not NULL. */
c906108c 8357
891813be
TT
8358void
8359dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8360{
976ca316 8361 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8362
976ca316 8363 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8364
077cbab2
TT
8365 /* If this psymtab is constructed from a debug-only objfile, the
8366 has_section_at_zero flag will not necessarily be correct. We
8367 can get the correct value for this flag by looking at the data
8368 associated with the (presumably stripped) associated objfile. */
8369 if (objfile->separate_debug_objfile_backlink)
c906108c 8370 {
976ca316 8371 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8372 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8373
976ca316
SM
8374 per_objfile->per_bfd->has_section_at_zero
8375 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8376 }
98bfdba5 8377
8566b89b 8378 expand_psymtab (objfile);
95554aad 8379
976ca316 8380 process_cu_includes (per_objfile);
c906108c 8381}
9cdd5dbd
DE
8382\f
8383/* Reading in full CUs. */
c906108c 8384
10b3939b
DJ
8385/* Add PER_CU to the queue. */
8386
8387static void
120ce1b5
SM
8388queue_comp_unit (dwarf2_per_cu_data *per_cu,
8389 dwarf2_per_objfile *per_objfile,
95554aad 8390 enum language pretend_language)
10b3939b 8391{
10b3939b 8392 per_cu->queued = 1;
08ac5771
SM
8393
8394 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8395 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8396}
8397
616c069a
SM
8398/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8399
89e63ee4
DE
8400 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8401 dependency.
69d751e3 8402
616c069a
SM
8403 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8404 DIEs, false otherwise.
8405
8406 Explanation: there is an invariant that if a CU is queued for expansion
8407 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8408 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8409 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8410 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8411 invariant is respected.
8412
8413 The caller is therefore not required to load the CU's DIEs (we return false)
8414 if:
8415
8416 - the CU is already expanded, and therefore does not get enqueued
8417 - the CU gets enqueued for expansion, but its DIEs are already loaded
8418
8419 Note that the caller should not use this function's return value as an
8420 indicator of whether the CU's DIEs are loaded right now, it should check
8421 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
8422
8423static int
89e63ee4 8424maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8425 dwarf2_per_cu_data *per_cu,
8426 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8427 enum language pretend_language)
8428{
8429 /* We may arrive here during partial symbol reading, if we need full
8430 DIEs to process an unusual case (e.g. template arguments). Do
8431 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8432 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 8433 {
7188ed02
SM
8434 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8435
8436 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
8437 return 1;
8438 return 0;
8439 }
8440
8441 /* Mark the dependence relation so that we don't flush PER_CU
8442 too early. */
89e63ee4 8443 if (dependent_cu != NULL)
347212b8 8444 dependent_cu->add_dependence (per_cu);
0907af0c
DE
8445
8446 /* If it's already on the queue, we have nothing to do. */
8447 if (per_cu->queued)
de53369b
SM
8448 {
8449 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8450 loaded. */
8451 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
8452
8453 /* If the CU is queued for expansion, it should not already be
8454 expanded. */
8455 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8456
8457 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
8458 return 0;
8459 }
0907af0c 8460
616c069a
SM
8461 bool queued = false;
8462 if (!per_objfile->symtab_set_p (per_cu))
8463 {
8464 /* Add it to the queue. */
8465 queue_comp_unit (per_cu, per_objfile, pretend_language);
8466 queued = true;
8467 }
8468
0907af0c
DE
8469 /* If the compilation unit is already loaded, just mark it as
8470 used. */
7188ed02
SM
8471 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8472 if (cu != nullptr)
616c069a 8473 cu->last_used = 0;
0907af0c 8474
616c069a
SM
8475 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8476 and the DIEs are not already loaded. */
8477 return queued && cu == nullptr;
0907af0c
DE
8478}
8479
10b3939b
DJ
8480/* Process the queue. */
8481
8482static void
976ca316 8483process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 8484{
6f738b01
SM
8485 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8486 objfile_name (per_objfile->objfile));
45cfd468 8487
03dd20cc
DJ
8488 /* The queue starts out with one item, but following a DIE reference
8489 may load a new CU, adding it to the end of the queue. */
08ac5771 8490 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 8491 {
08ac5771 8492 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 8493 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 8494
976ca316 8495 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 8496 {
976ca316 8497 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 8498
7188ed02
SM
8499 /* Skip dummy CUs. */
8500 if (cu != nullptr)
73be47f5 8501 {
7188ed02
SM
8502 unsigned int debug_print_threshold;
8503 char buf[100];
8504
8505 if (per_cu->is_debug_types)
8506 {
8507 struct signatured_type *sig_type =
8508 (struct signatured_type *) per_cu;
8509
8510 sprintf (buf, "TU %s at offset %s",
8511 hex_string (sig_type->signature),
8512 sect_offset_str (per_cu->sect_off));
8513 /* There can be 100s of TUs.
8514 Only print them in verbose mode. */
8515 debug_print_threshold = 2;
8516 }
8517 else
8518 {
8519 sprintf (buf, "CU at offset %s",
8520 sect_offset_str (per_cu->sect_off));
8521 debug_print_threshold = 1;
8522 }
247f5c4f 8523
7188ed02 8524 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8525 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 8526
7188ed02
SM
8527 if (per_cu->is_debug_types)
8528 process_full_type_unit (cu, item.pretend_language);
8529 else
8530 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 8531
7188ed02 8532 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8533 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 8534 }
f4dc4d17 8535 }
10b3939b 8536
7188ed02 8537 per_cu->queued = 0;
08ac5771 8538 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
8539 }
8540
6f738b01
SM
8541 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8542 objfile_name (per_objfile->objfile));
10b3939b
DJ
8543}
8544
10b3939b
DJ
8545/* Read in full symbols for PST, and anything it depends on. */
8546
8566b89b
TT
8547void
8548dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8549{
af758d11 8550 gdb_assert (!readin_p (objfile));
95554aad 8551
17ee85fc
TT
8552 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8553 free_cached_comp_units freer (per_objfile);
48993951 8554 expand_dependencies (objfile);
aaa75496 8555
97a1449a 8556 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 8557 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
8558}
8559
af758d11
SM
8560/* See psympriv.h. */
8561
8562bool
8563dwarf2_psymtab::readin_p (struct objfile *objfile) const
8564{
8565 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8566 return per_objfile->symtab_set_p (per_cu_data);
8567}
8568
8569/* See psympriv.h. */
8570
8571compunit_symtab *
8572dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8573{
8574 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8575 return per_objfile->get_symtab (per_cu_data);
8576}
8577
dee91e82
DE
8578/* Trivial hash function for die_info: the hash value of a DIE
8579 is its offset in .debug_info for this objfile. */
10b3939b 8580
dee91e82
DE
8581static hashval_t
8582die_hash (const void *item)
10b3939b 8583{
9a3c8263 8584 const struct die_info *die = (const struct die_info *) item;
6502dd73 8585
9c541725 8586 return to_underlying (die->sect_off);
dee91e82 8587}
63d06c5c 8588
dee91e82
DE
8589/* Trivial comparison function for die_info structures: two DIEs
8590 are equal if they have the same offset. */
98bfdba5 8591
dee91e82
DE
8592static int
8593die_eq (const void *item_lhs, const void *item_rhs)
8594{
9a3c8263
SM
8595 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8596 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8597
9c541725 8598 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8599}
c906108c 8600
4a636814
SM
8601/* Load the DIEs associated with PER_CU into memory.
8602
8603 In some cases, the caller, while reading partial symbols, will need to load
8604 the full symbols for the CU for some reason. It will already have a
8605 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8606 rather than creating a new one. */
c906108c 8607
dee91e82 8608static void
ab432490
SM
8609load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8610 dwarf2_per_objfile *per_objfile,
4a636814 8611 dwarf2_cu *existing_cu,
c0ab21c2
TT
8612 bool skip_partial,
8613 enum language pretend_language)
dee91e82 8614{
c0ab21c2
TT
8615 gdb_assert (! this_cu->is_debug_types);
8616
7188ed02 8617 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
8618 if (reader.dummy_p)
8619 return;
8620
8621 struct dwarf2_cu *cu = reader.cu;
8622 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8623
dee91e82
DE
8624 gdb_assert (cu->die_hash == NULL);
8625 cu->die_hash =
8626 htab_create_alloc_ex (cu->header.length / 12,
8627 die_hash,
8628 die_eq,
8629 NULL,
8630 &cu->comp_unit_obstack,
8631 hashtab_obstack_allocate,
8632 dummy_obstack_deallocate);
e142c38c 8633
3e225074 8634 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8635 reader.comp_unit_die->child
8636 = read_die_and_siblings (&reader, reader.info_ptr,
8637 &info_ptr, reader.comp_unit_die);
8638 cu->dies = reader.comp_unit_die;
dee91e82 8639 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8640
8641 /* We try not to read any attributes in this function, because not
9cdd5dbd 8642 all CUs needed for references have been loaded yet, and symbol
10b3939b 8643 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8644 or we won't be able to build types correctly.
8645 Similarly, if we do not read the producer, we can not apply
8646 producer-specific interpretation. */
c0ab21c2 8647 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8648
8649 reader.keep ();
10b3939b
DJ
8650}
8651
3da10d80
KS
8652/* Add a DIE to the delayed physname list. */
8653
8654static void
8655add_to_method_list (struct type *type, int fnfield_index, int index,
8656 const char *name, struct die_info *die,
8657 struct dwarf2_cu *cu)
8658{
8659 struct delayed_method_info mi;
8660 mi.type = type;
8661 mi.fnfield_index = fnfield_index;
8662 mi.index = index;
8663 mi.name = name;
8664 mi.die = die;
c89b44cd 8665 cu->method_list.push_back (mi);
3da10d80
KS
8666}
8667
3693fdb3
PA
8668/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8669 "const" / "volatile". If so, decrements LEN by the length of the
8670 modifier and return true. Otherwise return false. */
8671
8672template<size_t N>
8673static bool
8674check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8675{
8676 size_t mod_len = sizeof (mod) - 1;
8677 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8678 {
8679 len -= mod_len;
8680 return true;
8681 }
8682 return false;
8683}
8684
3da10d80
KS
8685/* Compute the physnames of any methods on the CU's method list.
8686
8687 The computation of method physnames is delayed in order to avoid the
8688 (bad) condition that one of the method's formal parameters is of an as yet
8689 incomplete type. */
8690
8691static void
8692compute_delayed_physnames (struct dwarf2_cu *cu)
8693{
3693fdb3 8694 /* Only C++ delays computing physnames. */
c89b44cd 8695 if (cu->method_list.empty ())
3693fdb3
PA
8696 return;
8697 gdb_assert (cu->language == language_cplus);
8698
52941706 8699 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8700 {
1d06ead6 8701 const char *physname;
3da10d80 8702 struct fn_fieldlist *fn_flp
c89b44cd
TT
8703 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8704 physname = dwarf2_physname (mi.name, mi.die, cu);
8705 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8706 = physname ? physname : "";
3693fdb3
PA
8707
8708 /* Since there's no tag to indicate whether a method is a
8709 const/volatile overload, extract that information out of the
8710 demangled name. */
8711 if (physname != NULL)
8712 {
8713 size_t len = strlen (physname);
8714
8715 while (1)
8716 {
8717 if (physname[len] == ')') /* shortcut */
8718 break;
8719 else if (check_modifier (physname, len, " const"))
c89b44cd 8720 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 8721 else if (check_modifier (physname, len, " volatile"))
c89b44cd 8722 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
8723 else
8724 break;
8725 }
8726 }
3da10d80 8727 }
c89b44cd
TT
8728
8729 /* The list is no longer needed. */
8730 cu->method_list.clear ();
3da10d80
KS
8731}
8732
a766d390
DE
8733/* Go objects should be embedded in a DW_TAG_module DIE,
8734 and it's not clear if/how imported objects will appear.
8735 To keep Go support simple until that's worked out,
8736 go back through what we've read and create something usable.
8737 We could do this while processing each DIE, and feels kinda cleaner,
8738 but that way is more invasive.
8739 This is to, for example, allow the user to type "p var" or "b main"
8740 without having to specify the package name, and allow lookups
8741 of module.object to work in contexts that use the expression
8742 parser. */
8743
8744static void
8745fixup_go_packaging (struct dwarf2_cu *cu)
8746{
421d1616 8747 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
8748 struct pending *list;
8749 int i;
8750
c24bdb02 8751 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
8752 list != NULL;
8753 list = list->next)
a766d390
DE
8754 {
8755 for (i = 0; i < list->nsyms; ++i)
8756 {
8757 struct symbol *sym = list->symbol[i];
8758
c1b5c1eb 8759 if (sym->language () == language_go
a766d390
DE
8760 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8761 {
421d1616
TT
8762 gdb::unique_xmalloc_ptr<char> this_package_name
8763 (go_symbol_package_name (sym));
a766d390
DE
8764
8765 if (this_package_name == NULL)
8766 continue;
8767 if (package_name == NULL)
421d1616 8768 package_name = std::move (this_package_name);
a766d390
DE
8769 else
8770 {
5e22e966 8771 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 8772 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 8773 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8774 (symbol_symtab (sym) != NULL
8775 ? symtab_to_filename_for_display
8776 (symbol_symtab (sym))
e3b94546 8777 : objfile_name (objfile)),
421d1616 8778 this_package_name.get (), package_name.get ());
a766d390
DE
8779 }
8780 }
8781 }
8782 }
8783
8784 if (package_name != NULL)
8785 {
5e22e966 8786 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 8787 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
8788 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8789 saved_package_name);
a766d390
DE
8790 struct symbol *sym;
8791
8c14c3a3 8792 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 8793 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 8794 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
8795 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8796 e.g., "main" finds the "main" module and not C's main(). */
8797 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8798 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8799 SYMBOL_TYPE (sym) = type;
8800
c24bdb02 8801 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
8802 }
8803}
8804
c9317f21
TT
8805/* Allocate a fully-qualified name consisting of the two parts on the
8806 obstack. */
8807
8808static const char *
8809rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8810{
8811 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8812}
8813
9c6a1327
TT
8814/* A helper that allocates a variant part to attach to a Rust enum
8815 type. OBSTACK is where the results should be allocated. TYPE is
8816 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
8817 discriminant. It must be the index of one of the fields of TYPE,
8818 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
8819 DEFAULT_INDEX is the index of the default field; or -1 if there is
8820 no default. RANGES is indexed by "effective" field number (the
8821 field index, but omitting the discriminant and default fields) and
8822 must hold the discriminant values used by the variants. Note that
8823 RANGES must have a lifetime at least as long as OBSTACK -- either
8824 already allocated on it, or static. */
c9317f21 8825
9c6a1327
TT
8826static void
8827alloc_rust_variant (struct obstack *obstack, struct type *type,
8828 int discriminant_index, int default_index,
8829 gdb::array_view<discriminant_range> ranges)
8830{
57d02173
TT
8831 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8832 gdb_assert (discriminant_index == -1
8833 || (discriminant_index >= 0
8834 && discriminant_index < type->num_fields ()));
c9317f21 8835 gdb_assert (default_index == -1
1f704f76 8836 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 8837
9c6a1327 8838 /* We have one variant for each non-discriminant field. */
57d02173
TT
8839 int n_variants = type->num_fields ();
8840 if (discriminant_index != -1)
8841 --n_variants;
c9317f21 8842
9c6a1327
TT
8843 variant *variants = new (obstack) variant[n_variants];
8844 int var_idx = 0;
8845 int range_idx = 0;
1f704f76 8846 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
8847 {
8848 if (i == discriminant_index)
8849 continue;
c9317f21 8850
9c6a1327
TT
8851 variants[var_idx].first_field = i;
8852 variants[var_idx].last_field = i + 1;
8853
8854 /* The default field does not need a range, but other fields do.
8855 We skipped the discriminant above. */
8856 if (i != default_index)
8857 {
8858 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8859 ++range_idx;
8860 }
c9317f21 8861
9c6a1327
TT
8862 ++var_idx;
8863 }
8864
8865 gdb_assert (range_idx == ranges.size ());
8866 gdb_assert (var_idx == n_variants);
8867
8868 variant_part *part = new (obstack) variant_part;
8869 part->discriminant_index = discriminant_index;
57d02173
TT
8870 /* If there is no discriminant, then whether it is signed is of no
8871 consequence. */
8872 part->is_unsigned
8873 = (discriminant_index == -1
8874 ? false
c6d940a9 8875 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
8876 part->variants = gdb::array_view<variant> (variants, n_variants);
8877
8878 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
8879 gdb::array_view<variant_part> *prop_value
8880 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 8881
9c6a1327 8882 struct dynamic_prop prop;
8c2e4e06 8883 prop.set_variant_parts (prop_value);
9c6a1327 8884
5c54719c 8885 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
8886}
8887
8888/* Some versions of rustc emitted enums in an unusual way.
8889
8890 Ordinary enums were emitted as unions. The first element of each
8891 structure in the union was named "RUST$ENUM$DISR". This element
8892 held the discriminant.
8893
8894 These versions of Rust also implemented the "non-zero"
8895 optimization. When the enum had two values, and one is empty and
8896 the other holds a pointer that cannot be zero, the pointer is used
8897 as the discriminant, with a zero value meaning the empty variant.
8898 Here, the union's first member is of the form
8899 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
8900 where the fieldnos are the indices of the fields that should be
8901 traversed in order to find the field (which may be several fields deep)
8902 and the variantname is the name of the variant of the case when the
8903 field is zero.
8904
8905 This function recognizes whether TYPE is of one of these forms,
8906 and, if so, smashes it to be a variant type. */
8907
8908static void
8909quirk_rust_enum (struct type *type, struct objfile *objfile)
8910{
78134374 8911 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
8912
8913 /* We don't need to deal with empty enums. */
1f704f76 8914 if (type->num_fields () == 0)
c9317f21
TT
8915 return;
8916
8917#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 8918 if (type->num_fields () == 1
c9317f21
TT
8919 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
8920 {
8921 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
8922
8923 /* Decode the field name to find the offset of the
8924 discriminant. */
8925 ULONGEST bit_offset = 0;
940da03e 8926 struct type *field_type = type->field (0).type ();
c9317f21
TT
8927 while (name[0] >= '0' && name[0] <= '9')
8928 {
8929 char *tail;
8930 unsigned long index = strtoul (name, &tail, 10);
8931 name = tail;
8932 if (*name != '$'
1f704f76 8933 || index >= field_type->num_fields ()
c9317f21
TT
8934 || (TYPE_FIELD_LOC_KIND (field_type, index)
8935 != FIELD_LOC_KIND_BITPOS))
8936 {
b98664d3 8937 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
8938 "[in module %s]"),
8939 TYPE_FIELD_NAME (type, 0),
8940 objfile_name (objfile));
8941 return;
8942 }
8943 ++name;
8944
8945 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 8946 field_type = field_type->field (index).type ();
c9317f21
TT
8947 }
8948
9c6a1327
TT
8949 /* Smash this type to be a structure type. We have to do this
8950 because the type has already been recorded. */
67607e24 8951 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 8952 type->set_num_fields (3);
9c6a1327 8953 /* Save the field we care about. */
ceacbf6e 8954 struct field saved_field = type->field (0);
3cabb6b0
SM
8955 type->set_fields
8956 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 8957
9c6a1327 8958 /* Put the discriminant at index 0. */
5d14b6e5 8959 type->field (0).set_type (field_type);
9c6a1327
TT
8960 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8961 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 8962 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
8963
8964 /* The order of fields doesn't really matter, so put the real
8965 field at index 1 and the data-less field at index 2. */
ceacbf6e 8966 type->field (1) = saved_field;
9c6a1327 8967 TYPE_FIELD_NAME (type, 1)
940da03e
SM
8968 = rust_last_path_segment (type->field (1).type ()->name ());
8969 type->field (1).type ()->set_name
7d93a1e0 8970 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 8971 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
8972
8973 const char *dataless_name
7d93a1e0 8974 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
8975 name);
8976 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8977 dataless_name);
5d14b6e5 8978 type->field (2).set_type (dataless_type);
c9317f21
TT
8979 /* NAME points into the original discriminant name, which
8980 already has the correct lifetime. */
9c6a1327 8981 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 8982 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 8983
9c6a1327
TT
8984 /* Indicate that this is a variant type. */
8985 static discriminant_range ranges[1] = { { 0, 0 } };
8986 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 8987 }
77c2dba3
TT
8988 /* A union with a single anonymous field is probably an old-style
8989 univariant enum. */
1f704f76 8990 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 8991 {
c9317f21
TT
8992 /* Smash this type to be a structure type. We have to do this
8993 because the type has already been recorded. */
67607e24 8994 type->set_code (TYPE_CODE_STRUCT);
c9317f21 8995
940da03e 8996 struct type *field_type = type->field (0).type ();
c9317f21 8997 const char *variant_name
7d93a1e0 8998 = rust_last_path_segment (field_type->name ());
9c6a1327 8999 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9000 field_type->set_name
9001 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9002 type->name (), variant_name));
57d02173
TT
9003
9004 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9005 }
9006 else
9007 {
9008 struct type *disr_type = nullptr;
1f704f76 9009 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9010 {
940da03e 9011 disr_type = type->field (i).type ();
c9317f21 9012
78134374 9013 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9014 {
9015 /* All fields of a true enum will be structs. */
9016 return;
9017 }
1f704f76 9018 else if (disr_type->num_fields () == 0)
c9317f21
TT
9019 {
9020 /* Could be data-less variant, so keep going. */
a037790e 9021 disr_type = nullptr;
c9317f21
TT
9022 }
9023 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9024 "RUST$ENUM$DISR") != 0)
9025 {
9026 /* Not a Rust enum. */
9027 return;
9028 }
9029 else
9030 {
9031 /* Found one. */
9032 break;
9033 }
9034 }
9035
9036 /* If we got here without a discriminant, then it's probably
9037 just a union. */
9038 if (disr_type == nullptr)
9039 return;
9040
9041 /* Smash this type to be a structure type. We have to do this
9042 because the type has already been recorded. */
67607e24 9043 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9044
9c6a1327 9045 /* Make space for the discriminant field. */
ceacbf6e 9046 struct field *disr_field = &disr_type->field (0);
9c6a1327 9047 field *new_fields
1f704f76 9048 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9049 * sizeof (struct field)));
80fc5e77 9050 memcpy (new_fields + 1, type->fields (),
1f704f76 9051 type->num_fields () * sizeof (struct field));
3cabb6b0 9052 type->set_fields (new_fields);
1f704f76 9053 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9054
9055 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9056 type->field (0) = *disr_field;
9c6a1327
TT
9057 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9058 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9059
9060 /* We need a way to find the correct discriminant given a
9061 variant name. For convenience we build a map here. */
b6cdac4b 9062 struct type *enum_type = disr_field->type ();
c9317f21 9063 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9064 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9065 {
9066 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9067 {
9068 const char *name
9069 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9070 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9071 }
9072 }
9073
1f704f76 9074 int n_fields = type->num_fields ();
9c6a1327
TT
9075 /* We don't need a range entry for the discriminant, but we do
9076 need one for every other field, as there is no default
9077 variant. */
9078 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9079 discriminant_range,
9080 n_fields - 1);
c9317f21
TT
9081 /* Skip the discriminant here. */
9082 for (int i = 1; i < n_fields; ++i)
9083 {
9084 /* Find the final word in the name of this variant's type.
9085 That name can be used to look up the correct
9086 discriminant. */
9087 const char *variant_name
940da03e 9088 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9089
9090 auto iter = discriminant_map.find (variant_name);
9091 if (iter != discriminant_map.end ())
9c6a1327 9092 {
57d02173
TT
9093 ranges[i - 1].low = iter->second;
9094 ranges[i - 1].high = iter->second;
9c6a1327 9095 }
c9317f21 9096
57d02173
TT
9097 /* In Rust, each element should have the size of the
9098 enclosing enum. */
9099 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9100
bedda9ac 9101 /* Remove the discriminant field, if it exists. */
940da03e 9102 struct type *sub_type = type->field (i).type ();
1f704f76 9103 if (sub_type->num_fields () > 0)
bedda9ac 9104 {
5e33d5f4 9105 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9106 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9107 }
9c6a1327 9108 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9109 sub_type->set_name
9110 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9111 type->name (), variant_name));
c9317f21 9112 }
9c6a1327
TT
9113
9114 /* Indicate that this is a variant type. */
a1520ad8 9115 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9116 gdb::array_view<discriminant_range> (ranges,
9117 n_fields - 1));
c9317f21
TT
9118 }
9119}
9120
9121/* Rewrite some Rust unions to be structures with variants parts. */
9122
9123static void
9124rust_union_quirks (struct dwarf2_cu *cu)
9125{
9126 gdb_assert (cu->language == language_rust);
52941706 9127 for (type *type_ : cu->rust_unions)
5e22e966 9128 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9129 /* We don't need this any more. */
9130 cu->rust_unions.clear ();
c9317f21
TT
9131}
9132
8adb8487
TT
9133/* See read.h. */
9134
9135type_unit_group_unshareable *
9136dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9137{
9138 auto iter = this->m_type_units.find (tu_group);
9139 if (iter != this->m_type_units.end ())
9140 return iter->second.get ();
9141
9142 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9143 type_unit_group_unshareable *result = uniq.get ();
9144 this->m_type_units[tu_group] = std::move (uniq);
9145 return result;
9146}
9147
e286671b
TT
9148struct type *
9149dwarf2_per_objfile::get_type_for_signatured_type
9150 (signatured_type *sig_type) const
9151{
9152 auto iter = this->m_type_map.find (sig_type);
9153 if (iter == this->m_type_map.end ())
9154 return nullptr;
9155
9156 return iter->second;
9157}
9158
9159void dwarf2_per_objfile::set_type_for_signatured_type
9160 (signatured_type *sig_type, struct type *type)
9161{
9162 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9163
9164 this->m_type_map[sig_type] = type;
9165}
9166
95554aad
TT
9167/* A helper function for computing the list of all symbol tables
9168 included by PER_CU. */
9169
9170static void
4c39bc03 9171recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9172 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9173 dwarf2_per_cu_data *per_cu,
9174 dwarf2_per_objfile *per_objfile,
43f3e411 9175 struct compunit_symtab *immediate_parent)
95554aad 9176{
af758d11 9177 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9178 if (*slot != NULL)
9179 {
9180 /* This inclusion and its children have been processed. */
9181 return;
9182 }
9183
9184 *slot = per_cu;
af758d11 9185
95554aad 9186 /* Only add a CU if it has a symbol table. */
43182c09 9187 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9188 if (cust != NULL)
ec94af83
DE
9189 {
9190 /* If this is a type unit only add its symbol table if we haven't
9191 seen it yet (type unit per_cu's can share symtabs). */
9192 if (per_cu->is_debug_types)
9193 {
43f3e411 9194 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9195 if (*slot == NULL)
9196 {
43f3e411 9197 *slot = cust;
4c39bc03 9198 result->push_back (cust);
43f3e411
DE
9199 if (cust->user == NULL)
9200 cust->user = immediate_parent;
ec94af83
DE
9201 }
9202 }
9203 else
f9125b6c 9204 {
4c39bc03 9205 result->push_back (cust);
43f3e411
DE
9206 if (cust->user == NULL)
9207 cust->user = immediate_parent;
f9125b6c 9208 }
ec94af83 9209 }
95554aad 9210
ae640021
AB
9211 if (!per_cu->imported_symtabs_empty ())
9212 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9213 {
9214 recursively_compute_inclusions (result, all_children,
43182c09
SM
9215 all_type_symtabs, ptr, per_objfile,
9216 cust);
ae640021 9217 }
95554aad
TT
9218}
9219
43f3e411 9220/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9221 PER_CU. */
9222
9223static void
43182c09
SM
9224compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9225 dwarf2_per_objfile *per_objfile)
95554aad 9226{
f4dc4d17
DE
9227 gdb_assert (! per_cu->is_debug_types);
9228
ae640021 9229 if (!per_cu->imported_symtabs_empty ())
95554aad 9230 {
ae640021 9231 int len;
4c39bc03 9232 std::vector<compunit_symtab *> result_symtabs;
43182c09 9233 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9234
9235 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9236 if (cust == NULL)
95554aad
TT
9237 return;
9238
280a9412
TT
9239 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9240 htab_eq_pointer,
9241 NULL, xcalloc, xfree));
9242 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9243 htab_eq_pointer,
9244 NULL, xcalloc, xfree));
95554aad 9245
ae640021 9246 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9247 {
280a9412
TT
9248 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9249 all_type_symtabs.get (), ptr,
9250 per_objfile, cust);
ec94af83 9251 }
95554aad 9252
ec94af83 9253 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9254 len = result_symtabs.size ();
43f3e411 9255 cust->includes
f6e649dd 9256 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9257 struct compunit_symtab *, len + 1);
4c39bc03
TT
9258 memcpy (cust->includes, result_symtabs.data (),
9259 len * sizeof (compunit_symtab *));
43f3e411 9260 cust->includes[len] = NULL;
95554aad
TT
9261 }
9262}
9263
9264/* Compute the 'includes' field for the symtabs of all the CUs we just
9265 read. */
9266
9267static void
976ca316 9268process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9269{
976ca316 9270 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9271 {
9272 if (! iter->is_debug_types)
976ca316 9273 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9274 }
95554aad 9275
976ca316 9276 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9277}
9278
8fc0b21d 9279/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9280 already been loaded into memory. */
9281
9282static void
8fc0b21d 9283process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9284{
976ca316
SM
9285 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9286 struct objfile *objfile = per_objfile->objfile;
08feed99 9287 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9288 CORE_ADDR lowpc, highpc;
43f3e411 9289 struct compunit_symtab *cust;
10b3939b 9290 CORE_ADDR baseaddr;
4359dff1 9291 struct block *static_block;
3e29f34a 9292 CORE_ADDR addr;
10b3939b 9293
b3b3bada 9294 baseaddr = objfile->text_section_offset ();
10b3939b 9295
c89b44cd
TT
9296 /* Clear the list here in case something was left over. */
9297 cu->method_list.clear ();
10b3939b 9298
95554aad
TT
9299 cu->language = pretend_language;
9300 cu->language_defn = language_def (cu->language);
9301
1c47ec3e
TV
9302 dwarf2_find_base_address (cu->dies, cu);
9303
702cf3f5
AB
9304 /* Before we start reading the top-level DIE, ensure it has a valid tag
9305 type. */
9306 switch (cu->dies->tag)
9307 {
9308 case DW_TAG_compile_unit:
9309 case DW_TAG_partial_unit:
9310 case DW_TAG_type_unit:
9311 break;
9312 default:
9313 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9314 dwarf_tag_name (cu->dies->tag),
9315 sect_offset_str (cu->per_cu->sect_off),
9316 objfile_name (per_objfile->objfile));
9317 }
9318
c906108c 9319 /* Do line number decoding in read_file_scope () */
10b3939b 9320 process_die (cu->dies, cu);
c906108c 9321
a766d390
DE
9322 /* For now fudge the Go package. */
9323 if (cu->language == language_go)
9324 fixup_go_packaging (cu);
9325
5f48f8f3 9326 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9327 should be complete, and it should now be safe to compute all of the
9328 physnames. */
9329 compute_delayed_physnames (cu);
3da10d80 9330
c9317f21
TT
9331 if (cu->language == language_rust)
9332 rust_union_quirks (cu);
9333
fae299cd
DC
9334 /* Some compilers don't define a DW_AT_high_pc attribute for the
9335 compilation unit. If the DW_AT_high_pc is missing, synthesize
9336 it, by scanning the DIE's below the compilation unit. */
10b3939b 9337 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9338
3e29f34a 9339 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9340 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9341
9342 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9343 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9344 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9345 addrmap to help ensure it has an accurate map of pc values belonging to
9346 this comp unit. */
9347 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9348
c24bdb02 9349 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9350 SECT_OFF_TEXT (objfile),
9351 0);
c906108c 9352
43f3e411 9353 if (cust != NULL)
c906108c 9354 {
df15bd07 9355 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9356
8be455d7
JK
9357 /* Set symtab language to language from DW_AT_language. If the
9358 compilation is from a C file generated by language preprocessors, do
9359 not set the language if it was already deduced by start_subfile. */
43f3e411 9360 if (!(cu->language == language_c
40e3ad0e 9361 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9362 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9363
9364 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9365 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9366 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9367 there were bugs in prologue debug info, fixed later in GCC-4.5
9368 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9369
9370 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9371 needed, it would be wrong due to missing DW_AT_producer there.
9372
9373 Still one can confuse GDB by using non-standard GCC compilation
9374 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9375 */
ab260dad 9376 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9377 cust->locations_valid = 1;
e0d00bc7
JK
9378
9379 if (gcc_4_minor >= 5)
43f3e411 9380 cust->epilogue_unwind_valid = 1;
96408a79 9381
43f3e411 9382 cust->call_site_htab = cu->call_site_htab;
c906108c 9383 }
9291a0cd 9384
976ca316 9385 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9386
95554aad 9387 /* Push it for inclusion processing later. */
976ca316 9388 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9389
9390 /* Not needed any more. */
c24bdb02 9391 cu->reset_builder ();
f4dc4d17 9392}
45cfd468 9393
8fc0b21d 9394/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9395 already been loaded into memory. */
9396
9397static void
8fc0b21d 9398process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9399 enum language pretend_language)
9400{
976ca316
SM
9401 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9402 struct objfile *objfile = per_objfile->objfile;
43f3e411 9403 struct compunit_symtab *cust;
0186c6a7
DE
9404 struct signatured_type *sig_type;
9405
8fc0b21d
SM
9406 gdb_assert (cu->per_cu->is_debug_types);
9407 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9408
c89b44cd
TT
9409 /* Clear the list here in case something was left over. */
9410 cu->method_list.clear ();
f4dc4d17 9411
f4dc4d17
DE
9412 cu->language = pretend_language;
9413 cu->language_defn = language_def (cu->language);
9414
9415 /* The symbol tables are set up in read_type_unit_scope. */
9416 process_die (cu->dies, cu);
9417
9418 /* For now fudge the Go package. */
9419 if (cu->language == language_go)
9420 fixup_go_packaging (cu);
9421
5f48f8f3 9422 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9423 should be complete, and it should now be safe to compute all of the
9424 physnames. */
9425 compute_delayed_physnames (cu);
f4dc4d17 9426
c9317f21
TT
9427 if (cu->language == language_rust)
9428 rust_union_quirks (cu);
9429
f4dc4d17
DE
9430 /* TUs share symbol tables.
9431 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9432 of it with end_expandable_symtab. Otherwise, complete the addition of
9433 this TU's symbols to the existing symtab. */
8adb8487 9434 type_unit_group_unshareable *tug_unshare =
976ca316 9435 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9436 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9437 {
c24bdb02
KS
9438 buildsym_compunit *builder = cu->get_builder ();
9439 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9440 tug_unshare->compunit_symtab = cust;
f4dc4d17 9441
43f3e411 9442 if (cust != NULL)
f4dc4d17
DE
9443 {
9444 /* Set symtab language to language from DW_AT_language. If the
9445 compilation is from a C file generated by language preprocessors,
9446 do not set the language if it was already deduced by
9447 start_subfile. */
43f3e411
DE
9448 if (!(cu->language == language_c
9449 && COMPUNIT_FILETABS (cust)->language != language_c))
9450 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9451 }
9452 }
9453 else
9454 {
c24bdb02 9455 cu->get_builder ()->augment_type_symtab ();
8adb8487 9456 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9457 }
9458
976ca316 9459 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9460
9461 /* Not needed any more. */
c24bdb02 9462 cu->reset_builder ();
c906108c
SS
9463}
9464
95554aad
TT
9465/* Process an imported unit DIE. */
9466
9467static void
9468process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9469{
9470 struct attribute *attr;
9471
f4dc4d17
DE
9472 /* For now we don't handle imported units in type units. */
9473 if (cu->per_cu->is_debug_types)
9474 {
9475 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9476 " supported in type units [in module %s]"),
5e22e966 9477 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9478 }
9479
95554aad
TT
9480 attr = dwarf2_attr (die, DW_AT_import, cu);
9481 if (attr != NULL)
9482 {
0826b30a 9483 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9484 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9485 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9486 dwarf2_per_cu_data *per_cu
ab432490 9487 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9488
58990295
TV
9489 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9490 into another compilation unit, at root level. Regard this as a hint,
9491 and ignore it. */
9492 if (die->parent && die->parent->parent == NULL
9493 && per_cu->unit_type == DW_UT_compile
9494 && per_cu->lang == language_cplus)
9495 return;
9496
69d751e3 9497 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 9498 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
9499 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9500 false, cu->language);
95554aad 9501
ae640021 9502 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9503 }
9504}
9505
4c8aa72d
PA
9506/* RAII object that represents a process_die scope: i.e.,
9507 starts/finishes processing a DIE. */
9508class process_die_scope
adde2bff 9509{
4c8aa72d
PA
9510public:
9511 process_die_scope (die_info *die, dwarf2_cu *cu)
9512 : m_die (die), m_cu (cu)
9513 {
9514 /* We should only be processing DIEs not already in process. */
9515 gdb_assert (!m_die->in_process);
9516 m_die->in_process = true;
9517 }
8c3cb9fa 9518
4c8aa72d
PA
9519 ~process_die_scope ()
9520 {
9521 m_die->in_process = false;
9522
9523 /* If we're done processing the DIE for the CU that owns the line
9524 header, we don't need the line header anymore. */
9525 if (m_cu->line_header_die_owner == m_die)
9526 {
9527 delete m_cu->line_header;
9528 m_cu->line_header = NULL;
9529 m_cu->line_header_die_owner = NULL;
9530 }
9531 }
9532
9533private:
9534 die_info *m_die;
9535 dwarf2_cu *m_cu;
9536};
adde2bff 9537
c906108c
SS
9538/* Process a die and its children. */
9539
9540static void
e7c27a73 9541process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9542{
4c8aa72d 9543 process_die_scope scope (die, cu);
adde2bff 9544
c906108c
SS
9545 switch (die->tag)
9546 {
9547 case DW_TAG_padding:
9548 break;
9549 case DW_TAG_compile_unit:
95554aad 9550 case DW_TAG_partial_unit:
e7c27a73 9551 read_file_scope (die, cu);
c906108c 9552 break;
348e048f
DE
9553 case DW_TAG_type_unit:
9554 read_type_unit_scope (die, cu);
9555 break;
c906108c 9556 case DW_TAG_subprogram:
0a4b0913
AB
9557 /* Nested subprograms in Fortran get a prefix. */
9558 if (cu->language == language_fortran
9559 && die->parent != NULL
9560 && die->parent->tag == DW_TAG_subprogram)
9561 cu->processing_has_namespace_info = true;
9562 /* Fall through. */
c906108c 9563 case DW_TAG_inlined_subroutine:
edb3359d 9564 read_func_scope (die, cu);
c906108c
SS
9565 break;
9566 case DW_TAG_lexical_block:
14898363
L
9567 case DW_TAG_try_block:
9568 case DW_TAG_catch_block:
e7c27a73 9569 read_lexical_block_scope (die, cu);
c906108c 9570 break;
216f72a1 9571 case DW_TAG_call_site:
96408a79
SA
9572 case DW_TAG_GNU_call_site:
9573 read_call_site_scope (die, cu);
9574 break;
c906108c 9575 case DW_TAG_class_type:
680b30c7 9576 case DW_TAG_interface_type:
c906108c
SS
9577 case DW_TAG_structure_type:
9578 case DW_TAG_union_type:
134d01f1 9579 process_structure_scope (die, cu);
c906108c
SS
9580 break;
9581 case DW_TAG_enumeration_type:
134d01f1 9582 process_enumeration_scope (die, cu);
c906108c 9583 break;
134d01f1 9584
f792889a
DJ
9585 /* These dies have a type, but processing them does not create
9586 a symbol or recurse to process the children. Therefore we can
9587 read them on-demand through read_type_die. */
c906108c 9588 case DW_TAG_subroutine_type:
72019c9c 9589 case DW_TAG_set_type:
c906108c 9590 case DW_TAG_pointer_type:
c906108c 9591 case DW_TAG_ptr_to_member_type:
c906108c 9592 case DW_TAG_reference_type:
4297a3f0 9593 case DW_TAG_rvalue_reference_type:
c906108c 9594 case DW_TAG_string_type:
c906108c 9595 break;
134d01f1 9596
d8f62e84
TT
9597 case DW_TAG_array_type:
9598 /* We only need to handle this case for Ada -- in other
9599 languages, it's normal for the compiler to emit a typedef
9600 instead. */
9601 if (cu->language != language_ada)
9602 break;
9603 /* FALLTHROUGH */
c906108c 9604 case DW_TAG_base_type:
a02abb62 9605 case DW_TAG_subrange_type:
cb249c71 9606 case DW_TAG_typedef:
134d01f1 9607 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 9608 DW_AT_name. */
f792889a 9609 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9610 break;
c906108c 9611 case DW_TAG_common_block:
e7c27a73 9612 read_common_block (die, cu);
c906108c
SS
9613 break;
9614 case DW_TAG_common_inclusion:
9615 break;
d9fa45fe 9616 case DW_TAG_namespace:
9068261f 9617 cu->processing_has_namespace_info = true;
e7c27a73 9618 read_namespace (die, cu);
d9fa45fe 9619 break;
5d7cb8df 9620 case DW_TAG_module:
9068261f 9621 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9622 read_module (die, cu);
9623 break;
d9fa45fe 9624 case DW_TAG_imported_declaration:
9068261f 9625 cu->processing_has_namespace_info = true;
74921315
KS
9626 if (read_namespace_alias (die, cu))
9627 break;
86a73007
TT
9628 /* The declaration is not a global namespace alias. */
9629 /* Fall through. */
d9fa45fe 9630 case DW_TAG_imported_module:
9068261f 9631 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9632 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9633 || cu->language != language_fortran))
b98664d3 9634 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9635 dwarf_tag_name (die->tag));
9636 read_import_statement (die, cu);
d9fa45fe 9637 break;
95554aad
TT
9638
9639 case DW_TAG_imported_unit:
9640 process_imported_unit_die (die, cu);
9641 break;
9642
71a3c369
TT
9643 case DW_TAG_variable:
9644 read_variable (die, cu);
9645 break;
9646
c906108c 9647 default:
e7c27a73 9648 new_symbol (die, NULL, cu);
c906108c
SS
9649 break;
9650 }
9651}
ca69b9e6
DE
9652\f
9653/* DWARF name computation. */
c906108c 9654
94af9270
KS
9655/* A helper function for dwarf2_compute_name which determines whether DIE
9656 needs to have the name of the scope prepended to the name listed in the
9657 die. */
9658
9659static int
9660die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9661{
1c809c68
TT
9662 struct attribute *attr;
9663
94af9270
KS
9664 switch (die->tag)
9665 {
9666 case DW_TAG_namespace:
9667 case DW_TAG_typedef:
9668 case DW_TAG_class_type:
9669 case DW_TAG_interface_type:
9670 case DW_TAG_structure_type:
9671 case DW_TAG_union_type:
9672 case DW_TAG_enumeration_type:
9673 case DW_TAG_enumerator:
9674 case DW_TAG_subprogram:
08a76f8a 9675 case DW_TAG_inlined_subroutine:
94af9270 9676 case DW_TAG_member:
74921315 9677 case DW_TAG_imported_declaration:
94af9270
KS
9678 return 1;
9679
9680 case DW_TAG_variable:
c2b0a229 9681 case DW_TAG_constant:
94af9270
KS
9682 /* We only need to prefix "globally" visible variables. These include
9683 any variable marked with DW_AT_external or any variable that
9684 lives in a namespace. [Variables in anonymous namespaces
9685 require prefixing, but they are not DW_AT_external.] */
9686
9687 if (dwarf2_attr (die, DW_AT_specification, cu))
9688 {
9689 struct dwarf2_cu *spec_cu = cu;
9a619af0 9690
94af9270
KS
9691 return die_needs_namespace (die_specification (die, &spec_cu),
9692 spec_cu);
9693 }
9694
1c809c68 9695 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9696 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9697 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9698 return 0;
9699 /* A variable in a lexical block of some kind does not need a
9700 namespace, even though in C++ such variables may be external
9701 and have a mangled name. */
9702 if (die->parent->tag == DW_TAG_lexical_block
9703 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9704 || die->parent->tag == DW_TAG_catch_block
9705 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9706 return 0;
9707 return 1;
94af9270
KS
9708
9709 default:
9710 return 0;
9711 }
9712}
9713
73b9be8b
KS
9714/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9715 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9716 defined for the given DIE. */
9717
9718static struct attribute *
9719dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9720{
9721 struct attribute *attr;
9722
9723 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9724 if (attr == NULL)
9725 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9726
9727 return attr;
9728}
9729
9730/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9731 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9732 defined for the given DIE. */
9733
9734static const char *
9735dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9736{
9737 const char *linkage_name;
9738
9739 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9740 if (linkage_name == NULL)
9741 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9742
787de330
TT
9743 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9744 See https://github.com/rust-lang/rust/issues/32925. */
9745 if (cu->language == language_rust && linkage_name != NULL
9746 && strchr (linkage_name, '{') != NULL)
9747 linkage_name = NULL;
9748
73b9be8b
KS
9749 return linkage_name;
9750}
9751
94af9270 9752/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9753 compute the physname for the object, which include a method's:
9c37b5ae 9754 - formal parameters (C++),
a766d390 9755 - receiver type (Go),
a766d390
DE
9756
9757 The term "physname" is a bit confusing.
9758 For C++, for example, it is the demangled name.
9759 For Go, for example, it's the mangled name.
94af9270 9760
af6b7be1
JB
9761 For Ada, return the DIE's linkage name rather than the fully qualified
9762 name. PHYSNAME is ignored..
9763
5989a64e 9764 The result is allocated on the objfile->per_bfd's obstack and
45940949 9765 canonicalized. */
94af9270
KS
9766
9767static const char *
15d034d0
TT
9768dwarf2_compute_name (const char *name,
9769 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9770 int physname)
9771{
5e22e966 9772 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 9773
94af9270
KS
9774 if (name == NULL)
9775 name = dwarf2_name (die, cu);
9776
2ee7123e
DE
9777 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9778 but otherwise compute it by typename_concat inside GDB.
9779 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 9780 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 9781 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
9782 will set the demangled name to the result of dwarf2_full_name, and it is
9783 the demangled name that GDB uses if it exists. */
f55ee35c
JK
9784 if (cu->language == language_ada
9785 || (cu->language == language_fortran && physname))
9786 {
9787 /* For Ada unit, we prefer the linkage name over the name, as
9788 the former contains the exported name, which the user expects
9789 to be able to reference. Ideally, we want the user to be able
9790 to reference this entity using either natural or linkage name,
9791 but we haven't started looking at this enhancement yet. */
73b9be8b 9792 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 9793
2ee7123e
DE
9794 if (linkage_name != NULL)
9795 return linkage_name;
f55ee35c
JK
9796 }
9797
94af9270
KS
9798 /* These are the only languages we know how to qualify names in. */
9799 if (name != NULL
9c37b5ae 9800 && (cu->language == language_cplus
c44af4eb
TT
9801 || cu->language == language_fortran || cu->language == language_d
9802 || cu->language == language_rust))
94af9270
KS
9803 {
9804 if (die_needs_namespace (die, cu))
9805 {
0d5cff50 9806 const char *prefix;
34a68019 9807 const char *canonical_name = NULL;
94af9270 9808
d7e74731
PA
9809 string_file buf;
9810
94af9270 9811 prefix = determine_prefix (die, cu);
94af9270
KS
9812 if (*prefix != '\0')
9813 {
43816ebc
TT
9814 gdb::unique_xmalloc_ptr<char> prefixed_name
9815 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 9816
43816ebc 9817 buf.puts (prefixed_name.get ());
94af9270
KS
9818 }
9819 else
d7e74731 9820 buf.puts (name);
94af9270 9821
98bfdba5
PA
9822 /* Template parameters may be specified in the DIE's DW_AT_name, or
9823 as children with DW_TAG_template_type_param or
9824 DW_TAG_value_type_param. If the latter, add them to the name
9825 here. If the name already has template parameters, then
9826 skip this step; some versions of GCC emit both, and
9827 it is more efficient to use the pre-computed name.
9828
9829 Something to keep in mind about this process: it is very
9830 unlikely, or in some cases downright impossible, to produce
9831 something that will match the mangled name of a function.
9832 If the definition of the function has the same debug info,
9833 we should be able to match up with it anyway. But fallbacks
9834 using the minimal symbol, for instance to find a method
9835 implemented in a stripped copy of libstdc++, will not work.
9836 If we do not have debug info for the definition, we will have to
9837 match them up some other way.
9838
9839 When we do name matching there is a related problem with function
9840 templates; two instantiated function templates are allowed to
9841 differ only by their return types, which we do not add here. */
9842
9843 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9844 {
9845 struct attribute *attr;
9846 struct die_info *child;
9847 int first = 1;
2c75ccb2 9848 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
9849
9850 die->building_fullname = 1;
9851
9852 for (child = die->child; child != NULL; child = child->sibling)
9853 {
9854 struct type *type;
12df843f 9855 LONGEST value;
d521ce57 9856 const gdb_byte *bytes;
98bfdba5
PA
9857 struct dwarf2_locexpr_baton *baton;
9858 struct value *v;
9859
9860 if (child->tag != DW_TAG_template_type_param
9861 && child->tag != DW_TAG_template_value_param)
9862 continue;
9863
9864 if (first)
9865 {
d7e74731 9866 buf.puts ("<");
98bfdba5
PA
9867 first = 0;
9868 }
9869 else
d7e74731 9870 buf.puts (", ");
98bfdba5
PA
9871
9872 attr = dwarf2_attr (child, DW_AT_type, cu);
9873 if (attr == NULL)
9874 {
b98664d3 9875 complaint (_("template parameter missing DW_AT_type"));
d7e74731 9876 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
9877 continue;
9878 }
9879 type = die_type (child, cu);
9880
9881 if (child->tag == DW_TAG_template_type_param)
9882 {
2c75ccb2
AB
9883 cplus_lang->print_type (type, "", &buf, -1, 0,
9884 &type_print_raw_options);
98bfdba5
PA
9885 continue;
9886 }
9887
9888 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9889 if (attr == NULL)
9890 {
b98664d3 9891 complaint (_("template parameter missing "
3e43a32a 9892 "DW_AT_const_value"));
d7e74731 9893 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
9894 continue;
9895 }
9896
9897 dwarf2_const_value_attr (attr, type, name,
9898 &cu->comp_unit_obstack, cu,
9899 &value, &bytes, &baton);
9900
20ce4123 9901 if (type->has_no_signedness ())
98bfdba5
PA
9902 /* GDB prints characters as NUMBER 'CHAR'. If that's
9903 changed, this can use value_print instead. */
2c75ccb2 9904 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
9905 else
9906 {
9907 struct value_print_options opts;
9908
9909 if (baton != NULL)
9910 v = dwarf2_evaluate_loc_desc (type, NULL,
9911 baton->data,
9912 baton->size,
9f47c707
SM
9913 baton->per_cu,
9914 baton->per_objfile);
98bfdba5
PA
9915 else if (bytes != NULL)
9916 {
9917 v = allocate_value (type);
9918 memcpy (value_contents_writeable (v), bytes,
9919 TYPE_LENGTH (type));
9920 }
9921 else
9922 v = value_from_longest (type, value);
9923
3e43a32a
MS
9924 /* Specify decimal so that we do not depend on
9925 the radix. */
98bfdba5
PA
9926 get_formatted_print_options (&opts, 'd');
9927 opts.raw = 1;
d7e74731 9928 value_print (v, &buf, &opts);
98bfdba5 9929 release_value (v);
98bfdba5
PA
9930 }
9931 }
9932
9933 die->building_fullname = 0;
9934
9935 if (!first)
9936 {
9937 /* Close the argument list, with a space if necessary
9938 (nested templates). */
d7e74731
PA
9939 if (!buf.empty () && buf.string ().back () == '>')
9940 buf.puts (" >");
98bfdba5 9941 else
d7e74731 9942 buf.puts (">");
98bfdba5
PA
9943 }
9944 }
9945
9c37b5ae 9946 /* For C++ methods, append formal parameter type
94af9270 9947 information, if PHYSNAME. */
6e70227d 9948
94af9270 9949 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 9950 && cu->language == language_cplus)
94af9270
KS
9951 {
9952 struct type *type = read_type_die (die, cu);
9953
d7e74731 9954 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 9955 &type_print_raw_options);
94af9270 9956
9c37b5ae 9957 if (cu->language == language_cplus)
94af9270 9958 {
60430eff
DJ
9959 /* Assume that an artificial first parameter is
9960 "this", but do not crash if it is not. RealView
9961 marks unnamed (and thus unused) parameters as
9962 artificial; there is no way to differentiate
9963 the two cases. */
1f704f76 9964 if (type->num_fields () > 0
94af9270 9965 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
9966 && type->field (0).type ()->code () == TYPE_CODE_PTR
9967 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 9968 buf.puts (" const");
94af9270
KS
9969 }
9970 }
9971
d7e74731 9972 const std::string &intermediate_name = buf.string ();
94af9270
KS
9973
9974 if (cu->language == language_cplus)
34a68019 9975 canonical_name
322a8516 9976 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 9977 objfile);
34a68019
TT
9978
9979 /* If we only computed INTERMEDIATE_NAME, or if
9980 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 9981 intern it. */
322a8516 9982 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 9983 name = objfile->intern (intermediate_name);
34a68019
TT
9984 else
9985 name = canonical_name;
94af9270
KS
9986 }
9987 }
9988
9989 return name;
9990}
9991
0114d602
DJ
9992/* Return the fully qualified name of DIE, based on its DW_AT_name.
9993 If scope qualifiers are appropriate they will be added. The result
34a68019 9994 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
9995 not have a name. NAME may either be from a previous call to
9996 dwarf2_name or NULL.
9997
9c37b5ae 9998 The output string will be canonicalized (if C++). */
0114d602
DJ
9999
10000static const char *
15d034d0 10001dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10002{
94af9270
KS
10003 return dwarf2_compute_name (name, die, cu, 0);
10004}
0114d602 10005
94af9270
KS
10006/* Construct a physname for the given DIE in CU. NAME may either be
10007 from a previous call to dwarf2_name or NULL. The result will be
10008 allocated on the objfile_objstack or NULL if the DIE does not have a
10009 name.
0114d602 10010
9c37b5ae 10011 The output string will be canonicalized (if C++). */
0114d602 10012
94af9270 10013static const char *
15d034d0 10014dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10015{
5e22e966 10016 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10017 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10018 int need_copy = 1;
10019
10020 /* In this case dwarf2_compute_name is just a shortcut not building anything
10021 on its own. */
10022 if (!die_needs_namespace (die, cu))
10023 return dwarf2_compute_name (name, die, cu, 1);
10024
906bb4c5
TT
10025 if (cu->language != language_rust)
10026 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10027
10028 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10029 has computed. */
791afaa2 10030 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10031 if (mangled != NULL)
900e11f9 10032 {
900e11f9 10033
d3355e4d 10034 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10035 {
10036 /* Do nothing (do not demangle the symbol name). */
10037 }
a766d390
DE
10038 else
10039 {
0eb876f5
JB
10040 /* Use DMGL_RET_DROP for C++ template functions to suppress
10041 their return type. It is easier for GDB users to search
10042 for such functions as `name(params)' than `long name(params)'.
10043 In such case the minimal symbol names do not match the full
10044 symbol names but for template functions there is never a need
10045 to look up their definition from their declaration so
10046 the only disadvantage remains the minimal symbol variant
10047 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10048 demangled.reset (gdb_demangle (mangled,
10049 (DMGL_PARAMS | DMGL_ANSI
10050 | DMGL_RET_DROP)));
a766d390 10051 }
900e11f9 10052 if (demangled)
791afaa2 10053 canon = demangled.get ();
900e11f9
JK
10054 else
10055 {
10056 canon = mangled;
10057 need_copy = 0;
10058 }
10059 }
10060
10061 if (canon == NULL || check_physname)
10062 {
10063 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10064
10065 if (canon != NULL && strcmp (physname, canon) != 0)
10066 {
10067 /* It may not mean a bug in GDB. The compiler could also
10068 compute DW_AT_linkage_name incorrectly. But in such case
10069 GDB would need to be bug-to-bug compatible. */
10070
b98664d3 10071 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10072 "(from linkage <%s>) - DIE at %s [in module %s]"),
10073 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10074 objfile_name (objfile));
900e11f9
JK
10075
10076 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10077 is available here - over computed PHYSNAME. It is safer
10078 against both buggy GDB and buggy compilers. */
10079
10080 retval = canon;
10081 }
10082 else
10083 {
10084 retval = physname;
10085 need_copy = 0;
10086 }
10087 }
10088 else
10089 retval = canon;
10090
10091 if (need_copy)
be1e3d3e 10092 retval = objfile->intern (retval);
900e11f9 10093
900e11f9 10094 return retval;
0114d602
DJ
10095}
10096
74921315
KS
10097/* Inspect DIE in CU for a namespace alias. If one exists, record
10098 a new symbol for it.
10099
10100 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10101
10102static int
10103read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10104{
10105 struct attribute *attr;
10106
10107 /* If the die does not have a name, this is not a namespace
10108 alias. */
10109 attr = dwarf2_attr (die, DW_AT_name, cu);
10110 if (attr != NULL)
10111 {
10112 int num;
10113 struct die_info *d = die;
10114 struct dwarf2_cu *imported_cu = cu;
10115
10116 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10117 keep inspecting DIEs until we hit the underlying import. */
10118#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10119 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10120 {
10121 attr = dwarf2_attr (d, DW_AT_import, cu);
10122 if (attr == NULL)
10123 break;
10124
10125 d = follow_die_ref (d, attr, &imported_cu);
10126 if (d->tag != DW_TAG_imported_declaration)
10127 break;
10128 }
10129
10130 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10131 {
b98664d3 10132 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10133 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10134 return 0;
10135 }
10136
10137 if (attr != NULL)
10138 {
10139 struct type *type;
0826b30a 10140 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10141
aa66c379 10142 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10143 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10144 {
10145 /* This declaration is a global namespace alias. Add
10146 a symbol for it whose type is the aliased namespace. */
10147 new_symbol (die, type, cu);
10148 return 1;
10149 }
10150 }
10151 }
10152
10153 return 0;
10154}
10155
22cee43f 10156/* Return the using directives repository (global or local?) to use in the
804d2729 10157 current context for CU.
22cee43f
PMR
10158
10159 For Ada, imported declarations can materialize renamings, which *may* be
10160 global. However it is impossible (for now?) in DWARF to distinguish
10161 "external" imported declarations and "static" ones. As all imported
10162 declarations seem to be static in all other languages, make them all CU-wide
10163 global only in Ada. */
10164
10165static struct using_direct **
804d2729 10166using_directives (struct dwarf2_cu *cu)
22cee43f 10167{
c24bdb02
KS
10168 if (cu->language == language_ada
10169 && cu->get_builder ()->outermost_context_p ())
10170 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10171 else
c24bdb02 10172 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10173}
10174
27aa8d6a
SW
10175/* Read the import statement specified by the given die and record it. */
10176
10177static void
10178read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10179{
5e22e966 10180 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10181 struct attribute *import_attr;
32019081 10182 struct die_info *imported_die, *child_die;
de4affc9 10183 struct dwarf2_cu *imported_cu;
27aa8d6a 10184 const char *imported_name;
794684b6 10185 const char *imported_name_prefix;
13387711
SW
10186 const char *canonical_name;
10187 const char *import_alias;
10188 const char *imported_declaration = NULL;
794684b6 10189 const char *import_prefix;
eb1e02fd 10190 std::vector<const char *> excludes;
13387711 10191
27aa8d6a
SW
10192 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10193 if (import_attr == NULL)
10194 {
b98664d3 10195 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10196 dwarf_tag_name (die->tag));
10197 return;
10198 }
10199
de4affc9
CC
10200 imported_cu = cu;
10201 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10202 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10203 if (imported_name == NULL)
10204 {
10205 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10206
dda83cd7
SM
10207 The import in the following code:
10208 namespace A
10209 {
10210 typedef int B;
10211 }
10212
10213 int main ()
10214 {
10215 using A::B;
10216 B b;
10217 return b;
10218 }
10219
10220 ...
10221 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10222 <52> DW_AT_decl_file : 1
10223 <53> DW_AT_decl_line : 6
10224 <54> DW_AT_import : <0x75>
10225 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10226 <59> DW_AT_name : B
10227 <5b> DW_AT_decl_file : 1
10228 <5c> DW_AT_decl_line : 2
10229 <5d> DW_AT_type : <0x6e>
10230 ...
10231 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10232 <76> DW_AT_byte_size : 4
10233 <77> DW_AT_encoding : 5 (signed)
10234
10235 imports the wrong die ( 0x75 instead of 0x58 ).
10236 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10237 return;
10238 }
10239
82856980
SW
10240 /* Figure out the local name after import. */
10241 import_alias = dwarf2_name (die, cu);
27aa8d6a 10242
794684b6
SW
10243 /* Figure out where the statement is being imported to. */
10244 import_prefix = determine_prefix (die, cu);
10245
10246 /* Figure out what the scope of the imported die is and prepend it
10247 to the name of the imported die. */
de4affc9 10248 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10249
f55ee35c
JK
10250 if (imported_die->tag != DW_TAG_namespace
10251 && imported_die->tag != DW_TAG_module)
794684b6 10252 {
13387711
SW
10253 imported_declaration = imported_name;
10254 canonical_name = imported_name_prefix;
794684b6 10255 }
13387711 10256 else if (strlen (imported_name_prefix) > 0)
12aaed36 10257 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10258 imported_name_prefix,
10259 (cu->language == language_d ? "." : "::"),
10260 imported_name, (char *) NULL);
13387711
SW
10261 else
10262 canonical_name = imported_name;
794684b6 10263
32019081
JK
10264 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10265 for (child_die = die->child; child_die && child_die->tag;
436c571c 10266 child_die = child_die->sibling)
32019081
JK
10267 {
10268 /* DWARF-4: A Fortran use statement with a “rename list” may be
10269 represented by an imported module entry with an import attribute
10270 referring to the module and owned entries corresponding to those
10271 entities that are renamed as part of being imported. */
10272
10273 if (child_die->tag != DW_TAG_imported_declaration)
10274 {
b98664d3 10275 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10276 "- DIE at %s [in module %s]"),
10277 sect_offset_str (child_die->sect_off),
10278 objfile_name (objfile));
32019081
JK
10279 continue;
10280 }
10281
10282 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10283 if (import_attr == NULL)
10284 {
b98664d3 10285 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10286 dwarf_tag_name (child_die->tag));
10287 continue;
10288 }
10289
10290 imported_cu = cu;
10291 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10292 &imported_cu);
10293 imported_name = dwarf2_name (imported_die, imported_cu);
10294 if (imported_name == NULL)
10295 {
b98664d3 10296 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10297 "imported name - DIE at %s [in module %s]"),
10298 sect_offset_str (child_die->sect_off),
10299 objfile_name (objfile));
32019081
JK
10300 continue;
10301 }
10302
eb1e02fd 10303 excludes.push_back (imported_name);
32019081
JK
10304
10305 process_die (child_die, cu);
10306 }
10307
804d2729 10308 add_using_directive (using_directives (cu),
22cee43f
PMR
10309 import_prefix,
10310 canonical_name,
10311 import_alias,
10312 imported_declaration,
10313 excludes,
10314 0,
10315 &objfile->objfile_obstack);
27aa8d6a
SW
10316}
10317
5230b05a
WT
10318/* ICC<14 does not output the required DW_AT_declaration on incomplete
10319 types, but gives them a size of zero. Starting with version 14,
10320 ICC is compatible with GCC. */
10321
9068261f 10322static bool
5230b05a
WT
10323producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10324{
10325 if (!cu->checked_producer)
10326 check_producer (cu);
10327
10328 return cu->producer_is_icc_lt_14;
10329}
10330
eb77c9df
AB
10331/* ICC generates a DW_AT_type for C void functions. This was observed on
10332 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10333 which says that void functions should not have a DW_AT_type. */
10334
10335static bool
10336producer_is_icc (struct dwarf2_cu *cu)
10337{
10338 if (!cu->checked_producer)
10339 check_producer (cu);
10340
10341 return cu->producer_is_icc;
10342}
10343
1b80a9fa
JK
10344/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10345 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10346 this, it was first present in GCC release 4.3.0. */
10347
9068261f 10348static bool
1b80a9fa
JK
10349producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10350{
10351 if (!cu->checked_producer)
10352 check_producer (cu);
10353
10354 return cu->producer_is_gcc_lt_4_3;
10355}
10356
d721ba37
PA
10357static file_and_directory
10358find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10359{
d721ba37
PA
10360 file_and_directory res;
10361
9291a0cd
TT
10362 /* Find the filename. Do not use dwarf2_name here, since the filename
10363 is not a source language identifier. */
d721ba37
PA
10364 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10365 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10366
d721ba37
PA
10367 if (res.comp_dir == NULL
10368 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10369 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10370 {
d721ba37
PA
10371 res.comp_dir_storage = ldirname (res.name);
10372 if (!res.comp_dir_storage.empty ())
10373 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10374 }
d721ba37 10375 if (res.comp_dir != NULL)
9291a0cd
TT
10376 {
10377 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10378 directory, get rid of it. */
d721ba37 10379 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10380
d721ba37
PA
10381 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10382 res.comp_dir = cp + 1;
9291a0cd
TT
10383 }
10384
d721ba37
PA
10385 if (res.name == NULL)
10386 res.name = "<unknown>";
10387
10388 return res;
9291a0cd
TT
10389}
10390
f4dc4d17
DE
10391/* Handle DW_AT_stmt_list for a compilation unit.
10392 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10393 COMP_DIR is the compilation directory. LOWPC is passed to
10394 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10395
10396static void
10397handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10398 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10399{
976ca316 10400 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10401 struct attribute *attr;
527f3840
JK
10402 struct line_header line_header_local;
10403 hashval_t line_header_local_hash;
527f3840
JK
10404 void **slot;
10405 int decode_mapping;
2ab95328 10406
f4dc4d17
DE
10407 gdb_assert (! cu->per_cu->is_debug_types);
10408
2ab95328 10409 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 10410 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
10411 return;
10412
d4df075e 10413 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
10414
10415 /* The line header hash table is only created if needed (it exists to
10416 prevent redundant reading of the line table for partial_units).
10417 If we're given a partial_unit, we'll need it. If we're given a
10418 compile_unit, then use the line header hash table if it's already
10419 created, but don't create one just yet. */
10420
976ca316 10421 if (per_objfile->line_header_hash == NULL
527f3840 10422 && die->tag == DW_TAG_partial_unit)
2ab95328 10423 {
976ca316 10424 per_objfile->line_header_hash
d15acc42
TT
10425 .reset (htab_create_alloc (127, line_header_hash_voidp,
10426 line_header_eq_voidp,
ef5f598c 10427 htab_delete_entry<line_header>,
d15acc42 10428 xcalloc, xfree));
527f3840 10429 }
2ab95328 10430
9c541725 10431 line_header_local.sect_off = line_offset;
527f3840
JK
10432 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10433 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10434 if (per_objfile->line_header_hash != NULL)
527f3840 10435 {
976ca316 10436 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10437 &line_header_local,
10438 line_header_local_hash, NO_INSERT);
10439
10440 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10441 is not present in *SLOT (since if there is something in *SLOT then
10442 it will be for a partial_unit). */
10443 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10444 {
527f3840 10445 gdb_assert (*slot != NULL);
9a3c8263 10446 cu->line_header = (struct line_header *) *slot;
527f3840 10447 return;
dee91e82 10448 }
2ab95328 10449 }
527f3840
JK
10450
10451 /* dwarf_decode_line_header does not yet provide sufficient information.
10452 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10453 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10454 if (lh == NULL)
527f3840 10455 return;
4c8aa72d
PA
10456
10457 cu->line_header = lh.release ();
10458 cu->line_header_die_owner = die;
527f3840 10459
976ca316 10460 if (per_objfile->line_header_hash == NULL)
527f3840
JK
10461 slot = NULL;
10462 else
10463 {
976ca316 10464 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10465 &line_header_local,
10466 line_header_local_hash, INSERT);
10467 gdb_assert (slot != NULL);
10468 }
10469 if (slot != NULL && *slot == NULL)
10470 {
10471 /* This newly decoded line number information unit will be owned
10472 by line_header_hash hash table. */
10473 *slot = cu->line_header;
4c8aa72d 10474 cu->line_header_die_owner = NULL;
527f3840
JK
10475 }
10476 else
10477 {
10478 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 10479 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
10480 line_header for this CU - it may happen at most once for each line
10481 number information unit. And if we're not using line_header_hash
10482 then this is what we want as well. */
10483 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10484 }
10485 decode_mapping = (die->tag != DW_TAG_partial_unit);
10486 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10487 decode_mapping);
fff8551c 10488
2ab95328
TT
10489}
10490
95554aad 10491/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10492
c906108c 10493static void
e7c27a73 10494read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10495{
976ca316
SM
10496 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10497 struct objfile *objfile = per_objfile->objfile;
08feed99 10498 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10499 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10500 CORE_ADDR highpc = ((CORE_ADDR) 0);
10501 struct attribute *attr;
c906108c 10502 struct die_info *child_die;
e142c38c 10503 CORE_ADDR baseaddr;
6e70227d 10504
380618d6 10505 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10506 baseaddr = objfile->text_section_offset ();
c906108c 10507
fae299cd 10508 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10509
10510 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10511 from finish_block. */
2acceee2 10512 if (lowpc == ((CORE_ADDR) -1))
c906108c 10513 lowpc = highpc;
3e29f34a 10514 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10515
d721ba37 10516 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10517
f4b8a18d
KW
10518 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10519 standardised yet. As a workaround for the language detection we fall
10520 back to the DW_AT_producer string. */
10521 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10522 cu->language = language_opencl;
10523
3019eac3
DE
10524 /* Similar hack for Go. */
10525 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10526 set_cu_language (DW_LANG_Go, cu);
10527
c24bdb02 10528 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10529
10530 /* Decode line number information if present. We do this before
10531 processing child DIEs, so that the line header table is available
10532 for DW_AT_decl_file. */
d721ba37 10533 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10534
10535 /* Process all dies in compilation unit. */
10536 if (die->child != NULL)
10537 {
10538 child_die = die->child;
10539 while (child_die && child_die->tag)
10540 {
10541 process_die (child_die, cu);
436c571c 10542 child_die = child_die->sibling;
3019eac3
DE
10543 }
10544 }
10545
10546 /* Decode macro information, if present. Dwarf 2 macro information
10547 refers to information in the line number info statement program
10548 header, so we can only read it if we've read the header
10549 successfully. */
0af92d60
JK
10550 attr = dwarf2_attr (die, DW_AT_macros, cu);
10551 if (attr == NULL)
10552 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 10553 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
10554 {
10555 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10556 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10557
529908cb 10558 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
10559 }
10560 else
10561 {
10562 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 10563 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 10564 {
529908cb 10565 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 10566
43f3e411 10567 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10568 }
10569 }
3019eac3
DE
10570}
10571
c24bdb02
KS
10572void
10573dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10574{
f4dc4d17
DE
10575 struct type_unit_group *tu_group;
10576 int first_time;
3019eac3 10577 struct attribute *attr;
9c541725 10578 unsigned int i;
0186c6a7 10579 struct signatured_type *sig_type;
3019eac3 10580
f4dc4d17 10581 gdb_assert (per_cu->is_debug_types);
0186c6a7 10582 sig_type = (struct signatured_type *) per_cu;
3019eac3 10583
c24bdb02 10584 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10585
f4dc4d17 10586 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10587 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10588 if (sig_type->type_unit_group == NULL)
c24bdb02 10589 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10590 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10591
10592 /* If we've already processed this stmt_list there's no real need to
10593 do it again, we could fake it and just recreate the part we need
10594 (file name,index -> symtab mapping). If data shows this optimization
10595 is useful we can do it then. */
8adb8487
TT
10596 type_unit_group_unshareable *tug_unshare
10597 = per_objfile->get_type_unit_group_unshareable (tu_group);
10598 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
10599
10600 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10601 debug info. */
fff8551c 10602 line_header_up lh;
d4df075e 10603 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 10604 {
d4df075e 10605 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 10606 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10607 }
10608 if (lh == NULL)
10609 {
10610 if (first_time)
c24bdb02 10611 start_symtab ("", NULL, 0);
f4dc4d17
DE
10612 else
10613 {
8adb8487 10614 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 10615 gdb_assert (m_builder == nullptr);
8adb8487 10616 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10617 m_builder.reset (new struct buildsym_compunit
10618 (COMPUNIT_OBJFILE (cust), "",
10619 COMPUNIT_DIRNAME (cust),
10620 compunit_language (cust),
10621 0, cust));
770479f2 10622 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10623 }
f4dc4d17 10624 return;
3019eac3
DE
10625 }
10626
c24bdb02
KS
10627 line_header = lh.release ();
10628 line_header_die_owner = die;
3019eac3 10629
f4dc4d17
DE
10630 if (first_time)
10631 {
c24bdb02 10632 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10633
1fd60fc0
DE
10634 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10635 still initializing it, and our caller (a few levels up)
10636 process_full_type_unit still needs to know if this is the first
10637 time. */
10638
8adb8487 10639 tug_unshare->symtabs
4ac93832
TT
10640 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10641 struct symtab *, line_header->file_names_size ());
3019eac3 10642
7ba99d21
AT
10643 auto &file_names = line_header->file_names ();
10644 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10645 {
7ba99d21 10646 file_entry &fe = file_names[i];
c24bdb02
KS
10647 dwarf2_start_subfile (this, fe.name,
10648 fe.include_dir (line_header));
10649 buildsym_compunit *b = get_builder ();
10650 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10651 {
4c8aa72d
PA
10652 /* NOTE: start_subfile will recognize when it's been
10653 passed a file it has already seen. So we can't
10654 assume there's a simple mapping from
10655 cu->line_header->file_names to subfiles, plus
10656 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10657 b->get_current_subfile ()->symtab
10658 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10659 }
10660
c24bdb02 10661 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 10662 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
10663 }
10664 }
10665 else
3019eac3 10666 {
c24bdb02 10667 gdb_assert (m_builder == nullptr);
8adb8487 10668 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10669 m_builder.reset (new struct buildsym_compunit
10670 (COMPUNIT_OBJFILE (cust), "",
10671 COMPUNIT_DIRNAME (cust),
10672 compunit_language (cust),
10673 0, cust));
770479f2 10674 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10675
7ba99d21
AT
10676 auto &file_names = line_header->file_names ();
10677 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10678 {
7ba99d21 10679 file_entry &fe = file_names[i];
8adb8487 10680 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 10681 }
3019eac3
DE
10682 }
10683
f4dc4d17
DE
10684 /* The main symtab is allocated last. Type units don't have DW_AT_name
10685 so they don't have a "real" (so to speak) symtab anyway.
10686 There is later code that will assign the main symtab to all symbols
10687 that don't have one. We need to handle the case of a symbol with a
10688 missing symtab (DW_AT_decl_file) anyway. */
10689}
3019eac3 10690
f4dc4d17
DE
10691/* Process DW_TAG_type_unit.
10692 For TUs we want to skip the first top level sibling if it's not the
10693 actual type being defined by this TU. In this case the first top
10694 level sibling is there to provide context only. */
3019eac3 10695
f4dc4d17
DE
10696static void
10697read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10698{
10699 struct die_info *child_die;
3019eac3 10700
f4dc4d17
DE
10701 prepare_one_comp_unit (cu, die, language_minimal);
10702
10703 /* Initialize (or reinitialize) the machinery for building symtabs.
10704 We do this before processing child DIEs, so that the line header table
10705 is available for DW_AT_decl_file. */
c24bdb02 10706 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10707
10708 if (die->child != NULL)
10709 {
10710 child_die = die->child;
10711 while (child_die && child_die->tag)
10712 {
10713 process_die (child_die, cu);
436c571c 10714 child_die = child_die->sibling;
f4dc4d17
DE
10715 }
10716 }
3019eac3
DE
10717}
10718\f
80626a55
DE
10719/* DWO/DWP files.
10720
10721 http://gcc.gnu.org/wiki/DebugFission
10722 http://gcc.gnu.org/wiki/DebugFissionDWP
10723
10724 To simplify handling of both DWO files ("object" files with the DWARF info)
10725 and DWP files (a file with the DWOs packaged up into one file), we treat
10726 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10727
10728static hashval_t
10729hash_dwo_file (const void *item)
10730{
9a3c8263 10731 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10732 hashval_t hash;
3019eac3 10733
a2ce51a0
DE
10734 hash = htab_hash_string (dwo_file->dwo_name);
10735 if (dwo_file->comp_dir != NULL)
10736 hash += htab_hash_string (dwo_file->comp_dir);
10737 return hash;
3019eac3
DE
10738}
10739
10740static int
10741eq_dwo_file (const void *item_lhs, const void *item_rhs)
10742{
9a3c8263
SM
10743 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10744 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10745
a2ce51a0
DE
10746 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10747 return 0;
10748 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10749 return lhs->comp_dir == rhs->comp_dir;
10750 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10751}
10752
10753/* Allocate a hash table for DWO files. */
10754
51ac9db5 10755static htab_up
298e9637 10756allocate_dwo_file_hash_table ()
3019eac3 10757{
bc68fb19
TT
10758 return htab_up (htab_create_alloc (41,
10759 hash_dwo_file,
10760 eq_dwo_file,
ef5f598c 10761 htab_delete_entry<dwo_file>,
bc68fb19 10762 xcalloc, xfree));
3019eac3
DE
10763}
10764
80626a55
DE
10765/* Lookup DWO file DWO_NAME. */
10766
10767static void **
976ca316 10768lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
10769 const char *dwo_name,
10770 const char *comp_dir)
80626a55
DE
10771{
10772 struct dwo_file find_entry;
10773 void **slot;
10774
976ca316
SM
10775 if (per_objfile->per_bfd->dwo_files == NULL)
10776 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 10777
0ac5b59e
DE
10778 find_entry.dwo_name = dwo_name;
10779 find_entry.comp_dir = comp_dir;
976ca316 10780 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 10781 INSERT);
80626a55
DE
10782
10783 return slot;
10784}
10785
3019eac3
DE
10786static hashval_t
10787hash_dwo_unit (const void *item)
10788{
9a3c8263 10789 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
10790
10791 /* This drops the top 32 bits of the id, but is ok for a hash. */
10792 return dwo_unit->signature;
10793}
10794
10795static int
10796eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10797{
9a3c8263
SM
10798 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10799 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
10800
10801 /* The signature is assumed to be unique within the DWO file.
10802 So while object file CU dwo_id's always have the value zero,
10803 that's OK, assuming each object file DWO file has only one CU,
10804 and that's the rule for now. */
10805 return lhs->signature == rhs->signature;
10806}
10807
10808/* Allocate a hash table for DWO CUs,TUs.
10809 There is one of these tables for each of CUs,TUs for each DWO file. */
10810
b0b6a987 10811static htab_up
298e9637 10812allocate_dwo_unit_table ()
3019eac3
DE
10813{
10814 /* Start out with a pretty small number.
10815 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
10816 return htab_up (htab_create_alloc (3,
10817 hash_dwo_unit,
10818 eq_dwo_unit,
10819 NULL, xcalloc, xfree));
3019eac3
DE
10820}
10821
19c3d4c9 10822/* die_reader_func for create_dwo_cu. */
3019eac3
DE
10823
10824static void
19c3d4c9
DE
10825create_dwo_cu_reader (const struct die_reader_specs *reader,
10826 const gdb_byte *info_ptr,
10827 struct die_info *comp_unit_die,
c0ab21c2
TT
10828 struct dwo_file *dwo_file,
10829 struct dwo_unit *dwo_unit)
3019eac3
DE
10830{
10831 struct dwarf2_cu *cu = reader->cu;
9c541725 10832 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 10833 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 10834
a084a2a6
AT
10835 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10836 if (!signature.has_value ())
3019eac3 10837 {
b98664d3 10838 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 10839 " its dwo_id [in module %s]"),
9d8780f0 10840 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
10841 return;
10842 }
10843
3019eac3 10844 dwo_unit->dwo_file = dwo_file;
a084a2a6 10845 dwo_unit->signature = *signature;
8a0459fd 10846 dwo_unit->section = section;
9c541725 10847 dwo_unit->sect_off = sect_off;
3019eac3
DE
10848 dwo_unit->length = cu->per_cu->length;
10849
6f738b01
SM
10850 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10851 sect_offset_str (sect_off),
10852 hex_string (dwo_unit->signature));
3019eac3
DE
10853}
10854
33c5cd75 10855/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 10856 Note: This function processes DWO files only, not DWP files. */
3019eac3 10857
33c5cd75 10858static void
976ca316 10859create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 10860 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 10861 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 10862{
976ca316
SM
10863 struct objfile *objfile = per_objfile->objfile;
10864 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 10865 const gdb_byte *info_ptr, *end_ptr;
3019eac3 10866
96b79293 10867 section.read (objfile);
33c5cd75 10868 info_ptr = section.buffer;
3019eac3
DE
10869
10870 if (info_ptr == NULL)
33c5cd75 10871 return;
3019eac3 10872
6f738b01
SM
10873 dwarf_read_debug_printf ("Reading %s for %s:",
10874 section.get_name (),
10875 section.get_file_name ());
3019eac3 10876
33c5cd75 10877 end_ptr = info_ptr + section.size;
3019eac3
DE
10878 while (info_ptr < end_ptr)
10879 {
10880 struct dwarf2_per_cu_data per_cu;
c0ab21c2 10881 struct dwo_unit read_unit {};
33c5cd75
DB
10882 struct dwo_unit *dwo_unit;
10883 void **slot;
10884 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 10885
1859c670 10886 per_cu.per_bfd = per_bfd;
3019eac3 10887 per_cu.is_debug_types = 0;
33c5cd75
DB
10888 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10889 per_cu.section = &section;
10890
976ca316 10891 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
10892 if (!reader.dummy_p)
10893 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 10894 &dwo_file, &read_unit);
33c5cd75
DB
10895 info_ptr += per_cu.length;
10896
10897 // If the unit could not be parsed, skip it.
c0ab21c2 10898 if (read_unit.dwo_file == NULL)
33c5cd75 10899 continue;
3019eac3 10900
33c5cd75 10901 if (cus_htab == NULL)
298e9637 10902 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 10903
1859c670 10904 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 10905 struct dwo_unit);
c0ab21c2 10906 *dwo_unit = read_unit;
b0b6a987 10907 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
10908 gdb_assert (slot != NULL);
10909 if (*slot != NULL)
19c3d4c9 10910 {
33c5cd75
DB
10911 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10912 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 10913
b98664d3 10914 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
10915 " the entry at offset %s, signature %s"),
10916 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 10917 hex_string (dwo_unit->signature));
19c3d4c9 10918 }
33c5cd75 10919 *slot = (void *)dwo_unit;
3019eac3 10920 }
3019eac3
DE
10921}
10922
80626a55
DE
10923/* DWP file .debug_{cu,tu}_index section format:
10924 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
10925 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10926
10927 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10928 officially standard DWP format was published with DWARF v5 and is called
10929 Version 5. There are no versions 3 or 4.
80626a55 10930
d2415c6c
DE
10931 DWP Version 1:
10932
80626a55
DE
10933 Both index sections have the same format, and serve to map a 64-bit
10934 signature to a set of section numbers. Each section begins with a header,
10935 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10936 indexes, and a pool of 32-bit section numbers. The index sections will be
10937 aligned at 8-byte boundaries in the file.
10938
d2415c6c
DE
10939 The index section header consists of:
10940
10941 V, 32 bit version number
10942 -, 32 bits unused
10943 N, 32 bit number of compilation units or type units in the index
10944 M, 32 bit number of slots in the hash table
80626a55 10945
d2415c6c 10946 Numbers are recorded using the byte order of the application binary.
80626a55 10947
d2415c6c
DE
10948 The hash table begins at offset 16 in the section, and consists of an array
10949 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10950 order of the application binary). Unused slots in the hash table are 0.
10951 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 10952
d2415c6c
DE
10953 The parallel table begins immediately after the hash table
10954 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10955 array of 32-bit indexes (using the byte order of the application binary),
10956 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10957 table contains a 32-bit index into the pool of section numbers. For unused
10958 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 10959
73869dc2
DE
10960 The pool of section numbers begins immediately following the hash table
10961 (at offset 16 + 12 * M from the beginning of the section). The pool of
10962 section numbers consists of an array of 32-bit words (using the byte order
10963 of the application binary). Each item in the array is indexed starting
10964 from 0. The hash table entry provides the index of the first section
10965 number in the set. Additional section numbers in the set follow, and the
10966 set is terminated by a 0 entry (section number 0 is not used in ELF).
10967
10968 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10969 section must be the first entry in the set, and the .debug_abbrev.dwo must
10970 be the second entry. Other members of the set may follow in any order.
10971
10972 ---
10973
d2854d8d 10974 DWP Versions 2 and 5:
73869dc2 10975
d2854d8d 10976 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
10977 and the entries in the index tables are now offsets into these sections.
10978 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10979 section.
10980
10981 Index Section Contents:
10982 Header
10983 Hash Table of Signatures dwp_hash_table.hash_table
10984 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
10985 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10986 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
10987
10988 The index section header consists of:
10989
10990 V, 32 bit version number
10991 L, 32 bit number of columns in the table of section offsets
10992 N, 32 bit number of compilation units or type units in the index
10993 M, 32 bit number of slots in the hash table
10994
10995 Numbers are recorded using the byte order of the application binary.
10996
10997 The hash table has the same format as version 1.
10998 The parallel table of indices has the same format as version 1,
10999 except that the entries are origin-1 indices into the table of sections
11000 offsets and the table of section sizes.
11001
11002 The table of offsets begins immediately following the parallel table
11003 (at offset 16 + 12 * M from the beginning of the section). The table is
11004 a two-dimensional array of 32-bit words (using the byte order of the
11005 application binary), with L columns and N+1 rows, in row-major order.
11006 Each row in the array is indexed starting from 0. The first row provides
11007 a key to the remaining rows: each column in this row provides an identifier
11008 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11009 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11010
11011 DW_SECT_INFO 1 .debug_info.dwo
11012 DW_SECT_TYPES 2 .debug_types.dwo
11013 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11014 DW_SECT_LINE 4 .debug_line.dwo
11015 DW_SECT_LOC 5 .debug_loc.dwo
11016 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11017 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11018 DW_SECT_MACRO 8 .debug_macro.dwo
11019
d2854d8d
CT
11020 The section identifiers for Version 5 are:
11021
11022 DW_SECT_INFO_V5 1 .debug_info.dwo
11023 DW_SECT_RESERVED_V5 2 --
11024 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11025 DW_SECT_LINE_V5 4 .debug_line.dwo
11026 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11027 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11028 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11029 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11030
73869dc2
DE
11031 The offsets provided by the CU and TU index sections are the base offsets
11032 for the contributions made by each CU or TU to the corresponding section
11033 in the package file. Each CU and TU header contains an abbrev_offset
11034 field, used to find the abbreviations table for that CU or TU within the
11035 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11036 be interpreted as relative to the base offset given in the index section.
11037 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11038 should be interpreted as relative to the base offset for .debug_line.dwo,
11039 and offsets into other debug sections obtained from DWARF attributes should
11040 also be interpreted as relative to the corresponding base offset.
11041
11042 The table of sizes begins immediately following the table of offsets.
11043 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11044 with L columns and N rows, in row-major order. Each row in the array is
11045 indexed starting from 1 (row 0 is shared by the two tables).
11046
11047 ---
11048
11049 Hash table lookup is handled the same in version 1 and 2:
11050
11051 We assume that N and M will not exceed 2^32 - 1.
11052 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11053
d2415c6c
DE
11054 Given a 64-bit compilation unit signature or a type signature S, an entry
11055 in the hash table is located as follows:
80626a55 11056
d2415c6c
DE
11057 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11058 the low-order k bits all set to 1.
80626a55 11059
d2415c6c 11060 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11061
d2415c6c
DE
11062 3) If the hash table entry at index H matches the signature, use that
11063 entry. If the hash table entry at index H is unused (all zeroes),
11064 terminate the search: the signature is not present in the table.
80626a55 11065
d2415c6c 11066 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11067
d2415c6c 11068 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11069 to stop at an unused slot or find the match. */
80626a55
DE
11070
11071/* Create a hash table to map DWO IDs to their CU/TU entry in
11072 .debug_{info,types}.dwo in DWP_FILE.
11073 Returns NULL if there isn't one.
11074 Note: This function processes DWP files only, not DWO files. */
11075
11076static struct dwp_hash_table *
976ca316 11077create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11078 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11079{
976ca316 11080 struct objfile *objfile = per_objfile->objfile;
400174b1 11081 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11082 const gdb_byte *index_ptr, *index_end;
80626a55 11083 struct dwarf2_section_info *index;
73869dc2 11084 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11085 struct dwp_hash_table *htab;
11086
11087 if (is_debug_types)
11088 index = &dwp_file->sections.tu_index;
11089 else
11090 index = &dwp_file->sections.cu_index;
11091
96b79293 11092 if (index->empty ())
80626a55 11093 return NULL;
96b79293 11094 index->read (objfile);
80626a55
DE
11095
11096 index_ptr = index->buffer;
11097 index_end = index_ptr + index->size;
11098
d2854d8d
CT
11099 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11100 For now it's safe to just read 4 bytes (particularly as it's difficult to
11101 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11102 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11103 index_ptr += 4;
d2854d8d 11104 if (version == 2 || version == 5)
73869dc2
DE
11105 nr_columns = read_4_bytes (dbfd, index_ptr);
11106 else
11107 nr_columns = 0;
11108 index_ptr += 4;
80626a55
DE
11109 nr_units = read_4_bytes (dbfd, index_ptr);
11110 index_ptr += 4;
11111 nr_slots = read_4_bytes (dbfd, index_ptr);
11112 index_ptr += 4;
11113
d2854d8d 11114 if (version != 1 && version != 2 && version != 5)
80626a55 11115 {
21aa081e 11116 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11117 " [in module %s]"),
21aa081e 11118 pulongest (version), dwp_file->name);
80626a55
DE
11119 }
11120 if (nr_slots != (nr_slots & -nr_slots))
11121 {
21aa081e 11122 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11123 " is not power of 2 [in module %s]"),
21aa081e 11124 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11125 }
11126
976ca316 11127 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11128 htab->version = version;
11129 htab->nr_columns = nr_columns;
80626a55
DE
11130 htab->nr_units = nr_units;
11131 htab->nr_slots = nr_slots;
11132 htab->hash_table = index_ptr;
11133 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11134
11135 /* Exit early if the table is empty. */
11136 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11137 || (version == 2 && nr_columns == 0)
11138 || (version == 5 && nr_columns == 0))
73869dc2
DE
11139 {
11140 /* All must be zero. */
11141 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11142 || (version == 2 && nr_columns != 0)
11143 || (version == 5 && nr_columns != 0))
73869dc2 11144 {
b98664d3 11145 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11146 " all zero [in modules %s]"),
11147 dwp_file->name);
11148 }
11149 return htab;
11150 }
11151
11152 if (version == 1)
11153 {
11154 htab->section_pool.v1.indices =
11155 htab->unit_table + sizeof (uint32_t) * nr_slots;
11156 /* It's harder to decide whether the section is too small in v1.
11157 V1 is deprecated anyway so we punt. */
11158 }
d2854d8d 11159 else if (version == 2)
73869dc2
DE
11160 {
11161 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11162 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11163 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11164 /* Reverse map for error checking. */
11165 int ids_seen[DW_SECT_MAX + 1];
11166 int i;
11167
11168 if (nr_columns < 2)
11169 {
11170 error (_("Dwarf Error: bad DWP hash table, too few columns"
11171 " in section table [in module %s]"),
11172 dwp_file->name);
11173 }
11174 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11175 {
11176 error (_("Dwarf Error: bad DWP hash table, too many columns"
11177 " in section table [in module %s]"),
11178 dwp_file->name);
11179 }
04fd5eed
GB
11180 memset (ids, 255, sizeof_ids);
11181 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11182 for (i = 0; i < nr_columns; ++i)
11183 {
11184 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11185
11186 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11187 {
11188 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11189 " in section table [in module %s]"),
11190 id, dwp_file->name);
11191 }
11192 if (ids_seen[id] != -1)
11193 {
11194 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11195 " id %d in section table [in module %s]"),
11196 id, dwp_file->name);
11197 }
11198 ids_seen[id] = i;
11199 ids[i] = id;
11200 }
11201 /* Must have exactly one info or types section. */
11202 if (((ids_seen[DW_SECT_INFO] != -1)
11203 + (ids_seen[DW_SECT_TYPES] != -1))
11204 != 1)
11205 {
11206 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11207 " DWO info/types section [in module %s]"),
11208 dwp_file->name);
11209 }
11210 /* Must have an abbrev section. */
11211 if (ids_seen[DW_SECT_ABBREV] == -1)
11212 {
11213 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11214 " section [in module %s]"),
11215 dwp_file->name);
11216 }
11217 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11218 htab->section_pool.v2.sizes =
11219 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11220 * nr_units * nr_columns);
11221 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11222 * nr_units * nr_columns))
11223 > index_end)
11224 {
11225 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11226 " [in module %s]"),
11227 dwp_file->name);
11228 }
11229 }
d2854d8d
CT
11230 else /* version == 5 */
11231 {
11232 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11233 int *ids = htab->section_pool.v5.section_ids;
11234 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11235 /* Reverse map for error checking. */
11236 int ids_seen[DW_SECT_MAX_V5 + 1];
11237
11238 if (nr_columns < 2)
11239 {
11240 error (_("Dwarf Error: bad DWP hash table, too few columns"
11241 " in section table [in module %s]"),
11242 dwp_file->name);
11243 }
11244 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11245 {
11246 error (_("Dwarf Error: bad DWP hash table, too many columns"
11247 " in section table [in module %s]"),
11248 dwp_file->name);
11249 }
11250 memset (ids, 255, sizeof_ids);
11251 memset (ids_seen, 255, sizeof (ids_seen));
11252 for (int i = 0; i < nr_columns; ++i)
11253 {
11254 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11255
11256 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11257 {
11258 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11259 " in section table [in module %s]"),
11260 id, dwp_file->name);
11261 }
11262 if (ids_seen[id] != -1)
11263 {
11264 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11265 " id %d in section table [in module %s]"),
11266 id, dwp_file->name);
11267 }
11268 ids_seen[id] = i;
11269 ids[i] = id;
11270 }
11271 /* Must have seen an info section. */
11272 if (ids_seen[DW_SECT_INFO_V5] == -1)
11273 {
11274 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11275 " DWO info/types section [in module %s]"),
11276 dwp_file->name);
11277 }
11278 /* Must have an abbrev section. */
11279 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11280 {
11281 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11282 " section [in module %s]"),
11283 dwp_file->name);
11284 }
11285 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11286 htab->section_pool.v5.sizes
11287 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11288 * nr_units * nr_columns);
11289 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11290 * nr_units * nr_columns))
11291 > index_end)
11292 {
11293 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11294 " [in module %s]"),
11295 dwp_file->name);
11296 }
11297 }
80626a55
DE
11298
11299 return htab;
11300}
11301
11302/* Update SECTIONS with the data from SECTP.
11303
5bb6e9dd
TT
11304 This function is like the other "locate" section routines, but in
11305 this context the sections to read comes from the DWP V1 hash table,
11306 not the full ELF section table.
80626a55
DE
11307
11308 The result is non-zero for success, or zero if an error was found. */
11309
11310static int
73869dc2
DE
11311locate_v1_virtual_dwo_sections (asection *sectp,
11312 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11313{
11314 const struct dwop_section_names *names = &dwop_section_names;
11315
fbedd546 11316 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
11317 {
11318 /* There can be only one. */
049412e3 11319 if (sections->abbrev.s.section != NULL)
80626a55 11320 return 0;
049412e3 11321 sections->abbrev.s.section = sectp;
fd361982 11322 sections->abbrev.size = bfd_section_size (sectp);
80626a55 11323 }
fbedd546
TT
11324 else if (names->info_dwo.matches (sectp->name)
11325 || names->types_dwo.matches (sectp->name))
80626a55
DE
11326 {
11327 /* There can be only one. */
049412e3 11328 if (sections->info_or_types.s.section != NULL)
80626a55 11329 return 0;
049412e3 11330 sections->info_or_types.s.section = sectp;
fd361982 11331 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 11332 }
fbedd546 11333 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
11334 {
11335 /* There can be only one. */
049412e3 11336 if (sections->line.s.section != NULL)
80626a55 11337 return 0;
049412e3 11338 sections->line.s.section = sectp;
fd361982 11339 sections->line.size = bfd_section_size (sectp);
80626a55 11340 }
fbedd546 11341 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
11342 {
11343 /* There can be only one. */
049412e3 11344 if (sections->loc.s.section != NULL)
80626a55 11345 return 0;
049412e3 11346 sections->loc.s.section = sectp;
fd361982 11347 sections->loc.size = bfd_section_size (sectp);
80626a55 11348 }
fbedd546 11349 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
11350 {
11351 /* There can be only one. */
049412e3 11352 if (sections->macinfo.s.section != NULL)
80626a55 11353 return 0;
049412e3 11354 sections->macinfo.s.section = sectp;
fd361982 11355 sections->macinfo.size = bfd_section_size (sectp);
80626a55 11356 }
fbedd546 11357 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
11358 {
11359 /* There can be only one. */
049412e3 11360 if (sections->macro.s.section != NULL)
80626a55 11361 return 0;
049412e3 11362 sections->macro.s.section = sectp;
fd361982 11363 sections->macro.size = bfd_section_size (sectp);
80626a55 11364 }
fbedd546 11365 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
11366 {
11367 /* There can be only one. */
049412e3 11368 if (sections->str_offsets.s.section != NULL)
80626a55 11369 return 0;
049412e3 11370 sections->str_offsets.s.section = sectp;
fd361982 11371 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11372 }
11373 else
11374 {
11375 /* No other kind of section is valid. */
11376 return 0;
11377 }
11378
11379 return 1;
11380}
11381
73869dc2
DE
11382/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11383 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11384 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11385 This is for DWP version 1 files. */
80626a55
DE
11386
11387static struct dwo_unit *
976ca316 11388create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11389 struct dwp_file *dwp_file,
73869dc2
DE
11390 uint32_t unit_index,
11391 const char *comp_dir,
11392 ULONGEST signature, int is_debug_types)
80626a55 11393{
73869dc2
DE
11394 const struct dwp_hash_table *dwp_htab =
11395 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11396 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11397 const char *kind = is_debug_types ? "TU" : "CU";
11398 struct dwo_file *dwo_file;
11399 struct dwo_unit *dwo_unit;
73869dc2 11400 struct virtual_v1_dwo_sections sections;
80626a55 11401 void **dwo_file_slot;
80626a55
DE
11402 int i;
11403
73869dc2
DE
11404 gdb_assert (dwp_file->version == 1);
11405
6f738b01
SM
11406 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11407 kind, pulongest (unit_index), hex_string (signature),
11408 dwp_file->name);
80626a55 11409
19ac8c2e 11410 /* Fetch the sections of this DWO unit.
80626a55
DE
11411 Put a limit on the number of sections we look for so that bad data
11412 doesn't cause us to loop forever. */
11413
73869dc2 11414#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11415 (1 /* .debug_info or .debug_types */ \
11416 + 1 /* .debug_abbrev */ \
11417 + 1 /* .debug_line */ \
11418 + 1 /* .debug_loc */ \
11419 + 1 /* .debug_str_offsets */ \
19ac8c2e 11420 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11421 + 1 /* trailing zero */)
11422
11423 memset (&sections, 0, sizeof (sections));
80626a55 11424
73869dc2 11425 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11426 {
11427 asection *sectp;
11428 uint32_t section_nr =
11429 read_4_bytes (dbfd,
73869dc2
DE
11430 dwp_htab->section_pool.v1.indices
11431 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11432
11433 if (section_nr == 0)
11434 break;
11435 if (section_nr >= dwp_file->num_sections)
11436 {
11437 error (_("Dwarf Error: bad DWP hash table, section number too large"
11438 " [in module %s]"),
11439 dwp_file->name);
11440 }
11441
11442 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11443 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11444 {
11445 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11446 " [in module %s]"),
11447 dwp_file->name);
11448 }
11449 }
11450
11451 if (i < 2
96b79293
TT
11452 || sections.info_or_types.empty ()
11453 || sections.abbrev.empty ())
80626a55
DE
11454 {
11455 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11456 " [in module %s]"),
11457 dwp_file->name);
11458 }
73869dc2 11459 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11460 {
11461 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11462 " [in module %s]"),
11463 dwp_file->name);
11464 }
11465
11466 /* It's easier for the rest of the code if we fake a struct dwo_file and
11467 have dwo_unit "live" in that. At least for now.
11468
11469 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11470 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11471 file, we can combine them back into a virtual DWO file to save space
11472 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11473 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11474
791afaa2
TT
11475 std::string virtual_dwo_name =
11476 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11477 sections.abbrev.get_id (),
11478 sections.line.get_id (),
11479 sections.loc.get_id (),
11480 sections.str_offsets.get_id ());
80626a55 11481 /* Can we use an existing virtual DWO file? */
976ca316 11482 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11483 comp_dir);
80626a55
DE
11484 /* Create one if necessary. */
11485 if (*dwo_file_slot == NULL)
11486 {
6f738b01
SM
11487 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11488 virtual_dwo_name.c_str ());
11489
51ac9db5 11490 dwo_file = new struct dwo_file;
976ca316 11491 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11492 dwo_file->comp_dir = comp_dir;
80626a55
DE
11493 dwo_file->sections.abbrev = sections.abbrev;
11494 dwo_file->sections.line = sections.line;
11495 dwo_file->sections.loc = sections.loc;
11496 dwo_file->sections.macinfo = sections.macinfo;
11497 dwo_file->sections.macro = sections.macro;
11498 dwo_file->sections.str_offsets = sections.str_offsets;
11499 /* The "str" section is global to the entire DWP file. */
11500 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11501 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11502 there's no need to record it in dwo_file.
11503 Also, we can't simply record type sections in dwo_file because
11504 we record a pointer into the vector in dwo_unit. As we collect more
11505 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11506 for it, invalidating all copies of pointers into the previous
11507 contents. */
80626a55
DE
11508 *dwo_file_slot = dwo_file;
11509 }
11510 else
11511 {
6f738b01
SM
11512 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11513 virtual_dwo_name.c_str ());
11514
9a3c8263 11515 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11516 }
80626a55 11517
976ca316 11518 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11519 dwo_unit->dwo_file = dwo_file;
11520 dwo_unit->signature = signature;
8d749320 11521 dwo_unit->section =
976ca316 11522 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11523 *dwo_unit->section = sections.info_or_types;
57d63ce2 11524 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11525
11526 return dwo_unit;
11527}
11528
d2854d8d
CT
11529/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11530 simplify them. Given a pointer to the containing section SECTION, and
11531 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11532 virtual section of just that piece. */
73869dc2
DE
11533
11534static struct dwarf2_section_info
d2854d8d
CT
11535create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11536 struct dwarf2_section_info *section,
11537 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
11538{
11539 struct dwarf2_section_info result;
11540 asection *sectp;
11541
11542 gdb_assert (section != NULL);
11543 gdb_assert (!section->is_virtual);
11544
11545 memset (&result, 0, sizeof (result));
11546 result.s.containing_section = section;
dc4ccb6f 11547 result.is_virtual = true;
73869dc2
DE
11548
11549 if (size == 0)
11550 return result;
11551
96b79293 11552 sectp = section->get_bfd_section ();
73869dc2
DE
11553
11554 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11555 bounds of the real section. This is a pretty-rare event, so just
11556 flag an error (easier) instead of a warning and trying to cope. */
11557 if (sectp == NULL
fd361982 11558 || offset + size > bfd_section_size (sectp))
73869dc2 11559 {
d2854d8d 11560 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 11561 " in section %s [in module %s]"),
fd361982 11562 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 11563 objfile_name (per_objfile->objfile));
73869dc2
DE
11564 }
11565
11566 result.virtual_offset = offset;
11567 result.size = size;
11568 return result;
11569}
11570
11571/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11572 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11573 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11574 This is for DWP version 2 files. */
11575
11576static struct dwo_unit *
976ca316 11577create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 11578 struct dwp_file *dwp_file,
73869dc2
DE
11579 uint32_t unit_index,
11580 const char *comp_dir,
11581 ULONGEST signature, int is_debug_types)
11582{
73869dc2
DE
11583 const struct dwp_hash_table *dwp_htab =
11584 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11585 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11586 const char *kind = is_debug_types ? "TU" : "CU";
11587 struct dwo_file *dwo_file;
11588 struct dwo_unit *dwo_unit;
d2854d8d 11589 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 11590 void **dwo_file_slot;
73869dc2
DE
11591 int i;
11592
11593 gdb_assert (dwp_file->version == 2);
11594
6f738b01
SM
11595 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11596 kind, pulongest (unit_index), hex_string (signature),
11597 dwp_file->name);
73869dc2
DE
11598
11599 /* Fetch the section offsets of this DWO unit. */
11600
11601 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11602
11603 for (i = 0; i < dwp_htab->nr_columns; ++i)
11604 {
11605 uint32_t offset = read_4_bytes (dbfd,
11606 dwp_htab->section_pool.v2.offsets
11607 + (((unit_index - 1) * dwp_htab->nr_columns
11608 + i)
11609 * sizeof (uint32_t)));
11610 uint32_t size = read_4_bytes (dbfd,
11611 dwp_htab->section_pool.v2.sizes
11612 + (((unit_index - 1) * dwp_htab->nr_columns
11613 + i)
11614 * sizeof (uint32_t)));
11615
11616 switch (dwp_htab->section_pool.v2.section_ids[i])
11617 {
11618 case DW_SECT_INFO:
11619 case DW_SECT_TYPES:
11620 sections.info_or_types_offset = offset;
11621 sections.info_or_types_size = size;
11622 break;
11623 case DW_SECT_ABBREV:
11624 sections.abbrev_offset = offset;
11625 sections.abbrev_size = size;
11626 break;
11627 case DW_SECT_LINE:
11628 sections.line_offset = offset;
11629 sections.line_size = size;
11630 break;
11631 case DW_SECT_LOC:
11632 sections.loc_offset = offset;
11633 sections.loc_size = size;
11634 break;
11635 case DW_SECT_STR_OFFSETS:
11636 sections.str_offsets_offset = offset;
11637 sections.str_offsets_size = size;
11638 break;
11639 case DW_SECT_MACINFO:
11640 sections.macinfo_offset = offset;
11641 sections.macinfo_size = size;
11642 break;
11643 case DW_SECT_MACRO:
11644 sections.macro_offset = offset;
11645 sections.macro_size = size;
11646 break;
11647 }
11648 }
11649
11650 /* It's easier for the rest of the code if we fake a struct dwo_file and
11651 have dwo_unit "live" in that. At least for now.
11652
11653 The DWP file can be made up of a random collection of CUs and TUs.
11654 However, for each CU + set of TUs that came from the same original DWO
11655 file, we can combine them back into a virtual DWO file to save space
11656 (fewer struct dwo_file objects to allocate). Remember that for really
11657 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11658
791afaa2
TT
11659 std::string virtual_dwo_name =
11660 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11661 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11662 (long) (sections.line_size ? sections.line_offset : 0),
11663 (long) (sections.loc_size ? sections.loc_offset : 0),
11664 (long) (sections.str_offsets_size
11665 ? sections.str_offsets_offset : 0));
73869dc2 11666 /* Can we use an existing virtual DWO file? */
976ca316 11667 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11668 comp_dir);
73869dc2
DE
11669 /* Create one if necessary. */
11670 if (*dwo_file_slot == NULL)
11671 {
6f738b01
SM
11672 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11673 virtual_dwo_name.c_str ());
11674
51ac9db5 11675 dwo_file = new struct dwo_file;
976ca316 11676 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
11677 dwo_file->comp_dir = comp_dir;
11678 dwo_file->sections.abbrev =
d2854d8d
CT
11679 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11680 sections.abbrev_offset,
11681 sections.abbrev_size);
73869dc2 11682 dwo_file->sections.line =
d2854d8d
CT
11683 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11684 sections.line_offset,
11685 sections.line_size);
73869dc2 11686 dwo_file->sections.loc =
d2854d8d
CT
11687 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11688 sections.loc_offset, sections.loc_size);
73869dc2 11689 dwo_file->sections.macinfo =
d2854d8d
CT
11690 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11691 sections.macinfo_offset,
11692 sections.macinfo_size);
73869dc2 11693 dwo_file->sections.macro =
d2854d8d
CT
11694 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11695 sections.macro_offset,
11696 sections.macro_size);
73869dc2 11697 dwo_file->sections.str_offsets =
d2854d8d
CT
11698 create_dwp_v2_or_v5_section (per_objfile,
11699 &dwp_file->sections.str_offsets,
11700 sections.str_offsets_offset,
11701 sections.str_offsets_size);
73869dc2
DE
11702 /* The "str" section is global to the entire DWP file. */
11703 dwo_file->sections.str = dwp_file->sections.str;
11704 /* The info or types section is assigned below to dwo_unit,
11705 there's no need to record it in dwo_file.
11706 Also, we can't simply record type sections in dwo_file because
11707 we record a pointer into the vector in dwo_unit. As we collect more
11708 types we'll grow the vector and eventually have to reallocate space
11709 for it, invalidating all copies of pointers into the previous
11710 contents. */
11711 *dwo_file_slot = dwo_file;
11712 }
11713 else
11714 {
6f738b01
SM
11715 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11716 virtual_dwo_name.c_str ());
11717
9a3c8263 11718 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11719 }
73869dc2 11720
976ca316 11721 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
11722 dwo_unit->dwo_file = dwo_file;
11723 dwo_unit->signature = signature;
8d749320 11724 dwo_unit->section =
976ca316 11725 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 11726 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 11727 (per_objfile,
d2854d8d
CT
11728 is_debug_types
11729 ? &dwp_file->sections.types
11730 : &dwp_file->sections.info,
11731 sections.info_or_types_offset,
11732 sections.info_or_types_size);
11733 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11734
11735 return dwo_unit;
11736}
11737
11738/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11739 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11740 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11741 This is for DWP version 5 files. */
11742
11743static struct dwo_unit *
11744create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11745 struct dwp_file *dwp_file,
11746 uint32_t unit_index,
11747 const char *comp_dir,
11748 ULONGEST signature, int is_debug_types)
11749{
11750 const struct dwp_hash_table *dwp_htab
11751 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11752 bfd *dbfd = dwp_file->dbfd.get ();
11753 const char *kind = is_debug_types ? "TU" : "CU";
11754 struct dwo_file *dwo_file;
11755 struct dwo_unit *dwo_unit;
11756 struct virtual_v2_or_v5_dwo_sections sections {};
11757 void **dwo_file_slot;
11758
11759 gdb_assert (dwp_file->version == 5);
11760
6f738b01
SM
11761 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11762 kind, pulongest (unit_index), hex_string (signature),
11763 dwp_file->name);
d2854d8d
CT
11764
11765 /* Fetch the section offsets of this DWO unit. */
11766
11767 /* memset (&sections, 0, sizeof (sections)); */
11768
11769 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11770 {
11771 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
11772 dwp_htab->section_pool.v5.offsets
11773 + (((unit_index - 1)
11774 * dwp_htab->nr_columns
11775 + i)
11776 * sizeof (uint32_t)));
d2854d8d 11777 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
11778 dwp_htab->section_pool.v5.sizes
11779 + (((unit_index - 1) * dwp_htab->nr_columns
11780 + i)
11781 * sizeof (uint32_t)));
d2854d8d
CT
11782
11783 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
11784 {
11785 case DW_SECT_ABBREV_V5:
11786 sections.abbrev_offset = offset;
11787 sections.abbrev_size = size;
11788 break;
11789 case DW_SECT_INFO_V5:
11790 sections.info_or_types_offset = offset;
11791 sections.info_or_types_size = size;
11792 break;
11793 case DW_SECT_LINE_V5:
11794 sections.line_offset = offset;
11795 sections.line_size = size;
11796 break;
11797 case DW_SECT_LOCLISTS_V5:
11798 sections.loclists_offset = offset;
11799 sections.loclists_size = size;
11800 break;
11801 case DW_SECT_MACRO_V5:
11802 sections.macro_offset = offset;
11803 sections.macro_size = size;
11804 break;
11805 case DW_SECT_RNGLISTS_V5:
11806 sections.rnglists_offset = offset;
11807 sections.rnglists_size = size;
11808 break;
11809 case DW_SECT_STR_OFFSETS_V5:
11810 sections.str_offsets_offset = offset;
11811 sections.str_offsets_size = size;
11812 break;
11813 case DW_SECT_RESERVED_V5:
11814 default:
11815 break;
11816 }
d2854d8d
CT
11817 }
11818
11819 /* It's easier for the rest of the code if we fake a struct dwo_file and
11820 have dwo_unit "live" in that. At least for now.
11821
11822 The DWP file can be made up of a random collection of CUs and TUs.
11823 However, for each CU + set of TUs that came from the same original DWO
11824 file, we can combine them back into a virtual DWO file to save space
11825 (fewer struct dwo_file objects to allocate). Remember that for really
11826 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11827
11828 std::string virtual_dwo_name =
11829 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
11830 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11831 (long) (sections.line_size ? sections.line_offset : 0),
11832 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11833 (long) (sections.str_offsets_size
11834 ? sections.str_offsets_offset : 0),
11835 (long) (sections.macro_size ? sections.macro_offset : 0),
11836 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
11837 /* Can we use an existing virtual DWO file? */
11838 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
11839 virtual_dwo_name.c_str (),
11840 comp_dir);
d2854d8d
CT
11841 /* Create one if necessary. */
11842 if (*dwo_file_slot == NULL)
11843 {
6f738b01
SM
11844 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11845 virtual_dwo_name.c_str ());
11846
d2854d8d
CT
11847 dwo_file = new struct dwo_file;
11848 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11849 dwo_file->comp_dir = comp_dir;
11850 dwo_file->sections.abbrev =
dda83cd7
SM
11851 create_dwp_v2_or_v5_section (per_objfile,
11852 &dwp_file->sections.abbrev,
11853 sections.abbrev_offset,
11854 sections.abbrev_size);
d2854d8d 11855 dwo_file->sections.line =
dda83cd7
SM
11856 create_dwp_v2_or_v5_section (per_objfile,
11857 &dwp_file->sections.line,
11858 sections.line_offset, sections.line_size);
d2854d8d 11859 dwo_file->sections.macro =
dda83cd7
SM
11860 create_dwp_v2_or_v5_section (per_objfile,
11861 &dwp_file->sections.macro,
11862 sections.macro_offset,
11863 sections.macro_size);
d2854d8d 11864 dwo_file->sections.loclists =
dda83cd7
SM
11865 create_dwp_v2_or_v5_section (per_objfile,
11866 &dwp_file->sections.loclists,
11867 sections.loclists_offset,
11868 sections.loclists_size);
d2854d8d 11869 dwo_file->sections.rnglists =
dda83cd7
SM
11870 create_dwp_v2_or_v5_section (per_objfile,
11871 &dwp_file->sections.rnglists,
11872 sections.rnglists_offset,
11873 sections.rnglists_size);
d2854d8d 11874 dwo_file->sections.str_offsets =
dda83cd7
SM
11875 create_dwp_v2_or_v5_section (per_objfile,
11876 &dwp_file->sections.str_offsets,
11877 sections.str_offsets_offset,
11878 sections.str_offsets_size);
d2854d8d
CT
11879 /* The "str" section is global to the entire DWP file. */
11880 dwo_file->sections.str = dwp_file->sections.str;
11881 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
11882 there's no need to record it in dwo_file.
11883 Also, we can't simply record type sections in dwo_file because
11884 we record a pointer into the vector in dwo_unit. As we collect more
11885 types we'll grow the vector and eventually have to reallocate space
11886 for it, invalidating all copies of pointers into the previous
11887 contents. */
d2854d8d
CT
11888 *dwo_file_slot = dwo_file;
11889 }
11890 else
11891 {
6f738b01
SM
11892 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11893 virtual_dwo_name.c_str ());
11894
d2854d8d
CT
11895 dwo_file = (struct dwo_file *) *dwo_file_slot;
11896 }
11897
11898 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11899 dwo_unit->dwo_file = dwo_file;
11900 dwo_unit->signature = signature;
11901 dwo_unit->section
11902 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11903 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
11904 &dwp_file->sections.info,
11905 sections.info_or_types_offset,
11906 sections.info_or_types_size);
73869dc2
DE
11907 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11908
11909 return dwo_unit;
11910}
11911
57d63ce2
DE
11912/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11913 Returns NULL if the signature isn't found. */
80626a55
DE
11914
11915static struct dwo_unit *
976ca316 11916lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 11917 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11918 ULONGEST signature, int is_debug_types)
80626a55 11919{
57d63ce2
DE
11920 const struct dwp_hash_table *dwp_htab =
11921 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11922 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11923 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11924 uint32_t hash = signature & mask;
11925 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11926 unsigned int i;
11927 void **slot;
870f88f7 11928 struct dwo_unit find_dwo_cu;
80626a55
DE
11929
11930 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11931 find_dwo_cu.signature = signature;
19ac8c2e 11932 slot = htab_find_slot (is_debug_types
48b490f2
TT
11933 ? dwp_file->loaded_tus.get ()
11934 : dwp_file->loaded_cus.get (),
19ac8c2e 11935 &find_dwo_cu, INSERT);
80626a55
DE
11936
11937 if (*slot != NULL)
9a3c8263 11938 return (struct dwo_unit *) *slot;
80626a55
DE
11939
11940 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11941 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11942 {
11943 ULONGEST signature_in_table;
11944
11945 signature_in_table =
57d63ce2 11946 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11947 if (signature_in_table == signature)
11948 {
57d63ce2
DE
11949 uint32_t unit_index =
11950 read_4_bytes (dbfd,
11951 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11952
73869dc2
DE
11953 if (dwp_file->version == 1)
11954 {
976ca316
SM
11955 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
11956 unit_index, comp_dir,
11957 signature, is_debug_types);
73869dc2 11958 }
d2854d8d 11959 else if (dwp_file->version == 2)
73869dc2 11960 {
976ca316
SM
11961 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
11962 unit_index, comp_dir,
11963 signature, is_debug_types);
73869dc2 11964 }
d2854d8d
CT
11965 else /* version == 5 */
11966 {
11967 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11968 unit_index, comp_dir,
11969 signature, is_debug_types);
11970 }
9a3c8263 11971 return (struct dwo_unit *) *slot;
80626a55
DE
11972 }
11973 if (signature_in_table == 0)
11974 return NULL;
11975 hash = (hash + hash2) & mask;
11976 }
11977
11978 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11979 " [in module %s]"),
11980 dwp_file->name);
11981}
11982
ab5088bf 11983/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11984 Open the file specified by FILE_NAME and hand it off to BFD for
11985 preliminary analysis. Return a newly initialized bfd *, which
11986 includes a canonicalized copy of FILE_NAME.
80626a55 11987 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11988 SEARCH_CWD is true if the current directory is to be searched.
11989 It will be searched before debug-file-directory.
13aaf454
DE
11990 If successful, the file is added to the bfd include table of the
11991 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11992 If unable to find/open the file, return NULL.
3019eac3
DE
11993 NOTE: This function is derived from symfile_bfd_open. */
11994
192b62ce 11995static gdb_bfd_ref_ptr
976ca316 11996try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 11997 const char *file_name, int is_dwp, int search_cwd)
3019eac3 11998{
24b9144d 11999 int desc;
9c02c129
DE
12000 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12001 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12002 to debug_file_directory. */
e0cc99a6 12003 const char *search_path;
9c02c129
DE
12004 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12005
e0cc99a6 12006 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12007 if (search_cwd)
12008 {
12009 if (*debug_file_directory != '\0')
e0cc99a6
TT
12010 {
12011 search_path_holder.reset (concat (".", dirname_separator_string,
12012 debug_file_directory,
12013 (char *) NULL));
12014 search_path = search_path_holder.get ();
12015 }
6ac97d4c 12016 else
e0cc99a6 12017 search_path = ".";
6ac97d4c 12018 }
9c02c129 12019 else
e0cc99a6 12020 search_path = debug_file_directory;
3019eac3 12021
56d467f4
CT
12022 /* Add the path for the executable binary to the list of search paths. */
12023 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12024 search_path_holder.reset (concat (objfile_dir.c_str (),
12025 dirname_separator_string,
12026 search_path, nullptr));
12027 search_path = search_path_holder.get ();
12028
24b9144d 12029 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12030 if (is_dwp)
12031 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12032
12033 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12034 desc = openp (search_path, flags, file_name,
3019eac3
DE
12035 O_RDONLY | O_BINARY, &absolute_name);
12036 if (desc < 0)
12037 return NULL;
12038
e0cc99a6
TT
12039 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12040 gnutarget, desc));
9c02c129
DE
12041 if (sym_bfd == NULL)
12042 return NULL;
192b62ce 12043 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12044
192b62ce
TT
12045 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12046 return NULL;
3019eac3 12047
13aaf454
DE
12048 /* Success. Record the bfd as having been included by the objfile's bfd.
12049 This is important because things like demangled_names_hash lives in the
12050 objfile's per_bfd space and may have references to things like symbol
12051 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12052 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12053
3019eac3
DE
12054 return sym_bfd;
12055}
12056
ab5088bf 12057/* Try to open DWO file FILE_NAME.
3019eac3
DE
12058 COMP_DIR is the DW_AT_comp_dir attribute.
12059 The result is the bfd handle of the file.
12060 If there is a problem finding or opening the file, return NULL.
12061 Upon success, the canonicalized path of the file is stored in the bfd,
12062 same as symfile_bfd_open. */
12063
192b62ce 12064static gdb_bfd_ref_ptr
976ca316 12065open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12066 const char *file_name, const char *comp_dir)
3019eac3 12067{
80626a55 12068 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12069 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12070 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12071
12072 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12073
12074 if (comp_dir != NULL)
12075 {
43816ebc
TT
12076 gdb::unique_xmalloc_ptr<char> path_to_try
12077 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12078
12079 /* NOTE: If comp_dir is a relative path, this will also try the
12080 search path, which seems useful. */
976ca316 12081 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12082 0 /*is_dwp*/,
192b62ce 12083 1 /*search_cwd*/));
3019eac3
DE
12084 if (abfd != NULL)
12085 return abfd;
12086 }
12087
12088 /* That didn't work, try debug-file-directory, which, despite its name,
12089 is a list of paths. */
12090
12091 if (*debug_file_directory == '\0')
12092 return NULL;
12093
976ca316 12094 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12095 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12096}
12097
80626a55
DE
12098/* This function is mapped across the sections and remembers the offset and
12099 size of each of the DWO debugging sections we are interested in. */
12100
12101static void
5bb6e9dd
TT
12102dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12103 dwo_sections *dwo_sections)
80626a55 12104{
80626a55
DE
12105 const struct dwop_section_names *names = &dwop_section_names;
12106
fbedd546 12107 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12108 {
049412e3 12109 dwo_sections->abbrev.s.section = sectp;
fd361982 12110 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12111 }
fbedd546 12112 else if (names->info_dwo.matches (sectp->name))
80626a55 12113 {
049412e3 12114 dwo_sections->info.s.section = sectp;
fd361982 12115 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12116 }
fbedd546 12117 else if (names->line_dwo.matches (sectp->name))
80626a55 12118 {
049412e3 12119 dwo_sections->line.s.section = sectp;
fd361982 12120 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12121 }
fbedd546 12122 else if (names->loc_dwo.matches (sectp->name))
80626a55 12123 {
049412e3 12124 dwo_sections->loc.s.section = sectp;
fd361982 12125 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12126 }
fbedd546 12127 else if (names->loclists_dwo.matches (sectp->name))
41144253 12128 {
12129 dwo_sections->loclists.s.section = sectp;
12130 dwo_sections->loclists.size = bfd_section_size (sectp);
12131 }
fbedd546 12132 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12133 {
049412e3 12134 dwo_sections->macinfo.s.section = sectp;
fd361982 12135 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12136 }
fbedd546 12137 else if (names->macro_dwo.matches (sectp->name))
80626a55 12138 {
049412e3 12139 dwo_sections->macro.s.section = sectp;
fd361982 12140 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12141 }
fbedd546 12142 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12143 {
12144 dwo_sections->rnglists.s.section = sectp;
12145 dwo_sections->rnglists.size = bfd_section_size (sectp);
12146 }
fbedd546 12147 else if (names->str_dwo.matches (sectp->name))
80626a55 12148 {
049412e3 12149 dwo_sections->str.s.section = sectp;
fd361982 12150 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12151 }
fbedd546 12152 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12153 {
049412e3 12154 dwo_sections->str_offsets.s.section = sectp;
fd361982 12155 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12156 }
fbedd546 12157 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12158 {
12159 struct dwarf2_section_info type_section;
12160
12161 memset (&type_section, 0, sizeof (type_section));
049412e3 12162 type_section.s.section = sectp;
fd361982 12163 type_section.size = bfd_section_size (sectp);
fd5866f6 12164 dwo_sections->types.push_back (type_section);
80626a55
DE
12165 }
12166}
12167
ab5088bf 12168/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12169 by PER_CU. This is for the non-DWP case.
80626a55 12170 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12171
12172static struct dwo_file *
4ab09049
SM
12173open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12174 const char *comp_dir)
3019eac3 12175{
976ca316 12176 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12177
976ca316 12178 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12179 if (dbfd == NULL)
12180 {
6f738b01
SM
12181 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12182
80626a55
DE
12183 return NULL;
12184 }
263db9a1 12185
51ac9db5 12186 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12187 dwo_file->dwo_name = dwo_name;
12188 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12189 dwo_file->dbfd = std::move (dbfd);
3019eac3 12190
5bb6e9dd
TT
12191 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12192 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12193 &dwo_file->sections);
3019eac3 12194
976ca316
SM
12195 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12196 dwo_file->cus);
3019eac3 12197
d2854d8d
CT
12198 if (cu->per_cu->dwarf_version < 5)
12199 {
12200 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12201 dwo_file->sections.types, dwo_file->tus);
12202 }
12203 else
12204 {
12205 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12206 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12207 rcuh_kind::COMPILE);
d2854d8d 12208 }
3019eac3 12209
6f738b01 12210 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12211
263db9a1 12212 return dwo_file.release ();
3019eac3
DE
12213}
12214
80626a55 12215/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12216 size of each of the DWP debugging sections common to version 1 and 2 that
12217 we are interested in. */
3019eac3 12218
80626a55 12219static void
73869dc2 12220dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12221 dwp_file *dwp_file)
3019eac3 12222{
80626a55
DE
12223 const struct dwop_section_names *names = &dwop_section_names;
12224 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12225
80626a55 12226 /* Record the ELF section number for later lookup: this is what the
73869dc2 12227 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12228 gdb_assert (elf_section_nr < dwp_file->num_sections);
12229 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12230
80626a55 12231 /* Look for specific sections that we need. */
fbedd546 12232 if (names->str_dwo.matches (sectp->name))
80626a55 12233 {
049412e3 12234 dwp_file->sections.str.s.section = sectp;
fd361982 12235 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12236 }
fbedd546 12237 else if (names->cu_index.matches (sectp->name))
80626a55 12238 {
049412e3 12239 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12240 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12241 }
fbedd546 12242 else if (names->tu_index.matches (sectp->name))
80626a55 12243 {
049412e3 12244 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12245 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12246 }
12247}
3019eac3 12248
73869dc2
DE
12249/* This function is mapped across the sections and remembers the offset and
12250 size of each of the DWP version 2 debugging sections that we are interested
12251 in. This is split into a separate function because we don't know if we
d2854d8d 12252 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12253
12254static void
12255dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12256{
9a3c8263 12257 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12258 const struct dwop_section_names *names = &dwop_section_names;
12259 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12260
12261 /* Record the ELF section number for later lookup: this is what the
12262 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12263 gdb_assert (elf_section_nr < dwp_file->num_sections);
12264 dwp_file->elf_sections[elf_section_nr] = sectp;
12265
12266 /* Look for specific sections that we need. */
fbedd546 12267 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12268 {
049412e3 12269 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12270 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12271 }
fbedd546 12272 else if (names->info_dwo.matches (sectp->name))
73869dc2 12273 {
049412e3 12274 dwp_file->sections.info.s.section = sectp;
fd361982 12275 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12276 }
fbedd546 12277 else if (names->line_dwo.matches (sectp->name))
73869dc2 12278 {
049412e3 12279 dwp_file->sections.line.s.section = sectp;
fd361982 12280 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 12281 }
fbedd546 12282 else if (names->loc_dwo.matches (sectp->name))
73869dc2 12283 {
049412e3 12284 dwp_file->sections.loc.s.section = sectp;
fd361982 12285 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 12286 }
fbedd546 12287 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 12288 {
049412e3 12289 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12290 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 12291 }
fbedd546 12292 else if (names->macro_dwo.matches (sectp->name))
73869dc2 12293 {
049412e3 12294 dwp_file->sections.macro.s.section = sectp;
fd361982 12295 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 12296 }
fbedd546 12297 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 12298 {
049412e3 12299 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12300 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 12301 }
fbedd546 12302 else if (names->types_dwo.matches (sectp->name))
73869dc2 12303 {
049412e3 12304 dwp_file->sections.types.s.section = sectp;
fd361982 12305 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12306 }
12307}
12308
d2854d8d
CT
12309/* This function is mapped across the sections and remembers the offset and
12310 size of each of the DWP version 5 debugging sections that we are interested
12311 in. This is split into a separate function because we don't know if we
12312 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12313
12314static void
12315dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12316{
12317 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12318 const struct dwop_section_names *names = &dwop_section_names;
12319 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12320
12321 /* Record the ELF section number for later lookup: this is what the
12322 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12323 gdb_assert (elf_section_nr < dwp_file->num_sections);
12324 dwp_file->elf_sections[elf_section_nr] = sectp;
12325
12326 /* Look for specific sections that we need. */
fbedd546 12327 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
12328 {
12329 dwp_file->sections.abbrev.s.section = sectp;
12330 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12331 }
fbedd546 12332 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
12333 {
12334 dwp_file->sections.info.s.section = sectp;
12335 dwp_file->sections.info.size = bfd_section_size (sectp);
12336 }
fbedd546 12337 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
12338 {
12339 dwp_file->sections.line.s.section = sectp;
12340 dwp_file->sections.line.size = bfd_section_size (sectp);
12341 }
fbedd546 12342 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
12343 {
12344 dwp_file->sections.loclists.s.section = sectp;
12345 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12346 }
fbedd546 12347 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
12348 {
12349 dwp_file->sections.macro.s.section = sectp;
12350 dwp_file->sections.macro.size = bfd_section_size (sectp);
12351 }
fbedd546 12352 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
12353 {
12354 dwp_file->sections.rnglists.s.section = sectp;
12355 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12356 }
fbedd546 12357 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
12358 {
12359 dwp_file->sections.str_offsets.s.section = sectp;
12360 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12361 }
12362}
12363
80626a55 12364/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12365
80626a55
DE
12366static hashval_t
12367hash_dwp_loaded_cutus (const void *item)
12368{
9a3c8263 12369 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12370
80626a55
DE
12371 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12372 return dwo_unit->signature;
3019eac3
DE
12373}
12374
80626a55 12375/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12376
80626a55
DE
12377static int
12378eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12379{
9a3c8263
SM
12380 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12381 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12382
80626a55
DE
12383 return dua->signature == dub->signature;
12384}
3019eac3 12385
80626a55 12386/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12387
48b490f2 12388static htab_up
298e9637 12389allocate_dwp_loaded_cutus_table ()
80626a55 12390{
48b490f2
TT
12391 return htab_up (htab_create_alloc (3,
12392 hash_dwp_loaded_cutus,
12393 eq_dwp_loaded_cutus,
12394 NULL, xcalloc, xfree));
80626a55 12395}
3019eac3 12396
ab5088bf
DE
12397/* Try to open DWP file FILE_NAME.
12398 The result is the bfd handle of the file.
12399 If there is a problem finding or opening the file, return NULL.
12400 Upon success, the canonicalized path of the file is stored in the bfd,
12401 same as symfile_bfd_open. */
12402
192b62ce 12403static gdb_bfd_ref_ptr
976ca316 12404open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12405{
976ca316 12406 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12407 1 /*is_dwp*/,
192b62ce 12408 1 /*search_cwd*/));
6ac97d4c
DE
12409 if (abfd != NULL)
12410 return abfd;
12411
12412 /* Work around upstream bug 15652.
12413 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12414 [Whether that's a "bug" is debatable, but it is getting in our way.]
12415 We have no real idea where the dwp file is, because gdb's realpath-ing
12416 of the executable's path may have discarded the needed info.
12417 [IWBN if the dwp file name was recorded in the executable, akin to
12418 .gnu_debuglink, but that doesn't exist yet.]
12419 Strip the directory from FILE_NAME and search again. */
12420 if (*debug_file_directory != '\0')
12421 {
12422 /* Don't implicitly search the current directory here.
12423 If the user wants to search "." to handle this case,
12424 it must be added to debug-file-directory. */
976ca316
SM
12425 return try_open_dwop_file (per_objfile, lbasename (file_name),
12426 1 /*is_dwp*/,
6ac97d4c
DE
12427 0 /*search_cwd*/);
12428 }
12429
12430 return NULL;
ab5088bf
DE
12431}
12432
80626a55
DE
12433/* Initialize the use of the DWP file for the current objfile.
12434 By convention the name of the DWP file is ${objfile}.dwp.
12435 The result is NULL if it can't be found. */
a766d390 12436
400174b1 12437static std::unique_ptr<struct dwp_file>
976ca316 12438open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12439{
976ca316 12440 struct objfile *objfile = per_objfile->objfile;
80626a55 12441
82bf32bc
JK
12442 /* Try to find first .dwp for the binary file before any symbolic links
12443 resolving. */
6c447423
DE
12444
12445 /* If the objfile is a debug file, find the name of the real binary
12446 file and get the name of dwp file from there. */
d721ba37 12447 std::string dwp_name;
6c447423
DE
12448 if (objfile->separate_debug_objfile_backlink != NULL)
12449 {
12450 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12451 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12452
d721ba37 12453 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12454 }
12455 else
d721ba37
PA
12456 dwp_name = objfile->original_name;
12457
12458 dwp_name += ".dwp";
80626a55 12459
976ca316 12460 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12461 if (dbfd == NULL
12462 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12463 {
12464 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12465 dwp_name = objfile_name (objfile);
12466 dwp_name += ".dwp";
976ca316 12467 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12468 }
12469
80626a55
DE
12470 if (dbfd == NULL)
12471 {
6f738b01
SM
12472 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12473
400174b1 12474 return std::unique_ptr<dwp_file> ();
3019eac3 12475 }
400174b1
TT
12476
12477 const char *name = bfd_get_filename (dbfd.get ());
12478 std::unique_ptr<struct dwp_file> dwp_file
12479 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12480
0a0f4c01 12481 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12482 dwp_file->elf_sections =
976ca316 12483 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12484 dwp_file->num_sections, asection *);
12485
5bb6e9dd
TT
12486 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12487 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12488 dwp_file.get ());
80626a55 12489
976ca316 12490 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12491
976ca316 12492 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12493
73869dc2 12494 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12495 if (dwp_file->cus && dwp_file->tus
12496 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12497 {
12498 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12499 pretty bizarre. We use pulongest here because that's the established
4d65956b 12500 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12501 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12502 " TU version %s [in DWP file %s]"),
12503 pulongest (dwp_file->cus->version),
d721ba37 12504 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12505 }
08302ed2
DE
12506
12507 if (dwp_file->cus)
12508 dwp_file->version = dwp_file->cus->version;
12509 else if (dwp_file->tus)
12510 dwp_file->version = dwp_file->tus->version;
12511 else
12512 dwp_file->version = 2;
73869dc2 12513
5bb6e9dd
TT
12514 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12515 {
12516 if (dwp_file->version == 2)
12517 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12518 dwp_file.get ());
12519 else
12520 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12521 dwp_file.get ());
12522 }
73869dc2 12523
298e9637
SM
12524 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12525 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12526
6f738b01
SM
12527 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12528 dwarf_read_debug_printf (" %s CUs, %s TUs",
12529 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12530 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12531
12532 return dwp_file;
3019eac3 12533}
c906108c 12534
ab5088bf
DE
12535/* Wrapper around open_and_init_dwp_file, only open it once. */
12536
12537static struct dwp_file *
976ca316 12538get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12539{
976ca316 12540 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12541 {
976ca316
SM
12542 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12543 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12544 }
976ca316 12545 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12546}
12547
80626a55
DE
12548/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12549 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12550 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12551 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12552 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12553
12554 This is called, for example, when wanting to read a variable with a
12555 complex location. Therefore we don't want to do file i/o for every call.
12556 Therefore we don't want to look for a DWO file on every call.
12557 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12558 then we check if we've already seen DWO_NAME, and only THEN do we check
12559 for a DWO file.
12560
1c658ad5 12561 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12562 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12563
3019eac3 12564static struct dwo_unit *
4ab09049 12565lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12566 ULONGEST signature, int is_debug_types)
3019eac3 12567{
976ca316
SM
12568 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12569 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12570 const char *kind = is_debug_types ? "TU" : "CU";
12571 void **dwo_file_slot;
3019eac3 12572 struct dwo_file *dwo_file;
80626a55 12573 struct dwp_file *dwp_file;
cb1df416 12574
6a506a2d
DE
12575 /* First see if there's a DWP file.
12576 If we have a DWP file but didn't find the DWO inside it, don't
12577 look for the original DWO file. It makes gdb behave differently
12578 depending on whether one is debugging in the build tree. */
cf2c3c16 12579
976ca316 12580 dwp_file = get_dwp_file (per_objfile);
80626a55 12581 if (dwp_file != NULL)
cf2c3c16 12582 {
80626a55
DE
12583 const struct dwp_hash_table *dwp_htab =
12584 is_debug_types ? dwp_file->tus : dwp_file->cus;
12585
12586 if (dwp_htab != NULL)
12587 {
12588 struct dwo_unit *dwo_cutu =
976ca316
SM
12589 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12590 is_debug_types);
80626a55
DE
12591
12592 if (dwo_cutu != NULL)
12593 {
6f738b01
SM
12594 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12595 kind, hex_string (signature),
12596 host_address_to_string (dwo_cutu));
12597
80626a55
DE
12598 return dwo_cutu;
12599 }
12600 }
12601 }
6a506a2d 12602 else
80626a55 12603 {
6a506a2d 12604 /* No DWP file, look for the DWO file. */
80626a55 12605
976ca316 12606 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 12607 if (*dwo_file_slot == NULL)
80626a55 12608 {
6a506a2d 12609 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12610 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12611 }
6a506a2d 12612 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12613 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12614
6a506a2d 12615 if (dwo_file != NULL)
19c3d4c9 12616 {
6a506a2d
DE
12617 struct dwo_unit *dwo_cutu = NULL;
12618
12619 if (is_debug_types && dwo_file->tus)
12620 {
12621 struct dwo_unit find_dwo_cutu;
12622
12623 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12624 find_dwo_cutu.signature = signature;
9a3c8263 12625 dwo_cutu
b0b6a987
TT
12626 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12627 &find_dwo_cutu);
6a506a2d 12628 }
33c5cd75 12629 else if (!is_debug_types && dwo_file->cus)
80626a55 12630 {
33c5cd75
DB
12631 struct dwo_unit find_dwo_cutu;
12632
12633 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12634 find_dwo_cutu.signature = signature;
b0b6a987 12635 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12636 &find_dwo_cutu);
6a506a2d
DE
12637 }
12638
12639 if (dwo_cutu != NULL)
12640 {
6f738b01
SM
12641 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12642 kind, dwo_name, hex_string (signature),
12643 host_address_to_string (dwo_cutu));
12644
6a506a2d 12645 return dwo_cutu;
80626a55
DE
12646 }
12647 }
2e276125 12648 }
9cdd5dbd 12649
80626a55
DE
12650 /* We didn't find it. This could mean a dwo_id mismatch, or
12651 someone deleted the DWO/DWP file, or the search path isn't set up
12652 correctly to find the file. */
12653
6f738b01
SM
12654 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12655 kind, dwo_name, hex_string (signature));
3019eac3 12656
6656a72d
DE
12657 /* This is a warning and not a complaint because it can be caused by
12658 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12659 {
12660 /* Print the name of the DWP file if we looked there, helps the user
12661 better diagnose the problem. */
791afaa2 12662 std::string dwp_text;
43942612
DE
12663
12664 if (dwp_file != NULL)
791afaa2
TT
12665 dwp_text = string_printf (" [in DWP file %s]",
12666 lbasename (dwp_file->name));
43942612 12667
9d8780f0 12668 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12669 " [in module %s]"),
4ab09049
SM
12670 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12671 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12672 }
3019eac3 12673 return NULL;
5fb290d7
DJ
12674}
12675
80626a55
DE
12676/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12677 See lookup_dwo_cutu_unit for details. */
12678
12679static struct dwo_unit *
4ab09049 12680lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12681 ULONGEST signature)
12682{
4ab09049
SM
12683 gdb_assert (!cu->per_cu->is_debug_types);
12684
12685 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12686}
12687
12688/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12689 See lookup_dwo_cutu_unit for details. */
12690
12691static struct dwo_unit *
4ab09049 12692lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12693{
4ab09049
SM
12694 gdb_assert (cu->per_cu->is_debug_types);
12695
12696 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12697
12698 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12699}
12700
89e63ee4
DE
12701/* Traversal function for queue_and_load_all_dwo_tus. */
12702
12703static int
12704queue_and_load_dwo_tu (void **slot, void *info)
12705{
12706 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12707 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12708 ULONGEST signature = dwo_unit->signature;
d460f660 12709 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12710
12711 if (sig_type != NULL)
12712 {
89e63ee4
DE
12713 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12714 a real dependency of PER_CU on SIG_TYPE. That is detected later
12715 while processing PER_CU. */
0d305d5c
TT
12716 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12717 cu->language))
12718 load_full_type_unit (sig_type, cu->per_objfile);
12719 cu->per_cu->imported_symtabs_push (sig_type);
89e63ee4
DE
12720 }
12721
12722 return 1;
12723}
12724
1b555f17 12725/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12726 The DWO may have the only definition of the type, though it may not be
12727 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12728 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12729
12730static void
1b555f17 12731queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12732{
12733 struct dwo_unit *dwo_unit;
12734 struct dwo_file *dwo_file;
12735
1b555f17
SM
12736 gdb_assert (cu != nullptr);
12737 gdb_assert (!cu->per_cu->is_debug_types);
12738 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12739
1b555f17 12740 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12741 gdb_assert (dwo_unit != NULL);
12742
12743 dwo_file = dwo_unit->dwo_file;
12744 if (dwo_file->tus != NULL)
1b555f17 12745 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
12746}
12747
3019eac3 12748/* Read in various DIEs. */
348e048f 12749
d389af10 12750/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12751 Inherit only the children of the DW_AT_abstract_origin DIE not being
12752 already referenced by DW_AT_abstract_origin from the children of the
12753 current DIE. */
d389af10
JK
12754
12755static void
12756inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12757{
12758 struct die_info *child_die;
791afaa2 12759 sect_offset *offsetp;
d389af10
JK
12760 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12761 struct die_info *origin_die;
12762 /* Iterator of the ORIGIN_DIE children. */
12763 struct die_info *origin_child_die;
d389af10 12764 struct attribute *attr;
cd02d79d
PA
12765 struct dwarf2_cu *origin_cu;
12766 struct pending **origin_previous_list_in_scope;
d389af10
JK
12767
12768 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12769 if (!attr)
12770 return;
12771
cd02d79d
PA
12772 /* Note that following die references may follow to a die in a
12773 different cu. */
12774
12775 origin_cu = cu;
12776 origin_die = follow_die_ref (die, attr, &origin_cu);
12777
12778 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12779 symbols in. */
12780 origin_previous_list_in_scope = origin_cu->list_in_scope;
12781 origin_cu->list_in_scope = cu->list_in_scope;
12782
edb3359d
DJ
12783 if (die->tag != origin_die->tag
12784 && !(die->tag == DW_TAG_inlined_subroutine
12785 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12786 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12787 sect_offset_str (die->sect_off),
12788 sect_offset_str (origin_die->sect_off));
d389af10 12789
34dc0f95
TBA
12790 /* Find if the concrete and abstract trees are structurally the
12791 same. This is a shallow traversal and it is not bullet-proof;
12792 the compiler can trick the debugger into believing that the trees
12793 are isomorphic, whereas they actually are not. However, the
12794 likelyhood of this happening is pretty low, and a full-fledged
12795 check would be an overkill. */
12796 bool are_isomorphic = true;
12797 die_info *concrete_child = die->child;
12798 die_info *abstract_child = origin_die->child;
12799 while (concrete_child != nullptr || abstract_child != nullptr)
12800 {
12801 if (concrete_child == nullptr
12802 || abstract_child == nullptr
12803 || concrete_child->tag != abstract_child->tag)
12804 {
12805 are_isomorphic = false;
12806 break;
12807 }
12808
12809 concrete_child = concrete_child->sibling;
12810 abstract_child = abstract_child->sibling;
12811 }
12812
12813 /* Walk the origin's children in parallel to the concrete children.
12814 This helps match an origin child in case the debug info misses
12815 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12816 origin tree may not have the same tree structure as the concrete
12817 DIE, though. */
12818 die_info *corresponding_abstract_child
12819 = are_isomorphic ? origin_die->child : nullptr;
12820
791afaa2 12821 std::vector<sect_offset> offsets;
d389af10 12822
3ea89b92
PMR
12823 for (child_die = die->child;
12824 child_die && child_die->tag;
436c571c 12825 child_die = child_die->sibling)
3ea89b92
PMR
12826 {
12827 struct die_info *child_origin_die;
12828 struct dwarf2_cu *child_origin_cu;
12829
12830 /* We are trying to process concrete instance entries:
216f72a1 12831 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12832 it's not relevant to our analysis here. i.e. detecting DIEs that are
12833 present in the abstract instance but not referenced in the concrete
12834 one. */
216f72a1 12835 if (child_die->tag == DW_TAG_call_site
dda83cd7 12836 || child_die->tag == DW_TAG_GNU_call_site)
34dc0f95
TBA
12837 {
12838 if (are_isomorphic)
12839 corresponding_abstract_child
12840 = corresponding_abstract_child->sibling;
12841 continue;
12842 }
3ea89b92 12843
c38f313d
DJ
12844 /* For each CHILD_DIE, find the corresponding child of
12845 ORIGIN_DIE. If there is more than one layer of
12846 DW_AT_abstract_origin, follow them all; there shouldn't be,
12847 but GCC versions at least through 4.4 generate this (GCC PR
12848 40573). */
3ea89b92
PMR
12849 child_origin_die = child_die;
12850 child_origin_cu = cu;
c38f313d
DJ
12851 while (1)
12852 {
cd02d79d
PA
12853 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12854 child_origin_cu);
c38f313d
DJ
12855 if (attr == NULL)
12856 break;
cd02d79d
PA
12857 child_origin_die = follow_die_ref (child_origin_die, attr,
12858 &child_origin_cu);
c38f313d
DJ
12859 }
12860
34dc0f95
TBA
12861 /* If missing DW_AT_abstract_origin, try the corresponding child
12862 of the origin. Clang emits such lexical scopes. */
12863 if (child_origin_die == child_die
12864 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12865 && are_isomorphic
12866 && child_die->tag == DW_TAG_lexical_block)
12867 child_origin_die = corresponding_abstract_child;
12868
d389af10
JK
12869 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12870 counterpart may exist. */
c38f313d 12871 if (child_origin_die != child_die)
d389af10 12872 {
edb3359d
DJ
12873 if (child_die->tag != child_origin_die->tag
12874 && !(child_die->tag == DW_TAG_inlined_subroutine
12875 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12876 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12877 "different tags"),
9d8780f0
SM
12878 sect_offset_str (child_die->sect_off),
12879 sect_offset_str (child_origin_die->sect_off));
c38f313d 12880 if (child_origin_die->parent != origin_die)
b98664d3 12881 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12882 "different parents"),
9d8780f0
SM
12883 sect_offset_str (child_die->sect_off),
12884 sect_offset_str (child_origin_die->sect_off));
c38f313d 12885 else
791afaa2 12886 offsets.push_back (child_origin_die->sect_off);
d389af10 12887 }
34dc0f95
TBA
12888
12889 if (are_isomorphic)
12890 corresponding_abstract_child = corresponding_abstract_child->sibling;
d389af10 12891 }
791afaa2
TT
12892 std::sort (offsets.begin (), offsets.end ());
12893 sect_offset *offsets_end = offsets.data () + offsets.size ();
12894 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12895 if (offsetp[-1] == *offsetp)
b98664d3 12896 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12897 "to DIE %s as their abstract origin"),
12898 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12899
791afaa2 12900 offsetp = offsets.data ();
d389af10
JK
12901 origin_child_die = origin_die->child;
12902 while (origin_child_die && origin_child_die->tag)
12903 {
12904 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12905 while (offsetp < offsets_end
9c541725 12906 && *offsetp < origin_child_die->sect_off)
d389af10 12907 offsetp++;
b64f50a1 12908 if (offsetp >= offsets_end
9c541725 12909 || *offsetp > origin_child_die->sect_off)
d389af10 12910 {
adde2bff
DE
12911 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12912 Check whether we're already processing ORIGIN_CHILD_DIE.
12913 This can happen with mutually referenced abstract_origins.
12914 PR 16581. */
12915 if (!origin_child_die->in_process)
12916 process_die (origin_child_die, origin_cu);
d389af10 12917 }
436c571c 12918 origin_child_die = origin_child_die->sibling;
d389af10 12919 }
cd02d79d 12920 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12921
12922 if (cu != origin_cu)
12923 compute_delayed_physnames (origin_cu);
d389af10
JK
12924}
12925
c906108c 12926static void
e7c27a73 12927read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12928{
5e22e966 12929 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 12930 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 12931 struct context_stack *newobj;
c906108c
SS
12932 CORE_ADDR lowpc;
12933 CORE_ADDR highpc;
12934 struct die_info *child_die;
edb3359d 12935 struct attribute *attr, *call_line, *call_file;
15d034d0 12936 const char *name;
e142c38c 12937 CORE_ADDR baseaddr;
801e3a5b 12938 struct block *block;
edb3359d 12939 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12940 std::vector<struct symbol *> template_args;
34eaf542 12941 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12942
12943 if (inlined_func)
12944 {
12945 /* If we do not have call site information, we can't show the
12946 caller of this inlined function. That's too confusing, so
12947 only use the scope for local variables. */
12948 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12949 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12950 if (call_line == NULL || call_file == NULL)
12951 {
12952 read_lexical_block_scope (die, cu);
12953 return;
12954 }
12955 }
c906108c 12956
b3b3bada 12957 baseaddr = objfile->text_section_offset ();
e142c38c 12958
94af9270 12959 name = dwarf2_name (die, cu);
c906108c 12960
e8d05480
JB
12961 /* Ignore functions with missing or empty names. These are actually
12962 illegal according to the DWARF standard. */
12963 if (name == NULL)
12964 {
b98664d3 12965 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12966 sect_offset_str (die->sect_off));
e8d05480
JB
12967 return;
12968 }
12969
12970 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12971 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12972 <= PC_BOUNDS_INVALID)
e8d05480 12973 {
ae4d0c03 12974 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 12975 if (attr == nullptr || !attr->as_boolean ())
b98664d3 12976 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12977 "for subprogram DIE at %s"),
12978 sect_offset_str (die->sect_off));
e8d05480
JB
12979 return;
12980 }
c906108c 12981
3e29f34a
MR
12982 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12983 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12984
34eaf542
TT
12985 /* If we have any template arguments, then we must allocate a
12986 different sort of symbol. */
436c571c 12987 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
12988 {
12989 if (child_die->tag == DW_TAG_template_type_param
12990 || child_die->tag == DW_TAG_template_value_param)
12991 {
8c14c3a3 12992 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 12993 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12994 break;
12995 }
12996 }
12997
702cf3f5 12998 gdb_assert (cu->get_builder () != nullptr);
c24bdb02 12999 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13000 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13001 (struct symbol *) templ_func);
4c2df51b 13002
81873cc8 13003 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13004 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13005 cu->language);
13006
4cecd739
DJ
13007 /* If there is a location expression for DW_AT_frame_base, record
13008 it. */
e142c38c 13009 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13010 if (attr != nullptr)
fe978cb0 13011 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13012
63e43d3a
PMR
13013 /* If there is a location for the static link, record it. */
13014 newobj->static_link = NULL;
13015 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13016 if (attr != nullptr)
63e43d3a 13017 {
224c3ddb
SM
13018 newobj->static_link
13019 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13020 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13021 cu->addr_type ());
63e43d3a
PMR
13022 }
13023
c24bdb02 13024 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13025
639d11d3 13026 if (die->child != NULL)
c906108c 13027 {
639d11d3 13028 child_die = die->child;
c906108c
SS
13029 while (child_die && child_die->tag)
13030 {
34eaf542
TT
13031 if (child_die->tag == DW_TAG_template_type_param
13032 || child_die->tag == DW_TAG_template_value_param)
13033 {
13034 struct symbol *arg = new_symbol (child_die, NULL, cu);
13035
f1078f66 13036 if (arg != NULL)
2f4732b0 13037 template_args.push_back (arg);
34eaf542
TT
13038 }
13039 else
13040 process_die (child_die, cu);
436c571c 13041 child_die = child_die->sibling;
c906108c
SS
13042 }
13043 }
13044
d389af10
JK
13045 inherit_abstract_dies (die, cu);
13046
4a811a97
UW
13047 /* If we have a DW_AT_specification, we might need to import using
13048 directives from the context of the specification DIE. See the
13049 comment in determine_prefix. */
13050 if (cu->language == language_cplus
13051 && dwarf2_attr (die, DW_AT_specification, cu))
13052 {
13053 struct dwarf2_cu *spec_cu = cu;
13054 struct die_info *spec_die = die_specification (die, &spec_cu);
13055
13056 while (spec_die)
13057 {
13058 child_die = spec_die->child;
13059 while (child_die && child_die->tag)
13060 {
13061 if (child_die->tag == DW_TAG_imported_module)
13062 process_die (child_die, spec_cu);
436c571c 13063 child_die = child_die->sibling;
4a811a97
UW
13064 }
13065
13066 /* In some cases, GCC generates specification DIEs that
13067 themselves contain DW_AT_specification attributes. */
13068 spec_die = die_specification (spec_die, &spec_cu);
13069 }
13070 }
13071
c24bdb02 13072 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13073 /* Make a block for the local symbols within. */
c24bdb02 13074 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13075 cstk.static_link, lowpc, highpc);
801e3a5b 13076
df8a16a1 13077 /* For C++, set the block's scope. */
45280282
IB
13078 if ((cu->language == language_cplus
13079 || cu->language == language_fortran
c44af4eb
TT
13080 || cu->language == language_d
13081 || cu->language == language_rust)
4d4ec4e5 13082 && cu->processing_has_namespace_info)
195a3f6c
TT
13083 block_set_scope (block, determine_prefix (die, cu),
13084 &objfile->objfile_obstack);
df8a16a1 13085
801e3a5b
JB
13086 /* If we have address ranges, record them. */
13087 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13088
a60f3166 13089 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13090
34eaf542 13091 /* Attach template arguments to function. */
2f4732b0 13092 if (!template_args.empty ())
34eaf542
TT
13093 {
13094 gdb_assert (templ_func != NULL);
13095
2f4732b0 13096 templ_func->n_template_arguments = template_args.size ();
34eaf542 13097 templ_func->template_arguments
dda83cd7 13098 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13099 templ_func->n_template_arguments);
34eaf542 13100 memcpy (templ_func->template_arguments,
2f4732b0 13101 template_args.data (),
34eaf542 13102 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13103
13104 /* Make sure that the symtab is set on the new symbols. Even
13105 though they don't appear in this symtab directly, other parts
13106 of gdb assume that symbols do, and this is reasonably
13107 true. */
8634679f 13108 for (symbol *sym : template_args)
3e1d3d8c 13109 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13110 }
13111
208d8187
JB
13112 /* In C++, we can have functions nested inside functions (e.g., when
13113 a function declares a class that has methods). This means that
13114 when we finish processing a function scope, we may need to go
13115 back to building a containing block's symbol lists. */
c24bdb02
KS
13116 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13117 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13118
921e78cf
JB
13119 /* If we've finished processing a top-level function, subsequent
13120 symbols go in the file symbol list. */
c24bdb02
KS
13121 if (cu->get_builder ()->outermost_context_p ())
13122 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13123}
13124
13125/* Process all the DIES contained within a lexical block scope. Start
13126 a new scope, process the dies, and then close the scope. */
13127
13128static void
e7c27a73 13129read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13130{
5e22e966 13131 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13132 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13133 CORE_ADDR lowpc, highpc;
13134 struct die_info *child_die;
e142c38c
DJ
13135 CORE_ADDR baseaddr;
13136
b3b3bada 13137 baseaddr = objfile->text_section_offset ();
c906108c
SS
13138
13139 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13140 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13141 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13142 be nasty. Might be easier to properly extend generic blocks to
af34e669 13143 describe ranges. */
e385593e
JK
13144 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13145 {
13146 case PC_BOUNDS_NOT_PRESENT:
13147 /* DW_TAG_lexical_block has no attributes, process its children as if
13148 there was no wrapping by that DW_TAG_lexical_block.
13149 GCC does no longer produces such DWARF since GCC r224161. */
13150 for (child_die = die->child;
13151 child_die != NULL && child_die->tag;
436c571c 13152 child_die = child_die->sibling)
4f7bc5ed
TT
13153 {
13154 /* We might already be processing this DIE. This can happen
13155 in an unusual circumstance -- where a subroutine A
13156 appears lexically in another subroutine B, but A actually
13157 inlines B. The recursion is broken here, rather than in
13158 inherit_abstract_dies, because it seems better to simply
13159 drop concrete children here. */
13160 if (!child_die->in_process)
13161 process_die (child_die, cu);
13162 }
e385593e
JK
13163 return;
13164 case PC_BOUNDS_INVALID:
13165 return;
13166 }
3e29f34a
MR
13167 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13168 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13169
c24bdb02 13170 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13171 if (die->child != NULL)
c906108c 13172 {
639d11d3 13173 child_die = die->child;
c906108c
SS
13174 while (child_die && child_die->tag)
13175 {
e7c27a73 13176 process_die (child_die, cu);
436c571c 13177 child_die = child_die->sibling;
c906108c
SS
13178 }
13179 }
3ea89b92 13180 inherit_abstract_dies (die, cu);
c24bdb02 13181 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13182
c24bdb02
KS
13183 if (*cu->get_builder ()->get_local_symbols () != NULL
13184 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13185 {
801e3a5b 13186 struct block *block
dda83cd7 13187 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13188 cstk.start_addr, highpc);
801e3a5b
JB
13189
13190 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13191 do here, means that recording a parent's ranges entails
13192 walking across all its children's ranges as they appear in
13193 the address map, which is quadratic behavior.
13194
13195 It would be nicer to record the parent's ranges before
13196 traversing its children, simply overriding whatever you find
13197 there. But since we don't even decide whether to create a
13198 block until after we've traversed its children, that's hard
13199 to do. */
801e3a5b 13200 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13201 }
c24bdb02
KS
13202 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13203 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13204}
13205
216f72a1 13206/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13207
13208static void
13209read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13210{
5e22e966 13211 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13212 struct objfile *objfile = per_objfile->objfile;
08feed99 13213 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13214 CORE_ADDR pc, baseaddr;
13215 struct attribute *attr;
13216 struct call_site *call_site, call_site_local;
13217 void **slot;
13218 int nparams;
13219 struct die_info *child_die;
13220
b3b3bada 13221 baseaddr = objfile->text_section_offset ();
96408a79 13222
216f72a1
JK
13223 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13224 if (attr == NULL)
13225 {
13226 /* This was a pre-DWARF-5 GNU extension alias
13227 for DW_AT_call_return_pc. */
13228 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13229 }
96408a79
SA
13230 if (!attr)
13231 {
b98664d3 13232 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13233 "DIE %s [in module %s]"),
13234 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13235 return;
13236 }
95f982e5 13237 pc = attr->as_address () + baseaddr;
3e29f34a 13238 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13239
13240 if (cu->call_site_htab == NULL)
13241 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13242 NULL, &objfile->objfile_obstack,
13243 hashtab_obstack_allocate, NULL);
13244 call_site_local.pc = pc;
13245 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13246 if (*slot != NULL)
13247 {
b98664d3 13248 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13249 "DIE %s [in module %s]"),
13250 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13251 objfile_name (objfile));
96408a79
SA
13252 return;
13253 }
13254
13255 /* Count parameters at the caller. */
13256
13257 nparams = 0;
13258 for (child_die = die->child; child_die && child_die->tag;
436c571c 13259 child_die = child_die->sibling)
96408a79 13260 {
216f72a1 13261 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13262 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13263 {
b98664d3 13264 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13265 "DW_TAG_call_site child DIE %s [in module %s]"),
13266 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13267 objfile_name (objfile));
96408a79
SA
13268 continue;
13269 }
13270
13271 nparams++;
13272 }
13273
224c3ddb
SM
13274 call_site
13275 = ((struct call_site *)
13276 obstack_alloc (&objfile->objfile_obstack,
13277 sizeof (*call_site)
13278 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13279 *slot = call_site;
13280 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13281 call_site->pc = pc;
13282
216f72a1
JK
13283 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13284 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13285 {
13286 struct die_info *func_die;
13287
13288 /* Skip also over DW_TAG_inlined_subroutine. */
13289 for (func_die = die->parent;
13290 func_die && func_die->tag != DW_TAG_subprogram
13291 && func_die->tag != DW_TAG_subroutine_type;
13292 func_die = func_die->parent);
13293
216f72a1
JK
13294 /* DW_AT_call_all_calls is a superset
13295 of DW_AT_call_all_tail_calls. */
96408a79 13296 if (func_die
dda83cd7
SM
13297 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13298 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13299 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13300 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13301 {
13302 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13303 not complete. But keep CALL_SITE for look ups via call_site_htab,
13304 both the initial caller containing the real return address PC and
13305 the final callee containing the current PC of a chain of tail
13306 calls do not need to have the tail call list complete. But any
13307 function candidate for a virtual tail call frame searched via
13308 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13309 determined unambiguously. */
13310 }
13311 else
13312 {
13313 struct type *func_type = NULL;
13314
13315 if (func_die)
13316 func_type = get_die_type (func_die, cu);
13317 if (func_type != NULL)
13318 {
78134374 13319 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13320
13321 /* Enlist this call site to the function. */
13322 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13323 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13324 }
13325 else
b98664d3 13326 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13327 "DIE %s [in module %s]"),
13328 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13329 }
13330 }
13331
216f72a1
JK
13332 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13333 if (attr == NULL)
13334 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13335 if (attr == NULL)
13336 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13337 if (attr == NULL)
216f72a1
JK
13338 {
13339 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13340 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13341 }
96408a79 13342 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13343 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13344 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13345 else if (attr->form_is_block ())
96408a79
SA
13346 {
13347 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13348 struct dwarf_block *block = attr->as_block ();
96408a79 13349
8d749320 13350 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13351 dlbaton->data = block->data;
13352 dlbaton->size = block->size;
a50264ba 13353 dlbaton->per_objfile = per_objfile;
96408a79
SA
13354 dlbaton->per_cu = cu->per_cu;
13355
13356 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13357 }
cd6c91b4 13358 else if (attr->form_is_ref ())
96408a79 13359 {
96408a79
SA
13360 struct dwarf2_cu *target_cu = cu;
13361 struct die_info *target_die;
13362
ac9ec31b 13363 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13364 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13365 if (die_is_declaration (target_die, target_cu))
13366 {
7d45c7c3 13367 const char *target_physname;
9112db09
JK
13368
13369 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13370 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13371 if (target_physname == NULL)
9112db09 13372 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13373 if (target_physname == NULL)
b98664d3 13374 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13375 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 13376 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13377 else
7d455152 13378 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13379 }
13380 else
13381 {
13382 CORE_ADDR lowpc;
13383
13384 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13385 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13386 <= PC_BOUNDS_INVALID)
b98664d3 13387 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13388 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 13389 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13390 else
3e29f34a
MR
13391 {
13392 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13393 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13394 }
96408a79
SA
13395 }
13396 }
13397 else
b98664d3 13398 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13399 "block nor reference, for DIE %s [in module %s]"),
13400 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13401
13402 call_site->per_cu = cu->per_cu;
9f47c707 13403 call_site->per_objfile = per_objfile;
96408a79
SA
13404
13405 for (child_die = die->child;
13406 child_die && child_die->tag;
436c571c 13407 child_die = child_die->sibling)
96408a79 13408 {
96408a79 13409 struct call_site_parameter *parameter;
1788b2d3 13410 struct attribute *loc, *origin;
96408a79 13411
216f72a1 13412 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13413 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13414 {
13415 /* Already printed the complaint above. */
13416 continue;
13417 }
13418
13419 gdb_assert (call_site->parameter_count < nparams);
13420 parameter = &call_site->parameter[call_site->parameter_count];
13421
1788b2d3
JK
13422 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13423 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13424 register is contained in DW_AT_call_value. */
96408a79 13425
24c5c679 13426 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13427 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13428 if (origin == NULL)
13429 {
13430 /* This was a pre-DWARF-5 GNU extension alias
13431 for DW_AT_call_parameter. */
13432 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13433 }
cd6c91b4 13434 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13435 {
1788b2d3 13436 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13437
0826b30a 13438 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13439 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13440 {
13441 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13442 binding can be done only inside one CU. Such referenced DIE
13443 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13444 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13445 "DW_TAG_call_site child DIE %s [in module %s]"),
13446 sect_offset_str (child_die->sect_off),
9c541725 13447 objfile_name (objfile));
d76b7dbc
JK
13448 continue;
13449 }
9c541725
PA
13450 parameter->u.param_cu_off
13451 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13452 }
4fc6c0d5 13453 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13454 {
b98664d3 13455 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13456 "DW_TAG_call_site child DIE %s [in module %s]"),
13457 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13458 continue;
13459 }
24c5c679 13460 else
96408a79 13461 {
9d2246fc
TT
13462 struct dwarf_block *block = loc->as_block ();
13463
24c5c679 13464 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 13465 (block->data, &block->data[block->size]);
24c5c679
JK
13466 if (parameter->u.dwarf_reg != -1)
13467 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
13468 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13469 &block->data[block->size],
24c5c679
JK
13470 &parameter->u.fb_offset))
13471 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13472 else
13473 {
b98664d3 13474 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13475 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13476 "DW_TAG_call_site child DIE %s "
24c5c679 13477 "[in module %s]"),
9d8780f0 13478 sect_offset_str (child_die->sect_off),
9c541725 13479 objfile_name (objfile));
24c5c679
JK
13480 continue;
13481 }
96408a79
SA
13482 }
13483
216f72a1
JK
13484 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13485 if (attr == NULL)
13486 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13487 if (attr == NULL || !attr->form_is_block ())
96408a79 13488 {
b98664d3 13489 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13490 "DW_TAG_call_site child DIE %s [in module %s]"),
13491 sect_offset_str (child_die->sect_off),
9c541725 13492 objfile_name (objfile));
96408a79
SA
13493 continue;
13494 }
9d2246fc
TT
13495
13496 struct dwarf_block *block = attr->as_block ();
13497 parameter->value = block->data;
13498 parameter->value_size = block->size;
96408a79
SA
13499
13500 /* Parameters are not pre-cleared by memset above. */
13501 parameter->data_value = NULL;
13502 parameter->data_value_size = 0;
13503 call_site->parameter_count++;
13504
216f72a1
JK
13505 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13506 if (attr == NULL)
13507 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13508 if (attr != nullptr)
96408a79 13509 {
4fc6c0d5 13510 if (!attr->form_is_block ())
b98664d3 13511 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13512 "DW_TAG_call_site child DIE %s [in module %s]"),
13513 sect_offset_str (child_die->sect_off),
9c541725 13514 objfile_name (objfile));
96408a79
SA
13515 else
13516 {
9d2246fc
TT
13517 block = attr->as_block ();
13518 parameter->data_value = block->data;
13519 parameter->data_value_size = block->size;
96408a79
SA
13520 }
13521 }
13522 }
13523}
13524
71a3c369
TT
13525/* Helper function for read_variable. If DIE represents a virtual
13526 table, then return the type of the concrete object that is
13527 associated with the virtual table. Otherwise, return NULL. */
13528
13529static struct type *
13530rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13531{
13532 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13533 if (attr == NULL)
13534 return NULL;
13535
13536 /* Find the type DIE. */
13537 struct die_info *type_die = NULL;
13538 struct dwarf2_cu *type_cu = cu;
13539
cd6c91b4 13540 if (attr->form_is_ref ())
71a3c369
TT
13541 type_die = follow_die_ref (die, attr, &type_cu);
13542 if (type_die == NULL)
13543 return NULL;
13544
13545 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13546 return NULL;
13547 return die_containing_type (type_die, type_cu);
13548}
13549
13550/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13551
13552static void
13553read_variable (struct die_info *die, struct dwarf2_cu *cu)
13554{
13555 struct rust_vtable_symbol *storage = NULL;
13556
13557 if (cu->language == language_rust)
13558 {
13559 struct type *containing_type = rust_containing_type (die, cu);
13560
13561 if (containing_type != NULL)
13562 {
5e22e966 13563 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13564
8c14c3a3 13565 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13566 storage->concrete_type = containing_type;
cf724bc9 13567 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13568 }
13569 }
13570
e4a62c65
TV
13571 struct symbol *res = new_symbol (die, NULL, cu, storage);
13572 struct attribute *abstract_origin
13573 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13574 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13575 if (res == NULL && loc && abstract_origin)
13576 {
13577 /* We have a variable without a name, but with a location and an abstract
13578 origin. This may be a concrete instance of an abstract variable
13579 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13580 later. */
13581 struct dwarf2_cu *origin_cu = cu;
13582 struct die_info *origin_die
13583 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13584 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13585 per_objfile->per_bfd->abstract_to_concrete
13586 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13587 }
71a3c369
TT
13588}
13589
43988095
JK
13590/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13591 reading .debug_rnglists.
13592 Callback's type should be:
13593 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13594 Return true if the attributes are present and valid, otherwise,
13595 return false. */
13596
13597template <typename Callback>
13598static bool
13599dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 13600 dwarf_tag tag, Callback &&callback)
43988095 13601{
976ca316
SM
13602 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13603 struct objfile *objfile = per_objfile->objfile;
43988095 13604 bfd *obfd = objfile->obfd;
43988095 13605 /* Base address selection entry. */
2b24b6e4 13606 gdb::optional<CORE_ADDR> base;
43988095 13607 const gdb_byte *buffer;
43988095
JK
13608 CORE_ADDR baseaddr;
13609 bool overflow = false;
d0ce17d8
CT
13610 ULONGEST addr_index;
13611 struct dwarf2_section_info *rnglists_section;
43988095 13612
43988095 13613 base = cu->base_address;
d0ce17d8
CT
13614 rnglists_section = cu_debug_rnglists_section (cu, tag);
13615 rnglists_section->read (objfile);
43988095 13616
d0ce17d8 13617 if (offset >= rnglists_section->size)
43988095 13618 {
b98664d3 13619 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13620 offset);
13621 return false;
13622 }
d0ce17d8 13623 buffer = rnglists_section->buffer + offset;
43988095 13624
b3b3bada 13625 baseaddr = objfile->text_section_offset ();
43988095
JK
13626
13627 while (1)
13628 {
7814882a
JK
13629 /* Initialize it due to a false compiler warning. */
13630 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
13631 const gdb_byte *buf_end = (rnglists_section->buffer
13632 + rnglists_section->size);
43988095
JK
13633 unsigned int bytes_read;
13634
13635 if (buffer == buf_end)
13636 {
13637 overflow = true;
13638 break;
13639 }
13640 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13641 switch (rlet)
13642 {
13643 case DW_RLE_end_of_list:
13644 break;
13645 case DW_RLE_base_address:
13646 if (buffer + cu->header.addr_size > buf_end)
13647 {
13648 overflow = true;
13649 break;
13650 }
c8a7a66f 13651 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13652 buffer += bytes_read;
13653 break;
dda83cd7
SM
13654 case DW_RLE_base_addressx:
13655 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13656 buffer += bytes_read;
13657 base = read_addr_index (cu, addr_index);
13658 break;
43988095
JK
13659 case DW_RLE_start_length:
13660 if (buffer + cu->header.addr_size > buf_end)
13661 {
13662 overflow = true;
13663 break;
13664 }
c8a7a66f
TT
13665 range_beginning = cu->header.read_address (obfd, buffer,
13666 &bytes_read);
43988095
JK
13667 buffer += bytes_read;
13668 range_end = (range_beginning
13669 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13670 buffer += bytes_read;
13671 if (buffer > buf_end)
13672 {
13673 overflow = true;
13674 break;
13675 }
13676 break;
d0ce17d8 13677 case DW_RLE_startx_length:
dda83cd7
SM
13678 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13679 buffer += bytes_read;
13680 range_beginning = read_addr_index (cu, addr_index);
13681 if (buffer > buf_end)
13682 {
13683 overflow = true;
13684 break;
13685 }
13686 range_end = (range_beginning
13687 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13688 buffer += bytes_read;
13689 break;
43988095
JK
13690 case DW_RLE_offset_pair:
13691 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13692 buffer += bytes_read;
13693 if (buffer > buf_end)
13694 {
13695 overflow = true;
13696 break;
13697 }
13698 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13699 buffer += bytes_read;
13700 if (buffer > buf_end)
13701 {
13702 overflow = true;
13703 break;
13704 }
13705 break;
13706 case DW_RLE_start_end:
13707 if (buffer + 2 * cu->header.addr_size > buf_end)
13708 {
13709 overflow = true;
13710 break;
13711 }
c8a7a66f
TT
13712 range_beginning = cu->header.read_address (obfd, buffer,
13713 &bytes_read);
43988095 13714 buffer += bytes_read;
c8a7a66f 13715 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13716 buffer += bytes_read;
13717 break;
d0ce17d8 13718 case DW_RLE_startx_endx:
dda83cd7
SM
13719 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13720 buffer += bytes_read;
13721 range_beginning = read_addr_index (cu, addr_index);
13722 if (buffer > buf_end)
13723 {
13724 overflow = true;
13725 break;
13726 }
13727 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13728 buffer += bytes_read;
13729 range_end = read_addr_index (cu, addr_index);
13730 break;
43988095 13731 default:
b98664d3 13732 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13733 return false;
13734 }
13735 if (rlet == DW_RLE_end_of_list || overflow)
13736 break;
13737 if (rlet == DW_RLE_base_address)
13738 continue;
13739
43988095
JK
13740 if (range_beginning > range_end)
13741 {
13742 /* Inverted range entries are invalid. */
b98664d3 13743 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13744 return false;
13745 }
13746
13747 /* Empty range entries have no effect. */
13748 if (range_beginning == range_end)
13749 continue;
13750
d0ce17d8
CT
13751 /* Only DW_RLE_offset_pair needs the base address added. */
13752 if (rlet == DW_RLE_offset_pair)
13753 {
13754 if (!base.has_value ())
13755 {
13756 /* We have no valid base address for the DW_RLE_offset_pair. */
13757 complaint (_("Invalid .debug_rnglists data (no base address for "
13758 "DW_RLE_offset_pair)"));
13759 return false;
13760 }
13761
13762 range_beginning += *base;
13763 range_end += *base;
13764 }
43988095
JK
13765
13766 /* A not-uncommon case of bad debug info.
13767 Don't pollute the addrmap with bad data. */
13768 if (range_beginning + baseaddr == 0
976ca316 13769 && !per_objfile->per_bfd->has_section_at_zero)
43988095 13770 {
b98664d3 13771 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13772 " [in module %s]"), objfile_name (objfile));
13773 continue;
13774 }
13775
13776 callback (range_beginning, range_end);
13777 }
13778
13779 if (overflow)
13780 {
b98664d3 13781 complaint (_("Offset %d is not terminated "
43988095
JK
13782 "for DW_AT_ranges attribute"),
13783 offset);
13784 return false;
13785 }
13786
13787 return true;
13788}
13789
13790/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13791 Callback's type should be:
13792 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13793 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13794
43988095 13795template <typename Callback>
43039443 13796static int
d0ce17d8 13797dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 13798 Callback &&callback)
43039443 13799{
5e22e966
SM
13800 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13801 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13802 struct comp_unit_head *cu_header = &cu->header;
13803 bfd *obfd = objfile->obfd;
13804 unsigned int addr_size = cu_header->addr_size;
13805 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13806 /* Base address selection entry. */
2b24b6e4 13807 gdb::optional<CORE_ADDR> base;
43039443 13808 unsigned int dummy;
d521ce57 13809 const gdb_byte *buffer;
ff013f42 13810 CORE_ADDR baseaddr;
43039443 13811
43988095 13812 if (cu_header->version >= 5)
d0ce17d8 13813 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 13814
d00adf39 13815 base = cu->base_address;
43039443 13816
5e22e966
SM
13817 per_objfile->per_bfd->ranges.read (objfile);
13818 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13819 {
b98664d3 13820 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13821 offset);
13822 return 0;
13823 }
5e22e966 13824 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13825
b3b3bada 13826 baseaddr = objfile->text_section_offset ();
ff013f42 13827
43039443
JK
13828 while (1)
13829 {
13830 CORE_ADDR range_beginning, range_end;
13831
c8a7a66f 13832 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13833 buffer += addr_size;
c8a7a66f 13834 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13835 buffer += addr_size;
13836 offset += 2 * addr_size;
13837
13838 /* An end of list marker is a pair of zero addresses. */
13839 if (range_beginning == 0 && range_end == 0)
13840 /* Found the end of list entry. */
13841 break;
13842
13843 /* Each base address selection entry is a pair of 2 values.
13844 The first is the largest possible address, the second is
13845 the base address. Check for a base address here. */
13846 if ((range_beginning & mask) == mask)
13847 {
28d2bfb9
AB
13848 /* If we found the largest possible address, then we already
13849 have the base address in range_end. */
13850 base = range_end;
43039443
JK
13851 continue;
13852 }
13853
2b24b6e4 13854 if (!base.has_value ())
43039443
JK
13855 {
13856 /* We have no valid base address for the ranges
13857 data. */
b98664d3 13858 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13859 return 0;
13860 }
13861
9277c30c
UW
13862 if (range_beginning > range_end)
13863 {
13864 /* Inverted range entries are invalid. */
b98664d3 13865 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13866 return 0;
13867 }
13868
13869 /* Empty range entries have no effect. */
13870 if (range_beginning == range_end)
13871 continue;
13872
2b24b6e4
TT
13873 range_beginning += *base;
13874 range_end += *base;
43039443 13875
01093045
DE
13876 /* A not-uncommon case of bad debug info.
13877 Don't pollute the addrmap with bad data. */
13878 if (range_beginning + baseaddr == 0
5e22e966 13879 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13880 {
b98664d3 13881 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13882 " [in module %s]"), objfile_name (objfile));
01093045
DE
13883 continue;
13884 }
13885
5f46c5a5
JK
13886 callback (range_beginning, range_end);
13887 }
13888
13889 return 1;
13890}
13891
13892/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13893 Return 1 if the attributes are present and valid, otherwise, return 0.
efd7398e 13894 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
5f46c5a5
JK
13895
13896static int
13897dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13898 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 13899 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 13900{
5e22e966 13901 struct objfile *objfile = cu->per_objfile->objfile;
84685904 13902 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 13903 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13904 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13905 int low_set = 0;
13906 CORE_ADDR low = 0;
13907 CORE_ADDR high = 0;
13908 int retval;
13909
d0ce17d8 13910 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
13911 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13912 {
9277c30c 13913 if (ranges_pst != NULL)
3e29f34a
MR
13914 {
13915 CORE_ADDR lowpc;
13916 CORE_ADDR highpc;
13917
79748972
TT
13918 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13919 range_beginning + baseaddr)
13920 - baseaddr);
13921 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13922 range_end + baseaddr)
13923 - baseaddr);
84685904 13924 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 13925 lowpc, highpc - 1, ranges_pst);
3e29f34a 13926 }
ff013f42 13927
43039443
JK
13928 /* FIXME: This is recording everything as a low-high
13929 segment of consecutive addresses. We should have a
13930 data structure for discontiguous block ranges
13931 instead. */
13932 if (! low_set)
13933 {
13934 low = range_beginning;
13935 high = range_end;
13936 low_set = 1;
13937 }
13938 else
13939 {
13940 if (range_beginning < low)
13941 low = range_beginning;
13942 if (range_end > high)
13943 high = range_end;
13944 }
5f46c5a5
JK
13945 });
13946 if (!retval)
13947 return 0;
43039443
JK
13948
13949 if (! low_set)
13950 /* If the first entry is an end-of-list marker, the range
13951 describes an empty scope, i.e. no instructions. */
13952 return 0;
13953
13954 if (low_return)
13955 *low_return = low;
13956 if (high_return)
13957 *high_return = high;
13958 return 1;
13959}
13960
3a2b436a
JK
13961/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13962 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13963 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13964
3a2b436a 13965static enum pc_bounds_kind
af34e669 13966dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13967 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13968 dwarf2_psymtab *pst)
c906108c 13969{
976ca316 13970 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 13971 struct attribute *attr;
91da1414 13972 struct attribute *attr_high;
af34e669
DJ
13973 CORE_ADDR low = 0;
13974 CORE_ADDR high = 0;
e385593e 13975 enum pc_bounds_kind ret;
c906108c 13976
91da1414
MW
13977 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13978 if (attr_high)
af34e669 13979 {
e142c38c 13980 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13981 if (attr != nullptr)
dda83cd7 13982 {
95f982e5
TT
13983 low = attr->as_address ();
13984 high = attr_high->as_address ();
cd6c91b4 13985 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13986 high += low;
91da1414 13987 }
af34e669
DJ
13988 else
13989 /* Found high w/o low attribute. */
e385593e 13990 return PC_BOUNDS_INVALID;
af34e669
DJ
13991
13992 /* Found consecutive range of addresses. */
3a2b436a 13993 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13994 }
c906108c 13995 else
af34e669 13996 {
e142c38c 13997 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 13998 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 13999 {
2b0c7f41
SM
14000 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14001 on DWARF version). */
14002 ULONGEST ranges_offset = attr->as_unsigned ();
14003
14004 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14005 this value. */
14006 if (die->tag != DW_TAG_compile_unit)
14007 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14008
af34e669 14009 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14010 .debug_ranges section. */
d0ce17d8
CT
14011 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14012 die->tag))
e385593e 14013 return PC_BOUNDS_INVALID;
43039443 14014 /* Found discontinuous range of addresses. */
3a2b436a 14015 ret = PC_BOUNDS_RANGES;
af34e669 14016 }
e385593e
JK
14017 else
14018 return PC_BOUNDS_NOT_PRESENT;
af34e669 14019 }
c906108c 14020
48fbe735 14021 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14022 if (high <= low)
e385593e 14023 return PC_BOUNDS_INVALID;
c906108c
SS
14024
14025 /* When using the GNU linker, .gnu.linkonce. sections are used to
14026 eliminate duplicate copies of functions and vtables and such.
14027 The linker will arbitrarily choose one and discard the others.
14028 The AT_*_pc values for such functions refer to local labels in
14029 these sections. If the section from that file was discarded, the
14030 labels are not in the output, so the relocs get a value of 0.
14031 If this is a discarded function, mark the pc bounds as invalid,
14032 so that GDB will ignore it. */
976ca316 14033 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14034 return PC_BOUNDS_INVALID;
c906108c
SS
14035
14036 *lowpc = low;
96408a79
SA
14037 if (highpc)
14038 *highpc = high;
af34e669 14039 return ret;
c906108c
SS
14040}
14041
b084d499
JB
14042/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14043 its low and high PC addresses. Do nothing if these addresses could not
14044 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14045 and HIGHPC to the high address if greater than HIGHPC. */
14046
14047static void
14048dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14049 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14050 struct dwarf2_cu *cu)
b084d499
JB
14051{
14052 CORE_ADDR low, high;
14053 struct die_info *child = die->child;
14054
e385593e 14055 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14056 {
325fac50
PA
14057 *lowpc = std::min (*lowpc, low);
14058 *highpc = std::max (*highpc, high);
b084d499
JB
14059 }
14060
14061 /* If the language does not allow nested subprograms (either inside
14062 subprograms or lexical blocks), we're done. */
14063 if (cu->language != language_ada)
14064 return;
6e70227d 14065
b084d499
JB
14066 /* Check all the children of the given DIE. If it contains nested
14067 subprograms, then check their pc bounds. Likewise, we need to
14068 check lexical blocks as well, as they may also contain subprogram
14069 definitions. */
14070 while (child && child->tag)
14071 {
14072 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14073 || child->tag == DW_TAG_lexical_block)
14074 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14075 child = child->sibling;
b084d499
JB
14076 }
14077}
14078
fae299cd
DC
14079/* Get the low and high pc's represented by the scope DIE, and store
14080 them in *LOWPC and *HIGHPC. If the correct values can't be
14081 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14082
14083static void
14084get_scope_pc_bounds (struct die_info *die,
14085 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14086 struct dwarf2_cu *cu)
14087{
14088 CORE_ADDR best_low = (CORE_ADDR) -1;
14089 CORE_ADDR best_high = (CORE_ADDR) 0;
14090 CORE_ADDR current_low, current_high;
14091
3a2b436a 14092 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14093 >= PC_BOUNDS_RANGES)
fae299cd
DC
14094 {
14095 best_low = current_low;
14096 best_high = current_high;
14097 }
14098 else
14099 {
14100 struct die_info *child = die->child;
14101
14102 while (child && child->tag)
14103 {
14104 switch (child->tag) {
14105 case DW_TAG_subprogram:
dda83cd7 14106 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14107 break;
14108 case DW_TAG_namespace:
f55ee35c 14109 case DW_TAG_module:
fae299cd
DC
14110 /* FIXME: carlton/2004-01-16: Should we do this for
14111 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14112 that current GCC's always emit the DIEs corresponding
14113 to definitions of methods of classes as children of a
14114 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14115 the DIEs giving the declarations, which could be
14116 anywhere). But I don't see any reason why the
14117 standards says that they have to be there. */
14118 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14119
14120 if (current_low != ((CORE_ADDR) -1))
14121 {
325fac50
PA
14122 best_low = std::min (best_low, current_low);
14123 best_high = std::max (best_high, current_high);
fae299cd
DC
14124 }
14125 break;
14126 default:
0963b4bd 14127 /* Ignore. */
fae299cd
DC
14128 break;
14129 }
14130
436c571c 14131 child = child->sibling;
fae299cd
DC
14132 }
14133 }
14134
14135 *lowpc = best_low;
14136 *highpc = best_high;
14137}
14138
801e3a5b
JB
14139/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14140 in DIE. */
380bca97 14141
801e3a5b
JB
14142static void
14143dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14144 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14145{
5e22e966 14146 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14147 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14148 struct attribute *attr;
91da1414 14149 struct attribute *attr_high;
801e3a5b 14150
91da1414
MW
14151 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14152 if (attr_high)
801e3a5b 14153 {
801e3a5b 14154 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14155 if (attr != nullptr)
dda83cd7 14156 {
95f982e5
TT
14157 CORE_ADDR low = attr->as_address ();
14158 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14159
cd6c91b4 14160 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14161 high += low;
9a619af0 14162
3e29f34a
MR
14163 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14164 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14165 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14166 }
801e3a5b
JB
14167 }
14168
14169 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14170 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14171 {
2b0c7f41
SM
14172 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14173 on DWARF version). */
14174 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14175
2b0c7f41
SM
14176 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14177 this value. */
14178 if (die->tag != DW_TAG_compile_unit)
14179 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14180
2d5f09ec 14181 std::vector<blockrange> blockvec;
2b0c7f41 14182 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14183 [&] (CORE_ADDR start, CORE_ADDR end)
14184 {
58fdfd2c
JK
14185 start += baseaddr;
14186 end += baseaddr;
5f46c5a5
JK
14187 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14188 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14189 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14190 blockvec.emplace_back (start, end);
5f46c5a5 14191 });
2d5f09ec
KB
14192
14193 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14194 }
14195}
14196
685b1105
JK
14197/* Check whether the producer field indicates either of GCC < 4.6, or the
14198 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14199
685b1105
JK
14200static void
14201check_producer (struct dwarf2_cu *cu)
60d5a603 14202{
38360086 14203 int major, minor;
60d5a603
JK
14204
14205 if (cu->producer == NULL)
14206 {
14207 /* For unknown compilers expect their behavior is DWARF version
14208 compliant.
14209
14210 GCC started to support .debug_types sections by -gdwarf-4 since
14211 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14212 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14213 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14214 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14215 }
b1ffba5a 14216 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14217 {
38360086
MW
14218 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14219 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14220 }
5230b05a 14221 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14222 {
14223 cu->producer_is_icc = true;
14224 cu->producer_is_icc_lt_14 = major < 14;
14225 }
c258c396
JD
14226 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14227 cu->producer_is_codewarrior = true;
685b1105
JK
14228 else
14229 {
14230 /* For other non-GCC compilers, expect their behavior is DWARF version
14231 compliant. */
60d5a603
JK
14232 }
14233
9068261f 14234 cu->checked_producer = true;
685b1105 14235}
ba919b58 14236
685b1105
JK
14237/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14238 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14239 during 4.6.0 experimental. */
14240
9068261f 14241static bool
685b1105
JK
14242producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14243{
14244 if (!cu->checked_producer)
14245 check_producer (cu);
14246
14247 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14248}
14249
c258c396
JD
14250
14251/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14252 with incorrect is_stmt attributes. */
14253
14254static bool
14255producer_is_codewarrior (struct dwarf2_cu *cu)
14256{
14257 if (!cu->checked_producer)
14258 check_producer (cu);
14259
14260 return cu->producer_is_codewarrior;
14261}
14262
bf23a268
TT
14263/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14264 If that attribute is not available, return the appropriate
14265 default. */
60d5a603
JK
14266
14267static enum dwarf_access_attribute
bf23a268 14268dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14269{
bf23a268
TT
14270 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14271 if (attr != nullptr)
14272 {
14273 LONGEST value = attr->constant_value (-1);
14274 if (value == DW_ACCESS_public
14275 || value == DW_ACCESS_protected
14276 || value == DW_ACCESS_private)
14277 return (dwarf_access_attribute) value;
14278 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14279 plongest (value));
14280 }
14281
60d5a603
JK
14282 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14283 {
14284 /* The default DWARF 2 accessibility for members is public, the default
14285 accessibility for inheritance is private. */
14286
14287 if (die->tag != DW_TAG_inheritance)
14288 return DW_ACCESS_public;
14289 else
14290 return DW_ACCESS_private;
14291 }
14292 else
14293 {
14294 /* DWARF 3+ defines the default accessibility a different way. The same
14295 rules apply now for DW_TAG_inheritance as for the members and it only
14296 depends on the container kind. */
14297
14298 if (die->parent->tag == DW_TAG_class_type)
14299 return DW_ACCESS_private;
14300 else
14301 return DW_ACCESS_public;
14302 }
14303}
14304
74ac6d43
TT
14305/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14306 offset. If the attribute was not found return 0, otherwise return
14307 1. If it was found but could not properly be handled, set *OFFSET
14308 to 0. */
14309
14310static int
14311handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14312 LONGEST *offset)
14313{
14314 struct attribute *attr;
14315
14316 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14317 if (attr != NULL)
14318 {
14319 *offset = 0;
14320
14321 /* Note that we do not check for a section offset first here.
14322 This is because DW_AT_data_member_location is new in DWARF 4,
14323 so if we see it, we can assume that a constant form is really
14324 a constant and not a section offset. */
cd6c91b4 14325 if (attr->form_is_constant ())
0826b30a 14326 *offset = attr->constant_value (0);
cd6c91b4 14327 else if (attr->form_is_section_offset ())
74ac6d43 14328 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14329 else if (attr->form_is_block ())
9d2246fc 14330 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14331 else
14332 dwarf2_complex_location_expr_complaint ();
14333
14334 return 1;
14335 }
14336
14337 return 0;
14338}
14339
7d79de9a
TT
14340/* Look for DW_AT_data_member_location and store the results in FIELD. */
14341
14342static void
14343handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14344 struct field *field)
14345{
14346 struct attribute *attr;
14347
14348 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14349 if (attr != NULL)
14350 {
14351 if (attr->form_is_constant ())
14352 {
14353 LONGEST offset = attr->constant_value (0);
14354 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14355 }
14356 else if (attr->form_is_section_offset ())
14357 dwarf2_complex_location_expr_complaint ();
14358 else if (attr->form_is_block ())
14359 {
14360 bool handled;
9d2246fc 14361 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14362 if (handled)
14363 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14364 else
14365 {
5e22e966
SM
14366 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14367 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14368 struct dwarf2_locexpr_baton *dlbaton
14369 = XOBNEW (&objfile->objfile_obstack,
14370 struct dwarf2_locexpr_baton);
9d2246fc
TT
14371 dlbaton->data = attr->as_block ()->data;
14372 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14373 /* When using this baton, we want to compute the address
14374 of the field, not the value. This is why
14375 is_reference is set to false here. */
14376 dlbaton->is_reference = false;
5e22e966 14377 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14378 dlbaton->per_cu = cu->per_cu;
14379
14380 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14381 }
14382 }
14383 else
14384 dwarf2_complex_location_expr_complaint ();
14385 }
14386}
14387
c906108c
SS
14388/* Add an aggregate field to the field list. */
14389
14390static void
107d2387 14391dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14392 struct dwarf2_cu *cu)
6e70227d 14393{
5e22e966 14394 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14395 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14396 struct nextfield *new_field;
14397 struct attribute *attr;
14398 struct field *fp;
15d034d0 14399 const char *fieldname = "";
c906108c 14400
7d0ccb61
DJ
14401 if (die->tag == DW_TAG_inheritance)
14402 {
be2daae6
TT
14403 fip->baseclasses.emplace_back ();
14404 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14405 }
14406 else
14407 {
be2daae6
TT
14408 fip->fields.emplace_back ();
14409 new_field = &fip->fields.back ();
7d0ccb61 14410 }
be2daae6 14411
9c6a1327
TT
14412 new_field->offset = die->sect_off;
14413
bf23a268 14414 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 14415 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 14416 fip->non_public_fields = true;
60d5a603 14417
e142c38c 14418 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14419 if (attr != nullptr)
23dca5c3 14420 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
14421 else
14422 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14423
14424 fp = &new_field->field;
a9a9bd0f 14425
e142c38c 14426 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14427 {
a9a9bd0f 14428 /* Data member other than a C++ static data member. */
6e70227d 14429
c906108c 14430 /* Get type of field. */
5d14b6e5 14431 fp->set_type (die_type (die, cu));
c906108c 14432
d6a843b5 14433 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14434
c906108c 14435 /* Get bit size of field (zero if none). */
e142c38c 14436 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14437 if (attr != nullptr)
c906108c 14438 {
529908cb 14439 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
14440 }
14441 else
14442 {
14443 FIELD_BITSIZE (*fp) = 0;
14444 }
14445
14446 /* Get bit offset of field. */
7d79de9a 14447 handle_data_member_location (die, cu, fp);
e142c38c 14448 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 14449 if (attr != nullptr && attr->form_is_constant ())
c906108c 14450 {
d5a22e77 14451 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14452 {
14453 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
14454 additional bit offset from the MSB of the containing
14455 anonymous object to the MSB of the field. We don't
14456 have to do anything special since we don't need to
14457 know the size of the anonymous object. */
529908cb 14458 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 14459 + attr->constant_value (0)));
c906108c
SS
14460 }
14461 else
14462 {
14463 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
14464 MSB of the anonymous object, subtract off the number of
14465 bits from the MSB of the field to the MSB of the
14466 object, and then subtract off the number of bits of
14467 the field itself. The result is the bit offset of
14468 the LSB of the field. */
c906108c 14469 int anonymous_size;
7c184d33 14470 int bit_offset = attr->constant_value (0);
c906108c 14471
e142c38c 14472 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 14473 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
14474 {
14475 /* The size of the anonymous object containing
14476 the bit field is explicit, so use the
14477 indicated size (in bytes). */
7c184d33 14478 anonymous_size = attr->constant_value (0);
c906108c
SS
14479 }
14480 else
14481 {
14482 /* The size of the anonymous object containing
14483 the bit field must be inferred from the type
14484 attribute of the data member containing the
14485 bit field. */
5d14b6e5 14486 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14487 }
f41f5e61
PA
14488 SET_FIELD_BITPOS (*fp,
14489 (FIELD_BITPOS (*fp)
14490 + anonymous_size * bits_per_byte
14491 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14492 }
14493 }
da5b30da
AA
14494 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14495 if (attr != NULL)
14496 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14497 + attr->constant_value (0)));
c906108c
SS
14498
14499 /* Get name of field. */
39cbfefa
DJ
14500 fieldname = dwarf2_name (die, cu);
14501 if (fieldname == NULL)
14502 fieldname = "";
d8151005
DJ
14503
14504 /* The name is already allocated along with this objfile, so we don't
14505 need to duplicate it for the type. */
14506 fp->name = fieldname;
c906108c
SS
14507
14508 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 14509 pointer or virtual base class pointer) to private. */
e142c38c 14510 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14511 {
d48cc9dd 14512 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 14513 new_field->accessibility = DW_ACCESS_private;
264fc0e2 14514 fip->non_public_fields = true;
c906108c
SS
14515 }
14516 }
a9a9bd0f 14517 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14518 {
a9a9bd0f
DC
14519 /* C++ static member. */
14520
14521 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14522 is a declaration, but all versions of G++ as of this writing
14523 (so through at least 3.2.1) incorrectly generate
14524 DW_TAG_variable tags. */
6e70227d 14525
ff355380 14526 const char *physname;
c906108c 14527
a9a9bd0f 14528 /* Get name of field. */
39cbfefa
DJ
14529 fieldname = dwarf2_name (die, cu);
14530 if (fieldname == NULL)
c906108c
SS
14531 return;
14532
254e6b9e 14533 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14534 if (attr
14535 /* Only create a symbol if this is an external value.
14536 new_symbol checks this and puts the value in the global symbol
14537 table, which we want. If it is not external, new_symbol
14538 will try to put the value in cu->list_in_scope which is wrong. */
14539 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14540 {
14541 /* A static const member, not much different than an enum as far as
14542 we're concerned, except that we can support more types. */
14543 new_symbol (die, NULL, cu);
14544 }
14545
2df3850c 14546 /* Get physical name. */
ff355380 14547 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14548
d8151005
DJ
14549 /* The name is already allocated along with this objfile, so we don't
14550 need to duplicate it for the type. */
14551 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 14552 fp->set_type (die_type (die, cu));
d8151005 14553 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14554 }
14555 else if (die->tag == DW_TAG_inheritance)
14556 {
74ac6d43 14557 /* C++ base class field. */
7d79de9a 14558 handle_data_member_location (die, cu, fp);
c906108c 14559 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
14560 fp->set_type (die_type (die, cu));
14561 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 14562 }
2ddeaf8a
TT
14563 else
14564 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14565}
14566
883fd55a
KS
14567/* Can the type given by DIE define another type? */
14568
14569static bool
14570type_can_define_types (const struct die_info *die)
14571{
14572 switch (die->tag)
14573 {
14574 case DW_TAG_typedef:
14575 case DW_TAG_class_type:
14576 case DW_TAG_structure_type:
14577 case DW_TAG_union_type:
14578 case DW_TAG_enumeration_type:
14579 return true;
14580
14581 default:
14582 return false;
14583 }
14584}
14585
14586/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14587
14588static void
883fd55a
KS
14589dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14590 struct dwarf2_cu *cu)
6e70227d 14591{
be2daae6
TT
14592 struct decl_field fp;
14593 memset (&fp, 0, sizeof (fp));
98751a41 14594
883fd55a 14595 gdb_assert (type_can_define_types (die));
98751a41 14596
883fd55a 14597 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14598 fp.name = dwarf2_name (die, cu);
14599 fp.type = read_type_die (die, cu);
98751a41 14600
c191a687 14601 /* Save accessibility. */
bf23a268 14602 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
14603 switch (accessibility)
14604 {
14605 case DW_ACCESS_public:
14606 /* The assumed value if neither private nor protected. */
14607 break;
14608 case DW_ACCESS_private:
be2daae6 14609 fp.is_private = 1;
c191a687
KS
14610 break;
14611 case DW_ACCESS_protected:
be2daae6 14612 fp.is_protected = 1;
c191a687 14613 break;
c191a687
KS
14614 }
14615
883fd55a 14616 if (die->tag == DW_TAG_typedef)
be2daae6 14617 fip->typedef_field_list.push_back (fp);
883fd55a 14618 else
be2daae6 14619 fip->nested_types_list.push_back (fp);
98751a41
JK
14620}
14621
9c6a1327
TT
14622/* A convenience typedef that's used when finding the discriminant
14623 field for a variant part. */
1b95cdb7
SM
14624typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14625 offset_map_type;
9c6a1327
TT
14626
14627/* Compute the discriminant range for a given variant. OBSTACK is
14628 where the results will be stored. VARIANT is the variant to
14629 process. IS_UNSIGNED indicates whether the discriminant is signed
14630 or unsigned. */
14631
14632static const gdb::array_view<discriminant_range>
14633convert_variant_range (struct obstack *obstack, const variant_field &variant,
14634 bool is_unsigned)
14635{
14636 std::vector<discriminant_range> ranges;
14637
14638 if (variant.default_branch)
14639 return {};
14640
14641 if (variant.discr_list_data == nullptr)
14642 {
14643 discriminant_range r
14644 = {variant.discriminant_value, variant.discriminant_value};
14645 ranges.push_back (r);
14646 }
14647 else
14648 {
14649 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14650 variant.discr_list_data->size);
14651 while (!data.empty ())
14652 {
14653 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14654 {
14655 complaint (_("invalid discriminant marker: %d"), data[0]);
14656 break;
14657 }
14658 bool is_range = data[0] == DW_DSC_range;
14659 data = data.slice (1);
14660
14661 ULONGEST low, high;
14662 unsigned int bytes_read;
14663
14664 if (data.empty ())
14665 {
14666 complaint (_("DW_AT_discr_list missing low value"));
14667 break;
14668 }
14669 if (is_unsigned)
14670 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14671 else
14672 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14673 &bytes_read);
14674 data = data.slice (bytes_read);
14675
14676 if (is_range)
14677 {
14678 if (data.empty ())
14679 {
14680 complaint (_("DW_AT_discr_list missing high value"));
14681 break;
14682 }
14683 if (is_unsigned)
14684 high = read_unsigned_leb128 (nullptr, data.data (),
14685 &bytes_read);
14686 else
14687 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14688 &bytes_read);
14689 data = data.slice (bytes_read);
14690 }
14691 else
14692 high = low;
14693
14694 ranges.push_back ({ low, high });
14695 }
14696 }
14697
14698 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14699 ranges.size ());
14700 std::copy (ranges.begin (), ranges.end (), result);
14701 return gdb::array_view<discriminant_range> (result, ranges.size ());
14702}
14703
14704static const gdb::array_view<variant_part> create_variant_parts
14705 (struct obstack *obstack,
14706 const offset_map_type &offset_map,
14707 struct field_info *fi,
14708 const std::vector<variant_part_builder> &variant_parts);
14709
14710/* Fill in a "struct variant" for a given variant field. RESULT is
14711 the variant to fill in. OBSTACK is where any needed allocations
14712 will be done. OFFSET_MAP holds the mapping from section offsets to
14713 fields for the type. FI describes the fields of the type we're
14714 processing. FIELD is the variant field we're converting. */
14715
14716static void
14717create_one_variant (variant &result, struct obstack *obstack,
14718 const offset_map_type &offset_map,
14719 struct field_info *fi, const variant_field &field)
14720{
14721 result.discriminants = convert_variant_range (obstack, field, false);
14722 result.first_field = field.first_field + fi->baseclasses.size ();
14723 result.last_field = field.last_field + fi->baseclasses.size ();
14724 result.parts = create_variant_parts (obstack, offset_map, fi,
14725 field.variant_parts);
14726}
14727
14728/* Fill in a "struct variant_part" for a given variant part. RESULT
14729 is the variant part to fill in. OBSTACK is where any needed
14730 allocations will be done. OFFSET_MAP holds the mapping from
14731 section offsets to fields for the type. FI describes the fields of
14732 the type we're processing. BUILDER is the variant part to be
14733 converted. */
14734
14735static void
14736create_one_variant_part (variant_part &result,
14737 struct obstack *obstack,
14738 const offset_map_type &offset_map,
14739 struct field_info *fi,
14740 const variant_part_builder &builder)
14741{
14742 auto iter = offset_map.find (builder.discriminant_offset);
14743 if (iter == offset_map.end ())
14744 {
14745 result.discriminant_index = -1;
14746 /* Doesn't matter. */
14747 result.is_unsigned = false;
14748 }
14749 else
14750 {
14751 result.discriminant_index = iter->second;
14752 result.is_unsigned
c6d940a9 14753 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
14754 }
14755
14756 size_t n = builder.variants.size ();
14757 variant *output = new (obstack) variant[n];
14758 for (size_t i = 0; i < n; ++i)
14759 create_one_variant (output[i], obstack, offset_map, fi,
14760 builder.variants[i]);
14761
14762 result.variants = gdb::array_view<variant> (output, n);
14763}
14764
14765/* Create a vector of variant parts that can be attached to a type.
14766 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14767 holds the mapping from section offsets to fields for the type. FI
14768 describes the fields of the type we're processing. VARIANT_PARTS
14769 is the vector to convert. */
14770
14771static const gdb::array_view<variant_part>
14772create_variant_parts (struct obstack *obstack,
14773 const offset_map_type &offset_map,
14774 struct field_info *fi,
14775 const std::vector<variant_part_builder> &variant_parts)
14776{
14777 if (variant_parts.empty ())
14778 return {};
14779
14780 size_t n = variant_parts.size ();
14781 variant_part *result = new (obstack) variant_part[n];
14782 for (size_t i = 0; i < n; ++i)
14783 create_one_variant_part (result[i], obstack, offset_map, fi,
14784 variant_parts[i]);
14785
14786 return gdb::array_view<variant_part> (result, n);
14787}
14788
14789/* Compute the variant part vector for FIP, attaching it to TYPE when
14790 done. */
14791
14792static void
14793add_variant_property (struct field_info *fip, struct type *type,
14794 struct dwarf2_cu *cu)
14795{
14796 /* Map section offsets of fields to their field index. Note the
14797 field index here does not take the number of baseclasses into
14798 account. */
14799 offset_map_type offset_map;
14800 for (int i = 0; i < fip->fields.size (); ++i)
14801 offset_map[fip->fields[i].offset] = i;
14802
5e22e966 14803 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14804 gdb::array_view<variant_part> parts
14805 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14806 fip->variant_parts);
14807
14808 struct dynamic_prop prop;
8c2e4e06
SM
14809 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14810 obstack_copy (&objfile->objfile_obstack, &parts,
14811 sizeof (parts)));
9c6a1327 14812
5c54719c 14813 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14814}
14815
c906108c
SS
14816/* Create the vector of fields, and attach it to the type. */
14817
14818static void
fba45db2 14819dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14820 struct dwarf2_cu *cu)
c906108c 14821{
317f7127 14822 int nfields = fip->nfields ();
c906108c
SS
14823
14824 /* Record the field count, allocate space for the array of fields,
14825 and create blank accessibility bitfields if necessary. */
5e33d5f4 14826 type->set_num_fields (nfields);
3cabb6b0
SM
14827 type->set_fields
14828 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14829
b4ba55a1 14830 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14831 {
14832 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14833
14834 TYPE_FIELD_PRIVATE_BITS (type) =
14835 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14836 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14837
14838 TYPE_FIELD_PROTECTED_BITS (type) =
14839 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14840 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14841
774b6a14
TT
14842 TYPE_FIELD_IGNORE_BITS (type) =
14843 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14844 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14845 }
14846
14847 /* If the type has baseclasses, allocate and clear a bit vector for
14848 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14849 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14850 {
be2daae6 14851 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14852 unsigned char *pointer;
c906108c
SS
14853
14854 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14855 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14856 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14857 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14858 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14859 }
14860
9c6a1327
TT
14861 if (!fip->variant_parts.empty ())
14862 add_variant_property (fip, type, cu);
2ddeaf8a 14863
be2daae6
TT
14864 /* Copy the saved-up fields into the field vector. */
14865 for (int i = 0; i < nfields; ++i)
c906108c 14866 {
be2daae6
TT
14867 struct nextfield &field
14868 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14869 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14870
ceacbf6e 14871 type->field (i) = field.field;
be2daae6 14872 switch (field.accessibility)
c906108c 14873 {
c5aa993b 14874 case DW_ACCESS_private:
b4ba55a1 14875 if (cu->language != language_ada)
be2daae6 14876 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14877 break;
c906108c 14878
c5aa993b 14879 case DW_ACCESS_protected:
b4ba55a1 14880 if (cu->language != language_ada)
be2daae6 14881 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14882 break;
c906108c 14883
c5aa993b
JM
14884 case DW_ACCESS_public:
14885 break;
c906108c 14886
c5aa993b
JM
14887 default:
14888 /* Unknown accessibility. Complain and treat it as public. */
14889 {
b98664d3 14890 complaint (_("unsupported accessibility %d"),
be2daae6 14891 field.accessibility);
c5aa993b
JM
14892 }
14893 break;
c906108c 14894 }
be2daae6 14895 if (i < fip->baseclasses.size ())
c906108c 14896 {
be2daae6 14897 switch (field.virtuality)
c906108c 14898 {
c5aa993b
JM
14899 case DW_VIRTUALITY_virtual:
14900 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14901 if (cu->language == language_ada)
a73c6dcd 14902 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14903 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14904 break;
c906108c
SS
14905 }
14906 }
c906108c
SS
14907 }
14908}
14909
7d27a96d
TT
14910/* Return true if this member function is a constructor, false
14911 otherwise. */
14912
14913static int
14914dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14915{
14916 const char *fieldname;
fe978cb0 14917 const char *type_name;
7d27a96d
TT
14918 int len;
14919
14920 if (die->parent == NULL)
14921 return 0;
14922
14923 if (die->parent->tag != DW_TAG_structure_type
14924 && die->parent->tag != DW_TAG_union_type
14925 && die->parent->tag != DW_TAG_class_type)
14926 return 0;
14927
14928 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14929 type_name = dwarf2_name (die->parent, cu);
14930 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14931 return 0;
14932
14933 len = strlen (fieldname);
fe978cb0
PA
14934 return (strncmp (fieldname, type_name, len) == 0
14935 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14936}
14937
c906108c
SS
14938/* Add a member function to the proper fieldlist. */
14939
14940static void
107d2387 14941dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14942 struct type *type, struct dwarf2_cu *cu)
c906108c 14943{
5e22e966 14944 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 14945 struct attribute *attr;
c906108c 14946 int i;
be2daae6 14947 struct fnfieldlist *flp = nullptr;
c906108c 14948 struct fn_field *fnp;
15d034d0 14949 const char *fieldname;
f792889a 14950 struct type *this_type;
c906108c 14951
b4ba55a1 14952 if (cu->language == language_ada)
a73c6dcd 14953 error (_("unexpected member function in Ada type"));
b4ba55a1 14954
2df3850c 14955 /* Get name of member function. */
39cbfefa
DJ
14956 fieldname = dwarf2_name (die, cu);
14957 if (fieldname == NULL)
2df3850c 14958 return;
c906108c 14959
c906108c 14960 /* Look up member function name in fieldlist. */
be2daae6 14961 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14962 {
27bfe10e 14963 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14964 {
14965 flp = &fip->fnfieldlists[i];
14966 break;
14967 }
c906108c
SS
14968 }
14969
be2daae6
TT
14970 /* Create a new fnfieldlist if necessary. */
14971 if (flp == nullptr)
c906108c 14972 {
be2daae6
TT
14973 fip->fnfieldlists.emplace_back ();
14974 flp = &fip->fnfieldlists.back ();
c906108c 14975 flp->name = fieldname;
be2daae6 14976 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14977 }
14978
be2daae6
TT
14979 /* Create a new member function field and add it to the vector of
14980 fnfieldlists. */
14981 flp->fnfields.emplace_back ();
14982 fnp = &flp->fnfields.back ();
3da10d80
KS
14983
14984 /* Delay processing of the physname until later. */
9c37b5ae 14985 if (cu->language == language_cplus)
be2daae6
TT
14986 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14987 die, cu);
3da10d80
KS
14988 else
14989 {
1d06ead6 14990 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14991 fnp->physname = physname ? physname : "";
14992 }
14993
c906108c 14994 fnp->type = alloc_type (objfile);
f792889a 14995 this_type = read_type_die (die, cu);
78134374 14996 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 14997 {
1f704f76 14998 int nparams = this_type->num_fields ();
c906108c 14999
f792889a 15000 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15001 of the method itself (TYPE_CODE_METHOD). */
15002 smash_to_method_type (fnp->type, type,
f792889a 15003 TYPE_TARGET_TYPE (this_type),
80fc5e77 15004 this_type->fields (),
1f704f76 15005 this_type->num_fields (),
a409645d 15006 this_type->has_varargs ());
c906108c
SS
15007
15008 /* Handle static member functions.
dda83cd7
SM
15009 Dwarf2 has no clean way to discern C++ static and non-static
15010 member functions. G++ helps GDB by marking the first
15011 parameter for non-static member functions (which is the this
15012 pointer) as artificial. We obtain this information from
15013 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15014 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15015 fnp->voffset = VOFFSET_STATIC;
15016 }
15017 else
b98664d3 15018 complaint (_("member function type missing for '%s'"),
3da10d80 15019 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15020
15021 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15022 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15023 fnp->fcontext = die_containing_type (die, cu);
c906108c 15024
3e43a32a
MS
15025 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15026 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15027
15028 /* Get accessibility. */
bf23a268 15029 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15030 switch (accessibility)
c906108c 15031 {
60d5a603
JK
15032 case DW_ACCESS_private:
15033 fnp->is_private = 1;
15034 break;
15035 case DW_ACCESS_protected:
15036 fnp->is_protected = 1;
15037 break;
c906108c
SS
15038 }
15039
b02dede2 15040 /* Check for artificial methods. */
e142c38c 15041 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15042 if (attr && attr->as_boolean ())
b02dede2
DJ
15043 fnp->is_artificial = 1;
15044
e35000a7
TBA
15045 /* Check for defaulted methods. */
15046 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15047 if (attr != nullptr)
15048 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15049
15050 /* Check for deleted methods. */
15051 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15052 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15053 fnp->is_deleted = 1;
15054
7d27a96d
TT
15055 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15056
0d564a31 15057 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15058 function. For older versions of GCC, this is an offset in the
15059 appropriate virtual table, as specified by DW_AT_containing_type.
15060 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15061 to the object address. */
15062
e142c38c 15063 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15064 if (attr != nullptr)
8e19ed76 15065 {
9d2246fc 15066 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15067 {
9d2246fc
TT
15068 struct dwarf_block *block = attr->as_block ();
15069
15070 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15071 {
15072 /* Old-style GCC. */
9d2246fc 15073 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15074 }
9d2246fc
TT
15075 else if (block->data[0] == DW_OP_deref
15076 || (block->size > 1
15077 && block->data[0] == DW_OP_deref_size
15078 && block->data[1] == cu->header.addr_size))
aec5aa8b 15079 {
9d2246fc 15080 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15081 if ((fnp->voffset % cu->header.addr_size) != 0)
15082 dwarf2_complex_location_expr_complaint ();
15083 else
15084 fnp->voffset /= cu->header.addr_size;
15085 fnp->voffset += 2;
15086 }
15087 else
15088 dwarf2_complex_location_expr_complaint ();
15089
15090 if (!fnp->fcontext)
7e993ebf
KS
15091 {
15092 /* If there is no `this' field and no DW_AT_containing_type,
15093 we cannot actually find a base class context for the
15094 vtable! */
1f704f76 15095 if (this_type->num_fields () == 0
7e993ebf
KS
15096 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15097 {
b98664d3 15098 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15099 "function \"%s\" (offset %s)"),
15100 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15101 }
15102 else
15103 {
15104 fnp->fcontext
940da03e 15105 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15106 }
15107 }
aec5aa8b 15108 }
cd6c91b4 15109 else if (attr->form_is_section_offset ())
dda83cd7 15110 {
4d3c2250 15111 dwarf2_complex_location_expr_complaint ();
dda83cd7 15112 }
8e19ed76 15113 else
dda83cd7 15114 {
4d3c2250
KB
15115 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15116 fieldname);
dda83cd7 15117 }
0d564a31 15118 }
d48cc9dd
DJ
15119 else
15120 {
15121 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15122 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15123 {
15124 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15125 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15126 "but the vtable offset is not specified"),
9d8780f0 15127 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15128 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15129 TYPE_CPLUS_DYNAMIC (type) = 1;
15130 }
15131 }
c906108c
SS
15132}
15133
15134/* Create the vector of member function fields, and attach it to the type. */
15135
15136static void
fba45db2 15137dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15138 struct dwarf2_cu *cu)
c906108c 15139{
b4ba55a1 15140 if (cu->language == language_ada)
a73c6dcd 15141 error (_("unexpected member functions in Ada type"));
b4ba55a1 15142
c906108c
SS
15143 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15144 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15145 TYPE_ALLOC (type,
15146 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15147
be2daae6 15148 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15149 {
be2daae6 15150 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15151 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15152
be2daae6
TT
15153 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15154 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15155 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15156 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15157
15158 for (int k = 0; k < nf.fnfields.size (); ++k)
15159 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15160 }
15161
be2daae6 15162 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15163}
15164
1168df01
JB
15165/* Returns non-zero if NAME is the name of a vtable member in CU's
15166 language, zero otherwise. */
15167static int
15168is_vtable_name (const char *name, struct dwarf2_cu *cu)
15169{
15170 static const char vptr[] = "_vptr";
15171
9c37b5ae
TT
15172 /* Look for the C++ form of the vtable. */
15173 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15174 return 1;
15175
15176 return 0;
15177}
15178
c0dd20ea 15179/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15180 functions, with the ABI-specified layout. If TYPE describes
15181 such a structure, smash it into a member function type.
61049d3b
DJ
15182
15183 GCC shouldn't do this; it should just output pointer to member DIEs.
15184 This is GCC PR debug/28767. */
c0dd20ea 15185
0b92b5bb
TT
15186static void
15187quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15188{
09e2d7c7 15189 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15190
15191 /* Check for a structure with no name and two children. */
1f704f76 15192 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15193 return;
c0dd20ea
DJ
15194
15195 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15196 if (TYPE_FIELD_NAME (type, 0) == NULL
15197 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15198 || TYPE_FIELD_NAME (type, 1) == NULL
15199 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15200 return;
c0dd20ea
DJ
15201
15202 /* Find the type of the method. */
940da03e 15203 pfn_type = type->field (0).type ();
c0dd20ea 15204 if (pfn_type == NULL
78134374
SM
15205 || pfn_type->code () != TYPE_CODE_PTR
15206 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15207 return;
c0dd20ea
DJ
15208
15209 /* Look for the "this" argument. */
15210 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15211 if (pfn_type->num_fields () == 0
940da03e
SM
15212 /* || pfn_type->field (0).type () == NULL */
15213 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15214 return;
c0dd20ea 15215
940da03e 15216 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15217 new_type = alloc_type (objfile);
09e2d7c7 15218 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15219 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15220 pfn_type->has_varargs ());
0b92b5bb 15221 smash_to_methodptr_type (type, new_type);
c0dd20ea 15222}
1168df01 15223
e26624c6
TT
15224/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15225 requires rewriting, then copy it and return the updated copy.
15226 Otherwise return nullptr. */
15227
15228static struct type *
15229rewrite_array_type (struct type *type)
15230{
15231 if (type->code () != TYPE_CODE_ARRAY)
15232 return nullptr;
15233
15234 struct type *index_type = type->index_type ();
15235 range_bounds *current_bounds = index_type->bounds ();
15236
15237 /* Handle multi-dimensional arrays. */
15238 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15239 if (new_target == nullptr)
15240 {
15241 /* Maybe we don't need to rewrite this array. */
15242 if (current_bounds->low.kind () == PROP_CONST
15243 && current_bounds->high.kind () == PROP_CONST)
15244 return nullptr;
15245 }
15246
15247 /* Either the target type was rewritten, or the bounds have to be
15248 updated. Either way we want to copy the type and update
15249 everything. */
15250 struct type *copy = copy_type (type);
15251 int nfields = copy->num_fields ();
15252 field *new_fields
15253 = ((struct field *) TYPE_ZALLOC (copy,
15254 nfields * sizeof (struct field)));
15255 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15256 copy->set_fields (new_fields);
15257 if (new_target != nullptr)
15258 TYPE_TARGET_TYPE (copy) = new_target;
15259
15260 struct type *index_copy = copy_type (index_type);
15261 range_bounds *bounds
15262 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15263 sizeof (range_bounds));
15264 *bounds = *current_bounds;
15265 bounds->low.set_const_val (1);
15266 bounds->high.set_const_val (0);
15267 index_copy->set_bounds (bounds);
15268 copy->set_index_type (index_copy);
15269
15270 return copy;
15271}
15272
57567375
TT
15273/* While some versions of GCC will generate complicated DWARF for an
15274 array (see quirk_ada_thick_pointer), more recent versions were
15275 modified to emit an explicit thick pointer structure. However, in
15276 this case, the array still has DWARF expressions for its ranges,
15277 and these must be ignored. */
15278
15279static void
15280quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15281 struct type *type)
15282{
15283 gdb_assert (cu->language == language_ada);
15284
15285 /* Check for a structure with two children. */
15286 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15287 return;
15288
15289 /* Check for P_ARRAY and P_BOUNDS members. */
15290 if (TYPE_FIELD_NAME (type, 0) == NULL
15291 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15292 || TYPE_FIELD_NAME (type, 1) == NULL
15293 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15294 return;
15295
15296 /* Make sure we're looking at a pointer to an array. */
15297 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15298 return;
57567375 15299
e26624c6
TT
15300 /* The Ada code already knows how to handle these types, so all that
15301 we need to do is turn the bounds into static bounds. However, we
15302 don't want to rewrite existing array or index types in-place,
15303 because those may be referenced in other contexts where this
15304 rewriting is undesirable. */
15305 struct type *new_ary_type
15306 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15307 if (new_ary_type != nullptr)
15308 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15309}
15310
2b4424c3
TT
15311/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15312 appropriate error checking and issuing complaints if there is a
15313 problem. */
15314
15315static ULONGEST
15316get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15317{
15318 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15319
15320 if (attr == nullptr)
15321 return 0;
15322
cd6c91b4 15323 if (!attr->form_is_constant ())
2b4424c3 15324 {
b98664d3 15325 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15326 " - DIE at %s [in module %s]"),
15327 sect_offset_str (die->sect_off),
5e22e966 15328 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15329 return 0;
15330 }
15331
529908cb
TT
15332 LONGEST val = attr->constant_value (0);
15333 if (val < 0)
2b4424c3 15334 {
529908cb
TT
15335 complaint (_("DW_AT_alignment value must not be negative"
15336 " - DIE at %s [in module %s]"),
15337 sect_offset_str (die->sect_off),
15338 objfile_name (cu->per_objfile->objfile));
15339 return 0;
2b4424c3 15340 }
529908cb 15341 ULONGEST align = val;
2b4424c3
TT
15342
15343 if (align == 0)
15344 {
b98664d3 15345 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15346 " - DIE at %s [in module %s]"),
15347 sect_offset_str (die->sect_off),
5e22e966 15348 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15349 return 0;
15350 }
15351 if ((align & (align - 1)) != 0)
15352 {
b98664d3 15353 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15354 " - DIE at %s [in module %s]"),
15355 sect_offset_str (die->sect_off),
5e22e966 15356 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15357 return 0;
15358 }
15359
15360 return align;
15361}
15362
15363/* If the DIE has a DW_AT_alignment attribute, use its value to set
15364 the alignment for TYPE. */
15365
15366static void
15367maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15368 struct type *type)
15369{
15370 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15371 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15372 " - DIE at %s [in module %s]"),
15373 sect_offset_str (die->sect_off),
5e22e966 15374 objfile_name (cu->per_objfile->objfile));
2b4424c3 15375}
685b1105 15376
e35000a7
TBA
15377/* Check if the given VALUE is a valid enum dwarf_calling_convention
15378 constant for a type, according to DWARF5 spec, Table 5.5. */
15379
15380static bool
15381is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15382{
15383 switch (value)
15384 {
15385 case DW_CC_normal:
15386 case DW_CC_pass_by_reference:
15387 case DW_CC_pass_by_value:
15388 return true;
15389
15390 default:
15391 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15392 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15393 return false;
15394 }
15395}
15396
d0922fcf
TBA
15397/* Check if the given VALUE is a valid enum dwarf_calling_convention
15398 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15399 also according to GNU-specific values (see include/dwarf2.h). */
15400
15401static bool
15402is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15403{
15404 switch (value)
15405 {
15406 case DW_CC_normal:
15407 case DW_CC_program:
15408 case DW_CC_nocall:
15409 return true;
15410
15411 case DW_CC_GNU_renesas_sh:
15412 case DW_CC_GNU_borland_fastcall_i386:
15413 case DW_CC_GDB_IBM_OpenCL:
15414 return true;
15415
15416 default:
15417 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15418 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15419 return false;
15420 }
15421}
15422
c906108c 15423/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15424 (definition) to create a type for the structure or union. Fill in
15425 the type's name and general properties; the members will not be
83655187
DE
15426 processed until process_structure_scope. A symbol table entry for
15427 the type will also not be done until process_structure_scope (assuming
15428 the type has a name).
c906108c 15429
c767944b
DJ
15430 NOTE: we need to call these functions regardless of whether or not the
15431 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15432 structure or union. This gets the type entered into our set of
83655187 15433 user defined types. */
c906108c 15434
f792889a 15435static struct type *
134d01f1 15436read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15437{
5e22e966 15438 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15439 struct type *type;
15440 struct attribute *attr;
15d034d0 15441 const char *name;
c906108c 15442
348e048f
DE
15443 /* If the definition of this type lives in .debug_types, read that type.
15444 Don't follow DW_AT_specification though, that will take us back up
15445 the chain and we want to go down. */
052c8bb8 15446 attr = die->attr (DW_AT_signature);
435d3d88 15447 if (attr != nullptr)
348e048f 15448 {
ac9ec31b 15449 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15450
ac9ec31b 15451 /* The type's CU may not be the same as CU.
02142a6c 15452 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15453 return set_die_type (die, type, cu);
15454 }
15455
c0dd20ea 15456 type = alloc_type (objfile);
c906108c 15457 INIT_CPLUS_SPECIFIC (type);
93311388 15458
39cbfefa
DJ
15459 name = dwarf2_name (die, cu);
15460 if (name != NULL)
c906108c 15461 {
987504bb 15462 if (cu->language == language_cplus
c44af4eb
TT
15463 || cu->language == language_d
15464 || cu->language == language_rust)
63d06c5c 15465 {
15d034d0 15466 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15467
15468 /* dwarf2_full_name might have already finished building the DIE's
15469 type. If so, there is no need to continue. */
15470 if (get_die_type (die, cu) != NULL)
15471 return get_die_type (die, cu);
15472
d0e39ea2 15473 type->set_name (full_name);
63d06c5c
DC
15474 }
15475 else
15476 {
d8151005
DJ
15477 /* The name is already allocated along with this objfile, so
15478 we don't need to duplicate it for the type. */
d0e39ea2 15479 type->set_name (name);
63d06c5c 15480 }
c906108c
SS
15481 }
15482
15483 if (die->tag == DW_TAG_structure_type)
15484 {
67607e24 15485 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15486 }
15487 else if (die->tag == DW_TAG_union_type)
15488 {
67607e24 15489 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15490 }
15491 else
15492 {
67607e24 15493 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15494 }
15495
0cc2414c 15496 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
aa70e35c 15497 type->set_is_declared_class (true);
0cc2414c 15498
e35000a7
TBA
15499 /* Store the calling convention in the type if it's available in
15500 the die. Otherwise the calling convention remains set to
15501 the default value DW_CC_normal. */
15502 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15503 if (attr != nullptr
529908cb 15504 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
15505 {
15506 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15507 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 15508 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
15509 }
15510
e142c38c 15511 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15512 if (attr != nullptr)
c906108c 15513 {
cd6c91b4 15514 if (attr->form_is_constant ())
dda83cd7 15515 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
15516 else
15517 {
f8e89861 15518 struct dynamic_prop prop;
293e7e51 15519 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15520 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 15521 TYPE_LENGTH (type) = 0;
155bfbd3 15522 }
c906108c
SS
15523 }
15524 else
15525 {
15526 TYPE_LENGTH (type) = 0;
15527 }
15528
2b4424c3
TT
15529 maybe_set_alignment (cu, die, type);
15530
5230b05a 15531 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15532 {
5230b05a
WT
15533 /* ICC<14 does not output the required DW_AT_declaration on
15534 incomplete types, but gives them a size of zero. */
b4b73759 15535 type->set_is_stub (true);
685b1105
JK
15536 }
15537 else
9baccff6 15538 type->set_stub_is_supported (true);
685b1105 15539
dc718098 15540 if (die_is_declaration (die, cu))
b4b73759 15541 type->set_is_stub (true);
a6c727b2
DJ
15542 else if (attr == NULL && die->child == NULL
15543 && producer_is_realview (cu->producer))
15544 /* RealView does not output the required DW_AT_declaration
15545 on incomplete types. */
b4b73759 15546 type->set_is_stub (true);
dc718098 15547
c906108c
SS
15548 /* We need to add the type field to the die immediately so we don't
15549 infinitely recurse when dealing with pointers to the structure
0963b4bd 15550 type within the structure itself. */
1c379e20 15551 set_die_type (die, type, cu);
c906108c 15552
7e314c57
JK
15553 /* set_die_type should be already done. */
15554 set_descriptive_type (type, die, cu);
15555
c767944b
DJ
15556 return type;
15557}
15558
9c6a1327
TT
15559static void handle_struct_member_die
15560 (struct die_info *child_die,
15561 struct type *type,
15562 struct field_info *fi,
15563 std::vector<struct symbol *> *template_args,
15564 struct dwarf2_cu *cu);
15565
15566/* A helper for handle_struct_member_die that handles
15567 DW_TAG_variant_part. */
15568
15569static void
15570handle_variant_part (struct die_info *die, struct type *type,
15571 struct field_info *fi,
15572 std::vector<struct symbol *> *template_args,
15573 struct dwarf2_cu *cu)
15574{
15575 variant_part_builder *new_part;
15576 if (fi->current_variant_part == nullptr)
15577 {
15578 fi->variant_parts.emplace_back ();
15579 new_part = &fi->variant_parts.back ();
15580 }
15581 else if (!fi->current_variant_part->processing_variant)
15582 {
15583 complaint (_("nested DW_TAG_variant_part seen "
15584 "- DIE at %s [in module %s]"),
15585 sect_offset_str (die->sect_off),
5e22e966 15586 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15587 return;
15588 }
15589 else
15590 {
15591 variant_field &current = fi->current_variant_part->variants.back ();
15592 current.variant_parts.emplace_back ();
15593 new_part = &current.variant_parts.back ();
15594 }
15595
15596 /* When we recurse, we want callees to add to this new variant
15597 part. */
15598 scoped_restore save_current_variant_part
15599 = make_scoped_restore (&fi->current_variant_part, new_part);
15600
15601 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15602 if (discr == NULL)
15603 {
15604 /* It's a univariant form, an extension we support. */
15605 }
15606 else if (discr->form_is_ref ())
15607 {
15608 struct dwarf2_cu *target_cu = cu;
15609 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15610
15611 new_part->discriminant_offset = target_die->sect_off;
15612 }
15613 else
15614 {
15615 complaint (_("DW_AT_discr does not have DIE reference form"
15616 " - DIE at %s [in module %s]"),
15617 sect_offset_str (die->sect_off),
5e22e966 15618 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15619 }
15620
15621 for (die_info *child_die = die->child;
15622 child_die != NULL;
15623 child_die = child_die->sibling)
15624 handle_struct_member_die (child_die, type, fi, template_args, cu);
15625}
15626
15627/* A helper for handle_struct_member_die that handles
15628 DW_TAG_variant. */
15629
15630static void
15631handle_variant (struct die_info *die, struct type *type,
15632 struct field_info *fi,
15633 std::vector<struct symbol *> *template_args,
15634 struct dwarf2_cu *cu)
15635{
15636 if (fi->current_variant_part == nullptr)
15637 {
15638 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15639 "- DIE at %s [in module %s]"),
15640 sect_offset_str (die->sect_off),
5e22e966 15641 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15642 return;
15643 }
15644 if (fi->current_variant_part->processing_variant)
15645 {
15646 complaint (_("nested DW_TAG_variant seen "
15647 "- DIE at %s [in module %s]"),
15648 sect_offset_str (die->sect_off),
5e22e966 15649 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15650 return;
15651 }
15652
15653 scoped_restore save_processing_variant
15654 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15655 true);
15656
15657 fi->current_variant_part->variants.emplace_back ();
15658 variant_field &variant = fi->current_variant_part->variants.back ();
15659 variant.first_field = fi->fields.size ();
15660
15661 /* In a variant we want to get the discriminant and also add a
15662 field for our sole member child. */
15663 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 15664 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
15665 {
15666 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 15667 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
15668 variant.default_branch = true;
15669 else
9d2246fc 15670 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
15671 }
15672 else
cae21f8e 15673 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
15674
15675 for (die_info *variant_child = die->child;
15676 variant_child != NULL;
15677 variant_child = variant_child->sibling)
15678 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15679
15680 variant.last_field = fi->fields.size ();
15681}
15682
2ddeaf8a
TT
15683/* A helper for process_structure_scope that handles a single member
15684 DIE. */
15685
15686static void
15687handle_struct_member_die (struct die_info *child_die, struct type *type,
15688 struct field_info *fi,
15689 std::vector<struct symbol *> *template_args,
15690 struct dwarf2_cu *cu)
15691{
15692 if (child_die->tag == DW_TAG_member
9c6a1327 15693 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15694 {
15695 /* NOTE: carlton/2002-11-05: A C++ static data member
15696 should be a DW_TAG_member that is a declaration, but
15697 all versions of G++ as of this writing (so through at
15698 least 3.2.1) incorrectly generate DW_TAG_variable
15699 tags for them instead. */
15700 dwarf2_add_field (fi, child_die, cu);
15701 }
15702 else if (child_die->tag == DW_TAG_subprogram)
15703 {
15704 /* Rust doesn't have member functions in the C++ sense.
15705 However, it does emit ordinary functions as children
15706 of a struct DIE. */
15707 if (cu->language == language_rust)
15708 read_func_scope (child_die, cu);
15709 else
15710 {
15711 /* C++ member function. */
15712 dwarf2_add_member_fn (fi, child_die, type, cu);
15713 }
15714 }
15715 else if (child_die->tag == DW_TAG_inheritance)
15716 {
15717 /* C++ base class field. */
15718 dwarf2_add_field (fi, child_die, cu);
15719 }
15720 else if (type_can_define_types (child_die))
15721 dwarf2_add_type_defn (fi, child_die, cu);
15722 else if (child_die->tag == DW_TAG_template_type_param
15723 || child_die->tag == DW_TAG_template_value_param)
15724 {
15725 struct symbol *arg = new_symbol (child_die, NULL, cu);
15726
15727 if (arg != NULL)
15728 template_args->push_back (arg);
15729 }
9c6a1327
TT
15730 else if (child_die->tag == DW_TAG_variant_part)
15731 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15732 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15733 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15734}
15735
c767944b
DJ
15736/* Finish creating a structure or union type, including filling in
15737 its members and creating a symbol for it. */
15738
15739static void
15740process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15741{
5e22e966 15742 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15743 struct die_info *child_die;
c767944b
DJ
15744 struct type *type;
15745
15746 type = get_die_type (die, cu);
15747 if (type == NULL)
15748 type = read_structure_type (die, cu);
15749
3e1d3d8c 15750 bool has_template_parameters = false;
e142c38c 15751 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15752 {
15753 struct field_info fi;
2f4732b0 15754 std::vector<struct symbol *> template_args;
c906108c 15755
639d11d3 15756 child_die = die->child;
c906108c
SS
15757
15758 while (child_die && child_die->tag)
15759 {
2ddeaf8a 15760 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15761 child_die = child_die->sibling;
c906108c
SS
15762 }
15763
34eaf542 15764 /* Attach template arguments to type. */
2f4732b0 15765 if (!template_args.empty ())
34eaf542 15766 {
3e1d3d8c 15767 has_template_parameters = true;
34eaf542 15768 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15769 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15770 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15771 = XOBNEWVEC (&objfile->objfile_obstack,
15772 struct symbol *,
15773 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15774 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15775 template_args.data (),
34eaf542
TT
15776 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15777 * sizeof (struct symbol *)));
34eaf542
TT
15778 }
15779
c906108c 15780 /* Attach fields and member functions to the type. */
317f7127 15781 if (fi.nfields () > 0)
e7c27a73 15782 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15783 if (!fi.fnfieldlists.empty ())
c906108c 15784 {
e7c27a73 15785 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15786
c5aa993b 15787 /* Get the type which refers to the base class (possibly this
c906108c 15788 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15789 class from the DW_AT_containing_type attribute. This use of
15790 DW_AT_containing_type is a GNU extension. */
c906108c 15791
e142c38c 15792 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15793 {
e7c27a73 15794 struct type *t = die_containing_type (die, cu);
c906108c 15795
ae6ae975 15796 set_type_vptr_basetype (type, t);
c906108c
SS
15797 if (type == t)
15798 {
c906108c
SS
15799 int i;
15800
15801 /* Our own class provides vtbl ptr. */
1f704f76 15802 for (i = t->num_fields () - 1;
c906108c
SS
15803 i >= TYPE_N_BASECLASSES (t);
15804 --i)
15805 {
0d5cff50 15806 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15807
dda83cd7 15808 if (is_vtable_name (fieldname, cu))
c906108c 15809 {
ae6ae975 15810 set_type_vptr_fieldno (type, i);
c906108c
SS
15811 break;
15812 }
15813 }
15814
15815 /* Complain if virtual function table field not found. */
15816 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15817 complaint (_("virtual function table pointer "
3e43a32a 15818 "not found when defining class '%s'"),
7d93a1e0 15819 type->name () ? type->name () : "");
c906108c
SS
15820 }
15821 else
15822 {
ae6ae975 15823 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15824 }
15825 }
f6235d4c 15826 else if (cu->producer
61012eef 15827 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15828 {
15829 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
15830 of the containing type, but the vtable pointer is
15831 always named __vfp. */
f6235d4c
EZ
15832
15833 int i;
15834
1f704f76 15835 for (i = type->num_fields () - 1;
f6235d4c
EZ
15836 i >= TYPE_N_BASECLASSES (type);
15837 --i)
15838 {
15839 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15840 {
ae6ae975
DE
15841 set_type_vptr_fieldno (type, i);
15842 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15843 break;
15844 }
15845 }
15846 }
c906108c 15847 }
98751a41
JK
15848
15849 /* Copy fi.typedef_field_list linked list elements content into the
15850 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15851 if (!fi.typedef_field_list.empty ())
98751a41 15852 {
be2daae6 15853 int count = fi.typedef_field_list.size ();
98751a41 15854
a0d7a4ff 15855 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15856 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15857 = ((struct decl_field *)
be2daae6
TT
15858 TYPE_ALLOC (type,
15859 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15860 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15861
be2daae6
TT
15862 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15863 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15864 }
c767944b 15865
883fd55a
KS
15866 /* Copy fi.nested_types_list linked list elements content into the
15867 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15868 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15869 {
be2daae6 15870 int count = fi.nested_types_list.size ();
883fd55a
KS
15871
15872 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15873 TYPE_NESTED_TYPES_ARRAY (type)
15874 = ((struct decl_field *)
be2daae6
TT
15875 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15876 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15877
be2daae6
TT
15878 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15879 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15880 }
c906108c 15881 }
63d06c5c 15882
bb5ed363 15883 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15884 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15885 cu->rust_unions.push_back (type);
57567375
TT
15886 else if (cu->language == language_ada)
15887 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 15888
90aeadfc
DC
15889 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15890 snapshots) has been known to create a die giving a declaration
15891 for a class that has, as a child, a die giving a definition for a
15892 nested class. So we have to process our children even if the
15893 current die is a declaration. Normally, of course, a declaration
15894 won't have any children at all. */
134d01f1 15895
ca040673
DE
15896 child_die = die->child;
15897
90aeadfc
DC
15898 while (child_die != NULL && child_die->tag)
15899 {
15900 if (child_die->tag == DW_TAG_member
15901 || child_die->tag == DW_TAG_variable
34eaf542
TT
15902 || child_die->tag == DW_TAG_inheritance
15903 || child_die->tag == DW_TAG_template_value_param
15904 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15905 {
90aeadfc 15906 /* Do nothing. */
134d01f1 15907 }
90aeadfc
DC
15908 else
15909 process_die (child_die, cu);
134d01f1 15910
436c571c 15911 child_die = child_die->sibling;
134d01f1
DJ
15912 }
15913
fa4028e9
JB
15914 /* Do not consider external references. According to the DWARF standard,
15915 these DIEs are identified by the fact that they have no byte_size
15916 attribute, and a declaration attribute. */
15917 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15918 || !die_is_declaration (die, cu)
15919 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15920 {
15921 struct symbol *sym = new_symbol (die, type, cu);
15922
15923 if (has_template_parameters)
15924 {
a776957c
TT
15925 struct symtab *symtab;
15926 if (sym != nullptr)
15927 symtab = symbol_symtab (sym);
15928 else if (cu->line_header != nullptr)
15929 {
15930 /* Any related symtab will do. */
15931 symtab
7ba99d21 15932 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15933 }
15934 else
15935 {
15936 symtab = nullptr;
15937 complaint (_("could not find suitable "
15938 "symtab for template parameter"
15939 " - DIE at %s [in module %s]"),
15940 sect_offset_str (die->sect_off),
15941 objfile_name (objfile));
15942 }
15943
15944 if (symtab != nullptr)
15945 {
15946 /* Make sure that the symtab is set on the new symbols.
15947 Even though they don't appear in this symtab directly,
15948 other parts of gdb assume that symbols do, and this is
15949 reasonably true. */
15950 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15951 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15952 }
3e1d3d8c
TT
15953 }
15954 }
134d01f1
DJ
15955}
15956
ed6acedd
TT
15957/* Assuming DIE is an enumeration type, and TYPE is its associated
15958 type, update TYPE using some information only available in DIE's
15959 children. In particular, the fields are computed. */
55426c9d
JB
15960
15961static void
15962update_enumeration_type_from_children (struct die_info *die,
15963 struct type *type,
15964 struct dwarf2_cu *cu)
15965{
60f7655a 15966 struct die_info *child_die;
55426c9d
JB
15967 int unsigned_enum = 1;
15968 int flag_enum = 1;
55426c9d 15969
8268c778 15970 auto_obstack obstack;
ed6acedd 15971 std::vector<struct field> fields;
55426c9d 15972
60f7655a
DE
15973 for (child_die = die->child;
15974 child_die != NULL && child_die->tag;
436c571c 15975 child_die = child_die->sibling)
55426c9d
JB
15976 {
15977 struct attribute *attr;
15978 LONGEST value;
15979 const gdb_byte *bytes;
15980 struct dwarf2_locexpr_baton *baton;
15981 const char *name;
60f7655a 15982
55426c9d
JB
15983 if (child_die->tag != DW_TAG_enumerator)
15984 continue;
15985
15986 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15987 if (attr == NULL)
15988 continue;
15989
15990 name = dwarf2_name (child_die, cu);
15991 if (name == NULL)
15992 name = "<anonymous enumerator>";
15993
15994 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15995 &value, &bytes, &baton);
15996 if (value < 0)
15997 {
15998 unsigned_enum = 0;
15999 flag_enum = 0;
16000 }
55426c9d 16001 else
edd45eb0
SM
16002 {
16003 if (count_one_bits_ll (value) >= 2)
16004 flag_enum = 0;
edd45eb0 16005 }
55426c9d 16006
ed6acedd
TT
16007 fields.emplace_back ();
16008 struct field &field = fields.back ();
16009 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16010 SET_FIELD_ENUMVAL (field, value);
16011 }
16012
16013 if (!fields.empty ())
16014 {
5e33d5f4 16015 type->set_num_fields (fields.size ());
3cabb6b0
SM
16016 type->set_fields
16017 ((struct field *)
16018 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16019 memcpy (type->fields (), fields.data (),
ed6acedd 16020 sizeof (struct field) * fields.size ());
55426c9d
JB
16021 }
16022
16023 if (unsigned_enum)
653223d3
SM
16024 type->set_is_unsigned (true);
16025
55426c9d 16026 if (flag_enum)
9902b327 16027 type->set_is_flag_enum (true);
55426c9d
JB
16028}
16029
134d01f1
DJ
16030/* Given a DW_AT_enumeration_type die, set its type. We do not
16031 complete the type's fields yet, or create any symbols. */
c906108c 16032
f792889a 16033static struct type *
134d01f1 16034read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16035{
5e22e966 16036 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16037 struct type *type;
c906108c 16038 struct attribute *attr;
0114d602 16039 const char *name;
134d01f1 16040
348e048f
DE
16041 /* If the definition of this type lives in .debug_types, read that type.
16042 Don't follow DW_AT_specification though, that will take us back up
16043 the chain and we want to go down. */
052c8bb8 16044 attr = die->attr (DW_AT_signature);
435d3d88 16045 if (attr != nullptr)
348e048f 16046 {
ac9ec31b 16047 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16048
ac9ec31b 16049 /* The type's CU may not be the same as CU.
02142a6c 16050 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16051 return set_die_type (die, type, cu);
16052 }
16053
c906108c
SS
16054 type = alloc_type (objfile);
16055
67607e24 16056 type->set_code (TYPE_CODE_ENUM);
94af9270 16057 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16058 if (name != NULL)
d0e39ea2 16059 type->set_name (name);
c906108c 16060
0626fc76
TT
16061 attr = dwarf2_attr (die, DW_AT_type, cu);
16062 if (attr != NULL)
16063 {
16064 struct type *underlying_type = die_type (die, cu);
16065
16066 TYPE_TARGET_TYPE (type) = underlying_type;
16067 }
16068
e142c38c 16069 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16070 if (attr != nullptr)
c906108c 16071 {
529908cb 16072 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16073 }
16074 else
16075 {
16076 TYPE_LENGTH (type) = 0;
16077 }
16078
2b4424c3
TT
16079 maybe_set_alignment (cu, die, type);
16080
137033e9
JB
16081 /* The enumeration DIE can be incomplete. In Ada, any type can be
16082 declared as private in the package spec, and then defined only
16083 inside the package body. Such types are known as Taft Amendment
16084 Types. When another package uses such a type, an incomplete DIE
16085 may be generated by the compiler. */
02eb380e 16086 if (die_is_declaration (die, cu))
b4b73759 16087 type->set_is_stub (true);
02eb380e 16088
0626fc76
TT
16089 /* If this type has an underlying type that is not a stub, then we
16090 may use its attributes. We always use the "unsigned" attribute
16091 in this situation, because ordinarily we guess whether the type
16092 is unsigned -- but the guess can be wrong and the underlying type
16093 can tell us the reality. However, we defer to a local size
16094 attribute if one exists, because this lets the compiler override
16095 the underlying type if needed. */
e46d3488 16096 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16097 {
9e7c9a03
HD
16098 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16099 underlying_type = check_typedef (underlying_type);
653223d3
SM
16100
16101 type->set_is_unsigned (underlying_type->is_unsigned ());
16102
0626fc76 16103 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16104 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16105
2b4424c3 16106 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16107 && TYPE_RAW_ALIGN (underlying_type) != 0)
16108 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16109 }
16110
aa70e35c 16111 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
3d567982 16112
ed6acedd
TT
16113 set_die_type (die, type, cu);
16114
16115 /* Finish the creation of this type by using the enum's children.
16116 Note that, as usual, this must come after set_die_type to avoid
16117 infinite recursion when trying to compute the names of the
16118 enumerators. */
16119 update_enumeration_type_from_children (die, type, cu);
16120
16121 return type;
134d01f1
DJ
16122}
16123
16124/* Given a pointer to a die which begins an enumeration, process all
16125 the dies that define the members of the enumeration, and create the
16126 symbol for the enumeration type.
16127
16128 NOTE: We reverse the order of the element list. */
16129
16130static void
16131process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16132{
f792889a 16133 struct type *this_type;
134d01f1 16134
f792889a
DJ
16135 this_type = get_die_type (die, cu);
16136 if (this_type == NULL)
16137 this_type = read_enumeration_type (die, cu);
9dc481d3 16138
639d11d3 16139 if (die->child != NULL)
c906108c 16140 {
9dc481d3 16141 struct die_info *child_die;
15d034d0 16142 const char *name;
9dc481d3 16143
639d11d3 16144 child_die = die->child;
c906108c
SS
16145 while (child_die && child_die->tag)
16146 {
16147 if (child_die->tag != DW_TAG_enumerator)
16148 {
e7c27a73 16149 process_die (child_die, cu);
c906108c
SS
16150 }
16151 else
16152 {
39cbfefa
DJ
16153 name = dwarf2_name (child_die, cu);
16154 if (name)
ed6acedd 16155 new_symbol (child_die, this_type, cu);
c906108c
SS
16156 }
16157
436c571c 16158 child_die = child_die->sibling;
c906108c 16159 }
c906108c 16160 }
134d01f1 16161
6c83ed52
TT
16162 /* If we are reading an enum from a .debug_types unit, and the enum
16163 is a declaration, and the enum is not the signatured type in the
16164 unit, then we do not want to add a symbol for it. Adding a
16165 symbol would in some cases obscure the true definition of the
16166 enum, giving users an incomplete type when the definition is
16167 actually available. Note that we do not want to do this for all
16168 enums which are just declarations, because C++0x allows forward
16169 enum declarations. */
3019eac3 16170 if (cu->per_cu->is_debug_types
6c83ed52
TT
16171 && die_is_declaration (die, cu))
16172 {
52dc124a 16173 struct signatured_type *sig_type;
6c83ed52 16174
c0f78cd4 16175 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16176 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16177 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16178 return;
16179 }
16180
f792889a 16181 new_symbol (die, this_type, cu);
c906108c
SS
16182}
16183
57567375
TT
16184/* Helper function for quirk_ada_thick_pointer that examines a bounds
16185 expression for an index type and finds the corresponding field
16186 offset in the hidden "P_BOUNDS" structure. Returns true on success
16187 and updates *FIELD, false if it fails to recognize an
16188 expression. */
16189
16190static bool
16191recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16192 int *bounds_offset, struct field *field,
16193 struct dwarf2_cu *cu)
16194{
16195 struct attribute *attr = dwarf2_attr (die, name, cu);
16196 if (attr == nullptr || !attr->form_is_block ())
16197 return false;
16198
16199 const struct dwarf_block *block = attr->as_block ();
16200 const gdb_byte *start = block->data;
16201 const gdb_byte *end = block->data + block->size;
16202
16203 /* The expression to recognize generally looks like:
16204
16205 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16206 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16207
16208 However, the second "plus_uconst" may be missing:
16209
16210 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16211 DW_OP_deref_size: 4)
16212
16213 This happens when the field is at the start of the structure.
16214
16215 Also, the final deref may not be sized:
16216
16217 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16218 DW_OP_deref)
16219
16220 This happens when the size of the index type happens to be the
16221 same as the architecture's word size. This can occur with or
16222 without the second plus_uconst. */
16223
16224 if (end - start < 2)
16225 return false;
16226 if (*start++ != DW_OP_push_object_address)
16227 return false;
16228 if (*start++ != DW_OP_plus_uconst)
16229 return false;
16230
16231 uint64_t this_bound_off;
16232 start = gdb_read_uleb128 (start, end, &this_bound_off);
16233 if (start == nullptr || (int) this_bound_off != this_bound_off)
16234 return false;
16235 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16236 is consistent among all bounds. */
16237 if (*bounds_offset == -1)
16238 *bounds_offset = this_bound_off;
16239 else if (*bounds_offset != this_bound_off)
16240 return false;
16241
16242 if (start == end || *start++ != DW_OP_deref)
16243 return false;
16244
16245 int offset = 0;
16246 if (start ==end)
16247 return false;
16248 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16249 {
16250 /* This means an offset of 0. */
16251 }
16252 else if (*start++ != DW_OP_plus_uconst)
16253 return false;
16254 else
16255 {
16256 /* The size is the parameter to DW_OP_plus_uconst. */
16257 uint64_t val;
16258 start = gdb_read_uleb128 (start, end, &val);
16259 if (start == nullptr)
16260 return false;
16261 if ((int) val != val)
16262 return false;
16263 offset = val;
16264 }
16265
16266 if (start == end)
16267 return false;
16268
16269 uint64_t size;
16270 if (*start == DW_OP_deref_size)
16271 {
16272 start = gdb_read_uleb128 (start + 1, end, &size);
16273 if (start == nullptr)
16274 return false;
16275 }
16276 else if (*start == DW_OP_deref)
16277 {
16278 size = cu->header.addr_size;
16279 ++start;
16280 }
16281 else
16282 return false;
16283
16284 SET_FIELD_BITPOS (*field, 8 * offset);
16285 if (size != TYPE_LENGTH (field->type ()))
16286 FIELD_BITSIZE (*field) = 8 * size;
16287
16288 return true;
16289}
16290
16291/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16292 some kinds of Ada arrays:
16293
16294 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16295 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16296 <11e0> DW_AT_data_location: 2 byte block: 97 6
16297 (DW_OP_push_object_address; DW_OP_deref)
16298 <11e3> DW_AT_type : <0x1173>
16299 <11e7> DW_AT_sibling : <0x1201>
16300 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16301 <11ec> DW_AT_type : <0x1206>
16302 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16303 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16304 DW_OP_deref_size: 4)
16305 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16306 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16307 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16308
16309 This actually represents a "thick pointer", which is a structure
16310 with two elements: one that is a pointer to the array data, and one
16311 that is a pointer to another structure; this second structure holds
16312 the array bounds.
16313
16314 This returns a new type on success, or nullptr if this didn't
16315 recognize the type. */
16316
16317static struct type *
16318quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16319 struct type *type)
16320{
16321 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16322 /* So far we've only seen this with block form. */
16323 if (attr == nullptr || !attr->form_is_block ())
16324 return nullptr;
16325
16326 /* Note that this will fail if the structure layout is changed by
16327 the compiler. However, we have no good way to recognize some
16328 other layout, because we don't know what expression the compiler
16329 might choose to emit should this happen. */
16330 struct dwarf_block *blk = attr->as_block ();
16331 if (blk->size != 2
16332 || blk->data[0] != DW_OP_push_object_address
16333 || blk->data[1] != DW_OP_deref)
16334 return nullptr;
16335
16336 int bounds_offset = -1;
16337 int max_align = -1;
16338 std::vector<struct field> range_fields;
16339 for (struct die_info *child_die = die->child;
16340 child_die;
16341 child_die = child_die->sibling)
16342 {
16343 if (child_die->tag == DW_TAG_subrange_type)
16344 {
16345 struct type *underlying = read_subrange_index_type (child_die, cu);
16346
16347 int this_align = type_align (underlying);
16348 if (this_align > max_align)
16349 max_align = this_align;
16350
16351 range_fields.emplace_back ();
16352 range_fields.emplace_back ();
16353
16354 struct field &lower = range_fields[range_fields.size () - 2];
16355 struct field &upper = range_fields[range_fields.size () - 1];
16356
16357 lower.set_type (underlying);
16358 FIELD_ARTIFICIAL (lower) = 1;
16359
16360 upper.set_type (underlying);
16361 FIELD_ARTIFICIAL (upper) = 1;
16362
16363 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16364 &bounds_offset, &lower, cu)
16365 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16366 &bounds_offset, &upper, cu))
16367 return nullptr;
16368 }
16369 }
16370
16371 /* This shouldn't really happen, but double-check that we found
16372 where the bounds are stored. */
16373 if (bounds_offset == -1)
16374 return nullptr;
16375
16376 struct objfile *objfile = cu->per_objfile->objfile;
16377 for (int i = 0; i < range_fields.size (); i += 2)
16378 {
16379 char name[20];
16380
16381 /* Set the name of each field in the bounds. */
16382 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16383 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16384 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16385 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16386 }
16387
16388 struct type *bounds = alloc_type (objfile);
16389 bounds->set_code (TYPE_CODE_STRUCT);
16390
16391 bounds->set_num_fields (range_fields.size ());
16392 bounds->set_fields
16393 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16394 * sizeof (struct field))));
16395 memcpy (bounds->fields (), range_fields.data (),
16396 bounds->num_fields () * sizeof (struct field));
16397
16398 int last_fieldno = range_fields.size () - 1;
16399 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16400 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16401 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16402
16403 /* Rewrite the existing array type in place. Specifically, we
16404 remove any dynamic properties we might have read, and we replace
16405 the index types. */
16406 struct type *iter = type;
16407 for (int i = 0; i < range_fields.size (); i += 2)
16408 {
16409 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16410 iter->main_type->dyn_prop_list = nullptr;
16411 iter->set_index_type
16412 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16413 iter = TYPE_TARGET_TYPE (iter);
16414 }
16415
16416 struct type *result = alloc_type (objfile);
16417 result->set_code (TYPE_CODE_STRUCT);
16418
16419 result->set_num_fields (2);
16420 result->set_fields
16421 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16422 * sizeof (struct field))));
16423
16424 /* The names are chosen to coincide with what the compiler does with
16425 -fgnat-encodings=all, which the Ada code in gdb already
16426 understands. */
16427 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16428 result->field (0).set_type (lookup_pointer_type (type));
16429
16430 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16431 result->field (1).set_type (lookup_pointer_type (bounds));
16432 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16433
16434 result->set_name (type->name ());
16435 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16436 + TYPE_LENGTH (result->field (1).type ()));
16437
16438 return result;
16439}
16440
c906108c
SS
16441/* Extract all information from a DW_TAG_array_type DIE and put it in
16442 the DIE's type field. For now, this only handles one dimensional
16443 arrays. */
16444
f792889a 16445static struct type *
e7c27a73 16446read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16447{
5e22e966 16448 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16449 struct die_info *child_die;
7e314c57 16450 struct type *type;
c906108c 16451 struct type *element_type, *range_type, *index_type;
c906108c 16452 struct attribute *attr;
15d034d0 16453 const char *name;
a405673c 16454 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16455 unsigned int bit_stride = 0;
c906108c 16456
e7c27a73 16457 element_type = die_type (die, cu);
c906108c 16458
7e314c57
JK
16459 /* The die_type call above may have already set the type for this DIE. */
16460 type = get_die_type (die, cu);
16461 if (type)
16462 return type;
16463
dc53a7ad
JB
16464 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16465 if (attr != NULL)
a405673c
JB
16466 {
16467 int stride_ok;
293e7e51 16468 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16469
16470 byte_stride_prop
16471 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16472 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16473 prop_type);
a405673c
JB
16474 if (!stride_ok)
16475 {
b98664d3 16476 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16477 " - DIE at %s [in module %s]"),
16478 sect_offset_str (die->sect_off),
5e22e966 16479 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16480 /* Ignore this attribute. We will likely not be able to print
16481 arrays of this type correctly, but there is little we can do
16482 to help if we cannot read the attribute's value. */
16483 byte_stride_prop = NULL;
16484 }
16485 }
dc53a7ad
JB
16486
16487 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16488 if (attr != NULL)
529908cb 16489 bit_stride = attr->constant_value (0);
dc53a7ad 16490
c906108c
SS
16491 /* Irix 6.2 native cc creates array types without children for
16492 arrays with unspecified length. */
639d11d3 16493 if (die->child == NULL)
c906108c 16494 {
46bf5051 16495 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16496 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16497 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16498 byte_stride_prop, bit_stride);
f792889a 16499 return set_die_type (die, type, cu);
c906108c
SS
16500 }
16501
791afaa2 16502 std::vector<struct type *> range_types;
639d11d3 16503 child_die = die->child;
c906108c
SS
16504 while (child_die && child_die->tag)
16505 {
16506 if (child_die->tag == DW_TAG_subrange_type)
16507 {
f792889a 16508 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16509
dda83cd7
SM
16510 if (child_type != NULL)
16511 {
0963b4bd 16512 /* The range type was succesfully read. Save it for the
dda83cd7 16513 array type creation. */
791afaa2 16514 range_types.push_back (child_type);
dda83cd7 16515 }
c906108c 16516 }
436c571c 16517 child_die = child_die->sibling;
c906108c
SS
16518 }
16519
cf2b2075
TV
16520 if (range_types.empty ())
16521 {
16522 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16523 sect_offset_str (die->sect_off),
16524 objfile_name (cu->per_objfile->objfile));
16525 return NULL;
16526 }
16527
c906108c
SS
16528 /* Dwarf2 dimensions are output from left to right, create the
16529 necessary array types in backwards order. */
7ca2d3a3 16530
c906108c 16531 type = element_type;
7ca2d3a3
DL
16532
16533 if (read_array_order (die, cu) == DW_ORD_col_major)
16534 {
16535 int i = 0;
9a619af0 16536
791afaa2 16537 while (i < range_types.size ())
10f6a3ad
TT
16538 {
16539 type = create_array_type_with_stride (NULL, type, range_types[i++],
16540 byte_stride_prop, bit_stride);
16541 bit_stride = 0;
16542 byte_stride_prop = nullptr;
16543 }
7ca2d3a3
DL
16544 }
16545 else
16546 {
791afaa2 16547 size_t ndim = range_types.size ();
7ca2d3a3 16548 while (ndim-- > 0)
10f6a3ad
TT
16549 {
16550 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16551 byte_stride_prop, bit_stride);
16552 bit_stride = 0;
16553 byte_stride_prop = nullptr;
16554 }
7ca2d3a3 16555 }
c906108c 16556
cf2b2075
TV
16557 gdb_assert (type != element_type);
16558
f5f8a009
EZ
16559 /* Understand Dwarf2 support for vector types (like they occur on
16560 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16561 array type. This is not part of the Dwarf2/3 standard yet, but a
16562 custom vendor extension. The main difference between a regular
16563 array and the vector variant is that vectors are passed by value
16564 to functions. */
e142c38c 16565 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16566 if (attr != nullptr)
ea37ba09 16567 make_vector_type (type);
f5f8a009 16568
dbc98a8b
KW
16569 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16570 implementation may choose to implement triple vectors using this
16571 attribute. */
16572 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 16573 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 16574 {
529908cb
TT
16575 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16576 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 16577 else
b98664d3 16578 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16579 "than the total size of elements"));
dbc98a8b
KW
16580 }
16581
39cbfefa
DJ
16582 name = dwarf2_name (die, cu);
16583 if (name)
d0e39ea2 16584 type->set_name (name);
6e70227d 16585
2b4424c3
TT
16586 maybe_set_alignment (cu, die, type);
16587
57567375
TT
16588 struct type *replacement_type = nullptr;
16589 if (cu->language == language_ada)
16590 {
16591 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16592 if (replacement_type != nullptr)
16593 type = replacement_type;
16594 }
16595
0963b4bd 16596 /* Install the type in the die. */
57567375 16597 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
16598
16599 /* set_die_type should be already done. */
b4ba55a1
JB
16600 set_descriptive_type (type, die, cu);
16601
7e314c57 16602 return type;
c906108c
SS
16603}
16604
7ca2d3a3 16605static enum dwarf_array_dim_ordering
6e70227d 16606read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16607{
16608 struct attribute *attr;
16609
16610 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16611
435d3d88 16612 if (attr != nullptr)
1bc397c5
TT
16613 {
16614 LONGEST val = attr->constant_value (-1);
16615 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16616 return (enum dwarf_array_dim_ordering) val;
16617 }
7ca2d3a3 16618
0963b4bd
MS
16619 /* GNU F77 is a special case, as at 08/2004 array type info is the
16620 opposite order to the dwarf2 specification, but data is still
16621 laid out as per normal fortran.
7ca2d3a3 16622
0963b4bd
MS
16623 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16624 version checking. */
7ca2d3a3 16625
905e0470
PM
16626 if (cu->language == language_fortran
16627 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16628 {
16629 return DW_ORD_row_major;
16630 }
16631
3a3440fb 16632 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
16633 {
16634 case array_column_major:
16635 return DW_ORD_col_major;
16636 case array_row_major:
16637 default:
16638 return DW_ORD_row_major;
16639 };
16640}
16641
72019c9c 16642/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16643 the DIE's type field. */
72019c9c 16644
f792889a 16645static struct type *
72019c9c
GM
16646read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16647{
7e314c57
JK
16648 struct type *domain_type, *set_type;
16649 struct attribute *attr;
f792889a 16650
7e314c57
JK
16651 domain_type = die_type (die, cu);
16652
16653 /* The die_type call above may have already set the type for this DIE. */
16654 set_type = get_die_type (die, cu);
16655 if (set_type)
16656 return set_type;
16657
16658 set_type = create_set_type (NULL, domain_type);
16659
16660 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
16661 if (attr != nullptr && attr->form_is_unsigned ())
16662 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 16663
2b4424c3
TT
16664 maybe_set_alignment (cu, die, set_type);
16665
f792889a 16666 return set_die_type (die, set_type, cu);
72019c9c 16667}
7ca2d3a3 16668
0971de02
TT
16669/* A helper for read_common_block that creates a locexpr baton.
16670 SYM is the symbol which we are marking as computed.
16671 COMMON_DIE is the DIE for the common block.
16672 COMMON_LOC is the location expression attribute for the common
16673 block itself.
16674 MEMBER_LOC is the location expression attribute for the particular
16675 member of the common block that we are processing.
16676 CU is the CU from which the above come. */
16677
16678static void
16679mark_common_block_symbol_computed (struct symbol *sym,
16680 struct die_info *common_die,
16681 struct attribute *common_loc,
16682 struct attribute *member_loc,
16683 struct dwarf2_cu *cu)
16684{
5e22e966 16685 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16686 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16687 struct dwarf2_locexpr_baton *baton;
16688 gdb_byte *ptr;
16689 unsigned int cu_off;
08feed99 16690 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16691 LONGEST offset = 0;
16692
16693 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16694 gdb_assert (common_loc->form_is_block ());
16695 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16696 || member_loc->form_is_constant ());
0971de02 16697
8d749320 16698 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16699 baton->per_objfile = per_objfile;
0971de02
TT
16700 baton->per_cu = cu->per_cu;
16701 gdb_assert (baton->per_cu);
16702
16703 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16704
cd6c91b4 16705 if (member_loc->form_is_constant ())
0971de02 16706 {
0826b30a 16707 offset = member_loc->constant_value (0);
0971de02
TT
16708 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16709 }
16710 else
9d2246fc 16711 baton->size += member_loc->as_block ()->size;
0971de02 16712
224c3ddb 16713 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16714 baton->data = ptr;
16715
16716 *ptr++ = DW_OP_call4;
9c541725 16717 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16718 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16719 ptr += 4;
16720
cd6c91b4 16721 if (member_loc->form_is_constant ())
0971de02
TT
16722 {
16723 *ptr++ = DW_OP_addr;
16724 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16725 ptr += cu->header.addr_size;
16726 }
16727 else
16728 {
16729 /* We have to copy the data here, because DW_OP_call4 will only
16730 use a DW_AT_location attribute. */
9d2246fc
TT
16731 struct dwarf_block *block = member_loc->as_block ();
16732 memcpy (ptr, block->data, block->size);
16733 ptr += block->size;
0971de02
TT
16734 }
16735
16736 *ptr++ = DW_OP_plus;
16737 gdb_assert (ptr - baton->data == baton->size);
16738
0971de02 16739 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16740 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16741}
16742
4357ac6c
TT
16743/* Create appropriate locally-scoped variables for all the
16744 DW_TAG_common_block entries. Also create a struct common_block
16745 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16746 is used to separate the common blocks name namespace from regular
4357ac6c 16747 variable names. */
c906108c
SS
16748
16749static void
e7c27a73 16750read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16751{
0971de02
TT
16752 struct attribute *attr;
16753
16754 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16755 if (attr != nullptr)
0971de02
TT
16756 {
16757 /* Support the .debug_loc offsets. */
4fc6c0d5 16758 if (attr->form_is_block ())
dda83cd7 16759 {
0971de02 16760 /* Ok. */
dda83cd7 16761 }
cd6c91b4 16762 else if (attr->form_is_section_offset ())
dda83cd7 16763 {
0971de02
TT
16764 dwarf2_complex_location_expr_complaint ();
16765 attr = NULL;
dda83cd7 16766 }
0971de02 16767 else
dda83cd7 16768 {
0971de02
TT
16769 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16770 "common block member");
16771 attr = NULL;
dda83cd7 16772 }
0971de02
TT
16773 }
16774
639d11d3 16775 if (die->child != NULL)
c906108c 16776 {
5e22e966 16777 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16778 struct die_info *child_die;
16779 size_t n_entries = 0, size;
16780 struct common_block *common_block;
16781 struct symbol *sym;
74ac6d43 16782
4357ac6c
TT
16783 for (child_die = die->child;
16784 child_die && child_die->tag;
436c571c 16785 child_die = child_die->sibling)
4357ac6c
TT
16786 ++n_entries;
16787
16788 size = (sizeof (struct common_block)
16789 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16790 common_block
16791 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16792 size);
4357ac6c
TT
16793 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16794 common_block->n_entries = 0;
16795
16796 for (child_die = die->child;
16797 child_die && child_die->tag;
436c571c 16798 child_die = child_die->sibling)
4357ac6c
TT
16799 {
16800 /* Create the symbol in the DW_TAG_common_block block in the current
16801 symbol scope. */
e7c27a73 16802 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16803 if (sym != NULL)
16804 {
16805 struct attribute *member_loc;
16806
16807 common_block->contents[common_block->n_entries++] = sym;
16808
16809 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16810 cu);
16811 if (member_loc)
16812 {
16813 /* GDB has handled this for a long time, but it is
16814 not specified by DWARF. It seems to have been
16815 emitted by gfortran at least as recently as:
16816 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16817 complaint (_("Variable in common block has "
0971de02 16818 "DW_AT_data_member_location "
9d8780f0
SM
16819 "- DIE at %s [in module %s]"),
16820 sect_offset_str (child_die->sect_off),
518817b3 16821 objfile_name (objfile));
0971de02 16822
cd6c91b4 16823 if (member_loc->form_is_section_offset ())
0971de02 16824 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16825 else if (member_loc->form_is_constant ()
4fc6c0d5 16826 || member_loc->form_is_block ())
0971de02 16827 {
435d3d88 16828 if (attr != nullptr)
0971de02
TT
16829 mark_common_block_symbol_computed (sym, die, attr,
16830 member_loc, cu);
16831 }
16832 else
16833 dwarf2_complex_location_expr_complaint ();
16834 }
16835 }
c906108c 16836 }
4357ac6c
TT
16837
16838 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16839 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16840 }
16841}
16842
0114d602 16843/* Create a type for a C++ namespace. */
d9fa45fe 16844
0114d602
DJ
16845static struct type *
16846read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16847{
5e22e966 16848 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16849 const char *previous_prefix, *name;
9219021c 16850 int is_anonymous;
0114d602
DJ
16851 struct type *type;
16852
16853 /* For extensions, reuse the type of the original namespace. */
16854 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16855 {
16856 struct die_info *ext_die;
16857 struct dwarf2_cu *ext_cu = cu;
9a619af0 16858
0114d602
DJ
16859 ext_die = dwarf2_extension (die, &ext_cu);
16860 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16861
16862 /* EXT_CU may not be the same as CU.
02142a6c 16863 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16864 return set_die_type (die, type, cu);
16865 }
9219021c 16866
e142c38c 16867 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16868
16869 /* Now build the name of the current namespace. */
16870
0114d602
DJ
16871 previous_prefix = determine_prefix (die, cu);
16872 if (previous_prefix[0] != '\0')
16873 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16874 previous_prefix, name, 0, cu);
0114d602
DJ
16875
16876 /* Create the type. */
19f392bc 16877 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16878
60531b24 16879 return set_die_type (die, type, cu);
0114d602
DJ
16880}
16881
22cee43f 16882/* Read a namespace scope. */
0114d602
DJ
16883
16884static void
16885read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16886{
5e22e966 16887 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16888 int is_anonymous;
9219021c 16889
5c4e30ca
DC
16890 /* Add a symbol associated to this if we haven't seen the namespace
16891 before. Also, add a using directive if it's an anonymous
16892 namespace. */
9219021c 16893
f2f0e013 16894 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16895 {
16896 struct type *type;
16897
0114d602 16898 type = read_type_die (die, cu);
e7c27a73 16899 new_symbol (die, type, cu);
5c4e30ca 16900
e8e80198 16901 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16902 if (is_anonymous)
0114d602
DJ
16903 {
16904 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16905
eb1e02fd 16906 std::vector<const char *> excludes;
804d2729 16907 add_using_directive (using_directives (cu),
7d93a1e0 16908 previous_prefix, type->name (), NULL,
eb1e02fd 16909 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16910 }
5c4e30ca 16911 }
9219021c 16912
639d11d3 16913 if (die->child != NULL)
d9fa45fe 16914 {
639d11d3 16915 struct die_info *child_die = die->child;
6e70227d 16916
d9fa45fe
DC
16917 while (child_die && child_die->tag)
16918 {
e7c27a73 16919 process_die (child_die, cu);
436c571c 16920 child_die = child_die->sibling;
d9fa45fe
DC
16921 }
16922 }
38d518c9
EZ
16923}
16924
f55ee35c
JK
16925/* Read a Fortran module as type. This DIE can be only a declaration used for
16926 imported module. Still we need that type as local Fortran "use ... only"
16927 declaration imports depend on the created type in determine_prefix. */
16928
16929static struct type *
16930read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16931{
5e22e966 16932 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16933 const char *module_name;
f55ee35c
JK
16934 struct type *type;
16935
16936 module_name = dwarf2_name (die, cu);
19f392bc 16937 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16938
f55ee35c
JK
16939 return set_die_type (die, type, cu);
16940}
16941
5d7cb8df
JK
16942/* Read a Fortran module. */
16943
16944static void
16945read_module (struct die_info *die, struct dwarf2_cu *cu)
16946{
16947 struct die_info *child_die = die->child;
530e8392
KB
16948 struct type *type;
16949
16950 type = read_type_die (die, cu);
16951 new_symbol (die, type, cu);
5d7cb8df 16952
5d7cb8df
JK
16953 while (child_die && child_die->tag)
16954 {
16955 process_die (child_die, cu);
436c571c 16956 child_die = child_die->sibling;
5d7cb8df
JK
16957 }
16958}
16959
38d518c9
EZ
16960/* Return the name of the namespace represented by DIE. Set
16961 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16962 namespace. */
16963
16964static const char *
e142c38c 16965namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16966{
16967 struct die_info *current_die;
16968 const char *name = NULL;
16969
16970 /* Loop through the extensions until we find a name. */
16971
16972 for (current_die = die;
16973 current_die != NULL;
f2f0e013 16974 current_die = dwarf2_extension (die, &cu))
38d518c9 16975 {
96553a0c
DE
16976 /* We don't use dwarf2_name here so that we can detect the absence
16977 of a name -> anonymous namespace. */
7d45c7c3 16978 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16979
38d518c9
EZ
16980 if (name != NULL)
16981 break;
16982 }
16983
16984 /* Is it an anonymous namespace? */
16985
16986 *is_anonymous = (name == NULL);
16987 if (*is_anonymous)
2b1dbab0 16988 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16989
16990 return name;
d9fa45fe
DC
16991}
16992
c906108c
SS
16993/* Extract all information from a DW_TAG_pointer_type DIE and add to
16994 the user defined type vector. */
16995
f792889a 16996static struct type *
e7c27a73 16997read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16998{
5e22e966 16999 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17000 struct comp_unit_head *cu_header = &cu->header;
c906108c 17001 struct type *type;
8b2dbe47
KB
17002 struct attribute *attr_byte_size;
17003 struct attribute *attr_address_class;
17004 int byte_size, addr_class;
7e314c57
JK
17005 struct type *target_type;
17006
17007 target_type = die_type (die, cu);
c906108c 17008
7e314c57
JK
17009 /* The die_type call above may have already set the type for this DIE. */
17010 type = get_die_type (die, cu);
17011 if (type)
17012 return type;
17013
17014 type = lookup_pointer_type (target_type);
8b2dbe47 17015
e142c38c 17016 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17017 if (attr_byte_size)
529908cb 17018 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17019 else
8b2dbe47
KB
17020 byte_size = cu_header->addr_size;
17021
e142c38c 17022 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17023 if (attr_address_class)
529908cb 17024 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17025 else
17026 addr_class = DW_ADDR_none;
17027
2b4424c3
TT
17028 ULONGEST alignment = get_alignment (cu, die);
17029
17030 /* If the pointer size, alignment, or address class is different
17031 than the default, create a type variant marked as such and set
17032 the length accordingly. */
17033 if (TYPE_LENGTH (type) != byte_size
17034 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17035 && alignment != TYPE_RAW_ALIGN (type))
17036 || addr_class != DW_ADDR_none)
c906108c 17037 {
5e2b427d 17038 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17039 {
314ad88d
PA
17040 type_instance_flags type_flags
17041 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17042 addr_class);
876cecd0
TT
17043 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17044 == 0);
8b2dbe47
KB
17045 type = make_type_with_address_space (type, type_flags);
17046 }
17047 else if (TYPE_LENGTH (type) != byte_size)
17048 {
b98664d3 17049 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17050 }
2b4424c3
TT
17051 else if (TYPE_RAW_ALIGN (type) != alignment)
17052 {
b98664d3 17053 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17054 " - DIE at %s [in module %s]"),
17055 sect_offset_str (die->sect_off),
5e22e966 17056 objfile_name (cu->per_objfile->objfile));
2b4424c3 17057 }
6e70227d 17058 else
9a619af0
MS
17059 {
17060 /* Should we also complain about unhandled address classes? */
17061 }
c906108c 17062 }
8b2dbe47
KB
17063
17064 TYPE_LENGTH (type) = byte_size;
2b4424c3 17065 set_type_align (type, alignment);
f792889a 17066 return set_die_type (die, type, cu);
c906108c
SS
17067}
17068
17069/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17070 the user defined type vector. */
17071
f792889a 17072static struct type *
e7c27a73 17073read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17074{
17075 struct type *type;
17076 struct type *to_type;
17077 struct type *domain;
17078
e7c27a73
DJ
17079 to_type = die_type (die, cu);
17080 domain = die_containing_type (die, cu);
0d5de010 17081
7e314c57
JK
17082 /* The calls above may have already set the type for this DIE. */
17083 type = get_die_type (die, cu);
17084 if (type)
17085 return type;
17086
78134374 17087 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17088 type = lookup_methodptr_type (to_type);
78134374 17089 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17090 {
5e22e966 17091 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17092
17093 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17094 to_type->fields (), to_type->num_fields (),
a409645d 17095 to_type->has_varargs ());
7078baeb
TT
17096 type = lookup_methodptr_type (new_type);
17097 }
0d5de010
DJ
17098 else
17099 type = lookup_memberptr_type (to_type, domain);
c906108c 17100
f792889a 17101 return set_die_type (die, type, cu);
c906108c
SS
17102}
17103
4297a3f0 17104/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17105 the user defined type vector. */
17106
f792889a 17107static struct type *
4297a3f0 17108read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17109 enum type_code refcode)
c906108c 17110{
e7c27a73 17111 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17112 struct type *type, *target_type;
c906108c
SS
17113 struct attribute *attr;
17114
4297a3f0
AV
17115 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17116
7e314c57
JK
17117 target_type = die_type (die, cu);
17118
17119 /* The die_type call above may have already set the type for this DIE. */
17120 type = get_die_type (die, cu);
17121 if (type)
17122 return type;
17123
4297a3f0 17124 type = lookup_reference_type (target_type, refcode);
e142c38c 17125 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17126 if (attr != nullptr)
c906108c 17127 {
529908cb 17128 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17129 }
17130 else
17131 {
107d2387 17132 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17133 }
2b4424c3 17134 maybe_set_alignment (cu, die, type);
f792889a 17135 return set_die_type (die, type, cu);
c906108c
SS
17136}
17137
cf363f18
MW
17138/* Add the given cv-qualifiers to the element type of the array. GCC
17139 outputs DWARF type qualifiers that apply to an array, not the
17140 element type. But GDB relies on the array element type to carry
17141 the cv-qualifiers. This mimics section 6.7.3 of the C99
17142 specification. */
17143
17144static struct type *
17145add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17146 struct type *base_type, int cnst, int voltl)
17147{
17148 struct type *el_type, *inner_array;
17149
17150 base_type = copy_type (base_type);
17151 inner_array = base_type;
17152
78134374 17153 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17154 {
17155 TYPE_TARGET_TYPE (inner_array) =
17156 copy_type (TYPE_TARGET_TYPE (inner_array));
17157 inner_array = TYPE_TARGET_TYPE (inner_array);
17158 }
17159
17160 el_type = TYPE_TARGET_TYPE (inner_array);
17161 cnst |= TYPE_CONST (el_type);
17162 voltl |= TYPE_VOLATILE (el_type);
17163 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17164
17165 return set_die_type (die, base_type, cu);
17166}
17167
f792889a 17168static struct type *
e7c27a73 17169read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17170{
f792889a 17171 struct type *base_type, *cv_type;
c906108c 17172
e7c27a73 17173 base_type = die_type (die, cu);
7e314c57
JK
17174
17175 /* The die_type call above may have already set the type for this DIE. */
17176 cv_type = get_die_type (die, cu);
17177 if (cv_type)
17178 return cv_type;
17179
2f608a3a
KW
17180 /* In case the const qualifier is applied to an array type, the element type
17181 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17182 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17183 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17184
f792889a
DJ
17185 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17186 return set_die_type (die, cv_type, cu);
c906108c
SS
17187}
17188
f792889a 17189static struct type *
e7c27a73 17190read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17191{
f792889a 17192 struct type *base_type, *cv_type;
c906108c 17193
e7c27a73 17194 base_type = die_type (die, cu);
7e314c57
JK
17195
17196 /* The die_type call above may have already set the type for this DIE. */
17197 cv_type = get_die_type (die, cu);
17198 if (cv_type)
17199 return cv_type;
17200
cf363f18
MW
17201 /* In case the volatile qualifier is applied to an array type, the
17202 element type is so qualified, not the array type (section 6.7.3
17203 of C99). */
78134374 17204 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17205 return add_array_cv_type (die, cu, base_type, 0, 1);
17206
f792889a
DJ
17207 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17208 return set_die_type (die, cv_type, cu);
c906108c
SS
17209}
17210
06d66ee9
TT
17211/* Handle DW_TAG_restrict_type. */
17212
17213static struct type *
17214read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17215{
17216 struct type *base_type, *cv_type;
17217
17218 base_type = die_type (die, cu);
17219
17220 /* The die_type call above may have already set the type for this DIE. */
17221 cv_type = get_die_type (die, cu);
17222 if (cv_type)
17223 return cv_type;
17224
17225 cv_type = make_restrict_type (base_type);
17226 return set_die_type (die, cv_type, cu);
17227}
17228
a2c2acaf
MW
17229/* Handle DW_TAG_atomic_type. */
17230
17231static struct type *
17232read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17233{
17234 struct type *base_type, *cv_type;
17235
17236 base_type = die_type (die, cu);
17237
17238 /* The die_type call above may have already set the type for this DIE. */
17239 cv_type = get_die_type (die, cu);
17240 if (cv_type)
17241 return cv_type;
17242
17243 cv_type = make_atomic_type (base_type);
17244 return set_die_type (die, cv_type, cu);
17245}
17246
c906108c
SS
17247/* Extract all information from a DW_TAG_string_type DIE and add to
17248 the user defined type vector. It isn't really a user defined type,
17249 but it behaves like one, with other DIE's using an AT_user_def_type
17250 attribute to reference it. */
17251
f792889a 17252static struct type *
e7c27a73 17253read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17254{
5e22e966 17255 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17256 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17257 struct type *type, *range_type, *index_type, *char_type;
17258 struct attribute *attr;
216a7e6b
AB
17259 struct dynamic_prop prop;
17260 bool length_is_constant = true;
17261 LONGEST length;
17262
17263 /* There are a couple of places where bit sizes might be made use of
17264 when parsing a DW_TAG_string_type, however, no producer that we know
17265 of make use of these. Handling bit sizes that are a multiple of the
17266 byte size is easy enough, but what about other bit sizes? Lets deal
17267 with that problem when we have to. Warn about these attributes being
17268 unsupported, then parse the type and ignore them like we always
17269 have. */
17270 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17271 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17272 {
17273 static bool warning_printed = false;
17274 if (!warning_printed)
17275 {
17276 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17277 "currently supported on DW_TAG_string_type."));
17278 warning_printed = true;
17279 }
17280 }
c906108c 17281
e142c38c 17282 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17283 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17284 {
17285 /* The string length describes the location at which the length of
17286 the string can be found. The size of the length field can be
17287 specified with one of the attributes below. */
17288 struct type *prop_type;
17289 struct attribute *len
17290 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17291 if (len == nullptr)
17292 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17293 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17294 {
17295 /* Pass 0 as the default as we know this attribute is constant
17296 and the default value will not be returned. */
0826b30a 17297 LONGEST sz = len->constant_value (0);
293e7e51 17298 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17299 }
17300 else
17301 {
17302 /* If the size is not specified then we assume it is the size of
17303 an address on this target. */
293e7e51 17304 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17305 }
17306
17307 /* Convert the attribute into a dynamic property. */
17308 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17309 length = 1;
17310 else
17311 length_is_constant = false;
17312 }
17313 else if (attr != nullptr)
17314 {
17315 /* This DW_AT_string_length just contains the length with no
17316 indirection. There's no need to create a dynamic property in this
17317 case. Pass 0 for the default value as we know it will not be
17318 returned in this case. */
0826b30a 17319 length = attr->constant_value (0);
216a7e6b
AB
17320 }
17321 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17322 {
216a7e6b 17323 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17324 length = attr->constant_value (1);
c906108c
SS
17325 }
17326 else
17327 {
216a7e6b
AB
17328 /* Use 1 as a fallback length if we have nothing else. */
17329 length = 1;
c906108c 17330 }
6ccb9162 17331
46bf5051 17332 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17333 if (length_is_constant)
17334 range_type = create_static_range_type (NULL, index_type, 1, length);
17335 else
17336 {
17337 struct dynamic_prop low_bound;
17338
8c2e4e06 17339 low_bound.set_const_val (1);
216a7e6b
AB
17340 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17341 }
3b7538c0
UW
17342 char_type = language_string_char_type (cu->language_defn, gdbarch);
17343 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17344
f792889a 17345 return set_die_type (die, type, cu);
c906108c
SS
17346}
17347
4d804846
JB
17348/* Assuming that DIE corresponds to a function, returns nonzero
17349 if the function is prototyped. */
17350
17351static int
17352prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17353{
17354 struct attribute *attr;
17355
17356 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17357 if (attr && attr->as_boolean ())
4d804846
JB
17358 return 1;
17359
17360 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17361 is only meaningful for C, but the concept also extends to other
4d804846
JB
17362 languages that allow unprototyped functions (Eg: Objective C).
17363 For all other languages, assume that functions are always
17364 prototyped. */
17365 if (cu->language != language_c
17366 && cu->language != language_objc
17367 && cu->language != language_opencl)
17368 return 1;
17369
17370 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17371 prototyped and unprototyped functions; default to prototyped,
17372 since that is more common in modern code (and RealView warns
17373 about unprototyped functions). */
17374 if (producer_is_realview (cu->producer))
17375 return 1;
17376
17377 return 0;
17378}
17379
c906108c
SS
17380/* Handle DIES due to C code like:
17381
17382 struct foo
c5aa993b
JM
17383 {
17384 int (*funcp)(int a, long l);
17385 int b;
17386 };
c906108c 17387
0963b4bd 17388 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17389
f792889a 17390static struct type *
e7c27a73 17391read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17392{
5e22e966 17393 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17394 struct type *type; /* Type that this function returns. */
17395 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17396 struct attribute *attr;
17397
e7c27a73 17398 type = die_type (die, cu);
7e314c57
JK
17399
17400 /* The die_type call above may have already set the type for this DIE. */
17401 ftype = get_die_type (die, cu);
17402 if (ftype)
17403 return ftype;
17404
0c8b41f1 17405 ftype = lookup_function_type (type);
c906108c 17406
4d804846 17407 if (prototyped_function_p (die, cu))
27e69b7a 17408 ftype->set_is_prototyped (true);
c906108c 17409
c055b101
CV
17410 /* Store the calling convention in the type if it's available in
17411 the subroutine die. Otherwise set the calling convention to
17412 the default value DW_CC_normal. */
17413 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 17414 if (attr != nullptr
529908cb 17415 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 17416 TYPE_CALLING_CONVENTION (ftype)
529908cb 17417 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
17418 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17419 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17420 else
17421 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17422
743649fd
MW
17423 /* Record whether the function returns normally to its caller or not
17424 if the DWARF producer set that information. */
17425 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 17426 if (attr && attr->as_boolean ())
743649fd
MW
17427 TYPE_NO_RETURN (ftype) = 1;
17428
76c10ea2
GM
17429 /* We need to add the subroutine type to the die immediately so
17430 we don't infinitely recurse when dealing with parameters
0963b4bd 17431 declared as the same subroutine type. */
76c10ea2 17432 set_die_type (die, ftype, cu);
6e70227d 17433
639d11d3 17434 if (die->child != NULL)
c906108c 17435 {
bb5ed363 17436 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17437 struct die_info *child_die;
8072405b 17438 int nparams, iparams;
c906108c
SS
17439
17440 /* Count the number of parameters.
dda83cd7
SM
17441 FIXME: GDB currently ignores vararg functions, but knows about
17442 vararg member functions. */
8072405b 17443 nparams = 0;
639d11d3 17444 child_die = die->child;
c906108c
SS
17445 while (child_die && child_die->tag)
17446 {
17447 if (child_die->tag == DW_TAG_formal_parameter)
17448 nparams++;
17449 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
17450 ftype->set_has_varargs (true);
17451
436c571c 17452 child_die = child_die->sibling;
c906108c
SS
17453 }
17454
17455 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17456 ftype->set_num_fields (nparams);
3cabb6b0
SM
17457 ftype->set_fields
17458 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17459
8072405b
JK
17460 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17461 even if we error out during the parameters reading below. */
17462 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17463 ftype->field (iparams).set_type (void_type);
8072405b
JK
17464
17465 iparams = 0;
639d11d3 17466 child_die = die->child;
c906108c
SS
17467 while (child_die && child_die->tag)
17468 {
17469 if (child_die->tag == DW_TAG_formal_parameter)
17470 {
3ce3b1ba
PA
17471 struct type *arg_type;
17472
17473 /* DWARF version 2 has no clean way to discern C++
17474 static and non-static member functions. G++ helps
17475 GDB by marking the first parameter for non-static
17476 member functions (which is the this pointer) as
17477 artificial. We pass this information to
17478 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17479
17480 DWARF version 3 added DW_AT_object_pointer, which GCC
17481 4.5 does not yet generate. */
e142c38c 17482 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17483 if (attr != nullptr)
c45bc3f8 17484 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 17485 else
9c37b5ae 17486 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17487 arg_type = die_type (child_die, cu);
17488
17489 /* RealView does not mark THIS as const, which the testsuite
17490 expects. GCC marks THIS as const in method definitions,
17491 but not in the class specifications (GCC PR 43053). */
17492 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17493 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17494 {
17495 int is_this = 0;
17496 struct dwarf2_cu *arg_cu = cu;
17497 const char *name = dwarf2_name (child_die, cu);
17498
17499 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17500 if (attr != nullptr)
3ce3b1ba
PA
17501 {
17502 /* If the compiler emits this, use it. */
17503 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17504 is_this = 1;
17505 }
17506 else if (name && strcmp (name, "this") == 0)
17507 /* Function definitions will have the argument names. */
17508 is_this = 1;
17509 else if (name == NULL && iparams == 0)
17510 /* Declarations may not have the names, so like
17511 elsewhere in GDB, assume an artificial first
17512 argument is "this". */
17513 is_this = 1;
17514
17515 if (is_this)
17516 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17517 arg_type, 0);
17518 }
17519
5d14b6e5 17520 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17521 iparams++;
17522 }
436c571c 17523 child_die = child_die->sibling;
c906108c
SS
17524 }
17525 }
17526
76c10ea2 17527 return ftype;
c906108c
SS
17528}
17529
f792889a 17530static struct type *
e7c27a73 17531read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17532{
5e22e966 17533 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17534 const char *name = NULL;
3c8e0968 17535 struct type *this_type, *target_type;
c906108c 17536
94af9270 17537 name = dwarf2_full_name (NULL, die, cu);
19f392bc 17538 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 17539 this_type->set_target_is_stub (true);
f792889a 17540 set_die_type (die, this_type, cu);
3c8e0968
DE
17541 target_type = die_type (die, cu);
17542 if (target_type != this_type)
17543 TYPE_TARGET_TYPE (this_type) = target_type;
17544 else
17545 {
17546 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17547 spec and cause infinite loops in GDB. */
b98664d3 17548 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17549 "- DIE at %s [in module %s]"),
17550 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17551 TYPE_TARGET_TYPE (this_type) = NULL;
17552 }
e4003a34
TV
17553 if (name == NULL)
17554 {
17555 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17556 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17557 Handle these by just returning the target type, rather than
17558 constructing an anonymous typedef type and trying to handle this
17559 elsewhere. */
17560 set_die_type (die, target_type, cu);
17561 return target_type;
17562 }
f792889a 17563 return this_type;
c906108c
SS
17564}
17565
a625a8c9
TT
17566/* Helper for get_dwarf2_rational_constant that computes the value of
17567 a given gmp_mpz given an attribute. */
17568
17569static void
17570get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17571{
17572 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17573 location expression that pushes an implicit value. */
17574 if (attr->form == DW_FORM_exprloc)
17575 {
17576 dwarf_block *blk = attr->as_block ();
17577 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17578 {
17579 uint64_t len;
17580 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17581 blk->data + blk->size,
17582 &len);
17583 if (ptr - blk->data + len <= blk->size)
17584 {
17585 mpz_import (value->val, len,
17586 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17587 1, 0, 0, ptr);
17588 return;
17589 }
17590 }
17591
17592 /* On failure set it to 1. */
17593 *value = gdb_mpz (1);
17594 }
17595 else if (attr->form_is_block ())
17596 {
17597 dwarf_block *blk = attr->as_block ();
17598 mpz_import (value->val, blk->size,
17599 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17600 1, 0, 0, blk->data);
17601 }
17602 else
17603 *value = gdb_mpz (attr->constant_value (1));
17604}
17605
09584414
JB
17606/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17607 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17608
17609 If the numerator and/or numerator attribute is missing,
17610 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17611 untouched. */
17612
17613static void
17614get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 17615 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
17616{
17617 struct attribute *num_attr, *denom_attr;
17618
17619 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17620 if (num_attr == nullptr)
17621 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17622 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17623
17624 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17625 if (denom_attr == nullptr)
17626 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17627 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17628
17629 if (num_attr == nullptr || denom_attr == nullptr)
17630 return;
17631
a625a8c9
TT
17632 get_mpz (cu, numerator, num_attr);
17633 get_mpz (cu, denominator, denom_attr);
09584414
JB
17634}
17635
17636/* Same as get_dwarf2_rational_constant, but extracting an unsigned
17637 rational constant, rather than a signed one.
17638
17639 If the rational constant has a negative value, a complaint
17640 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17641
17642static void
17643get_dwarf2_unsigned_rational_constant (struct die_info *die,
17644 struct dwarf2_cu *cu,
5cde1d82
TT
17645 gdb_mpz *numerator,
17646 gdb_mpz *denominator)
09584414 17647{
5cde1d82
TT
17648 gdb_mpz num (1);
17649 gdb_mpz denom (1);
09584414
JB
17650
17651 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 17652 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 17653 {
5cde1d82
TT
17654 mpz_neg (num.val, num.val);
17655 mpz_neg (denom.val, denom.val);
09584414 17656 }
5cde1d82 17657 else if (mpz_sgn (num.val) == -1)
09584414
JB
17658 {
17659 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17660 " in DIE at %s"),
17661 sect_offset_str (die->sect_off));
17662 return;
17663 }
5cde1d82 17664 else if (mpz_sgn (denom.val) == -1)
09584414
JB
17665 {
17666 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17667 " in DIE at %s"),
17668 sect_offset_str (die->sect_off));
17669 return;
17670 }
17671
5cde1d82
TT
17672 *numerator = std::move (num);
17673 *denominator = std::move (denom);
09584414
JB
17674}
17675
bbcdf9ab
TT
17676/* Assuming that ENCODING is a string whose contents starting at the
17677 K'th character is "_nn" where "nn" is a decimal number, scan that
17678 number and set RESULT to the value. K is updated to point to the
17679 character immediately following the number.
17680
17681 If the string does not conform to the format described above, false
17682 is returned, and K may or may not be changed. */
17683
17684static bool
17685ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17686{
17687 /* The next character should be an underscore ('_') followed
17688 by a digit. */
17689 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17690 return false;
17691
17692 /* Skip the underscore. */
17693 k++;
17694 int start = k;
17695
17696 /* Determine the number of digits for our number. */
17697 while (isdigit (encoding[k]))
17698 k++;
17699 if (k == start)
17700 return false;
17701
17702 std::string copy (&encoding[start], k - start);
17703 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17704 return false;
17705
17706 return true;
17707}
17708
17709/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17710 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17711 DENOM, update OFFSET, and return true on success. Return false on
17712 failure. */
17713
17714static bool
17715ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17716 gdb_mpz *num, gdb_mpz *denom)
17717{
17718 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17719 return false;
17720 return ada_get_gnat_encoded_number (encoding, offset, denom);
17721}
17722
09584414 17723/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
17724 of the corresponding TYPE by setting its type-specific data. CU is
17725 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17726 encodings. It is nullptr if the GNAT encoding should be
17727 ignored. */
09584414
JB
17728
17729static void
bbcdf9ab
TT
17730finish_fixed_point_type (struct type *type, const char *suffix,
17731 struct die_info *die, struct dwarf2_cu *cu)
09584414 17732{
09584414
JB
17733 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17734 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17735
bbcdf9ab
TT
17736 /* If GNAT encodings are preferred, don't examine the
17737 attributes. */
17738 struct attribute *attr = nullptr;
17739 if (suffix == nullptr)
17740 {
17741 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17742 if (attr == nullptr)
17743 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17744 if (attr == nullptr)
17745 attr = dwarf2_attr (die, DW_AT_small, cu);
17746 }
09584414 17747
5cde1d82
TT
17748 /* Numerator and denominator of our fixed-point type's scaling factor.
17749 The default is a scaling factor of 1, which we use as a fallback
17750 when we are not able to decode it (problem with the debugging info,
17751 unsupported forms, bug in GDB, etc...). Using that as the default
17752 allows us to at least print the unscaled value, which might still
17753 be useful to a user. */
17754 gdb_mpz scale_num (1);
17755 gdb_mpz scale_denom (1);
17756
09584414
JB
17757 if (attr == nullptr)
17758 {
bbcdf9ab
TT
17759 int offset = 0;
17760 if (suffix != nullptr
17761 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17762 &scale_denom)
17763 /* The number might be encoded as _nn_dd_nn_dd, where the
17764 second ratio is the 'small value. In this situation, we
17765 want the second value. */
17766 && (suffix[offset] != '_'
17767 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17768 &scale_denom)))
17769 {
17770 /* Found it. */
17771 }
17772 else
17773 {
17774 /* Scaling factor not found. Assume a scaling factor of 1,
17775 and hope for the best. At least the user will be able to
17776 see the encoded value. */
17777 scale_num = 1;
17778 scale_denom = 1;
17779 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17780 sect_offset_str (die->sect_off));
17781 }
09584414
JB
17782 }
17783 else if (attr->name == DW_AT_binary_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_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
17789 }
17790 else if (attr->name == DW_AT_decimal_scale)
17791 {
17792 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 17793 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 17794
5cde1d82 17795 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
17796 }
17797 else if (attr->name == DW_AT_small)
17798 {
17799 struct die_info *scale_die;
17800 struct dwarf2_cu *scale_cu = cu;
17801
17802 scale_die = follow_die_ref (die, attr, &scale_cu);
17803 if (scale_die->tag == DW_TAG_constant)
17804 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17805 &scale_num, &scale_denom);
17806 else
17807 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17808 " (DIE at %s)"),
17809 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17810 }
17811 else
17812 {
17813 complaint (_("unsupported scale attribute %s for fixed-point type"
17814 " (DIE at %s)"),
17815 dwarf_attr_name (attr->name),
17816 sect_offset_str (die->sect_off));
17817 }
17818
2a12c336 17819 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
17820 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17821 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
17822 mpq_canonicalize (scaling_factor.val);
17823}
17824
bbcdf9ab
TT
17825/* The gnat-encoding suffix for fixed point. */
17826
17827#define GNAT_FIXED_POINT_SUFFIX "___XF_"
17828
17829/* If NAME encodes an Ada fixed-point type, return a pointer to the
17830 "XF" suffix of the name. The text after this is what encodes the
17831 'small and 'delta information. Otherwise, return nullptr. */
17832
17833static const char *
17834gnat_encoded_fixed_point_type_info (const char *name)
17835{
17836 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17837}
17838
9b790ce7
UW
17839/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17840 (which may be different from NAME) to the architecture back-end to allow
17841 it to guess the correct format if necessary. */
17842
17843static struct type *
17844dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17845 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17846{
08feed99 17847 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17848 const struct floatformat **format;
17849 struct type *type;
17850
17851 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17852 if (format)
103a685e 17853 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17854 else
77b7c781 17855 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17856
17857 return type;
17858}
17859
eb77c9df
AB
17860/* Allocate an integer type of size BITS and name NAME. */
17861
17862static struct type *
17863dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17864 int bits, int unsigned_p, const char *name)
17865{
17866 struct type *type;
17867
17868 /* Versions of Intel's C Compiler generate an integer type called "void"
17869 instead of using DW_TAG_unspecified_type. This has been seen on
17870 at least versions 14, 17, and 18. */
35ee2dc2
AB
17871 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17872 && strcmp (name, "void") == 0)
eb77c9df
AB
17873 type = objfile_type (objfile)->builtin_void;
17874 else
17875 type = init_integer_type (objfile, bits, unsigned_p, name);
17876
17877 return type;
17878}
17879
09584414
JB
17880/* Return true if DIE has a DW_AT_small attribute whose value is
17881 a constant rational, where both the numerator and denominator
17882 are equal to zero.
17883
17884 CU is the DIE's Compilation Unit. */
17885
17886static bool
17887has_zero_over_zero_small_attribute (struct die_info *die,
17888 struct dwarf2_cu *cu)
17889{
17890 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
17891 if (attr == nullptr)
17892 return false;
17893
17894 struct dwarf2_cu *scale_cu = cu;
17895 struct die_info *scale_die
17896 = follow_die_ref (die, attr, &scale_cu);
17897
17898 if (scale_die->tag != DW_TAG_constant)
17899 return false;
17900
5cde1d82 17901 gdb_mpz num (1), denom (1);
09584414 17902 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 17903 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
17904}
17905
8bdc1658
AB
17906/* Initialise and return a floating point type of size BITS suitable for
17907 use as a component of a complex number. The NAME_HINT is passed through
17908 when initialising the floating point type and is the name of the complex
17909 type.
17910
17911 As DWARF doesn't currently provide an explicit name for the components
17912 of a complex number, but it can be helpful to have these components
17913 named, we try to select a suitable name based on the size of the
17914 component. */
17915static struct type *
17916dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17917 struct objfile *objfile,
103a685e
TT
17918 int bits, const char *name_hint,
17919 enum bfd_endian byte_order)
8bdc1658 17920{
08feed99 17921 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17922 struct type *tt = nullptr;
17923
35add35e
AB
17924 /* Try to find a suitable floating point builtin type of size BITS.
17925 We're going to use the name of this type as the name for the complex
17926 target type that we are about to create. */
1db455a7 17927 switch (cu->language)
8bdc1658 17928 {
1db455a7
AB
17929 case language_fortran:
17930 switch (bits)
17931 {
17932 case 32:
17933 tt = builtin_f_type (gdbarch)->builtin_real;
17934 break;
17935 case 64:
17936 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17937 break;
17938 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17939 case 128:
17940 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17941 break;
17942 }
8bdc1658 17943 break;
1db455a7
AB
17944 default:
17945 switch (bits)
17946 {
17947 case 32:
17948 tt = builtin_type (gdbarch)->builtin_float;
17949 break;
17950 case 64:
17951 tt = builtin_type (gdbarch)->builtin_double;
17952 break;
17953 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17954 case 128:
17955 tt = builtin_type (gdbarch)->builtin_long_double;
17956 break;
17957 }
8bdc1658
AB
17958 break;
17959 }
17960
35add35e
AB
17961 /* If the type we found doesn't match the size we were looking for, then
17962 pretend we didn't find a type at all, the complex target type we
17963 create will then be nameless. */
a12e5744 17964 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17965 tt = nullptr;
17966
7d93a1e0 17967 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17968 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17969}
17970
c906108c
SS
17971/* Find a representation of a given base type and install
17972 it in the TYPE field of the die. */
17973
f792889a 17974static struct type *
e7c27a73 17975read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17976{
5e22e966 17977 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17978 struct type *type;
17979 struct attribute *attr;
19f392bc 17980 int encoding = 0, bits = 0;
15d034d0 17981 const char *name;
34877895 17982 gdbarch *arch;
c906108c 17983
e142c38c 17984 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
17985 if (attr != nullptr && attr->form_is_constant ())
17986 encoding = attr->constant_value (0);
e142c38c 17987 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17988 if (attr != nullptr)
529908cb 17989 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 17990 name = dwarf2_name (die, cu);
6ccb9162 17991 if (!name)
34877895 17992 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17993
08feed99 17994 arch = objfile->arch ();
103a685e
TT
17995 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17996
34877895 17997 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 17998 if (attr != nullptr && attr->form_is_constant ())
103a685e 17999 {
529908cb 18000 int endianity = attr->constant_value (0);
103a685e
TT
18001
18002 switch (endianity)
18003 {
18004 case DW_END_big:
18005 byte_order = BFD_ENDIAN_BIG;
18006 break;
18007 case DW_END_little:
18008 byte_order = BFD_ENDIAN_LITTLE;
18009 break;
18010 default:
18011 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18012 break;
18013 }
18014 }
6ccb9162 18015
09584414
JB
18016 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18017 && cu->language == language_ada
18018 && has_zero_over_zero_small_attribute (die, cu))
18019 {
18020 /* brobecker/2018-02-24: This is a fixed point type for which
18021 the scaling factor is represented as fraction whose value
18022 does not make sense (zero divided by zero), so we should
18023 normally never see these. However, there is a small category
18024 of fixed point types for which GNAT is unable to provide
18025 the scaling factor via the standard DWARF mechanisms, and
18026 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18027 This is likely what this DIE is about. */
09584414
JB
18028 encoding = (encoding == DW_ATE_signed_fixed
18029 ? DW_ATE_signed
18030 : DW_ATE_unsigned);
18031 }
18032
bbcdf9ab
TT
18033 /* With GNAT encodings, fixed-point information will be encoded in
18034 the type name. Note that this can also occur with the above
18035 zero-over-zero case, which is why this is a separate "if" rather
18036 than an "else if". */
18037 const char *gnat_encoding_suffix = nullptr;
18038 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18039 && cu->language == language_ada
18040 && name != nullptr)
18041 {
18042 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18043 if (gnat_encoding_suffix != nullptr)
18044 {
18045 gdb_assert (startswith (gnat_encoding_suffix,
18046 GNAT_FIXED_POINT_SUFFIX));
18047 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18048 name, gnat_encoding_suffix - name);
18049 /* Use -1 here so that SUFFIX points at the "_" after the
18050 "XF". */
18051 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18052
18053 encoding = (encoding == DW_ATE_signed
18054 ? DW_ATE_signed_fixed
18055 : DW_ATE_unsigned_fixed);
18056 }
18057 }
18058
6ccb9162 18059 switch (encoding)
c906108c 18060 {
6ccb9162
UW
18061 case DW_ATE_address:
18062 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18063 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18064 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18065 break;
18066 case DW_ATE_boolean:
19f392bc 18067 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18068 break;
18069 case DW_ATE_complex_float:
103a685e
TT
18070 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18071 byte_order);
78134374 18072 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18073 {
18074 if (name == nullptr)
18075 {
18076 struct obstack *obstack
5e22e966 18077 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18078 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18079 nullptr);
18080 }
18081 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18082 }
18083 else
18084 type = init_complex_type (name, type);
6ccb9162
UW
18085 break;
18086 case DW_ATE_decimal_float:
19f392bc 18087 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18088 break;
18089 case DW_ATE_float:
103a685e 18090 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18091 break;
18092 case DW_ATE_signed:
eb77c9df 18093 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18094 break;
18095 case DW_ATE_unsigned:
3b2b8fea
TT
18096 if (cu->language == language_fortran
18097 && name
61012eef 18098 && startswith (name, "character("))
19f392bc
UW
18099 type = init_character_type (objfile, bits, 1, name);
18100 else
eb77c9df 18101 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18102 break;
18103 case DW_ATE_signed_char:
6e70227d 18104 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18105 || cu->language == language_pascal
18106 || cu->language == language_fortran)
19f392bc
UW
18107 type = init_character_type (objfile, bits, 0, name);
18108 else
eb77c9df 18109 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18110 break;
18111 case DW_ATE_unsigned_char:
868a0084 18112 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18113 || cu->language == language_pascal
c44af4eb
TT
18114 || cu->language == language_fortran
18115 || cu->language == language_rust)
19f392bc
UW
18116 type = init_character_type (objfile, bits, 1, name);
18117 else
eb77c9df 18118 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18119 break;
75079b2b 18120 case DW_ATE_UTF:
53e710ac 18121 {
53e710ac
PA
18122 if (bits == 16)
18123 type = builtin_type (arch)->builtin_char16;
18124 else if (bits == 32)
18125 type = builtin_type (arch)->builtin_char32;
18126 else
18127 {
b98664d3 18128 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18129 bits);
eb77c9df 18130 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18131 }
18132 return set_die_type (die, type, cu);
18133 }
75079b2b 18134 break;
09584414
JB
18135 case DW_ATE_signed_fixed:
18136 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18137 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18138 break;
18139 case DW_ATE_unsigned_fixed:
18140 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18141 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18142 break;
75079b2b 18143
6ccb9162 18144 default:
b98664d3 18145 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18146 dwarf_type_encoding_name (encoding));
77b7c781 18147 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18148 break;
c906108c 18149 }
6ccb9162 18150
0114d602 18151 if (name && strcmp (name, "char") == 0)
15152a54 18152 type->set_has_no_signedness (true);
0114d602 18153
2b4424c3
TT
18154 maybe_set_alignment (cu, die, type);
18155
db558e34 18156 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18157
20a5fcbd
TT
18158 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18159 {
18160 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18161 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18162 {
529908cb 18163 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18164 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18165 /* Only use the attributes if they make sense together. */
18166 if (attr == nullptr
529908cb
TT
18167 || (attr->as_unsigned () + real_bit_size
18168 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18169 {
18170 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18171 = real_bit_size;
18172 if (attr != nullptr)
18173 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18174 = attr->as_unsigned ();
20a5fcbd
TT
18175 }
18176 }
18177 }
18178
f792889a 18179 return set_die_type (die, type, cu);
c906108c
SS
18180}
18181
80180f79
SA
18182/* Parse dwarf attribute if it's a block, reference or constant and put the
18183 resulting value of the attribute into struct bound_prop.
18184 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18185
18186static int
18187attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18188 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18189 struct type *default_type)
80180f79
SA
18190{
18191 struct dwarf2_property_baton *baton;
5e22e966 18192 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18193 struct objfile *objfile = per_objfile->objfile;
18194 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18195
9a49df9d
AB
18196 gdb_assert (default_type != NULL);
18197
80180f79
SA
18198 if (attr == NULL || prop == NULL)
18199 return 0;
18200
4fc6c0d5 18201 if (attr->form_is_block ())
80180f79 18202 {
8d749320 18203 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18204 baton->property_type = default_type;
80180f79 18205 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18206 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18207
18208 struct dwarf_block *block = attr->as_block ();
18209 baton->locexpr.size = block->size;
18210 baton->locexpr.data = block->data;
216a7e6b
AB
18211 switch (attr->name)
18212 {
18213 case DW_AT_string_length:
18214 baton->locexpr.is_reference = true;
18215 break;
18216 default:
18217 baton->locexpr.is_reference = false;
18218 break;
18219 }
8c2e4e06
SM
18220
18221 prop->set_locexpr (baton);
18222 gdb_assert (prop->baton () != NULL);
80180f79 18223 }
cd6c91b4 18224 else if (attr->form_is_ref ())
80180f79
SA
18225 {
18226 struct dwarf2_cu *target_cu = cu;
18227 struct die_info *target_die;
18228 struct attribute *target_attr;
18229
18230 target_die = follow_die_ref (die, attr, &target_cu);
18231 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18232 if (target_attr == NULL)
18233 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18234 target_cu);
80180f79
SA
18235 if (target_attr == NULL)
18236 return 0;
18237
df25ebbd 18238 switch (target_attr->name)
80180f79 18239 {
df25ebbd 18240 case DW_AT_location:
cd6c91b4 18241 if (target_attr->form_is_section_offset ())
df25ebbd 18242 {
8d749320 18243 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18244 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18245 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18246 prop->set_loclist (baton);
18247 gdb_assert (prop->baton () != NULL);
df25ebbd 18248 }
4fc6c0d5 18249 else if (target_attr->form_is_block ())
df25ebbd 18250 {
8d749320 18251 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18252 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18253 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18254 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18255 struct dwarf_block *block = target_attr->as_block ();
18256 baton->locexpr.size = block->size;
18257 baton->locexpr.data = block->data;
9a49df9d 18258 baton->locexpr.is_reference = true;
8c2e4e06
SM
18259 prop->set_locexpr (baton);
18260 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18261 }
18262 else
18263 {
18264 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18265 "dynamic property");
18266 return 0;
18267 }
18268 break;
18269 case DW_AT_data_member_location:
18270 {
18271 LONGEST offset;
18272
18273 if (!handle_data_member_location (target_die, target_cu,
18274 &offset))
18275 return 0;
18276
8d749320 18277 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18278 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18279 target_cu);
df25ebbd
JB
18280 baton->offset_info.offset = offset;
18281 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18282 prop->set_addr_offset (baton);
df25ebbd
JB
18283 break;
18284 }
80180f79
SA
18285 }
18286 }
cd6c91b4 18287 else if (attr->form_is_constant ())
8c2e4e06 18288 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18289 else
18290 {
18291 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18292 dwarf2_name (die, cu));
18293 return 0;
18294 }
18295
18296 return 1;
18297}
18298
09ba997f 18299/* See read.h. */
9a49df9d 18300
09ba997f 18301struct type *
293e7e51 18302dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18303{
9a49df9d
AB
18304 struct type *int_type;
18305
18306 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18307#define TRY_TYPE(F) \
18308 int_type = (unsigned_p \
18309 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18310 : objfile_type (objfile)->builtin_ ## F); \
18311 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18312 return int_type
18313
18314 TRY_TYPE (char);
18315 TRY_TYPE (short);
18316 TRY_TYPE (int);
18317 TRY_TYPE (long);
18318 TRY_TYPE (long_long);
18319
18320#undef TRY_TYPE
18321
18322 gdb_assert_not_reached ("unable to find suitable integer type");
18323}
18324
b86352cf
AB
18325/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18326 present (which is valid) then compute the default type based on the
18327 compilation units address size. */
18328
18329static struct type *
18330read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18331{
18332 struct type *index_type = die_type (die, cu);
18333
18334 /* Dwarf-2 specifications explicitly allows to create subrange types
18335 without specifying a base type.
18336 In that case, the base type must be set to the type of
18337 the lower bound, upper bound or count, in that order, if any of these
18338 three attributes references an object that has a type.
18339 If no base type is found, the Dwarf-2 specifications say that
18340 a signed integer type of size equal to the size of an address should
18341 be used.
18342 For the following C code: `extern char gdb_int [];'
18343 GCC produces an empty range DIE.
18344 FIXME: muller/2010-05-28: Possible references to object for low bound,
18345 high bound or count are not yet handled by this code. */
78134374 18346 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18347 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18348
18349 return index_type;
18350}
18351
a02abb62
JB
18352/* Read the given DW_AT_subrange DIE. */
18353
f792889a 18354static struct type *
a02abb62
JB
18355read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18356{
4c9ad8c2 18357 struct type *base_type, *orig_base_type;
a02abb62
JB
18358 struct type *range_type;
18359 struct attribute *attr;
729efb13 18360 struct dynamic_prop low, high;
4fae6e18 18361 int low_default_is_valid;
c451ebe5 18362 int high_bound_is_count = 0;
15d034d0 18363 const char *name;
d359392f 18364 ULONGEST negative_mask;
e77813c8 18365
b86352cf
AB
18366 orig_base_type = read_subrange_index_type (die, cu);
18367
4c9ad8c2
TT
18368 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18369 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18370 creating the range type, but we use the result of check_typedef
18371 when examining properties of the type. */
18372 base_type = check_typedef (orig_base_type);
a02abb62 18373
7e314c57
JK
18374 /* The die_type call above may have already set the type for this DIE. */
18375 range_type = get_die_type (die, cu);
18376 if (range_type)
18377 return range_type;
18378
8c2e4e06 18379 high.set_const_val (0);
729efb13 18380
4fae6e18
JK
18381 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18382 omitting DW_AT_lower_bound. */
18383 switch (cu->language)
6e70227d 18384 {
4fae6e18
JK
18385 case language_c:
18386 case language_cplus:
8c2e4e06 18387 low.set_const_val (0);
4fae6e18
JK
18388 low_default_is_valid = 1;
18389 break;
18390 case language_fortran:
8c2e4e06 18391 low.set_const_val (1);
4fae6e18
JK
18392 low_default_is_valid = 1;
18393 break;
18394 case language_d:
4fae6e18 18395 case language_objc:
c44af4eb 18396 case language_rust:
8c2e4e06 18397 low.set_const_val (0);
4fae6e18
JK
18398 low_default_is_valid = (cu->header.version >= 4);
18399 break;
18400 case language_ada:
18401 case language_m2:
18402 case language_pascal:
8c2e4e06 18403 low.set_const_val (1);
4fae6e18
JK
18404 low_default_is_valid = (cu->header.version >= 4);
18405 break;
18406 default:
8c2e4e06 18407 low.set_const_val (0);
4fae6e18
JK
18408 low_default_is_valid = 0;
18409 break;
a02abb62
JB
18410 }
18411
e142c38c 18412 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18413 if (attr != nullptr)
9a49df9d 18414 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18415 else if (!low_default_is_valid)
b98664d3 18416 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18417 "- DIE at %s [in module %s]"),
18418 sect_offset_str (die->sect_off),
5e22e966 18419 objfile_name (cu->per_objfile->objfile));
a02abb62 18420
506f5c41
TV
18421 struct attribute *attr_ub, *attr_count;
18422 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18423 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18424 {
506f5c41 18425 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18426 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18427 {
c451ebe5 18428 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18429 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18430 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18431 else
18432 high_bound_is_count = 1;
c2ff108b 18433 }
506f5c41
TV
18434 else
18435 {
18436 if (attr_ub != NULL)
18437 complaint (_("Unresolved DW_AT_upper_bound "
18438 "- DIE at %s [in module %s]"),
18439 sect_offset_str (die->sect_off),
5e22e966 18440 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18441 if (attr_count != NULL)
18442 complaint (_("Unresolved DW_AT_count "
18443 "- DIE at %s [in module %s]"),
18444 sect_offset_str (die->sect_off),
5e22e966 18445 objfile_name (cu->per_objfile->objfile));
506f5c41 18446 }
e77813c8 18447 }
a02abb62 18448
4e962e74
TT
18449 LONGEST bias = 0;
18450 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18451 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18452 bias = bias_attr->constant_value (0);
4e962e74 18453
dbb9c2b1
JB
18454 /* Normally, the DWARF producers are expected to use a signed
18455 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18456 But this is unfortunately not always the case, as witnessed
18457 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18458 is used instead. To work around that ambiguity, we treat
18459 the bounds as signed, and thus sign-extend their values, when
18460 the base type is signed. */
6e70227d 18461 negative_mask =
d359392f 18462 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18463 if (low.kind () == PROP_CONST
c6d940a9 18464 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18465 low.set_const_val (low.const_val () | negative_mask);
18466 if (high.kind () == PROP_CONST
c6d940a9 18467 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18468 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18469
5bbd8269
AB
18470 /* Check for bit and byte strides. */
18471 struct dynamic_prop byte_stride_prop;
18472 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18473 if (attr_byte_stride != nullptr)
18474 {
293e7e51 18475 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18476 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18477 prop_type);
18478 }
18479
18480 struct dynamic_prop bit_stride_prop;
18481 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18482 if (attr_bit_stride != nullptr)
18483 {
18484 /* It only makes sense to have either a bit or byte stride. */
18485 if (attr_byte_stride != nullptr)
18486 {
18487 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18488 "- DIE at %s [in module %s]"),
18489 sect_offset_str (die->sect_off),
5e22e966 18490 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
18491 attr_bit_stride = nullptr;
18492 }
18493 else
18494 {
293e7e51 18495 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18496 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18497 prop_type);
18498 }
18499 }
18500
18501 if (attr_byte_stride != nullptr
18502 || attr_bit_stride != nullptr)
18503 {
18504 bool byte_stride_p = (attr_byte_stride != nullptr);
18505 struct dynamic_prop *stride
18506 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18507
18508 range_type
18509 = create_range_type_with_stride (NULL, orig_base_type, &low,
18510 &high, bias, stride, byte_stride_p);
18511 }
18512 else
18513 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18514
c451ebe5 18515 if (high_bound_is_count)
599088e3 18516 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 18517
c2ff108b
JK
18518 /* Ada expects an empty array on no boundary attributes. */
18519 if (attr == NULL && cu->language != language_ada)
8c2e4e06 18520 range_type->bounds ()->high.set_undefined ();
c2ff108b 18521
39cbfefa
DJ
18522 name = dwarf2_name (die, cu);
18523 if (name)
d0e39ea2 18524 range_type->set_name (name);
6e70227d 18525
e142c38c 18526 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18527 if (attr != nullptr)
529908cb 18528 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 18529
2b4424c3
TT
18530 maybe_set_alignment (cu, die, range_type);
18531
7e314c57
JK
18532 set_die_type (die, range_type, cu);
18533
18534 /* set_die_type should be already done. */
b4ba55a1
JB
18535 set_descriptive_type (range_type, die, cu);
18536
7e314c57 18537 return range_type;
a02abb62 18538}
6e70227d 18539
f792889a 18540static struct type *
81a17f79
JB
18541read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18542{
18543 struct type *type;
81a17f79 18544
5e22e966 18545 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 18546 type->set_name (dwarf2_name (die, cu));
81a17f79 18547
74a2f8ff 18548 /* In Ada, an unspecified type is typically used when the description
85102364 18549 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18550 such a type, we treat it as a stub, and try to resolve it later on,
18551 when needed. */
18552 if (cu->language == language_ada)
b4b73759 18553 type->set_is_stub (true);
74a2f8ff 18554
f792889a 18555 return set_die_type (die, type, cu);
81a17f79 18556}
a02abb62 18557
639d11d3
DC
18558/* Read a single die and all its descendents. Set the die's sibling
18559 field to NULL; set other fields in the die correctly, and set all
18560 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18561 location of the info_ptr after reading all of those dies. PARENT
18562 is the parent of the die in question. */
18563
18564static struct die_info *
dee91e82 18565read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18566 const gdb_byte *info_ptr,
18567 const gdb_byte **new_info_ptr,
dee91e82 18568 struct die_info *parent)
639d11d3
DC
18569{
18570 struct die_info *die;
d521ce57 18571 const gdb_byte *cur_ptr;
639d11d3 18572
3e225074 18573 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18574 if (die == NULL)
18575 {
18576 *new_info_ptr = cur_ptr;
18577 return NULL;
18578 }
93311388 18579 store_in_ref_table (die, reader->cu);
639d11d3 18580
3e225074 18581 if (die->has_children)
bf6af496 18582 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18583 else
18584 {
18585 die->child = NULL;
18586 *new_info_ptr = cur_ptr;
18587 }
18588
18589 die->sibling = NULL;
18590 die->parent = parent;
18591 return die;
18592}
18593
18594/* Read a die, all of its descendents, and all of its siblings; set
18595 all of the fields of all of the dies correctly. Arguments are as
18596 in read_die_and_children. */
18597
18598static struct die_info *
bf6af496 18599read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18600 const gdb_byte *info_ptr,
18601 const gdb_byte **new_info_ptr,
bf6af496 18602 struct die_info *parent)
639d11d3
DC
18603{
18604 struct die_info *first_die, *last_sibling;
d521ce57 18605 const gdb_byte *cur_ptr;
639d11d3 18606
c906108c 18607 cur_ptr = info_ptr;
639d11d3
DC
18608 first_die = last_sibling = NULL;
18609
18610 while (1)
c906108c 18611 {
639d11d3 18612 struct die_info *die
dee91e82 18613 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18614
1d325ec1 18615 if (die == NULL)
c906108c 18616 {
639d11d3
DC
18617 *new_info_ptr = cur_ptr;
18618 return first_die;
c906108c 18619 }
1d325ec1
DJ
18620
18621 if (!first_die)
18622 first_die = die;
c906108c 18623 else
1d325ec1
DJ
18624 last_sibling->sibling = die;
18625
18626 last_sibling = die;
c906108c 18627 }
c906108c
SS
18628}
18629
bf6af496
DE
18630/* Read a die, all of its descendents, and all of its siblings; set
18631 all of the fields of all of the dies correctly. Arguments are as
18632 in read_die_and_children.
18633 This the main entry point for reading a DIE and all its children. */
18634
18635static struct die_info *
18636read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18637 const gdb_byte *info_ptr,
18638 const gdb_byte **new_info_ptr,
bf6af496
DE
18639 struct die_info *parent)
18640{
18641 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18642 new_info_ptr, parent);
18643
b4f54984 18644 if (dwarf_die_debug)
bf6af496
DE
18645 {
18646 fprintf_unfiltered (gdb_stdlog,
18647 "Read die from %s@0x%x of %s:\n",
96b79293 18648 reader->die_section->get_name (),
bf6af496
DE
18649 (unsigned) (info_ptr - reader->die_section->buffer),
18650 bfd_get_filename (reader->abfd));
b4f54984 18651 dump_die (die, dwarf_die_debug);
bf6af496
DE
18652 }
18653
18654 return die;
18655}
18656
3019eac3
DE
18657/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18658 attributes.
18659 The caller is responsible for filling in the extra attributes
18660 and updating (*DIEP)->num_attrs.
18661 Set DIEP to point to a newly allocated die with its information,
3e225074 18662 except for its child, sibling, and parent fields. */
93311388 18663
d521ce57 18664static const gdb_byte *
3019eac3 18665read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18666 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18667 int num_extra_attrs)
93311388 18668{
b64f50a1 18669 unsigned int abbrev_number, bytes_read, i;
7c32eebb 18670 const struct abbrev_info *abbrev;
93311388
DE
18671 struct die_info *die;
18672 struct dwarf2_cu *cu = reader->cu;
18673 bfd *abfd = reader->abfd;
18674
9c541725 18675 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18676 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18677 info_ptr += bytes_read;
18678 if (!abbrev_number)
18679 {
18680 *diep = NULL;
93311388
DE
18681 return info_ptr;
18682 }
18683
685af9cd 18684 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18685 if (!abbrev)
348e048f
DE
18686 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18687 abbrev_number,
18688 bfd_get_filename (abfd));
18689
3019eac3 18690 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18691 die->sect_off = sect_off;
93311388
DE
18692 die->tag = abbrev->tag;
18693 die->abbrev = abbrev_number;
3e225074 18694 die->has_children = abbrev->has_children;
93311388 18695
3019eac3
DE
18696 /* Make the result usable.
18697 The caller needs to update num_attrs after adding the extra
18698 attributes. */
93311388
DE
18699 die->num_attrs = abbrev->num_attrs;
18700
7a5f294d 18701 bool any_need_reprocess = false;
93311388 18702 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 18703 {
7a5f294d
TT
18704 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18705 info_ptr);
18706 if (die->attrs[i].requires_reprocessing_p ())
18707 any_need_reprocess = true;
18a8505e
AT
18708 }
18709
052c8bb8 18710 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
18711 if (attr != nullptr && attr->form_is_unsigned ())
18712 cu->str_offsets_base = attr->as_unsigned ();
93311388 18713
41144253 18714 attr = die->attr (DW_AT_loclists_base);
18715 if (attr != nullptr)
529908cb 18716 cu->loclist_base = attr->as_unsigned ();
41144253 18717
a39fdb41 18718 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18719 if (maybe_addr_base.has_value ())
18720 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
18721
18722 attr = die->attr (DW_AT_rnglists_base);
18723 if (attr != nullptr)
2b0c7f41 18724 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 18725
7a5f294d
TT
18726 if (any_need_reprocess)
18727 {
18728 for (i = 0; i < abbrev->num_attrs; ++i)
18729 {
18730 if (die->attrs[i].requires_reprocessing_p ())
18731 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18732 }
18733 }
93311388 18734 *diep = die;
93311388
DE
18735 return info_ptr;
18736}
18737
3019eac3
DE
18738/* Read a die and all its attributes.
18739 Set DIEP to point to a newly allocated die with its information,
3e225074 18740 except for its child, sibling, and parent fields. */
3019eac3 18741
d521ce57 18742static const gdb_byte *
3019eac3 18743read_full_die (const struct die_reader_specs *reader,
3e225074 18744 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18745{
d521ce57 18746 const gdb_byte *result;
bf6af496 18747
3e225074 18748 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18749
b4f54984 18750 if (dwarf_die_debug)
bf6af496
DE
18751 {
18752 fprintf_unfiltered (gdb_stdlog,
18753 "Read die from %s@0x%x of %s:\n",
96b79293 18754 reader->die_section->get_name (),
bf6af496
DE
18755 (unsigned) (info_ptr - reader->die_section->buffer),
18756 bfd_get_filename (reader->abfd));
b4f54984 18757 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18758 }
18759
18760 return result;
3019eac3 18761}
433df2d4 18762\f
c906108c 18763
72bf9492
DJ
18764/* Returns nonzero if TAG represents a type that we might generate a partial
18765 symbol for. */
18766
18767static int
d8f62e84 18768is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
18769{
18770 switch (tag)
18771 {
18772#if 0
18773 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
18774 that we don't at present. Note that normally this does not
18775 matter, mainly because C compilers don't give names to these
18776 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
18777 case DW_TAG_file_type:
18778 case DW_TAG_ptr_to_member_type:
18779 case DW_TAG_set_type:
18780 case DW_TAG_string_type:
18781 case DW_TAG_subroutine_type:
18782#endif
d8f62e84
TT
18783
18784 /* GNAT may emit an array with a name, but no typedef, so we
18785 need to make a symbol in this case. */
18786 case DW_TAG_array_type:
18787 return lang == language_ada;
18788
72bf9492
DJ
18789 case DW_TAG_base_type:
18790 case DW_TAG_class_type:
680b30c7 18791 case DW_TAG_interface_type:
72bf9492
DJ
18792 case DW_TAG_enumeration_type:
18793 case DW_TAG_structure_type:
18794 case DW_TAG_subrange_type:
18795 case DW_TAG_typedef:
18796 case DW_TAG_union_type:
18797 return 1;
18798 default:
18799 return 0;
18800 }
18801}
18802
18803/* Load all DIEs that are interesting for partial symbols into memory. */
18804
18805static struct partial_die_info *
dee91e82 18806load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18807 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18808{
dee91e82 18809 struct dwarf2_cu *cu = reader->cu;
5e22e966 18810 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18811 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18812 unsigned int bytes_read;
5afb4e99 18813 unsigned int load_all = 0;
72bf9492
DJ
18814 int nesting_level = 1;
18815
18816 parent_die = NULL;
18817 last_die = NULL;
18818
7adf1e79
DE
18819 gdb_assert (cu->per_cu != NULL);
18820 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18821 load_all = 1;
18822
72bf9492
DJ
18823 cu->partial_dies
18824 = htab_create_alloc_ex (cu->header.length / 12,
18825 partial_die_hash,
18826 partial_die_eq,
18827 NULL,
18828 &cu->comp_unit_obstack,
18829 hashtab_obstack_allocate,
18830 dummy_obstack_deallocate);
18831
72bf9492
DJ
18832 while (1)
18833 {
7c32eebb
TT
18834 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18835 &bytes_read);
72bf9492
DJ
18836
18837 /* A NULL abbrev means the end of a series of children. */
18838 if (abbrev == NULL)
18839 {
18840 if (--nesting_level == 0)
cd9983dd
YQ
18841 return first_die;
18842
72bf9492
DJ
18843 info_ptr += bytes_read;
18844 last_die = parent_die;
18845 parent_die = parent_die->die_parent;
18846 continue;
18847 }
18848
98bfdba5
PA
18849 /* Check for template arguments. We never save these; if
18850 they're seen, we just mark the parent, and go on our way. */
18851 if (parent_die != NULL
18852 && cu->language == language_cplus
18853 && (abbrev->tag == DW_TAG_template_type_param
18854 || abbrev->tag == DW_TAG_template_value_param))
18855 {
18856 parent_die->has_template_arguments = 1;
18857
18858 if (!load_all)
18859 {
18860 /* We don't need a partial DIE for the template argument. */
dee91e82 18861 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18862 continue;
18863 }
18864 }
18865
0d99eb77 18866 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18867 Skip their other children. */
18868 if (!load_all
18869 && cu->language == language_cplus
18870 && parent_die != NULL
f9b5d5ea
TV
18871 && parent_die->tag == DW_TAG_subprogram
18872 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 18873 {
dee91e82 18874 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18875 continue;
18876 }
18877
5afb4e99
DJ
18878 /* Check whether this DIE is interesting enough to save. Normally
18879 we would not be interested in members here, but there may be
18880 later variables referencing them via DW_AT_specification (for
18881 static members). */
18882 if (!load_all
d8f62e84 18883 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 18884 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18885 && abbrev->tag != DW_TAG_enumerator
18886 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18887 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18888 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18889 && abbrev->tag != DW_TAG_variable
5afb4e99 18890 && abbrev->tag != DW_TAG_namespace
f55ee35c 18891 && abbrev->tag != DW_TAG_module
95554aad 18892 && abbrev->tag != DW_TAG_member
74921315
KS
18893 && abbrev->tag != DW_TAG_imported_unit
18894 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18895 {
18896 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18897 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18898 continue;
18899 }
18900
6f06d47b
YQ
18901 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18902 abbrev);
cd9983dd 18903
48fbe735 18904 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18905
18906 /* This two-pass algorithm for processing partial symbols has a
18907 high cost in cache pressure. Thus, handle some simple cases
18908 here which cover the majority of C partial symbols. DIEs
18909 which neither have specification tags in them, nor could have
18910 specification tags elsewhere pointing at them, can simply be
18911 processed and discarded.
18912
18913 This segment is also optional; scan_partial_symbols and
18914 add_partial_symbol will handle these DIEs if we chain
18915 them in normally. When compilers which do not emit large
18916 quantities of duplicate debug information are more common,
18917 this code can probably be removed. */
18918
18919 /* Any complete simple types at the top level (pretty much all
18920 of them, for a language without namespaces), can be processed
18921 directly. */
18922 if (parent_die == NULL
cd9983dd
YQ
18923 && pdi.has_specification == 0
18924 && pdi.is_declaration == 0
18925 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18926 || pdi.tag == DW_TAG_base_type
d8f62e84 18927 || pdi.tag == DW_TAG_array_type
cd9983dd 18928 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18929 {
7d00ffec 18930 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
18931 add_partial_symbol (&pdi, cu);
18932
cd9983dd 18933 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18934 continue;
18935 }
18936
d8228535
JK
18937 /* The exception for DW_TAG_typedef with has_children above is
18938 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18939 type_name_or_error will error on such types later.
d8228535
JK
18940
18941 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18942 it could not find the child DIEs referenced later, this is checked
18943 above. In correct DWARF DW_TAG_typedef should have no children. */
18944
cd9983dd 18945 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18946 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18947 "- DIE at %s [in module %s]"),
cd9983dd 18948 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18949
72bf9492
DJ
18950 /* If we're at the second level, and we're an enumerator, and
18951 our parent has no specification (meaning possibly lives in a
18952 namespace elsewhere), then we can add the partial symbol now
18953 instead of queueing it. */
cd9983dd 18954 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18955 && parent_die != NULL
18956 && parent_die->die_parent == NULL
18957 && parent_die->tag == DW_TAG_enumeration_type
18958 && parent_die->has_specification == 0)
18959 {
7d00ffec 18960 if (pdi.raw_name == NULL)
b98664d3 18961 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18962 else if (building_psymtab)
f0fbb768 18963 add_partial_symbol (&pdi, cu);
72bf9492 18964
cd9983dd 18965 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18966 continue;
18967 }
18968
cd9983dd 18969 struct partial_die_info *part_die
6f06d47b 18970 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18971
72bf9492
DJ
18972 /* We'll save this DIE so link it in. */
18973 part_die->die_parent = parent_die;
18974 part_die->die_sibling = NULL;
18975 part_die->die_child = NULL;
18976
18977 if (last_die && last_die == parent_die)
18978 last_die->die_child = part_die;
18979 else if (last_die)
18980 last_die->die_sibling = part_die;
18981
18982 last_die = part_die;
18983
18984 if (first_die == NULL)
18985 first_die = part_die;
18986
18987 /* Maybe add the DIE to the hash table. Not all DIEs that we
18988 find interesting need to be in the hash table, because we
18989 also have the parent/sibling/child chains; only those that we
18990 might refer to by offset later during partial symbol reading.
18991
18992 For now this means things that might have be the target of a
18993 DW_AT_specification, DW_AT_abstract_origin, or
18994 DW_AT_extension. DW_AT_extension will refer only to
18995 namespaces; DW_AT_abstract_origin refers to functions (and
18996 many things under the function DIE, but we do not recurse
18997 into function DIEs during partial symbol reading) and
18998 possibly variables as well; DW_AT_specification refers to
18999 declarations. Declarations ought to have the DW_AT_declaration
19000 flag. It happens that GCC forgets to put it in sometimes, but
19001 only for functions, not for types.
19002
19003 Adding more things than necessary to the hash table is harmless
19004 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19005 wasted time in find_partial_die, when we reread the compilation
19006 unit with load_all_dies set. */
72bf9492 19007
5afb4e99 19008 if (load_all
72929c62 19009 || abbrev->tag == DW_TAG_constant
5afb4e99 19010 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19011 || abbrev->tag == DW_TAG_variable
19012 || abbrev->tag == DW_TAG_namespace
19013 || part_die->is_declaration)
19014 {
19015 void **slot;
19016
19017 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19018 to_underlying (part_die->sect_off),
19019 INSERT);
72bf9492
DJ
19020 *slot = part_die;
19021 }
19022
72bf9492 19023 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19024 we have no reason to follow the children of structures; for other
98bfdba5
PA
19025 languages we have to, so that we can get at method physnames
19026 to infer fully qualified class names, for DW_AT_specification,
19027 and for C++ template arguments. For C++, we also look one level
19028 inside functions to find template arguments (if the name of the
19029 function does not already contain the template arguments).
bc30ff58 19030
0a4b0913
AB
19031 For Ada and Fortran, we need to scan the children of subprograms
19032 and lexical blocks as well because these languages allow the
19033 definition of nested entities that could be interesting for the
19034 debugger, such as nested subprograms for instance. */
72bf9492 19035 if (last_die->has_children
5afb4e99
DJ
19036 && (load_all
19037 || last_die->tag == DW_TAG_namespace
f55ee35c 19038 || last_die->tag == DW_TAG_module
72bf9492 19039 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19040 || (cu->language == language_cplus
19041 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19042 && (last_die->raw_name == NULL
19043 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19044 || (cu->language != language_c
19045 && (last_die->tag == DW_TAG_class_type
680b30c7 19046 || last_die->tag == DW_TAG_interface_type
72bf9492 19047 || last_die->tag == DW_TAG_structure_type
bc30ff58 19048 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19049 || ((cu->language == language_ada
19050 || cu->language == language_fortran)
bc30ff58
JB
19051 && (last_die->tag == DW_TAG_subprogram
19052 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19053 {
19054 nesting_level++;
19055 parent_die = last_die;
19056 continue;
19057 }
19058
19059 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19060 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19061
19062 /* Back to the top, do it again. */
19063 }
19064}
19065
6f06d47b 19066partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19067 const struct abbrev_info *abbrev)
6f06d47b
YQ
19068 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19069{
19070}
19071
7d00ffec
TT
19072/* See class definition. */
19073
19074const char *
19075partial_die_info::name (dwarf2_cu *cu)
19076{
19077 if (!canonical_name && raw_name != nullptr)
19078 {
19079 struct objfile *objfile = cu->per_objfile->objfile;
19080 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19081 canonical_name = 1;
19082 }
19083
19084 return raw_name;
19085}
19086
35cc7ed7
YQ
19087/* Read a minimal amount of information into the minimal die structure.
19088 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19089
48fbe735
YQ
19090const gdb_byte *
19091partial_die_info::read (const struct die_reader_specs *reader,
19092 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19093{
dee91e82 19094 struct dwarf2_cu *cu = reader->cu;
976ca316 19095 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19096 unsigned int i;
c5aa993b 19097 int has_low_pc_attr = 0;
c906108c 19098 int has_high_pc_attr = 0;
91da1414 19099 int high_pc_relative = 0;
c906108c 19100
fd0a254f 19101 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19102 {
e7da7f8f 19103 attribute attr;
7a5f294d 19104 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19105 /* String and address offsets that need to do the reprocessing have
dda83cd7 19106 already been read at this point, so there is no need to wait until
18a8505e 19107 the loop terminates to do the reprocessing. */
7a5f294d 19108 if (attr.requires_reprocessing_p ())
d0ce17d8 19109 read_attribute_reprocess (reader, &attr, tag);
c906108c 19110 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19111 partial symbol table. */
c906108c
SS
19112 switch (attr.name)
19113 {
19114 case DW_AT_name:
48fbe735 19115 switch (tag)
71c25dea
TT
19116 {
19117 case DW_TAG_compile_unit:
95554aad 19118 case DW_TAG_partial_unit:
348e048f 19119 case DW_TAG_type_unit:
71c25dea
TT
19120 /* Compilation units have a DW_AT_name that is a filename, not
19121 a source language identifier. */
19122 case DW_TAG_enumeration_type:
19123 case DW_TAG_enumerator:
19124 /* These tags always have simple identifiers already; no need
19125 to canonicalize them. */
7d00ffec 19126 canonical_name = 1;
2c830f54 19127 raw_name = attr.as_string ();
71c25dea
TT
19128 break;
19129 default:
7d00ffec 19130 canonical_name = 0;
2c830f54 19131 raw_name = attr.as_string ();
71c25dea
TT
19132 break;
19133 }
c906108c 19134 break;
31ef98ae 19135 case DW_AT_linkage_name:
c906108c 19136 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19137 /* Note that both forms of linkage name might appear. We
19138 assume they will be the same, and we only store the last
19139 one we see. */
95f982e5 19140 linkage_name = attr.as_string ();
c906108c
SS
19141 break;
19142 case DW_AT_low_pc:
19143 has_low_pc_attr = 1;
95f982e5 19144 lowpc = attr.as_address ();
c906108c
SS
19145 break;
19146 case DW_AT_high_pc:
19147 has_high_pc_attr = 1;
95f982e5 19148 highpc = attr.as_address ();
cd6c91b4 19149 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19150 high_pc_relative = 1;
c906108c
SS
19151 break;
19152 case DW_AT_location:
dda83cd7
SM
19153 /* Support the .debug_loc offsets. */
19154 if (attr.form_is_block ())
19155 {
9d2246fc 19156 d.locdesc = attr.as_block ();
dda83cd7
SM
19157 }
19158 else if (attr.form_is_section_offset ())
19159 {
4d3c2250 19160 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19161 }
19162 else
19163 {
4d3c2250
KB
19164 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19165 "partial symbol information");
dda83cd7 19166 }
c906108c 19167 break;
c906108c 19168 case DW_AT_external:
c45bc3f8 19169 is_external = attr.as_boolean ();
c906108c
SS
19170 break;
19171 case DW_AT_declaration:
c45bc3f8 19172 is_declaration = attr.as_boolean ();
c906108c
SS
19173 break;
19174 case DW_AT_type:
48fbe735 19175 has_type = 1;
c906108c
SS
19176 break;
19177 case DW_AT_abstract_origin:
19178 case DW_AT_specification:
72bf9492 19179 case DW_AT_extension:
48fbe735 19180 has_specification = 1;
0826b30a 19181 spec_offset = attr.get_ref_die_offset ();
48fbe735 19182 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19183 || cu->per_cu->is_dwz);
c906108c
SS
19184 break;
19185 case DW_AT_sibling:
19186 /* Ignore absolute siblings, they might point outside of
19187 the current compile unit. */
19188 if (attr.form == DW_FORM_ref_addr)
b98664d3 19189 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19190 else
b9502d3f 19191 {
48fbe735 19192 const gdb_byte *buffer = reader->buffer;
0826b30a 19193 sect_offset off = attr.get_ref_die_offset ();
9c541725 19194 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19195
19196 if (sibling_ptr < info_ptr)
b98664d3 19197 complaint (_("DW_AT_sibling points backwards"));
22869d73 19198 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19199 reader->die_section->overflow_complaint ();
b9502d3f 19200 else
48fbe735 19201 sibling = sibling_ptr;
b9502d3f 19202 }
c906108c 19203 break;
dda83cd7
SM
19204 case DW_AT_byte_size:
19205 has_byte_size = 1;
19206 break;
19207 case DW_AT_const_value:
19208 has_const_value = 1;
19209 break;
68511cec
CES
19210 case DW_AT_calling_convention:
19211 /* DWARF doesn't provide a way to identify a program's source-level
19212 entry point. DW_AT_calling_convention attributes are only meant
19213 to describe functions' calling conventions.
19214
19215 However, because it's a necessary piece of information in
0c1b455e
TT
19216 Fortran, and before DWARF 4 DW_CC_program was the only
19217 piece of debugging information whose definition refers to
19218 a 'main program' at all, several compilers marked Fortran
19219 main programs with DW_CC_program --- even when those
19220 functions use the standard calling conventions.
19221
19222 Although DWARF now specifies a way to provide this
19223 information, we support this practice for backward
19224 compatibility. */
529908cb 19225 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19226 && cu->language == language_fortran)
48fbe735 19227 main_subprogram = 1;
68511cec 19228 break;
481860b3 19229 case DW_AT_inline:
529908cb
TT
19230 {
19231 LONGEST value = attr.constant_value (-1);
19232 if (value == DW_INL_inlined
19233 || value == DW_INL_declared_inlined)
19234 may_be_inlined = 1;
19235 }
481860b3 19236 break;
95554aad
TT
19237
19238 case DW_AT_import:
48fbe735 19239 if (tag == DW_TAG_imported_unit)
36586728 19240 {
0826b30a 19241 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19242 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19243 || cu->per_cu->is_dwz);
19244 }
95554aad
TT
19245 break;
19246
0c1b455e 19247 case DW_AT_main_subprogram:
c45bc3f8 19248 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19249 break;
19250
05caa1d2
TT
19251 case DW_AT_ranges:
19252 {
2b0c7f41
SM
19253 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19254 on DWARF version). */
19255 ULONGEST ranges_offset = attr.as_unsigned ();
19256
19257 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19258 this value. */
19259 if (tag != DW_TAG_compile_unit)
19260 ranges_offset += cu->gnu_ranges_base;
19261
05caa1d2 19262 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19263 nullptr, tag))
05caa1d2
TT
19264 has_pc_info = 1;
19265 }
19266 break;
19267
c906108c
SS
19268 default:
19269 break;
19270 }
19271 }
19272
10d06d82
TT
19273 /* For Ada, if both the name and the linkage name appear, we prefer
19274 the latter. This lets "catch exception" work better, regardless
19275 of the order in which the name and linkage name were emitted.
19276 Really, though, this is just a workaround for the fact that gdb
19277 doesn't store both the name and the linkage name. */
19278 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19279 raw_name = linkage_name;
10d06d82 19280
91da1414 19281 if (high_pc_relative)
48fbe735 19282 highpc += lowpc;
91da1414 19283
9373cf26
JK
19284 if (has_low_pc_attr && has_high_pc_attr)
19285 {
19286 /* When using the GNU linker, .gnu.linkonce. sections are used to
19287 eliminate duplicate copies of functions and vtables and such.
19288 The linker will arbitrarily choose one and discard the others.
19289 The AT_*_pc values for such functions refer to local labels in
19290 these sections. If the section from that file was discarded, the
19291 labels are not in the output, so the relocs get a value of 0.
19292 If this is a discarded function, mark the pc bounds as invalid,
19293 so that GDB will ignore it. */
976ca316 19294 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19295 {
976ca316 19296 struct objfile *objfile = per_objfile->objfile;
08feed99 19297 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19298
b98664d3 19299 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19300 "for DIE at %s [in module %s]"),
48fbe735
YQ
19301 paddress (gdbarch, lowpc),
19302 sect_offset_str (sect_off),
9d8780f0 19303 objfile_name (objfile));
9373cf26
JK
19304 }
19305 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19306 else if (lowpc >= highpc)
9373cf26 19307 {
976ca316 19308 struct objfile *objfile = per_objfile->objfile;
08feed99 19309 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19310
b98664d3 19311 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19312 "for DIE at %s [in module %s]"),
48fbe735
YQ
19313 paddress (gdbarch, lowpc),
19314 paddress (gdbarch, highpc),
19315 sect_offset_str (sect_off),
9c541725 19316 objfile_name (objfile));
9373cf26
JK
19317 }
19318 else
48fbe735 19319 has_pc_info = 1;
9373cf26 19320 }
85cbf3d3 19321
c906108c
SS
19322 return info_ptr;
19323}
19324
72bf9492
DJ
19325/* Find a cached partial DIE at OFFSET in CU. */
19326
d590ff25
YQ
19327struct partial_die_info *
19328dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19329{
19330 struct partial_die_info *lookup_die = NULL;
6f06d47b 19331 struct partial_die_info part_die (sect_off);
72bf9492 19332
9a3c8263 19333 lookup_die = ((struct partial_die_info *)
d590ff25 19334 htab_find_with_hash (partial_dies, &part_die,
9c541725 19335 to_underlying (sect_off)));
72bf9492 19336
72bf9492
DJ
19337 return lookup_die;
19338}
19339
348e048f
DE
19340/* Find a partial DIE at OFFSET, which may or may not be in CU,
19341 except in the case of .debug_types DIEs which do not reference
19342 outside their CU (they do however referencing other types via
55f1336d 19343 DW_FORM_ref_sig8). */
72bf9492 19344
122cf0f2 19345static const struct cu_partial_die_info
9c541725 19346find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19347{
976ca316
SM
19348 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19349 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19350 struct partial_die_info *pd = NULL;
72bf9492 19351
36586728 19352 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19353 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19354 {
d590ff25 19355 pd = cu->find_partial_die (sect_off);
5afb4e99 19356 if (pd != NULL)
fb816e8b 19357 return { cu, pd };
0d99eb77
DE
19358 /* We missed recording what we needed.
19359 Load all dies and try again. */
5afb4e99 19360 }
0d99eb77
DE
19361 else
19362 {
19363 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19364 if (cu->per_cu->is_debug_types)
0d99eb77 19365 {
9d8780f0
SM
19366 error (_("Dwarf Error: Type Unit at offset %s contains"
19367 " external reference to offset %s [in module %s].\n"),
19368 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19369 bfd_get_filename (objfile->obfd));
19370 }
7188ed02
SM
19371 dwarf2_per_cu_data *per_cu
19372 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19373 per_objfile);
72bf9492 19374
976ca316 19375 cu = per_objfile->get_cu (per_cu);
7188ed02 19376 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19377 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19378
976ca316 19379 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19380
19381 cu->last_used = 0;
19382 pd = cu->find_partial_die (sect_off);
0d99eb77 19383 }
5afb4e99 19384
dee91e82
DE
19385 /* If we didn't find it, and not all dies have been loaded,
19386 load them all and try again. */
19387
7188ed02 19388 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 19389 {
7188ed02 19390 cu->per_cu->load_all_dies = 1;
fd820528
DE
19391
19392 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19393 THIS_CU->cu may already be in use. So we can't just free it and
19394 replace its DIEs with the ones we read in. Instead, we leave those
19395 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19396 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19397 set. */
976ca316 19398 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19399
7188ed02 19400 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19401 }
19402
19403 if (pd == NULL)
521894aa 19404 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 19405 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19406 return { cu, pd };
72bf9492
DJ
19407}
19408
abc72ce4
DE
19409/* See if we can figure out if the class lives in a namespace. We do
19410 this by looking for a member function; its demangled name will
19411 contain namespace info, if there is any. */
19412
19413static void
19414guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19415 struct dwarf2_cu *cu)
19416{
19417 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19418 what template types look like, because the demangler
19419 frequently doesn't give the same name as the debug info. We
19420 could fix this by only using the demangled name to get the
19421 prefix (but see comment in read_structure_type). */
19422
19423 struct partial_die_info *real_pdi;
19424 struct partial_die_info *child_pdi;
19425
19426 /* If this DIE (this DIE's specification, if any) has a parent, then
19427 we should not do this. We'll prepend the parent's fully qualified
19428 name when we create the partial symbol. */
19429
19430 real_pdi = struct_pdi;
19431 while (real_pdi->has_specification)
fb816e8b 19432 {
122cf0f2
AB
19433 auto res = find_partial_die (real_pdi->spec_offset,
19434 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19435 real_pdi = res.pdi;
19436 cu = res.cu;
19437 }
abc72ce4
DE
19438
19439 if (real_pdi->die_parent != NULL)
19440 return;
19441
19442 for (child_pdi = struct_pdi->die_child;
19443 child_pdi != NULL;
19444 child_pdi = child_pdi->die_sibling)
19445 {
19446 if (child_pdi->tag == DW_TAG_subprogram
19447 && child_pdi->linkage_name != NULL)
19448 {
43816ebc 19449 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19450 (cu->language_defn->class_name_from_physname
19451 (child_pdi->linkage_name));
abc72ce4
DE
19452 if (actual_class_name != NULL)
19453 {
5e22e966 19454 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19455 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19456 struct_pdi->canonical_name = 1;
abc72ce4
DE
19457 }
19458 break;
19459 }
19460 }
19461}
19462
25c11aca
TV
19463/* Return true if a DIE with TAG may have the DW_AT_const_value
19464 attribute. */
19465
19466static bool
19467can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19468{
19469 switch (tag)
19470 {
19471 case DW_TAG_constant:
19472 case DW_TAG_enumerator:
19473 case DW_TAG_formal_parameter:
19474 case DW_TAG_template_value_param:
19475 case DW_TAG_variable:
19476 return true;
19477 }
19478
19479 return false;
19480}
19481
52356b79
YQ
19482void
19483partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19484{
abc72ce4
DE
19485 /* Once we've fixed up a die, there's no point in doing so again.
19486 This also avoids a memory leak if we were to call
19487 guess_partial_die_structure_name multiple times. */
52356b79 19488 if (fixup_called)
abc72ce4
DE
19489 return;
19490
72bf9492
DJ
19491 /* If we found a reference attribute and the DIE has no name, try
19492 to find a name in the referred to DIE. */
19493
7d00ffec 19494 if (raw_name == NULL && has_specification)
72bf9492
DJ
19495 {
19496 struct partial_die_info *spec_die;
72bf9492 19497
122cf0f2 19498 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19499 spec_die = res.pdi;
19500 cu = res.cu;
72bf9492 19501
52356b79 19502 spec_die->fixup (cu);
72bf9492 19503
7d00ffec 19504 if (spec_die->raw_name)
72bf9492 19505 {
7d00ffec
TT
19506 raw_name = spec_die->raw_name;
19507 canonical_name = spec_die->canonical_name;
72bf9492
DJ
19508
19509 /* Copy DW_AT_external attribute if it is set. */
19510 if (spec_die->is_external)
52356b79 19511 is_external = spec_die->is_external;
72bf9492
DJ
19512 }
19513 }
19514
25c11aca
TV
19515 if (!has_const_value && has_specification
19516 && can_have_DW_AT_const_value_p (tag))
19517 {
19518 struct partial_die_info *spec_die;
19519
19520 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19521 spec_die = res.pdi;
19522 cu = res.cu;
19523
19524 spec_die->fixup (cu);
19525
19526 if (spec_die->has_const_value)
19527 {
19528 /* Copy DW_AT_const_value attribute if it is set. */
19529 has_const_value = spec_die->has_const_value;
19530 }
19531 }
19532
72bf9492 19533 /* Set default names for some unnamed DIEs. */
72bf9492 19534
7d00ffec
TT
19535 if (raw_name == NULL && tag == DW_TAG_namespace)
19536 {
19537 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19538 canonical_name = 1;
19539 }
72bf9492 19540
abc72ce4
DE
19541 /* If there is no parent die to provide a namespace, and there are
19542 children, see if we can determine the namespace from their linkage
122d1940 19543 name. */
abc72ce4 19544 if (cu->language == language_cplus
5e22e966 19545 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
19546 && die_parent == NULL
19547 && has_children
19548 && (tag == DW_TAG_class_type
19549 || tag == DW_TAG_structure_type
19550 || tag == DW_TAG_union_type))
19551 guess_partial_die_structure_name (this, cu);
abc72ce4 19552
53832f31
TT
19553 /* GCC might emit a nameless struct or union that has a linkage
19554 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 19555 if (raw_name == NULL
52356b79
YQ
19556 && (tag == DW_TAG_class_type
19557 || tag == DW_TAG_interface_type
19558 || tag == DW_TAG_structure_type
19559 || tag == DW_TAG_union_type)
19560 && linkage_name != NULL)
53832f31 19561 {
43816ebc
TT
19562 gdb::unique_xmalloc_ptr<char> demangled
19563 (gdb_demangle (linkage_name, DMGL_TYPES));
19564 if (demangled != nullptr)
53832f31 19565 {
96408a79
SA
19566 const char *base;
19567
19568 /* Strip any leading namespaces/classes, keep only the base name.
19569 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19570 base = strrchr (demangled.get (), ':');
19571 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19572 base++;
19573 else
43816ebc 19574 base = demangled.get ();
96408a79 19575
5e22e966 19576 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19577 raw_name = objfile->intern (base);
19578 canonical_name = 1;
53832f31
TT
19579 }
19580 }
19581
52356b79 19582 fixup_called = 1;
72bf9492
DJ
19583}
19584
d0ce17d8 19585/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
19586 contents from the given SECTION in the HEADER.
19587
19588 HEADER_OFFSET is the offset of the header in the section. */
41144253 19589static void
d0ce17d8 19590read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
19591 struct dwarf2_section_info *section,
19592 sect_offset header_offset)
41144253 19593{
19594 unsigned int bytes_read;
19595 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
19596 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19597
41144253 19598 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19599 info_ptr += bytes_read;
a1c40103 19600
41144253 19601 header->version = read_2_bytes (abfd, info_ptr);
19602 info_ptr += 2;
a1c40103 19603
41144253 19604 header->addr_size = read_1_byte (abfd, info_ptr);
19605 info_ptr += 1;
a1c40103 19606
41144253 19607 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19608 info_ptr += 1;
a1c40103 19609
41144253 19610 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19611}
19612
19613/* Return the DW_AT_loclists_base value for the CU. */
19614static ULONGEST
19615lookup_loclist_base (struct dwarf2_cu *cu)
19616{
19617 /* For the .dwo unit, the loclist_base points to the first offset following
19618 the header. The header consists of the following entities-
19619 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19620 bit format)
19621 2. version (2 bytes)
19622 3. address size (1 byte)
19623 4. segment selector size (1 byte)
19624 5. offset entry count (4 bytes)
19625 These sizes are derived as per the DWARFv5 standard. */
19626 if (cu->dwo_unit != nullptr)
19627 {
19628 if (cu->header.initial_length_size == 4)
19629 return LOCLIST_HEADER_SIZE32;
19630 return LOCLIST_HEADER_SIZE64;
19631 }
19632 return cu->loclist_base;
19633}
19634
19635/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19636 array of offsets in the .debug_loclists section. */
e57933dc
SM
19637
19638static sect_offset
41144253 19639read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19640{
976ca316
SM
19641 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19642 struct objfile *objfile = per_objfile->objfile;
41144253 19643 bfd *abfd = objfile->obfd;
a1c40103
SM
19644 ULONGEST loclist_header_size =
19645 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19646 : LOCLIST_HEADER_SIZE64);
41144253 19647 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
19648
19649 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19650 ULONGEST start_offset =
19651 loclist_base + loclist_index * cu->header.offset_size;
19652
a1c40103 19653 /* Get loclists section. */
41144253 19654 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19655
a1c40103 19656 /* Read the loclists section content. */
41144253 19657 section->read (objfile);
19658 if (section->buffer == NULL)
a0c1eeba
SM
19659 error (_("DW_FORM_loclistx used without .debug_loclists "
19660 "section [in module %s]"), objfile_name (objfile));
19661
a1c40103
SM
19662 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19663 so if loclist_base is smaller than the header size, we have a problem. */
19664 if (loclist_base < loclist_header_size)
19665 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19666 objfile_name (objfile));
19667
19668 /* Read the header of the loclists contribution. */
d0ce17d8 19669 struct loclists_rnglists_header header;
a1c40103
SM
19670 read_loclists_rnglists_header (&header, section,
19671 (sect_offset) (loclist_base - loclist_header_size));
19672
19673 /* Verify the loclist index is valid. */
41144253 19674 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
19675 error (_("DW_FORM_loclistx pointing outside of "
19676 ".debug_loclists offset array [in module %s]"),
19677 objfile_name (objfile));
19678
05787bad
SM
19679 /* Validate that reading won't go beyond the end of the section. */
19680 if (start_offset + cu->header.offset_size > section->size)
19681 error (_("Reading DW_FORM_loclistx index beyond end of"
19682 ".debug_loclists section [in module %s]"),
19683 objfile_name (objfile));
19684
19685 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 19686
19687 if (cu->header.offset_size == 4)
e57933dc 19688 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 19689 else
e57933dc 19690 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 19691}
19692
d0ce17d8
CT
19693/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19694 array of offsets in the .debug_rnglists section. */
e57933dc
SM
19695
19696static sect_offset
d0ce17d8
CT
19697read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19698 dwarf_tag tag)
19699{
19700 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19701 struct objfile *objfile = dwarf2_per_objfile->objfile;
19702 bfd *abfd = objfile->obfd;
19703 ULONGEST rnglist_header_size =
19704 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19705 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
19706
19707 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19708 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19709 doesn't apply. */
d0ce17d8 19710 ULONGEST rnglist_base =
2b0c7f41 19711 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
19712
19713 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
19714 ULONGEST start_offset =
19715 rnglist_base + rnglist_index * cu->header.offset_size;
19716
19717 /* Get rnglists section. */
19718 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19719
19720 /* Read the rnglists section content. */
19721 section->read (objfile);
19722 if (section->buffer == nullptr)
19723 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19724 "[in module %s]"),
19725 objfile_name (objfile));
19726
a1c40103
SM
19727 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19728 so if rnglist_base is smaller than the header size, we have a problem. */
19729 if (rnglist_base < rnglist_header_size)
19730 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19731 objfile_name (objfile));
19732
19733 /* Read the header of the rnglists contribution. */
d0ce17d8 19734 struct loclists_rnglists_header header;
a1c40103
SM
19735 read_loclists_rnglists_header (&header, section,
19736 (sect_offset) (rnglist_base - rnglist_header_size));
19737
19738 /* Verify the rnglist index is valid. */
d0ce17d8
CT
19739 if (rnglist_index >= header.offset_entry_count)
19740 error (_("DW_FORM_rnglistx index pointing outside of "
19741 ".debug_rnglists offset array [in module %s]"),
19742 objfile_name (objfile));
19743
d0ce17d8 19744 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 19745 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
19746 error (_("Reading DW_FORM_rnglistx index beyond end of"
19747 ".debug_rnglists section [in module %s]"),
19748 objfile_name (objfile));
19749
19750 const gdb_byte *info_ptr = section->buffer + start_offset;
19751
19752 if (cu->header.offset_size == 4)
e57933dc 19753 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 19754 else
e57933dc 19755 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
19756}
19757
18a8505e
AT
19758/* Process the attributes that had to be skipped in the first round. These
19759 attributes are the ones that need str_offsets_base or addr_base attributes.
19760 They could not have been processed in the first round, because at the time
19761 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19762static void
19763read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 19764 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
19765{
19766 struct dwarf2_cu *cu = reader->cu;
19767 switch (attr->form)
19768 {
19769 case DW_FORM_addrx:
19770 case DW_FORM_GNU_addr_index:
36d378cf
TT
19771 attr->set_address (read_addr_index (cu,
19772 attr->as_unsigned_reprocess ()));
dda83cd7 19773 break;
41144253 19774 case DW_FORM_loclistx:
b1829e1b 19775 {
e57933dc 19776 sect_offset loclists_sect_off
b1829e1b
SM
19777 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19778
e57933dc 19779 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
19780 }
19781 break;
d0ce17d8 19782 case DW_FORM_rnglistx:
b1829e1b 19783 {
e57933dc 19784 sect_offset rnglists_sect_off
b1829e1b
SM
19785 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19786
e57933dc 19787 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 19788 }
dda83cd7 19789 break;
18a8505e
AT
19790 case DW_FORM_strx:
19791 case DW_FORM_strx1:
19792 case DW_FORM_strx2:
19793 case DW_FORM_strx3:
19794 case DW_FORM_strx4:
19795 case DW_FORM_GNU_str_index:
19796 {
fe56917a 19797 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 19798 gdb_assert (!attr->canonical_string_p ());
18a8505e 19799 if (reader->dwo_file != NULL)
c6481205
TT
19800 attr->set_string_noncanonical (read_dwo_str_index (reader,
19801 str_index));
18a8505e 19802 else
c6481205
TT
19803 attr->set_string_noncanonical (read_stub_str_index (cu,
19804 str_index));
18a8505e
AT
19805 break;
19806 }
19807 default:
19808 gdb_assert_not_reached (_("Unexpected DWARF form."));
19809 }
19810}
19811
a8329558 19812/* Read an attribute value described by an attribute form. */
c906108c 19813
d521ce57 19814static const gdb_byte *
dee91e82
DE
19815read_attribute_value (const struct die_reader_specs *reader,
19816 struct attribute *attr, unsigned form,
7a5f294d 19817 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19818{
dee91e82 19819 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
19820 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19821 struct objfile *objfile = per_objfile->objfile;
dee91e82 19822 bfd *abfd = reader->abfd;
e7c27a73 19823 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19824 unsigned int bytes_read;
19825 struct dwarf_block *blk;
19826
aead7601 19827 attr->form = (enum dwarf_form) form;
a8329558 19828 switch (form)
c906108c 19829 {
c906108c 19830 case DW_FORM_ref_addr:
7c290a04
TT
19831 if (cu_header->version == 2)
19832 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 19833 &bytes_read));
ae411497 19834 else
7c290a04 19835 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19836 &bytes_read));
ae411497
TT
19837 info_ptr += bytes_read;
19838 break;
36586728 19839 case DW_FORM_GNU_ref_alt:
7c290a04 19840 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19841 &bytes_read));
36586728
TT
19842 info_ptr += bytes_read;
19843 break;
ae411497 19844 case DW_FORM_addr:
08feed99
TT
19845 {
19846 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 19847 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
19848 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19849 attr->set_address (addr);
08feed99
TT
19850 info_ptr += bytes_read;
19851 }
c906108c
SS
19852 break;
19853 case DW_FORM_block2:
7b5a2f43 19854 blk = dwarf_alloc_block (cu);
c906108c
SS
19855 blk->size = read_2_bytes (abfd, info_ptr);
19856 info_ptr += 2;
19857 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19858 info_ptr += blk->size;
9d2246fc 19859 attr->set_block (blk);
c906108c
SS
19860 break;
19861 case DW_FORM_block4:
7b5a2f43 19862 blk = dwarf_alloc_block (cu);
c906108c
SS
19863 blk->size = read_4_bytes (abfd, info_ptr);
19864 info_ptr += 4;
19865 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19866 info_ptr += blk->size;
9d2246fc 19867 attr->set_block (blk);
c906108c
SS
19868 break;
19869 case DW_FORM_data2:
414ad644 19870 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
19871 info_ptr += 2;
19872 break;
19873 case DW_FORM_data4:
414ad644 19874 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
19875 info_ptr += 4;
19876 break;
19877 case DW_FORM_data8:
414ad644 19878 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
19879 info_ptr += 8;
19880 break;
0224619f
JK
19881 case DW_FORM_data16:
19882 blk = dwarf_alloc_block (cu);
19883 blk->size = 16;
19884 blk->data = read_n_bytes (abfd, info_ptr, 16);
19885 info_ptr += 16;
9d2246fc 19886 attr->set_block (blk);
0224619f 19887 break;
2dc7f7b3 19888 case DW_FORM_sec_offset:
7c290a04 19889 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19890 &bytes_read));
2dc7f7b3
TT
19891 info_ptr += bytes_read;
19892 break;
41144253 19893 case DW_FORM_loclistx:
19894 {
7a5f294d
TT
19895 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19896 &bytes_read));
19897 info_ptr += bytes_read;
41144253 19898 }
19899 break;
c906108c 19900 case DW_FORM_string:
c6481205
TT
19901 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19902 &bytes_read));
c906108c
SS
19903 info_ptr += bytes_read;
19904 break;
4bdf3d34 19905 case DW_FORM_strp:
36586728
TT
19906 if (!cu->per_cu->is_dwz)
19907 {
c6481205
TT
19908 attr->set_string_noncanonical
19909 (read_indirect_string (per_objfile,
19910 abfd, info_ptr, cu_header,
19911 &bytes_read));
36586728
TT
19912 info_ptr += bytes_read;
19913 break;
19914 }
19915 /* FALLTHROUGH */
43988095
JK
19916 case DW_FORM_line_strp:
19917 if (!cu->per_cu->is_dwz)
19918 {
c6481205
TT
19919 attr->set_string_noncanonical
19920 (per_objfile->read_line_string (info_ptr, cu_header,
19921 &bytes_read));
43988095
JK
19922 info_ptr += bytes_read;
19923 break;
19924 }
19925 /* FALLTHROUGH */
36586728
TT
19926 case DW_FORM_GNU_strp_alt:
19927 {
a7308ce0 19928 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
19929 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19930 &bytes_read);
36586728 19931
c6481205
TT
19932 attr->set_string_noncanonical
19933 (dwz->read_string (objfile, str_offset));
36586728
TT
19934 info_ptr += bytes_read;
19935 }
4bdf3d34 19936 break;
2dc7f7b3 19937 case DW_FORM_exprloc:
c906108c 19938 case DW_FORM_block:
7b5a2f43 19939 blk = dwarf_alloc_block (cu);
c906108c
SS
19940 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19941 info_ptr += bytes_read;
19942 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19943 info_ptr += blk->size;
9d2246fc 19944 attr->set_block (blk);
c906108c
SS
19945 break;
19946 case DW_FORM_block1:
7b5a2f43 19947 blk = dwarf_alloc_block (cu);
c906108c
SS
19948 blk->size = read_1_byte (abfd, info_ptr);
19949 info_ptr += 1;
19950 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19951 info_ptr += blk->size;
9d2246fc 19952 attr->set_block (blk);
c906108c
SS
19953 break;
19954 case DW_FORM_data1:
c906108c 19955 case DW_FORM_flag:
414ad644 19956 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
19957 info_ptr += 1;
19958 break;
2dc7f7b3 19959 case DW_FORM_flag_present:
414ad644 19960 attr->set_unsigned (1);
2dc7f7b3 19961 break;
c906108c 19962 case DW_FORM_sdata:
1bc397c5 19963 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19964 info_ptr += bytes_read;
19965 break;
18a8505e 19966 case DW_FORM_rnglistx:
7a5f294d
TT
19967 {
19968 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19969 &bytes_read));
19970 info_ptr += bytes_read;
19971 }
19972 break;
d0ce17d8 19973 case DW_FORM_udata:
414ad644 19974 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19975 info_ptr += bytes_read;
19976 break;
19977 case DW_FORM_ref1:
7c290a04 19978 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 19979 + read_1_byte (abfd, info_ptr)));
c906108c
SS
19980 info_ptr += 1;
19981 break;
19982 case DW_FORM_ref2:
7c290a04 19983 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 19984 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
19985 info_ptr += 2;
19986 break;
19987 case DW_FORM_ref4:
7c290a04 19988 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 19989 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
19990 info_ptr += 4;
19991 break;
613e1657 19992 case DW_FORM_ref8:
7c290a04 19993 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 19994 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
19995 info_ptr += 8;
19996 break;
55f1336d 19997 case DW_FORM_ref_sig8:
630ed6b9 19998 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
19999 info_ptr += 8;
20000 break;
c906108c 20001 case DW_FORM_ref_udata:
7c290a04 20002 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
20003 + read_unsigned_leb128 (abfd, info_ptr,
20004 &bytes_read)));
c906108c
SS
20005 info_ptr += bytes_read;
20006 break;
c906108c 20007 case DW_FORM_indirect:
a8329558
KW
20008 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20009 info_ptr += bytes_read;
43988095
JK
20010 if (form == DW_FORM_implicit_const)
20011 {
20012 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20013 info_ptr += bytes_read;
20014 }
20015 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20016 info_ptr);
43988095
JK
20017 break;
20018 case DW_FORM_implicit_const:
1bc397c5 20019 attr->set_signed (implicit_const);
a8329558 20020 break;
336d760d 20021 case DW_FORM_addrx:
3019eac3 20022 case DW_FORM_GNU_addr_index:
fe56917a
TT
20023 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20024 &bytes_read));
3019eac3
DE
20025 info_ptr += bytes_read;
20026 break;
cf532bd1 20027 case DW_FORM_strx:
15f18d14
AT
20028 case DW_FORM_strx1:
20029 case DW_FORM_strx2:
20030 case DW_FORM_strx3:
20031 case DW_FORM_strx4:
3019eac3 20032 case DW_FORM_GNU_str_index:
3019eac3 20033 {
15f18d14
AT
20034 ULONGEST str_index;
20035 if (form == DW_FORM_strx1)
20036 {
20037 str_index = read_1_byte (abfd, info_ptr);
20038 info_ptr += 1;
20039 }
20040 else if (form == DW_FORM_strx2)
20041 {
20042 str_index = read_2_bytes (abfd, info_ptr);
20043 info_ptr += 2;
20044 }
20045 else if (form == DW_FORM_strx3)
20046 {
20047 str_index = read_3_bytes (abfd, info_ptr);
20048 info_ptr += 3;
20049 }
20050 else if (form == DW_FORM_strx4)
20051 {
20052 str_index = read_4_bytes (abfd, info_ptr);
20053 info_ptr += 4;
20054 }
20055 else
20056 {
20057 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20058 info_ptr += bytes_read;
20059 }
fe56917a 20060 attr->set_unsigned_reprocess (str_index);
7a5f294d 20061 }
3019eac3 20062 break;
c906108c 20063 default:
8a3fe4f8 20064 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20065 dwarf_form_name (form),
20066 bfd_get_filename (abfd));
c906108c 20067 }
28e94949 20068
36586728 20069 /* Super hack. */
cd6c91b4 20070 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20071 attr->form = DW_FORM_GNU_ref_alt;
20072
28e94949
JB
20073 /* We have seen instances where the compiler tried to emit a byte
20074 size attribute of -1 which ended up being encoded as an unsigned
20075 0xffffffff. Although 0xffffffff is technically a valid size value,
20076 an object of this size seems pretty unlikely so we can relatively
20077 safely treat these cases as if the size attribute was invalid and
20078 treat them as zero by default. */
20079 if (attr->name == DW_AT_byte_size
20080 && form == DW_FORM_data4
529908cb 20081 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20082 {
20083 complaint
dda83cd7
SM
20084 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20085 hex_string (attr->as_unsigned ()));
414ad644 20086 attr->set_unsigned (0);
01c66ae6 20087 }
28e94949 20088
c906108c
SS
20089 return info_ptr;
20090}
20091
a8329558
KW
20092/* Read an attribute described by an abbreviated attribute. */
20093
d521ce57 20094static const gdb_byte *
dee91e82 20095read_attribute (const struct die_reader_specs *reader,
4444f407 20096 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20097 const gdb_byte *info_ptr)
a8329558
KW
20098{
20099 attr->name = abbrev->name;
c6481205 20100 attr->string_is_canonical = 0;
fe56917a 20101 attr->requires_reprocessing = 0;
43988095 20102 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20103 abbrev->implicit_const, info_ptr);
a8329558
KW
20104}
20105
43988095
JK
20106/* Return pointer to string at .debug_str offset STR_OFFSET. */
20107
20108static const char *
976ca316 20109read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20110 LONGEST str_offset)
43988095 20111{
976ca316
SM
20112 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20113 str_offset, "DW_FORM_strp");
c906108c
SS
20114}
20115
43988095
JK
20116/* Return pointer to string at .debug_str offset as read from BUF.
20117 BUF is assumed to be in a compilation unit described by CU_HEADER.
20118 Return *BYTES_READ_PTR count of bytes read from BUF. */
20119
d521ce57 20120static const char *
976ca316 20121read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20122 const gdb_byte *buf,
cf2c3c16
TT
20123 const struct comp_unit_head *cu_header,
20124 unsigned int *bytes_read_ptr)
20125{
8266302d 20126 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20127
976ca316 20128 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20129}
20130
86c0bb4c 20131/* See read.h. */
43988095 20132
86c0bb4c
TT
20133const char *
20134dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20135 const struct comp_unit_head *cu_header,
20136 unsigned int *bytes_read_ptr)
43988095 20137{
86c0bb4c 20138 bfd *abfd = objfile->obfd;
8266302d 20139 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20140
5989a64e 20141 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20142}
20143
3019eac3 20144/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20145 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20146 ADDR_SIZE is the size of addresses from the CU header. */
20147
20148static CORE_ADDR
976ca316
SM
20149read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20150 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20151{
976ca316 20152 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20153 bfd *abfd = objfile->obfd;
20154 const gdb_byte *info_ptr;
18a8505e 20155 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20156
976ca316
SM
20157 per_objfile->per_bfd->addr.read (objfile);
20158 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20159 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20160 objfile_name (objfile));
18a8505e 20161 if (addr_base_or_zero + addr_index * addr_size
976ca316 20162 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20163 error (_("DW_FORM_addr_index pointing outside of "
20164 ".debug_addr section [in module %s]"),
4262abfb 20165 objfile_name (objfile));
976ca316
SM
20166 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20167 + addr_index * addr_size);
3019eac3
DE
20168 if (addr_size == 4)
20169 return bfd_get_32 (abfd, info_ptr);
20170 else
20171 return bfd_get_64 (abfd, info_ptr);
20172}
20173
20174/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20175
20176static CORE_ADDR
20177read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20178{
5e22e966 20179 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20180 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20181}
20182
20183/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20184
20185static CORE_ADDR
d521ce57 20186read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20187 unsigned int *bytes_read)
20188{
5e22e966 20189 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20190 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20191
20192 return read_addr_index (cu, addr_index);
20193}
20194
450a1bfc 20195/* See read.h. */
3019eac3
DE
20196
20197CORE_ADDR
82ca3f51 20198dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20199 dwarf2_per_objfile *per_objfile,
82ca3f51 20200 unsigned int addr_index)
3019eac3 20201{
976ca316 20202 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20203 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20204 int addr_size;
20205
3019eac3
DE
20206 /* We need addr_base and addr_size.
20207 If we don't have PER_CU->cu, we have to get it.
20208 Nasty, but the alternative is storing the needed info in PER_CU,
20209 which at this point doesn't seem justified: it's not clear how frequently
20210 it would get used and it would increase the size of every PER_CU.
20211 Entry points like dwarf2_per_cu_addr_size do a similar thing
20212 so we're not in uncharted territory here.
20213 Alas we need to be a bit more complicated as addr_base is contained
20214 in the DIE.
20215
20216 We don't need to read the entire CU(/TU).
20217 We just need the header and top level die.
a1b64ce1 20218
3019eac3 20219 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20220 For now we skip this optimization. */
3019eac3
DE
20221
20222 if (cu != NULL)
20223 {
20224 addr_base = cu->addr_base;
20225 addr_size = cu->header.addr_size;
20226 }
20227 else
20228 {
976ca316 20229 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20230 addr_base = reader.cu->addr_base;
20231 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20232 }
20233
976ca316 20234 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20235}
20236
18a8505e
AT
20237/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20238 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20239 DWO file. */
3019eac3 20240
d521ce57 20241static const char *
18a8505e
AT
20242read_str_index (struct dwarf2_cu *cu,
20243 struct dwarf2_section_info *str_section,
20244 struct dwarf2_section_info *str_offsets_section,
20245 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20246{
976ca316
SM
20247 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20248 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20249 const char *objf_name = objfile_name (objfile);
3019eac3 20250 bfd *abfd = objfile->obfd;
d521ce57 20251 const gdb_byte *info_ptr;
3019eac3 20252 ULONGEST str_offset;
cf532bd1 20253 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20254
96b79293
TT
20255 str_section->read (objfile);
20256 str_offsets_section->read (objfile);
73869dc2 20257 if (str_section->buffer == NULL)
18a8505e 20258 error (_("%s used without %s section"
9d8780f0 20259 " in CU at offset %s [in module %s]"),
96b79293 20260 form_name, str_section->get_name (),
dda83cd7 20261 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20262 if (str_offsets_section->buffer == NULL)
18a8505e 20263 error (_("%s used without %s section"
9d8780f0 20264 " in CU at offset %s [in module %s]"),
96b79293 20265 form_name, str_section->get_name (),
dda83cd7 20266 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20267 info_ptr = (str_offsets_section->buffer
18a8505e 20268 + str_offsets_base
3019eac3
DE
20269 + str_index * cu->header.offset_size);
20270 if (cu->header.offset_size == 4)
20271 str_offset = bfd_get_32 (abfd, info_ptr);
20272 else
20273 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20274 if (str_offset >= str_section->size)
57d63ce2 20275 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20276 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20277 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20278 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20279}
20280
18a8505e
AT
20281/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20282
20283static const char *
20284read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20285{
20286 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20287 ? reader->cu->header.addr_size : 0;
20288 return read_str_index (reader->cu,
20289 &reader->dwo_file->sections.str,
20290 &reader->dwo_file->sections.str_offsets,
20291 str_offsets_base, str_index);
20292}
20293
20294/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20295
20296static const char *
20297read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20298{
5e22e966 20299 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20300 const char *objf_name = objfile_name (objfile);
20301 static const char form_name[] = "DW_FORM_GNU_str_index";
20302 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20303
20304 if (!cu->str_offsets_base.has_value ())
20305 error (_("%s used in Fission stub without %s"
20306 " in CU at offset 0x%lx [in module %s]"),
20307 form_name, str_offsets_attr_name,
20308 (long) cu->header.offset_size, objf_name);
20309
20310 return read_str_index (cu,
5e22e966
SM
20311 &cu->per_objfile->per_bfd->str,
20312 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20313 *cu->str_offsets_base, str_index);
20314}
20315
3019eac3
DE
20316/* Return the length of an LEB128 number in BUF. */
20317
20318static int
20319leb128_size (const gdb_byte *buf)
20320{
20321 const gdb_byte *begin = buf;
20322 gdb_byte byte;
20323
20324 while (1)
20325 {
20326 byte = *buf++;
20327 if ((byte & 128) == 0)
20328 return buf - begin;
20329 }
20330}
20331
c906108c 20332static void
e142c38c 20333set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20334{
20335 switch (lang)
20336 {
20337 case DW_LANG_C89:
76bee0cc 20338 case DW_LANG_C99:
0cfd832f 20339 case DW_LANG_C11:
c906108c 20340 case DW_LANG_C:
d1be3247 20341 case DW_LANG_UPC:
e142c38c 20342 cu->language = language_c;
c906108c 20343 break;
9c37b5ae 20344 case DW_LANG_Java:
c906108c 20345 case DW_LANG_C_plus_plus:
0cfd832f
MW
20346 case DW_LANG_C_plus_plus_11:
20347 case DW_LANG_C_plus_plus_14:
e142c38c 20348 cu->language = language_cplus;
c906108c 20349 break;
6aecb9c2
JB
20350 case DW_LANG_D:
20351 cu->language = language_d;
20352 break;
c906108c
SS
20353 case DW_LANG_Fortran77:
20354 case DW_LANG_Fortran90:
b21b22e0 20355 case DW_LANG_Fortran95:
f7de9aab
MW
20356 case DW_LANG_Fortran03:
20357 case DW_LANG_Fortran08:
e142c38c 20358 cu->language = language_fortran;
c906108c 20359 break;
a766d390
DE
20360 case DW_LANG_Go:
20361 cu->language = language_go;
20362 break;
c906108c 20363 case DW_LANG_Mips_Assembler:
e142c38c 20364 cu->language = language_asm;
c906108c
SS
20365 break;
20366 case DW_LANG_Ada83:
8aaf0b47 20367 case DW_LANG_Ada95:
bc5f45f8
JB
20368 cu->language = language_ada;
20369 break;
72019c9c
GM
20370 case DW_LANG_Modula2:
20371 cu->language = language_m2;
20372 break;
fe8e67fd
PM
20373 case DW_LANG_Pascal83:
20374 cu->language = language_pascal;
20375 break;
22566fbd
DJ
20376 case DW_LANG_ObjC:
20377 cu->language = language_objc;
20378 break;
c44af4eb
TT
20379 case DW_LANG_Rust:
20380 case DW_LANG_Rust_old:
20381 cu->language = language_rust;
20382 break;
c906108c
SS
20383 case DW_LANG_Cobol74:
20384 case DW_LANG_Cobol85:
c906108c 20385 default:
e142c38c 20386 cu->language = language_minimal;
c906108c
SS
20387 break;
20388 }
e142c38c 20389 cu->language_defn = language_def (cu->language);
c906108c
SS
20390}
20391
20392/* Return the named attribute or NULL if not there. */
20393
20394static struct attribute *
e142c38c 20395dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20396{
a48e046c 20397 for (;;)
c906108c 20398 {
a48e046c
TT
20399 unsigned int i;
20400 struct attribute *spec = NULL;
20401
20402 for (i = 0; i < die->num_attrs; ++i)
20403 {
20404 if (die->attrs[i].name == name)
20405 return &die->attrs[i];
20406 if (die->attrs[i].name == DW_AT_specification
20407 || die->attrs[i].name == DW_AT_abstract_origin)
20408 spec = &die->attrs[i];
20409 }
20410
20411 if (!spec)
20412 break;
c906108c 20413
f2f0e013 20414 die = follow_die_ref (die, spec, &cu);
f2f0e013 20415 }
c5aa993b 20416
c906108c
SS
20417 return NULL;
20418}
20419
7d45c7c3
KB
20420/* Return the string associated with a string-typed attribute, or NULL if it
20421 is either not found or is of an incorrect type. */
20422
20423static const char *
20424dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20425{
20426 struct attribute *attr;
20427 const char *str = NULL;
20428
20429 attr = dwarf2_attr (die, name, cu);
20430
20431 if (attr != NULL)
20432 {
95f982e5 20433 str = attr->as_string ();
e61108c9 20434 if (str == nullptr)
dda83cd7 20435 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20436 "DIE at %s in module %s"),
20437 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20438 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20439 }
20440
20441 return str;
20442}
20443
a084a2a6 20444/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20445 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20446static const char *
20447dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20448{
20449 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20450 if (dwo_name == nullptr)
20451 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20452 return dwo_name;
20453}
20454
05cf31d1
JB
20455/* Return non-zero iff the attribute NAME is defined for the given DIE,
20456 and holds a non-zero value. This function should only be used for
2dc7f7b3 20457 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20458
20459static int
20460dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20461{
20462 struct attribute *attr = dwarf2_attr (die, name, cu);
20463
c45bc3f8 20464 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
20465}
20466
3ca72b44 20467static int
e142c38c 20468die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20469{
05cf31d1
JB
20470 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20471 which value is non-zero. However, we have to be careful with
20472 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20473 (via dwarf2_flag_true_p) follows this attribute. So we may
20474 end up accidently finding a declaration attribute that belongs
20475 to a different DIE referenced by the specification attribute,
20476 even though the given DIE does not have a declaration attribute. */
20477 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20478 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20479}
20480
63d06c5c 20481/* Return the die giving the specification for DIE, if there is
f2f0e013 20482 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20483 containing the return value on output. If there is no
20484 specification, but there is an abstract origin, that is
20485 returned. */
63d06c5c
DC
20486
20487static struct die_info *
f2f0e013 20488die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20489{
f2f0e013
DJ
20490 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20491 *spec_cu);
63d06c5c 20492
edb3359d
DJ
20493 if (spec_attr == NULL)
20494 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20495
63d06c5c
DC
20496 if (spec_attr == NULL)
20497 return NULL;
20498 else
f2f0e013 20499 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20500}
c906108c 20501
83769d0b 20502/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20503
20504static struct dwarf2_section_info *
20505get_debug_line_section (struct dwarf2_cu *cu)
20506{
20507 struct dwarf2_section_info *section;
976ca316 20508 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
20509
20510 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20511 DWO file. */
20512 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20513 section = &cu->dwo_unit->dwo_file->sections.line;
20514 else if (cu->per_cu->is_dwz)
20515 {
a7308ce0 20516 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
20517
20518 section = &dwz->line;
20519 }
20520 else
976ca316 20521 section = &per_objfile->per_bfd->line;
36586728
TT
20522
20523 return section;
20524}
20525
debd256d 20526/* Read the statement program header starting at OFFSET in
3019eac3 20527 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20528 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20529 Returns NULL if there is a problem reading the header, e.g., if it
20530 has a version we don't understand.
debd256d
JB
20531
20532 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20533 the returned object point into the dwarf line section buffer,
20534 and must not be freed. */
ae2de4f8 20535
fff8551c 20536static line_header_up
9c541725 20537dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20538{
3019eac3 20539 struct dwarf2_section_info *section;
976ca316 20540 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 20541
36586728 20542 section = get_debug_line_section (cu);
976ca316 20543 section->read (per_objfile->objfile);
3019eac3 20544 if (section->buffer == NULL)
debd256d 20545 {
3019eac3 20546 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20547 complaint (_("missing .debug_line.dwo section"));
3019eac3 20548 else
b98664d3 20549 complaint (_("missing .debug_line section"));
debd256d
JB
20550 return 0;
20551 }
20552
0df7ad3a 20553 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 20554 per_objfile, section, &cu->header);
debd256d 20555}
c906108c 20556
c6da4cef 20557/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20558 Return the file name of the psymtab for the given file_entry.
c6da4cef 20559 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20560 If space for the result is malloc'd, *NAME_HOLDER will be set.
20561 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20562
d521ce57 20563static const char *
7ba99d21 20564psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20565 const dwarf2_psymtab *pst,
c89b44cd
TT
20566 const char *comp_dir,
20567 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20568{
d521ce57
TT
20569 const char *include_name = fe.name;
20570 const char *include_name_to_compare = include_name;
72b9f47f 20571 const char *pst_filename;
c6da4cef
DE
20572 int file_is_pst;
20573
8c43009f 20574 const char *dir_name = fe.include_dir (lh);
c6da4cef 20575
c89b44cd 20576 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20577 if (!IS_ABSOLUTE_PATH (include_name)
20578 && (dir_name != NULL || comp_dir != NULL))
20579 {
20580 /* Avoid creating a duplicate psymtab for PST.
20581 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20582 Before we do the comparison, however, we need to account
20583 for DIR_NAME and COMP_DIR.
20584 First prepend dir_name (if non-NULL). If we still don't
20585 have an absolute path prepend comp_dir (if non-NULL).
20586 However, the directory we record in the include-file's
20587 psymtab does not contain COMP_DIR (to match the
20588 corresponding symtab(s)).
20589
20590 Example:
20591
20592 bash$ cd /tmp
20593 bash$ gcc -g ./hello.c
20594 include_name = "hello.c"
20595 dir_name = "."
20596 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20597 DW_AT_name = "./hello.c"
20598
20599 */
c6da4cef
DE
20600
20601 if (dir_name != NULL)
20602 {
c89b44cd
TT
20603 name_holder->reset (concat (dir_name, SLASH_STRING,
20604 include_name, (char *) NULL));
20605 include_name = name_holder->get ();
c6da4cef 20606 include_name_to_compare = include_name;
c6da4cef
DE
20607 }
20608 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20609 {
c89b44cd
TT
20610 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20611 include_name, (char *) NULL));
20612 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20613 }
20614 }
20615
20616 pst_filename = pst->filename;
c89b44cd 20617 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20618 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20619 {
c89b44cd
TT
20620 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20621 pst_filename, (char *) NULL));
20622 pst_filename = copied_name.get ();
c6da4cef
DE
20623 }
20624
1e3fad37 20625 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20626
c6da4cef
DE
20627 if (file_is_pst)
20628 return NULL;
20629 return include_name;
20630}
20631
d9b3de22
DE
20632/* State machine to track the state of the line number program. */
20633
6f77053d 20634class lnp_state_machine
d9b3de22 20635{
6f77053d
PA
20636public:
20637 /* Initialize a machine state for the start of a line number
20638 program. */
804d2729
TT
20639 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20640 bool record_lines_p);
6f77053d 20641
8c43009f
PA
20642 file_entry *current_file ()
20643 {
20644 /* lh->file_names is 0-based, but the file name numbers in the
20645 statement program are 1-based. */
6f77053d
PA
20646 return m_line_header->file_name_at (m_file);
20647 }
20648
20649 /* Record the line in the state machine. END_SEQUENCE is true if
20650 we're processing the end of a sequence. */
20651 void record_line (bool end_sequence);
20652
a8caed5d 20653 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 20654 nop-out rest of the lines in this sequence. */
6f77053d
PA
20655 void check_line_address (struct dwarf2_cu *cu,
20656 const gdb_byte *line_ptr,
7ab6656f 20657 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20658
20659 void handle_set_discriminator (unsigned int discriminator)
20660 {
20661 m_discriminator = discriminator;
20662 m_line_has_non_zero_discriminator |= discriminator != 0;
20663 }
20664
20665 /* Handle DW_LNE_set_address. */
20666 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20667 {
20668 m_op_index = 0;
20669 address += baseaddr;
20670 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20671 }
20672
20673 /* Handle DW_LNS_advance_pc. */
20674 void handle_advance_pc (CORE_ADDR adjust);
20675
20676 /* Handle a special opcode. */
20677 void handle_special_opcode (unsigned char op_code);
20678
20679 /* Handle DW_LNS_advance_line. */
20680 void handle_advance_line (int line_delta)
20681 {
20682 advance_line (line_delta);
20683 }
20684
20685 /* Handle DW_LNS_set_file. */
20686 void handle_set_file (file_name_index file);
20687
20688 /* Handle DW_LNS_negate_stmt. */
20689 void handle_negate_stmt ()
20690 {
20691 m_is_stmt = !m_is_stmt;
20692 }
20693
20694 /* Handle DW_LNS_const_add_pc. */
20695 void handle_const_add_pc ();
20696
20697 /* Handle DW_LNS_fixed_advance_pc. */
20698 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20699 {
20700 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20701 m_op_index = 0;
20702 }
20703
20704 /* Handle DW_LNS_copy. */
20705 void handle_copy ()
20706 {
20707 record_line (false);
20708 m_discriminator = 0;
20709 }
20710
20711 /* Handle DW_LNE_end_sequence. */
20712 void handle_end_sequence ()
20713 {
804d2729 20714 m_currently_recording_lines = true;
6f77053d
PA
20715 }
20716
20717private:
20718 /* Advance the line by LINE_DELTA. */
20719 void advance_line (int line_delta)
20720 {
20721 m_line += line_delta;
20722
20723 if (line_delta != 0)
20724 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20725 }
20726
804d2729
TT
20727 struct dwarf2_cu *m_cu;
20728
6f77053d
PA
20729 gdbarch *m_gdbarch;
20730
20731 /* True if we're recording lines.
20732 Otherwise we're building partial symtabs and are just interested in
20733 finding include files mentioned by the line number program. */
20734 bool m_record_lines_p;
20735
8c43009f 20736 /* The line number header. */
6f77053d 20737 line_header *m_line_header;
8c43009f 20738
6f77053d
PA
20739 /* These are part of the standard DWARF line number state machine,
20740 and initialized according to the DWARF spec. */
d9b3de22 20741
6f77053d 20742 unsigned char m_op_index = 0;
7ba99d21
AT
20743 /* The line table index of the current file. */
20744 file_name_index m_file = 1;
6f77053d
PA
20745 unsigned int m_line = 1;
20746
20747 /* These are initialized in the constructor. */
20748
20749 CORE_ADDR m_address;
20750 bool m_is_stmt;
20751 unsigned int m_discriminator;
d9b3de22
DE
20752
20753 /* Additional bits of state we need to track. */
20754
20755 /* The last file that we called dwarf2_start_subfile for.
20756 This is only used for TLLs. */
6f77053d 20757 unsigned int m_last_file = 0;
d9b3de22 20758 /* The last file a line number was recorded for. */
6f77053d 20759 struct subfile *m_last_subfile = NULL;
d9b3de22 20760
1313c56e
AB
20761 /* The address of the last line entry. */
20762 CORE_ADDR m_last_address;
20763
20764 /* Set to true when a previous line at the same address (using
20765 m_last_address) had m_is_stmt true. This is reset to false when a
20766 line entry at a new address (m_address different to m_last_address) is
20767 processed. */
20768 bool m_stmt_at_address = false;
20769
804d2729
TT
20770 /* When true, record the lines we decode. */
20771 bool m_currently_recording_lines = false;
d9b3de22
DE
20772
20773 /* The last line number that was recorded, used to coalesce
20774 consecutive entries for the same line. This can happen, for
20775 example, when discriminators are present. PR 17276. */
6f77053d
PA
20776 unsigned int m_last_line = 0;
20777 bool m_line_has_non_zero_discriminator = false;
8c43009f 20778};
d9b3de22 20779
6f77053d
PA
20780void
20781lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20782{
20783 CORE_ADDR addr_adj = (((m_op_index + adjust)
20784 / m_line_header->maximum_ops_per_instruction)
20785 * m_line_header->minimum_instruction_length);
20786 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20787 m_op_index = ((m_op_index + adjust)
20788 % m_line_header->maximum_ops_per_instruction);
20789}
d9b3de22 20790
6f77053d
PA
20791void
20792lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20793{
6f77053d 20794 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20795 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20796 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20797 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20798 / m_line_header->maximum_ops_per_instruction)
20799 * m_line_header->minimum_instruction_length);
20800 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20801 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20802 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20803
258bf0ee 20804 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20805 advance_line (line_delta);
20806 record_line (false);
20807 m_discriminator = 0;
20808}
d9b3de22 20809
6f77053d
PA
20810void
20811lnp_state_machine::handle_set_file (file_name_index file)
20812{
20813 m_file = file;
20814
20815 const file_entry *fe = current_file ();
20816 if (fe == NULL)
20817 dwarf2_debug_line_missing_file_complaint ();
20818 else if (m_record_lines_p)
20819 {
20820 const char *dir = fe->include_dir (m_line_header);
20821
c24bdb02 20822 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20823 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20824 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20825 }
20826}
20827
20828void
20829lnp_state_machine::handle_const_add_pc ()
20830{
20831 CORE_ADDR adjust
20832 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20833
20834 CORE_ADDR addr_adj
20835 = (((m_op_index + adjust)
20836 / m_line_header->maximum_ops_per_instruction)
20837 * m_line_header->minimum_instruction_length);
20838
20839 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20840 m_op_index = ((m_op_index + adjust)
20841 % m_line_header->maximum_ops_per_instruction);
20842}
d9b3de22 20843
a05a36a5
DE
20844/* Return non-zero if we should add LINE to the line number table.
20845 LINE is the line to add, LAST_LINE is the last line that was added,
20846 LAST_SUBFILE is the subfile for LAST_LINE.
20847 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20848 had a non-zero discriminator.
20849
20850 We have to be careful in the presence of discriminators.
20851 E.g., for this line:
20852
20853 for (i = 0; i < 100000; i++);
20854
20855 clang can emit four line number entries for that one line,
20856 each with a different discriminator.
20857 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20858
20859 However, we want gdb to coalesce all four entries into one.
20860 Otherwise the user could stepi into the middle of the line and
20861 gdb would get confused about whether the pc really was in the
20862 middle of the line.
20863
20864 Things are further complicated by the fact that two consecutive
20865 line number entries for the same line is a heuristic used by gcc
20866 to denote the end of the prologue. So we can't just discard duplicate
20867 entries, we have to be selective about it. The heuristic we use is
20868 that we only collapse consecutive entries for the same line if at least
20869 one of those entries has a non-zero discriminator. PR 17276.
20870
20871 Note: Addresses in the line number state machine can never go backwards
20872 within one sequence, thus this coalescing is ok. */
20873
20874static int
804d2729
TT
20875dwarf_record_line_p (struct dwarf2_cu *cu,
20876 unsigned int line, unsigned int last_line,
a05a36a5
DE
20877 int line_has_non_zero_discriminator,
20878 struct subfile *last_subfile)
20879{
c24bdb02 20880 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20881 return 1;
20882 if (line != last_line)
20883 return 1;
20884 /* Same line for the same file that we've seen already.
20885 As a last check, for pr 17276, only record the line if the line
20886 has never had a non-zero discriminator. */
20887 if (!line_has_non_zero_discriminator)
20888 return 1;
20889 return 0;
20890}
20891
804d2729
TT
20892/* Use the CU's builder to record line number LINE beginning at
20893 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20894
20895static void
d9b3de22 20896dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20897 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20898 struct dwarf2_cu *cu)
252a6764
DE
20899{
20900 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20901
27e0867f
DE
20902 if (dwarf_line_debug)
20903 {
20904 fprintf_unfiltered (gdb_stdlog,
20905 "Recording line %u, file %s, address %s\n",
20906 line, lbasename (subfile->name),
20907 paddress (gdbarch, address));
20908 }
20909
804d2729 20910 if (cu != nullptr)
8c95582d 20911 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20912}
20913
20914/* Subroutine of dwarf_decode_lines_1 to simplify it.
20915 Mark the end of a set of line number records.
d9b3de22 20916 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20917 If SUBFILE is NULL the request is ignored. */
20918
20919static void
20920dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20921 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20922{
27e0867f
DE
20923 if (subfile == NULL)
20924 return;
20925
20926 if (dwarf_line_debug)
20927 {
20928 fprintf_unfiltered (gdb_stdlog,
20929 "Finishing current line, file %s, address %s\n",
20930 lbasename (subfile->name),
20931 paddress (gdbarch, address));
20932 }
20933
8c95582d 20934 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20935}
20936
6f77053d
PA
20937void
20938lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20939{
d9b3de22
DE
20940 if (dwarf_line_debug)
20941 {
20942 fprintf_unfiltered (gdb_stdlog,
20943 "Processing actual line %u: file %u,"
94a72be7 20944 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20945 m_line, m_file,
6f77053d 20946 paddress (m_gdbarch, m_address),
94a72be7
AB
20947 m_is_stmt, m_discriminator,
20948 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20949 }
20950
6f77053d 20951 file_entry *fe = current_file ();
8c43009f
PA
20952
20953 if (fe == NULL)
d9b3de22
DE
20954 dwarf2_debug_line_missing_file_complaint ();
20955 /* For now we ignore lines not starting on an instruction boundary.
20956 But not when processing end_sequence for compatibility with the
20957 previous version of the code. */
6f77053d 20958 else if (m_op_index == 0 || end_sequence)
d9b3de22 20959 {
8c43009f 20960 fe->included_p = 1;
8c95582d 20961 if (m_record_lines_p)
d9b3de22 20962 {
1313c56e
AB
20963 /* When we switch files we insert an end maker in the first file,
20964 switch to the second file and add a new line entry. The
20965 problem is that the end marker inserted in the first file will
20966 discard any previous line entries at the same address. If the
20967 line entries in the first file are marked as is-stmt, while
20968 the new line in the second file is non-stmt, then this means
20969 the end marker will discard is-stmt lines so we can have a
20970 non-stmt line. This means that there are less addresses at
20971 which the user can insert a breakpoint.
20972
20973 To improve this we track the last address in m_last_address,
20974 and whether we have seen an is-stmt at this address. Then
20975 when switching files, if we have seen a stmt at the current
20976 address, and we are switching to create a non-stmt line, then
20977 discard the new line. */
20978 bool file_changed
20979 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20980 bool ignore_this_line
dda83cd7
SM
20981 = ((file_changed && !end_sequence && m_last_address == m_address
20982 && !m_is_stmt && m_stmt_at_address)
20983 || (!end_sequence && m_line == 0));
1313c56e
AB
20984
20985 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 20986 {
804d2729
TT
20987 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20988 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20989 }
20990
1313c56e 20991 if (!end_sequence && !ignore_this_line)
d9b3de22 20992 {
8c95582d
AB
20993 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20994
804d2729 20995 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20996 m_line_has_non_zero_discriminator,
20997 m_last_subfile))
d9b3de22 20998 {
c24bdb02 20999 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21000 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21001 builder->get_current_subfile (),
8c95582d 21002 m_line, m_address, is_stmt,
804d2729 21003 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21004 }
c24bdb02 21005 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21006 m_last_line = m_line;
d9b3de22
DE
21007 }
21008 }
21009 }
1313c56e
AB
21010
21011 /* Track whether we have seen any m_is_stmt true at m_address in case we
21012 have multiple line table entries all at m_address. */
21013 if (m_last_address != m_address)
21014 {
21015 m_stmt_at_address = false;
21016 m_last_address = m_address;
21017 }
21018 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21019}
21020
804d2729
TT
21021lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21022 line_header *lh, bool record_lines_p)
d9b3de22 21023{
804d2729 21024 m_cu = cu;
6f77053d
PA
21025 m_gdbarch = arch;
21026 m_record_lines_p = record_lines_p;
21027 m_line_header = lh;
d9b3de22 21028
804d2729 21029 m_currently_recording_lines = true;
d9b3de22 21030
d9b3de22
DE
21031 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21032 was a line entry for it so that the backend has a chance to adjust it
21033 and also record it in case it needs it. This is currently used by MIPS
21034 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21035 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21036 m_is_stmt = lh->default_is_stmt;
21037 m_discriminator = 0;
1313c56e
AB
21038
21039 m_last_address = m_address;
21040 m_stmt_at_address = false;
252a6764
DE
21041}
21042
6f77053d
PA
21043void
21044lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21045 const gdb_byte *line_ptr,
7ab6656f 21046 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21047{
a8caed5d
FS
21048 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21049 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21050 located at 0x0. In this case, additionally check that if
21051 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21052
a8caed5d
FS
21053 if ((address == 0 && address < unrelocated_lowpc)
21054 || address == (CORE_ADDR) -1)
924c2928
DE
21055 {
21056 /* This line table is for a function which has been
21057 GCd by the linker. Ignore it. PR gdb/12528 */
21058
5e22e966 21059 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21060 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21061
b98664d3 21062 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21063 line_offset, objfile_name (objfile));
804d2729
TT
21064 m_currently_recording_lines = false;
21065 /* Note: m_currently_recording_lines is left as false until we see
21066 DW_LNE_end_sequence. */
924c2928
DE
21067 }
21068}
21069
f3f5162e 21070/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21071 Process the line number information in LH.
21072 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21073 program in order to set included_p for every referenced header. */
debd256d 21074
c906108c 21075static void
43f3e411
DE
21076dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21077 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21078{
d521ce57
TT
21079 const gdb_byte *line_ptr, *extended_end;
21080 const gdb_byte *line_end;
a8c50c1f 21081 unsigned int bytes_read, extended_len;
699ca60a 21082 unsigned char op_code, extended_op;
e142c38c 21083 CORE_ADDR baseaddr;
5e22e966 21084 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21085 bfd *abfd = objfile->obfd;
08feed99 21086 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21087 /* True if we're recording line info (as opposed to building partial
21088 symtabs and just interested in finding include files mentioned by
21089 the line number program). */
21090 bool record_lines_p = !decode_for_pst_p;
e142c38c 21091
b3b3bada 21092 baseaddr = objfile->text_section_offset ();
c906108c 21093
debd256d
JB
21094 line_ptr = lh->statement_program_start;
21095 line_end = lh->statement_program_end;
c906108c
SS
21096
21097 /* Read the statement sequences until there's nothing left. */
21098 while (line_ptr < line_end)
21099 {
6f77053d
PA
21100 /* The DWARF line number program state machine. Reset the state
21101 machine at the start of each sequence. */
804d2729 21102 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21103 bool end_sequence = false;
d9b3de22 21104
8c43009f 21105 if (record_lines_p)
c906108c 21106 {
8c43009f
PA
21107 /* Start a subfile for the current file of the state
21108 machine. */
21109 const file_entry *fe = state_machine.current_file ();
21110
21111 if (fe != NULL)
804d2729 21112 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21113 }
21114
a738430d 21115 /* Decode the table. */
d9b3de22 21116 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21117 {
21118 op_code = read_1_byte (abfd, line_ptr);
21119 line_ptr += 1;
9aa1fe7e 21120
debd256d 21121 if (op_code >= lh->opcode_base)
6e70227d 21122 {
8e07a239 21123 /* Special opcode. */
6f77053d 21124 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21125 }
21126 else switch (op_code)
c906108c
SS
21127 {
21128 case DW_LNS_extended_op:
3e43a32a
MS
21129 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21130 &bytes_read);
473b7be6 21131 line_ptr += bytes_read;
a8c50c1f 21132 extended_end = line_ptr + extended_len;
c906108c
SS
21133 extended_op = read_1_byte (abfd, line_ptr);
21134 line_ptr += 1;
8f34b746
TV
21135 if (DW_LNE_lo_user <= extended_op
21136 && extended_op <= DW_LNE_hi_user)
21137 {
21138 /* Vendor extension, ignore. */
21139 line_ptr = extended_end;
21140 break;
21141 }
c906108c
SS
21142 switch (extended_op)
21143 {
21144 case DW_LNE_end_sequence:
6f77053d
PA
21145 state_machine.handle_end_sequence ();
21146 end_sequence = true;
c906108c
SS
21147 break;
21148 case DW_LNE_set_address:
d9b3de22
DE
21149 {
21150 CORE_ADDR address
c8a7a66f 21151 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21152 line_ptr += bytes_read;
6f77053d
PA
21153
21154 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21155 lowpc - baseaddr, address);
6f77053d 21156 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21157 }
c906108c
SS
21158 break;
21159 case DW_LNE_define_file:
dda83cd7
SM
21160 {
21161 const char *cur_file;
ecfb656c
PA
21162 unsigned int mod_time, length;
21163 dir_index dindex;
6e70227d 21164
dda83cd7 21165 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21166 &bytes_read);
dda83cd7
SM
21167 line_ptr += bytes_read;
21168 dindex = (dir_index)
21169 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21170 line_ptr += bytes_read;
21171 mod_time =
21172 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21173 line_ptr += bytes_read;
21174 length =
21175 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21176 line_ptr += bytes_read;
21177 lh->add_file_name (cur_file, dindex, mod_time, length);
21178 }
c906108c 21179 break;
d0c6ba3d 21180 case DW_LNE_set_discriminator:
6f77053d
PA
21181 {
21182 /* The discriminator is not interesting to the
21183 debugger; just ignore it. We still need to
21184 check its value though:
21185 if there are consecutive entries for the same
21186 (non-prologue) line we want to coalesce them.
21187 PR 17276. */
21188 unsigned int discr
21189 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21190 line_ptr += bytes_read;
21191
21192 state_machine.handle_set_discriminator (discr);
21193 }
d0c6ba3d 21194 break;
c906108c 21195 default:
b98664d3 21196 complaint (_("mangled .debug_line section"));
debd256d 21197 return;
c906108c 21198 }
a8c50c1f
DJ
21199 /* Make sure that we parsed the extended op correctly. If e.g.
21200 we expected a different address size than the producer used,
21201 we may have read the wrong number of bytes. */
21202 if (line_ptr != extended_end)
21203 {
b98664d3 21204 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21205 return;
21206 }
c906108c
SS
21207 break;
21208 case DW_LNS_copy:
6f77053d 21209 state_machine.handle_copy ();
c906108c
SS
21210 break;
21211 case DW_LNS_advance_pc:
2dc7f7b3
TT
21212 {
21213 CORE_ADDR adjust
21214 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21215 line_ptr += bytes_read;
6f77053d
PA
21216
21217 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21218 }
c906108c
SS
21219 break;
21220 case DW_LNS_advance_line:
a05a36a5
DE
21221 {
21222 int line_delta
21223 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21224 line_ptr += bytes_read;
6f77053d
PA
21225
21226 state_machine.handle_advance_line (line_delta);
a05a36a5 21227 }
c906108c
SS
21228 break;
21229 case DW_LNS_set_file:
d9b3de22 21230 {
6f77053d 21231 file_name_index file
ecfb656c
PA
21232 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21233 &bytes_read);
d9b3de22 21234 line_ptr += bytes_read;
8c43009f 21235
6f77053d 21236 state_machine.handle_set_file (file);
d9b3de22 21237 }
c906108c
SS
21238 break;
21239 case DW_LNS_set_column:
0ad93d4f 21240 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21241 line_ptr += bytes_read;
21242 break;
21243 case DW_LNS_negate_stmt:
6f77053d 21244 state_machine.handle_negate_stmt ();
c906108c
SS
21245 break;
21246 case DW_LNS_set_basic_block:
c906108c 21247 break;
c2c6d25f
JM
21248 /* Add to the address register of the state machine the
21249 address increment value corresponding to special opcode
a738430d
MK
21250 255. I.e., this value is scaled by the minimum
21251 instruction length since special opcode 255 would have
b021a221 21252 scaled the increment. */
c906108c 21253 case DW_LNS_const_add_pc:
6f77053d 21254 state_machine.handle_const_add_pc ();
c906108c
SS
21255 break;
21256 case DW_LNS_fixed_advance_pc:
3e29f34a 21257 {
6f77053d 21258 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21259 line_ptr += 2;
6f77053d
PA
21260
21261 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21262 }
c906108c 21263 break;
9aa1fe7e 21264 default:
a738430d
MK
21265 {
21266 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21267 int i;
a738430d 21268
debd256d 21269 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21270 {
21271 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21272 line_ptr += bytes_read;
21273 }
21274 }
c906108c
SS
21275 }
21276 }
d9b3de22
DE
21277
21278 if (!end_sequence)
21279 dwarf2_debug_line_missing_end_sequence_complaint ();
21280
21281 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21282 in which case we still finish recording the last line). */
6f77053d 21283 state_machine.record_line (true);
c906108c 21284 }
f3f5162e
DE
21285}
21286
21287/* Decode the Line Number Program (LNP) for the given line_header
21288 structure and CU. The actual information extracted and the type
21289 of structures created from the LNP depends on the value of PST.
21290
21291 1. If PST is NULL, then this procedure uses the data from the program
21292 to create all necessary symbol tables, and their linetables.
21293
21294 2. If PST is not NULL, this procedure reads the program to determine
21295 the list of files included by the unit represented by PST, and
21296 builds all the associated partial symbol tables.
21297
21298 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21299 It is used for relative paths in the line table.
21300 NOTE: When processing partial symtabs (pst != NULL),
21301 comp_dir == pst->dirname.
21302
21303 NOTE: It is important that psymtabs have the same file name (via strcmp)
21304 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21305 symtab we don't use it in the name of the psymtabs we create.
21306 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21307 A good testcase for this is mb-inline.exp.
21308
527f3840
JK
21309 LOWPC is the lowest address in CU (or 0 if not known).
21310
21311 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21312 for its PC<->lines mapping information. Otherwise only the filename
21313 table is read in. */
f3f5162e
DE
21314
21315static void
21316dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21317 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21318 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21319{
5e22e966 21320 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21321 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21322
527f3840
JK
21323 if (decode_mapping)
21324 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21325
21326 if (decode_for_pst_p)
21327 {
aaa75496 21328 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21329 create the psymtab of each included file. */
7ba99d21 21330 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21331 if (file_entry.included_p == 1)
21332 {
c89b44cd 21333 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21334 const char *include_name =
7ba99d21
AT
21335 psymtab_include_file_name (lh, file_entry, pst,
21336 comp_dir, &name_holder);
c6da4cef 21337 if (include_name != NULL)
7b249e47
TT
21338 dwarf2_create_include_psymtab
21339 (cu->per_objfile->per_bfd, include_name, pst,
21340 cu->per_objfile->per_bfd->partial_symtabs.get (),
0072c873 21341 objfile->per_bfd);
dda83cd7 21342 }
aaa75496 21343 }
cb1df416
DJ
21344 else
21345 {
21346 /* Make sure a symtab is created for every file, even files
21347 which contain only variables (i.e. no code with associated
21348 line numbers). */
c24bdb02
KS
21349 buildsym_compunit *builder = cu->get_builder ();
21350 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21351
7ba99d21 21352 for (auto &fe : lh->file_names ())
cb1df416 21353 {
804d2729 21354 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21355 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21356 {
c24bdb02 21357 builder->get_current_subfile ()->symtab
804d2729 21358 = allocate_symtab (cust,
c24bdb02 21359 builder->get_current_subfile ()->name);
43f3e411 21360 }
c24bdb02 21361 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21362 }
21363 }
c906108c
SS
21364}
21365
21366/* Start a subfile for DWARF. FILENAME is the name of the file and
21367 DIRNAME the name of the source directory which contains FILENAME
4d663531 21368 or NULL if not known.
c906108c
SS
21369 This routine tries to keep line numbers from identical absolute and
21370 relative file names in a common subfile.
21371
21372 Using the `list' example from the GDB testsuite, which resides in
21373 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21374 of /srcdir/list0.c yields the following debugging information for list0.c:
21375
c5aa993b 21376 DW_AT_name: /srcdir/list0.c
4d663531 21377 DW_AT_comp_dir: /compdir
357e46e7 21378 files.files[0].name: list0.h
c5aa993b 21379 files.files[0].dir: /srcdir
357e46e7 21380 files.files[1].name: list0.c
c5aa993b 21381 files.files[1].dir: /srcdir
c906108c
SS
21382
21383 The line number information for list0.c has to end up in a single
4f1520fb
FR
21384 subfile, so that `break /srcdir/list0.c:1' works as expected.
21385 start_subfile will ensure that this happens provided that we pass the
21386 concatenation of files.files[1].dir and files.files[1].name as the
21387 subfile's name. */
c906108c
SS
21388
21389static void
804d2729
TT
21390dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21391 const char *dirname)
c906108c 21392{
43816ebc 21393 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21394
4d663531 21395 /* In order not to lose the line information directory,
4f1520fb
FR
21396 we concatenate it to the filename when it makes sense.
21397 Note that the Dwarf3 standard says (speaking of filenames in line
21398 information): ``The directory index is ignored for file names
21399 that represent full path names''. Thus ignoring dirname in the
21400 `else' branch below isn't an issue. */
c906108c 21401
d5166ae1 21402 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21403 {
43816ebc
TT
21404 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21405 filename = copy.get ();
d521ce57 21406 }
c906108c 21407
c24bdb02 21408 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21409}
21410
4c2df51b
DJ
21411static void
21412var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21413 struct dwarf2_cu *cu)
4c2df51b 21414{
5e22e966 21415 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21416 struct comp_unit_head *cu_header = &cu->header;
21417
4c2df51b
DJ
21418 /* NOTE drow/2003-01-30: There used to be a comment and some special
21419 code here to turn a symbol with DW_AT_external and a
21420 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21421 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21422 with some versions of binutils) where shared libraries could have
21423 relocations against symbols in their debug information - the
21424 minimal symbol would have the right address, but the debug info
21425 would not. It's no longer necessary, because we will explicitly
21426 apply relocations when we read in the debug information now. */
21427
21428 /* A DW_AT_location attribute with no contents indicates that a
21429 variable has been optimized away. */
9d2246fc 21430 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21431 {
f1e6e072 21432 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21433 return;
21434 }
21435
21436 /* Handle one degenerate form of location expression specially, to
21437 preserve GDB's previous behavior when section offsets are
336d760d
AT
21438 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21439 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21440
9d2246fc
TT
21441 if (attr->form_is_block ())
21442 {
21443 struct dwarf_block *block = attr->as_block ();
21444
21445 if ((block->data[0] == DW_OP_addr
21446 && block->size == 1 + cu_header->addr_size)
21447 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 21448 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
21449 && (block->size
21450 == 1 + leb128_size (&block->data[1]))))
21451 {
21452 unsigned int dummy;
21453
21454 if (block->data[0] == DW_OP_addr)
21455 SET_SYMBOL_VALUE_ADDRESS
21456 (sym, cu->header.read_address (objfile->obfd,
21457 block->data + 1,
38583298 21458 &dummy));
9d2246fc
TT
21459 else
21460 SET_SYMBOL_VALUE_ADDRESS
21461 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21462 &dummy));
21463 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21464 fixup_symbol_section (sym, objfile);
21465 SET_SYMBOL_VALUE_ADDRESS
21466 (sym,
21467 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 21468 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
21469 return;
21470 }
4c2df51b
DJ
21471 }
21472
21473 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21474 expression evaluator, and use LOC_COMPUTED only when necessary
21475 (i.e. when the value of a register or memory location is
21476 referenced, or a thread-local block, etc.). Then again, it might
21477 not be worthwhile. I'm assuming that it isn't unless performance
21478 or memory numbers show me otherwise. */
21479
f1e6e072 21480 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21481
f1e6e072 21482 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21483 cu->has_loclist = true;
4c2df51b
DJ
21484}
21485
c906108c
SS
21486/* Given a pointer to a DWARF information entry, figure out if we need
21487 to make a symbol table entry for it, and if so, create a new entry
21488 and return a pointer to it.
21489 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21490 used the passed type.
21491 If SPACE is not NULL, use it to hold the new symbol. If it is
21492 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21493
21494static struct symbol *
5e2db402
TT
21495new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21496 struct symbol *space)
c906108c 21497{
976ca316
SM
21498 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21499 struct objfile *objfile = per_objfile->objfile;
08feed99 21500 struct gdbarch *gdbarch = objfile->arch ();
c906108c 21501 struct symbol *sym = NULL;
15d034d0 21502 const char *name;
c906108c
SS
21503 struct attribute *attr = NULL;
21504 struct attribute *attr2 = NULL;
e142c38c 21505 CORE_ADDR baseaddr;
e37fd15a
SW
21506 struct pending **list_to_add = NULL;
21507
edb3359d 21508 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21509
b3b3bada 21510 baseaddr = objfile->text_section_offset ();
c906108c 21511
94af9270 21512 name = dwarf2_name (die, cu);
c906108c
SS
21513 if (name)
21514 {
34eaf542 21515 int suppress_add = 0;
94af9270 21516
34eaf542
TT
21517 if (space)
21518 sym = space;
21519 else
8c14c3a3 21520 sym = new (&objfile->objfile_obstack) symbol;
c906108c 21521 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21522
21523 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21524 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
21525 /* Fortran does not have mangling standard and the mangling does differ
21526 between gfortran, iFort etc. */
bcfe6157
TT
21527 const char *physname
21528 = (cu->language == language_fortran
21529 ? dwarf2_full_name (name, die, cu)
21530 : dwarf2_physname (name, die, cu));
21531 const char *linkagename = dw2_linkage_name (die, cu);
21532
21533 if (linkagename == nullptr || cu->language == language_ada)
21534 sym->set_linkage_name (physname);
21535 else
21536 {
21537 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21538 sym->set_linkage_name (linkagename);
21539 }
f55ee35c 21540
c906108c 21541 /* Default assumptions.
dda83cd7 21542 Use the passed type or decode it from the die. */
176620f1 21543 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21544 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21545 if (type != NULL)
21546 SYMBOL_TYPE (sym) = type;
21547 else
e7c27a73 21548 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21549 attr = dwarf2_attr (die,
21550 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21551 cu);
435d3d88 21552 if (attr != nullptr)
529908cb 21553 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 21554
edb3359d
DJ
21555 attr = dwarf2_attr (die,
21556 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21557 cu);
aa659cfa 21558 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 21559 {
529908cb 21560 file_name_index file_index
aa659cfa 21561 = (file_name_index) attr->as_nonnegative ();
8c43009f 21562 struct file_entry *fe;
9a619af0 21563
ecfb656c
PA
21564 if (cu->line_header != NULL)
21565 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21566 else
21567 fe = NULL;
21568
21569 if (fe == NULL)
b98664d3 21570 complaint (_("file index out of range"));
8c43009f
PA
21571 else
21572 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21573 }
21574
c906108c
SS
21575 switch (die->tag)
21576 {
21577 case DW_TAG_label:
e142c38c 21578 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21579 if (attr != nullptr)
3e29f34a
MR
21580 {
21581 CORE_ADDR addr;
21582
95f982e5 21583 addr = attr->as_address ();
3e29f34a 21584 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21585 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 21586 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 21587 }
8f5c6526
TV
21588 else
21589 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
21590 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21591 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 21592 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21593 break;
21594 case DW_TAG_subprogram:
21595 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21596 finish_block. */
f1e6e072 21597 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21598 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 21599 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
21600 || cu->language == language_ada
21601 || cu->language == language_fortran)
c906108c 21602 {
dda83cd7
SM
21603 /* Subprograms marked external are stored as a global symbol.
21604 Ada and Fortran subprograms, whether marked external or
21605 not, are always stored as a global symbol, because we want
21606 to be able to access them globally. For instance, we want
21607 to be able to break on a nested subprogram without having
21608 to specify the context. */
c24bdb02 21609 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21610 }
21611 else
21612 {
e37fd15a 21613 list_to_add = cu->list_in_scope;
c906108c
SS
21614 }
21615 break;
edb3359d
DJ
21616 case DW_TAG_inlined_subroutine:
21617 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21618 finish_block. */
f1e6e072 21619 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21620 SYMBOL_INLINED (sym) = 1;
481860b3 21621 list_to_add = cu->list_in_scope;
edb3359d 21622 break;
34eaf542
TT
21623 case DW_TAG_template_value_param:
21624 suppress_add = 1;
21625 /* Fall through. */
72929c62 21626 case DW_TAG_constant:
c906108c 21627 case DW_TAG_variable:
254e6b9e 21628 case DW_TAG_member:
0963b4bd
MS
21629 /* Compilation with minimal debug info may result in
21630 variables with missing type entries. Change the
21631 misleading `void' type to something sensible. */
78134374 21632 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 21633 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21634
e142c38c 21635 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21636 /* In the case of DW_TAG_member, we should only be called for
21637 static const members. */
21638 if (die->tag == DW_TAG_member)
21639 {
3863f96c
DE
21640 /* dwarf2_add_field uses die_is_declaration,
21641 so we do the same. */
254e6b9e
DE
21642 gdb_assert (die_is_declaration (die, cu));
21643 gdb_assert (attr);
21644 }
435d3d88 21645 if (attr != nullptr)
c906108c 21646 {
e7c27a73 21647 dwarf2_const_value (attr, sym, cu);
e142c38c 21648 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21649 if (!suppress_add)
34eaf542 21650 {
c45bc3f8 21651 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 21652 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21653 else
e37fd15a 21654 list_to_add = cu->list_in_scope;
34eaf542 21655 }
c906108c
SS
21656 break;
21657 }
e142c38c 21658 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21659 if (attr != nullptr)
c906108c 21660 {
e7c27a73 21661 var_decode_location (attr, sym, cu);
e142c38c 21662 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21663
21664 /* Fortran explicitly imports any global symbols to the local
21665 scope by DW_TAG_common_block. */
21666 if (cu->language == language_fortran && die->parent
21667 && die->parent->tag == DW_TAG_common_block)
21668 attr2 = NULL;
21669
caac4577
JG
21670 if (SYMBOL_CLASS (sym) == LOC_STATIC
21671 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 21672 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
21673 {
21674 /* When a static variable is eliminated by the linker,
21675 the corresponding debug information is not stripped
21676 out, but the variable address is set to null;
21677 do not add such variables into symbol table. */
21678 }
c45bc3f8 21679 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 21680 {
4b610737
TT
21681 if (SYMBOL_CLASS (sym) == LOC_STATIC
21682 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 21683 && per_objfile->per_bfd->can_copy)
4b610737
TT
21684 {
21685 /* A global static variable might be subject to
21686 copy relocation. We first check for a local
21687 minsym, though, because maybe the symbol was
21688 marked hidden, in which case this would not
21689 apply. */
21690 bound_minimal_symbol found
21691 = (lookup_minimal_symbol_linkage
987012b8 21692 (sym->linkage_name (), objfile));
4b610737
TT
21693 if (found.minsym != nullptr)
21694 sym->maybe_copied = 1;
21695 }
f55ee35c 21696
1c809c68
TT
21697 /* A variable with DW_AT_external is never static,
21698 but it may be block-scoped. */
804d2729 21699 list_to_add
c24bdb02
KS
21700 = ((cu->list_in_scope
21701 == cu->get_builder ()->get_file_symbols ())
21702 ? cu->get_builder ()->get_global_symbols ()
804d2729 21703 : cu->list_in_scope);
1c809c68 21704 }
c906108c 21705 else
e37fd15a 21706 list_to_add = cu->list_in_scope;
c906108c
SS
21707 }
21708 else
21709 {
21710 /* We do not know the address of this symbol.
dda83cd7
SM
21711 If it is an external symbol and we have type information
21712 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21713 The address of the variable will then be determined from
21714 the minimal symbol table whenever the variable is
21715 referenced. */
e142c38c 21716 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21717
21718 /* Fortran explicitly imports any global symbols to the local
21719 scope by DW_TAG_common_block. */
21720 if (cu->language == language_fortran && die->parent
21721 && die->parent->tag == DW_TAG_common_block)
21722 {
21723 /* SYMBOL_CLASS doesn't matter here because
21724 read_common_block is going to reset it. */
21725 if (!suppress_add)
21726 list_to_add = cu->list_in_scope;
21727 }
c45bc3f8 21728 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 21729 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21730 {
0fe7935b
DJ
21731 /* A variable with DW_AT_external is never static, but it
21732 may be block-scoped. */
804d2729 21733 list_to_add
c24bdb02
KS
21734 = ((cu->list_in_scope
21735 == cu->get_builder ()->get_file_symbols ())
21736 ? cu->get_builder ()->get_global_symbols ()
804d2729 21737 : cu->list_in_scope);
0fe7935b 21738
f1e6e072 21739 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21740 }
442ddf59
JK
21741 else if (!die_is_declaration (die, cu))
21742 {
21743 /* Use the default LOC_OPTIMIZED_OUT class. */
21744 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21745 if (!suppress_add)
21746 list_to_add = cu->list_in_scope;
442ddf59 21747 }
c906108c
SS
21748 }
21749 break;
21750 case DW_TAG_formal_parameter:
a60f3166 21751 {
3db19b2d
TV
21752 /* If we are inside a function, mark this as an argument. If
21753 not, we might be looking at an argument to an inlined function
21754 when we do not have enough information to show inlined frames;
21755 pretend it's a local variable in that case so that the user can
21756 still see it. */
21757 struct context_stack *curr
21758 = cu->get_builder ()->get_current_context_stack ();
21759 if (curr != nullptr && curr->name != nullptr)
21760 SYMBOL_IS_ARGUMENT (sym) = 1;
a60f3166 21761 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21762 if (attr != nullptr)
a60f3166
TT
21763 {
21764 var_decode_location (attr, sym, cu);
21765 }
21766 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21767 if (attr != nullptr)
a60f3166
TT
21768 {
21769 dwarf2_const_value (attr, sym, cu);
21770 }
f346a30d 21771
a60f3166
TT
21772 list_to_add = cu->list_in_scope;
21773 }
c906108c
SS
21774 break;
21775 case DW_TAG_unspecified_parameters:
21776 /* From varargs functions; gdb doesn't seem to have any
21777 interest in this information, so just ignore it for now.
21778 (FIXME?) */
21779 break;
34eaf542
TT
21780 case DW_TAG_template_type_param:
21781 suppress_add = 1;
21782 /* Fall through. */
c906108c 21783 case DW_TAG_class_type:
680b30c7 21784 case DW_TAG_interface_type:
c906108c
SS
21785 case DW_TAG_structure_type:
21786 case DW_TAG_union_type:
72019c9c 21787 case DW_TAG_set_type:
c906108c 21788 case DW_TAG_enumeration_type:
f1e6e072 21789 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21790 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21791
63d06c5c 21792 {
9c37b5ae 21793 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21794 really ever be static objects: otherwise, if you try
21795 to, say, break of a class's method and you're in a file
21796 which doesn't mention that class, it won't work unless
21797 the check for all static symbols in lookup_symbol_aux
21798 saves you. See the OtherFileClass tests in
21799 gdb.c++/namespace.exp. */
21800
e37fd15a 21801 if (!suppress_add)
34eaf542 21802 {
c24bdb02 21803 buildsym_compunit *builder = cu->get_builder ();
804d2729 21804 list_to_add
c24bdb02 21805 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21806 && cu->language == language_cplus
c24bdb02 21807 ? builder->get_global_symbols ()
804d2729 21808 : cu->list_in_scope);
63d06c5c 21809
64382290 21810 /* The semantics of C++ state that "struct foo {
9c37b5ae 21811 ... }" also defines a typedef for "foo". */
64382290 21812 if (cu->language == language_cplus
45280282 21813 || cu->language == language_ada
c44af4eb
TT
21814 || cu->language == language_d
21815 || cu->language == language_rust)
64382290
TT
21816 {
21817 /* The symbol's name is already allocated along
21818 with this objfile, so we don't need to
21819 duplicate it for the type. */
7d93a1e0 21820 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21821 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21822 }
63d06c5c
DC
21823 }
21824 }
c906108c
SS
21825 break;
21826 case DW_TAG_typedef:
f1e6e072 21827 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21828 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21829 list_to_add = cu->list_in_scope;
63d06c5c 21830 break;
d8f62e84 21831 case DW_TAG_array_type:
c906108c 21832 case DW_TAG_base_type:
dda83cd7 21833 case DW_TAG_subrange_type:
f1e6e072 21834 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21835 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21836 list_to_add = cu->list_in_scope;
c906108c
SS
21837 break;
21838 case DW_TAG_enumerator:
e142c38c 21839 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21840 if (attr != nullptr)
c906108c 21841 {
e7c27a73 21842 dwarf2_const_value (attr, sym, cu);
c906108c 21843 }
63d06c5c
DC
21844 {
21845 /* NOTE: carlton/2003-11-10: See comment above in the
21846 DW_TAG_class_type, etc. block. */
21847
804d2729 21848 list_to_add
c24bdb02 21849 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21850 && cu->language == language_cplus
c24bdb02 21851 ? cu->get_builder ()->get_global_symbols ()
804d2729 21852 : cu->list_in_scope);
63d06c5c 21853 }
c906108c 21854 break;
74921315 21855 case DW_TAG_imported_declaration:
5c4e30ca 21856 case DW_TAG_namespace:
f1e6e072 21857 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21858 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21859 break;
530e8392
KB
21860 case DW_TAG_module:
21861 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21862 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21863 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21864 break;
4357ac6c 21865 case DW_TAG_common_block:
f1e6e072 21866 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21867 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21868 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21869 break;
c906108c
SS
21870 default:
21871 /* Not a tag we recognize. Hopefully we aren't processing
21872 trash data, but since we must specifically ignore things
21873 we don't recognize, there is nothing else we should do at
0963b4bd 21874 this point. */
b98664d3 21875 complaint (_("unsupported tag: '%s'"),
4d3c2250 21876 dwarf_tag_name (die->tag));
c906108c
SS
21877 break;
21878 }
df8a16a1 21879
e37fd15a
SW
21880 if (suppress_add)
21881 {
21882 sym->hash_next = objfile->template_symbols;
21883 objfile->template_symbols = sym;
21884 list_to_add = NULL;
21885 }
21886
21887 if (list_to_add != NULL)
d3cb6808 21888 add_symbol_to_list (sym, list_to_add);
e37fd15a 21889
df8a16a1
DJ
21890 /* For the benefit of old versions of GCC, check for anonymous
21891 namespaces based on the demangled name. */
4d4ec4e5 21892 if (!cu->processing_has_namespace_info
94af9270 21893 && cu->language == language_cplus)
c24bdb02 21894 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21895 }
21896 return (sym);
21897}
21898
98bfdba5
PA
21899/* Given an attr with a DW_FORM_dataN value in host byte order,
21900 zero-extend it as appropriate for the symbol's type. The DWARF
21901 standard (v4) is not entirely clear about the meaning of using
21902 DW_FORM_dataN for a constant with a signed type, where the type is
21903 wider than the data. The conclusion of a discussion on the DWARF
21904 list was that this is unspecified. We choose to always zero-extend
21905 because that is the interpretation long in use by GCC. */
c906108c 21906
98bfdba5 21907static gdb_byte *
ff39bb5e 21908dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21909 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21910{
5e22e966 21911 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21912 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21913 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 21914 LONGEST l = attr->constant_value (0);
98bfdba5
PA
21915
21916 if (bits < sizeof (*value) * 8)
21917 {
21918 l &= ((LONGEST) 1 << bits) - 1;
21919 *value = l;
21920 }
21921 else if (bits == sizeof (*value) * 8)
21922 *value = l;
21923 else
21924 {
224c3ddb 21925 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21926 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21927 return bytes;
21928 }
21929
21930 return NULL;
21931}
21932
21933/* Read a constant value from an attribute. Either set *VALUE, or if
21934 the value does not fit in *VALUE, set *BYTES - either already
21935 allocated on the objfile obstack, or newly allocated on OBSTACK,
21936 or, set *BATON, if we translated the constant to a location
21937 expression. */
21938
21939static void
ff39bb5e 21940dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21941 const char *name, struct obstack *obstack,
21942 struct dwarf2_cu *cu,
d521ce57 21943 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21944 struct dwarf2_locexpr_baton **baton)
21945{
5e22e966 21946 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21947 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21948 struct comp_unit_head *cu_header = &cu->header;
c906108c 21949 struct dwarf_block *blk;
98bfdba5
PA
21950 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21951 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21952
21953 *value = 0;
21954 *bytes = NULL;
21955 *baton = NULL;
c906108c
SS
21956
21957 switch (attr->form)
21958 {
21959 case DW_FORM_addr:
336d760d 21960 case DW_FORM_addrx:
3019eac3 21961 case DW_FORM_GNU_addr_index:
ac56253d 21962 {
ac56253d
TT
21963 gdb_byte *data;
21964
98bfdba5
PA
21965 if (TYPE_LENGTH (type) != cu_header->addr_size)
21966 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21967 cu_header->addr_size,
98bfdba5 21968 TYPE_LENGTH (type));
ac56253d
TT
21969 /* Symbols of this form are reasonably rare, so we just
21970 piggyback on the existing location code rather than writing
21971 a new implementation of symbol_computed_ops. */
8d749320 21972 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21973 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21974 (*baton)->per_cu = cu->per_cu;
21975 gdb_assert ((*baton)->per_cu);
ac56253d 21976
98bfdba5 21977 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21978 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21979 (*baton)->data = data;
ac56253d
TT
21980
21981 data[0] = DW_OP_addr;
21982 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 21983 byte_order, attr->as_address ());
ac56253d 21984 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21985 }
c906108c 21986 break;
4ac36638 21987 case DW_FORM_string:
93b5768b 21988 case DW_FORM_strp:
cf532bd1 21989 case DW_FORM_strx:
3019eac3 21990 case DW_FORM_GNU_str_index:
36586728 21991 case DW_FORM_GNU_strp_alt:
c6481205 21992 /* The string is already allocated on the objfile obstack, point
98bfdba5 21993 directly to it. */
2c830f54 21994 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 21995 break;
c906108c
SS
21996 case DW_FORM_block1:
21997 case DW_FORM_block2:
21998 case DW_FORM_block4:
21999 case DW_FORM_block:
2dc7f7b3 22000 case DW_FORM_exprloc:
0224619f 22001 case DW_FORM_data16:
9d2246fc 22002 blk = attr->as_block ();
98bfdba5
PA
22003 if (TYPE_LENGTH (type) != blk->size)
22004 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22005 TYPE_LENGTH (type));
22006 *bytes = blk->data;
c906108c 22007 break;
2df3850c
JM
22008
22009 /* The DW_AT_const_value attributes are supposed to carry the
22010 symbol's value "represented as it would be on the target
22011 architecture." By the time we get here, it's already been
22012 converted to host endianness, so we just need to sign- or
22013 zero-extend it as appropriate. */
22014 case DW_FORM_data1:
3aef2284 22015 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22016 break;
c906108c 22017 case DW_FORM_data2:
3aef2284 22018 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22019 break;
c906108c 22020 case DW_FORM_data4:
3aef2284 22021 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22022 break;
c906108c 22023 case DW_FORM_data8:
3aef2284 22024 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22025 break;
22026
c906108c 22027 case DW_FORM_sdata:
663c44ac 22028 case DW_FORM_implicit_const:
1bc397c5 22029 *value = attr->as_signed ();
2df3850c
JM
22030 break;
22031
c906108c 22032 case DW_FORM_udata:
529908cb 22033 *value = attr->as_unsigned ();
c906108c 22034 break;
2df3850c 22035
c906108c 22036 default:
b98664d3 22037 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22038 dwarf_form_name (attr->form));
98bfdba5 22039 *value = 0;
c906108c
SS
22040 break;
22041 }
22042}
22043
2df3850c 22044
98bfdba5
PA
22045/* Copy constant value from an attribute to a symbol. */
22046
2df3850c 22047static void
ff39bb5e 22048dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22049 struct dwarf2_cu *cu)
2df3850c 22050{
5e22e966 22051 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22052 LONGEST value;
d521ce57 22053 const gdb_byte *bytes;
98bfdba5 22054 struct dwarf2_locexpr_baton *baton;
2df3850c 22055
98bfdba5 22056 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22057 sym->print_name (),
98bfdba5
PA
22058 &objfile->objfile_obstack, cu,
22059 &value, &bytes, &baton);
2df3850c 22060
98bfdba5
PA
22061 if (baton != NULL)
22062 {
98bfdba5 22063 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22064 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22065 }
22066 else if (bytes != NULL)
22067 {
22068 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22069 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22070 }
22071 else
22072 {
22073 SYMBOL_VALUE (sym) = value;
f1e6e072 22074 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22075 }
2df3850c
JM
22076}
22077
c906108c
SS
22078/* Return the type of the die in question using its DW_AT_type attribute. */
22079
22080static struct type *
e7c27a73 22081die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22082{
c906108c 22083 struct attribute *type_attr;
c906108c 22084
e142c38c 22085 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22086 if (!type_attr)
22087 {
5e22e966 22088 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22089 /* A missing DW_AT_type represents a void type. */
518817b3 22090 return objfile_type (objfile)->builtin_void;
c906108c 22091 }
348e048f 22092
673bfd45 22093 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22094}
22095
b4ba55a1
JB
22096/* True iff CU's producer generates GNAT Ada auxiliary information
22097 that allows to find parallel types through that information instead
22098 of having to do expensive parallel lookups by type name. */
22099
22100static int
22101need_gnat_info (struct dwarf2_cu *cu)
22102{
de4cb04a
JB
22103 /* Assume that the Ada compiler was GNAT, which always produces
22104 the auxiliary information. */
22105 return (cu->language == language_ada);
b4ba55a1
JB
22106}
22107
b4ba55a1
JB
22108/* Return the auxiliary type of the die in question using its
22109 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22110 attribute is not present. */
22111
22112static struct type *
22113die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22114{
b4ba55a1 22115 struct attribute *type_attr;
b4ba55a1
JB
22116
22117 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22118 if (!type_attr)
22119 return NULL;
22120
673bfd45 22121 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22122}
22123
22124/* If DIE has a descriptive_type attribute, then set the TYPE's
22125 descriptive type accordingly. */
22126
22127static void
22128set_descriptive_type (struct type *type, struct die_info *die,
22129 struct dwarf2_cu *cu)
22130{
22131 struct type *descriptive_type = die_descriptive_type (die, cu);
22132
22133 if (descriptive_type)
22134 {
22135 ALLOCATE_GNAT_AUX_TYPE (type);
22136 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22137 }
22138}
22139
c906108c
SS
22140/* Return the containing type of the die in question using its
22141 DW_AT_containing_type attribute. */
22142
22143static struct type *
e7c27a73 22144die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22145{
c906108c 22146 struct attribute *type_attr;
5e22e966 22147 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22148
e142c38c 22149 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22150 if (!type_attr)
22151 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22152 "[in module %s]"), objfile_name (objfile));
33ac96f0 22153
673bfd45 22154 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22155}
22156
ac9ec31b
DE
22157/* Return an error marker type to use for the ill formed type in DIE/CU. */
22158
22159static struct type *
22160build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22161{
976ca316
SM
22162 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22163 struct objfile *objfile = per_objfile->objfile;
528e1572 22164 char *saved;
ac9ec31b 22165
528e1572
SM
22166 std::string message
22167 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22168 objfile_name (objfile),
22169 sect_offset_str (cu->header.sect_off),
22170 sect_offset_str (die->sect_off));
efba19b0 22171 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22172
19f392bc 22173 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22174}
22175
673bfd45 22176/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22177 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22178 DW_AT_containing_type.
673bfd45
DE
22179 If there is no type substitute an error marker. */
22180
c906108c 22181static struct type *
ff39bb5e 22182lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22183 struct dwarf2_cu *cu)
c906108c 22184{
976ca316
SM
22185 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22186 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22187 struct type *this_type;
22188
ac9ec31b
DE
22189 gdb_assert (attr->name == DW_AT_type
22190 || attr->name == DW_AT_GNAT_descriptive_type
22191 || attr->name == DW_AT_containing_type);
22192
673bfd45
DE
22193 /* First see if we have it cached. */
22194
36586728
TT
22195 if (attr->form == DW_FORM_GNU_ref_alt)
22196 {
22197 struct dwarf2_per_cu_data *per_cu;
0826b30a 22198 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22199
976ca316
SM
22200 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22201 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22202 }
cd6c91b4 22203 else if (attr->form_is_ref ())
673bfd45 22204 {
0826b30a 22205 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22206
976ca316 22207 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22208 }
55f1336d 22209 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22210 {
630ed6b9 22211 ULONGEST signature = attr->as_signature ();
673bfd45 22212
ac9ec31b 22213 return get_signatured_type (die, signature, cu);
673bfd45
DE
22214 }
22215 else
22216 {
b98664d3 22217 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22218 " at %s [in module %s]"),
22219 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22220 objfile_name (objfile));
ac9ec31b 22221 return build_error_marker_type (cu, die);
673bfd45
DE
22222 }
22223
22224 /* If not cached we need to read it in. */
22225
22226 if (this_type == NULL)
22227 {
ac9ec31b 22228 struct die_info *type_die = NULL;
673bfd45
DE
22229 struct dwarf2_cu *type_cu = cu;
22230
cd6c91b4 22231 if (attr->form_is_ref ())
ac9ec31b
DE
22232 type_die = follow_die_ref (die, attr, &type_cu);
22233 if (type_die == NULL)
22234 return build_error_marker_type (cu, die);
22235 /* If we find the type now, it's probably because the type came
3019eac3
DE
22236 from an inter-CU reference and the type's CU got expanded before
22237 ours. */
ac9ec31b 22238 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22239 }
22240
22241 /* If we still don't have a type use an error marker. */
22242
22243 if (this_type == NULL)
ac9ec31b 22244 return build_error_marker_type (cu, die);
673bfd45 22245
f792889a 22246 return this_type;
c906108c
SS
22247}
22248
673bfd45
DE
22249/* Return the type in DIE, CU.
22250 Returns NULL for invalid types.
22251
02142a6c 22252 This first does a lookup in die_type_hash,
673bfd45
DE
22253 and only reads the die in if necessary.
22254
22255 NOTE: This can be called when reading in partial or full symbols. */
22256
f792889a 22257static struct type *
e7c27a73 22258read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22259{
f792889a
DJ
22260 struct type *this_type;
22261
22262 this_type = get_die_type (die, cu);
22263 if (this_type)
22264 return this_type;
22265
673bfd45
DE
22266 return read_type_die_1 (die, cu);
22267}
22268
22269/* Read the type in DIE, CU.
22270 Returns NULL for invalid types. */
22271
22272static struct type *
22273read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22274{
22275 struct type *this_type = NULL;
22276
c906108c
SS
22277 switch (die->tag)
22278 {
22279 case DW_TAG_class_type:
680b30c7 22280 case DW_TAG_interface_type:
c906108c
SS
22281 case DW_TAG_structure_type:
22282 case DW_TAG_union_type:
f792889a 22283 this_type = read_structure_type (die, cu);
c906108c
SS
22284 break;
22285 case DW_TAG_enumeration_type:
f792889a 22286 this_type = read_enumeration_type (die, cu);
c906108c
SS
22287 break;
22288 case DW_TAG_subprogram:
22289 case DW_TAG_subroutine_type:
edb3359d 22290 case DW_TAG_inlined_subroutine:
f792889a 22291 this_type = read_subroutine_type (die, cu);
c906108c
SS
22292 break;
22293 case DW_TAG_array_type:
f792889a 22294 this_type = read_array_type (die, cu);
c906108c 22295 break;
72019c9c 22296 case DW_TAG_set_type:
f792889a 22297 this_type = read_set_type (die, cu);
72019c9c 22298 break;
c906108c 22299 case DW_TAG_pointer_type:
f792889a 22300 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22301 break;
22302 case DW_TAG_ptr_to_member_type:
f792889a 22303 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22304 break;
22305 case DW_TAG_reference_type:
4297a3f0
AV
22306 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22307 break;
22308 case DW_TAG_rvalue_reference_type:
22309 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22310 break;
22311 case DW_TAG_const_type:
f792889a 22312 this_type = read_tag_const_type (die, cu);
c906108c
SS
22313 break;
22314 case DW_TAG_volatile_type:
f792889a 22315 this_type = read_tag_volatile_type (die, cu);
c906108c 22316 break;
06d66ee9
TT
22317 case DW_TAG_restrict_type:
22318 this_type = read_tag_restrict_type (die, cu);
22319 break;
c906108c 22320 case DW_TAG_string_type:
f792889a 22321 this_type = read_tag_string_type (die, cu);
c906108c
SS
22322 break;
22323 case DW_TAG_typedef:
f792889a 22324 this_type = read_typedef (die, cu);
c906108c 22325 break;
a02abb62 22326 case DW_TAG_subrange_type:
f792889a 22327 this_type = read_subrange_type (die, cu);
a02abb62 22328 break;
c906108c 22329 case DW_TAG_base_type:
f792889a 22330 this_type = read_base_type (die, cu);
c906108c 22331 break;
81a17f79 22332 case DW_TAG_unspecified_type:
f792889a 22333 this_type = read_unspecified_type (die, cu);
81a17f79 22334 break;
0114d602
DJ
22335 case DW_TAG_namespace:
22336 this_type = read_namespace_type (die, cu);
22337 break;
f55ee35c
JK
22338 case DW_TAG_module:
22339 this_type = read_module_type (die, cu);
22340 break;
a2c2acaf
MW
22341 case DW_TAG_atomic_type:
22342 this_type = read_tag_atomic_type (die, cu);
22343 break;
c906108c 22344 default:
b98664d3 22345 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22346 dwarf_tag_name (die->tag));
c906108c
SS
22347 break;
22348 }
63d06c5c 22349
f792889a 22350 return this_type;
63d06c5c
DC
22351}
22352
abc72ce4
DE
22353/* See if we can figure out if the class lives in a namespace. We do
22354 this by looking for a member function; its demangled name will
22355 contain namespace info, if there is any.
22356 Return the computed name or NULL.
22357 Space for the result is allocated on the objfile's obstack.
22358 This is the full-die version of guess_partial_die_structure_name.
22359 In this case we know DIE has no useful parent. */
22360
43816ebc 22361static const char *
abc72ce4
DE
22362guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22363{
22364 struct die_info *spec_die;
22365 struct dwarf2_cu *spec_cu;
22366 struct die_info *child;
5e22e966 22367 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22368
22369 spec_cu = cu;
22370 spec_die = die_specification (die, &spec_cu);
22371 if (spec_die != NULL)
22372 {
22373 die = spec_die;
22374 cu = spec_cu;
22375 }
22376
22377 for (child = die->child;
22378 child != NULL;
22379 child = child->sibling)
22380 {
22381 if (child->tag == DW_TAG_subprogram)
22382 {
73b9be8b 22383 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22384
7d45c7c3 22385 if (linkage_name != NULL)
abc72ce4 22386 {
43816ebc 22387 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22388 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22389 const char *name = NULL;
abc72ce4
DE
22390
22391 if (actual_name != NULL)
22392 {
15d034d0 22393 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22394
22395 if (die_name != NULL
43816ebc 22396 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22397 {
22398 /* Strip off the class name from the full name.
22399 We want the prefix. */
22400 int die_name_len = strlen (die_name);
43816ebc
TT
22401 int actual_name_len = strlen (actual_name.get ());
22402 const char *ptr = actual_name.get ();
abc72ce4
DE
22403
22404 /* Test for '::' as a sanity check. */
22405 if (actual_name_len > die_name_len + 2
43816ebc 22406 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22407 name = obstack_strndup (
e3b94546 22408 &objfile->per_bfd->storage_obstack,
43816ebc 22409 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22410 }
22411 }
abc72ce4
DE
22412 return name;
22413 }
22414 }
22415 }
22416
22417 return NULL;
22418}
22419
96408a79
SA
22420/* GCC might emit a nameless typedef that has a linkage name. Determine the
22421 prefix part in such case. See
22422 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22423
a121b7c1 22424static const char *
96408a79
SA
22425anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22426{
22427 struct attribute *attr;
e6a959d6 22428 const char *base;
96408a79
SA
22429
22430 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22431 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22432 return NULL;
22433
7d45c7c3 22434 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22435 return NULL;
22436
73b9be8b 22437 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22438 const char *attr_name = attr->as_string ();
22439 if (attr == NULL || attr_name == NULL)
96408a79
SA
22440 return NULL;
22441
22442 /* dwarf2_name had to be already called. */
3b64bf15 22443 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22444
22445 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22446 base = strrchr (attr_name, ':');
22447 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22448 return "";
22449
5e22e966 22450 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22451 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22452 attr_name,
22453 &base[-1] - attr_name);
96408a79
SA
22454}
22455
fdde2d81 22456/* Return the name of the namespace/class that DIE is defined within,
0114d602 22457 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22458
0114d602
DJ
22459 For example, if we're within the method foo() in the following
22460 code:
22461
22462 namespace N {
22463 class C {
22464 void foo () {
22465 }
22466 };
22467 }
22468
22469 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22470
0d5cff50 22471static const char *
e142c38c 22472determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22473{
976ca316 22474 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
22475 struct die_info *parent, *spec_die;
22476 struct dwarf2_cu *spec_cu;
22477 struct type *parent_type;
a121b7c1 22478 const char *retval;
63d06c5c 22479
9c37b5ae 22480 if (cu->language != language_cplus
c44af4eb
TT
22481 && cu->language != language_fortran && cu->language != language_d
22482 && cu->language != language_rust)
0114d602
DJ
22483 return "";
22484
96408a79
SA
22485 retval = anonymous_struct_prefix (die, cu);
22486 if (retval)
22487 return retval;
22488
0114d602
DJ
22489 /* We have to be careful in the presence of DW_AT_specification.
22490 For example, with GCC 3.4, given the code
22491
22492 namespace N {
22493 void foo() {
22494 // Definition of N::foo.
22495 }
22496 }
22497
22498 then we'll have a tree of DIEs like this:
22499
22500 1: DW_TAG_compile_unit
22501 2: DW_TAG_namespace // N
22502 3: DW_TAG_subprogram // declaration of N::foo
22503 4: DW_TAG_subprogram // definition of N::foo
22504 DW_AT_specification // refers to die #3
22505
22506 Thus, when processing die #4, we have to pretend that we're in
22507 the context of its DW_AT_specification, namely the contex of die
22508 #3. */
22509 spec_cu = cu;
22510 spec_die = die_specification (die, &spec_cu);
22511 if (spec_die == NULL)
22512 parent = die->parent;
22513 else
63d06c5c 22514 {
0114d602
DJ
22515 parent = spec_die->parent;
22516 cu = spec_cu;
63d06c5c 22517 }
0114d602
DJ
22518
22519 if (parent == NULL)
22520 return "";
98bfdba5
PA
22521 else if (parent->building_fullname)
22522 {
22523 const char *name;
22524 const char *parent_name;
22525
22526 /* It has been seen on RealView 2.2 built binaries,
22527 DW_TAG_template_type_param types actually _defined_ as
22528 children of the parent class:
22529
22530 enum E {};
22531 template class <class Enum> Class{};
22532 Class<enum E> class_e;
22533
dda83cd7
SM
22534 1: DW_TAG_class_type (Class)
22535 2: DW_TAG_enumeration_type (E)
22536 3: DW_TAG_enumerator (enum1:0)
22537 3: DW_TAG_enumerator (enum2:1)
22538 ...
22539 2: DW_TAG_template_type_param
22540 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
22541
22542 Besides being broken debug info, it can put GDB into an
22543 infinite loop. Consider:
22544
22545 When we're building the full name for Class<E>, we'll start
22546 at Class, and go look over its template type parameters,
22547 finding E. We'll then try to build the full name of E, and
22548 reach here. We're now trying to build the full name of E,
22549 and look over the parent DIE for containing scope. In the
22550 broken case, if we followed the parent DIE of E, we'd again
22551 find Class, and once again go look at its template type
22552 arguments, etc., etc. Simply don't consider such parent die
22553 as source-level parent of this die (it can't be, the language
22554 doesn't allow it), and break the loop here. */
22555 name = dwarf2_name (die, cu);
22556 parent_name = dwarf2_name (parent, cu);
b98664d3 22557 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22558 name ? name : "<unknown>",
22559 parent_name ? parent_name : "<unknown>");
22560 return "";
22561 }
63d06c5c 22562 else
0114d602
DJ
22563 switch (parent->tag)
22564 {
63d06c5c 22565 case DW_TAG_namespace:
0114d602 22566 parent_type = read_type_die (parent, cu);
acebe513
UW
22567 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22568 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22569 Work around this problem here. */
22570 if (cu->language == language_cplus
7d93a1e0 22571 && strcmp (parent_type->name (), "::") == 0)
acebe513 22572 return "";
0114d602 22573 /* We give a name to even anonymous namespaces. */
7d93a1e0 22574 return parent_type->name ();
63d06c5c 22575 case DW_TAG_class_type:
680b30c7 22576 case DW_TAG_interface_type:
63d06c5c 22577 case DW_TAG_structure_type:
0114d602 22578 case DW_TAG_union_type:
f55ee35c 22579 case DW_TAG_module:
0114d602 22580 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
22581 if (parent_type->name () != NULL)
22582 return parent_type->name ();
0114d602
DJ
22583 else
22584 /* An anonymous structure is only allowed non-static data
22585 members; no typedefs, no member functions, et cetera.
22586 So it does not need a prefix. */
22587 return "";
abc72ce4 22588 case DW_TAG_compile_unit:
95554aad 22589 case DW_TAG_partial_unit:
abc72ce4
DE
22590 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22591 if (cu->language == language_cplus
976ca316 22592 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
22593 && die->child != NULL
22594 && (die->tag == DW_TAG_class_type
22595 || die->tag == DW_TAG_structure_type
22596 || die->tag == DW_TAG_union_type))
22597 {
43816ebc 22598 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22599 if (name != NULL)
22600 return name;
22601 }
22602 return "";
0a4b0913
AB
22603 case DW_TAG_subprogram:
22604 /* Nested subroutines in Fortran get a prefix with the name
22605 of the parent's subroutine. */
22606 if (cu->language == language_fortran)
22607 {
22608 if ((die->tag == DW_TAG_subprogram)
22609 && (dwarf2_name (parent, cu) != NULL))
22610 return dwarf2_name (parent, cu);
22611 }
22612 return determine_prefix (parent, cu);
3d567982
TT
22613 case DW_TAG_enumeration_type:
22614 parent_type = read_type_die (parent, cu);
3bc440a2 22615 if (parent_type->is_declared_class ())
3d567982 22616 {
7d93a1e0
SM
22617 if (parent_type->name () != NULL)
22618 return parent_type->name ();
3d567982
TT
22619 return "";
22620 }
22621 /* Fall through. */
63d06c5c 22622 default:
8176b9b8 22623 return determine_prefix (parent, cu);
63d06c5c 22624 }
63d06c5c
DC
22625}
22626
3e43a32a
MS
22627/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22628 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22629 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22630 an obconcat, otherwise allocate storage for the result. The CU argument is
22631 used to determine the language and hence, the appropriate separator. */
987504bb 22632
f55ee35c 22633#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22634
22635static char *
f55ee35c 22636typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 22637 int physname, struct dwarf2_cu *cu)
63d06c5c 22638{
f55ee35c 22639 const char *lead = "";
5c315b68 22640 const char *sep;
63d06c5c 22641
3e43a32a
MS
22642 if (suffix == NULL || suffix[0] == '\0'
22643 || prefix == NULL || prefix[0] == '\0')
987504bb 22644 sep = "";
45280282
IB
22645 else if (cu->language == language_d)
22646 {
22647 /* For D, the 'main' function could be defined in any module, but it
22648 should never be prefixed. */
22649 if (strcmp (suffix, "D main") == 0)
22650 {
22651 prefix = "";
22652 sep = "";
22653 }
22654 else
22655 sep = ".";
22656 }
f55ee35c
JK
22657 else if (cu->language == language_fortran && physname)
22658 {
22659 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22660 DW_AT_MIPS_linkage_name is preferred and used instead. */
22661
22662 lead = "__";
22663 sep = "_MOD_";
22664 }
987504bb
JJ
22665 else
22666 sep = "::";
63d06c5c 22667
6dd47d34
DE
22668 if (prefix == NULL)
22669 prefix = "";
22670 if (suffix == NULL)
22671 suffix = "";
22672
987504bb
JJ
22673 if (obs == NULL)
22674 {
3e43a32a 22675 char *retval
224c3ddb
SM
22676 = ((char *)
22677 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22678
f55ee35c
JK
22679 strcpy (retval, lead);
22680 strcat (retval, prefix);
6dd47d34
DE
22681 strcat (retval, sep);
22682 strcat (retval, suffix);
63d06c5c
DC
22683 return retval;
22684 }
987504bb
JJ
22685 else
22686 {
22687 /* We have an obstack. */
f55ee35c 22688 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22689 }
63d06c5c
DC
22690}
22691
71c25dea
TT
22692/* Get name of a die, return NULL if not found. */
22693
15d034d0
TT
22694static const char *
22695dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 22696 struct objfile *objfile)
71c25dea
TT
22697{
22698 if (name && cu->language == language_cplus)
22699 {
596dc4ad
TT
22700 gdb::unique_xmalloc_ptr<char> canon_name
22701 = cp_canonicalize_string (name);
71c25dea 22702
596dc4ad
TT
22703 if (canon_name != nullptr)
22704 name = objfile->intern (canon_name.get ());
71c25dea
TT
22705 }
22706
22707 return name;
c906108c
SS
22708}
22709
96553a0c
DE
22710/* Get name of a die, return NULL if not found.
22711 Anonymous namespaces are converted to their magic string. */
9219021c 22712
15d034d0 22713static const char *
e142c38c 22714dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22715{
22716 struct attribute *attr;
5e22e966 22717 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 22718
e142c38c 22719 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
22720 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22721 if (attr_name == nullptr
96553a0c 22722 && die->tag != DW_TAG_namespace
53832f31
TT
22723 && die->tag != DW_TAG_class_type
22724 && die->tag != DW_TAG_interface_type
22725 && die->tag != DW_TAG_structure_type
22726 && die->tag != DW_TAG_union_type)
71c25dea
TT
22727 return NULL;
22728
22729 switch (die->tag)
22730 {
22731 case DW_TAG_compile_unit:
95554aad 22732 case DW_TAG_partial_unit:
71c25dea
TT
22733 /* Compilation units have a DW_AT_name that is a filename, not
22734 a source language identifier. */
22735 case DW_TAG_enumeration_type:
22736 case DW_TAG_enumerator:
22737 /* These tags always have simple identifiers already; no need
22738 to canonicalize them. */
2c830f54 22739 return attr_name;
907af001 22740
96553a0c 22741 case DW_TAG_namespace:
2c830f54
TT
22742 if (attr_name != nullptr)
22743 return attr_name;
96553a0c
DE
22744 return CP_ANONYMOUS_NAMESPACE_STR;
22745
907af001
UW
22746 case DW_TAG_class_type:
22747 case DW_TAG_interface_type:
22748 case DW_TAG_structure_type:
22749 case DW_TAG_union_type:
22750 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22751 structures or unions. These were of the form "._%d" in GCC 4.1,
22752 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22753 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
22754 if (attr_name != nullptr
22755 && (startswith (attr_name, "._")
22756 || startswith (attr_name, "<anonymous")))
907af001 22757 return NULL;
53832f31
TT
22758
22759 /* GCC might emit a nameless typedef that has a linkage name. See
22760 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 22761 if (!attr || attr_name == NULL)
53832f31 22762 {
73b9be8b 22763 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 22764 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 22765 if (attr == NULL || attr_name == NULL)
53832f31
TT
22766 return NULL;
22767
2c830f54 22768 /* Avoid demangling attr_name the second time on a second
df5c6c50 22769 call for the same DIE. */
3b64bf15 22770 if (!attr->canonical_string_p ())
53832f31 22771 {
43816ebc 22772 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 22773 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
22774 if (demangled == nullptr)
22775 return nullptr;
43816ebc 22776
c6481205 22777 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 22778 attr_name = attr->as_string ();
53832f31 22779 }
67430cd0 22780
2c830f54
TT
22781 /* Strip any leading namespaces/classes, keep only the
22782 base name. DW_AT_name for named DIEs does not
22783 contain the prefixes. */
22784 const char *base = strrchr (attr_name, ':');
22785 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
22786 return &base[1];
22787 else
2c830f54 22788 return attr_name;
53832f31 22789 }
907af001
UW
22790 break;
22791
71c25dea 22792 default:
907af001
UW
22793 break;
22794 }
22795
3b64bf15 22796 if (!attr->canonical_string_p ())
c6481205
TT
22797 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22798 objfile));
2c830f54 22799 return attr->as_string ();
9219021c
DC
22800}
22801
22802/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22803 is none. *EXT_CU is the CU containing DIE on input, and the CU
22804 containing the return value on output. */
9219021c
DC
22805
22806static struct die_info *
f2f0e013 22807dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22808{
22809 struct attribute *attr;
9219021c 22810
f2f0e013 22811 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22812 if (attr == NULL)
22813 return NULL;
22814
f2f0e013 22815 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22816}
22817
f9aca02d 22818static void
d97bc12b 22819dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22820{
22821 unsigned int i;
22822
d97bc12b 22823 print_spaces (indent, f);
9d8780f0 22824 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22825 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22826 sect_offset_str (die->sect_off));
d97bc12b
DE
22827
22828 if (die->parent != NULL)
22829 {
22830 print_spaces (indent, f);
9d8780f0
SM
22831 fprintf_unfiltered (f, " parent at offset: %s\n",
22832 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22833 }
22834
22835 print_spaces (indent, f);
22836 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22837 dwarf_bool_name (die->child != NULL));
c906108c 22838
d97bc12b
DE
22839 print_spaces (indent, f);
22840 fprintf_unfiltered (f, " attributes:\n");
22841
c906108c
SS
22842 for (i = 0; i < die->num_attrs; ++i)
22843 {
d97bc12b
DE
22844 print_spaces (indent, f);
22845 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22846 dwarf_attr_name (die->attrs[i].name),
22847 dwarf_form_name (die->attrs[i].form));
d97bc12b 22848
c906108c
SS
22849 switch (die->attrs[i].form)
22850 {
c906108c 22851 case DW_FORM_addr:
336d760d 22852 case DW_FORM_addrx:
3019eac3 22853 case DW_FORM_GNU_addr_index:
d97bc12b 22854 fprintf_unfiltered (f, "address: ");
36d378cf 22855 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
22856 break;
22857 case DW_FORM_block2:
22858 case DW_FORM_block4:
22859 case DW_FORM_block:
22860 case DW_FORM_block1:
56eb65bd 22861 fprintf_unfiltered (f, "block: size %s",
9d2246fc 22862 pulongest (die->attrs[i].as_block ()->size));
c906108c 22863 break;
2dc7f7b3 22864 case DW_FORM_exprloc:
56eb65bd 22865 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 22866 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 22867 break;
0224619f
JK
22868 case DW_FORM_data16:
22869 fprintf_unfiltered (f, "constant of 16 bytes");
22870 break;
4568ecf9
DE
22871 case DW_FORM_ref_addr:
22872 fprintf_unfiltered (f, "ref address: ");
529908cb 22873 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 22874 break;
36586728
TT
22875 case DW_FORM_GNU_ref_alt:
22876 fprintf_unfiltered (f, "alt ref address: ");
529908cb 22877 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 22878 break;
10b3939b
DJ
22879 case DW_FORM_ref1:
22880 case DW_FORM_ref2:
22881 case DW_FORM_ref4:
4568ecf9
DE
22882 case DW_FORM_ref8:
22883 case DW_FORM_ref_udata:
d97bc12b 22884 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 22885 (long) (die->attrs[i].as_unsigned ()));
10b3939b 22886 break;
c906108c
SS
22887 case DW_FORM_data1:
22888 case DW_FORM_data2:
22889 case DW_FORM_data4:
ce5d95e1 22890 case DW_FORM_data8:
c906108c 22891 case DW_FORM_udata:
43bbcdc2 22892 fprintf_unfiltered (f, "constant: %s",
529908cb 22893 pulongest (die->attrs[i].as_unsigned ()));
c906108c 22894 break;
2dc7f7b3
TT
22895 case DW_FORM_sec_offset:
22896 fprintf_unfiltered (f, "section offset: %s",
529908cb 22897 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 22898 break;
55f1336d 22899 case DW_FORM_ref_sig8:
ac9ec31b 22900 fprintf_unfiltered (f, "signature: %s",
630ed6b9 22901 hex_string (die->attrs[i].as_signature ()));
348e048f 22902 break;
c906108c 22903 case DW_FORM_string:
4bdf3d34 22904 case DW_FORM_strp:
43988095 22905 case DW_FORM_line_strp:
cf532bd1 22906 case DW_FORM_strx:
3019eac3 22907 case DW_FORM_GNU_str_index:
36586728 22908 case DW_FORM_GNU_strp_alt:
8285870a 22909 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
22910 die->attrs[i].as_string ()
22911 ? die->attrs[i].as_string () : "",
22912 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
22913 break;
22914 case DW_FORM_flag:
c45bc3f8 22915 if (die->attrs[i].as_boolean ())
d97bc12b 22916 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22917 else
d97bc12b 22918 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22919 break;
2dc7f7b3
TT
22920 case DW_FORM_flag_present:
22921 fprintf_unfiltered (f, "flag: TRUE");
22922 break;
a8329558 22923 case DW_FORM_indirect:
0963b4bd
MS
22924 /* The reader will have reduced the indirect form to
22925 the "base form" so this form should not occur. */
5f48f8f3 22926 fprintf_unfiltered (f,
3e43a32a 22927 "unexpected attribute form: DW_FORM_indirect");
a8329558 22928 break;
1bc397c5 22929 case DW_FORM_sdata:
663c44ac
JK
22930 case DW_FORM_implicit_const:
22931 fprintf_unfiltered (f, "constant: %s",
1bc397c5 22932 plongest (die->attrs[i].as_signed ()));
663c44ac 22933 break;
c906108c 22934 default:
d97bc12b 22935 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22936 die->attrs[i].form);
d97bc12b 22937 break;
c906108c 22938 }
d97bc12b 22939 fprintf_unfiltered (f, "\n");
c906108c
SS
22940 }
22941}
22942
f9aca02d 22943static void
d97bc12b 22944dump_die_for_error (struct die_info *die)
c906108c 22945{
d97bc12b
DE
22946 dump_die_shallow (gdb_stderr, 0, die);
22947}
22948
22949static void
22950dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22951{
22952 int indent = level * 4;
22953
22954 gdb_assert (die != NULL);
22955
22956 if (level >= max_level)
22957 return;
22958
22959 dump_die_shallow (f, indent, die);
22960
22961 if (die->child != NULL)
c906108c 22962 {
d97bc12b
DE
22963 print_spaces (indent, f);
22964 fprintf_unfiltered (f, " Children:");
22965 if (level + 1 < max_level)
22966 {
22967 fprintf_unfiltered (f, "\n");
22968 dump_die_1 (f, level + 1, max_level, die->child);
22969 }
22970 else
22971 {
3e43a32a
MS
22972 fprintf_unfiltered (f,
22973 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22974 }
22975 }
22976
22977 if (die->sibling != NULL && level > 0)
22978 {
22979 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22980 }
22981}
22982
d97bc12b
DE
22983/* This is called from the pdie macro in gdbinit.in.
22984 It's not static so gcc will keep a copy callable from gdb. */
22985
22986void
22987dump_die (struct die_info *die, int max_level)
22988{
22989 dump_die_1 (gdb_stdlog, 0, max_level, die);
22990}
22991
f9aca02d 22992static void
51545339 22993store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22994{
51545339 22995 void **slot;
c906108c 22996
9c541725
PA
22997 slot = htab_find_slot_with_hash (cu->die_hash, die,
22998 to_underlying (die->sect_off),
b64f50a1 22999 INSERT);
51545339
DJ
23000
23001 *slot = die;
c906108c
SS
23002}
23003
348e048f
DE
23004/* Follow reference or signature attribute ATTR of SRC_DIE.
23005 On entry *REF_CU is the CU of SRC_DIE.
23006 On exit *REF_CU is the CU of the result. */
23007
23008static struct die_info *
ff39bb5e 23009follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23010 struct dwarf2_cu **ref_cu)
23011{
23012 struct die_info *die;
23013
cd6c91b4 23014 if (attr->form_is_ref ())
348e048f 23015 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23016 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23017 die = follow_die_sig (src_die, attr, ref_cu);
23018 else
23019 {
23020 dump_die_for_error (src_die);
23021 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23022 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23023 }
23024
23025 return die;
03dd20cc
DJ
23026}
23027
5c631832 23028/* Follow reference OFFSET.
673bfd45
DE
23029 On entry *REF_CU is the CU of the source die referencing OFFSET.
23030 On exit *REF_CU is the CU of the result.
23031 Returns NULL if OFFSET is invalid. */
f504f079 23032
f9aca02d 23033static struct die_info *
9c541725 23034follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23035 struct dwarf2_cu **ref_cu)
c906108c 23036{
10b3939b 23037 struct die_info temp_die;
f2f0e013 23038 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23039 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23040
348e048f
DE
23041 gdb_assert (cu->per_cu != NULL);
23042
98bfdba5
PA
23043 target_cu = cu;
23044
17e593e9
SM
23045 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23046 "source CU contains target offset: %d",
23047 sect_offset_str (cu->per_cu->sect_off),
23048 sect_offset_str (sect_off),
23049 cu->header.offset_in_cu_p (sect_off));
23050
3019eac3 23051 if (cu->per_cu->is_debug_types)
348e048f
DE
23052 {
23053 /* .debug_types CUs cannot reference anything outside their CU.
23054 If they need to, they have to reference a signatured type via
55f1336d 23055 DW_FORM_ref_sig8. */
4057dfde 23056 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23057 return NULL;
348e048f 23058 }
36586728 23059 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23060 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23061 {
23062 struct dwarf2_per_cu_data *per_cu;
9a619af0 23063
9c541725 23064 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23065 per_objfile);
03dd20cc 23066
17e593e9
SM
23067 dwarf_read_debug_printf_v ("target CU offset: %s, "
23068 "target CU DIEs loaded: %d",
23069 sect_offset_str (per_cu->sect_off),
23070 per_objfile->get_cu (per_cu) != nullptr);
23071
616c069a
SM
23072 /* If necessary, add it to the queue and load its DIEs.
23073
23074 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23075 it doesn't mean they are currently loaded. Since we require them
23076 to be loaded, we must check for ourselves. */
23077 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23078 || per_objfile->get_cu (per_cu) == nullptr)
4a636814
SM
23079 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23080 false, cu->language);
03dd20cc 23081
976ca316 23082 target_cu = per_objfile->get_cu (per_cu);
616c069a 23083 gdb_assert (target_cu != nullptr);
10b3939b 23084 }
98bfdba5
PA
23085 else if (cu->dies == NULL)
23086 {
23087 /* We're loading full DIEs during partial symbol reading. */
976ca316 23088 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23089 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23090 language_minimal);
98bfdba5 23091 }
c906108c 23092
f2f0e013 23093 *ref_cu = target_cu;
9c541725 23094 temp_die.sect_off = sect_off;
c24bdb02 23095
3db19b2d
TV
23096 if (target_cu != cu)
23097 target_cu->ancestor = cu;
23098
9a3c8263 23099 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23100 &temp_die,
23101 to_underlying (sect_off));
5c631832 23102}
10b3939b 23103
5c631832
JK
23104/* Follow reference attribute ATTR of SRC_DIE.
23105 On entry *REF_CU is the CU of SRC_DIE.
23106 On exit *REF_CU is the CU of the result. */
23107
23108static struct die_info *
ff39bb5e 23109follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23110 struct dwarf2_cu **ref_cu)
23111{
0826b30a 23112 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23113 struct dwarf2_cu *cu = *ref_cu;
23114 struct die_info *die;
23115
9c541725 23116 die = follow_die_offset (sect_off,
36586728
TT
23117 (attr->form == DW_FORM_GNU_ref_alt
23118 || cu->per_cu->is_dwz),
23119 ref_cu);
5c631832 23120 if (!die)
9d8780f0
SM
23121 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23122 "at %s [in module %s]"),
23123 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23124 objfile_name (cu->per_objfile->objfile));
348e048f 23125
5c631832
JK
23126 return die;
23127}
23128
d4c9a4f8 23129/* See read.h. */
5c631832
JK
23130
23131struct dwarf2_locexpr_baton
9c541725 23132dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23133 dwarf2_per_cu_data *per_cu,
976ca316 23134 dwarf2_per_objfile *per_objfile,
041d9819
SM
23135 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23136 bool resolve_abstract_p)
5c631832 23137{
5c631832
JK
23138 struct die_info *die;
23139 struct attribute *attr;
23140 struct dwarf2_locexpr_baton retval;
976ca316 23141 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23142
976ca316 23143 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23144 if (cu == nullptr)
976ca316 23145 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23146
23147 if (cu == nullptr)
cc12ce38
DE
23148 {
23149 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23150 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23151 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23152 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23153 }
918dd910 23154
9c541725 23155 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23156 if (!die)
9d8780f0
SM
23157 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23158 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23159
23160 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23161 if (!attr && resolve_abstract_p
976ca316
SM
23162 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23163 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23164 {
041d9819 23165 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23166 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23167 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23168
3360b6e7 23169 for (const auto &cand_off
976ca316 23170 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23171 {
3360b6e7
TV
23172 struct dwarf2_cu *cand_cu = cu;
23173 struct die_info *cand
23174 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23175 if (!cand
23176 || !cand->parent
e4a62c65
TV
23177 || cand->parent->tag != DW_TAG_subprogram)
23178 continue;
23179
23180 CORE_ADDR pc_low, pc_high;
23181 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23182 if (pc_low == ((CORE_ADDR) -1))
23183 continue;
23184 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23185 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23186 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23187 continue;
23188
23189 die = cand;
23190 attr = dwarf2_attr (die, DW_AT_location, cu);
23191 break;
23192 }
23193 }
23194
5c631832
JK
23195 if (!attr)
23196 {
e103e986
JK
23197 /* DWARF: "If there is no such attribute, then there is no effect.".
23198 DATA is ignored if SIZE is 0. */
5c631832 23199
e103e986 23200 retval.data = NULL;
5c631832
JK
23201 retval.size = 0;
23202 }
cd6c91b4 23203 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23204 {
23205 struct dwarf2_loclist_baton loclist_baton;
041d9819 23206 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23207 size_t size;
23208
23209 fill_in_loclist_baton (cu, &loclist_baton, attr);
23210
23211 retval.data = dwarf2_find_location_expression (&loclist_baton,
23212 &size, pc);
23213 retval.size = size;
23214 }
5c631832
JK
23215 else
23216 {
4fc6c0d5 23217 if (!attr->form_is_block ())
9d8780f0 23218 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23219 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23220 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23221
9d2246fc
TT
23222 struct dwarf_block *block = attr->as_block ();
23223 retval.data = block->data;
23224 retval.size = block->size;
5c631832 23225 }
976ca316 23226 retval.per_objfile = per_objfile;
5c631832 23227 retval.per_cu = cu->per_cu;
918dd910 23228
976ca316 23229 per_objfile->age_comp_units ();
918dd910 23230
5c631832 23231 return retval;
348e048f
DE
23232}
23233
d4c9a4f8 23234/* See read.h. */
8b9737bf
TT
23235
23236struct dwarf2_locexpr_baton
23237dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23238 dwarf2_per_cu_data *per_cu,
14095eb3 23239 dwarf2_per_objfile *per_objfile,
041d9819 23240 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23241{
9c541725 23242 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23243
14095eb3 23244 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23245 get_frame_pc);
8b9737bf
TT
23246}
23247
b6807d98
TT
23248/* Write a constant of a given type as target-ordered bytes into
23249 OBSTACK. */
23250
23251static const gdb_byte *
23252write_constant_as_bytes (struct obstack *obstack,
23253 enum bfd_endian byte_order,
23254 struct type *type,
23255 ULONGEST value,
23256 LONGEST *len)
23257{
23258 gdb_byte *result;
23259
23260 *len = TYPE_LENGTH (type);
224c3ddb 23261 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23262 store_unsigned_integer (result, *len, byte_order, value);
23263
23264 return result;
23265}
23266
d4c9a4f8 23267/* See read.h. */
b6807d98
TT
23268
23269const gdb_byte *
9c541725 23270dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23271 dwarf2_per_cu_data *per_cu,
14095eb3 23272 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23273 obstack *obstack,
b6807d98
TT
23274 LONGEST *len)
23275{
b6807d98
TT
23276 struct die_info *die;
23277 struct attribute *attr;
23278 const gdb_byte *result = NULL;
23279 struct type *type;
23280 LONGEST value;
23281 enum bfd_endian byte_order;
14095eb3 23282 struct objfile *objfile = per_objfile->objfile;
b6807d98 23283
7188ed02 23284 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23285 if (cu == nullptr)
23286 cu = load_cu (per_cu, per_objfile, false);
23287
23288 if (cu == nullptr)
cc12ce38
DE
23289 {
23290 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23291 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23292 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23293 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23294 }
b6807d98 23295
9c541725 23296 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23297 if (!die)
9d8780f0
SM
23298 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23299 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23300
23301 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23302 if (attr == NULL)
23303 return NULL;
23304
e3b94546 23305 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23306 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23307
23308 switch (attr->form)
23309 {
23310 case DW_FORM_addr:
336d760d 23311 case DW_FORM_addrx:
b6807d98
TT
23312 case DW_FORM_GNU_addr_index:
23313 {
23314 gdb_byte *tem;
23315
23316 *len = cu->header.addr_size;
224c3ddb 23317 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23318 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23319 result = tem;
23320 }
23321 break;
23322 case DW_FORM_string:
23323 case DW_FORM_strp:
cf532bd1 23324 case DW_FORM_strx:
b6807d98
TT
23325 case DW_FORM_GNU_str_index:
23326 case DW_FORM_GNU_strp_alt:
c6481205 23327 /* The string is already allocated on the objfile obstack, point
b6807d98 23328 directly to it. */
2c830f54
TT
23329 {
23330 const char *attr_name = attr->as_string ();
23331 result = (const gdb_byte *) attr_name;
23332 *len = strlen (attr_name);
23333 }
b6807d98
TT
23334 break;
23335 case DW_FORM_block1:
23336 case DW_FORM_block2:
23337 case DW_FORM_block4:
23338 case DW_FORM_block:
23339 case DW_FORM_exprloc:
0224619f 23340 case DW_FORM_data16:
9d2246fc
TT
23341 {
23342 struct dwarf_block *block = attr->as_block ();
23343 result = block->data;
23344 *len = block->size;
23345 }
b6807d98
TT
23346 break;
23347
23348 /* The DW_AT_const_value attributes are supposed to carry the
23349 symbol's value "represented as it would be on the target
23350 architecture." By the time we get here, it's already been
23351 converted to host endianness, so we just need to sign- or
23352 zero-extend it as appropriate. */
23353 case DW_FORM_data1:
23354 type = die_type (die, cu);
23355 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23356 if (result == NULL)
23357 result = write_constant_as_bytes (obstack, byte_order,
23358 type, value, len);
23359 break;
23360 case DW_FORM_data2:
23361 type = die_type (die, cu);
23362 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23363 if (result == NULL)
23364 result = write_constant_as_bytes (obstack, byte_order,
23365 type, value, len);
23366 break;
23367 case DW_FORM_data4:
23368 type = die_type (die, cu);
23369 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23370 if (result == NULL)
23371 result = write_constant_as_bytes (obstack, byte_order,
23372 type, value, len);
23373 break;
23374 case DW_FORM_data8:
23375 type = die_type (die, cu);
23376 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23377 if (result == NULL)
23378 result = write_constant_as_bytes (obstack, byte_order,
23379 type, value, len);
23380 break;
23381
23382 case DW_FORM_sdata:
663c44ac 23383 case DW_FORM_implicit_const:
b6807d98
TT
23384 type = die_type (die, cu);
23385 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23386 type, attr->as_signed (), len);
b6807d98
TT
23387 break;
23388
23389 case DW_FORM_udata:
23390 type = die_type (die, cu);
23391 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23392 type, attr->as_unsigned (), len);
b6807d98
TT
23393 break;
23394
23395 default:
b98664d3 23396 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23397 dwarf_form_name (attr->form));
23398 break;
23399 }
23400
23401 return result;
23402}
23403
d4c9a4f8 23404/* See read.h. */
7942e96e
AA
23405
23406struct type *
9c541725 23407dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
23408 dwarf2_per_cu_data *per_cu,
23409 dwarf2_per_objfile *per_objfile)
7942e96e 23410{
7942e96e
AA
23411 struct die_info *die;
23412
7188ed02 23413 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23414 if (cu == nullptr)
23415 cu = load_cu (per_cu, per_objfile, false);
23416
23417 if (cu == nullptr)
23418 return nullptr;
7942e96e 23419
9c541725 23420 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23421 if (!die)
23422 return NULL;
23423
23424 return die_type (die, cu);
23425}
23426
8cb5117c 23427/* See read.h. */
8a9b8146
TT
23428
23429struct type *
b64f50a1 23430dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23431 dwarf2_per_cu_data *per_cu,
23432 dwarf2_per_objfile *per_objfile)
8a9b8146 23433{
9c541725 23434 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23435 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23436}
23437
ac9ec31b 23438/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23439 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23440 On exit *REF_CU is the CU of the result.
23441 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23442
23443static struct die_info *
ac9ec31b
DE
23444follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23445 struct dwarf2_cu **ref_cu)
348e048f 23446{
348e048f 23447 struct die_info temp_die;
3db19b2d 23448 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 23449 struct die_info *die;
976ca316 23450 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23451
348e048f 23452
ac9ec31b
DE
23453 /* While it might be nice to assert sig_type->type == NULL here,
23454 we can get here for DW_AT_imported_declaration where we need
23455 the DIE not the type. */
348e048f 23456
616c069a 23457 /* If necessary, add it to the queue and load its DIEs.
348e048f 23458
616c069a
SM
23459 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23460 it doesn't mean they are currently loaded. Since we require them
23461 to be loaded, we must check for ourselves. */
0d305d5c 23462 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
616c069a 23463 language_minimal)
0d305d5c 23464 || per_objfile->get_cu (sig_type) == nullptr)
976ca316 23465 read_signatured_type (sig_type, per_objfile);
348e048f 23466
0d305d5c 23467 sig_cu = per_objfile->get_cu (sig_type);
69d751e3 23468 gdb_assert (sig_cu != NULL);
9c541725
PA
23469 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23470 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23471 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23472 to_underlying (temp_die.sect_off));
348e048f
DE
23473 if (die)
23474 {
796a7ff8
DE
23475 /* For .gdb_index version 7 keep track of included TUs.
23476 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
23477 if (per_objfile->per_bfd->index_table != NULL
23478 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 23479 {
ae640021 23480 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23481 }
23482
348e048f 23483 *ref_cu = sig_cu;
3db19b2d
TV
23484 if (sig_cu != cu)
23485 sig_cu->ancestor = cu;
c24bdb02 23486
348e048f
DE
23487 return die;
23488 }
23489
ac9ec31b
DE
23490 return NULL;
23491}
23492
23493/* Follow signatured type referenced by ATTR in SRC_DIE.
23494 On entry *REF_CU is the CU of SRC_DIE.
23495 On exit *REF_CU is the CU of the result.
23496 The result is the DIE of the type.
23497 If the referenced type cannot be found an error is thrown. */
23498
23499static struct die_info *
ff39bb5e 23500follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23501 struct dwarf2_cu **ref_cu)
23502{
630ed6b9 23503 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
23504 struct signatured_type *sig_type;
23505 struct die_info *die;
23506
23507 gdb_assert (attr->form == DW_FORM_ref_sig8);
23508
a2ce51a0 23509 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23510 /* sig_type will be NULL if the signatured type is missing from
23511 the debug info. */
23512 if (sig_type == NULL)
23513 {
23514 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
23515 " from DIE at %s [in module %s]"),
23516 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23517 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23518 }
23519
23520 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23521 if (die == NULL)
23522 {
23523 dump_die_for_error (src_die);
23524 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23525 " from DIE at %s [in module %s]"),
23526 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23527 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23528 }
23529
23530 return die;
23531}
23532
23533/* Get the type specified by SIGNATURE referenced in DIE/CU,
23534 reading in and processing the type unit if necessary. */
23535
23536static struct type *
23537get_signatured_type (struct die_info *die, ULONGEST signature,
23538 struct dwarf2_cu *cu)
23539{
976ca316 23540 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
23541 struct signatured_type *sig_type;
23542 struct dwarf2_cu *type_cu;
23543 struct die_info *type_die;
23544 struct type *type;
23545
a2ce51a0 23546 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23547 /* sig_type will be NULL if the signatured type is missing from
23548 the debug info. */
23549 if (sig_type == NULL)
23550 {
b98664d3 23551 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23552 " from DIE at %s [in module %s]"),
23553 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23554 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23555 return build_error_marker_type (cu, die);
23556 }
23557
23558 /* If we already know the type we're done. */
976ca316 23559 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
23560 if (type != nullptr)
23561 return type;
ac9ec31b
DE
23562
23563 type_cu = cu;
23564 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23565 if (type_die != NULL)
23566 {
23567 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23568 is created. This is important, for example, because for c++ classes
23569 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23570 type = read_type_die (type_die, type_cu);
23571 if (type == NULL)
23572 {
b98664d3 23573 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23574 " referenced from DIE at %s [in module %s]"),
23575 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23576 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23577 type = build_error_marker_type (cu, die);
23578 }
23579 }
23580 else
23581 {
b98664d3 23582 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23583 " from DIE at %s [in module %s]"),
23584 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23585 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23586 type = build_error_marker_type (cu, die);
23587 }
e286671b 23588
976ca316 23589 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
23590
23591 return type;
23592}
23593
23594/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23595 reading in and processing the type unit if necessary. */
23596
23597static struct type *
ff39bb5e 23598get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23599 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23600{
23601 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 23602 if (attr->form_is_ref ())
ac9ec31b
DE
23603 {
23604 struct dwarf2_cu *type_cu = cu;
23605 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23606
23607 return read_type_die (type_die, type_cu);
23608 }
23609 else if (attr->form == DW_FORM_ref_sig8)
23610 {
630ed6b9 23611 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
23612 }
23613 else
23614 {
976ca316 23615 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23616
b98664d3 23617 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23618 " at %s [in module %s]"),
23619 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 23620 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23621 return build_error_marker_type (cu, die);
23622 }
348e048f
DE
23623}
23624
e5fe5e75 23625/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23626
23627static void
ab432490
SM
23628load_full_type_unit (dwarf2_per_cu_data *per_cu,
23629 dwarf2_per_objfile *per_objfile)
348e048f 23630{
52dc124a 23631 struct signatured_type *sig_type;
348e048f 23632
f4dc4d17 23633 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 23634 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 23635
6721b2ec
DE
23636 /* We have the per_cu, but we need the signatured_type.
23637 Fortunately this is an easy translation. */
23638 gdb_assert (per_cu->is_debug_types);
23639 sig_type = (struct signatured_type *) per_cu;
348e048f 23640
7188ed02 23641 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 23642
ab432490 23643 read_signatured_type (sig_type, per_objfile);
348e048f 23644
7188ed02 23645 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
23646}
23647
3019eac3
DE
23648/* Read in a signatured type and build its CU and DIEs.
23649 If the type is a stub for the real type in a DWO file,
23650 read in the real type from the DWO file as well. */
dee91e82
DE
23651
23652static void
ab432490
SM
23653read_signatured_type (signatured_type *sig_type,
23654 dwarf2_per_objfile *per_objfile)
dee91e82 23655{
0d305d5c
TT
23656 gdb_assert (sig_type->is_debug_types);
23657 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
348e048f 23658
0d305d5c 23659 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
23660
23661 if (!reader.dummy_p)
23662 {
23663 struct dwarf2_cu *cu = reader.cu;
23664 const gdb_byte *info_ptr = reader.info_ptr;
23665
23666 gdb_assert (cu->die_hash == NULL);
23667 cu->die_hash =
23668 htab_create_alloc_ex (cu->header.length / 12,
23669 die_hash,
23670 die_eq,
23671 NULL,
23672 &cu->comp_unit_obstack,
23673 hashtab_obstack_allocate,
23674 dummy_obstack_deallocate);
23675
3e225074 23676 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
23677 reader.comp_unit_die->child
23678 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23679 reader.comp_unit_die);
23680 cu->dies = reader.comp_unit_die;
23681 /* comp_unit_die is not stored in die_hash, no need. */
23682
23683 /* We try not to read any attributes in this function, because
23684 not all CUs needed for references have been loaded yet, and
23685 symbol table processing isn't initialized. But we have to
23686 set the CU language, or we won't be able to build types
23687 correctly. Similarly, if we do not read the producer, we can
23688 not apply producer-specific interpretation. */
23689 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
23690
23691 reader.keep ();
c0ab21c2
TT
23692 }
23693
0d305d5c 23694 sig_type->tu_read = 1;
c906108c
SS
23695}
23696
c906108c
SS
23697/* Decode simple location descriptions.
23698 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
23699 the location and return the value. If COMPUTED is non-null, it is
23700 set to true to indicate that decoding was successful, and false
23701 otherwise. If COMPUTED is null, then this function may emit a
23702 complaint. */
c906108c
SS
23703
23704static CORE_ADDR
7d79de9a 23705decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 23706{
5e22e966 23707 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
23708 size_t i;
23709 size_t size = blk->size;
d521ce57 23710 const gdb_byte *data = blk->data;
21ae7a4d
JK
23711 CORE_ADDR stack[64];
23712 int stacki;
23713 unsigned int bytes_read, unsnd;
23714 gdb_byte op;
c906108c 23715
7d79de9a
TT
23716 if (computed != nullptr)
23717 *computed = false;
23718
21ae7a4d
JK
23719 i = 0;
23720 stacki = 0;
23721 stack[stacki] = 0;
23722 stack[++stacki] = 0;
23723
23724 while (i < size)
23725 {
23726 op = data[i++];
23727 switch (op)
23728 {
23729 case DW_OP_lit0:
23730 case DW_OP_lit1:
23731 case DW_OP_lit2:
23732 case DW_OP_lit3:
23733 case DW_OP_lit4:
23734 case DW_OP_lit5:
23735 case DW_OP_lit6:
23736 case DW_OP_lit7:
23737 case DW_OP_lit8:
23738 case DW_OP_lit9:
23739 case DW_OP_lit10:
23740 case DW_OP_lit11:
23741 case DW_OP_lit12:
23742 case DW_OP_lit13:
23743 case DW_OP_lit14:
23744 case DW_OP_lit15:
23745 case DW_OP_lit16:
23746 case DW_OP_lit17:
23747 case DW_OP_lit18:
23748 case DW_OP_lit19:
23749 case DW_OP_lit20:
23750 case DW_OP_lit21:
23751 case DW_OP_lit22:
23752 case DW_OP_lit23:
23753 case DW_OP_lit24:
23754 case DW_OP_lit25:
23755 case DW_OP_lit26:
23756 case DW_OP_lit27:
23757 case DW_OP_lit28:
23758 case DW_OP_lit29:
23759 case DW_OP_lit30:
23760 case DW_OP_lit31:
23761 stack[++stacki] = op - DW_OP_lit0;
23762 break;
f1bea926 23763
21ae7a4d
JK
23764 case DW_OP_reg0:
23765 case DW_OP_reg1:
23766 case DW_OP_reg2:
23767 case DW_OP_reg3:
23768 case DW_OP_reg4:
23769 case DW_OP_reg5:
23770 case DW_OP_reg6:
23771 case DW_OP_reg7:
23772 case DW_OP_reg8:
23773 case DW_OP_reg9:
23774 case DW_OP_reg10:
23775 case DW_OP_reg11:
23776 case DW_OP_reg12:
23777 case DW_OP_reg13:
23778 case DW_OP_reg14:
23779 case DW_OP_reg15:
23780 case DW_OP_reg16:
23781 case DW_OP_reg17:
23782 case DW_OP_reg18:
23783 case DW_OP_reg19:
23784 case DW_OP_reg20:
23785 case DW_OP_reg21:
23786 case DW_OP_reg22:
23787 case DW_OP_reg23:
23788 case DW_OP_reg24:
23789 case DW_OP_reg25:
23790 case DW_OP_reg26:
23791 case DW_OP_reg27:
23792 case DW_OP_reg28:
23793 case DW_OP_reg29:
23794 case DW_OP_reg30:
23795 case DW_OP_reg31:
23796 stack[++stacki] = op - DW_OP_reg0;
23797 if (i < size)
7d79de9a
TT
23798 {
23799 if (computed == nullptr)
23800 dwarf2_complex_location_expr_complaint ();
23801 else
23802 return 0;
23803 }
21ae7a4d 23804 break;
c906108c 23805
21ae7a4d
JK
23806 case DW_OP_regx:
23807 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23808 i += bytes_read;
23809 stack[++stacki] = unsnd;
23810 if (i < size)
7d79de9a
TT
23811 {
23812 if (computed == nullptr)
23813 dwarf2_complex_location_expr_complaint ();
23814 else
23815 return 0;
23816 }
21ae7a4d 23817 break;
c906108c 23818
21ae7a4d 23819 case DW_OP_addr:
c8a7a66f
TT
23820 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23821 &bytes_read);
21ae7a4d
JK
23822 i += bytes_read;
23823 break;
d53d4ac5 23824
21ae7a4d
JK
23825 case DW_OP_const1u:
23826 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23827 i += 1;
23828 break;
23829
23830 case DW_OP_const1s:
23831 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23832 i += 1;
23833 break;
23834
23835 case DW_OP_const2u:
23836 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23837 i += 2;
23838 break;
23839
23840 case DW_OP_const2s:
23841 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23842 i += 2;
23843 break;
d53d4ac5 23844
21ae7a4d
JK
23845 case DW_OP_const4u:
23846 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23847 i += 4;
23848 break;
23849
23850 case DW_OP_const4s:
23851 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23852 i += 4;
23853 break;
23854
585861ea
JK
23855 case DW_OP_const8u:
23856 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23857 i += 8;
23858 break;
23859
21ae7a4d
JK
23860 case DW_OP_constu:
23861 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23862 &bytes_read);
23863 i += bytes_read;
23864 break;
23865
23866 case DW_OP_consts:
23867 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23868 i += bytes_read;
23869 break;
23870
23871 case DW_OP_dup:
23872 stack[stacki + 1] = stack[stacki];
23873 stacki++;
23874 break;
23875
23876 case DW_OP_plus:
23877 stack[stacki - 1] += stack[stacki];
23878 stacki--;
23879 break;
23880
23881 case DW_OP_plus_uconst:
23882 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23883 &bytes_read);
23884 i += bytes_read;
23885 break;
23886
23887 case DW_OP_minus:
23888 stack[stacki - 1] -= stack[stacki];
23889 stacki--;
23890 break;
23891
23892 case DW_OP_deref:
23893 /* If we're not the last op, then we definitely can't encode
23894 this using GDB's address_class enum. This is valid for partial
23895 global symbols, although the variable's address will be bogus
23896 in the psymtab. */
23897 if (i < size)
7d79de9a
TT
23898 {
23899 if (computed == nullptr)
23900 dwarf2_complex_location_expr_complaint ();
23901 else
23902 return 0;
23903 }
21ae7a4d
JK
23904 break;
23905
dda83cd7 23906 case DW_OP_GNU_push_tls_address:
4aa4e28b 23907 case DW_OP_form_tls_address:
21ae7a4d
JK
23908 /* The top of the stack has the offset from the beginning
23909 of the thread control block at which the variable is located. */
23910 /* Nothing should follow this operator, so the top of stack would
23911 be returned. */
23912 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23913 address will be bogus in the psymtab. Make it always at least
23914 non-zero to not look as a variable garbage collected by linker
23915 which have DW_OP_addr 0. */
21ae7a4d 23916 if (i < size)
7d79de9a
TT
23917 {
23918 if (computed == nullptr)
23919 dwarf2_complex_location_expr_complaint ();
23920 else
23921 return 0;
23922 }
585861ea 23923 stack[stacki]++;
dda83cd7 23924 break;
21ae7a4d
JK
23925
23926 case DW_OP_GNU_uninit:
7d79de9a
TT
23927 if (computed != nullptr)
23928 return 0;
21ae7a4d
JK
23929 break;
23930
336d760d 23931 case DW_OP_addrx:
3019eac3 23932 case DW_OP_GNU_addr_index:
49f6c839 23933 case DW_OP_GNU_const_index:
3019eac3
DE
23934 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23935 &bytes_read);
23936 i += bytes_read;
23937 break;
23938
21ae7a4d 23939 default:
7d79de9a
TT
23940 if (computed == nullptr)
23941 {
23942 const char *name = get_DW_OP_name (op);
21ae7a4d 23943
7d79de9a
TT
23944 if (name)
23945 complaint (_("unsupported stack op: '%s'"),
23946 name);
23947 else
23948 complaint (_("unsupported stack op: '%02x'"),
23949 op);
23950 }
21ae7a4d
JK
23951
23952 return (stack[stacki]);
d53d4ac5 23953 }
3c6e0cb3 23954
21ae7a4d 23955 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 23956 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
23957 if (stacki >= ARRAY_SIZE (stack) - 1)
23958 {
7d79de9a
TT
23959 if (computed == nullptr)
23960 complaint (_("location description stack overflow"));
21ae7a4d
JK
23961 return 0;
23962 }
23963
23964 if (stacki <= 0)
23965 {
7d79de9a
TT
23966 if (computed == nullptr)
23967 complaint (_("location description stack underflow"));
21ae7a4d
JK
23968 return 0;
23969 }
23970 }
7d79de9a
TT
23971
23972 if (computed != nullptr)
23973 *computed = true;
21ae7a4d 23974 return (stack[stacki]);
c906108c
SS
23975}
23976
23977/* memory allocation interface */
23978
c906108c 23979static struct dwarf_block *
7b5a2f43 23980dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23981{
8d749320 23982 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23983}
23984
c906108c 23985static struct die_info *
b60c80d6 23986dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23987{
23988 struct die_info *die;
b60c80d6
DJ
23989 size_t size = sizeof (struct die_info);
23990
23991 if (num_attrs > 1)
23992 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23993
b60c80d6 23994 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23995 memset (die, 0, sizeof (struct die_info));
23996 return (die);
23997}
2e276125
JB
23998
23999\f
a036ba48 24000
c90ec28a 24001/* Macro support. */
cf2c3c16 24002
9eac9650
TT
24003/* An overload of dwarf_decode_macros that finds the correct section
24004 and ensures it is read in before calling the other overload. */
24005
24006static void
24007dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24008 int section_is_gnu)
24009{
976ca316
SM
24010 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24011 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24012 const struct line_header *lh = cu->line_header;
9eac9650
TT
24013 unsigned int offset_size = cu->header.offset_size;
24014 struct dwarf2_section_info *section;
24015 const char *section_name;
24016
24017 if (cu->dwo_unit != nullptr)
24018 {
24019 if (section_is_gnu)
24020 {
24021 section = &cu->dwo_unit->dwo_file->sections.macro;
24022 section_name = ".debug_macro.dwo";
24023 }
24024 else
24025 {
24026 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24027 section_name = ".debug_macinfo.dwo";
24028 }
24029 }
24030 else
24031 {
24032 if (section_is_gnu)
24033 {
976ca316 24034 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24035 section_name = ".debug_macro";
24036 }
24037 else
24038 {
976ca316 24039 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24040 section_name = ".debug_macinfo";
24041 }
24042 }
24043
24044 section->read (objfile);
24045 if (section->buffer == nullptr)
24046 {
24047 complaint (_("missing %s section"), section_name);
24048 return;
24049 }
24050
24051 buildsym_compunit *builder = cu->get_builder ();
24052
048fde1e 24053 struct dwarf2_section_info *str_offsets_section;
24054 struct dwarf2_section_info *str_section;
24055 ULONGEST str_offsets_base;
24056
24057 if (cu->dwo_unit != nullptr)
24058 {
24059 str_offsets_section = &cu->dwo_unit->dwo_file
24060 ->sections.str_offsets;
24061 str_section = &cu->dwo_unit->dwo_file->sections.str;
24062 str_offsets_base = cu->header.addr_size;
24063 }
24064 else
24065 {
24066 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24067 str_section = &per_objfile->per_bfd->str;
24068 str_offsets_base = *cu->str_offsets_base;
24069 }
24070
976ca316 24071 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24072 offset_size, offset, str_section, str_offsets_section,
24073 str_offsets_base, section_is_gnu);
9eac9650
TT
24074}
24075
3019eac3
DE
24076/* Return the .debug_loc section to use for CU.
24077 For DWO files use .debug_loc.dwo. */
24078
24079static struct dwarf2_section_info *
24080cu_debug_loc_section (struct dwarf2_cu *cu)
24081{
976ca316 24082 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24083
3019eac3 24084 if (cu->dwo_unit)
43988095
JK
24085 {
24086 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24087
43988095
JK
24088 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24089 }
976ca316
SM
24090 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24091 : &per_objfile->per_bfd->loc);
3019eac3
DE
24092}
24093
d0ce17d8
CT
24094/* Return the .debug_rnglists section to use for CU. */
24095static struct dwarf2_section_info *
24096cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24097{
24098 if (cu->header.version < 5)
24099 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24100 cu->header.version);
24101 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24102
24103 /* Make sure we read the .debug_rnglists section from the file that
24104 contains the DW_AT_ranges attribute we are reading. Normally that
24105 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24106 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24107 program. */
24108 if (cu->dwo_unit != nullptr
24109 && tag != DW_TAG_compile_unit
24110 && tag != DW_TAG_skeleton_unit)
24111 {
24112 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24113
24114 if (sections->rnglists.size > 0)
24115 return &sections->rnglists;
24116 else
24117 error (_(".debug_rnglists section is missing from .dwo file."));
24118 }
24119 return &dwarf2_per_objfile->per_bfd->rnglists;
24120}
24121
8cf6f0b1
TT
24122/* A helper function that fills in a dwarf2_loclist_baton. */
24123
24124static void
24125fill_in_loclist_baton (struct dwarf2_cu *cu,
24126 struct dwarf2_loclist_baton *baton,
ff39bb5e 24127 const struct attribute *attr)
8cf6f0b1 24128{
976ca316 24129 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24130 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24131
976ca316 24132 section->read (per_objfile->objfile);
8cf6f0b1 24133
976ca316 24134 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24135 baton->per_cu = cu->per_cu;
24136 gdb_assert (baton->per_cu);
24137 /* We don't know how long the location list is, but make sure we
24138 don't run off the edge of the section. */
d4df075e
TT
24139 baton->size = section->size - attr->as_unsigned ();
24140 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24141 if (cu->base_address.has_value ())
24142 baton->base_address = *cu->base_address;
24143 else
24144 baton->base_address = 0;
f664829e 24145 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24146}
24147
4c2df51b 24148static void
ff39bb5e 24149dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24150 struct dwarf2_cu *cu, int is_block)
4c2df51b 24151{
976ca316
SM
24152 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24153 struct objfile *objfile = per_objfile->objfile;
3019eac3 24154 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24155
cd6c91b4 24156 if (attr->form_is_section_offset ()
3019eac3 24157 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24158 the section. If so, fall through to the complaint in the
24159 other branch. */
d4df075e 24160 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24161 {
0d53c4c4 24162 struct dwarf2_loclist_baton *baton;
4c2df51b 24163
8d749320 24164 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24165
8cf6f0b1 24166 fill_in_loclist_baton (cu, baton, attr);
be391dca 24167
2b24b6e4 24168 if (!cu->base_address.has_value ())
b98664d3 24169 complaint (_("Location list used without "
3e43a32a 24170 "specifying the CU base address."));
4c2df51b 24171
f1e6e072
TT
24172 SYMBOL_ACLASS_INDEX (sym) = (is_block
24173 ? dwarf2_loclist_block_index
24174 : dwarf2_loclist_index);
0d53c4c4
DJ
24175 SYMBOL_LOCATION_BATON (sym) = baton;
24176 }
24177 else
24178 {
24179 struct dwarf2_locexpr_baton *baton;
24180
8d749320 24181 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24182 baton->per_objfile = per_objfile;
ae0d2f24
UW
24183 baton->per_cu = cu->per_cu;
24184 gdb_assert (baton->per_cu);
0d53c4c4 24185
4fc6c0d5 24186 if (attr->form_is_block ())
0d53c4c4
DJ
24187 {
24188 /* Note that we're just copying the block's data pointer
24189 here, not the actual data. We're still pointing into the
6502dd73
DJ
24190 info_buffer for SYM's objfile; right now we never release
24191 that buffer, but when we do clean up properly this may
24192 need to change. */
9d2246fc
TT
24193 struct dwarf_block *block = attr->as_block ();
24194 baton->size = block->size;
24195 baton->data = block->data;
0d53c4c4
DJ
24196 }
24197 else
24198 {
24199 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24200 sym->natural_name ());
0d53c4c4 24201 baton->size = 0;
0d53c4c4 24202 }
6e70227d 24203
f1e6e072
TT
24204 SYMBOL_ACLASS_INDEX (sym) = (is_block
24205 ? dwarf2_locexpr_block_index
24206 : dwarf2_locexpr_index);
0d53c4c4
DJ
24207 SYMBOL_LOCATION_BATON (sym) = baton;
24208 }
4c2df51b 24209}
6502dd73 24210
2e6a9f79 24211/* See read.h. */
96408a79 24212
2e6a9f79
SM
24213const comp_unit_head *
24214dwarf2_per_cu_data::get_header () const
96408a79 24215{
2e6a9f79
SM
24216 if (!m_header_read_in)
24217 {
24218 const gdb_byte *info_ptr
24219 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24220
2e6a9f79 24221 memset (&m_header, 0, sizeof (m_header));
96408a79 24222
2e6a9f79
SM
24223 read_comp_unit_head (&m_header, info_ptr, this->section,
24224 rcuh_kind::COMPILE);
a9f172c6
TT
24225
24226 m_header_read_in = true;
2e6a9f79 24227 }
96408a79 24228
2e6a9f79 24229 return &m_header;
96408a79
SA
24230}
24231
09ba997f 24232/* See read.h. */
ae0d2f24 24233
98714339 24234int
09ba997f 24235dwarf2_per_cu_data::addr_size () const
ae0d2f24 24236{
2e6a9f79 24237 return this->get_header ()->addr_size;
ae0d2f24
UW
24238}
24239
09ba997f 24240/* See read.h. */
9eae7c52
TT
24241
24242int
09ba997f 24243dwarf2_per_cu_data::offset_size () const
9eae7c52 24244{
2e6a9f79 24245 return this->get_header ()->offset_size;
96408a79
SA
24246}
24247
09ba997f 24248/* See read.h. */
96408a79
SA
24249
24250int
09ba997f 24251dwarf2_per_cu_data::ref_addr_size () const
96408a79 24252{
2e6a9f79 24253 const comp_unit_head *header = this->get_header ();
96408a79 24254
2e6a9f79
SM
24255 if (header->version == 2)
24256 return header->addr_size;
96408a79 24257 else
2e6a9f79 24258 return header->offset_size;
181cebd4
JK
24259}
24260
22b6cd70
TT
24261/* A helper function for dwarf2_find_containing_comp_unit that returns
24262 the index of the result, and that searches a vector. It will
24263 return a result even if the offset in question does not actually
24264 occur in any CU. This is separate so that it can be unit
24265 tested. */
ae038cb0 24266
22b6cd70
TT
24267static int
24268dwarf2_find_containing_comp_unit
24269 (sect_offset sect_off,
24270 unsigned int offset_in_dwz,
473ab964 24271 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
ae038cb0 24272{
ae038cb0
DJ
24273 int low, high;
24274
ae038cb0 24275 low = 0;
22b6cd70 24276 high = all_comp_units.size () - 1;
ae038cb0
DJ
24277 while (high > low)
24278 {
36586728 24279 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24280 int mid = low + (high - low) / 2;
9a619af0 24281
0d305d5c 24282 mid_cu = all_comp_units[mid].get ();
36586728 24283 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24284 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24285 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24286 high = mid;
24287 else
24288 low = mid + 1;
24289 }
24290 gdb_assert (low == high);
22b6cd70
TT
24291 return low;
24292}
24293
24294/* Locate the .debug_info compilation unit from CU's objfile which contains
24295 the DIE at OFFSET. Raises an error on failure. */
24296
24297static struct dwarf2_per_cu_data *
24298dwarf2_find_containing_comp_unit (sect_offset sect_off,
24299 unsigned int offset_in_dwz,
976ca316 24300 dwarf2_per_objfile *per_objfile)
22b6cd70 24301{
976ca316
SM
24302 int low = dwarf2_find_containing_comp_unit
24303 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
0d305d5c
TT
24304 dwarf2_per_cu_data *this_cu
24305 = per_objfile->per_bfd->all_comp_units[low].get ();
22b6cd70 24306
45b8ae0c 24307 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24308 {
36586728 24309 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24310 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24311 "offset %s [in module %s]"),
24312 sect_offset_str (sect_off),
976ca316 24313 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24314
976ca316 24315 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24316 <= sect_off);
0d305d5c 24317 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
ae038cb0
DJ
24318 }
24319 else
24320 {
976ca316 24321 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24322 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24323 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24324 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24325 return this_cu;
24326 }
24327}
24328
22b6cd70
TT
24329#if GDB_SELF_TEST
24330
24331namespace selftests {
24332namespace find_containing_comp_unit {
24333
24334static void
24335run_test ()
24336{
473ab964 24337 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
0d305d5c 24338 dwarf2_per_cu_data *one_ptr = one.get ();
473ab964 24339 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
0d305d5c 24340 dwarf2_per_cu_data *two_ptr = two.get ();
473ab964 24341 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
0d305d5c 24342 dwarf2_per_cu_data *three_ptr = three.get ();
473ab964 24343 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
0d305d5c
TT
24344 dwarf2_per_cu_data *four_ptr = four.get ();
24345
24346 one->length = 5;
24347 two->sect_off = sect_offset (one->length);
24348 two->length = 7;
24349
24350 three->length = 5;
24351 three->is_dwz = 1;
24352 four->sect_off = sect_offset (three->length);
24353 four->length = 7;
24354 four->is_dwz = 1;
24355
473ab964 24356 std::vector<dwarf2_per_cu_data_up> units;
0d305d5c
TT
24357 units.push_back (std::move (one));
24358 units.push_back (std::move (two));
24359 units.push_back (std::move (three));
24360 units.push_back (std::move (four));
22b6cd70
TT
24361
24362 int result;
24363
24364 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
0d305d5c 24365 SELF_CHECK (units[result].get () == one_ptr);
22b6cd70 24366 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
0d305d5c 24367 SELF_CHECK (units[result].get () == one_ptr);
22b6cd70 24368 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
0d305d5c 24369 SELF_CHECK (units[result].get () == two_ptr);
22b6cd70
TT
24370
24371 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
0d305d5c 24372 SELF_CHECK (units[result].get () == three_ptr);
22b6cd70 24373 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
0d305d5c 24374 SELF_CHECK (units[result].get () == three_ptr);
22b6cd70 24375 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
0d305d5c 24376 SELF_CHECK (units[result].get () == four_ptr);
22b6cd70
TT
24377}
24378
24379}
24380}
24381
24382#endif /* GDB_SELF_TEST */
24383
9816fde3
JK
24384/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24385
24386static void
95554aad
TT
24387prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24388 enum language pretend_language)
9816fde3
JK
24389{
24390 struct attribute *attr;
24391
24392 /* Set the language we're debugging. */
24393 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24394 if (attr != nullptr)
529908cb 24395 set_cu_language (attr->constant_value (0), cu);
9816fde3 24396 else
9cded63f 24397 {
95554aad 24398 cu->language = pretend_language;
9cded63f
TT
24399 cu->language_defn = language_def (cu->language);
24400 }
dee91e82 24401
7d45c7c3 24402 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24403}
24404
7188ed02 24405/* See read.h. */
ae038cb0 24406
7188ed02
SM
24407dwarf2_cu *
24408dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24409{
7188ed02
SM
24410 auto it = m_dwarf2_cus.find (per_cu);
24411 if (it == m_dwarf2_cus.end ())
24412 return nullptr;
ae038cb0 24413
7188ed02
SM
24414 return it->second;
24415}
24416
24417/* See read.h. */
24418
24419void
24420dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24421{
24422 gdb_assert (this->get_cu (per_cu) == nullptr);
24423
24424 m_dwarf2_cus[per_cu] = cu;
24425}
24426
24427/* See read.h. */
24428
24429void
24430dwarf2_per_objfile::age_comp_units ()
24431{
17e593e9
SM
24432 dwarf_read_debug_printf_v ("running");
24433
08ac5771
SM
24434 /* This is not expected to be called in the middle of CU expansion. There is
24435 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24436 loaded in memory. Calling age_comp_units while the queue is in use could
24437 make us free the DIEs for a CU that is in the queue and therefore break
24438 that invariant. */
24439 gdb_assert (!this->per_bfd->queue.has_value ());
24440
7188ed02
SM
24441 /* Start by clearing all marks. */
24442 for (auto pair : m_dwarf2_cus)
347212b8 24443 pair.second->clear_mark ();
7188ed02
SM
24444
24445 /* Traverse all CUs, mark them and their dependencies if used recently
24446 enough. */
24447 for (auto pair : m_dwarf2_cus)
ae038cb0 24448 {
7188ed02
SM
24449 dwarf2_cu *cu = pair.second;
24450
24451 cu->last_used++;
24452 if (cu->last_used <= dwarf_max_cache_age)
347212b8 24453 cu->mark ();
ae038cb0
DJ
24454 }
24455
7188ed02
SM
24456 /* Delete all CUs still not marked. */
24457 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 24458 {
7188ed02 24459 dwarf2_cu *cu = it->second;
ae038cb0 24460
347212b8 24461 if (!cu->is_marked ())
ae038cb0 24462 {
17e593e9
SM
24463 dwarf_read_debug_printf_v ("deleting old CU %s",
24464 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
24465 delete cu;
24466 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
24467 }
24468 else
7188ed02 24469 it++;
ae038cb0
DJ
24470 }
24471}
24472
7188ed02 24473/* See read.h. */
ae038cb0 24474
7188ed02
SM
24475void
24476dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24477{
7188ed02
SM
24478 auto it = m_dwarf2_cus.find (per_cu);
24479 if (it == m_dwarf2_cus.end ())
24480 return;
ae038cb0 24481
7188ed02 24482 delete it->second;
ae038cb0 24483
7188ed02
SM
24484 m_dwarf2_cus.erase (it);
24485}
ae038cb0 24486
7188ed02
SM
24487dwarf2_per_objfile::~dwarf2_per_objfile ()
24488{
24489 remove_all_cus ();
ae038cb0
DJ
24490}
24491
dee91e82
DE
24492/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24493 We store these in a hash table separate from the DIEs, and preserve them
24494 when the DIEs are flushed out of cache.
24495
24496 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24497 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24498 or the type may come from a DWO file. Furthermore, while it's more logical
24499 to use per_cu->section+offset, with Fission the section with the data is in
24500 the DWO file but we don't know that section at the point we need it.
24501 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24502 because we can enter the lookup routine, get_die_type_at_offset, from
24503 outside this file, and thus won't necessarily have PER_CU->cu.
24504 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24505
dee91e82 24506struct dwarf2_per_cu_offset_and_type
1c379e20 24507{
dee91e82 24508 const struct dwarf2_per_cu_data *per_cu;
9c541725 24509 sect_offset sect_off;
1c379e20
DJ
24510 struct type *type;
24511};
24512
dee91e82 24513/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24514
24515static hashval_t
dee91e82 24516per_cu_offset_and_type_hash (const void *item)
1c379e20 24517{
9a3c8263
SM
24518 const struct dwarf2_per_cu_offset_and_type *ofs
24519 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24520
9c541725 24521 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24522}
24523
dee91e82 24524/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24525
24526static int
dee91e82 24527per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24528{
9a3c8263
SM
24529 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24530 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24531 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24532 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24533
dee91e82 24534 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24535 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24536}
24537
24538/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24539 table if necessary. For convenience, return TYPE.
24540
24541 The DIEs reading must have careful ordering to:
85102364 24542 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24543 reading current DIE.
24544 * Not trying to dereference contents of still incompletely read in types
24545 while reading in other DIEs.
24546 * Enable referencing still incompletely read in types just by a pointer to
24547 the type without accessing its fields.
24548
24549 Therefore caller should follow these rules:
24550 * Try to fetch any prerequisite types we may need to build this DIE type
24551 before building the type and calling set_die_type.
e71ec853 24552 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24553 possible before fetching more types to complete the current type.
24554 * Make the type as complete as possible before fetching more types. */
1c379e20 24555
f792889a 24556static struct type *
57567375
TT
24557set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24558 bool skip_data_location)
1c379e20 24559{
976ca316 24560 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 24561 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 24562 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
24563 struct attribute *attr;
24564 struct dynamic_prop prop;
1c379e20 24565
b4ba55a1
JB
24566 /* For Ada types, make sure that the gnat-specific data is always
24567 initialized (if not already set). There are a few types where
24568 we should not be doing so, because the type-specific area is
24569 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24570 where the type-specific area is used to store the floatformat).
24571 But this is not a problem, because the gnat-specific information
24572 is actually not needed for these types. */
24573 if (need_gnat_info (cu)
78134374
SM
24574 && type->code () != TYPE_CODE_FUNC
24575 && type->code () != TYPE_CODE_FLT
24576 && type->code () != TYPE_CODE_METHODPTR
24577 && type->code () != TYPE_CODE_MEMBERPTR
24578 && type->code () != TYPE_CODE_METHOD
09584414 24579 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
24580 && !HAVE_GNAT_AUX_INFO (type))
24581 INIT_GNAT_SPECIFIC (type);
24582
3f2f83dd
KB
24583 /* Read DW_AT_allocated and set in type. */
24584 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 24585 if (attr != NULL)
3f2f83dd 24586 {
293e7e51 24587 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24588 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24589 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 24590 }
3f2f83dd
KB
24591
24592 /* Read DW_AT_associated and set in type. */
24593 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 24594 if (attr != NULL)
3f2f83dd 24595 {
293e7e51 24596 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24597 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24598 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 24599 }
3f2f83dd 24600
3cdcd0ce 24601 /* Read DW_AT_data_location and set in type. */
57567375
TT
24602 if (!skip_data_location)
24603 {
24604 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24605 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24606 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24607 }
3cdcd0ce 24608
976ca316
SM
24609 if (per_objfile->die_type_hash == NULL)
24610 per_objfile->die_type_hash
0335378b
TT
24611 = htab_up (htab_create_alloc (127,
24612 per_cu_offset_and_type_hash,
24613 per_cu_offset_and_type_eq,
24614 NULL, xcalloc, xfree));
1c379e20 24615
dee91e82 24616 ofs.per_cu = cu->per_cu;
9c541725 24617 ofs.sect_off = die->sect_off;
1c379e20 24618 ofs.type = type;
dee91e82 24619 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 24620 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24621 if (*slot)
b98664d3 24622 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24623 sect_offset_str (die->sect_off));
8d749320
SM
24624 *slot = XOBNEW (&objfile->objfile_obstack,
24625 struct dwarf2_per_cu_offset_and_type);
1c379e20 24626 **slot = ofs;
f792889a 24627 return type;
1c379e20
DJ
24628}
24629
9c541725 24630/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24631 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24632
24633static struct type *
9c541725 24634get_die_type_at_offset (sect_offset sect_off,
aa66c379 24635 dwarf2_per_cu_data *per_cu,
976ca316 24636 dwarf2_per_objfile *per_objfile)
1c379e20 24637{
dee91e82 24638 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 24639
976ca316 24640 if (per_objfile->die_type_hash == NULL)
f792889a 24641 return NULL;
1c379e20 24642
dee91e82 24643 ofs.per_cu = per_cu;
9c541725 24644 ofs.sect_off = sect_off;
9a3c8263 24645 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 24646 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24647 if (slot)
24648 return slot->type;
24649 else
24650 return NULL;
24651}
24652
02142a6c 24653/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24654 or return NULL if DIE does not have a saved type. */
24655
24656static struct type *
24657get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24658{
aa66c379 24659 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
24660}
24661
72bf9492
DJ
24662/* Trivial hash function for partial_die_info: the hash value of a DIE
24663 is its offset in .debug_info for this objfile. */
24664
24665static hashval_t
24666partial_die_hash (const void *item)
24667{
9a3c8263
SM
24668 const struct partial_die_info *part_die
24669 = (const struct partial_die_info *) item;
9a619af0 24670
9c541725 24671 return to_underlying (part_die->sect_off);
72bf9492
DJ
24672}
24673
24674/* Trivial comparison function for partial_die_info structures: two DIEs
24675 are equal if they have the same offset. */
24676
24677static int
24678partial_die_eq (const void *item_lhs, const void *item_rhs)
24679{
9a3c8263
SM
24680 const struct partial_die_info *part_die_lhs
24681 = (const struct partial_die_info *) item_lhs;
24682 const struct partial_die_info *part_die_rhs
24683 = (const struct partial_die_info *) item_rhs;
9a619af0 24684
9c541725 24685 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24686}
24687
3c3bb058
AB
24688struct cmd_list_element *set_dwarf_cmdlist;
24689struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 24690
9291a0cd 24691static void
cd4fb1b2
SM
24692show_check_physname (struct ui_file *file, int from_tty,
24693 struct cmd_list_element *c, const char *value)
9291a0cd 24694{
cd4fb1b2
SM
24695 fprintf_filtered (file,
24696 _("Whether to check \"physname\" is %s.\n"),
24697 value);
9291a0cd
TT
24698}
24699
6c265988 24700void _initialize_dwarf2_read ();
cd4fb1b2 24701void
6c265988 24702_initialize_dwarf2_read ()
9291a0cd 24703{
0743fc83 24704 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 24705Set DWARF specific variables.\n\
590042fc 24706Configure DWARF variables such as the cache size."),
2f822da5 24707 &set_dwarf_cmdlist,
0743fc83 24708 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24709
0743fc83 24710 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
24711Show DWARF specific variables.\n\
24712Show DWARF variables such as the cache size."),
2f822da5 24713 &show_dwarf_cmdlist,
0743fc83 24714 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24715
cd4fb1b2
SM
24716 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24717 &dwarf_max_cache_age, _("\
24718Set the upper bound on the age of cached DWARF compilation units."), _("\
24719Show the upper bound on the age of cached DWARF compilation units."), _("\
24720A higher limit means that cached compilation units will be stored\n\
24721in memory longer, and more total memory will be used. Zero disables\n\
24722caching, which can slow down startup."),
24723 NULL,
24724 show_dwarf_max_cache_age,
24725 &set_dwarf_cmdlist,
24726 &show_dwarf_cmdlist);
156942c7 24727
cd4fb1b2
SM
24728 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24729Set debugging of the DWARF reader."), _("\
24730Show debugging of the DWARF reader."), _("\
24731When enabled (non-zero), debugging messages are printed during DWARF\n\
24732reading and symtab expansion. A value of 1 (one) provides basic\n\
24733information. A value greater than 1 provides more verbose information."),
24734 NULL,
24735 NULL,
24736 &setdebuglist, &showdebuglist);
9291a0cd 24737
749369c4 24738 add_setshow_boolean_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
cd4fb1b2
SM
24739Set debugging of the DWARF DIE reader."), _("\
24740Show debugging of the DWARF DIE reader."), _("\
749369c4 24741When enabled, DIEs are dumped after they are read in.\n\
cd4fb1b2 24742The value is the maximum depth to print."),
749369c4
SM
24743 NULL,
24744 NULL,
24745 &setdebuglist, &showdebuglist);
9291a0cd 24746
cd4fb1b2
SM
24747 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24748Set debugging of the dwarf line reader."), _("\
24749Show debugging of the dwarf line reader."), _("\
24750When enabled (non-zero), line number entries are dumped as they are read in.\n\
24751A value of 1 (one) provides basic information.\n\
24752A value greater than 1 provides more verbose information."),
24753 NULL,
24754 NULL,
24755 &setdebuglist, &showdebuglist);
437afbb8 24756
cd4fb1b2
SM
24757 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24758Set cross-checking of \"physname\" code against demangler."), _("\
24759Show cross-checking of \"physname\" code against demangler."), _("\
24760When enabled, GDB's internal \"physname\" code is checked against\n\
24761the demangler."),
24762 NULL, show_check_physname,
24763 &setdebuglist, &showdebuglist);
900e11f9 24764
e615022a
DE
24765 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24766 no_class, &use_deprecated_index_sections, _("\
24767Set whether to use deprecated gdb_index sections."), _("\
24768Show whether to use deprecated gdb_index sections."), _("\
24769When enabled, deprecated .gdb_index sections are used anyway.\n\
24770Normally they are ignored either because of a missing feature or\n\
24771performance issue.\n\
24772Warning: This option must be enabled before gdb reads the file."),
24773 NULL,
24774 NULL,
24775 &setlist, &showlist);
24776
f1e6e072
TT
24777 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24778 &dwarf2_locexpr_funcs);
24779 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24780 &dwarf2_loclist_funcs);
24781
24782 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24783 &dwarf2_block_frame_base_locexpr_funcs);
24784 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24785 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24786
24787#if GDB_SELF_TEST
24788 selftests::register_test ("dw2_expand_symtabs_matching",
24789 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24790 selftests::register_test ("dwarf2_find_containing_comp_unit",
24791 selftests::find_containing_comp_unit::run_test);
c62446b1 24792#endif
6502dd73 24793}
This page took 9.456798 seconds and 4 git commands to generate.