[gdb/symtab] Fix zero address complaint for shlib
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
3666a048 3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
cd53fa40 35#include "dwarf2/comp-unit-head.h"
8ae78a44 36#include "dwarf2/cu.h"
82ca8957
TT
37#include "dwarf2/index-cache.h"
38#include "dwarf2/index-common.h"
f4382c45 39#include "dwarf2/leb.h"
8fdd972c 40#include "dwarf2/line-header.h"
9fda78b6 41#include "dwarf2/dwz.h"
c90ec28a 42#include "dwarf2/macro.h"
c2d50fd0 43#include "dwarf2/die.h"
c2a62a3d 44#include "dwarf2/sect-names.h"
2b2558bf 45#include "dwarf2/stringify.h"
70182375 46#include "dwarf2/public.h"
4de283e4
TT
47#include "bfd.h"
48#include "elf-bfd.h"
49#include "symtab.h"
50#include "gdbtypes.h"
51#include "objfiles.h"
d55e5aa6 52#include "dwarf2.h"
4de283e4
TT
53#include "demangle.h"
54#include "gdb-demangle.h"
4de283e4 55#include "filenames.h" /* for DOSish file names */
4de283e4
TT
56#include "language.h"
57#include "complaints.h"
82ca8957
TT
58#include "dwarf2/expr.h"
59#include "dwarf2/loc.h"
4de283e4
TT
60#include "cp-support.h"
61#include "hashtab.h"
62#include "command.h"
d55e5aa6 63#include "gdbcmd.h"
4de283e4
TT
64#include "block.h"
65#include "addrmap.h"
66#include "typeprint.h"
67#include "psympriv.h"
4de283e4 68#include "c-lang.h"
d55e5aa6 69#include "go-lang.h"
4de283e4
TT
70#include "valprint.h"
71#include "gdbcore.h" /* for gnutarget */
72#include "gdb/gdb-index.h"
4de283e4
TT
73#include "gdb_bfd.h"
74#include "f-lang.h"
75#include "source.h"
4de283e4 76#include "build-id.h"
d55e5aa6 77#include "namespace.h"
268a13a5
TT
78#include "gdbsupport/function-view.h"
79#include "gdbsupport/gdb_optional.h"
80#include "gdbsupport/underlying.h"
268a13a5 81#include "gdbsupport/hash_enum.h"
4de283e4 82#include "filename-seen-cache.h"
b32b108a 83#include "producer.h"
4de283e4 84#include <fcntl.h>
4de283e4 85#include <algorithm>
4de283e4 86#include <unordered_map>
268a13a5 87#include "gdbsupport/selftest.h"
c9317f21 88#include "rust-lang.h"
268a13a5 89#include "gdbsupport/pathstuff.h"
edd45eb0 90#include "count-one-bits.h"
9fc29955 91#include <unordered_set>
437afbb8 92
73be47f5
DE
93/* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
b4f54984
DE
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96static unsigned int dwarf_read_debug = 0;
45cfd468 97
6f738b01
SM
98/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
99
100#define dwarf_read_debug_printf(fmt, ...) \
74b773fc
SM
101 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
102 ##__VA_ARGS__)
6f738b01
SM
103
104/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
105
106#define dwarf_read_debug_printf_v(fmt, ...) \
74b773fc
SM
107 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
108 ##__VA_ARGS__)
6f738b01 109
3a706c17
SM
110/* When non-zero, dump DIEs after they are read in. */
111static unsigned int dwarf_die_debug = 0;
d97bc12b 112
27e0867f 113/* When non-zero, dump line number entries as they are read in. */
8fdd972c 114unsigned int dwarf_line_debug = 0;
27e0867f 115
491144b5
CB
116/* When true, cross-check physname against demangler. */
117static bool check_physname = false;
900e11f9 118
491144b5
CB
119/* When true, do not reject deprecated .gdb_index sections. */
120static bool use_deprecated_index_sections = false;
481860b3 121
17ee85fc
TT
122/* This is used to store the data that is always per objfile. */
123static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
124
125/* These are used to store the dwarf2_per_bfd objects.
126
127 objfiles having the same BFD, which doesn't require relocations, are going to
128 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
129
130 Other objfiles are not going to share a dwarf2_per_bfd with any other
131 objfiles, so they'll have their own version kept in the _objfile_data_key
132 version. */
133static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
134static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 135
f1e6e072
TT
136/* The "aclass" indices for various kinds of computed DWARF symbols. */
137
138static int dwarf2_locexpr_index;
139static int dwarf2_loclist_index;
140static int dwarf2_locexpr_block_index;
141static int dwarf2_loclist_block_index;
142
41144253 143/* Size of .debug_loclists section header for 32-bit DWARF format. */
144#define LOCLIST_HEADER_SIZE32 12
145
146/* Size of .debug_loclists section header for 64-bit DWARF format. */
147#define LOCLIST_HEADER_SIZE64 20
148
d0ce17d8
CT
149/* Size of .debug_rnglists section header for 32-bit DWARF format. */
150#define RNGLIST_HEADER_SIZE32 12
151
152/* Size of .debug_rnglists section header for 64-bit DWARF format. */
153#define RNGLIST_HEADER_SIZE64 20
154
3f563c84
PA
155/* An index into a (C++) symbol name component in a symbol name as
156 recorded in the mapped_index's symbol table. For each C++ symbol
157 in the symbol table, we record one entry for the start of each
158 component in the symbol in a table of name components, and then
159 sort the table, in order to be able to binary search symbol names,
160 ignoring leading namespaces, both completion and regular look up.
161 For example, for symbol "A::B::C", we'll have an entry that points
162 to "A::B::C", another that points to "B::C", and another for "C".
163 Note that function symbols in GDB index have no parameter
164 information, just the function/method names. You can convert a
165 name_component to a "const char *" using the
166 'mapped_index::symbol_name_at(offset_type)' method. */
167
168struct name_component
169{
170 /* Offset in the symbol name where the component starts. Stored as
171 a (32-bit) offset instead of a pointer to save memory and improve
172 locality on 64-bit architectures. */
173 offset_type name_offset;
174
175 /* The symbol's index in the symbol and constant pool tables of a
176 mapped_index. */
177 offset_type idx;
178};
179
44ed8f3e
PA
180/* Base class containing bits shared by both .gdb_index and
181 .debug_name indexes. */
182
183struct mapped_index_base
184{
22ca247e
TT
185 mapped_index_base () = default;
186 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
187
44ed8f3e
PA
188 /* The name_component table (a sorted vector). See name_component's
189 description above. */
190 std::vector<name_component> name_components;
191
192 /* How NAME_COMPONENTS is sorted. */
193 enum case_sensitivity name_components_casing;
194
195 /* Return the number of names in the symbol table. */
196 virtual size_t symbol_name_count () const = 0;
197
198 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
199 virtual const char *symbol_name_at
200 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
201
202 /* Return whether the name at IDX in the symbol table should be
203 ignored. */
204 virtual bool symbol_name_slot_invalid (offset_type idx) const
205 {
206 return false;
207 }
208
209 /* Build the symbol name component sorted vector, if we haven't
210 yet. */
fcf23d5b 211 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
212
213 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
214 possible matches for LN_NO_PARAMS in the name component
215 vector. */
216 std::pair<std::vector<name_component>::const_iterator,
217 std::vector<name_component>::const_iterator>
3b00ef10 218 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
219 enum language lang,
220 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
221
222 /* Prevent deleting/destroying via a base class pointer. */
223protected:
224 ~mapped_index_base() = default;
225};
226
42c2c694
TT
227/* This is a view into the index that converts from bytes to an
228 offset_type, and allows indexing. Unaligned bytes are specifically
229 allowed here, and handled via unpacking. */
230
231class offset_view
232{
233public:
234 offset_view () = default;
235
236 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
237 : m_bytes (bytes)
238 {
239 }
240
241 /* Extract the INDEXth offset_type from the array. */
242 offset_type operator[] (size_t index) const
243 {
244 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
245 return (offset_type) extract_unsigned_integer (bytes,
246 sizeof (offset_type),
247 BFD_ENDIAN_LITTLE);
248 }
249
250 /* Return the number of offset_types in this array. */
251 size_t size () const
252 {
253 return m_bytes.size () / sizeof (offset_type);
254 }
255
256 /* Return true if this view is empty. */
257 bool empty () const
258 {
259 return m_bytes.empty ();
260 }
261
262private:
263 /* The underlying bytes. */
264 gdb::array_view<const gdb_byte> m_bytes;
265};
266
9291a0cd
TT
267/* A description of the mapped index. The file format is described in
268 a comment by the code that writes the index. */
fc898b42 269struct mapped_index final : public mapped_index_base
9291a0cd 270{
559a7a62 271 /* Index data format version. */
3063847f 272 int version = 0;
559a7a62 273
f00a2de2
PA
274 /* The address table data. */
275 gdb::array_view<const gdb_byte> address_table;
b11b1f88 276
3876f04e 277 /* The symbol table, implemented as a hash table. */
42c2c694 278 offset_view symbol_table;
b11b1f88 279
9291a0cd 280 /* A pointer to the constant pool. */
42c2c694
TT
281 gdb::array_view<const gdb_byte> constant_pool;
282
283 /* Return the index into the constant pool of the name of the IDXth
284 symbol in the symbol table. */
285 offset_type symbol_name_index (offset_type idx) const
286 {
287 return symbol_table[2 * idx];
288 }
289
290 /* Return the index into the constant pool of the CU vector of the
291 IDXth symbol in the symbol table. */
292 offset_type symbol_vec_index (offset_type idx) const
293 {
294 return symbol_table[2 * idx + 1];
295 }
3f563c84 296
44ed8f3e
PA
297 bool symbol_name_slot_invalid (offset_type idx) const override
298 {
42c2c694
TT
299 return (symbol_name_index (idx) == 0
300 && symbol_vec_index (idx) == 0);
44ed8f3e 301 }
5c58de74 302
3f563c84
PA
303 /* Convenience method to get at the name of the symbol at IDX in the
304 symbol table. */
fcf23d5b
SM
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
42c2c694
TT
307 {
308 return (const char *) (this->constant_pool.data ()
309 + symbol_name_index (idx));
310 }
5c58de74 311
44ed8f3e 312 size_t symbol_name_count () const override
42c2c694 313 { return this->symbol_table.size () / 2; }
9291a0cd
TT
314};
315
927aa2e7
JK
316/* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
fc898b42 318struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
319{
320 bfd_endian dwarf5_byte_order;
321 bool dwarf5_is_dwarf64;
322 bool augmentation_is_gdb;
323 uint8_t offset_size;
324 uint32_t cu_count = 0;
325 uint32_t tu_count, bucket_count, name_count;
326 const gdb_byte *cu_table_reordered, *tu_table_reordered;
327 const uint32_t *bucket_table_reordered, *hash_table_reordered;
328 const gdb_byte *name_table_string_offs_reordered;
329 const gdb_byte *name_table_entry_offs_reordered;
330 const gdb_byte *entry_pool;
331
332 struct index_val
333 {
334 ULONGEST dwarf_tag;
335 struct attr
336 {
337 /* Attribute name DW_IDX_*. */
338 ULONGEST dw_idx;
339
340 /* Attribute form DW_FORM_*. */
341 ULONGEST form;
342
343 /* Value if FORM is DW_FORM_implicit_const. */
344 LONGEST implicit_const;
345 };
346 std::vector<attr> attr_vec;
347 };
348
349 std::unordered_map<ULONGEST, index_val> abbrev_map;
350
fcf23d5b
SM
351 const char *namei_to_name
352 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
353
354 /* Implementation of the mapped_index_base virtual interface, for
355 the name_components cache. */
356
fcf23d5b
SM
357 const char *symbol_name_at
358 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
359 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
360
361 size_t symbol_name_count () const override
362 { return this->name_count; }
927aa2e7
JK
363};
364
cd4fb1b2 365/* See dwarf2read.h. */
ed2dc618 366
cd4fb1b2 367dwarf2_per_objfile *
ed2dc618
SM
368get_dwarf2_per_objfile (struct objfile *objfile)
369{
5bfd760d 370 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 371}
c906108c 372
251d32d9 373/* Default names of the debugging sections. */
c906108c 374
233a11ab
CS
375/* Note that if the debugging section has been compressed, it might
376 have a name like .zdebug_info. */
377
9938d15a 378const struct dwarf2_debug_sections dwarf2_elf_names =
9cdd5dbd 379{
251d32d9
TG
380 { ".debug_info", ".zdebug_info" },
381 { ".debug_abbrev", ".zdebug_abbrev" },
382 { ".debug_line", ".zdebug_line" },
383 { ".debug_loc", ".zdebug_loc" },
43988095 384 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 385 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 386 { ".debug_macro", ".zdebug_macro" },
251d32d9 387 { ".debug_str", ".zdebug_str" },
18a8505e 388 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 389 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 390 { ".debug_ranges", ".zdebug_ranges" },
43988095 391 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 392 { ".debug_types", ".zdebug_types" },
3019eac3 393 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
394 { ".debug_frame", ".zdebug_frame" },
395 { ".eh_frame", NULL },
24d3216f 396 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
397 { ".debug_names", ".zdebug_names" },
398 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 399 23
251d32d9 400};
c906108c 401
80626a55 402/* List of DWO/DWP sections. */
3019eac3 403
80626a55 404static const struct dwop_section_names
3019eac3
DE
405{
406 struct dwarf2_section_names abbrev_dwo;
407 struct dwarf2_section_names info_dwo;
408 struct dwarf2_section_names line_dwo;
409 struct dwarf2_section_names loc_dwo;
43988095 410 struct dwarf2_section_names loclists_dwo;
09262596
DE
411 struct dwarf2_section_names macinfo_dwo;
412 struct dwarf2_section_names macro_dwo;
d0ce17d8 413 struct dwarf2_section_names rnglists_dwo;
3019eac3
DE
414 struct dwarf2_section_names str_dwo;
415 struct dwarf2_section_names str_offsets_dwo;
416 struct dwarf2_section_names types_dwo;
80626a55
DE
417 struct dwarf2_section_names cu_index;
418 struct dwarf2_section_names tu_index;
3019eac3 419}
80626a55 420dwop_section_names =
3019eac3
DE
421{
422 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
423 { ".debug_info.dwo", ".zdebug_info.dwo" },
424 { ".debug_line.dwo", ".zdebug_line.dwo" },
425 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 426 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
427 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
428 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
d0ce17d8 429 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
3019eac3
DE
430 { ".debug_str.dwo", ".zdebug_str.dwo" },
431 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
432 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
433 { ".debug_cu_index", ".zdebug_cu_index" },
434 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
435};
436
c906108c
SS
437/* local data types */
438
d0ce17d8
CT
439/* The location list and range list sections (.debug_loclists & .debug_rnglists)
440 begin with a header, which contains the following information. */
441struct loclists_rnglists_header
41144253 442{
443 /* A 4-byte or 12-byte length containing the length of the
444 set of entries for this compilation unit, not including the
445 length field itself. */
446 unsigned int length;
447
448 /* A 2-byte version identifier. */
449 short version;
450
451 /* A 1-byte unsigned integer containing the size in bytes of an address on
452 the target system. */
453 unsigned char addr_size;
454
455 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
456 on the target system. */
457 unsigned char segment_collector_size;
458
459 /* A 4-byte count of the number of offsets that follow the header. */
460 unsigned int offset_entry_count;
461};
462
094b34ac
DE
463/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
464 This includes type_unit_group and quick_file_names. */
465
466struct stmt_list_hash
467{
468 /* The DWO unit this table is from or NULL if there is none. */
469 struct dwo_unit *dwo_unit;
470
471 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 472 sect_offset line_sect_off;
094b34ac
DE
473};
474
5989a64e 475/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
476 an object of this type. This contains elements of type unit groups
477 that can be shared across objfiles. The non-shareable parts are in
478 type_unit_group_unshareable. */
f4dc4d17 479
0d305d5c 480struct type_unit_group : public dwarf2_per_cu_data
f4dc4d17 481{
0186c6a7
DE
482 /* The TUs that share this DW_AT_stmt_list entry.
483 This is added to while parsing type units to build partial symtabs,
484 and is deleted afterwards and not used again. */
0d305d5c 485 std::vector<signatured_type *> *tus = nullptr;
f4dc4d17 486
094b34ac 487 /* The data used to construct the hash key. */
0d305d5c 488 struct stmt_list_hash hash {};
f4dc4d17
DE
489};
490
73869dc2 491/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
492
493struct dwo_sections
494{
495 struct dwarf2_section_info abbrev;
3019eac3
DE
496 struct dwarf2_section_info line;
497 struct dwarf2_section_info loc;
43988095 498 struct dwarf2_section_info loclists;
09262596
DE
499 struct dwarf2_section_info macinfo;
500 struct dwarf2_section_info macro;
d0ce17d8 501 struct dwarf2_section_info rnglists;
3019eac3
DE
502 struct dwarf2_section_info str;
503 struct dwarf2_section_info str_offsets;
80626a55
DE
504 /* In the case of a virtual DWO file, these two are unused. */
505 struct dwarf2_section_info info;
fd5866f6 506 std::vector<dwarf2_section_info> types;
3019eac3
DE
507};
508
c88ee1f0 509/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
510
511struct dwo_unit
512{
513 /* Backlink to the containing struct dwo_file. */
514 struct dwo_file *dwo_file;
515
516 /* The "id" that distinguishes this CU/TU.
517 .debug_info calls this "dwo_id", .debug_types calls this "signature".
518 Since signatures came first, we stick with it for consistency. */
519 ULONGEST signature;
520
521 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 522 struct dwarf2_section_info *section;
3019eac3 523
9c541725
PA
524 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
525 sect_offset sect_off;
3019eac3
DE
526 unsigned int length;
527
528 /* For types, offset in the type's DIE of the type defined by this TU. */
529 cu_offset type_offset_in_tu;
530};
531
73869dc2
DE
532/* include/dwarf2.h defines the DWP section codes.
533 It defines a max value but it doesn't define a min value, which we
534 use for error checking, so provide one. */
535
536enum dwp_v2_section_ids
537{
538 DW_SECT_MIN = 1
539};
540
80626a55 541/* Data for one DWO file.
57d63ce2
DE
542
543 This includes virtual DWO files (a virtual DWO file is a DWO file as it
544 appears in a DWP file). DWP files don't really have DWO files per se -
545 comdat folding of types "loses" the DWO file they came from, and from
546 a high level view DWP files appear to contain a mass of random types.
547 However, to maintain consistency with the non-DWP case we pretend DWP
548 files contain virtual DWO files, and we assign each TU with one virtual
549 DWO file (generally based on the line and abbrev section offsets -
550 a heuristic that seems to work in practice). */
3019eac3
DE
551
552struct dwo_file
553{
51ac9db5
SM
554 dwo_file () = default;
555 DISABLE_COPY_AND_ASSIGN (dwo_file);
556
18a8505e 557 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
558 For virtual DWO files the name is constructed from the section offsets
559 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
560 from related CU+TUs. */
51ac9db5 561 const char *dwo_name = nullptr;
0ac5b59e
DE
562
563 /* The DW_AT_comp_dir attribute. */
51ac9db5 564 const char *comp_dir = nullptr;
3019eac3 565
80626a55
DE
566 /* The bfd, when the file is open. Otherwise this is NULL.
567 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 568 gdb_bfd_ref_ptr dbfd;
3019eac3 569
73869dc2 570 /* The sections that make up this DWO file.
d2854d8d 571 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
73869dc2 572 sections (for lack of a better name). */
51ac9db5 573 struct dwo_sections sections {};
3019eac3 574
33c5cd75
DB
575 /* The CUs in the file.
576 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
577 an extension to handle LLVM's Link Time Optimization output (where
578 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 579 htab_up cus;
3019eac3
DE
580
581 /* Table of TUs in the file.
582 Each element is a struct dwo_unit. */
b0b6a987 583 htab_up tus;
3019eac3
DE
584};
585
80626a55
DE
586/* These sections are what may appear in a DWP file. */
587
588struct dwp_sections
589{
d2854d8d 590 /* These are used by all DWP versions (1, 2 and 5). */
80626a55
DE
591 struct dwarf2_section_info str;
592 struct dwarf2_section_info cu_index;
593 struct dwarf2_section_info tu_index;
73869dc2 594
d2854d8d 595 /* These are only used by DWP version 2 and version 5 files.
73869dc2
DE
596 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
597 sections are referenced by section number, and are not recorded here.
d2854d8d
CT
598 In DWP version 2 or 5 there is at most one copy of all these sections,
599 each section being (effectively) comprised of the concatenation of all of
600 the individual sections that exist in the version 1 format.
73869dc2
DE
601 To keep the code simple we treat each of these concatenated pieces as a
602 section itself (a virtual section?). */
603 struct dwarf2_section_info abbrev;
604 struct dwarf2_section_info info;
605 struct dwarf2_section_info line;
606 struct dwarf2_section_info loc;
d2854d8d 607 struct dwarf2_section_info loclists;
73869dc2
DE
608 struct dwarf2_section_info macinfo;
609 struct dwarf2_section_info macro;
d2854d8d 610 struct dwarf2_section_info rnglists;
73869dc2
DE
611 struct dwarf2_section_info str_offsets;
612 struct dwarf2_section_info types;
80626a55
DE
613};
614
73869dc2
DE
615/* These sections are what may appear in a virtual DWO file in DWP version 1.
616 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 617
73869dc2 618struct virtual_v1_dwo_sections
80626a55
DE
619{
620 struct dwarf2_section_info abbrev;
621 struct dwarf2_section_info line;
622 struct dwarf2_section_info loc;
623 struct dwarf2_section_info macinfo;
624 struct dwarf2_section_info macro;
625 struct dwarf2_section_info str_offsets;
626 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 627 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
628 struct dwarf2_section_info info_or_types;
629};
630
d2854d8d 631/* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
73869dc2
DE
632 In version 2, the sections of the DWO files are concatenated together
633 and stored in one section of that name. Thus each ELF section contains
634 several "virtual" sections. */
635
d2854d8d 636struct virtual_v2_or_v5_dwo_sections
73869dc2
DE
637{
638 bfd_size_type abbrev_offset;
639 bfd_size_type abbrev_size;
640
641 bfd_size_type line_offset;
642 bfd_size_type line_size;
643
644 bfd_size_type loc_offset;
645 bfd_size_type loc_size;
646
d2854d8d
CT
647 bfd_size_type loclists_offset;
648 bfd_size_type loclists_size;
649
73869dc2
DE
650 bfd_size_type macinfo_offset;
651 bfd_size_type macinfo_size;
652
653 bfd_size_type macro_offset;
654 bfd_size_type macro_size;
655
d2854d8d
CT
656 bfd_size_type rnglists_offset;
657 bfd_size_type rnglists_size;
658
73869dc2
DE
659 bfd_size_type str_offsets_offset;
660 bfd_size_type str_offsets_size;
661
662 /* Each DWP hash table entry records one CU or one TU.
663 That is recorded here, and copied to dwo_unit.section. */
664 bfd_size_type info_or_types_offset;
665 bfd_size_type info_or_types_size;
666};
667
80626a55
DE
668/* Contents of DWP hash tables. */
669
670struct dwp_hash_table
671{
73869dc2 672 uint32_t version, nr_columns;
80626a55 673 uint32_t nr_units, nr_slots;
73869dc2
DE
674 const gdb_byte *hash_table, *unit_table;
675 union
676 {
677 struct
678 {
679 const gdb_byte *indices;
680 } v1;
681 struct
682 {
683 /* This is indexed by column number and gives the id of the section
684 in that column. */
685#define MAX_NR_V2_DWO_SECTIONS \
686 (1 /* .debug_info or .debug_types */ \
687 + 1 /* .debug_abbrev */ \
688 + 1 /* .debug_line */ \
689 + 1 /* .debug_loc */ \
690 + 1 /* .debug_str_offsets */ \
691 + 1 /* .debug_macro or .debug_macinfo */)
692 int section_ids[MAX_NR_V2_DWO_SECTIONS];
693 const gdb_byte *offsets;
694 const gdb_byte *sizes;
695 } v2;
d2854d8d
CT
696 struct
697 {
698 /* This is indexed by column number and gives the id of the section
699 in that column. */
700#define MAX_NR_V5_DWO_SECTIONS \
701 (1 /* .debug_info */ \
702 + 1 /* .debug_abbrev */ \
703 + 1 /* .debug_line */ \
704 + 1 /* .debug_loclists */ \
705 + 1 /* .debug_str_offsets */ \
706 + 1 /* .debug_macro */ \
707 + 1 /* .debug_rnglists */)
708 int section_ids[MAX_NR_V5_DWO_SECTIONS];
709 const gdb_byte *offsets;
710 const gdb_byte *sizes;
711 } v5;
73869dc2 712 } section_pool;
80626a55
DE
713};
714
715/* Data for one DWP file. */
716
717struct dwp_file
718{
400174b1
TT
719 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
720 : name (name_),
721 dbfd (std::move (abfd))
722 {
723 }
724
80626a55
DE
725 /* Name of the file. */
726 const char *name;
727
73869dc2 728 /* File format version. */
400174b1 729 int version = 0;
73869dc2 730
93417882 731 /* The bfd. */
400174b1 732 gdb_bfd_ref_ptr dbfd;
80626a55
DE
733
734 /* Section info for this file. */
400174b1 735 struct dwp_sections sections {};
80626a55 736
57d63ce2 737 /* Table of CUs in the file. */
400174b1 738 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
739
740 /* Table of TUs in the file. */
400174b1 741 const struct dwp_hash_table *tus = nullptr;
80626a55 742
19ac8c2e 743 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
744 htab_up loaded_cus;
745 htab_up loaded_tus;
80626a55 746
73869dc2
DE
747 /* Table to map ELF section numbers to their sections.
748 This is only needed for the DWP V1 file format. */
400174b1
TT
749 unsigned int num_sections = 0;
750 asection **elf_sections = nullptr;
80626a55
DE
751};
752
0963b4bd
MS
753/* Struct used to pass misc. parameters to read_die_and_children, et
754 al. which are used for both .debug_info and .debug_types dies.
755 All parameters here are unchanging for the life of the call. This
dee91e82 756 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
757
758struct die_reader_specs
759{
a32a8923 760 /* The bfd of die_section. */
0280fdcc 761 bfd *abfd;
93311388
DE
762
763 /* The CU of the DIE we are parsing. */
764 struct dwarf2_cu *cu;
765
80626a55 766 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
767 struct dwo_file *dwo_file;
768
dee91e82 769 /* The section the die comes from.
3019eac3 770 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
771 struct dwarf2_section_info *die_section;
772
773 /* die_section->buffer. */
d521ce57 774 const gdb_byte *buffer;
f664829e
DE
775
776 /* The end of the buffer. */
777 const gdb_byte *buffer_end;
a2ce51a0 778
685af9cd
TT
779 /* The abbreviation table to use when reading the DIEs. */
780 struct abbrev_table *abbrev_table;
93311388
DE
781};
782
c0ab21c2
TT
783/* A subclass of die_reader_specs that holds storage and has complex
784 constructor and destructor behavior. */
785
786class cutu_reader : public die_reader_specs
787{
788public:
789
ab432490
SM
790 cutu_reader (dwarf2_per_cu_data *this_cu,
791 dwarf2_per_objfile *per_objfile,
c0ab21c2 792 struct abbrev_table *abbrev_table,
2e671100 793 dwarf2_cu *existing_cu,
c0ab21c2
TT
794 bool skip_partial);
795
796 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 797 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
798 struct dwarf2_cu *parent_cu = nullptr,
799 struct dwo_file *dwo_file = nullptr);
800
c0ab21c2
TT
801 DISABLE_COPY_AND_ASSIGN (cutu_reader);
802
803 const gdb_byte *info_ptr = nullptr;
804 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
805 bool dummy_p = false;
806
6751ebae
TT
807 /* Release the new CU, putting it on the chain. This cannot be done
808 for dummy CUs. */
809 void keep ();
810
c0ab21c2 811private:
9e021579
SM
812 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
813 dwarf2_per_objfile *per_objfile,
2e671100 814 dwarf2_cu *existing_cu);
c0ab21c2
TT
815
816 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
817 std::unique_ptr<dwarf2_cu> m_new_cu;
818
819 /* The ordinary abbreviation table. */
820 abbrev_table_up m_abbrev_table_holder;
821
822 /* The DWO abbreviation table. */
823 abbrev_table_up m_dwo_abbrev_table;
824};
dee91e82 825
c906108c 826/* When we construct a partial symbol table entry we only
0963b4bd 827 need this much information. */
6f06d47b 828struct partial_die_info : public allocate_on_obstack
c906108c 829 {
7c32eebb 830 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
6f06d47b
YQ
831
832 /* Disable assign but still keep copy ctor, which is needed
833 load_partial_dies. */
834 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1b453aed 835 partial_die_info (const partial_die_info &) = default;
6f06d47b 836
52356b79
YQ
837 /* Adjust the partial die before generating a symbol for it. This
838 function may set the is_external flag or change the DIE's
839 name. */
840 void fixup (struct dwarf2_cu *cu);
841
48fbe735
YQ
842 /* Read a minimal amount of information into the minimal die
843 structure. */
844 const gdb_byte *read (const struct die_reader_specs *reader,
845 const struct abbrev_info &abbrev,
846 const gdb_byte *info_ptr);
847
7d00ffec
TT
848 /* Compute the name of this partial DIE. This memoizes the
849 result, so it is safe to call multiple times. */
850 const char *name (dwarf2_cu *cu);
851
72bf9492 852 /* Offset of this DIE. */
6f06d47b 853 const sect_offset sect_off;
72bf9492
DJ
854
855 /* DWARF-2 tag for this DIE. */
6f06d47b 856 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 857
72bf9492 858 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
859 const unsigned int has_children : 1;
860
72bf9492
DJ
861 unsigned int is_external : 1;
862 unsigned int is_declaration : 1;
863 unsigned int has_type : 1;
864 unsigned int has_specification : 1;
865 unsigned int has_pc_info : 1;
481860b3 866 unsigned int may_be_inlined : 1;
72bf9492 867
0c1b455e
TT
868 /* This DIE has been marked DW_AT_main_subprogram. */
869 unsigned int main_subprogram : 1;
870
72bf9492
DJ
871 /* Flag set if the SCOPE field of this structure has been
872 computed. */
873 unsigned int scope_set : 1;
874
fa4028e9
JB
875 /* Flag set if the DIE has a byte_size attribute. */
876 unsigned int has_byte_size : 1;
877
ff908ebf
AW
878 /* Flag set if the DIE has a DW_AT_const_value attribute. */
879 unsigned int has_const_value : 1;
880
98bfdba5
PA
881 /* Flag set if any of the DIE's children are template arguments. */
882 unsigned int has_template_arguments : 1;
883
52356b79 884 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
885 unsigned int fixup_called : 1;
886
36586728
TT
887 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
888 unsigned int is_dwz : 1;
889
890 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
891 unsigned int spec_is_dwz : 1;
892
7d00ffec
TT
893 unsigned int canonical_name : 1;
894
72bf9492 895 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 896 sometimes a default name for unnamed DIEs. */
7d00ffec 897 const char *raw_name = nullptr;
72bf9492 898
abc72ce4 899 /* The linkage name, if present. */
6f06d47b 900 const char *linkage_name = nullptr;
abc72ce4 901
72bf9492
DJ
902 /* The scope to prepend to our children. This is generally
903 allocated on the comp_unit_obstack, so will disappear
904 when this compilation unit leaves the cache. */
6f06d47b 905 const char *scope = nullptr;
72bf9492 906
95554aad
TT
907 /* Some data associated with the partial DIE. The tag determines
908 which field is live. */
909 union
910 {
911 /* The location description associated with this DIE, if any. */
912 struct dwarf_block *locdesc;
913 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 914 sect_offset sect_off;
6f06d47b 915 } d {};
72bf9492
DJ
916
917 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
918 CORE_ADDR lowpc = 0;
919 CORE_ADDR highpc = 0;
72bf9492 920
93311388 921 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 922 DW_AT_sibling, if any. */
48fbe735
YQ
923 /* NOTE: This member isn't strictly necessary, partial_die_info::read
924 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 925 const gdb_byte *sibling = nullptr;
72bf9492
DJ
926
927 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
928 DW_AT_specification (or DW_AT_abstract_origin or
929 DW_AT_extension). */
6f06d47b 930 sect_offset spec_offset {};
72bf9492
DJ
931
932 /* Pointers to this DIE's parent, first child, and next sibling,
933 if any. */
6f06d47b
YQ
934 struct partial_die_info *die_parent = nullptr;
935 struct partial_die_info *die_child = nullptr;
936 struct partial_die_info *die_sibling = nullptr;
937
938 friend struct partial_die_info *
939 dwarf2_cu::find_partial_die (sect_offset sect_off);
940
941 private:
942 /* Only need to do look up in dwarf2_cu::find_partial_die. */
943 partial_die_info (sect_offset sect_off)
944 : partial_die_info (sect_off, DW_TAG_padding, 0)
945 {
946 }
947
948 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
949 int has_children_)
950 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
951 {
952 is_external = 0;
953 is_declaration = 0;
954 has_type = 0;
955 has_specification = 0;
956 has_pc_info = 0;
957 may_be_inlined = 0;
958 main_subprogram = 0;
959 scope_set = 0;
960 has_byte_size = 0;
961 has_const_value = 0;
962 has_template_arguments = 0;
963 fixup_called = 0;
964 is_dwz = 0;
965 spec_is_dwz = 0;
7d00ffec 966 canonical_name = 0;
6f06d47b 967 }
c906108c
SS
968 };
969
c906108c
SS
970/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
971 but this would require a corresponding change in unpack_field_as_long
972 and friends. */
973static int bits_per_byte = 8;
974
9c6a1327
TT
975struct variant_part_builder;
976
977/* When reading a variant, we track a bit more information about the
978 field, and store it in an object of this type. */
2ddeaf8a
TT
979
980struct variant_field
981{
9c6a1327
TT
982 int first_field = -1;
983 int last_field = -1;
984
985 /* A variant can contain other variant parts. */
986 std::vector<variant_part_builder> variant_parts;
987
2ddeaf8a
TT
988 /* If we see a DW_TAG_variant, then this will be set if this is the
989 default branch. */
9c6a1327
TT
990 bool default_branch = false;
991 /* If we see a DW_AT_discr_value, then this will be the discriminant
992 value. */
993 ULONGEST discriminant_value = 0;
994 /* If we see a DW_AT_discr_list, then this is a pointer to the list
995 data. */
996 struct dwarf_block *discr_list_data = nullptr;
997};
998
999/* This represents a DW_TAG_variant_part. */
1000
1001struct variant_part_builder
1002{
1003 /* The offset of the discriminant field. */
1004 sect_offset discriminant_offset {};
1005
1006 /* Variants that are direct children of this variant part. */
1007 std::vector<variant_field> variants;
1008
1009 /* True if we're currently reading a variant. */
1010 bool processing_variant = false;
2ddeaf8a
TT
1011};
1012
52059ffd
TT
1013struct nextfield
1014{
be2daae6
TT
1015 int accessibility = 0;
1016 int virtuality = 0;
9c6a1327
TT
1017 /* Variant parts need to find the discriminant, which is a DIE
1018 reference. We track the section offset of each field to make
1019 this link. */
1020 sect_offset offset;
be2daae6 1021 struct field field {};
52059ffd
TT
1022};
1023
1024struct fnfieldlist
1025{
be2daae6
TT
1026 const char *name = nullptr;
1027 std::vector<struct fn_field> fnfields;
52059ffd
TT
1028};
1029
c906108c
SS
1030/* The routines that read and process dies for a C struct or C++ class
1031 pass lists of data member fields and lists of member function fields
1032 in an instance of a field_info structure, as defined below. */
1033struct field_info
2de01bdb
SM
1034{
1035 /* List of data member and baseclasses fields. */
1036 std::vector<struct nextfield> fields;
1037 std::vector<struct nextfield> baseclasses;
1038
1039 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1040 bool non_public_fields = false;
2de01bdb
SM
1041
1042 /* Member function fieldlist array, contains name of possibly overloaded
1043 member function, number of overloaded member functions and a pointer
1044 to the head of the member function field chain. */
1045 std::vector<struct fnfieldlist> fnfieldlists;
1046
1047 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1048 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1049 std::vector<struct decl_field> typedef_field_list;
1050
1051 /* Nested types defined by this class and the number of elements in this
1052 list. */
1053 std::vector<struct decl_field> nested_types_list;
1054
1055 /* If non-null, this is the variant part we are currently
1056 reading. */
1057 variant_part_builder *current_variant_part = nullptr;
1058 /* This holds all the top-level variant parts attached to the type
1059 we're reading. */
1060 std::vector<variant_part_builder> variant_parts;
1061
1062 /* Return the total number of fields (including baseclasses). */
1063 int nfields () const
c5aa993b 1064 {
2de01bdb
SM
1065 return fields.size () + baseclasses.size ();
1066 }
1067};
c906108c 1068
ae038cb0
DJ
1069/* Loaded secondary compilation units are kept in memory until they
1070 have not been referenced for the processing of this many
1071 compilation units. Set this to zero to disable caching. Cache
1072 sizes of up to at least twenty will improve startup time for
1073 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1074static int dwarf_max_cache_age = 5;
920d2a44 1075static void
b4f54984
DE
1076show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1077 struct cmd_list_element *c, const char *value)
920d2a44 1078{
3e43a32a 1079 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1080 "DWARF compilation units is %s.\n"),
920d2a44
AC
1081 value);
1082}
4390d890 1083\f
c906108c
SS
1084/* local function prototypes */
1085
918dd910
JK
1086static void dwarf2_find_base_address (struct die_info *die,
1087 struct dwarf2_cu *cu);
1088
891813be 1089static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1090 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1091 const char *name);
0018ea6f 1092
f1902523
JK
1093static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1094 const gdb_byte *info_ptr,
3e225074 1095 struct die_info *type_unit_die);
f1902523 1096
976ca316 1097static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1098
72bf9492
DJ
1099static void scan_partial_symbols (struct partial_die_info *,
1100 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1101 int, struct dwarf2_cu *);
c906108c 1102
72bf9492
DJ
1103static void add_partial_symbol (struct partial_die_info *,
1104 struct dwarf2_cu *);
63d06c5c 1105
72bf9492
DJ
1106static void add_partial_namespace (struct partial_die_info *pdi,
1107 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1108 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1109
5d7cb8df 1110static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1111 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1112 struct dwarf2_cu *cu);
1113
72bf9492
DJ
1114static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1115 struct dwarf2_cu *cu);
91c24f0a 1116
bc30ff58
JB
1117static void add_partial_subprogram (struct partial_die_info *pdi,
1118 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1119 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1120
d521ce57 1121static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1122
dee91e82 1123static struct partial_die_info *load_partial_dies
d521ce57 1124 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1125
fb816e8b
TV
1126/* A pair of partial_die_info and compilation unit. */
1127struct cu_partial_die_info
1128{
1129 /* The compilation unit of the partial_die_info. */
1130 struct dwarf2_cu *cu;
1131 /* A partial_die_info. */
1132 struct partial_die_info *pdi;
122cf0f2
AB
1133
1134 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1135 : cu (cu),
1136 pdi (pdi)
405feb71 1137 { /* Nothing. */ }
122cf0f2
AB
1138
1139private:
1140 cu_partial_die_info () = delete;
fb816e8b
TV
1141};
1142
122cf0f2
AB
1143static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1144 struct dwarf2_cu *);
72bf9492 1145
d521ce57 1146static const gdb_byte *read_attribute (const struct die_reader_specs *,
4444f407
TT
1147 struct attribute *,
1148 const struct attr_abbrev *,
7a5f294d 1149 const gdb_byte *);
18a8505e
AT
1150
1151static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1152 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1153
1154static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1155
976ca316
SM
1156static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1157 dwarf2_section_info *, sect_offset);
f4dc4d17 1158
ed2dc618 1159static const char *read_indirect_string
976ca316 1160 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1161 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1162
ed2dc618 1163static const char *read_indirect_string_at_offset
976ca316 1164 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1165
d521ce57
TT
1166static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1167 const gdb_byte *,
3019eac3
DE
1168 unsigned int *);
1169
18a8505e
AT
1170static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1171 ULONGEST str_index);
1172
1173static const char *read_stub_str_index (struct dwarf2_cu *cu,
1174 ULONGEST str_index);
3019eac3 1175
e142c38c
DJ
1176static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1177 struct dwarf2_cu *);
c906108c 1178
7d45c7c3 1179static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
dda83cd7 1180 struct dwarf2_cu *cu);
7d45c7c3 1181
a084a2a6
AT
1182static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1183
05cf31d1 1184static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
dda83cd7 1185 struct dwarf2_cu *cu);
05cf31d1 1186
e142c38c 1187static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1188
e142c38c 1189static struct die_info *die_specification (struct die_info *die,
f2f0e013 1190 struct dwarf2_cu **);
63d06c5c 1191
9c541725 1192static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1193 struct dwarf2_cu *cu);
debd256d 1194
f3f5162e 1195static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1196 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1197 CORE_ADDR, int decode_mapping);
c906108c 1198
804d2729
TT
1199static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1200 const char *);
c906108c 1201
a14ed312 1202static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1203 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1204
ff39bb5e 1205static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1206 struct dwarf2_cu *);
c906108c 1207
ff39bb5e 1208static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1209 struct type *type,
1210 const char *name,
1211 struct obstack *obstack,
12df843f 1212 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1213 const gdb_byte **bytes,
98bfdba5 1214 struct dwarf2_locexpr_baton **baton);
2df3850c 1215
57567375
TT
1216static struct type *read_subrange_index_type (struct die_info *die,
1217 struct dwarf2_cu *cu);
1218
e7c27a73 1219static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1220
b4ba55a1
JB
1221static int need_gnat_info (struct dwarf2_cu *);
1222
3e43a32a
MS
1223static struct type *die_descriptive_type (struct die_info *,
1224 struct dwarf2_cu *);
b4ba55a1
JB
1225
1226static void set_descriptive_type (struct type *, struct die_info *,
1227 struct dwarf2_cu *);
1228
e7c27a73
DJ
1229static struct type *die_containing_type (struct die_info *,
1230 struct dwarf2_cu *);
c906108c 1231
ff39bb5e 1232static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1233 struct dwarf2_cu *);
c906108c 1234
f792889a 1235static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1236
673bfd45
DE
1237static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1238
0d5cff50 1239static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1240
6e70227d 1241static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1242 const char *suffix, int physname,
1243 struct dwarf2_cu *cu);
63d06c5c 1244
e7c27a73 1245static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1246
348e048f
DE
1247static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1248
e7c27a73 1249static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1250
e7c27a73 1251static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1252
96408a79
SA
1253static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1254
71a3c369
TT
1255static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1256
41144253 1257/* Return the .debug_loclists section to use for cu. */
1258static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1259
d0ce17d8
CT
1260/* Return the .debug_rnglists section to use for cu. */
1261static struct dwarf2_section_info *cu_debug_rnglists_section
1262 (struct dwarf2_cu *cu, dwarf_tag tag);
1263
3a2b436a 1264/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1265 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1266enum pc_bounds_kind
1267{
e385593e 1268 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1269 PC_BOUNDS_NOT_PRESENT,
1270
e385593e
JK
1271 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1272 were present but they do not form a valid range of PC addresses. */
1273 PC_BOUNDS_INVALID,
1274
3a2b436a
JK
1275 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1276 PC_BOUNDS_RANGES,
1277
1278 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1279 PC_BOUNDS_HIGH_LOW,
1280};
1281
1282static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1283 CORE_ADDR *, CORE_ADDR *,
1284 struct dwarf2_cu *,
891813be 1285 dwarf2_psymtab *);
c906108c 1286
fae299cd
DC
1287static void get_scope_pc_bounds (struct die_info *,
1288 CORE_ADDR *, CORE_ADDR *,
1289 struct dwarf2_cu *);
1290
801e3a5b 1291static void dwarf2_record_block_ranges (struct die_info *, struct block *,
dda83cd7 1292 CORE_ADDR, struct dwarf2_cu *);
801e3a5b 1293
a14ed312 1294static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1295 struct dwarf2_cu *);
c906108c 1296
a14ed312 1297static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1298 struct type *, struct dwarf2_cu *);
c906108c 1299
a14ed312 1300static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1301 struct die_info *, struct type *,
e7c27a73 1302 struct dwarf2_cu *);
c906108c 1303
a14ed312 1304static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1305 struct type *,
1306 struct dwarf2_cu *);
c906108c 1307
134d01f1 1308static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1309
e7c27a73 1310static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1311
e7c27a73 1312static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1313
5d7cb8df
JK
1314static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1315
804d2729 1316static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1317
27aa8d6a
SW
1318static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1319
74921315
KS
1320static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1321
f55ee35c
JK
1322static struct type *read_module_type (struct die_info *die,
1323 struct dwarf2_cu *cu);
1324
38d518c9 1325static const char *namespace_name (struct die_info *die,
e142c38c 1326 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1327
134d01f1 1328static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1329
7d79de9a
TT
1330static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1331 bool * = nullptr);
c906108c 1332
6e70227d 1333static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1334 struct dwarf2_cu *);
1335
bf6af496 1336static struct die_info *read_die_and_siblings_1
d521ce57 1337 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1338 struct die_info *);
639d11d3 1339
dee91e82 1340static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1341 const gdb_byte *info_ptr,
1342 const gdb_byte **new_info_ptr,
639d11d3
DC
1343 struct die_info *parent);
1344
d521ce57
TT
1345static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1346 struct die_info **, const gdb_byte *,
3e225074 1347 int);
3019eac3 1348
d521ce57 1349static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1350 struct die_info **, const gdb_byte *);
93311388 1351
e7c27a73 1352static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1353
15d034d0 1354static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1355 struct objfile *);
71c25dea 1356
15d034d0 1357static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1358
15d034d0 1359static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1360 struct die_info *die,
1361 struct dwarf2_cu *cu);
1362
ca69b9e6
DE
1363static const char *dwarf2_physname (const char *name, struct die_info *die,
1364 struct dwarf2_cu *cu);
1365
e142c38c 1366static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1367 struct dwarf2_cu **);
9219021c 1368
d97bc12b
DE
1369static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1370
1371static void dump_die_for_error (struct die_info *);
1372
1373static void dump_die_1 (struct ui_file *, int level, int max_level,
1374 struct die_info *);
c906108c 1375
d97bc12b 1376/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1377
51545339 1378static void store_in_ref_table (struct die_info *,
10b3939b 1379 struct dwarf2_cu *);
c906108c 1380
348e048f 1381static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1382 const struct attribute *,
348e048f
DE
1383 struct dwarf2_cu **);
1384
10b3939b 1385static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1386 const struct attribute *,
f2f0e013 1387 struct dwarf2_cu **);
c906108c 1388
348e048f 1389static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1390 const struct attribute *,
348e048f
DE
1391 struct dwarf2_cu **);
1392
ac9ec31b
DE
1393static struct type *get_signatured_type (struct die_info *, ULONGEST,
1394 struct dwarf2_cu *);
1395
1396static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1397 const struct attribute *,
ac9ec31b
DE
1398 struct dwarf2_cu *);
1399
ab432490
SM
1400static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1401 dwarf2_per_objfile *per_objfile);
348e048f 1402
ab432490
SM
1403static void read_signatured_type (signatured_type *sig_type,
1404 dwarf2_per_objfile *per_objfile);
348e048f 1405
63e43d3a
PMR
1406static int attr_to_dynamic_prop (const struct attribute *attr,
1407 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1408 struct dynamic_prop *prop, struct type *type);
63e43d3a 1409
c906108c
SS
1410/* memory allocation interface */
1411
7b5a2f43 1412static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1413
b60c80d6 1414static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1415
43f3e411 1416static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1417
8cf6f0b1
TT
1418static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1419 struct dwarf2_loclist_baton *baton,
ff39bb5e 1420 const struct attribute *attr);
8cf6f0b1 1421
ff39bb5e 1422static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1423 struct symbol *sym,
f1e6e072
TT
1424 struct dwarf2_cu *cu,
1425 int is_block);
4c2df51b 1426
d521ce57
TT
1427static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1428 const gdb_byte *info_ptr,
7c32eebb 1429 const struct abbrev_info *abbrev);
4bb7a0a7 1430
72bf9492
DJ
1431static hashval_t partial_die_hash (const void *item);
1432
1433static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1434
ae038cb0 1435static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1436 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1437 dwarf2_per_objfile *per_objfile);
ae038cb0 1438
9816fde3 1439static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1440 struct die_info *comp_unit_die,
1441 enum language pretend_language);
93311388 1442
f792889a 1443static struct type *set_die_type (struct die_info *, struct type *,
57567375 1444 struct dwarf2_cu *, bool = false);
1c379e20 1445
976ca316 1446static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1447
ab432490
SM
1448static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1449 dwarf2_per_objfile *per_objfile,
4a636814 1450 dwarf2_cu *existing_cu,
ab432490
SM
1451 bool skip_partial,
1452 enum language pretend_language);
10b3939b 1453
8fc0b21d 1454static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1455 enum language pretend_language);
10b3939b 1456
8fc0b21d 1457static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1458 enum language pretend_language);
f4dc4d17 1459
b64f50a1 1460static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1461 dwarf2_per_cu_data *per_cu,
1462 dwarf2_per_objfile *per_objfile);
673bfd45 1463
f792889a 1464static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1465
120ce1b5
SM
1466static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1467 dwarf2_per_objfile *per_objfile,
95554aad
TT
1468 enum language pretend_language);
1469
976ca316 1470static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1471
b303c6f6
AB
1472/* Class, the destructor of which frees all allocated queue entries. This
1473 will only have work to do if an error was thrown while processing the
1474 dwarf. If no error was thrown then the queue entries should have all
1475 been processed, and freed, as we went along. */
1476
1477class dwarf2_queue_guard
1478{
1479public:
39856def
TT
1480 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1481 : m_per_objfile (per_objfile)
1482 {
08ac5771
SM
1483 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1484
1485 m_per_objfile->per_bfd->queue.emplace ();
39856def 1486 }
b303c6f6
AB
1487
1488 /* Free any entries remaining on the queue. There should only be
1489 entries left if we hit an error while processing the dwarf. */
1490 ~dwarf2_queue_guard ()
1491 {
08ac5771
SM
1492 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1493
1494 m_per_objfile->per_bfd->queue.reset ();
39856def 1495 }
b303c6f6 1496
39856def 1497 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1498
39856def
TT
1499private:
1500 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1501};
1502
39856def
TT
1503dwarf2_queue_item::~dwarf2_queue_item ()
1504{
1505 /* Anything still marked queued is likely to be in an
1506 inconsistent state, so discard it. */
1507 if (per_cu->queued)
1508 {
7188ed02 1509 per_objfile->remove_cu (per_cu);
39856def
TT
1510 per_cu->queued = 0;
1511 }
1512}
1513
473ab964
TT
1514/* See dwarf2/read.h. */
1515
1516void
1517dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1518{
1519 if (data->is_debug_types)
1520 delete static_cast<signatured_type *> (data);
1521 else
1522 delete data;
1523}
1524
d721ba37
PA
1525/* The return type of find_file_and_directory. Note, the enclosed
1526 string pointers are only valid while this object is valid. */
1527
1528struct file_and_directory
1529{
1530 /* The filename. This is never NULL. */
1531 const char *name;
1532
1533 /* The compilation directory. NULL if not known. If we needed to
1534 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1535 points directly to the DW_AT_comp_dir string attribute owned by
1536 the obstack that owns the DIE. */
1537 const char *comp_dir;
1538
1539 /* If we needed to build a new string for comp_dir, this is what
1540 owns the storage. */
1541 std::string comp_dir_storage;
1542};
1543
1544static file_and_directory find_file_and_directory (struct die_info *die,
1545 struct dwarf2_cu *cu);
9291a0cd 1546
298e9637 1547static htab_up allocate_signatured_type_table ();
1fd400ff 1548
298e9637 1549static htab_up allocate_dwo_unit_table ();
3019eac3 1550
57d63ce2 1551static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1552 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1553 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1554
976ca316 1555static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1556
3019eac3 1557static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1558 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1559 ULONGEST signature);
3019eac3
DE
1560
1561static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1562 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1563
1b555f17 1564static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1565
263db9a1
TT
1566/* A unique pointer to a dwo_file. */
1567
51ac9db5 1568typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1569
976ca316 1570static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1571
1b80a9fa 1572static void check_producer (struct dwarf2_cu *cu);
4390d890
DE
1573\f
1574/* Various complaints about symbol reading that don't abort the process. */
1575
4390d890
DE
1576static void
1577dwarf2_debug_line_missing_file_complaint (void)
1578{
b98664d3 1579 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1580}
1581
1582static void
1583dwarf2_debug_line_missing_end_sequence_complaint (void)
1584{
b98664d3 1585 complaint (_(".debug_line section has line "
4390d890
DE
1586 "program sequence without an end"));
1587}
1588
1589static void
1590dwarf2_complex_location_expr_complaint (void)
1591{
b98664d3 1592 complaint (_("location expression too complex"));
4390d890
DE
1593}
1594
1595static void
1596dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1597 int arg3)
1598{
b98664d3 1599 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1600 arg1, arg2, arg3);
1601}
1602
4390d890
DE
1603static void
1604dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1605{
b98664d3 1606 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1607 arg1, arg2);
1608}
527f3840
JK
1609
1610/* Hash function for line_header_hash. */
1611
1612static hashval_t
1613line_header_hash (const struct line_header *ofs)
1614{
9c541725 1615 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1616}
1617
1618/* Hash function for htab_create_alloc_ex for line_header_hash. */
1619
1620static hashval_t
1621line_header_hash_voidp (const void *item)
1622{
9a3c8263 1623 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1624
1625 return line_header_hash (ofs);
1626}
1627
1628/* Equality function for line_header_hash. */
1629
1630static int
1631line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1632{
9a3c8263
SM
1633 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1634 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1635
9c541725 1636 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1637 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1638}
1639
4390d890 1640\f
9291a0cd 1641
330cdd98
PA
1642/* See declaration. */
1643
5989a64e
SM
1644dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1645 bool can_copy_)
c3699833
SM
1646 : obfd (obfd),
1647 can_copy (can_copy_)
330cdd98
PA
1648{
1649 if (names == NULL)
1650 names = &dwarf2_elf_names;
1651
330cdd98
PA
1652 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1653 locate_sections (obfd, sec, *names);
1654}
1655
5989a64e 1656dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1657{
0d305d5c 1658 for (auto &per_cu : all_comp_units)
ae640021 1659 per_cu->imported_symtabs_free ();
fc8e7e75 1660
5989a64e 1661 /* Everything else should be on this->obstack. */
330cdd98
PA
1662}
1663
7188ed02 1664/* See read.h. */
330cdd98
PA
1665
1666void
7188ed02 1667dwarf2_per_objfile::remove_all_cus ()
330cdd98 1668{
08ac5771
SM
1669 gdb_assert (!this->per_bfd->queue.has_value ());
1670
7188ed02
SM
1671 for (auto pair : m_dwarf2_cus)
1672 delete pair.second;
330cdd98 1673
7188ed02 1674 m_dwarf2_cus.clear ();
330cdd98
PA
1675}
1676
11ed8cad
TT
1677/* A helper class that calls free_cached_comp_units on
1678 destruction. */
1679
1680class free_cached_comp_units
1681{
1682public:
1683
1684 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1685 : m_per_objfile (per_objfile)
1686 {
1687 }
1688
1689 ~free_cached_comp_units ()
1690 {
7188ed02 1691 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1692 }
1693
1694 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1695
1696private:
1697
1698 dwarf2_per_objfile *m_per_objfile;
1699};
1700
af758d11
SM
1701/* See read.h. */
1702
1703bool
1704dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1705{
1706 gdb_assert (per_cu->index < this->m_symtabs.size ());
1707
1708 return this->m_symtabs[per_cu->index] != nullptr;
1709}
1710
1711/* See read.h. */
1712
1713compunit_symtab *
1714dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1715{
1716 gdb_assert (per_cu->index < this->m_symtabs.size ());
1717
1718 return this->m_symtabs[per_cu->index];
1719}
1720
1721/* See read.h. */
1722
1723void
1724dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1725 compunit_symtab *symtab)
1726{
1727 gdb_assert (per_cu->index < this->m_symtabs.size ());
1728 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1729
1730 this->m_symtabs[per_cu->index] = symtab;
1731}
1732
c906108c 1733/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1734 information and return true if we have enough to do something.
1735 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1736 ELF names are used. CAN_COPY is true for formats where symbol
1737 interposition is possible and so symbol values must follow copy
1738 relocation rules. */
c906108c
SS
1739
1740int
251d32d9 1741dwarf2_has_info (struct objfile *objfile,
dda83cd7 1742 const struct dwarf2_debug_sections *names,
4b610737 1743 bool can_copy)
c906108c 1744{
97cbe998
SDJ
1745 if (objfile->flags & OBJF_READNEVER)
1746 return 0;
1747
976ca316 1748 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1749
976ca316 1750 if (per_objfile == NULL)
5989a64e 1751 {
17ee85fc
TT
1752 dwarf2_per_bfd *per_bfd;
1753
8a91fbdf
SM
1754 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1755 BFD doesn't require relocations.
1756
1757 We don't share with objfiles for which -readnow was requested,
1758 because it would complicate things when loading the same BFD with
1759 -readnow and then without -readnow. */
1760 if (!gdb_bfd_requires_relocations (objfile->obfd)
1761 && (objfile->flags & OBJF_READNOW) == 0)
17ee85fc
TT
1762 {
1763 /* See if one has been created for this BFD yet. */
1764 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1765
1766 if (per_bfd == nullptr)
1767 {
1768 /* No, create it now. */
1769 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1770 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1771 }
1772 }
1773 else
1774 {
1775 /* No sharing possible, create one specifically for this objfile. */
1776 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1777 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1778 }
5989a64e 1779
976ca316 1780 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1781 }
5bfd760d 1782
976ca316
SM
1783 return (!per_objfile->per_bfd->info.is_virtual
1784 && per_objfile->per_bfd->info.s.section != NULL
1785 && !per_objfile->per_bfd->abbrev.is_virtual
1786 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1787}
1788
330cdd98 1789/* See declaration. */
c906108c 1790
330cdd98 1791void
5989a64e
SM
1792dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1793 const dwarf2_debug_sections &names)
c906108c 1794{
fd361982 1795 flagword aflag = bfd_section_flags (sectp);
251d32d9 1796
dc7650b8
JK
1797 if ((aflag & SEC_HAS_CONTENTS) == 0)
1798 {
1799 }
950b7495
KS
1800 else if (elf_section_data (sectp)->this_hdr.sh_size
1801 > bfd_get_file_size (abfd))
1802 {
1803 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1804 warning (_("Discarding section %s which has a section size (%s"
1805 ") larger than the file size [in module %s]"),
1806 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1807 bfd_get_filename (abfd));
1808 }
fbedd546 1809 else if (names.info.matches (sectp->name))
c906108c 1810 {
330cdd98 1811 this->info.s.section = sectp;
fd361982 1812 this->info.size = bfd_section_size (sectp);
c906108c 1813 }
fbedd546 1814 else if (names.abbrev.matches (sectp->name))
c906108c 1815 {
330cdd98 1816 this->abbrev.s.section = sectp;
fd361982 1817 this->abbrev.size = bfd_section_size (sectp);
c906108c 1818 }
fbedd546 1819 else if (names.line.matches (sectp->name))
c906108c 1820 {
330cdd98 1821 this->line.s.section = sectp;
fd361982 1822 this->line.size = bfd_section_size (sectp);
c906108c 1823 }
fbedd546 1824 else if (names.loc.matches (sectp->name))
c906108c 1825 {
330cdd98 1826 this->loc.s.section = sectp;
fd361982 1827 this->loc.size = bfd_section_size (sectp);
c906108c 1828 }
fbedd546 1829 else if (names.loclists.matches (sectp->name))
43988095 1830 {
330cdd98 1831 this->loclists.s.section = sectp;
fd361982 1832 this->loclists.size = bfd_section_size (sectp);
43988095 1833 }
fbedd546 1834 else if (names.macinfo.matches (sectp->name))
c906108c 1835 {
330cdd98 1836 this->macinfo.s.section = sectp;
fd361982 1837 this->macinfo.size = bfd_section_size (sectp);
c906108c 1838 }
fbedd546 1839 else if (names.macro.matches (sectp->name))
cf2c3c16 1840 {
330cdd98 1841 this->macro.s.section = sectp;
fd361982 1842 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1843 }
fbedd546 1844 else if (names.str.matches (sectp->name))
c906108c 1845 {
330cdd98 1846 this->str.s.section = sectp;
fd361982 1847 this->str.size = bfd_section_size (sectp);
c906108c 1848 }
fbedd546 1849 else if (names.str_offsets.matches (sectp->name))
18a8505e
AT
1850 {
1851 this->str_offsets.s.section = sectp;
1852 this->str_offsets.size = bfd_section_size (sectp);
1853 }
fbedd546 1854 else if (names.line_str.matches (sectp->name))
43988095 1855 {
330cdd98 1856 this->line_str.s.section = sectp;
fd361982 1857 this->line_str.size = bfd_section_size (sectp);
43988095 1858 }
fbedd546 1859 else if (names.addr.matches (sectp->name))
3019eac3 1860 {
330cdd98 1861 this->addr.s.section = sectp;
fd361982 1862 this->addr.size = bfd_section_size (sectp);
3019eac3 1863 }
fbedd546 1864 else if (names.frame.matches (sectp->name))
b6af0555 1865 {
330cdd98 1866 this->frame.s.section = sectp;
fd361982 1867 this->frame.size = bfd_section_size (sectp);
b6af0555 1868 }
fbedd546 1869 else if (names.eh_frame.matches (sectp->name))
b6af0555 1870 {
330cdd98 1871 this->eh_frame.s.section = sectp;
fd361982 1872 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1873 }
fbedd546 1874 else if (names.ranges.matches (sectp->name))
af34e669 1875 {
330cdd98 1876 this->ranges.s.section = sectp;
fd361982 1877 this->ranges.size = bfd_section_size (sectp);
af34e669 1878 }
fbedd546 1879 else if (names.rnglists.matches (sectp->name))
43988095 1880 {
330cdd98 1881 this->rnglists.s.section = sectp;
fd361982 1882 this->rnglists.size = bfd_section_size (sectp);
43988095 1883 }
fbedd546 1884 else if (names.types.matches (sectp->name))
348e048f 1885 {
8b70b953
TT
1886 struct dwarf2_section_info type_section;
1887
1888 memset (&type_section, 0, sizeof (type_section));
049412e3 1889 type_section.s.section = sectp;
fd361982 1890 type_section.size = bfd_section_size (sectp);
8b70b953 1891
fd5866f6 1892 this->types.push_back (type_section);
348e048f 1893 }
fbedd546 1894 else if (names.gdb_index.matches (sectp->name))
9291a0cd 1895 {
330cdd98 1896 this->gdb_index.s.section = sectp;
fd361982 1897 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1898 }
fbedd546 1899 else if (names.debug_names.matches (sectp->name))
927aa2e7
JK
1900 {
1901 this->debug_names.s.section = sectp;
fd361982 1902 this->debug_names.size = bfd_section_size (sectp);
927aa2e7 1903 }
fbedd546 1904 else if (names.debug_aranges.matches (sectp->name))
927aa2e7
JK
1905 {
1906 this->debug_aranges.s.section = sectp;
fd361982 1907 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 1908 }
dce234bc 1909
fd361982
AM
1910 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1911 && bfd_section_vma (sectp) == 0)
330cdd98 1912 this->has_section_at_zero = true;
c906108c
SS
1913}
1914
dce234bc 1915/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1916 SECTION_NAME. */
af34e669 1917
dce234bc 1918void
3017a003 1919dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
1920 enum dwarf2_section_enum sect,
1921 asection **sectp, const gdb_byte **bufp,
1922 bfd_size_type *sizep)
dce234bc 1923{
976ca316 1924 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 1925 struct dwarf2_section_info *info;
a3b2a86b
TT
1926
1927 /* We may see an objfile without any DWARF, in which case we just
1928 return nothing. */
976ca316 1929 if (per_objfile == NULL)
a3b2a86b
TT
1930 {
1931 *sectp = NULL;
1932 *bufp = NULL;
1933 *sizep = 0;
1934 return;
1935 }
3017a003
TG
1936 switch (sect)
1937 {
1938 case DWARF2_DEBUG_FRAME:
976ca316 1939 info = &per_objfile->per_bfd->frame;
3017a003
TG
1940 break;
1941 case DWARF2_EH_FRAME:
976ca316 1942 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
1943 break;
1944 default:
1945 gdb_assert_not_reached ("unexpected section");
1946 }
dce234bc 1947
96b79293 1948 info->read (objfile);
dce234bc 1949
96b79293 1950 *sectp = info->get_bfd_section ();
dce234bc
PP
1951 *bufp = info->buffer;
1952 *sizep = info->size;
1953}
1954
9291a0cd 1955\f
39298a5d 1956/* DWARF quick_symbol_functions support. */
7b9f3c50
DE
1957
1958/* TUs can share .debug_line entries, and there can be a lot more TUs than
1959 unique line tables, so we maintain a separate table of all .debug_line
1960 derived entries to support the sharing.
1961 All the quick functions need is the list of file names. We discard the
1962 line_header when we're done and don't need to record it here. */
1963struct quick_file_names
1964{
094b34ac
DE
1965 /* The data used to construct the hash key. */
1966 struct stmt_list_hash hash;
7b9f3c50
DE
1967
1968 /* The number of entries in file_names, real_names. */
1969 unsigned int num_file_names;
1970
1971 /* The file names from the line table, after being run through
1972 file_full_name. */
1973 const char **file_names;
1974
1975 /* The file names from the line table after being run through
1976 gdb_realpath. These are computed lazily. */
1977 const char **real_names;
1978};
1979
1980/* When using the index (and thus not using psymtabs), each CU has an
1981 object of this type. This is used to hold information needed by
1982 the various "quick" methods. */
1983struct dwarf2_per_cu_quick_data
1984{
1985 /* The file table. This can be NULL if there was no file table
1986 or it's currently not read in.
5989a64e 1987 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
1988 struct quick_file_names *file_names;
1989
7b9f3c50
DE
1990 /* A temporary mark bit used when iterating over all CUs in
1991 expand_symtabs_matching. */
1992 unsigned int mark : 1;
1993
1994 /* True if we've tried to read the file table and found there isn't one.
1995 There will be no point in trying to read it again next time. */
1996 unsigned int no_file_data : 1;
1997};
1998
edc02ceb
TT
1999/* A subclass of psymbol_functions that arranges to read the DWARF
2000 partial symbols when needed. */
2001struct lazy_dwarf_reader : public psymbol_functions
2002{
2003 using psymbol_functions::psymbol_functions;
2004
2005 bool can_lazily_read_symbols () override
2006 {
2007 return true;
2008 }
2009
2010 void read_partial_symbols (struct objfile *objfile) override
2011 {
2012 if (dwarf2_has_info (objfile, nullptr))
2013 dwarf2_build_psymtabs (objfile, this);
2014 }
2015};
2016
2017static quick_symbol_functions_up
2018make_lazy_dwarf_reader ()
2019{
2020 return quick_symbol_functions_up (new lazy_dwarf_reader);
2021}
2022
39298a5d
TT
2023struct dwarf2_base_index_functions : public quick_symbol_functions
2024{
2025 bool has_symbols (struct objfile *objfile) override;
2026
fc4d5ebf
AB
2027 bool has_unexpanded_symtabs (struct objfile *objfile) override;
2028
39298a5d
TT
2029 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2030
2031 void forget_cached_source_info (struct objfile *objfile) override;
2032
39298a5d
TT
2033 enum language lookup_global_symbol_language (struct objfile *objfile,
2034 const char *name,
2035 domain_enum domain,
2036 bool *symbol_found_p) override
2037 {
2038 *symbol_found_p = false;
2039 return language_unknown;
2040 }
2041
4829711b 2042 void print_stats (struct objfile *objfile, bool print_bcache) override;
39298a5d
TT
2043
2044 void expand_all_symtabs (struct objfile *objfile) override;
2045
39298a5d
TT
2046 struct compunit_symtab *find_pc_sect_compunit_symtab
2047 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2048 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2049
2050 struct compunit_symtab *find_compunit_symtab_by_address
2051 (struct objfile *objfile, CORE_ADDR address) override
2052 {
2053 return nullptr;
2054 }
2055
2056 void map_symbol_filenames (struct objfile *objfile,
f4655dee
TT
2057 gdb::function_view<symbol_filename_ftype> fun,
2058 bool need_fullname) override;
39298a5d
TT
2059};
2060
2061struct dwarf2_gdb_index : public dwarf2_base_index_functions
2062{
39298a5d
TT
2063 void dump (struct objfile *objfile) override;
2064
0b7b2c2a 2065 void expand_matching_symbols
39298a5d
TT
2066 (struct objfile *,
2067 const lookup_name_info &lookup_name,
2068 domain_enum domain,
2069 int global,
39298a5d
TT
2070 symbol_compare_ftype *ordered_compare) override;
2071
df35e626 2072 bool expand_symtabs_matching
39298a5d
TT
2073 (struct objfile *objfile,
2074 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2075 const lookup_name_info *lookup_name,
2076 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2077 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2078 block_search_flags search_flags,
3bfa51a7 2079 domain_enum domain,
39298a5d
TT
2080 enum search_domain kind) override;
2081};
2082
2083struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2084{
39298a5d
TT
2085 void dump (struct objfile *objfile) override;
2086
0b7b2c2a 2087 void expand_matching_symbols
39298a5d
TT
2088 (struct objfile *,
2089 const lookup_name_info &lookup_name,
2090 domain_enum domain,
2091 int global,
39298a5d
TT
2092 symbol_compare_ftype *ordered_compare) override;
2093
df35e626 2094 bool expand_symtabs_matching
39298a5d
TT
2095 (struct objfile *objfile,
2096 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2097 const lookup_name_info *lookup_name,
2098 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2099 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2100 block_search_flags search_flags,
3bfa51a7 2101 domain_enum domain,
39298a5d
TT
2102 enum search_domain kind) override;
2103};
2104
edc02ceb 2105static quick_symbol_functions_up
39298a5d
TT
2106make_dwarf_gdb_index ()
2107{
2108 return quick_symbol_functions_up (new dwarf2_gdb_index);
2109}
2110
edc02ceb 2111static quick_symbol_functions_up
39298a5d
TT
2112make_dwarf_debug_names ()
2113{
2114 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2115}
2116
094b34ac
DE
2117/* Utility hash function for a stmt_list_hash. */
2118
2119static hashval_t
2120hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2121{
2122 hashval_t v = 0;
2123
2124 if (stmt_list_hash->dwo_unit != NULL)
2125 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2126 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2127 return v;
2128}
2129
2130/* Utility equality function for a stmt_list_hash. */
2131
2132static int
2133eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2134 const struct stmt_list_hash *rhs)
2135{
2136 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2137 return 0;
2138 if (lhs->dwo_unit != NULL
2139 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2140 return 0;
2141
9c541725 2142 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2143}
2144
7b9f3c50
DE
2145/* Hash function for a quick_file_names. */
2146
2147static hashval_t
2148hash_file_name_entry (const void *e)
2149{
9a3c8263
SM
2150 const struct quick_file_names *file_data
2151 = (const struct quick_file_names *) e;
7b9f3c50 2152
094b34ac 2153 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2154}
2155
2156/* Equality function for a quick_file_names. */
2157
2158static int
2159eq_file_name_entry (const void *a, const void *b)
2160{
9a3c8263
SM
2161 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2162 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2163
094b34ac 2164 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2165}
2166
2167/* Delete function for a quick_file_names. */
2168
2169static void
2170delete_file_name_entry (void *e)
2171{
9a3c8263 2172 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2173 int i;
2174
2175 for (i = 0; i < file_data->num_file_names; ++i)
2176 {
2177 xfree ((void*) file_data->file_names[i]);
2178 if (file_data->real_names)
2179 xfree ((void*) file_data->real_names[i]);
2180 }
2181
45940949
TT
2182 /* The space for the struct itself lives on the obstack, so we don't
2183 free it here. */
7b9f3c50
DE
2184}
2185
2186/* Create a quick_file_names hash table. */
2187
5895093f 2188static htab_up
7b9f3c50
DE
2189create_quick_file_names_table (unsigned int nr_initial_entries)
2190{
5895093f
TT
2191 return htab_up (htab_create_alloc (nr_initial_entries,
2192 hash_file_name_entry, eq_file_name_entry,
2193 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2194}
9291a0cd 2195
ab432490
SM
2196/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2197 function is unrelated to symtabs, symtab would have to be created afterwards.
2198 You should call age_cached_comp_units after processing the CU. */
918dd910 2199
1b555f17 2200static dwarf2_cu *
ab432490
SM
2201load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2202 bool skip_partial)
918dd910 2203{
3019eac3 2204 if (per_cu->is_debug_types)
ab432490 2205 load_full_type_unit (per_cu, per_objfile);
918dd910 2206 else
4a636814
SM
2207 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2208 skip_partial, language_minimal);
918dd910 2209
7188ed02
SM
2210 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2211 if (cu == nullptr)
1b555f17 2212 return nullptr; /* Dummy CU. */
2dc860c0 2213
7188ed02 2214 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2215
7188ed02 2216 return cu;
918dd910
JK
2217}
2218
1350c3b4 2219/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2220
9291a0cd 2221static void
97a1449a 2222dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2223 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2224{
f4dc4d17
DE
2225 /* Skip type_unit_groups, reading the type units they contain
2226 is handled elsewhere. */
197400e8 2227 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2228 return;
2229
08ac5771
SM
2230 {
2231 /* The destructor of dwarf2_queue_guard frees any entries left on
2232 the queue. After this point we're guaranteed to leave this function
2233 with the dwarf queue empty. */
2234 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2235
08ac5771
SM
2236 if (!per_objfile->symtab_set_p (per_cu))
2237 {
2238 queue_comp_unit (per_cu, per_objfile, language_minimal);
2239 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2240
2241 /* If we just loaded a CU from a DWO, and we're working with an index
2242 that may badly handle TUs, load all the TUs in that DWO as well.
2243 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2244 if (!per_cu->is_debug_types
2245 && cu != NULL
2246 && cu->dwo_unit != NULL
2247 && per_objfile->per_bfd->index_table != NULL
2248 && per_objfile->per_bfd->index_table->version <= 7
2249 /* DWP files aren't supported yet. */
2250 && get_dwp_file (per_objfile) == NULL)
2251 queue_and_load_all_dwo_tus (cu);
2252 }
9291a0cd 2253
08ac5771
SM
2254 process_queue (per_objfile);
2255 }
9291a0cd
TT
2256
2257 /* Age the cache, releasing compilation units that have not
2258 been used recently. */
976ca316 2259 per_objfile->age_comp_units ();
9291a0cd
TT
2260}
2261
97a1449a
SM
2262/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2263 the per-objfile for which this symtab is instantiated.
2264
2265 Returns the resulting symbol table. */
2fdf6df6 2266
43f3e411 2267static struct compunit_symtab *
97a1449a 2268dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2269 dwarf2_per_objfile *per_objfile,
97a1449a 2270 bool skip_partial)
9291a0cd 2271{
976ca316 2272 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2273
976ca316 2274 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2275 {
976ca316 2276 free_cached_comp_units freer (per_objfile);
c83dd867 2277 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2278 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2279 process_cu_includes (per_objfile);
9291a0cd 2280 }
f194fefb 2281
976ca316 2282 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2283}
2284
d3473f0c
TT
2285/* See read.h. */
2286
473ab964 2287dwarf2_per_cu_data_up
5989a64e 2288dwarf2_per_bfd::allocate_per_cu ()
d3473f0c 2289{
473ab964 2290 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
1859c670 2291 result->per_bfd = this;
c96e8b04 2292 result->index = all_comp_units.size ();
d3473f0c
TT
2293 return result;
2294}
2295
2296/* See read.h. */
2297
46c6bcf6 2298signatured_type_up
9ea36493 2299dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
d3473f0c 2300{
9ea36493 2301 signatured_type_up result (new signatured_type (signature));
0d305d5c 2302 result->per_bfd = this;
c96e8b04 2303 result->index = all_comp_units.size ();
cc653233 2304 result->is_debug_types = true;
91eea9cc 2305 tu_stats.nr_tus++;
d3473f0c
TT
2306 return result;
2307}
2308
168c9250 2309/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2310 obstack, and constructed with the specified field values. */
4b514bc8 2311
473ab964 2312static dwarf2_per_cu_data_up
168c9250
SM
2313create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2314 struct dwarf2_section_info *section,
2315 int is_dwz,
2316 sect_offset sect_off, ULONGEST length)
4b514bc8 2317{
473ab964 2318 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2319 the_cu->sect_off = sect_off;
2320 the_cu->length = length;
4b514bc8 2321 the_cu->section = section;
168c9250 2322 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2323 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2324 the_cu->is_dwz = is_dwz;
2325 return the_cu;
2326}
2327
2ec9a5e0
TT
2328/* A helper for create_cus_from_index that handles a given list of
2329 CUs. */
2fdf6df6 2330
74a0d9f6 2331static void
168c9250 2332create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2333 const gdb_byte *cu_list, offset_type n_elements,
2334 struct dwarf2_section_info *section,
b76e467d 2335 int is_dwz)
9291a0cd 2336{
12359b5e 2337 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2338 {
74a0d9f6 2339 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2340
2341 sect_offset sect_off
2342 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2343 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2344 cu_list += 2 * 8;
2345
473ab964 2346 dwarf2_per_cu_data_up per_cu
168c9250
SM
2347 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2348 length);
0d305d5c 2349 per_bfd->all_comp_units.push_back (std::move (per_cu));
9291a0cd 2350 }
9291a0cd
TT
2351}
2352
2ec9a5e0 2353/* Read the CU list from the mapped index, and use it to create all
168c9250 2354 the CU objects for PER_BFD. */
2ec9a5e0 2355
74a0d9f6 2356static void
168c9250 2357create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2358 const gdb_byte *cu_list, offset_type cu_list_elements,
2359 const gdb_byte *dwz_list, offset_type dwz_elements)
2360{
168c9250
SM
2361 gdb_assert (per_bfd->all_comp_units.empty ());
2362 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2363
168c9250
SM
2364 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2365 &per_bfd->info, 0);
2ec9a5e0
TT
2366
2367 if (dwz_elements == 0)
74a0d9f6 2368 return;
2ec9a5e0 2369
168c9250
SM
2370 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2371 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2372 &dwz->info, 1);
2ec9a5e0
TT
2373}
2374
1fd400ff 2375/* Create the signatured type hash table from the index. */
673bfd45 2376
74a0d9f6 2377static void
12359b5e 2378create_signatured_type_table_from_index
168c9250
SM
2379 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2380 const gdb_byte *bytes, offset_type elements)
1fd400ff 2381{
298e9637 2382 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2383
12359b5e 2384 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2385 {
46c6bcf6 2386 signatured_type_up sig_type;
9c541725 2387 ULONGEST signature;
1fd400ff 2388 void **slot;
9c541725 2389 cu_offset type_offset_in_tu;
1fd400ff 2390
74a0d9f6 2391 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2392 sect_offset sect_off
2393 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2394 type_offset_in_tu
2395 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2396 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2397 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2398 bytes += 3 * 8;
2399
9ea36493 2400 sig_type = per_bfd->allocate_signatured_type (signature);
9c541725 2401 sig_type->type_offset_in_tu = type_offset_in_tu;
0d305d5c
TT
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 {
46c6bcf6 2435 signatured_type_up 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
9ea36493
SM
2450 sig_type = per_objfile->per_bfd->allocate_signatured_type
2451 (cu_header.signature);
927aa2e7 2452 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
0d305d5c
TT
2453 sig_type->section = section;
2454 sig_type->sect_off = sect_off;
2455 sig_type->v.quick
976ca316 2456 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2457 struct dwarf2_per_cu_quick_data);
2458
0d305d5c
TT
2459 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2460 *slot = sig_type.get ();
927aa2e7 2461
473ab964 2462 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
927aa2e7
JK
2463 }
2464
976ca316 2465 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2466}
2467
9291a0cd 2468/* Read the address map data from the mapped index, and use it to
efd7398e 2469 populate the psymtabs_addrmap. */
2fdf6df6 2470
9291a0cd 2471static void
976ca316 2472create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2473 struct mapped_index *index)
9291a0cd 2474{
976ca316 2475 struct objfile *objfile = per_objfile->objfile;
efd7398e 2476 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
08feed99 2477 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2478 const gdb_byte *iter, *end;
9291a0cd 2479 struct addrmap *mutable_map;
9291a0cd
TT
2480 CORE_ADDR baseaddr;
2481
8268c778
PA
2482 auto_obstack temp_obstack;
2483
9291a0cd
TT
2484 mutable_map = addrmap_create_mutable (&temp_obstack);
2485
f00a2de2
PA
2486 iter = index->address_table.data ();
2487 end = iter + index->address_table.size ();
9291a0cd 2488
b3b3bada 2489 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2490
2491 while (iter < end)
2492 {
2493 ULONGEST hi, lo, cu_index;
2494 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2495 iter += 8;
2496 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2497 iter += 8;
2498 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2499 iter += 4;
f652bce2 2500
24a55014 2501 if (lo > hi)
f652bce2 2502 {
b98664d3 2503 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2504 hex_string (lo), hex_string (hi));
24a55014 2505 continue;
f652bce2 2506 }
24a55014 2507
efd7398e 2508 if (cu_index >= per_bfd->all_comp_units.size ())
f652bce2 2509 {
b98664d3 2510 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2511 (unsigned) cu_index);
24a55014 2512 continue;
f652bce2 2513 }
24a55014 2514
79748972
TT
2515 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2516 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2517 addrmap_set_empty (mutable_map, lo, hi - 1,
efd7398e 2518 per_bfd->get_cu (cu_index));
9291a0cd
TT
2519 }
2520
efd7398e
TT
2521 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2522 &per_bfd->obstack);
9291a0cd
TT
2523}
2524
927aa2e7 2525/* Read the address map data from DWARF-5 .debug_aranges, and use it to
efd7398e 2526 populate the psymtabs_addrmap. */
927aa2e7
JK
2527
2528static void
976ca316 2529create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2530 struct dwarf2_section_info *section)
2531{
976ca316 2532 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2533 bfd *abfd = objfile->obfd;
08feed99 2534 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2535 const CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e 2536 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
2537
2538 auto_obstack temp_obstack;
2539 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2540
2541 std::unordered_map<sect_offset,
2542 dwarf2_per_cu_data *,
2543 gdb::hash_enum<sect_offset>>
2544 debug_info_offset_to_per_cu;
0d305d5c 2545 for (const auto &per_cu : per_bfd->all_comp_units)
927aa2e7 2546 {
927aa2e7 2547 const auto insertpair
0d305d5c
TT
2548 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2549 per_cu.get ());
927aa2e7
JK
2550 if (!insertpair.second)
2551 {
2552 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2553 "debug_info_offset %s, ignoring .debug_aranges."),
2554 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2555 return;
2556 }
2557 }
2558
96b79293 2559 section->read (objfile);
927aa2e7
JK
2560
2561 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2562
2563 const gdb_byte *addr = section->buffer;
2564
2565 while (addr < section->buffer + section->size)
2566 {
2567 const gdb_byte *const entry_addr = addr;
2568 unsigned int bytes_read;
2569
2570 const LONGEST entry_length = read_initial_length (abfd, addr,
2571 &bytes_read);
2572 addr += bytes_read;
2573
2574 const gdb_byte *const entry_end = addr + entry_length;
2575 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2576 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2577 if (addr + entry_length > section->buffer + section->size)
2578 {
47e3f474 2579 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2580 "length %s exceeds section length %s, "
927aa2e7 2581 "ignoring .debug_aranges."),
47e3f474
TV
2582 objfile_name (objfile),
2583 plongest (entry_addr - section->buffer),
927aa2e7
JK
2584 plongest (bytes_read + entry_length),
2585 pulongest (section->size));
2586 return;
2587 }
2588
2589 /* The version number. */
2590 const uint16_t version = read_2_bytes (abfd, addr);
2591 addr += 2;
2592 if (version != 2)
2593 {
47e3f474 2594 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2595 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2596 objfile_name (objfile),
2597 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2598 return;
2599 }
2600
2601 const uint64_t debug_info_offset
2602 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2603 addr += offset_size;
2604 const auto per_cu_it
2605 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2606 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2607 {
47e3f474 2608 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2609 "debug_info_offset %s does not exists, "
2610 "ignoring .debug_aranges."),
47e3f474
TV
2611 objfile_name (objfile),
2612 plongest (entry_addr - section->buffer),
927aa2e7
JK
2613 pulongest (debug_info_offset));
2614 return;
2615 }
2616 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2617
2618 const uint8_t address_size = *addr++;
2619 if (address_size < 1 || address_size > 8)
2620 {
47e3f474 2621 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2622 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2623 objfile_name (objfile),
2624 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2625 return;
2626 }
2627
2628 const uint8_t segment_selector_size = *addr++;
2629 if (segment_selector_size != 0)
2630 {
47e3f474 2631 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2632 "segment_selector_size %u is not supported, "
2633 "ignoring .debug_aranges."),
47e3f474
TV
2634 objfile_name (objfile),
2635 plongest (entry_addr - section->buffer),
927aa2e7
JK
2636 segment_selector_size);
2637 return;
2638 }
2639
2640 /* Must pad to an alignment boundary that is twice the address
dda83cd7 2641 size. It is undocumented by the DWARF standard but GCC does
3da4c644
TT
2642 use it. However, not every compiler does this. We can see
2643 whether it has happened by looking at the total length of the
2644 contents of the aranges for this CU -- it if isn't a multiple
2645 of twice the address size, then we skip any leftover
2646 bytes. */
2647 addr += (entry_end - addr) % (2 * address_size);
927aa2e7
JK
2648
2649 for (;;)
2650 {
2651 if (addr + 2 * address_size > entry_end)
2652 {
47e3f474 2653 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2654 "address list is not properly terminated, "
2655 "ignoring .debug_aranges."),
47e3f474
TV
2656 objfile_name (objfile),
2657 plongest (entry_addr - section->buffer));
927aa2e7
JK
2658 return;
2659 }
2660 ULONGEST start = extract_unsigned_integer (addr, address_size,
2661 dwarf5_byte_order);
2662 addr += address_size;
2663 ULONGEST length = extract_unsigned_integer (addr, address_size,
2664 dwarf5_byte_order);
2665 addr += address_size;
2666 if (start == 0 && length == 0)
2667 break;
efd7398e 2668 if (start == 0 && !per_bfd->has_section_at_zero)
927aa2e7
JK
2669 {
2670 /* Symbol was eliminated due to a COMDAT group. */
2671 continue;
2672 }
2673 ULONGEST end = start + length;
79748972
TT
2674 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2675 - baseaddr);
2676 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2677 - baseaddr);
927aa2e7
JK
2678 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2679 }
2680 }
2681
efd7398e
TT
2682 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2683 &per_bfd->obstack);
927aa2e7
JK
2684}
2685
4485a1c1
SM
2686/* A helper function that reads the .gdb_index from BUFFER and fills
2687 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2688 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2689 ok to use deprecated sections.
2690
2691 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2692 out parameters that are filled in with information about the CU and
2693 TU lists in the section.
2694
4485a1c1 2695 Returns true if all went well, false otherwise. */
2fdf6df6 2696
d33bc52e 2697static bool
3810f182 2698read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2699 bool deprecated_ok,
2700 gdb::array_view<const gdb_byte> buffer,
2701 struct mapped_index *map,
2702 const gdb_byte **cu_list,
2703 offset_type *cu_list_elements,
2704 const gdb_byte **types_list,
2705 offset_type *types_list_elements)
2706{
2707 const gdb_byte *addr = &buffer[0];
42c2c694 2708 offset_view metadata (buffer);
82430852 2709
9291a0cd 2710 /* Version check. */
42c2c694 2711 offset_type version = metadata[0];
987d643c 2712 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2713 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2714 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2715 indices. */
831adc1f 2716 if (version < 4)
481860b3
GB
2717 {
2718 static int warning_printed = 0;
2719 if (!warning_printed)
2720 {
2721 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2722 filename);
481860b3
GB
2723 warning_printed = 1;
2724 }
2725 return 0;
2726 }
2727 /* Index version 4 uses a different hash function than index version
2728 5 and later.
2729
2730 Versions earlier than 6 did not emit psymbols for inlined
2731 functions. Using these files will cause GDB not to be able to
2732 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2733 indices unless the user has done
2734 "set use-deprecated-index-sections on". */
2ec9a5e0 2735 if (version < 6 && !deprecated_ok)
481860b3
GB
2736 {
2737 static int warning_printed = 0;
2738 if (!warning_printed)
2739 {
e615022a
DE
2740 warning (_("\
2741Skipping deprecated .gdb_index section in %s.\n\
2742Do \"set use-deprecated-index-sections on\" before the file is read\n\
2743to use the section anyway."),
2ec9a5e0 2744 filename);
481860b3
GB
2745 warning_printed = 1;
2746 }
2747 return 0;
2748 }
796a7ff8 2749 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2750 of the TU (for symbols coming from TUs),
2751 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2752 Plus gold-generated indices can have duplicate entries for global symbols,
2753 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2754 These are just performance bugs, and we can't distinguish gdb-generated
2755 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2756
481860b3 2757 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2758 longer backward compatible. */
796a7ff8 2759 if (version > 8)
594e8718 2760 return 0;
9291a0cd 2761
559a7a62 2762 map->version = version;
9291a0cd 2763
42c2c694
TT
2764 int i = 1;
2765 *cu_list = addr + metadata[i];
2766 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
1fd400ff
TT
2767 ++i;
2768
42c2c694
TT
2769 *types_list = addr + metadata[i];
2770 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
987d643c 2771 ++i;
1fd400ff 2772
42c2c694
TT
2773 const gdb_byte *address_table = addr + metadata[i];
2774 const gdb_byte *address_table_end = addr + metadata[i + 1];
f00a2de2
PA
2775 map->address_table
2776 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2777 ++i;
2778
42c2c694
TT
2779 const gdb_byte *symbol_table = addr + metadata[i];
2780 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
f00a2de2 2781 map->symbol_table
42c2c694
TT
2782 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2783 symbol_table_end));
9291a0cd 2784
f00a2de2 2785 ++i;
42c2c694 2786 map->constant_pool = buffer.slice (metadata[i]);
1fd400ff 2787
2ec9a5e0
TT
2788 return 1;
2789}
2790
4485a1c1
SM
2791/* Callback types for dwarf2_read_gdb_index. */
2792
2793typedef gdb::function_view
5989a64e 2794 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
2795 get_gdb_index_contents_ftype;
2796typedef gdb::function_view
2797 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2798 get_gdb_index_contents_dwz_ftype;
2799
927aa2e7 2800/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2801 elements of all the CUs and return 1. Otherwise, return 0. */
2802
2803static int
4485a1c1 2804dwarf2_read_gdb_index
976ca316 2805 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
2806 get_gdb_index_contents_ftype get_gdb_index_contents,
2807 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 2808{
2ec9a5e0
TT
2809 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2810 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2811 struct dwz_file *dwz;
976ca316 2812 struct objfile *objfile = per_objfile->objfile;
f8c41851 2813 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 2814
4485a1c1 2815 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 2816 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
2817
2818 if (main_index_contents.empty ())
2819 return 0;
2820
3063847f 2821 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 2822 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
2823 use_deprecated_index_sections,
2824 main_index_contents, map.get (), &cu_list,
2825 &cu_list_elements, &types_list,
2826 &types_list_elements))
2ec9a5e0
TT
2827 return 0;
2828
0fefef59 2829 /* Don't use the index if it's empty. */
3063847f 2830 if (map->symbol_table.empty ())
0fefef59
DE
2831 return 0;
2832
2ec9a5e0
TT
2833 /* If there is a .dwz file, read it so we can get its CU list as
2834 well. */
f8c41851 2835 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 2836 if (dwz != NULL)
2ec9a5e0 2837 {
2ec9a5e0
TT
2838 struct mapped_index dwz_map;
2839 const gdb_byte *dwz_types_ignore;
2840 offset_type dwz_types_elements_ignore;
2841
4485a1c1
SM
2842 gdb::array_view<const gdb_byte> dwz_index_content
2843 = get_gdb_index_contents_dwz (objfile, dwz);
2844
2845 if (dwz_index_content.empty ())
2846 return 0;
2847
3810f182 2848 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 2849 1, dwz_index_content, &dwz_map,
4485a1c1
SM
2850 &dwz_list, &dwz_list_elements,
2851 &dwz_types_ignore,
2852 &dwz_types_elements_ignore))
2ec9a5e0
TT
2853 {
2854 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 2855 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
2856 return 0;
2857 }
2858 }
2859
f8c41851
SM
2860 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2861 dwz_list_elements);
1fd400ff 2862
8b70b953
TT
2863 if (types_list_elements)
2864 {
8b70b953
TT
2865 /* We can only handle a single .debug_types when we have an
2866 index. */
f8c41851 2867 if (per_bfd->types.size () != 1)
8b70b953
TT
2868 return 0;
2869
f8c41851 2870 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 2871
f8c41851 2872 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 2873 types_list_elements);
8b70b953 2874 }
9291a0cd 2875
976ca316 2876 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 2877
f8c41851
SM
2878 per_bfd->index_table = std::move (map);
2879 per_bfd->using_index = 1;
2880 per_bfd->quick_file_names_table =
2881 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2882
9291a0cd
TT
2883 return 1;
2884}
2885
dee91e82 2886/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2887
dee91e82
DE
2888static void
2889dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 2890 struct die_info *comp_unit_die)
9291a0cd 2891{
dee91e82 2892 struct dwarf2_cu *cu = reader->cu;
ed2dc618 2893 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 2894 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 2895 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 2896 struct attribute *attr;
7b9f3c50
DE
2897 void **slot;
2898 struct quick_file_names *qfn;
9291a0cd 2899
0186c6a7
DE
2900 gdb_assert (! this_cu->is_debug_types);
2901
07261596
TT
2902 /* Our callers never want to match partial units -- instead they
2903 will match the enclosing full CU. */
2904 if (comp_unit_die->tag == DW_TAG_partial_unit)
2905 {
2906 this_cu->v.quick->no_file_data = 1;
2907 return;
2908 }
2909
0186c6a7 2910 lh_cu = this_cu;
7b9f3c50 2911 slot = NULL;
dee91e82 2912
fff8551c 2913 line_header_up lh;
9c541725 2914 sect_offset line_offset {};
fff8551c 2915
dee91e82 2916 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 2917 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 2918 {
7b9f3c50
DE
2919 struct quick_file_names find_entry;
2920
d4df075e 2921 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
2922
2923 /* We may have already read in this line header (TU line header sharing).
2924 If we have we're done. */
094b34ac 2925 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 2926 find_entry.hash.line_sect_off = line_offset;
976ca316 2927 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
2928 &find_entry, INSERT);
2929 if (*slot != NULL)
2930 {
9a3c8263 2931 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 2932 return;
7b9f3c50
DE
2933 }
2934
3019eac3 2935 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2936 }
2937 if (lh == NULL)
2938 {
094b34ac 2939 lh_cu->v.quick->no_file_data = 1;
dee91e82 2940 return;
9291a0cd
TT
2941 }
2942
976ca316 2943 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 2944 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 2945 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
2946 gdb_assert (slot != NULL);
2947 *slot = qfn;
9291a0cd 2948
d721ba37 2949 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 2950
aa391654
TT
2951 int offset = 0;
2952 if (strcmp (fnd.name, "<unknown>") != 0)
2953 ++offset;
2954
7ba99d21 2955 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 2956 qfn->file_names =
976ca316 2957 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 2958 qfn->num_file_names);
aa391654
TT
2959 if (offset != 0)
2960 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 2961 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
2962 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
2963 fnd.comp_dir).release ();
7b9f3c50 2964 qfn->real_names = NULL;
9291a0cd 2965
094b34ac 2966 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2967}
2968
2969/* A helper for the "quick" functions which attempts to read the line
2970 table for THIS_CU. */
2971
2972static struct quick_file_names *
ab432490
SM
2973dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2974 dwarf2_per_objfile *per_objfile)
dee91e82 2975{
0186c6a7
DE
2976 /* This should never be called for TUs. */
2977 gdb_assert (! this_cu->is_debug_types);
2978 /* Nor type unit groups. */
197400e8 2979 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 2980
dee91e82
DE
2981 if (this_cu->v.quick->file_names != NULL)
2982 return this_cu->v.quick->file_names;
2983 /* If we know there is no line data, no point in looking again. */
2984 if (this_cu->v.quick->no_file_data)
2985 return NULL;
2986
ab432490 2987 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 2988 if (!reader.dummy_p)
eb5dd737 2989 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
2990
2991 if (this_cu->v.quick->no_file_data)
2992 return NULL;
2993 return this_cu->v.quick->file_names;
9291a0cd
TT
2994}
2995
2996/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2997 real path for a given file name from the line table. */
2fdf6df6 2998
9291a0cd 2999static const char *
976ca316 3000dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3001 struct quick_file_names *qfn, int index)
9291a0cd 3002{
7b9f3c50 3003 if (qfn->real_names == NULL)
976ca316 3004 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3005 qfn->num_file_names, const char *);
9291a0cd 3006
7b9f3c50 3007 if (qfn->real_names[index] == NULL)
14278e1f 3008 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3009
7b9f3c50 3010 return qfn->real_names[index];
9291a0cd
TT
3011}
3012
39298a5d
TT
3013struct symtab *
3014dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
9291a0cd 3015{
976ca316 3016 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
0d305d5c
TT
3017 dwarf2_per_cu_data *dwarf_cu
3018 = per_objfile->per_bfd->all_comp_units.back ().get ();
976ca316 3019 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3020
43f3e411
DE
3021 if (cust == NULL)
3022 return NULL;
ed2dc618 3023
43f3e411 3024 return compunit_primary_filetab (cust);
9291a0cd
TT
3025}
3026
7b9f3c50
DE
3027/* Traversal function for dw2_forget_cached_source_info. */
3028
3029static int
3030dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3031{
7b9f3c50 3032 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3033
7b9f3c50 3034 if (file_data->real_names)
9291a0cd 3035 {
7b9f3c50 3036 int i;
9291a0cd 3037
7b9f3c50 3038 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3039 {
7b9f3c50
DE
3040 xfree ((void*) file_data->real_names[i]);
3041 file_data->real_names[i] = NULL;
9291a0cd
TT
3042 }
3043 }
7b9f3c50
DE
3044
3045 return 1;
3046}
3047
39298a5d
TT
3048void
3049dwarf2_base_index_functions::forget_cached_source_info
3050 (struct objfile *objfile)
7b9f3c50 3051{
976ca316 3052 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3053
976ca316 3054 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3055 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3056}
3057
da51c347
DE
3058/* Struct used to manage iterating over all CUs looking for a symbol. */
3059
3060struct dw2_symtab_iterator
9291a0cd 3061{
ed2dc618 3062 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3063 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3064 /* If set, only look for symbols that match that block. Valid values are
3065 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3066 gdb::optional<block_enum> block_index;
da51c347
DE
3067 /* The kind of symbol we're looking for. */
3068 domain_enum domain;
3069 /* The list of CUs from the index entry of the symbol,
3070 or NULL if not found. */
42c2c694 3071 offset_view vec;
da51c347
DE
3072 /* The next element in VEC to look at. */
3073 int next;
3074 /* The number of elements in VEC, or zero if there is no match. */
3075 int length;
8943b874
DE
3076 /* Have we seen a global version of the symbol?
3077 If so we can ignore all further global instances.
3078 This is to work around gold/15646, inefficient gold-generated
3079 indices. */
3080 int global_seen;
da51c347 3081};
9291a0cd 3082
7089bd88 3083/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2fdf6df6 3084
9291a0cd 3085static void
7089bd88
TT
3086dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3087 dwarf2_per_objfile *per_objfile,
3088 gdb::optional<block_enum> block_index,
3089 domain_enum domain, offset_type namei)
da51c347 3090{
976ca316 3091 iter->per_objfile = per_objfile;
da51c347
DE
3092 iter->block_index = block_index;
3093 iter->domain = domain;
3094 iter->next = 0;
8943b874 3095 iter->global_seen = 0;
42c2c694 3096 iter->vec = {};
e5f3ece2 3097 iter->length = 0;
e5f3ece2
TV
3098
3099 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3100 /* index is NULL if OBJF_READNOW. */
3101 if (index == NULL)
3102 return;
3103
3104 gdb_assert (!index->symbol_name_slot_invalid (namei));
42c2c694 3105 offset_type vec_idx = index->symbol_vec_index (namei);
e5f3ece2 3106
42c2c694
TT
3107 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3108 iter->length = iter->vec[0];
da51c347
DE
3109}
3110
3111/* Return the next matching CU or NULL if there are no more. */
3112
3113static struct dwarf2_per_cu_data *
3114dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3115{
976ca316 3116 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3117
da51c347
DE
3118 for ( ; iter->next < iter->length; ++iter->next)
3119 {
42c2c694 3120 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
da51c347 3121 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3122 gdb_index_symbol_kind symbol_kind =
3123 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3124 /* Only check the symbol attributes if they're present.
3125 Indices prior to version 7 don't record them,
3126 and indices >= 7 may elide them for certain symbols
3127 (gold does this). */
3128 int attrs_valid =
976ca316 3129 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3130 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3131
3190f0c6 3132 /* Don't crash on bad data. */
91eea9cc 3133 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3190f0c6 3134 {
b98664d3 3135 complaint (_(".gdb_index entry has bad CU index"
976ca316 3136 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3137 continue;
3138 }
3139
91eea9cc 3140 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3190f0c6 3141
da51c347 3142 /* Skip if already read in. */
976ca316 3143 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3144 continue;
3145
8943b874
DE
3146 /* Check static vs global. */
3147 if (attrs_valid)
3148 {
2b79f376
SM
3149 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3150
3151 if (iter->block_index.has_value ())
3152 {
3153 bool want_static = *iter->block_index == STATIC_BLOCK;
3154
3155 if (is_static != want_static)
3156 continue;
3157 }
3158
8943b874 3159 /* Work around gold/15646. */
f030440d
TV
3160 if (!is_static
3161 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3162 {
3163 if (iter->global_seen)
3164 continue;
3165
3166 iter->global_seen = 1;
3167 }
8943b874 3168 }
da51c347
DE
3169
3170 /* Only check the symbol's kind if it has one. */
3171 if (attrs_valid)
3172 {
3173 switch (iter->domain)
3174 {
3175 case VAR_DOMAIN:
3176 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3177 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3178 /* Some types are also in VAR_DOMAIN. */
3179 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3180 continue;
3181 break;
3182 case STRUCT_DOMAIN:
3183 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3184 continue;
3185 break;
3186 case LABEL_DOMAIN:
3187 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3188 continue;
3189 break;
59c35742
AB
3190 case MODULE_DOMAIN:
3191 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3192 continue;
3193 break;
da51c347
DE
3194 default:
3195 break;
3196 }
3197 }
3198
3199 ++iter->next;
3200 return per_cu;
3201 }
3202
3203 return NULL;
3204}
3205
39298a5d 3206void
4829711b
TT
3207dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3208 bool print_bcache)
9291a0cd 3209{
4829711b
TT
3210 if (print_bcache)
3211 return;
3212
976ca316 3213 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
91eea9cc 3214 int total = per_objfile->per_bfd->all_comp_units.size ();
ed2dc618 3215 int count = 0;
9291a0cd 3216
ed2dc618 3217 for (int i = 0; i < total; ++i)
9291a0cd 3218 {
91eea9cc 3219 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
9291a0cd 3220
976ca316 3221 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3222 ++count;
3223 }
e4a48d9d 3224 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3225 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3226}
3227
779bd270
DE
3228/* This dumps minimal information about the index.
3229 It is called via "mt print objfiles".
3230 One use is to verify .gdb_index has been loaded by the
3231 gdb.dwarf2/gdb-index.exp testcase. */
3232
39298a5d
TT
3233void
3234dwarf2_gdb_index::dump (struct objfile *objfile)
9291a0cd 3235{
976ca316 3236 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3237
976ca316 3238 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3239 printf_filtered (".gdb_index:");
976ca316 3240 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3241 {
3242 printf_filtered (" version %d\n",
976ca316 3243 per_objfile->per_bfd->index_table->version);
779bd270
DE
3244 }
3245 else
3246 printf_filtered (" faked for \"readnow\"\n");
3247 printf_filtered ("\n");
9291a0cd
TT
3248}
3249
39298a5d
TT
3250void
3251dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
9291a0cd 3252{
976ca316 3253 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
91eea9cc 3254 int total_units = per_objfile->per_bfd->all_comp_units.size ();
9291a0cd 3255
ed2dc618 3256 for (int i = 0; i < total_units; ++i)
9291a0cd 3257 {
91eea9cc 3258 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
9291a0cd 3259
58f0c718
TT
3260 /* We don't want to directly expand a partial CU, because if we
3261 read it with the wrong language, then assertion failures can
3262 be triggered later on. See PR symtab/23010. So, tell
3263 dw2_instantiate_symtab to skip partial CUs -- any important
3264 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3265 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3266 }
3267}
3268
df35e626 3269static bool
9a0bacfb
TV
3270dw2_expand_symtabs_matching_symbol
3271 (mapped_index_base &index,
3272 const lookup_name_info &lookup_name_in,
3273 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3274 gdb::function_view<bool (offset_type)> match_callback,
3275 dwarf2_per_objfile *per_objfile);
9a0bacfb 3276
df35e626 3277static bool
9a0bacfb 3278dw2_expand_symtabs_matching_one
97a1449a
SM
3279 (dwarf2_per_cu_data *per_cu,
3280 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3281 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3282 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3283
af82f89d 3284void
0b7b2c2a 3285dwarf2_gdb_index::expand_matching_symbols
199b4314 3286 (struct objfile *objfile,
b054970d 3287 const lookup_name_info &name, domain_enum domain,
199b4314 3288 int global,
199b4314 3289 symbol_compare_ftype *ordered_compare)
9291a0cd 3290{
1aa98955 3291 /* Used for Ada. */
976ca316 3292 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3293
9a0bacfb
TV
3294 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3295
976ca316 3296 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3297 {
976ca316 3298 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3299
9a0bacfb
TV
3300 const char *match_name = name.ada ().lookup_name ().c_str ();
3301 auto matcher = [&] (const char *symname)
3302 {
3303 if (ordered_compare == nullptr)
3304 return true;
3305 return ordered_compare (symname, match_name) == 0;
3306 };
3307
b64f703b 3308 dw2_expand_symtabs_matching_symbol (index, name, matcher,
9a0bacfb
TV
3309 [&] (offset_type namei)
3310 {
3311 struct dw2_symtab_iterator iter;
3312 struct dwarf2_per_cu_data *per_cu;
3313
976ca316 3314 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3315 namei);
9a0bacfb 3316 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3317 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3318 nullptr);
9a0bacfb 3319 return true;
976ca316 3320 }, per_objfile);
9a0bacfb
TV
3321 }
3322 else
3323 {
3324 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3325 proceed assuming all symtabs have been read in. */
3326 }
9291a0cd
TT
3327}
3328
e1ef7d7a
PA
3329/* Starting from a search name, return the string that finds the upper
3330 bound of all strings that start with SEARCH_NAME in a sorted name
3331 list. Returns the empty string to indicate that the upper bound is
3332 the end of the list. */
3333
3334static std::string
3335make_sort_after_prefix_name (const char *search_name)
3336{
3337 /* When looking to complete "func", we find the upper bound of all
3338 symbols that start with "func" by looking for where we'd insert
3339 the closest string that would follow "func" in lexicographical
3340 order. Usually, that's "func"-with-last-character-incremented,
3341 i.e. "fund". Mind non-ASCII characters, though. Usually those
3342 will be UTF-8 multi-byte sequences, but we can't be certain.
3343 Especially mind the 0xff character, which is a valid character in
3344 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3345 rule out compilers allowing it in identifiers. Note that
3346 conveniently, strcmp/strcasecmp are specified to compare
3347 characters interpreted as unsigned char. So what we do is treat
3348 the whole string as a base 256 number composed of a sequence of
3349 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3350 to 0, and carries 1 to the following more-significant position.
3351 If the very first character in SEARCH_NAME ends up incremented
3352 and carries/overflows, then the upper bound is the end of the
3353 list. The string after the empty string is also the empty
3354 string.
3355
3356 Some examples of this operation:
3357
3358 SEARCH_NAME => "+1" RESULT
3359
3360 "abc" => "abd"
3361 "ab\xff" => "ac"
3362 "\xff" "a" "\xff" => "\xff" "b"
3363 "\xff" => ""
3364 "\xff\xff" => ""
3365 "" => ""
3366
3367 Then, with these symbols for example:
3368
3369 func
3370 func1
3371 fund
3372
3373 completing "func" looks for symbols between "func" and
3374 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3375 which finds "func" and "func1", but not "fund".
3376
3377 And with:
3378
3379 funcÿ (Latin1 'ÿ' [0xff])
3380 funcÿ1
3381 fund
3382
3383 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3384 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3385
3386 And with:
3387
3388 ÿÿ (Latin1 'ÿ' [0xff])
3389 ÿÿ1
3390
3391 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3392 the end of the list.
3393 */
3394 std::string after = search_name;
3395 while (!after.empty () && (unsigned char) after.back () == 0xff)
3396 after.pop_back ();
3397 if (!after.empty ())
3398 after.back () = (unsigned char) after.back () + 1;
3399 return after;
3400}
3401
5c58de74 3402/* See declaration. */
61d96d7e 3403
5c58de74
PA
3404std::pair<std::vector<name_component>::const_iterator,
3405 std::vector<name_component>::const_iterator>
44ed8f3e 3406mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3407 (const lookup_name_info &lookup_name_without_params, language lang,
3408 dwarf2_per_objfile *per_objfile) const
3f563c84 3409{
5c58de74
PA
3410 auto *name_cmp
3411 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3412
3b00ef10 3413 const char *lang_name
e0802d59 3414 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3415
3f563c84
PA
3416 /* Comparison function object for lower_bound that matches against a
3417 given symbol name. */
3418 auto lookup_compare_lower = [&] (const name_component &elem,
3419 const char *name)
3420 {
fcf23d5b 3421 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3422 const char *elem_name = elem_qualified + elem.name_offset;
3423 return name_cmp (elem_name, name) < 0;
3424 };
3425
3426 /* Comparison function object for upper_bound that matches against a
3427 given symbol name. */
3428 auto lookup_compare_upper = [&] (const char *name,
3429 const name_component &elem)
3430 {
fcf23d5b 3431 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3432 const char *elem_name = elem_qualified + elem.name_offset;
3433 return name_cmp (name, elem_name) < 0;
3434 };
3435
5c58de74
PA
3436 auto begin = this->name_components.begin ();
3437 auto end = this->name_components.end ();
3f563c84
PA
3438
3439 /* Find the lower bound. */
3440 auto lower = [&] ()
3441 {
3b00ef10 3442 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3443 return begin;
3444 else
3b00ef10 3445 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3446 } ();
3447
3448 /* Find the upper bound. */
3449 auto upper = [&] ()
3450 {
5c58de74 3451 if (lookup_name_without_params.completion_mode ())
3f563c84 3452 {
e1ef7d7a
PA
3453 /* In completion mode, we want UPPER to point past all
3454 symbols names that have the same prefix. I.e., with
3455 these symbols, and completing "func":
3456
3457 function << lower bound
3458 function1
3459 other_function << upper bound
3460
3461 We find the upper bound by looking for the insertion
3462 point of "func"-with-last-character-incremented,
3463 i.e. "fund". */
3b00ef10 3464 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3465 if (after.empty ())
3f563c84 3466 return end;
e6b2f5ef
PA
3467 return std::lower_bound (lower, end, after.c_str (),
3468 lookup_compare_lower);
3f563c84
PA
3469 }
3470 else
3b00ef10 3471 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3472 } ();
3473
5c58de74
PA
3474 return {lower, upper};
3475}
3476
3477/* See declaration. */
3478
3479void
fcf23d5b 3480mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3481{
3482 if (!this->name_components.empty ())
3483 return;
3484
3485 this->name_components_casing = case_sensitivity;
3486 auto *name_cmp
3487 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3488
3489 /* The code below only knows how to break apart components of C++
3490 symbol names (and other languages that use '::' as
3b00ef10 3491 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3492 auto count = this->symbol_name_count ();
3493 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3494 {
44ed8f3e 3495 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3496 continue;
3497
fcf23d5b 3498 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3499
3500 /* Add each name component to the name component table. */
3501 unsigned int previous_len = 0;
3b00ef10
TT
3502
3503 if (strstr (name, "::") != nullptr)
3504 {
3505 for (unsigned int current_len = cp_find_first_component (name);
3506 name[current_len] != '\0';
3507 current_len += cp_find_first_component (name + current_len))
3508 {
3509 gdb_assert (name[current_len] == ':');
3510 this->name_components.push_back ({previous_len, idx});
3511 /* Skip the '::'. */
3512 current_len += 2;
3513 previous_len = current_len;
3514 }
3515 }
3516 else
5c58de74 3517 {
3b00ef10
TT
3518 /* Handle the Ada encoded (aka mangled) form here. */
3519 for (const char *iter = strstr (name, "__");
3520 iter != nullptr;
3521 iter = strstr (iter, "__"))
3522 {
3523 this->name_components.push_back ({previous_len, idx});
3524 iter += 2;
3525 previous_len = iter - name;
3526 }
5c58de74 3527 }
3b00ef10 3528
5c58de74
PA
3529 this->name_components.push_back ({previous_len, idx});
3530 }
3531
3532 /* Sort name_components elements by name. */
3533 auto name_comp_compare = [&] (const name_component &left,
3534 const name_component &right)
3535 {
fcf23d5b
SM
3536 const char *left_qualified
3537 = this->symbol_name_at (left.idx, per_objfile);
3538 const char *right_qualified
3539 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
3540
3541 const char *left_name = left_qualified + left.name_offset;
3542 const char *right_name = right_qualified + right.name_offset;
3543
3544 return name_cmp (left_name, right_name) < 0;
3545 };
3546
3547 std::sort (this->name_components.begin (),
3548 this->name_components.end (),
3549 name_comp_compare);
3550}
3551
3552/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3553 mapped_index_base instead of the containing objfile. This is split
3554 to a separate function in order to be able to unit test the
3555 name_components matching using a mock mapped_index_base. For each
5c58de74 3556 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3557 symbol's index in the mapped_index_base symbol table. */
5c58de74 3558
df35e626 3559static bool
5c58de74 3560dw2_expand_symtabs_matching_symbol
44ed8f3e 3561 (mapped_index_base &index,
5c58de74
PA
3562 const lookup_name_info &lookup_name_in,
3563 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3564 gdb::function_view<bool (offset_type)> match_callback,
3565 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3566{
3567 lookup_name_info lookup_name_without_params
3568 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3569
3570 /* Build the symbol name component sorted vector, if we haven't
3571 yet. */
fcf23d5b 3572 index.build_name_components (per_objfile);
5c58de74 3573
3f563c84
PA
3574 /* The same symbol may appear more than once in the range though.
3575 E.g., if we're looking for symbols that complete "w", and we have
3576 a symbol named "w1::w2", we'll find the two name components for
3577 that same symbol in the range. To be sure we only call the
3578 callback once per symbol, we first collect the symbol name
3579 indexes that matched in a temporary vector and ignore
3580 duplicates. */
3581 std::vector<offset_type> matches;
3f563c84 3582
3b00ef10
TT
3583 struct name_and_matcher
3584 {
3585 symbol_name_matcher_ftype *matcher;
ecc6c606 3586 const char *name;
3b00ef10
TT
3587
3588 bool operator== (const name_and_matcher &other) const
3f563c84 3589 {
ecc6c606 3590 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3591 }
3592 };
3593
3594 /* A vector holding all the different symbol name matchers, for all
3595 languages. */
3596 std::vector<name_and_matcher> matchers;
3597
3598 for (int i = 0; i < nr_languages; i++)
3599 {
3600 enum language lang_e = (enum language) i;
3601
3602 const language_defn *lang = language_def (lang_e);
3603 symbol_name_matcher_ftype *name_matcher
c9debfb9 3604 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 3605
3b00ef10 3606 name_and_matcher key {
dda83cd7 3607 name_matcher,
3b00ef10
TT
3608 lookup_name_without_params.language_lookup_name (lang_e)
3609 };
3610
3611 /* Don't insert the same comparison routine more than once.
3612 Note that we do this linear walk. This is not a problem in
3613 practice because the number of supported languages is
3614 low. */
3615 if (std::find (matchers.begin (), matchers.end (), key)
3616 != matchers.end ())
9291a0cd 3617 continue;
3b00ef10
TT
3618 matchers.push_back (std::move (key));
3619
3620 auto bounds
3621 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 3622 lang_e, per_objfile);
3b00ef10
TT
3623
3624 /* Now for each symbol name in range, check to see if we have a name
3625 match, and if so, call the MATCH_CALLBACK callback. */
3626
3627 for (; bounds.first != bounds.second; ++bounds.first)
3628 {
fcf23d5b
SM
3629 const char *qualified
3630 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
3631
3632 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3633 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3634 continue;
9291a0cd 3635
3b00ef10
TT
3636 matches.push_back (bounds.first->idx);
3637 }
3f563c84
PA
3638 }
3639
3640 std::sort (matches.begin (), matches.end ());
3641
3642 /* Finally call the callback, once per match. */
3643 ULONGEST prev = -1;
df35e626 3644 bool result = true;
3f563c84
PA
3645 for (offset_type idx : matches)
3646 {
3647 if (prev != idx)
3648 {
3b00ef10 3649 if (!match_callback (idx))
df35e626
TT
3650 {
3651 result = false;
3652 break;
3653 }
3f563c84
PA
3654 prev = idx;
3655 }
3656 }
3657
3658 /* Above we use a type wider than idx's for 'prev', since 0 and
3659 (offset_type)-1 are both possible values. */
3660 static_assert (sizeof (prev) > sizeof (offset_type), "");
df35e626
TT
3661
3662 return result;
3f563c84
PA
3663}
3664
c62446b1
PA
3665#if GDB_SELF_TEST
3666
3667namespace selftests { namespace dw2_expand_symtabs_matching {
3668
a3c5fafd
PA
3669/* A mock .gdb_index/.debug_names-like name index table, enough to
3670 exercise dw2_expand_symtabs_matching_symbol, which works with the
3671 mapped_index_base interface. Builds an index from the symbol list
3672 passed as parameter to the constructor. */
3673class mock_mapped_index : public mapped_index_base
c62446b1
PA
3674{
3675public:
a3c5fafd
PA
3676 mock_mapped_index (gdb::array_view<const char *> symbols)
3677 : m_symbol_table (symbols)
c62446b1
PA
3678 {}
3679
a3c5fafd 3680 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 3681
a3c5fafd 3682 /* Return the number of names in the symbol table. */
632e107b 3683 size_t symbol_name_count () const override
c62446b1 3684 {
a3c5fafd 3685 return m_symbol_table.size ();
c62446b1
PA
3686 }
3687
a3c5fafd 3688 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
3689 const char *symbol_name_at
3690 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
3691 {
3692 return m_symbol_table[idx];
3693 }
c62446b1 3694
a3c5fafd
PA
3695private:
3696 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
3697};
3698
3699/* Convenience function that converts a NULL pointer to a "<null>"
3700 string, to pass to print routines. */
3701
3702static const char *
3703string_or_null (const char *str)
3704{
3705 return str != NULL ? str : "<null>";
3706}
3707
3708/* Check if a lookup_name_info built from
3709 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3710 index. EXPECTED_LIST is the list of expected matches, in expected
3711 matching order. If no match expected, then an empty list is
3712 specified. Returns true on success. On failure prints a warning
3713 indicating the file:line that failed, and returns false. */
3714
3715static bool
3716check_match (const char *file, int line,
3717 mock_mapped_index &mock_index,
3718 const char *name, symbol_name_match_type match_type,
3719 bool completion_mode,
fcf23d5b
SM
3720 std::initializer_list<const char *> expected_list,
3721 dwarf2_per_objfile *per_objfile)
c62446b1
PA
3722{
3723 lookup_name_info lookup_name (name, match_type, completion_mode);
3724
3725 bool matched = true;
3726
3727 auto mismatch = [&] (const char *expected_str,
3728 const char *got)
3729 {
3730 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3731 "expected=\"%s\", got=\"%s\"\n"),
3732 file, line,
3733 (match_type == symbol_name_match_type::FULL
3734 ? "FULL" : "WILD"),
3735 name, string_or_null (expected_str), string_or_null (got));
3736 matched = false;
3737 };
3738
3739 auto expected_it = expected_list.begin ();
3740 auto expected_end = expected_list.end ();
3741
a3c5fafd 3742 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
b64f703b 3743 nullptr,
c62446b1
PA
3744 [&] (offset_type idx)
3745 {
fcf23d5b 3746 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
3747 const char *expected_str
3748 = expected_it == expected_end ? NULL : *expected_it++;
3749
3750 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3751 mismatch (expected_str, matched_name);
3b00ef10 3752 return true;
fcf23d5b 3753 }, per_objfile);
c62446b1
PA
3754
3755 const char *expected_str
3756 = expected_it == expected_end ? NULL : *expected_it++;
3757 if (expected_str != NULL)
3758 mismatch (expected_str, NULL);
3759
3760 return matched;
3761}
3762
3763/* The symbols added to the mock mapped_index for testing (in
3764 canonical form). */
3765static const char *test_symbols[] = {
3766 "function",
3767 "std::bar",
3768 "std::zfunction",
3769 "std::zfunction2",
3770 "w1::w2",
3771 "ns::foo<char*>",
3772 "ns::foo<int>",
3773 "ns::foo<long>",
a20714ff
PA
3774 "ns2::tmpl<int>::foo2",
3775 "(anonymous namespace)::A::B::C",
c62446b1 3776
e1ef7d7a
PA
3777 /* These are used to check that the increment-last-char in the
3778 matching algorithm for completion doesn't match "t1_fund" when
3779 completing "t1_func". */
3780 "t1_func",
3781 "t1_func1",
3782 "t1_fund",
3783 "t1_fund1",
3784
3785 /* A UTF-8 name with multi-byte sequences to make sure that
3786 cp-name-parser understands this as a single identifier ("função"
3787 is "function" in PT). */
3788 u8"u8função",
3789
3790 /* \377 (0xff) is Latin1 'ÿ'. */
3791 "yfunc\377",
3792
3793 /* \377 (0xff) is Latin1 'ÿ'. */
3794 "\377",
3795 "\377\377123",
3796
c62446b1
PA
3797 /* A name with all sorts of complications. Starts with "z" to make
3798 it easier for the completion tests below. */
3799#define Z_SYM_NAME \
3800 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3801 "::tuple<(anonymous namespace)::ui*, " \
3802 "std::default_delete<(anonymous namespace)::ui>, void>"
3803
3804 Z_SYM_NAME
3805};
3806
a3c5fafd
PA
3807/* Returns true if the mapped_index_base::find_name_component_bounds
3808 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3809 in completion mode. */
5c58de74
PA
3810
3811static bool
a3c5fafd 3812check_find_bounds_finds (mapped_index_base &index,
5c58de74 3813 const char *search_name,
fcf23d5b
SM
3814 gdb::array_view<const char *> expected_syms,
3815 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3816{
3817 lookup_name_info lookup_name (search_name,
3818 symbol_name_match_type::FULL, true);
3819
3b00ef10 3820 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
3821 language_cplus,
3822 per_objfile);
5c58de74
PA
3823
3824 size_t distance = std::distance (bounds.first, bounds.second);
3825 if (distance != expected_syms.size ())
3826 return false;
3827
3828 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3829 {
3830 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 3831 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
3832 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3833 return false;
3834 }
3835
3836 return true;
3837}
3838
3839/* Test the lower-level mapped_index::find_name_component_bounds
3840 method. */
3841
c62446b1 3842static void
5c58de74
PA
3843test_mapped_index_find_name_component_bounds ()
3844{
3845 mock_mapped_index mock_index (test_symbols);
3846
fcf23d5b 3847 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
3848
3849 /* Test the lower-level mapped_index::find_name_component_bounds
3850 method in completion mode. */
3851 {
3852 static const char *expected_syms[] = {
3853 "t1_func",
3854 "t1_func1",
5c58de74
PA
3855 };
3856
fcf23d5b
SM
3857 SELF_CHECK (check_find_bounds_finds
3858 (mock_index, "t1_func", expected_syms,
3859 NULL /* per_objfile */));
5c58de74
PA
3860 }
3861
3862 /* Check that the increment-last-char in the name matching algorithm
3863 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3864 {
3865 static const char *expected_syms1[] = {
3866 "\377",
3867 "\377\377123",
3868 };
fcf23d5b
SM
3869 SELF_CHECK (check_find_bounds_finds
3870 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
3871
3872 static const char *expected_syms2[] = {
3873 "\377\377123",
3874 };
fcf23d5b
SM
3875 SELF_CHECK (check_find_bounds_finds
3876 (mock_index, "\377\377", expected_syms2,
3877 NULL /* per_objfile */));
5c58de74
PA
3878 }
3879}
3880
3881/* Test dw2_expand_symtabs_matching_symbol. */
3882
3883static void
3884test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
3885{
3886 mock_mapped_index mock_index (test_symbols);
3887
3888 /* We let all tests run until the end even if some fails, for debug
3889 convenience. */
3890 bool any_mismatch = false;
3891
3892 /* Create the expected symbols list (an initializer_list). Needed
3893 because lists have commas, and we need to pass them to CHECK,
3894 which is a macro. */
3895#define EXPECT(...) { __VA_ARGS__ }
3896
3897 /* Wrapper for check_match that passes down the current
3898 __FILE__/__LINE__. */
3899#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3900 any_mismatch |= !check_match (__FILE__, __LINE__, \
3901 mock_index, \
3902 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 3903 EXPECTED_LIST, NULL)
c62446b1
PA
3904
3905 /* Identity checks. */
3906 for (const char *sym : test_symbols)
3907 {
3908 /* Should be able to match all existing symbols. */
3909 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3910 EXPECT (sym));
3911
3912 /* Should be able to match all existing symbols with
3913 parameters. */
3914 std::string with_params = std::string (sym) + "(int)";
3915 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3916 EXPECT (sym));
3917
3918 /* Should be able to match all existing symbols with
3919 parameters and qualifiers. */
3920 with_params = std::string (sym) + " ( int ) const";
3921 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3922 EXPECT (sym));
3923
3924 /* This should really find sym, but cp-name-parser.y doesn't
3925 know about lvalue/rvalue qualifiers yet. */
3926 with_params = std::string (sym) + " ( int ) &&";
3927 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3928 {});
3929 }
3930
e1ef7d7a
PA
3931 /* Check that the name matching algorithm for completion doesn't get
3932 confused with Latin1 'ÿ' / 0xff. */
3933 {
3934 static const char str[] = "\377";
3935 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3936 EXPECT ("\377", "\377\377123"));
3937 }
3938
3939 /* Check that the increment-last-char in the matching algorithm for
3940 completion doesn't match "t1_fund" when completing "t1_func". */
3941 {
3942 static const char str[] = "t1_func";
3943 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3944 EXPECT ("t1_func", "t1_func1"));
3945 }
3946
c62446b1
PA
3947 /* Check that completion mode works at each prefix of the expected
3948 symbol name. */
3949 {
3950 static const char str[] = "function(int)";
3951 size_t len = strlen (str);
3952 std::string lookup;
3953
3954 for (size_t i = 1; i < len; i++)
3955 {
3956 lookup.assign (str, i);
3957 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3958 EXPECT ("function"));
3959 }
3960 }
3961
3962 /* While "w" is a prefix of both components, the match function
3963 should still only be called once. */
3964 {
3965 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3966 EXPECT ("w1::w2"));
a20714ff
PA
3967 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3968 EXPECT ("w1::w2"));
c62446b1
PA
3969 }
3970
3971 /* Same, with a "complicated" symbol. */
3972 {
3973 static const char str[] = Z_SYM_NAME;
3974 size_t len = strlen (str);
3975 std::string lookup;
3976
3977 for (size_t i = 1; i < len; i++)
3978 {
3979 lookup.assign (str, i);
3980 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3981 EXPECT (Z_SYM_NAME));
3982 }
3983 }
3984
3985 /* In FULL mode, an incomplete symbol doesn't match. */
3986 {
3987 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3988 {});
3989 }
3990
3991 /* A complete symbol with parameters matches any overload, since the
3992 index has no overload info. */
3993 {
3994 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
3995 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
3996 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
3997 EXPECT ("std::zfunction", "std::zfunction2"));
3998 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
3999 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4000 }
4001
4002 /* Check that whitespace is ignored appropriately. A symbol with a
4003 template argument list. */
4004 {
4005 static const char expected[] = "ns::foo<int>";
4006 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4007 EXPECT (expected));
a20714ff
PA
4008 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4009 EXPECT (expected));
c62446b1
PA
4010 }
4011
4012 /* Check that whitespace is ignored appropriately. A symbol with a
4013 template argument list that includes a pointer. */
4014 {
4015 static const char expected[] = "ns::foo<char*>";
4016 /* Try both completion and non-completion modes. */
4017 static const bool completion_mode[2] = {false, true};
4018 for (size_t i = 0; i < 2; i++)
4019 {
4020 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4021 completion_mode[i], EXPECT (expected));
a20714ff
PA
4022 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4023 completion_mode[i], EXPECT (expected));
c62446b1
PA
4024
4025 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4026 completion_mode[i], EXPECT (expected));
a20714ff
PA
4027 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4028 completion_mode[i], EXPECT (expected));
c62446b1
PA
4029 }
4030 }
4031
4032 {
4033 /* Check method qualifiers are ignored. */
4034 static const char expected[] = "ns::foo<char*>";
4035 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4036 symbol_name_match_type::FULL, true, EXPECT (expected));
4037 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4038 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4039 CHECK_MATCH ("foo < char * > ( int ) const",
4040 symbol_name_match_type::WILD, true, EXPECT (expected));
4041 CHECK_MATCH ("foo < char * > ( int ) &&",
4042 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4043 }
4044
4045 /* Test lookup names that don't match anything. */
4046 {
a20714ff
PA
4047 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4048 {});
4049
c62446b1
PA
4050 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4051 {});
4052 }
4053
a20714ff
PA
4054 /* Some wild matching tests, exercising "(anonymous namespace)",
4055 which should not be confused with a parameter list. */
4056 {
4057 static const char *syms[] = {
4058 "A::B::C",
4059 "B::C",
4060 "C",
4061 "A :: B :: C ( int )",
4062 "B :: C ( int )",
4063 "C ( int )",
4064 };
4065
4066 for (const char *s : syms)
4067 {
4068 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4069 EXPECT ("(anonymous namespace)::A::B::C"));
4070 }
4071 }
4072
4073 {
4074 static const char expected[] = "ns2::tmpl<int>::foo2";
4075 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4076 EXPECT (expected));
4077 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4078 EXPECT (expected));
4079 }
4080
c62446b1
PA
4081 SELF_CHECK (!any_mismatch);
4082
4083#undef EXPECT
4084#undef CHECK_MATCH
4085}
4086
5c58de74
PA
4087static void
4088run_test ()
4089{
4090 test_mapped_index_find_name_component_bounds ();
4091 test_dw2_expand_symtabs_matching_symbol ();
4092}
4093
c62446b1
PA
4094}} // namespace selftests::dw2_expand_symtabs_matching
4095
4096#endif /* GDB_SELF_TEST */
4097
4b514bc8
JK
4098/* If FILE_MATCHER is NULL or if PER_CU has
4099 dwarf2_per_cu_quick_data::MARK set (see
4100 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4101 EXPANSION_NOTIFY on it. */
4102
df35e626 4103static bool
4b514bc8 4104dw2_expand_symtabs_matching_one
97a1449a
SM
4105 (dwarf2_per_cu_data *per_cu,
4106 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4107 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4108 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4109{
4110 if (file_matcher == NULL || per_cu->v.quick->mark)
4111 {
af758d11 4112 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4113
97a1449a
SM
4114 compunit_symtab *symtab
4115 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4116 gdb_assert (symtab != nullptr);
4b514bc8 4117
af758d11 4118 if (expansion_notify != NULL && symtab_was_null)
df35e626 4119 return expansion_notify (symtab);
4b514bc8 4120 }
df35e626 4121 return true;
4b514bc8
JK
4122}
4123
3f563c84
PA
4124/* Helper for dw2_expand_matching symtabs. Called on each symbol
4125 matched, to expand corresponding CUs that were marked. IDX is the
4126 index of the symbol name that matched. */
4127
df35e626 4128static bool
3f563c84 4129dw2_expand_marked_cus
976ca316 4130 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4131 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4132 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4133 block_search_flags search_flags,
3f563c84
PA
4134 search_domain kind)
4135{
42c2c694 4136 offset_type vec_len, vec_idx;
3f563c84 4137 bool global_seen = false;
976ca316 4138 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4139
42c2c694
TT
4140 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4141 vec_len = vec[0];
61920122
PA
4142 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4143 {
42c2c694 4144 offset_type cu_index_and_attrs = vec[vec_idx + 1];
61920122
PA
4145 /* This value is only valid for index versions >= 7. */
4146 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4147 gdb_index_symbol_kind symbol_kind =
4148 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4149 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4150 /* Only check the symbol attributes if they're present.
4151 Indices prior to version 7 don't record them,
4152 and indices >= 7 may elide them for certain symbols
4153 (gold does this). */
4154 int attrs_valid =
4155 (index.version >= 7
4156 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4157
4158 /* Work around gold/15646. */
f030440d
TV
4159 if (attrs_valid
4160 && !is_static
4161 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4162 {
f030440d 4163 if (global_seen)
61920122 4164 continue;
f030440d
TV
4165
4166 global_seen = true;
61920122 4167 }
3190f0c6 4168
61920122
PA
4169 /* Only check the symbol's kind if it has one. */
4170 if (attrs_valid)
4171 {
03a8ea51
TT
4172 if (is_static)
4173 {
4174 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4175 continue;
4176 }
4177 else
4178 {
4179 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4180 continue;
4181 }
4182
61920122 4183 switch (kind)
8943b874 4184 {
61920122
PA
4185 case VARIABLES_DOMAIN:
4186 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4187 continue;
4188 break;
4189 case FUNCTIONS_DOMAIN:
4190 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4191 continue;
61920122
PA
4192 break;
4193 case TYPES_DOMAIN:
4194 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4195 continue;
4196 break;
59c35742
AB
4197 case MODULES_DOMAIN:
4198 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4199 continue;
4200 break;
61920122
PA
4201 default:
4202 break;
8943b874 4203 }
61920122 4204 }
8943b874 4205
61920122 4206 /* Don't crash on bad data. */
91eea9cc 4207 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
61920122 4208 {
b98664d3 4209 complaint (_(".gdb_index entry has bad CU index"
976ca316 4210 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4211 continue;
4212 }
4213
91eea9cc 4214 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
df35e626
TT
4215 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4216 expansion_notify))
4217 return false;
61920122 4218 }
df35e626
TT
4219
4220 return true;
61920122
PA
4221}
4222
4b514bc8
JK
4223/* If FILE_MATCHER is non-NULL, set all the
4224 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4225 that match FILE_MATCHER. */
4226
61920122 4227static void
4b514bc8 4228dw_expand_symtabs_matching_file_matcher
976ca316 4229 (dwarf2_per_objfile *per_objfile,
ed2dc618 4230 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4231{
4b514bc8 4232 if (file_matcher == NULL)
61920122
PA
4233 return;
4234
4b514bc8
JK
4235 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4236 htab_eq_pointer,
4237 NULL, xcalloc, xfree));
4238 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4239 htab_eq_pointer,
4240 NULL, xcalloc, xfree));
61920122 4241
4b514bc8
JK
4242 /* The rule is CUs specify all the files, including those used by
4243 any TU, so there's no need to scan TUs here. */
61920122 4244
0d305d5c 4245 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4246 {
927aa2e7
JK
4247 QUIT;
4248
91eea9cc
TT
4249 if (per_cu->is_debug_types)
4250 continue;
927aa2e7
JK
4251 per_cu->v.quick->mark = 0;
4252
4253 /* We only need to look at symtabs not already expanded. */
0d305d5c 4254 if (per_objfile->symtab_set_p (per_cu.get ()))
927aa2e7
JK
4255 continue;
4256
0d305d5c
TT
4257 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4258 per_objfile);
927aa2e7
JK
4259 if (file_data == NULL)
4260 continue;
4261
4262 if (htab_find (visited_not_found.get (), file_data) != NULL)
4263 continue;
4264 else if (htab_find (visited_found.get (), file_data) != NULL)
4265 {
4266 per_cu->v.quick->mark = 1;
4267 continue;
4268 }
4269
b76e467d 4270 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4271 {
4272 const char *this_real_name;
4273
4274 if (file_matcher (file_data->file_names[j], false))
4275 {
4276 per_cu->v.quick->mark = 1;
4277 break;
4278 }
4279
4280 /* Before we invoke realpath, which can get expensive when many
4281 files are involved, do a quick comparison of the basenames. */
4282 if (!basenames_may_differ
4283 && !file_matcher (lbasename (file_data->file_names[j]),
4284 true))
4285 continue;
4286
976ca316 4287 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4288 if (file_matcher (this_real_name, false))
4289 {
4290 per_cu->v.quick->mark = 1;
4291 break;
4292 }
4293 }
4294
b76e467d
SM
4295 void **slot = htab_find_slot (per_cu->v.quick->mark
4296 ? visited_found.get ()
4297 : visited_not_found.get (),
4298 file_data, INSERT);
927aa2e7
JK
4299 *slot = file_data;
4300 }
4301}
4302
df35e626 4303bool
af82f89d
TT
4304dwarf2_gdb_index::expand_symtabs_matching
4305 (struct objfile *objfile,
4306 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4307 const lookup_name_info *lookup_name,
4308 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4309 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4310 block_search_flags search_flags,
3bfa51a7 4311 domain_enum domain,
af82f89d 4312 enum search_domain kind)
927aa2e7 4313{
976ca316 4314 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4315
4316 /* index_table is NULL if OBJF_READNOW. */
976ca316 4317 if (!per_objfile->per_bfd->index_table)
df35e626 4318 return true;
927aa2e7 4319
976ca316 4320 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4321
c1a66c06
TV
4322 if (symbol_matcher == NULL && lookup_name == NULL)
4323 {
0d305d5c 4324 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4325 {
4326 QUIT;
4327
0d305d5c 4328 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
df35e626
TT
4329 file_matcher,
4330 expansion_notify))
4331 return false;
c1a66c06 4332 }
df35e626 4333 return true;
c1a66c06
TV
4334 }
4335
976ca316 4336 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4337
df35e626
TT
4338 bool result
4339 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4340 symbol_matcher,
4341 [&] (offset_type idx)
927aa2e7 4342 {
df35e626 4343 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
03a8ea51 4344 expansion_notify, search_flags, kind))
df35e626 4345 return false;
3b00ef10 4346 return true;
976ca316 4347 }, per_objfile);
df35e626
TT
4348
4349 return result;
927aa2e7
JK
4350}
4351
4352/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4353 symtab. */
4354
4355static struct compunit_symtab *
4356recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4357 CORE_ADDR pc)
4358{
4359 int i;
4360
4361 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4362 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4363 return cust;
4364
4365 if (cust->includes == NULL)
4366 return NULL;
4367
4368 for (i = 0; cust->includes[i]; ++i)
4369 {
4370 struct compunit_symtab *s = cust->includes[i];
4371
4372 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4373 if (s != NULL)
4374 return s;
4375 }
4376
4377 return NULL;
4378}
4379
39298a5d
TT
4380struct compunit_symtab *
4381dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4382 (struct objfile *objfile,
4383 struct bound_minimal_symbol msymbol,
4384 CORE_ADDR pc,
4385 struct obj_section *section,
4386 int warn_if_readin)
927aa2e7
JK
4387{
4388 struct dwarf2_per_cu_data *data;
4389 struct compunit_symtab *result;
4390
efd7398e
TT
4391 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4392 if (per_objfile->per_bfd->index_addrmap == nullptr)
927aa2e7
JK
4393 return NULL;
4394
b3b3bada 4395 CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e
TT
4396 data = ((struct dwarf2_per_cu_data *)
4397 addrmap_find (per_objfile->per_bfd->index_addrmap,
4398 pc - baseaddr));
927aa2e7
JK
4399 if (!data)
4400 return NULL;
4401
af758d11 4402 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4403 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4404 paddress (objfile->arch (), pc));
927aa2e7 4405
97a1449a
SM
4406 result = recursively_find_pc_sect_compunit_symtab
4407 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4408
927aa2e7
JK
4409 gdb_assert (result != NULL);
4410 return result;
4411}
4412
39298a5d 4413void
f4655dee
TT
4414dwarf2_base_index_functions::map_symbol_filenames
4415 (struct objfile *objfile,
4416 gdb::function_view<symbol_filename_ftype> fun,
4417 bool need_fullname)
927aa2e7 4418{
976ca316 4419 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4420
9fc29955
AB
4421 /* Use caches to ensure we only call FUN once for each filename. */
4422 filename_seen_cache filenames_cache;
4423 std::unordered_set<quick_file_names *> qfn_cache;
927aa2e7 4424
9fc29955
AB
4425 /* The rule is CUs specify all the files, including those used by any TU,
4426 so there's no need to scan TUs here. We can ignore file names coming
4427 from already-expanded CUs. It is possible that an expanded CU might
4428 reuse the file names data from a currently unexpanded CU, in this
4429 case we don't want to report the files from the unexpanded CU. */
927aa2e7 4430
0d305d5c 4431 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
9fc29955 4432 {
0d305d5c 4433 if (per_objfile->symtab_set_p (per_cu.get ()))
927aa2e7 4434 {
9fc29955
AB
4435 if (per_cu->v.quick->file_names != nullptr)
4436 qfn_cache.insert (per_cu->v.quick->file_names);
927aa2e7 4437 }
9fc29955 4438 }
927aa2e7 4439
0d305d5c 4440 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
9fc29955
AB
4441 {
4442 /* We only need to look at symtabs not already expanded. */
0d305d5c 4443 if (per_objfile->symtab_set_p (per_cu.get ()))
9fc29955 4444 continue;
927aa2e7 4445
0d305d5c
TT
4446 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4447 per_objfile);
9fc29955
AB
4448 if (file_data == nullptr
4449 || qfn_cache.find (file_data) != qfn_cache.end ())
4450 continue;
927aa2e7 4451
9fc29955
AB
4452 for (int j = 0; j < file_data->num_file_names; ++j)
4453 {
4454 const char *filename = file_data->file_names[j];
4455 filenames_cache.seen (filename);
927aa2e7
JK
4456 }
4457 }
4458
9fc29955 4459 filenames_cache.traverse ([&] (const char *filename)
927aa2e7
JK
4460 {
4461 gdb::unique_xmalloc_ptr<char> this_real_name;
4462
4463 if (need_fullname)
4464 this_real_name = gdb_realpath (filename);
f4655dee 4465 fun (filename, this_real_name.get ());
927aa2e7
JK
4466 });
4467}
4468
39298a5d
TT
4469bool
4470dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
927aa2e7 4471{
fae2120b 4472 return true;
927aa2e7
JK
4473}
4474
fc4d5ebf
AB
4475/* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4476
4477bool
4478dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4479{
4480 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4481
4482 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4483 {
4484 /* Is this already expanded? */
4485 if (per_objfile->symtab_set_p (per_cu.get ()))
4486 continue;
4487
4488 /* It has not yet been expanded. */
4489 return true;
4490 }
4491
4492 return false;
4493}
4494
927aa2e7
JK
4495/* DWARF-5 debug_names reader. */
4496
4497/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4498static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4499
4500/* A helper function that reads the .debug_names section in SECTION
4501 and fills in MAP. FILENAME is the name of the file containing the
4502 section; it is used for error reporting.
4503
4504 Returns true if all went well, false otherwise. */
4505
4506static bool
4507read_debug_names_from_section (struct objfile *objfile,
4508 const char *filename,
4509 struct dwarf2_section_info *section,
4510 mapped_debug_names &map)
4511{
96b79293 4512 if (section->empty ())
927aa2e7
JK
4513 return false;
4514
4515 /* Older elfutils strip versions could keep the section in the main
4516 executable while splitting it for the separate debug info file. */
96b79293 4517 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4518 return false;
4519
96b79293 4520 section->read (objfile);
927aa2e7 4521
08feed99 4522 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4523
4524 const gdb_byte *addr = section->buffer;
4525
96b79293 4526 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4527
4528 unsigned int bytes_read;
4529 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4530 addr += bytes_read;
4531
4532 map.dwarf5_is_dwarf64 = bytes_read != 4;
4533 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4534 if (bytes_read + length != section->size)
4535 {
4536 /* There may be multiple per-CU indices. */
4537 warning (_("Section .debug_names in %s length %s does not match "
4538 "section length %s, ignoring .debug_names."),
4539 filename, plongest (bytes_read + length),
4540 pulongest (section->size));
4541 return false;
4542 }
4543
4544 /* The version number. */
4545 uint16_t version = read_2_bytes (abfd, addr);
4546 addr += 2;
4547 if (version != 5)
4548 {
4549 warning (_("Section .debug_names in %s has unsupported version %d, "
4550 "ignoring .debug_names."),
4551 filename, version);
4552 return false;
4553 }
4554
4555 /* Padding. */
4556 uint16_t padding = read_2_bytes (abfd, addr);
4557 addr += 2;
4558 if (padding != 0)
4559 {
4560 warning (_("Section .debug_names in %s has unsupported padding %d, "
4561 "ignoring .debug_names."),
4562 filename, padding);
4563 return false;
4564 }
4565
4566 /* comp_unit_count - The number of CUs in the CU list. */
4567 map.cu_count = read_4_bytes (abfd, addr);
4568 addr += 4;
4569
4570 /* local_type_unit_count - The number of TUs in the local TU
4571 list. */
4572 map.tu_count = read_4_bytes (abfd, addr);
4573 addr += 4;
4574
4575 /* foreign_type_unit_count - The number of TUs in the foreign TU
4576 list. */
4577 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4578 addr += 4;
4579 if (foreign_tu_count != 0)
4580 {
4581 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4582 "ignoring .debug_names."),
4583 filename, static_cast<unsigned long> (foreign_tu_count));
4584 return false;
4585 }
4586
4587 /* bucket_count - The number of hash buckets in the hash lookup
4588 table. */
4589 map.bucket_count = read_4_bytes (abfd, addr);
4590 addr += 4;
4591
4592 /* name_count - The number of unique names in the index. */
4593 map.name_count = read_4_bytes (abfd, addr);
4594 addr += 4;
4595
4596 /* abbrev_table_size - The size in bytes of the abbreviations
4597 table. */
4598 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4599 addr += 4;
4600
4601 /* augmentation_string_size - The size in bytes of the augmentation
4602 string. This value is rounded up to a multiple of 4. */
4603 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4604 addr += 4;
4605 map.augmentation_is_gdb = ((augmentation_string_size
4606 == sizeof (dwarf5_augmentation))
4607 && memcmp (addr, dwarf5_augmentation,
4608 sizeof (dwarf5_augmentation)) == 0);
4609 augmentation_string_size += (-augmentation_string_size) & 3;
4610 addr += augmentation_string_size;
4611
4612 /* List of CUs */
4613 map.cu_table_reordered = addr;
4614 addr += map.cu_count * map.offset_size;
4615
4616 /* List of Local TUs */
4617 map.tu_table_reordered = addr;
4618 addr += map.tu_count * map.offset_size;
4619
4620 /* Hash Lookup Table */
4621 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4622 addr += map.bucket_count * 4;
4623 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4624 addr += map.name_count * 4;
4625
4626 /* Name Table */
4627 map.name_table_string_offs_reordered = addr;
4628 addr += map.name_count * map.offset_size;
4629 map.name_table_entry_offs_reordered = addr;
4630 addr += map.name_count * map.offset_size;
4631
4632 const gdb_byte *abbrev_table_start = addr;
4633 for (;;)
4634 {
927aa2e7
JK
4635 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4636 addr += bytes_read;
4637 if (index_num == 0)
4638 break;
4639
4640 const auto insertpair
4641 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4642 if (!insertpair.second)
4643 {
4644 warning (_("Section .debug_names in %s has duplicate index %s, "
4645 "ignoring .debug_names."),
4646 filename, pulongest (index_num));
4647 return false;
4648 }
4649 mapped_debug_names::index_val &indexval = insertpair.first->second;
4650 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4651 addr += bytes_read;
4652
4653 for (;;)
4654 {
4655 mapped_debug_names::index_val::attr attr;
4656 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4657 addr += bytes_read;
4658 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4659 addr += bytes_read;
4660 if (attr.form == DW_FORM_implicit_const)
4661 {
4662 attr.implicit_const = read_signed_leb128 (abfd, addr,
4663 &bytes_read);
4664 addr += bytes_read;
4665 }
4666 if (attr.dw_idx == 0 && attr.form == 0)
4667 break;
4668 indexval.attr_vec.push_back (std::move (attr));
4669 }
4670 }
4671 if (addr != abbrev_table_start + abbrev_table_size)
4672 {
4673 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4674 "of size %s vs. written as %u, ignoring .debug_names."),
4675 filename, plongest (addr - abbrev_table_start),
4676 abbrev_table_size);
927aa2e7
JK
4677 return false;
4678 }
4679 map.entry_pool = addr;
4680
4681 return true;
4682}
4683
4684/* A helper for create_cus_from_debug_names that handles the MAP's CU
4685 list. */
4686
4687static void
168c9250 4688create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4689 const mapped_debug_names &map,
4690 dwarf2_section_info &section,
b76e467d 4691 bool is_dwz)
927aa2e7 4692{
3ee6bb11
TV
4693 if (!map.augmentation_is_gdb)
4694 {
fc9a13fb
TV
4695 for (uint32_t i = 0; i < map.cu_count; ++i)
4696 {
4697 sect_offset sect_off
4698 = (sect_offset) (extract_unsigned_integer
4699 (map.cu_table_reordered + i * map.offset_size,
4700 map.offset_size,
4701 map.dwarf5_byte_order));
4702 /* We don't know the length of the CU, because the CU list in a
4703 .debug_names index can be incomplete, so we can't use the start
4704 of the next CU as end of this CU. We create the CUs here with
4705 length 0, and in cutu_reader::cutu_reader we'll fill in the
4706 actual length. */
473ab964 4707 dwarf2_per_cu_data_up per_cu
fc9a13fb
TV
4708 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4709 sect_off, 0);
0d305d5c 4710 per_bfd->all_comp_units.push_back (std::move (per_cu));
fc9a13fb 4711 }
d3b54e63 4712 return;
3ee6bb11
TV
4713 }
4714
927aa2e7
JK
4715 sect_offset sect_off_prev;
4716 for (uint32_t i = 0; i <= map.cu_count; ++i)
4717 {
4718 sect_offset sect_off_next;
4719 if (i < map.cu_count)
4720 {
4721 sect_off_next
4722 = (sect_offset) (extract_unsigned_integer
4723 (map.cu_table_reordered + i * map.offset_size,
4724 map.offset_size,
4725 map.dwarf5_byte_order));
4726 }
4727 else
4728 sect_off_next = (sect_offset) section.size;
4729 if (i >= 1)
4730 {
4731 const ULONGEST length = sect_off_next - sect_off_prev;
473ab964 4732 dwarf2_per_cu_data_up per_cu
168c9250 4733 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 4734 sect_off_prev, length);
0d305d5c 4735 per_bfd->all_comp_units.push_back (std::move (per_cu));
927aa2e7
JK
4736 }
4737 sect_off_prev = sect_off_next;
4738 }
4739}
4740
4741/* Read the CU list from the mapped index, and use it to create all
ed2dc618 4742 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
4743
4744static void
168c9250 4745create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4746 const mapped_debug_names &map,
4747 const mapped_debug_names &dwz_map)
4748{
168c9250
SM
4749 gdb_assert (per_bfd->all_comp_units.empty ());
4750 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 4751
168c9250 4752 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 4753 false /* is_dwz */);
927aa2e7
JK
4754
4755 if (dwz_map.cu_count == 0)
4756 return;
4757
168c9250
SM
4758 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4759 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 4760 true /* is_dwz */);
927aa2e7
JK
4761}
4762
4763/* Read .debug_names. If everything went ok, initialize the "quick"
4764 elements of all the CUs and return true. Otherwise, return false. */
4765
4766static bool
976ca316 4767dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 4768{
fcf23d5b
SM
4769 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4770 mapped_debug_names dwz_map;
976ca316 4771 struct objfile *objfile = per_objfile->objfile;
f8c41851 4772 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
4773
4774 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
bab287cd 4775 &per_bfd->debug_names, *map))
927aa2e7
JK
4776 return false;
4777
4778 /* Don't use the index if it's empty. */
22ca247e 4779 if (map->name_count == 0)
927aa2e7
JK
4780 return false;
4781
4782 /* If there is a .dwz file, read it so we can get its CU list as
4783 well. */
f8c41851 4784 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
4785 if (dwz != NULL)
4786 {
4787 if (!read_debug_names_from_section (objfile,
00f93c44 4788 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
4789 &dwz->debug_names, dwz_map))
4790 {
4791 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 4792 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
4793 return false;
4794 }
4795 }
4796
f8c41851 4797 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 4798
22ca247e 4799 if (map->tu_count != 0)
927aa2e7
JK
4800 {
4801 /* We can only handle a single .debug_types when we have an
4802 index. */
f8c41851 4803 if (per_bfd->types.size () != 1)
927aa2e7
JK
4804 return false;
4805
f8c41851 4806 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
4807
4808 create_signatured_type_table_from_debug_names
f8c41851 4809 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
4810 }
4811
f8c41851 4812 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 4813
f8c41851
SM
4814 per_bfd->debug_names_table = std::move (map);
4815 per_bfd->using_index = 1;
4816 per_bfd->quick_file_names_table =
bab287cd 4817 create_quick_file_names_table (per_bfd->all_comp_units.size ());
927aa2e7
JK
4818
4819 return true;
4820}
4821
927aa2e7
JK
4822/* Type used to manage iterating over all CUs looking for a symbol for
4823 .debug_names. */
4824
4825class dw2_debug_names_iterator
4826{
4827public:
927aa2e7 4828 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4829 block_search_flags block_index,
2b79f376 4830 domain_enum domain,
fcf23d5b 4831 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 4832 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
4833 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4834 m_per_objfile (per_objfile)
927aa2e7
JK
4835 {}
4836
4837 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4838 search_domain search, uint32_t namei,
3bfa51a7
TT
4839 dwarf2_per_objfile *per_objfile,
4840 domain_enum domain = UNDEF_DOMAIN)
927aa2e7 4841 : m_map (map),
3bfa51a7 4842 m_domain (domain),
927aa2e7 4843 m_search (search),
fcf23d5b
SM
4844 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4845 m_per_objfile (per_objfile)
927aa2e7
JK
4846 {}
4847
3b00ef10 4848 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4849 block_search_flags block_index, domain_enum domain,
fcf23d5b 4850 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 4851 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
4852 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4853 m_per_objfile (per_objfile)
3b00ef10
TT
4854 {}
4855
927aa2e7
JK
4856 /* Return the next matching CU or NULL if there are no more. */
4857 dwarf2_per_cu_data *next ();
4858
4859private:
4860 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
4861 const char *name,
4862 dwarf2_per_objfile *per_objfile);
927aa2e7 4863 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
4864 uint32_t namei,
4865 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
4866
4867 /* The internalized form of .debug_names. */
4868 const mapped_debug_names &m_map;
4869
03a8ea51
TT
4870 /* Restrict the search to these blocks. */
4871 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4872 | SEARCH_STATIC_BLOCK);
927aa2e7
JK
4873
4874 /* The kind of symbol we're looking for. */
4875 const domain_enum m_domain = UNDEF_DOMAIN;
4876 const search_domain m_search = ALL_DOMAIN;
4877
4878 /* The list of CUs from the index entry of the symbol, or NULL if
4879 not found. */
4880 const gdb_byte *m_addr;
fcf23d5b
SM
4881
4882 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
4883};
4884
4885const char *
fcf23d5b 4886mapped_debug_names::namei_to_name
976ca316 4887 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
4888{
4889 const ULONGEST namei_string_offs
4890 = extract_unsigned_integer ((name_table_string_offs_reordered
4891 + namei * offset_size),
4892 offset_size,
4893 dwarf5_byte_order);
976ca316 4894 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
4895}
4896
4897/* Find a slot in .debug_names for the object named NAME. If NAME is
4898 found, return pointer to its pool data. If NAME cannot be found,
4899 return NULL. */
4900
4901const gdb_byte *
4902dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
4903 (const mapped_debug_names &map, const char *name,
4904 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
4905{
4906 int (*cmp) (const char *, const char *);
4907
54ee4252 4908 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
4909 if (current_language->la_language == language_cplus
4910 || current_language->la_language == language_fortran
4911 || current_language->la_language == language_d)
4912 {
4913 /* NAME is already canonical. Drop any qualifiers as
4914 .debug_names does not contain any. */
4915
4916 if (strchr (name, '(') != NULL)
4917 {
54ee4252 4918 without_params = cp_remove_params (name);
927aa2e7 4919 if (without_params != NULL)
54ee4252 4920 name = without_params.get ();
927aa2e7
JK
4921 }
4922 }
4923
4924 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4925
4926 const uint32_t full_hash = dwarf5_djb_hash (name);
4927 uint32_t namei
4928 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4929 (map.bucket_table_reordered
4930 + (full_hash % map.bucket_count)), 4,
4931 map.dwarf5_byte_order);
4932 if (namei == 0)
4933 return NULL;
4934 --namei;
4935 if (namei >= map.name_count)
4936 {
b98664d3 4937 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
4938 "[in module %s]"),
4939 namei, map.name_count,
fcf23d5b 4940 objfile_name (per_objfile->objfile));
927aa2e7
JK
4941 return NULL;
4942 }
4943
4944 for (;;)
4945 {
4946 const uint32_t namei_full_hash
4947 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4948 (map.hash_table_reordered + namei), 4,
4949 map.dwarf5_byte_order);
4950 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4951 return NULL;
4952
4953 if (full_hash == namei_full_hash)
4954 {
fcf23d5b 4955 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
4956
4957#if 0 /* An expensive sanity check. */
4958 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4959 {
b98664d3 4960 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
4961 "[in module %s]"),
4962 namei, objfile_name (dwarf2_per_objfile->objfile));
4963 return NULL;
4964 }
4965#endif
4966
4967 if (cmp (namei_string, name) == 0)
4968 {
4969 const ULONGEST namei_entry_offs
4970 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4971 + namei * map.offset_size),
4972 map.offset_size, map.dwarf5_byte_order);
4973 return map.entry_pool + namei_entry_offs;
4974 }
4975 }
4976
4977 ++namei;
4978 if (namei >= map.name_count)
4979 return NULL;
4980 }
4981}
4982
4983const gdb_byte *
4984dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 4985 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
4986{
4987 if (namei >= map.name_count)
4988 {
b98664d3 4989 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
4990 "[in module %s]"),
4991 namei, map.name_count,
fcf23d5b 4992 objfile_name (per_objfile->objfile));
927aa2e7
JK
4993 return NULL;
4994 }
4995
4996 const ULONGEST namei_entry_offs
4997 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4998 + namei * map.offset_size),
4999 map.offset_size, map.dwarf5_byte_order);
5000 return map.entry_pool + namei_entry_offs;
5001}
5002
5003/* See dw2_debug_names_iterator. */
5004
5005dwarf2_per_cu_data *
5006dw2_debug_names_iterator::next ()
5007{
5008 if (m_addr == NULL)
5009 return NULL;
5010
fcf23d5b
SM
5011 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5012 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5013 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5014
5015 again:
5016
5017 unsigned int bytes_read;
5018 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5019 m_addr += bytes_read;
5020 if (abbrev == 0)
5021 return NULL;
5022
5023 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5024 if (indexval_it == m_map.abbrev_map.cend ())
5025 {
b98664d3 5026 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5027 "[in module %s]"),
ed2dc618 5028 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5029 return NULL;
5030 }
5031 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5032 enum class symbol_linkage {
5033 unknown,
5034 static_,
5035 extern_,
23c13d42 5036 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5037 dwarf2_per_cu_data *per_cu = NULL;
5038 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5039 {
5040 ULONGEST ull;
5041 switch (attr.form)
5042 {
5043 case DW_FORM_implicit_const:
5044 ull = attr.implicit_const;
5045 break;
5046 case DW_FORM_flag_present:
5047 ull = 1;
5048 break;
5049 case DW_FORM_udata:
5050 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5051 m_addr += bytes_read;
5052 break;
6dc55ce9 5053 case DW_FORM_ref4:
5054 ull = read_4_bytes (abfd, m_addr);
5055 m_addr += 4;
5056 break;
5057 case DW_FORM_ref8:
5058 ull = read_8_bytes (abfd, m_addr);
5059 m_addr += 8;
5060 break;
5061 case DW_FORM_ref_sig8:
5062 ull = read_8_bytes (abfd, m_addr);
5063 m_addr += 8;
5064 break;
927aa2e7 5065 default:
b98664d3 5066 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5067 dwarf_form_name (attr.form),
ed2dc618 5068 objfile_name (objfile));
927aa2e7
JK
5069 return NULL;
5070 }
5071 switch (attr.dw_idx)
5072 {
5073 case DW_IDX_compile_unit:
5074 /* Don't crash on bad data. */
bab287cd 5075 if (ull >= per_bfd->all_comp_units.size ())
927aa2e7 5076 {
b98664d3 5077 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5078 " [in module %s]"),
5079 pulongest (ull),
fcf23d5b 5080 objfile_name (objfile));
927aa2e7
JK
5081 continue;
5082 }
91eea9cc 5083 per_cu = per_bfd->get_cu (ull);
927aa2e7 5084 break;
8af5c486
JK
5085 case DW_IDX_type_unit:
5086 /* Don't crash on bad data. */
91eea9cc 5087 if (ull >= per_bfd->tu_stats.nr_tus)
8af5c486 5088 {
b98664d3 5089 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5090 " [in module %s]"),
5091 pulongest (ull),
fcf23d5b 5092 objfile_name (objfile));
8af5c486
JK
5093 continue;
5094 }
91eea9cc 5095 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
8af5c486 5096 break;
6dc55ce9 5097 case DW_IDX_die_offset:
5098 /* In a per-CU index (as opposed to a per-module index), index
5099 entries without CU attribute implicitly refer to the single CU. */
5100 if (per_cu == NULL)
fcf23d5b 5101 per_cu = per_bfd->get_cu (0);
6dc55ce9 5102 break;
927aa2e7
JK
5103 case DW_IDX_GNU_internal:
5104 if (!m_map.augmentation_is_gdb)
5105 break;
23c13d42 5106 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5107 break;
5108 case DW_IDX_GNU_external:
5109 if (!m_map.augmentation_is_gdb)
5110 break;
23c13d42 5111 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5112 break;
5113 }
5114 }
5115
5116 /* Skip if already read in. */
fcf23d5b 5117 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5118 goto again;
5119
5120 /* Check static vs global. */
03a8ea51 5121 if (symbol_linkage_ != symbol_linkage::unknown)
927aa2e7 5122 {
03a8ea51
TT
5123 if (symbol_linkage_ == symbol_linkage::static_)
5124 {
5125 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5126 goto again;
5127 }
5128 else
5129 {
5130 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5131 goto again;
5132 }
927aa2e7
JK
5133 }
5134
5135 /* Match dw2_symtab_iter_next, symbol_kind
5136 and debug_names::psymbol_tag. */
5137 switch (m_domain)
5138 {
5139 case VAR_DOMAIN:
5140 switch (indexval.dwarf_tag)
5141 {
5142 case DW_TAG_variable:
5143 case DW_TAG_subprogram:
5144 /* Some types are also in VAR_DOMAIN. */
5145 case DW_TAG_typedef:
5146 case DW_TAG_structure_type:
5147 break;
5148 default:
5149 goto again;
5150 }
5151 break;
5152 case STRUCT_DOMAIN:
5153 switch (indexval.dwarf_tag)
5154 {
5155 case DW_TAG_typedef:
5156 case DW_TAG_structure_type:
5157 break;
5158 default:
5159 goto again;
5160 }
5161 break;
5162 case LABEL_DOMAIN:
5163 switch (indexval.dwarf_tag)
5164 {
5165 case 0:
5166 case DW_TAG_variable:
5167 break;
5168 default:
5169 goto again;
5170 }
5171 break;
59c35742
AB
5172 case MODULE_DOMAIN:
5173 switch (indexval.dwarf_tag)
5174 {
5175 case DW_TAG_module:
5176 break;
5177 default:
5178 goto again;
5179 }
5180 break;
927aa2e7
JK
5181 default:
5182 break;
5183 }
5184
5185 /* Match dw2_expand_symtabs_matching, symbol_kind and
5186 debug_names::psymbol_tag. */
5187 switch (m_search)
4b514bc8 5188 {
927aa2e7
JK
5189 case VARIABLES_DOMAIN:
5190 switch (indexval.dwarf_tag)
4b514bc8 5191 {
927aa2e7
JK
5192 case DW_TAG_variable:
5193 break;
5194 default:
5195 goto again;
4b514bc8 5196 }
927aa2e7
JK
5197 break;
5198 case FUNCTIONS_DOMAIN:
5199 switch (indexval.dwarf_tag)
4b514bc8 5200 {
927aa2e7
JK
5201 case DW_TAG_subprogram:
5202 break;
5203 default:
5204 goto again;
4b514bc8 5205 }
927aa2e7
JK
5206 break;
5207 case TYPES_DOMAIN:
5208 switch (indexval.dwarf_tag)
5209 {
5210 case DW_TAG_typedef:
5211 case DW_TAG_structure_type:
5212 break;
5213 default:
5214 goto again;
5215 }
5216 break;
59c35742
AB
5217 case MODULES_DOMAIN:
5218 switch (indexval.dwarf_tag)
5219 {
5220 case DW_TAG_module:
5221 break;
5222 default:
5223 goto again;
5224 }
927aa2e7
JK
5225 default:
5226 break;
4b514bc8 5227 }
927aa2e7
JK
5228
5229 return per_cu;
4b514bc8 5230}
61920122 5231
927aa2e7
JK
5232/* This dumps minimal information about .debug_names. It is called
5233 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5234 uses this to verify that .debug_names has been loaded. */
9291a0cd 5235
39298a5d
TT
5236void
5237dwarf2_debug_names_index::dump (struct objfile *objfile)
927aa2e7 5238{
976ca316 5239 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5240
976ca316 5241 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5242 printf_filtered (".debug_names:");
976ca316 5243 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5244 printf_filtered (" exists\n");
5245 else
5246 printf_filtered (" faked for \"readnow\"\n");
5247 printf_filtered ("\n");
9291a0cd
TT
5248}
5249
39298a5d 5250void
0b7b2c2a 5251dwarf2_debug_names_index::expand_matching_symbols
3b00ef10
TT
5252 (struct objfile *objfile,
5253 const lookup_name_info &name, domain_enum domain,
5254 int global,
3b00ef10
TT
5255 symbol_compare_ftype *ordered_compare)
5256{
976ca316 5257 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5258
5259 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5260 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5261 return;
5262
976ca316 5263 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
03a8ea51
TT
5264 const block_search_flags block_flags
5265 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
3b00ef10
TT
5266
5267 const char *match_name = name.ada ().lookup_name ().c_str ();
5268 auto matcher = [&] (const char *symname)
5269 {
5270 if (ordered_compare == nullptr)
5271 return true;
5272 return ordered_compare (symname, match_name) == 0;
5273 };
5274
b64f703b 5275 dw2_expand_symtabs_matching_symbol (map, name, matcher,
3b00ef10
TT
5276 [&] (offset_type namei)
5277 {
5278 /* The name was matched, now expand corresponding CUs that were
5279 marked. */
03a8ea51 5280 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
976ca316 5281 per_objfile);
3b00ef10
TT
5282
5283 struct dwarf2_per_cu_data *per_cu;
5284 while ((per_cu = iter.next ()) != NULL)
976ca316 5285 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5286 nullptr);
3b00ef10 5287 return true;
976ca316 5288 }, per_objfile);
3b00ef10
TT
5289}
5290
df35e626 5291bool
39298a5d 5292dwarf2_debug_names_index::expand_symtabs_matching
927aa2e7
JK
5293 (struct objfile *objfile,
5294 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5295 const lookup_name_info *lookup_name,
927aa2e7
JK
5296 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5297 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 5298 block_search_flags search_flags,
3bfa51a7 5299 domain_enum domain,
927aa2e7
JK
5300 enum search_domain kind)
5301{
976ca316 5302 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5303
927aa2e7 5304 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5305 if (!per_objfile->per_bfd->debug_names_table)
df35e626 5306 return true;
9291a0cd 5307
976ca316 5308 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5309
c1a66c06
TV
5310 if (symbol_matcher == NULL && lookup_name == NULL)
5311 {
0d305d5c 5312 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5313 {
5314 QUIT;
5315
0d305d5c 5316 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
df35e626
TT
5317 file_matcher,
5318 expansion_notify))
5319 return false;
c1a66c06 5320 }
df35e626 5321 return true;
c1a66c06
TV
5322 }
5323
976ca316 5324 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5325
df35e626
TT
5326 bool result
5327 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5328 symbol_matcher,
5329 [&] (offset_type namei)
927aa2e7 5330 {
927aa2e7
JK
5331 /* The name was matched, now expand corresponding CUs that were
5332 marked. */
3bfa51a7 5333 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
bbf2f4df 5334
927aa2e7
JK
5335 struct dwarf2_per_cu_data *per_cu;
5336 while ((per_cu = iter.next ()) != NULL)
df35e626
TT
5337 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5338 file_matcher,
5339 expansion_notify))
5340 return false;
3b00ef10 5341 return true;
976ca316 5342 }, per_objfile);
df35e626
TT
5343
5344 return result;
9291a0cd
TT
5345}
5346
4485a1c1 5347/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5348 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5349
5350template <typename T>
5351static gdb::array_view<const gdb_byte>
5352get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5353{
5354 dwarf2_section_info *section = &section_owner->gdb_index;
5355
96b79293 5356 if (section->empty ())
4485a1c1
SM
5357 return {};
5358
5359 /* Older elfutils strip versions could keep the section in the main
5360 executable while splitting it for the separate debug info file. */
96b79293 5361 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5362 return {};
5363
96b79293 5364 section->read (obj);
4485a1c1 5365
8bebfcda
PA
5366 /* dwarf2_section_info::size is a bfd_size_type, while
5367 gdb::array_view works with size_t. On 32-bit hosts, with
5368 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5369 is 32-bit. So we need an explicit narrowing conversion here.
5370 This is fine, because it's impossible to allocate or mmap an
5371 array/buffer larger than what size_t can represent. */
5372 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5373}
5374
87d6a7aa
SM
5375/* Lookup the index cache for the contents of the index associated to
5376 DWARF2_OBJ. */
5377
5378static gdb::array_view<const gdb_byte>
5989a64e 5379get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5380{
5381 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5382 if (build_id == nullptr)
5383 return {};
5384
5385 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5386 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5387}
5388
5389/* Same as the above, but for DWZ. */
5390
5391static gdb::array_view<const gdb_byte>
5392get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5393{
5394 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5395 if (build_id == nullptr)
5396 return {};
5397
5398 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5399}
5400
edc02ceb 5401/* See dwarf2/public.h. */
9291a0cd 5402
edc02ceb
TT
5403void
5404dwarf2_initialize_objfile (struct objfile *objfile)
9291a0cd 5405{
976ca316
SM
5406 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5407 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5408
850ed749
SM
5409 dwarf_read_debug_printf ("called");
5410
9291a0cd
TT
5411 /* If we're about to read full symbols, don't bother with the
5412 indices. In this case we also don't care if some other debug
5413 format is making psymtabs, because they are all about to be
5414 expanded anyway. */
5415 if ((objfile->flags & OBJF_READNOW))
5416 {
850ed749
SM
5417 dwarf_read_debug_printf ("readnow requested");
5418
17ee85fc
TT
5419 /* When using READNOW, the using_index flag (set below) indicates that
5420 PER_BFD was already initialized, when we loaded some other objfile. */
5421 if (per_bfd->using_index)
5422 {
850ed749 5423 dwarf_read_debug_printf ("using_index already set");
976ca316 5424 per_objfile->resize_symtabs ();
edc02ceb
TT
5425 objfile->qf.push_front (make_dwarf_gdb_index ());
5426 return;
17ee85fc
TT
5427 }
5428
5429 per_bfd->using_index = 1;
976ca316 5430 create_all_comp_units (per_objfile);
17ee85fc
TT
5431 per_bfd->quick_file_names_table
5432 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5433 per_objfile->resize_symtabs ();
9291a0cd 5434
91eea9cc 5435 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
9291a0cd 5436 {
91eea9cc 5437 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
9291a0cd 5438
17ee85fc 5439 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5440 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5441 }
5442
edc02ceb
TT
5443 /* Arrange for gdb to see the "quick" functions. However, these
5444 functions will be no-ops because we will have expanded all
5445 symtabs. */
5446 objfile->qf.push_front (make_dwarf_gdb_index ());
5447 return;
9291a0cd
TT
5448 }
5449
17ee85fc
TT
5450 /* Was a debug names index already read when we processed an objfile sharing
5451 PER_BFD? */
5452 if (per_bfd->debug_names_table != nullptr)
5453 {
850ed749 5454 dwarf_read_debug_printf ("re-using shared debug names table");
976ca316 5455 per_objfile->resize_symtabs ();
edc02ceb
TT
5456 objfile->qf.push_front (make_dwarf_debug_names ());
5457 return;
17ee85fc
TT
5458 }
5459
5460 /* Was a GDB index already read when we processed an objfile sharing
5461 PER_BFD? */
5462 if (per_bfd->index_table != nullptr)
5463 {
850ed749 5464 dwarf_read_debug_printf ("re-using shared index table");
976ca316 5465 per_objfile->resize_symtabs ();
edc02ceb
TT
5466 objfile->qf.push_front (make_dwarf_gdb_index ());
5467 return;
17ee85fc
TT
5468 }
5469
efb763a5
SM
5470 /* There might already be partial symtabs built for this BFD. This happens
5471 when loading the same binary twice with the index-cache enabled. If so,
5472 don't try to read an index. The objfile / per_objfile initialization will
5473 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5474 code path. */
5475 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
5476 {
5477 dwarf_read_debug_printf ("re-using shared partial symtabs");
edc02ceb
TT
5478 objfile->qf.push_front (make_lazy_dwarf_reader ());
5479 return;
850ed749 5480 }
efb763a5 5481
976ca316 5482 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 5483 {
850ed749 5484 dwarf_read_debug_printf ("found debug names");
976ca316 5485 per_objfile->resize_symtabs ();
edc02ceb
TT
5486 objfile->qf.push_front (make_dwarf_debug_names ());
5487 return;
3c0aa29a 5488 }
927aa2e7 5489
976ca316 5490 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 5491 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5492 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 5493 {
850ed749 5494 dwarf_read_debug_printf ("found gdb index from file");
976ca316 5495 per_objfile->resize_symtabs ();
edc02ceb
TT
5496 objfile->qf.push_front (make_dwarf_gdb_index ());
5497 return;
3c0aa29a 5498 }
9291a0cd 5499
87d6a7aa 5500 /* ... otherwise, try to find the index in the index cache. */
976ca316 5501 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
5502 get_gdb_index_contents_from_cache,
5503 get_gdb_index_contents_from_cache_dwz))
5504 {
850ed749 5505 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa 5506 global_index_cache.hit ();
976ca316 5507 per_objfile->resize_symtabs ();
edc02ceb
TT
5508 objfile->qf.push_front (make_dwarf_gdb_index ());
5509 return;
87d6a7aa
SM
5510 }
5511
5512 global_index_cache.miss ();
edc02ceb 5513 objfile->qf.push_front (make_lazy_dwarf_reader ());
9291a0cd
TT
5514}
5515
5516\f
5517
dce234bc
PP
5518/* Build a partial symbol table. */
5519
5520void
eb36a3eb 5521dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
dce234bc 5522{
976ca316
SM
5523 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5524 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
5525
5526 if (per_bfd->partial_symtabs != nullptr)
5527 {
5528 /* Partial symbols were already read, so now we can simply
5529 attach them. */
eb36a3eb
TT
5530 if (psf == nullptr)
5531 {
5532 psf = new psymbol_functions (per_bfd->partial_symtabs);
5533 objfile->qf.emplace_front (psf);
5534 }
5535 else
5536 psf->set_partial_symtabs (per_bfd->partial_symtabs);
976ca316 5537 per_objfile->resize_symtabs ();
17ee85fc
TT
5538 return;
5539 }
c9bf0622 5540
eb36a3eb
TT
5541 if (psf == nullptr)
5542 {
5543 psf = new psymbol_functions;
5544 objfile->qf.emplace_front (psf);
5545 }
5546 const std::shared_ptr<psymtab_storage> &partial_symtabs
5547 = psf->get_partial_symtabs ();
5548
51962708
TT
5549 /* Set the local reference to partial symtabs, so that we don't try
5550 to read them again if reading another objfile with the same BFD.
5551 If we can't in fact share, this won't make a difference anyway as
5552 the dwarf2_per_bfd object won't be shared. */
eb36a3eb 5553 per_bfd->partial_symtabs = partial_symtabs;
51962708 5554
a70b8144 5555 try
c9bf0622
TT
5556 {
5557 /* This isn't really ideal: all the data we allocate on the
5558 objfile's obstack is still uselessly kept around. However,
5559 freeing it seems unsafe. */
eb36a3eb 5560 psymtab_discarder psymtabs (partial_symtabs.get ());
976ca316 5561 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 5562 psymtabs.keep ();
87d6a7aa 5563
976ca316 5564 per_objfile->resize_symtabs ();
af758d11 5565
87d6a7aa 5566 /* (maybe) store an index in the cache. */
976ca316 5567 global_index_cache.store (per_objfile);
c9bf0622 5568 }
230d2906 5569 catch (const gdb_exception_error &except)
492d29ea
PA
5570 {
5571 exception_print (gdb_stderr, except);
5572 }
c906108c 5573}
c906108c 5574
3b80fe9b
DE
5575/* Find the base address of the compilation unit for range lists and
5576 location lists. It will normally be specified by DW_AT_low_pc.
5577 In DWARF-3 draft 4, the base address could be overridden by
5578 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5579 compilation units with discontinuous ranges. */
5580
5581static void
5582dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5583{
5584 struct attribute *attr;
5585
2b24b6e4 5586 cu->base_address.reset ();
3b80fe9b
DE
5587
5588 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5589 if (attr != nullptr)
95f982e5 5590 cu->base_address = attr->as_address ();
3b80fe9b
DE
5591 else
5592 {
5593 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5594 if (attr != nullptr)
95f982e5 5595 cu->base_address = attr->as_address ();
3b80fe9b
DE
5596 }
5597}
5598
36586728
TT
5599/* Helper function that returns the proper abbrev section for
5600 THIS_CU. */
5601
5602static struct dwarf2_section_info *
5603get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5604{
5605 struct dwarf2_section_info *abbrev;
c3699833 5606 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
5607
5608 if (this_cu->is_dwz)
a7308ce0 5609 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 5610 else
c3699833 5611 abbrev = &per_bfd->abbrev;
36586728
TT
5612
5613 return abbrev;
5614}
5615
f4dc4d17
DE
5616/* Fetch the abbreviation table offset from a comp or type unit header. */
5617
5618static sect_offset
976ca316 5619read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 5620 struct dwarf2_section_info *section,
9c541725 5621 sect_offset sect_off)
f4dc4d17 5622{
96b79293 5623 bfd *abfd = section->get_bfd_owner ();
d521ce57 5624 const gdb_byte *info_ptr;
ac298888 5625 unsigned int initial_length_size, offset_size;
43988095 5626 uint16_t version;
f4dc4d17 5627
976ca316 5628 section->read (per_objfile->objfile);
9c541725 5629 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5630 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5631 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5632 info_ptr += initial_length_size;
5633
5634 version = read_2_bytes (abfd, info_ptr);
5635 info_ptr += 2;
5636 if (version >= 5)
5637 {
5638 /* Skip unit type and address size. */
5639 info_ptr += 2;
5640 }
5641
24aa364d 5642 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5643}
5644
b83470bf
TT
5645/* A partial symtab that is used only for include files. */
5646struct dwarf2_include_psymtab : public partial_symtab
5647{
7b249e47
TT
5648 dwarf2_include_psymtab (const char *filename,
5649 psymtab_storage *partial_symtabs,
0072c873
SM
5650 objfile_per_bfd_storage *objfile_per_bfd)
5651 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
b83470bf
TT
5652 {
5653 }
5654
5655 void read_symtab (struct objfile *objfile) override
5656 {
194d088f
TV
5657 /* It's an include file, no symbols to read for it.
5658 Everything is in the includer symtab. */
5659
5660 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5661 expansion of the includer psymtab. We use the dependencies[0] field to
5662 model the includer. But if we go the regular route of calling
5663 expand_psymtab here, and having expand_psymtab call expand_dependencies
5664 to expand the includer, we'll only use expand_psymtab on the includer
5665 (making it a non-toplevel psymtab), while if we expand the includer via
5666 another path, we'll use read_symtab (making it a toplevel psymtab).
5667 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5668 psymtab, and trigger read_symtab on the includer here directly. */
5669 includer ()->read_symtab (objfile);
b83470bf
TT
5670 }
5671
5672 void expand_psymtab (struct objfile *objfile) override
5673 {
194d088f
TV
5674 /* This is not called by read_symtab, and should not be called by any
5675 expand_dependencies. */
5676 gdb_assert (false);
b83470bf
TT
5677 }
5678
5717c425 5679 bool readin_p (struct objfile *objfile) const override
b83470bf 5680 {
5717c425 5681 return includer ()->readin_p (objfile);
b83470bf
TT
5682 }
5683
5717c425 5684 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
5685 {
5686 return nullptr;
5687 }
5688
5689private:
194d088f
TV
5690 partial_symtab *includer () const
5691 {
5692 /* An include psymtab has exactly one dependency: the psymtab that
5693 includes it. */
5694 gdb_assert (this->number_of_dependencies == 1);
5695 return this->dependencies[0];
5696 }
b83470bf
TT
5697};
5698
aaa75496
JB
5699/* Allocate a new partial symtab for file named NAME and mark this new
5700 partial symtab as being an include of PST. */
5701
5702static void
84685904 5703dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
7b249e47
TT
5704 const char *name,
5705 dwarf2_psymtab *pst,
5706 psymtab_storage *partial_symtabs,
0072c873 5707 objfile_per_bfd_storage *objfile_per_bfd)
aaa75496 5708{
7b249e47 5709 dwarf2_include_psymtab *subpst
0072c873 5710 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
aaa75496 5711
fbd9ab74 5712 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 5713 subpst->dirname = pst->dirname;
fbd9ab74 5714
84685904 5715 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5716 subpst->dependencies[0] = pst;
5717 subpst->number_of_dependencies = 1;
aaa75496
JB
5718}
5719
5720/* Read the Line Number Program data and extract the list of files
5721 included by the source file represented by PST. Build an include
d85a05f0 5722 partial symtab for each of these included files. */
aaa75496
JB
5723
5724static void
5725dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5726 struct die_info *die,
891813be 5727 dwarf2_psymtab *pst)
aaa75496 5728{
fff8551c 5729 line_header_up lh;
d85a05f0 5730 struct attribute *attr;
aaa75496 5731
d85a05f0 5732 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
5733 if (attr != nullptr && attr->form_is_unsigned ())
5734 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
5735 if (lh == NULL)
5736 return; /* No linetable, so no includes. */
5737
79748972
TT
5738 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5739 that we pass in the raw text_low here; that is ok because we're
5740 only decoding the line table to make include partial symtabs, and
5741 so the addresses aren't really used. */
4ae976d1 5742 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5743 pst->raw_text_low (), 1);
aaa75496
JB
5744}
5745
348e048f 5746static hashval_t
52dc124a 5747hash_signatured_type (const void *item)
348e048f 5748{
9a3c8263
SM
5749 const struct signatured_type *sig_type
5750 = (const struct signatured_type *) item;
9a619af0 5751
348e048f 5752 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5753 return sig_type->signature;
348e048f
DE
5754}
5755
5756static int
52dc124a 5757eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5758{
9a3c8263
SM
5759 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5760 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5761
348e048f
DE
5762 return lhs->signature == rhs->signature;
5763}
5764
1fd400ff
TT
5765/* Allocate a hash table for signatured types. */
5766
b0b6a987 5767static htab_up
298e9637 5768allocate_signatured_type_table ()
1fd400ff 5769{
b0b6a987
TT
5770 return htab_up (htab_create_alloc (41,
5771 hash_signatured_type,
5772 eq_signatured_type,
5773 NULL, xcalloc, xfree));
1fd400ff
TT
5774}
5775
78d4d2c5 5776/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5777 and fill them into TYPES_HTAB. It will process only type units,
5778 therefore DW_UT_type. */
c88ee1f0 5779
78d4d2c5 5780static void
976ca316 5781create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 5782 struct dwo_file *dwo_file,
b0b6a987 5783 dwarf2_section_info *section, htab_up &types_htab,
43988095 5784 rcuh_kind section_kind)
348e048f 5785{
976ca316 5786 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 5787 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
5788 bfd *abfd;
5789 const gdb_byte *info_ptr, *end_ptr;
348e048f 5790
b8efb248 5791 abbrev_section = &dwo_file->sections.abbrev;
4bdcc0c1 5792
4800761a 5793 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
5794 section->get_name (),
5795 abbrev_section->get_file_name ());
09406207 5796
96b79293 5797 section->read (objfile);
78d4d2c5 5798 info_ptr = section->buffer;
348e048f 5799
78d4d2c5
JK
5800 if (info_ptr == NULL)
5801 return;
348e048f 5802
78d4d2c5
JK
5803 /* We can't set abfd until now because the section may be empty or
5804 not present, in which case the bfd is unknown. */
96b79293 5805 abfd = section->get_bfd_owner ();
348e048f 5806
c0ab21c2
TT
5807 /* We don't use cutu_reader here because we don't need to read
5808 any dies: the signature is in the header. */
3019eac3 5809
78d4d2c5
JK
5810 end_ptr = info_ptr + section->size;
5811 while (info_ptr < end_ptr)
5812 {
46c6bcf6 5813 signatured_type_up sig_type;
78d4d2c5
JK
5814 struct dwo_unit *dwo_tu;
5815 void **slot;
5816 const gdb_byte *ptr = info_ptr;
5817 struct comp_unit_head header;
5818 unsigned int length;
8b70b953 5819
9c541725 5820 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 5821
a49dd8dd
JK
5822 /* Initialize it due to a false compiler warning. */
5823 header.signature = -1;
9c541725 5824 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 5825
78d4d2c5
JK
5826 /* We need to read the type's signature in order to build the hash
5827 table, but we don't need anything else just yet. */
348e048f 5828
976ca316 5829 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 5830 abbrev_section, ptr, section_kind);
348e048f 5831
4057dfde 5832 length = header.get_length ();
6caca83c 5833
78d4d2c5
JK
5834 /* Skip dummy type units. */
5835 if (ptr >= info_ptr + length
43988095 5836 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
5837 || (header.unit_type != DW_UT_type
5838 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
5839 {
5840 info_ptr += length;
5841 continue;
5842 }
dee91e82 5843
78d4d2c5 5844 if (types_htab == NULL)
b8efb248 5845 types_htab = allocate_dwo_unit_table ();
8b70b953 5846
b8efb248
TT
5847 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5848 dwo_tu->dwo_file = dwo_file;
5849 dwo_tu->signature = header.signature;
5850 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5851 dwo_tu->section = section;
5852 dwo_tu->sect_off = sect_off;
5853 dwo_tu->length = length;
78d4d2c5 5854
b8efb248 5855 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
78d4d2c5
JK
5856 gdb_assert (slot != NULL);
5857 if (*slot != NULL)
b8efb248
TT
5858 complaint (_("debug type entry at offset %s is duplicate to"
5859 " the entry at offset %s, signature %s"),
5860 sect_offset_str (sect_off),
5861 sect_offset_str (dwo_tu->sect_off),
5862 hex_string (header.signature));
5863 *slot = dwo_tu;
3019eac3 5864
6f738b01
SM
5865 dwarf_read_debug_printf_v (" offset %s, signature %s",
5866 sect_offset_str (sect_off),
5867 hex_string (header.signature));
3019eac3 5868
78d4d2c5
JK
5869 info_ptr += length;
5870 }
5871}
3019eac3 5872
78d4d2c5
JK
5873/* Create the hash table of all entries in the .debug_types
5874 (or .debug_types.dwo) section(s).
b8efb248 5875 DWO_FILE is a pointer to the DWO file object.
b3c8eb43 5876
78d4d2c5 5877 The result is a pointer to the hash table or NULL if there are no types.
348e048f 5878
78d4d2c5 5879 Note: This function processes DWO files only, not DWP files. */
348e048f 5880
78d4d2c5 5881static void
976ca316 5882create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 5883 struct dwo_file *dwo_file,
fd5866f6 5884 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 5885 htab_up &types_htab)
78d4d2c5 5886{
fd5866f6 5887 for (dwarf2_section_info &section : type_sections)
976ca316
SM
5888 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5889 rcuh_kind::TYPE);
3019eac3
DE
5890}
5891
5989a64e 5892/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
5893 If SLOT is non-NULL, it is the entry to use in the hash table.
5894 Otherwise we find one. */
5895
5896static struct signatured_type *
976ca316 5897add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 5898{
91eea9cc
TT
5899 if (per_objfile->per_bfd->all_comp_units.size ()
5900 == per_objfile->per_bfd->all_comp_units.capacity ())
976ca316 5901 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 5902
46c6bcf6 5903 signatured_type_up sig_type_holder
9ea36493 5904 = per_objfile->per_bfd->allocate_signatured_type (sig);
0d305d5c 5905 signatured_type *sig_type = sig_type_holder.get ();
b2bdb8cf 5906
976ca316 5907 per_objfile->resize_symtabs ();
af758d11 5908
473ab964
TT
5909 per_objfile->per_bfd->all_comp_units.emplace_back
5910 (sig_type_holder.release ());
976ca316 5911 if (per_objfile->per_bfd->using_index)
6aa5f3a6 5912 {
0d305d5c 5913 sig_type->v.quick =
976ca316 5914 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
5915 struct dwarf2_per_cu_quick_data);
5916 }
5917
5918 if (slot == NULL)
5919 {
976ca316 5920 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
5921 sig_type, INSERT);
5922 }
5923 gdb_assert (*slot == NULL);
5924 *slot = sig_type;
5925 /* The rest of sig_type must be filled in by the caller. */
5926 return sig_type;
5927}
5928
a2ce51a0
DE
5929/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5930 Fill in SIG_ENTRY with DWO_ENTRY. */
5931
5932static void
976ca316 5933fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
5934 struct signatured_type *sig_entry,
5935 struct dwo_unit *dwo_entry)
5936{
976ca316 5937 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 5938
7ee85ab1 5939 /* Make sure we're not clobbering something we don't expect to. */
0d305d5c
TT
5940 gdb_assert (! sig_entry->queued);
5941 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
1859c670 5942 if (per_bfd->using_index)
6aa5f3a6 5943 {
0d305d5c
TT
5944 gdb_assert (sig_entry->v.quick != NULL);
5945 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6aa5f3a6
DE
5946 }
5947 else
0d305d5c 5948 gdb_assert (sig_entry->v.psymtab == NULL);
a2ce51a0 5949 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5950 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5951 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5952 gdb_assert (sig_entry->dwo_unit == NULL);
5953
0d305d5c
TT
5954 sig_entry->section = dwo_entry->section;
5955 sig_entry->sect_off = dwo_entry->sect_off;
5956 sig_entry->length = dwo_entry->length;
5957 sig_entry->reading_dwo_directly = 1;
5958 sig_entry->per_bfd = per_bfd;
a2ce51a0
DE
5959 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5960 sig_entry->dwo_unit = dwo_entry;
5961}
5962
5963/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5964 If we haven't read the TU yet, create the signatured_type data structure
5965 for a TU to be read in directly from a DWO file, bypassing the stub.
5966 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5967 using .gdb_index, then when reading a CU we want to stay in the DWO file
5968 containing that CU. Otherwise we could end up reading several other DWO
5969 files (due to comdat folding) to process the transitive closure of all the
5970 mentioned TUs, and that can be slow. The current DWO file will have every
5971 type signature that it needs.
a2ce51a0
DE
5972 We only do this for .gdb_index because in the psymtab case we already have
5973 to read all the DWOs to build the type unit groups. */
5974
5975static struct signatured_type *
5976lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5977{
976ca316 5978 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
5979 struct dwo_file *dwo_file;
5980 struct dwo_unit find_dwo_entry, *dwo_entry;
6aa5f3a6 5981 void **slot;
a2ce51a0 5982
976ca316 5983 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 5984
6aa5f3a6
DE
5985 /* If TU skeletons have been removed then we may not have read in any
5986 TUs yet. */
976ca316
SM
5987 if (per_objfile->per_bfd->signatured_types == NULL)
5988 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
5989
5990 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5991 Use the global signatured_types array to do our own comdat-folding
5992 of types. If this is the first time we're reading this TU, and
5993 the TU has an entry in .gdb_index, replace the recorded data from
5994 .gdb_index with this TU. */
a2ce51a0 5995
9ea36493 5996 signatured_type find_sig_entry (sig);
976ca316 5997 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 5998 &find_sig_entry, INSERT);
9ea36493 5999 signatured_type *sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6000
6001 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6002 read. Don't reassign the global entry to point to this DWO if that's
6003 the case. Also note that if the TU is already being read, it may not
6004 have come from a DWO, the program may be a mix of Fission-compiled
6005 code and non-Fission-compiled code. */
6006
6007 /* Have we already tried to read this TU?
6008 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6009 needn't exist in the global table yet). */
0d305d5c 6010 if (sig_entry != NULL && sig_entry->tu_read)
a2ce51a0
DE
6011 return sig_entry;
6012
6aa5f3a6
DE
6013 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6014 dwo_unit of the TU itself. */
6015 dwo_file = cu->dwo_unit->dwo_file;
6016
a2ce51a0
DE
6017 /* Ok, this is the first time we're reading this TU. */
6018 if (dwo_file->tus == NULL)
6019 return NULL;
6020 find_dwo_entry.signature = sig;
b0b6a987
TT
6021 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6022 &find_dwo_entry);
a2ce51a0
DE
6023 if (dwo_entry == NULL)
6024 return NULL;
6025
6aa5f3a6
DE
6026 /* If the global table doesn't have an entry for this TU, add one. */
6027 if (sig_entry == NULL)
976ca316 6028 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6029
976ca316 6030 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
0d305d5c 6031 sig_entry->tu_read = 1;
a2ce51a0
DE
6032 return sig_entry;
6033}
6034
a2ce51a0
DE
6035/* Subroutine of lookup_signatured_type.
6036 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6037 then try the DWP file. If the TU stub (skeleton) has been removed then
6038 it won't be in .gdb_index. */
a2ce51a0
DE
6039
6040static struct signatured_type *
6041lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6042{
976ca316
SM
6043 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6044 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0 6045 struct dwo_unit *dwo_entry;
6aa5f3a6 6046 void **slot;
a2ce51a0 6047
976ca316 6048 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6049 gdb_assert (dwp_file != NULL);
6050
6aa5f3a6
DE
6051 /* If TU skeletons have been removed then we may not have read in any
6052 TUs yet. */
976ca316
SM
6053 if (per_objfile->per_bfd->signatured_types == NULL)
6054 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6055
9ea36493 6056 signatured_type find_sig_entry (sig);
976ca316 6057 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6058 &find_sig_entry, INSERT);
9ea36493 6059 signatured_type *sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6060
6061 /* Have we already tried to read this TU?
6062 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6063 needn't exist in the global table yet). */
6064 if (sig_entry != NULL)
6065 return sig_entry;
6066
a2ce51a0
DE
6067 if (dwp_file->tus == NULL)
6068 return NULL;
976ca316
SM
6069 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6070 1 /* is_debug_types */);
a2ce51a0
DE
6071 if (dwo_entry == NULL)
6072 return NULL;
6073
976ca316
SM
6074 sig_entry = add_type_unit (per_objfile, sig, slot);
6075 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6076
a2ce51a0
DE
6077 return sig_entry;
6078}
6079
380bca97 6080/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6081 Returns NULL if signature SIG is not present in the table.
6082 It is up to the caller to complain about this. */
348e048f
DE
6083
6084static struct signatured_type *
a2ce51a0 6085lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6086{
976ca316 6087 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6088
976ca316 6089 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6090 {
6091 /* We're in a DWO/DWP file, and we're using .gdb_index.
6092 These cases require special processing. */
976ca316 6093 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6094 return lookup_dwo_signatured_type (cu, sig);
6095 else
6096 return lookup_dwp_signatured_type (cu, sig);
6097 }
6098 else
6099 {
976ca316 6100 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0 6101 return NULL;
9ea36493
SM
6102 signatured_type find_entry (sig);
6103 return ((struct signatured_type *)
6104 htab_find (per_objfile->per_bfd->signatured_types.get (),
6105 &find_entry));
a2ce51a0 6106 }
348e048f 6107}
18a8505e 6108
42e7ad6c 6109/* Low level DIE reading support. */
348e048f 6110
d85a05f0
DJ
6111/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6112
6113static void
6114init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6115 struct dwarf2_cu *cu,
3019eac3 6116 struct dwarf2_section_info *section,
685af9cd
TT
6117 struct dwo_file *dwo_file,
6118 struct abbrev_table *abbrev_table)
d85a05f0 6119{
fceca515 6120 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6121 reader->abfd = section->get_bfd_owner ();
d85a05f0 6122 reader->cu = cu;
3019eac3 6123 reader->dwo_file = dwo_file;
dee91e82
DE
6124 reader->die_section = section;
6125 reader->buffer = section->buffer;
f664829e 6126 reader->buffer_end = section->buffer + section->size;
685af9cd 6127 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6128}
6129
c0ab21c2 6130/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6131 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6132 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6133 already.
6134
6135 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6136 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6137 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6138 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6139 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6140 STUB_COMP_DIR may be non-NULL.
3e225074 6141 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6142 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6143 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6144 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6145 kept around for at least as long as *RESULT_READER.
6146
b0c7bfa9
DE
6147 The result is non-zero if a valid (non-dummy) DIE was found. */
6148
6149static int
4ab09049 6150read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6151 struct dwo_unit *dwo_unit,
b0c7bfa9 6152 struct die_info *stub_comp_unit_die,
a2ce51a0 6153 const char *stub_comp_dir,
b0c7bfa9 6154 struct die_reader_specs *result_reader,
d521ce57 6155 const gdb_byte **result_info_ptr,
b0c7bfa9 6156 struct die_info **result_comp_unit_die,
685af9cd 6157 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6158{
976ca316 6159 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6160 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6161 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6162 bfd *abfd;
d521ce57 6163 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6164 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6165 int i,num_extra_attrs;
6166 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6167 struct die_info *comp_unit_die;
6168
b0aeadb3
DE
6169 /* At most one of these may be provided. */
6170 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6171
b0c7bfa9
DE
6172 /* These attributes aren't processed until later:
6173 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6174 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6175 referenced later. However, these attributes are found in the stub
6176 which we won't have later. In order to not impose this complication
6177 on the rest of the code, we read them here and copy them to the
6178 DWO CU/TU die. */
b0c7bfa9
DE
6179
6180 stmt_list = NULL;
6181 low_pc = NULL;
6182 high_pc = NULL;
6183 ranges = NULL;
6184 comp_dir = NULL;
6185
6186 if (stub_comp_unit_die != NULL)
6187 {
6188 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6189 DWO file. */
4ab09049 6190 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6191 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6192 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6193 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6194 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6195 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6196
a39fdb41 6197 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6198
2b0c7f41
SM
6199 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6200 We need the value before we can process DW_AT_ranges values from the
6201 DWO. */
6202 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6203
6204 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6205 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6206 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6207 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6208 section. */
6209 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6210 }
a2ce51a0
DE
6211 else if (stub_comp_dir != NULL)
6212 {
6213 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6214 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6215 comp_dir->name = DW_AT_comp_dir;
6216 comp_dir->form = DW_FORM_string;
c6481205 6217 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6218 }
b0c7bfa9
DE
6219
6220 /* Set up for reading the DWO CU/TU. */
6221 cu->dwo_unit = dwo_unit;
685af9cd 6222 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6223 section->read (objfile);
6224 abfd = section->get_bfd_owner ();
9c541725
PA
6225 begin_info_ptr = info_ptr = (section->buffer
6226 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6227 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6228
4ab09049 6229 if (per_cu->is_debug_types)
b0c7bfa9 6230 {
4ab09049 6231 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6232
976ca316
SM
6233 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6234 section, dwo_abbrev_section,
43988095 6235 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6236 /* This is not an assert because it can be caused by bad debug info. */
43988095 6237 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6238 {
6239 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6240 " TU at offset %s [in module %s]"),
a2ce51a0 6241 hex_string (sig_type->signature),
43988095 6242 hex_string (cu->header.signature),
9d8780f0 6243 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6244 bfd_get_filename (abfd));
6245 }
9c541725 6246 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6247 /* For DWOs coming from DWP files, we don't know the CU length
6248 nor the type's offset in the TU until now. */
4057dfde 6249 dwo_unit->length = cu->header.get_length ();
9c541725 6250 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6251
6252 /* Establish the type offset that can be used to lookup the type.
6253 For DWO files, we don't know it until now. */
9c541725
PA
6254 sig_type->type_offset_in_section
6255 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6256 }
6257 else
6258 {
976ca316
SM
6259 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6260 section, dwo_abbrev_section,
43988095 6261 info_ptr, rcuh_kind::COMPILE);
9c541725 6262 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6263 /* For DWOs coming from DWP files, we don't know the CU length
6264 until now. */
4057dfde 6265 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6266 }
6267
606decb2 6268 dwo_abbrev_section->read (objfile);
685af9cd 6269 *result_dwo_abbrev_table
606decb2 6270 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6271 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6272 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6273
6274 /* Read in the die, but leave space to copy over the attributes
6275 from the stub. This has the benefit of simplifying the rest of
6276 the code - all the work to maintain the illusion of a single
6277 DW_TAG_{compile,type}_unit DIE is done here. */
6278 num_extra_attrs = ((stmt_list != NULL)
6279 + (low_pc != NULL)
6280 + (high_pc != NULL)
6281 + (ranges != NULL)
6282 + (comp_dir != NULL));
6283 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6284 num_extra_attrs);
b0c7bfa9
DE
6285
6286 /* Copy over the attributes from the stub to the DIE we just read in. */
6287 comp_unit_die = *result_comp_unit_die;
6288 i = comp_unit_die->num_attrs;
6289 if (stmt_list != NULL)
6290 comp_unit_die->attrs[i++] = *stmt_list;
6291 if (low_pc != NULL)
6292 comp_unit_die->attrs[i++] = *low_pc;
6293 if (high_pc != NULL)
6294 comp_unit_die->attrs[i++] = *high_pc;
6295 if (ranges != NULL)
6296 comp_unit_die->attrs[i++] = *ranges;
6297 if (comp_dir != NULL)
6298 comp_unit_die->attrs[i++] = *comp_dir;
6299 comp_unit_die->num_attrs += num_extra_attrs;
6300
b4f54984 6301 if (dwarf_die_debug)
bf6af496
DE
6302 {
6303 fprintf_unfiltered (gdb_stdlog,
6304 "Read die from %s@0x%x of %s:\n",
96b79293 6305 section->get_name (),
bf6af496
DE
6306 (unsigned) (begin_info_ptr - section->buffer),
6307 bfd_get_filename (abfd));
b4f54984 6308 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6309 }
6310
b0c7bfa9
DE
6311 /* Skip dummy compilation units. */
6312 if (info_ptr >= begin_info_ptr + dwo_unit->length
6313 || peek_abbrev_code (abfd, info_ptr) == 0)
6314 return 0;
6315
6316 *result_info_ptr = info_ptr;
6317 return 1;
6318}
6319
a084a2a6
AT
6320/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6321 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6322 signature is part of the header. */
6323static gdb::optional<ULONGEST>
6324lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6325{
6326 if (cu->header.version >= 5)
6327 return cu->header.signature;
6328 struct attribute *attr;
6329 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 6330 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 6331 return gdb::optional<ULONGEST> ();
529908cb 6332 return attr->as_unsigned ();
a084a2a6
AT
6333}
6334
c0ab21c2 6335/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6336 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6337 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6338
6339static struct dwo_unit *
4ab09049 6340lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6341{
4ab09049 6342 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6343 struct dwo_unit *dwo_unit;
c0ab21c2 6344 const char *comp_dir;
b0c7bfa9 6345
a2ce51a0
DE
6346 gdb_assert (cu != NULL);
6347
b0c7bfa9 6348 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6349 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6350 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6351
4ab09049
SM
6352 if (per_cu->is_debug_types)
6353 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6354 else
6355 {
a084a2a6 6356 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6357
a084a2a6 6358 if (!signature.has_value ())
b0c7bfa9
DE
6359 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6360 " [in module %s]"),
4ab09049
SM
6361 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6362
6363 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6364 }
6365
b0c7bfa9
DE
6366 return dwo_unit;
6367}
6368
c0ab21c2 6369/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6370 See it for a description of the parameters.
fcd3b13d 6371 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6372
c0ab21c2 6373void
9e021579
SM
6374cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6375 dwarf2_per_objfile *per_objfile,
2e671100 6376 dwarf2_cu *existing_cu)
a2ce51a0 6377{
a2ce51a0 6378 struct signatured_type *sig_type;
a2ce51a0
DE
6379
6380 /* Verify we can do the following downcast, and that we have the
6381 data we need. */
6382 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6383 sig_type = (struct signatured_type *) this_cu;
6384 gdb_assert (sig_type->dwo_unit != NULL);
6385
2e671100
SM
6386 dwarf2_cu *cu;
6387
6388 if (existing_cu != nullptr)
6aa5f3a6 6389 {
2e671100
SM
6390 cu = existing_cu;
6391 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6392 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6393 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6394 }
6395 else
6396 {
7188ed02 6397 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6398 in per_objfile yet. */
7188ed02 6399 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6400 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6401 cu = m_new_cu.get ();
6aa5f3a6
DE
6402 }
6403
6404 /* A future optimization, if needed, would be to use an existing
6405 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6406 could share abbrev tables. */
a2ce51a0 6407
2e671100 6408 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6409 NULL /* stub_comp_unit_die */,
6410 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6411 this, &info_ptr,
3e225074 6412 &comp_unit_die,
c0ab21c2 6413 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6414 {
6415 /* Dummy die. */
c0ab21c2 6416 dummy_p = true;
a2ce51a0 6417 }
a2ce51a0
DE
6418}
6419
fd820528 6420/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6421 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6422
f4dc4d17
DE
6423 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6424 Otherwise the table specified in the comp unit header is read in and used.
6425 This is an optimization for when we already have the abbrev table.
6426
2e671100
SM
6427 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6428 allocated. */
aaa75496 6429
ab432490 6430cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6431 dwarf2_per_objfile *per_objfile,
c0ab21c2 6432 struct abbrev_table *abbrev_table,
2e671100 6433 dwarf2_cu *existing_cu,
c0ab21c2
TT
6434 bool skip_partial)
6435 : die_reader_specs {},
6751ebae 6436 m_this_cu (this_cu)
c906108c 6437{
976ca316 6438 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6439 struct dwarf2_section_info *section = this_cu->section;
96b79293 6440 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 6441 const gdb_byte *begin_info_ptr;
dee91e82 6442 struct signatured_type *sig_type = NULL;
4bdcc0c1 6443 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6444 /* Non-zero if CU currently points to a DWO file and we need to
6445 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6446 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6447 int rereading_dwo_cu = 0;
c906108c 6448
b4f54984 6449 if (dwarf_die_debug)
9d8780f0 6450 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6451 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6452 sect_offset_str (this_cu->sect_off));
09406207 6453
a2ce51a0
DE
6454 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6455 file (instead of going through the stub), short-circuit all of this. */
6456 if (this_cu->reading_dwo_directly)
6457 {
6458 /* Narrow down the scope of possibilities to have to understand. */
6459 gdb_assert (this_cu->is_debug_types);
6460 gdb_assert (abbrev_table == NULL);
976ca316 6461 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
6462 return;
6463 }
6464
dee91e82 6465 /* This is cheap if the section is already read in. */
96b79293 6466 section->read (objfile);
dee91e82 6467
9c541725 6468 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6469
6470 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 6471
2e671100
SM
6472 dwarf2_cu *cu;
6473
6474 if (existing_cu != nullptr)
dee91e82 6475 {
2e671100 6476 cu = existing_cu;
42e7ad6c
DE
6477 /* If this CU is from a DWO file we need to start over, we need to
6478 refetch the attributes from the skeleton CU.
6479 This could be optimized by retrieving those attributes from when we
6480 were here the first time: the previous comp_unit_die was stored in
6481 comp_unit_obstack. But there's no data yet that we need this
6482 optimization. */
6483 if (cu->dwo_unit != NULL)
6484 rereading_dwo_cu = 1;
dee91e82
DE
6485 }
6486 else
6487 {
7188ed02 6488 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6489 in per_objfile yet. */
976ca316
SM
6490 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6491 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 6492 cu = m_new_cu.get ();
42e7ad6c 6493 }
dee91e82 6494
b0c7bfa9 6495 /* Get the header. */
9c541725 6496 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6497 {
6498 /* We already have the header, there's no need to read it in again. */
9c541725 6499 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6500 }
6501 else
6502 {
3019eac3 6503 if (this_cu->is_debug_types)
dee91e82 6504 {
976ca316
SM
6505 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6506 section, abbrev_section,
6507 info_ptr, rcuh_kind::TYPE);
dee91e82 6508
42e7ad6c
DE
6509 /* Since per_cu is the first member of struct signatured_type,
6510 we can go from a pointer to one to a pointer to the other. */
6511 sig_type = (struct signatured_type *) this_cu;
43988095 6512 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6513 gdb_assert (sig_type->type_offset_in_tu
6514 == cu->header.type_cu_offset_in_tu);
6515 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6516
42e7ad6c
DE
6517 /* LENGTH has not been set yet for type units if we're
6518 using .gdb_index. */
4057dfde 6519 this_cu->length = cu->header.get_length ();
3019eac3
DE
6520
6521 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6522 sig_type->type_offset_in_section =
6523 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6524
6525 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6526 }
6527 else
6528 {
976ca316
SM
6529 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6530 section, abbrev_section,
43988095
JK
6531 info_ptr,
6532 rcuh_kind::COMPILE);
dee91e82 6533
9c541725 6534 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
6535 if (this_cu->length == 0)
6536 this_cu->length = cu->header.get_length ();
6537 else
6538 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6539 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6540 }
6541 }
10b3939b 6542
6caca83c 6543 /* Skip dummy compilation units. */
dee91e82 6544 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6545 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6546 {
6547 dummy_p = true;
6548 return;
6549 }
6caca83c 6550
433df2d4
DE
6551 /* If we don't have them yet, read the abbrevs for this compilation unit.
6552 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6553 done. */
f4dc4d17 6554 if (abbrev_table != NULL)
685af9cd
TT
6555 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6556 else
f4dc4d17 6557 {
606decb2 6558 abbrev_section->read (objfile);
c0ab21c2 6559 m_abbrev_table_holder
606decb2 6560 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 6561 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6562 }
af703f96 6563
dee91e82 6564 /* Read the top level CU/TU die. */
c0ab21c2 6565 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6566 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6567
58f0c718 6568 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6569 {
6570 dummy_p = true;
6571 return;
6572 }
58f0c718 6573
b0c7bfa9 6574 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6575 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6576 table from the DWO file and pass the ownership over to us. It will be
6577 referenced from READER, so we must make sure to free it after we're done
6578 with READER.
6579
b0c7bfa9
DE
6580 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6581 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6582 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6583 if (dwo_name != nullptr)
3019eac3 6584 {
3019eac3 6585 struct dwo_unit *dwo_unit;
b0c7bfa9 6586 struct die_info *dwo_comp_unit_die;
3019eac3 6587
3e225074 6588 if (comp_unit_die->has_children)
6a506a2d 6589 {
b98664d3 6590 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6591 " has children (offset %s) [in module %s]"),
6592 sect_offset_str (this_cu->sect_off),
6593 bfd_get_filename (abfd));
6a506a2d 6594 }
4ab09049 6595 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 6596 if (dwo_unit != NULL)
3019eac3 6597 {
4ab09049 6598 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 6599 comp_unit_die, NULL,
c0ab21c2 6600 this, &info_ptr,
3e225074 6601 &dwo_comp_unit_die,
c0ab21c2 6602 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6603 {
6604 /* Dummy die. */
c0ab21c2 6605 dummy_p = true;
6a506a2d
DE
6606 return;
6607 }
6608 comp_unit_die = dwo_comp_unit_die;
6609 }
6610 else
6611 {
6612 /* Yikes, we couldn't find the rest of the DIE, we only have
6613 the stub. A complaint has already been logged. There's
6614 not much more we can do except pass on the stub DIE to
6615 die_reader_func. We don't want to throw an error on bad
6616 debug info. */
3019eac3
DE
6617 }
6618 }
c0ab21c2 6619}
3019eac3 6620
6751ebae
TT
6621void
6622cutu_reader::keep ()
c0ab21c2 6623{
b0c7bfa9 6624 /* Done, clean up. */
6751ebae
TT
6625 gdb_assert (!dummy_p);
6626 if (m_new_cu != NULL)
348e048f 6627 {
7188ed02 6628 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 6629 now. */
7188ed02
SM
6630 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6631 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 6632 }
dee91e82
DE
6633}
6634
18a8505e
AT
6635/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6636 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6637 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6638
6639 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6640 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6641
6642 We fill in THIS_CU->length.
6643
dee91e82 6644 THIS_CU->cu is always freed when done.
3019eac3 6645 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6646 to care whether it refers to the "main" CU or the DWO CU.
6647
6648 When parent_cu is passed, it is used to provide a default value for
6649 str_offsets_base and addr_base from the parent. */
dee91e82 6650
ab432490 6651cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6652 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
6653 struct dwarf2_cu *parent_cu,
6654 struct dwo_file *dwo_file)
6655 : die_reader_specs {},
6656 m_this_cu (this_cu)
dee91e82 6657{
976ca316 6658 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6659 struct dwarf2_section_info *section = this_cu->section;
96b79293 6660 bfd *abfd = section->get_bfd_owner ();
33e80786 6661 struct dwarf2_section_info *abbrev_section;
d521ce57 6662 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6663
b4f54984 6664 if (dwarf_die_debug)
9d8780f0 6665 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6666 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6667 sect_offset_str (this_cu->sect_off));
09406207 6668
976ca316 6669 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 6670
33e80786
DE
6671 abbrev_section = (dwo_file != NULL
6672 ? &dwo_file->sections.abbrev
6673 : get_abbrev_section_for_cu (this_cu));
6674
dee91e82 6675 /* This is cheap if the section is already read in. */
96b79293 6676 section->read (objfile);
dee91e82 6677
976ca316 6678 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 6679
9c541725 6680 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
6681 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6682 section, abbrev_section, info_ptr,
43988095
JK
6683 (this_cu->is_debug_types
6684 ? rcuh_kind::TYPE
6685 : rcuh_kind::COMPILE));
dee91e82 6686
18a8505e
AT
6687 if (parent_cu != nullptr)
6688 {
c0ab21c2
TT
6689 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6690 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 6691 }
4057dfde 6692 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
6693
6694 /* Skip dummy compilation units. */
6695 if (info_ptr >= begin_info_ptr + this_cu->length
6696 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6697 {
6698 dummy_p = true;
6699 return;
6700 }
72bf9492 6701
606decb2 6702 abbrev_section->read (objfile);
c0ab21c2 6703 m_abbrev_table_holder
606decb2 6704 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 6705
c0ab21c2
TT
6706 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6707 m_abbrev_table_holder.get ());
3e225074 6708 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
6709}
6710
0018ea6f
DE
6711\f
6712/* Type Unit Groups.
dee91e82 6713
0018ea6f
DE
6714 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6715 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6716 so that all types coming from the same compilation (.o file) are grouped
6717 together. A future step could be to put the types in the same symtab as
6718 the CU the types ultimately came from. */
ff013f42 6719
f4dc4d17
DE
6720static hashval_t
6721hash_type_unit_group (const void *item)
6722{
9a3c8263
SM
6723 const struct type_unit_group *tu_group
6724 = (const struct type_unit_group *) item;
f4dc4d17 6725
094b34ac 6726 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6727}
348e048f
DE
6728
6729static int
f4dc4d17 6730eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6731{
9a3c8263
SM
6732 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6733 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6734
094b34ac 6735 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6736}
348e048f 6737
f4dc4d17
DE
6738/* Allocate a hash table for type unit groups. */
6739
eaa5fa8b 6740static htab_up
298e9637 6741allocate_type_unit_groups_table ()
f4dc4d17 6742{
eaa5fa8b
TT
6743 return htab_up (htab_create_alloc (3,
6744 hash_type_unit_group,
6745 eq_type_unit_group,
ef5f598c 6746 htab_delete_entry<type_unit_group>,
0d305d5c 6747 xcalloc, xfree));
f4dc4d17 6748}
dee91e82 6749
f4dc4d17
DE
6750/* Type units that don't have DW_AT_stmt_list are grouped into their own
6751 partial symtabs. We combine several TUs per psymtab to not let the size
6752 of any one psymtab grow too big. */
6753#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6754#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6755
094b34ac 6756/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6757 Create the type_unit_group object used to hold one or more TUs. */
6758
0d305d5c 6759static std::unique_ptr<type_unit_group>
094b34ac 6760create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 6761{
976ca316
SM
6762 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6763 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 6764
0d305d5c
TT
6765 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6766 tu_group->per_bfd = per_bfd;
f4dc4d17 6767
1859c670 6768 if (per_bfd->using_index)
094b34ac 6769 {
0d305d5c
TT
6770 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6771 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6772 }
6773 else
6774 {
9c541725 6775 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 6776 dwarf2_psymtab *pst;
528e1572 6777 std::string name;
094b34ac
DE
6778
6779 /* Give the symtab a useful name for debug purposes. */
6780 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
6781 name = string_printf ("<type_units_%d>",
6782 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 6783 else
528e1572 6784 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 6785
0d305d5c
TT
6786 pst = create_partial_symtab (tu_group.get (), per_objfile,
6787 name.c_str ());
6d94535f 6788 pst->anonymous = true;
094b34ac 6789 }
f4dc4d17 6790
094b34ac 6791 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6792 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6793
6794 return tu_group;
6795}
6796
094b34ac
DE
6797/* Look up the type_unit_group for type unit CU, and create it if necessary.
6798 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6799
6800static struct type_unit_group *
ff39bb5e 6801get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 6802{
976ca316
SM
6803 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6804 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
6805 struct type_unit_group *tu_group;
6806 void **slot;
6807 unsigned int line_offset;
6808 struct type_unit_group type_unit_group_for_lookup;
6809
976ca316
SM
6810 if (per_objfile->per_bfd->type_unit_groups == NULL)
6811 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
6812
6813 /* Do we need to create a new group, or can we use an existing one? */
6814
529908cb 6815 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 6816 {
529908cb 6817 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
6818 ++tu_stats->nr_symtab_sharers;
6819 }
6820 else
6821 {
6822 /* Ugh, no stmt_list. Rare, but we have to handle it.
6823 We can do various things here like create one group per TU or
6824 spread them over multiple groups to split up the expansion work.
6825 To avoid worst case scenarios (too many groups or too large groups)
6826 we, umm, group them in bunches. */
6827 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6828 | (tu_stats->nr_stmt_less_type_units
6829 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6830 ++tu_stats->nr_stmt_less_type_units;
6831 }
6832
094b34ac 6833 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6834 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 6835 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17 6836 &type_unit_group_for_lookup, INSERT);
0d305d5c 6837 if (*slot == nullptr)
f4dc4d17 6838 {
9c541725 6839 sect_offset line_offset_struct = (sect_offset) line_offset;
0d305d5c
TT
6840 std::unique_ptr<type_unit_group> grp
6841 = create_type_unit_group (cu, line_offset_struct);
6842 *slot = grp.release ();
f4dc4d17
DE
6843 ++tu_stats->nr_symtabs;
6844 }
6845
0d305d5c
TT
6846 tu_group = (struct type_unit_group *) *slot;
6847 gdb_assert (tu_group != nullptr);
f4dc4d17
DE
6848 return tu_group;
6849}
0018ea6f
DE
6850\f
6851/* Partial symbol tables. */
6852
6853/* Create a psymtab named NAME and assign it to PER_CU.
6854
6855 The caller must fill in the following details:
6856 dirname, textlow, texthigh. */
6857
891813be 6858static dwarf2_psymtab *
7aa104c4
SM
6859create_partial_symtab (dwarf2_per_cu_data *per_cu,
6860 dwarf2_per_objfile *per_objfile,
6861 const char *name)
0018ea6f 6862{
0072c873
SM
6863 dwarf2_psymtab *pst
6864 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6865 per_objfile->objfile->per_bfd, per_cu);
0018ea6f 6866
6d94535f 6867 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
6868
6869 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
6870 per_cu->v.psymtab = pst;
6871
6872 return pst;
6873}
6874
c0ab21c2 6875/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
6876
6877static void
6878process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6879 const gdb_byte *info_ptr,
0018ea6f 6880 struct die_info *comp_unit_die,
c0ab21c2 6881 enum language pretend_language)
0018ea6f
DE
6882{
6883 struct dwarf2_cu *cu = reader->cu;
7aa104c4 6884 dwarf2_per_objfile *per_objfile = cu->per_objfile;
84685904 6885 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7aa104c4 6886 struct objfile *objfile = per_objfile->objfile;
08feed99 6887 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 6888 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6889 CORE_ADDR baseaddr;
6890 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 6891 dwarf2_psymtab *pst;
3a2b436a 6892 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6893 const char *filename;
0018ea6f 6894
0018ea6f
DE
6895 gdb_assert (! per_cu->is_debug_types);
6896
c0ab21c2 6897 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 6898
0018ea6f 6899 /* Allocate a new partial symbol table structure. */
2e927613
TV
6900 gdb::unique_xmalloc_ptr<char> debug_filename;
6901 static const char artificial[] = "<artificial>";
7d45c7c3
KB
6902 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6903 if (filename == NULL)
0018ea6f 6904 filename = "";
2e927613
TV
6905 else if (strcmp (filename, artificial) == 0)
6906 {
6907 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
6908 sect_offset_str (per_cu->sect_off),
6909 (char *) NULL));
2e927613
TV
6910 filename = debug_filename.get ();
6911 }
0018ea6f 6912
7aa104c4 6913 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
6914
6915 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6916 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 6917
b3b3bada 6918 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
6919
6920 dwarf2_find_base_address (comp_unit_die, cu);
6921
6922 /* Possibly set the default values of LOWPC and HIGHPC from
6923 `DW_AT_ranges'. */
3a2b436a
JK
6924 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6925 &best_highpc, cu, pst);
6926 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
6927 {
6928 CORE_ADDR low
6929 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
6930 - baseaddr);
6931 CORE_ADDR high
6932 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
6933 - baseaddr - 1);
6934 /* Store the contiguous range if it is not empty; it can be
6935 empty for CUs with no code. */
84685904 6936 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 6937 low, high, pst);
79748972 6938 }
0018ea6f
DE
6939
6940 /* Check if comp unit has_children.
6941 If so, read the rest of the partial symbols from this comp unit.
6942 If not, there's no more debug_info for this comp unit. */
3e225074 6943 if (comp_unit_die->has_children)
0018ea6f
DE
6944 {
6945 struct partial_die_info *first_die;
6946 CORE_ADDR lowpc, highpc;
6947
6948 lowpc = ((CORE_ADDR) -1);
6949 highpc = ((CORE_ADDR) 0);
6950
6951 first_die = load_partial_dies (reader, info_ptr, 1);
6952
6953 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6954 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6955
6956 /* If we didn't find a lowpc, set it to highpc to avoid
6957 complaints from `maint check'. */
6958 if (lowpc == ((CORE_ADDR) -1))
6959 lowpc = highpc;
6960
6961 /* If the compilation unit didn't have an explicit address range,
6962 then use the information extracted from its child dies. */
e385593e 6963 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6964 {
6965 best_lowpc = lowpc;
6966 best_highpc = highpc;
6967 }
6968 }
4ae976d1 6969 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
6970 best_lowpc + baseaddr)
6971 - baseaddr);
4ae976d1 6972 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
6973 best_highpc + baseaddr)
6974 - baseaddr);
0018ea6f 6975
ae7754b2 6976 pst->end ();
0018ea6f 6977
ae640021 6978 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
6979 {
6980 int i;
ae640021 6981 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
6982
6983 /* Fill in 'dependencies' here; we fill in 'users' in a
6984 post-pass. */
6985 pst->number_of_dependencies = len;
a9342b62 6986 pst->dependencies
84685904 6987 = per_bfd->partial_symtabs->allocate_dependencies (len);
ae640021
AB
6988 for (i = 0; i < len; ++i)
6989 {
6990 pst->dependencies[i]
6991 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
6992 }
0018ea6f 6993
ae640021 6994 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
6995 }
6996
6997 /* Get the list of files included in the current compilation unit,
6998 and build a psymtab for each of them. */
6999 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7000
6f738b01
SM
7001 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7002 ", %d global, %d static syms",
7003 per_cu->is_debug_types ? "type" : "comp",
7004 sect_offset_str (per_cu->sect_off),
7005 paddress (gdbarch, pst->text_low (objfile)),
7006 paddress (gdbarch, pst->text_high (objfile)),
7007 (int) pst->global_psymbols.size (),
7008 (int) pst->static_psymbols.size ());
0018ea6f
DE
7009}
7010
7011/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7012 Process compilation unit THIS_CU for a psymtab. */
7013
7014static void
ab432490
SM
7015process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7016 dwarf2_per_objfile *per_objfile,
135f5437 7017 bool want_partial_unit,
b93601f3 7018 enum language pretend_language)
0018ea6f
DE
7019{
7020 /* If this compilation unit was already read in, free the
7021 cached copy in order to read it in again. This is
7022 necessary because we skipped some symbols when we first
7023 read in the compilation unit (see load_partial_dies).
7024 This problem could be avoided, but the benefit is unclear. */
7188ed02 7025 per_objfile->remove_cu (this_cu);
0018ea6f 7026
2e671100 7027 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7028
248f7165
TV
7029 if (reader.comp_unit_die == nullptr)
7030 return;
7031
58990295
TV
7032 switch (reader.comp_unit_die->tag)
7033 {
7034 case DW_TAG_compile_unit:
7035 this_cu->unit_type = DW_UT_compile;
7036 break;
7037 case DW_TAG_partial_unit:
7038 this_cu->unit_type = DW_UT_partial;
7039 break;
e77b0004
TV
7040 case DW_TAG_type_unit:
7041 this_cu->unit_type = DW_UT_type;
7042 break;
58990295 7043 default:
702cf3f5
AB
7044 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7045 dwarf_tag_name (reader.comp_unit_die->tag),
7046 sect_offset_str (reader.cu->per_cu->sect_off),
7047 objfile_name (per_objfile->objfile));
58990295
TV
7048 }
7049
c0ab21c2 7050 if (reader.dummy_p)
f1902523 7051 {
c0ab21c2 7052 /* Nothing. */
f1902523 7053 }
c0ab21c2 7054 else if (this_cu->is_debug_types)
3e225074
TT
7055 build_type_psymtabs_reader (&reader, reader.info_ptr,
7056 reader.comp_unit_die);
135f5437
TT
7057 else if (want_partial_unit
7058 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7059 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7060 reader.comp_unit_die,
c0ab21c2 7061 pretend_language);
0018ea6f
DE
7062
7063 /* Age out any secondary CUs. */
7188ed02 7064 per_objfile->age_comp_units ();
0018ea6f 7065}
f4dc4d17
DE
7066
7067/* Reader function for build_type_psymtabs. */
7068
7069static void
7070build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7071 const gdb_byte *info_ptr,
3e225074 7072 struct die_info *type_unit_die)
f4dc4d17 7073{
976ca316 7074 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7075 struct dwarf2_cu *cu = reader->cu;
7076 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7077 struct signatured_type *sig_type;
f4dc4d17
DE
7078 struct type_unit_group *tu_group;
7079 struct attribute *attr;
7080 struct partial_die_info *first_die;
7081 CORE_ADDR lowpc, highpc;
891813be 7082 dwarf2_psymtab *pst;
f4dc4d17 7083
0186c6a7
DE
7084 gdb_assert (per_cu->is_debug_types);
7085 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7086
3e225074 7087 if (! type_unit_die->has_children)
f4dc4d17
DE
7088 return;
7089
052c8bb8 7090 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7091 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7092
df07e2c7 7093 if (tu_group->tus == nullptr)
a8b3b8e9 7094 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7095 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7096
7097 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7098 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7099 pst->anonymous = true;
f4dc4d17
DE
7100
7101 first_die = load_partial_dies (reader, info_ptr, 1);
7102
7103 lowpc = (CORE_ADDR) -1;
7104 highpc = (CORE_ADDR) 0;
7105 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7106
ae7754b2 7107 pst->end ();
f4dc4d17
DE
7108}
7109
73051182
DE
7110/* Struct used to sort TUs by their abbreviation table offset. */
7111
7112struct tu_abbrev_offset
7113{
b2bdb8cf
SM
7114 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7115 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7116 {}
7117
16e0020b 7118 /* This is used when sorting. */
5433e20e 7119 bool operator< (const tu_abbrev_offset &other) const
16e0020b
TT
7120 {
7121 return abbrev_offset < other.abbrev_offset;
7122 }
7123
b2bdb8cf 7124 signatured_type *sig_type;
73051182
DE
7125 sect_offset abbrev_offset;
7126};
7127
73051182 7128/* Efficiently read all the type units.
73051182
DE
7129
7130 The efficiency is because we sort TUs by the abbrev table they use and
7131 only read each abbrev table once. In one program there are 200K TUs
7132 sharing 8K abbrev tables.
7133
7134 The main purpose of this function is to support building the
5989a64e 7135 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7136 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7137 can collapse the search space by grouping them by stmt_list.
7138 The savings can be significant, in the same program from above the 200K TUs
7139 share 8K stmt_list tables.
7140
7141 FUNC is expected to call get_type_unit_group, which will create the
7142 struct type_unit_group if necessary and add it to
5989a64e 7143 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7144
7145static void
b8efb248 7146build_type_psymtabs (dwarf2_per_objfile *per_objfile)
73051182 7147{
976ca316 7148 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7149 abbrev_table_up abbrev_table;
73051182 7150 sect_offset abbrev_offset;
73051182
DE
7151
7152 /* It's up to the caller to not call us multiple times. */
976ca316 7153 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7154
91eea9cc 7155 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
73051182
DE
7156 return;
7157
7158 /* TUs typically share abbrev tables, and there can be way more TUs than
7159 abbrev tables. Sort by abbrev table to reduce the number of times we
7160 read each abbrev table in.
7161 Alternatives are to punt or to maintain a cache of abbrev tables.
7162 This is simpler and efficient enough for now.
7163
7164 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7165 symtab to use). Typically TUs with the same abbrev offset have the same
7166 stmt_list value too so in practice this should work well.
7167
7168 The basic algorithm here is:
7169
7170 sort TUs by abbrev table
7171 for each TU with same abbrev table:
7172 read abbrev table if first user
7173 read TU top level DIE
7174 [IWBN if DWO skeletons had DW_AT_stmt_list]
7175 call FUNC */
7176
6f738b01 7177 dwarf_read_debug_printf ("Building type unit groups ...");
73051182 7178
91eea9cc 7179 /* Sort in a separate table to maintain the order of all_comp_units
73051182 7180 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7181 std::vector<tu_abbrev_offset> sorted_by_abbrev;
91eea9cc 7182 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
b2bdb8cf 7183
91eea9cc
TT
7184 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7185 {
7186 if (cu->is_debug_types)
7187 {
7188 auto sig_type = static_cast<signatured_type *> (cu.get ());
7189 sorted_by_abbrev.emplace_back
7190 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7191 sig_type->sect_off));
7192 }
7193 }
73051182 7194
16e0020b 7195 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
73051182 7196
9c541725 7197 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7198
b2bdb8cf 7199 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7200 {
73051182
DE
7201 /* Switch to the next abbrev table if necessary. */
7202 if (abbrev_table == NULL
b2bdb8cf 7203 || tu.abbrev_offset != abbrev_offset)
73051182 7204 {
b2bdb8cf 7205 abbrev_offset = tu.abbrev_offset;
606decb2 7206 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7207 abbrev_table =
606decb2 7208 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7209 ++tu_stats->nr_uniq_abbrev_tables;
7210 }
7211
0d305d5c 7212 cutu_reader reader (tu.sig_type, per_objfile,
2e671100 7213 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7214 if (!reader.dummy_p)
7215 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7216 reader.comp_unit_die);
73051182 7217 }
6aa5f3a6 7218}
73051182 7219
6aa5f3a6
DE
7220/* Print collected type unit statistics. */
7221
7222static void
976ca316 7223print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7224{
976ca316 7225 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7226
6f738b01 7227 dwarf_read_debug_printf ("Type unit statistics:");
91eea9cc 7228 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
6f738b01
SM
7229 dwarf_read_debug_printf (" %d uniq abbrev tables",
7230 tu_stats->nr_uniq_abbrev_tables);
7231 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7232 tu_stats->nr_symtabs);
7233 dwarf_read_debug_printf (" %d symtab sharers",
7234 tu_stats->nr_symtab_sharers);
7235 dwarf_read_debug_printf (" %d type units without a stmt_list",
7236 tu_stats->nr_stmt_less_type_units);
7237 dwarf_read_debug_printf (" %d all_type_units reallocs",
7238 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7239}
7240
f4dc4d17
DE
7241/* Traversal function for build_type_psymtabs. */
7242
7243static int
7244build_type_psymtab_dependencies (void **slot, void *info)
7245{
976ca316 7246 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
84685904 7247 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 7248 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
0d305d5c 7249 dwarf2_psymtab *pst = tu_group->v.psymtab;
df07e2c7 7250 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7251 int i;
7252
7253 gdb_assert (len > 0);
0d305d5c 7254 gdb_assert (tu_group->type_unit_group_p ());
f4dc4d17
DE
7255
7256 pst->number_of_dependencies = len;
84685904 7257 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
df07e2c7 7258 for (i = 0; i < len; ++i)
f4dc4d17 7259 {
df07e2c7 7260 struct signatured_type *iter = tu_group->tus->at (i);
0d305d5c
TT
7261 gdb_assert (iter->is_debug_types);
7262 pst->dependencies[i] = iter->v.psymtab;
796a7ff8 7263 iter->type_unit_group = tu_group;
f4dc4d17
DE
7264 }
7265
df07e2c7
AB
7266 delete tu_group->tus;
7267 tu_group->tus = nullptr;
348e048f
DE
7268
7269 return 1;
7270}
7271
6aa5f3a6
DE
7272/* Traversal function for process_skeletonless_type_unit.
7273 Read a TU in a DWO file and build partial symbols for it. */
7274
7275static int
7276process_skeletonless_type_unit (void **slot, void *info)
7277{
7278 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7279 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7280
7281 /* If this TU doesn't exist in the global table, add it and read it in. */
7282
976ca316
SM
7283 if (per_objfile->per_bfd->signatured_types == NULL)
7284 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6 7285
9ea36493 7286 signatured_type find_entry (dwo_unit->signature);
976ca316 7287 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7288 &find_entry, INSERT);
6aa5f3a6
DE
7289 /* If we've already seen this type there's nothing to do. What's happening
7290 is we're doing our own version of comdat-folding here. */
7291 if (*slot != NULL)
7292 return 1;
7293
b8efb248 7294 /* This does the job that create_all_comp_units would have done for
6aa5f3a6 7295 this TU. */
9ea36493
SM
7296 signatured_type *entry
7297 = add_type_unit (per_objfile, dwo_unit->signature, slot);
976ca316 7298 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7299 *slot = entry;
7300
b8efb248 7301 /* This does the job that build_type_psymtabs would have done. */
0d305d5c 7302 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7303 if (!reader.dummy_p)
7304 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7305 reader.comp_unit_die);
6aa5f3a6
DE
7306
7307 return 1;
7308}
7309
7310/* Traversal function for process_skeletonless_type_units. */
7311
7312static int
7313process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7314{
7315 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7316
7317 if (dwo_file->tus != NULL)
b0b6a987
TT
7318 htab_traverse_noresize (dwo_file->tus.get (),
7319 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7320
7321 return 1;
7322}
7323
7324/* Scan all TUs of DWO files, verifying we've processed them.
7325 This is needed in case a TU was emitted without its skeleton.
7326 Note: This can't be done until we know what all the DWO files are. */
7327
7328static void
976ca316 7329process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7330{
7331 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7332 if (get_dwp_file (per_objfile) == NULL
7333 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7334 {
976ca316 7335 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7336 process_dwo_file_for_skeletonless_type_units,
976ca316 7337 per_objfile);
6aa5f3a6 7338 }
348e048f
DE
7339}
7340
ed2dc618 7341/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7342
7343static void
976ca316 7344set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7345{
0d305d5c 7346 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7347 {
891813be 7348 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7349
36586728
TT
7350 if (pst == NULL)
7351 continue;
7352
b76e467d 7353 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7354 {
7355 /* Set the 'user' field only if it is not already set. */
7356 if (pst->dependencies[j]->user == NULL)
7357 pst->dependencies[j]->user = pst;
7358 }
7359 }
7360}
7361
93311388
DE
7362/* Build the partial symbol table by doing a quick pass through the
7363 .debug_info and .debug_abbrev sections. */
72bf9492 7364
93311388 7365static void
976ca316 7366dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 7367{
976ca316 7368 struct objfile *objfile = per_objfile->objfile;
84685904 7369 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
93311388 7370
6f738b01
SM
7371 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7372 objfile_name (objfile));
45cfd468 7373
76935768 7374 scoped_restore restore_reading_psyms
bab287cd 7375 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
98bfdba5 7376
84685904 7377 per_bfd->info.read (objfile);
91c24f0a 7378
93311388
DE
7379 /* Any cached compilation units will be linked by the per-objfile
7380 read_in_chain. Make sure to free them when we're done. */
976ca316 7381 free_cached_comp_units freer (per_objfile);
72bf9492 7382
976ca316 7383 create_all_comp_units (per_objfile);
b8efb248 7384 build_type_psymtabs (per_objfile);
c906108c 7385
60606b2c
TT
7386 /* Create a temporary address map on a temporary obstack. We later
7387 copy this to the final obstack. */
8268c778 7388 auto_obstack temp_obstack;
791afaa2
TT
7389
7390 scoped_restore save_psymtabs_addrmap
84685904 7391 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
791afaa2 7392 addrmap_create_mutable (&temp_obstack));
72bf9492 7393
0d305d5c 7394 for (const auto &per_cu : per_bfd->all_comp_units)
3d5afab3
TV
7395 {
7396 if (per_cu->v.psymtab != NULL)
7397 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7398 continue;
0d305d5c 7399 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
ab432490 7400 language_minimal);
3d5afab3 7401 }
ff013f42 7402
6aa5f3a6 7403 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 7404 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
7405
7406 /* Now that all TUs have been processed we can fill in the dependencies. */
84685904 7407 if (per_bfd->type_unit_groups != NULL)
6aa5f3a6 7408 {
84685904 7409 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
976ca316 7410 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
7411 }
7412
6f738b01 7413 if (dwarf_read_debug > 0)
976ca316 7414 print_tu_stats (per_objfile);
6aa5f3a6 7415
976ca316 7416 set_partial_user (per_objfile);
95554aad 7417
84685904
TT
7418 per_bfd->partial_symtabs->psymtabs_addrmap
7419 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7420 per_bfd->partial_symtabs->obstack ());
791afaa2
TT
7421 /* At this point we want to keep the address map. */
7422 save_psymtabs_addrmap.release ();
ff013f42 7423
6f738b01
SM
7424 dwarf_read_debug_printf ("Done building psymtabs of %s",
7425 objfile_name (objfile));
ae038cb0
DJ
7426}
7427
dee91e82
DE
7428/* Load the partial DIEs for a secondary CU into memory.
7429 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7430
dee91e82 7431static void
ab432490 7432load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
7433 dwarf2_per_objfile *per_objfile,
7434 dwarf2_cu *existing_cu)
dee91e82 7435{
2e671100 7436 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
7437
7438 if (!reader.dummy_p)
7439 {
7440 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7441 language_minimal);
7442
7443 /* Check if comp unit has_children.
7444 If so, read the rest of the partial symbols from this comp unit.
7445 If not, there's no more debug_info for this comp unit. */
3e225074 7446 if (reader.comp_unit_die->has_children)
c0ab21c2 7447 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7448
7449 reader.keep ();
c0ab21c2 7450 }
ae038cb0
DJ
7451}
7452
ae038cb0 7453static void
976ca316 7454read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 7455 struct dwarf2_section_info *section,
f1902523 7456 struct dwarf2_section_info *abbrev_section,
b8efb248
TT
7457 unsigned int is_dwz,
7458 htab_up &types_htab,
7459 rcuh_kind section_kind)
ae038cb0 7460{
d521ce57 7461 const gdb_byte *info_ptr;
976ca316 7462 struct objfile *objfile = per_objfile->objfile;
be391dca 7463
6f738b01
SM
7464 dwarf_read_debug_printf ("Reading %s for %s",
7465 section->get_name (),
7466 section->get_file_name ());
bf6af496 7467
96b79293 7468 section->read (objfile);
ae038cb0 7469
36586728 7470 info_ptr = section->buffer;
6e70227d 7471
36586728 7472 while (info_ptr < section->buffer + section->size)
ae038cb0 7473 {
473ab964 7474 dwarf2_per_cu_data_up this_cu;
ae038cb0 7475
9c541725 7476 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7477
f1902523 7478 comp_unit_head cu_header;
976ca316 7479 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 7480 abbrev_section, info_ptr,
b8efb248 7481 section_kind);
ae038cb0
DJ
7482
7483 /* Save the compilation unit for later lookup. */
f1902523 7484 if (cu_header.unit_type != DW_UT_type)
976ca316 7485 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
7486 else
7487 {
b8efb248
TT
7488 if (types_htab == nullptr)
7489 types_htab = allocate_signatured_type_table ();
7490
9ea36493
SM
7491 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7492 (cu_header.signature);
b8efb248 7493 signatured_type *sig_ptr = sig_type.get ();
f1902523 7494 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
473ab964 7495 this_cu.reset (sig_type.release ());
b8efb248
TT
7496
7497 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7498 gdb_assert (slot != nullptr);
7499 if (*slot != nullptr)
7500 complaint (_("debug type entry at offset %s is duplicate to"
7501 " the entry at offset %s, signature %s"),
7502 sect_offset_str (sect_off),
7503 sect_offset_str (sig_ptr->sect_off),
7504 hex_string (sig_ptr->signature));
7505 *slot = sig_ptr;
f1902523 7506 }
9c541725 7507 this_cu->sect_off = sect_off;
f1902523 7508 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7509 this_cu->is_dwz = is_dwz;
8a0459fd 7510 this_cu->section = section;
ae038cb0 7511
ae038cb0 7512 info_ptr = info_ptr + this_cu->length;
0d305d5c 7513 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
ae038cb0 7514 }
36586728
TT
7515}
7516
7517/* Create a list of all compilation units in OBJFILE.
7518 This is only done for -readnow and building partial symtabs. */
7519
7520static void
976ca316 7521create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 7522{
b8efb248
TT
7523 htab_up types_htab;
7524
976ca316 7525 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
b8efb248
TT
7526 &per_objfile->per_bfd->abbrev, 0,
7527 types_htab, rcuh_kind::COMPILE);
7528 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7529 read_comp_units_from_section (per_objfile, &section,
7530 &per_objfile->per_bfd->abbrev, 0,
7531 types_htab, rcuh_kind::TYPE);
36586728 7532
976ca316 7533 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 7534 if (dwz != NULL)
b8efb248
TT
7535 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7536 types_htab, rcuh_kind::COMPILE);
7537
7538 per_objfile->per_bfd->signatured_types = std::move (types_htab);
c906108c
SS
7539}
7540
5734ee8b 7541/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7542 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7543 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7544 DW_AT_ranges). See the comments of add_partial_subprogram on how
7545 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7546
72bf9492
DJ
7547static void
7548scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7549 CORE_ADDR *highpc, int set_addrmap,
7550 struct dwarf2_cu *cu)
c906108c 7551{
72bf9492 7552 struct partial_die_info *pdi;
c906108c 7553
91c24f0a
DC
7554 /* Now, march along the PDI's, descending into ones which have
7555 interesting children but skipping the children of the other ones,
7556 until we reach the end of the compilation unit. */
c906108c 7557
72bf9492 7558 pdi = first_die;
91c24f0a 7559
72bf9492
DJ
7560 while (pdi != NULL)
7561 {
52356b79 7562 pdi->fixup (cu);
c906108c 7563
f55ee35c 7564 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7565 children, so we need to look at them. Ditto for anonymous
7566 enums. */
933c6fe4 7567
7d00ffec 7568 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7569 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7570 || pdi->tag == DW_TAG_imported_unit
7571 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7572 {
72bf9492 7573 switch (pdi->tag)
c906108c
SS
7574 {
7575 case DW_TAG_subprogram:
b1dc1806 7576 case DW_TAG_inlined_subroutine:
cdc07690 7577 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
3e9f1ca1 7578 if (cu->per_cu->lang == language_cplus)
f9b5d5ea
TV
7579 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7580 set_addrmap, cu);
c906108c 7581 break;
72929c62 7582 case DW_TAG_constant:
c906108c
SS
7583 case DW_TAG_variable:
7584 case DW_TAG_typedef:
91c24f0a 7585 case DW_TAG_union_type:
317d2668
TV
7586 if (!pdi->is_declaration
7587 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 7588 {
72bf9492 7589 add_partial_symbol (pdi, cu);
63d06c5c
DC
7590 }
7591 break;
c906108c 7592 case DW_TAG_class_type:
680b30c7 7593 case DW_TAG_interface_type:
c906108c 7594 case DW_TAG_structure_type:
72bf9492 7595 if (!pdi->is_declaration)
c906108c 7596 {
72bf9492 7597 add_partial_symbol (pdi, cu);
c906108c 7598 }
3e9f1ca1
TT
7599 if ((cu->per_cu->lang == language_rust
7600 || cu->per_cu->lang == language_cplus)
7601 && pdi->has_children)
e98c9e7c
TT
7602 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7603 set_addrmap, cu);
c906108c 7604 break;
91c24f0a 7605 case DW_TAG_enumeration_type:
72bf9492
DJ
7606 if (!pdi->is_declaration)
7607 add_partial_enumeration (pdi, cu);
c906108c
SS
7608 break;
7609 case DW_TAG_base_type:
dda83cd7 7610 case DW_TAG_subrange_type:
c906108c 7611 /* File scope base type definitions are added to the partial
dda83cd7 7612 symbol table. */
72bf9492 7613 add_partial_symbol (pdi, cu);
c906108c 7614 break;
d9fa45fe 7615 case DW_TAG_namespace:
cdc07690 7616 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7617 break;
5d7cb8df 7618 case DW_TAG_module:
59c35742
AB
7619 if (!pdi->is_declaration)
7620 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7621 break;
95554aad
TT
7622 case DW_TAG_imported_unit:
7623 {
7624 struct dwarf2_per_cu_data *per_cu;
7625
f4dc4d17
DE
7626 /* For now we don't handle imported units in type units. */
7627 if (cu->per_cu->is_debug_types)
7628 {
7629 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7630 " supported in type units [in module %s]"),
5e22e966 7631 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
7632 }
7633
e3b94546 7634 per_cu = dwarf2_find_containing_comp_unit
5e22e966 7635 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
7636
7637 /* Go read the partial unit, if needed. */
7638 if (per_cu->v.psymtab == NULL)
ab432490 7639 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
3e9f1ca1 7640 cu->per_cu->lang);
95554aad 7641
ae640021 7642 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7643 }
7644 break;
74921315
KS
7645 case DW_TAG_imported_declaration:
7646 add_partial_symbol (pdi, cu);
7647 break;
c906108c
SS
7648 default:
7649 break;
7650 }
7651 }
7652
72bf9492
DJ
7653 /* If the die has a sibling, skip to the sibling. */
7654
7655 pdi = pdi->die_sibling;
7656 }
7657}
7658
7659/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7660
72bf9492 7661 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7662 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7663 Enumerators are an exception; they use the scope of their parent
7664 enumeration type, i.e. the name of the enumeration type is not
7665 prepended to the enumerator.
91c24f0a 7666
72bf9492
DJ
7667 There are two complexities. One is DW_AT_specification; in this
7668 case "parent" means the parent of the target of the specification,
7669 instead of the direct parent of the DIE. The other is compilers
7670 which do not emit DW_TAG_namespace; in this case we try to guess
7671 the fully qualified name of structure types from their members'
7672 linkage names. This must be done using the DIE's children rather
7673 than the children of any DW_AT_specification target. We only need
7674 to do this for structures at the top level, i.e. if the target of
7675 any DW_AT_specification (if any; otherwise the DIE itself) does not
7676 have a parent. */
7677
7678/* Compute the scope prefix associated with PDI's parent, in
7679 compilation unit CU. The result will be allocated on CU's
7680 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7681 field. NULL is returned if no prefix is necessary. */
15d034d0 7682static const char *
72bf9492
DJ
7683partial_die_parent_scope (struct partial_die_info *pdi,
7684 struct dwarf2_cu *cu)
7685{
15d034d0 7686 const char *grandparent_scope;
72bf9492 7687 struct partial_die_info *parent, *real_pdi;
91c24f0a 7688
72bf9492
DJ
7689 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7690 then this means the parent of the specification DIE. */
7691
7692 real_pdi = pdi;
72bf9492 7693 while (real_pdi->has_specification)
fb816e8b 7694 {
122cf0f2
AB
7695 auto res = find_partial_die (real_pdi->spec_offset,
7696 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
7697 real_pdi = res.pdi;
7698 cu = res.cu;
7699 }
72bf9492
DJ
7700
7701 parent = real_pdi->die_parent;
7702 if (parent == NULL)
7703 return NULL;
7704
7705 if (parent->scope_set)
7706 return parent->scope;
7707
52356b79 7708 parent->fixup (cu);
72bf9492 7709
10b3939b 7710 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7711
acebe513
UW
7712 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7713 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7714 Work around this problem here. */
3e9f1ca1 7715 if (cu->per_cu->lang == language_cplus
6e70227d 7716 && parent->tag == DW_TAG_namespace
7d00ffec 7717 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
7718 && grandparent_scope == NULL)
7719 {
7720 parent->scope = NULL;
7721 parent->scope_set = 1;
7722 return NULL;
7723 }
7724
0a4b0913 7725 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
7726 if (pdi->tag == DW_TAG_enumerator)
7727 /* Enumerators should not get the name of the enumeration as a prefix. */
7728 parent->scope = grandparent_scope;
7729 else if (parent->tag == DW_TAG_namespace
f55ee35c 7730 || parent->tag == DW_TAG_module
72bf9492
DJ
7731 || parent->tag == DW_TAG_structure_type
7732 || parent->tag == DW_TAG_class_type
680b30c7 7733 || parent->tag == DW_TAG_interface_type
ceeb3d5a 7734 || parent->tag == DW_TAG_union_type
0a4b0913 7735 || parent->tag == DW_TAG_enumeration_type
3e9f1ca1 7736 || (cu->per_cu->lang == language_fortran
0a4b0913
AB
7737 && parent->tag == DW_TAG_subprogram
7738 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
7739 {
7740 if (grandparent_scope == NULL)
7d00ffec 7741 parent->scope = parent->name (cu);
72bf9492 7742 else
3e43a32a
MS
7743 parent->scope = typename_concat (&cu->comp_unit_obstack,
7744 grandparent_scope,
7d00ffec 7745 parent->name (cu), 0, cu);
72bf9492 7746 }
72bf9492
DJ
7747 else
7748 {
7749 /* FIXME drow/2004-04-01: What should we be doing with
7750 function-local names? For partial symbols, we should probably be
7751 ignoring them. */
fa9c3fa0
TT
7752 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7753 dwarf_tag_name (parent->tag),
7754 sect_offset_str (pdi->sect_off));
72bf9492 7755 parent->scope = grandparent_scope;
c906108c
SS
7756 }
7757
72bf9492
DJ
7758 parent->scope_set = 1;
7759 return parent->scope;
7760}
7761
7762/* Return the fully scoped name associated with PDI, from compilation unit
7763 CU. The result will be allocated with malloc. */
4568ecf9 7764
43816ebc 7765static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
7766partial_die_full_name (struct partial_die_info *pdi,
7767 struct dwarf2_cu *cu)
7768{
15d034d0 7769 const char *parent_scope;
72bf9492 7770
98bfdba5
PA
7771 /* If this is a template instantiation, we can not work out the
7772 template arguments from partial DIEs. So, unfortunately, we have
7773 to go through the full DIEs. At least any work we do building
7774 types here will be reused if full symbols are loaded later. */
7775 if (pdi->has_template_arguments)
7776 {
52356b79 7777 pdi->fixup (cu);
98bfdba5 7778
7d00ffec 7779 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
7780 {
7781 struct die_info *die;
7782 struct attribute attr;
7783 struct dwarf2_cu *ref_cu = cu;
7784
b64f50a1 7785 /* DW_FORM_ref_addr is using section offset. */
b4069958 7786 attr.name = (enum dwarf_attribute) 0;
98bfdba5 7787 attr.form = DW_FORM_ref_addr;
9c541725 7788 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
7789 die = follow_die_ref (NULL, &attr, &ref_cu);
7790
43816ebc 7791 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
7792 }
7793 }
7794
72bf9492
DJ
7795 parent_scope = partial_die_parent_scope (pdi, cu);
7796 if (parent_scope == NULL)
7797 return NULL;
7798 else
43816ebc 7799 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
7800 pdi->name (cu),
7801 0, cu));
c906108c
SS
7802}
7803
7804static void
72bf9492 7805add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 7806{
976ca316
SM
7807 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7808 struct objfile *objfile = per_objfile->objfile;
08feed99 7809 struct gdbarch *gdbarch = objfile->arch ();
c906108c 7810 CORE_ADDR addr = 0;
15d034d0 7811 const char *actual_name = NULL;
e142c38c
DJ
7812 CORE_ADDR baseaddr;
7813
b3b3bada 7814 baseaddr = objfile->text_section_offset ();
c906108c 7815
43816ebc
TT
7816 gdb::unique_xmalloc_ptr<char> built_actual_name
7817 = partial_die_full_name (pdi, cu);
15d034d0 7818 if (built_actual_name != NULL)
43816ebc 7819 actual_name = built_actual_name.get ();
63d06c5c 7820
72bf9492 7821 if (actual_name == NULL)
7d00ffec 7822 actual_name = pdi->name (cu);
72bf9492 7823
76e288d1
TT
7824 partial_symbol psymbol;
7825 memset (&psymbol, 0, sizeof (psymbol));
3e9f1ca1
TT
7826 psymbol.ginfo.set_language (cu->per_cu->lang,
7827 &objfile->objfile_obstack);
a52d653e 7828 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
7829
7830 /* The code below indicates that the psymbol should be installed by
7831 setting this. */
7832 gdb::optional<psymbol_placement> where;
7833
c906108c
SS
7834 switch (pdi->tag)
7835 {
b1dc1806 7836 case DW_TAG_inlined_subroutine:
c906108c 7837 case DW_TAG_subprogram:
79748972
TT
7838 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7839 - baseaddr);
0a4b0913 7840 if (pdi->is_external
3e9f1ca1
TT
7841 || cu->per_cu->lang == language_ada
7842 || (cu->per_cu->lang == language_fortran
0a4b0913
AB
7843 && pdi->die_parent != NULL
7844 && pdi->die_parent->tag == DW_TAG_subprogram))
7845 {
dda83cd7
SM
7846 /* Normally, only "external" DIEs are part of the global scope.
7847 But in Ada and Fortran, we want to be able to access nested
7848 procedures globally. So all Ada and Fortran subprograms are
7849 stored in the global scope. */
76e288d1 7850 where = psymbol_placement::GLOBAL;
c906108c
SS
7851 }
7852 else
76e288d1
TT
7853 where = psymbol_placement::STATIC;
7854
7855 psymbol.domain = VAR_DOMAIN;
7856 psymbol.aclass = LOC_BLOCK;
a52d653e 7857 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 7858 psymbol.ginfo.value.address = addr;
0c1b455e
TT
7859
7860 if (pdi->main_subprogram && actual_name != NULL)
3e9f1ca1 7861 set_objfile_main_name (objfile, actual_name, cu->per_cu->lang);
c906108c 7862 break;
72929c62 7863 case DW_TAG_constant:
76e288d1
TT
7864 psymbol.domain = VAR_DOMAIN;
7865 psymbol.aclass = LOC_STATIC;
7866 where = (pdi->is_external
7867 ? psymbol_placement::GLOBAL
7868 : psymbol_placement::STATIC);
72929c62 7869 break;
c906108c 7870 case DW_TAG_variable:
95554aad
TT
7871 if (pdi->d.locdesc)
7872 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7873
95554aad 7874 if (pdi->d.locdesc
caac4577 7875 && addr == 0
976ca316 7876 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
7877 {
7878 /* A global or static variable may also have been stripped
7879 out by the linker if unused, in which case its address
7880 will be nullified; do not add such variables into partial
7881 symbol table then. */
7882 }
7883 else if (pdi->is_external)
c906108c
SS
7884 {
7885 /* Global Variable.
7886 Don't enter into the minimal symbol tables as there is
7887 a minimal symbol table entry from the ELF symbols already.
7888 Enter into partial symbol table if it has a location
7889 descriptor or a type.
7890 If the location descriptor is missing, new_symbol will create
7891 a LOC_UNRESOLVED symbol, the address of the variable will then
7892 be determined from the minimal symbol table whenever the variable
7893 is referenced.
7894 The address for the partial symbol table entry is not
7895 used by GDB, but it comes in handy for debugging partial symbol
7896 table building. */
7897
95554aad 7898 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
7899 {
7900 psymbol.domain = VAR_DOMAIN;
7901 psymbol.aclass = LOC_STATIC;
a52d653e 7902 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
7903 psymbol.ginfo.value.address = addr;
7904 where = psymbol_placement::GLOBAL;
7905 }
c906108c
SS
7906 }
7907 else
7908 {
ff908ebf
AW
7909 int has_loc = pdi->d.locdesc != NULL;
7910
7911 /* Static Variable. Skip symbols whose value we cannot know (those
7912 without location descriptors or constant values). */
7913 if (!has_loc && !pdi->has_const_value)
43816ebc 7914 return;
ff908ebf 7915
76e288d1
TT
7916 psymbol.domain = VAR_DOMAIN;
7917 psymbol.aclass = LOC_STATIC;
a52d653e 7918 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
7919 if (has_loc)
7920 psymbol.ginfo.value.address = addr;
7921 where = psymbol_placement::STATIC;
c906108c
SS
7922 }
7923 break;
d8f62e84 7924 case DW_TAG_array_type:
c906108c
SS
7925 case DW_TAG_typedef:
7926 case DW_TAG_base_type:
a02abb62 7927 case DW_TAG_subrange_type:
76e288d1
TT
7928 psymbol.domain = VAR_DOMAIN;
7929 psymbol.aclass = LOC_TYPEDEF;
7930 where = psymbol_placement::STATIC;
c906108c 7931 break;
74921315 7932 case DW_TAG_imported_declaration:
72bf9492 7933 case DW_TAG_namespace:
76e288d1
TT
7934 psymbol.domain = VAR_DOMAIN;
7935 psymbol.aclass = LOC_TYPEDEF;
7936 where = psymbol_placement::GLOBAL;
72bf9492 7937 break;
530e8392 7938 case DW_TAG_module:
a5fd13a9 7939 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
7940 available without any name. If so, we skip the module as it
7941 doesn't bring any value. */
a5fd13a9 7942 if (actual_name != nullptr)
76e288d1
TT
7943 {
7944 psymbol.domain = MODULE_DOMAIN;
7945 psymbol.aclass = LOC_TYPEDEF;
7946 where = psymbol_placement::GLOBAL;
7947 }
530e8392 7948 break;
c906108c 7949 case DW_TAG_class_type:
680b30c7 7950 case DW_TAG_interface_type:
c906108c
SS
7951 case DW_TAG_structure_type:
7952 case DW_TAG_union_type:
7953 case DW_TAG_enumeration_type:
fa4028e9 7954 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
7955 about "Structure, Union, and Class Type Entries": "An incomplete
7956 structure, union or class type is represented by a structure,
7957 union or class entry that does not have a byte size attribute
7958 and that has a DW_AT_declaration attribute." */
fa4028e9 7959 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 7960 return;
fa4028e9 7961
63d06c5c
DC
7962 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7963 static vs. global. */
76e288d1
TT
7964 psymbol.domain = STRUCT_DOMAIN;
7965 psymbol.aclass = LOC_TYPEDEF;
3e9f1ca1 7966 where = (cu->per_cu->lang == language_cplus
76e288d1
TT
7967 ? psymbol_placement::GLOBAL
7968 : psymbol_placement::STATIC);
c906108c
SS
7969 break;
7970 case DW_TAG_enumerator:
76e288d1
TT
7971 psymbol.domain = VAR_DOMAIN;
7972 psymbol.aclass = LOC_CONST;
3e9f1ca1 7973 where = (cu->per_cu->lang == language_cplus
76e288d1
TT
7974 ? psymbol_placement::GLOBAL
7975 : psymbol_placement::STATIC);
c906108c
SS
7976 break;
7977 default:
7978 break;
7979 }
76e288d1
TT
7980
7981 if (where.has_value ())
7982 {
f049a313
TT
7983 if (built_actual_name != nullptr)
7984 actual_name = objfile->intern (actual_name);
3e9f1ca1
TT
7985 if (pdi->linkage_name == nullptr
7986 || cu->per_cu->lang == language_ada)
bcfe6157
TT
7987 psymbol.ginfo.set_linkage_name (actual_name);
7988 else
7989 {
7990 psymbol.ginfo.set_demangled_name (actual_name,
7991 &objfile->objfile_obstack);
7992 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
7993 }
7b249e47
TT
7994 cu->per_cu->v.psymtab->add_psymbol
7995 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
7996 objfile);
76e288d1 7997 }
c906108c
SS
7998}
7999
5c4e30ca
DC
8000/* Read a partial die corresponding to a namespace; also, add a symbol
8001 corresponding to that namespace to the symbol table. NAMESPACE is
8002 the name of the enclosing namespace. */
91c24f0a 8003
72bf9492
DJ
8004static void
8005add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8006 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8007 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8008{
72bf9492 8009 /* Add a symbol for the namespace. */
e7c27a73 8010
72bf9492 8011 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8012
8013 /* Now scan partial symbols in that namespace. */
8014
91c24f0a 8015 if (pdi->has_children)
cdc07690 8016 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8017}
8018
5d7cb8df
JK
8019/* Read a partial die corresponding to a Fortran module. */
8020
8021static void
8022add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8023 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8024{
530e8392
KB
8025 /* Add a symbol for the namespace. */
8026
8027 add_partial_symbol (pdi, cu);
8028
f55ee35c 8029 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8030
8031 if (pdi->has_children)
cdc07690 8032 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8033}
8034
b1dc1806
XR
8035/* Read a partial die corresponding to a subprogram or an inlined
8036 subprogram and create a partial symbol for that subprogram.
8037 When the CU language allows it, this routine also defines a partial
8038 symbol for each nested subprogram that this subprogram contains.
8039 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8040 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8041
cdc07690
YQ
8042 PDI may also be a lexical block, in which case we simply search
8043 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8044 Again, this is only performed when the CU language allows this
8045 type of definitions. */
8046
8047static void
8048add_partial_subprogram (struct partial_die_info *pdi,
8049 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8050 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8051{
b1dc1806 8052 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8053 {
8054 if (pdi->has_pc_info)
dda83cd7
SM
8055 {
8056 if (pdi->lowpc < *lowpc)
8057 *lowpc = pdi->lowpc;
8058 if (pdi->highpc > *highpc)
8059 *highpc = pdi->highpc;
cdc07690 8060 if (set_addrmap)
5734ee8b 8061 {
5e22e966 8062 struct objfile *objfile = cu->per_objfile->objfile;
84685904 8063 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 8064 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8065 CORE_ADDR baseaddr;
b926417a
TT
8066 CORE_ADDR this_highpc;
8067 CORE_ADDR this_lowpc;
5734ee8b 8068
b3b3bada 8069 baseaddr = objfile->text_section_offset ();
b926417a
TT
8070 this_lowpc
8071 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8072 pdi->lowpc + baseaddr)
8073 - baseaddr);
8074 this_highpc
8075 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8076 pdi->highpc + baseaddr)
8077 - baseaddr);
84685904 8078 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
b926417a 8079 this_lowpc, this_highpc - 1,
9291a0cd 8080 cu->per_cu->v.psymtab);
5734ee8b 8081 }
dda83cd7 8082 }
481860b3
GB
8083
8084 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8085 {
dda83cd7 8086 if (!pdi->is_declaration)
e8d05480
JB
8087 /* Ignore subprogram DIEs that do not have a name, they are
8088 illegal. Do not emit a complaint at this point, we will
8089 do so when we convert this psymtab into a symtab. */
7d00ffec 8090 if (pdi->name (cu))
e8d05480 8091 add_partial_symbol (pdi, cu);
dda83cd7 8092 }
bc30ff58 8093 }
6e70227d 8094
bc30ff58
JB
8095 if (! pdi->has_children)
8096 return;
8097
3e9f1ca1
TT
8098 if (cu->per_cu->lang == language_ada
8099 || cu->per_cu->lang == language_fortran)
bc30ff58
JB
8100 {
8101 pdi = pdi->die_child;
8102 while (pdi != NULL)
8103 {
52356b79 8104 pdi->fixup (cu);
bc30ff58 8105 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8106 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8107 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8108 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8109 pdi = pdi->die_sibling;
8110 }
8111 }
8112}
8113
91c24f0a
DC
8114/* Read a partial die corresponding to an enumeration type. */
8115
72bf9492
DJ
8116static void
8117add_partial_enumeration (struct partial_die_info *enum_pdi,
8118 struct dwarf2_cu *cu)
91c24f0a 8119{
72bf9492 8120 struct partial_die_info *pdi;
91c24f0a 8121
7d00ffec 8122 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8123 add_partial_symbol (enum_pdi, cu);
8124
8125 pdi = enum_pdi->die_child;
8126 while (pdi)
91c24f0a 8127 {
7d00ffec 8128 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8129 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8130 else
72bf9492
DJ
8131 add_partial_symbol (pdi, cu);
8132 pdi = pdi->die_sibling;
91c24f0a 8133 }
91c24f0a
DC
8134}
8135
6caca83c
CC
8136/* Return the initial uleb128 in the die at INFO_PTR. */
8137
8138static unsigned int
d521ce57 8139peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8140{
8141 unsigned int bytes_read;
8142
8143 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8144}
8145
685af9cd
TT
8146/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8147 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8148
4bb7a0a7
DJ
8149 Return the corresponding abbrev, or NULL if the number is zero (indicating
8150 an empty DIE). In either case *BYTES_READ will be set to the length of
8151 the initial number. */
8152
7c32eebb 8153static const struct abbrev_info *
685af9cd
TT
8154peek_die_abbrev (const die_reader_specs &reader,
8155 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8156{
685af9cd 8157 dwarf2_cu *cu = reader.cu;
0280fdcc 8158 bfd *abfd = reader.abfd;
685af9cd
TT
8159 unsigned int abbrev_number
8160 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8161
8162 if (abbrev_number == 0)
8163 return NULL;
8164
7c32eebb
TT
8165 const abbrev_info *abbrev
8166 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8167 if (!abbrev)
8168 {
422b9917 8169 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8170 " at offset %s [in module %s]"),
422b9917 8171 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8172 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8173 }
8174
8175 return abbrev;
8176}
8177
93311388
DE
8178/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8179 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8180 DIE. Any children of the skipped DIEs will also be skipped. */
8181
d521ce57
TT
8182static const gdb_byte *
8183skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8184{
4bb7a0a7
DJ
8185 while (1)
8186 {
685af9cd 8187 unsigned int bytes_read;
7c32eebb
TT
8188 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8189 &bytes_read);
685af9cd 8190
4bb7a0a7
DJ
8191 if (abbrev == NULL)
8192 return info_ptr + bytes_read;
8193 else
dee91e82 8194 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8195 }
8196}
8197
93311388
DE
8198/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8199 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8200 abbrev corresponding to that skipped uleb128 should be passed in
8201 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8202 children. */
8203
d521ce57
TT
8204static const gdb_byte *
8205skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8206 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8207{
8208 unsigned int bytes_read;
8209 struct attribute attr;
dee91e82
DE
8210 bfd *abfd = reader->abfd;
8211 struct dwarf2_cu *cu = reader->cu;
d521ce57 8212 const gdb_byte *buffer = reader->buffer;
f664829e 8213 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8214 unsigned int form, i;
8215
8216 for (i = 0; i < abbrev->num_attrs; i++)
8217 {
8218 /* The only abbrev we care about is DW_AT_sibling. */
8219 if (abbrev->attrs[i].name == DW_AT_sibling)
8220 {
7a5f294d 8221 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8222 if (attr.form == DW_FORM_ref_addr)
b98664d3 8223 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8224 else
b9502d3f 8225 {
0826b30a 8226 sect_offset off = attr.get_ref_die_offset ();
9c541725 8227 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8228
8229 if (sibling_ptr < info_ptr)
b98664d3 8230 complaint (_("DW_AT_sibling points backwards"));
22869d73 8231 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8232 reader->die_section->overflow_complaint ();
b9502d3f
WN
8233 else
8234 return sibling_ptr;
8235 }
4bb7a0a7
DJ
8236 }
8237
8238 /* If it isn't DW_AT_sibling, skip this attribute. */
8239 form = abbrev->attrs[i].form;
8240 skip_attribute:
8241 switch (form)
8242 {
4bb7a0a7 8243 case DW_FORM_ref_addr:
ae411497
TT
8244 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8245 and later it is offset sized. */
8246 if (cu->header.version == 2)
8247 info_ptr += cu->header.addr_size;
8248 else
8249 info_ptr += cu->header.offset_size;
8250 break;
36586728
TT
8251 case DW_FORM_GNU_ref_alt:
8252 info_ptr += cu->header.offset_size;
8253 break;
ae411497 8254 case DW_FORM_addr:
4bb7a0a7
DJ
8255 info_ptr += cu->header.addr_size;
8256 break;
8257 case DW_FORM_data1:
8258 case DW_FORM_ref1:
8259 case DW_FORM_flag:
8fe0f950 8260 case DW_FORM_strx1:
4bb7a0a7
DJ
8261 info_ptr += 1;
8262 break;
2dc7f7b3 8263 case DW_FORM_flag_present:
43988095 8264 case DW_FORM_implicit_const:
2dc7f7b3 8265 break;
4bb7a0a7
DJ
8266 case DW_FORM_data2:
8267 case DW_FORM_ref2:
8fe0f950 8268 case DW_FORM_strx2:
4bb7a0a7
DJ
8269 info_ptr += 2;
8270 break;
8fe0f950
AT
8271 case DW_FORM_strx3:
8272 info_ptr += 3;
8273 break;
4bb7a0a7
DJ
8274 case DW_FORM_data4:
8275 case DW_FORM_ref4:
8fe0f950 8276 case DW_FORM_strx4:
4bb7a0a7
DJ
8277 info_ptr += 4;
8278 break;
8279 case DW_FORM_data8:
8280 case DW_FORM_ref8:
55f1336d 8281 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8282 info_ptr += 8;
8283 break;
0224619f
JK
8284 case DW_FORM_data16:
8285 info_ptr += 16;
8286 break;
4bb7a0a7 8287 case DW_FORM_string:
9b1c24c8 8288 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8289 info_ptr += bytes_read;
8290 break;
2dc7f7b3 8291 case DW_FORM_sec_offset:
4bb7a0a7 8292 case DW_FORM_strp:
36586728 8293 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8294 info_ptr += cu->header.offset_size;
8295 break;
2dc7f7b3 8296 case DW_FORM_exprloc:
4bb7a0a7
DJ
8297 case DW_FORM_block:
8298 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8299 info_ptr += bytes_read;
8300 break;
8301 case DW_FORM_block1:
8302 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8303 break;
8304 case DW_FORM_block2:
8305 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8306 break;
8307 case DW_FORM_block4:
8308 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8309 break;
336d760d 8310 case DW_FORM_addrx:
cf532bd1 8311 case DW_FORM_strx:
4bb7a0a7
DJ
8312 case DW_FORM_sdata:
8313 case DW_FORM_udata:
8314 case DW_FORM_ref_udata:
3019eac3
DE
8315 case DW_FORM_GNU_addr_index:
8316 case DW_FORM_GNU_str_index:
18a8505e 8317 case DW_FORM_rnglistx:
41144253 8318 case DW_FORM_loclistx:
d521ce57 8319 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8320 break;
8321 case DW_FORM_indirect:
8322 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8323 info_ptr += bytes_read;
8324 /* We need to continue parsing from here, so just go back to
8325 the top. */
8326 goto skip_attribute;
8327
8328 default:
3e43a32a
MS
8329 error (_("Dwarf Error: Cannot handle %s "
8330 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8331 dwarf_form_name (form),
8332 bfd_get_filename (abfd));
8333 }
8334 }
8335
8336 if (abbrev->has_children)
dee91e82 8337 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8338 else
8339 return info_ptr;
8340}
8341
93311388 8342/* Locate ORIG_PDI's sibling.
dee91e82 8343 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8344
d521ce57 8345static const gdb_byte *
dee91e82
DE
8346locate_pdi_sibling (const struct die_reader_specs *reader,
8347 struct partial_die_info *orig_pdi,
d521ce57 8348 const gdb_byte *info_ptr)
91c24f0a
DC
8349{
8350 /* Do we know the sibling already? */
72bf9492 8351
91c24f0a
DC
8352 if (orig_pdi->sibling)
8353 return orig_pdi->sibling;
8354
8355 /* Are there any children to deal with? */
8356
8357 if (!orig_pdi->has_children)
8358 return info_ptr;
8359
4bb7a0a7 8360 /* Skip the children the long way. */
91c24f0a 8361
dee91e82 8362 return skip_children (reader, info_ptr);
91c24f0a
DC
8363}
8364
257e7a09 8365/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8366 not NULL. */
c906108c 8367
891813be
TT
8368void
8369dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8370{
976ca316 8371 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8372
976ca316 8373 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8374
077cbab2
TT
8375 /* If this psymtab is constructed from a debug-only objfile, the
8376 has_section_at_zero flag will not necessarily be correct. We
8377 can get the correct value for this flag by looking at the data
8378 associated with the (presumably stripped) associated objfile. */
8379 if (objfile->separate_debug_objfile_backlink)
c906108c 8380 {
976ca316 8381 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8382 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8383
976ca316
SM
8384 per_objfile->per_bfd->has_section_at_zero
8385 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8386 }
98bfdba5 8387
8566b89b 8388 expand_psymtab (objfile);
95554aad 8389
976ca316 8390 process_cu_includes (per_objfile);
c906108c 8391}
9cdd5dbd
DE
8392\f
8393/* Reading in full CUs. */
c906108c 8394
10b3939b
DJ
8395/* Add PER_CU to the queue. */
8396
8397static void
120ce1b5
SM
8398queue_comp_unit (dwarf2_per_cu_data *per_cu,
8399 dwarf2_per_objfile *per_objfile,
95554aad 8400 enum language pretend_language)
10b3939b 8401{
10b3939b 8402 per_cu->queued = 1;
08ac5771
SM
8403
8404 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8405 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8406}
8407
616c069a
SM
8408/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8409
89e63ee4
DE
8410 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8411 dependency.
69d751e3 8412
616c069a
SM
8413 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8414 DIEs, false otherwise.
8415
8416 Explanation: there is an invariant that if a CU is queued for expansion
8417 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8418 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8419 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8420 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8421 invariant is respected.
8422
8423 The caller is therefore not required to load the CU's DIEs (we return false)
8424 if:
8425
8426 - the CU is already expanded, and therefore does not get enqueued
8427 - the CU gets enqueued for expansion, but its DIEs are already loaded
8428
8429 Note that the caller should not use this function's return value as an
8430 indicator of whether the CU's DIEs are loaded right now, it should check
8431 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
8432
8433static int
89e63ee4 8434maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8435 dwarf2_per_cu_data *per_cu,
8436 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8437 enum language pretend_language)
8438{
8439 /* We may arrive here during partial symbol reading, if we need full
8440 DIEs to process an unusual case (e.g. template arguments). Do
8441 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8442 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 8443 {
7188ed02
SM
8444 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8445
8446 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
8447 return 1;
8448 return 0;
8449 }
8450
8451 /* Mark the dependence relation so that we don't flush PER_CU
8452 too early. */
89e63ee4 8453 if (dependent_cu != NULL)
347212b8 8454 dependent_cu->add_dependence (per_cu);
0907af0c
DE
8455
8456 /* If it's already on the queue, we have nothing to do. */
8457 if (per_cu->queued)
de53369b
SM
8458 {
8459 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8460 loaded. */
8461 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
8462
8463 /* If the CU is queued for expansion, it should not already be
8464 expanded. */
8465 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8466
8467 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
8468 return 0;
8469 }
0907af0c 8470
616c069a
SM
8471 bool queued = false;
8472 if (!per_objfile->symtab_set_p (per_cu))
8473 {
8474 /* Add it to the queue. */
8475 queue_comp_unit (per_cu, per_objfile, pretend_language);
8476 queued = true;
8477 }
8478
0907af0c
DE
8479 /* If the compilation unit is already loaded, just mark it as
8480 used. */
7188ed02
SM
8481 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8482 if (cu != nullptr)
616c069a 8483 cu->last_used = 0;
0907af0c 8484
616c069a
SM
8485 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8486 and the DIEs are not already loaded. */
8487 return queued && cu == nullptr;
0907af0c
DE
8488}
8489
10b3939b
DJ
8490/* Process the queue. */
8491
8492static void
976ca316 8493process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 8494{
6f738b01
SM
8495 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8496 objfile_name (per_objfile->objfile));
45cfd468 8497
03dd20cc
DJ
8498 /* The queue starts out with one item, but following a DIE reference
8499 may load a new CU, adding it to the end of the queue. */
08ac5771 8500 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 8501 {
08ac5771 8502 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 8503 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 8504
976ca316 8505 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 8506 {
976ca316 8507 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 8508
7188ed02
SM
8509 /* Skip dummy CUs. */
8510 if (cu != nullptr)
73be47f5 8511 {
7188ed02
SM
8512 unsigned int debug_print_threshold;
8513 char buf[100];
8514
8515 if (per_cu->is_debug_types)
8516 {
8517 struct signatured_type *sig_type =
8518 (struct signatured_type *) per_cu;
8519
8520 sprintf (buf, "TU %s at offset %s",
8521 hex_string (sig_type->signature),
8522 sect_offset_str (per_cu->sect_off));
8523 /* There can be 100s of TUs.
8524 Only print them in verbose mode. */
8525 debug_print_threshold = 2;
8526 }
8527 else
8528 {
8529 sprintf (buf, "CU at offset %s",
8530 sect_offset_str (per_cu->sect_off));
8531 debug_print_threshold = 1;
8532 }
247f5c4f 8533
7188ed02 8534 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8535 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 8536
7188ed02
SM
8537 if (per_cu->is_debug_types)
8538 process_full_type_unit (cu, item.pretend_language);
8539 else
8540 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 8541
7188ed02 8542 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8543 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 8544 }
f4dc4d17 8545 }
10b3939b 8546
7188ed02 8547 per_cu->queued = 0;
08ac5771 8548 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
8549 }
8550
6f738b01
SM
8551 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8552 objfile_name (per_objfile->objfile));
10b3939b
DJ
8553}
8554
10b3939b
DJ
8555/* Read in full symbols for PST, and anything it depends on. */
8556
8566b89b
TT
8557void
8558dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8559{
af758d11 8560 gdb_assert (!readin_p (objfile));
95554aad 8561
17ee85fc
TT
8562 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8563 free_cached_comp_units freer (per_objfile);
48993951 8564 expand_dependencies (objfile);
aaa75496 8565
97a1449a 8566 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 8567 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
8568}
8569
af758d11
SM
8570/* See psympriv.h. */
8571
8572bool
8573dwarf2_psymtab::readin_p (struct objfile *objfile) const
8574{
8575 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8576 return per_objfile->symtab_set_p (per_cu_data);
8577}
8578
8579/* See psympriv.h. */
8580
8581compunit_symtab *
8582dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8583{
8584 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8585 return per_objfile->get_symtab (per_cu_data);
8586}
8587
dee91e82
DE
8588/* Trivial hash function for die_info: the hash value of a DIE
8589 is its offset in .debug_info for this objfile. */
10b3939b 8590
dee91e82
DE
8591static hashval_t
8592die_hash (const void *item)
10b3939b 8593{
9a3c8263 8594 const struct die_info *die = (const struct die_info *) item;
6502dd73 8595
9c541725 8596 return to_underlying (die->sect_off);
dee91e82 8597}
63d06c5c 8598
dee91e82
DE
8599/* Trivial comparison function for die_info structures: two DIEs
8600 are equal if they have the same offset. */
98bfdba5 8601
dee91e82
DE
8602static int
8603die_eq (const void *item_lhs, const void *item_rhs)
8604{
9a3c8263
SM
8605 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8606 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8607
9c541725 8608 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8609}
c906108c 8610
4a636814
SM
8611/* Load the DIEs associated with PER_CU into memory.
8612
8613 In some cases, the caller, while reading partial symbols, will need to load
8614 the full symbols for the CU for some reason. It will already have a
8615 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8616 rather than creating a new one. */
c906108c 8617
dee91e82 8618static void
ab432490
SM
8619load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8620 dwarf2_per_objfile *per_objfile,
4a636814 8621 dwarf2_cu *existing_cu,
c0ab21c2
TT
8622 bool skip_partial,
8623 enum language pretend_language)
dee91e82 8624{
c0ab21c2
TT
8625 gdb_assert (! this_cu->is_debug_types);
8626
7188ed02 8627 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
8628 if (reader.dummy_p)
8629 return;
8630
8631 struct dwarf2_cu *cu = reader.cu;
8632 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8633
dee91e82
DE
8634 gdb_assert (cu->die_hash == NULL);
8635 cu->die_hash =
8636 htab_create_alloc_ex (cu->header.length / 12,
8637 die_hash,
8638 die_eq,
8639 NULL,
8640 &cu->comp_unit_obstack,
8641 hashtab_obstack_allocate,
8642 dummy_obstack_deallocate);
e142c38c 8643
3e225074 8644 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8645 reader.comp_unit_die->child
8646 = read_die_and_siblings (&reader, reader.info_ptr,
8647 &info_ptr, reader.comp_unit_die);
8648 cu->dies = reader.comp_unit_die;
dee91e82 8649 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8650
8651 /* We try not to read any attributes in this function, because not
9cdd5dbd 8652 all CUs needed for references have been loaded yet, and symbol
10b3939b 8653 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8654 or we won't be able to build types correctly.
8655 Similarly, if we do not read the producer, we can not apply
8656 producer-specific interpretation. */
c0ab21c2 8657 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8658
8659 reader.keep ();
10b3939b
DJ
8660}
8661
3da10d80
KS
8662/* Add a DIE to the delayed physname list. */
8663
8664static void
8665add_to_method_list (struct type *type, int fnfield_index, int index,
8666 const char *name, struct die_info *die,
8667 struct dwarf2_cu *cu)
8668{
8669 struct delayed_method_info mi;
8670 mi.type = type;
8671 mi.fnfield_index = fnfield_index;
8672 mi.index = index;
8673 mi.name = name;
8674 mi.die = die;
c89b44cd 8675 cu->method_list.push_back (mi);
3da10d80
KS
8676}
8677
3693fdb3
PA
8678/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8679 "const" / "volatile". If so, decrements LEN by the length of the
8680 modifier and return true. Otherwise return false. */
8681
8682template<size_t N>
8683static bool
8684check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8685{
8686 size_t mod_len = sizeof (mod) - 1;
8687 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8688 {
8689 len -= mod_len;
8690 return true;
8691 }
8692 return false;
8693}
8694
3da10d80
KS
8695/* Compute the physnames of any methods on the CU's method list.
8696
8697 The computation of method physnames is delayed in order to avoid the
8698 (bad) condition that one of the method's formal parameters is of an as yet
8699 incomplete type. */
8700
8701static void
8702compute_delayed_physnames (struct dwarf2_cu *cu)
8703{
3693fdb3 8704 /* Only C++ delays computing physnames. */
c89b44cd 8705 if (cu->method_list.empty ())
3693fdb3 8706 return;
3e9f1ca1 8707 gdb_assert (cu->per_cu->lang == language_cplus);
3693fdb3 8708
52941706 8709 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8710 {
1d06ead6 8711 const char *physname;
3da10d80 8712 struct fn_fieldlist *fn_flp
c89b44cd
TT
8713 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8714 physname = dwarf2_physname (mi.name, mi.die, cu);
8715 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8716 = physname ? physname : "";
3693fdb3
PA
8717
8718 /* Since there's no tag to indicate whether a method is a
8719 const/volatile overload, extract that information out of the
8720 demangled name. */
8721 if (physname != NULL)
8722 {
8723 size_t len = strlen (physname);
8724
8725 while (1)
8726 {
8727 if (physname[len] == ')') /* shortcut */
8728 break;
8729 else if (check_modifier (physname, len, " const"))
c89b44cd 8730 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 8731 else if (check_modifier (physname, len, " volatile"))
c89b44cd 8732 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
8733 else
8734 break;
8735 }
8736 }
3da10d80 8737 }
c89b44cd
TT
8738
8739 /* The list is no longer needed. */
8740 cu->method_list.clear ();
3da10d80
KS
8741}
8742
a766d390
DE
8743/* Go objects should be embedded in a DW_TAG_module DIE,
8744 and it's not clear if/how imported objects will appear.
8745 To keep Go support simple until that's worked out,
8746 go back through what we've read and create something usable.
8747 We could do this while processing each DIE, and feels kinda cleaner,
8748 but that way is more invasive.
8749 This is to, for example, allow the user to type "p var" or "b main"
8750 without having to specify the package name, and allow lookups
8751 of module.object to work in contexts that use the expression
8752 parser. */
8753
8754static void
8755fixup_go_packaging (struct dwarf2_cu *cu)
8756{
421d1616 8757 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
8758 struct pending *list;
8759 int i;
8760
c24bdb02 8761 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
8762 list != NULL;
8763 list = list->next)
a766d390
DE
8764 {
8765 for (i = 0; i < list->nsyms; ++i)
8766 {
8767 struct symbol *sym = list->symbol[i];
8768
c1b5c1eb 8769 if (sym->language () == language_go
a766d390
DE
8770 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8771 {
421d1616
TT
8772 gdb::unique_xmalloc_ptr<char> this_package_name
8773 (go_symbol_package_name (sym));
a766d390
DE
8774
8775 if (this_package_name == NULL)
8776 continue;
8777 if (package_name == NULL)
421d1616 8778 package_name = std::move (this_package_name);
a766d390
DE
8779 else
8780 {
5e22e966 8781 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 8782 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 8783 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8784 (symbol_symtab (sym) != NULL
8785 ? symtab_to_filename_for_display
8786 (symbol_symtab (sym))
e3b94546 8787 : objfile_name (objfile)),
421d1616 8788 this_package_name.get (), package_name.get ());
a766d390
DE
8789 }
8790 }
8791 }
8792 }
8793
8794 if (package_name != NULL)
8795 {
5e22e966 8796 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 8797 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
8798 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8799 saved_package_name);
a766d390
DE
8800 struct symbol *sym;
8801
8c14c3a3 8802 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 8803 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 8804 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
8805 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8806 e.g., "main" finds the "main" module and not C's main(). */
8807 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8808 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8809 SYMBOL_TYPE (sym) = type;
8810
c24bdb02 8811 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
8812 }
8813}
8814
c9317f21
TT
8815/* Allocate a fully-qualified name consisting of the two parts on the
8816 obstack. */
8817
8818static const char *
8819rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8820{
8821 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8822}
8823
9c6a1327
TT
8824/* A helper that allocates a variant part to attach to a Rust enum
8825 type. OBSTACK is where the results should be allocated. TYPE is
8826 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
8827 discriminant. It must be the index of one of the fields of TYPE,
8828 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
8829 DEFAULT_INDEX is the index of the default field; or -1 if there is
8830 no default. RANGES is indexed by "effective" field number (the
8831 field index, but omitting the discriminant and default fields) and
8832 must hold the discriminant values used by the variants. Note that
8833 RANGES must have a lifetime at least as long as OBSTACK -- either
8834 already allocated on it, or static. */
c9317f21 8835
9c6a1327
TT
8836static void
8837alloc_rust_variant (struct obstack *obstack, struct type *type,
8838 int discriminant_index, int default_index,
8839 gdb::array_view<discriminant_range> ranges)
8840{
57d02173
TT
8841 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8842 gdb_assert (discriminant_index == -1
8843 || (discriminant_index >= 0
8844 && discriminant_index < type->num_fields ()));
c9317f21 8845 gdb_assert (default_index == -1
1f704f76 8846 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 8847
9c6a1327 8848 /* We have one variant for each non-discriminant field. */
57d02173
TT
8849 int n_variants = type->num_fields ();
8850 if (discriminant_index != -1)
8851 --n_variants;
c9317f21 8852
9c6a1327
TT
8853 variant *variants = new (obstack) variant[n_variants];
8854 int var_idx = 0;
8855 int range_idx = 0;
1f704f76 8856 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
8857 {
8858 if (i == discriminant_index)
8859 continue;
c9317f21 8860
9c6a1327
TT
8861 variants[var_idx].first_field = i;
8862 variants[var_idx].last_field = i + 1;
8863
8864 /* The default field does not need a range, but other fields do.
8865 We skipped the discriminant above. */
8866 if (i != default_index)
8867 {
8868 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8869 ++range_idx;
8870 }
c9317f21 8871
9c6a1327
TT
8872 ++var_idx;
8873 }
8874
8875 gdb_assert (range_idx == ranges.size ());
8876 gdb_assert (var_idx == n_variants);
8877
8878 variant_part *part = new (obstack) variant_part;
8879 part->discriminant_index = discriminant_index;
57d02173
TT
8880 /* If there is no discriminant, then whether it is signed is of no
8881 consequence. */
8882 part->is_unsigned
8883 = (discriminant_index == -1
8884 ? false
c6d940a9 8885 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
8886 part->variants = gdb::array_view<variant> (variants, n_variants);
8887
8888 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
8889 gdb::array_view<variant_part> *prop_value
8890 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 8891
9c6a1327 8892 struct dynamic_prop prop;
8c2e4e06 8893 prop.set_variant_parts (prop_value);
9c6a1327 8894
5c54719c 8895 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
8896}
8897
8898/* Some versions of rustc emitted enums in an unusual way.
8899
8900 Ordinary enums were emitted as unions. The first element of each
8901 structure in the union was named "RUST$ENUM$DISR". This element
8902 held the discriminant.
8903
8904 These versions of Rust also implemented the "non-zero"
8905 optimization. When the enum had two values, and one is empty and
8906 the other holds a pointer that cannot be zero, the pointer is used
8907 as the discriminant, with a zero value meaning the empty variant.
8908 Here, the union's first member is of the form
8909 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
8910 where the fieldnos are the indices of the fields that should be
8911 traversed in order to find the field (which may be several fields deep)
8912 and the variantname is the name of the variant of the case when the
8913 field is zero.
8914
8915 This function recognizes whether TYPE is of one of these forms,
8916 and, if so, smashes it to be a variant type. */
8917
8918static void
8919quirk_rust_enum (struct type *type, struct objfile *objfile)
8920{
78134374 8921 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
8922
8923 /* We don't need to deal with empty enums. */
1f704f76 8924 if (type->num_fields () == 0)
c9317f21
TT
8925 return;
8926
8927#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 8928 if (type->num_fields () == 1
c9317f21
TT
8929 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
8930 {
8931 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
8932
8933 /* Decode the field name to find the offset of the
8934 discriminant. */
8935 ULONGEST bit_offset = 0;
940da03e 8936 struct type *field_type = type->field (0).type ();
c9317f21
TT
8937 while (name[0] >= '0' && name[0] <= '9')
8938 {
8939 char *tail;
8940 unsigned long index = strtoul (name, &tail, 10);
8941 name = tail;
8942 if (*name != '$'
1f704f76 8943 || index >= field_type->num_fields ()
c9317f21
TT
8944 || (TYPE_FIELD_LOC_KIND (field_type, index)
8945 != FIELD_LOC_KIND_BITPOS))
8946 {
b98664d3 8947 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
8948 "[in module %s]"),
8949 TYPE_FIELD_NAME (type, 0),
8950 objfile_name (objfile));
8951 return;
8952 }
8953 ++name;
8954
8955 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 8956 field_type = field_type->field (index).type ();
c9317f21
TT
8957 }
8958
9c6a1327
TT
8959 /* Smash this type to be a structure type. We have to do this
8960 because the type has already been recorded. */
67607e24 8961 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 8962 type->set_num_fields (3);
9c6a1327 8963 /* Save the field we care about. */
ceacbf6e 8964 struct field saved_field = type->field (0);
3cabb6b0
SM
8965 type->set_fields
8966 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 8967
9c6a1327 8968 /* Put the discriminant at index 0. */
5d14b6e5 8969 type->field (0).set_type (field_type);
9c6a1327
TT
8970 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8971 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 8972 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
8973
8974 /* The order of fields doesn't really matter, so put the real
8975 field at index 1 and the data-less field at index 2. */
ceacbf6e 8976 type->field (1) = saved_field;
9c6a1327 8977 TYPE_FIELD_NAME (type, 1)
940da03e
SM
8978 = rust_last_path_segment (type->field (1).type ()->name ());
8979 type->field (1).type ()->set_name
7d93a1e0 8980 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 8981 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
8982
8983 const char *dataless_name
7d93a1e0 8984 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
8985 name);
8986 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8987 dataless_name);
5d14b6e5 8988 type->field (2).set_type (dataless_type);
c9317f21
TT
8989 /* NAME points into the original discriminant name, which
8990 already has the correct lifetime. */
9c6a1327 8991 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 8992 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 8993
9c6a1327
TT
8994 /* Indicate that this is a variant type. */
8995 static discriminant_range ranges[1] = { { 0, 0 } };
8996 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 8997 }
77c2dba3
TT
8998 /* A union with a single anonymous field is probably an old-style
8999 univariant enum. */
1f704f76 9000 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9001 {
c9317f21
TT
9002 /* Smash this type to be a structure type. We have to do this
9003 because the type has already been recorded. */
67607e24 9004 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9005
940da03e 9006 struct type *field_type = type->field (0).type ();
c9317f21 9007 const char *variant_name
7d93a1e0 9008 = rust_last_path_segment (field_type->name ());
9c6a1327 9009 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9010 field_type->set_name
9011 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9012 type->name (), variant_name));
57d02173
TT
9013
9014 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9015 }
9016 else
9017 {
9018 struct type *disr_type = nullptr;
1f704f76 9019 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9020 {
940da03e 9021 disr_type = type->field (i).type ();
c9317f21 9022
78134374 9023 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9024 {
9025 /* All fields of a true enum will be structs. */
9026 return;
9027 }
1f704f76 9028 else if (disr_type->num_fields () == 0)
c9317f21
TT
9029 {
9030 /* Could be data-less variant, so keep going. */
a037790e 9031 disr_type = nullptr;
c9317f21
TT
9032 }
9033 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9034 "RUST$ENUM$DISR") != 0)
9035 {
9036 /* Not a Rust enum. */
9037 return;
9038 }
9039 else
9040 {
9041 /* Found one. */
9042 break;
9043 }
9044 }
9045
9046 /* If we got here without a discriminant, then it's probably
9047 just a union. */
9048 if (disr_type == nullptr)
9049 return;
9050
9051 /* Smash this type to be a structure type. We have to do this
9052 because the type has already been recorded. */
67607e24 9053 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9054
9c6a1327 9055 /* Make space for the discriminant field. */
ceacbf6e 9056 struct field *disr_field = &disr_type->field (0);
9c6a1327 9057 field *new_fields
1f704f76 9058 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9059 * sizeof (struct field)));
80fc5e77 9060 memcpy (new_fields + 1, type->fields (),
1f704f76 9061 type->num_fields () * sizeof (struct field));
3cabb6b0 9062 type->set_fields (new_fields);
1f704f76 9063 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9064
9065 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9066 type->field (0) = *disr_field;
9c6a1327
TT
9067 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9068 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9069
9070 /* We need a way to find the correct discriminant given a
9071 variant name. For convenience we build a map here. */
b6cdac4b 9072 struct type *enum_type = disr_field->type ();
c9317f21 9073 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9074 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9075 {
9076 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9077 {
9078 const char *name
9079 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9080 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9081 }
9082 }
9083
1f704f76 9084 int n_fields = type->num_fields ();
9c6a1327
TT
9085 /* We don't need a range entry for the discriminant, but we do
9086 need one for every other field, as there is no default
9087 variant. */
9088 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9089 discriminant_range,
9090 n_fields - 1);
c9317f21
TT
9091 /* Skip the discriminant here. */
9092 for (int i = 1; i < n_fields; ++i)
9093 {
9094 /* Find the final word in the name of this variant's type.
9095 That name can be used to look up the correct
9096 discriminant. */
9097 const char *variant_name
940da03e 9098 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9099
9100 auto iter = discriminant_map.find (variant_name);
9101 if (iter != discriminant_map.end ())
9c6a1327 9102 {
57d02173
TT
9103 ranges[i - 1].low = iter->second;
9104 ranges[i - 1].high = iter->second;
9c6a1327 9105 }
c9317f21 9106
57d02173
TT
9107 /* In Rust, each element should have the size of the
9108 enclosing enum. */
9109 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9110
bedda9ac 9111 /* Remove the discriminant field, if it exists. */
940da03e 9112 struct type *sub_type = type->field (i).type ();
1f704f76 9113 if (sub_type->num_fields () > 0)
bedda9ac 9114 {
5e33d5f4 9115 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9116 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9117 }
9c6a1327 9118 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9119 sub_type->set_name
9120 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9121 type->name (), variant_name));
c9317f21 9122 }
9c6a1327
TT
9123
9124 /* Indicate that this is a variant type. */
a1520ad8 9125 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9126 gdb::array_view<discriminant_range> (ranges,
9127 n_fields - 1));
c9317f21
TT
9128 }
9129}
9130
9131/* Rewrite some Rust unions to be structures with variants parts. */
9132
9133static void
9134rust_union_quirks (struct dwarf2_cu *cu)
9135{
3e9f1ca1 9136 gdb_assert (cu->per_cu->lang == language_rust);
52941706 9137 for (type *type_ : cu->rust_unions)
5e22e966 9138 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9139 /* We don't need this any more. */
9140 cu->rust_unions.clear ();
c9317f21
TT
9141}
9142
8adb8487
TT
9143/* See read.h. */
9144
9145type_unit_group_unshareable *
9146dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9147{
9148 auto iter = this->m_type_units.find (tu_group);
9149 if (iter != this->m_type_units.end ())
9150 return iter->second.get ();
9151
9152 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9153 type_unit_group_unshareable *result = uniq.get ();
9154 this->m_type_units[tu_group] = std::move (uniq);
9155 return result;
9156}
9157
e286671b
TT
9158struct type *
9159dwarf2_per_objfile::get_type_for_signatured_type
9160 (signatured_type *sig_type) const
9161{
9162 auto iter = this->m_type_map.find (sig_type);
9163 if (iter == this->m_type_map.end ())
9164 return nullptr;
9165
9166 return iter->second;
9167}
9168
9169void dwarf2_per_objfile::set_type_for_signatured_type
9170 (signatured_type *sig_type, struct type *type)
9171{
9172 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9173
9174 this->m_type_map[sig_type] = type;
9175}
9176
95554aad
TT
9177/* A helper function for computing the list of all symbol tables
9178 included by PER_CU. */
9179
9180static void
4c39bc03 9181recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9182 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9183 dwarf2_per_cu_data *per_cu,
9184 dwarf2_per_objfile *per_objfile,
43f3e411 9185 struct compunit_symtab *immediate_parent)
95554aad 9186{
af758d11 9187 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9188 if (*slot != NULL)
9189 {
9190 /* This inclusion and its children have been processed. */
9191 return;
9192 }
9193
9194 *slot = per_cu;
af758d11 9195
95554aad 9196 /* Only add a CU if it has a symbol table. */
43182c09 9197 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9198 if (cust != NULL)
ec94af83
DE
9199 {
9200 /* If this is a type unit only add its symbol table if we haven't
9201 seen it yet (type unit per_cu's can share symtabs). */
9202 if (per_cu->is_debug_types)
9203 {
43f3e411 9204 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9205 if (*slot == NULL)
9206 {
43f3e411 9207 *slot = cust;
4c39bc03 9208 result->push_back (cust);
43f3e411
DE
9209 if (cust->user == NULL)
9210 cust->user = immediate_parent;
ec94af83
DE
9211 }
9212 }
9213 else
f9125b6c 9214 {
4c39bc03 9215 result->push_back (cust);
43f3e411
DE
9216 if (cust->user == NULL)
9217 cust->user = immediate_parent;
f9125b6c 9218 }
ec94af83 9219 }
95554aad 9220
ae640021
AB
9221 if (!per_cu->imported_symtabs_empty ())
9222 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9223 {
9224 recursively_compute_inclusions (result, all_children,
43182c09
SM
9225 all_type_symtabs, ptr, per_objfile,
9226 cust);
ae640021 9227 }
95554aad
TT
9228}
9229
43f3e411 9230/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9231 PER_CU. */
9232
9233static void
43182c09
SM
9234compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9235 dwarf2_per_objfile *per_objfile)
95554aad 9236{
f4dc4d17
DE
9237 gdb_assert (! per_cu->is_debug_types);
9238
ae640021 9239 if (!per_cu->imported_symtabs_empty ())
95554aad 9240 {
ae640021 9241 int len;
4c39bc03 9242 std::vector<compunit_symtab *> result_symtabs;
43182c09 9243 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9244
9245 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9246 if (cust == NULL)
95554aad
TT
9247 return;
9248
280a9412
TT
9249 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9250 htab_eq_pointer,
9251 NULL, xcalloc, xfree));
9252 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9253 htab_eq_pointer,
9254 NULL, xcalloc, xfree));
95554aad 9255
ae640021 9256 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9257 {
280a9412
TT
9258 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9259 all_type_symtabs.get (), ptr,
9260 per_objfile, cust);
ec94af83 9261 }
95554aad 9262
ec94af83 9263 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9264 len = result_symtabs.size ();
43f3e411 9265 cust->includes
f6e649dd 9266 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9267 struct compunit_symtab *, len + 1);
4c39bc03
TT
9268 memcpy (cust->includes, result_symtabs.data (),
9269 len * sizeof (compunit_symtab *));
43f3e411 9270 cust->includes[len] = NULL;
95554aad
TT
9271 }
9272}
9273
9274/* Compute the 'includes' field for the symtabs of all the CUs we just
9275 read. */
9276
9277static void
976ca316 9278process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9279{
976ca316 9280 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9281 {
9282 if (! iter->is_debug_types)
976ca316 9283 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9284 }
95554aad 9285
976ca316 9286 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9287}
9288
8fc0b21d 9289/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9290 already been loaded into memory. */
9291
9292static void
8fc0b21d 9293process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9294{
976ca316
SM
9295 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9296 struct objfile *objfile = per_objfile->objfile;
08feed99 9297 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9298 CORE_ADDR lowpc, highpc;
43f3e411 9299 struct compunit_symtab *cust;
10b3939b 9300 CORE_ADDR baseaddr;
4359dff1 9301 struct block *static_block;
3e29f34a 9302 CORE_ADDR addr;
10b3939b 9303
b3b3bada 9304 baseaddr = objfile->text_section_offset ();
10b3939b 9305
c89b44cd
TT
9306 /* Clear the list here in case something was left over. */
9307 cu->method_list.clear ();
10b3939b 9308
1c47ec3e
TV
9309 dwarf2_find_base_address (cu->dies, cu);
9310
702cf3f5
AB
9311 /* Before we start reading the top-level DIE, ensure it has a valid tag
9312 type. */
9313 switch (cu->dies->tag)
9314 {
9315 case DW_TAG_compile_unit:
9316 case DW_TAG_partial_unit:
9317 case DW_TAG_type_unit:
9318 break;
9319 default:
9320 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9321 dwarf_tag_name (cu->dies->tag),
9322 sect_offset_str (cu->per_cu->sect_off),
9323 objfile_name (per_objfile->objfile));
9324 }
9325
c906108c 9326 /* Do line number decoding in read_file_scope () */
10b3939b 9327 process_die (cu->dies, cu);
c906108c 9328
a766d390 9329 /* For now fudge the Go package. */
3e9f1ca1 9330 if (cu->per_cu->lang == language_go)
a766d390
DE
9331 fixup_go_packaging (cu);
9332
5f48f8f3 9333 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9334 should be complete, and it should now be safe to compute all of the
9335 physnames. */
9336 compute_delayed_physnames (cu);
3da10d80 9337
3e9f1ca1 9338 if (cu->per_cu->lang == language_rust)
c9317f21
TT
9339 rust_union_quirks (cu);
9340
fae299cd
DC
9341 /* Some compilers don't define a DW_AT_high_pc attribute for the
9342 compilation unit. If the DW_AT_high_pc is missing, synthesize
9343 it, by scanning the DIE's below the compilation unit. */
10b3939b 9344 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9345
3e29f34a 9346 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9347 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9348
9349 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9350 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9351 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9352 addrmap to help ensure it has an accurate map of pc values belonging to
9353 this comp unit. */
9354 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9355
c24bdb02 9356 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9357 SECT_OFF_TEXT (objfile),
9358 0);
c906108c 9359
43f3e411 9360 if (cust != NULL)
c906108c 9361 {
df15bd07 9362 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9363
8be455d7
JK
9364 /* Set symtab language to language from DW_AT_language. If the
9365 compilation is from a C file generated by language preprocessors, do
9366 not set the language if it was already deduced by start_subfile. */
3e9f1ca1 9367 if (!(cu->per_cu->lang == language_c
40e3ad0e 9368 && COMPUNIT_FILETABS (cust)->language != language_unknown))
3e9f1ca1 9369 COMPUNIT_FILETABS (cust)->language = cu->per_cu->lang;
8be455d7
JK
9370
9371 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9372 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9373 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9374 there were bugs in prologue debug info, fixed later in GCC-4.5
9375 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9376
9377 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9378 needed, it would be wrong due to missing DW_AT_producer there.
9379
9380 Still one can confuse GDB by using non-standard GCC compilation
9381 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9382 */
ab260dad 9383 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9384 cust->locations_valid = 1;
e0d00bc7
JK
9385
9386 if (gcc_4_minor >= 5)
43f3e411 9387 cust->epilogue_unwind_valid = 1;
96408a79 9388
43f3e411 9389 cust->call_site_htab = cu->call_site_htab;
c906108c 9390 }
9291a0cd 9391
976ca316 9392 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9393
95554aad 9394 /* Push it for inclusion processing later. */
976ca316 9395 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9396
9397 /* Not needed any more. */
c24bdb02 9398 cu->reset_builder ();
f4dc4d17 9399}
45cfd468 9400
8fc0b21d 9401/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9402 already been loaded into memory. */
9403
9404static void
8fc0b21d 9405process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9406 enum language pretend_language)
9407{
976ca316
SM
9408 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9409 struct objfile *objfile = per_objfile->objfile;
43f3e411 9410 struct compunit_symtab *cust;
0186c6a7
DE
9411 struct signatured_type *sig_type;
9412
8fc0b21d
SM
9413 gdb_assert (cu->per_cu->is_debug_types);
9414 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9415
c89b44cd
TT
9416 /* Clear the list here in case something was left over. */
9417 cu->method_list.clear ();
f4dc4d17 9418
f4dc4d17
DE
9419 /* The symbol tables are set up in read_type_unit_scope. */
9420 process_die (cu->dies, cu);
9421
9422 /* For now fudge the Go package. */
3e9f1ca1 9423 if (cu->per_cu->lang == language_go)
f4dc4d17
DE
9424 fixup_go_packaging (cu);
9425
5f48f8f3 9426 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9427 should be complete, and it should now be safe to compute all of the
9428 physnames. */
9429 compute_delayed_physnames (cu);
f4dc4d17 9430
3e9f1ca1 9431 if (cu->per_cu->lang == language_rust)
c9317f21
TT
9432 rust_union_quirks (cu);
9433
f4dc4d17
DE
9434 /* TUs share symbol tables.
9435 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9436 of it with end_expandable_symtab. Otherwise, complete the addition of
9437 this TU's symbols to the existing symtab. */
8adb8487 9438 type_unit_group_unshareable *tug_unshare =
976ca316 9439 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9440 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9441 {
c24bdb02
KS
9442 buildsym_compunit *builder = cu->get_builder ();
9443 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9444 tug_unshare->compunit_symtab = cust;
f4dc4d17 9445
43f3e411 9446 if (cust != NULL)
f4dc4d17
DE
9447 {
9448 /* Set symtab language to language from DW_AT_language. If the
9449 compilation is from a C file generated by language preprocessors,
9450 do not set the language if it was already deduced by
9451 start_subfile. */
3e9f1ca1 9452 if (!(cu->per_cu->lang == language_c
43f3e411 9453 && COMPUNIT_FILETABS (cust)->language != language_c))
3e9f1ca1 9454 COMPUNIT_FILETABS (cust)->language = cu->per_cu->lang;
f4dc4d17
DE
9455 }
9456 }
9457 else
9458 {
c24bdb02 9459 cu->get_builder ()->augment_type_symtab ();
8adb8487 9460 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9461 }
9462
976ca316 9463 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9464
9465 /* Not needed any more. */
c24bdb02 9466 cu->reset_builder ();
c906108c
SS
9467}
9468
95554aad
TT
9469/* Process an imported unit DIE. */
9470
9471static void
9472process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9473{
9474 struct attribute *attr;
9475
f4dc4d17
DE
9476 /* For now we don't handle imported units in type units. */
9477 if (cu->per_cu->is_debug_types)
9478 {
9479 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9480 " supported in type units [in module %s]"),
5e22e966 9481 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9482 }
9483
95554aad
TT
9484 attr = dwarf2_attr (die, DW_AT_import, cu);
9485 if (attr != NULL)
9486 {
0826b30a 9487 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9488 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9489 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9490 dwarf2_per_cu_data *per_cu
ab432490 9491 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9492
58990295
TV
9493 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9494 into another compilation unit, at root level. Regard this as a hint,
9495 and ignore it. */
9496 if (die->parent && die->parent->parent == NULL
9497 && per_cu->unit_type == DW_UT_compile
9498 && per_cu->lang == language_cplus)
9499 return;
9500
69d751e3 9501 /* If necessary, add it to the queue and load its DIEs. */
3e9f1ca1
TT
9502 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
9503 cu->per_cu->lang))
4a636814 9504 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
3e9f1ca1 9505 false, cu->per_cu->lang);
95554aad 9506
ae640021 9507 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9508 }
9509}
9510
4c8aa72d
PA
9511/* RAII object that represents a process_die scope: i.e.,
9512 starts/finishes processing a DIE. */
9513class process_die_scope
adde2bff 9514{
4c8aa72d
PA
9515public:
9516 process_die_scope (die_info *die, dwarf2_cu *cu)
9517 : m_die (die), m_cu (cu)
9518 {
9519 /* We should only be processing DIEs not already in process. */
9520 gdb_assert (!m_die->in_process);
9521 m_die->in_process = true;
9522 }
8c3cb9fa 9523
4c8aa72d
PA
9524 ~process_die_scope ()
9525 {
9526 m_die->in_process = false;
9527
9528 /* If we're done processing the DIE for the CU that owns the line
9529 header, we don't need the line header anymore. */
9530 if (m_cu->line_header_die_owner == m_die)
9531 {
9532 delete m_cu->line_header;
9533 m_cu->line_header = NULL;
9534 m_cu->line_header_die_owner = NULL;
9535 }
9536 }
9537
9538private:
9539 die_info *m_die;
9540 dwarf2_cu *m_cu;
9541};
adde2bff 9542
c906108c
SS
9543/* Process a die and its children. */
9544
9545static void
e7c27a73 9546process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9547{
4c8aa72d 9548 process_die_scope scope (die, cu);
adde2bff 9549
c906108c
SS
9550 switch (die->tag)
9551 {
9552 case DW_TAG_padding:
9553 break;
9554 case DW_TAG_compile_unit:
95554aad 9555 case DW_TAG_partial_unit:
e7c27a73 9556 read_file_scope (die, cu);
c906108c 9557 break;
348e048f
DE
9558 case DW_TAG_type_unit:
9559 read_type_unit_scope (die, cu);
9560 break;
c906108c 9561 case DW_TAG_subprogram:
0a4b0913 9562 /* Nested subprograms in Fortran get a prefix. */
3e9f1ca1 9563 if (cu->per_cu->lang == language_fortran
0a4b0913
AB
9564 && die->parent != NULL
9565 && die->parent->tag == DW_TAG_subprogram)
9566 cu->processing_has_namespace_info = true;
9567 /* Fall through. */
c906108c 9568 case DW_TAG_inlined_subroutine:
edb3359d 9569 read_func_scope (die, cu);
c906108c
SS
9570 break;
9571 case DW_TAG_lexical_block:
14898363
L
9572 case DW_TAG_try_block:
9573 case DW_TAG_catch_block:
e7c27a73 9574 read_lexical_block_scope (die, cu);
c906108c 9575 break;
216f72a1 9576 case DW_TAG_call_site:
96408a79
SA
9577 case DW_TAG_GNU_call_site:
9578 read_call_site_scope (die, cu);
9579 break;
c906108c 9580 case DW_TAG_class_type:
680b30c7 9581 case DW_TAG_interface_type:
c906108c
SS
9582 case DW_TAG_structure_type:
9583 case DW_TAG_union_type:
134d01f1 9584 process_structure_scope (die, cu);
c906108c
SS
9585 break;
9586 case DW_TAG_enumeration_type:
134d01f1 9587 process_enumeration_scope (die, cu);
c906108c 9588 break;
134d01f1 9589
f792889a
DJ
9590 /* These dies have a type, but processing them does not create
9591 a symbol or recurse to process the children. Therefore we can
9592 read them on-demand through read_type_die. */
c906108c 9593 case DW_TAG_subroutine_type:
72019c9c 9594 case DW_TAG_set_type:
c906108c 9595 case DW_TAG_pointer_type:
c906108c 9596 case DW_TAG_ptr_to_member_type:
c906108c 9597 case DW_TAG_reference_type:
4297a3f0 9598 case DW_TAG_rvalue_reference_type:
c906108c 9599 case DW_TAG_string_type:
c906108c 9600 break;
134d01f1 9601
d8f62e84
TT
9602 case DW_TAG_array_type:
9603 /* We only need to handle this case for Ada -- in other
9604 languages, it's normal for the compiler to emit a typedef
9605 instead. */
3e9f1ca1 9606 if (cu->per_cu->lang != language_ada)
d8f62e84
TT
9607 break;
9608 /* FALLTHROUGH */
c906108c 9609 case DW_TAG_base_type:
a02abb62 9610 case DW_TAG_subrange_type:
cb249c71 9611 case DW_TAG_typedef:
134d01f1 9612 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 9613 DW_AT_name. */
f792889a 9614 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9615 break;
c906108c 9616 case DW_TAG_common_block:
e7c27a73 9617 read_common_block (die, cu);
c906108c
SS
9618 break;
9619 case DW_TAG_common_inclusion:
9620 break;
d9fa45fe 9621 case DW_TAG_namespace:
9068261f 9622 cu->processing_has_namespace_info = true;
e7c27a73 9623 read_namespace (die, cu);
d9fa45fe 9624 break;
5d7cb8df 9625 case DW_TAG_module:
9068261f 9626 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9627 read_module (die, cu);
9628 break;
d9fa45fe 9629 case DW_TAG_imported_declaration:
9068261f 9630 cu->processing_has_namespace_info = true;
74921315
KS
9631 if (read_namespace_alias (die, cu))
9632 break;
86a73007
TT
9633 /* The declaration is not a global namespace alias. */
9634 /* Fall through. */
d9fa45fe 9635 case DW_TAG_imported_module:
9068261f 9636 cu->processing_has_namespace_info = true;
27aa8d6a 9637 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
3e9f1ca1 9638 || cu->per_cu->lang != language_fortran))
b98664d3 9639 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9640 dwarf_tag_name (die->tag));
9641 read_import_statement (die, cu);
d9fa45fe 9642 break;
95554aad
TT
9643
9644 case DW_TAG_imported_unit:
9645 process_imported_unit_die (die, cu);
9646 break;
9647
71a3c369
TT
9648 case DW_TAG_variable:
9649 read_variable (die, cu);
9650 break;
9651
c906108c 9652 default:
e7c27a73 9653 new_symbol (die, NULL, cu);
c906108c
SS
9654 break;
9655 }
9656}
ca69b9e6
DE
9657\f
9658/* DWARF name computation. */
c906108c 9659
94af9270
KS
9660/* A helper function for dwarf2_compute_name which determines whether DIE
9661 needs to have the name of the scope prepended to the name listed in the
9662 die. */
9663
9664static int
9665die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9666{
1c809c68
TT
9667 struct attribute *attr;
9668
94af9270
KS
9669 switch (die->tag)
9670 {
9671 case DW_TAG_namespace:
9672 case DW_TAG_typedef:
9673 case DW_TAG_class_type:
9674 case DW_TAG_interface_type:
9675 case DW_TAG_structure_type:
9676 case DW_TAG_union_type:
9677 case DW_TAG_enumeration_type:
9678 case DW_TAG_enumerator:
9679 case DW_TAG_subprogram:
08a76f8a 9680 case DW_TAG_inlined_subroutine:
94af9270 9681 case DW_TAG_member:
74921315 9682 case DW_TAG_imported_declaration:
94af9270
KS
9683 return 1;
9684
9685 case DW_TAG_variable:
c2b0a229 9686 case DW_TAG_constant:
94af9270
KS
9687 /* We only need to prefix "globally" visible variables. These include
9688 any variable marked with DW_AT_external or any variable that
9689 lives in a namespace. [Variables in anonymous namespaces
9690 require prefixing, but they are not DW_AT_external.] */
9691
9692 if (dwarf2_attr (die, DW_AT_specification, cu))
9693 {
9694 struct dwarf2_cu *spec_cu = cu;
9a619af0 9695
94af9270
KS
9696 return die_needs_namespace (die_specification (die, &spec_cu),
9697 spec_cu);
9698 }
9699
1c809c68 9700 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9701 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9702 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9703 return 0;
9704 /* A variable in a lexical block of some kind does not need a
9705 namespace, even though in C++ such variables may be external
9706 and have a mangled name. */
9707 if (die->parent->tag == DW_TAG_lexical_block
9708 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9709 || die->parent->tag == DW_TAG_catch_block
9710 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9711 return 0;
9712 return 1;
94af9270
KS
9713
9714 default:
9715 return 0;
9716 }
9717}
9718
73b9be8b
KS
9719/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9720 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9721 defined for the given DIE. */
9722
9723static struct attribute *
9724dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9725{
9726 struct attribute *attr;
9727
9728 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9729 if (attr == NULL)
9730 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9731
9732 return attr;
9733}
9734
9735/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9736 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9737 defined for the given DIE. */
9738
9739static const char *
9740dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9741{
9742 const char *linkage_name;
9743
9744 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9745 if (linkage_name == NULL)
9746 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9747
787de330
TT
9748 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9749 See https://github.com/rust-lang/rust/issues/32925. */
3e9f1ca1 9750 if (cu->per_cu->lang == language_rust && linkage_name != NULL
787de330
TT
9751 && strchr (linkage_name, '{') != NULL)
9752 linkage_name = NULL;
9753
73b9be8b
KS
9754 return linkage_name;
9755}
9756
94af9270 9757/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9758 compute the physname for the object, which include a method's:
9c37b5ae 9759 - formal parameters (C++),
a766d390 9760 - receiver type (Go),
a766d390
DE
9761
9762 The term "physname" is a bit confusing.
9763 For C++, for example, it is the demangled name.
9764 For Go, for example, it's the mangled name.
94af9270 9765
af6b7be1
JB
9766 For Ada, return the DIE's linkage name rather than the fully qualified
9767 name. PHYSNAME is ignored..
9768
5989a64e 9769 The result is allocated on the objfile->per_bfd's obstack and
45940949 9770 canonicalized. */
94af9270
KS
9771
9772static const char *
15d034d0
TT
9773dwarf2_compute_name (const char *name,
9774 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9775 int physname)
9776{
5e22e966 9777 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 9778
94af9270
KS
9779 if (name == NULL)
9780 name = dwarf2_name (die, cu);
9781
3e9f1ca1
TT
9782 enum language lang = cu->per_cu->lang;
9783
2ee7123e
DE
9784 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9785 but otherwise compute it by typename_concat inside GDB.
9786 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 9787 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 9788 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
9789 will set the demangled name to the result of dwarf2_full_name, and it is
9790 the demangled name that GDB uses if it exists. */
3e9f1ca1
TT
9791 if (lang == language_ada
9792 || (lang == language_fortran && physname))
f55ee35c
JK
9793 {
9794 /* For Ada unit, we prefer the linkage name over the name, as
9795 the former contains the exported name, which the user expects
9796 to be able to reference. Ideally, we want the user to be able
9797 to reference this entity using either natural or linkage name,
9798 but we haven't started looking at this enhancement yet. */
73b9be8b 9799 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 9800
2ee7123e
DE
9801 if (linkage_name != NULL)
9802 return linkage_name;
f55ee35c
JK
9803 }
9804
94af9270
KS
9805 /* These are the only languages we know how to qualify names in. */
9806 if (name != NULL
3e9f1ca1
TT
9807 && (lang == language_cplus
9808 || lang == language_fortran || lang == language_d
9809 || lang == language_rust))
94af9270
KS
9810 {
9811 if (die_needs_namespace (die, cu))
9812 {
0d5cff50 9813 const char *prefix;
34a68019 9814 const char *canonical_name = NULL;
94af9270 9815
d7e74731
PA
9816 string_file buf;
9817
94af9270 9818 prefix = determine_prefix (die, cu);
94af9270
KS
9819 if (*prefix != '\0')
9820 {
43816ebc
TT
9821 gdb::unique_xmalloc_ptr<char> prefixed_name
9822 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 9823
43816ebc 9824 buf.puts (prefixed_name.get ());
94af9270
KS
9825 }
9826 else
d7e74731 9827 buf.puts (name);
94af9270 9828
98bfdba5
PA
9829 /* Template parameters may be specified in the DIE's DW_AT_name, or
9830 as children with DW_TAG_template_type_param or
9831 DW_TAG_value_type_param. If the latter, add them to the name
9832 here. If the name already has template parameters, then
9833 skip this step; some versions of GCC emit both, and
9834 it is more efficient to use the pre-computed name.
9835
9836 Something to keep in mind about this process: it is very
9837 unlikely, or in some cases downright impossible, to produce
9838 something that will match the mangled name of a function.
9839 If the definition of the function has the same debug info,
9840 we should be able to match up with it anyway. But fallbacks
9841 using the minimal symbol, for instance to find a method
9842 implemented in a stripped copy of libstdc++, will not work.
9843 If we do not have debug info for the definition, we will have to
9844 match them up some other way.
9845
9846 When we do name matching there is a related problem with function
9847 templates; two instantiated function templates are allowed to
9848 differ only by their return types, which we do not add here. */
9849
3e9f1ca1 9850 if (lang == language_cplus && strchr (name, '<') == NULL)
98bfdba5
PA
9851 {
9852 struct attribute *attr;
9853 struct die_info *child;
9854 int first = 1;
9855
9856 die->building_fullname = 1;
9857
9858 for (child = die->child; child != NULL; child = child->sibling)
9859 {
9860 struct type *type;
12df843f 9861 LONGEST value;
d521ce57 9862 const gdb_byte *bytes;
98bfdba5
PA
9863 struct dwarf2_locexpr_baton *baton;
9864 struct value *v;
9865
9866 if (child->tag != DW_TAG_template_type_param
9867 && child->tag != DW_TAG_template_value_param)
9868 continue;
9869
9870 if (first)
9871 {
d7e74731 9872 buf.puts ("<");
98bfdba5
PA
9873 first = 0;
9874 }
9875 else
d7e74731 9876 buf.puts (", ");
98bfdba5
PA
9877
9878 attr = dwarf2_attr (child, DW_AT_type, cu);
9879 if (attr == NULL)
9880 {
b98664d3 9881 complaint (_("template parameter missing DW_AT_type"));
d7e74731 9882 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
9883 continue;
9884 }
9885 type = die_type (child, cu);
9886
9887 if (child->tag == DW_TAG_template_type_param)
9888 {
3e9f1ca1
TT
9889 cu->language_defn->print_type (type, "", &buf, -1, 0,
9890 &type_print_raw_options);
98bfdba5
PA
9891 continue;
9892 }
9893
9894 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9895 if (attr == NULL)
9896 {
b98664d3 9897 complaint (_("template parameter missing "
3e43a32a 9898 "DW_AT_const_value"));
d7e74731 9899 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
9900 continue;
9901 }
9902
9903 dwarf2_const_value_attr (attr, type, name,
9904 &cu->comp_unit_obstack, cu,
9905 &value, &bytes, &baton);
9906
20ce4123 9907 if (type->has_no_signedness ())
98bfdba5
PA
9908 /* GDB prints characters as NUMBER 'CHAR'. If that's
9909 changed, this can use value_print instead. */
3e9f1ca1 9910 cu->language_defn->printchar (value, type, &buf);
98bfdba5
PA
9911 else
9912 {
9913 struct value_print_options opts;
9914
9915 if (baton != NULL)
9916 v = dwarf2_evaluate_loc_desc (type, NULL,
9917 baton->data,
9918 baton->size,
9f47c707
SM
9919 baton->per_cu,
9920 baton->per_objfile);
98bfdba5
PA
9921 else if (bytes != NULL)
9922 {
9923 v = allocate_value (type);
9924 memcpy (value_contents_writeable (v), bytes,
9925 TYPE_LENGTH (type));
9926 }
9927 else
9928 v = value_from_longest (type, value);
9929
3e43a32a
MS
9930 /* Specify decimal so that we do not depend on
9931 the radix. */
98bfdba5
PA
9932 get_formatted_print_options (&opts, 'd');
9933 opts.raw = 1;
d7e74731 9934 value_print (v, &buf, &opts);
98bfdba5 9935 release_value (v);
98bfdba5
PA
9936 }
9937 }
9938
9939 die->building_fullname = 0;
9940
9941 if (!first)
9942 {
9943 /* Close the argument list, with a space if necessary
9944 (nested templates). */
d7e74731
PA
9945 if (!buf.empty () && buf.string ().back () == '>')
9946 buf.puts (" >");
98bfdba5 9947 else
d7e74731 9948 buf.puts (">");
98bfdba5
PA
9949 }
9950 }
9951
9c37b5ae 9952 /* For C++ methods, append formal parameter type
94af9270 9953 information, if PHYSNAME. */
6e70227d 9954
94af9270 9955 if (physname && die->tag == DW_TAG_subprogram
3e9f1ca1 9956 && lang == language_cplus)
94af9270
KS
9957 {
9958 struct type *type = read_type_die (die, cu);
9959
3e9f1ca1 9960 c_type_print_args (type, &buf, 1, lang,
79d43c61 9961 &type_print_raw_options);
94af9270 9962
3e9f1ca1 9963 if (lang == language_cplus)
94af9270 9964 {
60430eff
DJ
9965 /* Assume that an artificial first parameter is
9966 "this", but do not crash if it is not. RealView
9967 marks unnamed (and thus unused) parameters as
9968 artificial; there is no way to differentiate
9969 the two cases. */
1f704f76 9970 if (type->num_fields () > 0
94af9270 9971 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
9972 && type->field (0).type ()->code () == TYPE_CODE_PTR
9973 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 9974 buf.puts (" const");
94af9270
KS
9975 }
9976 }
9977
d7e74731 9978 const std::string &intermediate_name = buf.string ();
94af9270 9979
3e9f1ca1 9980 if (lang == language_cplus)
34a68019 9981 canonical_name
322a8516 9982 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 9983 objfile);
34a68019
TT
9984
9985 /* If we only computed INTERMEDIATE_NAME, or if
9986 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 9987 intern it. */
322a8516 9988 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 9989 name = objfile->intern (intermediate_name);
34a68019
TT
9990 else
9991 name = canonical_name;
94af9270
KS
9992 }
9993 }
9994
9995 return name;
9996}
9997
0114d602
DJ
9998/* Return the fully qualified name of DIE, based on its DW_AT_name.
9999 If scope qualifiers are appropriate they will be added. The result
34a68019 10000 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10001 not have a name. NAME may either be from a previous call to
10002 dwarf2_name or NULL.
10003
9c37b5ae 10004 The output string will be canonicalized (if C++). */
0114d602
DJ
10005
10006static const char *
15d034d0 10007dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10008{
94af9270
KS
10009 return dwarf2_compute_name (name, die, cu, 0);
10010}
0114d602 10011
94af9270
KS
10012/* Construct a physname for the given DIE in CU. NAME may either be
10013 from a previous call to dwarf2_name or NULL. The result will be
10014 allocated on the objfile_objstack or NULL if the DIE does not have a
10015 name.
0114d602 10016
9c37b5ae 10017 The output string will be canonicalized (if C++). */
0114d602 10018
94af9270 10019static const char *
15d034d0 10020dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10021{
5e22e966 10022 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10023 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10024 int need_copy = 1;
10025
10026 /* In this case dwarf2_compute_name is just a shortcut not building anything
10027 on its own. */
10028 if (!die_needs_namespace (die, cu))
10029 return dwarf2_compute_name (name, die, cu, 1);
10030
3e9f1ca1 10031 if (cu->per_cu->lang != language_rust)
906bb4c5 10032 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10033
10034 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10035 has computed. */
791afaa2 10036 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10037 if (mangled != NULL)
900e11f9 10038 {
3e9f1ca1 10039 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10040 {
10041 /* Do nothing (do not demangle the symbol name). */
10042 }
a766d390
DE
10043 else
10044 {
0eb876f5
JB
10045 /* Use DMGL_RET_DROP for C++ template functions to suppress
10046 their return type. It is easier for GDB users to search
10047 for such functions as `name(params)' than `long name(params)'.
10048 In such case the minimal symbol names do not match the full
10049 symbol names but for template functions there is never a need
10050 to look up their definition from their declaration so
10051 the only disadvantage remains the minimal symbol variant
10052 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10053 demangled.reset (gdb_demangle (mangled,
10054 (DMGL_PARAMS | DMGL_ANSI
10055 | DMGL_RET_DROP)));
a766d390 10056 }
900e11f9 10057 if (demangled)
791afaa2 10058 canon = demangled.get ();
900e11f9
JK
10059 else
10060 {
10061 canon = mangled;
10062 need_copy = 0;
10063 }
10064 }
10065
10066 if (canon == NULL || check_physname)
10067 {
10068 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10069
10070 if (canon != NULL && strcmp (physname, canon) != 0)
10071 {
10072 /* It may not mean a bug in GDB. The compiler could also
10073 compute DW_AT_linkage_name incorrectly. But in such case
10074 GDB would need to be bug-to-bug compatible. */
10075
b98664d3 10076 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10077 "(from linkage <%s>) - DIE at %s [in module %s]"),
10078 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10079 objfile_name (objfile));
900e11f9
JK
10080
10081 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10082 is available here - over computed PHYSNAME. It is safer
10083 against both buggy GDB and buggy compilers. */
10084
10085 retval = canon;
10086 }
10087 else
10088 {
10089 retval = physname;
10090 need_copy = 0;
10091 }
10092 }
10093 else
10094 retval = canon;
10095
10096 if (need_copy)
be1e3d3e 10097 retval = objfile->intern (retval);
900e11f9 10098
900e11f9 10099 return retval;
0114d602
DJ
10100}
10101
74921315
KS
10102/* Inspect DIE in CU for a namespace alias. If one exists, record
10103 a new symbol for it.
10104
10105 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10106
10107static int
10108read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10109{
10110 struct attribute *attr;
10111
10112 /* If the die does not have a name, this is not a namespace
10113 alias. */
10114 attr = dwarf2_attr (die, DW_AT_name, cu);
10115 if (attr != NULL)
10116 {
10117 int num;
10118 struct die_info *d = die;
10119 struct dwarf2_cu *imported_cu = cu;
10120
10121 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10122 keep inspecting DIEs until we hit the underlying import. */
10123#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10124 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10125 {
10126 attr = dwarf2_attr (d, DW_AT_import, cu);
10127 if (attr == NULL)
10128 break;
10129
10130 d = follow_die_ref (d, attr, &imported_cu);
10131 if (d->tag != DW_TAG_imported_declaration)
10132 break;
10133 }
10134
10135 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10136 {
b98664d3 10137 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10138 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10139 return 0;
10140 }
10141
10142 if (attr != NULL)
10143 {
10144 struct type *type;
0826b30a 10145 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10146
aa66c379 10147 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10148 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10149 {
10150 /* This declaration is a global namespace alias. Add
10151 a symbol for it whose type is the aliased namespace. */
10152 new_symbol (die, type, cu);
10153 return 1;
10154 }
10155 }
10156 }
10157
10158 return 0;
10159}
10160
22cee43f 10161/* Return the using directives repository (global or local?) to use in the
804d2729 10162 current context for CU.
22cee43f
PMR
10163
10164 For Ada, imported declarations can materialize renamings, which *may* be
10165 global. However it is impossible (for now?) in DWARF to distinguish
10166 "external" imported declarations and "static" ones. As all imported
10167 declarations seem to be static in all other languages, make them all CU-wide
10168 global only in Ada. */
10169
10170static struct using_direct **
804d2729 10171using_directives (struct dwarf2_cu *cu)
22cee43f 10172{
3e9f1ca1 10173 if (cu->per_cu->lang == language_ada
c24bdb02
KS
10174 && cu->get_builder ()->outermost_context_p ())
10175 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10176 else
c24bdb02 10177 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10178}
10179
27aa8d6a
SW
10180/* Read the import statement specified by the given die and record it. */
10181
10182static void
10183read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10184{
5e22e966 10185 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10186 struct attribute *import_attr;
32019081 10187 struct die_info *imported_die, *child_die;
de4affc9 10188 struct dwarf2_cu *imported_cu;
27aa8d6a 10189 const char *imported_name;
794684b6 10190 const char *imported_name_prefix;
13387711
SW
10191 const char *canonical_name;
10192 const char *import_alias;
10193 const char *imported_declaration = NULL;
794684b6 10194 const char *import_prefix;
eb1e02fd 10195 std::vector<const char *> excludes;
13387711 10196
27aa8d6a
SW
10197 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10198 if (import_attr == NULL)
10199 {
b98664d3 10200 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10201 dwarf_tag_name (die->tag));
10202 return;
10203 }
10204
de4affc9
CC
10205 imported_cu = cu;
10206 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10207 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10208 if (imported_name == NULL)
10209 {
10210 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10211
dda83cd7
SM
10212 The import in the following code:
10213 namespace A
10214 {
10215 typedef int B;
10216 }
10217
10218 int main ()
10219 {
10220 using A::B;
10221 B b;
10222 return b;
10223 }
10224
10225 ...
10226 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10227 <52> DW_AT_decl_file : 1
10228 <53> DW_AT_decl_line : 6
10229 <54> DW_AT_import : <0x75>
10230 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10231 <59> DW_AT_name : B
10232 <5b> DW_AT_decl_file : 1
10233 <5c> DW_AT_decl_line : 2
10234 <5d> DW_AT_type : <0x6e>
10235 ...
10236 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10237 <76> DW_AT_byte_size : 4
10238 <77> DW_AT_encoding : 5 (signed)
10239
10240 imports the wrong die ( 0x75 instead of 0x58 ).
10241 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10242 return;
10243 }
10244
82856980
SW
10245 /* Figure out the local name after import. */
10246 import_alias = dwarf2_name (die, cu);
27aa8d6a 10247
794684b6
SW
10248 /* Figure out where the statement is being imported to. */
10249 import_prefix = determine_prefix (die, cu);
10250
10251 /* Figure out what the scope of the imported die is and prepend it
10252 to the name of the imported die. */
de4affc9 10253 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10254
f55ee35c
JK
10255 if (imported_die->tag != DW_TAG_namespace
10256 && imported_die->tag != DW_TAG_module)
794684b6 10257 {
13387711
SW
10258 imported_declaration = imported_name;
10259 canonical_name = imported_name_prefix;
794684b6 10260 }
13387711 10261 else if (strlen (imported_name_prefix) > 0)
12aaed36 10262 canonical_name = obconcat (&objfile->objfile_obstack,
45280282 10263 imported_name_prefix,
3e9f1ca1
TT
10264 (cu->per_cu->lang == language_d
10265 ? "."
10266 : "::"),
45280282 10267 imported_name, (char *) NULL);
13387711
SW
10268 else
10269 canonical_name = imported_name;
794684b6 10270
3e9f1ca1
TT
10271 if (die->tag == DW_TAG_imported_module
10272 && cu->per_cu->lang == language_fortran)
32019081 10273 for (child_die = die->child; child_die && child_die->tag;
436c571c 10274 child_die = child_die->sibling)
32019081
JK
10275 {
10276 /* DWARF-4: A Fortran use statement with a “rename list” may be
10277 represented by an imported module entry with an import attribute
10278 referring to the module and owned entries corresponding to those
10279 entities that are renamed as part of being imported. */
10280
10281 if (child_die->tag != DW_TAG_imported_declaration)
10282 {
b98664d3 10283 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10284 "- DIE at %s [in module %s]"),
10285 sect_offset_str (child_die->sect_off),
10286 objfile_name (objfile));
32019081
JK
10287 continue;
10288 }
10289
10290 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10291 if (import_attr == NULL)
10292 {
b98664d3 10293 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10294 dwarf_tag_name (child_die->tag));
10295 continue;
10296 }
10297
10298 imported_cu = cu;
10299 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10300 &imported_cu);
10301 imported_name = dwarf2_name (imported_die, imported_cu);
10302 if (imported_name == NULL)
10303 {
b98664d3 10304 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10305 "imported name - DIE at %s [in module %s]"),
10306 sect_offset_str (child_die->sect_off),
10307 objfile_name (objfile));
32019081
JK
10308 continue;
10309 }
10310
eb1e02fd 10311 excludes.push_back (imported_name);
32019081
JK
10312
10313 process_die (child_die, cu);
10314 }
10315
804d2729 10316 add_using_directive (using_directives (cu),
22cee43f
PMR
10317 import_prefix,
10318 canonical_name,
10319 import_alias,
10320 imported_declaration,
10321 excludes,
10322 0,
10323 &objfile->objfile_obstack);
27aa8d6a
SW
10324}
10325
5230b05a
WT
10326/* ICC<14 does not output the required DW_AT_declaration on incomplete
10327 types, but gives them a size of zero. Starting with version 14,
10328 ICC is compatible with GCC. */
10329
9068261f 10330static bool
5230b05a
WT
10331producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10332{
10333 if (!cu->checked_producer)
10334 check_producer (cu);
10335
10336 return cu->producer_is_icc_lt_14;
10337}
10338
eb77c9df
AB
10339/* ICC generates a DW_AT_type for C void functions. This was observed on
10340 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10341 which says that void functions should not have a DW_AT_type. */
10342
10343static bool
10344producer_is_icc (struct dwarf2_cu *cu)
10345{
10346 if (!cu->checked_producer)
10347 check_producer (cu);
10348
10349 return cu->producer_is_icc;
10350}
10351
1b80a9fa
JK
10352/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10353 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10354 this, it was first present in GCC release 4.3.0. */
10355
9068261f 10356static bool
1b80a9fa
JK
10357producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10358{
10359 if (!cu->checked_producer)
10360 check_producer (cu);
10361
10362 return cu->producer_is_gcc_lt_4_3;
10363}
10364
d721ba37
PA
10365static file_and_directory
10366find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10367{
d721ba37
PA
10368 file_and_directory res;
10369
9291a0cd
TT
10370 /* Find the filename. Do not use dwarf2_name here, since the filename
10371 is not a source language identifier. */
d721ba37
PA
10372 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10373 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10374
d721ba37
PA
10375 if (res.comp_dir == NULL
10376 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10377 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10378 {
d721ba37
PA
10379 res.comp_dir_storage = ldirname (res.name);
10380 if (!res.comp_dir_storage.empty ())
10381 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10382 }
d721ba37 10383 if (res.comp_dir != NULL)
9291a0cd
TT
10384 {
10385 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10386 directory, get rid of it. */
d721ba37 10387 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10388
d721ba37
PA
10389 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10390 res.comp_dir = cp + 1;
9291a0cd
TT
10391 }
10392
d721ba37
PA
10393 if (res.name == NULL)
10394 res.name = "<unknown>";
10395
10396 return res;
9291a0cd
TT
10397}
10398
f4dc4d17
DE
10399/* Handle DW_AT_stmt_list for a compilation unit.
10400 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10401 COMP_DIR is the compilation directory. LOWPC is passed to
10402 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10403
10404static void
10405handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10406 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10407{
976ca316 10408 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10409 struct attribute *attr;
527f3840
JK
10410 struct line_header line_header_local;
10411 hashval_t line_header_local_hash;
527f3840
JK
10412 void **slot;
10413 int decode_mapping;
2ab95328 10414
f4dc4d17
DE
10415 gdb_assert (! cu->per_cu->is_debug_types);
10416
2ab95328 10417 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 10418 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
10419 return;
10420
d4df075e 10421 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
10422
10423 /* The line header hash table is only created if needed (it exists to
10424 prevent redundant reading of the line table for partial_units).
10425 If we're given a partial_unit, we'll need it. If we're given a
10426 compile_unit, then use the line header hash table if it's already
10427 created, but don't create one just yet. */
10428
976ca316 10429 if (per_objfile->line_header_hash == NULL
527f3840 10430 && die->tag == DW_TAG_partial_unit)
2ab95328 10431 {
976ca316 10432 per_objfile->line_header_hash
d15acc42
TT
10433 .reset (htab_create_alloc (127, line_header_hash_voidp,
10434 line_header_eq_voidp,
ef5f598c 10435 htab_delete_entry<line_header>,
d15acc42 10436 xcalloc, xfree));
527f3840 10437 }
2ab95328 10438
9c541725 10439 line_header_local.sect_off = line_offset;
527f3840
JK
10440 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10441 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10442 if (per_objfile->line_header_hash != NULL)
527f3840 10443 {
976ca316 10444 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10445 &line_header_local,
10446 line_header_local_hash, NO_INSERT);
10447
10448 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10449 is not present in *SLOT (since if there is something in *SLOT then
10450 it will be for a partial_unit). */
10451 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10452 {
527f3840 10453 gdb_assert (*slot != NULL);
9a3c8263 10454 cu->line_header = (struct line_header *) *slot;
527f3840 10455 return;
dee91e82 10456 }
2ab95328 10457 }
527f3840
JK
10458
10459 /* dwarf_decode_line_header does not yet provide sufficient information.
10460 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10461 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10462 if (lh == NULL)
527f3840 10463 return;
4c8aa72d
PA
10464
10465 cu->line_header = lh.release ();
10466 cu->line_header_die_owner = die;
527f3840 10467
976ca316 10468 if (per_objfile->line_header_hash == NULL)
527f3840
JK
10469 slot = NULL;
10470 else
10471 {
976ca316 10472 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10473 &line_header_local,
10474 line_header_local_hash, INSERT);
10475 gdb_assert (slot != NULL);
10476 }
10477 if (slot != NULL && *slot == NULL)
10478 {
10479 /* This newly decoded line number information unit will be owned
10480 by line_header_hash hash table. */
10481 *slot = cu->line_header;
4c8aa72d 10482 cu->line_header_die_owner = NULL;
527f3840
JK
10483 }
10484 else
10485 {
10486 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 10487 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
10488 line_header for this CU - it may happen at most once for each line
10489 number information unit. And if we're not using line_header_hash
10490 then this is what we want as well. */
10491 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10492 }
10493 decode_mapping = (die->tag != DW_TAG_partial_unit);
10494 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10495 decode_mapping);
fff8551c 10496
2ab95328
TT
10497}
10498
95554aad 10499/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10500
c906108c 10501static void
e7c27a73 10502read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10503{
976ca316
SM
10504 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10505 struct objfile *objfile = per_objfile->objfile;
08feed99 10506 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10507 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10508 CORE_ADDR highpc = ((CORE_ADDR) 0);
10509 struct attribute *attr;
c906108c 10510 struct die_info *child_die;
e142c38c 10511 CORE_ADDR baseaddr;
6e70227d 10512
3e9f1ca1 10513 prepare_one_comp_unit (cu, die, cu->per_cu->lang);
b3b3bada 10514 baseaddr = objfile->text_section_offset ();
c906108c 10515
fae299cd 10516 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10517
10518 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10519 from finish_block. */
2acceee2 10520 if (lowpc == ((CORE_ADDR) -1))
c906108c 10521 lowpc = highpc;
3e29f34a 10522 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10523
d721ba37 10524 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10525
c24bdb02 10526 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3 10527
8457e5ec
TV
10528 gdb_assert (per_objfile->sym_cu == nullptr);
10529 scoped_restore restore_sym_cu
10530 = make_scoped_restore (&per_objfile->sym_cu, cu);
10531
3019eac3
DE
10532 /* Decode line number information if present. We do this before
10533 processing child DIEs, so that the line header table is available
10534 for DW_AT_decl_file. */
d721ba37 10535 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10536
10537 /* Process all dies in compilation unit. */
10538 if (die->child != NULL)
10539 {
10540 child_die = die->child;
10541 while (child_die && child_die->tag)
10542 {
10543 process_die (child_die, cu);
436c571c 10544 child_die = child_die->sibling;
3019eac3
DE
10545 }
10546 }
8457e5ec 10547 per_objfile->sym_cu = nullptr;
3019eac3
DE
10548
10549 /* Decode macro information, if present. Dwarf 2 macro information
10550 refers to information in the line number info statement program
10551 header, so we can only read it if we've read the header
10552 successfully. */
0af92d60
JK
10553 attr = dwarf2_attr (die, DW_AT_macros, cu);
10554 if (attr == NULL)
10555 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 10556 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
10557 {
10558 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10559 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10560
529908cb 10561 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
10562 }
10563 else
10564 {
10565 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 10566 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 10567 {
529908cb 10568 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 10569
43f3e411 10570 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10571 }
10572 }
3019eac3
DE
10573}
10574
c24bdb02
KS
10575void
10576dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10577{
f4dc4d17
DE
10578 struct type_unit_group *tu_group;
10579 int first_time;
3019eac3 10580 struct attribute *attr;
9c541725 10581 unsigned int i;
0186c6a7 10582 struct signatured_type *sig_type;
3019eac3 10583
f4dc4d17 10584 gdb_assert (per_cu->is_debug_types);
0186c6a7 10585 sig_type = (struct signatured_type *) per_cu;
3019eac3 10586
c24bdb02 10587 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10588
f4dc4d17 10589 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10590 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10591 if (sig_type->type_unit_group == NULL)
c24bdb02 10592 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10593 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10594
10595 /* If we've already processed this stmt_list there's no real need to
10596 do it again, we could fake it and just recreate the part we need
10597 (file name,index -> symtab mapping). If data shows this optimization
10598 is useful we can do it then. */
8adb8487
TT
10599 type_unit_group_unshareable *tug_unshare
10600 = per_objfile->get_type_unit_group_unshareable (tu_group);
10601 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
10602
10603 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10604 debug info. */
fff8551c 10605 line_header_up lh;
d4df075e 10606 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 10607 {
d4df075e 10608 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 10609 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10610 }
10611 if (lh == NULL)
10612 {
10613 if (first_time)
c24bdb02 10614 start_symtab ("", NULL, 0);
f4dc4d17
DE
10615 else
10616 {
8adb8487 10617 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 10618 gdb_assert (m_builder == nullptr);
8adb8487 10619 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10620 m_builder.reset (new struct buildsym_compunit
10621 (COMPUNIT_OBJFILE (cust), "",
10622 COMPUNIT_DIRNAME (cust),
10623 compunit_language (cust),
10624 0, cust));
770479f2 10625 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10626 }
f4dc4d17 10627 return;
3019eac3
DE
10628 }
10629
c24bdb02
KS
10630 line_header = lh.release ();
10631 line_header_die_owner = die;
3019eac3 10632
f4dc4d17
DE
10633 if (first_time)
10634 {
c24bdb02 10635 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10636
1fd60fc0
DE
10637 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10638 still initializing it, and our caller (a few levels up)
10639 process_full_type_unit still needs to know if this is the first
10640 time. */
10641
8adb8487 10642 tug_unshare->symtabs
4ac93832
TT
10643 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10644 struct symtab *, line_header->file_names_size ());
3019eac3 10645
7ba99d21
AT
10646 auto &file_names = line_header->file_names ();
10647 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10648 {
7ba99d21 10649 file_entry &fe = file_names[i];
c24bdb02
KS
10650 dwarf2_start_subfile (this, fe.name,
10651 fe.include_dir (line_header));
10652 buildsym_compunit *b = get_builder ();
10653 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10654 {
4c8aa72d
PA
10655 /* NOTE: start_subfile will recognize when it's been
10656 passed a file it has already seen. So we can't
10657 assume there's a simple mapping from
10658 cu->line_header->file_names to subfiles, plus
10659 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10660 b->get_current_subfile ()->symtab
10661 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10662 }
10663
c24bdb02 10664 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 10665 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
10666 }
10667 }
10668 else
3019eac3 10669 {
c24bdb02 10670 gdb_assert (m_builder == nullptr);
8adb8487 10671 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10672 m_builder.reset (new struct buildsym_compunit
10673 (COMPUNIT_OBJFILE (cust), "",
10674 COMPUNIT_DIRNAME (cust),
10675 compunit_language (cust),
10676 0, cust));
770479f2 10677 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10678
7ba99d21
AT
10679 auto &file_names = line_header->file_names ();
10680 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10681 {
7ba99d21 10682 file_entry &fe = file_names[i];
8adb8487 10683 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 10684 }
3019eac3
DE
10685 }
10686
f4dc4d17
DE
10687 /* The main symtab is allocated last. Type units don't have DW_AT_name
10688 so they don't have a "real" (so to speak) symtab anyway.
10689 There is later code that will assign the main symtab to all symbols
10690 that don't have one. We need to handle the case of a symbol with a
10691 missing symtab (DW_AT_decl_file) anyway. */
10692}
3019eac3 10693
f4dc4d17
DE
10694/* Process DW_TAG_type_unit.
10695 For TUs we want to skip the first top level sibling if it's not the
10696 actual type being defined by this TU. In this case the first top
10697 level sibling is there to provide context only. */
3019eac3 10698
f4dc4d17
DE
10699static void
10700read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10701{
10702 struct die_info *child_die;
3019eac3 10703
f4dc4d17
DE
10704 prepare_one_comp_unit (cu, die, language_minimal);
10705
10706 /* Initialize (or reinitialize) the machinery for building symtabs.
10707 We do this before processing child DIEs, so that the line header table
10708 is available for DW_AT_decl_file. */
c24bdb02 10709 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10710
10711 if (die->child != NULL)
10712 {
10713 child_die = die->child;
10714 while (child_die && child_die->tag)
10715 {
10716 process_die (child_die, cu);
436c571c 10717 child_die = child_die->sibling;
f4dc4d17
DE
10718 }
10719 }
3019eac3
DE
10720}
10721\f
80626a55
DE
10722/* DWO/DWP files.
10723
10724 http://gcc.gnu.org/wiki/DebugFission
10725 http://gcc.gnu.org/wiki/DebugFissionDWP
10726
10727 To simplify handling of both DWO files ("object" files with the DWARF info)
10728 and DWP files (a file with the DWOs packaged up into one file), we treat
10729 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10730
10731static hashval_t
10732hash_dwo_file (const void *item)
10733{
9a3c8263 10734 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10735 hashval_t hash;
3019eac3 10736
a2ce51a0
DE
10737 hash = htab_hash_string (dwo_file->dwo_name);
10738 if (dwo_file->comp_dir != NULL)
10739 hash += htab_hash_string (dwo_file->comp_dir);
10740 return hash;
3019eac3
DE
10741}
10742
10743static int
10744eq_dwo_file (const void *item_lhs, const void *item_rhs)
10745{
9a3c8263
SM
10746 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10747 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10748
a2ce51a0
DE
10749 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10750 return 0;
10751 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10752 return lhs->comp_dir == rhs->comp_dir;
10753 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10754}
10755
10756/* Allocate a hash table for DWO files. */
10757
51ac9db5 10758static htab_up
298e9637 10759allocate_dwo_file_hash_table ()
3019eac3 10760{
bc68fb19
TT
10761 return htab_up (htab_create_alloc (41,
10762 hash_dwo_file,
10763 eq_dwo_file,
ef5f598c 10764 htab_delete_entry<dwo_file>,
bc68fb19 10765 xcalloc, xfree));
3019eac3
DE
10766}
10767
80626a55
DE
10768/* Lookup DWO file DWO_NAME. */
10769
10770static void **
976ca316 10771lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
10772 const char *dwo_name,
10773 const char *comp_dir)
80626a55
DE
10774{
10775 struct dwo_file find_entry;
10776 void **slot;
10777
976ca316
SM
10778 if (per_objfile->per_bfd->dwo_files == NULL)
10779 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 10780
0ac5b59e
DE
10781 find_entry.dwo_name = dwo_name;
10782 find_entry.comp_dir = comp_dir;
976ca316 10783 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 10784 INSERT);
80626a55
DE
10785
10786 return slot;
10787}
10788
3019eac3
DE
10789static hashval_t
10790hash_dwo_unit (const void *item)
10791{
9a3c8263 10792 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
10793
10794 /* This drops the top 32 bits of the id, but is ok for a hash. */
10795 return dwo_unit->signature;
10796}
10797
10798static int
10799eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10800{
9a3c8263
SM
10801 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10802 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
10803
10804 /* The signature is assumed to be unique within the DWO file.
10805 So while object file CU dwo_id's always have the value zero,
10806 that's OK, assuming each object file DWO file has only one CU,
10807 and that's the rule for now. */
10808 return lhs->signature == rhs->signature;
10809}
10810
10811/* Allocate a hash table for DWO CUs,TUs.
10812 There is one of these tables for each of CUs,TUs for each DWO file. */
10813
b0b6a987 10814static htab_up
298e9637 10815allocate_dwo_unit_table ()
3019eac3
DE
10816{
10817 /* Start out with a pretty small number.
10818 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
10819 return htab_up (htab_create_alloc (3,
10820 hash_dwo_unit,
10821 eq_dwo_unit,
10822 NULL, xcalloc, xfree));
3019eac3
DE
10823}
10824
19c3d4c9 10825/* die_reader_func for create_dwo_cu. */
3019eac3
DE
10826
10827static void
19c3d4c9
DE
10828create_dwo_cu_reader (const struct die_reader_specs *reader,
10829 const gdb_byte *info_ptr,
10830 struct die_info *comp_unit_die,
c0ab21c2
TT
10831 struct dwo_file *dwo_file,
10832 struct dwo_unit *dwo_unit)
3019eac3
DE
10833{
10834 struct dwarf2_cu *cu = reader->cu;
9c541725 10835 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 10836 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 10837
a084a2a6
AT
10838 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10839 if (!signature.has_value ())
3019eac3 10840 {
b98664d3 10841 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 10842 " its dwo_id [in module %s]"),
9d8780f0 10843 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
10844 return;
10845 }
10846
3019eac3 10847 dwo_unit->dwo_file = dwo_file;
a084a2a6 10848 dwo_unit->signature = *signature;
8a0459fd 10849 dwo_unit->section = section;
9c541725 10850 dwo_unit->sect_off = sect_off;
3019eac3
DE
10851 dwo_unit->length = cu->per_cu->length;
10852
6f738b01
SM
10853 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10854 sect_offset_str (sect_off),
10855 hex_string (dwo_unit->signature));
3019eac3
DE
10856}
10857
33c5cd75 10858/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 10859 Note: This function processes DWO files only, not DWP files. */
3019eac3 10860
33c5cd75 10861static void
976ca316 10862create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 10863 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 10864 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 10865{
976ca316
SM
10866 struct objfile *objfile = per_objfile->objfile;
10867 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 10868 const gdb_byte *info_ptr, *end_ptr;
3019eac3 10869
96b79293 10870 section.read (objfile);
33c5cd75 10871 info_ptr = section.buffer;
3019eac3
DE
10872
10873 if (info_ptr == NULL)
33c5cd75 10874 return;
3019eac3 10875
6f738b01
SM
10876 dwarf_read_debug_printf ("Reading %s for %s:",
10877 section.get_name (),
10878 section.get_file_name ());
3019eac3 10879
33c5cd75 10880 end_ptr = info_ptr + section.size;
3019eac3
DE
10881 while (info_ptr < end_ptr)
10882 {
10883 struct dwarf2_per_cu_data per_cu;
c0ab21c2 10884 struct dwo_unit read_unit {};
33c5cd75
DB
10885 struct dwo_unit *dwo_unit;
10886 void **slot;
10887 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 10888
1859c670 10889 per_cu.per_bfd = per_bfd;
3019eac3 10890 per_cu.is_debug_types = 0;
33c5cd75
DB
10891 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10892 per_cu.section = &section;
10893
976ca316 10894 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
10895 if (!reader.dummy_p)
10896 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 10897 &dwo_file, &read_unit);
33c5cd75
DB
10898 info_ptr += per_cu.length;
10899
10900 // If the unit could not be parsed, skip it.
c0ab21c2 10901 if (read_unit.dwo_file == NULL)
33c5cd75 10902 continue;
3019eac3 10903
33c5cd75 10904 if (cus_htab == NULL)
298e9637 10905 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 10906
1859c670 10907 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 10908 struct dwo_unit);
c0ab21c2 10909 *dwo_unit = read_unit;
b0b6a987 10910 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
10911 gdb_assert (slot != NULL);
10912 if (*slot != NULL)
19c3d4c9 10913 {
33c5cd75
DB
10914 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10915 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 10916
b98664d3 10917 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
10918 " the entry at offset %s, signature %s"),
10919 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 10920 hex_string (dwo_unit->signature));
19c3d4c9 10921 }
33c5cd75 10922 *slot = (void *)dwo_unit;
3019eac3 10923 }
3019eac3
DE
10924}
10925
80626a55
DE
10926/* DWP file .debug_{cu,tu}_index section format:
10927 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
10928 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10929
10930 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10931 officially standard DWP format was published with DWARF v5 and is called
10932 Version 5. There are no versions 3 or 4.
80626a55 10933
d2415c6c
DE
10934 DWP Version 1:
10935
80626a55
DE
10936 Both index sections have the same format, and serve to map a 64-bit
10937 signature to a set of section numbers. Each section begins with a header,
10938 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10939 indexes, and a pool of 32-bit section numbers. The index sections will be
10940 aligned at 8-byte boundaries in the file.
10941
d2415c6c
DE
10942 The index section header consists of:
10943
10944 V, 32 bit version number
10945 -, 32 bits unused
10946 N, 32 bit number of compilation units or type units in the index
10947 M, 32 bit number of slots in the hash table
80626a55 10948
d2415c6c 10949 Numbers are recorded using the byte order of the application binary.
80626a55 10950
d2415c6c
DE
10951 The hash table begins at offset 16 in the section, and consists of an array
10952 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10953 order of the application binary). Unused slots in the hash table are 0.
10954 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 10955
d2415c6c
DE
10956 The parallel table begins immediately after the hash table
10957 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10958 array of 32-bit indexes (using the byte order of the application binary),
10959 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10960 table contains a 32-bit index into the pool of section numbers. For unused
10961 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 10962
73869dc2
DE
10963 The pool of section numbers begins immediately following the hash table
10964 (at offset 16 + 12 * M from the beginning of the section). The pool of
10965 section numbers consists of an array of 32-bit words (using the byte order
10966 of the application binary). Each item in the array is indexed starting
10967 from 0. The hash table entry provides the index of the first section
10968 number in the set. Additional section numbers in the set follow, and the
10969 set is terminated by a 0 entry (section number 0 is not used in ELF).
10970
10971 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10972 section must be the first entry in the set, and the .debug_abbrev.dwo must
10973 be the second entry. Other members of the set may follow in any order.
10974
10975 ---
10976
d2854d8d 10977 DWP Versions 2 and 5:
73869dc2 10978
d2854d8d 10979 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
10980 and the entries in the index tables are now offsets into these sections.
10981 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10982 section.
10983
10984 Index Section Contents:
10985 Header
10986 Hash Table of Signatures dwp_hash_table.hash_table
10987 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
10988 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10989 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
10990
10991 The index section header consists of:
10992
10993 V, 32 bit version number
10994 L, 32 bit number of columns in the table of section offsets
10995 N, 32 bit number of compilation units or type units in the index
10996 M, 32 bit number of slots in the hash table
10997
10998 Numbers are recorded using the byte order of the application binary.
10999
11000 The hash table has the same format as version 1.
11001 The parallel table of indices has the same format as version 1,
11002 except that the entries are origin-1 indices into the table of sections
11003 offsets and the table of section sizes.
11004
11005 The table of offsets begins immediately following the parallel table
11006 (at offset 16 + 12 * M from the beginning of the section). The table is
11007 a two-dimensional array of 32-bit words (using the byte order of the
11008 application binary), with L columns and N+1 rows, in row-major order.
11009 Each row in the array is indexed starting from 0. The first row provides
11010 a key to the remaining rows: each column in this row provides an identifier
11011 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11012 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11013
11014 DW_SECT_INFO 1 .debug_info.dwo
11015 DW_SECT_TYPES 2 .debug_types.dwo
11016 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11017 DW_SECT_LINE 4 .debug_line.dwo
11018 DW_SECT_LOC 5 .debug_loc.dwo
11019 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11020 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11021 DW_SECT_MACRO 8 .debug_macro.dwo
11022
d2854d8d
CT
11023 The section identifiers for Version 5 are:
11024
11025 DW_SECT_INFO_V5 1 .debug_info.dwo
11026 DW_SECT_RESERVED_V5 2 --
11027 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11028 DW_SECT_LINE_V5 4 .debug_line.dwo
11029 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11030 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11031 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11032 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11033
73869dc2
DE
11034 The offsets provided by the CU and TU index sections are the base offsets
11035 for the contributions made by each CU or TU to the corresponding section
11036 in the package file. Each CU and TU header contains an abbrev_offset
11037 field, used to find the abbreviations table for that CU or TU within the
11038 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11039 be interpreted as relative to the base offset given in the index section.
11040 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11041 should be interpreted as relative to the base offset for .debug_line.dwo,
11042 and offsets into other debug sections obtained from DWARF attributes should
11043 also be interpreted as relative to the corresponding base offset.
11044
11045 The table of sizes begins immediately following the table of offsets.
11046 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11047 with L columns and N rows, in row-major order. Each row in the array is
11048 indexed starting from 1 (row 0 is shared by the two tables).
11049
11050 ---
11051
11052 Hash table lookup is handled the same in version 1 and 2:
11053
11054 We assume that N and M will not exceed 2^32 - 1.
11055 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11056
d2415c6c
DE
11057 Given a 64-bit compilation unit signature or a type signature S, an entry
11058 in the hash table is located as follows:
80626a55 11059
d2415c6c
DE
11060 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11061 the low-order k bits all set to 1.
80626a55 11062
d2415c6c 11063 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11064
d2415c6c
DE
11065 3) If the hash table entry at index H matches the signature, use that
11066 entry. If the hash table entry at index H is unused (all zeroes),
11067 terminate the search: the signature is not present in the table.
80626a55 11068
d2415c6c 11069 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11070
d2415c6c 11071 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11072 to stop at an unused slot or find the match. */
80626a55
DE
11073
11074/* Create a hash table to map DWO IDs to their CU/TU entry in
11075 .debug_{info,types}.dwo in DWP_FILE.
11076 Returns NULL if there isn't one.
11077 Note: This function processes DWP files only, not DWO files. */
11078
11079static struct dwp_hash_table *
976ca316 11080create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11081 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11082{
976ca316 11083 struct objfile *objfile = per_objfile->objfile;
400174b1 11084 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11085 const gdb_byte *index_ptr, *index_end;
80626a55 11086 struct dwarf2_section_info *index;
73869dc2 11087 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11088 struct dwp_hash_table *htab;
11089
11090 if (is_debug_types)
11091 index = &dwp_file->sections.tu_index;
11092 else
11093 index = &dwp_file->sections.cu_index;
11094
96b79293 11095 if (index->empty ())
80626a55 11096 return NULL;
96b79293 11097 index->read (objfile);
80626a55
DE
11098
11099 index_ptr = index->buffer;
11100 index_end = index_ptr + index->size;
11101
d2854d8d
CT
11102 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11103 For now it's safe to just read 4 bytes (particularly as it's difficult to
11104 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11105 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11106 index_ptr += 4;
d2854d8d 11107 if (version == 2 || version == 5)
73869dc2
DE
11108 nr_columns = read_4_bytes (dbfd, index_ptr);
11109 else
11110 nr_columns = 0;
11111 index_ptr += 4;
80626a55
DE
11112 nr_units = read_4_bytes (dbfd, index_ptr);
11113 index_ptr += 4;
11114 nr_slots = read_4_bytes (dbfd, index_ptr);
11115 index_ptr += 4;
11116
d2854d8d 11117 if (version != 1 && version != 2 && version != 5)
80626a55 11118 {
21aa081e 11119 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11120 " [in module %s]"),
21aa081e 11121 pulongest (version), dwp_file->name);
80626a55
DE
11122 }
11123 if (nr_slots != (nr_slots & -nr_slots))
11124 {
21aa081e 11125 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11126 " is not power of 2 [in module %s]"),
21aa081e 11127 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11128 }
11129
976ca316 11130 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11131 htab->version = version;
11132 htab->nr_columns = nr_columns;
80626a55
DE
11133 htab->nr_units = nr_units;
11134 htab->nr_slots = nr_slots;
11135 htab->hash_table = index_ptr;
11136 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11137
11138 /* Exit early if the table is empty. */
11139 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11140 || (version == 2 && nr_columns == 0)
11141 || (version == 5 && nr_columns == 0))
73869dc2
DE
11142 {
11143 /* All must be zero. */
11144 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11145 || (version == 2 && nr_columns != 0)
11146 || (version == 5 && nr_columns != 0))
73869dc2 11147 {
b98664d3 11148 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11149 " all zero [in modules %s]"),
11150 dwp_file->name);
11151 }
11152 return htab;
11153 }
11154
11155 if (version == 1)
11156 {
11157 htab->section_pool.v1.indices =
11158 htab->unit_table + sizeof (uint32_t) * nr_slots;
11159 /* It's harder to decide whether the section is too small in v1.
11160 V1 is deprecated anyway so we punt. */
11161 }
d2854d8d 11162 else if (version == 2)
73869dc2
DE
11163 {
11164 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11165 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11166 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11167 /* Reverse map for error checking. */
11168 int ids_seen[DW_SECT_MAX + 1];
11169 int i;
11170
11171 if (nr_columns < 2)
11172 {
11173 error (_("Dwarf Error: bad DWP hash table, too few columns"
11174 " in section table [in module %s]"),
11175 dwp_file->name);
11176 }
11177 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11178 {
11179 error (_("Dwarf Error: bad DWP hash table, too many columns"
11180 " in section table [in module %s]"),
11181 dwp_file->name);
11182 }
04fd5eed
GB
11183 memset (ids, 255, sizeof_ids);
11184 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11185 for (i = 0; i < nr_columns; ++i)
11186 {
11187 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11188
11189 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11190 {
11191 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11192 " in section table [in module %s]"),
11193 id, dwp_file->name);
11194 }
11195 if (ids_seen[id] != -1)
11196 {
11197 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11198 " id %d in section table [in module %s]"),
11199 id, dwp_file->name);
11200 }
11201 ids_seen[id] = i;
11202 ids[i] = id;
11203 }
11204 /* Must have exactly one info or types section. */
11205 if (((ids_seen[DW_SECT_INFO] != -1)
11206 + (ids_seen[DW_SECT_TYPES] != -1))
11207 != 1)
11208 {
11209 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11210 " DWO info/types section [in module %s]"),
11211 dwp_file->name);
11212 }
11213 /* Must have an abbrev section. */
11214 if (ids_seen[DW_SECT_ABBREV] == -1)
11215 {
11216 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11217 " section [in module %s]"),
11218 dwp_file->name);
11219 }
11220 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11221 htab->section_pool.v2.sizes =
11222 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11223 * nr_units * nr_columns);
11224 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11225 * nr_units * nr_columns))
11226 > index_end)
11227 {
11228 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11229 " [in module %s]"),
11230 dwp_file->name);
11231 }
11232 }
d2854d8d
CT
11233 else /* version == 5 */
11234 {
11235 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11236 int *ids = htab->section_pool.v5.section_ids;
11237 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11238 /* Reverse map for error checking. */
11239 int ids_seen[DW_SECT_MAX_V5 + 1];
11240
11241 if (nr_columns < 2)
11242 {
11243 error (_("Dwarf Error: bad DWP hash table, too few columns"
11244 " in section table [in module %s]"),
11245 dwp_file->name);
11246 }
11247 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11248 {
11249 error (_("Dwarf Error: bad DWP hash table, too many columns"
11250 " in section table [in module %s]"),
11251 dwp_file->name);
11252 }
11253 memset (ids, 255, sizeof_ids);
11254 memset (ids_seen, 255, sizeof (ids_seen));
11255 for (int i = 0; i < nr_columns; ++i)
11256 {
11257 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11258
11259 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11260 {
11261 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11262 " in section table [in module %s]"),
11263 id, dwp_file->name);
11264 }
11265 if (ids_seen[id] != -1)
11266 {
11267 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11268 " id %d in section table [in module %s]"),
11269 id, dwp_file->name);
11270 }
11271 ids_seen[id] = i;
11272 ids[i] = id;
11273 }
11274 /* Must have seen an info section. */
11275 if (ids_seen[DW_SECT_INFO_V5] == -1)
11276 {
11277 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11278 " DWO info/types section [in module %s]"),
11279 dwp_file->name);
11280 }
11281 /* Must have an abbrev section. */
11282 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11283 {
11284 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11285 " section [in module %s]"),
11286 dwp_file->name);
11287 }
11288 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11289 htab->section_pool.v5.sizes
11290 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11291 * nr_units * nr_columns);
11292 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11293 * nr_units * nr_columns))
11294 > index_end)
11295 {
11296 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11297 " [in module %s]"),
11298 dwp_file->name);
11299 }
11300 }
80626a55
DE
11301
11302 return htab;
11303}
11304
11305/* Update SECTIONS with the data from SECTP.
11306
5bb6e9dd
TT
11307 This function is like the other "locate" section routines, but in
11308 this context the sections to read comes from the DWP V1 hash table,
11309 not the full ELF section table.
80626a55
DE
11310
11311 The result is non-zero for success, or zero if an error was found. */
11312
11313static int
73869dc2
DE
11314locate_v1_virtual_dwo_sections (asection *sectp,
11315 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11316{
11317 const struct dwop_section_names *names = &dwop_section_names;
11318
fbedd546 11319 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
11320 {
11321 /* There can be only one. */
049412e3 11322 if (sections->abbrev.s.section != NULL)
80626a55 11323 return 0;
049412e3 11324 sections->abbrev.s.section = sectp;
fd361982 11325 sections->abbrev.size = bfd_section_size (sectp);
80626a55 11326 }
fbedd546
TT
11327 else if (names->info_dwo.matches (sectp->name)
11328 || names->types_dwo.matches (sectp->name))
80626a55
DE
11329 {
11330 /* There can be only one. */
049412e3 11331 if (sections->info_or_types.s.section != NULL)
80626a55 11332 return 0;
049412e3 11333 sections->info_or_types.s.section = sectp;
fd361982 11334 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 11335 }
fbedd546 11336 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
11337 {
11338 /* There can be only one. */
049412e3 11339 if (sections->line.s.section != NULL)
80626a55 11340 return 0;
049412e3 11341 sections->line.s.section = sectp;
fd361982 11342 sections->line.size = bfd_section_size (sectp);
80626a55 11343 }
fbedd546 11344 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
11345 {
11346 /* There can be only one. */
049412e3 11347 if (sections->loc.s.section != NULL)
80626a55 11348 return 0;
049412e3 11349 sections->loc.s.section = sectp;
fd361982 11350 sections->loc.size = bfd_section_size (sectp);
80626a55 11351 }
fbedd546 11352 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
11353 {
11354 /* There can be only one. */
049412e3 11355 if (sections->macinfo.s.section != NULL)
80626a55 11356 return 0;
049412e3 11357 sections->macinfo.s.section = sectp;
fd361982 11358 sections->macinfo.size = bfd_section_size (sectp);
80626a55 11359 }
fbedd546 11360 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
11361 {
11362 /* There can be only one. */
049412e3 11363 if (sections->macro.s.section != NULL)
80626a55 11364 return 0;
049412e3 11365 sections->macro.s.section = sectp;
fd361982 11366 sections->macro.size = bfd_section_size (sectp);
80626a55 11367 }
fbedd546 11368 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
11369 {
11370 /* There can be only one. */
049412e3 11371 if (sections->str_offsets.s.section != NULL)
80626a55 11372 return 0;
049412e3 11373 sections->str_offsets.s.section = sectp;
fd361982 11374 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11375 }
11376 else
11377 {
11378 /* No other kind of section is valid. */
11379 return 0;
11380 }
11381
11382 return 1;
11383}
11384
73869dc2
DE
11385/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11386 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11387 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11388 This is for DWP version 1 files. */
80626a55
DE
11389
11390static struct dwo_unit *
976ca316 11391create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11392 struct dwp_file *dwp_file,
73869dc2
DE
11393 uint32_t unit_index,
11394 const char *comp_dir,
11395 ULONGEST signature, int is_debug_types)
80626a55 11396{
73869dc2
DE
11397 const struct dwp_hash_table *dwp_htab =
11398 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11399 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11400 const char *kind = is_debug_types ? "TU" : "CU";
11401 struct dwo_file *dwo_file;
11402 struct dwo_unit *dwo_unit;
73869dc2 11403 struct virtual_v1_dwo_sections sections;
80626a55 11404 void **dwo_file_slot;
80626a55
DE
11405 int i;
11406
73869dc2
DE
11407 gdb_assert (dwp_file->version == 1);
11408
6f738b01
SM
11409 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11410 kind, pulongest (unit_index), hex_string (signature),
11411 dwp_file->name);
80626a55 11412
19ac8c2e 11413 /* Fetch the sections of this DWO unit.
80626a55
DE
11414 Put a limit on the number of sections we look for so that bad data
11415 doesn't cause us to loop forever. */
11416
73869dc2 11417#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11418 (1 /* .debug_info or .debug_types */ \
11419 + 1 /* .debug_abbrev */ \
11420 + 1 /* .debug_line */ \
11421 + 1 /* .debug_loc */ \
11422 + 1 /* .debug_str_offsets */ \
19ac8c2e 11423 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11424 + 1 /* trailing zero */)
11425
11426 memset (&sections, 0, sizeof (sections));
80626a55 11427
73869dc2 11428 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11429 {
11430 asection *sectp;
11431 uint32_t section_nr =
11432 read_4_bytes (dbfd,
73869dc2
DE
11433 dwp_htab->section_pool.v1.indices
11434 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11435
11436 if (section_nr == 0)
11437 break;
11438 if (section_nr >= dwp_file->num_sections)
11439 {
11440 error (_("Dwarf Error: bad DWP hash table, section number too large"
11441 " [in module %s]"),
11442 dwp_file->name);
11443 }
11444
11445 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11446 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11447 {
11448 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11449 " [in module %s]"),
11450 dwp_file->name);
11451 }
11452 }
11453
11454 if (i < 2
96b79293
TT
11455 || sections.info_or_types.empty ()
11456 || sections.abbrev.empty ())
80626a55
DE
11457 {
11458 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11459 " [in module %s]"),
11460 dwp_file->name);
11461 }
73869dc2 11462 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11463 {
11464 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11465 " [in module %s]"),
11466 dwp_file->name);
11467 }
11468
11469 /* It's easier for the rest of the code if we fake a struct dwo_file and
11470 have dwo_unit "live" in that. At least for now.
11471
11472 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11473 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11474 file, we can combine them back into a virtual DWO file to save space
11475 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11476 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11477
791afaa2
TT
11478 std::string virtual_dwo_name =
11479 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11480 sections.abbrev.get_id (),
11481 sections.line.get_id (),
11482 sections.loc.get_id (),
11483 sections.str_offsets.get_id ());
80626a55 11484 /* Can we use an existing virtual DWO file? */
976ca316 11485 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11486 comp_dir);
80626a55
DE
11487 /* Create one if necessary. */
11488 if (*dwo_file_slot == NULL)
11489 {
6f738b01
SM
11490 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11491 virtual_dwo_name.c_str ());
11492
51ac9db5 11493 dwo_file = new struct dwo_file;
976ca316 11494 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11495 dwo_file->comp_dir = comp_dir;
80626a55
DE
11496 dwo_file->sections.abbrev = sections.abbrev;
11497 dwo_file->sections.line = sections.line;
11498 dwo_file->sections.loc = sections.loc;
11499 dwo_file->sections.macinfo = sections.macinfo;
11500 dwo_file->sections.macro = sections.macro;
11501 dwo_file->sections.str_offsets = sections.str_offsets;
11502 /* The "str" section is global to the entire DWP file. */
11503 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11504 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11505 there's no need to record it in dwo_file.
11506 Also, we can't simply record type sections in dwo_file because
11507 we record a pointer into the vector in dwo_unit. As we collect more
11508 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11509 for it, invalidating all copies of pointers into the previous
11510 contents. */
80626a55
DE
11511 *dwo_file_slot = dwo_file;
11512 }
11513 else
11514 {
6f738b01
SM
11515 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11516 virtual_dwo_name.c_str ());
11517
9a3c8263 11518 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11519 }
80626a55 11520
976ca316 11521 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11522 dwo_unit->dwo_file = dwo_file;
11523 dwo_unit->signature = signature;
8d749320 11524 dwo_unit->section =
976ca316 11525 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11526 *dwo_unit->section = sections.info_or_types;
57d63ce2 11527 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11528
11529 return dwo_unit;
11530}
11531
d2854d8d
CT
11532/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11533 simplify them. Given a pointer to the containing section SECTION, and
11534 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11535 virtual section of just that piece. */
73869dc2
DE
11536
11537static struct dwarf2_section_info
d2854d8d
CT
11538create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11539 struct dwarf2_section_info *section,
11540 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
11541{
11542 struct dwarf2_section_info result;
11543 asection *sectp;
11544
11545 gdb_assert (section != NULL);
11546 gdb_assert (!section->is_virtual);
11547
11548 memset (&result, 0, sizeof (result));
11549 result.s.containing_section = section;
dc4ccb6f 11550 result.is_virtual = true;
73869dc2
DE
11551
11552 if (size == 0)
11553 return result;
11554
96b79293 11555 sectp = section->get_bfd_section ();
73869dc2
DE
11556
11557 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11558 bounds of the real section. This is a pretty-rare event, so just
11559 flag an error (easier) instead of a warning and trying to cope. */
11560 if (sectp == NULL
fd361982 11561 || offset + size > bfd_section_size (sectp))
73869dc2 11562 {
d2854d8d 11563 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 11564 " in section %s [in module %s]"),
fd361982 11565 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 11566 objfile_name (per_objfile->objfile));
73869dc2
DE
11567 }
11568
11569 result.virtual_offset = offset;
11570 result.size = size;
11571 return result;
11572}
11573
11574/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11575 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11576 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11577 This is for DWP version 2 files. */
11578
11579static struct dwo_unit *
976ca316 11580create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 11581 struct dwp_file *dwp_file,
73869dc2
DE
11582 uint32_t unit_index,
11583 const char *comp_dir,
11584 ULONGEST signature, int is_debug_types)
11585{
73869dc2
DE
11586 const struct dwp_hash_table *dwp_htab =
11587 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11588 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11589 const char *kind = is_debug_types ? "TU" : "CU";
11590 struct dwo_file *dwo_file;
11591 struct dwo_unit *dwo_unit;
d2854d8d 11592 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 11593 void **dwo_file_slot;
73869dc2
DE
11594 int i;
11595
11596 gdb_assert (dwp_file->version == 2);
11597
6f738b01
SM
11598 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11599 kind, pulongest (unit_index), hex_string (signature),
11600 dwp_file->name);
73869dc2
DE
11601
11602 /* Fetch the section offsets of this DWO unit. */
11603
11604 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11605
11606 for (i = 0; i < dwp_htab->nr_columns; ++i)
11607 {
11608 uint32_t offset = read_4_bytes (dbfd,
11609 dwp_htab->section_pool.v2.offsets
11610 + (((unit_index - 1) * dwp_htab->nr_columns
11611 + i)
11612 * sizeof (uint32_t)));
11613 uint32_t size = read_4_bytes (dbfd,
11614 dwp_htab->section_pool.v2.sizes
11615 + (((unit_index - 1) * dwp_htab->nr_columns
11616 + i)
11617 * sizeof (uint32_t)));
11618
11619 switch (dwp_htab->section_pool.v2.section_ids[i])
11620 {
11621 case DW_SECT_INFO:
11622 case DW_SECT_TYPES:
11623 sections.info_or_types_offset = offset;
11624 sections.info_or_types_size = size;
11625 break;
11626 case DW_SECT_ABBREV:
11627 sections.abbrev_offset = offset;
11628 sections.abbrev_size = size;
11629 break;
11630 case DW_SECT_LINE:
11631 sections.line_offset = offset;
11632 sections.line_size = size;
11633 break;
11634 case DW_SECT_LOC:
11635 sections.loc_offset = offset;
11636 sections.loc_size = size;
11637 break;
11638 case DW_SECT_STR_OFFSETS:
11639 sections.str_offsets_offset = offset;
11640 sections.str_offsets_size = size;
11641 break;
11642 case DW_SECT_MACINFO:
11643 sections.macinfo_offset = offset;
11644 sections.macinfo_size = size;
11645 break;
11646 case DW_SECT_MACRO:
11647 sections.macro_offset = offset;
11648 sections.macro_size = size;
11649 break;
11650 }
11651 }
11652
11653 /* It's easier for the rest of the code if we fake a struct dwo_file and
11654 have dwo_unit "live" in that. At least for now.
11655
11656 The DWP file can be made up of a random collection of CUs and TUs.
11657 However, for each CU + set of TUs that came from the same original DWO
11658 file, we can combine them back into a virtual DWO file to save space
11659 (fewer struct dwo_file objects to allocate). Remember that for really
11660 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11661
791afaa2
TT
11662 std::string virtual_dwo_name =
11663 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11664 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11665 (long) (sections.line_size ? sections.line_offset : 0),
11666 (long) (sections.loc_size ? sections.loc_offset : 0),
11667 (long) (sections.str_offsets_size
11668 ? sections.str_offsets_offset : 0));
73869dc2 11669 /* Can we use an existing virtual DWO file? */
976ca316 11670 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11671 comp_dir);
73869dc2
DE
11672 /* Create one if necessary. */
11673 if (*dwo_file_slot == NULL)
11674 {
6f738b01
SM
11675 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11676 virtual_dwo_name.c_str ());
11677
51ac9db5 11678 dwo_file = new struct dwo_file;
976ca316 11679 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
11680 dwo_file->comp_dir = comp_dir;
11681 dwo_file->sections.abbrev =
d2854d8d
CT
11682 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11683 sections.abbrev_offset,
11684 sections.abbrev_size);
73869dc2 11685 dwo_file->sections.line =
d2854d8d
CT
11686 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11687 sections.line_offset,
11688 sections.line_size);
73869dc2 11689 dwo_file->sections.loc =
d2854d8d
CT
11690 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11691 sections.loc_offset, sections.loc_size);
73869dc2 11692 dwo_file->sections.macinfo =
d2854d8d
CT
11693 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11694 sections.macinfo_offset,
11695 sections.macinfo_size);
73869dc2 11696 dwo_file->sections.macro =
d2854d8d
CT
11697 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11698 sections.macro_offset,
11699 sections.macro_size);
73869dc2 11700 dwo_file->sections.str_offsets =
d2854d8d
CT
11701 create_dwp_v2_or_v5_section (per_objfile,
11702 &dwp_file->sections.str_offsets,
11703 sections.str_offsets_offset,
11704 sections.str_offsets_size);
73869dc2
DE
11705 /* The "str" section is global to the entire DWP file. */
11706 dwo_file->sections.str = dwp_file->sections.str;
11707 /* The info or types section is assigned below to dwo_unit,
11708 there's no need to record it in dwo_file.
11709 Also, we can't simply record type sections in dwo_file because
11710 we record a pointer into the vector in dwo_unit. As we collect more
11711 types we'll grow the vector and eventually have to reallocate space
11712 for it, invalidating all copies of pointers into the previous
11713 contents. */
11714 *dwo_file_slot = dwo_file;
11715 }
11716 else
11717 {
6f738b01
SM
11718 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11719 virtual_dwo_name.c_str ());
11720
9a3c8263 11721 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11722 }
73869dc2 11723
976ca316 11724 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
11725 dwo_unit->dwo_file = dwo_file;
11726 dwo_unit->signature = signature;
8d749320 11727 dwo_unit->section =
976ca316 11728 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 11729 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 11730 (per_objfile,
d2854d8d
CT
11731 is_debug_types
11732 ? &dwp_file->sections.types
11733 : &dwp_file->sections.info,
11734 sections.info_or_types_offset,
11735 sections.info_or_types_size);
11736 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11737
11738 return dwo_unit;
11739}
11740
11741/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11742 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11743 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11744 This is for DWP version 5 files. */
11745
11746static struct dwo_unit *
11747create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11748 struct dwp_file *dwp_file,
11749 uint32_t unit_index,
11750 const char *comp_dir,
11751 ULONGEST signature, int is_debug_types)
11752{
11753 const struct dwp_hash_table *dwp_htab
11754 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11755 bfd *dbfd = dwp_file->dbfd.get ();
11756 const char *kind = is_debug_types ? "TU" : "CU";
11757 struct dwo_file *dwo_file;
11758 struct dwo_unit *dwo_unit;
11759 struct virtual_v2_or_v5_dwo_sections sections {};
11760 void **dwo_file_slot;
11761
11762 gdb_assert (dwp_file->version == 5);
11763
6f738b01
SM
11764 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11765 kind, pulongest (unit_index), hex_string (signature),
11766 dwp_file->name);
d2854d8d
CT
11767
11768 /* Fetch the section offsets of this DWO unit. */
11769
11770 /* memset (&sections, 0, sizeof (sections)); */
11771
11772 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11773 {
11774 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
11775 dwp_htab->section_pool.v5.offsets
11776 + (((unit_index - 1)
11777 * dwp_htab->nr_columns
11778 + i)
11779 * sizeof (uint32_t)));
d2854d8d 11780 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
11781 dwp_htab->section_pool.v5.sizes
11782 + (((unit_index - 1) * dwp_htab->nr_columns
11783 + i)
11784 * sizeof (uint32_t)));
d2854d8d
CT
11785
11786 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
11787 {
11788 case DW_SECT_ABBREV_V5:
11789 sections.abbrev_offset = offset;
11790 sections.abbrev_size = size;
11791 break;
11792 case DW_SECT_INFO_V5:
11793 sections.info_or_types_offset = offset;
11794 sections.info_or_types_size = size;
11795 break;
11796 case DW_SECT_LINE_V5:
11797 sections.line_offset = offset;
11798 sections.line_size = size;
11799 break;
11800 case DW_SECT_LOCLISTS_V5:
11801 sections.loclists_offset = offset;
11802 sections.loclists_size = size;
11803 break;
11804 case DW_SECT_MACRO_V5:
11805 sections.macro_offset = offset;
11806 sections.macro_size = size;
11807 break;
11808 case DW_SECT_RNGLISTS_V5:
11809 sections.rnglists_offset = offset;
11810 sections.rnglists_size = size;
11811 break;
11812 case DW_SECT_STR_OFFSETS_V5:
11813 sections.str_offsets_offset = offset;
11814 sections.str_offsets_size = size;
11815 break;
11816 case DW_SECT_RESERVED_V5:
11817 default:
11818 break;
11819 }
d2854d8d
CT
11820 }
11821
11822 /* It's easier for the rest of the code if we fake a struct dwo_file and
11823 have dwo_unit "live" in that. At least for now.
11824
11825 The DWP file can be made up of a random collection of CUs and TUs.
11826 However, for each CU + set of TUs that came from the same original DWO
11827 file, we can combine them back into a virtual DWO file to save space
11828 (fewer struct dwo_file objects to allocate). Remember that for really
11829 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11830
11831 std::string virtual_dwo_name =
11832 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
11833 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11834 (long) (sections.line_size ? sections.line_offset : 0),
11835 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11836 (long) (sections.str_offsets_size
11837 ? sections.str_offsets_offset : 0),
11838 (long) (sections.macro_size ? sections.macro_offset : 0),
11839 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
11840 /* Can we use an existing virtual DWO file? */
11841 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
11842 virtual_dwo_name.c_str (),
11843 comp_dir);
d2854d8d
CT
11844 /* Create one if necessary. */
11845 if (*dwo_file_slot == NULL)
11846 {
6f738b01
SM
11847 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11848 virtual_dwo_name.c_str ());
11849
d2854d8d
CT
11850 dwo_file = new struct dwo_file;
11851 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11852 dwo_file->comp_dir = comp_dir;
11853 dwo_file->sections.abbrev =
dda83cd7
SM
11854 create_dwp_v2_or_v5_section (per_objfile,
11855 &dwp_file->sections.abbrev,
11856 sections.abbrev_offset,
11857 sections.abbrev_size);
d2854d8d 11858 dwo_file->sections.line =
dda83cd7
SM
11859 create_dwp_v2_or_v5_section (per_objfile,
11860 &dwp_file->sections.line,
11861 sections.line_offset, sections.line_size);
d2854d8d 11862 dwo_file->sections.macro =
dda83cd7
SM
11863 create_dwp_v2_or_v5_section (per_objfile,
11864 &dwp_file->sections.macro,
11865 sections.macro_offset,
11866 sections.macro_size);
d2854d8d 11867 dwo_file->sections.loclists =
dda83cd7
SM
11868 create_dwp_v2_or_v5_section (per_objfile,
11869 &dwp_file->sections.loclists,
11870 sections.loclists_offset,
11871 sections.loclists_size);
d2854d8d 11872 dwo_file->sections.rnglists =
dda83cd7
SM
11873 create_dwp_v2_or_v5_section (per_objfile,
11874 &dwp_file->sections.rnglists,
11875 sections.rnglists_offset,
11876 sections.rnglists_size);
d2854d8d 11877 dwo_file->sections.str_offsets =
dda83cd7
SM
11878 create_dwp_v2_or_v5_section (per_objfile,
11879 &dwp_file->sections.str_offsets,
11880 sections.str_offsets_offset,
11881 sections.str_offsets_size);
d2854d8d
CT
11882 /* The "str" section is global to the entire DWP file. */
11883 dwo_file->sections.str = dwp_file->sections.str;
11884 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
11885 there's no need to record it in dwo_file.
11886 Also, we can't simply record type sections in dwo_file because
11887 we record a pointer into the vector in dwo_unit. As we collect more
11888 types we'll grow the vector and eventually have to reallocate space
11889 for it, invalidating all copies of pointers into the previous
11890 contents. */
d2854d8d
CT
11891 *dwo_file_slot = dwo_file;
11892 }
11893 else
11894 {
6f738b01
SM
11895 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11896 virtual_dwo_name.c_str ());
11897
d2854d8d
CT
11898 dwo_file = (struct dwo_file *) *dwo_file_slot;
11899 }
11900
11901 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11902 dwo_unit->dwo_file = dwo_file;
11903 dwo_unit->signature = signature;
11904 dwo_unit->section
11905 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11906 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
11907 &dwp_file->sections.info,
11908 sections.info_or_types_offset,
11909 sections.info_or_types_size);
73869dc2
DE
11910 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11911
11912 return dwo_unit;
11913}
11914
57d63ce2
DE
11915/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11916 Returns NULL if the signature isn't found. */
80626a55
DE
11917
11918static struct dwo_unit *
976ca316 11919lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 11920 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11921 ULONGEST signature, int is_debug_types)
80626a55 11922{
57d63ce2
DE
11923 const struct dwp_hash_table *dwp_htab =
11924 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11925 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11926 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11927 uint32_t hash = signature & mask;
11928 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11929 unsigned int i;
11930 void **slot;
870f88f7 11931 struct dwo_unit find_dwo_cu;
80626a55
DE
11932
11933 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11934 find_dwo_cu.signature = signature;
19ac8c2e 11935 slot = htab_find_slot (is_debug_types
48b490f2
TT
11936 ? dwp_file->loaded_tus.get ()
11937 : dwp_file->loaded_cus.get (),
19ac8c2e 11938 &find_dwo_cu, INSERT);
80626a55
DE
11939
11940 if (*slot != NULL)
9a3c8263 11941 return (struct dwo_unit *) *slot;
80626a55
DE
11942
11943 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11944 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11945 {
11946 ULONGEST signature_in_table;
11947
11948 signature_in_table =
57d63ce2 11949 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11950 if (signature_in_table == signature)
11951 {
57d63ce2
DE
11952 uint32_t unit_index =
11953 read_4_bytes (dbfd,
11954 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11955
73869dc2
DE
11956 if (dwp_file->version == 1)
11957 {
976ca316
SM
11958 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
11959 unit_index, comp_dir,
11960 signature, is_debug_types);
73869dc2 11961 }
d2854d8d 11962 else if (dwp_file->version == 2)
73869dc2 11963 {
976ca316
SM
11964 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
11965 unit_index, comp_dir,
11966 signature, is_debug_types);
73869dc2 11967 }
d2854d8d
CT
11968 else /* version == 5 */
11969 {
11970 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11971 unit_index, comp_dir,
11972 signature, is_debug_types);
11973 }
9a3c8263 11974 return (struct dwo_unit *) *slot;
80626a55
DE
11975 }
11976 if (signature_in_table == 0)
11977 return NULL;
11978 hash = (hash + hash2) & mask;
11979 }
11980
11981 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11982 " [in module %s]"),
11983 dwp_file->name);
11984}
11985
ab5088bf 11986/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11987 Open the file specified by FILE_NAME and hand it off to BFD for
11988 preliminary analysis. Return a newly initialized bfd *, which
11989 includes a canonicalized copy of FILE_NAME.
80626a55 11990 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11991 SEARCH_CWD is true if the current directory is to be searched.
11992 It will be searched before debug-file-directory.
13aaf454
DE
11993 If successful, the file is added to the bfd include table of the
11994 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11995 If unable to find/open the file, return NULL.
3019eac3
DE
11996 NOTE: This function is derived from symfile_bfd_open. */
11997
192b62ce 11998static gdb_bfd_ref_ptr
976ca316 11999try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12000 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12001{
24b9144d 12002 int desc;
9c02c129
DE
12003 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12004 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12005 to debug_file_directory. */
e0cc99a6 12006 const char *search_path;
9c02c129
DE
12007 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12008
e0cc99a6 12009 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12010 if (search_cwd)
12011 {
12012 if (*debug_file_directory != '\0')
e0cc99a6
TT
12013 {
12014 search_path_holder.reset (concat (".", dirname_separator_string,
12015 debug_file_directory,
12016 (char *) NULL));
12017 search_path = search_path_holder.get ();
12018 }
6ac97d4c 12019 else
e0cc99a6 12020 search_path = ".";
6ac97d4c 12021 }
9c02c129 12022 else
e0cc99a6 12023 search_path = debug_file_directory;
3019eac3 12024
56d467f4
CT
12025 /* Add the path for the executable binary to the list of search paths. */
12026 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12027 search_path_holder.reset (concat (objfile_dir.c_str (),
12028 dirname_separator_string,
12029 search_path, nullptr));
12030 search_path = search_path_holder.get ();
12031
24b9144d 12032 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12033 if (is_dwp)
12034 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12035
12036 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12037 desc = openp (search_path, flags, file_name,
3019eac3
DE
12038 O_RDONLY | O_BINARY, &absolute_name);
12039 if (desc < 0)
12040 return NULL;
12041
e0cc99a6
TT
12042 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12043 gnutarget, desc));
9c02c129
DE
12044 if (sym_bfd == NULL)
12045 return NULL;
192b62ce 12046 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12047
192b62ce
TT
12048 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12049 return NULL;
3019eac3 12050
13aaf454
DE
12051 /* Success. Record the bfd as having been included by the objfile's bfd.
12052 This is important because things like demangled_names_hash lives in the
12053 objfile's per_bfd space and may have references to things like symbol
12054 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12055 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12056
3019eac3
DE
12057 return sym_bfd;
12058}
12059
ab5088bf 12060/* Try to open DWO file FILE_NAME.
3019eac3
DE
12061 COMP_DIR is the DW_AT_comp_dir attribute.
12062 The result is the bfd handle of the file.
12063 If there is a problem finding or opening the file, return NULL.
12064 Upon success, the canonicalized path of the file is stored in the bfd,
12065 same as symfile_bfd_open. */
12066
192b62ce 12067static gdb_bfd_ref_ptr
976ca316 12068open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12069 const char *file_name, const char *comp_dir)
3019eac3 12070{
80626a55 12071 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12072 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12073 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12074
12075 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12076
12077 if (comp_dir != NULL)
12078 {
43816ebc
TT
12079 gdb::unique_xmalloc_ptr<char> path_to_try
12080 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12081
12082 /* NOTE: If comp_dir is a relative path, this will also try the
12083 search path, which seems useful. */
976ca316 12084 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12085 0 /*is_dwp*/,
192b62ce 12086 1 /*search_cwd*/));
3019eac3
DE
12087 if (abfd != NULL)
12088 return abfd;
12089 }
12090
12091 /* That didn't work, try debug-file-directory, which, despite its name,
12092 is a list of paths. */
12093
12094 if (*debug_file_directory == '\0')
12095 return NULL;
12096
976ca316 12097 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12098 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12099}
12100
80626a55
DE
12101/* This function is mapped across the sections and remembers the offset and
12102 size of each of the DWO debugging sections we are interested in. */
12103
12104static void
5bb6e9dd
TT
12105dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12106 dwo_sections *dwo_sections)
80626a55 12107{
80626a55
DE
12108 const struct dwop_section_names *names = &dwop_section_names;
12109
fbedd546 12110 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12111 {
049412e3 12112 dwo_sections->abbrev.s.section = sectp;
fd361982 12113 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12114 }
fbedd546 12115 else if (names->info_dwo.matches (sectp->name))
80626a55 12116 {
049412e3 12117 dwo_sections->info.s.section = sectp;
fd361982 12118 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12119 }
fbedd546 12120 else if (names->line_dwo.matches (sectp->name))
80626a55 12121 {
049412e3 12122 dwo_sections->line.s.section = sectp;
fd361982 12123 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12124 }
fbedd546 12125 else if (names->loc_dwo.matches (sectp->name))
80626a55 12126 {
049412e3 12127 dwo_sections->loc.s.section = sectp;
fd361982 12128 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12129 }
fbedd546 12130 else if (names->loclists_dwo.matches (sectp->name))
41144253 12131 {
12132 dwo_sections->loclists.s.section = sectp;
12133 dwo_sections->loclists.size = bfd_section_size (sectp);
12134 }
fbedd546 12135 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12136 {
049412e3 12137 dwo_sections->macinfo.s.section = sectp;
fd361982 12138 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12139 }
fbedd546 12140 else if (names->macro_dwo.matches (sectp->name))
80626a55 12141 {
049412e3 12142 dwo_sections->macro.s.section = sectp;
fd361982 12143 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12144 }
fbedd546 12145 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12146 {
12147 dwo_sections->rnglists.s.section = sectp;
12148 dwo_sections->rnglists.size = bfd_section_size (sectp);
12149 }
fbedd546 12150 else if (names->str_dwo.matches (sectp->name))
80626a55 12151 {
049412e3 12152 dwo_sections->str.s.section = sectp;
fd361982 12153 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12154 }
fbedd546 12155 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12156 {
049412e3 12157 dwo_sections->str_offsets.s.section = sectp;
fd361982 12158 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12159 }
fbedd546 12160 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12161 {
12162 struct dwarf2_section_info type_section;
12163
12164 memset (&type_section, 0, sizeof (type_section));
049412e3 12165 type_section.s.section = sectp;
fd361982 12166 type_section.size = bfd_section_size (sectp);
fd5866f6 12167 dwo_sections->types.push_back (type_section);
80626a55
DE
12168 }
12169}
12170
ab5088bf 12171/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12172 by PER_CU. This is for the non-DWP case.
80626a55 12173 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12174
12175static struct dwo_file *
4ab09049
SM
12176open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12177 const char *comp_dir)
3019eac3 12178{
976ca316 12179 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12180
976ca316 12181 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12182 if (dbfd == NULL)
12183 {
6f738b01
SM
12184 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12185
80626a55
DE
12186 return NULL;
12187 }
263db9a1 12188
51ac9db5 12189 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12190 dwo_file->dwo_name = dwo_name;
12191 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12192 dwo_file->dbfd = std::move (dbfd);
3019eac3 12193
5bb6e9dd
TT
12194 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12195 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12196 &dwo_file->sections);
3019eac3 12197
976ca316
SM
12198 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12199 dwo_file->cus);
3019eac3 12200
d2854d8d
CT
12201 if (cu->per_cu->dwarf_version < 5)
12202 {
12203 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12204 dwo_file->sections.types, dwo_file->tus);
12205 }
12206 else
12207 {
12208 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12209 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12210 rcuh_kind::COMPILE);
d2854d8d 12211 }
3019eac3 12212
6f738b01 12213 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12214
263db9a1 12215 return dwo_file.release ();
3019eac3
DE
12216}
12217
80626a55 12218/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12219 size of each of the DWP debugging sections common to version 1 and 2 that
12220 we are interested in. */
3019eac3 12221
80626a55 12222static void
73869dc2 12223dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12224 dwp_file *dwp_file)
3019eac3 12225{
80626a55
DE
12226 const struct dwop_section_names *names = &dwop_section_names;
12227 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12228
80626a55 12229 /* Record the ELF section number for later lookup: this is what the
73869dc2 12230 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12231 gdb_assert (elf_section_nr < dwp_file->num_sections);
12232 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12233
80626a55 12234 /* Look for specific sections that we need. */
fbedd546 12235 if (names->str_dwo.matches (sectp->name))
80626a55 12236 {
049412e3 12237 dwp_file->sections.str.s.section = sectp;
fd361982 12238 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12239 }
fbedd546 12240 else if (names->cu_index.matches (sectp->name))
80626a55 12241 {
049412e3 12242 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12243 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12244 }
fbedd546 12245 else if (names->tu_index.matches (sectp->name))
80626a55 12246 {
049412e3 12247 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12248 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12249 }
12250}
3019eac3 12251
73869dc2
DE
12252/* This function is mapped across the sections and remembers the offset and
12253 size of each of the DWP version 2 debugging sections that we are interested
12254 in. This is split into a separate function because we don't know if we
d2854d8d 12255 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12256
12257static void
12258dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12259{
9a3c8263 12260 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12261 const struct dwop_section_names *names = &dwop_section_names;
12262 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12263
12264 /* Record the ELF section number for later lookup: this is what the
12265 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12266 gdb_assert (elf_section_nr < dwp_file->num_sections);
12267 dwp_file->elf_sections[elf_section_nr] = sectp;
12268
12269 /* Look for specific sections that we need. */
fbedd546 12270 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12271 {
049412e3 12272 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12273 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12274 }
fbedd546 12275 else if (names->info_dwo.matches (sectp->name))
73869dc2 12276 {
049412e3 12277 dwp_file->sections.info.s.section = sectp;
fd361982 12278 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12279 }
fbedd546 12280 else if (names->line_dwo.matches (sectp->name))
73869dc2 12281 {
049412e3 12282 dwp_file->sections.line.s.section = sectp;
fd361982 12283 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 12284 }
fbedd546 12285 else if (names->loc_dwo.matches (sectp->name))
73869dc2 12286 {
049412e3 12287 dwp_file->sections.loc.s.section = sectp;
fd361982 12288 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 12289 }
fbedd546 12290 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 12291 {
049412e3 12292 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12293 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 12294 }
fbedd546 12295 else if (names->macro_dwo.matches (sectp->name))
73869dc2 12296 {
049412e3 12297 dwp_file->sections.macro.s.section = sectp;
fd361982 12298 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 12299 }
fbedd546 12300 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 12301 {
049412e3 12302 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12303 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 12304 }
fbedd546 12305 else if (names->types_dwo.matches (sectp->name))
73869dc2 12306 {
049412e3 12307 dwp_file->sections.types.s.section = sectp;
fd361982 12308 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12309 }
12310}
12311
d2854d8d
CT
12312/* This function is mapped across the sections and remembers the offset and
12313 size of each of the DWP version 5 debugging sections that we are interested
12314 in. This is split into a separate function because we don't know if we
12315 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12316
12317static void
12318dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12319{
12320 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12321 const struct dwop_section_names *names = &dwop_section_names;
12322 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12323
12324 /* Record the ELF section number for later lookup: this is what the
12325 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12326 gdb_assert (elf_section_nr < dwp_file->num_sections);
12327 dwp_file->elf_sections[elf_section_nr] = sectp;
12328
12329 /* Look for specific sections that we need. */
fbedd546 12330 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
12331 {
12332 dwp_file->sections.abbrev.s.section = sectp;
12333 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12334 }
fbedd546 12335 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
12336 {
12337 dwp_file->sections.info.s.section = sectp;
12338 dwp_file->sections.info.size = bfd_section_size (sectp);
12339 }
fbedd546 12340 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
12341 {
12342 dwp_file->sections.line.s.section = sectp;
12343 dwp_file->sections.line.size = bfd_section_size (sectp);
12344 }
fbedd546 12345 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
12346 {
12347 dwp_file->sections.loclists.s.section = sectp;
12348 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12349 }
fbedd546 12350 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
12351 {
12352 dwp_file->sections.macro.s.section = sectp;
12353 dwp_file->sections.macro.size = bfd_section_size (sectp);
12354 }
fbedd546 12355 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
12356 {
12357 dwp_file->sections.rnglists.s.section = sectp;
12358 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12359 }
fbedd546 12360 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
12361 {
12362 dwp_file->sections.str_offsets.s.section = sectp;
12363 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12364 }
12365}
12366
80626a55 12367/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12368
80626a55
DE
12369static hashval_t
12370hash_dwp_loaded_cutus (const void *item)
12371{
9a3c8263 12372 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12373
80626a55
DE
12374 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12375 return dwo_unit->signature;
3019eac3
DE
12376}
12377
80626a55 12378/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12379
80626a55
DE
12380static int
12381eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12382{
9a3c8263
SM
12383 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12384 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12385
80626a55
DE
12386 return dua->signature == dub->signature;
12387}
3019eac3 12388
80626a55 12389/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12390
48b490f2 12391static htab_up
298e9637 12392allocate_dwp_loaded_cutus_table ()
80626a55 12393{
48b490f2
TT
12394 return htab_up (htab_create_alloc (3,
12395 hash_dwp_loaded_cutus,
12396 eq_dwp_loaded_cutus,
12397 NULL, xcalloc, xfree));
80626a55 12398}
3019eac3 12399
ab5088bf
DE
12400/* Try to open DWP file FILE_NAME.
12401 The result is the bfd handle of the file.
12402 If there is a problem finding or opening the file, return NULL.
12403 Upon success, the canonicalized path of the file is stored in the bfd,
12404 same as symfile_bfd_open. */
12405
192b62ce 12406static gdb_bfd_ref_ptr
976ca316 12407open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12408{
976ca316 12409 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12410 1 /*is_dwp*/,
192b62ce 12411 1 /*search_cwd*/));
6ac97d4c
DE
12412 if (abfd != NULL)
12413 return abfd;
12414
12415 /* Work around upstream bug 15652.
12416 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12417 [Whether that's a "bug" is debatable, but it is getting in our way.]
12418 We have no real idea where the dwp file is, because gdb's realpath-ing
12419 of the executable's path may have discarded the needed info.
12420 [IWBN if the dwp file name was recorded in the executable, akin to
12421 .gnu_debuglink, but that doesn't exist yet.]
12422 Strip the directory from FILE_NAME and search again. */
12423 if (*debug_file_directory != '\0')
12424 {
12425 /* Don't implicitly search the current directory here.
12426 If the user wants to search "." to handle this case,
12427 it must be added to debug-file-directory. */
976ca316
SM
12428 return try_open_dwop_file (per_objfile, lbasename (file_name),
12429 1 /*is_dwp*/,
6ac97d4c
DE
12430 0 /*search_cwd*/);
12431 }
12432
12433 return NULL;
ab5088bf
DE
12434}
12435
80626a55
DE
12436/* Initialize the use of the DWP file for the current objfile.
12437 By convention the name of the DWP file is ${objfile}.dwp.
12438 The result is NULL if it can't be found. */
a766d390 12439
400174b1 12440static std::unique_ptr<struct dwp_file>
976ca316 12441open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12442{
976ca316 12443 struct objfile *objfile = per_objfile->objfile;
80626a55 12444
82bf32bc
JK
12445 /* Try to find first .dwp for the binary file before any symbolic links
12446 resolving. */
6c447423
DE
12447
12448 /* If the objfile is a debug file, find the name of the real binary
12449 file and get the name of dwp file from there. */
d721ba37 12450 std::string dwp_name;
6c447423
DE
12451 if (objfile->separate_debug_objfile_backlink != NULL)
12452 {
12453 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12454 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12455
d721ba37 12456 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12457 }
12458 else
d721ba37
PA
12459 dwp_name = objfile->original_name;
12460
12461 dwp_name += ".dwp";
80626a55 12462
976ca316 12463 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12464 if (dbfd == NULL
12465 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12466 {
12467 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12468 dwp_name = objfile_name (objfile);
12469 dwp_name += ".dwp";
976ca316 12470 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12471 }
12472
80626a55
DE
12473 if (dbfd == NULL)
12474 {
6f738b01
SM
12475 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12476
400174b1 12477 return std::unique_ptr<dwp_file> ();
3019eac3 12478 }
400174b1
TT
12479
12480 const char *name = bfd_get_filename (dbfd.get ());
12481 std::unique_ptr<struct dwp_file> dwp_file
12482 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12483
0a0f4c01 12484 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12485 dwp_file->elf_sections =
976ca316 12486 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12487 dwp_file->num_sections, asection *);
12488
5bb6e9dd
TT
12489 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12490 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12491 dwp_file.get ());
80626a55 12492
976ca316 12493 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12494
976ca316 12495 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12496
73869dc2 12497 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12498 if (dwp_file->cus && dwp_file->tus
12499 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12500 {
12501 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12502 pretty bizarre. We use pulongest here because that's the established
4d65956b 12503 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12504 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12505 " TU version %s [in DWP file %s]"),
12506 pulongest (dwp_file->cus->version),
d721ba37 12507 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12508 }
08302ed2
DE
12509
12510 if (dwp_file->cus)
12511 dwp_file->version = dwp_file->cus->version;
12512 else if (dwp_file->tus)
12513 dwp_file->version = dwp_file->tus->version;
12514 else
12515 dwp_file->version = 2;
73869dc2 12516
5bb6e9dd
TT
12517 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12518 {
12519 if (dwp_file->version == 2)
12520 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12521 dwp_file.get ());
12522 else
12523 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12524 dwp_file.get ());
12525 }
73869dc2 12526
298e9637
SM
12527 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12528 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12529
6f738b01
SM
12530 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12531 dwarf_read_debug_printf (" %s CUs, %s TUs",
12532 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12533 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12534
12535 return dwp_file;
3019eac3 12536}
c906108c 12537
ab5088bf
DE
12538/* Wrapper around open_and_init_dwp_file, only open it once. */
12539
12540static struct dwp_file *
976ca316 12541get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12542{
976ca316 12543 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12544 {
976ca316
SM
12545 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12546 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12547 }
976ca316 12548 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12549}
12550
80626a55
DE
12551/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12552 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12553 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12554 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12555 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12556
12557 This is called, for example, when wanting to read a variable with a
12558 complex location. Therefore we don't want to do file i/o for every call.
12559 Therefore we don't want to look for a DWO file on every call.
12560 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12561 then we check if we've already seen DWO_NAME, and only THEN do we check
12562 for a DWO file.
12563
1c658ad5 12564 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12565 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12566
3019eac3 12567static struct dwo_unit *
4ab09049 12568lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12569 ULONGEST signature, int is_debug_types)
3019eac3 12570{
976ca316
SM
12571 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12572 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12573 const char *kind = is_debug_types ? "TU" : "CU";
12574 void **dwo_file_slot;
3019eac3 12575 struct dwo_file *dwo_file;
80626a55 12576 struct dwp_file *dwp_file;
cb1df416 12577
6a506a2d
DE
12578 /* First see if there's a DWP file.
12579 If we have a DWP file but didn't find the DWO inside it, don't
12580 look for the original DWO file. It makes gdb behave differently
12581 depending on whether one is debugging in the build tree. */
cf2c3c16 12582
976ca316 12583 dwp_file = get_dwp_file (per_objfile);
80626a55 12584 if (dwp_file != NULL)
cf2c3c16 12585 {
80626a55
DE
12586 const struct dwp_hash_table *dwp_htab =
12587 is_debug_types ? dwp_file->tus : dwp_file->cus;
12588
12589 if (dwp_htab != NULL)
12590 {
12591 struct dwo_unit *dwo_cutu =
976ca316
SM
12592 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12593 is_debug_types);
80626a55
DE
12594
12595 if (dwo_cutu != NULL)
12596 {
6f738b01
SM
12597 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12598 kind, hex_string (signature),
12599 host_address_to_string (dwo_cutu));
12600
80626a55
DE
12601 return dwo_cutu;
12602 }
12603 }
12604 }
6a506a2d 12605 else
80626a55 12606 {
6a506a2d 12607 /* No DWP file, look for the DWO file. */
80626a55 12608
976ca316 12609 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 12610 if (*dwo_file_slot == NULL)
80626a55 12611 {
6a506a2d 12612 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12613 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12614 }
6a506a2d 12615 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12616 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12617
6a506a2d 12618 if (dwo_file != NULL)
19c3d4c9 12619 {
6a506a2d
DE
12620 struct dwo_unit *dwo_cutu = NULL;
12621
12622 if (is_debug_types && dwo_file->tus)
12623 {
12624 struct dwo_unit find_dwo_cutu;
12625
12626 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12627 find_dwo_cutu.signature = signature;
9a3c8263 12628 dwo_cutu
b0b6a987
TT
12629 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12630 &find_dwo_cutu);
6a506a2d 12631 }
33c5cd75 12632 else if (!is_debug_types && dwo_file->cus)
80626a55 12633 {
33c5cd75
DB
12634 struct dwo_unit find_dwo_cutu;
12635
12636 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12637 find_dwo_cutu.signature = signature;
b0b6a987 12638 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12639 &find_dwo_cutu);
6a506a2d
DE
12640 }
12641
12642 if (dwo_cutu != NULL)
12643 {
6f738b01
SM
12644 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12645 kind, dwo_name, hex_string (signature),
12646 host_address_to_string (dwo_cutu));
12647
6a506a2d 12648 return dwo_cutu;
80626a55
DE
12649 }
12650 }
2e276125 12651 }
9cdd5dbd 12652
80626a55
DE
12653 /* We didn't find it. This could mean a dwo_id mismatch, or
12654 someone deleted the DWO/DWP file, or the search path isn't set up
12655 correctly to find the file. */
12656
6f738b01
SM
12657 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12658 kind, dwo_name, hex_string (signature));
3019eac3 12659
6656a72d
DE
12660 /* This is a warning and not a complaint because it can be caused by
12661 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12662 {
12663 /* Print the name of the DWP file if we looked there, helps the user
12664 better diagnose the problem. */
791afaa2 12665 std::string dwp_text;
43942612
DE
12666
12667 if (dwp_file != NULL)
791afaa2
TT
12668 dwp_text = string_printf (" [in DWP file %s]",
12669 lbasename (dwp_file->name));
43942612 12670
9d8780f0 12671 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12672 " [in module %s]"),
4ab09049
SM
12673 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12674 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12675 }
3019eac3 12676 return NULL;
5fb290d7
DJ
12677}
12678
80626a55
DE
12679/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12680 See lookup_dwo_cutu_unit for details. */
12681
12682static struct dwo_unit *
4ab09049 12683lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12684 ULONGEST signature)
12685{
4ab09049
SM
12686 gdb_assert (!cu->per_cu->is_debug_types);
12687
12688 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12689}
12690
12691/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12692 See lookup_dwo_cutu_unit for details. */
12693
12694static struct dwo_unit *
4ab09049 12695lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12696{
4ab09049
SM
12697 gdb_assert (cu->per_cu->is_debug_types);
12698
12699 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12700
12701 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12702}
12703
89e63ee4
DE
12704/* Traversal function for queue_and_load_all_dwo_tus. */
12705
12706static int
12707queue_and_load_dwo_tu (void **slot, void *info)
12708{
12709 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12710 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12711 ULONGEST signature = dwo_unit->signature;
d460f660 12712 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12713
12714 if (sig_type != NULL)
12715 {
89e63ee4
DE
12716 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12717 a real dependency of PER_CU on SIG_TYPE. That is detected later
12718 while processing PER_CU. */
0d305d5c 12719 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
3e9f1ca1 12720 cu->per_cu->lang))
0d305d5c
TT
12721 load_full_type_unit (sig_type, cu->per_objfile);
12722 cu->per_cu->imported_symtabs_push (sig_type);
89e63ee4
DE
12723 }
12724
12725 return 1;
12726}
12727
1b555f17 12728/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12729 The DWO may have the only definition of the type, though it may not be
12730 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12731 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12732
12733static void
1b555f17 12734queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12735{
12736 struct dwo_unit *dwo_unit;
12737 struct dwo_file *dwo_file;
12738
1b555f17
SM
12739 gdb_assert (cu != nullptr);
12740 gdb_assert (!cu->per_cu->is_debug_types);
12741 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12742
1b555f17 12743 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12744 gdb_assert (dwo_unit != NULL);
12745
12746 dwo_file = dwo_unit->dwo_file;
12747 if (dwo_file->tus != NULL)
1b555f17 12748 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
12749}
12750
3019eac3 12751/* Read in various DIEs. */
348e048f 12752
d389af10 12753/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12754 Inherit only the children of the DW_AT_abstract_origin DIE not being
12755 already referenced by DW_AT_abstract_origin from the children of the
12756 current DIE. */
d389af10
JK
12757
12758static void
12759inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12760{
12761 struct die_info *child_die;
791afaa2 12762 sect_offset *offsetp;
d389af10
JK
12763 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12764 struct die_info *origin_die;
12765 /* Iterator of the ORIGIN_DIE children. */
12766 struct die_info *origin_child_die;
d389af10 12767 struct attribute *attr;
cd02d79d
PA
12768 struct dwarf2_cu *origin_cu;
12769 struct pending **origin_previous_list_in_scope;
d389af10
JK
12770
12771 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12772 if (!attr)
12773 return;
12774
cd02d79d
PA
12775 /* Note that following die references may follow to a die in a
12776 different cu. */
12777
12778 origin_cu = cu;
12779 origin_die = follow_die_ref (die, attr, &origin_cu);
12780
12781 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12782 symbols in. */
12783 origin_previous_list_in_scope = origin_cu->list_in_scope;
12784 origin_cu->list_in_scope = cu->list_in_scope;
12785
edb3359d
DJ
12786 if (die->tag != origin_die->tag
12787 && !(die->tag == DW_TAG_inlined_subroutine
12788 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12789 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12790 sect_offset_str (die->sect_off),
12791 sect_offset_str (origin_die->sect_off));
d389af10 12792
34dc0f95
TBA
12793 /* Find if the concrete and abstract trees are structurally the
12794 same. This is a shallow traversal and it is not bullet-proof;
12795 the compiler can trick the debugger into believing that the trees
12796 are isomorphic, whereas they actually are not. However, the
12797 likelyhood of this happening is pretty low, and a full-fledged
12798 check would be an overkill. */
12799 bool are_isomorphic = true;
12800 die_info *concrete_child = die->child;
12801 die_info *abstract_child = origin_die->child;
12802 while (concrete_child != nullptr || abstract_child != nullptr)
12803 {
12804 if (concrete_child == nullptr
12805 || abstract_child == nullptr
12806 || concrete_child->tag != abstract_child->tag)
12807 {
12808 are_isomorphic = false;
12809 break;
12810 }
12811
12812 concrete_child = concrete_child->sibling;
12813 abstract_child = abstract_child->sibling;
12814 }
12815
12816 /* Walk the origin's children in parallel to the concrete children.
12817 This helps match an origin child in case the debug info misses
12818 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12819 origin tree may not have the same tree structure as the concrete
12820 DIE, though. */
12821 die_info *corresponding_abstract_child
12822 = are_isomorphic ? origin_die->child : nullptr;
12823
791afaa2 12824 std::vector<sect_offset> offsets;
d389af10 12825
3ea89b92
PMR
12826 for (child_die = die->child;
12827 child_die && child_die->tag;
436c571c 12828 child_die = child_die->sibling)
3ea89b92
PMR
12829 {
12830 struct die_info *child_origin_die;
12831 struct dwarf2_cu *child_origin_cu;
12832
12833 /* We are trying to process concrete instance entries:
216f72a1 12834 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12835 it's not relevant to our analysis here. i.e. detecting DIEs that are
12836 present in the abstract instance but not referenced in the concrete
12837 one. */
216f72a1 12838 if (child_die->tag == DW_TAG_call_site
dda83cd7 12839 || child_die->tag == DW_TAG_GNU_call_site)
34dc0f95
TBA
12840 {
12841 if (are_isomorphic)
12842 corresponding_abstract_child
12843 = corresponding_abstract_child->sibling;
12844 continue;
12845 }
3ea89b92 12846
c38f313d
DJ
12847 /* For each CHILD_DIE, find the corresponding child of
12848 ORIGIN_DIE. If there is more than one layer of
12849 DW_AT_abstract_origin, follow them all; there shouldn't be,
12850 but GCC versions at least through 4.4 generate this (GCC PR
12851 40573). */
3ea89b92
PMR
12852 child_origin_die = child_die;
12853 child_origin_cu = cu;
c38f313d
DJ
12854 while (1)
12855 {
cd02d79d
PA
12856 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12857 child_origin_cu);
c38f313d
DJ
12858 if (attr == NULL)
12859 break;
cd02d79d
PA
12860 child_origin_die = follow_die_ref (child_origin_die, attr,
12861 &child_origin_cu);
c38f313d
DJ
12862 }
12863
34dc0f95
TBA
12864 /* If missing DW_AT_abstract_origin, try the corresponding child
12865 of the origin. Clang emits such lexical scopes. */
12866 if (child_origin_die == child_die
12867 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12868 && are_isomorphic
12869 && child_die->tag == DW_TAG_lexical_block)
12870 child_origin_die = corresponding_abstract_child;
12871
d389af10
JK
12872 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12873 counterpart may exist. */
c38f313d 12874 if (child_origin_die != child_die)
d389af10 12875 {
edb3359d
DJ
12876 if (child_die->tag != child_origin_die->tag
12877 && !(child_die->tag == DW_TAG_inlined_subroutine
12878 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12879 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12880 "different tags"),
9d8780f0
SM
12881 sect_offset_str (child_die->sect_off),
12882 sect_offset_str (child_origin_die->sect_off));
c38f313d 12883 if (child_origin_die->parent != origin_die)
b98664d3 12884 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12885 "different parents"),
9d8780f0
SM
12886 sect_offset_str (child_die->sect_off),
12887 sect_offset_str (child_origin_die->sect_off));
c38f313d 12888 else
791afaa2 12889 offsets.push_back (child_origin_die->sect_off);
d389af10 12890 }
34dc0f95
TBA
12891
12892 if (are_isomorphic)
12893 corresponding_abstract_child = corresponding_abstract_child->sibling;
d389af10 12894 }
791afaa2
TT
12895 std::sort (offsets.begin (), offsets.end ());
12896 sect_offset *offsets_end = offsets.data () + offsets.size ();
12897 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12898 if (offsetp[-1] == *offsetp)
b98664d3 12899 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12900 "to DIE %s as their abstract origin"),
12901 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12902
791afaa2 12903 offsetp = offsets.data ();
d389af10
JK
12904 origin_child_die = origin_die->child;
12905 while (origin_child_die && origin_child_die->tag)
12906 {
12907 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12908 while (offsetp < offsets_end
9c541725 12909 && *offsetp < origin_child_die->sect_off)
d389af10 12910 offsetp++;
b64f50a1 12911 if (offsetp >= offsets_end
9c541725 12912 || *offsetp > origin_child_die->sect_off)
d389af10 12913 {
adde2bff
DE
12914 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12915 Check whether we're already processing ORIGIN_CHILD_DIE.
12916 This can happen with mutually referenced abstract_origins.
12917 PR 16581. */
12918 if (!origin_child_die->in_process)
12919 process_die (origin_child_die, origin_cu);
d389af10 12920 }
436c571c 12921 origin_child_die = origin_child_die->sibling;
d389af10 12922 }
cd02d79d 12923 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12924
12925 if (cu != origin_cu)
12926 compute_delayed_physnames (origin_cu);
d389af10
JK
12927}
12928
c906108c 12929static void
e7c27a73 12930read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12931{
5e22e966 12932 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 12933 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 12934 struct context_stack *newobj;
c906108c
SS
12935 CORE_ADDR lowpc;
12936 CORE_ADDR highpc;
12937 struct die_info *child_die;
edb3359d 12938 struct attribute *attr, *call_line, *call_file;
15d034d0 12939 const char *name;
e142c38c 12940 CORE_ADDR baseaddr;
801e3a5b 12941 struct block *block;
edb3359d 12942 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12943 std::vector<struct symbol *> template_args;
34eaf542 12944 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12945
12946 if (inlined_func)
12947 {
12948 /* If we do not have call site information, we can't show the
12949 caller of this inlined function. That's too confusing, so
12950 only use the scope for local variables. */
12951 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12952 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12953 if (call_line == NULL || call_file == NULL)
12954 {
12955 read_lexical_block_scope (die, cu);
12956 return;
12957 }
12958 }
c906108c 12959
b3b3bada 12960 baseaddr = objfile->text_section_offset ();
e142c38c 12961
94af9270 12962 name = dwarf2_name (die, cu);
c906108c 12963
e8d05480
JB
12964 /* Ignore functions with missing or empty names. These are actually
12965 illegal according to the DWARF standard. */
12966 if (name == NULL)
12967 {
b98664d3 12968 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12969 sect_offset_str (die->sect_off));
e8d05480
JB
12970 return;
12971 }
12972
12973 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12974 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12975 <= PC_BOUNDS_INVALID)
e8d05480 12976 {
ae4d0c03 12977 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 12978 if (attr == nullptr || !attr->as_boolean ())
b98664d3 12979 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12980 "for subprogram DIE at %s"),
12981 sect_offset_str (die->sect_off));
e8d05480
JB
12982 return;
12983 }
c906108c 12984
3e29f34a
MR
12985 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12986 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12987
34eaf542
TT
12988 /* If we have any template arguments, then we must allocate a
12989 different sort of symbol. */
436c571c 12990 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
12991 {
12992 if (child_die->tag == DW_TAG_template_type_param
12993 || child_die->tag == DW_TAG_template_value_param)
12994 {
8c14c3a3 12995 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 12996 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12997 break;
12998 }
12999 }
13000
702cf3f5 13001 gdb_assert (cu->get_builder () != nullptr);
c24bdb02 13002 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13003 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13004 (struct symbol *) templ_func);
4c2df51b 13005
81873cc8 13006 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13007 set_objfile_main_name (objfile, newobj->name->linkage_name (),
3e9f1ca1 13008 cu->per_cu->lang);
81873cc8 13009
4cecd739
DJ
13010 /* If there is a location expression for DW_AT_frame_base, record
13011 it. */
e142c38c 13012 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13013 if (attr != nullptr)
fe978cb0 13014 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13015
63e43d3a
PMR
13016 /* If there is a location for the static link, record it. */
13017 newobj->static_link = NULL;
13018 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13019 if (attr != nullptr)
63e43d3a 13020 {
224c3ddb
SM
13021 newobj->static_link
13022 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13023 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13024 cu->addr_type ());
63e43d3a
PMR
13025 }
13026
c24bdb02 13027 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13028
639d11d3 13029 if (die->child != NULL)
c906108c 13030 {
639d11d3 13031 child_die = die->child;
c906108c
SS
13032 while (child_die && child_die->tag)
13033 {
34eaf542
TT
13034 if (child_die->tag == DW_TAG_template_type_param
13035 || child_die->tag == DW_TAG_template_value_param)
13036 {
13037 struct symbol *arg = new_symbol (child_die, NULL, cu);
13038
f1078f66 13039 if (arg != NULL)
2f4732b0 13040 template_args.push_back (arg);
34eaf542
TT
13041 }
13042 else
13043 process_die (child_die, cu);
436c571c 13044 child_die = child_die->sibling;
c906108c
SS
13045 }
13046 }
13047
d389af10
JK
13048 inherit_abstract_dies (die, cu);
13049
4a811a97
UW
13050 /* If we have a DW_AT_specification, we might need to import using
13051 directives from the context of the specification DIE. See the
13052 comment in determine_prefix. */
3e9f1ca1 13053 if (cu->per_cu->lang == language_cplus
4a811a97
UW
13054 && dwarf2_attr (die, DW_AT_specification, cu))
13055 {
13056 struct dwarf2_cu *spec_cu = cu;
13057 struct die_info *spec_die = die_specification (die, &spec_cu);
13058
13059 while (spec_die)
13060 {
13061 child_die = spec_die->child;
13062 while (child_die && child_die->tag)
13063 {
13064 if (child_die->tag == DW_TAG_imported_module)
13065 process_die (child_die, spec_cu);
436c571c 13066 child_die = child_die->sibling;
4a811a97
UW
13067 }
13068
13069 /* In some cases, GCC generates specification DIEs that
13070 themselves contain DW_AT_specification attributes. */
13071 spec_die = die_specification (spec_die, &spec_cu);
13072 }
13073 }
13074
c24bdb02 13075 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13076 /* Make a block for the local symbols within. */
c24bdb02 13077 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13078 cstk.static_link, lowpc, highpc);
801e3a5b 13079
df8a16a1 13080 /* For C++, set the block's scope. */
3e9f1ca1
TT
13081 if ((cu->per_cu->lang == language_cplus
13082 || cu->per_cu->lang == language_fortran
13083 || cu->per_cu->lang == language_d
13084 || cu->per_cu->lang == language_rust)
4d4ec4e5 13085 && cu->processing_has_namespace_info)
195a3f6c
TT
13086 block_set_scope (block, determine_prefix (die, cu),
13087 &objfile->objfile_obstack);
df8a16a1 13088
801e3a5b
JB
13089 /* If we have address ranges, record them. */
13090 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13091
a60f3166 13092 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13093
34eaf542 13094 /* Attach template arguments to function. */
2f4732b0 13095 if (!template_args.empty ())
34eaf542
TT
13096 {
13097 gdb_assert (templ_func != NULL);
13098
2f4732b0 13099 templ_func->n_template_arguments = template_args.size ();
34eaf542 13100 templ_func->template_arguments
dda83cd7 13101 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13102 templ_func->n_template_arguments);
34eaf542 13103 memcpy (templ_func->template_arguments,
2f4732b0 13104 template_args.data (),
34eaf542 13105 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13106
13107 /* Make sure that the symtab is set on the new symbols. Even
13108 though they don't appear in this symtab directly, other parts
13109 of gdb assume that symbols do, and this is reasonably
13110 true. */
8634679f 13111 for (symbol *sym : template_args)
3e1d3d8c 13112 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13113 }
13114
208d8187
JB
13115 /* In C++, we can have functions nested inside functions (e.g., when
13116 a function declares a class that has methods). This means that
13117 when we finish processing a function scope, we may need to go
13118 back to building a containing block's symbol lists. */
c24bdb02
KS
13119 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13120 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13121
921e78cf
JB
13122 /* If we've finished processing a top-level function, subsequent
13123 symbols go in the file symbol list. */
c24bdb02
KS
13124 if (cu->get_builder ()->outermost_context_p ())
13125 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13126}
13127
13128/* Process all the DIES contained within a lexical block scope. Start
13129 a new scope, process the dies, and then close the scope. */
13130
13131static void
e7c27a73 13132read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13133{
5e22e966 13134 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13135 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13136 CORE_ADDR lowpc, highpc;
13137 struct die_info *child_die;
e142c38c
DJ
13138 CORE_ADDR baseaddr;
13139
b3b3bada 13140 baseaddr = objfile->text_section_offset ();
c906108c
SS
13141
13142 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13143 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13144 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13145 be nasty. Might be easier to properly extend generic blocks to
af34e669 13146 describe ranges. */
e385593e
JK
13147 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13148 {
13149 case PC_BOUNDS_NOT_PRESENT:
13150 /* DW_TAG_lexical_block has no attributes, process its children as if
13151 there was no wrapping by that DW_TAG_lexical_block.
13152 GCC does no longer produces such DWARF since GCC r224161. */
13153 for (child_die = die->child;
13154 child_die != NULL && child_die->tag;
436c571c 13155 child_die = child_die->sibling)
4f7bc5ed
TT
13156 {
13157 /* We might already be processing this DIE. This can happen
13158 in an unusual circumstance -- where a subroutine A
13159 appears lexically in another subroutine B, but A actually
13160 inlines B. The recursion is broken here, rather than in
13161 inherit_abstract_dies, because it seems better to simply
13162 drop concrete children here. */
13163 if (!child_die->in_process)
13164 process_die (child_die, cu);
13165 }
e385593e
JK
13166 return;
13167 case PC_BOUNDS_INVALID:
13168 return;
13169 }
3e29f34a
MR
13170 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13171 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13172
c24bdb02 13173 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13174 if (die->child != NULL)
c906108c 13175 {
639d11d3 13176 child_die = die->child;
c906108c
SS
13177 while (child_die && child_die->tag)
13178 {
e7c27a73 13179 process_die (child_die, cu);
436c571c 13180 child_die = child_die->sibling;
c906108c
SS
13181 }
13182 }
3ea89b92 13183 inherit_abstract_dies (die, cu);
c24bdb02 13184 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13185
c24bdb02
KS
13186 if (*cu->get_builder ()->get_local_symbols () != NULL
13187 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13188 {
801e3a5b 13189 struct block *block
dda83cd7 13190 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13191 cstk.start_addr, highpc);
801e3a5b
JB
13192
13193 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13194 do here, means that recording a parent's ranges entails
13195 walking across all its children's ranges as they appear in
13196 the address map, which is quadratic behavior.
13197
13198 It would be nicer to record the parent's ranges before
13199 traversing its children, simply overriding whatever you find
13200 there. But since we don't even decide whether to create a
13201 block until after we've traversed its children, that's hard
13202 to do. */
801e3a5b 13203 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13204 }
c24bdb02
KS
13205 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13206 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13207}
13208
216f72a1 13209/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13210
13211static void
13212read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13213{
5e22e966 13214 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13215 struct objfile *objfile = per_objfile->objfile;
08feed99 13216 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13217 CORE_ADDR pc, baseaddr;
13218 struct attribute *attr;
13219 struct call_site *call_site, call_site_local;
13220 void **slot;
13221 int nparams;
13222 struct die_info *child_die;
13223
b3b3bada 13224 baseaddr = objfile->text_section_offset ();
96408a79 13225
216f72a1
JK
13226 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13227 if (attr == NULL)
13228 {
13229 /* This was a pre-DWARF-5 GNU extension alias
13230 for DW_AT_call_return_pc. */
13231 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13232 }
96408a79
SA
13233 if (!attr)
13234 {
b98664d3 13235 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13236 "DIE %s [in module %s]"),
13237 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13238 return;
13239 }
95f982e5 13240 pc = attr->as_address () + baseaddr;
3e29f34a 13241 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13242
13243 if (cu->call_site_htab == NULL)
13244 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13245 NULL, &objfile->objfile_obstack,
13246 hashtab_obstack_allocate, NULL);
13247 call_site_local.pc = pc;
13248 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13249 if (*slot != NULL)
13250 {
b98664d3 13251 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13252 "DIE %s [in module %s]"),
13253 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13254 objfile_name (objfile));
96408a79
SA
13255 return;
13256 }
13257
13258 /* Count parameters at the caller. */
13259
13260 nparams = 0;
13261 for (child_die = die->child; child_die && child_die->tag;
436c571c 13262 child_die = child_die->sibling)
96408a79 13263 {
216f72a1 13264 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13265 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13266 {
b98664d3 13267 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13268 "DW_TAG_call_site child DIE %s [in module %s]"),
13269 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13270 objfile_name (objfile));
96408a79
SA
13271 continue;
13272 }
13273
13274 nparams++;
13275 }
13276
224c3ddb
SM
13277 call_site
13278 = ((struct call_site *)
13279 obstack_alloc (&objfile->objfile_obstack,
13280 sizeof (*call_site)
13281 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13282 *slot = call_site;
13283 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13284 call_site->pc = pc;
13285
216f72a1
JK
13286 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13287 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13288 {
13289 struct die_info *func_die;
13290
13291 /* Skip also over DW_TAG_inlined_subroutine. */
13292 for (func_die = die->parent;
13293 func_die && func_die->tag != DW_TAG_subprogram
13294 && func_die->tag != DW_TAG_subroutine_type;
13295 func_die = func_die->parent);
13296
216f72a1
JK
13297 /* DW_AT_call_all_calls is a superset
13298 of DW_AT_call_all_tail_calls. */
96408a79 13299 if (func_die
dda83cd7
SM
13300 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13301 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13302 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13303 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13304 {
13305 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13306 not complete. But keep CALL_SITE for look ups via call_site_htab,
13307 both the initial caller containing the real return address PC and
13308 the final callee containing the current PC of a chain of tail
13309 calls do not need to have the tail call list complete. But any
13310 function candidate for a virtual tail call frame searched via
13311 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13312 determined unambiguously. */
13313 }
13314 else
13315 {
13316 struct type *func_type = NULL;
13317
13318 if (func_die)
13319 func_type = get_die_type (func_die, cu);
13320 if (func_type != NULL)
13321 {
78134374 13322 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13323
13324 /* Enlist this call site to the function. */
13325 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13326 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13327 }
13328 else
b98664d3 13329 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13330 "DIE %s [in module %s]"),
13331 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13332 }
13333 }
13334
216f72a1
JK
13335 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13336 if (attr == NULL)
13337 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13338 if (attr == NULL)
13339 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13340 if (attr == NULL)
216f72a1
JK
13341 {
13342 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13343 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13344 }
96408a79 13345 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13346 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13347 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13348 else if (attr->form_is_block ())
96408a79
SA
13349 {
13350 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13351 struct dwarf_block *block = attr->as_block ();
96408a79 13352
8d749320 13353 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13354 dlbaton->data = block->data;
13355 dlbaton->size = block->size;
a50264ba 13356 dlbaton->per_objfile = per_objfile;
96408a79
SA
13357 dlbaton->per_cu = cu->per_cu;
13358
13359 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13360 }
cd6c91b4 13361 else if (attr->form_is_ref ())
96408a79 13362 {
96408a79
SA
13363 struct dwarf2_cu *target_cu = cu;
13364 struct die_info *target_die;
13365
ac9ec31b 13366 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13367 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13368 if (die_is_declaration (target_die, target_cu))
13369 {
7d45c7c3 13370 const char *target_physname;
9112db09
JK
13371
13372 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13373 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13374 if (target_physname == NULL)
9112db09 13375 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13376 if (target_physname == NULL)
b98664d3 13377 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13378 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 13379 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13380 else
7d455152 13381 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13382 }
13383 else
13384 {
13385 CORE_ADDR lowpc;
13386
13387 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13388 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13389 <= PC_BOUNDS_INVALID)
b98664d3 13390 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13391 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 13392 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13393 else
3e29f34a
MR
13394 {
13395 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13396 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13397 }
96408a79
SA
13398 }
13399 }
13400 else
b98664d3 13401 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13402 "block nor reference, for DIE %s [in module %s]"),
13403 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13404
13405 call_site->per_cu = cu->per_cu;
9f47c707 13406 call_site->per_objfile = per_objfile;
96408a79
SA
13407
13408 for (child_die = die->child;
13409 child_die && child_die->tag;
436c571c 13410 child_die = child_die->sibling)
96408a79 13411 {
96408a79 13412 struct call_site_parameter *parameter;
1788b2d3 13413 struct attribute *loc, *origin;
96408a79 13414
216f72a1 13415 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13416 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13417 {
13418 /* Already printed the complaint above. */
13419 continue;
13420 }
13421
13422 gdb_assert (call_site->parameter_count < nparams);
13423 parameter = &call_site->parameter[call_site->parameter_count];
13424
1788b2d3
JK
13425 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13426 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13427 register is contained in DW_AT_call_value. */
96408a79 13428
24c5c679 13429 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13430 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13431 if (origin == NULL)
13432 {
13433 /* This was a pre-DWARF-5 GNU extension alias
13434 for DW_AT_call_parameter. */
13435 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13436 }
cd6c91b4 13437 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13438 {
1788b2d3 13439 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13440
0826b30a 13441 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13442 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13443 {
13444 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13445 binding can be done only inside one CU. Such referenced DIE
13446 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13447 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13448 "DW_TAG_call_site child DIE %s [in module %s]"),
13449 sect_offset_str (child_die->sect_off),
9c541725 13450 objfile_name (objfile));
d76b7dbc
JK
13451 continue;
13452 }
9c541725
PA
13453 parameter->u.param_cu_off
13454 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13455 }
4fc6c0d5 13456 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13457 {
b98664d3 13458 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13459 "DW_TAG_call_site child DIE %s [in module %s]"),
13460 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13461 continue;
13462 }
24c5c679 13463 else
96408a79 13464 {
9d2246fc
TT
13465 struct dwarf_block *block = loc->as_block ();
13466
24c5c679 13467 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 13468 (block->data, &block->data[block->size]);
24c5c679
JK
13469 if (parameter->u.dwarf_reg != -1)
13470 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
13471 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13472 &block->data[block->size],
24c5c679
JK
13473 &parameter->u.fb_offset))
13474 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13475 else
13476 {
b98664d3 13477 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13478 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13479 "DW_TAG_call_site child DIE %s "
24c5c679 13480 "[in module %s]"),
9d8780f0 13481 sect_offset_str (child_die->sect_off),
9c541725 13482 objfile_name (objfile));
24c5c679
JK
13483 continue;
13484 }
96408a79
SA
13485 }
13486
216f72a1
JK
13487 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13488 if (attr == NULL)
13489 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13490 if (attr == NULL || !attr->form_is_block ())
96408a79 13491 {
b98664d3 13492 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13493 "DW_TAG_call_site child DIE %s [in module %s]"),
13494 sect_offset_str (child_die->sect_off),
9c541725 13495 objfile_name (objfile));
96408a79
SA
13496 continue;
13497 }
9d2246fc
TT
13498
13499 struct dwarf_block *block = attr->as_block ();
13500 parameter->value = block->data;
13501 parameter->value_size = block->size;
96408a79
SA
13502
13503 /* Parameters are not pre-cleared by memset above. */
13504 parameter->data_value = NULL;
13505 parameter->data_value_size = 0;
13506 call_site->parameter_count++;
13507
216f72a1
JK
13508 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13509 if (attr == NULL)
13510 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13511 if (attr != nullptr)
96408a79 13512 {
4fc6c0d5 13513 if (!attr->form_is_block ())
b98664d3 13514 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13515 "DW_TAG_call_site child DIE %s [in module %s]"),
13516 sect_offset_str (child_die->sect_off),
9c541725 13517 objfile_name (objfile));
96408a79
SA
13518 else
13519 {
9d2246fc
TT
13520 block = attr->as_block ();
13521 parameter->data_value = block->data;
13522 parameter->data_value_size = block->size;
96408a79
SA
13523 }
13524 }
13525 }
13526}
13527
71a3c369
TT
13528/* Helper function for read_variable. If DIE represents a virtual
13529 table, then return the type of the concrete object that is
13530 associated with the virtual table. Otherwise, return NULL. */
13531
13532static struct type *
13533rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13534{
13535 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13536 if (attr == NULL)
13537 return NULL;
13538
13539 /* Find the type DIE. */
13540 struct die_info *type_die = NULL;
13541 struct dwarf2_cu *type_cu = cu;
13542
cd6c91b4 13543 if (attr->form_is_ref ())
71a3c369
TT
13544 type_die = follow_die_ref (die, attr, &type_cu);
13545 if (type_die == NULL)
13546 return NULL;
13547
13548 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13549 return NULL;
13550 return die_containing_type (type_die, type_cu);
13551}
13552
13553/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13554
13555static void
13556read_variable (struct die_info *die, struct dwarf2_cu *cu)
13557{
13558 struct rust_vtable_symbol *storage = NULL;
13559
3e9f1ca1 13560 if (cu->per_cu->lang == language_rust)
71a3c369
TT
13561 {
13562 struct type *containing_type = rust_containing_type (die, cu);
13563
13564 if (containing_type != NULL)
13565 {
5e22e966 13566 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13567
8c14c3a3 13568 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13569 storage->concrete_type = containing_type;
cf724bc9 13570 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13571 }
13572 }
13573
e4a62c65
TV
13574 struct symbol *res = new_symbol (die, NULL, cu, storage);
13575 struct attribute *abstract_origin
13576 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13577 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13578 if (res == NULL && loc && abstract_origin)
13579 {
13580 /* We have a variable without a name, but with a location and an abstract
13581 origin. This may be a concrete instance of an abstract variable
13582 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13583 later. */
13584 struct dwarf2_cu *origin_cu = cu;
13585 struct die_info *origin_die
13586 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13587 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13588 per_objfile->per_bfd->abstract_to_concrete
13589 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13590 }
71a3c369
TT
13591}
13592
43988095
JK
13593/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13594 reading .debug_rnglists.
13595 Callback's type should be:
13596 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13597 Return true if the attributes are present and valid, otherwise,
13598 return false. */
13599
13600template <typename Callback>
13601static bool
13602dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 13603 dwarf_tag tag, Callback &&callback)
43988095 13604{
976ca316
SM
13605 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13606 struct objfile *objfile = per_objfile->objfile;
43988095 13607 bfd *obfd = objfile->obfd;
43988095 13608 /* Base address selection entry. */
2b24b6e4 13609 gdb::optional<CORE_ADDR> base;
43988095 13610 const gdb_byte *buffer;
43988095 13611 bool overflow = false;
d0ce17d8
CT
13612 ULONGEST addr_index;
13613 struct dwarf2_section_info *rnglists_section;
43988095 13614
43988095 13615 base = cu->base_address;
d0ce17d8
CT
13616 rnglists_section = cu_debug_rnglists_section (cu, tag);
13617 rnglists_section->read (objfile);
43988095 13618
d0ce17d8 13619 if (offset >= rnglists_section->size)
43988095 13620 {
b98664d3 13621 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13622 offset);
13623 return false;
13624 }
d0ce17d8 13625 buffer = rnglists_section->buffer + offset;
43988095 13626
43988095
JK
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. */
a656ee08 13768 if (range_beginning == 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;
43039443 13810
43988095 13811 if (cu_header->version >= 5)
d0ce17d8 13812 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 13813
d00adf39 13814 base = cu->base_address;
43039443 13815
5e22e966
SM
13816 per_objfile->per_bfd->ranges.read (objfile);
13817 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13818 {
b98664d3 13819 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13820 offset);
13821 return 0;
13822 }
5e22e966 13823 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13824
43039443
JK
13825 while (1)
13826 {
13827 CORE_ADDR range_beginning, range_end;
13828
c8a7a66f 13829 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13830 buffer += addr_size;
c8a7a66f 13831 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13832 buffer += addr_size;
13833 offset += 2 * addr_size;
13834
13835 /* An end of list marker is a pair of zero addresses. */
13836 if (range_beginning == 0 && range_end == 0)
13837 /* Found the end of list entry. */
13838 break;
13839
13840 /* Each base address selection entry is a pair of 2 values.
13841 The first is the largest possible address, the second is
13842 the base address. Check for a base address here. */
13843 if ((range_beginning & mask) == mask)
13844 {
28d2bfb9
AB
13845 /* If we found the largest possible address, then we already
13846 have the base address in range_end. */
13847 base = range_end;
43039443
JK
13848 continue;
13849 }
13850
2b24b6e4 13851 if (!base.has_value ())
43039443
JK
13852 {
13853 /* We have no valid base address for the ranges
13854 data. */
b98664d3 13855 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13856 return 0;
13857 }
13858
9277c30c
UW
13859 if (range_beginning > range_end)
13860 {
13861 /* Inverted range entries are invalid. */
b98664d3 13862 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13863 return 0;
13864 }
13865
13866 /* Empty range entries have no effect. */
13867 if (range_beginning == range_end)
13868 continue;
13869
2b24b6e4
TT
13870 range_beginning += *base;
13871 range_end += *base;
43039443 13872
01093045
DE
13873 /* A not-uncommon case of bad debug info.
13874 Don't pollute the addrmap with bad data. */
a656ee08 13875 if (range_beginning == 0
5e22e966 13876 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13877 {
b98664d3 13878 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13879 " [in module %s]"), objfile_name (objfile));
01093045
DE
13880 continue;
13881 }
13882
5f46c5a5
JK
13883 callback (range_beginning, range_end);
13884 }
13885
13886 return 1;
13887}
13888
13889/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13890 Return 1 if the attributes are present and valid, otherwise, return 0.
efd7398e 13891 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
5f46c5a5
JK
13892
13893static int
13894dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13895 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 13896 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 13897{
5e22e966 13898 struct objfile *objfile = cu->per_objfile->objfile;
84685904 13899 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 13900 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13901 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13902 int low_set = 0;
13903 CORE_ADDR low = 0;
13904 CORE_ADDR high = 0;
13905 int retval;
13906
d0ce17d8 13907 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
13908 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13909 {
9277c30c 13910 if (ranges_pst != NULL)
3e29f34a
MR
13911 {
13912 CORE_ADDR lowpc;
13913 CORE_ADDR highpc;
13914
79748972
TT
13915 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13916 range_beginning + baseaddr)
13917 - baseaddr);
13918 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13919 range_end + baseaddr)
13920 - baseaddr);
84685904 13921 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 13922 lowpc, highpc - 1, ranges_pst);
3e29f34a 13923 }
ff013f42 13924
43039443
JK
13925 /* FIXME: This is recording everything as a low-high
13926 segment of consecutive addresses. We should have a
13927 data structure for discontiguous block ranges
13928 instead. */
13929 if (! low_set)
13930 {
13931 low = range_beginning;
13932 high = range_end;
13933 low_set = 1;
13934 }
13935 else
13936 {
13937 if (range_beginning < low)
13938 low = range_beginning;
13939 if (range_end > high)
13940 high = range_end;
13941 }
5f46c5a5
JK
13942 });
13943 if (!retval)
13944 return 0;
43039443
JK
13945
13946 if (! low_set)
13947 /* If the first entry is an end-of-list marker, the range
13948 describes an empty scope, i.e. no instructions. */
13949 return 0;
13950
13951 if (low_return)
13952 *low_return = low;
13953 if (high_return)
13954 *high_return = high;
13955 return 1;
13956}
13957
3a2b436a
JK
13958/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13959 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13960 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13961
3a2b436a 13962static enum pc_bounds_kind
af34e669 13963dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13964 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13965 dwarf2_psymtab *pst)
c906108c 13966{
976ca316 13967 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 13968 struct attribute *attr;
91da1414 13969 struct attribute *attr_high;
af34e669
DJ
13970 CORE_ADDR low = 0;
13971 CORE_ADDR high = 0;
e385593e 13972 enum pc_bounds_kind ret;
c906108c 13973
91da1414
MW
13974 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13975 if (attr_high)
af34e669 13976 {
e142c38c 13977 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13978 if (attr != nullptr)
dda83cd7 13979 {
95f982e5
TT
13980 low = attr->as_address ();
13981 high = attr_high->as_address ();
cd6c91b4 13982 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13983 high += low;
91da1414 13984 }
af34e669
DJ
13985 else
13986 /* Found high w/o low attribute. */
e385593e 13987 return PC_BOUNDS_INVALID;
af34e669
DJ
13988
13989 /* Found consecutive range of addresses. */
3a2b436a 13990 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13991 }
c906108c 13992 else
af34e669 13993 {
e142c38c 13994 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 13995 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 13996 {
2b0c7f41
SM
13997 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13998 on DWARF version). */
13999 ULONGEST ranges_offset = attr->as_unsigned ();
14000
14001 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14002 this value. */
14003 if (die->tag != DW_TAG_compile_unit)
14004 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14005
af34e669 14006 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14007 .debug_ranges section. */
d0ce17d8
CT
14008 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14009 die->tag))
e385593e 14010 return PC_BOUNDS_INVALID;
43039443 14011 /* Found discontinuous range of addresses. */
3a2b436a 14012 ret = PC_BOUNDS_RANGES;
af34e669 14013 }
e385593e
JK
14014 else
14015 return PC_BOUNDS_NOT_PRESENT;
af34e669 14016 }
c906108c 14017
48fbe735 14018 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14019 if (high <= low)
e385593e 14020 return PC_BOUNDS_INVALID;
c906108c
SS
14021
14022 /* When using the GNU linker, .gnu.linkonce. sections are used to
14023 eliminate duplicate copies of functions and vtables and such.
14024 The linker will arbitrarily choose one and discard the others.
14025 The AT_*_pc values for such functions refer to local labels in
14026 these sections. If the section from that file was discarded, the
14027 labels are not in the output, so the relocs get a value of 0.
14028 If this is a discarded function, mark the pc bounds as invalid,
14029 so that GDB will ignore it. */
976ca316 14030 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14031 return PC_BOUNDS_INVALID;
c906108c
SS
14032
14033 *lowpc = low;
96408a79
SA
14034 if (highpc)
14035 *highpc = high;
af34e669 14036 return ret;
c906108c
SS
14037}
14038
b084d499
JB
14039/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14040 its low and high PC addresses. Do nothing if these addresses could not
14041 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14042 and HIGHPC to the high address if greater than HIGHPC. */
14043
14044static void
14045dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14046 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14047 struct dwarf2_cu *cu)
b084d499
JB
14048{
14049 CORE_ADDR low, high;
14050 struct die_info *child = die->child;
14051
e385593e 14052 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14053 {
325fac50
PA
14054 *lowpc = std::min (*lowpc, low);
14055 *highpc = std::max (*highpc, high);
b084d499
JB
14056 }
14057
14058 /* If the language does not allow nested subprograms (either inside
14059 subprograms or lexical blocks), we're done. */
3e9f1ca1 14060 if (cu->per_cu->lang != language_ada)
b084d499 14061 return;
6e70227d 14062
b084d499
JB
14063 /* Check all the children of the given DIE. If it contains nested
14064 subprograms, then check their pc bounds. Likewise, we need to
14065 check lexical blocks as well, as they may also contain subprogram
14066 definitions. */
14067 while (child && child->tag)
14068 {
14069 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14070 || child->tag == DW_TAG_lexical_block)
14071 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14072 child = child->sibling;
b084d499
JB
14073 }
14074}
14075
fae299cd
DC
14076/* Get the low and high pc's represented by the scope DIE, and store
14077 them in *LOWPC and *HIGHPC. If the correct values can't be
14078 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14079
14080static void
14081get_scope_pc_bounds (struct die_info *die,
14082 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14083 struct dwarf2_cu *cu)
14084{
14085 CORE_ADDR best_low = (CORE_ADDR) -1;
14086 CORE_ADDR best_high = (CORE_ADDR) 0;
14087 CORE_ADDR current_low, current_high;
14088
3a2b436a 14089 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14090 >= PC_BOUNDS_RANGES)
fae299cd
DC
14091 {
14092 best_low = current_low;
14093 best_high = current_high;
14094 }
14095 else
14096 {
14097 struct die_info *child = die->child;
14098
14099 while (child && child->tag)
14100 {
14101 switch (child->tag) {
14102 case DW_TAG_subprogram:
dda83cd7 14103 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14104 break;
14105 case DW_TAG_namespace:
f55ee35c 14106 case DW_TAG_module:
fae299cd
DC
14107 /* FIXME: carlton/2004-01-16: Should we do this for
14108 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14109 that current GCC's always emit the DIEs corresponding
14110 to definitions of methods of classes as children of a
14111 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14112 the DIEs giving the declarations, which could be
14113 anywhere). But I don't see any reason why the
14114 standards says that they have to be there. */
14115 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14116
14117 if (current_low != ((CORE_ADDR) -1))
14118 {
325fac50
PA
14119 best_low = std::min (best_low, current_low);
14120 best_high = std::max (best_high, current_high);
fae299cd
DC
14121 }
14122 break;
14123 default:
0963b4bd 14124 /* Ignore. */
fae299cd
DC
14125 break;
14126 }
14127
436c571c 14128 child = child->sibling;
fae299cd
DC
14129 }
14130 }
14131
14132 *lowpc = best_low;
14133 *highpc = best_high;
14134}
14135
801e3a5b
JB
14136/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14137 in DIE. */
380bca97 14138
801e3a5b
JB
14139static void
14140dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14141 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14142{
5e22e966 14143 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14144 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14145 struct attribute *attr;
91da1414 14146 struct attribute *attr_high;
801e3a5b 14147
91da1414
MW
14148 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14149 if (attr_high)
801e3a5b 14150 {
801e3a5b 14151 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14152 if (attr != nullptr)
dda83cd7 14153 {
95f982e5
TT
14154 CORE_ADDR low = attr->as_address ();
14155 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14156
cd6c91b4 14157 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14158 high += low;
9a619af0 14159
3e29f34a
MR
14160 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14161 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14162 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14163 }
801e3a5b
JB
14164 }
14165
14166 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14167 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14168 {
2b0c7f41
SM
14169 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14170 on DWARF version). */
14171 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14172
2b0c7f41
SM
14173 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14174 this value. */
14175 if (die->tag != DW_TAG_compile_unit)
14176 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14177
2d5f09ec 14178 std::vector<blockrange> blockvec;
2b0c7f41 14179 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14180 [&] (CORE_ADDR start, CORE_ADDR end)
14181 {
58fdfd2c
JK
14182 start += baseaddr;
14183 end += baseaddr;
5f46c5a5
JK
14184 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14185 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14186 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14187 blockvec.emplace_back (start, end);
5f46c5a5 14188 });
2d5f09ec
KB
14189
14190 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14191 }
14192}
14193
685b1105
JK
14194/* Check whether the producer field indicates either of GCC < 4.6, or the
14195 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14196
685b1105
JK
14197static void
14198check_producer (struct dwarf2_cu *cu)
60d5a603 14199{
38360086 14200 int major, minor;
60d5a603
JK
14201
14202 if (cu->producer == NULL)
14203 {
14204 /* For unknown compilers expect their behavior is DWARF version
14205 compliant.
14206
14207 GCC started to support .debug_types sections by -gdwarf-4 since
14208 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14209 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14210 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14211 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14212 }
b1ffba5a 14213 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14214 {
38360086
MW
14215 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14216 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14217 }
5230b05a 14218 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14219 {
14220 cu->producer_is_icc = true;
14221 cu->producer_is_icc_lt_14 = major < 14;
14222 }
c258c396
JD
14223 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14224 cu->producer_is_codewarrior = true;
685b1105
JK
14225 else
14226 {
14227 /* For other non-GCC compilers, expect their behavior is DWARF version
14228 compliant. */
60d5a603
JK
14229 }
14230
9068261f 14231 cu->checked_producer = true;
685b1105 14232}
ba919b58 14233
685b1105
JK
14234/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14235 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14236 during 4.6.0 experimental. */
14237
9068261f 14238static bool
685b1105
JK
14239producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14240{
14241 if (!cu->checked_producer)
14242 check_producer (cu);
14243
14244 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14245}
14246
c258c396
JD
14247
14248/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14249 with incorrect is_stmt attributes. */
14250
14251static bool
14252producer_is_codewarrior (struct dwarf2_cu *cu)
14253{
14254 if (!cu->checked_producer)
14255 check_producer (cu);
14256
14257 return cu->producer_is_codewarrior;
14258}
14259
bf23a268
TT
14260/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14261 If that attribute is not available, return the appropriate
14262 default. */
60d5a603
JK
14263
14264static enum dwarf_access_attribute
bf23a268 14265dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14266{
bf23a268
TT
14267 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14268 if (attr != nullptr)
14269 {
14270 LONGEST value = attr->constant_value (-1);
14271 if (value == DW_ACCESS_public
14272 || value == DW_ACCESS_protected
14273 || value == DW_ACCESS_private)
14274 return (dwarf_access_attribute) value;
14275 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14276 plongest (value));
14277 }
14278
60d5a603
JK
14279 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14280 {
14281 /* The default DWARF 2 accessibility for members is public, the default
14282 accessibility for inheritance is private. */
14283
14284 if (die->tag != DW_TAG_inheritance)
14285 return DW_ACCESS_public;
14286 else
14287 return DW_ACCESS_private;
14288 }
14289 else
14290 {
14291 /* DWARF 3+ defines the default accessibility a different way. The same
14292 rules apply now for DW_TAG_inheritance as for the members and it only
14293 depends on the container kind. */
14294
14295 if (die->parent->tag == DW_TAG_class_type)
14296 return DW_ACCESS_private;
14297 else
14298 return DW_ACCESS_public;
14299 }
14300}
14301
74ac6d43
TT
14302/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14303 offset. If the attribute was not found return 0, otherwise return
14304 1. If it was found but could not properly be handled, set *OFFSET
14305 to 0. */
14306
14307static int
14308handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14309 LONGEST *offset)
14310{
14311 struct attribute *attr;
14312
14313 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14314 if (attr != NULL)
14315 {
14316 *offset = 0;
14317
14318 /* Note that we do not check for a section offset first here.
14319 This is because DW_AT_data_member_location is new in DWARF 4,
14320 so if we see it, we can assume that a constant form is really
14321 a constant and not a section offset. */
cd6c91b4 14322 if (attr->form_is_constant ())
0826b30a 14323 *offset = attr->constant_value (0);
cd6c91b4 14324 else if (attr->form_is_section_offset ())
74ac6d43 14325 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14326 else if (attr->form_is_block ())
9d2246fc 14327 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14328 else
14329 dwarf2_complex_location_expr_complaint ();
14330
14331 return 1;
14332 }
14333
14334 return 0;
14335}
14336
7d79de9a
TT
14337/* Look for DW_AT_data_member_location and store the results in FIELD. */
14338
14339static void
14340handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14341 struct field *field)
14342{
14343 struct attribute *attr;
14344
14345 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14346 if (attr != NULL)
14347 {
14348 if (attr->form_is_constant ())
14349 {
14350 LONGEST offset = attr->constant_value (0);
14351 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14352 }
14353 else if (attr->form_is_section_offset ())
14354 dwarf2_complex_location_expr_complaint ();
14355 else if (attr->form_is_block ())
14356 {
14357 bool handled;
9d2246fc 14358 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14359 if (handled)
14360 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14361 else
14362 {
5e22e966
SM
14363 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14364 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14365 struct dwarf2_locexpr_baton *dlbaton
14366 = XOBNEW (&objfile->objfile_obstack,
14367 struct dwarf2_locexpr_baton);
9d2246fc
TT
14368 dlbaton->data = attr->as_block ()->data;
14369 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14370 /* When using this baton, we want to compute the address
14371 of the field, not the value. This is why
14372 is_reference is set to false here. */
14373 dlbaton->is_reference = false;
5e22e966 14374 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14375 dlbaton->per_cu = cu->per_cu;
14376
14377 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14378 }
14379 }
14380 else
14381 dwarf2_complex_location_expr_complaint ();
14382 }
14383}
14384
c906108c
SS
14385/* Add an aggregate field to the field list. */
14386
14387static void
107d2387 14388dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14389 struct dwarf2_cu *cu)
6e70227d 14390{
5e22e966 14391 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14392 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14393 struct nextfield *new_field;
14394 struct attribute *attr;
14395 struct field *fp;
15d034d0 14396 const char *fieldname = "";
c906108c 14397
7d0ccb61
DJ
14398 if (die->tag == DW_TAG_inheritance)
14399 {
be2daae6
TT
14400 fip->baseclasses.emplace_back ();
14401 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14402 }
14403 else
14404 {
be2daae6
TT
14405 fip->fields.emplace_back ();
14406 new_field = &fip->fields.back ();
7d0ccb61 14407 }
be2daae6 14408
9c6a1327
TT
14409 new_field->offset = die->sect_off;
14410
bf23a268 14411 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 14412 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 14413 fip->non_public_fields = true;
60d5a603 14414
e142c38c 14415 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14416 if (attr != nullptr)
23dca5c3 14417 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
14418 else
14419 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14420
14421 fp = &new_field->field;
a9a9bd0f 14422
e142c38c 14423 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14424 {
a9a9bd0f 14425 /* Data member other than a C++ static data member. */
6e70227d 14426
c906108c 14427 /* Get type of field. */
5d14b6e5 14428 fp->set_type (die_type (die, cu));
c906108c 14429
d6a843b5 14430 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14431
c906108c 14432 /* Get bit size of field (zero if none). */
e142c38c 14433 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14434 if (attr != nullptr)
c906108c 14435 {
529908cb 14436 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
14437 }
14438 else
14439 {
14440 FIELD_BITSIZE (*fp) = 0;
14441 }
14442
14443 /* Get bit offset of field. */
7d79de9a 14444 handle_data_member_location (die, cu, fp);
e142c38c 14445 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 14446 if (attr != nullptr && attr->form_is_constant ())
c906108c 14447 {
d5a22e77 14448 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14449 {
14450 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
14451 additional bit offset from the MSB of the containing
14452 anonymous object to the MSB of the field. We don't
14453 have to do anything special since we don't need to
14454 know the size of the anonymous object. */
529908cb 14455 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 14456 + attr->constant_value (0)));
c906108c
SS
14457 }
14458 else
14459 {
14460 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
14461 MSB of the anonymous object, subtract off the number of
14462 bits from the MSB of the field to the MSB of the
14463 object, and then subtract off the number of bits of
14464 the field itself. The result is the bit offset of
14465 the LSB of the field. */
c906108c 14466 int anonymous_size;
7c184d33 14467 int bit_offset = attr->constant_value (0);
c906108c 14468
e142c38c 14469 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 14470 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
14471 {
14472 /* The size of the anonymous object containing
14473 the bit field is explicit, so use the
14474 indicated size (in bytes). */
7c184d33 14475 anonymous_size = attr->constant_value (0);
c906108c
SS
14476 }
14477 else
14478 {
14479 /* The size of the anonymous object containing
14480 the bit field must be inferred from the type
14481 attribute of the data member containing the
14482 bit field. */
5d14b6e5 14483 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14484 }
f41f5e61
PA
14485 SET_FIELD_BITPOS (*fp,
14486 (FIELD_BITPOS (*fp)
14487 + anonymous_size * bits_per_byte
14488 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14489 }
14490 }
da5b30da
AA
14491 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14492 if (attr != NULL)
14493 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14494 + attr->constant_value (0)));
c906108c
SS
14495
14496 /* Get name of field. */
39cbfefa
DJ
14497 fieldname = dwarf2_name (die, cu);
14498 if (fieldname == NULL)
14499 fieldname = "";
d8151005
DJ
14500
14501 /* The name is already allocated along with this objfile, so we don't
14502 need to duplicate it for the type. */
14503 fp->name = fieldname;
c906108c
SS
14504
14505 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 14506 pointer or virtual base class pointer) to private. */
e142c38c 14507 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14508 {
d48cc9dd 14509 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 14510 new_field->accessibility = DW_ACCESS_private;
264fc0e2 14511 fip->non_public_fields = true;
c906108c
SS
14512 }
14513 }
a9a9bd0f 14514 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14515 {
a9a9bd0f
DC
14516 /* C++ static member. */
14517
14518 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14519 is a declaration, but all versions of G++ as of this writing
14520 (so through at least 3.2.1) incorrectly generate
14521 DW_TAG_variable tags. */
6e70227d 14522
ff355380 14523 const char *physname;
c906108c 14524
a9a9bd0f 14525 /* Get name of field. */
39cbfefa
DJ
14526 fieldname = dwarf2_name (die, cu);
14527 if (fieldname == NULL)
c906108c
SS
14528 return;
14529
254e6b9e 14530 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14531 if (attr
14532 /* Only create a symbol if this is an external value.
14533 new_symbol checks this and puts the value in the global symbol
14534 table, which we want. If it is not external, new_symbol
14535 will try to put the value in cu->list_in_scope which is wrong. */
14536 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14537 {
14538 /* A static const member, not much different than an enum as far as
14539 we're concerned, except that we can support more types. */
14540 new_symbol (die, NULL, cu);
14541 }
14542
2df3850c 14543 /* Get physical name. */
ff355380 14544 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14545
d8151005
DJ
14546 /* The name is already allocated along with this objfile, so we don't
14547 need to duplicate it for the type. */
14548 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 14549 fp->set_type (die_type (die, cu));
d8151005 14550 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14551 }
14552 else if (die->tag == DW_TAG_inheritance)
14553 {
74ac6d43 14554 /* C++ base class field. */
7d79de9a 14555 handle_data_member_location (die, cu, fp);
c906108c 14556 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
14557 fp->set_type (die_type (die, cu));
14558 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 14559 }
2ddeaf8a
TT
14560 else
14561 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14562}
14563
883fd55a
KS
14564/* Can the type given by DIE define another type? */
14565
14566static bool
14567type_can_define_types (const struct die_info *die)
14568{
14569 switch (die->tag)
14570 {
14571 case DW_TAG_typedef:
14572 case DW_TAG_class_type:
14573 case DW_TAG_structure_type:
14574 case DW_TAG_union_type:
14575 case DW_TAG_enumeration_type:
14576 return true;
14577
14578 default:
14579 return false;
14580 }
14581}
14582
14583/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14584
14585static void
883fd55a
KS
14586dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14587 struct dwarf2_cu *cu)
6e70227d 14588{
be2daae6
TT
14589 struct decl_field fp;
14590 memset (&fp, 0, sizeof (fp));
98751a41 14591
883fd55a 14592 gdb_assert (type_can_define_types (die));
98751a41 14593
883fd55a 14594 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14595 fp.name = dwarf2_name (die, cu);
14596 fp.type = read_type_die (die, cu);
98751a41 14597
c191a687 14598 /* Save accessibility. */
bf23a268 14599 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
14600 switch (accessibility)
14601 {
14602 case DW_ACCESS_public:
14603 /* The assumed value if neither private nor protected. */
14604 break;
14605 case DW_ACCESS_private:
be2daae6 14606 fp.is_private = 1;
c191a687
KS
14607 break;
14608 case DW_ACCESS_protected:
be2daae6 14609 fp.is_protected = 1;
c191a687 14610 break;
c191a687
KS
14611 }
14612
883fd55a 14613 if (die->tag == DW_TAG_typedef)
be2daae6 14614 fip->typedef_field_list.push_back (fp);
883fd55a 14615 else
be2daae6 14616 fip->nested_types_list.push_back (fp);
98751a41
JK
14617}
14618
9c6a1327
TT
14619/* A convenience typedef that's used when finding the discriminant
14620 field for a variant part. */
1b95cdb7
SM
14621typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14622 offset_map_type;
9c6a1327
TT
14623
14624/* Compute the discriminant range for a given variant. OBSTACK is
14625 where the results will be stored. VARIANT is the variant to
14626 process. IS_UNSIGNED indicates whether the discriminant is signed
14627 or unsigned. */
14628
14629static const gdb::array_view<discriminant_range>
14630convert_variant_range (struct obstack *obstack, const variant_field &variant,
14631 bool is_unsigned)
14632{
14633 std::vector<discriminant_range> ranges;
14634
14635 if (variant.default_branch)
14636 return {};
14637
14638 if (variant.discr_list_data == nullptr)
14639 {
14640 discriminant_range r
14641 = {variant.discriminant_value, variant.discriminant_value};
14642 ranges.push_back (r);
14643 }
14644 else
14645 {
14646 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14647 variant.discr_list_data->size);
14648 while (!data.empty ())
14649 {
14650 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14651 {
14652 complaint (_("invalid discriminant marker: %d"), data[0]);
14653 break;
14654 }
14655 bool is_range = data[0] == DW_DSC_range;
14656 data = data.slice (1);
14657
14658 ULONGEST low, high;
14659 unsigned int bytes_read;
14660
14661 if (data.empty ())
14662 {
14663 complaint (_("DW_AT_discr_list missing low value"));
14664 break;
14665 }
14666 if (is_unsigned)
14667 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14668 else
14669 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14670 &bytes_read);
14671 data = data.slice (bytes_read);
14672
14673 if (is_range)
14674 {
14675 if (data.empty ())
14676 {
14677 complaint (_("DW_AT_discr_list missing high value"));
14678 break;
14679 }
14680 if (is_unsigned)
14681 high = read_unsigned_leb128 (nullptr, data.data (),
14682 &bytes_read);
14683 else
14684 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14685 &bytes_read);
14686 data = data.slice (bytes_read);
14687 }
14688 else
14689 high = low;
14690
14691 ranges.push_back ({ low, high });
14692 }
14693 }
14694
14695 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14696 ranges.size ());
14697 std::copy (ranges.begin (), ranges.end (), result);
14698 return gdb::array_view<discriminant_range> (result, ranges.size ());
14699}
14700
14701static const gdb::array_view<variant_part> create_variant_parts
14702 (struct obstack *obstack,
14703 const offset_map_type &offset_map,
14704 struct field_info *fi,
14705 const std::vector<variant_part_builder> &variant_parts);
14706
14707/* Fill in a "struct variant" for a given variant field. RESULT is
14708 the variant to fill in. OBSTACK is where any needed allocations
14709 will be done. OFFSET_MAP holds the mapping from section offsets to
14710 fields for the type. FI describes the fields of the type we're
14711 processing. FIELD is the variant field we're converting. */
14712
14713static void
14714create_one_variant (variant &result, struct obstack *obstack,
14715 const offset_map_type &offset_map,
14716 struct field_info *fi, const variant_field &field)
14717{
14718 result.discriminants = convert_variant_range (obstack, field, false);
14719 result.first_field = field.first_field + fi->baseclasses.size ();
14720 result.last_field = field.last_field + fi->baseclasses.size ();
14721 result.parts = create_variant_parts (obstack, offset_map, fi,
14722 field.variant_parts);
14723}
14724
14725/* Fill in a "struct variant_part" for a given variant part. RESULT
14726 is the variant part to fill in. OBSTACK is where any needed
14727 allocations will be done. OFFSET_MAP holds the mapping from
14728 section offsets to fields for the type. FI describes the fields of
14729 the type we're processing. BUILDER is the variant part to be
14730 converted. */
14731
14732static void
14733create_one_variant_part (variant_part &result,
14734 struct obstack *obstack,
14735 const offset_map_type &offset_map,
14736 struct field_info *fi,
14737 const variant_part_builder &builder)
14738{
14739 auto iter = offset_map.find (builder.discriminant_offset);
14740 if (iter == offset_map.end ())
14741 {
14742 result.discriminant_index = -1;
14743 /* Doesn't matter. */
14744 result.is_unsigned = false;
14745 }
14746 else
14747 {
14748 result.discriminant_index = iter->second;
14749 result.is_unsigned
c6d940a9 14750 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
14751 }
14752
14753 size_t n = builder.variants.size ();
14754 variant *output = new (obstack) variant[n];
14755 for (size_t i = 0; i < n; ++i)
14756 create_one_variant (output[i], obstack, offset_map, fi,
14757 builder.variants[i]);
14758
14759 result.variants = gdb::array_view<variant> (output, n);
14760}
14761
14762/* Create a vector of variant parts that can be attached to a type.
14763 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14764 holds the mapping from section offsets to fields for the type. FI
14765 describes the fields of the type we're processing. VARIANT_PARTS
14766 is the vector to convert. */
14767
14768static const gdb::array_view<variant_part>
14769create_variant_parts (struct obstack *obstack,
14770 const offset_map_type &offset_map,
14771 struct field_info *fi,
14772 const std::vector<variant_part_builder> &variant_parts)
14773{
14774 if (variant_parts.empty ())
14775 return {};
14776
14777 size_t n = variant_parts.size ();
14778 variant_part *result = new (obstack) variant_part[n];
14779 for (size_t i = 0; i < n; ++i)
14780 create_one_variant_part (result[i], obstack, offset_map, fi,
14781 variant_parts[i]);
14782
14783 return gdb::array_view<variant_part> (result, n);
14784}
14785
14786/* Compute the variant part vector for FIP, attaching it to TYPE when
14787 done. */
14788
14789static void
14790add_variant_property (struct field_info *fip, struct type *type,
14791 struct dwarf2_cu *cu)
14792{
14793 /* Map section offsets of fields to their field index. Note the
14794 field index here does not take the number of baseclasses into
14795 account. */
14796 offset_map_type offset_map;
14797 for (int i = 0; i < fip->fields.size (); ++i)
14798 offset_map[fip->fields[i].offset] = i;
14799
5e22e966 14800 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14801 gdb::array_view<variant_part> parts
14802 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14803 fip->variant_parts);
14804
14805 struct dynamic_prop prop;
8c2e4e06
SM
14806 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14807 obstack_copy (&objfile->objfile_obstack, &parts,
14808 sizeof (parts)));
9c6a1327 14809
5c54719c 14810 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14811}
14812
c906108c
SS
14813/* Create the vector of fields, and attach it to the type. */
14814
14815static void
fba45db2 14816dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14817 struct dwarf2_cu *cu)
c906108c 14818{
317f7127 14819 int nfields = fip->nfields ();
c906108c
SS
14820
14821 /* Record the field count, allocate space for the array of fields,
14822 and create blank accessibility bitfields if necessary. */
5e33d5f4 14823 type->set_num_fields (nfields);
3cabb6b0
SM
14824 type->set_fields
14825 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14826
3e9f1ca1 14827 if (fip->non_public_fields && cu->per_cu->lang != language_ada)
c906108c
SS
14828 {
14829 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14830
14831 TYPE_FIELD_PRIVATE_BITS (type) =
14832 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14833 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14834
14835 TYPE_FIELD_PROTECTED_BITS (type) =
14836 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14837 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14838
774b6a14
TT
14839 TYPE_FIELD_IGNORE_BITS (type) =
14840 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14841 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14842 }
14843
14844 /* If the type has baseclasses, allocate and clear a bit vector for
14845 TYPE_FIELD_VIRTUAL_BITS. */
3e9f1ca1 14846 if (!fip->baseclasses.empty () && cu->per_cu->lang != language_ada)
c906108c 14847 {
be2daae6 14848 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14849 unsigned char *pointer;
c906108c
SS
14850
14851 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14852 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14853 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14854 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14855 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14856 }
14857
9c6a1327
TT
14858 if (!fip->variant_parts.empty ())
14859 add_variant_property (fip, type, cu);
2ddeaf8a 14860
be2daae6
TT
14861 /* Copy the saved-up fields into the field vector. */
14862 for (int i = 0; i < nfields; ++i)
c906108c 14863 {
be2daae6
TT
14864 struct nextfield &field
14865 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14866 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14867
ceacbf6e 14868 type->field (i) = field.field;
be2daae6 14869 switch (field.accessibility)
c906108c 14870 {
c5aa993b 14871 case DW_ACCESS_private:
3e9f1ca1 14872 if (cu->per_cu->lang != language_ada)
be2daae6 14873 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14874 break;
c906108c 14875
c5aa993b 14876 case DW_ACCESS_protected:
3e9f1ca1 14877 if (cu->per_cu->lang != language_ada)
be2daae6 14878 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14879 break;
c906108c 14880
c5aa993b
JM
14881 case DW_ACCESS_public:
14882 break;
c906108c 14883
c5aa993b
JM
14884 default:
14885 /* Unknown accessibility. Complain and treat it as public. */
14886 {
b98664d3 14887 complaint (_("unsupported accessibility %d"),
be2daae6 14888 field.accessibility);
c5aa993b
JM
14889 }
14890 break;
c906108c 14891 }
be2daae6 14892 if (i < fip->baseclasses.size ())
c906108c 14893 {
be2daae6 14894 switch (field.virtuality)
c906108c 14895 {
c5aa993b
JM
14896 case DW_VIRTUALITY_virtual:
14897 case DW_VIRTUALITY_pure_virtual:
3e9f1ca1 14898 if (cu->per_cu->lang == language_ada)
a73c6dcd 14899 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14900 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14901 break;
c906108c
SS
14902 }
14903 }
c906108c
SS
14904 }
14905}
14906
7d27a96d
TT
14907/* Return true if this member function is a constructor, false
14908 otherwise. */
14909
14910static int
14911dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14912{
14913 const char *fieldname;
fe978cb0 14914 const char *type_name;
7d27a96d
TT
14915 int len;
14916
14917 if (die->parent == NULL)
14918 return 0;
14919
14920 if (die->parent->tag != DW_TAG_structure_type
14921 && die->parent->tag != DW_TAG_union_type
14922 && die->parent->tag != DW_TAG_class_type)
14923 return 0;
14924
14925 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14926 type_name = dwarf2_name (die->parent, cu);
14927 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14928 return 0;
14929
14930 len = strlen (fieldname);
fe978cb0
PA
14931 return (strncmp (fieldname, type_name, len) == 0
14932 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14933}
14934
c906108c
SS
14935/* Add a member function to the proper fieldlist. */
14936
14937static void
107d2387 14938dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14939 struct type *type, struct dwarf2_cu *cu)
c906108c 14940{
5e22e966 14941 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 14942 struct attribute *attr;
c906108c 14943 int i;
be2daae6 14944 struct fnfieldlist *flp = nullptr;
c906108c 14945 struct fn_field *fnp;
15d034d0 14946 const char *fieldname;
f792889a 14947 struct type *this_type;
c906108c 14948
3e9f1ca1 14949 if (cu->per_cu->lang == language_ada)
a73c6dcd 14950 error (_("unexpected member function in Ada type"));
b4ba55a1 14951
2df3850c 14952 /* Get name of member function. */
39cbfefa
DJ
14953 fieldname = dwarf2_name (die, cu);
14954 if (fieldname == NULL)
2df3850c 14955 return;
c906108c 14956
c906108c 14957 /* Look up member function name in fieldlist. */
be2daae6 14958 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14959 {
27bfe10e 14960 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14961 {
14962 flp = &fip->fnfieldlists[i];
14963 break;
14964 }
c906108c
SS
14965 }
14966
be2daae6
TT
14967 /* Create a new fnfieldlist if necessary. */
14968 if (flp == nullptr)
c906108c 14969 {
be2daae6
TT
14970 fip->fnfieldlists.emplace_back ();
14971 flp = &fip->fnfieldlists.back ();
c906108c 14972 flp->name = fieldname;
be2daae6 14973 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14974 }
14975
be2daae6
TT
14976 /* Create a new member function field and add it to the vector of
14977 fnfieldlists. */
14978 flp->fnfields.emplace_back ();
14979 fnp = &flp->fnfields.back ();
3da10d80
KS
14980
14981 /* Delay processing of the physname until later. */
3e9f1ca1 14982 if (cu->per_cu->lang == language_cplus)
be2daae6
TT
14983 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14984 die, cu);
3da10d80
KS
14985 else
14986 {
1d06ead6 14987 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14988 fnp->physname = physname ? physname : "";
14989 }
14990
c906108c 14991 fnp->type = alloc_type (objfile);
f792889a 14992 this_type = read_type_die (die, cu);
78134374 14993 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 14994 {
1f704f76 14995 int nparams = this_type->num_fields ();
c906108c 14996
f792889a 14997 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14998 of the method itself (TYPE_CODE_METHOD). */
14999 smash_to_method_type (fnp->type, type,
f792889a 15000 TYPE_TARGET_TYPE (this_type),
80fc5e77 15001 this_type->fields (),
1f704f76 15002 this_type->num_fields (),
a409645d 15003 this_type->has_varargs ());
c906108c
SS
15004
15005 /* Handle static member functions.
dda83cd7
SM
15006 Dwarf2 has no clean way to discern C++ static and non-static
15007 member functions. G++ helps GDB by marking the first
15008 parameter for non-static member functions (which is the this
15009 pointer) as artificial. We obtain this information from
15010 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15011 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15012 fnp->voffset = VOFFSET_STATIC;
15013 }
15014 else
b98664d3 15015 complaint (_("member function type missing for '%s'"),
3da10d80 15016 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15017
15018 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15019 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15020 fnp->fcontext = die_containing_type (die, cu);
c906108c 15021
3e43a32a
MS
15022 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15023 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15024
15025 /* Get accessibility. */
bf23a268 15026 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15027 switch (accessibility)
c906108c 15028 {
60d5a603
JK
15029 case DW_ACCESS_private:
15030 fnp->is_private = 1;
15031 break;
15032 case DW_ACCESS_protected:
15033 fnp->is_protected = 1;
15034 break;
c906108c
SS
15035 }
15036
b02dede2 15037 /* Check for artificial methods. */
e142c38c 15038 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15039 if (attr && attr->as_boolean ())
b02dede2
DJ
15040 fnp->is_artificial = 1;
15041
e35000a7
TBA
15042 /* Check for defaulted methods. */
15043 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15044 if (attr != nullptr)
15045 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15046
15047 /* Check for deleted methods. */
15048 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15049 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15050 fnp->is_deleted = 1;
15051
7d27a96d
TT
15052 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15053
0d564a31 15054 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15055 function. For older versions of GCC, this is an offset in the
15056 appropriate virtual table, as specified by DW_AT_containing_type.
15057 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15058 to the object address. */
15059
e142c38c 15060 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15061 if (attr != nullptr)
8e19ed76 15062 {
9d2246fc 15063 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15064 {
9d2246fc
TT
15065 struct dwarf_block *block = attr->as_block ();
15066
15067 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15068 {
15069 /* Old-style GCC. */
9d2246fc 15070 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15071 }
9d2246fc
TT
15072 else if (block->data[0] == DW_OP_deref
15073 || (block->size > 1
15074 && block->data[0] == DW_OP_deref_size
15075 && block->data[1] == cu->header.addr_size))
aec5aa8b 15076 {
9d2246fc 15077 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15078 if ((fnp->voffset % cu->header.addr_size) != 0)
15079 dwarf2_complex_location_expr_complaint ();
15080 else
15081 fnp->voffset /= cu->header.addr_size;
15082 fnp->voffset += 2;
15083 }
15084 else
15085 dwarf2_complex_location_expr_complaint ();
15086
15087 if (!fnp->fcontext)
7e993ebf
KS
15088 {
15089 /* If there is no `this' field and no DW_AT_containing_type,
15090 we cannot actually find a base class context for the
15091 vtable! */
1f704f76 15092 if (this_type->num_fields () == 0
7e993ebf
KS
15093 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15094 {
b98664d3 15095 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15096 "function \"%s\" (offset %s)"),
15097 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15098 }
15099 else
15100 {
15101 fnp->fcontext
940da03e 15102 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15103 }
15104 }
aec5aa8b 15105 }
cd6c91b4 15106 else if (attr->form_is_section_offset ())
dda83cd7 15107 {
4d3c2250 15108 dwarf2_complex_location_expr_complaint ();
dda83cd7 15109 }
8e19ed76 15110 else
dda83cd7 15111 {
4d3c2250
KB
15112 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15113 fieldname);
dda83cd7 15114 }
0d564a31 15115 }
d48cc9dd
DJ
15116 else
15117 {
15118 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15119 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15120 {
15121 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15122 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15123 "but the vtable offset is not specified"),
9d8780f0 15124 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15125 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15126 TYPE_CPLUS_DYNAMIC (type) = 1;
15127 }
15128 }
c906108c
SS
15129}
15130
15131/* Create the vector of member function fields, and attach it to the type. */
15132
15133static void
fba45db2 15134dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15135 struct dwarf2_cu *cu)
c906108c 15136{
3e9f1ca1 15137 if (cu->per_cu->lang == language_ada)
a73c6dcd 15138 error (_("unexpected member functions in Ada type"));
b4ba55a1 15139
c906108c
SS
15140 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15141 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15142 TYPE_ALLOC (type,
15143 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15144
be2daae6 15145 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15146 {
be2daae6 15147 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15148 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15149
be2daae6
TT
15150 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15151 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15152 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15153 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15154
15155 for (int k = 0; k < nf.fnfields.size (); ++k)
15156 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15157 }
15158
be2daae6 15159 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15160}
15161
1168df01
JB
15162/* Returns non-zero if NAME is the name of a vtable member in CU's
15163 language, zero otherwise. */
15164static int
15165is_vtable_name (const char *name, struct dwarf2_cu *cu)
15166{
15167 static const char vptr[] = "_vptr";
15168
9c37b5ae
TT
15169 /* Look for the C++ form of the vtable. */
15170 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15171 return 1;
15172
15173 return 0;
15174}
15175
c0dd20ea 15176/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15177 functions, with the ABI-specified layout. If TYPE describes
15178 such a structure, smash it into a member function type.
61049d3b
DJ
15179
15180 GCC shouldn't do this; it should just output pointer to member DIEs.
15181 This is GCC PR debug/28767. */
c0dd20ea 15182
0b92b5bb
TT
15183static void
15184quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15185{
09e2d7c7 15186 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15187
15188 /* Check for a structure with no name and two children. */
1f704f76 15189 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15190 return;
c0dd20ea
DJ
15191
15192 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15193 if (TYPE_FIELD_NAME (type, 0) == NULL
15194 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15195 || TYPE_FIELD_NAME (type, 1) == NULL
15196 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15197 return;
c0dd20ea
DJ
15198
15199 /* Find the type of the method. */
940da03e 15200 pfn_type = type->field (0).type ();
c0dd20ea 15201 if (pfn_type == NULL
78134374
SM
15202 || pfn_type->code () != TYPE_CODE_PTR
15203 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15204 return;
c0dd20ea
DJ
15205
15206 /* Look for the "this" argument. */
15207 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15208 if (pfn_type->num_fields () == 0
940da03e
SM
15209 /* || pfn_type->field (0).type () == NULL */
15210 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15211 return;
c0dd20ea 15212
940da03e 15213 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15214 new_type = alloc_type (objfile);
09e2d7c7 15215 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15216 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15217 pfn_type->has_varargs ());
0b92b5bb 15218 smash_to_methodptr_type (type, new_type);
c0dd20ea 15219}
1168df01 15220
e26624c6
TT
15221/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15222 requires rewriting, then copy it and return the updated copy.
15223 Otherwise return nullptr. */
15224
15225static struct type *
15226rewrite_array_type (struct type *type)
15227{
15228 if (type->code () != TYPE_CODE_ARRAY)
15229 return nullptr;
15230
15231 struct type *index_type = type->index_type ();
15232 range_bounds *current_bounds = index_type->bounds ();
15233
15234 /* Handle multi-dimensional arrays. */
15235 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15236 if (new_target == nullptr)
15237 {
15238 /* Maybe we don't need to rewrite this array. */
15239 if (current_bounds->low.kind () == PROP_CONST
15240 && current_bounds->high.kind () == PROP_CONST)
15241 return nullptr;
15242 }
15243
15244 /* Either the target type was rewritten, or the bounds have to be
15245 updated. Either way we want to copy the type and update
15246 everything. */
15247 struct type *copy = copy_type (type);
15248 int nfields = copy->num_fields ();
15249 field *new_fields
15250 = ((struct field *) TYPE_ZALLOC (copy,
15251 nfields * sizeof (struct field)));
15252 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15253 copy->set_fields (new_fields);
15254 if (new_target != nullptr)
15255 TYPE_TARGET_TYPE (copy) = new_target;
15256
15257 struct type *index_copy = copy_type (index_type);
15258 range_bounds *bounds
15259 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15260 sizeof (range_bounds));
15261 *bounds = *current_bounds;
15262 bounds->low.set_const_val (1);
15263 bounds->high.set_const_val (0);
15264 index_copy->set_bounds (bounds);
15265 copy->set_index_type (index_copy);
15266
15267 return copy;
15268}
15269
57567375
TT
15270/* While some versions of GCC will generate complicated DWARF for an
15271 array (see quirk_ada_thick_pointer), more recent versions were
15272 modified to emit an explicit thick pointer structure. However, in
15273 this case, the array still has DWARF expressions for its ranges,
15274 and these must be ignored. */
15275
15276static void
15277quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15278 struct type *type)
15279{
3e9f1ca1 15280 gdb_assert (cu->per_cu->lang == language_ada);
57567375
TT
15281
15282 /* Check for a structure with two children. */
15283 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15284 return;
15285
15286 /* Check for P_ARRAY and P_BOUNDS members. */
15287 if (TYPE_FIELD_NAME (type, 0) == NULL
15288 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15289 || TYPE_FIELD_NAME (type, 1) == NULL
15290 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15291 return;
15292
15293 /* Make sure we're looking at a pointer to an array. */
15294 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15295 return;
57567375 15296
e26624c6
TT
15297 /* The Ada code already knows how to handle these types, so all that
15298 we need to do is turn the bounds into static bounds. However, we
15299 don't want to rewrite existing array or index types in-place,
15300 because those may be referenced in other contexts where this
15301 rewriting is undesirable. */
15302 struct type *new_ary_type
15303 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15304 if (new_ary_type != nullptr)
15305 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15306}
15307
2b4424c3
TT
15308/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15309 appropriate error checking and issuing complaints if there is a
15310 problem. */
15311
15312static ULONGEST
15313get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15314{
15315 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15316
15317 if (attr == nullptr)
15318 return 0;
15319
cd6c91b4 15320 if (!attr->form_is_constant ())
2b4424c3 15321 {
b98664d3 15322 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15323 " - DIE at %s [in module %s]"),
15324 sect_offset_str (die->sect_off),
5e22e966 15325 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15326 return 0;
15327 }
15328
529908cb
TT
15329 LONGEST val = attr->constant_value (0);
15330 if (val < 0)
2b4424c3 15331 {
529908cb
TT
15332 complaint (_("DW_AT_alignment value must not be negative"
15333 " - DIE at %s [in module %s]"),
15334 sect_offset_str (die->sect_off),
15335 objfile_name (cu->per_objfile->objfile));
15336 return 0;
2b4424c3 15337 }
529908cb 15338 ULONGEST align = val;
2b4424c3
TT
15339
15340 if (align == 0)
15341 {
b98664d3 15342 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15343 " - DIE at %s [in module %s]"),
15344 sect_offset_str (die->sect_off),
5e22e966 15345 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15346 return 0;
15347 }
15348 if ((align & (align - 1)) != 0)
15349 {
b98664d3 15350 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15351 " - DIE at %s [in module %s]"),
15352 sect_offset_str (die->sect_off),
5e22e966 15353 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15354 return 0;
15355 }
15356
15357 return align;
15358}
15359
15360/* If the DIE has a DW_AT_alignment attribute, use its value to set
15361 the alignment for TYPE. */
15362
15363static void
15364maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15365 struct type *type)
15366{
15367 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15368 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15369 " - DIE at %s [in module %s]"),
15370 sect_offset_str (die->sect_off),
5e22e966 15371 objfile_name (cu->per_objfile->objfile));
2b4424c3 15372}
685b1105 15373
e35000a7
TBA
15374/* Check if the given VALUE is a valid enum dwarf_calling_convention
15375 constant for a type, according to DWARF5 spec, Table 5.5. */
15376
15377static bool
15378is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15379{
15380 switch (value)
15381 {
15382 case DW_CC_normal:
15383 case DW_CC_pass_by_reference:
15384 case DW_CC_pass_by_value:
15385 return true;
15386
15387 default:
15388 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15389 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15390 return false;
15391 }
15392}
15393
d0922fcf
TBA
15394/* Check if the given VALUE is a valid enum dwarf_calling_convention
15395 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15396 also according to GNU-specific values (see include/dwarf2.h). */
15397
15398static bool
15399is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15400{
15401 switch (value)
15402 {
15403 case DW_CC_normal:
15404 case DW_CC_program:
15405 case DW_CC_nocall:
15406 return true;
15407
15408 case DW_CC_GNU_renesas_sh:
15409 case DW_CC_GNU_borland_fastcall_i386:
15410 case DW_CC_GDB_IBM_OpenCL:
15411 return true;
15412
15413 default:
15414 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15415 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15416 return false;
15417 }
15418}
15419
c906108c 15420/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15421 (definition) to create a type for the structure or union. Fill in
15422 the type's name and general properties; the members will not be
83655187
DE
15423 processed until process_structure_scope. A symbol table entry for
15424 the type will also not be done until process_structure_scope (assuming
15425 the type has a name).
c906108c 15426
c767944b
DJ
15427 NOTE: we need to call these functions regardless of whether or not the
15428 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15429 structure or union. This gets the type entered into our set of
83655187 15430 user defined types. */
c906108c 15431
f792889a 15432static struct type *
134d01f1 15433read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15434{
5e22e966 15435 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15436 struct type *type;
15437 struct attribute *attr;
15d034d0 15438 const char *name;
c906108c 15439
348e048f
DE
15440 /* If the definition of this type lives in .debug_types, read that type.
15441 Don't follow DW_AT_specification though, that will take us back up
15442 the chain and we want to go down. */
052c8bb8 15443 attr = die->attr (DW_AT_signature);
435d3d88 15444 if (attr != nullptr)
348e048f 15445 {
ac9ec31b 15446 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15447
ac9ec31b 15448 /* The type's CU may not be the same as CU.
02142a6c 15449 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15450 return set_die_type (die, type, cu);
15451 }
15452
c0dd20ea 15453 type = alloc_type (objfile);
c906108c 15454 INIT_CPLUS_SPECIFIC (type);
93311388 15455
39cbfefa
DJ
15456 name = dwarf2_name (die, cu);
15457 if (name != NULL)
c906108c 15458 {
3e9f1ca1
TT
15459 if (cu->per_cu->lang == language_cplus
15460 || cu->per_cu->lang == language_d
15461 || cu->per_cu->lang == language_rust)
63d06c5c 15462 {
15d034d0 15463 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15464
15465 /* dwarf2_full_name might have already finished building the DIE's
15466 type. If so, there is no need to continue. */
15467 if (get_die_type (die, cu) != NULL)
15468 return get_die_type (die, cu);
15469
d0e39ea2 15470 type->set_name (full_name);
63d06c5c
DC
15471 }
15472 else
15473 {
d8151005
DJ
15474 /* The name is already allocated along with this objfile, so
15475 we don't need to duplicate it for the type. */
d0e39ea2 15476 type->set_name (name);
63d06c5c 15477 }
c906108c
SS
15478 }
15479
15480 if (die->tag == DW_TAG_structure_type)
15481 {
67607e24 15482 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15483 }
15484 else if (die->tag == DW_TAG_union_type)
15485 {
67607e24 15486 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15487 }
15488 else
15489 {
67607e24 15490 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15491 }
15492
3e9f1ca1 15493 if (cu->per_cu->lang == language_cplus && die->tag == DW_TAG_class_type)
aa70e35c 15494 type->set_is_declared_class (true);
0cc2414c 15495
e35000a7
TBA
15496 /* Store the calling convention in the type if it's available in
15497 the die. Otherwise the calling convention remains set to
15498 the default value DW_CC_normal. */
15499 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15500 if (attr != nullptr
529908cb 15501 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
15502 {
15503 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15504 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 15505 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
15506 }
15507
e142c38c 15508 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15509 if (attr != nullptr)
c906108c 15510 {
cd6c91b4 15511 if (attr->form_is_constant ())
dda83cd7 15512 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
15513 else
15514 {
f8e89861 15515 struct dynamic_prop prop;
293e7e51 15516 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15517 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 15518 TYPE_LENGTH (type) = 0;
155bfbd3 15519 }
c906108c
SS
15520 }
15521 else
15522 {
15523 TYPE_LENGTH (type) = 0;
15524 }
15525
2b4424c3
TT
15526 maybe_set_alignment (cu, die, type);
15527
5230b05a 15528 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15529 {
5230b05a
WT
15530 /* ICC<14 does not output the required DW_AT_declaration on
15531 incomplete types, but gives them a size of zero. */
b4b73759 15532 type->set_is_stub (true);
685b1105
JK
15533 }
15534 else
9baccff6 15535 type->set_stub_is_supported (true);
685b1105 15536
dc718098 15537 if (die_is_declaration (die, cu))
b4b73759 15538 type->set_is_stub (true);
a6c727b2
DJ
15539 else if (attr == NULL && die->child == NULL
15540 && producer_is_realview (cu->producer))
15541 /* RealView does not output the required DW_AT_declaration
15542 on incomplete types. */
b4b73759 15543 type->set_is_stub (true);
dc718098 15544
c906108c
SS
15545 /* We need to add the type field to the die immediately so we don't
15546 infinitely recurse when dealing with pointers to the structure
0963b4bd 15547 type within the structure itself. */
1c379e20 15548 set_die_type (die, type, cu);
c906108c 15549
7e314c57
JK
15550 /* set_die_type should be already done. */
15551 set_descriptive_type (type, die, cu);
15552
c767944b
DJ
15553 return type;
15554}
15555
9c6a1327
TT
15556static void handle_struct_member_die
15557 (struct die_info *child_die,
15558 struct type *type,
15559 struct field_info *fi,
15560 std::vector<struct symbol *> *template_args,
15561 struct dwarf2_cu *cu);
15562
15563/* A helper for handle_struct_member_die that handles
15564 DW_TAG_variant_part. */
15565
15566static void
15567handle_variant_part (struct die_info *die, struct type *type,
15568 struct field_info *fi,
15569 std::vector<struct symbol *> *template_args,
15570 struct dwarf2_cu *cu)
15571{
15572 variant_part_builder *new_part;
15573 if (fi->current_variant_part == nullptr)
15574 {
15575 fi->variant_parts.emplace_back ();
15576 new_part = &fi->variant_parts.back ();
15577 }
15578 else if (!fi->current_variant_part->processing_variant)
15579 {
15580 complaint (_("nested DW_TAG_variant_part seen "
15581 "- DIE at %s [in module %s]"),
15582 sect_offset_str (die->sect_off),
5e22e966 15583 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15584 return;
15585 }
15586 else
15587 {
15588 variant_field &current = fi->current_variant_part->variants.back ();
15589 current.variant_parts.emplace_back ();
15590 new_part = &current.variant_parts.back ();
15591 }
15592
15593 /* When we recurse, we want callees to add to this new variant
15594 part. */
15595 scoped_restore save_current_variant_part
15596 = make_scoped_restore (&fi->current_variant_part, new_part);
15597
15598 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15599 if (discr == NULL)
15600 {
15601 /* It's a univariant form, an extension we support. */
15602 }
15603 else if (discr->form_is_ref ())
15604 {
15605 struct dwarf2_cu *target_cu = cu;
15606 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15607
15608 new_part->discriminant_offset = target_die->sect_off;
15609 }
15610 else
15611 {
15612 complaint (_("DW_AT_discr does not have DIE reference form"
15613 " - DIE at %s [in module %s]"),
15614 sect_offset_str (die->sect_off),
5e22e966 15615 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15616 }
15617
15618 for (die_info *child_die = die->child;
15619 child_die != NULL;
15620 child_die = child_die->sibling)
15621 handle_struct_member_die (child_die, type, fi, template_args, cu);
15622}
15623
15624/* A helper for handle_struct_member_die that handles
15625 DW_TAG_variant. */
15626
15627static void
15628handle_variant (struct die_info *die, struct type *type,
15629 struct field_info *fi,
15630 std::vector<struct symbol *> *template_args,
15631 struct dwarf2_cu *cu)
15632{
15633 if (fi->current_variant_part == nullptr)
15634 {
15635 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15636 "- DIE at %s [in module %s]"),
15637 sect_offset_str (die->sect_off),
5e22e966 15638 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15639 return;
15640 }
15641 if (fi->current_variant_part->processing_variant)
15642 {
15643 complaint (_("nested DW_TAG_variant seen "
15644 "- DIE at %s [in module %s]"),
15645 sect_offset_str (die->sect_off),
5e22e966 15646 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15647 return;
15648 }
15649
15650 scoped_restore save_processing_variant
15651 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15652 true);
15653
15654 fi->current_variant_part->variants.emplace_back ();
15655 variant_field &variant = fi->current_variant_part->variants.back ();
15656 variant.first_field = fi->fields.size ();
15657
15658 /* In a variant we want to get the discriminant and also add a
15659 field for our sole member child. */
15660 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 15661 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
15662 {
15663 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 15664 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
15665 variant.default_branch = true;
15666 else
9d2246fc 15667 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
15668 }
15669 else
cae21f8e 15670 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
15671
15672 for (die_info *variant_child = die->child;
15673 variant_child != NULL;
15674 variant_child = variant_child->sibling)
15675 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15676
15677 variant.last_field = fi->fields.size ();
15678}
15679
2ddeaf8a
TT
15680/* A helper for process_structure_scope that handles a single member
15681 DIE. */
15682
15683static void
15684handle_struct_member_die (struct die_info *child_die, struct type *type,
15685 struct field_info *fi,
15686 std::vector<struct symbol *> *template_args,
15687 struct dwarf2_cu *cu)
15688{
15689 if (child_die->tag == DW_TAG_member
9c6a1327 15690 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15691 {
15692 /* NOTE: carlton/2002-11-05: A C++ static data member
15693 should be a DW_TAG_member that is a declaration, but
15694 all versions of G++ as of this writing (so through at
15695 least 3.2.1) incorrectly generate DW_TAG_variable
15696 tags for them instead. */
15697 dwarf2_add_field (fi, child_die, cu);
15698 }
15699 else if (child_die->tag == DW_TAG_subprogram)
15700 {
15701 /* Rust doesn't have member functions in the C++ sense.
15702 However, it does emit ordinary functions as children
15703 of a struct DIE. */
3e9f1ca1 15704 if (cu->per_cu->lang == language_rust)
2ddeaf8a
TT
15705 read_func_scope (child_die, cu);
15706 else
15707 {
15708 /* C++ member function. */
15709 dwarf2_add_member_fn (fi, child_die, type, cu);
15710 }
15711 }
15712 else if (child_die->tag == DW_TAG_inheritance)
15713 {
15714 /* C++ base class field. */
15715 dwarf2_add_field (fi, child_die, cu);
15716 }
15717 else if (type_can_define_types (child_die))
15718 dwarf2_add_type_defn (fi, child_die, cu);
15719 else if (child_die->tag == DW_TAG_template_type_param
15720 || child_die->tag == DW_TAG_template_value_param)
15721 {
15722 struct symbol *arg = new_symbol (child_die, NULL, cu);
15723
15724 if (arg != NULL)
15725 template_args->push_back (arg);
15726 }
9c6a1327
TT
15727 else if (child_die->tag == DW_TAG_variant_part)
15728 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15729 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15730 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15731}
15732
c767944b
DJ
15733/* Finish creating a structure or union type, including filling in
15734 its members and creating a symbol for it. */
15735
15736static void
15737process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15738{
5e22e966 15739 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15740 struct die_info *child_die;
c767944b
DJ
15741 struct type *type;
15742
15743 type = get_die_type (die, cu);
15744 if (type == NULL)
15745 type = read_structure_type (die, cu);
15746
3e1d3d8c 15747 bool has_template_parameters = false;
e142c38c 15748 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15749 {
15750 struct field_info fi;
2f4732b0 15751 std::vector<struct symbol *> template_args;
c906108c 15752
639d11d3 15753 child_die = die->child;
c906108c
SS
15754
15755 while (child_die && child_die->tag)
15756 {
2ddeaf8a 15757 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15758 child_die = child_die->sibling;
c906108c
SS
15759 }
15760
34eaf542 15761 /* Attach template arguments to type. */
2f4732b0 15762 if (!template_args.empty ())
34eaf542 15763 {
3e1d3d8c 15764 has_template_parameters = true;
34eaf542 15765 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15766 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15767 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15768 = XOBNEWVEC (&objfile->objfile_obstack,
15769 struct symbol *,
15770 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15771 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15772 template_args.data (),
34eaf542
TT
15773 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15774 * sizeof (struct symbol *)));
34eaf542
TT
15775 }
15776
c906108c 15777 /* Attach fields and member functions to the type. */
317f7127 15778 if (fi.nfields () > 0)
e7c27a73 15779 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15780 if (!fi.fnfieldlists.empty ())
c906108c 15781 {
e7c27a73 15782 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15783
c5aa993b 15784 /* Get the type which refers to the base class (possibly this
c906108c 15785 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15786 class from the DW_AT_containing_type attribute. This use of
15787 DW_AT_containing_type is a GNU extension. */
c906108c 15788
e142c38c 15789 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15790 {
e7c27a73 15791 struct type *t = die_containing_type (die, cu);
c906108c 15792
ae6ae975 15793 set_type_vptr_basetype (type, t);
c906108c
SS
15794 if (type == t)
15795 {
c906108c
SS
15796 int i;
15797
15798 /* Our own class provides vtbl ptr. */
1f704f76 15799 for (i = t->num_fields () - 1;
c906108c
SS
15800 i >= TYPE_N_BASECLASSES (t);
15801 --i)
15802 {
0d5cff50 15803 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15804
dda83cd7 15805 if (is_vtable_name (fieldname, cu))
c906108c 15806 {
ae6ae975 15807 set_type_vptr_fieldno (type, i);
c906108c
SS
15808 break;
15809 }
15810 }
15811
15812 /* Complain if virtual function table field not found. */
15813 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15814 complaint (_("virtual function table pointer "
3e43a32a 15815 "not found when defining class '%s'"),
7d93a1e0 15816 type->name () ? type->name () : "");
c906108c
SS
15817 }
15818 else
15819 {
ae6ae975 15820 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15821 }
15822 }
f6235d4c 15823 else if (cu->producer
61012eef 15824 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15825 {
15826 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
15827 of the containing type, but the vtable pointer is
15828 always named __vfp. */
f6235d4c
EZ
15829
15830 int i;
15831
1f704f76 15832 for (i = type->num_fields () - 1;
f6235d4c
EZ
15833 i >= TYPE_N_BASECLASSES (type);
15834 --i)
15835 {
15836 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15837 {
ae6ae975
DE
15838 set_type_vptr_fieldno (type, i);
15839 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15840 break;
15841 }
15842 }
15843 }
c906108c 15844 }
98751a41
JK
15845
15846 /* Copy fi.typedef_field_list linked list elements content into the
15847 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15848 if (!fi.typedef_field_list.empty ())
98751a41 15849 {
be2daae6 15850 int count = fi.typedef_field_list.size ();
98751a41 15851
a0d7a4ff 15852 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15853 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15854 = ((struct decl_field *)
be2daae6
TT
15855 TYPE_ALLOC (type,
15856 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15857 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15858
be2daae6
TT
15859 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15860 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15861 }
c767944b 15862
883fd55a
KS
15863 /* Copy fi.nested_types_list linked list elements content into the
15864 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
3e9f1ca1
TT
15865 if (!fi.nested_types_list.empty ()
15866 && cu->per_cu->lang != language_ada)
883fd55a 15867 {
be2daae6 15868 int count = fi.nested_types_list.size ();
883fd55a
KS
15869
15870 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15871 TYPE_NESTED_TYPES_ARRAY (type)
15872 = ((struct decl_field *)
be2daae6
TT
15873 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15874 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15875
be2daae6
TT
15876 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15877 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15878 }
c906108c 15879 }
63d06c5c 15880
bb5ed363 15881 quirk_gcc_member_function_pointer (type, objfile);
3e9f1ca1 15882 if (cu->per_cu->lang == language_rust && die->tag == DW_TAG_union_type)
c9317f21 15883 cu->rust_unions.push_back (type);
3e9f1ca1 15884 else if (cu->per_cu->lang == language_ada)
57567375 15885 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 15886
90aeadfc
DC
15887 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15888 snapshots) has been known to create a die giving a declaration
15889 for a class that has, as a child, a die giving a definition for a
15890 nested class. So we have to process our children even if the
15891 current die is a declaration. Normally, of course, a declaration
15892 won't have any children at all. */
134d01f1 15893
ca040673
DE
15894 child_die = die->child;
15895
90aeadfc
DC
15896 while (child_die != NULL && child_die->tag)
15897 {
15898 if (child_die->tag == DW_TAG_member
15899 || child_die->tag == DW_TAG_variable
34eaf542
TT
15900 || child_die->tag == DW_TAG_inheritance
15901 || child_die->tag == DW_TAG_template_value_param
15902 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15903 {
90aeadfc 15904 /* Do nothing. */
134d01f1 15905 }
90aeadfc
DC
15906 else
15907 process_die (child_die, cu);
134d01f1 15908
436c571c 15909 child_die = child_die->sibling;
134d01f1
DJ
15910 }
15911
fa4028e9
JB
15912 /* Do not consider external references. According to the DWARF standard,
15913 these DIEs are identified by the fact that they have no byte_size
15914 attribute, and a declaration attribute. */
15915 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15916 || !die_is_declaration (die, cu)
15917 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15918 {
15919 struct symbol *sym = new_symbol (die, type, cu);
15920
15921 if (has_template_parameters)
15922 {
a776957c
TT
15923 struct symtab *symtab;
15924 if (sym != nullptr)
15925 symtab = symbol_symtab (sym);
15926 else if (cu->line_header != nullptr)
15927 {
15928 /* Any related symtab will do. */
15929 symtab
7ba99d21 15930 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15931 }
15932 else
15933 {
15934 symtab = nullptr;
15935 complaint (_("could not find suitable "
15936 "symtab for template parameter"
15937 " - DIE at %s [in module %s]"),
15938 sect_offset_str (die->sect_off),
15939 objfile_name (objfile));
15940 }
15941
15942 if (symtab != nullptr)
15943 {
15944 /* Make sure that the symtab is set on the new symbols.
15945 Even though they don't appear in this symtab directly,
15946 other parts of gdb assume that symbols do, and this is
15947 reasonably true. */
15948 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15949 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15950 }
3e1d3d8c
TT
15951 }
15952 }
134d01f1
DJ
15953}
15954
ed6acedd
TT
15955/* Assuming DIE is an enumeration type, and TYPE is its associated
15956 type, update TYPE using some information only available in DIE's
15957 children. In particular, the fields are computed. */
55426c9d
JB
15958
15959static void
15960update_enumeration_type_from_children (struct die_info *die,
15961 struct type *type,
15962 struct dwarf2_cu *cu)
15963{
60f7655a 15964 struct die_info *child_die;
55426c9d
JB
15965 int unsigned_enum = 1;
15966 int flag_enum = 1;
55426c9d 15967
8268c778 15968 auto_obstack obstack;
ed6acedd 15969 std::vector<struct field> fields;
55426c9d 15970
60f7655a
DE
15971 for (child_die = die->child;
15972 child_die != NULL && child_die->tag;
436c571c 15973 child_die = child_die->sibling)
55426c9d
JB
15974 {
15975 struct attribute *attr;
15976 LONGEST value;
15977 const gdb_byte *bytes;
15978 struct dwarf2_locexpr_baton *baton;
15979 const char *name;
60f7655a 15980
55426c9d
JB
15981 if (child_die->tag != DW_TAG_enumerator)
15982 continue;
15983
15984 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15985 if (attr == NULL)
15986 continue;
15987
15988 name = dwarf2_name (child_die, cu);
15989 if (name == NULL)
15990 name = "<anonymous enumerator>";
15991
15992 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15993 &value, &bytes, &baton);
15994 if (value < 0)
15995 {
15996 unsigned_enum = 0;
15997 flag_enum = 0;
15998 }
55426c9d 15999 else
edd45eb0
SM
16000 {
16001 if (count_one_bits_ll (value) >= 2)
16002 flag_enum = 0;
edd45eb0 16003 }
55426c9d 16004
ed6acedd
TT
16005 fields.emplace_back ();
16006 struct field &field = fields.back ();
16007 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16008 SET_FIELD_ENUMVAL (field, value);
16009 }
16010
16011 if (!fields.empty ())
16012 {
5e33d5f4 16013 type->set_num_fields (fields.size ());
3cabb6b0
SM
16014 type->set_fields
16015 ((struct field *)
16016 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16017 memcpy (type->fields (), fields.data (),
ed6acedd 16018 sizeof (struct field) * fields.size ());
55426c9d
JB
16019 }
16020
16021 if (unsigned_enum)
653223d3
SM
16022 type->set_is_unsigned (true);
16023
55426c9d 16024 if (flag_enum)
9902b327 16025 type->set_is_flag_enum (true);
55426c9d
JB
16026}
16027
134d01f1
DJ
16028/* Given a DW_AT_enumeration_type die, set its type. We do not
16029 complete the type's fields yet, or create any symbols. */
c906108c 16030
f792889a 16031static struct type *
134d01f1 16032read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16033{
5e22e966 16034 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16035 struct type *type;
c906108c 16036 struct attribute *attr;
0114d602 16037 const char *name;
134d01f1 16038
348e048f
DE
16039 /* If the definition of this type lives in .debug_types, read that type.
16040 Don't follow DW_AT_specification though, that will take us back up
16041 the chain and we want to go down. */
052c8bb8 16042 attr = die->attr (DW_AT_signature);
435d3d88 16043 if (attr != nullptr)
348e048f 16044 {
ac9ec31b 16045 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16046
ac9ec31b 16047 /* The type's CU may not be the same as CU.
02142a6c 16048 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16049 return set_die_type (die, type, cu);
16050 }
16051
c906108c
SS
16052 type = alloc_type (objfile);
16053
67607e24 16054 type->set_code (TYPE_CODE_ENUM);
94af9270 16055 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16056 if (name != NULL)
d0e39ea2 16057 type->set_name (name);
c906108c 16058
0626fc76
TT
16059 attr = dwarf2_attr (die, DW_AT_type, cu);
16060 if (attr != NULL)
16061 {
16062 struct type *underlying_type = die_type (die, cu);
16063
16064 TYPE_TARGET_TYPE (type) = underlying_type;
16065 }
16066
e142c38c 16067 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16068 if (attr != nullptr)
c906108c 16069 {
529908cb 16070 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16071 }
16072 else
16073 {
16074 TYPE_LENGTH (type) = 0;
16075 }
16076
2b4424c3
TT
16077 maybe_set_alignment (cu, die, type);
16078
137033e9
JB
16079 /* The enumeration DIE can be incomplete. In Ada, any type can be
16080 declared as private in the package spec, and then defined only
16081 inside the package body. Such types are known as Taft Amendment
16082 Types. When another package uses such a type, an incomplete DIE
16083 may be generated by the compiler. */
02eb380e 16084 if (die_is_declaration (die, cu))
b4b73759 16085 type->set_is_stub (true);
02eb380e 16086
0626fc76
TT
16087 /* If this type has an underlying type that is not a stub, then we
16088 may use its attributes. We always use the "unsigned" attribute
16089 in this situation, because ordinarily we guess whether the type
16090 is unsigned -- but the guess can be wrong and the underlying type
16091 can tell us the reality. However, we defer to a local size
16092 attribute if one exists, because this lets the compiler override
16093 the underlying type if needed. */
e46d3488 16094 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16095 {
9e7c9a03
HD
16096 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16097 underlying_type = check_typedef (underlying_type);
653223d3
SM
16098
16099 type->set_is_unsigned (underlying_type->is_unsigned ());
16100
0626fc76 16101 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16102 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16103
2b4424c3 16104 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16105 && TYPE_RAW_ALIGN (underlying_type) != 0)
16106 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16107 }
16108
aa70e35c 16109 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
3d567982 16110
ed6acedd
TT
16111 set_die_type (die, type, cu);
16112
16113 /* Finish the creation of this type by using the enum's children.
16114 Note that, as usual, this must come after set_die_type to avoid
16115 infinite recursion when trying to compute the names of the
16116 enumerators. */
16117 update_enumeration_type_from_children (die, type, cu);
16118
16119 return type;
134d01f1
DJ
16120}
16121
16122/* Given a pointer to a die which begins an enumeration, process all
16123 the dies that define the members of the enumeration, and create the
16124 symbol for the enumeration type.
16125
16126 NOTE: We reverse the order of the element list. */
16127
16128static void
16129process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16130{
f792889a 16131 struct type *this_type;
134d01f1 16132
f792889a
DJ
16133 this_type = get_die_type (die, cu);
16134 if (this_type == NULL)
16135 this_type = read_enumeration_type (die, cu);
9dc481d3 16136
639d11d3 16137 if (die->child != NULL)
c906108c 16138 {
9dc481d3 16139 struct die_info *child_die;
15d034d0 16140 const char *name;
9dc481d3 16141
639d11d3 16142 child_die = die->child;
c906108c
SS
16143 while (child_die && child_die->tag)
16144 {
16145 if (child_die->tag != DW_TAG_enumerator)
16146 {
e7c27a73 16147 process_die (child_die, cu);
c906108c
SS
16148 }
16149 else
16150 {
39cbfefa
DJ
16151 name = dwarf2_name (child_die, cu);
16152 if (name)
ed6acedd 16153 new_symbol (child_die, this_type, cu);
c906108c
SS
16154 }
16155
436c571c 16156 child_die = child_die->sibling;
c906108c 16157 }
c906108c 16158 }
134d01f1 16159
6c83ed52
TT
16160 /* If we are reading an enum from a .debug_types unit, and the enum
16161 is a declaration, and the enum is not the signatured type in the
16162 unit, then we do not want to add a symbol for it. Adding a
16163 symbol would in some cases obscure the true definition of the
16164 enum, giving users an incomplete type when the definition is
16165 actually available. Note that we do not want to do this for all
16166 enums which are just declarations, because C++0x allows forward
16167 enum declarations. */
3019eac3 16168 if (cu->per_cu->is_debug_types
6c83ed52
TT
16169 && die_is_declaration (die, cu))
16170 {
52dc124a 16171 struct signatured_type *sig_type;
6c83ed52 16172
c0f78cd4 16173 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16174 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16175 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16176 return;
16177 }
16178
f792889a 16179 new_symbol (die, this_type, cu);
c906108c
SS
16180}
16181
57567375
TT
16182/* Helper function for quirk_ada_thick_pointer that examines a bounds
16183 expression for an index type and finds the corresponding field
16184 offset in the hidden "P_BOUNDS" structure. Returns true on success
16185 and updates *FIELD, false if it fails to recognize an
16186 expression. */
16187
16188static bool
16189recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16190 int *bounds_offset, struct field *field,
16191 struct dwarf2_cu *cu)
16192{
16193 struct attribute *attr = dwarf2_attr (die, name, cu);
16194 if (attr == nullptr || !attr->form_is_block ())
16195 return false;
16196
16197 const struct dwarf_block *block = attr->as_block ();
16198 const gdb_byte *start = block->data;
16199 const gdb_byte *end = block->data + block->size;
16200
16201 /* The expression to recognize generally looks like:
16202
16203 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16204 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16205
16206 However, the second "plus_uconst" may be missing:
16207
16208 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16209 DW_OP_deref_size: 4)
16210
16211 This happens when the field is at the start of the structure.
16212
16213 Also, the final deref may not be sized:
16214
16215 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16216 DW_OP_deref)
16217
16218 This happens when the size of the index type happens to be the
16219 same as the architecture's word size. This can occur with or
16220 without the second plus_uconst. */
16221
16222 if (end - start < 2)
16223 return false;
16224 if (*start++ != DW_OP_push_object_address)
16225 return false;
16226 if (*start++ != DW_OP_plus_uconst)
16227 return false;
16228
16229 uint64_t this_bound_off;
16230 start = gdb_read_uleb128 (start, end, &this_bound_off);
16231 if (start == nullptr || (int) this_bound_off != this_bound_off)
16232 return false;
16233 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16234 is consistent among all bounds. */
16235 if (*bounds_offset == -1)
16236 *bounds_offset = this_bound_off;
16237 else if (*bounds_offset != this_bound_off)
16238 return false;
16239
16240 if (start == end || *start++ != DW_OP_deref)
16241 return false;
16242
16243 int offset = 0;
16244 if (start ==end)
16245 return false;
16246 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16247 {
16248 /* This means an offset of 0. */
16249 }
16250 else if (*start++ != DW_OP_plus_uconst)
16251 return false;
16252 else
16253 {
16254 /* The size is the parameter to DW_OP_plus_uconst. */
16255 uint64_t val;
16256 start = gdb_read_uleb128 (start, end, &val);
16257 if (start == nullptr)
16258 return false;
16259 if ((int) val != val)
16260 return false;
16261 offset = val;
16262 }
16263
16264 if (start == end)
16265 return false;
16266
16267 uint64_t size;
16268 if (*start == DW_OP_deref_size)
16269 {
16270 start = gdb_read_uleb128 (start + 1, end, &size);
16271 if (start == nullptr)
16272 return false;
16273 }
16274 else if (*start == DW_OP_deref)
16275 {
16276 size = cu->header.addr_size;
16277 ++start;
16278 }
16279 else
16280 return false;
16281
16282 SET_FIELD_BITPOS (*field, 8 * offset);
16283 if (size != TYPE_LENGTH (field->type ()))
16284 FIELD_BITSIZE (*field) = 8 * size;
16285
16286 return true;
16287}
16288
16289/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16290 some kinds of Ada arrays:
16291
16292 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16293 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16294 <11e0> DW_AT_data_location: 2 byte block: 97 6
16295 (DW_OP_push_object_address; DW_OP_deref)
16296 <11e3> DW_AT_type : <0x1173>
16297 <11e7> DW_AT_sibling : <0x1201>
16298 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16299 <11ec> DW_AT_type : <0x1206>
16300 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16301 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16302 DW_OP_deref_size: 4)
16303 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16304 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16305 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16306
16307 This actually represents a "thick pointer", which is a structure
16308 with two elements: one that is a pointer to the array data, and one
16309 that is a pointer to another structure; this second structure holds
16310 the array bounds.
16311
16312 This returns a new type on success, or nullptr if this didn't
16313 recognize the type. */
16314
16315static struct type *
16316quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16317 struct type *type)
16318{
16319 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16320 /* So far we've only seen this with block form. */
16321 if (attr == nullptr || !attr->form_is_block ())
16322 return nullptr;
16323
16324 /* Note that this will fail if the structure layout is changed by
16325 the compiler. However, we have no good way to recognize some
16326 other layout, because we don't know what expression the compiler
16327 might choose to emit should this happen. */
16328 struct dwarf_block *blk = attr->as_block ();
16329 if (blk->size != 2
16330 || blk->data[0] != DW_OP_push_object_address
16331 || blk->data[1] != DW_OP_deref)
16332 return nullptr;
16333
16334 int bounds_offset = -1;
16335 int max_align = -1;
16336 std::vector<struct field> range_fields;
16337 for (struct die_info *child_die = die->child;
16338 child_die;
16339 child_die = child_die->sibling)
16340 {
16341 if (child_die->tag == DW_TAG_subrange_type)
16342 {
16343 struct type *underlying = read_subrange_index_type (child_die, cu);
16344
16345 int this_align = type_align (underlying);
16346 if (this_align > max_align)
16347 max_align = this_align;
16348
16349 range_fields.emplace_back ();
16350 range_fields.emplace_back ();
16351
16352 struct field &lower = range_fields[range_fields.size () - 2];
16353 struct field &upper = range_fields[range_fields.size () - 1];
16354
16355 lower.set_type (underlying);
16356 FIELD_ARTIFICIAL (lower) = 1;
16357
16358 upper.set_type (underlying);
16359 FIELD_ARTIFICIAL (upper) = 1;
16360
16361 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16362 &bounds_offset, &lower, cu)
16363 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16364 &bounds_offset, &upper, cu))
16365 return nullptr;
16366 }
16367 }
16368
16369 /* This shouldn't really happen, but double-check that we found
16370 where the bounds are stored. */
16371 if (bounds_offset == -1)
16372 return nullptr;
16373
16374 struct objfile *objfile = cu->per_objfile->objfile;
16375 for (int i = 0; i < range_fields.size (); i += 2)
16376 {
16377 char name[20];
16378
16379 /* Set the name of each field in the bounds. */
16380 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16381 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16382 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16383 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16384 }
16385
16386 struct type *bounds = alloc_type (objfile);
16387 bounds->set_code (TYPE_CODE_STRUCT);
16388
16389 bounds->set_num_fields (range_fields.size ());
16390 bounds->set_fields
16391 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16392 * sizeof (struct field))));
16393 memcpy (bounds->fields (), range_fields.data (),
16394 bounds->num_fields () * sizeof (struct field));
16395
16396 int last_fieldno = range_fields.size () - 1;
16397 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16398 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16399 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16400
16401 /* Rewrite the existing array type in place. Specifically, we
16402 remove any dynamic properties we might have read, and we replace
16403 the index types. */
16404 struct type *iter = type;
16405 for (int i = 0; i < range_fields.size (); i += 2)
16406 {
16407 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16408 iter->main_type->dyn_prop_list = nullptr;
16409 iter->set_index_type
16410 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16411 iter = TYPE_TARGET_TYPE (iter);
16412 }
16413
16414 struct type *result = alloc_type (objfile);
16415 result->set_code (TYPE_CODE_STRUCT);
16416
16417 result->set_num_fields (2);
16418 result->set_fields
16419 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16420 * sizeof (struct field))));
16421
16422 /* The names are chosen to coincide with what the compiler does with
16423 -fgnat-encodings=all, which the Ada code in gdb already
16424 understands. */
16425 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16426 result->field (0).set_type (lookup_pointer_type (type));
16427
16428 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16429 result->field (1).set_type (lookup_pointer_type (bounds));
16430 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16431
16432 result->set_name (type->name ());
16433 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16434 + TYPE_LENGTH (result->field (1).type ()));
16435
16436 return result;
16437}
16438
c906108c
SS
16439/* Extract all information from a DW_TAG_array_type DIE and put it in
16440 the DIE's type field. For now, this only handles one dimensional
16441 arrays. */
16442
f792889a 16443static struct type *
e7c27a73 16444read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16445{
5e22e966 16446 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16447 struct die_info *child_die;
7e314c57 16448 struct type *type;
c906108c 16449 struct type *element_type, *range_type, *index_type;
c906108c 16450 struct attribute *attr;
15d034d0 16451 const char *name;
a405673c 16452 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16453 unsigned int bit_stride = 0;
c906108c 16454
e7c27a73 16455 element_type = die_type (die, cu);
c906108c 16456
7e314c57
JK
16457 /* The die_type call above may have already set the type for this DIE. */
16458 type = get_die_type (die, cu);
16459 if (type)
16460 return type;
16461
dc53a7ad
JB
16462 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16463 if (attr != NULL)
a405673c
JB
16464 {
16465 int stride_ok;
293e7e51 16466 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16467
16468 byte_stride_prop
16469 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16470 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16471 prop_type);
a405673c
JB
16472 if (!stride_ok)
16473 {
b98664d3 16474 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16475 " - DIE at %s [in module %s]"),
16476 sect_offset_str (die->sect_off),
5e22e966 16477 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16478 /* Ignore this attribute. We will likely not be able to print
16479 arrays of this type correctly, but there is little we can do
16480 to help if we cannot read the attribute's value. */
16481 byte_stride_prop = NULL;
16482 }
16483 }
dc53a7ad
JB
16484
16485 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16486 if (attr != NULL)
529908cb 16487 bit_stride = attr->constant_value (0);
dc53a7ad 16488
c906108c
SS
16489 /* Irix 6.2 native cc creates array types without children for
16490 arrays with unspecified length. */
639d11d3 16491 if (die->child == NULL)
c906108c 16492 {
46bf5051 16493 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16494 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16495 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16496 byte_stride_prop, bit_stride);
f792889a 16497 return set_die_type (die, type, cu);
c906108c
SS
16498 }
16499
791afaa2 16500 std::vector<struct type *> range_types;
639d11d3 16501 child_die = die->child;
c906108c
SS
16502 while (child_die && child_die->tag)
16503 {
16504 if (child_die->tag == DW_TAG_subrange_type)
16505 {
f792889a 16506 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16507
dda83cd7
SM
16508 if (child_type != NULL)
16509 {
0963b4bd 16510 /* The range type was succesfully read. Save it for the
dda83cd7 16511 array type creation. */
791afaa2 16512 range_types.push_back (child_type);
dda83cd7 16513 }
c906108c 16514 }
436c571c 16515 child_die = child_die->sibling;
c906108c
SS
16516 }
16517
cf2b2075
TV
16518 if (range_types.empty ())
16519 {
16520 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16521 sect_offset_str (die->sect_off),
16522 objfile_name (cu->per_objfile->objfile));
16523 return NULL;
16524 }
16525
c906108c
SS
16526 /* Dwarf2 dimensions are output from left to right, create the
16527 necessary array types in backwards order. */
7ca2d3a3 16528
c906108c 16529 type = element_type;
7ca2d3a3
DL
16530
16531 if (read_array_order (die, cu) == DW_ORD_col_major)
16532 {
16533 int i = 0;
9a619af0 16534
791afaa2 16535 while (i < range_types.size ())
10f6a3ad
TT
16536 {
16537 type = create_array_type_with_stride (NULL, type, range_types[i++],
16538 byte_stride_prop, bit_stride);
16539 bit_stride = 0;
16540 byte_stride_prop = nullptr;
16541 }
7ca2d3a3
DL
16542 }
16543 else
16544 {
791afaa2 16545 size_t ndim = range_types.size ();
7ca2d3a3 16546 while (ndim-- > 0)
10f6a3ad
TT
16547 {
16548 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16549 byte_stride_prop, bit_stride);
16550 bit_stride = 0;
16551 byte_stride_prop = nullptr;
16552 }
7ca2d3a3 16553 }
c906108c 16554
cf2b2075
TV
16555 gdb_assert (type != element_type);
16556
f5f8a009
EZ
16557 /* Understand Dwarf2 support for vector types (like they occur on
16558 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16559 array type. This is not part of the Dwarf2/3 standard yet, but a
16560 custom vendor extension. The main difference between a regular
16561 array and the vector variant is that vectors are passed by value
16562 to functions. */
e142c38c 16563 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16564 if (attr != nullptr)
ea37ba09 16565 make_vector_type (type);
f5f8a009 16566
dbc98a8b
KW
16567 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16568 implementation may choose to implement triple vectors using this
16569 attribute. */
16570 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 16571 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 16572 {
529908cb
TT
16573 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16574 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 16575 else
b98664d3 16576 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16577 "than the total size of elements"));
dbc98a8b
KW
16578 }
16579
39cbfefa
DJ
16580 name = dwarf2_name (die, cu);
16581 if (name)
d0e39ea2 16582 type->set_name (name);
6e70227d 16583
2b4424c3
TT
16584 maybe_set_alignment (cu, die, type);
16585
57567375 16586 struct type *replacement_type = nullptr;
3e9f1ca1 16587 if (cu->per_cu->lang == language_ada)
57567375
TT
16588 {
16589 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16590 if (replacement_type != nullptr)
16591 type = replacement_type;
16592 }
16593
0963b4bd 16594 /* Install the type in the die. */
57567375 16595 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
16596
16597 /* set_die_type should be already done. */
b4ba55a1
JB
16598 set_descriptive_type (type, die, cu);
16599
7e314c57 16600 return type;
c906108c
SS
16601}
16602
7ca2d3a3 16603static enum dwarf_array_dim_ordering
6e70227d 16604read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16605{
16606 struct attribute *attr;
16607
16608 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16609
435d3d88 16610 if (attr != nullptr)
1bc397c5
TT
16611 {
16612 LONGEST val = attr->constant_value (-1);
16613 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16614 return (enum dwarf_array_dim_ordering) val;
16615 }
7ca2d3a3 16616
0963b4bd
MS
16617 /* GNU F77 is a special case, as at 08/2004 array type info is the
16618 opposite order to the dwarf2 specification, but data is still
16619 laid out as per normal fortran.
7ca2d3a3 16620
0963b4bd
MS
16621 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16622 version checking. */
7ca2d3a3 16623
3e9f1ca1 16624 if (cu->per_cu->lang == language_fortran
905e0470 16625 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16626 {
16627 return DW_ORD_row_major;
16628 }
16629
3a3440fb 16630 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
16631 {
16632 case array_column_major:
16633 return DW_ORD_col_major;
16634 case array_row_major:
16635 default:
16636 return DW_ORD_row_major;
16637 };
16638}
16639
72019c9c 16640/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16641 the DIE's type field. */
72019c9c 16642
f792889a 16643static struct type *
72019c9c
GM
16644read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16645{
7e314c57
JK
16646 struct type *domain_type, *set_type;
16647 struct attribute *attr;
f792889a 16648
7e314c57
JK
16649 domain_type = die_type (die, cu);
16650
16651 /* The die_type call above may have already set the type for this DIE. */
16652 set_type = get_die_type (die, cu);
16653 if (set_type)
16654 return set_type;
16655
16656 set_type = create_set_type (NULL, domain_type);
16657
16658 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
16659 if (attr != nullptr && attr->form_is_unsigned ())
16660 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 16661
2b4424c3
TT
16662 maybe_set_alignment (cu, die, set_type);
16663
f792889a 16664 return set_die_type (die, set_type, cu);
72019c9c 16665}
7ca2d3a3 16666
0971de02
TT
16667/* A helper for read_common_block that creates a locexpr baton.
16668 SYM is the symbol which we are marking as computed.
16669 COMMON_DIE is the DIE for the common block.
16670 COMMON_LOC is the location expression attribute for the common
16671 block itself.
16672 MEMBER_LOC is the location expression attribute for the particular
16673 member of the common block that we are processing.
16674 CU is the CU from which the above come. */
16675
16676static void
16677mark_common_block_symbol_computed (struct symbol *sym,
16678 struct die_info *common_die,
16679 struct attribute *common_loc,
16680 struct attribute *member_loc,
16681 struct dwarf2_cu *cu)
16682{
5e22e966 16683 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16684 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16685 struct dwarf2_locexpr_baton *baton;
16686 gdb_byte *ptr;
16687 unsigned int cu_off;
08feed99 16688 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16689 LONGEST offset = 0;
16690
16691 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16692 gdb_assert (common_loc->form_is_block ());
16693 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16694 || member_loc->form_is_constant ());
0971de02 16695
8d749320 16696 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16697 baton->per_objfile = per_objfile;
0971de02
TT
16698 baton->per_cu = cu->per_cu;
16699 gdb_assert (baton->per_cu);
16700
16701 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16702
cd6c91b4 16703 if (member_loc->form_is_constant ())
0971de02 16704 {
0826b30a 16705 offset = member_loc->constant_value (0);
0971de02
TT
16706 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16707 }
16708 else
9d2246fc 16709 baton->size += member_loc->as_block ()->size;
0971de02 16710
224c3ddb 16711 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16712 baton->data = ptr;
16713
16714 *ptr++ = DW_OP_call4;
9c541725 16715 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16716 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16717 ptr += 4;
16718
cd6c91b4 16719 if (member_loc->form_is_constant ())
0971de02
TT
16720 {
16721 *ptr++ = DW_OP_addr;
16722 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16723 ptr += cu->header.addr_size;
16724 }
16725 else
16726 {
16727 /* We have to copy the data here, because DW_OP_call4 will only
16728 use a DW_AT_location attribute. */
9d2246fc
TT
16729 struct dwarf_block *block = member_loc->as_block ();
16730 memcpy (ptr, block->data, block->size);
16731 ptr += block->size;
0971de02
TT
16732 }
16733
16734 *ptr++ = DW_OP_plus;
16735 gdb_assert (ptr - baton->data == baton->size);
16736
0971de02 16737 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16738 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16739}
16740
4357ac6c
TT
16741/* Create appropriate locally-scoped variables for all the
16742 DW_TAG_common_block entries. Also create a struct common_block
16743 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16744 is used to separate the common blocks name namespace from regular
4357ac6c 16745 variable names. */
c906108c
SS
16746
16747static void
e7c27a73 16748read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16749{
0971de02
TT
16750 struct attribute *attr;
16751
16752 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16753 if (attr != nullptr)
0971de02
TT
16754 {
16755 /* Support the .debug_loc offsets. */
4fc6c0d5 16756 if (attr->form_is_block ())
dda83cd7 16757 {
0971de02 16758 /* Ok. */
dda83cd7 16759 }
cd6c91b4 16760 else if (attr->form_is_section_offset ())
dda83cd7 16761 {
0971de02
TT
16762 dwarf2_complex_location_expr_complaint ();
16763 attr = NULL;
dda83cd7 16764 }
0971de02 16765 else
dda83cd7 16766 {
0971de02
TT
16767 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16768 "common block member");
16769 attr = NULL;
dda83cd7 16770 }
0971de02
TT
16771 }
16772
639d11d3 16773 if (die->child != NULL)
c906108c 16774 {
5e22e966 16775 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16776 struct die_info *child_die;
16777 size_t n_entries = 0, size;
16778 struct common_block *common_block;
16779 struct symbol *sym;
74ac6d43 16780
4357ac6c
TT
16781 for (child_die = die->child;
16782 child_die && child_die->tag;
436c571c 16783 child_die = child_die->sibling)
4357ac6c
TT
16784 ++n_entries;
16785
16786 size = (sizeof (struct common_block)
16787 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16788 common_block
16789 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16790 size);
4357ac6c
TT
16791 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16792 common_block->n_entries = 0;
16793
16794 for (child_die = die->child;
16795 child_die && child_die->tag;
436c571c 16796 child_die = child_die->sibling)
4357ac6c
TT
16797 {
16798 /* Create the symbol in the DW_TAG_common_block block in the current
16799 symbol scope. */
e7c27a73 16800 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16801 if (sym != NULL)
16802 {
16803 struct attribute *member_loc;
16804
16805 common_block->contents[common_block->n_entries++] = sym;
16806
16807 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16808 cu);
16809 if (member_loc)
16810 {
16811 /* GDB has handled this for a long time, but it is
16812 not specified by DWARF. It seems to have been
16813 emitted by gfortran at least as recently as:
16814 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16815 complaint (_("Variable in common block has "
0971de02 16816 "DW_AT_data_member_location "
9d8780f0
SM
16817 "- DIE at %s [in module %s]"),
16818 sect_offset_str (child_die->sect_off),
518817b3 16819 objfile_name (objfile));
0971de02 16820
cd6c91b4 16821 if (member_loc->form_is_section_offset ())
0971de02 16822 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16823 else if (member_loc->form_is_constant ()
4fc6c0d5 16824 || member_loc->form_is_block ())
0971de02 16825 {
435d3d88 16826 if (attr != nullptr)
0971de02
TT
16827 mark_common_block_symbol_computed (sym, die, attr,
16828 member_loc, cu);
16829 }
16830 else
16831 dwarf2_complex_location_expr_complaint ();
16832 }
16833 }
c906108c 16834 }
4357ac6c
TT
16835
16836 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16837 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16838 }
16839}
16840
0114d602 16841/* Create a type for a C++ namespace. */
d9fa45fe 16842
0114d602
DJ
16843static struct type *
16844read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16845{
5e22e966 16846 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16847 const char *previous_prefix, *name;
9219021c 16848 int is_anonymous;
0114d602
DJ
16849 struct type *type;
16850
16851 /* For extensions, reuse the type of the original namespace. */
16852 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16853 {
16854 struct die_info *ext_die;
16855 struct dwarf2_cu *ext_cu = cu;
9a619af0 16856
0114d602
DJ
16857 ext_die = dwarf2_extension (die, &ext_cu);
16858 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16859
16860 /* EXT_CU may not be the same as CU.
02142a6c 16861 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16862 return set_die_type (die, type, cu);
16863 }
9219021c 16864
e142c38c 16865 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16866
16867 /* Now build the name of the current namespace. */
16868
0114d602
DJ
16869 previous_prefix = determine_prefix (die, cu);
16870 if (previous_prefix[0] != '\0')
16871 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16872 previous_prefix, name, 0, cu);
0114d602
DJ
16873
16874 /* Create the type. */
19f392bc 16875 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16876
60531b24 16877 return set_die_type (die, type, cu);
0114d602
DJ
16878}
16879
22cee43f 16880/* Read a namespace scope. */
0114d602
DJ
16881
16882static void
16883read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16884{
5e22e966 16885 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16886 int is_anonymous;
9219021c 16887
5c4e30ca
DC
16888 /* Add a symbol associated to this if we haven't seen the namespace
16889 before. Also, add a using directive if it's an anonymous
16890 namespace. */
9219021c 16891
f2f0e013 16892 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16893 {
16894 struct type *type;
16895
0114d602 16896 type = read_type_die (die, cu);
e7c27a73 16897 new_symbol (die, type, cu);
5c4e30ca 16898
e8e80198 16899 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16900 if (is_anonymous)
0114d602
DJ
16901 {
16902 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16903
eb1e02fd 16904 std::vector<const char *> excludes;
804d2729 16905 add_using_directive (using_directives (cu),
7d93a1e0 16906 previous_prefix, type->name (), NULL,
eb1e02fd 16907 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16908 }
5c4e30ca 16909 }
9219021c 16910
639d11d3 16911 if (die->child != NULL)
d9fa45fe 16912 {
639d11d3 16913 struct die_info *child_die = die->child;
6e70227d 16914
d9fa45fe
DC
16915 while (child_die && child_die->tag)
16916 {
e7c27a73 16917 process_die (child_die, cu);
436c571c 16918 child_die = child_die->sibling;
d9fa45fe
DC
16919 }
16920 }
38d518c9
EZ
16921}
16922
f55ee35c
JK
16923/* Read a Fortran module as type. This DIE can be only a declaration used for
16924 imported module. Still we need that type as local Fortran "use ... only"
16925 declaration imports depend on the created type in determine_prefix. */
16926
16927static struct type *
16928read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16929{
5e22e966 16930 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16931 const char *module_name;
f55ee35c
JK
16932 struct type *type;
16933
16934 module_name = dwarf2_name (die, cu);
19f392bc 16935 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16936
f55ee35c
JK
16937 return set_die_type (die, type, cu);
16938}
16939
5d7cb8df
JK
16940/* Read a Fortran module. */
16941
16942static void
16943read_module (struct die_info *die, struct dwarf2_cu *cu)
16944{
16945 struct die_info *child_die = die->child;
530e8392
KB
16946 struct type *type;
16947
16948 type = read_type_die (die, cu);
16949 new_symbol (die, type, cu);
5d7cb8df 16950
5d7cb8df
JK
16951 while (child_die && child_die->tag)
16952 {
16953 process_die (child_die, cu);
436c571c 16954 child_die = child_die->sibling;
5d7cb8df
JK
16955 }
16956}
16957
38d518c9
EZ
16958/* Return the name of the namespace represented by DIE. Set
16959 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16960 namespace. */
16961
16962static const char *
e142c38c 16963namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16964{
16965 struct die_info *current_die;
16966 const char *name = NULL;
16967
16968 /* Loop through the extensions until we find a name. */
16969
16970 for (current_die = die;
16971 current_die != NULL;
f2f0e013 16972 current_die = dwarf2_extension (die, &cu))
38d518c9 16973 {
96553a0c
DE
16974 /* We don't use dwarf2_name here so that we can detect the absence
16975 of a name -> anonymous namespace. */
7d45c7c3 16976 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16977
38d518c9
EZ
16978 if (name != NULL)
16979 break;
16980 }
16981
16982 /* Is it an anonymous namespace? */
16983
16984 *is_anonymous = (name == NULL);
16985 if (*is_anonymous)
2b1dbab0 16986 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16987
16988 return name;
d9fa45fe
DC
16989}
16990
c906108c
SS
16991/* Extract all information from a DW_TAG_pointer_type DIE and add to
16992 the user defined type vector. */
16993
f792889a 16994static struct type *
e7c27a73 16995read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16996{
5e22e966 16997 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16998 struct comp_unit_head *cu_header = &cu->header;
c906108c 16999 struct type *type;
8b2dbe47
KB
17000 struct attribute *attr_byte_size;
17001 struct attribute *attr_address_class;
17002 int byte_size, addr_class;
7e314c57
JK
17003 struct type *target_type;
17004
17005 target_type = die_type (die, cu);
c906108c 17006
7e314c57
JK
17007 /* The die_type call above may have already set the type for this DIE. */
17008 type = get_die_type (die, cu);
17009 if (type)
17010 return type;
17011
17012 type = lookup_pointer_type (target_type);
8b2dbe47 17013
e142c38c 17014 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17015 if (attr_byte_size)
529908cb 17016 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17017 else
8b2dbe47
KB
17018 byte_size = cu_header->addr_size;
17019
e142c38c 17020 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17021 if (attr_address_class)
529908cb 17022 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17023 else
17024 addr_class = DW_ADDR_none;
17025
2b4424c3
TT
17026 ULONGEST alignment = get_alignment (cu, die);
17027
17028 /* If the pointer size, alignment, or address class is different
17029 than the default, create a type variant marked as such and set
17030 the length accordingly. */
17031 if (TYPE_LENGTH (type) != byte_size
17032 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17033 && alignment != TYPE_RAW_ALIGN (type))
17034 || addr_class != DW_ADDR_none)
c906108c 17035 {
5e2b427d 17036 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17037 {
314ad88d
PA
17038 type_instance_flags type_flags
17039 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17040 addr_class);
876cecd0
TT
17041 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17042 == 0);
8b2dbe47
KB
17043 type = make_type_with_address_space (type, type_flags);
17044 }
17045 else if (TYPE_LENGTH (type) != byte_size)
17046 {
b98664d3 17047 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17048 }
2b4424c3
TT
17049 else if (TYPE_RAW_ALIGN (type) != alignment)
17050 {
b98664d3 17051 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17052 " - DIE at %s [in module %s]"),
17053 sect_offset_str (die->sect_off),
5e22e966 17054 objfile_name (cu->per_objfile->objfile));
2b4424c3 17055 }
6e70227d 17056 else
9a619af0
MS
17057 {
17058 /* Should we also complain about unhandled address classes? */
17059 }
c906108c 17060 }
8b2dbe47
KB
17061
17062 TYPE_LENGTH (type) = byte_size;
2b4424c3 17063 set_type_align (type, alignment);
f792889a 17064 return set_die_type (die, type, cu);
c906108c
SS
17065}
17066
17067/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17068 the user defined type vector. */
17069
f792889a 17070static struct type *
e7c27a73 17071read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17072{
17073 struct type *type;
17074 struct type *to_type;
17075 struct type *domain;
17076
e7c27a73
DJ
17077 to_type = die_type (die, cu);
17078 domain = die_containing_type (die, cu);
0d5de010 17079
7e314c57
JK
17080 /* The calls above may have already set the type for this DIE. */
17081 type = get_die_type (die, cu);
17082 if (type)
17083 return type;
17084
78134374 17085 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17086 type = lookup_methodptr_type (to_type);
78134374 17087 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17088 {
5e22e966 17089 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17090
17091 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17092 to_type->fields (), to_type->num_fields (),
a409645d 17093 to_type->has_varargs ());
7078baeb
TT
17094 type = lookup_methodptr_type (new_type);
17095 }
0d5de010
DJ
17096 else
17097 type = lookup_memberptr_type (to_type, domain);
c906108c 17098
f792889a 17099 return set_die_type (die, type, cu);
c906108c
SS
17100}
17101
4297a3f0 17102/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17103 the user defined type vector. */
17104
f792889a 17105static struct type *
4297a3f0 17106read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17107 enum type_code refcode)
c906108c 17108{
e7c27a73 17109 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17110 struct type *type, *target_type;
c906108c
SS
17111 struct attribute *attr;
17112
4297a3f0
AV
17113 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17114
7e314c57
JK
17115 target_type = die_type (die, cu);
17116
17117 /* The die_type call above may have already set the type for this DIE. */
17118 type = get_die_type (die, cu);
17119 if (type)
17120 return type;
17121
4297a3f0 17122 type = lookup_reference_type (target_type, refcode);
e142c38c 17123 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17124 if (attr != nullptr)
c906108c 17125 {
529908cb 17126 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17127 }
17128 else
17129 {
107d2387 17130 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17131 }
2b4424c3 17132 maybe_set_alignment (cu, die, type);
f792889a 17133 return set_die_type (die, type, cu);
c906108c
SS
17134}
17135
cf363f18
MW
17136/* Add the given cv-qualifiers to the element type of the array. GCC
17137 outputs DWARF type qualifiers that apply to an array, not the
17138 element type. But GDB relies on the array element type to carry
17139 the cv-qualifiers. This mimics section 6.7.3 of the C99
17140 specification. */
17141
17142static struct type *
17143add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17144 struct type *base_type, int cnst, int voltl)
17145{
17146 struct type *el_type, *inner_array;
17147
17148 base_type = copy_type (base_type);
17149 inner_array = base_type;
17150
78134374 17151 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17152 {
17153 TYPE_TARGET_TYPE (inner_array) =
17154 copy_type (TYPE_TARGET_TYPE (inner_array));
17155 inner_array = TYPE_TARGET_TYPE (inner_array);
17156 }
17157
17158 el_type = TYPE_TARGET_TYPE (inner_array);
17159 cnst |= TYPE_CONST (el_type);
17160 voltl |= TYPE_VOLATILE (el_type);
17161 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17162
17163 return set_die_type (die, base_type, cu);
17164}
17165
f792889a 17166static struct type *
e7c27a73 17167read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17168{
f792889a 17169 struct type *base_type, *cv_type;
c906108c 17170
e7c27a73 17171 base_type = die_type (die, cu);
7e314c57
JK
17172
17173 /* The die_type call above may have already set the type for this DIE. */
17174 cv_type = get_die_type (die, cu);
17175 if (cv_type)
17176 return cv_type;
17177
2f608a3a
KW
17178 /* In case the const qualifier is applied to an array type, the element type
17179 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17180 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17181 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17182
f792889a
DJ
17183 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17184 return set_die_type (die, cv_type, cu);
c906108c
SS
17185}
17186
f792889a 17187static struct type *
e7c27a73 17188read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17189{
f792889a 17190 struct type *base_type, *cv_type;
c906108c 17191
e7c27a73 17192 base_type = die_type (die, cu);
7e314c57
JK
17193
17194 /* The die_type call above may have already set the type for this DIE. */
17195 cv_type = get_die_type (die, cu);
17196 if (cv_type)
17197 return cv_type;
17198
cf363f18
MW
17199 /* In case the volatile qualifier is applied to an array type, the
17200 element type is so qualified, not the array type (section 6.7.3
17201 of C99). */
78134374 17202 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17203 return add_array_cv_type (die, cu, base_type, 0, 1);
17204
f792889a
DJ
17205 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17206 return set_die_type (die, cv_type, cu);
c906108c
SS
17207}
17208
06d66ee9
TT
17209/* Handle DW_TAG_restrict_type. */
17210
17211static struct type *
17212read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17213{
17214 struct type *base_type, *cv_type;
17215
17216 base_type = die_type (die, cu);
17217
17218 /* The die_type call above may have already set the type for this DIE. */
17219 cv_type = get_die_type (die, cu);
17220 if (cv_type)
17221 return cv_type;
17222
17223 cv_type = make_restrict_type (base_type);
17224 return set_die_type (die, cv_type, cu);
17225}
17226
a2c2acaf
MW
17227/* Handle DW_TAG_atomic_type. */
17228
17229static struct type *
17230read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17231{
17232 struct type *base_type, *cv_type;
17233
17234 base_type = die_type (die, cu);
17235
17236 /* The die_type call above may have already set the type for this DIE. */
17237 cv_type = get_die_type (die, cu);
17238 if (cv_type)
17239 return cv_type;
17240
17241 cv_type = make_atomic_type (base_type);
17242 return set_die_type (die, cv_type, cu);
17243}
17244
c906108c
SS
17245/* Extract all information from a DW_TAG_string_type DIE and add to
17246 the user defined type vector. It isn't really a user defined type,
17247 but it behaves like one, with other DIE's using an AT_user_def_type
17248 attribute to reference it. */
17249
f792889a 17250static struct type *
e7c27a73 17251read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17252{
5e22e966 17253 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17254 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17255 struct type *type, *range_type, *index_type, *char_type;
17256 struct attribute *attr;
216a7e6b
AB
17257 struct dynamic_prop prop;
17258 bool length_is_constant = true;
17259 LONGEST length;
17260
17261 /* There are a couple of places where bit sizes might be made use of
17262 when parsing a DW_TAG_string_type, however, no producer that we know
17263 of make use of these. Handling bit sizes that are a multiple of the
17264 byte size is easy enough, but what about other bit sizes? Lets deal
17265 with that problem when we have to. Warn about these attributes being
17266 unsupported, then parse the type and ignore them like we always
17267 have. */
17268 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17269 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17270 {
17271 static bool warning_printed = false;
17272 if (!warning_printed)
17273 {
17274 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17275 "currently supported on DW_TAG_string_type."));
17276 warning_printed = true;
17277 }
17278 }
c906108c 17279
e142c38c 17280 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17281 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17282 {
17283 /* The string length describes the location at which the length of
17284 the string can be found. The size of the length field can be
17285 specified with one of the attributes below. */
17286 struct type *prop_type;
17287 struct attribute *len
17288 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17289 if (len == nullptr)
17290 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17291 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17292 {
17293 /* Pass 0 as the default as we know this attribute is constant
17294 and the default value will not be returned. */
0826b30a 17295 LONGEST sz = len->constant_value (0);
293e7e51 17296 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17297 }
17298 else
17299 {
17300 /* If the size is not specified then we assume it is the size of
17301 an address on this target. */
293e7e51 17302 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17303 }
17304
17305 /* Convert the attribute into a dynamic property. */
17306 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17307 length = 1;
17308 else
17309 length_is_constant = false;
17310 }
17311 else if (attr != nullptr)
17312 {
17313 /* This DW_AT_string_length just contains the length with no
17314 indirection. There's no need to create a dynamic property in this
17315 case. Pass 0 for the default value as we know it will not be
17316 returned in this case. */
0826b30a 17317 length = attr->constant_value (0);
216a7e6b
AB
17318 }
17319 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17320 {
216a7e6b 17321 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17322 length = attr->constant_value (1);
c906108c
SS
17323 }
17324 else
17325 {
216a7e6b
AB
17326 /* Use 1 as a fallback length if we have nothing else. */
17327 length = 1;
c906108c 17328 }
6ccb9162 17329
46bf5051 17330 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17331 if (length_is_constant)
17332 range_type = create_static_range_type (NULL, index_type, 1, length);
17333 else
17334 {
17335 struct dynamic_prop low_bound;
17336
8c2e4e06 17337 low_bound.set_const_val (1);
216a7e6b
AB
17338 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17339 }
3b7538c0
UW
17340 char_type = language_string_char_type (cu->language_defn, gdbarch);
17341 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17342
f792889a 17343 return set_die_type (die, type, cu);
c906108c
SS
17344}
17345
4d804846
JB
17346/* Assuming that DIE corresponds to a function, returns nonzero
17347 if the function is prototyped. */
17348
17349static int
17350prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17351{
17352 struct attribute *attr;
17353
17354 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17355 if (attr && attr->as_boolean ())
4d804846
JB
17356 return 1;
17357
17358 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17359 is only meaningful for C, but the concept also extends to other
4d804846
JB
17360 languages that allow unprototyped functions (Eg: Objective C).
17361 For all other languages, assume that functions are always
17362 prototyped. */
3e9f1ca1
TT
17363 if (cu->per_cu->lang != language_c
17364 && cu->per_cu->lang != language_objc
17365 && cu->per_cu->lang != language_opencl)
4d804846
JB
17366 return 1;
17367
17368 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17369 prototyped and unprototyped functions; default to prototyped,
17370 since that is more common in modern code (and RealView warns
17371 about unprototyped functions). */
17372 if (producer_is_realview (cu->producer))
17373 return 1;
17374
17375 return 0;
17376}
17377
c906108c
SS
17378/* Handle DIES due to C code like:
17379
17380 struct foo
c5aa993b
JM
17381 {
17382 int (*funcp)(int a, long l);
17383 int b;
17384 };
c906108c 17385
0963b4bd 17386 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17387
f792889a 17388static struct type *
e7c27a73 17389read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17390{
5e22e966 17391 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17392 struct type *type; /* Type that this function returns. */
17393 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17394 struct attribute *attr;
17395
e7c27a73 17396 type = die_type (die, cu);
7e314c57
JK
17397
17398 /* The die_type call above may have already set the type for this DIE. */
17399 ftype = get_die_type (die, cu);
17400 if (ftype)
17401 return ftype;
17402
0c8b41f1 17403 ftype = lookup_function_type (type);
c906108c 17404
4d804846 17405 if (prototyped_function_p (die, cu))
27e69b7a 17406 ftype->set_is_prototyped (true);
c906108c 17407
c055b101
CV
17408 /* Store the calling convention in the type if it's available in
17409 the subroutine die. Otherwise set the calling convention to
17410 the default value DW_CC_normal. */
17411 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 17412 if (attr != nullptr
529908cb 17413 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 17414 TYPE_CALLING_CONVENTION (ftype)
529908cb 17415 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
17416 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17417 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17418 else
17419 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17420
743649fd
MW
17421 /* Record whether the function returns normally to its caller or not
17422 if the DWARF producer set that information. */
17423 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 17424 if (attr && attr->as_boolean ())
743649fd
MW
17425 TYPE_NO_RETURN (ftype) = 1;
17426
76c10ea2
GM
17427 /* We need to add the subroutine type to the die immediately so
17428 we don't infinitely recurse when dealing with parameters
0963b4bd 17429 declared as the same subroutine type. */
76c10ea2 17430 set_die_type (die, ftype, cu);
6e70227d 17431
639d11d3 17432 if (die->child != NULL)
c906108c 17433 {
bb5ed363 17434 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17435 struct die_info *child_die;
8072405b 17436 int nparams, iparams;
c906108c
SS
17437
17438 /* Count the number of parameters.
dda83cd7
SM
17439 FIXME: GDB currently ignores vararg functions, but knows about
17440 vararg member functions. */
8072405b 17441 nparams = 0;
639d11d3 17442 child_die = die->child;
c906108c
SS
17443 while (child_die && child_die->tag)
17444 {
17445 if (child_die->tag == DW_TAG_formal_parameter)
17446 nparams++;
17447 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
17448 ftype->set_has_varargs (true);
17449
436c571c 17450 child_die = child_die->sibling;
c906108c
SS
17451 }
17452
17453 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17454 ftype->set_num_fields (nparams);
3cabb6b0
SM
17455 ftype->set_fields
17456 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17457
8072405b
JK
17458 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17459 even if we error out during the parameters reading below. */
17460 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17461 ftype->field (iparams).set_type (void_type);
8072405b
JK
17462
17463 iparams = 0;
639d11d3 17464 child_die = die->child;
c906108c
SS
17465 while (child_die && child_die->tag)
17466 {
17467 if (child_die->tag == DW_TAG_formal_parameter)
17468 {
3ce3b1ba
PA
17469 struct type *arg_type;
17470
17471 /* DWARF version 2 has no clean way to discern C++
17472 static and non-static member functions. G++ helps
17473 GDB by marking the first parameter for non-static
17474 member functions (which is the this pointer) as
17475 artificial. We pass this information to
17476 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17477
17478 DWARF version 3 added DW_AT_object_pointer, which GCC
17479 4.5 does not yet generate. */
e142c38c 17480 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17481 if (attr != nullptr)
c45bc3f8 17482 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 17483 else
9c37b5ae 17484 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17485 arg_type = die_type (child_die, cu);
17486
17487 /* RealView does not mark THIS as const, which the testsuite
17488 expects. GCC marks THIS as const in method definitions,
17489 but not in the class specifications (GCC PR 43053). */
3e9f1ca1
TT
17490 if (cu->per_cu->lang == language_cplus
17491 && !TYPE_CONST (arg_type)
3ce3b1ba
PA
17492 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17493 {
17494 int is_this = 0;
17495 struct dwarf2_cu *arg_cu = cu;
17496 const char *name = dwarf2_name (child_die, cu);
17497
17498 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17499 if (attr != nullptr)
3ce3b1ba
PA
17500 {
17501 /* If the compiler emits this, use it. */
17502 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17503 is_this = 1;
17504 }
17505 else if (name && strcmp (name, "this") == 0)
17506 /* Function definitions will have the argument names. */
17507 is_this = 1;
17508 else if (name == NULL && iparams == 0)
17509 /* Declarations may not have the names, so like
17510 elsewhere in GDB, assume an artificial first
17511 argument is "this". */
17512 is_this = 1;
17513
17514 if (is_this)
17515 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17516 arg_type, 0);
17517 }
17518
5d14b6e5 17519 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17520 iparams++;
17521 }
436c571c 17522 child_die = child_die->sibling;
c906108c
SS
17523 }
17524 }
17525
76c10ea2 17526 return ftype;
c906108c
SS
17527}
17528
f792889a 17529static struct type *
e7c27a73 17530read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17531{
5e22e966 17532 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17533 const char *name = NULL;
3c8e0968 17534 struct type *this_type, *target_type;
c906108c 17535
94af9270 17536 name = dwarf2_full_name (NULL, die, cu);
19f392bc 17537 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 17538 this_type->set_target_is_stub (true);
f792889a 17539 set_die_type (die, this_type, cu);
3c8e0968
DE
17540 target_type = die_type (die, cu);
17541 if (target_type != this_type)
17542 TYPE_TARGET_TYPE (this_type) = target_type;
17543 else
17544 {
17545 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17546 spec and cause infinite loops in GDB. */
b98664d3 17547 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17548 "- DIE at %s [in module %s]"),
17549 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17550 TYPE_TARGET_TYPE (this_type) = NULL;
17551 }
e4003a34
TV
17552 if (name == NULL)
17553 {
17554 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17555 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17556 Handle these by just returning the target type, rather than
17557 constructing an anonymous typedef type and trying to handle this
17558 elsewhere. */
17559 set_die_type (die, target_type, cu);
17560 return target_type;
17561 }
f792889a 17562 return this_type;
c906108c
SS
17563}
17564
a625a8c9
TT
17565/* Helper for get_dwarf2_rational_constant that computes the value of
17566 a given gmp_mpz given an attribute. */
17567
17568static void
17569get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17570{
17571 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17572 location expression that pushes an implicit value. */
17573 if (attr->form == DW_FORM_exprloc)
17574 {
17575 dwarf_block *blk = attr->as_block ();
17576 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17577 {
17578 uint64_t len;
17579 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17580 blk->data + blk->size,
17581 &len);
17582 if (ptr - blk->data + len <= blk->size)
17583 {
17584 mpz_import (value->val, len,
17585 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17586 1, 0, 0, ptr);
17587 return;
17588 }
17589 }
17590
17591 /* On failure set it to 1. */
17592 *value = gdb_mpz (1);
17593 }
17594 else if (attr->form_is_block ())
17595 {
17596 dwarf_block *blk = attr->as_block ();
17597 mpz_import (value->val, blk->size,
17598 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17599 1, 0, 0, blk->data);
17600 }
17601 else
17602 *value = gdb_mpz (attr->constant_value (1));
17603}
17604
09584414
JB
17605/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17606 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17607
17608 If the numerator and/or numerator attribute is missing,
17609 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17610 untouched. */
17611
17612static void
17613get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 17614 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
17615{
17616 struct attribute *num_attr, *denom_attr;
17617
17618 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17619 if (num_attr == nullptr)
17620 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17621 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17622
17623 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17624 if (denom_attr == nullptr)
17625 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17626 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17627
17628 if (num_attr == nullptr || denom_attr == nullptr)
17629 return;
17630
a625a8c9
TT
17631 get_mpz (cu, numerator, num_attr);
17632 get_mpz (cu, denominator, denom_attr);
09584414
JB
17633}
17634
17635/* Same as get_dwarf2_rational_constant, but extracting an unsigned
17636 rational constant, rather than a signed one.
17637
17638 If the rational constant has a negative value, a complaint
17639 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17640
17641static void
17642get_dwarf2_unsigned_rational_constant (struct die_info *die,
17643 struct dwarf2_cu *cu,
5cde1d82
TT
17644 gdb_mpz *numerator,
17645 gdb_mpz *denominator)
09584414 17646{
5cde1d82
TT
17647 gdb_mpz num (1);
17648 gdb_mpz denom (1);
09584414
JB
17649
17650 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 17651 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 17652 {
5cde1d82
TT
17653 mpz_neg (num.val, num.val);
17654 mpz_neg (denom.val, denom.val);
09584414 17655 }
5cde1d82 17656 else if (mpz_sgn (num.val) == -1)
09584414
JB
17657 {
17658 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17659 " in DIE at %s"),
17660 sect_offset_str (die->sect_off));
17661 return;
17662 }
5cde1d82 17663 else if (mpz_sgn (denom.val) == -1)
09584414
JB
17664 {
17665 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17666 " in DIE at %s"),
17667 sect_offset_str (die->sect_off));
17668 return;
17669 }
17670
5cde1d82
TT
17671 *numerator = std::move (num);
17672 *denominator = std::move (denom);
09584414
JB
17673}
17674
bbcdf9ab
TT
17675/* Assuming that ENCODING is a string whose contents starting at the
17676 K'th character is "_nn" where "nn" is a decimal number, scan that
17677 number and set RESULT to the value. K is updated to point to the
17678 character immediately following the number.
17679
17680 If the string does not conform to the format described above, false
17681 is returned, and K may or may not be changed. */
17682
17683static bool
17684ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17685{
17686 /* The next character should be an underscore ('_') followed
17687 by a digit. */
17688 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17689 return false;
17690
17691 /* Skip the underscore. */
17692 k++;
17693 int start = k;
17694
17695 /* Determine the number of digits for our number. */
17696 while (isdigit (encoding[k]))
17697 k++;
17698 if (k == start)
17699 return false;
17700
17701 std::string copy (&encoding[start], k - start);
17702 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17703 return false;
17704
17705 return true;
17706}
17707
17708/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17709 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17710 DENOM, update OFFSET, and return true on success. Return false on
17711 failure. */
17712
17713static bool
17714ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17715 gdb_mpz *num, gdb_mpz *denom)
17716{
17717 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17718 return false;
17719 return ada_get_gnat_encoded_number (encoding, offset, denom);
17720}
17721
09584414 17722/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
17723 of the corresponding TYPE by setting its type-specific data. CU is
17724 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17725 encodings. It is nullptr if the GNAT encoding should be
17726 ignored. */
09584414
JB
17727
17728static void
bbcdf9ab
TT
17729finish_fixed_point_type (struct type *type, const char *suffix,
17730 struct die_info *die, struct dwarf2_cu *cu)
09584414 17731{
09584414
JB
17732 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17733 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17734
bbcdf9ab
TT
17735 /* If GNAT encodings are preferred, don't examine the
17736 attributes. */
17737 struct attribute *attr = nullptr;
17738 if (suffix == nullptr)
17739 {
17740 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17741 if (attr == nullptr)
17742 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17743 if (attr == nullptr)
17744 attr = dwarf2_attr (die, DW_AT_small, cu);
17745 }
09584414 17746
5cde1d82
TT
17747 /* Numerator and denominator of our fixed-point type's scaling factor.
17748 The default is a scaling factor of 1, which we use as a fallback
17749 when we are not able to decode it (problem with the debugging info,
17750 unsupported forms, bug in GDB, etc...). Using that as the default
17751 allows us to at least print the unscaled value, which might still
17752 be useful to a user. */
17753 gdb_mpz scale_num (1);
17754 gdb_mpz scale_denom (1);
17755
09584414
JB
17756 if (attr == nullptr)
17757 {
bbcdf9ab
TT
17758 int offset = 0;
17759 if (suffix != nullptr
17760 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17761 &scale_denom)
17762 /* The number might be encoded as _nn_dd_nn_dd, where the
17763 second ratio is the 'small value. In this situation, we
17764 want the second value. */
17765 && (suffix[offset] != '_'
17766 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17767 &scale_denom)))
17768 {
17769 /* Found it. */
17770 }
17771 else
17772 {
17773 /* Scaling factor not found. Assume a scaling factor of 1,
17774 and hope for the best. At least the user will be able to
17775 see the encoded value. */
17776 scale_num = 1;
17777 scale_denom = 1;
17778 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17779 sect_offset_str (die->sect_off));
17780 }
09584414
JB
17781 }
17782 else if (attr->name == DW_AT_binary_scale)
17783 {
17784 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 17785 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 17786
5cde1d82 17787 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
17788 }
17789 else if (attr->name == DW_AT_decimal_scale)
17790 {
17791 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 17792 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 17793
5cde1d82 17794 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
17795 }
17796 else if (attr->name == DW_AT_small)
17797 {
17798 struct die_info *scale_die;
17799 struct dwarf2_cu *scale_cu = cu;
17800
17801 scale_die = follow_die_ref (die, attr, &scale_cu);
17802 if (scale_die->tag == DW_TAG_constant)
17803 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17804 &scale_num, &scale_denom);
17805 else
17806 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17807 " (DIE at %s)"),
17808 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17809 }
17810 else
17811 {
17812 complaint (_("unsupported scale attribute %s for fixed-point type"
17813 " (DIE at %s)"),
17814 dwarf_attr_name (attr->name),
17815 sect_offset_str (die->sect_off));
17816 }
17817
2a12c336 17818 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
17819 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17820 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
17821 mpq_canonicalize (scaling_factor.val);
17822}
17823
bbcdf9ab
TT
17824/* The gnat-encoding suffix for fixed point. */
17825
17826#define GNAT_FIXED_POINT_SUFFIX "___XF_"
17827
17828/* If NAME encodes an Ada fixed-point type, return a pointer to the
17829 "XF" suffix of the name. The text after this is what encodes the
17830 'small and 'delta information. Otherwise, return nullptr. */
17831
17832static const char *
17833gnat_encoded_fixed_point_type_info (const char *name)
17834{
17835 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17836}
17837
9b790ce7
UW
17838/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17839 (which may be different from NAME) to the architecture back-end to allow
17840 it to guess the correct format if necessary. */
17841
17842static struct type *
17843dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17844 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17845{
08feed99 17846 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17847 const struct floatformat **format;
17848 struct type *type;
17849
17850 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17851 if (format)
103a685e 17852 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17853 else
77b7c781 17854 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17855
17856 return type;
17857}
17858
eb77c9df
AB
17859/* Allocate an integer type of size BITS and name NAME. */
17860
17861static struct type *
17862dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17863 int bits, int unsigned_p, const char *name)
17864{
17865 struct type *type;
17866
17867 /* Versions of Intel's C Compiler generate an integer type called "void"
17868 instead of using DW_TAG_unspecified_type. This has been seen on
17869 at least versions 14, 17, and 18. */
35ee2dc2
AB
17870 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17871 && strcmp (name, "void") == 0)
eb77c9df
AB
17872 type = objfile_type (objfile)->builtin_void;
17873 else
17874 type = init_integer_type (objfile, bits, unsigned_p, name);
17875
17876 return type;
17877}
17878
09584414
JB
17879/* Return true if DIE has a DW_AT_small attribute whose value is
17880 a constant rational, where both the numerator and denominator
17881 are equal to zero.
17882
17883 CU is the DIE's Compilation Unit. */
17884
17885static bool
17886has_zero_over_zero_small_attribute (struct die_info *die,
17887 struct dwarf2_cu *cu)
17888{
17889 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
17890 if (attr == nullptr)
17891 return false;
17892
17893 struct dwarf2_cu *scale_cu = cu;
17894 struct die_info *scale_die
17895 = follow_die_ref (die, attr, &scale_cu);
17896
17897 if (scale_die->tag != DW_TAG_constant)
17898 return false;
17899
5cde1d82 17900 gdb_mpz num (1), denom (1);
09584414 17901 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 17902 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
17903}
17904
8bdc1658
AB
17905/* Initialise and return a floating point type of size BITS suitable for
17906 use as a component of a complex number. The NAME_HINT is passed through
17907 when initialising the floating point type and is the name of the complex
17908 type.
17909
17910 As DWARF doesn't currently provide an explicit name for the components
17911 of a complex number, but it can be helpful to have these components
17912 named, we try to select a suitable name based on the size of the
17913 component. */
17914static struct type *
17915dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17916 struct objfile *objfile,
103a685e
TT
17917 int bits, const char *name_hint,
17918 enum bfd_endian byte_order)
8bdc1658 17919{
08feed99 17920 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17921 struct type *tt = nullptr;
17922
35add35e
AB
17923 /* Try to find a suitable floating point builtin type of size BITS.
17924 We're going to use the name of this type as the name for the complex
17925 target type that we are about to create. */
3e9f1ca1 17926 switch (cu->per_cu->lang)
8bdc1658 17927 {
1db455a7
AB
17928 case language_fortran:
17929 switch (bits)
17930 {
17931 case 32:
17932 tt = builtin_f_type (gdbarch)->builtin_real;
17933 break;
17934 case 64:
17935 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17936 break;
17937 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17938 case 128:
17939 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17940 break;
17941 }
8bdc1658 17942 break;
1db455a7
AB
17943 default:
17944 switch (bits)
17945 {
17946 case 32:
17947 tt = builtin_type (gdbarch)->builtin_float;
17948 break;
17949 case 64:
17950 tt = builtin_type (gdbarch)->builtin_double;
17951 break;
17952 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17953 case 128:
17954 tt = builtin_type (gdbarch)->builtin_long_double;
17955 break;
17956 }
8bdc1658
AB
17957 break;
17958 }
17959
35add35e
AB
17960 /* If the type we found doesn't match the size we were looking for, then
17961 pretend we didn't find a type at all, the complex target type we
17962 create will then be nameless. */
a12e5744 17963 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17964 tt = nullptr;
17965
7d93a1e0 17966 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17967 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17968}
17969
c906108c
SS
17970/* Find a representation of a given base type and install
17971 it in the TYPE field of the die. */
17972
f792889a 17973static struct type *
e7c27a73 17974read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17975{
5e22e966 17976 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17977 struct type *type;
17978 struct attribute *attr;
19f392bc 17979 int encoding = 0, bits = 0;
15d034d0 17980 const char *name;
34877895 17981 gdbarch *arch;
c906108c 17982
e142c38c 17983 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
17984 if (attr != nullptr && attr->form_is_constant ())
17985 encoding = attr->constant_value (0);
e142c38c 17986 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17987 if (attr != nullptr)
529908cb 17988 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 17989 name = dwarf2_name (die, cu);
6ccb9162 17990 if (!name)
34877895 17991 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17992
08feed99 17993 arch = objfile->arch ();
103a685e
TT
17994 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17995
34877895 17996 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 17997 if (attr != nullptr && attr->form_is_constant ())
103a685e 17998 {
529908cb 17999 int endianity = attr->constant_value (0);
103a685e
TT
18000
18001 switch (endianity)
18002 {
18003 case DW_END_big:
18004 byte_order = BFD_ENDIAN_BIG;
18005 break;
18006 case DW_END_little:
18007 byte_order = BFD_ENDIAN_LITTLE;
18008 break;
18009 default:
18010 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18011 break;
18012 }
18013 }
6ccb9162 18014
09584414 18015 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
3e9f1ca1 18016 && cu->per_cu->lang == language_ada
09584414
JB
18017 && has_zero_over_zero_small_attribute (die, cu))
18018 {
18019 /* brobecker/2018-02-24: This is a fixed point type for which
18020 the scaling factor is represented as fraction whose value
18021 does not make sense (zero divided by zero), so we should
18022 normally never see these. However, there is a small category
18023 of fixed point types for which GNAT is unable to provide
18024 the scaling factor via the standard DWARF mechanisms, and
18025 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18026 This is likely what this DIE is about. */
09584414
JB
18027 encoding = (encoding == DW_ATE_signed_fixed
18028 ? DW_ATE_signed
18029 : DW_ATE_unsigned);
18030 }
18031
bbcdf9ab
TT
18032 /* With GNAT encodings, fixed-point information will be encoded in
18033 the type name. Note that this can also occur with the above
18034 zero-over-zero case, which is why this is a separate "if" rather
18035 than an "else if". */
18036 const char *gnat_encoding_suffix = nullptr;
18037 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
3e9f1ca1 18038 && cu->per_cu->lang == language_ada
bbcdf9ab
TT
18039 && name != nullptr)
18040 {
18041 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18042 if (gnat_encoding_suffix != nullptr)
18043 {
18044 gdb_assert (startswith (gnat_encoding_suffix,
18045 GNAT_FIXED_POINT_SUFFIX));
18046 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18047 name, gnat_encoding_suffix - name);
18048 /* Use -1 here so that SUFFIX points at the "_" after the
18049 "XF". */
18050 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18051
18052 encoding = (encoding == DW_ATE_signed
18053 ? DW_ATE_signed_fixed
18054 : DW_ATE_unsigned_fixed);
18055 }
18056 }
18057
6ccb9162 18058 switch (encoding)
c906108c 18059 {
6ccb9162
UW
18060 case DW_ATE_address:
18061 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18062 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18063 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18064 break;
18065 case DW_ATE_boolean:
19f392bc 18066 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18067 break;
18068 case DW_ATE_complex_float:
103a685e
TT
18069 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18070 byte_order);
78134374 18071 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18072 {
18073 if (name == nullptr)
18074 {
18075 struct obstack *obstack
5e22e966 18076 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18077 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18078 nullptr);
18079 }
18080 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18081 }
18082 else
18083 type = init_complex_type (name, type);
6ccb9162
UW
18084 break;
18085 case DW_ATE_decimal_float:
19f392bc 18086 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18087 break;
18088 case DW_ATE_float:
103a685e 18089 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18090 break;
18091 case DW_ATE_signed:
eb77c9df 18092 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18093 break;
18094 case DW_ATE_unsigned:
3e9f1ca1 18095 if (cu->per_cu->lang == language_fortran
3b2b8fea 18096 && name
61012eef 18097 && startswith (name, "character("))
19f392bc
UW
18098 type = init_character_type (objfile, bits, 1, name);
18099 else
eb77c9df 18100 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18101 break;
18102 case DW_ATE_signed_char:
3e9f1ca1
TT
18103 if (cu->per_cu->lang == language_ada
18104 || cu->per_cu->lang == language_m2
18105 || cu->per_cu->lang == language_pascal
18106 || cu->per_cu->lang == 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:
3e9f1ca1
TT
18112 if (cu->per_cu->lang == language_ada
18113 || cu->per_cu->lang == language_m2
18114 || cu->per_cu->lang == language_pascal
18115 || cu->per_cu->lang == language_fortran
18116 || cu->per_cu->lang == language_rust)
19f392bc
UW
18117 type = init_character_type (objfile, bits, 1, name);
18118 else
eb77c9df 18119 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18120 break;
75079b2b 18121 case DW_ATE_UTF:
53e710ac 18122 {
53e710ac
PA
18123 if (bits == 16)
18124 type = builtin_type (arch)->builtin_char16;
18125 else if (bits == 32)
18126 type = builtin_type (arch)->builtin_char32;
18127 else
18128 {
b98664d3 18129 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18130 bits);
eb77c9df 18131 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18132 }
18133 return set_die_type (die, type, cu);
18134 }
75079b2b 18135 break;
09584414
JB
18136 case DW_ATE_signed_fixed:
18137 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18138 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18139 break;
18140 case DW_ATE_unsigned_fixed:
18141 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18142 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18143 break;
75079b2b 18144
6ccb9162 18145 default:
b98664d3 18146 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18147 dwarf_type_encoding_name (encoding));
77b7c781 18148 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18149 break;
c906108c 18150 }
6ccb9162 18151
0114d602 18152 if (name && strcmp (name, "char") == 0)
15152a54 18153 type->set_has_no_signedness (true);
0114d602 18154
2b4424c3
TT
18155 maybe_set_alignment (cu, die, type);
18156
db558e34 18157 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18158
20a5fcbd
TT
18159 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18160 {
18161 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18162 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18163 {
529908cb 18164 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18165 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18166 /* Only use the attributes if they make sense together. */
18167 if (attr == nullptr
529908cb
TT
18168 || (attr->as_unsigned () + real_bit_size
18169 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18170 {
18171 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18172 = real_bit_size;
18173 if (attr != nullptr)
18174 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18175 = attr->as_unsigned ();
20a5fcbd
TT
18176 }
18177 }
18178 }
18179
f792889a 18180 return set_die_type (die, type, cu);
c906108c
SS
18181}
18182
386de171
TT
18183/* A helper function that returns the name of DIE, if it refers to a
18184 variable declaration. */
18185
18186static const char *
18187var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
18188{
18189 if (die->tag != DW_TAG_variable)
18190 return nullptr;
18191
18192 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
18193 if (attr == nullptr || !attr->as_boolean ())
18194 return nullptr;
18195
18196 attr = dwarf2_attr (die, DW_AT_name, cu);
18197 if (attr == nullptr)
18198 return nullptr;
18199 return attr->as_string ();
18200}
18201
80180f79
SA
18202/* Parse dwarf attribute if it's a block, reference or constant and put the
18203 resulting value of the attribute into struct bound_prop.
18204 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18205
18206static int
18207attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18208 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18209 struct type *default_type)
80180f79
SA
18210{
18211 struct dwarf2_property_baton *baton;
5e22e966 18212 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18213 struct objfile *objfile = per_objfile->objfile;
18214 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18215
9a49df9d
AB
18216 gdb_assert (default_type != NULL);
18217
80180f79
SA
18218 if (attr == NULL || prop == NULL)
18219 return 0;
18220
4fc6c0d5 18221 if (attr->form_is_block ())
80180f79 18222 {
8d749320 18223 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18224 baton->property_type = default_type;
80180f79 18225 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18226 baton->locexpr.per_objfile = per_objfile;
9d2246fc 18227
52a49e16
TV
18228 struct dwarf_block *block;
18229 if (attr->form == DW_FORM_data16)
18230 {
18231 size_t data_size = 16;
18232 block = XOBNEW (obstack, struct dwarf_block);
18233 block->size = (data_size
18234 + 2 /* Extra bytes for DW_OP and arg. */);
18235 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
18236 data[0] = DW_OP_implicit_value;
18237 data[1] = data_size;
18238 memcpy (&data[2], attr->as_block ()->data, data_size);
18239 block->data = data;
18240 }
18241 else
18242 block = attr->as_block ();
18243
9d2246fc
TT
18244 baton->locexpr.size = block->size;
18245 baton->locexpr.data = block->data;
216a7e6b
AB
18246 switch (attr->name)
18247 {
18248 case DW_AT_string_length:
18249 baton->locexpr.is_reference = true;
18250 break;
18251 default:
18252 baton->locexpr.is_reference = false;
18253 break;
18254 }
8c2e4e06
SM
18255
18256 prop->set_locexpr (baton);
18257 gdb_assert (prop->baton () != NULL);
80180f79 18258 }
cd6c91b4 18259 else if (attr->form_is_ref ())
80180f79
SA
18260 {
18261 struct dwarf2_cu *target_cu = cu;
18262 struct die_info *target_die;
18263 struct attribute *target_attr;
18264
18265 target_die = follow_die_ref (die, attr, &target_cu);
18266 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18267 if (target_attr == NULL)
18268 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18269 target_cu);
80180f79 18270 if (target_attr == NULL)
386de171
TT
18271 {
18272 const char *name = var_decl_name (target_die, target_cu);
18273 if (name != nullptr)
18274 {
18275 prop->set_variable_name (name);
18276 return 1;
18277 }
18278 return 0;
18279 }
80180f79 18280
df25ebbd 18281 switch (target_attr->name)
80180f79 18282 {
df25ebbd 18283 case DW_AT_location:
cd6c91b4 18284 if (target_attr->form_is_section_offset ())
df25ebbd 18285 {
8d749320 18286 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18287 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18288 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18289 prop->set_loclist (baton);
18290 gdb_assert (prop->baton () != NULL);
df25ebbd 18291 }
4fc6c0d5 18292 else if (target_attr->form_is_block ())
df25ebbd 18293 {
8d749320 18294 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18295 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18296 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18297 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18298 struct dwarf_block *block = target_attr->as_block ();
18299 baton->locexpr.size = block->size;
18300 baton->locexpr.data = block->data;
9a49df9d 18301 baton->locexpr.is_reference = true;
8c2e4e06
SM
18302 prop->set_locexpr (baton);
18303 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18304 }
18305 else
18306 {
18307 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18308 "dynamic property");
18309 return 0;
18310 }
18311 break;
18312 case DW_AT_data_member_location:
18313 {
18314 LONGEST offset;
18315
18316 if (!handle_data_member_location (target_die, target_cu,
18317 &offset))
18318 return 0;
18319
8d749320 18320 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18321 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18322 target_cu);
df25ebbd
JB
18323 baton->offset_info.offset = offset;
18324 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18325 prop->set_addr_offset (baton);
df25ebbd
JB
18326 break;
18327 }
80180f79
SA
18328 }
18329 }
cd6c91b4 18330 else if (attr->form_is_constant ())
8c2e4e06 18331 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18332 else
18333 {
18334 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18335 dwarf2_name (die, cu));
18336 return 0;
18337 }
18338
18339 return 1;
18340}
18341
09ba997f 18342/* See read.h. */
9a49df9d 18343
09ba997f 18344struct type *
293e7e51 18345dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18346{
9a49df9d
AB
18347 struct type *int_type;
18348
18349 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18350#define TRY_TYPE(F) \
18351 int_type = (unsigned_p \
18352 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18353 : objfile_type (objfile)->builtin_ ## F); \
18354 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18355 return int_type
18356
18357 TRY_TYPE (char);
18358 TRY_TYPE (short);
18359 TRY_TYPE (int);
18360 TRY_TYPE (long);
18361 TRY_TYPE (long_long);
18362
18363#undef TRY_TYPE
18364
18365 gdb_assert_not_reached ("unable to find suitable integer type");
18366}
18367
b86352cf
AB
18368/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18369 present (which is valid) then compute the default type based on the
18370 compilation units address size. */
18371
18372static struct type *
18373read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18374{
18375 struct type *index_type = die_type (die, cu);
18376
18377 /* Dwarf-2 specifications explicitly allows to create subrange types
18378 without specifying a base type.
18379 In that case, the base type must be set to the type of
18380 the lower bound, upper bound or count, in that order, if any of these
18381 three attributes references an object that has a type.
18382 If no base type is found, the Dwarf-2 specifications say that
18383 a signed integer type of size equal to the size of an address should
18384 be used.
18385 For the following C code: `extern char gdb_int [];'
18386 GCC produces an empty range DIE.
18387 FIXME: muller/2010-05-28: Possible references to object for low bound,
18388 high bound or count are not yet handled by this code. */
78134374 18389 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18390 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18391
18392 return index_type;
18393}
18394
a02abb62
JB
18395/* Read the given DW_AT_subrange DIE. */
18396
f792889a 18397static struct type *
a02abb62
JB
18398read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18399{
4c9ad8c2 18400 struct type *base_type, *orig_base_type;
a02abb62
JB
18401 struct type *range_type;
18402 struct attribute *attr;
729efb13 18403 struct dynamic_prop low, high;
4fae6e18 18404 int low_default_is_valid;
c451ebe5 18405 int high_bound_is_count = 0;
15d034d0 18406 const char *name;
d359392f 18407 ULONGEST negative_mask;
e77813c8 18408
b86352cf
AB
18409 orig_base_type = read_subrange_index_type (die, cu);
18410
4c9ad8c2
TT
18411 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18412 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18413 creating the range type, but we use the result of check_typedef
18414 when examining properties of the type. */
18415 base_type = check_typedef (orig_base_type);
a02abb62 18416
7e314c57
JK
18417 /* The die_type call above may have already set the type for this DIE. */
18418 range_type = get_die_type (die, cu);
18419 if (range_type)
18420 return range_type;
18421
8c2e4e06 18422 high.set_const_val (0);
729efb13 18423
4fae6e18
JK
18424 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18425 omitting DW_AT_lower_bound. */
3e9f1ca1 18426 switch (cu->per_cu->lang)
6e70227d 18427 {
4fae6e18
JK
18428 case language_c:
18429 case language_cplus:
8c2e4e06 18430 low.set_const_val (0);
4fae6e18
JK
18431 low_default_is_valid = 1;
18432 break;
18433 case language_fortran:
8c2e4e06 18434 low.set_const_val (1);
4fae6e18
JK
18435 low_default_is_valid = 1;
18436 break;
18437 case language_d:
4fae6e18 18438 case language_objc:
c44af4eb 18439 case language_rust:
8c2e4e06 18440 low.set_const_val (0);
4fae6e18
JK
18441 low_default_is_valid = (cu->header.version >= 4);
18442 break;
18443 case language_ada:
18444 case language_m2:
18445 case language_pascal:
8c2e4e06 18446 low.set_const_val (1);
4fae6e18
JK
18447 low_default_is_valid = (cu->header.version >= 4);
18448 break;
18449 default:
8c2e4e06 18450 low.set_const_val (0);
4fae6e18
JK
18451 low_default_is_valid = 0;
18452 break;
a02abb62
JB
18453 }
18454
e142c38c 18455 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18456 if (attr != nullptr)
9a49df9d 18457 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18458 else if (!low_default_is_valid)
b98664d3 18459 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18460 "- DIE at %s [in module %s]"),
18461 sect_offset_str (die->sect_off),
5e22e966 18462 objfile_name (cu->per_objfile->objfile));
a02abb62 18463
506f5c41
TV
18464 struct attribute *attr_ub, *attr_count;
18465 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18466 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18467 {
506f5c41 18468 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18469 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18470 {
c451ebe5 18471 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18472 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18473 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18474 else
18475 high_bound_is_count = 1;
c2ff108b 18476 }
506f5c41
TV
18477 else
18478 {
18479 if (attr_ub != NULL)
18480 complaint (_("Unresolved DW_AT_upper_bound "
18481 "- DIE at %s [in module %s]"),
18482 sect_offset_str (die->sect_off),
5e22e966 18483 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18484 if (attr_count != NULL)
18485 complaint (_("Unresolved DW_AT_count "
18486 "- DIE at %s [in module %s]"),
18487 sect_offset_str (die->sect_off),
5e22e966 18488 objfile_name (cu->per_objfile->objfile));
506f5c41 18489 }
e77813c8 18490 }
a02abb62 18491
4e962e74
TT
18492 LONGEST bias = 0;
18493 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18494 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18495 bias = bias_attr->constant_value (0);
4e962e74 18496
dbb9c2b1
JB
18497 /* Normally, the DWARF producers are expected to use a signed
18498 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18499 But this is unfortunately not always the case, as witnessed
18500 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18501 is used instead. To work around that ambiguity, we treat
18502 the bounds as signed, and thus sign-extend their values, when
18503 the base type is signed. */
6e70227d 18504 negative_mask =
d359392f 18505 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18506 if (low.kind () == PROP_CONST
c6d940a9 18507 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18508 low.set_const_val (low.const_val () | negative_mask);
18509 if (high.kind () == PROP_CONST
c6d940a9 18510 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18511 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18512
5bbd8269
AB
18513 /* Check for bit and byte strides. */
18514 struct dynamic_prop byte_stride_prop;
18515 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18516 if (attr_byte_stride != nullptr)
18517 {
293e7e51 18518 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18519 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18520 prop_type);
18521 }
18522
18523 struct dynamic_prop bit_stride_prop;
18524 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18525 if (attr_bit_stride != nullptr)
18526 {
18527 /* It only makes sense to have either a bit or byte stride. */
18528 if (attr_byte_stride != nullptr)
18529 {
18530 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18531 "- DIE at %s [in module %s]"),
18532 sect_offset_str (die->sect_off),
5e22e966 18533 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
18534 attr_bit_stride = nullptr;
18535 }
18536 else
18537 {
293e7e51 18538 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18539 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18540 prop_type);
18541 }
18542 }
18543
18544 if (attr_byte_stride != nullptr
18545 || attr_bit_stride != nullptr)
18546 {
18547 bool byte_stride_p = (attr_byte_stride != nullptr);
18548 struct dynamic_prop *stride
18549 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18550
18551 range_type
18552 = create_range_type_with_stride (NULL, orig_base_type, &low,
18553 &high, bias, stride, byte_stride_p);
18554 }
18555 else
18556 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18557
c451ebe5 18558 if (high_bound_is_count)
599088e3 18559 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 18560
c2ff108b 18561 /* Ada expects an empty array on no boundary attributes. */
3e9f1ca1 18562 if (attr == NULL && cu->per_cu->lang != language_ada)
8c2e4e06 18563 range_type->bounds ()->high.set_undefined ();
c2ff108b 18564
39cbfefa
DJ
18565 name = dwarf2_name (die, cu);
18566 if (name)
d0e39ea2 18567 range_type->set_name (name);
6e70227d 18568
e142c38c 18569 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18570 if (attr != nullptr)
529908cb 18571 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 18572
2b4424c3
TT
18573 maybe_set_alignment (cu, die, range_type);
18574
7e314c57
JK
18575 set_die_type (die, range_type, cu);
18576
18577 /* set_die_type should be already done. */
b4ba55a1
JB
18578 set_descriptive_type (range_type, die, cu);
18579
7e314c57 18580 return range_type;
a02abb62 18581}
6e70227d 18582
f792889a 18583static struct type *
81a17f79
JB
18584read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18585{
18586 struct type *type;
81a17f79 18587
5e22e966 18588 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 18589 type->set_name (dwarf2_name (die, cu));
81a17f79 18590
74a2f8ff 18591 /* In Ada, an unspecified type is typically used when the description
85102364 18592 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18593 such a type, we treat it as a stub, and try to resolve it later on,
18594 when needed. */
3e9f1ca1 18595 if (cu->per_cu->lang == language_ada)
b4b73759 18596 type->set_is_stub (true);
74a2f8ff 18597
f792889a 18598 return set_die_type (die, type, cu);
81a17f79 18599}
a02abb62 18600
639d11d3
DC
18601/* Read a single die and all its descendents. Set the die's sibling
18602 field to NULL; set other fields in the die correctly, and set all
18603 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18604 location of the info_ptr after reading all of those dies. PARENT
18605 is the parent of the die in question. */
18606
18607static struct die_info *
dee91e82 18608read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18609 const gdb_byte *info_ptr,
18610 const gdb_byte **new_info_ptr,
dee91e82 18611 struct die_info *parent)
639d11d3
DC
18612{
18613 struct die_info *die;
d521ce57 18614 const gdb_byte *cur_ptr;
639d11d3 18615
3e225074 18616 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18617 if (die == NULL)
18618 {
18619 *new_info_ptr = cur_ptr;
18620 return NULL;
18621 }
93311388 18622 store_in_ref_table (die, reader->cu);
639d11d3 18623
3e225074 18624 if (die->has_children)
bf6af496 18625 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18626 else
18627 {
18628 die->child = NULL;
18629 *new_info_ptr = cur_ptr;
18630 }
18631
18632 die->sibling = NULL;
18633 die->parent = parent;
18634 return die;
18635}
18636
18637/* Read a die, all of its descendents, and all of its siblings; set
18638 all of the fields of all of the dies correctly. Arguments are as
18639 in read_die_and_children. */
18640
18641static struct die_info *
bf6af496 18642read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18643 const gdb_byte *info_ptr,
18644 const gdb_byte **new_info_ptr,
bf6af496 18645 struct die_info *parent)
639d11d3
DC
18646{
18647 struct die_info *first_die, *last_sibling;
d521ce57 18648 const gdb_byte *cur_ptr;
639d11d3 18649
c906108c 18650 cur_ptr = info_ptr;
639d11d3
DC
18651 first_die = last_sibling = NULL;
18652
18653 while (1)
c906108c 18654 {
639d11d3 18655 struct die_info *die
dee91e82 18656 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18657
1d325ec1 18658 if (die == NULL)
c906108c 18659 {
639d11d3
DC
18660 *new_info_ptr = cur_ptr;
18661 return first_die;
c906108c 18662 }
1d325ec1
DJ
18663
18664 if (!first_die)
18665 first_die = die;
c906108c 18666 else
1d325ec1
DJ
18667 last_sibling->sibling = die;
18668
18669 last_sibling = die;
c906108c 18670 }
c906108c
SS
18671}
18672
bf6af496
DE
18673/* Read a die, all of its descendents, and all of its siblings; set
18674 all of the fields of all of the dies correctly. Arguments are as
18675 in read_die_and_children.
18676 This the main entry point for reading a DIE and all its children. */
18677
18678static struct die_info *
18679read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18680 const gdb_byte *info_ptr,
18681 const gdb_byte **new_info_ptr,
bf6af496
DE
18682 struct die_info *parent)
18683{
18684 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18685 new_info_ptr, parent);
18686
b4f54984 18687 if (dwarf_die_debug)
bf6af496
DE
18688 {
18689 fprintf_unfiltered (gdb_stdlog,
18690 "Read die from %s@0x%x of %s:\n",
96b79293 18691 reader->die_section->get_name (),
bf6af496
DE
18692 (unsigned) (info_ptr - reader->die_section->buffer),
18693 bfd_get_filename (reader->abfd));
b4f54984 18694 dump_die (die, dwarf_die_debug);
bf6af496
DE
18695 }
18696
18697 return die;
18698}
18699
3019eac3
DE
18700/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18701 attributes.
18702 The caller is responsible for filling in the extra attributes
18703 and updating (*DIEP)->num_attrs.
18704 Set DIEP to point to a newly allocated die with its information,
3e225074 18705 except for its child, sibling, and parent fields. */
93311388 18706
d521ce57 18707static const gdb_byte *
3019eac3 18708read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18709 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18710 int num_extra_attrs)
93311388 18711{
b64f50a1 18712 unsigned int abbrev_number, bytes_read, i;
7c32eebb 18713 const struct abbrev_info *abbrev;
93311388
DE
18714 struct die_info *die;
18715 struct dwarf2_cu *cu = reader->cu;
18716 bfd *abfd = reader->abfd;
18717
9c541725 18718 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18719 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18720 info_ptr += bytes_read;
18721 if (!abbrev_number)
18722 {
18723 *diep = NULL;
93311388
DE
18724 return info_ptr;
18725 }
18726
685af9cd 18727 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18728 if (!abbrev)
348e048f
DE
18729 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18730 abbrev_number,
18731 bfd_get_filename (abfd));
18732
3019eac3 18733 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18734 die->sect_off = sect_off;
93311388
DE
18735 die->tag = abbrev->tag;
18736 die->abbrev = abbrev_number;
3e225074 18737 die->has_children = abbrev->has_children;
93311388 18738
3019eac3
DE
18739 /* Make the result usable.
18740 The caller needs to update num_attrs after adding the extra
18741 attributes. */
93311388
DE
18742 die->num_attrs = abbrev->num_attrs;
18743
7a5f294d 18744 bool any_need_reprocess = false;
93311388 18745 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 18746 {
7a5f294d
TT
18747 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18748 info_ptr);
18749 if (die->attrs[i].requires_reprocessing_p ())
18750 any_need_reprocess = true;
18a8505e
AT
18751 }
18752
052c8bb8 18753 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
18754 if (attr != nullptr && attr->form_is_unsigned ())
18755 cu->str_offsets_base = attr->as_unsigned ();
93311388 18756
41144253 18757 attr = die->attr (DW_AT_loclists_base);
18758 if (attr != nullptr)
529908cb 18759 cu->loclist_base = attr->as_unsigned ();
41144253 18760
a39fdb41 18761 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18762 if (maybe_addr_base.has_value ())
18763 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
18764
18765 attr = die->attr (DW_AT_rnglists_base);
18766 if (attr != nullptr)
2b0c7f41 18767 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 18768
7a5f294d
TT
18769 if (any_need_reprocess)
18770 {
18771 for (i = 0; i < abbrev->num_attrs; ++i)
18772 {
18773 if (die->attrs[i].requires_reprocessing_p ())
18774 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18775 }
18776 }
93311388 18777 *diep = die;
93311388
DE
18778 return info_ptr;
18779}
18780
3019eac3
DE
18781/* Read a die and all its attributes.
18782 Set DIEP to point to a newly allocated die with its information,
3e225074 18783 except for its child, sibling, and parent fields. */
3019eac3 18784
d521ce57 18785static const gdb_byte *
3019eac3 18786read_full_die (const struct die_reader_specs *reader,
3e225074 18787 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18788{
d521ce57 18789 const gdb_byte *result;
bf6af496 18790
3e225074 18791 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18792
b4f54984 18793 if (dwarf_die_debug)
bf6af496
DE
18794 {
18795 fprintf_unfiltered (gdb_stdlog,
18796 "Read die from %s@0x%x of %s:\n",
96b79293 18797 reader->die_section->get_name (),
bf6af496
DE
18798 (unsigned) (info_ptr - reader->die_section->buffer),
18799 bfd_get_filename (reader->abfd));
b4f54984 18800 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18801 }
18802
18803 return result;
3019eac3 18804}
433df2d4 18805\f
c906108c 18806
72bf9492
DJ
18807/* Returns nonzero if TAG represents a type that we might generate a partial
18808 symbol for. */
18809
18810static int
d8f62e84 18811is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
18812{
18813 switch (tag)
18814 {
18815#if 0
18816 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
18817 that we don't at present. Note that normally this does not
18818 matter, mainly because C compilers don't give names to these
18819 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
18820 case DW_TAG_file_type:
18821 case DW_TAG_ptr_to_member_type:
18822 case DW_TAG_set_type:
18823 case DW_TAG_string_type:
18824 case DW_TAG_subroutine_type:
18825#endif
d8f62e84
TT
18826
18827 /* GNAT may emit an array with a name, but no typedef, so we
18828 need to make a symbol in this case. */
18829 case DW_TAG_array_type:
18830 return lang == language_ada;
18831
72bf9492
DJ
18832 case DW_TAG_base_type:
18833 case DW_TAG_class_type:
680b30c7 18834 case DW_TAG_interface_type:
72bf9492
DJ
18835 case DW_TAG_enumeration_type:
18836 case DW_TAG_structure_type:
18837 case DW_TAG_subrange_type:
18838 case DW_TAG_typedef:
18839 case DW_TAG_union_type:
18840 return 1;
18841 default:
18842 return 0;
18843 }
18844}
18845
18846/* Load all DIEs that are interesting for partial symbols into memory. */
18847
18848static struct partial_die_info *
dee91e82 18849load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18850 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18851{
dee91e82 18852 struct dwarf2_cu *cu = reader->cu;
5e22e966 18853 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18854 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18855 unsigned int bytes_read;
5afb4e99 18856 unsigned int load_all = 0;
72bf9492
DJ
18857 int nesting_level = 1;
18858
18859 parent_die = NULL;
18860 last_die = NULL;
18861
7adf1e79 18862 gdb_assert (cu->per_cu != NULL);
6dcd1193 18863 if (cu->load_all_dies)
5afb4e99
DJ
18864 load_all = 1;
18865
72bf9492
DJ
18866 cu->partial_dies
18867 = htab_create_alloc_ex (cu->header.length / 12,
18868 partial_die_hash,
18869 partial_die_eq,
18870 NULL,
18871 &cu->comp_unit_obstack,
18872 hashtab_obstack_allocate,
18873 dummy_obstack_deallocate);
18874
72bf9492
DJ
18875 while (1)
18876 {
7c32eebb
TT
18877 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18878 &bytes_read);
72bf9492
DJ
18879
18880 /* A NULL abbrev means the end of a series of children. */
18881 if (abbrev == NULL)
18882 {
18883 if (--nesting_level == 0)
cd9983dd
YQ
18884 return first_die;
18885
72bf9492
DJ
18886 info_ptr += bytes_read;
18887 last_die = parent_die;
18888 parent_die = parent_die->die_parent;
18889 continue;
18890 }
18891
98bfdba5
PA
18892 /* Check for template arguments. We never save these; if
18893 they're seen, we just mark the parent, and go on our way. */
18894 if (parent_die != NULL
3e9f1ca1 18895 && cu->per_cu->lang == language_cplus
98bfdba5
PA
18896 && (abbrev->tag == DW_TAG_template_type_param
18897 || abbrev->tag == DW_TAG_template_value_param))
18898 {
18899 parent_die->has_template_arguments = 1;
18900
18901 if (!load_all)
18902 {
18903 /* We don't need a partial DIE for the template argument. */
dee91e82 18904 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18905 continue;
18906 }
18907 }
18908
0d99eb77 18909 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18910 Skip their other children. */
18911 if (!load_all
3e9f1ca1 18912 && cu->per_cu->lang == language_cplus
98bfdba5 18913 && parent_die != NULL
f9b5d5ea
TV
18914 && parent_die->tag == DW_TAG_subprogram
18915 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 18916 {
dee91e82 18917 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18918 continue;
18919 }
18920
5afb4e99
DJ
18921 /* Check whether this DIE is interesting enough to save. Normally
18922 we would not be interested in members here, but there may be
18923 later variables referencing them via DW_AT_specification (for
18924 static members). */
18925 if (!load_all
3e9f1ca1 18926 && !is_type_tag_for_partial (abbrev->tag, cu->per_cu->lang)
72929c62 18927 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18928 && abbrev->tag != DW_TAG_enumerator
18929 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18930 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18931 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18932 && abbrev->tag != DW_TAG_variable
5afb4e99 18933 && abbrev->tag != DW_TAG_namespace
f55ee35c 18934 && abbrev->tag != DW_TAG_module
95554aad 18935 && abbrev->tag != DW_TAG_member
74921315
KS
18936 && abbrev->tag != DW_TAG_imported_unit
18937 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18938 {
18939 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18940 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18941 continue;
18942 }
18943
6f06d47b
YQ
18944 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18945 abbrev);
cd9983dd 18946
48fbe735 18947 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18948
18949 /* This two-pass algorithm for processing partial symbols has a
18950 high cost in cache pressure. Thus, handle some simple cases
18951 here which cover the majority of C partial symbols. DIEs
18952 which neither have specification tags in them, nor could have
18953 specification tags elsewhere pointing at them, can simply be
18954 processed and discarded.
18955
18956 This segment is also optional; scan_partial_symbols and
18957 add_partial_symbol will handle these DIEs if we chain
18958 them in normally. When compilers which do not emit large
18959 quantities of duplicate debug information are more common,
18960 this code can probably be removed. */
18961
18962 /* Any complete simple types at the top level (pretty much all
18963 of them, for a language without namespaces), can be processed
18964 directly. */
18965 if (parent_die == NULL
cd9983dd
YQ
18966 && pdi.has_specification == 0
18967 && pdi.is_declaration == 0
18968 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18969 || pdi.tag == DW_TAG_base_type
d8f62e84 18970 || pdi.tag == DW_TAG_array_type
cd9983dd 18971 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18972 {
7d00ffec 18973 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
18974 add_partial_symbol (&pdi, cu);
18975
cd9983dd 18976 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18977 continue;
18978 }
18979
d8228535
JK
18980 /* The exception for DW_TAG_typedef with has_children above is
18981 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18982 type_name_or_error will error on such types later.
d8228535
JK
18983
18984 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18985 it could not find the child DIEs referenced later, this is checked
18986 above. In correct DWARF DW_TAG_typedef should have no children. */
18987
cd9983dd 18988 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18989 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18990 "- DIE at %s [in module %s]"),
cd9983dd 18991 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18992
72bf9492
DJ
18993 /* If we're at the second level, and we're an enumerator, and
18994 our parent has no specification (meaning possibly lives in a
18995 namespace elsewhere), then we can add the partial symbol now
18996 instead of queueing it. */
cd9983dd 18997 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18998 && parent_die != NULL
18999 && parent_die->die_parent == NULL
19000 && parent_die->tag == DW_TAG_enumeration_type
19001 && parent_die->has_specification == 0)
19002 {
7d00ffec 19003 if (pdi.raw_name == NULL)
b98664d3 19004 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19005 else if (building_psymtab)
f0fbb768 19006 add_partial_symbol (&pdi, cu);
72bf9492 19007
cd9983dd 19008 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19009 continue;
19010 }
19011
cd9983dd 19012 struct partial_die_info *part_die
6f06d47b 19013 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19014
72bf9492
DJ
19015 /* We'll save this DIE so link it in. */
19016 part_die->die_parent = parent_die;
19017 part_die->die_sibling = NULL;
19018 part_die->die_child = NULL;
19019
19020 if (last_die && last_die == parent_die)
19021 last_die->die_child = part_die;
19022 else if (last_die)
19023 last_die->die_sibling = part_die;
19024
19025 last_die = part_die;
19026
19027 if (first_die == NULL)
19028 first_die = part_die;
19029
19030 /* Maybe add the DIE to the hash table. Not all DIEs that we
19031 find interesting need to be in the hash table, because we
19032 also have the parent/sibling/child chains; only those that we
19033 might refer to by offset later during partial symbol reading.
19034
19035 For now this means things that might have be the target of a
19036 DW_AT_specification, DW_AT_abstract_origin, or
19037 DW_AT_extension. DW_AT_extension will refer only to
19038 namespaces; DW_AT_abstract_origin refers to functions (and
19039 many things under the function DIE, but we do not recurse
19040 into function DIEs during partial symbol reading) and
19041 possibly variables as well; DW_AT_specification refers to
19042 declarations. Declarations ought to have the DW_AT_declaration
19043 flag. It happens that GCC forgets to put it in sometimes, but
19044 only for functions, not for types.
19045
19046 Adding more things than necessary to the hash table is harmless
19047 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19048 wasted time in find_partial_die, when we reread the compilation
19049 unit with load_all_dies set. */
72bf9492 19050
5afb4e99 19051 if (load_all
72929c62 19052 || abbrev->tag == DW_TAG_constant
5afb4e99 19053 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19054 || abbrev->tag == DW_TAG_variable
19055 || abbrev->tag == DW_TAG_namespace
19056 || part_die->is_declaration)
19057 {
19058 void **slot;
19059
19060 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19061 to_underlying (part_die->sect_off),
19062 INSERT);
72bf9492
DJ
19063 *slot = part_die;
19064 }
19065
72bf9492 19066 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19067 we have no reason to follow the children of structures; for other
98bfdba5
PA
19068 languages we have to, so that we can get at method physnames
19069 to infer fully qualified class names, for DW_AT_specification,
19070 and for C++ template arguments. For C++, we also look one level
19071 inside functions to find template arguments (if the name of the
19072 function does not already contain the template arguments).
bc30ff58 19073
0a4b0913
AB
19074 For Ada and Fortran, we need to scan the children of subprograms
19075 and lexical blocks as well because these languages allow the
19076 definition of nested entities that could be interesting for the
19077 debugger, such as nested subprograms for instance. */
72bf9492 19078 if (last_die->has_children
5afb4e99
DJ
19079 && (load_all
19080 || last_die->tag == DW_TAG_namespace
f55ee35c 19081 || last_die->tag == DW_TAG_module
72bf9492 19082 || last_die->tag == DW_TAG_enumeration_type
3e9f1ca1 19083 || (cu->per_cu->lang == language_cplus
98bfdba5 19084 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19085 && (last_die->raw_name == NULL
19086 || strchr (last_die->raw_name, '<') == NULL))
3e9f1ca1 19087 || (cu->per_cu->lang != language_c
72bf9492 19088 && (last_die->tag == DW_TAG_class_type
680b30c7 19089 || last_die->tag == DW_TAG_interface_type
72bf9492 19090 || last_die->tag == DW_TAG_structure_type
bc30ff58 19091 || last_die->tag == DW_TAG_union_type))
3e9f1ca1
TT
19092 || ((cu->per_cu->lang == language_ada
19093 || cu->per_cu->lang == language_fortran)
bc30ff58
JB
19094 && (last_die->tag == DW_TAG_subprogram
19095 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19096 {
19097 nesting_level++;
19098 parent_die = last_die;
19099 continue;
19100 }
19101
19102 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19103 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19104
19105 /* Back to the top, do it again. */
19106 }
19107}
19108
6f06d47b 19109partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19110 const struct abbrev_info *abbrev)
6f06d47b
YQ
19111 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19112{
19113}
19114
7d00ffec
TT
19115/* See class definition. */
19116
19117const char *
19118partial_die_info::name (dwarf2_cu *cu)
19119{
19120 if (!canonical_name && raw_name != nullptr)
19121 {
19122 struct objfile *objfile = cu->per_objfile->objfile;
19123 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19124 canonical_name = 1;
19125 }
19126
19127 return raw_name;
19128}
19129
35cc7ed7
YQ
19130/* Read a minimal amount of information into the minimal die structure.
19131 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19132
48fbe735
YQ
19133const gdb_byte *
19134partial_die_info::read (const struct die_reader_specs *reader,
19135 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19136{
dee91e82 19137 struct dwarf2_cu *cu = reader->cu;
976ca316 19138 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19139 unsigned int i;
c5aa993b 19140 int has_low_pc_attr = 0;
c906108c 19141 int has_high_pc_attr = 0;
91da1414 19142 int high_pc_relative = 0;
c906108c 19143
fd0a254f 19144 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19145 {
e7da7f8f 19146 attribute attr;
7a5f294d 19147 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19148 /* String and address offsets that need to do the reprocessing have
dda83cd7 19149 already been read at this point, so there is no need to wait until
18a8505e 19150 the loop terminates to do the reprocessing. */
7a5f294d 19151 if (attr.requires_reprocessing_p ())
d0ce17d8 19152 read_attribute_reprocess (reader, &attr, tag);
c906108c 19153 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19154 partial symbol table. */
c906108c
SS
19155 switch (attr.name)
19156 {
19157 case DW_AT_name:
48fbe735 19158 switch (tag)
71c25dea
TT
19159 {
19160 case DW_TAG_compile_unit:
95554aad 19161 case DW_TAG_partial_unit:
348e048f 19162 case DW_TAG_type_unit:
71c25dea
TT
19163 /* Compilation units have a DW_AT_name that is a filename, not
19164 a source language identifier. */
19165 case DW_TAG_enumeration_type:
19166 case DW_TAG_enumerator:
19167 /* These tags always have simple identifiers already; no need
19168 to canonicalize them. */
7d00ffec 19169 canonical_name = 1;
2c830f54 19170 raw_name = attr.as_string ();
71c25dea
TT
19171 break;
19172 default:
7d00ffec 19173 canonical_name = 0;
2c830f54 19174 raw_name = attr.as_string ();
71c25dea
TT
19175 break;
19176 }
c906108c 19177 break;
31ef98ae 19178 case DW_AT_linkage_name:
c906108c 19179 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19180 /* Note that both forms of linkage name might appear. We
19181 assume they will be the same, and we only store the last
19182 one we see. */
95f982e5 19183 linkage_name = attr.as_string ();
c906108c
SS
19184 break;
19185 case DW_AT_low_pc:
19186 has_low_pc_attr = 1;
95f982e5 19187 lowpc = attr.as_address ();
c906108c
SS
19188 break;
19189 case DW_AT_high_pc:
19190 has_high_pc_attr = 1;
95f982e5 19191 highpc = attr.as_address ();
cd6c91b4 19192 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19193 high_pc_relative = 1;
c906108c
SS
19194 break;
19195 case DW_AT_location:
dda83cd7
SM
19196 /* Support the .debug_loc offsets. */
19197 if (attr.form_is_block ())
19198 {
9d2246fc 19199 d.locdesc = attr.as_block ();
dda83cd7
SM
19200 }
19201 else if (attr.form_is_section_offset ())
19202 {
4d3c2250 19203 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19204 }
19205 else
19206 {
4d3c2250
KB
19207 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19208 "partial symbol information");
dda83cd7 19209 }
c906108c 19210 break;
c906108c 19211 case DW_AT_external:
c45bc3f8 19212 is_external = attr.as_boolean ();
c906108c
SS
19213 break;
19214 case DW_AT_declaration:
c45bc3f8 19215 is_declaration = attr.as_boolean ();
c906108c
SS
19216 break;
19217 case DW_AT_type:
48fbe735 19218 has_type = 1;
c906108c
SS
19219 break;
19220 case DW_AT_abstract_origin:
19221 case DW_AT_specification:
72bf9492 19222 case DW_AT_extension:
48fbe735 19223 has_specification = 1;
0826b30a 19224 spec_offset = attr.get_ref_die_offset ();
48fbe735 19225 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19226 || cu->per_cu->is_dwz);
c906108c
SS
19227 break;
19228 case DW_AT_sibling:
19229 /* Ignore absolute siblings, they might point outside of
19230 the current compile unit. */
19231 if (attr.form == DW_FORM_ref_addr)
b98664d3 19232 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19233 else
b9502d3f 19234 {
48fbe735 19235 const gdb_byte *buffer = reader->buffer;
0826b30a 19236 sect_offset off = attr.get_ref_die_offset ();
9c541725 19237 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19238
19239 if (sibling_ptr < info_ptr)
b98664d3 19240 complaint (_("DW_AT_sibling points backwards"));
22869d73 19241 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19242 reader->die_section->overflow_complaint ();
b9502d3f 19243 else
48fbe735 19244 sibling = sibling_ptr;
b9502d3f 19245 }
c906108c 19246 break;
dda83cd7
SM
19247 case DW_AT_byte_size:
19248 has_byte_size = 1;
19249 break;
19250 case DW_AT_const_value:
19251 has_const_value = 1;
19252 break;
68511cec
CES
19253 case DW_AT_calling_convention:
19254 /* DWARF doesn't provide a way to identify a program's source-level
19255 entry point. DW_AT_calling_convention attributes are only meant
19256 to describe functions' calling conventions.
19257
19258 However, because it's a necessary piece of information in
0c1b455e
TT
19259 Fortran, and before DWARF 4 DW_CC_program was the only
19260 piece of debugging information whose definition refers to
19261 a 'main program' at all, several compilers marked Fortran
19262 main programs with DW_CC_program --- even when those
19263 functions use the standard calling conventions.
19264
19265 Although DWARF now specifies a way to provide this
19266 information, we support this practice for backward
19267 compatibility. */
529908cb 19268 if (attr.constant_value (0) == DW_CC_program
3e9f1ca1 19269 && cu->per_cu->lang == language_fortran)
48fbe735 19270 main_subprogram = 1;
68511cec 19271 break;
481860b3 19272 case DW_AT_inline:
529908cb
TT
19273 {
19274 LONGEST value = attr.constant_value (-1);
19275 if (value == DW_INL_inlined
19276 || value == DW_INL_declared_inlined)
19277 may_be_inlined = 1;
19278 }
481860b3 19279 break;
95554aad
TT
19280
19281 case DW_AT_import:
48fbe735 19282 if (tag == DW_TAG_imported_unit)
36586728 19283 {
0826b30a 19284 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19285 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19286 || cu->per_cu->is_dwz);
19287 }
95554aad
TT
19288 break;
19289
0c1b455e 19290 case DW_AT_main_subprogram:
c45bc3f8 19291 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19292 break;
19293
05caa1d2
TT
19294 case DW_AT_ranges:
19295 {
2b0c7f41
SM
19296 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19297 on DWARF version). */
19298 ULONGEST ranges_offset = attr.as_unsigned ();
19299
19300 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19301 this value. */
19302 if (tag != DW_TAG_compile_unit)
19303 ranges_offset += cu->gnu_ranges_base;
19304
05caa1d2 19305 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19306 nullptr, tag))
05caa1d2
TT
19307 has_pc_info = 1;
19308 }
19309 break;
19310
c906108c
SS
19311 default:
19312 break;
19313 }
19314 }
19315
10d06d82
TT
19316 /* For Ada, if both the name and the linkage name appear, we prefer
19317 the latter. This lets "catch exception" work better, regardless
19318 of the order in which the name and linkage name were emitted.
19319 Really, though, this is just a workaround for the fact that gdb
19320 doesn't store both the name and the linkage name. */
3e9f1ca1 19321 if (cu->per_cu->lang == language_ada && linkage_name != nullptr)
7d00ffec 19322 raw_name = linkage_name;
10d06d82 19323
91da1414 19324 if (high_pc_relative)
48fbe735 19325 highpc += lowpc;
91da1414 19326
9373cf26
JK
19327 if (has_low_pc_attr && has_high_pc_attr)
19328 {
19329 /* When using the GNU linker, .gnu.linkonce. sections are used to
19330 eliminate duplicate copies of functions and vtables and such.
19331 The linker will arbitrarily choose one and discard the others.
19332 The AT_*_pc values for such functions refer to local labels in
19333 these sections. If the section from that file was discarded, the
19334 labels are not in the output, so the relocs get a value of 0.
19335 If this is a discarded function, mark the pc bounds as invalid,
19336 so that GDB will ignore it. */
976ca316 19337 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19338 {
976ca316 19339 struct objfile *objfile = per_objfile->objfile;
08feed99 19340 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19341
b98664d3 19342 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19343 "for DIE at %s [in module %s]"),
48fbe735
YQ
19344 paddress (gdbarch, lowpc),
19345 sect_offset_str (sect_off),
9d8780f0 19346 objfile_name (objfile));
9373cf26
JK
19347 }
19348 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19349 else if (lowpc >= highpc)
9373cf26 19350 {
976ca316 19351 struct objfile *objfile = per_objfile->objfile;
08feed99 19352 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19353
b98664d3 19354 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19355 "for DIE at %s [in module %s]"),
48fbe735
YQ
19356 paddress (gdbarch, lowpc),
19357 paddress (gdbarch, highpc),
19358 sect_offset_str (sect_off),
9c541725 19359 objfile_name (objfile));
9373cf26
JK
19360 }
19361 else
48fbe735 19362 has_pc_info = 1;
9373cf26 19363 }
85cbf3d3 19364
c906108c
SS
19365 return info_ptr;
19366}
19367
72bf9492
DJ
19368/* Find a cached partial DIE at OFFSET in CU. */
19369
d590ff25
YQ
19370struct partial_die_info *
19371dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19372{
19373 struct partial_die_info *lookup_die = NULL;
6f06d47b 19374 struct partial_die_info part_die (sect_off);
72bf9492 19375
9a3c8263 19376 lookup_die = ((struct partial_die_info *)
d590ff25 19377 htab_find_with_hash (partial_dies, &part_die,
9c541725 19378 to_underlying (sect_off)));
72bf9492 19379
72bf9492
DJ
19380 return lookup_die;
19381}
19382
348e048f
DE
19383/* Find a partial DIE at OFFSET, which may or may not be in CU,
19384 except in the case of .debug_types DIEs which do not reference
19385 outside their CU (they do however referencing other types via
55f1336d 19386 DW_FORM_ref_sig8). */
72bf9492 19387
122cf0f2 19388static const struct cu_partial_die_info
9c541725 19389find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19390{
976ca316
SM
19391 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19392 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19393 struct partial_die_info *pd = NULL;
72bf9492 19394
36586728 19395 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19396 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19397 {
d590ff25 19398 pd = cu->find_partial_die (sect_off);
5afb4e99 19399 if (pd != NULL)
fb816e8b 19400 return { cu, pd };
0d99eb77
DE
19401 /* We missed recording what we needed.
19402 Load all dies and try again. */
5afb4e99 19403 }
0d99eb77
DE
19404 else
19405 {
19406 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19407 if (cu->per_cu->is_debug_types)
0d99eb77 19408 {
9d8780f0
SM
19409 error (_("Dwarf Error: Type Unit at offset %s contains"
19410 " external reference to offset %s [in module %s].\n"),
19411 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19412 bfd_get_filename (objfile->obfd));
19413 }
7188ed02
SM
19414 dwarf2_per_cu_data *per_cu
19415 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19416 per_objfile);
72bf9492 19417
976ca316 19418 cu = per_objfile->get_cu (per_cu);
7188ed02 19419 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19420 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19421
976ca316 19422 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19423
19424 cu->last_used = 0;
19425 pd = cu->find_partial_die (sect_off);
0d99eb77 19426 }
5afb4e99 19427
dee91e82
DE
19428 /* If we didn't find it, and not all dies have been loaded,
19429 load them all and try again. */
19430
6dcd1193 19431 if (pd == NULL && cu->load_all_dies == 0)
5afb4e99 19432 {
6dcd1193 19433 cu->load_all_dies = 1;
fd820528
DE
19434
19435 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19436 THIS_CU->cu may already be in use. So we can't just free it and
19437 replace its DIEs with the ones we read in. Instead, we leave those
19438 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19439 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19440 set. */
976ca316 19441 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19442
7188ed02 19443 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19444 }
19445
19446 if (pd == NULL)
2152b4fd 19447 error (_("Dwarf Error: Cannot find DIE at %s [from module %s]\n"),
9d8780f0 19448 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19449 return { cu, pd };
72bf9492
DJ
19450}
19451
abc72ce4
DE
19452/* See if we can figure out if the class lives in a namespace. We do
19453 this by looking for a member function; its demangled name will
19454 contain namespace info, if there is any. */
19455
19456static void
19457guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19458 struct dwarf2_cu *cu)
19459{
19460 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19461 what template types look like, because the demangler
19462 frequently doesn't give the same name as the debug info. We
19463 could fix this by only using the demangled name to get the
19464 prefix (but see comment in read_structure_type). */
19465
19466 struct partial_die_info *real_pdi;
19467 struct partial_die_info *child_pdi;
19468
19469 /* If this DIE (this DIE's specification, if any) has a parent, then
19470 we should not do this. We'll prepend the parent's fully qualified
19471 name when we create the partial symbol. */
19472
19473 real_pdi = struct_pdi;
19474 while (real_pdi->has_specification)
fb816e8b 19475 {
122cf0f2
AB
19476 auto res = find_partial_die (real_pdi->spec_offset,
19477 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19478 real_pdi = res.pdi;
19479 cu = res.cu;
19480 }
abc72ce4
DE
19481
19482 if (real_pdi->die_parent != NULL)
19483 return;
19484
19485 for (child_pdi = struct_pdi->die_child;
19486 child_pdi != NULL;
19487 child_pdi = child_pdi->die_sibling)
19488 {
19489 if (child_pdi->tag == DW_TAG_subprogram
19490 && child_pdi->linkage_name != NULL)
19491 {
43816ebc 19492 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19493 (cu->language_defn->class_name_from_physname
19494 (child_pdi->linkage_name));
abc72ce4
DE
19495 if (actual_class_name != NULL)
19496 {
5e22e966 19497 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19498 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19499 struct_pdi->canonical_name = 1;
abc72ce4
DE
19500 }
19501 break;
19502 }
19503 }
19504}
19505
25c11aca
TV
19506/* Return true if a DIE with TAG may have the DW_AT_const_value
19507 attribute. */
19508
19509static bool
19510can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19511{
19512 switch (tag)
19513 {
19514 case DW_TAG_constant:
19515 case DW_TAG_enumerator:
19516 case DW_TAG_formal_parameter:
19517 case DW_TAG_template_value_param:
19518 case DW_TAG_variable:
19519 return true;
19520 }
19521
19522 return false;
19523}
19524
52356b79
YQ
19525void
19526partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19527{
abc72ce4
DE
19528 /* Once we've fixed up a die, there's no point in doing so again.
19529 This also avoids a memory leak if we were to call
19530 guess_partial_die_structure_name multiple times. */
52356b79 19531 if (fixup_called)
abc72ce4
DE
19532 return;
19533
72bf9492
DJ
19534 /* If we found a reference attribute and the DIE has no name, try
19535 to find a name in the referred to DIE. */
19536
7d00ffec 19537 if (raw_name == NULL && has_specification)
72bf9492
DJ
19538 {
19539 struct partial_die_info *spec_die;
72bf9492 19540
122cf0f2 19541 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19542 spec_die = res.pdi;
19543 cu = res.cu;
72bf9492 19544
52356b79 19545 spec_die->fixup (cu);
72bf9492 19546
7d00ffec 19547 if (spec_die->raw_name)
72bf9492 19548 {
7d00ffec
TT
19549 raw_name = spec_die->raw_name;
19550 canonical_name = spec_die->canonical_name;
72bf9492
DJ
19551
19552 /* Copy DW_AT_external attribute if it is set. */
19553 if (spec_die->is_external)
52356b79 19554 is_external = spec_die->is_external;
72bf9492
DJ
19555 }
19556 }
19557
25c11aca
TV
19558 if (!has_const_value && has_specification
19559 && can_have_DW_AT_const_value_p (tag))
19560 {
19561 struct partial_die_info *spec_die;
19562
19563 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19564 spec_die = res.pdi;
19565 cu = res.cu;
19566
19567 spec_die->fixup (cu);
19568
19569 if (spec_die->has_const_value)
19570 {
19571 /* Copy DW_AT_const_value attribute if it is set. */
19572 has_const_value = spec_die->has_const_value;
19573 }
19574 }
19575
72bf9492 19576 /* Set default names for some unnamed DIEs. */
72bf9492 19577
7d00ffec
TT
19578 if (raw_name == NULL && tag == DW_TAG_namespace)
19579 {
19580 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19581 canonical_name = 1;
19582 }
72bf9492 19583
abc72ce4
DE
19584 /* If there is no parent die to provide a namespace, and there are
19585 children, see if we can determine the namespace from their linkage
122d1940 19586 name. */
3e9f1ca1 19587 if (cu->per_cu->lang == language_cplus
5e22e966 19588 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
19589 && die_parent == NULL
19590 && has_children
19591 && (tag == DW_TAG_class_type
19592 || tag == DW_TAG_structure_type
19593 || tag == DW_TAG_union_type))
19594 guess_partial_die_structure_name (this, cu);
abc72ce4 19595
53832f31
TT
19596 /* GCC might emit a nameless struct or union that has a linkage
19597 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 19598 if (raw_name == NULL
52356b79
YQ
19599 && (tag == DW_TAG_class_type
19600 || tag == DW_TAG_interface_type
19601 || tag == DW_TAG_structure_type
19602 || tag == DW_TAG_union_type)
19603 && linkage_name != NULL)
53832f31 19604 {
43816ebc
TT
19605 gdb::unique_xmalloc_ptr<char> demangled
19606 (gdb_demangle (linkage_name, DMGL_TYPES));
19607 if (demangled != nullptr)
53832f31 19608 {
96408a79
SA
19609 const char *base;
19610
19611 /* Strip any leading namespaces/classes, keep only the base name.
19612 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19613 base = strrchr (demangled.get (), ':');
19614 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19615 base++;
19616 else
43816ebc 19617 base = demangled.get ();
96408a79 19618
5e22e966 19619 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19620 raw_name = objfile->intern (base);
19621 canonical_name = 1;
53832f31
TT
19622 }
19623 }
19624
52356b79 19625 fixup_called = 1;
72bf9492
DJ
19626}
19627
d0ce17d8 19628/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
19629 contents from the given SECTION in the HEADER.
19630
19631 HEADER_OFFSET is the offset of the header in the section. */
41144253 19632static void
d0ce17d8 19633read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
19634 struct dwarf2_section_info *section,
19635 sect_offset header_offset)
41144253 19636{
19637 unsigned int bytes_read;
19638 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
19639 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19640
41144253 19641 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19642 info_ptr += bytes_read;
a1c40103 19643
41144253 19644 header->version = read_2_bytes (abfd, info_ptr);
19645 info_ptr += 2;
a1c40103 19646
41144253 19647 header->addr_size = read_1_byte (abfd, info_ptr);
19648 info_ptr += 1;
a1c40103 19649
41144253 19650 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19651 info_ptr += 1;
a1c40103 19652
41144253 19653 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19654}
19655
19656/* Return the DW_AT_loclists_base value for the CU. */
19657static ULONGEST
19658lookup_loclist_base (struct dwarf2_cu *cu)
19659{
19660 /* For the .dwo unit, the loclist_base points to the first offset following
19661 the header. The header consists of the following entities-
19662 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19663 bit format)
19664 2. version (2 bytes)
19665 3. address size (1 byte)
19666 4. segment selector size (1 byte)
19667 5. offset entry count (4 bytes)
19668 These sizes are derived as per the DWARFv5 standard. */
19669 if (cu->dwo_unit != nullptr)
19670 {
19671 if (cu->header.initial_length_size == 4)
19672 return LOCLIST_HEADER_SIZE32;
19673 return LOCLIST_HEADER_SIZE64;
19674 }
19675 return cu->loclist_base;
19676}
19677
19678/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19679 array of offsets in the .debug_loclists section. */
e57933dc
SM
19680
19681static sect_offset
41144253 19682read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19683{
976ca316
SM
19684 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19685 struct objfile *objfile = per_objfile->objfile;
41144253 19686 bfd *abfd = objfile->obfd;
a1c40103
SM
19687 ULONGEST loclist_header_size =
19688 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19689 : LOCLIST_HEADER_SIZE64);
41144253 19690 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
19691
19692 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19693 ULONGEST start_offset =
19694 loclist_base + loclist_index * cu->header.offset_size;
19695
a1c40103 19696 /* Get loclists section. */
41144253 19697 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19698
a1c40103 19699 /* Read the loclists section content. */
41144253 19700 section->read (objfile);
19701 if (section->buffer == NULL)
a0c1eeba
SM
19702 error (_("DW_FORM_loclistx used without .debug_loclists "
19703 "section [in module %s]"), objfile_name (objfile));
19704
a1c40103
SM
19705 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19706 so if loclist_base is smaller than the header size, we have a problem. */
19707 if (loclist_base < loclist_header_size)
19708 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19709 objfile_name (objfile));
19710
19711 /* Read the header of the loclists contribution. */
d0ce17d8 19712 struct loclists_rnglists_header header;
a1c40103
SM
19713 read_loclists_rnglists_header (&header, section,
19714 (sect_offset) (loclist_base - loclist_header_size));
19715
19716 /* Verify the loclist index is valid. */
41144253 19717 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
19718 error (_("DW_FORM_loclistx pointing outside of "
19719 ".debug_loclists offset array [in module %s]"),
19720 objfile_name (objfile));
19721
05787bad
SM
19722 /* Validate that reading won't go beyond the end of the section. */
19723 if (start_offset + cu->header.offset_size > section->size)
19724 error (_("Reading DW_FORM_loclistx index beyond end of"
19725 ".debug_loclists section [in module %s]"),
19726 objfile_name (objfile));
19727
19728 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 19729
19730 if (cu->header.offset_size == 4)
e57933dc 19731 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 19732 else
e57933dc 19733 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 19734}
19735
d0ce17d8
CT
19736/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19737 array of offsets in the .debug_rnglists section. */
e57933dc
SM
19738
19739static sect_offset
d0ce17d8
CT
19740read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19741 dwarf_tag tag)
19742{
19743 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19744 struct objfile *objfile = dwarf2_per_objfile->objfile;
19745 bfd *abfd = objfile->obfd;
19746 ULONGEST rnglist_header_size =
19747 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19748 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
19749
19750 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19751 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19752 doesn't apply. */
d0ce17d8 19753 ULONGEST rnglist_base =
2b0c7f41 19754 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
19755
19756 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
19757 ULONGEST start_offset =
19758 rnglist_base + rnglist_index * cu->header.offset_size;
19759
19760 /* Get rnglists section. */
19761 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19762
19763 /* Read the rnglists section content. */
19764 section->read (objfile);
19765 if (section->buffer == nullptr)
19766 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19767 "[in module %s]"),
19768 objfile_name (objfile));
19769
a1c40103
SM
19770 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19771 so if rnglist_base is smaller than the header size, we have a problem. */
19772 if (rnglist_base < rnglist_header_size)
19773 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19774 objfile_name (objfile));
19775
19776 /* Read the header of the rnglists contribution. */
d0ce17d8 19777 struct loclists_rnglists_header header;
a1c40103
SM
19778 read_loclists_rnglists_header (&header, section,
19779 (sect_offset) (rnglist_base - rnglist_header_size));
19780
19781 /* Verify the rnglist index is valid. */
d0ce17d8
CT
19782 if (rnglist_index >= header.offset_entry_count)
19783 error (_("DW_FORM_rnglistx index pointing outside of "
19784 ".debug_rnglists offset array [in module %s]"),
19785 objfile_name (objfile));
19786
d0ce17d8 19787 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 19788 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
19789 error (_("Reading DW_FORM_rnglistx index beyond end of"
19790 ".debug_rnglists section [in module %s]"),
19791 objfile_name (objfile));
19792
19793 const gdb_byte *info_ptr = section->buffer + start_offset;
19794
19795 if (cu->header.offset_size == 4)
e57933dc 19796 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 19797 else
e57933dc 19798 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
19799}
19800
18a8505e
AT
19801/* Process the attributes that had to be skipped in the first round. These
19802 attributes are the ones that need str_offsets_base or addr_base attributes.
19803 They could not have been processed in the first round, because at the time
19804 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19805static void
19806read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 19807 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
19808{
19809 struct dwarf2_cu *cu = reader->cu;
19810 switch (attr->form)
19811 {
19812 case DW_FORM_addrx:
19813 case DW_FORM_GNU_addr_index:
36d378cf
TT
19814 attr->set_address (read_addr_index (cu,
19815 attr->as_unsigned_reprocess ()));
dda83cd7 19816 break;
41144253 19817 case DW_FORM_loclistx:
b1829e1b 19818 {
e57933dc 19819 sect_offset loclists_sect_off
b1829e1b
SM
19820 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19821
e57933dc 19822 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
19823 }
19824 break;
d0ce17d8 19825 case DW_FORM_rnglistx:
b1829e1b 19826 {
e57933dc 19827 sect_offset rnglists_sect_off
b1829e1b
SM
19828 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19829
e57933dc 19830 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 19831 }
dda83cd7 19832 break;
18a8505e
AT
19833 case DW_FORM_strx:
19834 case DW_FORM_strx1:
19835 case DW_FORM_strx2:
19836 case DW_FORM_strx3:
19837 case DW_FORM_strx4:
19838 case DW_FORM_GNU_str_index:
19839 {
fe56917a 19840 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 19841 gdb_assert (!attr->canonical_string_p ());
18a8505e 19842 if (reader->dwo_file != NULL)
c6481205
TT
19843 attr->set_string_noncanonical (read_dwo_str_index (reader,
19844 str_index));
18a8505e 19845 else
c6481205
TT
19846 attr->set_string_noncanonical (read_stub_str_index (cu,
19847 str_index));
18a8505e
AT
19848 break;
19849 }
19850 default:
19851 gdb_assert_not_reached (_("Unexpected DWARF form."));
19852 }
19853}
19854
a8329558 19855/* Read an attribute value described by an attribute form. */
c906108c 19856
d521ce57 19857static const gdb_byte *
dee91e82
DE
19858read_attribute_value (const struct die_reader_specs *reader,
19859 struct attribute *attr, unsigned form,
7a5f294d 19860 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19861{
dee91e82 19862 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
19863 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19864 struct objfile *objfile = per_objfile->objfile;
dee91e82 19865 bfd *abfd = reader->abfd;
e7c27a73 19866 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19867 unsigned int bytes_read;
19868 struct dwarf_block *blk;
19869
aead7601 19870 attr->form = (enum dwarf_form) form;
a8329558 19871 switch (form)
c906108c 19872 {
c906108c 19873 case DW_FORM_ref_addr:
7c290a04
TT
19874 if (cu_header->version == 2)
19875 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 19876 &bytes_read));
ae411497 19877 else
7c290a04 19878 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19879 &bytes_read));
ae411497
TT
19880 info_ptr += bytes_read;
19881 break;
36586728 19882 case DW_FORM_GNU_ref_alt:
7c290a04 19883 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19884 &bytes_read));
36586728
TT
19885 info_ptr += bytes_read;
19886 break;
ae411497 19887 case DW_FORM_addr:
08feed99
TT
19888 {
19889 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 19890 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
19891 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19892 attr->set_address (addr);
08feed99
TT
19893 info_ptr += bytes_read;
19894 }
c906108c
SS
19895 break;
19896 case DW_FORM_block2:
7b5a2f43 19897 blk = dwarf_alloc_block (cu);
c906108c
SS
19898 blk->size = read_2_bytes (abfd, info_ptr);
19899 info_ptr += 2;
19900 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19901 info_ptr += blk->size;
9d2246fc 19902 attr->set_block (blk);
c906108c
SS
19903 break;
19904 case DW_FORM_block4:
7b5a2f43 19905 blk = dwarf_alloc_block (cu);
c906108c
SS
19906 blk->size = read_4_bytes (abfd, info_ptr);
19907 info_ptr += 4;
19908 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19909 info_ptr += blk->size;
9d2246fc 19910 attr->set_block (blk);
c906108c
SS
19911 break;
19912 case DW_FORM_data2:
414ad644 19913 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
19914 info_ptr += 2;
19915 break;
19916 case DW_FORM_data4:
414ad644 19917 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
19918 info_ptr += 4;
19919 break;
19920 case DW_FORM_data8:
414ad644 19921 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
19922 info_ptr += 8;
19923 break;
0224619f
JK
19924 case DW_FORM_data16:
19925 blk = dwarf_alloc_block (cu);
19926 blk->size = 16;
19927 blk->data = read_n_bytes (abfd, info_ptr, 16);
19928 info_ptr += 16;
9d2246fc 19929 attr->set_block (blk);
0224619f 19930 break;
2dc7f7b3 19931 case DW_FORM_sec_offset:
7c290a04 19932 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19933 &bytes_read));
2dc7f7b3
TT
19934 info_ptr += bytes_read;
19935 break;
41144253 19936 case DW_FORM_loclistx:
19937 {
7a5f294d
TT
19938 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19939 &bytes_read));
19940 info_ptr += bytes_read;
41144253 19941 }
19942 break;
c906108c 19943 case DW_FORM_string:
c6481205
TT
19944 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19945 &bytes_read));
c906108c
SS
19946 info_ptr += bytes_read;
19947 break;
4bdf3d34 19948 case DW_FORM_strp:
36586728
TT
19949 if (!cu->per_cu->is_dwz)
19950 {
c6481205
TT
19951 attr->set_string_noncanonical
19952 (read_indirect_string (per_objfile,
19953 abfd, info_ptr, cu_header,
19954 &bytes_read));
36586728
TT
19955 info_ptr += bytes_read;
19956 break;
19957 }
19958 /* FALLTHROUGH */
43988095
JK
19959 case DW_FORM_line_strp:
19960 if (!cu->per_cu->is_dwz)
19961 {
c6481205
TT
19962 attr->set_string_noncanonical
19963 (per_objfile->read_line_string (info_ptr, cu_header,
19964 &bytes_read));
43988095
JK
19965 info_ptr += bytes_read;
19966 break;
19967 }
19968 /* FALLTHROUGH */
36586728
TT
19969 case DW_FORM_GNU_strp_alt:
19970 {
a7308ce0 19971 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
19972 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19973 &bytes_read);
36586728 19974
c6481205
TT
19975 attr->set_string_noncanonical
19976 (dwz->read_string (objfile, str_offset));
36586728
TT
19977 info_ptr += bytes_read;
19978 }
4bdf3d34 19979 break;
2dc7f7b3 19980 case DW_FORM_exprloc:
c906108c 19981 case DW_FORM_block:
7b5a2f43 19982 blk = dwarf_alloc_block (cu);
c906108c
SS
19983 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19984 info_ptr += bytes_read;
19985 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19986 info_ptr += blk->size;
9d2246fc 19987 attr->set_block (blk);
c906108c
SS
19988 break;
19989 case DW_FORM_block1:
7b5a2f43 19990 blk = dwarf_alloc_block (cu);
c906108c
SS
19991 blk->size = read_1_byte (abfd, info_ptr);
19992 info_ptr += 1;
19993 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19994 info_ptr += blk->size;
9d2246fc 19995 attr->set_block (blk);
c906108c
SS
19996 break;
19997 case DW_FORM_data1:
c906108c 19998 case DW_FORM_flag:
414ad644 19999 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20000 info_ptr += 1;
20001 break;
2dc7f7b3 20002 case DW_FORM_flag_present:
414ad644 20003 attr->set_unsigned (1);
2dc7f7b3 20004 break;
c906108c 20005 case DW_FORM_sdata:
1bc397c5 20006 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20007 info_ptr += bytes_read;
20008 break;
18a8505e 20009 case DW_FORM_rnglistx:
7a5f294d
TT
20010 {
20011 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20012 &bytes_read));
20013 info_ptr += bytes_read;
20014 }
20015 break;
d0ce17d8 20016 case DW_FORM_udata:
414ad644 20017 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20018 info_ptr += bytes_read;
20019 break;
20020 case DW_FORM_ref1:
7c290a04 20021 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20022 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20023 info_ptr += 1;
20024 break;
20025 case DW_FORM_ref2:
7c290a04 20026 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20027 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20028 info_ptr += 2;
20029 break;
20030 case DW_FORM_ref4:
7c290a04 20031 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20032 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20033 info_ptr += 4;
20034 break;
613e1657 20035 case DW_FORM_ref8:
7c290a04 20036 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20037 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20038 info_ptr += 8;
20039 break;
55f1336d 20040 case DW_FORM_ref_sig8:
630ed6b9 20041 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20042 info_ptr += 8;
20043 break;
c906108c 20044 case DW_FORM_ref_udata:
7c290a04 20045 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
20046 + read_unsigned_leb128 (abfd, info_ptr,
20047 &bytes_read)));
c906108c
SS
20048 info_ptr += bytes_read;
20049 break;
c906108c 20050 case DW_FORM_indirect:
a8329558
KW
20051 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20052 info_ptr += bytes_read;
43988095
JK
20053 if (form == DW_FORM_implicit_const)
20054 {
20055 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20056 info_ptr += bytes_read;
20057 }
20058 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20059 info_ptr);
43988095
JK
20060 break;
20061 case DW_FORM_implicit_const:
1bc397c5 20062 attr->set_signed (implicit_const);
a8329558 20063 break;
336d760d 20064 case DW_FORM_addrx:
3019eac3 20065 case DW_FORM_GNU_addr_index:
fe56917a
TT
20066 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20067 &bytes_read));
3019eac3
DE
20068 info_ptr += bytes_read;
20069 break;
cf532bd1 20070 case DW_FORM_strx:
15f18d14
AT
20071 case DW_FORM_strx1:
20072 case DW_FORM_strx2:
20073 case DW_FORM_strx3:
20074 case DW_FORM_strx4:
3019eac3 20075 case DW_FORM_GNU_str_index:
3019eac3 20076 {
15f18d14
AT
20077 ULONGEST str_index;
20078 if (form == DW_FORM_strx1)
20079 {
20080 str_index = read_1_byte (abfd, info_ptr);
20081 info_ptr += 1;
20082 }
20083 else if (form == DW_FORM_strx2)
20084 {
20085 str_index = read_2_bytes (abfd, info_ptr);
20086 info_ptr += 2;
20087 }
20088 else if (form == DW_FORM_strx3)
20089 {
20090 str_index = read_3_bytes (abfd, info_ptr);
20091 info_ptr += 3;
20092 }
20093 else if (form == DW_FORM_strx4)
20094 {
20095 str_index = read_4_bytes (abfd, info_ptr);
20096 info_ptr += 4;
20097 }
20098 else
20099 {
20100 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20101 info_ptr += bytes_read;
20102 }
fe56917a 20103 attr->set_unsigned_reprocess (str_index);
7a5f294d 20104 }
3019eac3 20105 break;
c906108c 20106 default:
8a3fe4f8 20107 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20108 dwarf_form_name (form),
20109 bfd_get_filename (abfd));
c906108c 20110 }
28e94949 20111
36586728 20112 /* Super hack. */
cd6c91b4 20113 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20114 attr->form = DW_FORM_GNU_ref_alt;
20115
28e94949
JB
20116 /* We have seen instances where the compiler tried to emit a byte
20117 size attribute of -1 which ended up being encoded as an unsigned
20118 0xffffffff. Although 0xffffffff is technically a valid size value,
20119 an object of this size seems pretty unlikely so we can relatively
20120 safely treat these cases as if the size attribute was invalid and
20121 treat them as zero by default. */
20122 if (attr->name == DW_AT_byte_size
20123 && form == DW_FORM_data4
529908cb 20124 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20125 {
20126 complaint
dda83cd7
SM
20127 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20128 hex_string (attr->as_unsigned ()));
414ad644 20129 attr->set_unsigned (0);
01c66ae6 20130 }
28e94949 20131
c906108c
SS
20132 return info_ptr;
20133}
20134
a8329558
KW
20135/* Read an attribute described by an abbreviated attribute. */
20136
d521ce57 20137static const gdb_byte *
dee91e82 20138read_attribute (const struct die_reader_specs *reader,
4444f407 20139 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20140 const gdb_byte *info_ptr)
a8329558
KW
20141{
20142 attr->name = abbrev->name;
c6481205 20143 attr->string_is_canonical = 0;
fe56917a 20144 attr->requires_reprocessing = 0;
43988095 20145 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20146 abbrev->implicit_const, info_ptr);
a8329558
KW
20147}
20148
43988095
JK
20149/* Return pointer to string at .debug_str offset STR_OFFSET. */
20150
20151static const char *
976ca316 20152read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20153 LONGEST str_offset)
43988095 20154{
976ca316
SM
20155 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20156 str_offset, "DW_FORM_strp");
c906108c
SS
20157}
20158
43988095
JK
20159/* Return pointer to string at .debug_str offset as read from BUF.
20160 BUF is assumed to be in a compilation unit described by CU_HEADER.
20161 Return *BYTES_READ_PTR count of bytes read from BUF. */
20162
d521ce57 20163static const char *
976ca316 20164read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20165 const gdb_byte *buf,
cf2c3c16
TT
20166 const struct comp_unit_head *cu_header,
20167 unsigned int *bytes_read_ptr)
20168{
8266302d 20169 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20170
976ca316 20171 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20172}
20173
86c0bb4c 20174/* See read.h. */
43988095 20175
86c0bb4c
TT
20176const char *
20177dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20178 const struct comp_unit_head *cu_header,
20179 unsigned int *bytes_read_ptr)
43988095 20180{
86c0bb4c 20181 bfd *abfd = objfile->obfd;
8266302d 20182 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20183
5989a64e 20184 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20185}
20186
3019eac3 20187/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20188 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20189 ADDR_SIZE is the size of addresses from the CU header. */
20190
20191static CORE_ADDR
976ca316
SM
20192read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20193 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20194{
976ca316 20195 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20196 bfd *abfd = objfile->obfd;
20197 const gdb_byte *info_ptr;
18a8505e 20198 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20199
976ca316
SM
20200 per_objfile->per_bfd->addr.read (objfile);
20201 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20202 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20203 objfile_name (objfile));
18a8505e 20204 if (addr_base_or_zero + addr_index * addr_size
976ca316 20205 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20206 error (_("DW_FORM_addr_index pointing outside of "
20207 ".debug_addr section [in module %s]"),
4262abfb 20208 objfile_name (objfile));
976ca316
SM
20209 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20210 + addr_index * addr_size);
3019eac3
DE
20211 if (addr_size == 4)
20212 return bfd_get_32 (abfd, info_ptr);
20213 else
20214 return bfd_get_64 (abfd, info_ptr);
20215}
20216
20217/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20218
20219static CORE_ADDR
20220read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20221{
5e22e966 20222 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20223 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20224}
20225
20226/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20227
20228static CORE_ADDR
d521ce57 20229read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20230 unsigned int *bytes_read)
20231{
5e22e966 20232 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20233 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20234
20235 return read_addr_index (cu, addr_index);
20236}
20237
450a1bfc 20238/* See read.h. */
3019eac3
DE
20239
20240CORE_ADDR
82ca3f51 20241dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20242 dwarf2_per_objfile *per_objfile,
82ca3f51 20243 unsigned int addr_index)
3019eac3 20244{
976ca316 20245 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20246 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20247 int addr_size;
20248
3019eac3
DE
20249 /* We need addr_base and addr_size.
20250 If we don't have PER_CU->cu, we have to get it.
20251 Nasty, but the alternative is storing the needed info in PER_CU,
20252 which at this point doesn't seem justified: it's not clear how frequently
20253 it would get used and it would increase the size of every PER_CU.
20254 Entry points like dwarf2_per_cu_addr_size do a similar thing
20255 so we're not in uncharted territory here.
20256 Alas we need to be a bit more complicated as addr_base is contained
20257 in the DIE.
20258
20259 We don't need to read the entire CU(/TU).
20260 We just need the header and top level die.
a1b64ce1 20261
3019eac3 20262 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20263 For now we skip this optimization. */
3019eac3
DE
20264
20265 if (cu != NULL)
20266 {
20267 addr_base = cu->addr_base;
20268 addr_size = cu->header.addr_size;
20269 }
20270 else
20271 {
976ca316 20272 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20273 addr_base = reader.cu->addr_base;
20274 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20275 }
20276
976ca316 20277 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20278}
20279
18a8505e
AT
20280/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20281 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20282 DWO file. */
3019eac3 20283
d521ce57 20284static const char *
18a8505e
AT
20285read_str_index (struct dwarf2_cu *cu,
20286 struct dwarf2_section_info *str_section,
20287 struct dwarf2_section_info *str_offsets_section,
20288 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20289{
976ca316
SM
20290 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20291 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20292 const char *objf_name = objfile_name (objfile);
3019eac3 20293 bfd *abfd = objfile->obfd;
d521ce57 20294 const gdb_byte *info_ptr;
3019eac3 20295 ULONGEST str_offset;
cf532bd1 20296 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20297
96b79293
TT
20298 str_section->read (objfile);
20299 str_offsets_section->read (objfile);
73869dc2 20300 if (str_section->buffer == NULL)
18a8505e 20301 error (_("%s used without %s section"
9d8780f0 20302 " in CU at offset %s [in module %s]"),
96b79293 20303 form_name, str_section->get_name (),
dda83cd7 20304 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20305 if (str_offsets_section->buffer == NULL)
18a8505e 20306 error (_("%s used without %s section"
9d8780f0 20307 " in CU at offset %s [in module %s]"),
96b79293 20308 form_name, str_section->get_name (),
dda83cd7 20309 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20310 info_ptr = (str_offsets_section->buffer
18a8505e 20311 + str_offsets_base
3019eac3
DE
20312 + str_index * cu->header.offset_size);
20313 if (cu->header.offset_size == 4)
20314 str_offset = bfd_get_32 (abfd, info_ptr);
20315 else
20316 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20317 if (str_offset >= str_section->size)
57d63ce2 20318 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20319 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20320 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20321 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20322}
20323
18a8505e
AT
20324/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20325
20326static const char *
20327read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20328{
20329 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20330 ? reader->cu->header.addr_size : 0;
20331 return read_str_index (reader->cu,
20332 &reader->dwo_file->sections.str,
20333 &reader->dwo_file->sections.str_offsets,
20334 str_offsets_base, str_index);
20335}
20336
20337/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20338
20339static const char *
20340read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20341{
5e22e966 20342 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20343 const char *objf_name = objfile_name (objfile);
20344 static const char form_name[] = "DW_FORM_GNU_str_index";
20345 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20346
20347 if (!cu->str_offsets_base.has_value ())
20348 error (_("%s used in Fission stub without %s"
20349 " in CU at offset 0x%lx [in module %s]"),
20350 form_name, str_offsets_attr_name,
20351 (long) cu->header.offset_size, objf_name);
20352
20353 return read_str_index (cu,
5e22e966
SM
20354 &cu->per_objfile->per_bfd->str,
20355 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20356 *cu->str_offsets_base, str_index);
20357}
20358
3019eac3
DE
20359/* Return the length of an LEB128 number in BUF. */
20360
20361static int
20362leb128_size (const gdb_byte *buf)
20363{
20364 const gdb_byte *begin = buf;
20365 gdb_byte byte;
20366
20367 while (1)
20368 {
20369 byte = *buf++;
20370 if ((byte & 128) == 0)
20371 return buf - begin;
20372 }
20373}
20374
bf1dcdb3
TT
20375static enum language
20376dwarf_lang_to_enum_language (unsigned int lang)
c906108c 20377{
bf1dcdb3
TT
20378 enum language language;
20379
c906108c
SS
20380 switch (lang)
20381 {
20382 case DW_LANG_C89:
76bee0cc 20383 case DW_LANG_C99:
0cfd832f 20384 case DW_LANG_C11:
c906108c 20385 case DW_LANG_C:
d1be3247 20386 case DW_LANG_UPC:
bf1dcdb3 20387 language = language_c;
c906108c 20388 break;
9c37b5ae 20389 case DW_LANG_Java:
c906108c 20390 case DW_LANG_C_plus_plus:
0cfd832f
MW
20391 case DW_LANG_C_plus_plus_11:
20392 case DW_LANG_C_plus_plus_14:
bf1dcdb3 20393 language = language_cplus;
c906108c 20394 break;
6aecb9c2 20395 case DW_LANG_D:
bf1dcdb3 20396 language = language_d;
6aecb9c2 20397 break;
c906108c
SS
20398 case DW_LANG_Fortran77:
20399 case DW_LANG_Fortran90:
b21b22e0 20400 case DW_LANG_Fortran95:
f7de9aab
MW
20401 case DW_LANG_Fortran03:
20402 case DW_LANG_Fortran08:
bf1dcdb3 20403 language = language_fortran;
c906108c 20404 break;
a766d390 20405 case DW_LANG_Go:
bf1dcdb3 20406 language = language_go;
a766d390 20407 break;
c906108c 20408 case DW_LANG_Mips_Assembler:
bf1dcdb3 20409 language = language_asm;
c906108c
SS
20410 break;
20411 case DW_LANG_Ada83:
8aaf0b47 20412 case DW_LANG_Ada95:
bf1dcdb3 20413 language = language_ada;
bc5f45f8 20414 break;
72019c9c 20415 case DW_LANG_Modula2:
bf1dcdb3 20416 language = language_m2;
72019c9c 20417 break;
fe8e67fd 20418 case DW_LANG_Pascal83:
bf1dcdb3 20419 language = language_pascal;
fe8e67fd 20420 break;
22566fbd 20421 case DW_LANG_ObjC:
bf1dcdb3 20422 language = language_objc;
22566fbd 20423 break;
c44af4eb
TT
20424 case DW_LANG_Rust:
20425 case DW_LANG_Rust_old:
bf1dcdb3
TT
20426 language = language_rust;
20427 break;
20428 case DW_LANG_OpenCL:
20429 language = language_opencl;
c44af4eb 20430 break;
c906108c
SS
20431 case DW_LANG_Cobol74:
20432 case DW_LANG_Cobol85:
c906108c 20433 default:
bf1dcdb3 20434 language = language_minimal;
c906108c
SS
20435 break;
20436 }
bf1dcdb3
TT
20437
20438 return language;
c906108c
SS
20439}
20440
20441/* Return the named attribute or NULL if not there. */
20442
20443static struct attribute *
e142c38c 20444dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20445{
a48e046c 20446 for (;;)
c906108c 20447 {
a48e046c
TT
20448 unsigned int i;
20449 struct attribute *spec = NULL;
20450
20451 for (i = 0; i < die->num_attrs; ++i)
20452 {
20453 if (die->attrs[i].name == name)
20454 return &die->attrs[i];
20455 if (die->attrs[i].name == DW_AT_specification
20456 || die->attrs[i].name == DW_AT_abstract_origin)
20457 spec = &die->attrs[i];
20458 }
20459
20460 if (!spec)
20461 break;
c906108c 20462
f2f0e013 20463 die = follow_die_ref (die, spec, &cu);
f2f0e013 20464 }
c5aa993b 20465
c906108c
SS
20466 return NULL;
20467}
20468
7d45c7c3
KB
20469/* Return the string associated with a string-typed attribute, or NULL if it
20470 is either not found or is of an incorrect type. */
20471
20472static const char *
20473dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20474{
20475 struct attribute *attr;
20476 const char *str = NULL;
20477
20478 attr = dwarf2_attr (die, name, cu);
20479
20480 if (attr != NULL)
20481 {
95f982e5 20482 str = attr->as_string ();
e61108c9 20483 if (str == nullptr)
dda83cd7 20484 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20485 "DIE at %s in module %s"),
20486 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20487 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20488 }
20489
20490 return str;
20491}
20492
a084a2a6 20493/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20494 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20495static const char *
20496dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20497{
20498 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20499 if (dwo_name == nullptr)
20500 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20501 return dwo_name;
20502}
20503
05cf31d1
JB
20504/* Return non-zero iff the attribute NAME is defined for the given DIE,
20505 and holds a non-zero value. This function should only be used for
2dc7f7b3 20506 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20507
20508static int
20509dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20510{
20511 struct attribute *attr = dwarf2_attr (die, name, cu);
20512
c45bc3f8 20513 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
20514}
20515
3ca72b44 20516static int
e142c38c 20517die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20518{
05cf31d1
JB
20519 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20520 which value is non-zero. However, we have to be careful with
20521 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20522 (via dwarf2_flag_true_p) follows this attribute. So we may
20523 end up accidently finding a declaration attribute that belongs
20524 to a different DIE referenced by the specification attribute,
20525 even though the given DIE does not have a declaration attribute. */
20526 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20527 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20528}
20529
63d06c5c 20530/* Return the die giving the specification for DIE, if there is
f2f0e013 20531 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20532 containing the return value on output. If there is no
20533 specification, but there is an abstract origin, that is
20534 returned. */
63d06c5c
DC
20535
20536static struct die_info *
f2f0e013 20537die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20538{
f2f0e013
DJ
20539 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20540 *spec_cu);
63d06c5c 20541
edb3359d
DJ
20542 if (spec_attr == NULL)
20543 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20544
63d06c5c
DC
20545 if (spec_attr == NULL)
20546 return NULL;
20547 else
f2f0e013 20548 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20549}
c906108c 20550
83769d0b 20551/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20552
20553static struct dwarf2_section_info *
20554get_debug_line_section (struct dwarf2_cu *cu)
20555{
20556 struct dwarf2_section_info *section;
976ca316 20557 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
20558
20559 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20560 DWO file. */
20561 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20562 section = &cu->dwo_unit->dwo_file->sections.line;
20563 else if (cu->per_cu->is_dwz)
20564 {
a7308ce0 20565 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
20566
20567 section = &dwz->line;
20568 }
20569 else
976ca316 20570 section = &per_objfile->per_bfd->line;
36586728
TT
20571
20572 return section;
20573}
20574
debd256d 20575/* Read the statement program header starting at OFFSET in
3019eac3 20576 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20577 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20578 Returns NULL if there is a problem reading the header, e.g., if it
20579 has a version we don't understand.
debd256d
JB
20580
20581 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20582 the returned object point into the dwarf line section buffer,
20583 and must not be freed. */
ae2de4f8 20584
fff8551c 20585static line_header_up
9c541725 20586dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20587{
3019eac3 20588 struct dwarf2_section_info *section;
976ca316 20589 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 20590
36586728 20591 section = get_debug_line_section (cu);
976ca316 20592 section->read (per_objfile->objfile);
3019eac3 20593 if (section->buffer == NULL)
debd256d 20594 {
3019eac3 20595 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20596 complaint (_("missing .debug_line.dwo section"));
3019eac3 20597 else
b98664d3 20598 complaint (_("missing .debug_line section"));
debd256d
JB
20599 return 0;
20600 }
20601
0df7ad3a 20602 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 20603 per_objfile, section, &cu->header);
debd256d 20604}
c906108c 20605
c6da4cef 20606/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20607 Return the file name of the psymtab for the given file_entry.
c6da4cef 20608 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20609 If space for the result is malloc'd, *NAME_HOLDER will be set.
20610 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20611
d521ce57 20612static const char *
7ba99d21 20613psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20614 const dwarf2_psymtab *pst,
c89b44cd
TT
20615 const char *comp_dir,
20616 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20617{
d521ce57
TT
20618 const char *include_name = fe.name;
20619 const char *include_name_to_compare = include_name;
72b9f47f 20620 const char *pst_filename;
c6da4cef
DE
20621 int file_is_pst;
20622
8c43009f 20623 const char *dir_name = fe.include_dir (lh);
c6da4cef 20624
c89b44cd 20625 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20626 if (!IS_ABSOLUTE_PATH (include_name)
20627 && (dir_name != NULL || comp_dir != NULL))
20628 {
20629 /* Avoid creating a duplicate psymtab for PST.
20630 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20631 Before we do the comparison, however, we need to account
20632 for DIR_NAME and COMP_DIR.
20633 First prepend dir_name (if non-NULL). If we still don't
20634 have an absolute path prepend comp_dir (if non-NULL).
20635 However, the directory we record in the include-file's
20636 psymtab does not contain COMP_DIR (to match the
20637 corresponding symtab(s)).
20638
20639 Example:
20640
20641 bash$ cd /tmp
20642 bash$ gcc -g ./hello.c
20643 include_name = "hello.c"
20644 dir_name = "."
20645 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20646 DW_AT_name = "./hello.c"
20647
20648 */
c6da4cef
DE
20649
20650 if (dir_name != NULL)
20651 {
c89b44cd
TT
20652 name_holder->reset (concat (dir_name, SLASH_STRING,
20653 include_name, (char *) NULL));
20654 include_name = name_holder->get ();
c6da4cef 20655 include_name_to_compare = include_name;
c6da4cef
DE
20656 }
20657 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20658 {
c89b44cd
TT
20659 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20660 include_name, (char *) NULL));
20661 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20662 }
20663 }
20664
20665 pst_filename = pst->filename;
c89b44cd 20666 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20667 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20668 {
c89b44cd
TT
20669 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20670 pst_filename, (char *) NULL));
20671 pst_filename = copied_name.get ();
c6da4cef
DE
20672 }
20673
1e3fad37 20674 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20675
c6da4cef
DE
20676 if (file_is_pst)
20677 return NULL;
20678 return include_name;
20679}
20680
d9b3de22
DE
20681/* State machine to track the state of the line number program. */
20682
6f77053d 20683class lnp_state_machine
d9b3de22 20684{
6f77053d
PA
20685public:
20686 /* Initialize a machine state for the start of a line number
20687 program. */
804d2729
TT
20688 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20689 bool record_lines_p);
6f77053d 20690
8c43009f
PA
20691 file_entry *current_file ()
20692 {
20693 /* lh->file_names is 0-based, but the file name numbers in the
20694 statement program are 1-based. */
6f77053d
PA
20695 return m_line_header->file_name_at (m_file);
20696 }
20697
20698 /* Record the line in the state machine. END_SEQUENCE is true if
20699 we're processing the end of a sequence. */
20700 void record_line (bool end_sequence);
20701
a8caed5d 20702 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 20703 nop-out rest of the lines in this sequence. */
6f77053d
PA
20704 void check_line_address (struct dwarf2_cu *cu,
20705 const gdb_byte *line_ptr,
7ab6656f 20706 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20707
20708 void handle_set_discriminator (unsigned int discriminator)
20709 {
20710 m_discriminator = discriminator;
20711 m_line_has_non_zero_discriminator |= discriminator != 0;
20712 }
20713
20714 /* Handle DW_LNE_set_address. */
20715 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20716 {
20717 m_op_index = 0;
20718 address += baseaddr;
20719 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20720 }
20721
20722 /* Handle DW_LNS_advance_pc. */
20723 void handle_advance_pc (CORE_ADDR adjust);
20724
20725 /* Handle a special opcode. */
20726 void handle_special_opcode (unsigned char op_code);
20727
20728 /* Handle DW_LNS_advance_line. */
20729 void handle_advance_line (int line_delta)
20730 {
20731 advance_line (line_delta);
20732 }
20733
20734 /* Handle DW_LNS_set_file. */
20735 void handle_set_file (file_name_index file);
20736
20737 /* Handle DW_LNS_negate_stmt. */
20738 void handle_negate_stmt ()
20739 {
20740 m_is_stmt = !m_is_stmt;
20741 }
20742
20743 /* Handle DW_LNS_const_add_pc. */
20744 void handle_const_add_pc ();
20745
20746 /* Handle DW_LNS_fixed_advance_pc. */
20747 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20748 {
20749 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20750 m_op_index = 0;
20751 }
20752
20753 /* Handle DW_LNS_copy. */
20754 void handle_copy ()
20755 {
20756 record_line (false);
20757 m_discriminator = 0;
20758 }
20759
20760 /* Handle DW_LNE_end_sequence. */
20761 void handle_end_sequence ()
20762 {
804d2729 20763 m_currently_recording_lines = true;
6f77053d
PA
20764 }
20765
20766private:
20767 /* Advance the line by LINE_DELTA. */
20768 void advance_line (int line_delta)
20769 {
20770 m_line += line_delta;
20771
20772 if (line_delta != 0)
20773 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20774 }
20775
804d2729
TT
20776 struct dwarf2_cu *m_cu;
20777
6f77053d
PA
20778 gdbarch *m_gdbarch;
20779
20780 /* True if we're recording lines.
20781 Otherwise we're building partial symtabs and are just interested in
20782 finding include files mentioned by the line number program. */
20783 bool m_record_lines_p;
20784
8c43009f 20785 /* The line number header. */
6f77053d 20786 line_header *m_line_header;
8c43009f 20787
6f77053d
PA
20788 /* These are part of the standard DWARF line number state machine,
20789 and initialized according to the DWARF spec. */
d9b3de22 20790
6f77053d 20791 unsigned char m_op_index = 0;
7ba99d21
AT
20792 /* The line table index of the current file. */
20793 file_name_index m_file = 1;
6f77053d
PA
20794 unsigned int m_line = 1;
20795
20796 /* These are initialized in the constructor. */
20797
20798 CORE_ADDR m_address;
20799 bool m_is_stmt;
20800 unsigned int m_discriminator;
d9b3de22
DE
20801
20802 /* Additional bits of state we need to track. */
20803
20804 /* The last file that we called dwarf2_start_subfile for.
20805 This is only used for TLLs. */
6f77053d 20806 unsigned int m_last_file = 0;
d9b3de22 20807 /* The last file a line number was recorded for. */
6f77053d 20808 struct subfile *m_last_subfile = NULL;
d9b3de22 20809
1313c56e
AB
20810 /* The address of the last line entry. */
20811 CORE_ADDR m_last_address;
20812
20813 /* Set to true when a previous line at the same address (using
20814 m_last_address) had m_is_stmt true. This is reset to false when a
20815 line entry at a new address (m_address different to m_last_address) is
20816 processed. */
20817 bool m_stmt_at_address = false;
20818
804d2729
TT
20819 /* When true, record the lines we decode. */
20820 bool m_currently_recording_lines = false;
d9b3de22
DE
20821
20822 /* The last line number that was recorded, used to coalesce
20823 consecutive entries for the same line. This can happen, for
20824 example, when discriminators are present. PR 17276. */
6f77053d
PA
20825 unsigned int m_last_line = 0;
20826 bool m_line_has_non_zero_discriminator = false;
8c43009f 20827};
d9b3de22 20828
6f77053d
PA
20829void
20830lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20831{
20832 CORE_ADDR addr_adj = (((m_op_index + adjust)
20833 / m_line_header->maximum_ops_per_instruction)
20834 * m_line_header->minimum_instruction_length);
20835 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20836 m_op_index = ((m_op_index + adjust)
20837 % m_line_header->maximum_ops_per_instruction);
20838}
d9b3de22 20839
6f77053d
PA
20840void
20841lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20842{
6f77053d 20843 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20844 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20845 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20846 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20847 / m_line_header->maximum_ops_per_instruction)
20848 * m_line_header->minimum_instruction_length);
20849 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20850 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20851 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20852
258bf0ee 20853 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20854 advance_line (line_delta);
20855 record_line (false);
20856 m_discriminator = 0;
20857}
d9b3de22 20858
6f77053d
PA
20859void
20860lnp_state_machine::handle_set_file (file_name_index file)
20861{
20862 m_file = file;
20863
20864 const file_entry *fe = current_file ();
20865 if (fe == NULL)
20866 dwarf2_debug_line_missing_file_complaint ();
20867 else if (m_record_lines_p)
20868 {
20869 const char *dir = fe->include_dir (m_line_header);
20870
c24bdb02 20871 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20872 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20873 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20874 }
20875}
20876
20877void
20878lnp_state_machine::handle_const_add_pc ()
20879{
20880 CORE_ADDR adjust
20881 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20882
20883 CORE_ADDR addr_adj
20884 = (((m_op_index + adjust)
20885 / m_line_header->maximum_ops_per_instruction)
20886 * m_line_header->minimum_instruction_length);
20887
20888 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20889 m_op_index = ((m_op_index + adjust)
20890 % m_line_header->maximum_ops_per_instruction);
20891}
d9b3de22 20892
a05a36a5
DE
20893/* Return non-zero if we should add LINE to the line number table.
20894 LINE is the line to add, LAST_LINE is the last line that was added,
20895 LAST_SUBFILE is the subfile for LAST_LINE.
20896 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20897 had a non-zero discriminator.
20898
20899 We have to be careful in the presence of discriminators.
20900 E.g., for this line:
20901
20902 for (i = 0; i < 100000; i++);
20903
20904 clang can emit four line number entries for that one line,
20905 each with a different discriminator.
20906 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20907
20908 However, we want gdb to coalesce all four entries into one.
20909 Otherwise the user could stepi into the middle of the line and
20910 gdb would get confused about whether the pc really was in the
20911 middle of the line.
20912
20913 Things are further complicated by the fact that two consecutive
20914 line number entries for the same line is a heuristic used by gcc
20915 to denote the end of the prologue. So we can't just discard duplicate
20916 entries, we have to be selective about it. The heuristic we use is
20917 that we only collapse consecutive entries for the same line if at least
20918 one of those entries has a non-zero discriminator. PR 17276.
20919
20920 Note: Addresses in the line number state machine can never go backwards
20921 within one sequence, thus this coalescing is ok. */
20922
20923static int
804d2729
TT
20924dwarf_record_line_p (struct dwarf2_cu *cu,
20925 unsigned int line, unsigned int last_line,
a05a36a5
DE
20926 int line_has_non_zero_discriminator,
20927 struct subfile *last_subfile)
20928{
c24bdb02 20929 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20930 return 1;
20931 if (line != last_line)
20932 return 1;
20933 /* Same line for the same file that we've seen already.
20934 As a last check, for pr 17276, only record the line if the line
20935 has never had a non-zero discriminator. */
20936 if (!line_has_non_zero_discriminator)
20937 return 1;
20938 return 0;
20939}
20940
804d2729
TT
20941/* Use the CU's builder to record line number LINE beginning at
20942 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20943
20944static void
d9b3de22 20945dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20946 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20947 struct dwarf2_cu *cu)
252a6764
DE
20948{
20949 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20950
27e0867f
DE
20951 if (dwarf_line_debug)
20952 {
20953 fprintf_unfiltered (gdb_stdlog,
20954 "Recording line %u, file %s, address %s\n",
20955 line, lbasename (subfile->name),
20956 paddress (gdbarch, address));
20957 }
20958
804d2729 20959 if (cu != nullptr)
8c95582d 20960 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20961}
20962
20963/* Subroutine of dwarf_decode_lines_1 to simplify it.
20964 Mark the end of a set of line number records.
d9b3de22 20965 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20966 If SUBFILE is NULL the request is ignored. */
20967
20968static void
20969dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20970 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20971{
27e0867f
DE
20972 if (subfile == NULL)
20973 return;
20974
20975 if (dwarf_line_debug)
20976 {
20977 fprintf_unfiltered (gdb_stdlog,
20978 "Finishing current line, file %s, address %s\n",
20979 lbasename (subfile->name),
20980 paddress (gdbarch, address));
20981 }
20982
8c95582d 20983 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20984}
20985
6f77053d
PA
20986void
20987lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20988{
d9b3de22
DE
20989 if (dwarf_line_debug)
20990 {
20991 fprintf_unfiltered (gdb_stdlog,
20992 "Processing actual line %u: file %u,"
94a72be7 20993 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20994 m_line, m_file,
6f77053d 20995 paddress (m_gdbarch, m_address),
94a72be7
AB
20996 m_is_stmt, m_discriminator,
20997 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20998 }
20999
6f77053d 21000 file_entry *fe = current_file ();
8c43009f
PA
21001
21002 if (fe == NULL)
d9b3de22
DE
21003 dwarf2_debug_line_missing_file_complaint ();
21004 /* For now we ignore lines not starting on an instruction boundary.
21005 But not when processing end_sequence for compatibility with the
21006 previous version of the code. */
6f77053d 21007 else if (m_op_index == 0 || end_sequence)
d9b3de22 21008 {
8c43009f 21009 fe->included_p = 1;
8c95582d 21010 if (m_record_lines_p)
d9b3de22 21011 {
1313c56e
AB
21012 /* When we switch files we insert an end maker in the first file,
21013 switch to the second file and add a new line entry. The
21014 problem is that the end marker inserted in the first file will
21015 discard any previous line entries at the same address. If the
21016 line entries in the first file are marked as is-stmt, while
21017 the new line in the second file is non-stmt, then this means
21018 the end marker will discard is-stmt lines so we can have a
21019 non-stmt line. This means that there are less addresses at
21020 which the user can insert a breakpoint.
21021
21022 To improve this we track the last address in m_last_address,
21023 and whether we have seen an is-stmt at this address. Then
21024 when switching files, if we have seen a stmt at the current
21025 address, and we are switching to create a non-stmt line, then
21026 discard the new line. */
21027 bool file_changed
21028 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21029 bool ignore_this_line
dda83cd7
SM
21030 = ((file_changed && !end_sequence && m_last_address == m_address
21031 && !m_is_stmt && m_stmt_at_address)
21032 || (!end_sequence && m_line == 0));
1313c56e
AB
21033
21034 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21035 {
804d2729
TT
21036 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21037 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21038 }
21039
1313c56e 21040 if (!end_sequence && !ignore_this_line)
d9b3de22 21041 {
8c95582d
AB
21042 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21043
804d2729 21044 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21045 m_line_has_non_zero_discriminator,
21046 m_last_subfile))
d9b3de22 21047 {
c24bdb02 21048 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21049 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21050 builder->get_current_subfile (),
8c95582d 21051 m_line, m_address, is_stmt,
804d2729 21052 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21053 }
c24bdb02 21054 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21055 m_last_line = m_line;
d9b3de22
DE
21056 }
21057 }
21058 }
1313c56e
AB
21059
21060 /* Track whether we have seen any m_is_stmt true at m_address in case we
21061 have multiple line table entries all at m_address. */
21062 if (m_last_address != m_address)
21063 {
21064 m_stmt_at_address = false;
21065 m_last_address = m_address;
21066 }
21067 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21068}
21069
804d2729
TT
21070lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21071 line_header *lh, bool record_lines_p)
d9b3de22 21072{
804d2729 21073 m_cu = cu;
6f77053d
PA
21074 m_gdbarch = arch;
21075 m_record_lines_p = record_lines_p;
21076 m_line_header = lh;
d9b3de22 21077
804d2729 21078 m_currently_recording_lines = true;
d9b3de22 21079
d9b3de22
DE
21080 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21081 was a line entry for it so that the backend has a chance to adjust it
21082 and also record it in case it needs it. This is currently used by MIPS
21083 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21084 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21085 m_is_stmt = lh->default_is_stmt;
21086 m_discriminator = 0;
1313c56e
AB
21087
21088 m_last_address = m_address;
21089 m_stmt_at_address = false;
252a6764
DE
21090}
21091
6f77053d
PA
21092void
21093lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21094 const gdb_byte *line_ptr,
7ab6656f 21095 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21096{
a8caed5d
FS
21097 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21098 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21099 located at 0x0. In this case, additionally check that if
21100 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21101
a8caed5d
FS
21102 if ((address == 0 && address < unrelocated_lowpc)
21103 || address == (CORE_ADDR) -1)
924c2928
DE
21104 {
21105 /* This line table is for a function which has been
21106 GCd by the linker. Ignore it. PR gdb/12528 */
21107
5e22e966 21108 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21109 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21110
b98664d3 21111 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21112 line_offset, objfile_name (objfile));
804d2729
TT
21113 m_currently_recording_lines = false;
21114 /* Note: m_currently_recording_lines is left as false until we see
21115 DW_LNE_end_sequence. */
924c2928
DE
21116 }
21117}
21118
f3f5162e 21119/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21120 Process the line number information in LH.
21121 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21122 program in order to set included_p for every referenced header. */
debd256d 21123
c906108c 21124static void
43f3e411
DE
21125dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21126 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21127{
d521ce57
TT
21128 const gdb_byte *line_ptr, *extended_end;
21129 const gdb_byte *line_end;
a8c50c1f 21130 unsigned int bytes_read, extended_len;
699ca60a 21131 unsigned char op_code, extended_op;
e142c38c 21132 CORE_ADDR baseaddr;
5e22e966 21133 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21134 bfd *abfd = objfile->obfd;
08feed99 21135 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21136 /* True if we're recording line info (as opposed to building partial
21137 symtabs and just interested in finding include files mentioned by
21138 the line number program). */
21139 bool record_lines_p = !decode_for_pst_p;
e142c38c 21140
b3b3bada 21141 baseaddr = objfile->text_section_offset ();
c906108c 21142
debd256d
JB
21143 line_ptr = lh->statement_program_start;
21144 line_end = lh->statement_program_end;
c906108c
SS
21145
21146 /* Read the statement sequences until there's nothing left. */
21147 while (line_ptr < line_end)
21148 {
6f77053d
PA
21149 /* The DWARF line number program state machine. Reset the state
21150 machine at the start of each sequence. */
804d2729 21151 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21152 bool end_sequence = false;
d9b3de22 21153
8c43009f 21154 if (record_lines_p)
c906108c 21155 {
8c43009f
PA
21156 /* Start a subfile for the current file of the state
21157 machine. */
21158 const file_entry *fe = state_machine.current_file ();
21159
21160 if (fe != NULL)
804d2729 21161 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21162 }
21163
a738430d 21164 /* Decode the table. */
d9b3de22 21165 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21166 {
21167 op_code = read_1_byte (abfd, line_ptr);
21168 line_ptr += 1;
9aa1fe7e 21169
debd256d 21170 if (op_code >= lh->opcode_base)
6e70227d 21171 {
8e07a239 21172 /* Special opcode. */
6f77053d 21173 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21174 }
21175 else switch (op_code)
c906108c
SS
21176 {
21177 case DW_LNS_extended_op:
3e43a32a
MS
21178 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21179 &bytes_read);
473b7be6 21180 line_ptr += bytes_read;
a8c50c1f 21181 extended_end = line_ptr + extended_len;
c906108c
SS
21182 extended_op = read_1_byte (abfd, line_ptr);
21183 line_ptr += 1;
8f34b746
TV
21184 if (DW_LNE_lo_user <= extended_op
21185 && extended_op <= DW_LNE_hi_user)
21186 {
21187 /* Vendor extension, ignore. */
21188 line_ptr = extended_end;
21189 break;
21190 }
c906108c
SS
21191 switch (extended_op)
21192 {
21193 case DW_LNE_end_sequence:
6f77053d
PA
21194 state_machine.handle_end_sequence ();
21195 end_sequence = true;
c906108c
SS
21196 break;
21197 case DW_LNE_set_address:
d9b3de22
DE
21198 {
21199 CORE_ADDR address
c8a7a66f 21200 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21201 line_ptr += bytes_read;
6f77053d
PA
21202
21203 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21204 lowpc - baseaddr, address);
6f77053d 21205 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21206 }
c906108c
SS
21207 break;
21208 case DW_LNE_define_file:
dda83cd7
SM
21209 {
21210 const char *cur_file;
ecfb656c
PA
21211 unsigned int mod_time, length;
21212 dir_index dindex;
6e70227d 21213
dda83cd7 21214 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21215 &bytes_read);
dda83cd7
SM
21216 line_ptr += bytes_read;
21217 dindex = (dir_index)
21218 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21219 line_ptr += bytes_read;
21220 mod_time =
21221 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21222 line_ptr += bytes_read;
21223 length =
21224 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21225 line_ptr += bytes_read;
21226 lh->add_file_name (cur_file, dindex, mod_time, length);
21227 }
c906108c 21228 break;
d0c6ba3d 21229 case DW_LNE_set_discriminator:
6f77053d
PA
21230 {
21231 /* The discriminator is not interesting to the
21232 debugger; just ignore it. We still need to
21233 check its value though:
21234 if there are consecutive entries for the same
21235 (non-prologue) line we want to coalesce them.
21236 PR 17276. */
21237 unsigned int discr
21238 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21239 line_ptr += bytes_read;
21240
21241 state_machine.handle_set_discriminator (discr);
21242 }
d0c6ba3d 21243 break;
c906108c 21244 default:
b98664d3 21245 complaint (_("mangled .debug_line section"));
debd256d 21246 return;
c906108c 21247 }
a8c50c1f
DJ
21248 /* Make sure that we parsed the extended op correctly. If e.g.
21249 we expected a different address size than the producer used,
21250 we may have read the wrong number of bytes. */
21251 if (line_ptr != extended_end)
21252 {
b98664d3 21253 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21254 return;
21255 }
c906108c
SS
21256 break;
21257 case DW_LNS_copy:
6f77053d 21258 state_machine.handle_copy ();
c906108c
SS
21259 break;
21260 case DW_LNS_advance_pc:
2dc7f7b3
TT
21261 {
21262 CORE_ADDR adjust
21263 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21264 line_ptr += bytes_read;
6f77053d
PA
21265
21266 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21267 }
c906108c
SS
21268 break;
21269 case DW_LNS_advance_line:
a05a36a5
DE
21270 {
21271 int line_delta
21272 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21273 line_ptr += bytes_read;
6f77053d
PA
21274
21275 state_machine.handle_advance_line (line_delta);
a05a36a5 21276 }
c906108c
SS
21277 break;
21278 case DW_LNS_set_file:
d9b3de22 21279 {
6f77053d 21280 file_name_index file
ecfb656c
PA
21281 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21282 &bytes_read);
d9b3de22 21283 line_ptr += bytes_read;
8c43009f 21284
6f77053d 21285 state_machine.handle_set_file (file);
d9b3de22 21286 }
c906108c
SS
21287 break;
21288 case DW_LNS_set_column:
0ad93d4f 21289 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21290 line_ptr += bytes_read;
21291 break;
21292 case DW_LNS_negate_stmt:
6f77053d 21293 state_machine.handle_negate_stmt ();
c906108c
SS
21294 break;
21295 case DW_LNS_set_basic_block:
c906108c 21296 break;
c2c6d25f
JM
21297 /* Add to the address register of the state machine the
21298 address increment value corresponding to special opcode
a738430d
MK
21299 255. I.e., this value is scaled by the minimum
21300 instruction length since special opcode 255 would have
b021a221 21301 scaled the increment. */
c906108c 21302 case DW_LNS_const_add_pc:
6f77053d 21303 state_machine.handle_const_add_pc ();
c906108c
SS
21304 break;
21305 case DW_LNS_fixed_advance_pc:
3e29f34a 21306 {
6f77053d 21307 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21308 line_ptr += 2;
6f77053d
PA
21309
21310 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21311 }
c906108c 21312 break;
9aa1fe7e 21313 default:
a738430d
MK
21314 {
21315 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21316 int i;
a738430d 21317
debd256d 21318 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21319 {
21320 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21321 line_ptr += bytes_read;
21322 }
21323 }
c906108c
SS
21324 }
21325 }
d9b3de22
DE
21326
21327 if (!end_sequence)
21328 dwarf2_debug_line_missing_end_sequence_complaint ();
21329
21330 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21331 in which case we still finish recording the last line). */
6f77053d 21332 state_machine.record_line (true);
c906108c 21333 }
f3f5162e
DE
21334}
21335
21336/* Decode the Line Number Program (LNP) for the given line_header
21337 structure and CU. The actual information extracted and the type
21338 of structures created from the LNP depends on the value of PST.
21339
21340 1. If PST is NULL, then this procedure uses the data from the program
21341 to create all necessary symbol tables, and their linetables.
21342
21343 2. If PST is not NULL, this procedure reads the program to determine
21344 the list of files included by the unit represented by PST, and
21345 builds all the associated partial symbol tables.
21346
21347 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21348 It is used for relative paths in the line table.
21349 NOTE: When processing partial symtabs (pst != NULL),
21350 comp_dir == pst->dirname.
21351
21352 NOTE: It is important that psymtabs have the same file name (via strcmp)
21353 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21354 symtab we don't use it in the name of the psymtabs we create.
21355 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21356 A good testcase for this is mb-inline.exp.
21357
527f3840
JK
21358 LOWPC is the lowest address in CU (or 0 if not known).
21359
21360 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21361 for its PC<->lines mapping information. Otherwise only the filename
21362 table is read in. */
f3f5162e
DE
21363
21364static void
21365dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21366 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21367 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21368{
5e22e966 21369 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21370 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21371
527f3840
JK
21372 if (decode_mapping)
21373 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21374
21375 if (decode_for_pst_p)
21376 {
aaa75496 21377 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21378 create the psymtab of each included file. */
7ba99d21 21379 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21380 if (file_entry.included_p == 1)
21381 {
c89b44cd 21382 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21383 const char *include_name =
7ba99d21
AT
21384 psymtab_include_file_name (lh, file_entry, pst,
21385 comp_dir, &name_holder);
c6da4cef 21386 if (include_name != NULL)
7b249e47
TT
21387 dwarf2_create_include_psymtab
21388 (cu->per_objfile->per_bfd, include_name, pst,
21389 cu->per_objfile->per_bfd->partial_symtabs.get (),
0072c873 21390 objfile->per_bfd);
dda83cd7 21391 }
aaa75496 21392 }
cb1df416
DJ
21393 else
21394 {
21395 /* Make sure a symtab is created for every file, even files
21396 which contain only variables (i.e. no code with associated
21397 line numbers). */
c24bdb02
KS
21398 buildsym_compunit *builder = cu->get_builder ();
21399 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21400
7ba99d21 21401 for (auto &fe : lh->file_names ())
cb1df416 21402 {
804d2729 21403 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21404 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21405 {
c24bdb02 21406 builder->get_current_subfile ()->symtab
804d2729 21407 = allocate_symtab (cust,
c24bdb02 21408 builder->get_current_subfile ()->name);
43f3e411 21409 }
c24bdb02 21410 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21411 }
21412 }
c906108c
SS
21413}
21414
21415/* Start a subfile for DWARF. FILENAME is the name of the file and
21416 DIRNAME the name of the source directory which contains FILENAME
4d663531 21417 or NULL if not known.
c906108c
SS
21418 This routine tries to keep line numbers from identical absolute and
21419 relative file names in a common subfile.
21420
21421 Using the `list' example from the GDB testsuite, which resides in
21422 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21423 of /srcdir/list0.c yields the following debugging information for list0.c:
21424
c5aa993b 21425 DW_AT_name: /srcdir/list0.c
4d663531 21426 DW_AT_comp_dir: /compdir
357e46e7 21427 files.files[0].name: list0.h
c5aa993b 21428 files.files[0].dir: /srcdir
357e46e7 21429 files.files[1].name: list0.c
c5aa993b 21430 files.files[1].dir: /srcdir
c906108c
SS
21431
21432 The line number information for list0.c has to end up in a single
4f1520fb
FR
21433 subfile, so that `break /srcdir/list0.c:1' works as expected.
21434 start_subfile will ensure that this happens provided that we pass the
21435 concatenation of files.files[1].dir and files.files[1].name as the
21436 subfile's name. */
c906108c
SS
21437
21438static void
804d2729
TT
21439dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21440 const char *dirname)
c906108c 21441{
43816ebc 21442 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21443
4d663531 21444 /* In order not to lose the line information directory,
4f1520fb
FR
21445 we concatenate it to the filename when it makes sense.
21446 Note that the Dwarf3 standard says (speaking of filenames in line
21447 information): ``The directory index is ignored for file names
21448 that represent full path names''. Thus ignoring dirname in the
21449 `else' branch below isn't an issue. */
c906108c 21450
d5166ae1 21451 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21452 {
43816ebc
TT
21453 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21454 filename = copy.get ();
d521ce57 21455 }
c906108c 21456
c24bdb02 21457 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21458}
21459
4c2df51b
DJ
21460static void
21461var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21462 struct dwarf2_cu *cu)
4c2df51b 21463{
5e22e966 21464 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21465 struct comp_unit_head *cu_header = &cu->header;
21466
4c2df51b
DJ
21467 /* NOTE drow/2003-01-30: There used to be a comment and some special
21468 code here to turn a symbol with DW_AT_external and a
21469 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21470 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21471 with some versions of binutils) where shared libraries could have
21472 relocations against symbols in their debug information - the
21473 minimal symbol would have the right address, but the debug info
21474 would not. It's no longer necessary, because we will explicitly
21475 apply relocations when we read in the debug information now. */
21476
21477 /* A DW_AT_location attribute with no contents indicates that a
21478 variable has been optimized away. */
9d2246fc 21479 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21480 {
f1e6e072 21481 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21482 return;
21483 }
21484
21485 /* Handle one degenerate form of location expression specially, to
21486 preserve GDB's previous behavior when section offsets are
336d760d
AT
21487 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21488 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21489
9d2246fc
TT
21490 if (attr->form_is_block ())
21491 {
21492 struct dwarf_block *block = attr->as_block ();
21493
21494 if ((block->data[0] == DW_OP_addr
21495 && block->size == 1 + cu_header->addr_size)
21496 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 21497 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
21498 && (block->size
21499 == 1 + leb128_size (&block->data[1]))))
21500 {
21501 unsigned int dummy;
21502
21503 if (block->data[0] == DW_OP_addr)
21504 SET_SYMBOL_VALUE_ADDRESS
21505 (sym, cu->header.read_address (objfile->obfd,
21506 block->data + 1,
38583298 21507 &dummy));
9d2246fc
TT
21508 else
21509 SET_SYMBOL_VALUE_ADDRESS
21510 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21511 &dummy));
21512 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21513 fixup_symbol_section (sym, objfile);
21514 SET_SYMBOL_VALUE_ADDRESS
21515 (sym,
21516 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 21517 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
21518 return;
21519 }
4c2df51b
DJ
21520 }
21521
21522 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21523 expression evaluator, and use LOC_COMPUTED only when necessary
21524 (i.e. when the value of a register or memory location is
21525 referenced, or a thread-local block, etc.). Then again, it might
21526 not be worthwhile. I'm assuming that it isn't unless performance
21527 or memory numbers show me otherwise. */
21528
f1e6e072 21529 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21530
f1e6e072 21531 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21532 cu->has_loclist = true;
4c2df51b
DJ
21533}
21534
c906108c
SS
21535/* Given a pointer to a DWARF information entry, figure out if we need
21536 to make a symbol table entry for it, and if so, create a new entry
21537 and return a pointer to it.
21538 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21539 used the passed type.
21540 If SPACE is not NULL, use it to hold the new symbol. If it is
21541 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21542
21543static struct symbol *
5e2db402
TT
21544new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21545 struct symbol *space)
c906108c 21546{
976ca316
SM
21547 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21548 struct objfile *objfile = per_objfile->objfile;
08feed99 21549 struct gdbarch *gdbarch = objfile->arch ();
c906108c 21550 struct symbol *sym = NULL;
15d034d0 21551 const char *name;
c906108c
SS
21552 struct attribute *attr = NULL;
21553 struct attribute *attr2 = NULL;
e142c38c 21554 CORE_ADDR baseaddr;
e37fd15a
SW
21555 struct pending **list_to_add = NULL;
21556
edb3359d 21557 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21558
b3b3bada 21559 baseaddr = objfile->text_section_offset ();
c906108c 21560
94af9270 21561 name = dwarf2_name (die, cu);
c906108c
SS
21562 if (name)
21563 {
34eaf542 21564 int suppress_add = 0;
94af9270 21565
34eaf542
TT
21566 if (space)
21567 sym = space;
21568 else
8c14c3a3 21569 sym = new (&objfile->objfile_obstack) symbol;
c906108c 21570 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21571
21572 /* Cache this symbol's name and the name's demangled form (if any). */
3e9f1ca1 21573 sym->set_language (cu->per_cu->lang, &objfile->objfile_obstack);
f55ee35c
JK
21574 /* Fortran does not have mangling standard and the mangling does differ
21575 between gfortran, iFort etc. */
bcfe6157 21576 const char *physname
3e9f1ca1 21577 = (cu->per_cu->lang == language_fortran
bcfe6157
TT
21578 ? dwarf2_full_name (name, die, cu)
21579 : dwarf2_physname (name, die, cu));
21580 const char *linkagename = dw2_linkage_name (die, cu);
21581
3e9f1ca1 21582 if (linkagename == nullptr || cu->per_cu->lang == language_ada)
bcfe6157
TT
21583 sym->set_linkage_name (physname);
21584 else
21585 {
21586 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21587 sym->set_linkage_name (linkagename);
21588 }
f55ee35c 21589
c906108c 21590 /* Default assumptions.
dda83cd7 21591 Use the passed type or decode it from the die. */
176620f1 21592 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21593 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21594 if (type != NULL)
21595 SYMBOL_TYPE (sym) = type;
21596 else
e7c27a73 21597 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21598 attr = dwarf2_attr (die,
21599 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21600 cu);
435d3d88 21601 if (attr != nullptr)
529908cb 21602 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 21603
edb3359d
DJ
21604 attr = dwarf2_attr (die,
21605 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21606 cu);
aa659cfa 21607 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 21608 {
529908cb 21609 file_name_index file_index
aa659cfa 21610 = (file_name_index) attr->as_nonnegative ();
8c43009f 21611 struct file_entry *fe;
9a619af0 21612
ecfb656c
PA
21613 if (cu->line_header != NULL)
21614 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21615 else
21616 fe = NULL;
21617
21618 if (fe == NULL)
b98664d3 21619 complaint (_("file index out of range"));
8c43009f
PA
21620 else
21621 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21622 }
21623
c906108c
SS
21624 switch (die->tag)
21625 {
21626 case DW_TAG_label:
e142c38c 21627 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21628 if (attr != nullptr)
3e29f34a
MR
21629 {
21630 CORE_ADDR addr;
21631
95f982e5 21632 addr = attr->as_address ();
3e29f34a 21633 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21634 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 21635 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 21636 }
8f5c6526
TV
21637 else
21638 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
21639 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21640 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 21641 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21642 break;
21643 case DW_TAG_subprogram:
21644 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21645 finish_block. */
f1e6e072 21646 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21647 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 21648 if ((attr2 != nullptr && attr2->as_boolean ())
3e9f1ca1
TT
21649 || cu->per_cu->lang == language_ada
21650 || cu->per_cu->lang == language_fortran)
c906108c 21651 {
dda83cd7
SM
21652 /* Subprograms marked external are stored as a global symbol.
21653 Ada and Fortran subprograms, whether marked external or
21654 not, are always stored as a global symbol, because we want
21655 to be able to access them globally. For instance, we want
21656 to be able to break on a nested subprogram without having
21657 to specify the context. */
c24bdb02 21658 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21659 }
21660 else
21661 {
e37fd15a 21662 list_to_add = cu->list_in_scope;
c906108c
SS
21663 }
21664 break;
edb3359d
DJ
21665 case DW_TAG_inlined_subroutine:
21666 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21667 finish_block. */
f1e6e072 21668 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21669 SYMBOL_INLINED (sym) = 1;
481860b3 21670 list_to_add = cu->list_in_scope;
edb3359d 21671 break;
34eaf542
TT
21672 case DW_TAG_template_value_param:
21673 suppress_add = 1;
21674 /* Fall through. */
72929c62 21675 case DW_TAG_constant:
c906108c 21676 case DW_TAG_variable:
254e6b9e 21677 case DW_TAG_member:
0963b4bd
MS
21678 /* Compilation with minimal debug info may result in
21679 variables with missing type entries. Change the
21680 misleading `void' type to something sensible. */
78134374 21681 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 21682 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21683
e142c38c 21684 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21685 /* In the case of DW_TAG_member, we should only be called for
21686 static const members. */
21687 if (die->tag == DW_TAG_member)
21688 {
3863f96c
DE
21689 /* dwarf2_add_field uses die_is_declaration,
21690 so we do the same. */
254e6b9e
DE
21691 gdb_assert (die_is_declaration (die, cu));
21692 gdb_assert (attr);
21693 }
435d3d88 21694 if (attr != nullptr)
c906108c 21695 {
e7c27a73 21696 dwarf2_const_value (attr, sym, cu);
e142c38c 21697 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21698 if (!suppress_add)
34eaf542 21699 {
c45bc3f8 21700 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 21701 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21702 else
e37fd15a 21703 list_to_add = cu->list_in_scope;
34eaf542 21704 }
c906108c
SS
21705 break;
21706 }
e142c38c 21707 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21708 if (attr != nullptr)
c906108c 21709 {
e7c27a73 21710 var_decode_location (attr, sym, cu);
e142c38c 21711 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21712
21713 /* Fortran explicitly imports any global symbols to the local
21714 scope by DW_TAG_common_block. */
3e9f1ca1 21715 if (cu->per_cu->lang == language_fortran && die->parent
4357ac6c
TT
21716 && die->parent->tag == DW_TAG_common_block)
21717 attr2 = NULL;
21718
caac4577
JG
21719 if (SYMBOL_CLASS (sym) == LOC_STATIC
21720 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 21721 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
21722 {
21723 /* When a static variable is eliminated by the linker,
21724 the corresponding debug information is not stripped
21725 out, but the variable address is set to null;
21726 do not add such variables into symbol table. */
21727 }
c45bc3f8 21728 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 21729 {
4b610737
TT
21730 if (SYMBOL_CLASS (sym) == LOC_STATIC
21731 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 21732 && per_objfile->per_bfd->can_copy)
4b610737
TT
21733 {
21734 /* A global static variable might be subject to
21735 copy relocation. We first check for a local
21736 minsym, though, because maybe the symbol was
21737 marked hidden, in which case this would not
21738 apply. */
21739 bound_minimal_symbol found
21740 = (lookup_minimal_symbol_linkage
987012b8 21741 (sym->linkage_name (), objfile));
4b610737
TT
21742 if (found.minsym != nullptr)
21743 sym->maybe_copied = 1;
21744 }
f55ee35c 21745
1c809c68
TT
21746 /* A variable with DW_AT_external is never static,
21747 but it may be block-scoped. */
804d2729 21748 list_to_add
c24bdb02
KS
21749 = ((cu->list_in_scope
21750 == cu->get_builder ()->get_file_symbols ())
21751 ? cu->get_builder ()->get_global_symbols ()
804d2729 21752 : cu->list_in_scope);
1c809c68 21753 }
c906108c 21754 else
e37fd15a 21755 list_to_add = cu->list_in_scope;
c906108c
SS
21756 }
21757 else
21758 {
21759 /* We do not know the address of this symbol.
dda83cd7
SM
21760 If it is an external symbol and we have type information
21761 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21762 The address of the variable will then be determined from
21763 the minimal symbol table whenever the variable is
21764 referenced. */
e142c38c 21765 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21766
21767 /* Fortran explicitly imports any global symbols to the local
21768 scope by DW_TAG_common_block. */
3e9f1ca1 21769 if (cu->per_cu->lang == language_fortran && die->parent
0971de02
TT
21770 && die->parent->tag == DW_TAG_common_block)
21771 {
21772 /* SYMBOL_CLASS doesn't matter here because
21773 read_common_block is going to reset it. */
21774 if (!suppress_add)
21775 list_to_add = cu->list_in_scope;
21776 }
c45bc3f8 21777 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 21778 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21779 {
0fe7935b
DJ
21780 /* A variable with DW_AT_external is never static, but it
21781 may be block-scoped. */
804d2729 21782 list_to_add
c24bdb02
KS
21783 = ((cu->list_in_scope
21784 == cu->get_builder ()->get_file_symbols ())
21785 ? cu->get_builder ()->get_global_symbols ()
804d2729 21786 : cu->list_in_scope);
0fe7935b 21787
f1e6e072 21788 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21789 }
442ddf59
JK
21790 else if (!die_is_declaration (die, cu))
21791 {
21792 /* Use the default LOC_OPTIMIZED_OUT class. */
21793 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21794 if (!suppress_add)
21795 list_to_add = cu->list_in_scope;
442ddf59 21796 }
c906108c
SS
21797 }
21798 break;
21799 case DW_TAG_formal_parameter:
a60f3166 21800 {
3db19b2d
TV
21801 /* If we are inside a function, mark this as an argument. If
21802 not, we might be looking at an argument to an inlined function
21803 when we do not have enough information to show inlined frames;
21804 pretend it's a local variable in that case so that the user can
21805 still see it. */
21806 struct context_stack *curr
21807 = cu->get_builder ()->get_current_context_stack ();
21808 if (curr != nullptr && curr->name != nullptr)
21809 SYMBOL_IS_ARGUMENT (sym) = 1;
a60f3166 21810 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21811 if (attr != nullptr)
a60f3166
TT
21812 {
21813 var_decode_location (attr, sym, cu);
21814 }
21815 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21816 if (attr != nullptr)
a60f3166
TT
21817 {
21818 dwarf2_const_value (attr, sym, cu);
21819 }
f346a30d 21820
a60f3166
TT
21821 list_to_add = cu->list_in_scope;
21822 }
c906108c
SS
21823 break;
21824 case DW_TAG_unspecified_parameters:
21825 /* From varargs functions; gdb doesn't seem to have any
21826 interest in this information, so just ignore it for now.
21827 (FIXME?) */
21828 break;
34eaf542
TT
21829 case DW_TAG_template_type_param:
21830 suppress_add = 1;
21831 /* Fall through. */
c906108c 21832 case DW_TAG_class_type:
680b30c7 21833 case DW_TAG_interface_type:
c906108c
SS
21834 case DW_TAG_structure_type:
21835 case DW_TAG_union_type:
72019c9c 21836 case DW_TAG_set_type:
c906108c 21837 case DW_TAG_enumeration_type:
f1e6e072 21838 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21839 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21840
63d06c5c 21841 {
9c37b5ae 21842 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21843 really ever be static objects: otherwise, if you try
21844 to, say, break of a class's method and you're in a file
21845 which doesn't mention that class, it won't work unless
21846 the check for all static symbols in lookup_symbol_aux
21847 saves you. See the OtherFileClass tests in
21848 gdb.c++/namespace.exp. */
21849
e37fd15a 21850 if (!suppress_add)
34eaf542 21851 {
c24bdb02 21852 buildsym_compunit *builder = cu->get_builder ();
804d2729 21853 list_to_add
c24bdb02 21854 = (cu->list_in_scope == builder->get_file_symbols ()
3e9f1ca1 21855 && cu->per_cu->lang == language_cplus
c24bdb02 21856 ? builder->get_global_symbols ()
804d2729 21857 : cu->list_in_scope);
63d06c5c 21858
64382290 21859 /* The semantics of C++ state that "struct foo {
9c37b5ae 21860 ... }" also defines a typedef for "foo". */
3e9f1ca1
TT
21861 if (cu->per_cu->lang == language_cplus
21862 || cu->per_cu->lang == language_ada
21863 || cu->per_cu->lang == language_d
21864 || cu->per_cu->lang == language_rust)
64382290
TT
21865 {
21866 /* The symbol's name is already allocated along
21867 with this objfile, so we don't need to
21868 duplicate it for the type. */
7d93a1e0 21869 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21870 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21871 }
63d06c5c
DC
21872 }
21873 }
c906108c
SS
21874 break;
21875 case DW_TAG_typedef:
f1e6e072 21876 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21877 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21878 list_to_add = cu->list_in_scope;
63d06c5c 21879 break;
d8f62e84 21880 case DW_TAG_array_type:
c906108c 21881 case DW_TAG_base_type:
dda83cd7 21882 case DW_TAG_subrange_type:
f1e6e072 21883 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21884 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21885 list_to_add = cu->list_in_scope;
c906108c
SS
21886 break;
21887 case DW_TAG_enumerator:
e142c38c 21888 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21889 if (attr != nullptr)
c906108c 21890 {
e7c27a73 21891 dwarf2_const_value (attr, sym, cu);
c906108c 21892 }
63d06c5c
DC
21893 {
21894 /* NOTE: carlton/2003-11-10: See comment above in the
21895 DW_TAG_class_type, etc. block. */
21896
804d2729 21897 list_to_add
c24bdb02 21898 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
3e9f1ca1 21899 && cu->per_cu->lang == language_cplus
c24bdb02 21900 ? cu->get_builder ()->get_global_symbols ()
804d2729 21901 : cu->list_in_scope);
63d06c5c 21902 }
c906108c 21903 break;
74921315 21904 case DW_TAG_imported_declaration:
5c4e30ca 21905 case DW_TAG_namespace:
f1e6e072 21906 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21907 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21908 break;
530e8392
KB
21909 case DW_TAG_module:
21910 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21911 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21912 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21913 break;
4357ac6c 21914 case DW_TAG_common_block:
f1e6e072 21915 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21916 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21917 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21918 break;
c906108c
SS
21919 default:
21920 /* Not a tag we recognize. Hopefully we aren't processing
21921 trash data, but since we must specifically ignore things
21922 we don't recognize, there is nothing else we should do at
0963b4bd 21923 this point. */
b98664d3 21924 complaint (_("unsupported tag: '%s'"),
4d3c2250 21925 dwarf_tag_name (die->tag));
c906108c
SS
21926 break;
21927 }
df8a16a1 21928
e37fd15a
SW
21929 if (suppress_add)
21930 {
21931 sym->hash_next = objfile->template_symbols;
21932 objfile->template_symbols = sym;
21933 list_to_add = NULL;
21934 }
21935
21936 if (list_to_add != NULL)
d3cb6808 21937 add_symbol_to_list (sym, list_to_add);
e37fd15a 21938
df8a16a1
DJ
21939 /* For the benefit of old versions of GCC, check for anonymous
21940 namespaces based on the demangled name. */
4d4ec4e5 21941 if (!cu->processing_has_namespace_info
3e9f1ca1 21942 && cu->per_cu->lang == language_cplus)
c24bdb02 21943 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21944 }
21945 return (sym);
21946}
21947
98bfdba5
PA
21948/* Given an attr with a DW_FORM_dataN value in host byte order,
21949 zero-extend it as appropriate for the symbol's type. The DWARF
21950 standard (v4) is not entirely clear about the meaning of using
21951 DW_FORM_dataN for a constant with a signed type, where the type is
21952 wider than the data. The conclusion of a discussion on the DWARF
21953 list was that this is unspecified. We choose to always zero-extend
21954 because that is the interpretation long in use by GCC. */
c906108c 21955
98bfdba5 21956static gdb_byte *
ff39bb5e 21957dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21958 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21959{
5e22e966 21960 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21961 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21962 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 21963 LONGEST l = attr->constant_value (0);
98bfdba5
PA
21964
21965 if (bits < sizeof (*value) * 8)
21966 {
21967 l &= ((LONGEST) 1 << bits) - 1;
21968 *value = l;
21969 }
21970 else if (bits == sizeof (*value) * 8)
21971 *value = l;
21972 else
21973 {
224c3ddb 21974 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21975 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21976 return bytes;
21977 }
21978
21979 return NULL;
21980}
21981
21982/* Read a constant value from an attribute. Either set *VALUE, or if
21983 the value does not fit in *VALUE, set *BYTES - either already
21984 allocated on the objfile obstack, or newly allocated on OBSTACK,
21985 or, set *BATON, if we translated the constant to a location
21986 expression. */
21987
21988static void
ff39bb5e 21989dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21990 const char *name, struct obstack *obstack,
21991 struct dwarf2_cu *cu,
d521ce57 21992 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21993 struct dwarf2_locexpr_baton **baton)
21994{
5e22e966 21995 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21996 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21997 struct comp_unit_head *cu_header = &cu->header;
c906108c 21998 struct dwarf_block *blk;
98bfdba5
PA
21999 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22000 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22001
22002 *value = 0;
22003 *bytes = NULL;
22004 *baton = NULL;
c906108c
SS
22005
22006 switch (attr->form)
22007 {
22008 case DW_FORM_addr:
336d760d 22009 case DW_FORM_addrx:
3019eac3 22010 case DW_FORM_GNU_addr_index:
ac56253d 22011 {
ac56253d
TT
22012 gdb_byte *data;
22013
98bfdba5
PA
22014 if (TYPE_LENGTH (type) != cu_header->addr_size)
22015 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22016 cu_header->addr_size,
98bfdba5 22017 TYPE_LENGTH (type));
ac56253d
TT
22018 /* Symbols of this form are reasonably rare, so we just
22019 piggyback on the existing location code rather than writing
22020 a new implementation of symbol_computed_ops. */
8d749320 22021 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22022 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22023 (*baton)->per_cu = cu->per_cu;
22024 gdb_assert ((*baton)->per_cu);
ac56253d 22025
98bfdba5 22026 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22027 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22028 (*baton)->data = data;
ac56253d
TT
22029
22030 data[0] = DW_OP_addr;
22031 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22032 byte_order, attr->as_address ());
ac56253d 22033 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22034 }
c906108c 22035 break;
4ac36638 22036 case DW_FORM_string:
93b5768b 22037 case DW_FORM_strp:
cf532bd1 22038 case DW_FORM_strx:
3019eac3 22039 case DW_FORM_GNU_str_index:
36586728 22040 case DW_FORM_GNU_strp_alt:
c6481205 22041 /* The string is already allocated on the objfile obstack, point
98bfdba5 22042 directly to it. */
2c830f54 22043 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22044 break;
c906108c
SS
22045 case DW_FORM_block1:
22046 case DW_FORM_block2:
22047 case DW_FORM_block4:
22048 case DW_FORM_block:
2dc7f7b3 22049 case DW_FORM_exprloc:
0224619f 22050 case DW_FORM_data16:
9d2246fc 22051 blk = attr->as_block ();
98bfdba5
PA
22052 if (TYPE_LENGTH (type) != blk->size)
22053 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22054 TYPE_LENGTH (type));
22055 *bytes = blk->data;
c906108c 22056 break;
2df3850c
JM
22057
22058 /* The DW_AT_const_value attributes are supposed to carry the
22059 symbol's value "represented as it would be on the target
22060 architecture." By the time we get here, it's already been
22061 converted to host endianness, so we just need to sign- or
22062 zero-extend it as appropriate. */
22063 case DW_FORM_data1:
3aef2284 22064 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22065 break;
c906108c 22066 case DW_FORM_data2:
3aef2284 22067 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22068 break;
c906108c 22069 case DW_FORM_data4:
3aef2284 22070 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22071 break;
c906108c 22072 case DW_FORM_data8:
3aef2284 22073 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22074 break;
22075
c906108c 22076 case DW_FORM_sdata:
663c44ac 22077 case DW_FORM_implicit_const:
1bc397c5 22078 *value = attr->as_signed ();
2df3850c
JM
22079 break;
22080
c906108c 22081 case DW_FORM_udata:
529908cb 22082 *value = attr->as_unsigned ();
c906108c 22083 break;
2df3850c 22084
c906108c 22085 default:
b98664d3 22086 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22087 dwarf_form_name (attr->form));
98bfdba5 22088 *value = 0;
c906108c
SS
22089 break;
22090 }
22091}
22092
2df3850c 22093
98bfdba5
PA
22094/* Copy constant value from an attribute to a symbol. */
22095
2df3850c 22096static void
ff39bb5e 22097dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22098 struct dwarf2_cu *cu)
2df3850c 22099{
5e22e966 22100 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22101 LONGEST value;
d521ce57 22102 const gdb_byte *bytes;
98bfdba5 22103 struct dwarf2_locexpr_baton *baton;
2df3850c 22104
98bfdba5 22105 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22106 sym->print_name (),
98bfdba5
PA
22107 &objfile->objfile_obstack, cu,
22108 &value, &bytes, &baton);
2df3850c 22109
98bfdba5
PA
22110 if (baton != NULL)
22111 {
98bfdba5 22112 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22113 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22114 }
22115 else if (bytes != NULL)
22116 {
22117 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22118 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22119 }
22120 else
22121 {
22122 SYMBOL_VALUE (sym) = value;
f1e6e072 22123 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22124 }
2df3850c
JM
22125}
22126
c906108c
SS
22127/* Return the type of the die in question using its DW_AT_type attribute. */
22128
22129static struct type *
e7c27a73 22130die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22131{
c906108c 22132 struct attribute *type_attr;
c906108c 22133
e142c38c 22134 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22135 if (!type_attr)
22136 {
5e22e966 22137 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22138 /* A missing DW_AT_type represents a void type. */
518817b3 22139 return objfile_type (objfile)->builtin_void;
c906108c 22140 }
348e048f 22141
673bfd45 22142 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22143}
22144
b4ba55a1
JB
22145/* True iff CU's producer generates GNAT Ada auxiliary information
22146 that allows to find parallel types through that information instead
22147 of having to do expensive parallel lookups by type name. */
22148
22149static int
22150need_gnat_info (struct dwarf2_cu *cu)
22151{
de4cb04a
JB
22152 /* Assume that the Ada compiler was GNAT, which always produces
22153 the auxiliary information. */
3e9f1ca1 22154 return (cu->per_cu->lang == language_ada);
b4ba55a1
JB
22155}
22156
b4ba55a1
JB
22157/* Return the auxiliary type of the die in question using its
22158 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22159 attribute is not present. */
22160
22161static struct type *
22162die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22163{
b4ba55a1 22164 struct attribute *type_attr;
b4ba55a1
JB
22165
22166 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22167 if (!type_attr)
22168 return NULL;
22169
673bfd45 22170 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22171}
22172
22173/* If DIE has a descriptive_type attribute, then set the TYPE's
22174 descriptive type accordingly. */
22175
22176static void
22177set_descriptive_type (struct type *type, struct die_info *die,
22178 struct dwarf2_cu *cu)
22179{
22180 struct type *descriptive_type = die_descriptive_type (die, cu);
22181
22182 if (descriptive_type)
22183 {
22184 ALLOCATE_GNAT_AUX_TYPE (type);
22185 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22186 }
22187}
22188
c906108c
SS
22189/* Return the containing type of the die in question using its
22190 DW_AT_containing_type attribute. */
22191
22192static struct type *
e7c27a73 22193die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22194{
c906108c 22195 struct attribute *type_attr;
5e22e966 22196 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22197
e142c38c 22198 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22199 if (!type_attr)
22200 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22201 "[in module %s]"), objfile_name (objfile));
33ac96f0 22202
673bfd45 22203 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22204}
22205
ac9ec31b
DE
22206/* Return an error marker type to use for the ill formed type in DIE/CU. */
22207
22208static struct type *
22209build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22210{
976ca316
SM
22211 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22212 struct objfile *objfile = per_objfile->objfile;
528e1572 22213 char *saved;
ac9ec31b 22214
528e1572
SM
22215 std::string message
22216 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22217 objfile_name (objfile),
22218 sect_offset_str (cu->header.sect_off),
22219 sect_offset_str (die->sect_off));
efba19b0 22220 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22221
19f392bc 22222 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22223}
22224
673bfd45 22225/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22226 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22227 DW_AT_containing_type.
673bfd45
DE
22228 If there is no type substitute an error marker. */
22229
c906108c 22230static struct type *
ff39bb5e 22231lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22232 struct dwarf2_cu *cu)
c906108c 22233{
976ca316
SM
22234 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22235 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22236 struct type *this_type;
22237
ac9ec31b
DE
22238 gdb_assert (attr->name == DW_AT_type
22239 || attr->name == DW_AT_GNAT_descriptive_type
22240 || attr->name == DW_AT_containing_type);
22241
673bfd45
DE
22242 /* First see if we have it cached. */
22243
36586728
TT
22244 if (attr->form == DW_FORM_GNU_ref_alt)
22245 {
22246 struct dwarf2_per_cu_data *per_cu;
0826b30a 22247 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22248
976ca316
SM
22249 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22250 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22251 }
cd6c91b4 22252 else if (attr->form_is_ref ())
673bfd45 22253 {
0826b30a 22254 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22255
976ca316 22256 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22257 }
55f1336d 22258 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22259 {
630ed6b9 22260 ULONGEST signature = attr->as_signature ();
673bfd45 22261
ac9ec31b 22262 return get_signatured_type (die, signature, cu);
673bfd45
DE
22263 }
22264 else
22265 {
b98664d3 22266 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22267 " at %s [in module %s]"),
22268 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22269 objfile_name (objfile));
ac9ec31b 22270 return build_error_marker_type (cu, die);
673bfd45
DE
22271 }
22272
22273 /* If not cached we need to read it in. */
22274
22275 if (this_type == NULL)
22276 {
ac9ec31b 22277 struct die_info *type_die = NULL;
673bfd45
DE
22278 struct dwarf2_cu *type_cu = cu;
22279
cd6c91b4 22280 if (attr->form_is_ref ())
ac9ec31b
DE
22281 type_die = follow_die_ref (die, attr, &type_cu);
22282 if (type_die == NULL)
22283 return build_error_marker_type (cu, die);
22284 /* If we find the type now, it's probably because the type came
3019eac3
DE
22285 from an inter-CU reference and the type's CU got expanded before
22286 ours. */
ac9ec31b 22287 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22288 }
22289
22290 /* If we still don't have a type use an error marker. */
22291
22292 if (this_type == NULL)
ac9ec31b 22293 return build_error_marker_type (cu, die);
673bfd45 22294
f792889a 22295 return this_type;
c906108c
SS
22296}
22297
673bfd45
DE
22298/* Return the type in DIE, CU.
22299 Returns NULL for invalid types.
22300
02142a6c 22301 This first does a lookup in die_type_hash,
673bfd45
DE
22302 and only reads the die in if necessary.
22303
22304 NOTE: This can be called when reading in partial or full symbols. */
22305
f792889a 22306static struct type *
e7c27a73 22307read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22308{
f792889a
DJ
22309 struct type *this_type;
22310
22311 this_type = get_die_type (die, cu);
22312 if (this_type)
22313 return this_type;
22314
673bfd45
DE
22315 return read_type_die_1 (die, cu);
22316}
22317
22318/* Read the type in DIE, CU.
22319 Returns NULL for invalid types. */
22320
22321static struct type *
22322read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22323{
22324 struct type *this_type = NULL;
22325
c906108c
SS
22326 switch (die->tag)
22327 {
22328 case DW_TAG_class_type:
680b30c7 22329 case DW_TAG_interface_type:
c906108c
SS
22330 case DW_TAG_structure_type:
22331 case DW_TAG_union_type:
f792889a 22332 this_type = read_structure_type (die, cu);
c906108c
SS
22333 break;
22334 case DW_TAG_enumeration_type:
f792889a 22335 this_type = read_enumeration_type (die, cu);
c906108c
SS
22336 break;
22337 case DW_TAG_subprogram:
22338 case DW_TAG_subroutine_type:
edb3359d 22339 case DW_TAG_inlined_subroutine:
f792889a 22340 this_type = read_subroutine_type (die, cu);
c906108c
SS
22341 break;
22342 case DW_TAG_array_type:
f792889a 22343 this_type = read_array_type (die, cu);
c906108c 22344 break;
72019c9c 22345 case DW_TAG_set_type:
f792889a 22346 this_type = read_set_type (die, cu);
72019c9c 22347 break;
c906108c 22348 case DW_TAG_pointer_type:
f792889a 22349 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22350 break;
22351 case DW_TAG_ptr_to_member_type:
f792889a 22352 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22353 break;
22354 case DW_TAG_reference_type:
4297a3f0
AV
22355 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22356 break;
22357 case DW_TAG_rvalue_reference_type:
22358 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22359 break;
22360 case DW_TAG_const_type:
f792889a 22361 this_type = read_tag_const_type (die, cu);
c906108c
SS
22362 break;
22363 case DW_TAG_volatile_type:
f792889a 22364 this_type = read_tag_volatile_type (die, cu);
c906108c 22365 break;
06d66ee9
TT
22366 case DW_TAG_restrict_type:
22367 this_type = read_tag_restrict_type (die, cu);
22368 break;
c906108c 22369 case DW_TAG_string_type:
f792889a 22370 this_type = read_tag_string_type (die, cu);
c906108c
SS
22371 break;
22372 case DW_TAG_typedef:
f792889a 22373 this_type = read_typedef (die, cu);
c906108c 22374 break;
a02abb62 22375 case DW_TAG_subrange_type:
f792889a 22376 this_type = read_subrange_type (die, cu);
a02abb62 22377 break;
c906108c 22378 case DW_TAG_base_type:
f792889a 22379 this_type = read_base_type (die, cu);
c906108c 22380 break;
81a17f79 22381 case DW_TAG_unspecified_type:
f792889a 22382 this_type = read_unspecified_type (die, cu);
81a17f79 22383 break;
0114d602
DJ
22384 case DW_TAG_namespace:
22385 this_type = read_namespace_type (die, cu);
22386 break;
f55ee35c
JK
22387 case DW_TAG_module:
22388 this_type = read_module_type (die, cu);
22389 break;
a2c2acaf
MW
22390 case DW_TAG_atomic_type:
22391 this_type = read_tag_atomic_type (die, cu);
22392 break;
c906108c 22393 default:
b98664d3 22394 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22395 dwarf_tag_name (die->tag));
c906108c
SS
22396 break;
22397 }
63d06c5c 22398
f792889a 22399 return this_type;
63d06c5c
DC
22400}
22401
abc72ce4
DE
22402/* See if we can figure out if the class lives in a namespace. We do
22403 this by looking for a member function; its demangled name will
22404 contain namespace info, if there is any.
22405 Return the computed name or NULL.
22406 Space for the result is allocated on the objfile's obstack.
22407 This is the full-die version of guess_partial_die_structure_name.
22408 In this case we know DIE has no useful parent. */
22409
43816ebc 22410static const char *
abc72ce4
DE
22411guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22412{
22413 struct die_info *spec_die;
22414 struct dwarf2_cu *spec_cu;
22415 struct die_info *child;
5e22e966 22416 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22417
22418 spec_cu = cu;
22419 spec_die = die_specification (die, &spec_cu);
22420 if (spec_die != NULL)
22421 {
22422 die = spec_die;
22423 cu = spec_cu;
22424 }
22425
22426 for (child = die->child;
22427 child != NULL;
22428 child = child->sibling)
22429 {
22430 if (child->tag == DW_TAG_subprogram)
22431 {
73b9be8b 22432 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22433
7d45c7c3 22434 if (linkage_name != NULL)
abc72ce4 22435 {
43816ebc 22436 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22437 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22438 const char *name = NULL;
abc72ce4
DE
22439
22440 if (actual_name != NULL)
22441 {
15d034d0 22442 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22443
22444 if (die_name != NULL
43816ebc 22445 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22446 {
22447 /* Strip off the class name from the full name.
22448 We want the prefix. */
22449 int die_name_len = strlen (die_name);
43816ebc
TT
22450 int actual_name_len = strlen (actual_name.get ());
22451 const char *ptr = actual_name.get ();
abc72ce4
DE
22452
22453 /* Test for '::' as a sanity check. */
22454 if (actual_name_len > die_name_len + 2
43816ebc 22455 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22456 name = obstack_strndup (
e3b94546 22457 &objfile->per_bfd->storage_obstack,
43816ebc 22458 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22459 }
22460 }
abc72ce4
DE
22461 return name;
22462 }
22463 }
22464 }
22465
22466 return NULL;
22467}
22468
96408a79
SA
22469/* GCC might emit a nameless typedef that has a linkage name. Determine the
22470 prefix part in such case. See
22471 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22472
a121b7c1 22473static const char *
96408a79
SA
22474anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22475{
22476 struct attribute *attr;
e6a959d6 22477 const char *base;
96408a79
SA
22478
22479 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22480 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22481 return NULL;
22482
7d45c7c3 22483 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22484 return NULL;
22485
73b9be8b 22486 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22487 const char *attr_name = attr->as_string ();
22488 if (attr == NULL || attr_name == NULL)
96408a79
SA
22489 return NULL;
22490
22491 /* dwarf2_name had to be already called. */
3b64bf15 22492 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22493
22494 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22495 base = strrchr (attr_name, ':');
22496 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22497 return "";
22498
5e22e966 22499 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22500 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22501 attr_name,
22502 &base[-1] - attr_name);
96408a79
SA
22503}
22504
fdde2d81 22505/* Return the name of the namespace/class that DIE is defined within,
0114d602 22506 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22507
0114d602
DJ
22508 For example, if we're within the method foo() in the following
22509 code:
22510
22511 namespace N {
22512 class C {
22513 void foo () {
22514 }
22515 };
22516 }
22517
22518 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22519
0d5cff50 22520static const char *
e142c38c 22521determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22522{
976ca316 22523 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
22524 struct die_info *parent, *spec_die;
22525 struct dwarf2_cu *spec_cu;
22526 struct type *parent_type;
a121b7c1 22527 const char *retval;
63d06c5c 22528
3e9f1ca1
TT
22529 if (cu->per_cu->lang != language_cplus
22530 && cu->per_cu->lang != language_fortran
22531 && cu->per_cu->lang != language_d
22532 && cu->per_cu->lang != language_rust)
0114d602
DJ
22533 return "";
22534
96408a79
SA
22535 retval = anonymous_struct_prefix (die, cu);
22536 if (retval)
22537 return retval;
22538
0114d602
DJ
22539 /* We have to be careful in the presence of DW_AT_specification.
22540 For example, with GCC 3.4, given the code
22541
22542 namespace N {
22543 void foo() {
22544 // Definition of N::foo.
22545 }
22546 }
22547
22548 then we'll have a tree of DIEs like this:
22549
22550 1: DW_TAG_compile_unit
22551 2: DW_TAG_namespace // N
22552 3: DW_TAG_subprogram // declaration of N::foo
22553 4: DW_TAG_subprogram // definition of N::foo
22554 DW_AT_specification // refers to die #3
22555
22556 Thus, when processing die #4, we have to pretend that we're in
22557 the context of its DW_AT_specification, namely the contex of die
22558 #3. */
22559 spec_cu = cu;
22560 spec_die = die_specification (die, &spec_cu);
22561 if (spec_die == NULL)
22562 parent = die->parent;
22563 else
63d06c5c 22564 {
0114d602
DJ
22565 parent = spec_die->parent;
22566 cu = spec_cu;
63d06c5c 22567 }
0114d602
DJ
22568
22569 if (parent == NULL)
22570 return "";
98bfdba5
PA
22571 else if (parent->building_fullname)
22572 {
22573 const char *name;
22574 const char *parent_name;
22575
22576 /* It has been seen on RealView 2.2 built binaries,
22577 DW_TAG_template_type_param types actually _defined_ as
22578 children of the parent class:
22579
22580 enum E {};
22581 template class <class Enum> Class{};
22582 Class<enum E> class_e;
22583
dda83cd7
SM
22584 1: DW_TAG_class_type (Class)
22585 2: DW_TAG_enumeration_type (E)
22586 3: DW_TAG_enumerator (enum1:0)
22587 3: DW_TAG_enumerator (enum2:1)
22588 ...
22589 2: DW_TAG_template_type_param
22590 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
22591
22592 Besides being broken debug info, it can put GDB into an
22593 infinite loop. Consider:
22594
22595 When we're building the full name for Class<E>, we'll start
22596 at Class, and go look over its template type parameters,
22597 finding E. We'll then try to build the full name of E, and
22598 reach here. We're now trying to build the full name of E,
22599 and look over the parent DIE for containing scope. In the
22600 broken case, if we followed the parent DIE of E, we'd again
22601 find Class, and once again go look at its template type
22602 arguments, etc., etc. Simply don't consider such parent die
22603 as source-level parent of this die (it can't be, the language
22604 doesn't allow it), and break the loop here. */
22605 name = dwarf2_name (die, cu);
22606 parent_name = dwarf2_name (parent, cu);
b98664d3 22607 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22608 name ? name : "<unknown>",
22609 parent_name ? parent_name : "<unknown>");
22610 return "";
22611 }
63d06c5c 22612 else
0114d602
DJ
22613 switch (parent->tag)
22614 {
63d06c5c 22615 case DW_TAG_namespace:
0114d602 22616 parent_type = read_type_die (parent, cu);
acebe513
UW
22617 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22618 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22619 Work around this problem here. */
3e9f1ca1 22620 if (cu->per_cu->lang == language_cplus
7d93a1e0 22621 && strcmp (parent_type->name (), "::") == 0)
acebe513 22622 return "";
0114d602 22623 /* We give a name to even anonymous namespaces. */
7d93a1e0 22624 return parent_type->name ();
63d06c5c 22625 case DW_TAG_class_type:
680b30c7 22626 case DW_TAG_interface_type:
63d06c5c 22627 case DW_TAG_structure_type:
0114d602 22628 case DW_TAG_union_type:
f55ee35c 22629 case DW_TAG_module:
0114d602 22630 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
22631 if (parent_type->name () != NULL)
22632 return parent_type->name ();
0114d602
DJ
22633 else
22634 /* An anonymous structure is only allowed non-static data
22635 members; no typedefs, no member functions, et cetera.
22636 So it does not need a prefix. */
22637 return "";
abc72ce4 22638 case DW_TAG_compile_unit:
95554aad 22639 case DW_TAG_partial_unit:
abc72ce4 22640 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
3e9f1ca1 22641 if (cu->per_cu->lang == language_cplus
976ca316 22642 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
22643 && die->child != NULL
22644 && (die->tag == DW_TAG_class_type
22645 || die->tag == DW_TAG_structure_type
22646 || die->tag == DW_TAG_union_type))
22647 {
43816ebc 22648 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22649 if (name != NULL)
22650 return name;
22651 }
22652 return "";
0a4b0913
AB
22653 case DW_TAG_subprogram:
22654 /* Nested subroutines in Fortran get a prefix with the name
22655 of the parent's subroutine. */
3e9f1ca1 22656 if (cu->per_cu->lang == language_fortran)
0a4b0913
AB
22657 {
22658 if ((die->tag == DW_TAG_subprogram)
22659 && (dwarf2_name (parent, cu) != NULL))
22660 return dwarf2_name (parent, cu);
22661 }
22662 return determine_prefix (parent, cu);
3d567982
TT
22663 case DW_TAG_enumeration_type:
22664 parent_type = read_type_die (parent, cu);
3bc440a2 22665 if (parent_type->is_declared_class ())
3d567982 22666 {
7d93a1e0
SM
22667 if (parent_type->name () != NULL)
22668 return parent_type->name ();
3d567982
TT
22669 return "";
22670 }
22671 /* Fall through. */
63d06c5c 22672 default:
8176b9b8 22673 return determine_prefix (parent, cu);
63d06c5c 22674 }
63d06c5c
DC
22675}
22676
3e43a32a
MS
22677/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22678 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22679 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22680 an obconcat, otherwise allocate storage for the result. The CU argument is
22681 used to determine the language and hence, the appropriate separator. */
987504bb 22682
f55ee35c 22683#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22684
22685static char *
f55ee35c 22686typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 22687 int physname, struct dwarf2_cu *cu)
63d06c5c 22688{
f55ee35c 22689 const char *lead = "";
5c315b68 22690 const char *sep;
63d06c5c 22691
3e43a32a
MS
22692 if (suffix == NULL || suffix[0] == '\0'
22693 || prefix == NULL || prefix[0] == '\0')
987504bb 22694 sep = "";
3e9f1ca1 22695 else if (cu->per_cu->lang == language_d)
45280282
IB
22696 {
22697 /* For D, the 'main' function could be defined in any module, but it
22698 should never be prefixed. */
22699 if (strcmp (suffix, "D main") == 0)
22700 {
22701 prefix = "";
22702 sep = "";
22703 }
22704 else
22705 sep = ".";
22706 }
3e9f1ca1 22707 else if (cu->per_cu->lang == language_fortran && physname)
f55ee35c
JK
22708 {
22709 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22710 DW_AT_MIPS_linkage_name is preferred and used instead. */
22711
22712 lead = "__";
22713 sep = "_MOD_";
22714 }
987504bb
JJ
22715 else
22716 sep = "::";
63d06c5c 22717
6dd47d34
DE
22718 if (prefix == NULL)
22719 prefix = "";
22720 if (suffix == NULL)
22721 suffix = "";
22722
987504bb
JJ
22723 if (obs == NULL)
22724 {
3e43a32a 22725 char *retval
224c3ddb
SM
22726 = ((char *)
22727 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22728
f55ee35c
JK
22729 strcpy (retval, lead);
22730 strcat (retval, prefix);
6dd47d34
DE
22731 strcat (retval, sep);
22732 strcat (retval, suffix);
63d06c5c
DC
22733 return retval;
22734 }
987504bb
JJ
22735 else
22736 {
22737 /* We have an obstack. */
f55ee35c 22738 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22739 }
63d06c5c
DC
22740}
22741
71c25dea
TT
22742/* Get name of a die, return NULL if not found. */
22743
15d034d0
TT
22744static const char *
22745dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 22746 struct objfile *objfile)
71c25dea 22747{
3e9f1ca1 22748 if (name && cu->per_cu->lang == language_cplus)
71c25dea 22749 {
596dc4ad
TT
22750 gdb::unique_xmalloc_ptr<char> canon_name
22751 = cp_canonicalize_string (name);
71c25dea 22752
596dc4ad
TT
22753 if (canon_name != nullptr)
22754 name = objfile->intern (canon_name.get ());
71c25dea
TT
22755 }
22756
22757 return name;
c906108c
SS
22758}
22759
96553a0c
DE
22760/* Get name of a die, return NULL if not found.
22761 Anonymous namespaces are converted to their magic string. */
9219021c 22762
15d034d0 22763static const char *
e142c38c 22764dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22765{
22766 struct attribute *attr;
5e22e966 22767 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 22768
e142c38c 22769 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
22770 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22771 if (attr_name == nullptr
96553a0c 22772 && die->tag != DW_TAG_namespace
53832f31
TT
22773 && die->tag != DW_TAG_class_type
22774 && die->tag != DW_TAG_interface_type
22775 && die->tag != DW_TAG_structure_type
22776 && die->tag != DW_TAG_union_type)
71c25dea
TT
22777 return NULL;
22778
22779 switch (die->tag)
22780 {
22781 case DW_TAG_compile_unit:
95554aad 22782 case DW_TAG_partial_unit:
71c25dea
TT
22783 /* Compilation units have a DW_AT_name that is a filename, not
22784 a source language identifier. */
22785 case DW_TAG_enumeration_type:
22786 case DW_TAG_enumerator:
22787 /* These tags always have simple identifiers already; no need
22788 to canonicalize them. */
2c830f54 22789 return attr_name;
907af001 22790
96553a0c 22791 case DW_TAG_namespace:
2c830f54
TT
22792 if (attr_name != nullptr)
22793 return attr_name;
96553a0c
DE
22794 return CP_ANONYMOUS_NAMESPACE_STR;
22795
907af001
UW
22796 case DW_TAG_class_type:
22797 case DW_TAG_interface_type:
22798 case DW_TAG_structure_type:
22799 case DW_TAG_union_type:
22800 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22801 structures or unions. These were of the form "._%d" in GCC 4.1,
22802 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22803 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
22804 if (attr_name != nullptr
22805 && (startswith (attr_name, "._")
22806 || startswith (attr_name, "<anonymous")))
907af001 22807 return NULL;
53832f31
TT
22808
22809 /* GCC might emit a nameless typedef that has a linkage name. See
22810 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 22811 if (!attr || attr_name == NULL)
53832f31 22812 {
73b9be8b 22813 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 22814 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 22815 if (attr == NULL || attr_name == NULL)
53832f31
TT
22816 return NULL;
22817
2c830f54 22818 /* Avoid demangling attr_name the second time on a second
df5c6c50 22819 call for the same DIE. */
3b64bf15 22820 if (!attr->canonical_string_p ())
53832f31 22821 {
43816ebc 22822 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 22823 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
22824 if (demangled == nullptr)
22825 return nullptr;
43816ebc 22826
c6481205 22827 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 22828 attr_name = attr->as_string ();
53832f31 22829 }
67430cd0 22830
2c830f54
TT
22831 /* Strip any leading namespaces/classes, keep only the
22832 base name. DW_AT_name for named DIEs does not
22833 contain the prefixes. */
22834 const char *base = strrchr (attr_name, ':');
22835 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
22836 return &base[1];
22837 else
2c830f54 22838 return attr_name;
53832f31 22839 }
907af001
UW
22840 break;
22841
71c25dea 22842 default:
907af001
UW
22843 break;
22844 }
22845
3b64bf15 22846 if (!attr->canonical_string_p ())
c6481205
TT
22847 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22848 objfile));
2c830f54 22849 return attr->as_string ();
9219021c
DC
22850}
22851
22852/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22853 is none. *EXT_CU is the CU containing DIE on input, and the CU
22854 containing the return value on output. */
9219021c
DC
22855
22856static struct die_info *
f2f0e013 22857dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22858{
22859 struct attribute *attr;
9219021c 22860
f2f0e013 22861 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22862 if (attr == NULL)
22863 return NULL;
22864
f2f0e013 22865 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22866}
22867
f9aca02d 22868static void
d97bc12b 22869dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22870{
22871 unsigned int i;
22872
d97bc12b 22873 print_spaces (indent, f);
9d8780f0 22874 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22875 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22876 sect_offset_str (die->sect_off));
d97bc12b
DE
22877
22878 if (die->parent != NULL)
22879 {
22880 print_spaces (indent, f);
9d8780f0
SM
22881 fprintf_unfiltered (f, " parent at offset: %s\n",
22882 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22883 }
22884
22885 print_spaces (indent, f);
22886 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22887 dwarf_bool_name (die->child != NULL));
c906108c 22888
d97bc12b
DE
22889 print_spaces (indent, f);
22890 fprintf_unfiltered (f, " attributes:\n");
22891
c906108c
SS
22892 for (i = 0; i < die->num_attrs; ++i)
22893 {
d97bc12b
DE
22894 print_spaces (indent, f);
22895 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22896 dwarf_attr_name (die->attrs[i].name),
22897 dwarf_form_name (die->attrs[i].form));
d97bc12b 22898
c906108c
SS
22899 switch (die->attrs[i].form)
22900 {
c906108c 22901 case DW_FORM_addr:
336d760d 22902 case DW_FORM_addrx:
3019eac3 22903 case DW_FORM_GNU_addr_index:
d97bc12b 22904 fprintf_unfiltered (f, "address: ");
36d378cf 22905 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
22906 break;
22907 case DW_FORM_block2:
22908 case DW_FORM_block4:
22909 case DW_FORM_block:
22910 case DW_FORM_block1:
56eb65bd 22911 fprintf_unfiltered (f, "block: size %s",
9d2246fc 22912 pulongest (die->attrs[i].as_block ()->size));
c906108c 22913 break;
2dc7f7b3 22914 case DW_FORM_exprloc:
56eb65bd 22915 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 22916 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 22917 break;
0224619f
JK
22918 case DW_FORM_data16:
22919 fprintf_unfiltered (f, "constant of 16 bytes");
22920 break;
4568ecf9
DE
22921 case DW_FORM_ref_addr:
22922 fprintf_unfiltered (f, "ref address: ");
529908cb 22923 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 22924 break;
36586728
TT
22925 case DW_FORM_GNU_ref_alt:
22926 fprintf_unfiltered (f, "alt ref address: ");
529908cb 22927 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 22928 break;
10b3939b
DJ
22929 case DW_FORM_ref1:
22930 case DW_FORM_ref2:
22931 case DW_FORM_ref4:
4568ecf9
DE
22932 case DW_FORM_ref8:
22933 case DW_FORM_ref_udata:
d97bc12b 22934 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 22935 (long) (die->attrs[i].as_unsigned ()));
10b3939b 22936 break;
c906108c
SS
22937 case DW_FORM_data1:
22938 case DW_FORM_data2:
22939 case DW_FORM_data4:
ce5d95e1 22940 case DW_FORM_data8:
c906108c 22941 case DW_FORM_udata:
43bbcdc2 22942 fprintf_unfiltered (f, "constant: %s",
529908cb 22943 pulongest (die->attrs[i].as_unsigned ()));
c906108c 22944 break;
2dc7f7b3
TT
22945 case DW_FORM_sec_offset:
22946 fprintf_unfiltered (f, "section offset: %s",
529908cb 22947 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 22948 break;
55f1336d 22949 case DW_FORM_ref_sig8:
ac9ec31b 22950 fprintf_unfiltered (f, "signature: %s",
630ed6b9 22951 hex_string (die->attrs[i].as_signature ()));
348e048f 22952 break;
c906108c 22953 case DW_FORM_string:
4bdf3d34 22954 case DW_FORM_strp:
43988095 22955 case DW_FORM_line_strp:
cf532bd1 22956 case DW_FORM_strx:
3019eac3 22957 case DW_FORM_GNU_str_index:
36586728 22958 case DW_FORM_GNU_strp_alt:
8285870a 22959 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
22960 die->attrs[i].as_string ()
22961 ? die->attrs[i].as_string () : "",
22962 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
22963 break;
22964 case DW_FORM_flag:
c45bc3f8 22965 if (die->attrs[i].as_boolean ())
d97bc12b 22966 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22967 else
d97bc12b 22968 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22969 break;
2dc7f7b3
TT
22970 case DW_FORM_flag_present:
22971 fprintf_unfiltered (f, "flag: TRUE");
22972 break;
a8329558 22973 case DW_FORM_indirect:
0963b4bd
MS
22974 /* The reader will have reduced the indirect form to
22975 the "base form" so this form should not occur. */
5f48f8f3 22976 fprintf_unfiltered (f,
3e43a32a 22977 "unexpected attribute form: DW_FORM_indirect");
a8329558 22978 break;
1bc397c5 22979 case DW_FORM_sdata:
663c44ac
JK
22980 case DW_FORM_implicit_const:
22981 fprintf_unfiltered (f, "constant: %s",
1bc397c5 22982 plongest (die->attrs[i].as_signed ()));
663c44ac 22983 break;
c906108c 22984 default:
d97bc12b 22985 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22986 die->attrs[i].form);
d97bc12b 22987 break;
c906108c 22988 }
d97bc12b 22989 fprintf_unfiltered (f, "\n");
c906108c
SS
22990 }
22991}
22992
f9aca02d 22993static void
d97bc12b 22994dump_die_for_error (struct die_info *die)
c906108c 22995{
d97bc12b
DE
22996 dump_die_shallow (gdb_stderr, 0, die);
22997}
22998
22999static void
23000dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23001{
23002 int indent = level * 4;
23003
23004 gdb_assert (die != NULL);
23005
23006 if (level >= max_level)
23007 return;
23008
23009 dump_die_shallow (f, indent, die);
23010
23011 if (die->child != NULL)
c906108c 23012 {
d97bc12b
DE
23013 print_spaces (indent, f);
23014 fprintf_unfiltered (f, " Children:");
23015 if (level + 1 < max_level)
23016 {
23017 fprintf_unfiltered (f, "\n");
23018 dump_die_1 (f, level + 1, max_level, die->child);
23019 }
23020 else
23021 {
3e43a32a
MS
23022 fprintf_unfiltered (f,
23023 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23024 }
23025 }
23026
23027 if (die->sibling != NULL && level > 0)
23028 {
23029 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23030 }
23031}
23032
d97bc12b
DE
23033/* This is called from the pdie macro in gdbinit.in.
23034 It's not static so gcc will keep a copy callable from gdb. */
23035
23036void
23037dump_die (struct die_info *die, int max_level)
23038{
23039 dump_die_1 (gdb_stdlog, 0, max_level, die);
23040}
23041
f9aca02d 23042static void
51545339 23043store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23044{
51545339 23045 void **slot;
c906108c 23046
9c541725
PA
23047 slot = htab_find_slot_with_hash (cu->die_hash, die,
23048 to_underlying (die->sect_off),
b64f50a1 23049 INSERT);
51545339
DJ
23050
23051 *slot = die;
c906108c
SS
23052}
23053
348e048f
DE
23054/* Follow reference or signature attribute ATTR of SRC_DIE.
23055 On entry *REF_CU is the CU of SRC_DIE.
23056 On exit *REF_CU is the CU of the result. */
23057
23058static struct die_info *
ff39bb5e 23059follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23060 struct dwarf2_cu **ref_cu)
23061{
23062 struct die_info *die;
23063
cd6c91b4 23064 if (attr->form_is_ref ())
348e048f 23065 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23066 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23067 die = follow_die_sig (src_die, attr, ref_cu);
23068 else
23069 {
23070 dump_die_for_error (src_die);
23071 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23072 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23073 }
23074
23075 return die;
03dd20cc
DJ
23076}
23077
5c631832 23078/* Follow reference OFFSET.
673bfd45
DE
23079 On entry *REF_CU is the CU of the source die referencing OFFSET.
23080 On exit *REF_CU is the CU of the result.
23081 Returns NULL if OFFSET is invalid. */
f504f079 23082
f9aca02d 23083static struct die_info *
9c541725 23084follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23085 struct dwarf2_cu **ref_cu)
c906108c 23086{
10b3939b 23087 struct die_info temp_die;
f2f0e013 23088 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23089 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23090
348e048f
DE
23091 gdb_assert (cu->per_cu != NULL);
23092
98bfdba5
PA
23093 target_cu = cu;
23094
17e593e9
SM
23095 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23096 "source CU contains target offset: %d",
23097 sect_offset_str (cu->per_cu->sect_off),
23098 sect_offset_str (sect_off),
23099 cu->header.offset_in_cu_p (sect_off));
23100
3019eac3 23101 if (cu->per_cu->is_debug_types)
348e048f
DE
23102 {
23103 /* .debug_types CUs cannot reference anything outside their CU.
23104 If they need to, they have to reference a signatured type via
55f1336d 23105 DW_FORM_ref_sig8. */
4057dfde 23106 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23107 return NULL;
348e048f 23108 }
36586728 23109 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23110 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23111 {
23112 struct dwarf2_per_cu_data *per_cu;
9a619af0 23113
9c541725 23114 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23115 per_objfile);
03dd20cc 23116
17e593e9
SM
23117 dwarf_read_debug_printf_v ("target CU offset: %s, "
23118 "target CU DIEs loaded: %d",
23119 sect_offset_str (per_cu->sect_off),
23120 per_objfile->get_cu (per_cu) != nullptr);
23121
616c069a
SM
23122 /* If necessary, add it to the queue and load its DIEs.
23123
23124 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23125 it doesn't mean they are currently loaded. Since we require them
23126 to be loaded, we must check for ourselves. */
3e9f1ca1 23127 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->per_cu->lang)
616c069a 23128 || per_objfile->get_cu (per_cu) == nullptr)
4a636814 23129 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
3e9f1ca1 23130 false, cu->per_cu->lang);
03dd20cc 23131
976ca316 23132 target_cu = per_objfile->get_cu (per_cu);
616c069a 23133 gdb_assert (target_cu != nullptr);
10b3939b 23134 }
98bfdba5
PA
23135 else if (cu->dies == NULL)
23136 {
23137 /* We're loading full DIEs during partial symbol reading. */
976ca316 23138 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23139 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23140 language_minimal);
98bfdba5 23141 }
c906108c 23142
f2f0e013 23143 *ref_cu = target_cu;
9c541725 23144 temp_die.sect_off = sect_off;
c24bdb02 23145
9a3c8263 23146 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23147 &temp_die,
23148 to_underlying (sect_off));
5c631832 23149}
10b3939b 23150
5c631832
JK
23151/* Follow reference attribute ATTR of SRC_DIE.
23152 On entry *REF_CU is the CU of SRC_DIE.
23153 On exit *REF_CU is the CU of the result. */
23154
23155static struct die_info *
ff39bb5e 23156follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23157 struct dwarf2_cu **ref_cu)
23158{
0826b30a 23159 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23160 struct dwarf2_cu *cu = *ref_cu;
23161 struct die_info *die;
23162
9c541725 23163 die = follow_die_offset (sect_off,
36586728
TT
23164 (attr->form == DW_FORM_GNU_ref_alt
23165 || cu->per_cu->is_dwz),
23166 ref_cu);
5c631832 23167 if (!die)
9d8780f0
SM
23168 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23169 "at %s [in module %s]"),
23170 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23171 objfile_name (cu->per_objfile->objfile));
348e048f 23172
5c631832
JK
23173 return die;
23174}
23175
d4c9a4f8 23176/* See read.h. */
5c631832
JK
23177
23178struct dwarf2_locexpr_baton
9c541725 23179dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23180 dwarf2_per_cu_data *per_cu,
976ca316 23181 dwarf2_per_objfile *per_objfile,
041d9819
SM
23182 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23183 bool resolve_abstract_p)
5c631832 23184{
5c631832
JK
23185 struct die_info *die;
23186 struct attribute *attr;
23187 struct dwarf2_locexpr_baton retval;
976ca316 23188 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23189
976ca316 23190 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23191 if (cu == nullptr)
976ca316 23192 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23193
23194 if (cu == nullptr)
cc12ce38
DE
23195 {
23196 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23197 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23198 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23199 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23200 }
918dd910 23201
9c541725 23202 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23203 if (!die)
9d8780f0
SM
23204 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23205 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23206
23207 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23208 if (!attr && resolve_abstract_p
976ca316
SM
23209 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23210 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23211 {
041d9819 23212 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23213 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23214 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23215
3360b6e7 23216 for (const auto &cand_off
976ca316 23217 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23218 {
3360b6e7
TV
23219 struct dwarf2_cu *cand_cu = cu;
23220 struct die_info *cand
23221 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23222 if (!cand
23223 || !cand->parent
e4a62c65
TV
23224 || cand->parent->tag != DW_TAG_subprogram)
23225 continue;
23226
23227 CORE_ADDR pc_low, pc_high;
23228 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23229 if (pc_low == ((CORE_ADDR) -1))
23230 continue;
23231 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23232 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23233 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23234 continue;
23235
23236 die = cand;
23237 attr = dwarf2_attr (die, DW_AT_location, cu);
23238 break;
23239 }
23240 }
23241
5c631832
JK
23242 if (!attr)
23243 {
e103e986
JK
23244 /* DWARF: "If there is no such attribute, then there is no effect.".
23245 DATA is ignored if SIZE is 0. */
5c631832 23246
e103e986 23247 retval.data = NULL;
5c631832
JK
23248 retval.size = 0;
23249 }
cd6c91b4 23250 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23251 {
23252 struct dwarf2_loclist_baton loclist_baton;
041d9819 23253 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23254 size_t size;
23255
23256 fill_in_loclist_baton (cu, &loclist_baton, attr);
23257
23258 retval.data = dwarf2_find_location_expression (&loclist_baton,
23259 &size, pc);
23260 retval.size = size;
23261 }
5c631832
JK
23262 else
23263 {
4fc6c0d5 23264 if (!attr->form_is_block ())
9d8780f0 23265 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23266 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23267 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23268
9d2246fc
TT
23269 struct dwarf_block *block = attr->as_block ();
23270 retval.data = block->data;
23271 retval.size = block->size;
5c631832 23272 }
976ca316 23273 retval.per_objfile = per_objfile;
5c631832 23274 retval.per_cu = cu->per_cu;
918dd910 23275
976ca316 23276 per_objfile->age_comp_units ();
918dd910 23277
5c631832 23278 return retval;
348e048f
DE
23279}
23280
d4c9a4f8 23281/* See read.h. */
8b9737bf
TT
23282
23283struct dwarf2_locexpr_baton
23284dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23285 dwarf2_per_cu_data *per_cu,
14095eb3 23286 dwarf2_per_objfile *per_objfile,
041d9819 23287 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23288{
9c541725 23289 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23290
14095eb3 23291 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23292 get_frame_pc);
8b9737bf
TT
23293}
23294
b6807d98
TT
23295/* Write a constant of a given type as target-ordered bytes into
23296 OBSTACK. */
23297
23298static const gdb_byte *
23299write_constant_as_bytes (struct obstack *obstack,
23300 enum bfd_endian byte_order,
23301 struct type *type,
23302 ULONGEST value,
23303 LONGEST *len)
23304{
23305 gdb_byte *result;
23306
23307 *len = TYPE_LENGTH (type);
224c3ddb 23308 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23309 store_unsigned_integer (result, *len, byte_order, value);
23310
23311 return result;
23312}
23313
d4c9a4f8 23314/* See read.h. */
b6807d98
TT
23315
23316const gdb_byte *
9c541725 23317dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23318 dwarf2_per_cu_data *per_cu,
14095eb3 23319 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23320 obstack *obstack,
b6807d98
TT
23321 LONGEST *len)
23322{
b6807d98
TT
23323 struct die_info *die;
23324 struct attribute *attr;
23325 const gdb_byte *result = NULL;
23326 struct type *type;
23327 LONGEST value;
23328 enum bfd_endian byte_order;
14095eb3 23329 struct objfile *objfile = per_objfile->objfile;
b6807d98 23330
7188ed02 23331 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23332 if (cu == nullptr)
23333 cu = load_cu (per_cu, per_objfile, false);
23334
23335 if (cu == nullptr)
cc12ce38
DE
23336 {
23337 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23338 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23339 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23340 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23341 }
b6807d98 23342
9c541725 23343 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23344 if (!die)
9d8780f0
SM
23345 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23346 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23347
23348 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23349 if (attr == NULL)
23350 return NULL;
23351
e3b94546 23352 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23353 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23354
23355 switch (attr->form)
23356 {
23357 case DW_FORM_addr:
336d760d 23358 case DW_FORM_addrx:
b6807d98
TT
23359 case DW_FORM_GNU_addr_index:
23360 {
23361 gdb_byte *tem;
23362
23363 *len = cu->header.addr_size;
224c3ddb 23364 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23365 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23366 result = tem;
23367 }
23368 break;
23369 case DW_FORM_string:
23370 case DW_FORM_strp:
cf532bd1 23371 case DW_FORM_strx:
b6807d98
TT
23372 case DW_FORM_GNU_str_index:
23373 case DW_FORM_GNU_strp_alt:
c6481205 23374 /* The string is already allocated on the objfile obstack, point
b6807d98 23375 directly to it. */
2c830f54
TT
23376 {
23377 const char *attr_name = attr->as_string ();
23378 result = (const gdb_byte *) attr_name;
23379 *len = strlen (attr_name);
23380 }
b6807d98
TT
23381 break;
23382 case DW_FORM_block1:
23383 case DW_FORM_block2:
23384 case DW_FORM_block4:
23385 case DW_FORM_block:
23386 case DW_FORM_exprloc:
0224619f 23387 case DW_FORM_data16:
9d2246fc
TT
23388 {
23389 struct dwarf_block *block = attr->as_block ();
23390 result = block->data;
23391 *len = block->size;
23392 }
b6807d98
TT
23393 break;
23394
23395 /* The DW_AT_const_value attributes are supposed to carry the
23396 symbol's value "represented as it would be on the target
23397 architecture." By the time we get here, it's already been
23398 converted to host endianness, so we just need to sign- or
23399 zero-extend it as appropriate. */
23400 case DW_FORM_data1:
23401 type = die_type (die, cu);
23402 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23403 if (result == NULL)
23404 result = write_constant_as_bytes (obstack, byte_order,
23405 type, value, len);
23406 break;
23407 case DW_FORM_data2:
23408 type = die_type (die, cu);
23409 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23410 if (result == NULL)
23411 result = write_constant_as_bytes (obstack, byte_order,
23412 type, value, len);
23413 break;
23414 case DW_FORM_data4:
23415 type = die_type (die, cu);
23416 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23417 if (result == NULL)
23418 result = write_constant_as_bytes (obstack, byte_order,
23419 type, value, len);
23420 break;
23421 case DW_FORM_data8:
23422 type = die_type (die, cu);
23423 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23424 if (result == NULL)
23425 result = write_constant_as_bytes (obstack, byte_order,
23426 type, value, len);
23427 break;
23428
23429 case DW_FORM_sdata:
663c44ac 23430 case DW_FORM_implicit_const:
b6807d98
TT
23431 type = die_type (die, cu);
23432 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23433 type, attr->as_signed (), len);
b6807d98
TT
23434 break;
23435
23436 case DW_FORM_udata:
23437 type = die_type (die, cu);
23438 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23439 type, attr->as_unsigned (), len);
b6807d98
TT
23440 break;
23441
23442 default:
b98664d3 23443 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23444 dwarf_form_name (attr->form));
23445 break;
23446 }
23447
23448 return result;
23449}
23450
d4c9a4f8 23451/* See read.h. */
7942e96e
AA
23452
23453struct type *
9c541725 23454dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3 23455 dwarf2_per_cu_data *per_cu,
386de171
TT
23456 dwarf2_per_objfile *per_objfile,
23457 const char **var_name)
7942e96e 23458{
7942e96e
AA
23459 struct die_info *die;
23460
7188ed02 23461 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23462 if (cu == nullptr)
23463 cu = load_cu (per_cu, per_objfile, false);
23464
23465 if (cu == nullptr)
23466 return nullptr;
7942e96e 23467
9c541725 23468 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23469 if (!die)
23470 return NULL;
23471
386de171
TT
23472 if (var_name != nullptr)
23473 *var_name = var_decl_name (die, cu);
7942e96e
AA
23474 return die_type (die, cu);
23475}
23476
8cb5117c 23477/* See read.h. */
8a9b8146
TT
23478
23479struct type *
b64f50a1 23480dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23481 dwarf2_per_cu_data *per_cu,
23482 dwarf2_per_objfile *per_objfile)
8a9b8146 23483{
9c541725 23484 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23485 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23486}
23487
ac9ec31b 23488/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23489 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23490 On exit *REF_CU is the CU of the result.
23491 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23492
23493static struct die_info *
ac9ec31b
DE
23494follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23495 struct dwarf2_cu **ref_cu)
348e048f 23496{
348e048f 23497 struct die_info temp_die;
8457e5ec 23498 struct dwarf2_cu *sig_cu;
348e048f 23499 struct die_info *die;
976ca316 23500 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23501
348e048f 23502
ac9ec31b
DE
23503 /* While it might be nice to assert sig_type->type == NULL here,
23504 we can get here for DW_AT_imported_declaration where we need
23505 the DIE not the type. */
348e048f 23506
616c069a 23507 /* If necessary, add it to the queue and load its DIEs.
348e048f 23508
616c069a
SM
23509 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23510 it doesn't mean they are currently loaded. Since we require them
23511 to be loaded, we must check for ourselves. */
0d305d5c 23512 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
616c069a 23513 language_minimal)
0d305d5c 23514 || per_objfile->get_cu (sig_type) == nullptr)
976ca316 23515 read_signatured_type (sig_type, per_objfile);
348e048f 23516
0d305d5c 23517 sig_cu = per_objfile->get_cu (sig_type);
69d751e3 23518 gdb_assert (sig_cu != NULL);
9c541725
PA
23519 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23520 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23521 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23522 to_underlying (temp_die.sect_off));
348e048f
DE
23523 if (die)
23524 {
796a7ff8
DE
23525 /* For .gdb_index version 7 keep track of included TUs.
23526 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
23527 if (per_objfile->per_bfd->index_table != NULL
23528 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 23529 {
ae640021 23530 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23531 }
23532
348e048f
DE
23533 *ref_cu = sig_cu;
23534 return die;
23535 }
23536
ac9ec31b
DE
23537 return NULL;
23538}
23539
23540/* Follow signatured type referenced by ATTR in SRC_DIE.
23541 On entry *REF_CU is the CU of SRC_DIE.
23542 On exit *REF_CU is the CU of the result.
23543 The result is the DIE of the type.
23544 If the referenced type cannot be found an error is thrown. */
23545
23546static struct die_info *
ff39bb5e 23547follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23548 struct dwarf2_cu **ref_cu)
23549{
630ed6b9 23550 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
23551 struct signatured_type *sig_type;
23552 struct die_info *die;
23553
23554 gdb_assert (attr->form == DW_FORM_ref_sig8);
23555
a2ce51a0 23556 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23557 /* sig_type will be NULL if the signatured type is missing from
23558 the debug info. */
23559 if (sig_type == NULL)
23560 {
23561 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
23562 " from DIE at %s [in module %s]"),
23563 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23564 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23565 }
23566
23567 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23568 if (die == NULL)
23569 {
23570 dump_die_for_error (src_die);
23571 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23572 " from DIE at %s [in module %s]"),
23573 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23574 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23575 }
23576
23577 return die;
23578}
23579
23580/* Get the type specified by SIGNATURE referenced in DIE/CU,
23581 reading in and processing the type unit if necessary. */
23582
23583static struct type *
23584get_signatured_type (struct die_info *die, ULONGEST signature,
23585 struct dwarf2_cu *cu)
23586{
976ca316 23587 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
23588 struct signatured_type *sig_type;
23589 struct dwarf2_cu *type_cu;
23590 struct die_info *type_die;
23591 struct type *type;
23592
a2ce51a0 23593 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23594 /* sig_type will be NULL if the signatured type is missing from
23595 the debug info. */
23596 if (sig_type == NULL)
23597 {
b98664d3 23598 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23599 " from DIE at %s [in module %s]"),
23600 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23601 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23602 return build_error_marker_type (cu, die);
23603 }
23604
23605 /* If we already know the type we're done. */
976ca316 23606 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
23607 if (type != nullptr)
23608 return type;
ac9ec31b
DE
23609
23610 type_cu = cu;
23611 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23612 if (type_die != NULL)
23613 {
23614 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23615 is created. This is important, for example, because for c++ classes
23616 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23617 type = read_type_die (type_die, type_cu);
23618 if (type == NULL)
23619 {
b98664d3 23620 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23621 " referenced from DIE at %s [in module %s]"),
23622 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23623 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23624 type = build_error_marker_type (cu, die);
23625 }
23626 }
23627 else
23628 {
b98664d3 23629 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23630 " from DIE at %s [in module %s]"),
23631 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23632 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23633 type = build_error_marker_type (cu, die);
23634 }
e286671b 23635
976ca316 23636 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
23637
23638 return type;
23639}
23640
23641/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23642 reading in and processing the type unit if necessary. */
23643
23644static struct type *
ff39bb5e 23645get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23646 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23647{
23648 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 23649 if (attr->form_is_ref ())
ac9ec31b
DE
23650 {
23651 struct dwarf2_cu *type_cu = cu;
23652 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23653
23654 return read_type_die (type_die, type_cu);
23655 }
23656 else if (attr->form == DW_FORM_ref_sig8)
23657 {
630ed6b9 23658 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
23659 }
23660 else
23661 {
976ca316 23662 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23663
b98664d3 23664 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23665 " at %s [in module %s]"),
23666 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 23667 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23668 return build_error_marker_type (cu, die);
23669 }
348e048f
DE
23670}
23671
e5fe5e75 23672/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23673
23674static void
ab432490
SM
23675load_full_type_unit (dwarf2_per_cu_data *per_cu,
23676 dwarf2_per_objfile *per_objfile)
348e048f 23677{
52dc124a 23678 struct signatured_type *sig_type;
348e048f 23679
f4dc4d17 23680 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 23681 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 23682
6721b2ec
DE
23683 /* We have the per_cu, but we need the signatured_type.
23684 Fortunately this is an easy translation. */
23685 gdb_assert (per_cu->is_debug_types);
23686 sig_type = (struct signatured_type *) per_cu;
348e048f 23687
7188ed02 23688 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 23689
ab432490 23690 read_signatured_type (sig_type, per_objfile);
348e048f 23691
7188ed02 23692 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
23693}
23694
3019eac3
DE
23695/* Read in a signatured type and build its CU and DIEs.
23696 If the type is a stub for the real type in a DWO file,
23697 read in the real type from the DWO file as well. */
dee91e82
DE
23698
23699static void
ab432490
SM
23700read_signatured_type (signatured_type *sig_type,
23701 dwarf2_per_objfile *per_objfile)
dee91e82 23702{
0d305d5c
TT
23703 gdb_assert (sig_type->is_debug_types);
23704 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
348e048f 23705
0d305d5c 23706 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
23707
23708 if (!reader.dummy_p)
23709 {
23710 struct dwarf2_cu *cu = reader.cu;
23711 const gdb_byte *info_ptr = reader.info_ptr;
23712
23713 gdb_assert (cu->die_hash == NULL);
23714 cu->die_hash =
23715 htab_create_alloc_ex (cu->header.length / 12,
23716 die_hash,
23717 die_eq,
23718 NULL,
23719 &cu->comp_unit_obstack,
23720 hashtab_obstack_allocate,
23721 dummy_obstack_deallocate);
23722
3e225074 23723 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
23724 reader.comp_unit_die->child
23725 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23726 reader.comp_unit_die);
23727 cu->dies = reader.comp_unit_die;
23728 /* comp_unit_die is not stored in die_hash, no need. */
23729
23730 /* We try not to read any attributes in this function, because
23731 not all CUs needed for references have been loaded yet, and
23732 symbol table processing isn't initialized. But we have to
23733 set the CU language, or we won't be able to build types
23734 correctly. Similarly, if we do not read the producer, we can
23735 not apply producer-specific interpretation. */
23736 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
23737
23738 reader.keep ();
c0ab21c2
TT
23739 }
23740
0d305d5c 23741 sig_type->tu_read = 1;
c906108c
SS
23742}
23743
c906108c
SS
23744/* Decode simple location descriptions.
23745 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
23746 the location and return the value. If COMPUTED is non-null, it is
23747 set to true to indicate that decoding was successful, and false
23748 otherwise. If COMPUTED is null, then this function may emit a
23749 complaint. */
c906108c
SS
23750
23751static CORE_ADDR
7d79de9a 23752decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 23753{
5e22e966 23754 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
23755 size_t i;
23756 size_t size = blk->size;
d521ce57 23757 const gdb_byte *data = blk->data;
21ae7a4d
JK
23758 CORE_ADDR stack[64];
23759 int stacki;
23760 unsigned int bytes_read, unsnd;
23761 gdb_byte op;
c906108c 23762
7d79de9a
TT
23763 if (computed != nullptr)
23764 *computed = false;
23765
21ae7a4d
JK
23766 i = 0;
23767 stacki = 0;
23768 stack[stacki] = 0;
23769 stack[++stacki] = 0;
23770
23771 while (i < size)
23772 {
23773 op = data[i++];
23774 switch (op)
23775 {
23776 case DW_OP_lit0:
23777 case DW_OP_lit1:
23778 case DW_OP_lit2:
23779 case DW_OP_lit3:
23780 case DW_OP_lit4:
23781 case DW_OP_lit5:
23782 case DW_OP_lit6:
23783 case DW_OP_lit7:
23784 case DW_OP_lit8:
23785 case DW_OP_lit9:
23786 case DW_OP_lit10:
23787 case DW_OP_lit11:
23788 case DW_OP_lit12:
23789 case DW_OP_lit13:
23790 case DW_OP_lit14:
23791 case DW_OP_lit15:
23792 case DW_OP_lit16:
23793 case DW_OP_lit17:
23794 case DW_OP_lit18:
23795 case DW_OP_lit19:
23796 case DW_OP_lit20:
23797 case DW_OP_lit21:
23798 case DW_OP_lit22:
23799 case DW_OP_lit23:
23800 case DW_OP_lit24:
23801 case DW_OP_lit25:
23802 case DW_OP_lit26:
23803 case DW_OP_lit27:
23804 case DW_OP_lit28:
23805 case DW_OP_lit29:
23806 case DW_OP_lit30:
23807 case DW_OP_lit31:
23808 stack[++stacki] = op - DW_OP_lit0;
23809 break;
f1bea926 23810
21ae7a4d
JK
23811 case DW_OP_reg0:
23812 case DW_OP_reg1:
23813 case DW_OP_reg2:
23814 case DW_OP_reg3:
23815 case DW_OP_reg4:
23816 case DW_OP_reg5:
23817 case DW_OP_reg6:
23818 case DW_OP_reg7:
23819 case DW_OP_reg8:
23820 case DW_OP_reg9:
23821 case DW_OP_reg10:
23822 case DW_OP_reg11:
23823 case DW_OP_reg12:
23824 case DW_OP_reg13:
23825 case DW_OP_reg14:
23826 case DW_OP_reg15:
23827 case DW_OP_reg16:
23828 case DW_OP_reg17:
23829 case DW_OP_reg18:
23830 case DW_OP_reg19:
23831 case DW_OP_reg20:
23832 case DW_OP_reg21:
23833 case DW_OP_reg22:
23834 case DW_OP_reg23:
23835 case DW_OP_reg24:
23836 case DW_OP_reg25:
23837 case DW_OP_reg26:
23838 case DW_OP_reg27:
23839 case DW_OP_reg28:
23840 case DW_OP_reg29:
23841 case DW_OP_reg30:
23842 case DW_OP_reg31:
23843 stack[++stacki] = op - DW_OP_reg0;
23844 if (i < size)
7d79de9a
TT
23845 {
23846 if (computed == nullptr)
23847 dwarf2_complex_location_expr_complaint ();
23848 else
23849 return 0;
23850 }
21ae7a4d 23851 break;
c906108c 23852
21ae7a4d
JK
23853 case DW_OP_regx:
23854 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23855 i += bytes_read;
23856 stack[++stacki] = unsnd;
23857 if (i < size)
7d79de9a
TT
23858 {
23859 if (computed == nullptr)
23860 dwarf2_complex_location_expr_complaint ();
23861 else
23862 return 0;
23863 }
21ae7a4d 23864 break;
c906108c 23865
21ae7a4d 23866 case DW_OP_addr:
c8a7a66f
TT
23867 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23868 &bytes_read);
21ae7a4d
JK
23869 i += bytes_read;
23870 break;
d53d4ac5 23871
21ae7a4d
JK
23872 case DW_OP_const1u:
23873 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23874 i += 1;
23875 break;
23876
23877 case DW_OP_const1s:
23878 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23879 i += 1;
23880 break;
23881
23882 case DW_OP_const2u:
23883 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23884 i += 2;
23885 break;
23886
23887 case DW_OP_const2s:
23888 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23889 i += 2;
23890 break;
d53d4ac5 23891
21ae7a4d
JK
23892 case DW_OP_const4u:
23893 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23894 i += 4;
23895 break;
23896
23897 case DW_OP_const4s:
23898 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23899 i += 4;
23900 break;
23901
585861ea
JK
23902 case DW_OP_const8u:
23903 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23904 i += 8;
23905 break;
23906
21ae7a4d
JK
23907 case DW_OP_constu:
23908 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23909 &bytes_read);
23910 i += bytes_read;
23911 break;
23912
23913 case DW_OP_consts:
23914 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23915 i += bytes_read;
23916 break;
23917
23918 case DW_OP_dup:
23919 stack[stacki + 1] = stack[stacki];
23920 stacki++;
23921 break;
23922
23923 case DW_OP_plus:
23924 stack[stacki - 1] += stack[stacki];
23925 stacki--;
23926 break;
23927
23928 case DW_OP_plus_uconst:
23929 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23930 &bytes_read);
23931 i += bytes_read;
23932 break;
23933
23934 case DW_OP_minus:
23935 stack[stacki - 1] -= stack[stacki];
23936 stacki--;
23937 break;
23938
23939 case DW_OP_deref:
23940 /* If we're not the last op, then we definitely can't encode
23941 this using GDB's address_class enum. This is valid for partial
23942 global symbols, although the variable's address will be bogus
23943 in the psymtab. */
23944 if (i < size)
7d79de9a
TT
23945 {
23946 if (computed == nullptr)
23947 dwarf2_complex_location_expr_complaint ();
23948 else
23949 return 0;
23950 }
21ae7a4d
JK
23951 break;
23952
dda83cd7 23953 case DW_OP_GNU_push_tls_address:
4aa4e28b 23954 case DW_OP_form_tls_address:
21ae7a4d
JK
23955 /* The top of the stack has the offset from the beginning
23956 of the thread control block at which the variable is located. */
23957 /* Nothing should follow this operator, so the top of stack would
23958 be returned. */
23959 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23960 address will be bogus in the psymtab. Make it always at least
23961 non-zero to not look as a variable garbage collected by linker
23962 which have DW_OP_addr 0. */
21ae7a4d 23963 if (i < size)
7d79de9a
TT
23964 {
23965 if (computed == nullptr)
23966 dwarf2_complex_location_expr_complaint ();
23967 else
23968 return 0;
23969 }
585861ea 23970 stack[stacki]++;
dda83cd7 23971 break;
21ae7a4d
JK
23972
23973 case DW_OP_GNU_uninit:
7d79de9a
TT
23974 if (computed != nullptr)
23975 return 0;
21ae7a4d
JK
23976 break;
23977
336d760d 23978 case DW_OP_addrx:
3019eac3 23979 case DW_OP_GNU_addr_index:
49f6c839 23980 case DW_OP_GNU_const_index:
3019eac3
DE
23981 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23982 &bytes_read);
23983 i += bytes_read;
23984 break;
23985
21ae7a4d 23986 default:
7d79de9a
TT
23987 if (computed == nullptr)
23988 {
23989 const char *name = get_DW_OP_name (op);
21ae7a4d 23990
7d79de9a
TT
23991 if (name)
23992 complaint (_("unsupported stack op: '%s'"),
23993 name);
23994 else
23995 complaint (_("unsupported stack op: '%02x'"),
23996 op);
23997 }
21ae7a4d
JK
23998
23999 return (stack[stacki]);
d53d4ac5 24000 }
3c6e0cb3 24001
21ae7a4d 24002 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24003 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24004 if (stacki >= ARRAY_SIZE (stack) - 1)
24005 {
7d79de9a
TT
24006 if (computed == nullptr)
24007 complaint (_("location description stack overflow"));
21ae7a4d
JK
24008 return 0;
24009 }
24010
24011 if (stacki <= 0)
24012 {
7d79de9a
TT
24013 if (computed == nullptr)
24014 complaint (_("location description stack underflow"));
21ae7a4d
JK
24015 return 0;
24016 }
24017 }
7d79de9a
TT
24018
24019 if (computed != nullptr)
24020 *computed = true;
21ae7a4d 24021 return (stack[stacki]);
c906108c
SS
24022}
24023
24024/* memory allocation interface */
24025
c906108c 24026static struct dwarf_block *
7b5a2f43 24027dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24028{
8d749320 24029 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24030}
24031
c906108c 24032static struct die_info *
b60c80d6 24033dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24034{
24035 struct die_info *die;
b60c80d6
DJ
24036 size_t size = sizeof (struct die_info);
24037
24038 if (num_attrs > 1)
24039 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24040
b60c80d6 24041 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24042 memset (die, 0, sizeof (struct die_info));
24043 return (die);
24044}
2e276125
JB
24045
24046\f
a036ba48 24047
c90ec28a 24048/* Macro support. */
cf2c3c16 24049
9eac9650
TT
24050/* An overload of dwarf_decode_macros that finds the correct section
24051 and ensures it is read in before calling the other overload. */
24052
24053static void
24054dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24055 int section_is_gnu)
24056{
976ca316
SM
24057 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24058 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24059 const struct line_header *lh = cu->line_header;
9eac9650
TT
24060 unsigned int offset_size = cu->header.offset_size;
24061 struct dwarf2_section_info *section;
24062 const char *section_name;
24063
24064 if (cu->dwo_unit != nullptr)
24065 {
24066 if (section_is_gnu)
24067 {
24068 section = &cu->dwo_unit->dwo_file->sections.macro;
24069 section_name = ".debug_macro.dwo";
24070 }
24071 else
24072 {
24073 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24074 section_name = ".debug_macinfo.dwo";
24075 }
24076 }
24077 else
24078 {
24079 if (section_is_gnu)
24080 {
976ca316 24081 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24082 section_name = ".debug_macro";
24083 }
24084 else
24085 {
976ca316 24086 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24087 section_name = ".debug_macinfo";
24088 }
24089 }
24090
24091 section->read (objfile);
24092 if (section->buffer == nullptr)
24093 {
24094 complaint (_("missing %s section"), section_name);
24095 return;
24096 }
24097
24098 buildsym_compunit *builder = cu->get_builder ();
24099
048fde1e 24100 struct dwarf2_section_info *str_offsets_section;
24101 struct dwarf2_section_info *str_section;
24102 ULONGEST str_offsets_base;
24103
24104 if (cu->dwo_unit != nullptr)
24105 {
24106 str_offsets_section = &cu->dwo_unit->dwo_file
24107 ->sections.str_offsets;
24108 str_section = &cu->dwo_unit->dwo_file->sections.str;
24109 str_offsets_base = cu->header.addr_size;
24110 }
24111 else
24112 {
24113 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24114 str_section = &per_objfile->per_bfd->str;
24115 str_offsets_base = *cu->str_offsets_base;
24116 }
24117
976ca316 24118 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24119 offset_size, offset, str_section, str_offsets_section,
24120 str_offsets_base, section_is_gnu);
9eac9650
TT
24121}
24122
3019eac3
DE
24123/* Return the .debug_loc section to use for CU.
24124 For DWO files use .debug_loc.dwo. */
24125
24126static struct dwarf2_section_info *
24127cu_debug_loc_section (struct dwarf2_cu *cu)
24128{
976ca316 24129 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24130
3019eac3 24131 if (cu->dwo_unit)
43988095
JK
24132 {
24133 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24134
43988095
JK
24135 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24136 }
976ca316
SM
24137 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24138 : &per_objfile->per_bfd->loc);
3019eac3
DE
24139}
24140
d0ce17d8
CT
24141/* Return the .debug_rnglists section to use for CU. */
24142static struct dwarf2_section_info *
24143cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24144{
24145 if (cu->header.version < 5)
24146 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24147 cu->header.version);
24148 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24149
24150 /* Make sure we read the .debug_rnglists section from the file that
24151 contains the DW_AT_ranges attribute we are reading. Normally that
24152 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24153 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24154 program. */
24155 if (cu->dwo_unit != nullptr
24156 && tag != DW_TAG_compile_unit
24157 && tag != DW_TAG_skeleton_unit)
24158 {
24159 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24160
24161 if (sections->rnglists.size > 0)
24162 return &sections->rnglists;
24163 else
24164 error (_(".debug_rnglists section is missing from .dwo file."));
24165 }
24166 return &dwarf2_per_objfile->per_bfd->rnglists;
24167}
24168
8cf6f0b1
TT
24169/* A helper function that fills in a dwarf2_loclist_baton. */
24170
24171static void
24172fill_in_loclist_baton (struct dwarf2_cu *cu,
24173 struct dwarf2_loclist_baton *baton,
ff39bb5e 24174 const struct attribute *attr)
8cf6f0b1 24175{
976ca316 24176 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24177 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24178
976ca316 24179 section->read (per_objfile->objfile);
8cf6f0b1 24180
976ca316 24181 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24182 baton->per_cu = cu->per_cu;
24183 gdb_assert (baton->per_cu);
24184 /* We don't know how long the location list is, but make sure we
24185 don't run off the edge of the section. */
d4df075e
TT
24186 baton->size = section->size - attr->as_unsigned ();
24187 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24188 if (cu->base_address.has_value ())
24189 baton->base_address = *cu->base_address;
24190 else
24191 baton->base_address = 0;
f664829e 24192 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24193}
24194
4c2df51b 24195static void
ff39bb5e 24196dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24197 struct dwarf2_cu *cu, int is_block)
4c2df51b 24198{
976ca316
SM
24199 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24200 struct objfile *objfile = per_objfile->objfile;
3019eac3 24201 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24202
cd6c91b4 24203 if (attr->form_is_section_offset ()
3019eac3 24204 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24205 the section. If so, fall through to the complaint in the
24206 other branch. */
d4df075e 24207 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24208 {
0d53c4c4 24209 struct dwarf2_loclist_baton *baton;
4c2df51b 24210
8d749320 24211 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24212
8cf6f0b1 24213 fill_in_loclist_baton (cu, baton, attr);
be391dca 24214
2b24b6e4 24215 if (!cu->base_address.has_value ())
b98664d3 24216 complaint (_("Location list used without "
3e43a32a 24217 "specifying the CU base address."));
4c2df51b 24218
f1e6e072
TT
24219 SYMBOL_ACLASS_INDEX (sym) = (is_block
24220 ? dwarf2_loclist_block_index
24221 : dwarf2_loclist_index);
0d53c4c4
DJ
24222 SYMBOL_LOCATION_BATON (sym) = baton;
24223 }
24224 else
24225 {
24226 struct dwarf2_locexpr_baton *baton;
24227
8d749320 24228 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24229 baton->per_objfile = per_objfile;
ae0d2f24
UW
24230 baton->per_cu = cu->per_cu;
24231 gdb_assert (baton->per_cu);
0d53c4c4 24232
4fc6c0d5 24233 if (attr->form_is_block ())
0d53c4c4
DJ
24234 {
24235 /* Note that we're just copying the block's data pointer
24236 here, not the actual data. We're still pointing into the
6502dd73
DJ
24237 info_buffer for SYM's objfile; right now we never release
24238 that buffer, but when we do clean up properly this may
24239 need to change. */
9d2246fc
TT
24240 struct dwarf_block *block = attr->as_block ();
24241 baton->size = block->size;
24242 baton->data = block->data;
0d53c4c4
DJ
24243 }
24244 else
24245 {
24246 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24247 sym->natural_name ());
0d53c4c4 24248 baton->size = 0;
0d53c4c4 24249 }
6e70227d 24250
f1e6e072
TT
24251 SYMBOL_ACLASS_INDEX (sym) = (is_block
24252 ? dwarf2_locexpr_block_index
24253 : dwarf2_locexpr_index);
0d53c4c4
DJ
24254 SYMBOL_LOCATION_BATON (sym) = baton;
24255 }
4c2df51b 24256}
6502dd73 24257
2e6a9f79 24258/* See read.h. */
96408a79 24259
2e6a9f79
SM
24260const comp_unit_head *
24261dwarf2_per_cu_data::get_header () const
96408a79 24262{
2e6a9f79
SM
24263 if (!m_header_read_in)
24264 {
24265 const gdb_byte *info_ptr
24266 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24267
2e6a9f79 24268 memset (&m_header, 0, sizeof (m_header));
96408a79 24269
2e6a9f79
SM
24270 read_comp_unit_head (&m_header, info_ptr, this->section,
24271 rcuh_kind::COMPILE);
a9f172c6
TT
24272
24273 m_header_read_in = true;
2e6a9f79 24274 }
96408a79 24275
2e6a9f79 24276 return &m_header;
96408a79
SA
24277}
24278
09ba997f 24279/* See read.h. */
ae0d2f24 24280
98714339 24281int
09ba997f 24282dwarf2_per_cu_data::addr_size () const
ae0d2f24 24283{
2e6a9f79 24284 return this->get_header ()->addr_size;
ae0d2f24
UW
24285}
24286
09ba997f 24287/* See read.h. */
9eae7c52
TT
24288
24289int
09ba997f 24290dwarf2_per_cu_data::offset_size () const
9eae7c52 24291{
2e6a9f79 24292 return this->get_header ()->offset_size;
96408a79
SA
24293}
24294
09ba997f 24295/* See read.h. */
96408a79
SA
24296
24297int
09ba997f 24298dwarf2_per_cu_data::ref_addr_size () const
96408a79 24299{
2e6a9f79 24300 const comp_unit_head *header = this->get_header ();
96408a79 24301
2e6a9f79
SM
24302 if (header->version == 2)
24303 return header->addr_size;
96408a79 24304 else
2e6a9f79 24305 return header->offset_size;
181cebd4
JK
24306}
24307
22b6cd70
TT
24308/* A helper function for dwarf2_find_containing_comp_unit that returns
24309 the index of the result, and that searches a vector. It will
24310 return a result even if the offset in question does not actually
24311 occur in any CU. This is separate so that it can be unit
24312 tested. */
ae038cb0 24313
22b6cd70
TT
24314static int
24315dwarf2_find_containing_comp_unit
24316 (sect_offset sect_off,
24317 unsigned int offset_in_dwz,
473ab964 24318 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
ae038cb0 24319{
ae038cb0
DJ
24320 int low, high;
24321
ae038cb0 24322 low = 0;
22b6cd70 24323 high = all_comp_units.size () - 1;
ae038cb0
DJ
24324 while (high > low)
24325 {
36586728 24326 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24327 int mid = low + (high - low) / 2;
9a619af0 24328
0d305d5c 24329 mid_cu = all_comp_units[mid].get ();
36586728 24330 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24331 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24332 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24333 high = mid;
24334 else
24335 low = mid + 1;
24336 }
24337 gdb_assert (low == high);
22b6cd70
TT
24338 return low;
24339}
24340
24341/* Locate the .debug_info compilation unit from CU's objfile which contains
24342 the DIE at OFFSET. Raises an error on failure. */
24343
24344static struct dwarf2_per_cu_data *
24345dwarf2_find_containing_comp_unit (sect_offset sect_off,
24346 unsigned int offset_in_dwz,
976ca316 24347 dwarf2_per_objfile *per_objfile)
22b6cd70 24348{
976ca316
SM
24349 int low = dwarf2_find_containing_comp_unit
24350 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
0d305d5c
TT
24351 dwarf2_per_cu_data *this_cu
24352 = per_objfile->per_bfd->all_comp_units[low].get ();
22b6cd70 24353
45b8ae0c 24354 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24355 {
36586728 24356 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24357 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24358 "offset %s [in module %s]"),
24359 sect_offset_str (sect_off),
976ca316 24360 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24361
976ca316 24362 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24363 <= sect_off);
0d305d5c 24364 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
ae038cb0
DJ
24365 }
24366 else
24367 {
976ca316 24368 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24369 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24370 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24371 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24372 return this_cu;
24373 }
24374}
24375
22b6cd70
TT
24376#if GDB_SELF_TEST
24377
24378namespace selftests {
24379namespace find_containing_comp_unit {
24380
24381static void
24382run_test ()
24383{
473ab964 24384 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
0d305d5c 24385 dwarf2_per_cu_data *one_ptr = one.get ();
473ab964 24386 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
0d305d5c 24387 dwarf2_per_cu_data *two_ptr = two.get ();
473ab964 24388 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
0d305d5c 24389 dwarf2_per_cu_data *three_ptr = three.get ();
473ab964 24390 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
0d305d5c
TT
24391 dwarf2_per_cu_data *four_ptr = four.get ();
24392
24393 one->length = 5;
24394 two->sect_off = sect_offset (one->length);
24395 two->length = 7;
24396
24397 three->length = 5;
24398 three->is_dwz = 1;
24399 four->sect_off = sect_offset (three->length);
24400 four->length = 7;
24401 four->is_dwz = 1;
24402
473ab964 24403 std::vector<dwarf2_per_cu_data_up> units;
0d305d5c
TT
24404 units.push_back (std::move (one));
24405 units.push_back (std::move (two));
24406 units.push_back (std::move (three));
24407 units.push_back (std::move (four));
22b6cd70
TT
24408
24409 int result;
24410
24411 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
0d305d5c 24412 SELF_CHECK (units[result].get () == one_ptr);
22b6cd70 24413 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
0d305d5c 24414 SELF_CHECK (units[result].get () == one_ptr);
22b6cd70 24415 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
0d305d5c 24416 SELF_CHECK (units[result].get () == two_ptr);
22b6cd70
TT
24417
24418 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
0d305d5c 24419 SELF_CHECK (units[result].get () == three_ptr);
22b6cd70 24420 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
0d305d5c 24421 SELF_CHECK (units[result].get () == three_ptr);
22b6cd70 24422 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
0d305d5c 24423 SELF_CHECK (units[result].get () == four_ptr);
22b6cd70
TT
24424}
24425
24426}
24427}
24428
24429#endif /* GDB_SELF_TEST */
24430
9816fde3
JK
24431/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24432
24433static void
95554aad
TT
24434prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24435 enum language pretend_language)
9816fde3
JK
24436{
24437 struct attribute *attr;
24438
bf1dcdb3
TT
24439 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24440
9816fde3
JK
24441 /* Set the language we're debugging. */
24442 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
bf1dcdb3
TT
24443 if (cu->producer != nullptr
24444 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9cded63f 24445 {
bf1dcdb3
TT
24446 /* The XLCL doesn't generate DW_LANG_OpenCL because this
24447 attribute is not standardised yet. As a workaround for the
24448 language detection we fall back to the DW_AT_producer
24449 string. */
3e9f1ca1 24450 cu->per_cu->lang = language_opencl;
9cded63f 24451 }
bf1dcdb3
TT
24452 else if (cu->producer != nullptr
24453 && strstr (cu->producer, "GNU Go ") != NULL)
24454 {
24455 /* Similar hack for Go. */
3e9f1ca1 24456 cu->per_cu->lang = language_go;
bf1dcdb3
TT
24457 }
24458 else if (attr != nullptr)
3e9f1ca1 24459 cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
bf1dcdb3 24460 else
3e9f1ca1
TT
24461 cu->per_cu->lang = pretend_language;
24462 cu->language_defn = language_def (cu->per_cu->lang);
93311388
DE
24463}
24464
7188ed02 24465/* See read.h. */
ae038cb0 24466
7188ed02
SM
24467dwarf2_cu *
24468dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24469{
7188ed02
SM
24470 auto it = m_dwarf2_cus.find (per_cu);
24471 if (it == m_dwarf2_cus.end ())
24472 return nullptr;
ae038cb0 24473
7188ed02
SM
24474 return it->second;
24475}
24476
24477/* See read.h. */
24478
24479void
24480dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24481{
24482 gdb_assert (this->get_cu (per_cu) == nullptr);
24483
24484 m_dwarf2_cus[per_cu] = cu;
24485}
24486
24487/* See read.h. */
24488
24489void
24490dwarf2_per_objfile::age_comp_units ()
24491{
17e593e9
SM
24492 dwarf_read_debug_printf_v ("running");
24493
08ac5771
SM
24494 /* This is not expected to be called in the middle of CU expansion. There is
24495 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24496 loaded in memory. Calling age_comp_units while the queue is in use could
24497 make us free the DIEs for a CU that is in the queue and therefore break
24498 that invariant. */
24499 gdb_assert (!this->per_bfd->queue.has_value ());
24500
7188ed02
SM
24501 /* Start by clearing all marks. */
24502 for (auto pair : m_dwarf2_cus)
347212b8 24503 pair.second->clear_mark ();
7188ed02
SM
24504
24505 /* Traverse all CUs, mark them and their dependencies if used recently
24506 enough. */
24507 for (auto pair : m_dwarf2_cus)
ae038cb0 24508 {
7188ed02
SM
24509 dwarf2_cu *cu = pair.second;
24510
24511 cu->last_used++;
24512 if (cu->last_used <= dwarf_max_cache_age)
347212b8 24513 cu->mark ();
ae038cb0
DJ
24514 }
24515
7188ed02
SM
24516 /* Delete all CUs still not marked. */
24517 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 24518 {
7188ed02 24519 dwarf2_cu *cu = it->second;
ae038cb0 24520
347212b8 24521 if (!cu->is_marked ())
ae038cb0 24522 {
17e593e9
SM
24523 dwarf_read_debug_printf_v ("deleting old CU %s",
24524 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
24525 delete cu;
24526 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
24527 }
24528 else
7188ed02 24529 it++;
ae038cb0
DJ
24530 }
24531}
24532
7188ed02 24533/* See read.h. */
ae038cb0 24534
7188ed02
SM
24535void
24536dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24537{
7188ed02
SM
24538 auto it = m_dwarf2_cus.find (per_cu);
24539 if (it == m_dwarf2_cus.end ())
24540 return;
ae038cb0 24541
7188ed02 24542 delete it->second;
ae038cb0 24543
7188ed02
SM
24544 m_dwarf2_cus.erase (it);
24545}
ae038cb0 24546
7188ed02
SM
24547dwarf2_per_objfile::~dwarf2_per_objfile ()
24548{
24549 remove_all_cus ();
ae038cb0
DJ
24550}
24551
dee91e82
DE
24552/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24553 We store these in a hash table separate from the DIEs, and preserve them
24554 when the DIEs are flushed out of cache.
24555
24556 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24557 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24558 or the type may come from a DWO file. Furthermore, while it's more logical
24559 to use per_cu->section+offset, with Fission the section with the data is in
24560 the DWO file but we don't know that section at the point we need it.
24561 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24562 because we can enter the lookup routine, get_die_type_at_offset, from
24563 outside this file, and thus won't necessarily have PER_CU->cu.
24564 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24565
dee91e82 24566struct dwarf2_per_cu_offset_and_type
1c379e20 24567{
dee91e82 24568 const struct dwarf2_per_cu_data *per_cu;
9c541725 24569 sect_offset sect_off;
1c379e20
DJ
24570 struct type *type;
24571};
24572
dee91e82 24573/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24574
24575static hashval_t
dee91e82 24576per_cu_offset_and_type_hash (const void *item)
1c379e20 24577{
9a3c8263
SM
24578 const struct dwarf2_per_cu_offset_and_type *ofs
24579 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24580
9c541725 24581 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24582}
24583
dee91e82 24584/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24585
24586static int
dee91e82 24587per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24588{
9a3c8263
SM
24589 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24590 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24591 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24592 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24593
dee91e82 24594 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24595 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24596}
24597
24598/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24599 table if necessary. For convenience, return TYPE.
24600
24601 The DIEs reading must have careful ordering to:
85102364 24602 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24603 reading current DIE.
24604 * Not trying to dereference contents of still incompletely read in types
24605 while reading in other DIEs.
24606 * Enable referencing still incompletely read in types just by a pointer to
24607 the type without accessing its fields.
24608
24609 Therefore caller should follow these rules:
24610 * Try to fetch any prerequisite types we may need to build this DIE type
24611 before building the type and calling set_die_type.
e71ec853 24612 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24613 possible before fetching more types to complete the current type.
24614 * Make the type as complete as possible before fetching more types. */
1c379e20 24615
f792889a 24616static struct type *
57567375
TT
24617set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24618 bool skip_data_location)
1c379e20 24619{
976ca316 24620 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 24621 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 24622 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
24623 struct attribute *attr;
24624 struct dynamic_prop prop;
1c379e20 24625
b4ba55a1
JB
24626 /* For Ada types, make sure that the gnat-specific data is always
24627 initialized (if not already set). There are a few types where
24628 we should not be doing so, because the type-specific area is
24629 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24630 where the type-specific area is used to store the floatformat).
24631 But this is not a problem, because the gnat-specific information
24632 is actually not needed for these types. */
24633 if (need_gnat_info (cu)
78134374
SM
24634 && type->code () != TYPE_CODE_FUNC
24635 && type->code () != TYPE_CODE_FLT
24636 && type->code () != TYPE_CODE_METHODPTR
24637 && type->code () != TYPE_CODE_MEMBERPTR
24638 && type->code () != TYPE_CODE_METHOD
09584414 24639 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
24640 && !HAVE_GNAT_AUX_INFO (type))
24641 INIT_GNAT_SPECIFIC (type);
24642
3f2f83dd
KB
24643 /* Read DW_AT_allocated and set in type. */
24644 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 24645 if (attr != NULL)
3f2f83dd 24646 {
293e7e51 24647 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24648 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24649 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 24650 }
3f2f83dd
KB
24651
24652 /* Read DW_AT_associated and set in type. */
24653 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 24654 if (attr != NULL)
3f2f83dd 24655 {
293e7e51 24656 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24657 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24658 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 24659 }
3f2f83dd 24660
3cdcd0ce 24661 /* Read DW_AT_data_location and set in type. */
57567375
TT
24662 if (!skip_data_location)
24663 {
24664 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24665 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24666 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24667 }
3cdcd0ce 24668
976ca316
SM
24669 if (per_objfile->die_type_hash == NULL)
24670 per_objfile->die_type_hash
0335378b
TT
24671 = htab_up (htab_create_alloc (127,
24672 per_cu_offset_and_type_hash,
24673 per_cu_offset_and_type_eq,
24674 NULL, xcalloc, xfree));
1c379e20 24675
dee91e82 24676 ofs.per_cu = cu->per_cu;
9c541725 24677 ofs.sect_off = die->sect_off;
1c379e20 24678 ofs.type = type;
dee91e82 24679 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 24680 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24681 if (*slot)
b98664d3 24682 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24683 sect_offset_str (die->sect_off));
8d749320
SM
24684 *slot = XOBNEW (&objfile->objfile_obstack,
24685 struct dwarf2_per_cu_offset_and_type);
1c379e20 24686 **slot = ofs;
f792889a 24687 return type;
1c379e20
DJ
24688}
24689
9c541725 24690/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24691 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24692
24693static struct type *
9c541725 24694get_die_type_at_offset (sect_offset sect_off,
aa66c379 24695 dwarf2_per_cu_data *per_cu,
976ca316 24696 dwarf2_per_objfile *per_objfile)
1c379e20 24697{
dee91e82 24698 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 24699
976ca316 24700 if (per_objfile->die_type_hash == NULL)
f792889a 24701 return NULL;
1c379e20 24702
dee91e82 24703 ofs.per_cu = per_cu;
9c541725 24704 ofs.sect_off = sect_off;
9a3c8263 24705 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 24706 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24707 if (slot)
24708 return slot->type;
24709 else
24710 return NULL;
24711}
24712
02142a6c 24713/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24714 or return NULL if DIE does not have a saved type. */
24715
24716static struct type *
24717get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24718{
aa66c379 24719 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
24720}
24721
72bf9492
DJ
24722/* Trivial hash function for partial_die_info: the hash value of a DIE
24723 is its offset in .debug_info for this objfile. */
24724
24725static hashval_t
24726partial_die_hash (const void *item)
24727{
9a3c8263
SM
24728 const struct partial_die_info *part_die
24729 = (const struct partial_die_info *) item;
9a619af0 24730
9c541725 24731 return to_underlying (part_die->sect_off);
72bf9492
DJ
24732}
24733
24734/* Trivial comparison function for partial_die_info structures: two DIEs
24735 are equal if they have the same offset. */
24736
24737static int
24738partial_die_eq (const void *item_lhs, const void *item_rhs)
24739{
9a3c8263
SM
24740 const struct partial_die_info *part_die_lhs
24741 = (const struct partial_die_info *) item_lhs;
24742 const struct partial_die_info *part_die_rhs
24743 = (const struct partial_die_info *) item_rhs;
9a619af0 24744
9c541725 24745 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24746}
24747
3c3bb058
AB
24748struct cmd_list_element *set_dwarf_cmdlist;
24749struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 24750
9291a0cd 24751static void
cd4fb1b2
SM
24752show_check_physname (struct ui_file *file, int from_tty,
24753 struct cmd_list_element *c, const char *value)
9291a0cd 24754{
cd4fb1b2
SM
24755 fprintf_filtered (file,
24756 _("Whether to check \"physname\" is %s.\n"),
24757 value);
9291a0cd
TT
24758}
24759
6c265988 24760void _initialize_dwarf2_read ();
cd4fb1b2 24761void
6c265988 24762_initialize_dwarf2_read ()
9291a0cd 24763{
0743fc83 24764 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 24765Set DWARF specific variables.\n\
590042fc 24766Configure DWARF variables such as the cache size."),
2f822da5 24767 &set_dwarf_cmdlist,
0743fc83 24768 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24769
0743fc83 24770 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
24771Show DWARF specific variables.\n\
24772Show DWARF variables such as the cache size."),
2f822da5 24773 &show_dwarf_cmdlist,
0743fc83 24774 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24775
cd4fb1b2
SM
24776 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24777 &dwarf_max_cache_age, _("\
24778Set the upper bound on the age of cached DWARF compilation units."), _("\
24779Show the upper bound on the age of cached DWARF compilation units."), _("\
24780A higher limit means that cached compilation units will be stored\n\
24781in memory longer, and more total memory will be used. Zero disables\n\
24782caching, which can slow down startup."),
24783 NULL,
24784 show_dwarf_max_cache_age,
24785 &set_dwarf_cmdlist,
24786 &show_dwarf_cmdlist);
156942c7 24787
cd4fb1b2
SM
24788 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24789Set debugging of the DWARF reader."), _("\
24790Show debugging of the DWARF reader."), _("\
24791When enabled (non-zero), debugging messages are printed during DWARF\n\
24792reading and symtab expansion. A value of 1 (one) provides basic\n\
24793information. A value greater than 1 provides more verbose information."),
24794 NULL,
24795 NULL,
24796 &setdebuglist, &showdebuglist);
9291a0cd 24797
3a706c17 24798 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
cd4fb1b2
SM
24799Set debugging of the DWARF DIE reader."), _("\
24800Show debugging of the DWARF DIE reader."), _("\
3a706c17 24801When enabled (non-zero), DIEs are dumped after they are read in.\n\
cd4fb1b2 24802The value is the maximum depth to print."),
3a706c17
SM
24803 NULL,
24804 NULL,
24805 &setdebuglist, &showdebuglist);
9291a0cd 24806
cd4fb1b2
SM
24807 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24808Set debugging of the dwarf line reader."), _("\
24809Show debugging of the dwarf line reader."), _("\
24810When enabled (non-zero), line number entries are dumped as they are read in.\n\
24811A value of 1 (one) provides basic information.\n\
24812A value greater than 1 provides more verbose information."),
24813 NULL,
24814 NULL,
24815 &setdebuglist, &showdebuglist);
437afbb8 24816
cd4fb1b2
SM
24817 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24818Set cross-checking of \"physname\" code against demangler."), _("\
24819Show cross-checking of \"physname\" code against demangler."), _("\
24820When enabled, GDB's internal \"physname\" code is checked against\n\
24821the demangler."),
24822 NULL, show_check_physname,
24823 &setdebuglist, &showdebuglist);
900e11f9 24824
e615022a
DE
24825 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24826 no_class, &use_deprecated_index_sections, _("\
24827Set whether to use deprecated gdb_index sections."), _("\
24828Show whether to use deprecated gdb_index sections."), _("\
24829When enabled, deprecated .gdb_index sections are used anyway.\n\
24830Normally they are ignored either because of a missing feature or\n\
24831performance issue.\n\
24832Warning: This option must be enabled before gdb reads the file."),
24833 NULL,
24834 NULL,
24835 &setlist, &showlist);
24836
f1e6e072
TT
24837 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24838 &dwarf2_locexpr_funcs);
24839 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24840 &dwarf2_loclist_funcs);
24841
24842 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24843 &dwarf2_block_frame_base_locexpr_funcs);
24844 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24845 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24846
24847#if GDB_SELF_TEST
24848 selftests::register_test ("dw2_expand_symtabs_matching",
24849 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24850 selftests::register_test ("dwarf2_find_containing_comp_unit",
24851 selftests::find_containing_comp_unit::run_test);
c62446b1 24852#endif
6502dd73 24853}
This page took 8.501589 seconds and 4 git commands to generate.