Mark tu_abbrev_offset::operator<() const.
[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
d97bc12b 110/* When non-zero, dump DIEs after they are read in. */
b4f54984 111static unsigned int dwarf_die_debug = 0;
d97bc12b 112
27e0867f 113/* When non-zero, dump line number entries as they are read in. */
8fdd972c 114unsigned int dwarf_line_debug = 0;
27e0867f 115
491144b5
CB
116/* When true, cross-check physname against demangler. */
117static bool check_physname = false;
900e11f9 118
491144b5
CB
119/* When true, do not reject deprecated .gdb_index sections. */
120static bool use_deprecated_index_sections = false;
481860b3 121
17ee85fc
TT
122/* This is used to store the data that is always per objfile. */
123static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
124
125/* These are used to store the dwarf2_per_bfd objects.
126
127 objfiles having the same BFD, which doesn't require relocations, are going to
128 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
129
130 Other objfiles are not going to share a dwarf2_per_bfd with any other
131 objfiles, so they'll have their own version kept in the _objfile_data_key
132 version. */
133static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
134static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 135
f1e6e072
TT
136/* The "aclass" indices for various kinds of computed DWARF symbols. */
137
138static int dwarf2_locexpr_index;
139static int dwarf2_loclist_index;
140static int dwarf2_locexpr_block_index;
141static int dwarf2_loclist_block_index;
142
41144253 143/* Size of .debug_loclists section header for 32-bit DWARF format. */
144#define LOCLIST_HEADER_SIZE32 12
145
146/* Size of .debug_loclists section header for 64-bit DWARF format. */
147#define LOCLIST_HEADER_SIZE64 20
148
d0ce17d8
CT
149/* Size of .debug_rnglists section header for 32-bit DWARF format. */
150#define RNGLIST_HEADER_SIZE32 12
151
152/* Size of .debug_rnglists section header for 64-bit DWARF format. */
153#define RNGLIST_HEADER_SIZE64 20
154
3f563c84
PA
155/* An index into a (C++) symbol name component in a symbol name as
156 recorded in the mapped_index's symbol table. For each C++ symbol
157 in the symbol table, we record one entry for the start of each
158 component in the symbol in a table of name components, and then
159 sort the table, in order to be able to binary search symbol names,
160 ignoring leading namespaces, both completion and regular look up.
161 For example, for symbol "A::B::C", we'll have an entry that points
162 to "A::B::C", another that points to "B::C", and another for "C".
163 Note that function symbols in GDB index have no parameter
164 information, just the function/method names. You can convert a
165 name_component to a "const char *" using the
166 'mapped_index::symbol_name_at(offset_type)' method. */
167
168struct name_component
169{
170 /* Offset in the symbol name where the component starts. Stored as
171 a (32-bit) offset instead of a pointer to save memory and improve
172 locality on 64-bit architectures. */
173 offset_type name_offset;
174
175 /* The symbol's index in the symbol and constant pool tables of a
176 mapped_index. */
177 offset_type idx;
178};
179
44ed8f3e
PA
180/* Base class containing bits shared by both .gdb_index and
181 .debug_name indexes. */
182
183struct mapped_index_base
184{
22ca247e
TT
185 mapped_index_base () = default;
186 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
187
44ed8f3e
PA
188 /* The name_component table (a sorted vector). See name_component's
189 description above. */
190 std::vector<name_component> name_components;
191
192 /* How NAME_COMPONENTS is sorted. */
193 enum case_sensitivity name_components_casing;
194
195 /* Return the number of names in the symbol table. */
196 virtual size_t symbol_name_count () const = 0;
197
198 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
199 virtual const char *symbol_name_at
200 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
201
202 /* Return whether the name at IDX in the symbol table should be
203 ignored. */
204 virtual bool symbol_name_slot_invalid (offset_type idx) const
205 {
206 return false;
207 }
208
209 /* Build the symbol name component sorted vector, if we haven't
210 yet. */
fcf23d5b 211 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
212
213 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
214 possible matches for LN_NO_PARAMS in the name component
215 vector. */
216 std::pair<std::vector<name_component>::const_iterator,
217 std::vector<name_component>::const_iterator>
3b00ef10 218 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
219 enum language lang,
220 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
221
222 /* Prevent deleting/destroying via a base class pointer. */
223protected:
224 ~mapped_index_base() = default;
225};
226
42c2c694
TT
227/* This is a view into the index that converts from bytes to an
228 offset_type, and allows indexing. Unaligned bytes are specifically
229 allowed here, and handled via unpacking. */
230
231class offset_view
232{
233public:
234 offset_view () = default;
235
236 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
237 : m_bytes (bytes)
238 {
239 }
240
241 /* Extract the INDEXth offset_type from the array. */
242 offset_type operator[] (size_t index) const
243 {
244 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
245 return (offset_type) extract_unsigned_integer (bytes,
246 sizeof (offset_type),
247 BFD_ENDIAN_LITTLE);
248 }
249
250 /* Return the number of offset_types in this array. */
251 size_t size () const
252 {
253 return m_bytes.size () / sizeof (offset_type);
254 }
255
256 /* Return true if this view is empty. */
257 bool empty () const
258 {
259 return m_bytes.empty ();
260 }
261
262private:
263 /* The underlying bytes. */
264 gdb::array_view<const gdb_byte> m_bytes;
265};
266
9291a0cd
TT
267/* A description of the mapped index. The file format is described in
268 a comment by the code that writes the index. */
fc898b42 269struct mapped_index final : public mapped_index_base
9291a0cd 270{
559a7a62 271 /* Index data format version. */
3063847f 272 int version = 0;
559a7a62 273
f00a2de2
PA
274 /* The address table data. */
275 gdb::array_view<const gdb_byte> address_table;
b11b1f88 276
3876f04e 277 /* The symbol table, implemented as a hash table. */
42c2c694 278 offset_view symbol_table;
b11b1f88 279
9291a0cd 280 /* A pointer to the constant pool. */
42c2c694
TT
281 gdb::array_view<const gdb_byte> constant_pool;
282
283 /* Return the index into the constant pool of the name of the IDXth
284 symbol in the symbol table. */
285 offset_type symbol_name_index (offset_type idx) const
286 {
287 return symbol_table[2 * idx];
288 }
289
290 /* Return the index into the constant pool of the CU vector of the
291 IDXth symbol in the symbol table. */
292 offset_type symbol_vec_index (offset_type idx) const
293 {
294 return symbol_table[2 * idx + 1];
295 }
3f563c84 296
44ed8f3e
PA
297 bool symbol_name_slot_invalid (offset_type idx) const override
298 {
42c2c694
TT
299 return (symbol_name_index (idx) == 0
300 && symbol_vec_index (idx) == 0);
44ed8f3e 301 }
5c58de74 302
3f563c84
PA
303 /* Convenience method to get at the name of the symbol at IDX in the
304 symbol table. */
fcf23d5b
SM
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
42c2c694
TT
307 {
308 return (const char *) (this->constant_pool.data ()
309 + symbol_name_index (idx));
310 }
5c58de74 311
44ed8f3e 312 size_t symbol_name_count () const override
42c2c694 313 { return this->symbol_table.size () / 2; }
9291a0cd
TT
314};
315
927aa2e7
JK
316/* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
fc898b42 318struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
319{
320 bfd_endian dwarf5_byte_order;
321 bool dwarf5_is_dwarf64;
322 bool augmentation_is_gdb;
323 uint8_t offset_size;
324 uint32_t cu_count = 0;
325 uint32_t tu_count, bucket_count, name_count;
326 const gdb_byte *cu_table_reordered, *tu_table_reordered;
327 const uint32_t *bucket_table_reordered, *hash_table_reordered;
328 const gdb_byte *name_table_string_offs_reordered;
329 const gdb_byte *name_table_entry_offs_reordered;
330 const gdb_byte *entry_pool;
331
332 struct index_val
333 {
334 ULONGEST dwarf_tag;
335 struct attr
336 {
337 /* Attribute name DW_IDX_*. */
338 ULONGEST dw_idx;
339
340 /* Attribute form DW_FORM_*. */
341 ULONGEST form;
342
343 /* Value if FORM is DW_FORM_implicit_const. */
344 LONGEST implicit_const;
345 };
346 std::vector<attr> attr_vec;
347 };
348
349 std::unordered_map<ULONGEST, index_val> abbrev_map;
350
fcf23d5b
SM
351 const char *namei_to_name
352 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
353
354 /* Implementation of the mapped_index_base virtual interface, for
355 the name_components cache. */
356
fcf23d5b
SM
357 const char *symbol_name_at
358 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
359 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
360
361 size_t symbol_name_count () const override
362 { return this->name_count; }
927aa2e7
JK
363};
364
cd4fb1b2 365/* See dwarf2read.h. */
ed2dc618 366
cd4fb1b2 367dwarf2_per_objfile *
ed2dc618
SM
368get_dwarf2_per_objfile (struct objfile *objfile)
369{
5bfd760d 370 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 371}
c906108c 372
251d32d9 373/* Default names of the debugging sections. */
c906108c 374
233a11ab
CS
375/* Note that if the debugging section has been compressed, it might
376 have a name like .zdebug_info. */
377
9938d15a 378const struct dwarf2_debug_sections dwarf2_elf_names =
9cdd5dbd 379{
251d32d9
TG
380 { ".debug_info", ".zdebug_info" },
381 { ".debug_abbrev", ".zdebug_abbrev" },
382 { ".debug_line", ".zdebug_line" },
383 { ".debug_loc", ".zdebug_loc" },
43988095 384 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 385 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 386 { ".debug_macro", ".zdebug_macro" },
251d32d9 387 { ".debug_str", ".zdebug_str" },
18a8505e 388 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 389 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 390 { ".debug_ranges", ".zdebug_ranges" },
43988095 391 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 392 { ".debug_types", ".zdebug_types" },
3019eac3 393 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
394 { ".debug_frame", ".zdebug_frame" },
395 { ".eh_frame", NULL },
24d3216f 396 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
397 { ".debug_names", ".zdebug_names" },
398 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 399 23
251d32d9 400};
c906108c 401
80626a55 402/* List of DWO/DWP sections. */
3019eac3 403
80626a55 404static const struct dwop_section_names
3019eac3
DE
405{
406 struct dwarf2_section_names abbrev_dwo;
407 struct dwarf2_section_names info_dwo;
408 struct dwarf2_section_names line_dwo;
409 struct dwarf2_section_names loc_dwo;
43988095 410 struct dwarf2_section_names loclists_dwo;
09262596
DE
411 struct dwarf2_section_names macinfo_dwo;
412 struct dwarf2_section_names macro_dwo;
d0ce17d8 413 struct dwarf2_section_names rnglists_dwo;
3019eac3
DE
414 struct dwarf2_section_names str_dwo;
415 struct dwarf2_section_names str_offsets_dwo;
416 struct dwarf2_section_names types_dwo;
80626a55
DE
417 struct dwarf2_section_names cu_index;
418 struct dwarf2_section_names tu_index;
3019eac3 419}
80626a55 420dwop_section_names =
3019eac3
DE
421{
422 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
423 { ".debug_info.dwo", ".zdebug_info.dwo" },
424 { ".debug_line.dwo", ".zdebug_line.dwo" },
425 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 426 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
427 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
428 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
d0ce17d8 429 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
3019eac3
DE
430 { ".debug_str.dwo", ".zdebug_str.dwo" },
431 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
432 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
433 { ".debug_cu_index", ".zdebug_cu_index" },
434 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
435};
436
c906108c
SS
437/* local data types */
438
d0ce17d8
CT
439/* The location list and range list sections (.debug_loclists & .debug_rnglists)
440 begin with a header, which contains the following information. */
441struct loclists_rnglists_header
41144253 442{
443 /* A 4-byte or 12-byte length containing the length of the
444 set of entries for this compilation unit, not including the
445 length field itself. */
446 unsigned int length;
447
448 /* A 2-byte version identifier. */
449 short version;
450
451 /* A 1-byte unsigned integer containing the size in bytes of an address on
452 the target system. */
453 unsigned char addr_size;
454
455 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
456 on the target system. */
457 unsigned char segment_collector_size;
458
459 /* A 4-byte count of the number of offsets that follow the header. */
460 unsigned int offset_entry_count;
461};
462
094b34ac
DE
463/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
464 This includes type_unit_group and quick_file_names. */
465
466struct stmt_list_hash
467{
468 /* The DWO unit this table is from or NULL if there is none. */
469 struct dwo_unit *dwo_unit;
470
471 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 472 sect_offset line_sect_off;
094b34ac
DE
473};
474
5989a64e 475/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
476 an object of this type. This contains elements of type unit groups
477 that can be shared across objfiles. The non-shareable parts are in
478 type_unit_group_unshareable. */
f4dc4d17 479
0d305d5c 480struct type_unit_group : public dwarf2_per_cu_data
f4dc4d17 481{
0186c6a7
DE
482 /* The TUs that share this DW_AT_stmt_list entry.
483 This is added to while parsing type units to build partial symtabs,
484 and is deleted afterwards and not used again. */
0d305d5c 485 std::vector<signatured_type *> *tus = nullptr;
f4dc4d17 486
094b34ac 487 /* The data used to construct the hash key. */
0d305d5c 488 struct stmt_list_hash hash {};
f4dc4d17
DE
489};
490
73869dc2 491/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
492
493struct dwo_sections
494{
495 struct dwarf2_section_info abbrev;
3019eac3
DE
496 struct dwarf2_section_info line;
497 struct dwarf2_section_info loc;
43988095 498 struct dwarf2_section_info loclists;
09262596
DE
499 struct dwarf2_section_info macinfo;
500 struct dwarf2_section_info macro;
d0ce17d8 501 struct dwarf2_section_info rnglists;
3019eac3
DE
502 struct dwarf2_section_info str;
503 struct dwarf2_section_info str_offsets;
80626a55
DE
504 /* In the case of a virtual DWO file, these two are unused. */
505 struct dwarf2_section_info info;
fd5866f6 506 std::vector<dwarf2_section_info> types;
3019eac3
DE
507};
508
c88ee1f0 509/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
510
511struct dwo_unit
512{
513 /* Backlink to the containing struct dwo_file. */
514 struct dwo_file *dwo_file;
515
516 /* The "id" that distinguishes this CU/TU.
517 .debug_info calls this "dwo_id", .debug_types calls this "signature".
518 Since signatures came first, we stick with it for consistency. */
519 ULONGEST signature;
520
521 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 522 struct dwarf2_section_info *section;
3019eac3 523
9c541725
PA
524 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
525 sect_offset sect_off;
3019eac3
DE
526 unsigned int length;
527
528 /* For types, offset in the type's DIE of the type defined by this TU. */
529 cu_offset type_offset_in_tu;
530};
531
73869dc2
DE
532/* include/dwarf2.h defines the DWP section codes.
533 It defines a max value but it doesn't define a min value, which we
534 use for error checking, so provide one. */
535
536enum dwp_v2_section_ids
537{
538 DW_SECT_MIN = 1
539};
540
80626a55 541/* Data for one DWO file.
57d63ce2
DE
542
543 This includes virtual DWO files (a virtual DWO file is a DWO file as it
544 appears in a DWP file). DWP files don't really have DWO files per se -
545 comdat folding of types "loses" the DWO file they came from, and from
546 a high level view DWP files appear to contain a mass of random types.
547 However, to maintain consistency with the non-DWP case we pretend DWP
548 files contain virtual DWO files, and we assign each TU with one virtual
549 DWO file (generally based on the line and abbrev section offsets -
550 a heuristic that seems to work in practice). */
3019eac3
DE
551
552struct dwo_file
553{
51ac9db5
SM
554 dwo_file () = default;
555 DISABLE_COPY_AND_ASSIGN (dwo_file);
556
18a8505e 557 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
558 For virtual DWO files the name is constructed from the section offsets
559 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
560 from related CU+TUs. */
51ac9db5 561 const char *dwo_name = nullptr;
0ac5b59e
DE
562
563 /* The DW_AT_comp_dir attribute. */
51ac9db5 564 const char *comp_dir = nullptr;
3019eac3 565
80626a55
DE
566 /* The bfd, when the file is open. Otherwise this is NULL.
567 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 568 gdb_bfd_ref_ptr dbfd;
3019eac3 569
73869dc2 570 /* The sections that make up this DWO file.
d2854d8d 571 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
73869dc2 572 sections (for lack of a better name). */
51ac9db5 573 struct dwo_sections sections {};
3019eac3 574
33c5cd75
DB
575 /* The CUs in the file.
576 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
577 an extension to handle LLVM's Link Time Optimization output (where
578 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 579 htab_up cus;
3019eac3
DE
580
581 /* Table of TUs in the file.
582 Each element is a struct dwo_unit. */
b0b6a987 583 htab_up tus;
3019eac3
DE
584};
585
80626a55
DE
586/* These sections are what may appear in a DWP file. */
587
588struct dwp_sections
589{
d2854d8d 590 /* These are used by all DWP versions (1, 2 and 5). */
80626a55
DE
591 struct dwarf2_section_info str;
592 struct dwarf2_section_info cu_index;
593 struct dwarf2_section_info tu_index;
73869dc2 594
d2854d8d 595 /* These are only used by DWP version 2 and version 5 files.
73869dc2
DE
596 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
597 sections are referenced by section number, and are not recorded here.
d2854d8d
CT
598 In DWP version 2 or 5 there is at most one copy of all these sections,
599 each section being (effectively) comprised of the concatenation of all of
600 the individual sections that exist in the version 1 format.
73869dc2
DE
601 To keep the code simple we treat each of these concatenated pieces as a
602 section itself (a virtual section?). */
603 struct dwarf2_section_info abbrev;
604 struct dwarf2_section_info info;
605 struct dwarf2_section_info line;
606 struct dwarf2_section_info loc;
d2854d8d 607 struct dwarf2_section_info loclists;
73869dc2
DE
608 struct dwarf2_section_info macinfo;
609 struct dwarf2_section_info macro;
d2854d8d 610 struct dwarf2_section_info rnglists;
73869dc2
DE
611 struct dwarf2_section_info str_offsets;
612 struct dwarf2_section_info types;
80626a55
DE
613};
614
73869dc2
DE
615/* These sections are what may appear in a virtual DWO file in DWP version 1.
616 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 617
73869dc2 618struct virtual_v1_dwo_sections
80626a55
DE
619{
620 struct dwarf2_section_info abbrev;
621 struct dwarf2_section_info line;
622 struct dwarf2_section_info loc;
623 struct dwarf2_section_info macinfo;
624 struct dwarf2_section_info macro;
625 struct dwarf2_section_info str_offsets;
626 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 627 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
628 struct dwarf2_section_info info_or_types;
629};
630
d2854d8d 631/* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
73869dc2
DE
632 In version 2, the sections of the DWO files are concatenated together
633 and stored in one section of that name. Thus each ELF section contains
634 several "virtual" sections. */
635
d2854d8d 636struct virtual_v2_or_v5_dwo_sections
73869dc2
DE
637{
638 bfd_size_type abbrev_offset;
639 bfd_size_type abbrev_size;
640
641 bfd_size_type line_offset;
642 bfd_size_type line_size;
643
644 bfd_size_type loc_offset;
645 bfd_size_type loc_size;
646
d2854d8d
CT
647 bfd_size_type loclists_offset;
648 bfd_size_type loclists_size;
649
73869dc2
DE
650 bfd_size_type macinfo_offset;
651 bfd_size_type macinfo_size;
652
653 bfd_size_type macro_offset;
654 bfd_size_type macro_size;
655
d2854d8d
CT
656 bfd_size_type rnglists_offset;
657 bfd_size_type rnglists_size;
658
73869dc2
DE
659 bfd_size_type str_offsets_offset;
660 bfd_size_type str_offsets_size;
661
662 /* Each DWP hash table entry records one CU or one TU.
663 That is recorded here, and copied to dwo_unit.section. */
664 bfd_size_type info_or_types_offset;
665 bfd_size_type info_or_types_size;
666};
667
80626a55
DE
668/* Contents of DWP hash tables. */
669
670struct dwp_hash_table
671{
73869dc2 672 uint32_t version, nr_columns;
80626a55 673 uint32_t nr_units, nr_slots;
73869dc2
DE
674 const gdb_byte *hash_table, *unit_table;
675 union
676 {
677 struct
678 {
679 const gdb_byte *indices;
680 } v1;
681 struct
682 {
683 /* This is indexed by column number and gives the id of the section
684 in that column. */
685#define MAX_NR_V2_DWO_SECTIONS \
686 (1 /* .debug_info or .debug_types */ \
687 + 1 /* .debug_abbrev */ \
688 + 1 /* .debug_line */ \
689 + 1 /* .debug_loc */ \
690 + 1 /* .debug_str_offsets */ \
691 + 1 /* .debug_macro or .debug_macinfo */)
692 int section_ids[MAX_NR_V2_DWO_SECTIONS];
693 const gdb_byte *offsets;
694 const gdb_byte *sizes;
695 } v2;
d2854d8d
CT
696 struct
697 {
698 /* This is indexed by column number and gives the id of the section
699 in that column. */
700#define MAX_NR_V5_DWO_SECTIONS \
701 (1 /* .debug_info */ \
702 + 1 /* .debug_abbrev */ \
703 + 1 /* .debug_line */ \
704 + 1 /* .debug_loclists */ \
705 + 1 /* .debug_str_offsets */ \
706 + 1 /* .debug_macro */ \
707 + 1 /* .debug_rnglists */)
708 int section_ids[MAX_NR_V5_DWO_SECTIONS];
709 const gdb_byte *offsets;
710 const gdb_byte *sizes;
711 } v5;
73869dc2 712 } section_pool;
80626a55
DE
713};
714
715/* Data for one DWP file. */
716
717struct dwp_file
718{
400174b1
TT
719 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
720 : name (name_),
721 dbfd (std::move (abfd))
722 {
723 }
724
80626a55
DE
725 /* Name of the file. */
726 const char *name;
727
73869dc2 728 /* File format version. */
400174b1 729 int version = 0;
73869dc2 730
93417882 731 /* The bfd. */
400174b1 732 gdb_bfd_ref_ptr dbfd;
80626a55
DE
733
734 /* Section info for this file. */
400174b1 735 struct dwp_sections sections {};
80626a55 736
57d63ce2 737 /* Table of CUs in the file. */
400174b1 738 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
739
740 /* Table of TUs in the file. */
400174b1 741 const struct dwp_hash_table *tus = nullptr;
80626a55 742
19ac8c2e 743 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
744 htab_up loaded_cus;
745 htab_up loaded_tus;
80626a55 746
73869dc2
DE
747 /* Table to map ELF section numbers to their sections.
748 This is only needed for the DWP V1 file format. */
400174b1
TT
749 unsigned int num_sections = 0;
750 asection **elf_sections = nullptr;
80626a55
DE
751};
752
0963b4bd
MS
753/* Struct used to pass misc. parameters to read_die_and_children, et
754 al. which are used for both .debug_info and .debug_types dies.
755 All parameters here are unchanging for the life of the call. This
dee91e82 756 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
757
758struct die_reader_specs
759{
a32a8923 760 /* The bfd of die_section. */
0280fdcc 761 bfd *abfd;
93311388
DE
762
763 /* The CU of the DIE we are parsing. */
764 struct dwarf2_cu *cu;
765
80626a55 766 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
767 struct dwo_file *dwo_file;
768
dee91e82 769 /* The section the die comes from.
3019eac3 770 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
771 struct dwarf2_section_info *die_section;
772
773 /* die_section->buffer. */
d521ce57 774 const gdb_byte *buffer;
f664829e
DE
775
776 /* The end of the buffer. */
777 const gdb_byte *buffer_end;
a2ce51a0 778
685af9cd
TT
779 /* The abbreviation table to use when reading the DIEs. */
780 struct abbrev_table *abbrev_table;
93311388
DE
781};
782
c0ab21c2
TT
783/* A subclass of die_reader_specs that holds storage and has complex
784 constructor and destructor behavior. */
785
786class cutu_reader : public die_reader_specs
787{
788public:
789
ab432490
SM
790 cutu_reader (dwarf2_per_cu_data *this_cu,
791 dwarf2_per_objfile *per_objfile,
c0ab21c2 792 struct abbrev_table *abbrev_table,
2e671100 793 dwarf2_cu *existing_cu,
c0ab21c2
TT
794 bool skip_partial);
795
796 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 797 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
798 struct dwarf2_cu *parent_cu = nullptr,
799 struct dwo_file *dwo_file = nullptr);
800
c0ab21c2
TT
801 DISABLE_COPY_AND_ASSIGN (cutu_reader);
802
803 const gdb_byte *info_ptr = nullptr;
804 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
805 bool dummy_p = false;
806
6751ebae
TT
807 /* Release the new CU, putting it on the chain. This cannot be done
808 for dummy CUs. */
809 void keep ();
810
c0ab21c2 811private:
9e021579
SM
812 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
813 dwarf2_per_objfile *per_objfile,
2e671100 814 dwarf2_cu *existing_cu);
c0ab21c2
TT
815
816 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
817 std::unique_ptr<dwarf2_cu> m_new_cu;
818
819 /* The ordinary abbreviation table. */
820 abbrev_table_up m_abbrev_table_holder;
821
822 /* The DWO abbreviation table. */
823 abbrev_table_up m_dwo_abbrev_table;
824};
dee91e82 825
c906108c 826/* When we construct a partial symbol table entry we only
0963b4bd 827 need this much information. */
6f06d47b 828struct partial_die_info : public allocate_on_obstack
c906108c 829 {
7c32eebb 830 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
6f06d47b
YQ
831
832 /* Disable assign but still keep copy ctor, which is needed
833 load_partial_dies. */
834 partial_die_info& operator=(const partial_die_info& rhs) = delete;
835
52356b79
YQ
836 /* Adjust the partial die before generating a symbol for it. This
837 function may set the is_external flag or change the DIE's
838 name. */
839 void fixup (struct dwarf2_cu *cu);
840
48fbe735
YQ
841 /* Read a minimal amount of information into the minimal die
842 structure. */
843 const gdb_byte *read (const struct die_reader_specs *reader,
844 const struct abbrev_info &abbrev,
845 const gdb_byte *info_ptr);
846
7d00ffec
TT
847 /* Compute the name of this partial DIE. This memoizes the
848 result, so it is safe to call multiple times. */
849 const char *name (dwarf2_cu *cu);
850
72bf9492 851 /* Offset of this DIE. */
6f06d47b 852 const sect_offset sect_off;
72bf9492
DJ
853
854 /* DWARF-2 tag for this DIE. */
6f06d47b 855 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 856
72bf9492 857 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
858 const unsigned int has_children : 1;
859
72bf9492
DJ
860 unsigned int is_external : 1;
861 unsigned int is_declaration : 1;
862 unsigned int has_type : 1;
863 unsigned int has_specification : 1;
864 unsigned int has_pc_info : 1;
481860b3 865 unsigned int may_be_inlined : 1;
72bf9492 866
0c1b455e
TT
867 /* This DIE has been marked DW_AT_main_subprogram. */
868 unsigned int main_subprogram : 1;
869
72bf9492
DJ
870 /* Flag set if the SCOPE field of this structure has been
871 computed. */
872 unsigned int scope_set : 1;
873
fa4028e9
JB
874 /* Flag set if the DIE has a byte_size attribute. */
875 unsigned int has_byte_size : 1;
876
ff908ebf
AW
877 /* Flag set if the DIE has a DW_AT_const_value attribute. */
878 unsigned int has_const_value : 1;
879
98bfdba5
PA
880 /* Flag set if any of the DIE's children are template arguments. */
881 unsigned int has_template_arguments : 1;
882
52356b79 883 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
884 unsigned int fixup_called : 1;
885
36586728
TT
886 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
887 unsigned int is_dwz : 1;
888
889 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
890 unsigned int spec_is_dwz : 1;
891
7d00ffec
TT
892 unsigned int canonical_name : 1;
893
72bf9492 894 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 895 sometimes a default name for unnamed DIEs. */
7d00ffec 896 const char *raw_name = nullptr;
72bf9492 897
abc72ce4 898 /* The linkage name, if present. */
6f06d47b 899 const char *linkage_name = nullptr;
abc72ce4 900
72bf9492
DJ
901 /* The scope to prepend to our children. This is generally
902 allocated on the comp_unit_obstack, so will disappear
903 when this compilation unit leaves the cache. */
6f06d47b 904 const char *scope = nullptr;
72bf9492 905
95554aad
TT
906 /* Some data associated with the partial DIE. The tag determines
907 which field is live. */
908 union
909 {
910 /* The location description associated with this DIE, if any. */
911 struct dwarf_block *locdesc;
912 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 913 sect_offset sect_off;
6f06d47b 914 } d {};
72bf9492
DJ
915
916 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
917 CORE_ADDR lowpc = 0;
918 CORE_ADDR highpc = 0;
72bf9492 919
93311388 920 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 921 DW_AT_sibling, if any. */
48fbe735
YQ
922 /* NOTE: This member isn't strictly necessary, partial_die_info::read
923 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 924 const gdb_byte *sibling = nullptr;
72bf9492
DJ
925
926 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
927 DW_AT_specification (or DW_AT_abstract_origin or
928 DW_AT_extension). */
6f06d47b 929 sect_offset spec_offset {};
72bf9492
DJ
930
931 /* Pointers to this DIE's parent, first child, and next sibling,
932 if any. */
6f06d47b
YQ
933 struct partial_die_info *die_parent = nullptr;
934 struct partial_die_info *die_child = nullptr;
935 struct partial_die_info *die_sibling = nullptr;
936
937 friend struct partial_die_info *
938 dwarf2_cu::find_partial_die (sect_offset sect_off);
939
940 private:
941 /* Only need to do look up in dwarf2_cu::find_partial_die. */
942 partial_die_info (sect_offset sect_off)
943 : partial_die_info (sect_off, DW_TAG_padding, 0)
944 {
945 }
946
947 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
948 int has_children_)
949 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
950 {
951 is_external = 0;
952 is_declaration = 0;
953 has_type = 0;
954 has_specification = 0;
955 has_pc_info = 0;
956 may_be_inlined = 0;
957 main_subprogram = 0;
958 scope_set = 0;
959 has_byte_size = 0;
960 has_const_value = 0;
961 has_template_arguments = 0;
962 fixup_called = 0;
963 is_dwz = 0;
964 spec_is_dwz = 0;
7d00ffec 965 canonical_name = 0;
6f06d47b 966 }
c906108c
SS
967 };
968
c906108c
SS
969/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
970 but this would require a corresponding change in unpack_field_as_long
971 and friends. */
972static int bits_per_byte = 8;
973
9c6a1327
TT
974struct variant_part_builder;
975
976/* When reading a variant, we track a bit more information about the
977 field, and store it in an object of this type. */
2ddeaf8a
TT
978
979struct variant_field
980{
9c6a1327
TT
981 int first_field = -1;
982 int last_field = -1;
983
984 /* A variant can contain other variant parts. */
985 std::vector<variant_part_builder> variant_parts;
986
2ddeaf8a
TT
987 /* If we see a DW_TAG_variant, then this will be set if this is the
988 default branch. */
9c6a1327
TT
989 bool default_branch = false;
990 /* If we see a DW_AT_discr_value, then this will be the discriminant
991 value. */
992 ULONGEST discriminant_value = 0;
993 /* If we see a DW_AT_discr_list, then this is a pointer to the list
994 data. */
995 struct dwarf_block *discr_list_data = nullptr;
996};
997
998/* This represents a DW_TAG_variant_part. */
999
1000struct variant_part_builder
1001{
1002 /* The offset of the discriminant field. */
1003 sect_offset discriminant_offset {};
1004
1005 /* Variants that are direct children of this variant part. */
1006 std::vector<variant_field> variants;
1007
1008 /* True if we're currently reading a variant. */
1009 bool processing_variant = false;
2ddeaf8a
TT
1010};
1011
52059ffd
TT
1012struct nextfield
1013{
be2daae6
TT
1014 int accessibility = 0;
1015 int virtuality = 0;
9c6a1327
TT
1016 /* Variant parts need to find the discriminant, which is a DIE
1017 reference. We track the section offset of each field to make
1018 this link. */
1019 sect_offset offset;
be2daae6 1020 struct field field {};
52059ffd
TT
1021};
1022
1023struct fnfieldlist
1024{
be2daae6
TT
1025 const char *name = nullptr;
1026 std::vector<struct fn_field> fnfields;
52059ffd
TT
1027};
1028
c906108c
SS
1029/* The routines that read and process dies for a C struct or C++ class
1030 pass lists of data member fields and lists of member function fields
1031 in an instance of a field_info structure, as defined below. */
1032struct field_info
2de01bdb
SM
1033{
1034 /* List of data member and baseclasses fields. */
1035 std::vector<struct nextfield> fields;
1036 std::vector<struct nextfield> baseclasses;
1037
1038 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1039 bool non_public_fields = false;
2de01bdb
SM
1040
1041 /* Member function fieldlist array, contains name of possibly overloaded
1042 member function, number of overloaded member functions and a pointer
1043 to the head of the member function field chain. */
1044 std::vector<struct fnfieldlist> fnfieldlists;
1045
1046 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1047 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1048 std::vector<struct decl_field> typedef_field_list;
1049
1050 /* Nested types defined by this class and the number of elements in this
1051 list. */
1052 std::vector<struct decl_field> nested_types_list;
1053
1054 /* If non-null, this is the variant part we are currently
1055 reading. */
1056 variant_part_builder *current_variant_part = nullptr;
1057 /* This holds all the top-level variant parts attached to the type
1058 we're reading. */
1059 std::vector<variant_part_builder> variant_parts;
1060
1061 /* Return the total number of fields (including baseclasses). */
1062 int nfields () const
c5aa993b 1063 {
2de01bdb
SM
1064 return fields.size () + baseclasses.size ();
1065 }
1066};
c906108c 1067
ae038cb0
DJ
1068/* Loaded secondary compilation units are kept in memory until they
1069 have not been referenced for the processing of this many
1070 compilation units. Set this to zero to disable caching. Cache
1071 sizes of up to at least twenty will improve startup time for
1072 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1073static int dwarf_max_cache_age = 5;
920d2a44 1074static void
b4f54984
DE
1075show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1076 struct cmd_list_element *c, const char *value)
920d2a44 1077{
3e43a32a 1078 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1079 "DWARF compilation units is %s.\n"),
920d2a44
AC
1080 value);
1081}
4390d890 1082\f
c906108c
SS
1083/* local function prototypes */
1084
918dd910
JK
1085static void dwarf2_find_base_address (struct die_info *die,
1086 struct dwarf2_cu *cu);
1087
891813be 1088static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1089 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1090 const char *name);
0018ea6f 1091
f1902523
JK
1092static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1093 const gdb_byte *info_ptr,
3e225074 1094 struct die_info *type_unit_die);
f1902523 1095
976ca316 1096static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1097
72bf9492
DJ
1098static void scan_partial_symbols (struct partial_die_info *,
1099 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1100 int, struct dwarf2_cu *);
c906108c 1101
72bf9492
DJ
1102static void add_partial_symbol (struct partial_die_info *,
1103 struct dwarf2_cu *);
63d06c5c 1104
72bf9492
DJ
1105static void add_partial_namespace (struct partial_die_info *pdi,
1106 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1107 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1108
5d7cb8df 1109static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1110 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1111 struct dwarf2_cu *cu);
1112
72bf9492
DJ
1113static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1114 struct dwarf2_cu *cu);
91c24f0a 1115
bc30ff58
JB
1116static void add_partial_subprogram (struct partial_die_info *pdi,
1117 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1118 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1119
d521ce57 1120static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1121
dee91e82 1122static struct partial_die_info *load_partial_dies
d521ce57 1123 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1124
fb816e8b
TV
1125/* A pair of partial_die_info and compilation unit. */
1126struct cu_partial_die_info
1127{
1128 /* The compilation unit of the partial_die_info. */
1129 struct dwarf2_cu *cu;
1130 /* A partial_die_info. */
1131 struct partial_die_info *pdi;
122cf0f2
AB
1132
1133 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1134 : cu (cu),
1135 pdi (pdi)
405feb71 1136 { /* Nothing. */ }
122cf0f2
AB
1137
1138private:
1139 cu_partial_die_info () = delete;
fb816e8b
TV
1140};
1141
122cf0f2
AB
1142static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1143 struct dwarf2_cu *);
72bf9492 1144
d521ce57 1145static const gdb_byte *read_attribute (const struct die_reader_specs *,
4444f407
TT
1146 struct attribute *,
1147 const struct attr_abbrev *,
7a5f294d 1148 const gdb_byte *);
18a8505e
AT
1149
1150static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1151 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1152
1153static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1154
976ca316
SM
1155static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1156 dwarf2_section_info *, sect_offset);
f4dc4d17 1157
ed2dc618 1158static const char *read_indirect_string
976ca316 1159 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1160 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1161
ed2dc618 1162static const char *read_indirect_string_at_offset
976ca316 1163 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1164
d521ce57
TT
1165static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1166 const gdb_byte *,
3019eac3
DE
1167 unsigned int *);
1168
18a8505e
AT
1169static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1170 ULONGEST str_index);
1171
1172static const char *read_stub_str_index (struct dwarf2_cu *cu,
1173 ULONGEST str_index);
3019eac3 1174
e142c38c 1175static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1176
e142c38c
DJ
1177static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1178 struct dwarf2_cu *);
c906108c 1179
7d45c7c3 1180static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
dda83cd7 1181 struct dwarf2_cu *cu);
7d45c7c3 1182
a084a2a6
AT
1183static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1184
05cf31d1 1185static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
dda83cd7 1186 struct dwarf2_cu *cu);
05cf31d1 1187
e142c38c 1188static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1189
e142c38c 1190static struct die_info *die_specification (struct die_info *die,
f2f0e013 1191 struct dwarf2_cu **);
63d06c5c 1192
9c541725 1193static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1194 struct dwarf2_cu *cu);
debd256d 1195
f3f5162e 1196static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1197 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1198 CORE_ADDR, int decode_mapping);
c906108c 1199
804d2729
TT
1200static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1201 const char *);
c906108c 1202
a14ed312 1203static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1204 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1205
ff39bb5e 1206static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1207 struct dwarf2_cu *);
c906108c 1208
ff39bb5e 1209static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1210 struct type *type,
1211 const char *name,
1212 struct obstack *obstack,
12df843f 1213 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1214 const gdb_byte **bytes,
98bfdba5 1215 struct dwarf2_locexpr_baton **baton);
2df3850c 1216
57567375
TT
1217static struct type *read_subrange_index_type (struct die_info *die,
1218 struct dwarf2_cu *cu);
1219
e7c27a73 1220static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1221
b4ba55a1
JB
1222static int need_gnat_info (struct dwarf2_cu *);
1223
3e43a32a
MS
1224static struct type *die_descriptive_type (struct die_info *,
1225 struct dwarf2_cu *);
b4ba55a1
JB
1226
1227static void set_descriptive_type (struct type *, struct die_info *,
1228 struct dwarf2_cu *);
1229
e7c27a73
DJ
1230static struct type *die_containing_type (struct die_info *,
1231 struct dwarf2_cu *);
c906108c 1232
ff39bb5e 1233static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1234 struct dwarf2_cu *);
c906108c 1235
f792889a 1236static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1237
673bfd45
DE
1238static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1239
0d5cff50 1240static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1241
6e70227d 1242static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1243 const char *suffix, int physname,
1244 struct dwarf2_cu *cu);
63d06c5c 1245
e7c27a73 1246static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1247
348e048f
DE
1248static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1249
e7c27a73 1250static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1251
e7c27a73 1252static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1253
96408a79
SA
1254static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1255
71a3c369
TT
1256static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1257
41144253 1258/* Return the .debug_loclists section to use for cu. */
1259static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1260
d0ce17d8
CT
1261/* Return the .debug_rnglists section to use for cu. */
1262static struct dwarf2_section_info *cu_debug_rnglists_section
1263 (struct dwarf2_cu *cu, dwarf_tag tag);
1264
3a2b436a 1265/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1266 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1267enum pc_bounds_kind
1268{
e385593e 1269 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1270 PC_BOUNDS_NOT_PRESENT,
1271
e385593e
JK
1272 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1273 were present but they do not form a valid range of PC addresses. */
1274 PC_BOUNDS_INVALID,
1275
3a2b436a
JK
1276 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1277 PC_BOUNDS_RANGES,
1278
1279 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1280 PC_BOUNDS_HIGH_LOW,
1281};
1282
1283static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1284 CORE_ADDR *, CORE_ADDR *,
1285 struct dwarf2_cu *,
891813be 1286 dwarf2_psymtab *);
c906108c 1287
fae299cd
DC
1288static void get_scope_pc_bounds (struct die_info *,
1289 CORE_ADDR *, CORE_ADDR *,
1290 struct dwarf2_cu *);
1291
801e3a5b 1292static void dwarf2_record_block_ranges (struct die_info *, struct block *,
dda83cd7 1293 CORE_ADDR, struct dwarf2_cu *);
801e3a5b 1294
a14ed312 1295static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1296 struct dwarf2_cu *);
c906108c 1297
a14ed312 1298static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1299 struct type *, struct dwarf2_cu *);
c906108c 1300
a14ed312 1301static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1302 struct die_info *, struct type *,
e7c27a73 1303 struct dwarf2_cu *);
c906108c 1304
a14ed312 1305static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1306 struct type *,
1307 struct dwarf2_cu *);
c906108c 1308
134d01f1 1309static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1310
e7c27a73 1311static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1312
e7c27a73 1313static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1314
5d7cb8df
JK
1315static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1316
804d2729 1317static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1318
27aa8d6a
SW
1319static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1320
74921315
KS
1321static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1322
f55ee35c
JK
1323static struct type *read_module_type (struct die_info *die,
1324 struct dwarf2_cu *cu);
1325
38d518c9 1326static const char *namespace_name (struct die_info *die,
e142c38c 1327 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1328
134d01f1 1329static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1330
7d79de9a
TT
1331static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1332 bool * = nullptr);
c906108c 1333
6e70227d 1334static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1335 struct dwarf2_cu *);
1336
bf6af496 1337static struct die_info *read_die_and_siblings_1
d521ce57 1338 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1339 struct die_info *);
639d11d3 1340
dee91e82 1341static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1342 const gdb_byte *info_ptr,
1343 const gdb_byte **new_info_ptr,
639d11d3
DC
1344 struct die_info *parent);
1345
d521ce57
TT
1346static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1347 struct die_info **, const gdb_byte *,
3e225074 1348 int);
3019eac3 1349
d521ce57 1350static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1351 struct die_info **, const gdb_byte *);
93311388 1352
e7c27a73 1353static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1354
15d034d0 1355static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1356 struct objfile *);
71c25dea 1357
15d034d0 1358static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1359
15d034d0 1360static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1361 struct die_info *die,
1362 struct dwarf2_cu *cu);
1363
ca69b9e6
DE
1364static const char *dwarf2_physname (const char *name, struct die_info *die,
1365 struct dwarf2_cu *cu);
1366
e142c38c 1367static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1368 struct dwarf2_cu **);
9219021c 1369
d97bc12b
DE
1370static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1371
1372static void dump_die_for_error (struct die_info *);
1373
1374static void dump_die_1 (struct ui_file *, int level, int max_level,
1375 struct die_info *);
c906108c 1376
d97bc12b 1377/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1378
51545339 1379static void store_in_ref_table (struct die_info *,
10b3939b 1380 struct dwarf2_cu *);
c906108c 1381
348e048f 1382static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1383 const struct attribute *,
348e048f
DE
1384 struct dwarf2_cu **);
1385
10b3939b 1386static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1387 const struct attribute *,
f2f0e013 1388 struct dwarf2_cu **);
c906108c 1389
348e048f 1390static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1391 const struct attribute *,
348e048f
DE
1392 struct dwarf2_cu **);
1393
ac9ec31b
DE
1394static struct type *get_signatured_type (struct die_info *, ULONGEST,
1395 struct dwarf2_cu *);
1396
1397static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1398 const struct attribute *,
ac9ec31b
DE
1399 struct dwarf2_cu *);
1400
ab432490
SM
1401static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1402 dwarf2_per_objfile *per_objfile);
348e048f 1403
ab432490
SM
1404static void read_signatured_type (signatured_type *sig_type,
1405 dwarf2_per_objfile *per_objfile);
348e048f 1406
63e43d3a
PMR
1407static int attr_to_dynamic_prop (const struct attribute *attr,
1408 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1409 struct dynamic_prop *prop, struct type *type);
63e43d3a 1410
c906108c
SS
1411/* memory allocation interface */
1412
7b5a2f43 1413static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1414
b60c80d6 1415static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1416
43f3e411 1417static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1418
8cf6f0b1
TT
1419static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1420 struct dwarf2_loclist_baton *baton,
ff39bb5e 1421 const struct attribute *attr);
8cf6f0b1 1422
ff39bb5e 1423static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1424 struct symbol *sym,
f1e6e072
TT
1425 struct dwarf2_cu *cu,
1426 int is_block);
4c2df51b 1427
d521ce57
TT
1428static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1429 const gdb_byte *info_ptr,
7c32eebb 1430 const struct abbrev_info *abbrev);
4bb7a0a7 1431
72bf9492
DJ
1432static hashval_t partial_die_hash (const void *item);
1433
1434static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1435
ae038cb0 1436static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1437 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1438 dwarf2_per_objfile *per_objfile);
ae038cb0 1439
9816fde3 1440static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1441 struct die_info *comp_unit_die,
1442 enum language pretend_language);
93311388 1443
f792889a 1444static struct type *set_die_type (struct die_info *, struct type *,
57567375 1445 struct dwarf2_cu *, bool = false);
1c379e20 1446
976ca316 1447static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1448
ab432490
SM
1449static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1450 dwarf2_per_objfile *per_objfile,
4a636814 1451 dwarf2_cu *existing_cu,
ab432490
SM
1452 bool skip_partial,
1453 enum language pretend_language);
10b3939b 1454
8fc0b21d 1455static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1456 enum language pretend_language);
10b3939b 1457
8fc0b21d 1458static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1459 enum language pretend_language);
f4dc4d17 1460
b64f50a1 1461static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1462 dwarf2_per_cu_data *per_cu,
1463 dwarf2_per_objfile *per_objfile);
673bfd45 1464
f792889a 1465static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1466
120ce1b5
SM
1467static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1468 dwarf2_per_objfile *per_objfile,
95554aad
TT
1469 enum language pretend_language);
1470
976ca316 1471static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1472
b303c6f6
AB
1473/* Class, the destructor of which frees all allocated queue entries. This
1474 will only have work to do if an error was thrown while processing the
1475 dwarf. If no error was thrown then the queue entries should have all
1476 been processed, and freed, as we went along. */
1477
1478class dwarf2_queue_guard
1479{
1480public:
39856def
TT
1481 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1482 : m_per_objfile (per_objfile)
1483 {
08ac5771
SM
1484 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1485
1486 m_per_objfile->per_bfd->queue.emplace ();
39856def 1487 }
b303c6f6
AB
1488
1489 /* Free any entries remaining on the queue. There should only be
1490 entries left if we hit an error while processing the dwarf. */
1491 ~dwarf2_queue_guard ()
1492 {
08ac5771
SM
1493 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1494
1495 m_per_objfile->per_bfd->queue.reset ();
39856def 1496 }
b303c6f6 1497
39856def 1498 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1499
39856def
TT
1500private:
1501 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1502};
1503
39856def
TT
1504dwarf2_queue_item::~dwarf2_queue_item ()
1505{
1506 /* Anything still marked queued is likely to be in an
1507 inconsistent state, so discard it. */
1508 if (per_cu->queued)
1509 {
7188ed02 1510 per_objfile->remove_cu (per_cu);
39856def
TT
1511 per_cu->queued = 0;
1512 }
1513}
1514
473ab964
TT
1515/* See dwarf2/read.h. */
1516
1517void
1518dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1519{
1520 if (data->is_debug_types)
1521 delete static_cast<signatured_type *> (data);
1522 else
1523 delete data;
1524}
1525
d721ba37
PA
1526/* The return type of find_file_and_directory. Note, the enclosed
1527 string pointers are only valid while this object is valid. */
1528
1529struct file_and_directory
1530{
1531 /* The filename. This is never NULL. */
1532 const char *name;
1533
1534 /* The compilation directory. NULL if not known. If we needed to
1535 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1536 points directly to the DW_AT_comp_dir string attribute owned by
1537 the obstack that owns the DIE. */
1538 const char *comp_dir;
1539
1540 /* If we needed to build a new string for comp_dir, this is what
1541 owns the storage. */
1542 std::string comp_dir_storage;
1543};
1544
1545static file_and_directory find_file_and_directory (struct die_info *die,
1546 struct dwarf2_cu *cu);
9291a0cd 1547
298e9637 1548static htab_up allocate_signatured_type_table ();
1fd400ff 1549
298e9637 1550static htab_up allocate_dwo_unit_table ();
3019eac3 1551
57d63ce2 1552static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1553 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1554 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1555
976ca316 1556static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1557
3019eac3 1558static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1559 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1560 ULONGEST signature);
3019eac3
DE
1561
1562static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1563 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1564
1b555f17 1565static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1566
263db9a1
TT
1567/* A unique pointer to a dwo_file. */
1568
51ac9db5 1569typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1570
976ca316 1571static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1572
1b80a9fa 1573static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1574
1575static void free_line_header_voidp (void *arg);
4390d890
DE
1576\f
1577/* Various complaints about symbol reading that don't abort the process. */
1578
4390d890
DE
1579static void
1580dwarf2_debug_line_missing_file_complaint (void)
1581{
b98664d3 1582 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1583}
1584
1585static void
1586dwarf2_debug_line_missing_end_sequence_complaint (void)
1587{
b98664d3 1588 complaint (_(".debug_line section has line "
4390d890
DE
1589 "program sequence without an end"));
1590}
1591
1592static void
1593dwarf2_complex_location_expr_complaint (void)
1594{
b98664d3 1595 complaint (_("location expression too complex"));
4390d890
DE
1596}
1597
1598static void
1599dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1600 int arg3)
1601{
b98664d3 1602 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1603 arg1, arg2, arg3);
1604}
1605
4390d890
DE
1606static void
1607dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1608{
b98664d3 1609 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1610 arg1, arg2);
1611}
527f3840
JK
1612
1613/* Hash function for line_header_hash. */
1614
1615static hashval_t
1616line_header_hash (const struct line_header *ofs)
1617{
9c541725 1618 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1619}
1620
1621/* Hash function for htab_create_alloc_ex for line_header_hash. */
1622
1623static hashval_t
1624line_header_hash_voidp (const void *item)
1625{
9a3c8263 1626 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1627
1628 return line_header_hash (ofs);
1629}
1630
1631/* Equality function for line_header_hash. */
1632
1633static int
1634line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1635{
9a3c8263
SM
1636 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1637 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1638
9c541725 1639 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1640 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1641}
1642
4390d890 1643\f
9291a0cd 1644
330cdd98
PA
1645/* See declaration. */
1646
5989a64e
SM
1647dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1648 bool can_copy_)
c3699833
SM
1649 : obfd (obfd),
1650 can_copy (can_copy_)
330cdd98
PA
1651{
1652 if (names == NULL)
1653 names = &dwarf2_elf_names;
1654
330cdd98
PA
1655 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1656 locate_sections (obfd, sec, *names);
1657}
1658
5989a64e 1659dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1660{
0d305d5c 1661 for (auto &per_cu : all_comp_units)
ae640021 1662 per_cu->imported_symtabs_free ();
fc8e7e75 1663
5989a64e 1664 /* Everything else should be on this->obstack. */
330cdd98
PA
1665}
1666
7188ed02 1667/* See read.h. */
330cdd98
PA
1668
1669void
7188ed02 1670dwarf2_per_objfile::remove_all_cus ()
330cdd98 1671{
08ac5771
SM
1672 gdb_assert (!this->per_bfd->queue.has_value ());
1673
7188ed02
SM
1674 for (auto pair : m_dwarf2_cus)
1675 delete pair.second;
330cdd98 1676
7188ed02 1677 m_dwarf2_cus.clear ();
330cdd98
PA
1678}
1679
11ed8cad
TT
1680/* A helper class that calls free_cached_comp_units on
1681 destruction. */
1682
1683class free_cached_comp_units
1684{
1685public:
1686
1687 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1688 : m_per_objfile (per_objfile)
1689 {
1690 }
1691
1692 ~free_cached_comp_units ()
1693 {
7188ed02 1694 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1695 }
1696
1697 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1698
1699private:
1700
1701 dwarf2_per_objfile *m_per_objfile;
1702};
1703
af758d11
SM
1704/* See read.h. */
1705
1706bool
1707dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1708{
1709 gdb_assert (per_cu->index < this->m_symtabs.size ());
1710
1711 return this->m_symtabs[per_cu->index] != nullptr;
1712}
1713
1714/* See read.h. */
1715
1716compunit_symtab *
1717dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1718{
1719 gdb_assert (per_cu->index < this->m_symtabs.size ());
1720
1721 return this->m_symtabs[per_cu->index];
1722}
1723
1724/* See read.h. */
1725
1726void
1727dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1728 compunit_symtab *symtab)
1729{
1730 gdb_assert (per_cu->index < this->m_symtabs.size ());
1731 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1732
1733 this->m_symtabs[per_cu->index] = symtab;
1734}
1735
c906108c 1736/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1737 information and return true if we have enough to do something.
1738 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1739 ELF names are used. CAN_COPY is true for formats where symbol
1740 interposition is possible and so symbol values must follow copy
1741 relocation rules. */
c906108c
SS
1742
1743int
251d32d9 1744dwarf2_has_info (struct objfile *objfile,
dda83cd7 1745 const struct dwarf2_debug_sections *names,
4b610737 1746 bool can_copy)
c906108c 1747{
97cbe998
SDJ
1748 if (objfile->flags & OBJF_READNEVER)
1749 return 0;
1750
976ca316 1751 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1752
976ca316 1753 if (per_objfile == NULL)
5989a64e 1754 {
17ee85fc
TT
1755 dwarf2_per_bfd *per_bfd;
1756
8a91fbdf
SM
1757 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1758 BFD doesn't require relocations.
1759
1760 We don't share with objfiles for which -readnow was requested,
1761 because it would complicate things when loading the same BFD with
1762 -readnow and then without -readnow. */
1763 if (!gdb_bfd_requires_relocations (objfile->obfd)
1764 && (objfile->flags & OBJF_READNOW) == 0)
17ee85fc
TT
1765 {
1766 /* See if one has been created for this BFD yet. */
1767 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1768
1769 if (per_bfd == nullptr)
1770 {
1771 /* No, create it now. */
1772 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1773 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1774 }
1775 }
1776 else
1777 {
1778 /* No sharing possible, create one specifically for this objfile. */
1779 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1780 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1781 }
5989a64e 1782
976ca316 1783 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1784 }
5bfd760d 1785
976ca316
SM
1786 return (!per_objfile->per_bfd->info.is_virtual
1787 && per_objfile->per_bfd->info.s.section != NULL
1788 && !per_objfile->per_bfd->abbrev.is_virtual
1789 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1790}
1791
330cdd98 1792/* See declaration. */
c906108c 1793
330cdd98 1794void
5989a64e
SM
1795dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1796 const dwarf2_debug_sections &names)
c906108c 1797{
fd361982 1798 flagword aflag = bfd_section_flags (sectp);
251d32d9 1799
dc7650b8
JK
1800 if ((aflag & SEC_HAS_CONTENTS) == 0)
1801 {
1802 }
950b7495
KS
1803 else if (elf_section_data (sectp)->this_hdr.sh_size
1804 > bfd_get_file_size (abfd))
1805 {
1806 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1807 warning (_("Discarding section %s which has a section size (%s"
1808 ") larger than the file size [in module %s]"),
1809 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1810 bfd_get_filename (abfd));
1811 }
fbedd546 1812 else if (names.info.matches (sectp->name))
c906108c 1813 {
330cdd98 1814 this->info.s.section = sectp;
fd361982 1815 this->info.size = bfd_section_size (sectp);
c906108c 1816 }
fbedd546 1817 else if (names.abbrev.matches (sectp->name))
c906108c 1818 {
330cdd98 1819 this->abbrev.s.section = sectp;
fd361982 1820 this->abbrev.size = bfd_section_size (sectp);
c906108c 1821 }
fbedd546 1822 else if (names.line.matches (sectp->name))
c906108c 1823 {
330cdd98 1824 this->line.s.section = sectp;
fd361982 1825 this->line.size = bfd_section_size (sectp);
c906108c 1826 }
fbedd546 1827 else if (names.loc.matches (sectp->name))
c906108c 1828 {
330cdd98 1829 this->loc.s.section = sectp;
fd361982 1830 this->loc.size = bfd_section_size (sectp);
c906108c 1831 }
fbedd546 1832 else if (names.loclists.matches (sectp->name))
43988095 1833 {
330cdd98 1834 this->loclists.s.section = sectp;
fd361982 1835 this->loclists.size = bfd_section_size (sectp);
43988095 1836 }
fbedd546 1837 else if (names.macinfo.matches (sectp->name))
c906108c 1838 {
330cdd98 1839 this->macinfo.s.section = sectp;
fd361982 1840 this->macinfo.size = bfd_section_size (sectp);
c906108c 1841 }
fbedd546 1842 else if (names.macro.matches (sectp->name))
cf2c3c16 1843 {
330cdd98 1844 this->macro.s.section = sectp;
fd361982 1845 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1846 }
fbedd546 1847 else if (names.str.matches (sectp->name))
c906108c 1848 {
330cdd98 1849 this->str.s.section = sectp;
fd361982 1850 this->str.size = bfd_section_size (sectp);
c906108c 1851 }
fbedd546 1852 else if (names.str_offsets.matches (sectp->name))
18a8505e
AT
1853 {
1854 this->str_offsets.s.section = sectp;
1855 this->str_offsets.size = bfd_section_size (sectp);
1856 }
fbedd546 1857 else if (names.line_str.matches (sectp->name))
43988095 1858 {
330cdd98 1859 this->line_str.s.section = sectp;
fd361982 1860 this->line_str.size = bfd_section_size (sectp);
43988095 1861 }
fbedd546 1862 else if (names.addr.matches (sectp->name))
3019eac3 1863 {
330cdd98 1864 this->addr.s.section = sectp;
fd361982 1865 this->addr.size = bfd_section_size (sectp);
3019eac3 1866 }
fbedd546 1867 else if (names.frame.matches (sectp->name))
b6af0555 1868 {
330cdd98 1869 this->frame.s.section = sectp;
fd361982 1870 this->frame.size = bfd_section_size (sectp);
b6af0555 1871 }
fbedd546 1872 else if (names.eh_frame.matches (sectp->name))
b6af0555 1873 {
330cdd98 1874 this->eh_frame.s.section = sectp;
fd361982 1875 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1876 }
fbedd546 1877 else if (names.ranges.matches (sectp->name))
af34e669 1878 {
330cdd98 1879 this->ranges.s.section = sectp;
fd361982 1880 this->ranges.size = bfd_section_size (sectp);
af34e669 1881 }
fbedd546 1882 else if (names.rnglists.matches (sectp->name))
43988095 1883 {
330cdd98 1884 this->rnglists.s.section = sectp;
fd361982 1885 this->rnglists.size = bfd_section_size (sectp);
43988095 1886 }
fbedd546 1887 else if (names.types.matches (sectp->name))
348e048f 1888 {
8b70b953
TT
1889 struct dwarf2_section_info type_section;
1890
1891 memset (&type_section, 0, sizeof (type_section));
049412e3 1892 type_section.s.section = sectp;
fd361982 1893 type_section.size = bfd_section_size (sectp);
8b70b953 1894
fd5866f6 1895 this->types.push_back (type_section);
348e048f 1896 }
fbedd546 1897 else if (names.gdb_index.matches (sectp->name))
9291a0cd 1898 {
330cdd98 1899 this->gdb_index.s.section = sectp;
fd361982 1900 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1901 }
fbedd546 1902 else if (names.debug_names.matches (sectp->name))
927aa2e7
JK
1903 {
1904 this->debug_names.s.section = sectp;
fd361982 1905 this->debug_names.size = bfd_section_size (sectp);
927aa2e7 1906 }
fbedd546 1907 else if (names.debug_aranges.matches (sectp->name))
927aa2e7
JK
1908 {
1909 this->debug_aranges.s.section = sectp;
fd361982 1910 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 1911 }
dce234bc 1912
fd361982
AM
1913 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1914 && bfd_section_vma (sectp) == 0)
330cdd98 1915 this->has_section_at_zero = true;
c906108c
SS
1916}
1917
dce234bc 1918/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1919 SECTION_NAME. */
af34e669 1920
dce234bc 1921void
3017a003 1922dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
1923 enum dwarf2_section_enum sect,
1924 asection **sectp, const gdb_byte **bufp,
1925 bfd_size_type *sizep)
dce234bc 1926{
976ca316 1927 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 1928 struct dwarf2_section_info *info;
a3b2a86b
TT
1929
1930 /* We may see an objfile without any DWARF, in which case we just
1931 return nothing. */
976ca316 1932 if (per_objfile == NULL)
a3b2a86b
TT
1933 {
1934 *sectp = NULL;
1935 *bufp = NULL;
1936 *sizep = 0;
1937 return;
1938 }
3017a003
TG
1939 switch (sect)
1940 {
1941 case DWARF2_DEBUG_FRAME:
976ca316 1942 info = &per_objfile->per_bfd->frame;
3017a003
TG
1943 break;
1944 case DWARF2_EH_FRAME:
976ca316 1945 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
1946 break;
1947 default:
1948 gdb_assert_not_reached ("unexpected section");
1949 }
dce234bc 1950
96b79293 1951 info->read (objfile);
dce234bc 1952
96b79293 1953 *sectp = info->get_bfd_section ();
dce234bc
PP
1954 *bufp = info->buffer;
1955 *sizep = info->size;
1956}
1957
9291a0cd 1958\f
39298a5d 1959/* DWARF quick_symbol_functions support. */
7b9f3c50
DE
1960
1961/* TUs can share .debug_line entries, and there can be a lot more TUs than
1962 unique line tables, so we maintain a separate table of all .debug_line
1963 derived entries to support the sharing.
1964 All the quick functions need is the list of file names. We discard the
1965 line_header when we're done and don't need to record it here. */
1966struct quick_file_names
1967{
094b34ac
DE
1968 /* The data used to construct the hash key. */
1969 struct stmt_list_hash hash;
7b9f3c50
DE
1970
1971 /* The number of entries in file_names, real_names. */
1972 unsigned int num_file_names;
1973
1974 /* The file names from the line table, after being run through
1975 file_full_name. */
1976 const char **file_names;
1977
1978 /* The file names from the line table after being run through
1979 gdb_realpath. These are computed lazily. */
1980 const char **real_names;
1981};
1982
1983/* When using the index (and thus not using psymtabs), each CU has an
1984 object of this type. This is used to hold information needed by
1985 the various "quick" methods. */
1986struct dwarf2_per_cu_quick_data
1987{
1988 /* The file table. This can be NULL if there was no file table
1989 or it's currently not read in.
5989a64e 1990 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
1991 struct quick_file_names *file_names;
1992
7b9f3c50
DE
1993 /* A temporary mark bit used when iterating over all CUs in
1994 expand_symtabs_matching. */
1995 unsigned int mark : 1;
1996
1997 /* True if we've tried to read the file table and found there isn't one.
1998 There will be no point in trying to read it again next time. */
1999 unsigned int no_file_data : 1;
2000};
2001
edc02ceb
TT
2002/* A subclass of psymbol_functions that arranges to read the DWARF
2003 partial symbols when needed. */
2004struct lazy_dwarf_reader : public psymbol_functions
2005{
2006 using psymbol_functions::psymbol_functions;
2007
2008 bool can_lazily_read_symbols () override
2009 {
2010 return true;
2011 }
2012
2013 void read_partial_symbols (struct objfile *objfile) override
2014 {
2015 if (dwarf2_has_info (objfile, nullptr))
2016 dwarf2_build_psymtabs (objfile, this);
2017 }
2018};
2019
2020static quick_symbol_functions_up
2021make_lazy_dwarf_reader ()
2022{
2023 return quick_symbol_functions_up (new lazy_dwarf_reader);
2024}
2025
39298a5d
TT
2026struct dwarf2_base_index_functions : public quick_symbol_functions
2027{
2028 bool has_symbols (struct objfile *objfile) override;
2029
2030 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2031
2032 void forget_cached_source_info (struct objfile *objfile) override;
2033
39298a5d
TT
2034 enum language lookup_global_symbol_language (struct objfile *objfile,
2035 const char *name,
2036 domain_enum domain,
2037 bool *symbol_found_p) override
2038 {
2039 *symbol_found_p = false;
2040 return language_unknown;
2041 }
2042
4829711b 2043 void print_stats (struct objfile *objfile, bool print_bcache) override;
39298a5d
TT
2044
2045 void expand_all_symtabs (struct objfile *objfile) override;
2046
39298a5d
TT
2047 struct compunit_symtab *find_pc_sect_compunit_symtab
2048 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2049 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2050
2051 struct compunit_symtab *find_compunit_symtab_by_address
2052 (struct objfile *objfile, CORE_ADDR address) override
2053 {
2054 return nullptr;
2055 }
2056
2057 void map_symbol_filenames (struct objfile *objfile,
f4655dee
TT
2058 gdb::function_view<symbol_filename_ftype> fun,
2059 bool need_fullname) override;
39298a5d
TT
2060};
2061
2062struct dwarf2_gdb_index : public dwarf2_base_index_functions
2063{
39298a5d
TT
2064 void dump (struct objfile *objfile) override;
2065
0b7b2c2a 2066 void expand_matching_symbols
39298a5d
TT
2067 (struct objfile *,
2068 const lookup_name_info &lookup_name,
2069 domain_enum domain,
2070 int global,
39298a5d
TT
2071 symbol_compare_ftype *ordered_compare) override;
2072
df35e626 2073 bool expand_symtabs_matching
39298a5d
TT
2074 (struct objfile *objfile,
2075 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2076 const lookup_name_info *lookup_name,
2077 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2078 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2079 block_search_flags search_flags,
3bfa51a7 2080 domain_enum domain,
39298a5d
TT
2081 enum search_domain kind) override;
2082};
2083
2084struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2085{
39298a5d
TT
2086 void dump (struct objfile *objfile) override;
2087
0b7b2c2a 2088 void expand_matching_symbols
39298a5d
TT
2089 (struct objfile *,
2090 const lookup_name_info &lookup_name,
2091 domain_enum domain,
2092 int global,
39298a5d
TT
2093 symbol_compare_ftype *ordered_compare) override;
2094
df35e626 2095 bool expand_symtabs_matching
39298a5d
TT
2096 (struct objfile *objfile,
2097 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2098 const lookup_name_info *lookup_name,
2099 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2100 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2101 block_search_flags search_flags,
3bfa51a7 2102 domain_enum domain,
39298a5d
TT
2103 enum search_domain kind) override;
2104};
2105
edc02ceb 2106static quick_symbol_functions_up
39298a5d
TT
2107make_dwarf_gdb_index ()
2108{
2109 return quick_symbol_functions_up (new dwarf2_gdb_index);
2110}
2111
edc02ceb 2112static quick_symbol_functions_up
39298a5d
TT
2113make_dwarf_debug_names ()
2114{
2115 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2116}
2117
094b34ac
DE
2118/* Utility hash function for a stmt_list_hash. */
2119
2120static hashval_t
2121hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2122{
2123 hashval_t v = 0;
2124
2125 if (stmt_list_hash->dwo_unit != NULL)
2126 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2127 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2128 return v;
2129}
2130
2131/* Utility equality function for a stmt_list_hash. */
2132
2133static int
2134eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2135 const struct stmt_list_hash *rhs)
2136{
2137 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2138 return 0;
2139 if (lhs->dwo_unit != NULL
2140 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2141 return 0;
2142
9c541725 2143 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2144}
2145
7b9f3c50
DE
2146/* Hash function for a quick_file_names. */
2147
2148static hashval_t
2149hash_file_name_entry (const void *e)
2150{
9a3c8263
SM
2151 const struct quick_file_names *file_data
2152 = (const struct quick_file_names *) e;
7b9f3c50 2153
094b34ac 2154 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2155}
2156
2157/* Equality function for a quick_file_names. */
2158
2159static int
2160eq_file_name_entry (const void *a, const void *b)
2161{
9a3c8263
SM
2162 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2163 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2164
094b34ac 2165 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2166}
2167
2168/* Delete function for a quick_file_names. */
2169
2170static void
2171delete_file_name_entry (void *e)
2172{
9a3c8263 2173 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2174 int i;
2175
2176 for (i = 0; i < file_data->num_file_names; ++i)
2177 {
2178 xfree ((void*) file_data->file_names[i]);
2179 if (file_data->real_names)
2180 xfree ((void*) file_data->real_names[i]);
2181 }
2182
45940949
TT
2183 /* The space for the struct itself lives on the obstack, so we don't
2184 free it here. */
7b9f3c50
DE
2185}
2186
2187/* Create a quick_file_names hash table. */
2188
5895093f 2189static htab_up
7b9f3c50
DE
2190create_quick_file_names_table (unsigned int nr_initial_entries)
2191{
5895093f
TT
2192 return htab_up (htab_create_alloc (nr_initial_entries,
2193 hash_file_name_entry, eq_file_name_entry,
2194 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2195}
9291a0cd 2196
ab432490
SM
2197/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2198 function is unrelated to symtabs, symtab would have to be created afterwards.
2199 You should call age_cached_comp_units after processing the CU. */
918dd910 2200
1b555f17 2201static dwarf2_cu *
ab432490
SM
2202load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2203 bool skip_partial)
918dd910 2204{
3019eac3 2205 if (per_cu->is_debug_types)
ab432490 2206 load_full_type_unit (per_cu, per_objfile);
918dd910 2207 else
4a636814
SM
2208 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2209 skip_partial, language_minimal);
918dd910 2210
7188ed02
SM
2211 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2212 if (cu == nullptr)
1b555f17 2213 return nullptr; /* Dummy CU. */
2dc860c0 2214
7188ed02 2215 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2216
7188ed02 2217 return cu;
918dd910
JK
2218}
2219
1350c3b4 2220/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2221
9291a0cd 2222static void
97a1449a 2223dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2224 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2225{
f4dc4d17
DE
2226 /* Skip type_unit_groups, reading the type units they contain
2227 is handled elsewhere. */
197400e8 2228 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2229 return;
2230
08ac5771
SM
2231 {
2232 /* The destructor of dwarf2_queue_guard frees any entries left on
2233 the queue. After this point we're guaranteed to leave this function
2234 with the dwarf queue empty. */
2235 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2236
08ac5771
SM
2237 if (!per_objfile->symtab_set_p (per_cu))
2238 {
2239 queue_comp_unit (per_cu, per_objfile, language_minimal);
2240 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2241
2242 /* If we just loaded a CU from a DWO, and we're working with an index
2243 that may badly handle TUs, load all the TUs in that DWO as well.
2244 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2245 if (!per_cu->is_debug_types
2246 && cu != NULL
2247 && cu->dwo_unit != NULL
2248 && per_objfile->per_bfd->index_table != NULL
2249 && per_objfile->per_bfd->index_table->version <= 7
2250 /* DWP files aren't supported yet. */
2251 && get_dwp_file (per_objfile) == NULL)
2252 queue_and_load_all_dwo_tus (cu);
2253 }
9291a0cd 2254
08ac5771
SM
2255 process_queue (per_objfile);
2256 }
9291a0cd
TT
2257
2258 /* Age the cache, releasing compilation units that have not
2259 been used recently. */
976ca316 2260 per_objfile->age_comp_units ();
9291a0cd
TT
2261}
2262
97a1449a
SM
2263/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2264 the per-objfile for which this symtab is instantiated.
2265
2266 Returns the resulting symbol table. */
2fdf6df6 2267
43f3e411 2268static struct compunit_symtab *
97a1449a 2269dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2270 dwarf2_per_objfile *per_objfile,
97a1449a 2271 bool skip_partial)
9291a0cd 2272{
976ca316 2273 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2274
976ca316 2275 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2276 {
976ca316 2277 free_cached_comp_units freer (per_objfile);
c83dd867 2278 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2279 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2280 process_cu_includes (per_objfile);
9291a0cd 2281 }
f194fefb 2282
976ca316 2283 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2284}
2285
d3473f0c
TT
2286/* See read.h. */
2287
473ab964 2288dwarf2_per_cu_data_up
5989a64e 2289dwarf2_per_bfd::allocate_per_cu ()
d3473f0c 2290{
473ab964 2291 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
1859c670 2292 result->per_bfd = this;
d3473f0c
TT
2293 result->index = m_num_psymtabs++;
2294 return result;
2295}
2296
2297/* See read.h. */
2298
0d305d5c 2299std::unique_ptr<signatured_type>
5989a64e 2300dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c 2301{
0d305d5c
TT
2302 std::unique_ptr<signatured_type> result (new signatured_type);
2303 result->per_bfd = this;
2304 result->index = m_num_psymtabs++;
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 {
0d305d5c 2386 std::unique_ptr<signatured_type> 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
168c9250 2400 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2401 sig_type->signature = signature;
9c541725 2402 sig_type->type_offset_in_tu = type_offset_in_tu;
0d305d5c
TT
2403 sig_type->is_debug_types = 1;
2404 sig_type->section = section;
2405 sig_type->sect_off = sect_off;
2406 sig_type->v.quick
168c9250 2407 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2408 struct dwarf2_per_cu_quick_data);
2409
0d305d5c
TT
2410 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2411 *slot = sig_type.get ();
1fd400ff 2412
473ab964 2413 per_bfd->all_comp_units.emplace_back (sig_type.release ());
1fd400ff
TT
2414 }
2415
168c9250 2416 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2417}
2418
927aa2e7
JK
2419/* Create the signatured type hash table from .debug_names. */
2420
2421static void
2422create_signatured_type_table_from_debug_names
976ca316 2423 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2424 const mapped_debug_names &map,
2425 struct dwarf2_section_info *section,
2426 struct dwarf2_section_info *abbrev_section)
2427{
976ca316 2428 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2429
96b79293
TT
2430 section->read (objfile);
2431 abbrev_section->read (objfile);
927aa2e7 2432
298e9637 2433 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2434
2435 for (uint32_t i = 0; i < map.tu_count; ++i)
2436 {
0d305d5c 2437 std::unique_ptr<signatured_type> sig_type;
927aa2e7 2438 void **slot;
927aa2e7
JK
2439
2440 sect_offset sect_off
2441 = (sect_offset) (extract_unsigned_integer
2442 (map.tu_table_reordered + i * map.offset_size,
2443 map.offset_size,
2444 map.dwarf5_byte_order));
2445
2446 comp_unit_head cu_header;
976ca316 2447 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2448 abbrev_section,
927aa2e7
JK
2449 section->buffer + to_underlying (sect_off),
2450 rcuh_kind::TYPE);
2451
976ca316 2452 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2453 sig_type->signature = cu_header.signature;
2454 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
0d305d5c
TT
2455 sig_type->is_debug_types = 1;
2456 sig_type->section = section;
2457 sig_type->sect_off = sect_off;
2458 sig_type->v.quick
976ca316 2459 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2460 struct dwarf2_per_cu_quick_data);
2461
0d305d5c
TT
2462 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2463 *slot = sig_type.get ();
927aa2e7 2464
473ab964 2465 per_objfile->per_bfd->all_comp_units.emplace_back (sig_type.release ());
927aa2e7
JK
2466 }
2467
976ca316 2468 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2469}
2470
9291a0cd 2471/* Read the address map data from the mapped index, and use it to
efd7398e 2472 populate the psymtabs_addrmap. */
2fdf6df6 2473
9291a0cd 2474static void
976ca316 2475create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2476 struct mapped_index *index)
9291a0cd 2477{
976ca316 2478 struct objfile *objfile = per_objfile->objfile;
efd7398e 2479 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
08feed99 2480 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2481 const gdb_byte *iter, *end;
9291a0cd 2482 struct addrmap *mutable_map;
9291a0cd
TT
2483 CORE_ADDR baseaddr;
2484
8268c778
PA
2485 auto_obstack temp_obstack;
2486
9291a0cd
TT
2487 mutable_map = addrmap_create_mutable (&temp_obstack);
2488
f00a2de2
PA
2489 iter = index->address_table.data ();
2490 end = iter + index->address_table.size ();
9291a0cd 2491
b3b3bada 2492 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2493
2494 while (iter < end)
2495 {
2496 ULONGEST hi, lo, cu_index;
2497 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2498 iter += 8;
2499 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2500 iter += 8;
2501 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2502 iter += 4;
f652bce2 2503
24a55014 2504 if (lo > hi)
f652bce2 2505 {
b98664d3 2506 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2507 hex_string (lo), hex_string (hi));
24a55014 2508 continue;
f652bce2 2509 }
24a55014 2510
efd7398e 2511 if (cu_index >= per_bfd->all_comp_units.size ())
f652bce2 2512 {
b98664d3 2513 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2514 (unsigned) cu_index);
24a55014 2515 continue;
f652bce2 2516 }
24a55014 2517
79748972
TT
2518 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2519 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2520 addrmap_set_empty (mutable_map, lo, hi - 1,
efd7398e 2521 per_bfd->get_cu (cu_index));
9291a0cd
TT
2522 }
2523
efd7398e
TT
2524 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2525 &per_bfd->obstack);
9291a0cd
TT
2526}
2527
927aa2e7 2528/* Read the address map data from DWARF-5 .debug_aranges, and use it to
efd7398e 2529 populate the psymtabs_addrmap. */
927aa2e7
JK
2530
2531static void
976ca316 2532create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2533 struct dwarf2_section_info *section)
2534{
976ca316 2535 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2536 bfd *abfd = objfile->obfd;
08feed99 2537 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2538 const CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e 2539 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
2540
2541 auto_obstack temp_obstack;
2542 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2543
2544 std::unordered_map<sect_offset,
2545 dwarf2_per_cu_data *,
2546 gdb::hash_enum<sect_offset>>
2547 debug_info_offset_to_per_cu;
0d305d5c 2548 for (const auto &per_cu : per_bfd->all_comp_units)
927aa2e7 2549 {
927aa2e7 2550 const auto insertpair
0d305d5c
TT
2551 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2552 per_cu.get ());
927aa2e7
JK
2553 if (!insertpair.second)
2554 {
2555 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2556 "debug_info_offset %s, ignoring .debug_aranges."),
2557 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2558 return;
2559 }
2560 }
2561
96b79293 2562 section->read (objfile);
927aa2e7
JK
2563
2564 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2565
2566 const gdb_byte *addr = section->buffer;
2567
2568 while (addr < section->buffer + section->size)
2569 {
2570 const gdb_byte *const entry_addr = addr;
2571 unsigned int bytes_read;
2572
2573 const LONGEST entry_length = read_initial_length (abfd, addr,
2574 &bytes_read);
2575 addr += bytes_read;
2576
2577 const gdb_byte *const entry_end = addr + entry_length;
2578 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2579 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2580 if (addr + entry_length > section->buffer + section->size)
2581 {
47e3f474 2582 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2583 "length %s exceeds section length %s, "
927aa2e7 2584 "ignoring .debug_aranges."),
47e3f474
TV
2585 objfile_name (objfile),
2586 plongest (entry_addr - section->buffer),
927aa2e7
JK
2587 plongest (bytes_read + entry_length),
2588 pulongest (section->size));
2589 return;
2590 }
2591
2592 /* The version number. */
2593 const uint16_t version = read_2_bytes (abfd, addr);
2594 addr += 2;
2595 if (version != 2)
2596 {
47e3f474 2597 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2598 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2599 objfile_name (objfile),
2600 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2601 return;
2602 }
2603
2604 const uint64_t debug_info_offset
2605 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2606 addr += offset_size;
2607 const auto per_cu_it
2608 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2609 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2610 {
47e3f474 2611 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2612 "debug_info_offset %s does not exists, "
2613 "ignoring .debug_aranges."),
47e3f474
TV
2614 objfile_name (objfile),
2615 plongest (entry_addr - section->buffer),
927aa2e7
JK
2616 pulongest (debug_info_offset));
2617 return;
2618 }
2619 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2620
2621 const uint8_t address_size = *addr++;
2622 if (address_size < 1 || address_size > 8)
2623 {
47e3f474 2624 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2625 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2626 objfile_name (objfile),
2627 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2628 return;
2629 }
2630
2631 const uint8_t segment_selector_size = *addr++;
2632 if (segment_selector_size != 0)
2633 {
47e3f474 2634 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2635 "segment_selector_size %u is not supported, "
2636 "ignoring .debug_aranges."),
47e3f474
TV
2637 objfile_name (objfile),
2638 plongest (entry_addr - section->buffer),
927aa2e7
JK
2639 segment_selector_size);
2640 return;
2641 }
2642
2643 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2644 size. It is undocumented by the DWARF standard but GCC does
2645 use it. */
927aa2e7
JK
2646 for (size_t padding = ((-(addr - section->buffer))
2647 & (2 * address_size - 1));
dda83cd7 2648 padding > 0; padding--)
927aa2e7
JK
2649 if (*addr++ != 0)
2650 {
47e3f474 2651 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2652 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2653 objfile_name (objfile),
2654 plongest (entry_addr - section->buffer));
927aa2e7
JK
2655 return;
2656 }
2657
2658 for (;;)
2659 {
2660 if (addr + 2 * address_size > entry_end)
2661 {
47e3f474 2662 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2663 "address list is not properly terminated, "
2664 "ignoring .debug_aranges."),
47e3f474
TV
2665 objfile_name (objfile),
2666 plongest (entry_addr - section->buffer));
927aa2e7
JK
2667 return;
2668 }
2669 ULONGEST start = extract_unsigned_integer (addr, address_size,
2670 dwarf5_byte_order);
2671 addr += address_size;
2672 ULONGEST length = extract_unsigned_integer (addr, address_size,
2673 dwarf5_byte_order);
2674 addr += address_size;
2675 if (start == 0 && length == 0)
2676 break;
efd7398e 2677 if (start == 0 && !per_bfd->has_section_at_zero)
927aa2e7
JK
2678 {
2679 /* Symbol was eliminated due to a COMDAT group. */
2680 continue;
2681 }
2682 ULONGEST end = start + length;
79748972
TT
2683 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2684 - baseaddr);
2685 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2686 - baseaddr);
927aa2e7
JK
2687 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2688 }
2689 }
2690
efd7398e
TT
2691 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2692 &per_bfd->obstack);
927aa2e7
JK
2693}
2694
4485a1c1
SM
2695/* A helper function that reads the .gdb_index from BUFFER and fills
2696 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2697 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2698 ok to use deprecated sections.
2699
2700 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2701 out parameters that are filled in with information about the CU and
2702 TU lists in the section.
2703
4485a1c1 2704 Returns true if all went well, false otherwise. */
2fdf6df6 2705
d33bc52e 2706static bool
3810f182 2707read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2708 bool deprecated_ok,
2709 gdb::array_view<const gdb_byte> buffer,
2710 struct mapped_index *map,
2711 const gdb_byte **cu_list,
2712 offset_type *cu_list_elements,
2713 const gdb_byte **types_list,
2714 offset_type *types_list_elements)
2715{
2716 const gdb_byte *addr = &buffer[0];
42c2c694 2717 offset_view metadata (buffer);
82430852 2718
9291a0cd 2719 /* Version check. */
42c2c694 2720 offset_type version = metadata[0];
987d643c 2721 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2722 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2723 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2724 indices. */
831adc1f 2725 if (version < 4)
481860b3
GB
2726 {
2727 static int warning_printed = 0;
2728 if (!warning_printed)
2729 {
2730 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2731 filename);
481860b3
GB
2732 warning_printed = 1;
2733 }
2734 return 0;
2735 }
2736 /* Index version 4 uses a different hash function than index version
2737 5 and later.
2738
2739 Versions earlier than 6 did not emit psymbols for inlined
2740 functions. Using these files will cause GDB not to be able to
2741 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2742 indices unless the user has done
2743 "set use-deprecated-index-sections on". */
2ec9a5e0 2744 if (version < 6 && !deprecated_ok)
481860b3
GB
2745 {
2746 static int warning_printed = 0;
2747 if (!warning_printed)
2748 {
e615022a
DE
2749 warning (_("\
2750Skipping deprecated .gdb_index section in %s.\n\
2751Do \"set use-deprecated-index-sections on\" before the file is read\n\
2752to use the section anyway."),
2ec9a5e0 2753 filename);
481860b3
GB
2754 warning_printed = 1;
2755 }
2756 return 0;
2757 }
796a7ff8 2758 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2759 of the TU (for symbols coming from TUs),
2760 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2761 Plus gold-generated indices can have duplicate entries for global symbols,
2762 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2763 These are just performance bugs, and we can't distinguish gdb-generated
2764 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2765
481860b3 2766 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2767 longer backward compatible. */
796a7ff8 2768 if (version > 8)
594e8718 2769 return 0;
9291a0cd 2770
559a7a62 2771 map->version = version;
9291a0cd 2772
42c2c694
TT
2773 int i = 1;
2774 *cu_list = addr + metadata[i];
2775 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
1fd400ff
TT
2776 ++i;
2777
42c2c694
TT
2778 *types_list = addr + metadata[i];
2779 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
987d643c 2780 ++i;
1fd400ff 2781
42c2c694
TT
2782 const gdb_byte *address_table = addr + metadata[i];
2783 const gdb_byte *address_table_end = addr + metadata[i + 1];
f00a2de2
PA
2784 map->address_table
2785 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2786 ++i;
2787
42c2c694
TT
2788 const gdb_byte *symbol_table = addr + metadata[i];
2789 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
f00a2de2 2790 map->symbol_table
42c2c694
TT
2791 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2792 symbol_table_end));
9291a0cd 2793
f00a2de2 2794 ++i;
42c2c694 2795 map->constant_pool = buffer.slice (metadata[i]);
1fd400ff 2796
2ec9a5e0
TT
2797 return 1;
2798}
2799
4485a1c1
SM
2800/* Callback types for dwarf2_read_gdb_index. */
2801
2802typedef gdb::function_view
5989a64e 2803 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
2804 get_gdb_index_contents_ftype;
2805typedef gdb::function_view
2806 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2807 get_gdb_index_contents_dwz_ftype;
2808
927aa2e7 2809/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2810 elements of all the CUs and return 1. Otherwise, return 0. */
2811
2812static int
4485a1c1 2813dwarf2_read_gdb_index
976ca316 2814 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
2815 get_gdb_index_contents_ftype get_gdb_index_contents,
2816 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 2817{
2ec9a5e0
TT
2818 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2819 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2820 struct dwz_file *dwz;
976ca316 2821 struct objfile *objfile = per_objfile->objfile;
f8c41851 2822 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 2823
4485a1c1 2824 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 2825 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
2826
2827 if (main_index_contents.empty ())
2828 return 0;
2829
3063847f 2830 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 2831 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
2832 use_deprecated_index_sections,
2833 main_index_contents, map.get (), &cu_list,
2834 &cu_list_elements, &types_list,
2835 &types_list_elements))
2ec9a5e0
TT
2836 return 0;
2837
0fefef59 2838 /* Don't use the index if it's empty. */
3063847f 2839 if (map->symbol_table.empty ())
0fefef59
DE
2840 return 0;
2841
2ec9a5e0
TT
2842 /* If there is a .dwz file, read it so we can get its CU list as
2843 well. */
f8c41851 2844 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 2845 if (dwz != NULL)
2ec9a5e0 2846 {
2ec9a5e0
TT
2847 struct mapped_index dwz_map;
2848 const gdb_byte *dwz_types_ignore;
2849 offset_type dwz_types_elements_ignore;
2850
4485a1c1
SM
2851 gdb::array_view<const gdb_byte> dwz_index_content
2852 = get_gdb_index_contents_dwz (objfile, dwz);
2853
2854 if (dwz_index_content.empty ())
2855 return 0;
2856
3810f182 2857 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 2858 1, dwz_index_content, &dwz_map,
4485a1c1
SM
2859 &dwz_list, &dwz_list_elements,
2860 &dwz_types_ignore,
2861 &dwz_types_elements_ignore))
2ec9a5e0
TT
2862 {
2863 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 2864 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
2865 return 0;
2866 }
2867 }
2868
f8c41851
SM
2869 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2870 dwz_list_elements);
1fd400ff 2871
8b70b953
TT
2872 if (types_list_elements)
2873 {
8b70b953
TT
2874 /* We can only handle a single .debug_types when we have an
2875 index. */
f8c41851 2876 if (per_bfd->types.size () != 1)
8b70b953
TT
2877 return 0;
2878
f8c41851 2879 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 2880
f8c41851 2881 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 2882 types_list_elements);
8b70b953 2883 }
9291a0cd 2884
976ca316 2885 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 2886
f8c41851
SM
2887 per_bfd->index_table = std::move (map);
2888 per_bfd->using_index = 1;
2889 per_bfd->quick_file_names_table =
2890 create_quick_file_names_table (per_bfd->all_comp_units.size ());
2891
9291a0cd
TT
2892 return 1;
2893}
2894
dee91e82 2895/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2896
dee91e82
DE
2897static void
2898dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 2899 struct die_info *comp_unit_die)
9291a0cd 2900{
dee91e82 2901 struct dwarf2_cu *cu = reader->cu;
ed2dc618 2902 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 2903 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 2904 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 2905 struct attribute *attr;
7b9f3c50
DE
2906 void **slot;
2907 struct quick_file_names *qfn;
9291a0cd 2908
0186c6a7
DE
2909 gdb_assert (! this_cu->is_debug_types);
2910
07261596
TT
2911 /* Our callers never want to match partial units -- instead they
2912 will match the enclosing full CU. */
2913 if (comp_unit_die->tag == DW_TAG_partial_unit)
2914 {
2915 this_cu->v.quick->no_file_data = 1;
2916 return;
2917 }
2918
0186c6a7 2919 lh_cu = this_cu;
7b9f3c50 2920 slot = NULL;
dee91e82 2921
fff8551c 2922 line_header_up lh;
9c541725 2923 sect_offset line_offset {};
fff8551c 2924
dee91e82 2925 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 2926 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 2927 {
7b9f3c50
DE
2928 struct quick_file_names find_entry;
2929
d4df075e 2930 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
2931
2932 /* We may have already read in this line header (TU line header sharing).
2933 If we have we're done. */
094b34ac 2934 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 2935 find_entry.hash.line_sect_off = line_offset;
976ca316 2936 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
2937 &find_entry, INSERT);
2938 if (*slot != NULL)
2939 {
9a3c8263 2940 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 2941 return;
7b9f3c50
DE
2942 }
2943
3019eac3 2944 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2945 }
2946 if (lh == NULL)
2947 {
094b34ac 2948 lh_cu->v.quick->no_file_data = 1;
dee91e82 2949 return;
9291a0cd
TT
2950 }
2951
976ca316 2952 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 2953 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 2954 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
2955 gdb_assert (slot != NULL);
2956 *slot = qfn;
9291a0cd 2957
d721ba37 2958 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 2959
aa391654
TT
2960 int offset = 0;
2961 if (strcmp (fnd.name, "<unknown>") != 0)
2962 ++offset;
2963
7ba99d21 2964 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 2965 qfn->file_names =
976ca316 2966 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 2967 qfn->num_file_names);
aa391654
TT
2968 if (offset != 0)
2969 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 2970 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
2971 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
2972 fnd.comp_dir).release ();
7b9f3c50 2973 qfn->real_names = NULL;
9291a0cd 2974
094b34ac 2975 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
2976}
2977
2978/* A helper for the "quick" functions which attempts to read the line
2979 table for THIS_CU. */
2980
2981static struct quick_file_names *
ab432490
SM
2982dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2983 dwarf2_per_objfile *per_objfile)
dee91e82 2984{
0186c6a7
DE
2985 /* This should never be called for TUs. */
2986 gdb_assert (! this_cu->is_debug_types);
2987 /* Nor type unit groups. */
197400e8 2988 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 2989
dee91e82
DE
2990 if (this_cu->v.quick->file_names != NULL)
2991 return this_cu->v.quick->file_names;
2992 /* If we know there is no line data, no point in looking again. */
2993 if (this_cu->v.quick->no_file_data)
2994 return NULL;
2995
ab432490 2996 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 2997 if (!reader.dummy_p)
eb5dd737 2998 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
2999
3000 if (this_cu->v.quick->no_file_data)
3001 return NULL;
3002 return this_cu->v.quick->file_names;
9291a0cd
TT
3003}
3004
3005/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3006 real path for a given file name from the line table. */
2fdf6df6 3007
9291a0cd 3008static const char *
976ca316 3009dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3010 struct quick_file_names *qfn, int index)
9291a0cd 3011{
7b9f3c50 3012 if (qfn->real_names == NULL)
976ca316 3013 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3014 qfn->num_file_names, const char *);
9291a0cd 3015
7b9f3c50 3016 if (qfn->real_names[index] == NULL)
14278e1f 3017 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3018
7b9f3c50 3019 return qfn->real_names[index];
9291a0cd
TT
3020}
3021
39298a5d
TT
3022struct symtab *
3023dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
9291a0cd 3024{
976ca316 3025 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
0d305d5c
TT
3026 dwarf2_per_cu_data *dwarf_cu
3027 = per_objfile->per_bfd->all_comp_units.back ().get ();
976ca316 3028 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3029
43f3e411
DE
3030 if (cust == NULL)
3031 return NULL;
ed2dc618 3032
43f3e411 3033 return compunit_primary_filetab (cust);
9291a0cd
TT
3034}
3035
7b9f3c50
DE
3036/* Traversal function for dw2_forget_cached_source_info. */
3037
3038static int
3039dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3040{
7b9f3c50 3041 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3042
7b9f3c50 3043 if (file_data->real_names)
9291a0cd 3044 {
7b9f3c50 3045 int i;
9291a0cd 3046
7b9f3c50 3047 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3048 {
7b9f3c50
DE
3049 xfree ((void*) file_data->real_names[i]);
3050 file_data->real_names[i] = NULL;
9291a0cd
TT
3051 }
3052 }
7b9f3c50
DE
3053
3054 return 1;
3055}
3056
39298a5d
TT
3057void
3058dwarf2_base_index_functions::forget_cached_source_info
3059 (struct objfile *objfile)
7b9f3c50 3060{
976ca316 3061 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3062
976ca316 3063 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3064 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3065}
3066
da51c347
DE
3067/* Struct used to manage iterating over all CUs looking for a symbol. */
3068
3069struct dw2_symtab_iterator
9291a0cd 3070{
ed2dc618 3071 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3072 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3073 /* If set, only look for symbols that match that block. Valid values are
3074 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3075 gdb::optional<block_enum> block_index;
da51c347
DE
3076 /* The kind of symbol we're looking for. */
3077 domain_enum domain;
3078 /* The list of CUs from the index entry of the symbol,
3079 or NULL if not found. */
42c2c694 3080 offset_view vec;
da51c347
DE
3081 /* The next element in VEC to look at. */
3082 int next;
3083 /* The number of elements in VEC, or zero if there is no match. */
3084 int length;
8943b874
DE
3085 /* Have we seen a global version of the symbol?
3086 If so we can ignore all further global instances.
3087 This is to work around gold/15646, inefficient gold-generated
3088 indices. */
3089 int global_seen;
da51c347 3090};
9291a0cd 3091
7089bd88 3092/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2fdf6df6 3093
9291a0cd 3094static void
7089bd88
TT
3095dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3096 dwarf2_per_objfile *per_objfile,
3097 gdb::optional<block_enum> block_index,
3098 domain_enum domain, offset_type namei)
da51c347 3099{
976ca316 3100 iter->per_objfile = per_objfile;
da51c347
DE
3101 iter->block_index = block_index;
3102 iter->domain = domain;
3103 iter->next = 0;
8943b874 3104 iter->global_seen = 0;
42c2c694 3105 iter->vec = {};
e5f3ece2 3106 iter->length = 0;
e5f3ece2
TV
3107
3108 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3109 /* index is NULL if OBJF_READNOW. */
3110 if (index == NULL)
3111 return;
3112
3113 gdb_assert (!index->symbol_name_slot_invalid (namei));
42c2c694 3114 offset_type vec_idx = index->symbol_vec_index (namei);
e5f3ece2 3115
42c2c694
TT
3116 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3117 iter->length = iter->vec[0];
da51c347
DE
3118}
3119
3120/* Return the next matching CU or NULL if there are no more. */
3121
3122static struct dwarf2_per_cu_data *
3123dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3124{
976ca316 3125 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3126
da51c347
DE
3127 for ( ; iter->next < iter->length; ++iter->next)
3128 {
42c2c694 3129 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
da51c347 3130 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3131 gdb_index_symbol_kind symbol_kind =
3132 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3133 /* Only check the symbol attributes if they're present.
3134 Indices prior to version 7 don't record them,
3135 and indices >= 7 may elide them for certain symbols
3136 (gold does this). */
3137 int attrs_valid =
976ca316 3138 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3139 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3140
3190f0c6 3141 /* Don't crash on bad data. */
91eea9cc 3142 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3190f0c6 3143 {
b98664d3 3144 complaint (_(".gdb_index entry has bad CU index"
976ca316 3145 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3146 continue;
3147 }
3148
91eea9cc 3149 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3190f0c6 3150
da51c347 3151 /* Skip if already read in. */
976ca316 3152 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3153 continue;
3154
8943b874
DE
3155 /* Check static vs global. */
3156 if (attrs_valid)
3157 {
2b79f376
SM
3158 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3159
3160 if (iter->block_index.has_value ())
3161 {
3162 bool want_static = *iter->block_index == STATIC_BLOCK;
3163
3164 if (is_static != want_static)
3165 continue;
3166 }
3167
8943b874 3168 /* Work around gold/15646. */
f030440d
TV
3169 if (!is_static
3170 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3171 {
3172 if (iter->global_seen)
3173 continue;
3174
3175 iter->global_seen = 1;
3176 }
8943b874 3177 }
da51c347
DE
3178
3179 /* Only check the symbol's kind if it has one. */
3180 if (attrs_valid)
3181 {
3182 switch (iter->domain)
3183 {
3184 case VAR_DOMAIN:
3185 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3186 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3187 /* Some types are also in VAR_DOMAIN. */
3188 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3189 continue;
3190 break;
3191 case STRUCT_DOMAIN:
3192 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3193 continue;
3194 break;
3195 case LABEL_DOMAIN:
3196 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3197 continue;
3198 break;
59c35742
AB
3199 case MODULE_DOMAIN:
3200 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3201 continue;
3202 break;
da51c347
DE
3203 default:
3204 break;
3205 }
3206 }
3207
3208 ++iter->next;
3209 return per_cu;
3210 }
3211
3212 return NULL;
3213}
3214
39298a5d 3215void
4829711b
TT
3216dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3217 bool print_bcache)
9291a0cd 3218{
4829711b
TT
3219 if (print_bcache)
3220 return;
3221
976ca316 3222 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
91eea9cc 3223 int total = per_objfile->per_bfd->all_comp_units.size ();
ed2dc618 3224 int count = 0;
9291a0cd 3225
ed2dc618 3226 for (int i = 0; i < total; ++i)
9291a0cd 3227 {
91eea9cc 3228 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
9291a0cd 3229
976ca316 3230 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3231 ++count;
3232 }
e4a48d9d 3233 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3234 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3235}
3236
779bd270
DE
3237/* This dumps minimal information about the index.
3238 It is called via "mt print objfiles".
3239 One use is to verify .gdb_index has been loaded by the
3240 gdb.dwarf2/gdb-index.exp testcase. */
3241
39298a5d
TT
3242void
3243dwarf2_gdb_index::dump (struct objfile *objfile)
9291a0cd 3244{
976ca316 3245 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3246
976ca316 3247 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3248 printf_filtered (".gdb_index:");
976ca316 3249 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3250 {
3251 printf_filtered (" version %d\n",
976ca316 3252 per_objfile->per_bfd->index_table->version);
779bd270
DE
3253 }
3254 else
3255 printf_filtered (" faked for \"readnow\"\n");
3256 printf_filtered ("\n");
9291a0cd
TT
3257}
3258
39298a5d
TT
3259void
3260dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
9291a0cd 3261{
976ca316 3262 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
91eea9cc 3263 int total_units = per_objfile->per_bfd->all_comp_units.size ();
9291a0cd 3264
ed2dc618 3265 for (int i = 0; i < total_units; ++i)
9291a0cd 3266 {
91eea9cc 3267 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
9291a0cd 3268
58f0c718
TT
3269 /* We don't want to directly expand a partial CU, because if we
3270 read it with the wrong language, then assertion failures can
3271 be triggered later on. See PR symtab/23010. So, tell
3272 dw2_instantiate_symtab to skip partial CUs -- any important
3273 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3274 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3275 }
3276}
3277
df35e626 3278static bool
9a0bacfb
TV
3279dw2_expand_symtabs_matching_symbol
3280 (mapped_index_base &index,
3281 const lookup_name_info &lookup_name_in,
3282 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3283 gdb::function_view<bool (offset_type)> match_callback,
3284 dwarf2_per_objfile *per_objfile);
9a0bacfb 3285
df35e626 3286static bool
9a0bacfb 3287dw2_expand_symtabs_matching_one
97a1449a
SM
3288 (dwarf2_per_cu_data *per_cu,
3289 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3290 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3291 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3292
af82f89d 3293void
0b7b2c2a 3294dwarf2_gdb_index::expand_matching_symbols
199b4314 3295 (struct objfile *objfile,
b054970d 3296 const lookup_name_info &name, domain_enum domain,
199b4314 3297 int global,
199b4314 3298 symbol_compare_ftype *ordered_compare)
9291a0cd 3299{
1aa98955 3300 /* Used for Ada. */
976ca316 3301 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3302
9a0bacfb
TV
3303 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3304
976ca316 3305 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3306 {
976ca316 3307 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3308
9a0bacfb
TV
3309 const char *match_name = name.ada ().lookup_name ().c_str ();
3310 auto matcher = [&] (const char *symname)
3311 {
3312 if (ordered_compare == nullptr)
3313 return true;
3314 return ordered_compare (symname, match_name) == 0;
3315 };
3316
b64f703b 3317 dw2_expand_symtabs_matching_symbol (index, name, matcher,
9a0bacfb
TV
3318 [&] (offset_type namei)
3319 {
3320 struct dw2_symtab_iterator iter;
3321 struct dwarf2_per_cu_data *per_cu;
3322
976ca316 3323 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3324 namei);
9a0bacfb 3325 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3326 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3327 nullptr);
9a0bacfb 3328 return true;
976ca316 3329 }, per_objfile);
9a0bacfb
TV
3330 }
3331 else
3332 {
3333 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3334 proceed assuming all symtabs have been read in. */
3335 }
9291a0cd
TT
3336}
3337
e1ef7d7a
PA
3338/* Starting from a search name, return the string that finds the upper
3339 bound of all strings that start with SEARCH_NAME in a sorted name
3340 list. Returns the empty string to indicate that the upper bound is
3341 the end of the list. */
3342
3343static std::string
3344make_sort_after_prefix_name (const char *search_name)
3345{
3346 /* When looking to complete "func", we find the upper bound of all
3347 symbols that start with "func" by looking for where we'd insert
3348 the closest string that would follow "func" in lexicographical
3349 order. Usually, that's "func"-with-last-character-incremented,
3350 i.e. "fund". Mind non-ASCII characters, though. Usually those
3351 will be UTF-8 multi-byte sequences, but we can't be certain.
3352 Especially mind the 0xff character, which is a valid character in
3353 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3354 rule out compilers allowing it in identifiers. Note that
3355 conveniently, strcmp/strcasecmp are specified to compare
3356 characters interpreted as unsigned char. So what we do is treat
3357 the whole string as a base 256 number composed of a sequence of
3358 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3359 to 0, and carries 1 to the following more-significant position.
3360 If the very first character in SEARCH_NAME ends up incremented
3361 and carries/overflows, then the upper bound is the end of the
3362 list. The string after the empty string is also the empty
3363 string.
3364
3365 Some examples of this operation:
3366
3367 SEARCH_NAME => "+1" RESULT
3368
3369 "abc" => "abd"
3370 "ab\xff" => "ac"
3371 "\xff" "a" "\xff" => "\xff" "b"
3372 "\xff" => ""
3373 "\xff\xff" => ""
3374 "" => ""
3375
3376 Then, with these symbols for example:
3377
3378 func
3379 func1
3380 fund
3381
3382 completing "func" looks for symbols between "func" and
3383 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3384 which finds "func" and "func1", but not "fund".
3385
3386 And with:
3387
3388 funcÿ (Latin1 'ÿ' [0xff])
3389 funcÿ1
3390 fund
3391
3392 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3393 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3394
3395 And with:
3396
3397 ÿÿ (Latin1 'ÿ' [0xff])
3398 ÿÿ1
3399
3400 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3401 the end of the list.
3402 */
3403 std::string after = search_name;
3404 while (!after.empty () && (unsigned char) after.back () == 0xff)
3405 after.pop_back ();
3406 if (!after.empty ())
3407 after.back () = (unsigned char) after.back () + 1;
3408 return after;
3409}
3410
5c58de74 3411/* See declaration. */
61d96d7e 3412
5c58de74
PA
3413std::pair<std::vector<name_component>::const_iterator,
3414 std::vector<name_component>::const_iterator>
44ed8f3e 3415mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3416 (const lookup_name_info &lookup_name_without_params, language lang,
3417 dwarf2_per_objfile *per_objfile) const
3f563c84 3418{
5c58de74
PA
3419 auto *name_cmp
3420 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3421
3b00ef10 3422 const char *lang_name
e0802d59 3423 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3424
3f563c84
PA
3425 /* Comparison function object for lower_bound that matches against a
3426 given symbol name. */
3427 auto lookup_compare_lower = [&] (const name_component &elem,
3428 const char *name)
3429 {
fcf23d5b 3430 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3431 const char *elem_name = elem_qualified + elem.name_offset;
3432 return name_cmp (elem_name, name) < 0;
3433 };
3434
3435 /* Comparison function object for upper_bound that matches against a
3436 given symbol name. */
3437 auto lookup_compare_upper = [&] (const char *name,
3438 const name_component &elem)
3439 {
fcf23d5b 3440 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3441 const char *elem_name = elem_qualified + elem.name_offset;
3442 return name_cmp (name, elem_name) < 0;
3443 };
3444
5c58de74
PA
3445 auto begin = this->name_components.begin ();
3446 auto end = this->name_components.end ();
3f563c84
PA
3447
3448 /* Find the lower bound. */
3449 auto lower = [&] ()
3450 {
3b00ef10 3451 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3452 return begin;
3453 else
3b00ef10 3454 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3455 } ();
3456
3457 /* Find the upper bound. */
3458 auto upper = [&] ()
3459 {
5c58de74 3460 if (lookup_name_without_params.completion_mode ())
3f563c84 3461 {
e1ef7d7a
PA
3462 /* In completion mode, we want UPPER to point past all
3463 symbols names that have the same prefix. I.e., with
3464 these symbols, and completing "func":
3465
3466 function << lower bound
3467 function1
3468 other_function << upper bound
3469
3470 We find the upper bound by looking for the insertion
3471 point of "func"-with-last-character-incremented,
3472 i.e. "fund". */
3b00ef10 3473 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3474 if (after.empty ())
3f563c84 3475 return end;
e6b2f5ef
PA
3476 return std::lower_bound (lower, end, after.c_str (),
3477 lookup_compare_lower);
3f563c84
PA
3478 }
3479 else
3b00ef10 3480 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3481 } ();
3482
5c58de74
PA
3483 return {lower, upper};
3484}
3485
3486/* See declaration. */
3487
3488void
fcf23d5b 3489mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3490{
3491 if (!this->name_components.empty ())
3492 return;
3493
3494 this->name_components_casing = case_sensitivity;
3495 auto *name_cmp
3496 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3497
3498 /* The code below only knows how to break apart components of C++
3499 symbol names (and other languages that use '::' as
3b00ef10 3500 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3501 auto count = this->symbol_name_count ();
3502 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3503 {
44ed8f3e 3504 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3505 continue;
3506
fcf23d5b 3507 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3508
3509 /* Add each name component to the name component table. */
3510 unsigned int previous_len = 0;
3b00ef10
TT
3511
3512 if (strstr (name, "::") != nullptr)
3513 {
3514 for (unsigned int current_len = cp_find_first_component (name);
3515 name[current_len] != '\0';
3516 current_len += cp_find_first_component (name + current_len))
3517 {
3518 gdb_assert (name[current_len] == ':');
3519 this->name_components.push_back ({previous_len, idx});
3520 /* Skip the '::'. */
3521 current_len += 2;
3522 previous_len = current_len;
3523 }
3524 }
3525 else
5c58de74 3526 {
3b00ef10
TT
3527 /* Handle the Ada encoded (aka mangled) form here. */
3528 for (const char *iter = strstr (name, "__");
3529 iter != nullptr;
3530 iter = strstr (iter, "__"))
3531 {
3532 this->name_components.push_back ({previous_len, idx});
3533 iter += 2;
3534 previous_len = iter - name;
3535 }
5c58de74 3536 }
3b00ef10 3537
5c58de74
PA
3538 this->name_components.push_back ({previous_len, idx});
3539 }
3540
3541 /* Sort name_components elements by name. */
3542 auto name_comp_compare = [&] (const name_component &left,
3543 const name_component &right)
3544 {
fcf23d5b
SM
3545 const char *left_qualified
3546 = this->symbol_name_at (left.idx, per_objfile);
3547 const char *right_qualified
3548 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
3549
3550 const char *left_name = left_qualified + left.name_offset;
3551 const char *right_name = right_qualified + right.name_offset;
3552
3553 return name_cmp (left_name, right_name) < 0;
3554 };
3555
3556 std::sort (this->name_components.begin (),
3557 this->name_components.end (),
3558 name_comp_compare);
3559}
3560
3561/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3562 mapped_index_base instead of the containing objfile. This is split
3563 to a separate function in order to be able to unit test the
3564 name_components matching using a mock mapped_index_base. For each
5c58de74 3565 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3566 symbol's index in the mapped_index_base symbol table. */
5c58de74 3567
df35e626 3568static bool
5c58de74 3569dw2_expand_symtabs_matching_symbol
44ed8f3e 3570 (mapped_index_base &index,
5c58de74
PA
3571 const lookup_name_info &lookup_name_in,
3572 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3573 gdb::function_view<bool (offset_type)> match_callback,
3574 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3575{
3576 lookup_name_info lookup_name_without_params
3577 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3578
3579 /* Build the symbol name component sorted vector, if we haven't
3580 yet. */
fcf23d5b 3581 index.build_name_components (per_objfile);
5c58de74 3582
3f563c84
PA
3583 /* The same symbol may appear more than once in the range though.
3584 E.g., if we're looking for symbols that complete "w", and we have
3585 a symbol named "w1::w2", we'll find the two name components for
3586 that same symbol in the range. To be sure we only call the
3587 callback once per symbol, we first collect the symbol name
3588 indexes that matched in a temporary vector and ignore
3589 duplicates. */
3590 std::vector<offset_type> matches;
3f563c84 3591
3b00ef10
TT
3592 struct name_and_matcher
3593 {
3594 symbol_name_matcher_ftype *matcher;
ecc6c606 3595 const char *name;
3b00ef10
TT
3596
3597 bool operator== (const name_and_matcher &other) const
3f563c84 3598 {
ecc6c606 3599 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3600 }
3601 };
3602
3603 /* A vector holding all the different symbol name matchers, for all
3604 languages. */
3605 std::vector<name_and_matcher> matchers;
3606
3607 for (int i = 0; i < nr_languages; i++)
3608 {
3609 enum language lang_e = (enum language) i;
3610
3611 const language_defn *lang = language_def (lang_e);
3612 symbol_name_matcher_ftype *name_matcher
c9debfb9 3613 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 3614
3b00ef10 3615 name_and_matcher key {
dda83cd7 3616 name_matcher,
3b00ef10
TT
3617 lookup_name_without_params.language_lookup_name (lang_e)
3618 };
3619
3620 /* Don't insert the same comparison routine more than once.
3621 Note that we do this linear walk. This is not a problem in
3622 practice because the number of supported languages is
3623 low. */
3624 if (std::find (matchers.begin (), matchers.end (), key)
3625 != matchers.end ())
9291a0cd 3626 continue;
3b00ef10
TT
3627 matchers.push_back (std::move (key));
3628
3629 auto bounds
3630 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 3631 lang_e, per_objfile);
3b00ef10
TT
3632
3633 /* Now for each symbol name in range, check to see if we have a name
3634 match, and if so, call the MATCH_CALLBACK callback. */
3635
3636 for (; bounds.first != bounds.second; ++bounds.first)
3637 {
fcf23d5b
SM
3638 const char *qualified
3639 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
3640
3641 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3642 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3643 continue;
9291a0cd 3644
3b00ef10
TT
3645 matches.push_back (bounds.first->idx);
3646 }
3f563c84
PA
3647 }
3648
3649 std::sort (matches.begin (), matches.end ());
3650
3651 /* Finally call the callback, once per match. */
3652 ULONGEST prev = -1;
df35e626 3653 bool result = true;
3f563c84
PA
3654 for (offset_type idx : matches)
3655 {
3656 if (prev != idx)
3657 {
3b00ef10 3658 if (!match_callback (idx))
df35e626
TT
3659 {
3660 result = false;
3661 break;
3662 }
3f563c84
PA
3663 prev = idx;
3664 }
3665 }
3666
3667 /* Above we use a type wider than idx's for 'prev', since 0 and
3668 (offset_type)-1 are both possible values. */
3669 static_assert (sizeof (prev) > sizeof (offset_type), "");
df35e626
TT
3670
3671 return result;
3f563c84
PA
3672}
3673
c62446b1
PA
3674#if GDB_SELF_TEST
3675
3676namespace selftests { namespace dw2_expand_symtabs_matching {
3677
a3c5fafd
PA
3678/* A mock .gdb_index/.debug_names-like name index table, enough to
3679 exercise dw2_expand_symtabs_matching_symbol, which works with the
3680 mapped_index_base interface. Builds an index from the symbol list
3681 passed as parameter to the constructor. */
3682class mock_mapped_index : public mapped_index_base
c62446b1
PA
3683{
3684public:
a3c5fafd
PA
3685 mock_mapped_index (gdb::array_view<const char *> symbols)
3686 : m_symbol_table (symbols)
c62446b1
PA
3687 {}
3688
a3c5fafd 3689 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 3690
a3c5fafd 3691 /* Return the number of names in the symbol table. */
632e107b 3692 size_t symbol_name_count () const override
c62446b1 3693 {
a3c5fafd 3694 return m_symbol_table.size ();
c62446b1
PA
3695 }
3696
a3c5fafd 3697 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
3698 const char *symbol_name_at
3699 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
3700 {
3701 return m_symbol_table[idx];
3702 }
c62446b1 3703
a3c5fafd
PA
3704private:
3705 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
3706};
3707
3708/* Convenience function that converts a NULL pointer to a "<null>"
3709 string, to pass to print routines. */
3710
3711static const char *
3712string_or_null (const char *str)
3713{
3714 return str != NULL ? str : "<null>";
3715}
3716
3717/* Check if a lookup_name_info built from
3718 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3719 index. EXPECTED_LIST is the list of expected matches, in expected
3720 matching order. If no match expected, then an empty list is
3721 specified. Returns true on success. On failure prints a warning
3722 indicating the file:line that failed, and returns false. */
3723
3724static bool
3725check_match (const char *file, int line,
3726 mock_mapped_index &mock_index,
3727 const char *name, symbol_name_match_type match_type,
3728 bool completion_mode,
fcf23d5b
SM
3729 std::initializer_list<const char *> expected_list,
3730 dwarf2_per_objfile *per_objfile)
c62446b1
PA
3731{
3732 lookup_name_info lookup_name (name, match_type, completion_mode);
3733
3734 bool matched = true;
3735
3736 auto mismatch = [&] (const char *expected_str,
3737 const char *got)
3738 {
3739 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3740 "expected=\"%s\", got=\"%s\"\n"),
3741 file, line,
3742 (match_type == symbol_name_match_type::FULL
3743 ? "FULL" : "WILD"),
3744 name, string_or_null (expected_str), string_or_null (got));
3745 matched = false;
3746 };
3747
3748 auto expected_it = expected_list.begin ();
3749 auto expected_end = expected_list.end ();
3750
a3c5fafd 3751 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
b64f703b 3752 nullptr,
c62446b1
PA
3753 [&] (offset_type idx)
3754 {
fcf23d5b 3755 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
3756 const char *expected_str
3757 = expected_it == expected_end ? NULL : *expected_it++;
3758
3759 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3760 mismatch (expected_str, matched_name);
3b00ef10 3761 return true;
fcf23d5b 3762 }, per_objfile);
c62446b1
PA
3763
3764 const char *expected_str
3765 = expected_it == expected_end ? NULL : *expected_it++;
3766 if (expected_str != NULL)
3767 mismatch (expected_str, NULL);
3768
3769 return matched;
3770}
3771
3772/* The symbols added to the mock mapped_index for testing (in
3773 canonical form). */
3774static const char *test_symbols[] = {
3775 "function",
3776 "std::bar",
3777 "std::zfunction",
3778 "std::zfunction2",
3779 "w1::w2",
3780 "ns::foo<char*>",
3781 "ns::foo<int>",
3782 "ns::foo<long>",
a20714ff
PA
3783 "ns2::tmpl<int>::foo2",
3784 "(anonymous namespace)::A::B::C",
c62446b1 3785
e1ef7d7a
PA
3786 /* These are used to check that the increment-last-char in the
3787 matching algorithm for completion doesn't match "t1_fund" when
3788 completing "t1_func". */
3789 "t1_func",
3790 "t1_func1",
3791 "t1_fund",
3792 "t1_fund1",
3793
3794 /* A UTF-8 name with multi-byte sequences to make sure that
3795 cp-name-parser understands this as a single identifier ("função"
3796 is "function" in PT). */
3797 u8"u8função",
3798
3799 /* \377 (0xff) is Latin1 'ÿ'. */
3800 "yfunc\377",
3801
3802 /* \377 (0xff) is Latin1 'ÿ'. */
3803 "\377",
3804 "\377\377123",
3805
c62446b1
PA
3806 /* A name with all sorts of complications. Starts with "z" to make
3807 it easier for the completion tests below. */
3808#define Z_SYM_NAME \
3809 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3810 "::tuple<(anonymous namespace)::ui*, " \
3811 "std::default_delete<(anonymous namespace)::ui>, void>"
3812
3813 Z_SYM_NAME
3814};
3815
a3c5fafd
PA
3816/* Returns true if the mapped_index_base::find_name_component_bounds
3817 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3818 in completion mode. */
5c58de74
PA
3819
3820static bool
a3c5fafd 3821check_find_bounds_finds (mapped_index_base &index,
5c58de74 3822 const char *search_name,
fcf23d5b
SM
3823 gdb::array_view<const char *> expected_syms,
3824 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3825{
3826 lookup_name_info lookup_name (search_name,
3827 symbol_name_match_type::FULL, true);
3828
3b00ef10 3829 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
3830 language_cplus,
3831 per_objfile);
5c58de74
PA
3832
3833 size_t distance = std::distance (bounds.first, bounds.second);
3834 if (distance != expected_syms.size ())
3835 return false;
3836
3837 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3838 {
3839 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 3840 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
3841 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3842 return false;
3843 }
3844
3845 return true;
3846}
3847
3848/* Test the lower-level mapped_index::find_name_component_bounds
3849 method. */
3850
c62446b1 3851static void
5c58de74
PA
3852test_mapped_index_find_name_component_bounds ()
3853{
3854 mock_mapped_index mock_index (test_symbols);
3855
fcf23d5b 3856 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
3857
3858 /* Test the lower-level mapped_index::find_name_component_bounds
3859 method in completion mode. */
3860 {
3861 static const char *expected_syms[] = {
3862 "t1_func",
3863 "t1_func1",
5c58de74
PA
3864 };
3865
fcf23d5b
SM
3866 SELF_CHECK (check_find_bounds_finds
3867 (mock_index, "t1_func", expected_syms,
3868 NULL /* per_objfile */));
5c58de74
PA
3869 }
3870
3871 /* Check that the increment-last-char in the name matching algorithm
3872 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3873 {
3874 static const char *expected_syms1[] = {
3875 "\377",
3876 "\377\377123",
3877 };
fcf23d5b
SM
3878 SELF_CHECK (check_find_bounds_finds
3879 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
3880
3881 static const char *expected_syms2[] = {
3882 "\377\377123",
3883 };
fcf23d5b
SM
3884 SELF_CHECK (check_find_bounds_finds
3885 (mock_index, "\377\377", expected_syms2,
3886 NULL /* per_objfile */));
5c58de74
PA
3887 }
3888}
3889
3890/* Test dw2_expand_symtabs_matching_symbol. */
3891
3892static void
3893test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
3894{
3895 mock_mapped_index mock_index (test_symbols);
3896
3897 /* We let all tests run until the end even if some fails, for debug
3898 convenience. */
3899 bool any_mismatch = false;
3900
3901 /* Create the expected symbols list (an initializer_list). Needed
3902 because lists have commas, and we need to pass them to CHECK,
3903 which is a macro. */
3904#define EXPECT(...) { __VA_ARGS__ }
3905
3906 /* Wrapper for check_match that passes down the current
3907 __FILE__/__LINE__. */
3908#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3909 any_mismatch |= !check_match (__FILE__, __LINE__, \
3910 mock_index, \
3911 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 3912 EXPECTED_LIST, NULL)
c62446b1
PA
3913
3914 /* Identity checks. */
3915 for (const char *sym : test_symbols)
3916 {
3917 /* Should be able to match all existing symbols. */
3918 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3919 EXPECT (sym));
3920
3921 /* Should be able to match all existing symbols with
3922 parameters. */
3923 std::string with_params = std::string (sym) + "(int)";
3924 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3925 EXPECT (sym));
3926
3927 /* Should be able to match all existing symbols with
3928 parameters and qualifiers. */
3929 with_params = std::string (sym) + " ( int ) const";
3930 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3931 EXPECT (sym));
3932
3933 /* This should really find sym, but cp-name-parser.y doesn't
3934 know about lvalue/rvalue qualifiers yet. */
3935 with_params = std::string (sym) + " ( int ) &&";
3936 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3937 {});
3938 }
3939
e1ef7d7a
PA
3940 /* Check that the name matching algorithm for completion doesn't get
3941 confused with Latin1 'ÿ' / 0xff. */
3942 {
3943 static const char str[] = "\377";
3944 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3945 EXPECT ("\377", "\377\377123"));
3946 }
3947
3948 /* Check that the increment-last-char in the matching algorithm for
3949 completion doesn't match "t1_fund" when completing "t1_func". */
3950 {
3951 static const char str[] = "t1_func";
3952 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3953 EXPECT ("t1_func", "t1_func1"));
3954 }
3955
c62446b1
PA
3956 /* Check that completion mode works at each prefix of the expected
3957 symbol name. */
3958 {
3959 static const char str[] = "function(int)";
3960 size_t len = strlen (str);
3961 std::string lookup;
3962
3963 for (size_t i = 1; i < len; i++)
3964 {
3965 lookup.assign (str, i);
3966 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3967 EXPECT ("function"));
3968 }
3969 }
3970
3971 /* While "w" is a prefix of both components, the match function
3972 should still only be called once. */
3973 {
3974 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3975 EXPECT ("w1::w2"));
a20714ff
PA
3976 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3977 EXPECT ("w1::w2"));
c62446b1
PA
3978 }
3979
3980 /* Same, with a "complicated" symbol. */
3981 {
3982 static const char str[] = Z_SYM_NAME;
3983 size_t len = strlen (str);
3984 std::string lookup;
3985
3986 for (size_t i = 1; i < len; i++)
3987 {
3988 lookup.assign (str, i);
3989 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3990 EXPECT (Z_SYM_NAME));
3991 }
3992 }
3993
3994 /* In FULL mode, an incomplete symbol doesn't match. */
3995 {
3996 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3997 {});
3998 }
3999
4000 /* A complete symbol with parameters matches any overload, since the
4001 index has no overload info. */
4002 {
4003 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4004 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4005 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4006 EXPECT ("std::zfunction", "std::zfunction2"));
4007 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4008 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4009 }
4010
4011 /* Check that whitespace is ignored appropriately. A symbol with a
4012 template argument list. */
4013 {
4014 static const char expected[] = "ns::foo<int>";
4015 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4016 EXPECT (expected));
a20714ff
PA
4017 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4018 EXPECT (expected));
c62446b1
PA
4019 }
4020
4021 /* Check that whitespace is ignored appropriately. A symbol with a
4022 template argument list that includes a pointer. */
4023 {
4024 static const char expected[] = "ns::foo<char*>";
4025 /* Try both completion and non-completion modes. */
4026 static const bool completion_mode[2] = {false, true};
4027 for (size_t i = 0; i < 2; i++)
4028 {
4029 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4030 completion_mode[i], EXPECT (expected));
a20714ff
PA
4031 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4032 completion_mode[i], EXPECT (expected));
c62446b1
PA
4033
4034 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4035 completion_mode[i], EXPECT (expected));
a20714ff
PA
4036 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4037 completion_mode[i], EXPECT (expected));
c62446b1
PA
4038 }
4039 }
4040
4041 {
4042 /* Check method qualifiers are ignored. */
4043 static const char expected[] = "ns::foo<char*>";
4044 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4045 symbol_name_match_type::FULL, true, EXPECT (expected));
4046 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4047 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4048 CHECK_MATCH ("foo < char * > ( int ) const",
4049 symbol_name_match_type::WILD, true, EXPECT (expected));
4050 CHECK_MATCH ("foo < char * > ( int ) &&",
4051 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4052 }
4053
4054 /* Test lookup names that don't match anything. */
4055 {
a20714ff
PA
4056 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4057 {});
4058
c62446b1
PA
4059 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4060 {});
4061 }
4062
a20714ff
PA
4063 /* Some wild matching tests, exercising "(anonymous namespace)",
4064 which should not be confused with a parameter list. */
4065 {
4066 static const char *syms[] = {
4067 "A::B::C",
4068 "B::C",
4069 "C",
4070 "A :: B :: C ( int )",
4071 "B :: C ( int )",
4072 "C ( int )",
4073 };
4074
4075 for (const char *s : syms)
4076 {
4077 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4078 EXPECT ("(anonymous namespace)::A::B::C"));
4079 }
4080 }
4081
4082 {
4083 static const char expected[] = "ns2::tmpl<int>::foo2";
4084 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4085 EXPECT (expected));
4086 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4087 EXPECT (expected));
4088 }
4089
c62446b1
PA
4090 SELF_CHECK (!any_mismatch);
4091
4092#undef EXPECT
4093#undef CHECK_MATCH
4094}
4095
5c58de74
PA
4096static void
4097run_test ()
4098{
4099 test_mapped_index_find_name_component_bounds ();
4100 test_dw2_expand_symtabs_matching_symbol ();
4101}
4102
c62446b1
PA
4103}} // namespace selftests::dw2_expand_symtabs_matching
4104
4105#endif /* GDB_SELF_TEST */
4106
4b514bc8
JK
4107/* If FILE_MATCHER is NULL or if PER_CU has
4108 dwarf2_per_cu_quick_data::MARK set (see
4109 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4110 EXPANSION_NOTIFY on it. */
4111
df35e626 4112static bool
4b514bc8 4113dw2_expand_symtabs_matching_one
97a1449a
SM
4114 (dwarf2_per_cu_data *per_cu,
4115 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4116 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4117 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4118{
4119 if (file_matcher == NULL || per_cu->v.quick->mark)
4120 {
af758d11 4121 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4122
97a1449a
SM
4123 compunit_symtab *symtab
4124 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4125 gdb_assert (symtab != nullptr);
4b514bc8 4126
af758d11 4127 if (expansion_notify != NULL && symtab_was_null)
df35e626 4128 return expansion_notify (symtab);
4b514bc8 4129 }
df35e626 4130 return true;
4b514bc8
JK
4131}
4132
3f563c84
PA
4133/* Helper for dw2_expand_matching symtabs. Called on each symbol
4134 matched, to expand corresponding CUs that were marked. IDX is the
4135 index of the symbol name that matched. */
4136
df35e626 4137static bool
3f563c84 4138dw2_expand_marked_cus
976ca316 4139 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4140 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4141 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4142 block_search_flags search_flags,
3f563c84
PA
4143 search_domain kind)
4144{
42c2c694 4145 offset_type vec_len, vec_idx;
3f563c84 4146 bool global_seen = false;
976ca316 4147 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4148
42c2c694
TT
4149 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4150 vec_len = vec[0];
61920122
PA
4151 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4152 {
42c2c694 4153 offset_type cu_index_and_attrs = vec[vec_idx + 1];
61920122
PA
4154 /* This value is only valid for index versions >= 7. */
4155 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4156 gdb_index_symbol_kind symbol_kind =
4157 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4158 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4159 /* Only check the symbol attributes if they're present.
4160 Indices prior to version 7 don't record them,
4161 and indices >= 7 may elide them for certain symbols
4162 (gold does this). */
4163 int attrs_valid =
4164 (index.version >= 7
4165 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4166
4167 /* Work around gold/15646. */
f030440d
TV
4168 if (attrs_valid
4169 && !is_static
4170 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4171 {
f030440d 4172 if (global_seen)
61920122 4173 continue;
f030440d
TV
4174
4175 global_seen = true;
61920122 4176 }
3190f0c6 4177
61920122
PA
4178 /* Only check the symbol's kind if it has one. */
4179 if (attrs_valid)
4180 {
03a8ea51
TT
4181 if (is_static)
4182 {
4183 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4184 continue;
4185 }
4186 else
4187 {
4188 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4189 continue;
4190 }
4191
61920122 4192 switch (kind)
8943b874 4193 {
61920122
PA
4194 case VARIABLES_DOMAIN:
4195 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4196 continue;
4197 break;
4198 case FUNCTIONS_DOMAIN:
4199 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4200 continue;
61920122
PA
4201 break;
4202 case TYPES_DOMAIN:
4203 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4204 continue;
4205 break;
59c35742
AB
4206 case MODULES_DOMAIN:
4207 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4208 continue;
4209 break;
61920122
PA
4210 default:
4211 break;
8943b874 4212 }
61920122 4213 }
8943b874 4214
61920122 4215 /* Don't crash on bad data. */
91eea9cc 4216 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
61920122 4217 {
b98664d3 4218 complaint (_(".gdb_index entry has bad CU index"
976ca316 4219 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4220 continue;
4221 }
4222
91eea9cc 4223 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
df35e626
TT
4224 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4225 expansion_notify))
4226 return false;
61920122 4227 }
df35e626
TT
4228
4229 return true;
61920122
PA
4230}
4231
4b514bc8
JK
4232/* If FILE_MATCHER is non-NULL, set all the
4233 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4234 that match FILE_MATCHER. */
4235
61920122 4236static void
4b514bc8 4237dw_expand_symtabs_matching_file_matcher
976ca316 4238 (dwarf2_per_objfile *per_objfile,
ed2dc618 4239 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4240{
4b514bc8 4241 if (file_matcher == NULL)
61920122
PA
4242 return;
4243
4b514bc8
JK
4244 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4245 htab_eq_pointer,
4246 NULL, xcalloc, xfree));
4247 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4248 htab_eq_pointer,
4249 NULL, xcalloc, xfree));
61920122 4250
4b514bc8
JK
4251 /* The rule is CUs specify all the files, including those used by
4252 any TU, so there's no need to scan TUs here. */
61920122 4253
0d305d5c 4254 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4255 {
927aa2e7
JK
4256 QUIT;
4257
91eea9cc
TT
4258 if (per_cu->is_debug_types)
4259 continue;
927aa2e7
JK
4260 per_cu->v.quick->mark = 0;
4261
4262 /* We only need to look at symtabs not already expanded. */
0d305d5c 4263 if (per_objfile->symtab_set_p (per_cu.get ()))
927aa2e7
JK
4264 continue;
4265
0d305d5c
TT
4266 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4267 per_objfile);
927aa2e7
JK
4268 if (file_data == NULL)
4269 continue;
4270
4271 if (htab_find (visited_not_found.get (), file_data) != NULL)
4272 continue;
4273 else if (htab_find (visited_found.get (), file_data) != NULL)
4274 {
4275 per_cu->v.quick->mark = 1;
4276 continue;
4277 }
4278
b76e467d 4279 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4280 {
4281 const char *this_real_name;
4282
4283 if (file_matcher (file_data->file_names[j], false))
4284 {
4285 per_cu->v.quick->mark = 1;
4286 break;
4287 }
4288
4289 /* Before we invoke realpath, which can get expensive when many
4290 files are involved, do a quick comparison of the basenames. */
4291 if (!basenames_may_differ
4292 && !file_matcher (lbasename (file_data->file_names[j]),
4293 true))
4294 continue;
4295
976ca316 4296 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4297 if (file_matcher (this_real_name, false))
4298 {
4299 per_cu->v.quick->mark = 1;
4300 break;
4301 }
4302 }
4303
b76e467d
SM
4304 void **slot = htab_find_slot (per_cu->v.quick->mark
4305 ? visited_found.get ()
4306 : visited_not_found.get (),
4307 file_data, INSERT);
927aa2e7
JK
4308 *slot = file_data;
4309 }
4310}
4311
df35e626 4312bool
af82f89d
TT
4313dwarf2_gdb_index::expand_symtabs_matching
4314 (struct objfile *objfile,
4315 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4316 const lookup_name_info *lookup_name,
4317 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4318 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4319 block_search_flags search_flags,
3bfa51a7 4320 domain_enum domain,
af82f89d 4321 enum search_domain kind)
927aa2e7 4322{
976ca316 4323 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4324
4325 /* index_table is NULL if OBJF_READNOW. */
976ca316 4326 if (!per_objfile->per_bfd->index_table)
df35e626 4327 return true;
927aa2e7 4328
976ca316 4329 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4330
c1a66c06
TV
4331 if (symbol_matcher == NULL && lookup_name == NULL)
4332 {
0d305d5c 4333 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4334 {
4335 QUIT;
4336
0d305d5c 4337 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
df35e626
TT
4338 file_matcher,
4339 expansion_notify))
4340 return false;
c1a66c06 4341 }
df35e626 4342 return true;
c1a66c06
TV
4343 }
4344
976ca316 4345 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4346
df35e626
TT
4347 bool result
4348 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4349 symbol_matcher,
4350 [&] (offset_type idx)
927aa2e7 4351 {
df35e626 4352 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
03a8ea51 4353 expansion_notify, search_flags, kind))
df35e626 4354 return false;
3b00ef10 4355 return true;
976ca316 4356 }, per_objfile);
df35e626
TT
4357
4358 return result;
927aa2e7
JK
4359}
4360
4361/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4362 symtab. */
4363
4364static struct compunit_symtab *
4365recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4366 CORE_ADDR pc)
4367{
4368 int i;
4369
4370 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4371 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4372 return cust;
4373
4374 if (cust->includes == NULL)
4375 return NULL;
4376
4377 for (i = 0; cust->includes[i]; ++i)
4378 {
4379 struct compunit_symtab *s = cust->includes[i];
4380
4381 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4382 if (s != NULL)
4383 return s;
4384 }
4385
4386 return NULL;
4387}
4388
39298a5d
TT
4389struct compunit_symtab *
4390dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4391 (struct objfile *objfile,
4392 struct bound_minimal_symbol msymbol,
4393 CORE_ADDR pc,
4394 struct obj_section *section,
4395 int warn_if_readin)
927aa2e7
JK
4396{
4397 struct dwarf2_per_cu_data *data;
4398 struct compunit_symtab *result;
4399
efd7398e
TT
4400 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4401 if (per_objfile->per_bfd->index_addrmap == nullptr)
927aa2e7
JK
4402 return NULL;
4403
b3b3bada 4404 CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e
TT
4405 data = ((struct dwarf2_per_cu_data *)
4406 addrmap_find (per_objfile->per_bfd->index_addrmap,
4407 pc - baseaddr));
927aa2e7
JK
4408 if (!data)
4409 return NULL;
4410
af758d11 4411 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4412 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4413 paddress (objfile->arch (), pc));
927aa2e7 4414
97a1449a
SM
4415 result = recursively_find_pc_sect_compunit_symtab
4416 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4417
927aa2e7
JK
4418 gdb_assert (result != NULL);
4419 return result;
4420}
4421
39298a5d 4422void
f4655dee
TT
4423dwarf2_base_index_functions::map_symbol_filenames
4424 (struct objfile *objfile,
4425 gdb::function_view<symbol_filename_ftype> fun,
4426 bool need_fullname)
927aa2e7 4427{
976ca316 4428 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4429
9fc29955
AB
4430 /* Use caches to ensure we only call FUN once for each filename. */
4431 filename_seen_cache filenames_cache;
4432 std::unordered_set<quick_file_names *> qfn_cache;
927aa2e7 4433
9fc29955
AB
4434 /* The rule is CUs specify all the files, including those used by any TU,
4435 so there's no need to scan TUs here. We can ignore file names coming
4436 from already-expanded CUs. It is possible that an expanded CU might
4437 reuse the file names data from a currently unexpanded CU, in this
4438 case we don't want to report the files from the unexpanded CU. */
927aa2e7 4439
0d305d5c 4440 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
9fc29955 4441 {
0d305d5c 4442 if (per_objfile->symtab_set_p (per_cu.get ()))
927aa2e7 4443 {
9fc29955
AB
4444 if (per_cu->v.quick->file_names != nullptr)
4445 qfn_cache.insert (per_cu->v.quick->file_names);
927aa2e7 4446 }
9fc29955 4447 }
927aa2e7 4448
0d305d5c 4449 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
9fc29955
AB
4450 {
4451 /* We only need to look at symtabs not already expanded. */
0d305d5c 4452 if (per_objfile->symtab_set_p (per_cu.get ()))
9fc29955 4453 continue;
927aa2e7 4454
0d305d5c
TT
4455 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4456 per_objfile);
9fc29955
AB
4457 if (file_data == nullptr
4458 || qfn_cache.find (file_data) != qfn_cache.end ())
4459 continue;
927aa2e7 4460
9fc29955
AB
4461 for (int j = 0; j < file_data->num_file_names; ++j)
4462 {
4463 const char *filename = file_data->file_names[j];
4464 filenames_cache.seen (filename);
927aa2e7
JK
4465 }
4466 }
4467
9fc29955 4468 filenames_cache.traverse ([&] (const char *filename)
927aa2e7
JK
4469 {
4470 gdb::unique_xmalloc_ptr<char> this_real_name;
4471
4472 if (need_fullname)
4473 this_real_name = gdb_realpath (filename);
f4655dee 4474 fun (filename, this_real_name.get ());
927aa2e7
JK
4475 });
4476}
4477
39298a5d
TT
4478bool
4479dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
927aa2e7 4480{
fae2120b 4481 return true;
927aa2e7
JK
4482}
4483
927aa2e7
JK
4484/* DWARF-5 debug_names reader. */
4485
4486/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4487static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4488
4489/* A helper function that reads the .debug_names section in SECTION
4490 and fills in MAP. FILENAME is the name of the file containing the
4491 section; it is used for error reporting.
4492
4493 Returns true if all went well, false otherwise. */
4494
4495static bool
4496read_debug_names_from_section (struct objfile *objfile,
4497 const char *filename,
4498 struct dwarf2_section_info *section,
4499 mapped_debug_names &map)
4500{
96b79293 4501 if (section->empty ())
927aa2e7
JK
4502 return false;
4503
4504 /* Older elfutils strip versions could keep the section in the main
4505 executable while splitting it for the separate debug info file. */
96b79293 4506 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4507 return false;
4508
96b79293 4509 section->read (objfile);
927aa2e7 4510
08feed99 4511 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4512
4513 const gdb_byte *addr = section->buffer;
4514
96b79293 4515 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4516
4517 unsigned int bytes_read;
4518 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4519 addr += bytes_read;
4520
4521 map.dwarf5_is_dwarf64 = bytes_read != 4;
4522 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4523 if (bytes_read + length != section->size)
4524 {
4525 /* There may be multiple per-CU indices. */
4526 warning (_("Section .debug_names in %s length %s does not match "
4527 "section length %s, ignoring .debug_names."),
4528 filename, plongest (bytes_read + length),
4529 pulongest (section->size));
4530 return false;
4531 }
4532
4533 /* The version number. */
4534 uint16_t version = read_2_bytes (abfd, addr);
4535 addr += 2;
4536 if (version != 5)
4537 {
4538 warning (_("Section .debug_names in %s has unsupported version %d, "
4539 "ignoring .debug_names."),
4540 filename, version);
4541 return false;
4542 }
4543
4544 /* Padding. */
4545 uint16_t padding = read_2_bytes (abfd, addr);
4546 addr += 2;
4547 if (padding != 0)
4548 {
4549 warning (_("Section .debug_names in %s has unsupported padding %d, "
4550 "ignoring .debug_names."),
4551 filename, padding);
4552 return false;
4553 }
4554
4555 /* comp_unit_count - The number of CUs in the CU list. */
4556 map.cu_count = read_4_bytes (abfd, addr);
4557 addr += 4;
4558
4559 /* local_type_unit_count - The number of TUs in the local TU
4560 list. */
4561 map.tu_count = read_4_bytes (abfd, addr);
4562 addr += 4;
4563
4564 /* foreign_type_unit_count - The number of TUs in the foreign TU
4565 list. */
4566 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4567 addr += 4;
4568 if (foreign_tu_count != 0)
4569 {
4570 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4571 "ignoring .debug_names."),
4572 filename, static_cast<unsigned long> (foreign_tu_count));
4573 return false;
4574 }
4575
4576 /* bucket_count - The number of hash buckets in the hash lookup
4577 table. */
4578 map.bucket_count = read_4_bytes (abfd, addr);
4579 addr += 4;
4580
4581 /* name_count - The number of unique names in the index. */
4582 map.name_count = read_4_bytes (abfd, addr);
4583 addr += 4;
4584
4585 /* abbrev_table_size - The size in bytes of the abbreviations
4586 table. */
4587 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4588 addr += 4;
4589
4590 /* augmentation_string_size - The size in bytes of the augmentation
4591 string. This value is rounded up to a multiple of 4. */
4592 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4593 addr += 4;
4594 map.augmentation_is_gdb = ((augmentation_string_size
4595 == sizeof (dwarf5_augmentation))
4596 && memcmp (addr, dwarf5_augmentation,
4597 sizeof (dwarf5_augmentation)) == 0);
4598 augmentation_string_size += (-augmentation_string_size) & 3;
4599 addr += augmentation_string_size;
4600
4601 /* List of CUs */
4602 map.cu_table_reordered = addr;
4603 addr += map.cu_count * map.offset_size;
4604
4605 /* List of Local TUs */
4606 map.tu_table_reordered = addr;
4607 addr += map.tu_count * map.offset_size;
4608
4609 /* Hash Lookup Table */
4610 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4611 addr += map.bucket_count * 4;
4612 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4613 addr += map.name_count * 4;
4614
4615 /* Name Table */
4616 map.name_table_string_offs_reordered = addr;
4617 addr += map.name_count * map.offset_size;
4618 map.name_table_entry_offs_reordered = addr;
4619 addr += map.name_count * map.offset_size;
4620
4621 const gdb_byte *abbrev_table_start = addr;
4622 for (;;)
4623 {
927aa2e7
JK
4624 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4625 addr += bytes_read;
4626 if (index_num == 0)
4627 break;
4628
4629 const auto insertpair
4630 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4631 if (!insertpair.second)
4632 {
4633 warning (_("Section .debug_names in %s has duplicate index %s, "
4634 "ignoring .debug_names."),
4635 filename, pulongest (index_num));
4636 return false;
4637 }
4638 mapped_debug_names::index_val &indexval = insertpair.first->second;
4639 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4640 addr += bytes_read;
4641
4642 for (;;)
4643 {
4644 mapped_debug_names::index_val::attr attr;
4645 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4646 addr += bytes_read;
4647 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4648 addr += bytes_read;
4649 if (attr.form == DW_FORM_implicit_const)
4650 {
4651 attr.implicit_const = read_signed_leb128 (abfd, addr,
4652 &bytes_read);
4653 addr += bytes_read;
4654 }
4655 if (attr.dw_idx == 0 && attr.form == 0)
4656 break;
4657 indexval.attr_vec.push_back (std::move (attr));
4658 }
4659 }
4660 if (addr != abbrev_table_start + abbrev_table_size)
4661 {
4662 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4663 "of size %s vs. written as %u, ignoring .debug_names."),
4664 filename, plongest (addr - abbrev_table_start),
4665 abbrev_table_size);
927aa2e7
JK
4666 return false;
4667 }
4668 map.entry_pool = addr;
4669
4670 return true;
4671}
4672
4673/* A helper for create_cus_from_debug_names that handles the MAP's CU
4674 list. */
4675
4676static void
168c9250 4677create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4678 const mapped_debug_names &map,
4679 dwarf2_section_info &section,
b76e467d 4680 bool is_dwz)
927aa2e7 4681{
3ee6bb11
TV
4682 if (!map.augmentation_is_gdb)
4683 {
fc9a13fb
TV
4684 for (uint32_t i = 0; i < map.cu_count; ++i)
4685 {
4686 sect_offset sect_off
4687 = (sect_offset) (extract_unsigned_integer
4688 (map.cu_table_reordered + i * map.offset_size,
4689 map.offset_size,
4690 map.dwarf5_byte_order));
4691 /* We don't know the length of the CU, because the CU list in a
4692 .debug_names index can be incomplete, so we can't use the start
4693 of the next CU as end of this CU. We create the CUs here with
4694 length 0, and in cutu_reader::cutu_reader we'll fill in the
4695 actual length. */
473ab964 4696 dwarf2_per_cu_data_up per_cu
fc9a13fb
TV
4697 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4698 sect_off, 0);
0d305d5c 4699 per_bfd->all_comp_units.push_back (std::move (per_cu));
fc9a13fb 4700 }
d3b54e63 4701 return;
3ee6bb11
TV
4702 }
4703
927aa2e7
JK
4704 sect_offset sect_off_prev;
4705 for (uint32_t i = 0; i <= map.cu_count; ++i)
4706 {
4707 sect_offset sect_off_next;
4708 if (i < map.cu_count)
4709 {
4710 sect_off_next
4711 = (sect_offset) (extract_unsigned_integer
4712 (map.cu_table_reordered + i * map.offset_size,
4713 map.offset_size,
4714 map.dwarf5_byte_order));
4715 }
4716 else
4717 sect_off_next = (sect_offset) section.size;
4718 if (i >= 1)
4719 {
4720 const ULONGEST length = sect_off_next - sect_off_prev;
473ab964 4721 dwarf2_per_cu_data_up per_cu
168c9250 4722 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 4723 sect_off_prev, length);
0d305d5c 4724 per_bfd->all_comp_units.push_back (std::move (per_cu));
927aa2e7
JK
4725 }
4726 sect_off_prev = sect_off_next;
4727 }
4728}
4729
4730/* Read the CU list from the mapped index, and use it to create all
ed2dc618 4731 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
4732
4733static void
168c9250 4734create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4735 const mapped_debug_names &map,
4736 const mapped_debug_names &dwz_map)
4737{
168c9250
SM
4738 gdb_assert (per_bfd->all_comp_units.empty ());
4739 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 4740
168c9250 4741 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 4742 false /* is_dwz */);
927aa2e7
JK
4743
4744 if (dwz_map.cu_count == 0)
4745 return;
4746
168c9250
SM
4747 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4748 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 4749 true /* is_dwz */);
927aa2e7
JK
4750}
4751
4752/* Read .debug_names. If everything went ok, initialize the "quick"
4753 elements of all the CUs and return true. Otherwise, return false. */
4754
4755static bool
976ca316 4756dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 4757{
fcf23d5b
SM
4758 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4759 mapped_debug_names dwz_map;
976ca316 4760 struct objfile *objfile = per_objfile->objfile;
f8c41851 4761 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
4762
4763 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
bab287cd 4764 &per_bfd->debug_names, *map))
927aa2e7
JK
4765 return false;
4766
4767 /* Don't use the index if it's empty. */
22ca247e 4768 if (map->name_count == 0)
927aa2e7
JK
4769 return false;
4770
4771 /* If there is a .dwz file, read it so we can get its CU list as
4772 well. */
f8c41851 4773 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
4774 if (dwz != NULL)
4775 {
4776 if (!read_debug_names_from_section (objfile,
00f93c44 4777 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
4778 &dwz->debug_names, dwz_map))
4779 {
4780 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 4781 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
4782 return false;
4783 }
4784 }
4785
f8c41851 4786 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 4787
22ca247e 4788 if (map->tu_count != 0)
927aa2e7
JK
4789 {
4790 /* We can only handle a single .debug_types when we have an
4791 index. */
f8c41851 4792 if (per_bfd->types.size () != 1)
927aa2e7
JK
4793 return false;
4794
f8c41851 4795 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
4796
4797 create_signatured_type_table_from_debug_names
f8c41851 4798 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
4799 }
4800
f8c41851 4801 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 4802
f8c41851
SM
4803 per_bfd->debug_names_table = std::move (map);
4804 per_bfd->using_index = 1;
4805 per_bfd->quick_file_names_table =
bab287cd 4806 create_quick_file_names_table (per_bfd->all_comp_units.size ());
927aa2e7
JK
4807
4808 return true;
4809}
4810
927aa2e7
JK
4811/* Type used to manage iterating over all CUs looking for a symbol for
4812 .debug_names. */
4813
4814class dw2_debug_names_iterator
4815{
4816public:
927aa2e7 4817 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4818 block_search_flags block_index,
2b79f376 4819 domain_enum domain,
fcf23d5b 4820 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 4821 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
4822 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4823 m_per_objfile (per_objfile)
927aa2e7
JK
4824 {}
4825
4826 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4827 search_domain search, uint32_t namei,
3bfa51a7
TT
4828 dwarf2_per_objfile *per_objfile,
4829 domain_enum domain = UNDEF_DOMAIN)
927aa2e7 4830 : m_map (map),
3bfa51a7 4831 m_domain (domain),
927aa2e7 4832 m_search (search),
fcf23d5b
SM
4833 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4834 m_per_objfile (per_objfile)
927aa2e7
JK
4835 {}
4836
3b00ef10 4837 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 4838 block_search_flags block_index, domain_enum domain,
fcf23d5b 4839 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 4840 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
4841 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4842 m_per_objfile (per_objfile)
3b00ef10
TT
4843 {}
4844
927aa2e7
JK
4845 /* Return the next matching CU or NULL if there are no more. */
4846 dwarf2_per_cu_data *next ();
4847
4848private:
4849 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
4850 const char *name,
4851 dwarf2_per_objfile *per_objfile);
927aa2e7 4852 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
4853 uint32_t namei,
4854 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
4855
4856 /* The internalized form of .debug_names. */
4857 const mapped_debug_names &m_map;
4858
03a8ea51
TT
4859 /* Restrict the search to these blocks. */
4860 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4861 | SEARCH_STATIC_BLOCK);
927aa2e7
JK
4862
4863 /* The kind of symbol we're looking for. */
4864 const domain_enum m_domain = UNDEF_DOMAIN;
4865 const search_domain m_search = ALL_DOMAIN;
4866
4867 /* The list of CUs from the index entry of the symbol, or NULL if
4868 not found. */
4869 const gdb_byte *m_addr;
fcf23d5b
SM
4870
4871 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
4872};
4873
4874const char *
fcf23d5b 4875mapped_debug_names::namei_to_name
976ca316 4876 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
4877{
4878 const ULONGEST namei_string_offs
4879 = extract_unsigned_integer ((name_table_string_offs_reordered
4880 + namei * offset_size),
4881 offset_size,
4882 dwarf5_byte_order);
976ca316 4883 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
4884}
4885
4886/* Find a slot in .debug_names for the object named NAME. If NAME is
4887 found, return pointer to its pool data. If NAME cannot be found,
4888 return NULL. */
4889
4890const gdb_byte *
4891dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
4892 (const mapped_debug_names &map, const char *name,
4893 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
4894{
4895 int (*cmp) (const char *, const char *);
4896
54ee4252 4897 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
4898 if (current_language->la_language == language_cplus
4899 || current_language->la_language == language_fortran
4900 || current_language->la_language == language_d)
4901 {
4902 /* NAME is already canonical. Drop any qualifiers as
4903 .debug_names does not contain any. */
4904
4905 if (strchr (name, '(') != NULL)
4906 {
54ee4252 4907 without_params = cp_remove_params (name);
927aa2e7 4908 if (without_params != NULL)
54ee4252 4909 name = without_params.get ();
927aa2e7
JK
4910 }
4911 }
4912
4913 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4914
4915 const uint32_t full_hash = dwarf5_djb_hash (name);
4916 uint32_t namei
4917 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4918 (map.bucket_table_reordered
4919 + (full_hash % map.bucket_count)), 4,
4920 map.dwarf5_byte_order);
4921 if (namei == 0)
4922 return NULL;
4923 --namei;
4924 if (namei >= map.name_count)
4925 {
b98664d3 4926 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
4927 "[in module %s]"),
4928 namei, map.name_count,
fcf23d5b 4929 objfile_name (per_objfile->objfile));
927aa2e7
JK
4930 return NULL;
4931 }
4932
4933 for (;;)
4934 {
4935 const uint32_t namei_full_hash
4936 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4937 (map.hash_table_reordered + namei), 4,
4938 map.dwarf5_byte_order);
4939 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4940 return NULL;
4941
4942 if (full_hash == namei_full_hash)
4943 {
fcf23d5b 4944 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
4945
4946#if 0 /* An expensive sanity check. */
4947 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4948 {
b98664d3 4949 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
4950 "[in module %s]"),
4951 namei, objfile_name (dwarf2_per_objfile->objfile));
4952 return NULL;
4953 }
4954#endif
4955
4956 if (cmp (namei_string, name) == 0)
4957 {
4958 const ULONGEST namei_entry_offs
4959 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4960 + namei * map.offset_size),
4961 map.offset_size, map.dwarf5_byte_order);
4962 return map.entry_pool + namei_entry_offs;
4963 }
4964 }
4965
4966 ++namei;
4967 if (namei >= map.name_count)
4968 return NULL;
4969 }
4970}
4971
4972const gdb_byte *
4973dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 4974 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
4975{
4976 if (namei >= map.name_count)
4977 {
b98664d3 4978 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
4979 "[in module %s]"),
4980 namei, map.name_count,
fcf23d5b 4981 objfile_name (per_objfile->objfile));
927aa2e7
JK
4982 return NULL;
4983 }
4984
4985 const ULONGEST namei_entry_offs
4986 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4987 + namei * map.offset_size),
4988 map.offset_size, map.dwarf5_byte_order);
4989 return map.entry_pool + namei_entry_offs;
4990}
4991
4992/* See dw2_debug_names_iterator. */
4993
4994dwarf2_per_cu_data *
4995dw2_debug_names_iterator::next ()
4996{
4997 if (m_addr == NULL)
4998 return NULL;
4999
fcf23d5b
SM
5000 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5001 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5002 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5003
5004 again:
5005
5006 unsigned int bytes_read;
5007 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5008 m_addr += bytes_read;
5009 if (abbrev == 0)
5010 return NULL;
5011
5012 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5013 if (indexval_it == m_map.abbrev_map.cend ())
5014 {
b98664d3 5015 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5016 "[in module %s]"),
ed2dc618 5017 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5018 return NULL;
5019 }
5020 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5021 enum class symbol_linkage {
5022 unknown,
5023 static_,
5024 extern_,
23c13d42 5025 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5026 dwarf2_per_cu_data *per_cu = NULL;
5027 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5028 {
5029 ULONGEST ull;
5030 switch (attr.form)
5031 {
5032 case DW_FORM_implicit_const:
5033 ull = attr.implicit_const;
5034 break;
5035 case DW_FORM_flag_present:
5036 ull = 1;
5037 break;
5038 case DW_FORM_udata:
5039 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5040 m_addr += bytes_read;
5041 break;
6dc55ce9 5042 case DW_FORM_ref4:
5043 ull = read_4_bytes (abfd, m_addr);
5044 m_addr += 4;
5045 break;
5046 case DW_FORM_ref8:
5047 ull = read_8_bytes (abfd, m_addr);
5048 m_addr += 8;
5049 break;
5050 case DW_FORM_ref_sig8:
5051 ull = read_8_bytes (abfd, m_addr);
5052 m_addr += 8;
5053 break;
927aa2e7 5054 default:
b98664d3 5055 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5056 dwarf_form_name (attr.form),
ed2dc618 5057 objfile_name (objfile));
927aa2e7
JK
5058 return NULL;
5059 }
5060 switch (attr.dw_idx)
5061 {
5062 case DW_IDX_compile_unit:
5063 /* Don't crash on bad data. */
bab287cd 5064 if (ull >= per_bfd->all_comp_units.size ())
927aa2e7 5065 {
b98664d3 5066 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5067 " [in module %s]"),
5068 pulongest (ull),
fcf23d5b 5069 objfile_name (objfile));
927aa2e7
JK
5070 continue;
5071 }
91eea9cc 5072 per_cu = per_bfd->get_cu (ull);
927aa2e7 5073 break;
8af5c486
JK
5074 case DW_IDX_type_unit:
5075 /* Don't crash on bad data. */
91eea9cc 5076 if (ull >= per_bfd->tu_stats.nr_tus)
8af5c486 5077 {
b98664d3 5078 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5079 " [in module %s]"),
5080 pulongest (ull),
fcf23d5b 5081 objfile_name (objfile));
8af5c486
JK
5082 continue;
5083 }
91eea9cc 5084 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
8af5c486 5085 break;
6dc55ce9 5086 case DW_IDX_die_offset:
5087 /* In a per-CU index (as opposed to a per-module index), index
5088 entries without CU attribute implicitly refer to the single CU. */
5089 if (per_cu == NULL)
fcf23d5b 5090 per_cu = per_bfd->get_cu (0);
6dc55ce9 5091 break;
927aa2e7
JK
5092 case DW_IDX_GNU_internal:
5093 if (!m_map.augmentation_is_gdb)
5094 break;
23c13d42 5095 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5096 break;
5097 case DW_IDX_GNU_external:
5098 if (!m_map.augmentation_is_gdb)
5099 break;
23c13d42 5100 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5101 break;
5102 }
5103 }
5104
5105 /* Skip if already read in. */
fcf23d5b 5106 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5107 goto again;
5108
5109 /* Check static vs global. */
03a8ea51 5110 if (symbol_linkage_ != symbol_linkage::unknown)
927aa2e7 5111 {
03a8ea51
TT
5112 if (symbol_linkage_ == symbol_linkage::static_)
5113 {
5114 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5115 goto again;
5116 }
5117 else
5118 {
5119 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5120 goto again;
5121 }
927aa2e7
JK
5122 }
5123
5124 /* Match dw2_symtab_iter_next, symbol_kind
5125 and debug_names::psymbol_tag. */
5126 switch (m_domain)
5127 {
5128 case VAR_DOMAIN:
5129 switch (indexval.dwarf_tag)
5130 {
5131 case DW_TAG_variable:
5132 case DW_TAG_subprogram:
5133 /* Some types are also in VAR_DOMAIN. */
5134 case DW_TAG_typedef:
5135 case DW_TAG_structure_type:
5136 break;
5137 default:
5138 goto again;
5139 }
5140 break;
5141 case STRUCT_DOMAIN:
5142 switch (indexval.dwarf_tag)
5143 {
5144 case DW_TAG_typedef:
5145 case DW_TAG_structure_type:
5146 break;
5147 default:
5148 goto again;
5149 }
5150 break;
5151 case LABEL_DOMAIN:
5152 switch (indexval.dwarf_tag)
5153 {
5154 case 0:
5155 case DW_TAG_variable:
5156 break;
5157 default:
5158 goto again;
5159 }
5160 break;
59c35742
AB
5161 case MODULE_DOMAIN:
5162 switch (indexval.dwarf_tag)
5163 {
5164 case DW_TAG_module:
5165 break;
5166 default:
5167 goto again;
5168 }
5169 break;
927aa2e7
JK
5170 default:
5171 break;
5172 }
5173
5174 /* Match dw2_expand_symtabs_matching, symbol_kind and
5175 debug_names::psymbol_tag. */
5176 switch (m_search)
4b514bc8 5177 {
927aa2e7
JK
5178 case VARIABLES_DOMAIN:
5179 switch (indexval.dwarf_tag)
4b514bc8 5180 {
927aa2e7
JK
5181 case DW_TAG_variable:
5182 break;
5183 default:
5184 goto again;
4b514bc8 5185 }
927aa2e7
JK
5186 break;
5187 case FUNCTIONS_DOMAIN:
5188 switch (indexval.dwarf_tag)
4b514bc8 5189 {
927aa2e7
JK
5190 case DW_TAG_subprogram:
5191 break;
5192 default:
5193 goto again;
4b514bc8 5194 }
927aa2e7
JK
5195 break;
5196 case TYPES_DOMAIN:
5197 switch (indexval.dwarf_tag)
5198 {
5199 case DW_TAG_typedef:
5200 case DW_TAG_structure_type:
5201 break;
5202 default:
5203 goto again;
5204 }
5205 break;
59c35742
AB
5206 case MODULES_DOMAIN:
5207 switch (indexval.dwarf_tag)
5208 {
5209 case DW_TAG_module:
5210 break;
5211 default:
5212 goto again;
5213 }
927aa2e7
JK
5214 default:
5215 break;
4b514bc8 5216 }
927aa2e7
JK
5217
5218 return per_cu;
4b514bc8 5219}
61920122 5220
927aa2e7
JK
5221/* This dumps minimal information about .debug_names. It is called
5222 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5223 uses this to verify that .debug_names has been loaded. */
9291a0cd 5224
39298a5d
TT
5225void
5226dwarf2_debug_names_index::dump (struct objfile *objfile)
927aa2e7 5227{
976ca316 5228 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5229
976ca316 5230 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5231 printf_filtered (".debug_names:");
976ca316 5232 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5233 printf_filtered (" exists\n");
5234 else
5235 printf_filtered (" faked for \"readnow\"\n");
5236 printf_filtered ("\n");
9291a0cd
TT
5237}
5238
39298a5d 5239void
0b7b2c2a 5240dwarf2_debug_names_index::expand_matching_symbols
3b00ef10
TT
5241 (struct objfile *objfile,
5242 const lookup_name_info &name, domain_enum domain,
5243 int global,
3b00ef10
TT
5244 symbol_compare_ftype *ordered_compare)
5245{
976ca316 5246 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5247
5248 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5249 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5250 return;
5251
976ca316 5252 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
03a8ea51
TT
5253 const block_search_flags block_flags
5254 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
3b00ef10
TT
5255
5256 const char *match_name = name.ada ().lookup_name ().c_str ();
5257 auto matcher = [&] (const char *symname)
5258 {
5259 if (ordered_compare == nullptr)
5260 return true;
5261 return ordered_compare (symname, match_name) == 0;
5262 };
5263
b64f703b 5264 dw2_expand_symtabs_matching_symbol (map, name, matcher,
3b00ef10
TT
5265 [&] (offset_type namei)
5266 {
5267 /* The name was matched, now expand corresponding CUs that were
5268 marked. */
03a8ea51 5269 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
976ca316 5270 per_objfile);
3b00ef10
TT
5271
5272 struct dwarf2_per_cu_data *per_cu;
5273 while ((per_cu = iter.next ()) != NULL)
976ca316 5274 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5275 nullptr);
3b00ef10 5276 return true;
976ca316 5277 }, per_objfile);
3b00ef10
TT
5278}
5279
df35e626 5280bool
39298a5d 5281dwarf2_debug_names_index::expand_symtabs_matching
927aa2e7
JK
5282 (struct objfile *objfile,
5283 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5284 const lookup_name_info *lookup_name,
927aa2e7
JK
5285 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5286 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 5287 block_search_flags search_flags,
3bfa51a7 5288 domain_enum domain,
927aa2e7
JK
5289 enum search_domain kind)
5290{
976ca316 5291 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5292
927aa2e7 5293 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5294 if (!per_objfile->per_bfd->debug_names_table)
df35e626 5295 return true;
9291a0cd 5296
976ca316 5297 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5298
c1a66c06
TV
5299 if (symbol_matcher == NULL && lookup_name == NULL)
5300 {
0d305d5c 5301 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5302 {
5303 QUIT;
5304
0d305d5c 5305 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
df35e626
TT
5306 file_matcher,
5307 expansion_notify))
5308 return false;
c1a66c06 5309 }
df35e626 5310 return true;
c1a66c06
TV
5311 }
5312
976ca316 5313 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5314
df35e626
TT
5315 bool result
5316 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5317 symbol_matcher,
5318 [&] (offset_type namei)
927aa2e7 5319 {
927aa2e7
JK
5320 /* The name was matched, now expand corresponding CUs that were
5321 marked. */
3bfa51a7 5322 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
bbf2f4df 5323
927aa2e7
JK
5324 struct dwarf2_per_cu_data *per_cu;
5325 while ((per_cu = iter.next ()) != NULL)
df35e626
TT
5326 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5327 file_matcher,
5328 expansion_notify))
5329 return false;
3b00ef10 5330 return true;
976ca316 5331 }, per_objfile);
df35e626
TT
5332
5333 return result;
9291a0cd
TT
5334}
5335
4485a1c1 5336/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5337 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5338
5339template <typename T>
5340static gdb::array_view<const gdb_byte>
5341get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5342{
5343 dwarf2_section_info *section = &section_owner->gdb_index;
5344
96b79293 5345 if (section->empty ())
4485a1c1
SM
5346 return {};
5347
5348 /* Older elfutils strip versions could keep the section in the main
5349 executable while splitting it for the separate debug info file. */
96b79293 5350 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5351 return {};
5352
96b79293 5353 section->read (obj);
4485a1c1 5354
8bebfcda
PA
5355 /* dwarf2_section_info::size is a bfd_size_type, while
5356 gdb::array_view works with size_t. On 32-bit hosts, with
5357 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5358 is 32-bit. So we need an explicit narrowing conversion here.
5359 This is fine, because it's impossible to allocate or mmap an
5360 array/buffer larger than what size_t can represent. */
5361 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5362}
5363
87d6a7aa
SM
5364/* Lookup the index cache for the contents of the index associated to
5365 DWARF2_OBJ. */
5366
5367static gdb::array_view<const gdb_byte>
5989a64e 5368get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5369{
5370 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5371 if (build_id == nullptr)
5372 return {};
5373
5374 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5375 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5376}
5377
5378/* Same as the above, but for DWZ. */
5379
5380static gdb::array_view<const gdb_byte>
5381get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5382{
5383 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5384 if (build_id == nullptr)
5385 return {};
5386
5387 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5388}
5389
edc02ceb 5390/* See dwarf2/public.h. */
9291a0cd 5391
edc02ceb
TT
5392void
5393dwarf2_initialize_objfile (struct objfile *objfile)
9291a0cd 5394{
976ca316
SM
5395 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5396 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5397
850ed749
SM
5398 dwarf_read_debug_printf ("called");
5399
9291a0cd
TT
5400 /* If we're about to read full symbols, don't bother with the
5401 indices. In this case we also don't care if some other debug
5402 format is making psymtabs, because they are all about to be
5403 expanded anyway. */
5404 if ((objfile->flags & OBJF_READNOW))
5405 {
850ed749
SM
5406 dwarf_read_debug_printf ("readnow requested");
5407
17ee85fc
TT
5408 /* When using READNOW, the using_index flag (set below) indicates that
5409 PER_BFD was already initialized, when we loaded some other objfile. */
5410 if (per_bfd->using_index)
5411 {
850ed749 5412 dwarf_read_debug_printf ("using_index already set");
976ca316 5413 per_objfile->resize_symtabs ();
edc02ceb
TT
5414 objfile->qf.push_front (make_dwarf_gdb_index ());
5415 return;
17ee85fc
TT
5416 }
5417
5418 per_bfd->using_index = 1;
976ca316 5419 create_all_comp_units (per_objfile);
17ee85fc
TT
5420 per_bfd->quick_file_names_table
5421 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5422 per_objfile->resize_symtabs ();
9291a0cd 5423
91eea9cc 5424 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
9291a0cd 5425 {
91eea9cc 5426 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
9291a0cd 5427
17ee85fc 5428 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5429 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5430 }
5431
edc02ceb
TT
5432 /* Arrange for gdb to see the "quick" functions. However, these
5433 functions will be no-ops because we will have expanded all
5434 symtabs. */
5435 objfile->qf.push_front (make_dwarf_gdb_index ());
5436 return;
9291a0cd
TT
5437 }
5438
17ee85fc
TT
5439 /* Was a debug names index already read when we processed an objfile sharing
5440 PER_BFD? */
5441 if (per_bfd->debug_names_table != nullptr)
5442 {
850ed749 5443 dwarf_read_debug_printf ("re-using shared debug names table");
976ca316 5444 per_objfile->resize_symtabs ();
edc02ceb
TT
5445 objfile->qf.push_front (make_dwarf_debug_names ());
5446 return;
17ee85fc
TT
5447 }
5448
5449 /* Was a GDB index already read when we processed an objfile sharing
5450 PER_BFD? */
5451 if (per_bfd->index_table != nullptr)
5452 {
850ed749 5453 dwarf_read_debug_printf ("re-using shared index table");
976ca316 5454 per_objfile->resize_symtabs ();
edc02ceb
TT
5455 objfile->qf.push_front (make_dwarf_gdb_index ());
5456 return;
17ee85fc
TT
5457 }
5458
efb763a5
SM
5459 /* There might already be partial symtabs built for this BFD. This happens
5460 when loading the same binary twice with the index-cache enabled. If so,
5461 don't try to read an index. The objfile / per_objfile initialization will
5462 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5463 code path. */
5464 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
5465 {
5466 dwarf_read_debug_printf ("re-using shared partial symtabs");
edc02ceb
TT
5467 objfile->qf.push_front (make_lazy_dwarf_reader ());
5468 return;
850ed749 5469 }
efb763a5 5470
976ca316 5471 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 5472 {
850ed749 5473 dwarf_read_debug_printf ("found debug names");
976ca316 5474 per_objfile->resize_symtabs ();
edc02ceb
TT
5475 objfile->qf.push_front (make_dwarf_debug_names ());
5476 return;
3c0aa29a 5477 }
927aa2e7 5478
976ca316 5479 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 5480 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5481 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 5482 {
850ed749 5483 dwarf_read_debug_printf ("found gdb index from file");
976ca316 5484 per_objfile->resize_symtabs ();
edc02ceb
TT
5485 objfile->qf.push_front (make_dwarf_gdb_index ());
5486 return;
3c0aa29a 5487 }
9291a0cd 5488
87d6a7aa 5489 /* ... otherwise, try to find the index in the index cache. */
976ca316 5490 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
5491 get_gdb_index_contents_from_cache,
5492 get_gdb_index_contents_from_cache_dwz))
5493 {
850ed749 5494 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa 5495 global_index_cache.hit ();
976ca316 5496 per_objfile->resize_symtabs ();
edc02ceb
TT
5497 objfile->qf.push_front (make_dwarf_gdb_index ());
5498 return;
87d6a7aa
SM
5499 }
5500
5501 global_index_cache.miss ();
edc02ceb 5502 objfile->qf.push_front (make_lazy_dwarf_reader ());
9291a0cd
TT
5503}
5504
5505\f
5506
dce234bc
PP
5507/* Build a partial symbol table. */
5508
5509void
eb36a3eb 5510dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
dce234bc 5511{
976ca316
SM
5512 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5513 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
5514
5515 if (per_bfd->partial_symtabs != nullptr)
5516 {
5517 /* Partial symbols were already read, so now we can simply
5518 attach them. */
eb36a3eb
TT
5519 if (psf == nullptr)
5520 {
5521 psf = new psymbol_functions (per_bfd->partial_symtabs);
5522 objfile->qf.emplace_front (psf);
5523 }
5524 else
5525 psf->set_partial_symtabs (per_bfd->partial_symtabs);
976ca316 5526 per_objfile->resize_symtabs ();
17ee85fc
TT
5527 return;
5528 }
c9bf0622 5529
eb36a3eb
TT
5530 if (psf == nullptr)
5531 {
5532 psf = new psymbol_functions;
5533 objfile->qf.emplace_front (psf);
5534 }
5535 const std::shared_ptr<psymtab_storage> &partial_symtabs
5536 = psf->get_partial_symtabs ();
5537
51962708
TT
5538 /* Set the local reference to partial symtabs, so that we don't try
5539 to read them again if reading another objfile with the same BFD.
5540 If we can't in fact share, this won't make a difference anyway as
5541 the dwarf2_per_bfd object won't be shared. */
eb36a3eb 5542 per_bfd->partial_symtabs = partial_symtabs;
51962708 5543
a70b8144 5544 try
c9bf0622
TT
5545 {
5546 /* This isn't really ideal: all the data we allocate on the
5547 objfile's obstack is still uselessly kept around. However,
5548 freeing it seems unsafe. */
eb36a3eb 5549 psymtab_discarder psymtabs (partial_symtabs.get ());
976ca316 5550 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 5551 psymtabs.keep ();
87d6a7aa 5552
976ca316 5553 per_objfile->resize_symtabs ();
af758d11 5554
87d6a7aa 5555 /* (maybe) store an index in the cache. */
976ca316 5556 global_index_cache.store (per_objfile);
c9bf0622 5557 }
230d2906 5558 catch (const gdb_exception_error &except)
492d29ea
PA
5559 {
5560 exception_print (gdb_stderr, except);
5561 }
c906108c 5562}
c906108c 5563
3b80fe9b
DE
5564/* Find the base address of the compilation unit for range lists and
5565 location lists. It will normally be specified by DW_AT_low_pc.
5566 In DWARF-3 draft 4, the base address could be overridden by
5567 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5568 compilation units with discontinuous ranges. */
5569
5570static void
5571dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5572{
5573 struct attribute *attr;
5574
2b24b6e4 5575 cu->base_address.reset ();
3b80fe9b
DE
5576
5577 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5578 if (attr != nullptr)
95f982e5 5579 cu->base_address = attr->as_address ();
3b80fe9b
DE
5580 else
5581 {
5582 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5583 if (attr != nullptr)
95f982e5 5584 cu->base_address = attr->as_address ();
3b80fe9b
DE
5585 }
5586}
5587
36586728
TT
5588/* Helper function that returns the proper abbrev section for
5589 THIS_CU. */
5590
5591static struct dwarf2_section_info *
5592get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5593{
5594 struct dwarf2_section_info *abbrev;
c3699833 5595 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
5596
5597 if (this_cu->is_dwz)
a7308ce0 5598 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 5599 else
c3699833 5600 abbrev = &per_bfd->abbrev;
36586728
TT
5601
5602 return abbrev;
5603}
5604
f4dc4d17
DE
5605/* Fetch the abbreviation table offset from a comp or type unit header. */
5606
5607static sect_offset
976ca316 5608read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 5609 struct dwarf2_section_info *section,
9c541725 5610 sect_offset sect_off)
f4dc4d17 5611{
96b79293 5612 bfd *abfd = section->get_bfd_owner ();
d521ce57 5613 const gdb_byte *info_ptr;
ac298888 5614 unsigned int initial_length_size, offset_size;
43988095 5615 uint16_t version;
f4dc4d17 5616
976ca316 5617 section->read (per_objfile->objfile);
9c541725 5618 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5619 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5620 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5621 info_ptr += initial_length_size;
5622
5623 version = read_2_bytes (abfd, info_ptr);
5624 info_ptr += 2;
5625 if (version >= 5)
5626 {
5627 /* Skip unit type and address size. */
5628 info_ptr += 2;
5629 }
5630
24aa364d 5631 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5632}
5633
b83470bf
TT
5634/* A partial symtab that is used only for include files. */
5635struct dwarf2_include_psymtab : public partial_symtab
5636{
7b249e47
TT
5637 dwarf2_include_psymtab (const char *filename,
5638 psymtab_storage *partial_symtabs,
0072c873
SM
5639 objfile_per_bfd_storage *objfile_per_bfd)
5640 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
b83470bf
TT
5641 {
5642 }
5643
5644 void read_symtab (struct objfile *objfile) override
5645 {
194d088f
TV
5646 /* It's an include file, no symbols to read for it.
5647 Everything is in the includer symtab. */
5648
5649 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5650 expansion of the includer psymtab. We use the dependencies[0] field to
5651 model the includer. But if we go the regular route of calling
5652 expand_psymtab here, and having expand_psymtab call expand_dependencies
5653 to expand the includer, we'll only use expand_psymtab on the includer
5654 (making it a non-toplevel psymtab), while if we expand the includer via
5655 another path, we'll use read_symtab (making it a toplevel psymtab).
5656 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5657 psymtab, and trigger read_symtab on the includer here directly. */
5658 includer ()->read_symtab (objfile);
b83470bf
TT
5659 }
5660
5661 void expand_psymtab (struct objfile *objfile) override
5662 {
194d088f
TV
5663 /* This is not called by read_symtab, and should not be called by any
5664 expand_dependencies. */
5665 gdb_assert (false);
b83470bf
TT
5666 }
5667
5717c425 5668 bool readin_p (struct objfile *objfile) const override
b83470bf 5669 {
5717c425 5670 return includer ()->readin_p (objfile);
b83470bf
TT
5671 }
5672
5717c425 5673 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
5674 {
5675 return nullptr;
5676 }
5677
5678private:
194d088f
TV
5679 partial_symtab *includer () const
5680 {
5681 /* An include psymtab has exactly one dependency: the psymtab that
5682 includes it. */
5683 gdb_assert (this->number_of_dependencies == 1);
5684 return this->dependencies[0];
5685 }
b83470bf
TT
5686};
5687
aaa75496
JB
5688/* Allocate a new partial symtab for file named NAME and mark this new
5689 partial symtab as being an include of PST. */
5690
5691static void
84685904 5692dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
7b249e47
TT
5693 const char *name,
5694 dwarf2_psymtab *pst,
5695 psymtab_storage *partial_symtabs,
0072c873 5696 objfile_per_bfd_storage *objfile_per_bfd)
aaa75496 5697{
7b249e47 5698 dwarf2_include_psymtab *subpst
0072c873 5699 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
aaa75496 5700
fbd9ab74 5701 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 5702 subpst->dirname = pst->dirname;
fbd9ab74 5703
84685904 5704 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5705 subpst->dependencies[0] = pst;
5706 subpst->number_of_dependencies = 1;
aaa75496
JB
5707}
5708
5709/* Read the Line Number Program data and extract the list of files
5710 included by the source file represented by PST. Build an include
d85a05f0 5711 partial symtab for each of these included files. */
aaa75496
JB
5712
5713static void
5714dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5715 struct die_info *die,
891813be 5716 dwarf2_psymtab *pst)
aaa75496 5717{
fff8551c 5718 line_header_up lh;
d85a05f0 5719 struct attribute *attr;
aaa75496 5720
d85a05f0 5721 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
5722 if (attr != nullptr && attr->form_is_unsigned ())
5723 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
5724 if (lh == NULL)
5725 return; /* No linetable, so no includes. */
5726
79748972
TT
5727 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5728 that we pass in the raw text_low here; that is ok because we're
5729 only decoding the line table to make include partial symtabs, and
5730 so the addresses aren't really used. */
4ae976d1 5731 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5732 pst->raw_text_low (), 1);
aaa75496
JB
5733}
5734
348e048f 5735static hashval_t
52dc124a 5736hash_signatured_type (const void *item)
348e048f 5737{
9a3c8263
SM
5738 const struct signatured_type *sig_type
5739 = (const struct signatured_type *) item;
9a619af0 5740
348e048f 5741 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5742 return sig_type->signature;
348e048f
DE
5743}
5744
5745static int
52dc124a 5746eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5747{
9a3c8263
SM
5748 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5749 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5750
348e048f
DE
5751 return lhs->signature == rhs->signature;
5752}
5753
1fd400ff
TT
5754/* Allocate a hash table for signatured types. */
5755
b0b6a987 5756static htab_up
298e9637 5757allocate_signatured_type_table ()
1fd400ff 5758{
b0b6a987
TT
5759 return htab_up (htab_create_alloc (41,
5760 hash_signatured_type,
5761 eq_signatured_type,
5762 NULL, xcalloc, xfree));
1fd400ff
TT
5763}
5764
78d4d2c5 5765/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5766 and fill them into TYPES_HTAB. It will process only type units,
5767 therefore DW_UT_type. */
c88ee1f0 5768
78d4d2c5 5769static void
976ca316 5770create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 5771 struct dwo_file *dwo_file,
b0b6a987 5772 dwarf2_section_info *section, htab_up &types_htab,
43988095 5773 rcuh_kind section_kind)
348e048f 5774{
976ca316 5775 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 5776 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
5777 bfd *abfd;
5778 const gdb_byte *info_ptr, *end_ptr;
348e048f 5779
b8efb248 5780 abbrev_section = &dwo_file->sections.abbrev;
4bdcc0c1 5781
4800761a 5782 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
5783 section->get_name (),
5784 abbrev_section->get_file_name ());
09406207 5785
96b79293 5786 section->read (objfile);
78d4d2c5 5787 info_ptr = section->buffer;
348e048f 5788
78d4d2c5
JK
5789 if (info_ptr == NULL)
5790 return;
348e048f 5791
78d4d2c5
JK
5792 /* We can't set abfd until now because the section may be empty or
5793 not present, in which case the bfd is unknown. */
96b79293 5794 abfd = section->get_bfd_owner ();
348e048f 5795
c0ab21c2
TT
5796 /* We don't use cutu_reader here because we don't need to read
5797 any dies: the signature is in the header. */
3019eac3 5798
78d4d2c5
JK
5799 end_ptr = info_ptr + section->size;
5800 while (info_ptr < end_ptr)
5801 {
0d305d5c 5802 std::unique_ptr<signatured_type> sig_type;
78d4d2c5
JK
5803 struct dwo_unit *dwo_tu;
5804 void **slot;
5805 const gdb_byte *ptr = info_ptr;
5806 struct comp_unit_head header;
5807 unsigned int length;
8b70b953 5808
9c541725 5809 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 5810
a49dd8dd
JK
5811 /* Initialize it due to a false compiler warning. */
5812 header.signature = -1;
9c541725 5813 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 5814
78d4d2c5
JK
5815 /* We need to read the type's signature in order to build the hash
5816 table, but we don't need anything else just yet. */
348e048f 5817
976ca316 5818 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 5819 abbrev_section, ptr, section_kind);
348e048f 5820
4057dfde 5821 length = header.get_length ();
6caca83c 5822
78d4d2c5
JK
5823 /* Skip dummy type units. */
5824 if (ptr >= info_ptr + length
43988095 5825 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
5826 || (header.unit_type != DW_UT_type
5827 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
5828 {
5829 info_ptr += length;
5830 continue;
5831 }
dee91e82 5832
78d4d2c5 5833 if (types_htab == NULL)
b8efb248 5834 types_htab = allocate_dwo_unit_table ();
8b70b953 5835
b8efb248
TT
5836 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5837 dwo_tu->dwo_file = dwo_file;
5838 dwo_tu->signature = header.signature;
5839 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5840 dwo_tu->section = section;
5841 dwo_tu->sect_off = sect_off;
5842 dwo_tu->length = length;
78d4d2c5 5843
b8efb248 5844 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
78d4d2c5
JK
5845 gdb_assert (slot != NULL);
5846 if (*slot != NULL)
b8efb248
TT
5847 complaint (_("debug type entry at offset %s is duplicate to"
5848 " the entry at offset %s, signature %s"),
5849 sect_offset_str (sect_off),
5850 sect_offset_str (dwo_tu->sect_off),
5851 hex_string (header.signature));
5852 *slot = dwo_tu;
3019eac3 5853
6f738b01
SM
5854 dwarf_read_debug_printf_v (" offset %s, signature %s",
5855 sect_offset_str (sect_off),
5856 hex_string (header.signature));
3019eac3 5857
78d4d2c5
JK
5858 info_ptr += length;
5859 }
5860}
3019eac3 5861
78d4d2c5
JK
5862/* Create the hash table of all entries in the .debug_types
5863 (or .debug_types.dwo) section(s).
b8efb248 5864 DWO_FILE is a pointer to the DWO file object.
b3c8eb43 5865
78d4d2c5 5866 The result is a pointer to the hash table or NULL if there are no types.
348e048f 5867
78d4d2c5 5868 Note: This function processes DWO files only, not DWP files. */
348e048f 5869
78d4d2c5 5870static void
976ca316 5871create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 5872 struct dwo_file *dwo_file,
fd5866f6 5873 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 5874 htab_up &types_htab)
78d4d2c5 5875{
fd5866f6 5876 for (dwarf2_section_info &section : type_sections)
976ca316
SM
5877 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5878 rcuh_kind::TYPE);
3019eac3
DE
5879}
5880
5989a64e 5881/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
5882 If SLOT is non-NULL, it is the entry to use in the hash table.
5883 Otherwise we find one. */
5884
5885static struct signatured_type *
976ca316 5886add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 5887{
91eea9cc
TT
5888 if (per_objfile->per_bfd->all_comp_units.size ()
5889 == per_objfile->per_bfd->all_comp_units.capacity ())
976ca316 5890 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 5891
0d305d5c
TT
5892 std::unique_ptr<signatured_type> sig_type_holder
5893 = per_objfile->per_bfd->allocate_signatured_type ();
5894 signatured_type *sig_type = sig_type_holder.get ();
b2bdb8cf 5895
976ca316 5896 per_objfile->resize_symtabs ();
af758d11 5897
473ab964
TT
5898 per_objfile->per_bfd->all_comp_units.emplace_back
5899 (sig_type_holder.release ());
6aa5f3a6 5900 sig_type->signature = sig;
0d305d5c 5901 sig_type->is_debug_types = 1;
976ca316 5902 if (per_objfile->per_bfd->using_index)
6aa5f3a6 5903 {
0d305d5c 5904 sig_type->v.quick =
976ca316 5905 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
5906 struct dwarf2_per_cu_quick_data);
5907 }
5908
5909 if (slot == NULL)
5910 {
976ca316 5911 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
5912 sig_type, INSERT);
5913 }
5914 gdb_assert (*slot == NULL);
5915 *slot = sig_type;
5916 /* The rest of sig_type must be filled in by the caller. */
5917 return sig_type;
5918}
5919
a2ce51a0
DE
5920/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5921 Fill in SIG_ENTRY with DWO_ENTRY. */
5922
5923static void
976ca316 5924fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
5925 struct signatured_type *sig_entry,
5926 struct dwo_unit *dwo_entry)
5927{
976ca316 5928 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 5929
7ee85ab1 5930 /* Make sure we're not clobbering something we don't expect to. */
0d305d5c
TT
5931 gdb_assert (! sig_entry->queued);
5932 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
1859c670 5933 if (per_bfd->using_index)
6aa5f3a6 5934 {
0d305d5c
TT
5935 gdb_assert (sig_entry->v.quick != NULL);
5936 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6aa5f3a6
DE
5937 }
5938 else
0d305d5c 5939 gdb_assert (sig_entry->v.psymtab == NULL);
a2ce51a0 5940 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 5941 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 5942 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
5943 gdb_assert (sig_entry->dwo_unit == NULL);
5944
0d305d5c
TT
5945 sig_entry->section = dwo_entry->section;
5946 sig_entry->sect_off = dwo_entry->sect_off;
5947 sig_entry->length = dwo_entry->length;
5948 sig_entry->reading_dwo_directly = 1;
5949 sig_entry->per_bfd = per_bfd;
a2ce51a0
DE
5950 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5951 sig_entry->dwo_unit = dwo_entry;
5952}
5953
5954/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
5955 If we haven't read the TU yet, create the signatured_type data structure
5956 for a TU to be read in directly from a DWO file, bypassing the stub.
5957 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5958 using .gdb_index, then when reading a CU we want to stay in the DWO file
5959 containing that CU. Otherwise we could end up reading several other DWO
5960 files (due to comdat folding) to process the transitive closure of all the
5961 mentioned TUs, and that can be slow. The current DWO file will have every
5962 type signature that it needs.
a2ce51a0
DE
5963 We only do this for .gdb_index because in the psymtab case we already have
5964 to read all the DWOs to build the type unit groups. */
5965
5966static struct signatured_type *
5967lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5968{
976ca316 5969 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
5970 struct dwo_file *dwo_file;
5971 struct dwo_unit find_dwo_entry, *dwo_entry;
5972 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 5973 void **slot;
a2ce51a0 5974
976ca316 5975 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 5976
6aa5f3a6
DE
5977 /* If TU skeletons have been removed then we may not have read in any
5978 TUs yet. */
976ca316
SM
5979 if (per_objfile->per_bfd->signatured_types == NULL)
5980 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
5981
5982 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
5983 Use the global signatured_types array to do our own comdat-folding
5984 of types. If this is the first time we're reading this TU, and
5985 the TU has an entry in .gdb_index, replace the recorded data from
5986 .gdb_index with this TU. */
a2ce51a0 5987
a2ce51a0 5988 find_sig_entry.signature = sig;
976ca316 5989 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 5990 &find_sig_entry, INSERT);
9a3c8263 5991 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
5992
5993 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
5994 read. Don't reassign the global entry to point to this DWO if that's
5995 the case. Also note that if the TU is already being read, it may not
5996 have come from a DWO, the program may be a mix of Fission-compiled
5997 code and non-Fission-compiled code. */
5998
5999 /* Have we already tried to read this TU?
6000 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6001 needn't exist in the global table yet). */
0d305d5c 6002 if (sig_entry != NULL && sig_entry->tu_read)
a2ce51a0
DE
6003 return sig_entry;
6004
6aa5f3a6
DE
6005 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6006 dwo_unit of the TU itself. */
6007 dwo_file = cu->dwo_unit->dwo_file;
6008
a2ce51a0
DE
6009 /* Ok, this is the first time we're reading this TU. */
6010 if (dwo_file->tus == NULL)
6011 return NULL;
6012 find_dwo_entry.signature = sig;
b0b6a987
TT
6013 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6014 &find_dwo_entry);
a2ce51a0
DE
6015 if (dwo_entry == NULL)
6016 return NULL;
6017
6aa5f3a6
DE
6018 /* If the global table doesn't have an entry for this TU, add one. */
6019 if (sig_entry == NULL)
976ca316 6020 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6021
976ca316 6022 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
0d305d5c 6023 sig_entry->tu_read = 1;
a2ce51a0
DE
6024 return sig_entry;
6025}
6026
a2ce51a0
DE
6027/* Subroutine of lookup_signatured_type.
6028 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6029 then try the DWP file. If the TU stub (skeleton) has been removed then
6030 it won't be in .gdb_index. */
a2ce51a0
DE
6031
6032static struct signatured_type *
6033lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6034{
976ca316
SM
6035 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6036 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6037 struct dwo_unit *dwo_entry;
6038 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6039 void **slot;
a2ce51a0 6040
976ca316 6041 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6042 gdb_assert (dwp_file != NULL);
6043
6aa5f3a6
DE
6044 /* If TU skeletons have been removed then we may not have read in any
6045 TUs yet. */
976ca316
SM
6046 if (per_objfile->per_bfd->signatured_types == NULL)
6047 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6048
6aa5f3a6 6049 find_sig_entry.signature = sig;
976ca316 6050 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6051 &find_sig_entry, INSERT);
9a3c8263 6052 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6053
6054 /* Have we already tried to read this TU?
6055 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6056 needn't exist in the global table yet). */
6057 if (sig_entry != NULL)
6058 return sig_entry;
6059
a2ce51a0
DE
6060 if (dwp_file->tus == NULL)
6061 return NULL;
976ca316
SM
6062 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6063 1 /* is_debug_types */);
a2ce51a0
DE
6064 if (dwo_entry == NULL)
6065 return NULL;
6066
976ca316
SM
6067 sig_entry = add_type_unit (per_objfile, sig, slot);
6068 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6069
a2ce51a0
DE
6070 return sig_entry;
6071}
6072
380bca97 6073/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6074 Returns NULL if signature SIG is not present in the table.
6075 It is up to the caller to complain about this. */
348e048f
DE
6076
6077static struct signatured_type *
a2ce51a0 6078lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6079{
976ca316 6080 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6081
976ca316 6082 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6083 {
6084 /* We're in a DWO/DWP file, and we're using .gdb_index.
6085 These cases require special processing. */
976ca316 6086 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6087 return lookup_dwo_signatured_type (cu, sig);
6088 else
6089 return lookup_dwp_signatured_type (cu, sig);
6090 }
6091 else
6092 {
6093 struct signatured_type find_entry, *entry;
348e048f 6094
976ca316 6095 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6096 return NULL;
6097 find_entry.signature = sig;
9a3c8263 6098 entry = ((struct signatured_type *)
976ca316 6099 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6100 &find_entry));
a2ce51a0
DE
6101 return entry;
6102 }
348e048f 6103}
18a8505e 6104
42e7ad6c 6105/* Low level DIE reading support. */
348e048f 6106
d85a05f0
DJ
6107/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6108
6109static void
6110init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6111 struct dwarf2_cu *cu,
3019eac3 6112 struct dwarf2_section_info *section,
685af9cd
TT
6113 struct dwo_file *dwo_file,
6114 struct abbrev_table *abbrev_table)
d85a05f0 6115{
fceca515 6116 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6117 reader->abfd = section->get_bfd_owner ();
d85a05f0 6118 reader->cu = cu;
3019eac3 6119 reader->dwo_file = dwo_file;
dee91e82
DE
6120 reader->die_section = section;
6121 reader->buffer = section->buffer;
f664829e 6122 reader->buffer_end = section->buffer + section->size;
685af9cd 6123 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6124}
6125
c0ab21c2 6126/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6127 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6128 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6129 already.
6130
6131 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6132 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6133 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6134 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6135 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6136 STUB_COMP_DIR may be non-NULL.
3e225074 6137 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6138 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6139 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6140 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6141 kept around for at least as long as *RESULT_READER.
6142
b0c7bfa9
DE
6143 The result is non-zero if a valid (non-dummy) DIE was found. */
6144
6145static int
4ab09049 6146read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6147 struct dwo_unit *dwo_unit,
b0c7bfa9 6148 struct die_info *stub_comp_unit_die,
a2ce51a0 6149 const char *stub_comp_dir,
b0c7bfa9 6150 struct die_reader_specs *result_reader,
d521ce57 6151 const gdb_byte **result_info_ptr,
b0c7bfa9 6152 struct die_info **result_comp_unit_die,
685af9cd 6153 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6154{
976ca316 6155 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6156 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6157 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6158 bfd *abfd;
d521ce57 6159 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6160 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6161 int i,num_extra_attrs;
6162 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6163 struct die_info *comp_unit_die;
6164
b0aeadb3
DE
6165 /* At most one of these may be provided. */
6166 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6167
b0c7bfa9
DE
6168 /* These attributes aren't processed until later:
6169 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6170 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6171 referenced later. However, these attributes are found in the stub
6172 which we won't have later. In order to not impose this complication
6173 on the rest of the code, we read them here and copy them to the
6174 DWO CU/TU die. */
b0c7bfa9
DE
6175
6176 stmt_list = NULL;
6177 low_pc = NULL;
6178 high_pc = NULL;
6179 ranges = NULL;
6180 comp_dir = NULL;
6181
6182 if (stub_comp_unit_die != NULL)
6183 {
6184 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6185 DWO file. */
4ab09049 6186 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6187 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6188 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6189 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6190 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6191 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6192
a39fdb41 6193 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6194
2b0c7f41
SM
6195 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6196 We need the value before we can process DW_AT_ranges values from the
6197 DWO. */
6198 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6199
6200 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6201 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6202 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6203 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6204 section. */
6205 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6206 }
a2ce51a0
DE
6207 else if (stub_comp_dir != NULL)
6208 {
6209 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6210 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6211 comp_dir->name = DW_AT_comp_dir;
6212 comp_dir->form = DW_FORM_string;
c6481205 6213 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6214 }
b0c7bfa9
DE
6215
6216 /* Set up for reading the DWO CU/TU. */
6217 cu->dwo_unit = dwo_unit;
685af9cd 6218 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6219 section->read (objfile);
6220 abfd = section->get_bfd_owner ();
9c541725
PA
6221 begin_info_ptr = info_ptr = (section->buffer
6222 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6223 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6224
4ab09049 6225 if (per_cu->is_debug_types)
b0c7bfa9 6226 {
4ab09049 6227 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6228
976ca316
SM
6229 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6230 section, dwo_abbrev_section,
43988095 6231 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6232 /* This is not an assert because it can be caused by bad debug info. */
43988095 6233 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6234 {
6235 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6236 " TU at offset %s [in module %s]"),
a2ce51a0 6237 hex_string (sig_type->signature),
43988095 6238 hex_string (cu->header.signature),
9d8780f0 6239 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6240 bfd_get_filename (abfd));
6241 }
9c541725 6242 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6243 /* For DWOs coming from DWP files, we don't know the CU length
6244 nor the type's offset in the TU until now. */
4057dfde 6245 dwo_unit->length = cu->header.get_length ();
9c541725 6246 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6247
6248 /* Establish the type offset that can be used to lookup the type.
6249 For DWO files, we don't know it until now. */
9c541725
PA
6250 sig_type->type_offset_in_section
6251 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6252 }
6253 else
6254 {
976ca316
SM
6255 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6256 section, dwo_abbrev_section,
43988095 6257 info_ptr, rcuh_kind::COMPILE);
9c541725 6258 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6259 /* For DWOs coming from DWP files, we don't know the CU length
6260 until now. */
4057dfde 6261 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6262 }
6263
606decb2 6264 dwo_abbrev_section->read (objfile);
685af9cd 6265 *result_dwo_abbrev_table
606decb2 6266 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6267 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6268 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6269
6270 /* Read in the die, but leave space to copy over the attributes
6271 from the stub. This has the benefit of simplifying the rest of
6272 the code - all the work to maintain the illusion of a single
6273 DW_TAG_{compile,type}_unit DIE is done here. */
6274 num_extra_attrs = ((stmt_list != NULL)
6275 + (low_pc != NULL)
6276 + (high_pc != NULL)
6277 + (ranges != NULL)
6278 + (comp_dir != NULL));
6279 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6280 num_extra_attrs);
b0c7bfa9
DE
6281
6282 /* Copy over the attributes from the stub to the DIE we just read in. */
6283 comp_unit_die = *result_comp_unit_die;
6284 i = comp_unit_die->num_attrs;
6285 if (stmt_list != NULL)
6286 comp_unit_die->attrs[i++] = *stmt_list;
6287 if (low_pc != NULL)
6288 comp_unit_die->attrs[i++] = *low_pc;
6289 if (high_pc != NULL)
6290 comp_unit_die->attrs[i++] = *high_pc;
6291 if (ranges != NULL)
6292 comp_unit_die->attrs[i++] = *ranges;
6293 if (comp_dir != NULL)
6294 comp_unit_die->attrs[i++] = *comp_dir;
6295 comp_unit_die->num_attrs += num_extra_attrs;
6296
b4f54984 6297 if (dwarf_die_debug)
bf6af496
DE
6298 {
6299 fprintf_unfiltered (gdb_stdlog,
6300 "Read die from %s@0x%x of %s:\n",
96b79293 6301 section->get_name (),
bf6af496
DE
6302 (unsigned) (begin_info_ptr - section->buffer),
6303 bfd_get_filename (abfd));
b4f54984 6304 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6305 }
6306
b0c7bfa9
DE
6307 /* Skip dummy compilation units. */
6308 if (info_ptr >= begin_info_ptr + dwo_unit->length
6309 || peek_abbrev_code (abfd, info_ptr) == 0)
6310 return 0;
6311
6312 *result_info_ptr = info_ptr;
6313 return 1;
6314}
6315
a084a2a6
AT
6316/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6317 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6318 signature is part of the header. */
6319static gdb::optional<ULONGEST>
6320lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6321{
6322 if (cu->header.version >= 5)
6323 return cu->header.signature;
6324 struct attribute *attr;
6325 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 6326 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 6327 return gdb::optional<ULONGEST> ();
529908cb 6328 return attr->as_unsigned ();
a084a2a6
AT
6329}
6330
c0ab21c2 6331/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6332 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6333 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6334
6335static struct dwo_unit *
4ab09049 6336lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6337{
4ab09049 6338 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6339 struct dwo_unit *dwo_unit;
c0ab21c2 6340 const char *comp_dir;
b0c7bfa9 6341
a2ce51a0
DE
6342 gdb_assert (cu != NULL);
6343
b0c7bfa9 6344 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6345 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6346 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6347
4ab09049
SM
6348 if (per_cu->is_debug_types)
6349 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6350 else
6351 {
a084a2a6 6352 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6353
a084a2a6 6354 if (!signature.has_value ())
b0c7bfa9
DE
6355 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6356 " [in module %s]"),
4ab09049
SM
6357 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6358
6359 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6360 }
6361
b0c7bfa9
DE
6362 return dwo_unit;
6363}
6364
c0ab21c2 6365/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6366 See it for a description of the parameters.
fcd3b13d 6367 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6368
c0ab21c2 6369void
9e021579
SM
6370cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6371 dwarf2_per_objfile *per_objfile,
2e671100 6372 dwarf2_cu *existing_cu)
a2ce51a0 6373{
a2ce51a0 6374 struct signatured_type *sig_type;
a2ce51a0
DE
6375
6376 /* Verify we can do the following downcast, and that we have the
6377 data we need. */
6378 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6379 sig_type = (struct signatured_type *) this_cu;
6380 gdb_assert (sig_type->dwo_unit != NULL);
6381
2e671100
SM
6382 dwarf2_cu *cu;
6383
6384 if (existing_cu != nullptr)
6aa5f3a6 6385 {
2e671100
SM
6386 cu = existing_cu;
6387 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6388 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6389 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6390 }
6391 else
6392 {
7188ed02 6393 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6394 in per_objfile yet. */
7188ed02 6395 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6396 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6397 cu = m_new_cu.get ();
6aa5f3a6
DE
6398 }
6399
6400 /* A future optimization, if needed, would be to use an existing
6401 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6402 could share abbrev tables. */
a2ce51a0 6403
2e671100 6404 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6405 NULL /* stub_comp_unit_die */,
6406 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6407 this, &info_ptr,
3e225074 6408 &comp_unit_die,
c0ab21c2 6409 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6410 {
6411 /* Dummy die. */
c0ab21c2 6412 dummy_p = true;
a2ce51a0 6413 }
a2ce51a0
DE
6414}
6415
fd820528 6416/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6417 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6418
f4dc4d17
DE
6419 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6420 Otherwise the table specified in the comp unit header is read in and used.
6421 This is an optimization for when we already have the abbrev table.
6422
2e671100
SM
6423 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6424 allocated. */
aaa75496 6425
ab432490 6426cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6427 dwarf2_per_objfile *per_objfile,
c0ab21c2 6428 struct abbrev_table *abbrev_table,
2e671100 6429 dwarf2_cu *existing_cu,
c0ab21c2
TT
6430 bool skip_partial)
6431 : die_reader_specs {},
6751ebae 6432 m_this_cu (this_cu)
c906108c 6433{
976ca316 6434 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6435 struct dwarf2_section_info *section = this_cu->section;
96b79293 6436 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 6437 const gdb_byte *begin_info_ptr;
dee91e82 6438 struct signatured_type *sig_type = NULL;
4bdcc0c1 6439 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6440 /* Non-zero if CU currently points to a DWO file and we need to
6441 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6442 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6443 int rereading_dwo_cu = 0;
c906108c 6444
b4f54984 6445 if (dwarf_die_debug)
9d8780f0 6446 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6447 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6448 sect_offset_str (this_cu->sect_off));
09406207 6449
a2ce51a0
DE
6450 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6451 file (instead of going through the stub), short-circuit all of this. */
6452 if (this_cu->reading_dwo_directly)
6453 {
6454 /* Narrow down the scope of possibilities to have to understand. */
6455 gdb_assert (this_cu->is_debug_types);
6456 gdb_assert (abbrev_table == NULL);
976ca316 6457 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
6458 return;
6459 }
6460
dee91e82 6461 /* This is cheap if the section is already read in. */
96b79293 6462 section->read (objfile);
dee91e82 6463
9c541725 6464 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6465
6466 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 6467
2e671100
SM
6468 dwarf2_cu *cu;
6469
6470 if (existing_cu != nullptr)
dee91e82 6471 {
2e671100 6472 cu = existing_cu;
42e7ad6c
DE
6473 /* If this CU is from a DWO file we need to start over, we need to
6474 refetch the attributes from the skeleton CU.
6475 This could be optimized by retrieving those attributes from when we
6476 were here the first time: the previous comp_unit_die was stored in
6477 comp_unit_obstack. But there's no data yet that we need this
6478 optimization. */
6479 if (cu->dwo_unit != NULL)
6480 rereading_dwo_cu = 1;
dee91e82
DE
6481 }
6482 else
6483 {
7188ed02 6484 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6485 in per_objfile yet. */
976ca316
SM
6486 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6487 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 6488 cu = m_new_cu.get ();
42e7ad6c 6489 }
dee91e82 6490
b0c7bfa9 6491 /* Get the header. */
9c541725 6492 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6493 {
6494 /* We already have the header, there's no need to read it in again. */
9c541725 6495 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6496 }
6497 else
6498 {
3019eac3 6499 if (this_cu->is_debug_types)
dee91e82 6500 {
976ca316
SM
6501 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6502 section, abbrev_section,
6503 info_ptr, rcuh_kind::TYPE);
dee91e82 6504
42e7ad6c
DE
6505 /* Since per_cu is the first member of struct signatured_type,
6506 we can go from a pointer to one to a pointer to the other. */
6507 sig_type = (struct signatured_type *) this_cu;
43988095 6508 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6509 gdb_assert (sig_type->type_offset_in_tu
6510 == cu->header.type_cu_offset_in_tu);
6511 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6512
42e7ad6c
DE
6513 /* LENGTH has not been set yet for type units if we're
6514 using .gdb_index. */
4057dfde 6515 this_cu->length = cu->header.get_length ();
3019eac3
DE
6516
6517 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6518 sig_type->type_offset_in_section =
6519 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6520
6521 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6522 }
6523 else
6524 {
976ca316
SM
6525 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6526 section, abbrev_section,
43988095
JK
6527 info_ptr,
6528 rcuh_kind::COMPILE);
dee91e82 6529
9c541725 6530 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
6531 if (this_cu->length == 0)
6532 this_cu->length = cu->header.get_length ();
6533 else
6534 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6535 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6536 }
6537 }
10b3939b 6538
6caca83c 6539 /* Skip dummy compilation units. */
dee91e82 6540 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6541 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6542 {
6543 dummy_p = true;
6544 return;
6545 }
6caca83c 6546
433df2d4
DE
6547 /* If we don't have them yet, read the abbrevs for this compilation unit.
6548 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6549 done. */
f4dc4d17 6550 if (abbrev_table != NULL)
685af9cd
TT
6551 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6552 else
f4dc4d17 6553 {
606decb2 6554 abbrev_section->read (objfile);
c0ab21c2 6555 m_abbrev_table_holder
606decb2 6556 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 6557 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6558 }
af703f96 6559
dee91e82 6560 /* Read the top level CU/TU die. */
c0ab21c2 6561 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6562 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6563
58f0c718 6564 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6565 {
6566 dummy_p = true;
6567 return;
6568 }
58f0c718 6569
b0c7bfa9 6570 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6571 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6572 table from the DWO file and pass the ownership over to us. It will be
6573 referenced from READER, so we must make sure to free it after we're done
6574 with READER.
6575
b0c7bfa9
DE
6576 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6577 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6578 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6579 if (dwo_name != nullptr)
3019eac3 6580 {
3019eac3 6581 struct dwo_unit *dwo_unit;
b0c7bfa9 6582 struct die_info *dwo_comp_unit_die;
3019eac3 6583
3e225074 6584 if (comp_unit_die->has_children)
6a506a2d 6585 {
b98664d3 6586 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6587 " has children (offset %s) [in module %s]"),
6588 sect_offset_str (this_cu->sect_off),
6589 bfd_get_filename (abfd));
6a506a2d 6590 }
4ab09049 6591 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 6592 if (dwo_unit != NULL)
3019eac3 6593 {
4ab09049 6594 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 6595 comp_unit_die, NULL,
c0ab21c2 6596 this, &info_ptr,
3e225074 6597 &dwo_comp_unit_die,
c0ab21c2 6598 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6599 {
6600 /* Dummy die. */
c0ab21c2 6601 dummy_p = true;
6a506a2d
DE
6602 return;
6603 }
6604 comp_unit_die = dwo_comp_unit_die;
6605 }
6606 else
6607 {
6608 /* Yikes, we couldn't find the rest of the DIE, we only have
6609 the stub. A complaint has already been logged. There's
6610 not much more we can do except pass on the stub DIE to
6611 die_reader_func. We don't want to throw an error on bad
6612 debug info. */
3019eac3
DE
6613 }
6614 }
c0ab21c2 6615}
3019eac3 6616
6751ebae
TT
6617void
6618cutu_reader::keep ()
c0ab21c2 6619{
b0c7bfa9 6620 /* Done, clean up. */
6751ebae
TT
6621 gdb_assert (!dummy_p);
6622 if (m_new_cu != NULL)
348e048f 6623 {
7188ed02 6624 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 6625 now. */
7188ed02
SM
6626 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6627 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 6628 }
dee91e82
DE
6629}
6630
18a8505e
AT
6631/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6632 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6633 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6634
6635 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6636 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6637
6638 We fill in THIS_CU->length.
6639
dee91e82 6640 THIS_CU->cu is always freed when done.
3019eac3 6641 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6642 to care whether it refers to the "main" CU or the DWO CU.
6643
6644 When parent_cu is passed, it is used to provide a default value for
6645 str_offsets_base and addr_base from the parent. */
dee91e82 6646
ab432490 6647cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6648 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
6649 struct dwarf2_cu *parent_cu,
6650 struct dwo_file *dwo_file)
6651 : die_reader_specs {},
6652 m_this_cu (this_cu)
dee91e82 6653{
976ca316 6654 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6655 struct dwarf2_section_info *section = this_cu->section;
96b79293 6656 bfd *abfd = section->get_bfd_owner ();
33e80786 6657 struct dwarf2_section_info *abbrev_section;
d521ce57 6658 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6659
b4f54984 6660 if (dwarf_die_debug)
9d8780f0 6661 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6662 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6663 sect_offset_str (this_cu->sect_off));
09406207 6664
976ca316 6665 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 6666
33e80786
DE
6667 abbrev_section = (dwo_file != NULL
6668 ? &dwo_file->sections.abbrev
6669 : get_abbrev_section_for_cu (this_cu));
6670
dee91e82 6671 /* This is cheap if the section is already read in. */
96b79293 6672 section->read (objfile);
dee91e82 6673
976ca316 6674 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 6675
9c541725 6676 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
6677 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6678 section, abbrev_section, info_ptr,
43988095
JK
6679 (this_cu->is_debug_types
6680 ? rcuh_kind::TYPE
6681 : rcuh_kind::COMPILE));
dee91e82 6682
18a8505e
AT
6683 if (parent_cu != nullptr)
6684 {
c0ab21c2
TT
6685 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6686 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 6687 }
4057dfde 6688 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
6689
6690 /* Skip dummy compilation units. */
6691 if (info_ptr >= begin_info_ptr + this_cu->length
6692 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6693 {
6694 dummy_p = true;
6695 return;
6696 }
72bf9492 6697
606decb2 6698 abbrev_section->read (objfile);
c0ab21c2 6699 m_abbrev_table_holder
606decb2 6700 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 6701
c0ab21c2
TT
6702 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6703 m_abbrev_table_holder.get ());
3e225074 6704 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
6705}
6706
0018ea6f
DE
6707\f
6708/* Type Unit Groups.
dee91e82 6709
0018ea6f
DE
6710 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6711 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6712 so that all types coming from the same compilation (.o file) are grouped
6713 together. A future step could be to put the types in the same symtab as
6714 the CU the types ultimately came from. */
ff013f42 6715
f4dc4d17
DE
6716static hashval_t
6717hash_type_unit_group (const void *item)
6718{
9a3c8263
SM
6719 const struct type_unit_group *tu_group
6720 = (const struct type_unit_group *) item;
f4dc4d17 6721
094b34ac 6722 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6723}
348e048f
DE
6724
6725static int
f4dc4d17 6726eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6727{
9a3c8263
SM
6728 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6729 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6730
094b34ac 6731 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6732}
348e048f 6733
f4dc4d17
DE
6734/* Allocate a hash table for type unit groups. */
6735
eaa5fa8b 6736static htab_up
298e9637 6737allocate_type_unit_groups_table ()
f4dc4d17 6738{
eaa5fa8b
TT
6739 return htab_up (htab_create_alloc (3,
6740 hash_type_unit_group,
6741 eq_type_unit_group,
0d305d5c
TT
6742 [] (void *arg)
6743 {
6744 type_unit_group *grp
6745 = (type_unit_group *) arg;
6746 delete grp;
6747 },
6748 xcalloc, xfree));
f4dc4d17 6749}
dee91e82 6750
f4dc4d17
DE
6751/* Type units that don't have DW_AT_stmt_list are grouped into their own
6752 partial symtabs. We combine several TUs per psymtab to not let the size
6753 of any one psymtab grow too big. */
6754#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6755#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6756
094b34ac 6757/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6758 Create the type_unit_group object used to hold one or more TUs. */
6759
0d305d5c 6760static std::unique_ptr<type_unit_group>
094b34ac 6761create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 6762{
976ca316
SM
6763 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6764 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 6765
0d305d5c
TT
6766 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6767 tu_group->per_bfd = per_bfd;
f4dc4d17 6768
1859c670 6769 if (per_bfd->using_index)
094b34ac 6770 {
0d305d5c
TT
6771 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6772 struct dwarf2_per_cu_quick_data);
094b34ac
DE
6773 }
6774 else
6775 {
9c541725 6776 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 6777 dwarf2_psymtab *pst;
528e1572 6778 std::string name;
094b34ac
DE
6779
6780 /* Give the symtab a useful name for debug purposes. */
6781 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
6782 name = string_printf ("<type_units_%d>",
6783 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 6784 else
528e1572 6785 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 6786
0d305d5c
TT
6787 pst = create_partial_symtab (tu_group.get (), per_objfile,
6788 name.c_str ());
6d94535f 6789 pst->anonymous = true;
094b34ac 6790 }
f4dc4d17 6791
094b34ac 6792 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 6793 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
6794
6795 return tu_group;
6796}
6797
094b34ac
DE
6798/* Look up the type_unit_group for type unit CU, and create it if necessary.
6799 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
6800
6801static struct type_unit_group *
ff39bb5e 6802get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 6803{
976ca316
SM
6804 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6805 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
6806 struct type_unit_group *tu_group;
6807 void **slot;
6808 unsigned int line_offset;
6809 struct type_unit_group type_unit_group_for_lookup;
6810
976ca316
SM
6811 if (per_objfile->per_bfd->type_unit_groups == NULL)
6812 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
6813
6814 /* Do we need to create a new group, or can we use an existing one? */
6815
529908cb 6816 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 6817 {
529908cb 6818 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
6819 ++tu_stats->nr_symtab_sharers;
6820 }
6821 else
6822 {
6823 /* Ugh, no stmt_list. Rare, but we have to handle it.
6824 We can do various things here like create one group per TU or
6825 spread them over multiple groups to split up the expansion work.
6826 To avoid worst case scenarios (too many groups or too large groups)
6827 we, umm, group them in bunches. */
6828 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6829 | (tu_stats->nr_stmt_less_type_units
6830 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6831 ++tu_stats->nr_stmt_less_type_units;
6832 }
6833
094b34ac 6834 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 6835 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 6836 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17 6837 &type_unit_group_for_lookup, INSERT);
0d305d5c 6838 if (*slot == nullptr)
f4dc4d17 6839 {
9c541725 6840 sect_offset line_offset_struct = (sect_offset) line_offset;
0d305d5c
TT
6841 std::unique_ptr<type_unit_group> grp
6842 = create_type_unit_group (cu, line_offset_struct);
6843 *slot = grp.release ();
f4dc4d17
DE
6844 ++tu_stats->nr_symtabs;
6845 }
6846
0d305d5c
TT
6847 tu_group = (struct type_unit_group *) *slot;
6848 gdb_assert (tu_group != nullptr);
f4dc4d17
DE
6849 return tu_group;
6850}
0018ea6f
DE
6851\f
6852/* Partial symbol tables. */
6853
6854/* Create a psymtab named NAME and assign it to PER_CU.
6855
6856 The caller must fill in the following details:
6857 dirname, textlow, texthigh. */
6858
891813be 6859static dwarf2_psymtab *
7aa104c4
SM
6860create_partial_symtab (dwarf2_per_cu_data *per_cu,
6861 dwarf2_per_objfile *per_objfile,
6862 const char *name)
0018ea6f 6863{
0072c873
SM
6864 dwarf2_psymtab *pst
6865 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
6866 per_objfile->objfile->per_bfd, per_cu);
0018ea6f 6867
6d94535f 6868 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
6869
6870 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
6871 per_cu->v.psymtab = pst;
6872
6873 return pst;
6874}
6875
c0ab21c2 6876/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
6877
6878static void
6879process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 6880 const gdb_byte *info_ptr,
0018ea6f 6881 struct die_info *comp_unit_die,
c0ab21c2 6882 enum language pretend_language)
0018ea6f
DE
6883{
6884 struct dwarf2_cu *cu = reader->cu;
7aa104c4 6885 dwarf2_per_objfile *per_objfile = cu->per_objfile;
84685904 6886 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7aa104c4 6887 struct objfile *objfile = per_objfile->objfile;
08feed99 6888 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 6889 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
6890 CORE_ADDR baseaddr;
6891 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 6892 dwarf2_psymtab *pst;
3a2b436a 6893 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 6894 const char *filename;
0018ea6f 6895
0018ea6f
DE
6896 gdb_assert (! per_cu->is_debug_types);
6897
c0ab21c2 6898 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 6899
0018ea6f 6900 /* Allocate a new partial symbol table structure. */
2e927613
TV
6901 gdb::unique_xmalloc_ptr<char> debug_filename;
6902 static const char artificial[] = "<artificial>";
7d45c7c3
KB
6903 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6904 if (filename == NULL)
0018ea6f 6905 filename = "";
2e927613
TV
6906 else if (strcmp (filename, artificial) == 0)
6907 {
6908 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
6909 sect_offset_str (per_cu->sect_off),
6910 (char *) NULL));
2e927613
TV
6911 filename = debug_filename.get ();
6912 }
0018ea6f 6913
7aa104c4 6914 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
6915
6916 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 6917 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 6918
b3b3bada 6919 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
6920
6921 dwarf2_find_base_address (comp_unit_die, cu);
6922
6923 /* Possibly set the default values of LOWPC and HIGHPC from
6924 `DW_AT_ranges'. */
3a2b436a
JK
6925 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6926 &best_highpc, cu, pst);
6927 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
6928 {
6929 CORE_ADDR low
6930 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
6931 - baseaddr);
6932 CORE_ADDR high
6933 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
6934 - baseaddr - 1);
6935 /* Store the contiguous range if it is not empty; it can be
6936 empty for CUs with no code. */
84685904 6937 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 6938 low, high, pst);
79748972 6939 }
0018ea6f
DE
6940
6941 /* Check if comp unit has_children.
6942 If so, read the rest of the partial symbols from this comp unit.
6943 If not, there's no more debug_info for this comp unit. */
3e225074 6944 if (comp_unit_die->has_children)
0018ea6f
DE
6945 {
6946 struct partial_die_info *first_die;
6947 CORE_ADDR lowpc, highpc;
6948
6949 lowpc = ((CORE_ADDR) -1);
6950 highpc = ((CORE_ADDR) 0);
6951
6952 first_die = load_partial_dies (reader, info_ptr, 1);
6953
6954 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 6955 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
6956
6957 /* If we didn't find a lowpc, set it to highpc to avoid
6958 complaints from `maint check'. */
6959 if (lowpc == ((CORE_ADDR) -1))
6960 lowpc = highpc;
6961
6962 /* If the compilation unit didn't have an explicit address range,
6963 then use the information extracted from its child dies. */
e385593e 6964 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
6965 {
6966 best_lowpc = lowpc;
6967 best_highpc = highpc;
6968 }
6969 }
4ae976d1 6970 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
6971 best_lowpc + baseaddr)
6972 - baseaddr);
4ae976d1 6973 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
6974 best_highpc + baseaddr)
6975 - baseaddr);
0018ea6f 6976
ae7754b2 6977 pst->end ();
0018ea6f 6978
ae640021 6979 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
6980 {
6981 int i;
ae640021 6982 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
6983
6984 /* Fill in 'dependencies' here; we fill in 'users' in a
6985 post-pass. */
6986 pst->number_of_dependencies = len;
a9342b62 6987 pst->dependencies
84685904 6988 = per_bfd->partial_symtabs->allocate_dependencies (len);
ae640021
AB
6989 for (i = 0; i < len; ++i)
6990 {
6991 pst->dependencies[i]
6992 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
6993 }
0018ea6f 6994
ae640021 6995 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
6996 }
6997
6998 /* Get the list of files included in the current compilation unit,
6999 and build a psymtab for each of them. */
7000 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7001
6f738b01
SM
7002 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7003 ", %d global, %d static syms",
7004 per_cu->is_debug_types ? "type" : "comp",
7005 sect_offset_str (per_cu->sect_off),
7006 paddress (gdbarch, pst->text_low (objfile)),
7007 paddress (gdbarch, pst->text_high (objfile)),
7008 (int) pst->global_psymbols.size (),
7009 (int) pst->static_psymbols.size ());
0018ea6f
DE
7010}
7011
7012/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7013 Process compilation unit THIS_CU for a psymtab. */
7014
7015static void
ab432490
SM
7016process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7017 dwarf2_per_objfile *per_objfile,
135f5437 7018 bool want_partial_unit,
b93601f3 7019 enum language pretend_language)
0018ea6f
DE
7020{
7021 /* If this compilation unit was already read in, free the
7022 cached copy in order to read it in again. This is
7023 necessary because we skipped some symbols when we first
7024 read in the compilation unit (see load_partial_dies).
7025 This problem could be avoided, but the benefit is unclear. */
7188ed02 7026 per_objfile->remove_cu (this_cu);
0018ea6f 7027
2e671100 7028 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7029
58990295
TV
7030 switch (reader.comp_unit_die->tag)
7031 {
7032 case DW_TAG_compile_unit:
7033 this_cu->unit_type = DW_UT_compile;
7034 break;
7035 case DW_TAG_partial_unit:
7036 this_cu->unit_type = DW_UT_partial;
7037 break;
e77b0004
TV
7038 case DW_TAG_type_unit:
7039 this_cu->unit_type = DW_UT_type;
7040 break;
58990295 7041 default:
702cf3f5
AB
7042 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7043 dwarf_tag_name (reader.comp_unit_die->tag),
7044 sect_offset_str (reader.cu->per_cu->sect_off),
7045 objfile_name (per_objfile->objfile));
58990295
TV
7046 }
7047
c0ab21c2 7048 if (reader.dummy_p)
f1902523 7049 {
c0ab21c2 7050 /* Nothing. */
f1902523 7051 }
c0ab21c2 7052 else if (this_cu->is_debug_types)
3e225074
TT
7053 build_type_psymtabs_reader (&reader, reader.info_ptr,
7054 reader.comp_unit_die);
135f5437
TT
7055 else if (want_partial_unit
7056 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7057 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7058 reader.comp_unit_die,
c0ab21c2 7059 pretend_language);
0018ea6f 7060
7188ed02 7061 this_cu->lang = reader.cu->language;
58990295 7062
0018ea6f 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 struct signatured_type find_entry, *entry;
7281
7282 /* If this TU doesn't exist in the global table, add it and read it in. */
7283
976ca316
SM
7284 if (per_objfile->per_bfd->signatured_types == NULL)
7285 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7286
7287 find_entry.signature = dwo_unit->signature;
976ca316 7288 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7289 &find_entry, INSERT);
6aa5f3a6
DE
7290 /* If we've already seen this type there's nothing to do. What's happening
7291 is we're doing our own version of comdat-folding here. */
7292 if (*slot != NULL)
7293 return 1;
7294
b8efb248 7295 /* This does the job that create_all_comp_units would have done for
6aa5f3a6 7296 this TU. */
976ca316
SM
7297 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
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
976ca316 7491 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b8efb248 7492 signatured_type *sig_ptr = sig_type.get ();
f1902523
JK
7493 sig_type->signature = cu_header.signature;
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
JK
7506 }
7507 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7508 this_cu->sect_off = sect_off;
f1902523 7509 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7510 this_cu->is_dwz = is_dwz;
8a0459fd 7511 this_cu->section = section;
ae038cb0 7512
ae038cb0 7513 info_ptr = info_ptr + this_cu->length;
0d305d5c 7514 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
ae038cb0 7515 }
36586728
TT
7516}
7517
7518/* Create a list of all compilation units in OBJFILE.
7519 This is only done for -readnow and building partial symtabs. */
7520
7521static void
976ca316 7522create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 7523{
b8efb248
TT
7524 htab_up types_htab;
7525
976ca316 7526 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
b8efb248
TT
7527 &per_objfile->per_bfd->abbrev, 0,
7528 types_htab, rcuh_kind::COMPILE);
7529 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7530 read_comp_units_from_section (per_objfile, &section,
7531 &per_objfile->per_bfd->abbrev, 0,
7532 types_htab, rcuh_kind::TYPE);
36586728 7533
976ca316 7534 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 7535 if (dwz != NULL)
b8efb248
TT
7536 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7537 types_htab, rcuh_kind::COMPILE);
7538
7539 per_objfile->per_bfd->signatured_types = std::move (types_htab);
c906108c
SS
7540}
7541
5734ee8b 7542/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7543 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7544 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7545 DW_AT_ranges). See the comments of add_partial_subprogram on how
7546 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7547
72bf9492
DJ
7548static void
7549scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7550 CORE_ADDR *highpc, int set_addrmap,
7551 struct dwarf2_cu *cu)
c906108c 7552{
72bf9492 7553 struct partial_die_info *pdi;
c906108c 7554
91c24f0a
DC
7555 /* Now, march along the PDI's, descending into ones which have
7556 interesting children but skipping the children of the other ones,
7557 until we reach the end of the compilation unit. */
c906108c 7558
72bf9492 7559 pdi = first_die;
91c24f0a 7560
72bf9492
DJ
7561 while (pdi != NULL)
7562 {
52356b79 7563 pdi->fixup (cu);
c906108c 7564
f55ee35c 7565 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7566 children, so we need to look at them. Ditto for anonymous
7567 enums. */
933c6fe4 7568
7d00ffec 7569 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7570 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7571 || pdi->tag == DW_TAG_imported_unit
7572 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7573 {
72bf9492 7574 switch (pdi->tag)
c906108c
SS
7575 {
7576 case DW_TAG_subprogram:
b1dc1806 7577 case DW_TAG_inlined_subroutine:
cdc07690 7578 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
7579 if (cu->language == language_cplus)
7580 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7581 set_addrmap, cu);
c906108c 7582 break;
72929c62 7583 case DW_TAG_constant:
c906108c
SS
7584 case DW_TAG_variable:
7585 case DW_TAG_typedef:
91c24f0a 7586 case DW_TAG_union_type:
317d2668
TV
7587 if (!pdi->is_declaration
7588 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 7589 {
72bf9492 7590 add_partial_symbol (pdi, cu);
63d06c5c
DC
7591 }
7592 break;
c906108c 7593 case DW_TAG_class_type:
680b30c7 7594 case DW_TAG_interface_type:
c906108c 7595 case DW_TAG_structure_type:
72bf9492 7596 if (!pdi->is_declaration)
c906108c 7597 {
72bf9492 7598 add_partial_symbol (pdi, cu);
c906108c 7599 }
b7fee5a3
KS
7600 if ((cu->language == language_rust
7601 || cu->language == language_cplus) && 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
SM
7639 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7640 cu->language);
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. */
7715 if (cu->language == 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
AB
7735 || parent->tag == DW_TAG_enumeration_type
7736 || (cu->language == language_fortran
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));
7826 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 7827 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
7828
7829 /* The code below indicates that the psymbol should be installed by
7830 setting this. */
7831 gdb::optional<psymbol_placement> where;
7832
c906108c
SS
7833 switch (pdi->tag)
7834 {
b1dc1806 7835 case DW_TAG_inlined_subroutine:
c906108c 7836 case DW_TAG_subprogram:
79748972
TT
7837 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
7838 - baseaddr);
0a4b0913
AB
7839 if (pdi->is_external
7840 || cu->language == language_ada
7841 || (cu->language == language_fortran
7842 && pdi->die_parent != NULL
7843 && pdi->die_parent->tag == DW_TAG_subprogram))
7844 {
dda83cd7
SM
7845 /* Normally, only "external" DIEs are part of the global scope.
7846 But in Ada and Fortran, we want to be able to access nested
7847 procedures globally. So all Ada and Fortran subprograms are
7848 stored in the global scope. */
76e288d1 7849 where = psymbol_placement::GLOBAL;
c906108c
SS
7850 }
7851 else
76e288d1
TT
7852 where = psymbol_placement::STATIC;
7853
7854 psymbol.domain = VAR_DOMAIN;
7855 psymbol.aclass = LOC_BLOCK;
a52d653e 7856 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 7857 psymbol.ginfo.value.address = addr;
0c1b455e
TT
7858
7859 if (pdi->main_subprogram && actual_name != NULL)
7860 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 7861 break;
72929c62 7862 case DW_TAG_constant:
76e288d1
TT
7863 psymbol.domain = VAR_DOMAIN;
7864 psymbol.aclass = LOC_STATIC;
7865 where = (pdi->is_external
7866 ? psymbol_placement::GLOBAL
7867 : psymbol_placement::STATIC);
72929c62 7868 break;
c906108c 7869 case DW_TAG_variable:
95554aad
TT
7870 if (pdi->d.locdesc)
7871 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 7872
95554aad 7873 if (pdi->d.locdesc
caac4577 7874 && addr == 0
976ca316 7875 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
7876 {
7877 /* A global or static variable may also have been stripped
7878 out by the linker if unused, in which case its address
7879 will be nullified; do not add such variables into partial
7880 symbol table then. */
7881 }
7882 else if (pdi->is_external)
c906108c
SS
7883 {
7884 /* Global Variable.
7885 Don't enter into the minimal symbol tables as there is
7886 a minimal symbol table entry from the ELF symbols already.
7887 Enter into partial symbol table if it has a location
7888 descriptor or a type.
7889 If the location descriptor is missing, new_symbol will create
7890 a LOC_UNRESOLVED symbol, the address of the variable will then
7891 be determined from the minimal symbol table whenever the variable
7892 is referenced.
7893 The address for the partial symbol table entry is not
7894 used by GDB, but it comes in handy for debugging partial symbol
7895 table building. */
7896
95554aad 7897 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
7898 {
7899 psymbol.domain = VAR_DOMAIN;
7900 psymbol.aclass = LOC_STATIC;
a52d653e 7901 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
7902 psymbol.ginfo.value.address = addr;
7903 where = psymbol_placement::GLOBAL;
7904 }
c906108c
SS
7905 }
7906 else
7907 {
ff908ebf
AW
7908 int has_loc = pdi->d.locdesc != NULL;
7909
7910 /* Static Variable. Skip symbols whose value we cannot know (those
7911 without location descriptors or constant values). */
7912 if (!has_loc && !pdi->has_const_value)
43816ebc 7913 return;
ff908ebf 7914
76e288d1
TT
7915 psymbol.domain = VAR_DOMAIN;
7916 psymbol.aclass = LOC_STATIC;
a52d653e 7917 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
7918 if (has_loc)
7919 psymbol.ginfo.value.address = addr;
7920 where = psymbol_placement::STATIC;
c906108c
SS
7921 }
7922 break;
d8f62e84 7923 case DW_TAG_array_type:
c906108c
SS
7924 case DW_TAG_typedef:
7925 case DW_TAG_base_type:
a02abb62 7926 case DW_TAG_subrange_type:
76e288d1
TT
7927 psymbol.domain = VAR_DOMAIN;
7928 psymbol.aclass = LOC_TYPEDEF;
7929 where = psymbol_placement::STATIC;
c906108c 7930 break;
74921315 7931 case DW_TAG_imported_declaration:
72bf9492 7932 case DW_TAG_namespace:
76e288d1
TT
7933 psymbol.domain = VAR_DOMAIN;
7934 psymbol.aclass = LOC_TYPEDEF;
7935 where = psymbol_placement::GLOBAL;
72bf9492 7936 break;
530e8392 7937 case DW_TAG_module:
a5fd13a9 7938 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
7939 available without any name. If so, we skip the module as it
7940 doesn't bring any value. */
a5fd13a9 7941 if (actual_name != nullptr)
76e288d1
TT
7942 {
7943 psymbol.domain = MODULE_DOMAIN;
7944 psymbol.aclass = LOC_TYPEDEF;
7945 where = psymbol_placement::GLOBAL;
7946 }
530e8392 7947 break;
c906108c 7948 case DW_TAG_class_type:
680b30c7 7949 case DW_TAG_interface_type:
c906108c
SS
7950 case DW_TAG_structure_type:
7951 case DW_TAG_union_type:
7952 case DW_TAG_enumeration_type:
fa4028e9 7953 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
7954 about "Structure, Union, and Class Type Entries": "An incomplete
7955 structure, union or class type is represented by a structure,
7956 union or class entry that does not have a byte size attribute
7957 and that has a DW_AT_declaration attribute." */
fa4028e9 7958 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 7959 return;
fa4028e9 7960
63d06c5c
DC
7961 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7962 static vs. global. */
76e288d1
TT
7963 psymbol.domain = STRUCT_DOMAIN;
7964 psymbol.aclass = LOC_TYPEDEF;
7965 where = (cu->language == language_cplus
7966 ? psymbol_placement::GLOBAL
7967 : psymbol_placement::STATIC);
c906108c
SS
7968 break;
7969 case DW_TAG_enumerator:
76e288d1
TT
7970 psymbol.domain = VAR_DOMAIN;
7971 psymbol.aclass = LOC_CONST;
7972 where = (cu->language == language_cplus
7973 ? psymbol_placement::GLOBAL
7974 : psymbol_placement::STATIC);
c906108c
SS
7975 break;
7976 default:
7977 break;
7978 }
76e288d1
TT
7979
7980 if (where.has_value ())
7981 {
f049a313
TT
7982 if (built_actual_name != nullptr)
7983 actual_name = objfile->intern (actual_name);
bcfe6157
TT
7984 if (pdi->linkage_name == nullptr || cu->language == language_ada)
7985 psymbol.ginfo.set_linkage_name (actual_name);
7986 else
7987 {
7988 psymbol.ginfo.set_demangled_name (actual_name,
7989 &objfile->objfile_obstack);
7990 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
7991 }
7b249e47
TT
7992 cu->per_cu->v.psymtab->add_psymbol
7993 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
7994 objfile);
76e288d1 7995 }
c906108c
SS
7996}
7997
5c4e30ca
DC
7998/* Read a partial die corresponding to a namespace; also, add a symbol
7999 corresponding to that namespace to the symbol table. NAMESPACE is
8000 the name of the enclosing namespace. */
91c24f0a 8001
72bf9492
DJ
8002static void
8003add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8004 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8005 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8006{
72bf9492 8007 /* Add a symbol for the namespace. */
e7c27a73 8008
72bf9492 8009 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8010
8011 /* Now scan partial symbols in that namespace. */
8012
91c24f0a 8013 if (pdi->has_children)
cdc07690 8014 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8015}
8016
5d7cb8df
JK
8017/* Read a partial die corresponding to a Fortran module. */
8018
8019static void
8020add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8021 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8022{
530e8392
KB
8023 /* Add a symbol for the namespace. */
8024
8025 add_partial_symbol (pdi, cu);
8026
f55ee35c 8027 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8028
8029 if (pdi->has_children)
cdc07690 8030 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8031}
8032
b1dc1806
XR
8033/* Read a partial die corresponding to a subprogram or an inlined
8034 subprogram and create a partial symbol for that subprogram.
8035 When the CU language allows it, this routine also defines a partial
8036 symbol for each nested subprogram that this subprogram contains.
8037 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8038 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8039
cdc07690
YQ
8040 PDI may also be a lexical block, in which case we simply search
8041 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8042 Again, this is only performed when the CU language allows this
8043 type of definitions. */
8044
8045static void
8046add_partial_subprogram (struct partial_die_info *pdi,
8047 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8048 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8049{
b1dc1806 8050 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8051 {
8052 if (pdi->has_pc_info)
dda83cd7
SM
8053 {
8054 if (pdi->lowpc < *lowpc)
8055 *lowpc = pdi->lowpc;
8056 if (pdi->highpc > *highpc)
8057 *highpc = pdi->highpc;
cdc07690 8058 if (set_addrmap)
5734ee8b 8059 {
5e22e966 8060 struct objfile *objfile = cu->per_objfile->objfile;
84685904 8061 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 8062 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8063 CORE_ADDR baseaddr;
b926417a
TT
8064 CORE_ADDR this_highpc;
8065 CORE_ADDR this_lowpc;
5734ee8b 8066
b3b3bada 8067 baseaddr = objfile->text_section_offset ();
b926417a
TT
8068 this_lowpc
8069 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8070 pdi->lowpc + baseaddr)
8071 - baseaddr);
8072 this_highpc
8073 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8074 pdi->highpc + baseaddr)
8075 - baseaddr);
84685904 8076 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
b926417a 8077 this_lowpc, this_highpc - 1,
9291a0cd 8078 cu->per_cu->v.psymtab);
5734ee8b 8079 }
dda83cd7 8080 }
481860b3
GB
8081
8082 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8083 {
dda83cd7 8084 if (!pdi->is_declaration)
e8d05480
JB
8085 /* Ignore subprogram DIEs that do not have a name, they are
8086 illegal. Do not emit a complaint at this point, we will
8087 do so when we convert this psymtab into a symtab. */
7d00ffec 8088 if (pdi->name (cu))
e8d05480 8089 add_partial_symbol (pdi, cu);
dda83cd7 8090 }
bc30ff58 8091 }
6e70227d 8092
bc30ff58
JB
8093 if (! pdi->has_children)
8094 return;
8095
0a4b0913 8096 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8097 {
8098 pdi = pdi->die_child;
8099 while (pdi != NULL)
8100 {
52356b79 8101 pdi->fixup (cu);
bc30ff58 8102 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8103 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8104 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8105 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8106 pdi = pdi->die_sibling;
8107 }
8108 }
8109}
8110
91c24f0a
DC
8111/* Read a partial die corresponding to an enumeration type. */
8112
72bf9492
DJ
8113static void
8114add_partial_enumeration (struct partial_die_info *enum_pdi,
8115 struct dwarf2_cu *cu)
91c24f0a 8116{
72bf9492 8117 struct partial_die_info *pdi;
91c24f0a 8118
7d00ffec 8119 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8120 add_partial_symbol (enum_pdi, cu);
8121
8122 pdi = enum_pdi->die_child;
8123 while (pdi)
91c24f0a 8124 {
7d00ffec 8125 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8126 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8127 else
72bf9492
DJ
8128 add_partial_symbol (pdi, cu);
8129 pdi = pdi->die_sibling;
91c24f0a 8130 }
91c24f0a
DC
8131}
8132
6caca83c
CC
8133/* Return the initial uleb128 in the die at INFO_PTR. */
8134
8135static unsigned int
d521ce57 8136peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8137{
8138 unsigned int bytes_read;
8139
8140 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8141}
8142
685af9cd
TT
8143/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8144 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8145
4bb7a0a7
DJ
8146 Return the corresponding abbrev, or NULL if the number is zero (indicating
8147 an empty DIE). In either case *BYTES_READ will be set to the length of
8148 the initial number. */
8149
7c32eebb 8150static const struct abbrev_info *
685af9cd
TT
8151peek_die_abbrev (const die_reader_specs &reader,
8152 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8153{
685af9cd 8154 dwarf2_cu *cu = reader.cu;
0280fdcc 8155 bfd *abfd = reader.abfd;
685af9cd
TT
8156 unsigned int abbrev_number
8157 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8158
8159 if (abbrev_number == 0)
8160 return NULL;
8161
7c32eebb
TT
8162 const abbrev_info *abbrev
8163 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8164 if (!abbrev)
8165 {
422b9917 8166 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8167 " at offset %s [in module %s]"),
422b9917 8168 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8169 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8170 }
8171
8172 return abbrev;
8173}
8174
93311388
DE
8175/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8176 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8177 DIE. Any children of the skipped DIEs will also be skipped. */
8178
d521ce57
TT
8179static const gdb_byte *
8180skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8181{
4bb7a0a7
DJ
8182 while (1)
8183 {
685af9cd 8184 unsigned int bytes_read;
7c32eebb
TT
8185 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8186 &bytes_read);
685af9cd 8187
4bb7a0a7
DJ
8188 if (abbrev == NULL)
8189 return info_ptr + bytes_read;
8190 else
dee91e82 8191 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8192 }
8193}
8194
93311388
DE
8195/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8196 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8197 abbrev corresponding to that skipped uleb128 should be passed in
8198 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8199 children. */
8200
d521ce57
TT
8201static const gdb_byte *
8202skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8203 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8204{
8205 unsigned int bytes_read;
8206 struct attribute attr;
dee91e82
DE
8207 bfd *abfd = reader->abfd;
8208 struct dwarf2_cu *cu = reader->cu;
d521ce57 8209 const gdb_byte *buffer = reader->buffer;
f664829e 8210 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8211 unsigned int form, i;
8212
8213 for (i = 0; i < abbrev->num_attrs; i++)
8214 {
8215 /* The only abbrev we care about is DW_AT_sibling. */
8216 if (abbrev->attrs[i].name == DW_AT_sibling)
8217 {
7a5f294d 8218 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8219 if (attr.form == DW_FORM_ref_addr)
b98664d3 8220 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8221 else
b9502d3f 8222 {
0826b30a 8223 sect_offset off = attr.get_ref_die_offset ();
9c541725 8224 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8225
8226 if (sibling_ptr < info_ptr)
b98664d3 8227 complaint (_("DW_AT_sibling points backwards"));
22869d73 8228 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8229 reader->die_section->overflow_complaint ();
b9502d3f
WN
8230 else
8231 return sibling_ptr;
8232 }
4bb7a0a7
DJ
8233 }
8234
8235 /* If it isn't DW_AT_sibling, skip this attribute. */
8236 form = abbrev->attrs[i].form;
8237 skip_attribute:
8238 switch (form)
8239 {
4bb7a0a7 8240 case DW_FORM_ref_addr:
ae411497
TT
8241 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8242 and later it is offset sized. */
8243 if (cu->header.version == 2)
8244 info_ptr += cu->header.addr_size;
8245 else
8246 info_ptr += cu->header.offset_size;
8247 break;
36586728
TT
8248 case DW_FORM_GNU_ref_alt:
8249 info_ptr += cu->header.offset_size;
8250 break;
ae411497 8251 case DW_FORM_addr:
4bb7a0a7
DJ
8252 info_ptr += cu->header.addr_size;
8253 break;
8254 case DW_FORM_data1:
8255 case DW_FORM_ref1:
8256 case DW_FORM_flag:
8fe0f950 8257 case DW_FORM_strx1:
4bb7a0a7
DJ
8258 info_ptr += 1;
8259 break;
2dc7f7b3 8260 case DW_FORM_flag_present:
43988095 8261 case DW_FORM_implicit_const:
2dc7f7b3 8262 break;
4bb7a0a7
DJ
8263 case DW_FORM_data2:
8264 case DW_FORM_ref2:
8fe0f950 8265 case DW_FORM_strx2:
4bb7a0a7
DJ
8266 info_ptr += 2;
8267 break;
8fe0f950
AT
8268 case DW_FORM_strx3:
8269 info_ptr += 3;
8270 break;
4bb7a0a7
DJ
8271 case DW_FORM_data4:
8272 case DW_FORM_ref4:
8fe0f950 8273 case DW_FORM_strx4:
4bb7a0a7
DJ
8274 info_ptr += 4;
8275 break;
8276 case DW_FORM_data8:
8277 case DW_FORM_ref8:
55f1336d 8278 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8279 info_ptr += 8;
8280 break;
0224619f
JK
8281 case DW_FORM_data16:
8282 info_ptr += 16;
8283 break;
4bb7a0a7 8284 case DW_FORM_string:
9b1c24c8 8285 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8286 info_ptr += bytes_read;
8287 break;
2dc7f7b3 8288 case DW_FORM_sec_offset:
4bb7a0a7 8289 case DW_FORM_strp:
36586728 8290 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8291 info_ptr += cu->header.offset_size;
8292 break;
2dc7f7b3 8293 case DW_FORM_exprloc:
4bb7a0a7
DJ
8294 case DW_FORM_block:
8295 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8296 info_ptr += bytes_read;
8297 break;
8298 case DW_FORM_block1:
8299 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8300 break;
8301 case DW_FORM_block2:
8302 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8303 break;
8304 case DW_FORM_block4:
8305 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8306 break;
336d760d 8307 case DW_FORM_addrx:
cf532bd1 8308 case DW_FORM_strx:
4bb7a0a7
DJ
8309 case DW_FORM_sdata:
8310 case DW_FORM_udata:
8311 case DW_FORM_ref_udata:
3019eac3
DE
8312 case DW_FORM_GNU_addr_index:
8313 case DW_FORM_GNU_str_index:
18a8505e 8314 case DW_FORM_rnglistx:
41144253 8315 case DW_FORM_loclistx:
d521ce57 8316 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8317 break;
8318 case DW_FORM_indirect:
8319 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8320 info_ptr += bytes_read;
8321 /* We need to continue parsing from here, so just go back to
8322 the top. */
8323 goto skip_attribute;
8324
8325 default:
3e43a32a
MS
8326 error (_("Dwarf Error: Cannot handle %s "
8327 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8328 dwarf_form_name (form),
8329 bfd_get_filename (abfd));
8330 }
8331 }
8332
8333 if (abbrev->has_children)
dee91e82 8334 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8335 else
8336 return info_ptr;
8337}
8338
93311388 8339/* Locate ORIG_PDI's sibling.
dee91e82 8340 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8341
d521ce57 8342static const gdb_byte *
dee91e82
DE
8343locate_pdi_sibling (const struct die_reader_specs *reader,
8344 struct partial_die_info *orig_pdi,
d521ce57 8345 const gdb_byte *info_ptr)
91c24f0a
DC
8346{
8347 /* Do we know the sibling already? */
72bf9492 8348
91c24f0a
DC
8349 if (orig_pdi->sibling)
8350 return orig_pdi->sibling;
8351
8352 /* Are there any children to deal with? */
8353
8354 if (!orig_pdi->has_children)
8355 return info_ptr;
8356
4bb7a0a7 8357 /* Skip the children the long way. */
91c24f0a 8358
dee91e82 8359 return skip_children (reader, info_ptr);
91c24f0a
DC
8360}
8361
257e7a09 8362/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8363 not NULL. */
c906108c 8364
891813be
TT
8365void
8366dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8367{
976ca316 8368 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8369
976ca316 8370 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8371
077cbab2
TT
8372 /* If this psymtab is constructed from a debug-only objfile, the
8373 has_section_at_zero flag will not necessarily be correct. We
8374 can get the correct value for this flag by looking at the data
8375 associated with the (presumably stripped) associated objfile. */
8376 if (objfile->separate_debug_objfile_backlink)
c906108c 8377 {
976ca316 8378 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8379 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8380
976ca316
SM
8381 per_objfile->per_bfd->has_section_at_zero
8382 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8383 }
98bfdba5 8384
8566b89b 8385 expand_psymtab (objfile);
95554aad 8386
976ca316 8387 process_cu_includes (per_objfile);
c906108c 8388}
9cdd5dbd
DE
8389\f
8390/* Reading in full CUs. */
c906108c 8391
10b3939b
DJ
8392/* Add PER_CU to the queue. */
8393
8394static void
120ce1b5
SM
8395queue_comp_unit (dwarf2_per_cu_data *per_cu,
8396 dwarf2_per_objfile *per_objfile,
95554aad 8397 enum language pretend_language)
10b3939b 8398{
10b3939b 8399 per_cu->queued = 1;
08ac5771
SM
8400
8401 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8402 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8403}
8404
616c069a
SM
8405/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8406
89e63ee4
DE
8407 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8408 dependency.
69d751e3 8409
616c069a
SM
8410 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8411 DIEs, false otherwise.
8412
8413 Explanation: there is an invariant that if a CU is queued for expansion
8414 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8415 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8416 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8417 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8418 invariant is respected.
8419
8420 The caller is therefore not required to load the CU's DIEs (we return false)
8421 if:
8422
8423 - the CU is already expanded, and therefore does not get enqueued
8424 - the CU gets enqueued for expansion, but its DIEs are already loaded
8425
8426 Note that the caller should not use this function's return value as an
8427 indicator of whether the CU's DIEs are loaded right now, it should check
8428 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
8429
8430static int
89e63ee4 8431maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8432 dwarf2_per_cu_data *per_cu,
8433 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8434 enum language pretend_language)
8435{
8436 /* We may arrive here during partial symbol reading, if we need full
8437 DIEs to process an unusual case (e.g. template arguments). Do
8438 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8439 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 8440 {
7188ed02
SM
8441 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8442
8443 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
8444 return 1;
8445 return 0;
8446 }
8447
8448 /* Mark the dependence relation so that we don't flush PER_CU
8449 too early. */
89e63ee4 8450 if (dependent_cu != NULL)
347212b8 8451 dependent_cu->add_dependence (per_cu);
0907af0c
DE
8452
8453 /* If it's already on the queue, we have nothing to do. */
8454 if (per_cu->queued)
de53369b
SM
8455 {
8456 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8457 loaded. */
8458 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
8459
8460 /* If the CU is queued for expansion, it should not already be
8461 expanded. */
8462 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8463
8464 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
8465 return 0;
8466 }
0907af0c 8467
616c069a
SM
8468 bool queued = false;
8469 if (!per_objfile->symtab_set_p (per_cu))
8470 {
8471 /* Add it to the queue. */
8472 queue_comp_unit (per_cu, per_objfile, pretend_language);
8473 queued = true;
8474 }
8475
0907af0c
DE
8476 /* If the compilation unit is already loaded, just mark it as
8477 used. */
7188ed02
SM
8478 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8479 if (cu != nullptr)
616c069a 8480 cu->last_used = 0;
0907af0c 8481
616c069a
SM
8482 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8483 and the DIEs are not already loaded. */
8484 return queued && cu == nullptr;
0907af0c
DE
8485}
8486
10b3939b
DJ
8487/* Process the queue. */
8488
8489static void
976ca316 8490process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 8491{
6f738b01
SM
8492 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8493 objfile_name (per_objfile->objfile));
45cfd468 8494
03dd20cc
DJ
8495 /* The queue starts out with one item, but following a DIE reference
8496 may load a new CU, adding it to the end of the queue. */
08ac5771 8497 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 8498 {
08ac5771 8499 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 8500 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 8501
976ca316 8502 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 8503 {
976ca316 8504 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 8505
7188ed02
SM
8506 /* Skip dummy CUs. */
8507 if (cu != nullptr)
73be47f5 8508 {
7188ed02
SM
8509 unsigned int debug_print_threshold;
8510 char buf[100];
8511
8512 if (per_cu->is_debug_types)
8513 {
8514 struct signatured_type *sig_type =
8515 (struct signatured_type *) per_cu;
8516
8517 sprintf (buf, "TU %s at offset %s",
8518 hex_string (sig_type->signature),
8519 sect_offset_str (per_cu->sect_off));
8520 /* There can be 100s of TUs.
8521 Only print them in verbose mode. */
8522 debug_print_threshold = 2;
8523 }
8524 else
8525 {
8526 sprintf (buf, "CU at offset %s",
8527 sect_offset_str (per_cu->sect_off));
8528 debug_print_threshold = 1;
8529 }
247f5c4f 8530
7188ed02 8531 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8532 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 8533
7188ed02
SM
8534 if (per_cu->is_debug_types)
8535 process_full_type_unit (cu, item.pretend_language);
8536 else
8537 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 8538
7188ed02 8539 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8540 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 8541 }
f4dc4d17 8542 }
10b3939b 8543
7188ed02 8544 per_cu->queued = 0;
08ac5771 8545 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
8546 }
8547
6f738b01
SM
8548 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8549 objfile_name (per_objfile->objfile));
10b3939b
DJ
8550}
8551
10b3939b
DJ
8552/* Read in full symbols for PST, and anything it depends on. */
8553
8566b89b
TT
8554void
8555dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8556{
af758d11 8557 gdb_assert (!readin_p (objfile));
95554aad 8558
17ee85fc
TT
8559 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8560 free_cached_comp_units freer (per_objfile);
48993951 8561 expand_dependencies (objfile);
aaa75496 8562
97a1449a 8563 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 8564 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
8565}
8566
af758d11
SM
8567/* See psympriv.h. */
8568
8569bool
8570dwarf2_psymtab::readin_p (struct objfile *objfile) const
8571{
8572 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8573 return per_objfile->symtab_set_p (per_cu_data);
8574}
8575
8576/* See psympriv.h. */
8577
8578compunit_symtab *
8579dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8580{
8581 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8582 return per_objfile->get_symtab (per_cu_data);
8583}
8584
dee91e82
DE
8585/* Trivial hash function for die_info: the hash value of a DIE
8586 is its offset in .debug_info for this objfile. */
10b3939b 8587
dee91e82
DE
8588static hashval_t
8589die_hash (const void *item)
10b3939b 8590{
9a3c8263 8591 const struct die_info *die = (const struct die_info *) item;
6502dd73 8592
9c541725 8593 return to_underlying (die->sect_off);
dee91e82 8594}
63d06c5c 8595
dee91e82
DE
8596/* Trivial comparison function for die_info structures: two DIEs
8597 are equal if they have the same offset. */
98bfdba5 8598
dee91e82
DE
8599static int
8600die_eq (const void *item_lhs, const void *item_rhs)
8601{
9a3c8263
SM
8602 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8603 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8604
9c541725 8605 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8606}
c906108c 8607
4a636814
SM
8608/* Load the DIEs associated with PER_CU into memory.
8609
8610 In some cases, the caller, while reading partial symbols, will need to load
8611 the full symbols for the CU for some reason. It will already have a
8612 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8613 rather than creating a new one. */
c906108c 8614
dee91e82 8615static void
ab432490
SM
8616load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8617 dwarf2_per_objfile *per_objfile,
4a636814 8618 dwarf2_cu *existing_cu,
c0ab21c2
TT
8619 bool skip_partial,
8620 enum language pretend_language)
dee91e82 8621{
c0ab21c2
TT
8622 gdb_assert (! this_cu->is_debug_types);
8623
7188ed02 8624 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
8625 if (reader.dummy_p)
8626 return;
8627
8628 struct dwarf2_cu *cu = reader.cu;
8629 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8630
dee91e82
DE
8631 gdb_assert (cu->die_hash == NULL);
8632 cu->die_hash =
8633 htab_create_alloc_ex (cu->header.length / 12,
8634 die_hash,
8635 die_eq,
8636 NULL,
8637 &cu->comp_unit_obstack,
8638 hashtab_obstack_allocate,
8639 dummy_obstack_deallocate);
e142c38c 8640
3e225074 8641 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8642 reader.comp_unit_die->child
8643 = read_die_and_siblings (&reader, reader.info_ptr,
8644 &info_ptr, reader.comp_unit_die);
8645 cu->dies = reader.comp_unit_die;
dee91e82 8646 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8647
8648 /* We try not to read any attributes in this function, because not
9cdd5dbd 8649 all CUs needed for references have been loaded yet, and symbol
10b3939b 8650 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8651 or we won't be able to build types correctly.
8652 Similarly, if we do not read the producer, we can not apply
8653 producer-specific interpretation. */
c0ab21c2 8654 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8655
8656 reader.keep ();
10b3939b
DJ
8657}
8658
3da10d80
KS
8659/* Add a DIE to the delayed physname list. */
8660
8661static void
8662add_to_method_list (struct type *type, int fnfield_index, int index,
8663 const char *name, struct die_info *die,
8664 struct dwarf2_cu *cu)
8665{
8666 struct delayed_method_info mi;
8667 mi.type = type;
8668 mi.fnfield_index = fnfield_index;
8669 mi.index = index;
8670 mi.name = name;
8671 mi.die = die;
c89b44cd 8672 cu->method_list.push_back (mi);
3da10d80
KS
8673}
8674
3693fdb3
PA
8675/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8676 "const" / "volatile". If so, decrements LEN by the length of the
8677 modifier and return true. Otherwise return false. */
8678
8679template<size_t N>
8680static bool
8681check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8682{
8683 size_t mod_len = sizeof (mod) - 1;
8684 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8685 {
8686 len -= mod_len;
8687 return true;
8688 }
8689 return false;
8690}
8691
3da10d80
KS
8692/* Compute the physnames of any methods on the CU's method list.
8693
8694 The computation of method physnames is delayed in order to avoid the
8695 (bad) condition that one of the method's formal parameters is of an as yet
8696 incomplete type. */
8697
8698static void
8699compute_delayed_physnames (struct dwarf2_cu *cu)
8700{
3693fdb3 8701 /* Only C++ delays computing physnames. */
c89b44cd 8702 if (cu->method_list.empty ())
3693fdb3
PA
8703 return;
8704 gdb_assert (cu->language == language_cplus);
8705
52941706 8706 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8707 {
1d06ead6 8708 const char *physname;
3da10d80 8709 struct fn_fieldlist *fn_flp
c89b44cd
TT
8710 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8711 physname = dwarf2_physname (mi.name, mi.die, cu);
8712 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8713 = physname ? physname : "";
3693fdb3
PA
8714
8715 /* Since there's no tag to indicate whether a method is a
8716 const/volatile overload, extract that information out of the
8717 demangled name. */
8718 if (physname != NULL)
8719 {
8720 size_t len = strlen (physname);
8721
8722 while (1)
8723 {
8724 if (physname[len] == ')') /* shortcut */
8725 break;
8726 else if (check_modifier (physname, len, " const"))
c89b44cd 8727 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 8728 else if (check_modifier (physname, len, " volatile"))
c89b44cd 8729 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
8730 else
8731 break;
8732 }
8733 }
3da10d80 8734 }
c89b44cd
TT
8735
8736 /* The list is no longer needed. */
8737 cu->method_list.clear ();
3da10d80
KS
8738}
8739
a766d390
DE
8740/* Go objects should be embedded in a DW_TAG_module DIE,
8741 and it's not clear if/how imported objects will appear.
8742 To keep Go support simple until that's worked out,
8743 go back through what we've read and create something usable.
8744 We could do this while processing each DIE, and feels kinda cleaner,
8745 but that way is more invasive.
8746 This is to, for example, allow the user to type "p var" or "b main"
8747 without having to specify the package name, and allow lookups
8748 of module.object to work in contexts that use the expression
8749 parser. */
8750
8751static void
8752fixup_go_packaging (struct dwarf2_cu *cu)
8753{
421d1616 8754 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
8755 struct pending *list;
8756 int i;
8757
c24bdb02 8758 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
8759 list != NULL;
8760 list = list->next)
a766d390
DE
8761 {
8762 for (i = 0; i < list->nsyms; ++i)
8763 {
8764 struct symbol *sym = list->symbol[i];
8765
c1b5c1eb 8766 if (sym->language () == language_go
a766d390
DE
8767 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8768 {
421d1616
TT
8769 gdb::unique_xmalloc_ptr<char> this_package_name
8770 (go_symbol_package_name (sym));
a766d390
DE
8771
8772 if (this_package_name == NULL)
8773 continue;
8774 if (package_name == NULL)
421d1616 8775 package_name = std::move (this_package_name);
a766d390
DE
8776 else
8777 {
5e22e966 8778 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 8779 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 8780 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
8781 (symbol_symtab (sym) != NULL
8782 ? symtab_to_filename_for_display
8783 (symbol_symtab (sym))
e3b94546 8784 : objfile_name (objfile)),
421d1616 8785 this_package_name.get (), package_name.get ());
a766d390
DE
8786 }
8787 }
8788 }
8789 }
8790
8791 if (package_name != NULL)
8792 {
5e22e966 8793 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 8794 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
8795 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8796 saved_package_name);
a766d390
DE
8797 struct symbol *sym;
8798
8c14c3a3 8799 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 8800 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 8801 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
8802 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8803 e.g., "main" finds the "main" module and not C's main(). */
8804 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 8805 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
8806 SYMBOL_TYPE (sym) = type;
8807
c24bdb02 8808 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
8809 }
8810}
8811
c9317f21
TT
8812/* Allocate a fully-qualified name consisting of the two parts on the
8813 obstack. */
8814
8815static const char *
8816rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
8817{
8818 return obconcat (obstack, p1, "::", p2, (char *) NULL);
8819}
8820
9c6a1327
TT
8821/* A helper that allocates a variant part to attach to a Rust enum
8822 type. OBSTACK is where the results should be allocated. TYPE is
8823 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
8824 discriminant. It must be the index of one of the fields of TYPE,
8825 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
8826 DEFAULT_INDEX is the index of the default field; or -1 if there is
8827 no default. RANGES is indexed by "effective" field number (the
8828 field index, but omitting the discriminant and default fields) and
8829 must hold the discriminant values used by the variants. Note that
8830 RANGES must have a lifetime at least as long as OBSTACK -- either
8831 already allocated on it, or static. */
c9317f21 8832
9c6a1327
TT
8833static void
8834alloc_rust_variant (struct obstack *obstack, struct type *type,
8835 int discriminant_index, int default_index,
8836 gdb::array_view<discriminant_range> ranges)
8837{
57d02173
TT
8838 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
8839 gdb_assert (discriminant_index == -1
8840 || (discriminant_index >= 0
8841 && discriminant_index < type->num_fields ()));
c9317f21 8842 gdb_assert (default_index == -1
1f704f76 8843 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 8844
9c6a1327 8845 /* We have one variant for each non-discriminant field. */
57d02173
TT
8846 int n_variants = type->num_fields ();
8847 if (discriminant_index != -1)
8848 --n_variants;
c9317f21 8849
9c6a1327
TT
8850 variant *variants = new (obstack) variant[n_variants];
8851 int var_idx = 0;
8852 int range_idx = 0;
1f704f76 8853 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
8854 {
8855 if (i == discriminant_index)
8856 continue;
c9317f21 8857
9c6a1327
TT
8858 variants[var_idx].first_field = i;
8859 variants[var_idx].last_field = i + 1;
8860
8861 /* The default field does not need a range, but other fields do.
8862 We skipped the discriminant above. */
8863 if (i != default_index)
8864 {
8865 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
8866 ++range_idx;
8867 }
c9317f21 8868
9c6a1327
TT
8869 ++var_idx;
8870 }
8871
8872 gdb_assert (range_idx == ranges.size ());
8873 gdb_assert (var_idx == n_variants);
8874
8875 variant_part *part = new (obstack) variant_part;
8876 part->discriminant_index = discriminant_index;
57d02173
TT
8877 /* If there is no discriminant, then whether it is signed is of no
8878 consequence. */
8879 part->is_unsigned
8880 = (discriminant_index == -1
8881 ? false
c6d940a9 8882 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
8883 part->variants = gdb::array_view<variant> (variants, n_variants);
8884
8885 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
8886 gdb::array_view<variant_part> *prop_value
8887 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 8888
9c6a1327 8889 struct dynamic_prop prop;
8c2e4e06 8890 prop.set_variant_parts (prop_value);
9c6a1327 8891
5c54719c 8892 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
8893}
8894
8895/* Some versions of rustc emitted enums in an unusual way.
8896
8897 Ordinary enums were emitted as unions. The first element of each
8898 structure in the union was named "RUST$ENUM$DISR". This element
8899 held the discriminant.
8900
8901 These versions of Rust also implemented the "non-zero"
8902 optimization. When the enum had two values, and one is empty and
8903 the other holds a pointer that cannot be zero, the pointer is used
8904 as the discriminant, with a zero value meaning the empty variant.
8905 Here, the union's first member is of the form
8906 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
8907 where the fieldnos are the indices of the fields that should be
8908 traversed in order to find the field (which may be several fields deep)
8909 and the variantname is the name of the variant of the case when the
8910 field is zero.
8911
8912 This function recognizes whether TYPE is of one of these forms,
8913 and, if so, smashes it to be a variant type. */
8914
8915static void
8916quirk_rust_enum (struct type *type, struct objfile *objfile)
8917{
78134374 8918 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
8919
8920 /* We don't need to deal with empty enums. */
1f704f76 8921 if (type->num_fields () == 0)
c9317f21
TT
8922 return;
8923
8924#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 8925 if (type->num_fields () == 1
c9317f21
TT
8926 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
8927 {
8928 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
8929
8930 /* Decode the field name to find the offset of the
8931 discriminant. */
8932 ULONGEST bit_offset = 0;
940da03e 8933 struct type *field_type = type->field (0).type ();
c9317f21
TT
8934 while (name[0] >= '0' && name[0] <= '9')
8935 {
8936 char *tail;
8937 unsigned long index = strtoul (name, &tail, 10);
8938 name = tail;
8939 if (*name != '$'
1f704f76 8940 || index >= field_type->num_fields ()
c9317f21
TT
8941 || (TYPE_FIELD_LOC_KIND (field_type, index)
8942 != FIELD_LOC_KIND_BITPOS))
8943 {
b98664d3 8944 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
8945 "[in module %s]"),
8946 TYPE_FIELD_NAME (type, 0),
8947 objfile_name (objfile));
8948 return;
8949 }
8950 ++name;
8951
8952 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 8953 field_type = field_type->field (index).type ();
c9317f21
TT
8954 }
8955
9c6a1327
TT
8956 /* Smash this type to be a structure type. We have to do this
8957 because the type has already been recorded. */
67607e24 8958 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 8959 type->set_num_fields (3);
9c6a1327 8960 /* Save the field we care about. */
ceacbf6e 8961 struct field saved_field = type->field (0);
3cabb6b0
SM
8962 type->set_fields
8963 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 8964
9c6a1327 8965 /* Put the discriminant at index 0. */
5d14b6e5 8966 type->field (0).set_type (field_type);
9c6a1327
TT
8967 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8968 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 8969 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
8970
8971 /* The order of fields doesn't really matter, so put the real
8972 field at index 1 and the data-less field at index 2. */
ceacbf6e 8973 type->field (1) = saved_field;
9c6a1327 8974 TYPE_FIELD_NAME (type, 1)
940da03e
SM
8975 = rust_last_path_segment (type->field (1).type ()->name ());
8976 type->field (1).type ()->set_name
7d93a1e0 8977 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 8978 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
8979
8980 const char *dataless_name
7d93a1e0 8981 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
8982 name);
8983 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8984 dataless_name);
5d14b6e5 8985 type->field (2).set_type (dataless_type);
c9317f21
TT
8986 /* NAME points into the original discriminant name, which
8987 already has the correct lifetime. */
9c6a1327 8988 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 8989 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 8990
9c6a1327
TT
8991 /* Indicate that this is a variant type. */
8992 static discriminant_range ranges[1] = { { 0, 0 } };
8993 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 8994 }
77c2dba3
TT
8995 /* A union with a single anonymous field is probably an old-style
8996 univariant enum. */
1f704f76 8997 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 8998 {
c9317f21
TT
8999 /* Smash this type to be a structure type. We have to do this
9000 because the type has already been recorded. */
67607e24 9001 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9002
940da03e 9003 struct type *field_type = type->field (0).type ();
c9317f21 9004 const char *variant_name
7d93a1e0 9005 = rust_last_path_segment (field_type->name ());
9c6a1327 9006 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9007 field_type->set_name
9008 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9009 type->name (), variant_name));
57d02173
TT
9010
9011 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9012 }
9013 else
9014 {
9015 struct type *disr_type = nullptr;
1f704f76 9016 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9017 {
940da03e 9018 disr_type = type->field (i).type ();
c9317f21 9019
78134374 9020 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9021 {
9022 /* All fields of a true enum will be structs. */
9023 return;
9024 }
1f704f76 9025 else if (disr_type->num_fields () == 0)
c9317f21
TT
9026 {
9027 /* Could be data-less variant, so keep going. */
a037790e 9028 disr_type = nullptr;
c9317f21
TT
9029 }
9030 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9031 "RUST$ENUM$DISR") != 0)
9032 {
9033 /* Not a Rust enum. */
9034 return;
9035 }
9036 else
9037 {
9038 /* Found one. */
9039 break;
9040 }
9041 }
9042
9043 /* If we got here without a discriminant, then it's probably
9044 just a union. */
9045 if (disr_type == nullptr)
9046 return;
9047
9048 /* Smash this type to be a structure type. We have to do this
9049 because the type has already been recorded. */
67607e24 9050 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9051
9c6a1327 9052 /* Make space for the discriminant field. */
ceacbf6e 9053 struct field *disr_field = &disr_type->field (0);
9c6a1327 9054 field *new_fields
1f704f76 9055 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9056 * sizeof (struct field)));
80fc5e77 9057 memcpy (new_fields + 1, type->fields (),
1f704f76 9058 type->num_fields () * sizeof (struct field));
3cabb6b0 9059 type->set_fields (new_fields);
1f704f76 9060 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9061
9062 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9063 type->field (0) = *disr_field;
9c6a1327
TT
9064 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9065 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9066
9067 /* We need a way to find the correct discriminant given a
9068 variant name. For convenience we build a map here. */
b6cdac4b 9069 struct type *enum_type = disr_field->type ();
c9317f21 9070 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9071 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9072 {
9073 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9074 {
9075 const char *name
9076 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9077 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9078 }
9079 }
9080
1f704f76 9081 int n_fields = type->num_fields ();
9c6a1327
TT
9082 /* We don't need a range entry for the discriminant, but we do
9083 need one for every other field, as there is no default
9084 variant. */
9085 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9086 discriminant_range,
9087 n_fields - 1);
c9317f21
TT
9088 /* Skip the discriminant here. */
9089 for (int i = 1; i < n_fields; ++i)
9090 {
9091 /* Find the final word in the name of this variant's type.
9092 That name can be used to look up the correct
9093 discriminant. */
9094 const char *variant_name
940da03e 9095 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9096
9097 auto iter = discriminant_map.find (variant_name);
9098 if (iter != discriminant_map.end ())
9c6a1327 9099 {
57d02173
TT
9100 ranges[i - 1].low = iter->second;
9101 ranges[i - 1].high = iter->second;
9c6a1327 9102 }
c9317f21 9103
57d02173
TT
9104 /* In Rust, each element should have the size of the
9105 enclosing enum. */
9106 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9107
bedda9ac 9108 /* Remove the discriminant field, if it exists. */
940da03e 9109 struct type *sub_type = type->field (i).type ();
1f704f76 9110 if (sub_type->num_fields () > 0)
bedda9ac 9111 {
5e33d5f4 9112 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9113 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9114 }
9c6a1327 9115 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9116 sub_type->set_name
9117 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9118 type->name (), variant_name));
c9317f21 9119 }
9c6a1327
TT
9120
9121 /* Indicate that this is a variant type. */
a1520ad8 9122 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9123 gdb::array_view<discriminant_range> (ranges,
9124 n_fields - 1));
c9317f21
TT
9125 }
9126}
9127
9128/* Rewrite some Rust unions to be structures with variants parts. */
9129
9130static void
9131rust_union_quirks (struct dwarf2_cu *cu)
9132{
9133 gdb_assert (cu->language == language_rust);
52941706 9134 for (type *type_ : cu->rust_unions)
5e22e966 9135 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9136 /* We don't need this any more. */
9137 cu->rust_unions.clear ();
c9317f21
TT
9138}
9139
8adb8487
TT
9140/* See read.h. */
9141
9142type_unit_group_unshareable *
9143dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9144{
9145 auto iter = this->m_type_units.find (tu_group);
9146 if (iter != this->m_type_units.end ())
9147 return iter->second.get ();
9148
9149 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9150 type_unit_group_unshareable *result = uniq.get ();
9151 this->m_type_units[tu_group] = std::move (uniq);
9152 return result;
9153}
9154
e286671b
TT
9155struct type *
9156dwarf2_per_objfile::get_type_for_signatured_type
9157 (signatured_type *sig_type) const
9158{
9159 auto iter = this->m_type_map.find (sig_type);
9160 if (iter == this->m_type_map.end ())
9161 return nullptr;
9162
9163 return iter->second;
9164}
9165
9166void dwarf2_per_objfile::set_type_for_signatured_type
9167 (signatured_type *sig_type, struct type *type)
9168{
9169 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9170
9171 this->m_type_map[sig_type] = type;
9172}
9173
95554aad
TT
9174/* A helper function for computing the list of all symbol tables
9175 included by PER_CU. */
9176
9177static void
4c39bc03 9178recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9179 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9180 dwarf2_per_cu_data *per_cu,
9181 dwarf2_per_objfile *per_objfile,
43f3e411 9182 struct compunit_symtab *immediate_parent)
95554aad 9183{
af758d11 9184 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9185 if (*slot != NULL)
9186 {
9187 /* This inclusion and its children have been processed. */
9188 return;
9189 }
9190
9191 *slot = per_cu;
af758d11 9192
95554aad 9193 /* Only add a CU if it has a symbol table. */
43182c09 9194 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9195 if (cust != NULL)
ec94af83
DE
9196 {
9197 /* If this is a type unit only add its symbol table if we haven't
9198 seen it yet (type unit per_cu's can share symtabs). */
9199 if (per_cu->is_debug_types)
9200 {
43f3e411 9201 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9202 if (*slot == NULL)
9203 {
43f3e411 9204 *slot = cust;
4c39bc03 9205 result->push_back (cust);
43f3e411
DE
9206 if (cust->user == NULL)
9207 cust->user = immediate_parent;
ec94af83
DE
9208 }
9209 }
9210 else
f9125b6c 9211 {
4c39bc03 9212 result->push_back (cust);
43f3e411
DE
9213 if (cust->user == NULL)
9214 cust->user = immediate_parent;
f9125b6c 9215 }
ec94af83 9216 }
95554aad 9217
ae640021
AB
9218 if (!per_cu->imported_symtabs_empty ())
9219 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9220 {
9221 recursively_compute_inclusions (result, all_children,
43182c09
SM
9222 all_type_symtabs, ptr, per_objfile,
9223 cust);
ae640021 9224 }
95554aad
TT
9225}
9226
43f3e411 9227/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9228 PER_CU. */
9229
9230static void
43182c09
SM
9231compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9232 dwarf2_per_objfile *per_objfile)
95554aad 9233{
f4dc4d17
DE
9234 gdb_assert (! per_cu->is_debug_types);
9235
ae640021 9236 if (!per_cu->imported_symtabs_empty ())
95554aad 9237 {
ae640021 9238 int len;
4c39bc03 9239 std::vector<compunit_symtab *> result_symtabs;
43182c09 9240 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9241
9242 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9243 if (cust == NULL)
95554aad
TT
9244 return;
9245
280a9412
TT
9246 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9247 htab_eq_pointer,
9248 NULL, xcalloc, xfree));
9249 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9250 htab_eq_pointer,
9251 NULL, xcalloc, xfree));
95554aad 9252
ae640021 9253 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9254 {
280a9412
TT
9255 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9256 all_type_symtabs.get (), ptr,
9257 per_objfile, cust);
ec94af83 9258 }
95554aad 9259
ec94af83 9260 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9261 len = result_symtabs.size ();
43f3e411 9262 cust->includes
f6e649dd 9263 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9264 struct compunit_symtab *, len + 1);
4c39bc03
TT
9265 memcpy (cust->includes, result_symtabs.data (),
9266 len * sizeof (compunit_symtab *));
43f3e411 9267 cust->includes[len] = NULL;
95554aad
TT
9268 }
9269}
9270
9271/* Compute the 'includes' field for the symtabs of all the CUs we just
9272 read. */
9273
9274static void
976ca316 9275process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9276{
976ca316 9277 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9278 {
9279 if (! iter->is_debug_types)
976ca316 9280 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9281 }
95554aad 9282
976ca316 9283 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9284}
9285
8fc0b21d 9286/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9287 already been loaded into memory. */
9288
9289static void
8fc0b21d 9290process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9291{
976ca316
SM
9292 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9293 struct objfile *objfile = per_objfile->objfile;
08feed99 9294 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9295 CORE_ADDR lowpc, highpc;
43f3e411 9296 struct compunit_symtab *cust;
10b3939b 9297 CORE_ADDR baseaddr;
4359dff1 9298 struct block *static_block;
3e29f34a 9299 CORE_ADDR addr;
10b3939b 9300
b3b3bada 9301 baseaddr = objfile->text_section_offset ();
10b3939b 9302
c89b44cd
TT
9303 /* Clear the list here in case something was left over. */
9304 cu->method_list.clear ();
10b3939b 9305
95554aad
TT
9306 cu->language = pretend_language;
9307 cu->language_defn = language_def (cu->language);
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
DE
9329 /* For now fudge the Go package. */
9330 if (cu->language == language_go)
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
c9317f21
TT
9338 if (cu->language == language_rust)
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. */
43f3e411 9367 if (!(cu->language == language_c
40e3ad0e 9368 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9369 COMPUNIT_FILETABS (cust)->language = cu->language;
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 cu->language = pretend_language;
9420 cu->language_defn = language_def (cu->language);
9421
9422 /* The symbol tables are set up in read_type_unit_scope. */
9423 process_die (cu->dies, cu);
9424
9425 /* For now fudge the Go package. */
9426 if (cu->language == language_go)
9427 fixup_go_packaging (cu);
9428
5f48f8f3 9429 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9430 should be complete, and it should now be safe to compute all of the
9431 physnames. */
9432 compute_delayed_physnames (cu);
f4dc4d17 9433
c9317f21
TT
9434 if (cu->language == language_rust)
9435 rust_union_quirks (cu);
9436
f4dc4d17
DE
9437 /* TUs share symbol tables.
9438 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9439 of it with end_expandable_symtab. Otherwise, complete the addition of
9440 this TU's symbols to the existing symtab. */
8adb8487 9441 type_unit_group_unshareable *tug_unshare =
976ca316 9442 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9443 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9444 {
c24bdb02
KS
9445 buildsym_compunit *builder = cu->get_builder ();
9446 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9447 tug_unshare->compunit_symtab = cust;
f4dc4d17 9448
43f3e411 9449 if (cust != NULL)
f4dc4d17
DE
9450 {
9451 /* Set symtab language to language from DW_AT_language. If the
9452 compilation is from a C file generated by language preprocessors,
9453 do not set the language if it was already deduced by
9454 start_subfile. */
43f3e411
DE
9455 if (!(cu->language == language_c
9456 && COMPUNIT_FILETABS (cust)->language != language_c))
9457 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9458 }
9459 }
9460 else
9461 {
c24bdb02 9462 cu->get_builder ()->augment_type_symtab ();
8adb8487 9463 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9464 }
9465
976ca316 9466 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9467
9468 /* Not needed any more. */
c24bdb02 9469 cu->reset_builder ();
c906108c
SS
9470}
9471
95554aad
TT
9472/* Process an imported unit DIE. */
9473
9474static void
9475process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9476{
9477 struct attribute *attr;
9478
f4dc4d17
DE
9479 /* For now we don't handle imported units in type units. */
9480 if (cu->per_cu->is_debug_types)
9481 {
9482 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9483 " supported in type units [in module %s]"),
5e22e966 9484 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9485 }
9486
95554aad
TT
9487 attr = dwarf2_attr (die, DW_AT_import, cu);
9488 if (attr != NULL)
9489 {
0826b30a 9490 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9491 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9492 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9493 dwarf2_per_cu_data *per_cu
ab432490 9494 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9495
58990295
TV
9496 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9497 into another compilation unit, at root level. Regard this as a hint,
9498 and ignore it. */
9499 if (die->parent && die->parent->parent == NULL
9500 && per_cu->unit_type == DW_UT_compile
9501 && per_cu->lang == language_cplus)
9502 return;
9503
69d751e3 9504 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 9505 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
9506 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9507 false, cu->language);
95554aad 9508
ae640021 9509 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9510 }
9511}
9512
4c8aa72d
PA
9513/* RAII object that represents a process_die scope: i.e.,
9514 starts/finishes processing a DIE. */
9515class process_die_scope
adde2bff 9516{
4c8aa72d
PA
9517public:
9518 process_die_scope (die_info *die, dwarf2_cu *cu)
9519 : m_die (die), m_cu (cu)
9520 {
9521 /* We should only be processing DIEs not already in process. */
9522 gdb_assert (!m_die->in_process);
9523 m_die->in_process = true;
9524 }
8c3cb9fa 9525
4c8aa72d
PA
9526 ~process_die_scope ()
9527 {
9528 m_die->in_process = false;
9529
9530 /* If we're done processing the DIE for the CU that owns the line
9531 header, we don't need the line header anymore. */
9532 if (m_cu->line_header_die_owner == m_die)
9533 {
9534 delete m_cu->line_header;
9535 m_cu->line_header = NULL;
9536 m_cu->line_header_die_owner = NULL;
9537 }
9538 }
9539
9540private:
9541 die_info *m_die;
9542 dwarf2_cu *m_cu;
9543};
adde2bff 9544
c906108c
SS
9545/* Process a die and its children. */
9546
9547static void
e7c27a73 9548process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9549{
4c8aa72d 9550 process_die_scope scope (die, cu);
adde2bff 9551
c906108c
SS
9552 switch (die->tag)
9553 {
9554 case DW_TAG_padding:
9555 break;
9556 case DW_TAG_compile_unit:
95554aad 9557 case DW_TAG_partial_unit:
e7c27a73 9558 read_file_scope (die, cu);
c906108c 9559 break;
348e048f
DE
9560 case DW_TAG_type_unit:
9561 read_type_unit_scope (die, cu);
9562 break;
c906108c 9563 case DW_TAG_subprogram:
0a4b0913
AB
9564 /* Nested subprograms in Fortran get a prefix. */
9565 if (cu->language == language_fortran
9566 && die->parent != NULL
9567 && die->parent->tag == DW_TAG_subprogram)
9568 cu->processing_has_namespace_info = true;
9569 /* Fall through. */
c906108c 9570 case DW_TAG_inlined_subroutine:
edb3359d 9571 read_func_scope (die, cu);
c906108c
SS
9572 break;
9573 case DW_TAG_lexical_block:
14898363
L
9574 case DW_TAG_try_block:
9575 case DW_TAG_catch_block:
e7c27a73 9576 read_lexical_block_scope (die, cu);
c906108c 9577 break;
216f72a1 9578 case DW_TAG_call_site:
96408a79
SA
9579 case DW_TAG_GNU_call_site:
9580 read_call_site_scope (die, cu);
9581 break;
c906108c 9582 case DW_TAG_class_type:
680b30c7 9583 case DW_TAG_interface_type:
c906108c
SS
9584 case DW_TAG_structure_type:
9585 case DW_TAG_union_type:
134d01f1 9586 process_structure_scope (die, cu);
c906108c
SS
9587 break;
9588 case DW_TAG_enumeration_type:
134d01f1 9589 process_enumeration_scope (die, cu);
c906108c 9590 break;
134d01f1 9591
f792889a
DJ
9592 /* These dies have a type, but processing them does not create
9593 a symbol or recurse to process the children. Therefore we can
9594 read them on-demand through read_type_die. */
c906108c 9595 case DW_TAG_subroutine_type:
72019c9c 9596 case DW_TAG_set_type:
c906108c 9597 case DW_TAG_pointer_type:
c906108c 9598 case DW_TAG_ptr_to_member_type:
c906108c 9599 case DW_TAG_reference_type:
4297a3f0 9600 case DW_TAG_rvalue_reference_type:
c906108c 9601 case DW_TAG_string_type:
c906108c 9602 break;
134d01f1 9603
d8f62e84
TT
9604 case DW_TAG_array_type:
9605 /* We only need to handle this case for Ada -- in other
9606 languages, it's normal for the compiler to emit a typedef
9607 instead. */
9608 if (cu->language != language_ada)
9609 break;
9610 /* FALLTHROUGH */
c906108c 9611 case DW_TAG_base_type:
a02abb62 9612 case DW_TAG_subrange_type:
cb249c71 9613 case DW_TAG_typedef:
134d01f1 9614 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 9615 DW_AT_name. */
f792889a 9616 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9617 break;
c906108c 9618 case DW_TAG_common_block:
e7c27a73 9619 read_common_block (die, cu);
c906108c
SS
9620 break;
9621 case DW_TAG_common_inclusion:
9622 break;
d9fa45fe 9623 case DW_TAG_namespace:
9068261f 9624 cu->processing_has_namespace_info = true;
e7c27a73 9625 read_namespace (die, cu);
d9fa45fe 9626 break;
5d7cb8df 9627 case DW_TAG_module:
9068261f 9628 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9629 read_module (die, cu);
9630 break;
d9fa45fe 9631 case DW_TAG_imported_declaration:
9068261f 9632 cu->processing_has_namespace_info = true;
74921315
KS
9633 if (read_namespace_alias (die, cu))
9634 break;
86a73007
TT
9635 /* The declaration is not a global namespace alias. */
9636 /* Fall through. */
d9fa45fe 9637 case DW_TAG_imported_module:
9068261f 9638 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9639 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9640 || cu->language != language_fortran))
b98664d3 9641 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9642 dwarf_tag_name (die->tag));
9643 read_import_statement (die, cu);
d9fa45fe 9644 break;
95554aad
TT
9645
9646 case DW_TAG_imported_unit:
9647 process_imported_unit_die (die, cu);
9648 break;
9649
71a3c369
TT
9650 case DW_TAG_variable:
9651 read_variable (die, cu);
9652 break;
9653
c906108c 9654 default:
e7c27a73 9655 new_symbol (die, NULL, cu);
c906108c
SS
9656 break;
9657 }
9658}
ca69b9e6
DE
9659\f
9660/* DWARF name computation. */
c906108c 9661
94af9270
KS
9662/* A helper function for dwarf2_compute_name which determines whether DIE
9663 needs to have the name of the scope prepended to the name listed in the
9664 die. */
9665
9666static int
9667die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9668{
1c809c68
TT
9669 struct attribute *attr;
9670
94af9270
KS
9671 switch (die->tag)
9672 {
9673 case DW_TAG_namespace:
9674 case DW_TAG_typedef:
9675 case DW_TAG_class_type:
9676 case DW_TAG_interface_type:
9677 case DW_TAG_structure_type:
9678 case DW_TAG_union_type:
9679 case DW_TAG_enumeration_type:
9680 case DW_TAG_enumerator:
9681 case DW_TAG_subprogram:
08a76f8a 9682 case DW_TAG_inlined_subroutine:
94af9270 9683 case DW_TAG_member:
74921315 9684 case DW_TAG_imported_declaration:
94af9270
KS
9685 return 1;
9686
9687 case DW_TAG_variable:
c2b0a229 9688 case DW_TAG_constant:
94af9270
KS
9689 /* We only need to prefix "globally" visible variables. These include
9690 any variable marked with DW_AT_external or any variable that
9691 lives in a namespace. [Variables in anonymous namespaces
9692 require prefixing, but they are not DW_AT_external.] */
9693
9694 if (dwarf2_attr (die, DW_AT_specification, cu))
9695 {
9696 struct dwarf2_cu *spec_cu = cu;
9a619af0 9697
94af9270
KS
9698 return die_needs_namespace (die_specification (die, &spec_cu),
9699 spec_cu);
9700 }
9701
1c809c68 9702 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9703 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9704 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9705 return 0;
9706 /* A variable in a lexical block of some kind does not need a
9707 namespace, even though in C++ such variables may be external
9708 and have a mangled name. */
9709 if (die->parent->tag == DW_TAG_lexical_block
9710 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9711 || die->parent->tag == DW_TAG_catch_block
9712 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9713 return 0;
9714 return 1;
94af9270
KS
9715
9716 default:
9717 return 0;
9718 }
9719}
9720
73b9be8b
KS
9721/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9722 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9723 defined for the given DIE. */
9724
9725static struct attribute *
9726dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9727{
9728 struct attribute *attr;
9729
9730 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9731 if (attr == NULL)
9732 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9733
9734 return attr;
9735}
9736
9737/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9738 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9739 defined for the given DIE. */
9740
9741static const char *
9742dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9743{
9744 const char *linkage_name;
9745
9746 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9747 if (linkage_name == NULL)
9748 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9749
787de330
TT
9750 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9751 See https://github.com/rust-lang/rust/issues/32925. */
9752 if (cu->language == language_rust && linkage_name != NULL
9753 && strchr (linkage_name, '{') != NULL)
9754 linkage_name = NULL;
9755
73b9be8b
KS
9756 return linkage_name;
9757}
9758
94af9270 9759/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9760 compute the physname for the object, which include a method's:
9c37b5ae 9761 - formal parameters (C++),
a766d390 9762 - receiver type (Go),
a766d390
DE
9763
9764 The term "physname" is a bit confusing.
9765 For C++, for example, it is the demangled name.
9766 For Go, for example, it's the mangled name.
94af9270 9767
af6b7be1
JB
9768 For Ada, return the DIE's linkage name rather than the fully qualified
9769 name. PHYSNAME is ignored..
9770
5989a64e 9771 The result is allocated on the objfile->per_bfd's obstack and
45940949 9772 canonicalized. */
94af9270
KS
9773
9774static const char *
15d034d0
TT
9775dwarf2_compute_name (const char *name,
9776 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9777 int physname)
9778{
5e22e966 9779 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 9780
94af9270
KS
9781 if (name == NULL)
9782 name = dwarf2_name (die, cu);
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. */
f55ee35c
JK
9791 if (cu->language == language_ada
9792 || (cu->language == language_fortran && physname))
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
9c37b5ae 9807 && (cu->language == language_cplus
c44af4eb
TT
9808 || cu->language == language_fortran || cu->language == language_d
9809 || cu->language == 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
9850 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9851 {
9852 struct attribute *attr;
9853 struct die_info *child;
9854 int first = 1;
2c75ccb2 9855 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
9856
9857 die->building_fullname = 1;
9858
9859 for (child = die->child; child != NULL; child = child->sibling)
9860 {
9861 struct type *type;
12df843f 9862 LONGEST value;
d521ce57 9863 const gdb_byte *bytes;
98bfdba5
PA
9864 struct dwarf2_locexpr_baton *baton;
9865 struct value *v;
9866
9867 if (child->tag != DW_TAG_template_type_param
9868 && child->tag != DW_TAG_template_value_param)
9869 continue;
9870
9871 if (first)
9872 {
d7e74731 9873 buf.puts ("<");
98bfdba5
PA
9874 first = 0;
9875 }
9876 else
d7e74731 9877 buf.puts (", ");
98bfdba5
PA
9878
9879 attr = dwarf2_attr (child, DW_AT_type, cu);
9880 if (attr == NULL)
9881 {
b98664d3 9882 complaint (_("template parameter missing DW_AT_type"));
d7e74731 9883 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
9884 continue;
9885 }
9886 type = die_type (child, cu);
9887
9888 if (child->tag == DW_TAG_template_type_param)
9889 {
2c75ccb2
AB
9890 cplus_lang->print_type (type, "", &buf, -1, 0,
9891 &type_print_raw_options);
98bfdba5
PA
9892 continue;
9893 }
9894
9895 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9896 if (attr == NULL)
9897 {
b98664d3 9898 complaint (_("template parameter missing "
3e43a32a 9899 "DW_AT_const_value"));
d7e74731 9900 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
9901 continue;
9902 }
9903
9904 dwarf2_const_value_attr (attr, type, name,
9905 &cu->comp_unit_obstack, cu,
9906 &value, &bytes, &baton);
9907
20ce4123 9908 if (type->has_no_signedness ())
98bfdba5
PA
9909 /* GDB prints characters as NUMBER 'CHAR'. If that's
9910 changed, this can use value_print instead. */
2c75ccb2 9911 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
9912 else
9913 {
9914 struct value_print_options opts;
9915
9916 if (baton != NULL)
9917 v = dwarf2_evaluate_loc_desc (type, NULL,
9918 baton->data,
9919 baton->size,
9f47c707
SM
9920 baton->per_cu,
9921 baton->per_objfile);
98bfdba5
PA
9922 else if (bytes != NULL)
9923 {
9924 v = allocate_value (type);
9925 memcpy (value_contents_writeable (v), bytes,
9926 TYPE_LENGTH (type));
9927 }
9928 else
9929 v = value_from_longest (type, value);
9930
3e43a32a
MS
9931 /* Specify decimal so that we do not depend on
9932 the radix. */
98bfdba5
PA
9933 get_formatted_print_options (&opts, 'd');
9934 opts.raw = 1;
d7e74731 9935 value_print (v, &buf, &opts);
98bfdba5 9936 release_value (v);
98bfdba5
PA
9937 }
9938 }
9939
9940 die->building_fullname = 0;
9941
9942 if (!first)
9943 {
9944 /* Close the argument list, with a space if necessary
9945 (nested templates). */
d7e74731
PA
9946 if (!buf.empty () && buf.string ().back () == '>')
9947 buf.puts (" >");
98bfdba5 9948 else
d7e74731 9949 buf.puts (">");
98bfdba5
PA
9950 }
9951 }
9952
9c37b5ae 9953 /* For C++ methods, append formal parameter type
94af9270 9954 information, if PHYSNAME. */
6e70227d 9955
94af9270 9956 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 9957 && cu->language == language_cplus)
94af9270
KS
9958 {
9959 struct type *type = read_type_die (die, cu);
9960
d7e74731 9961 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 9962 &type_print_raw_options);
94af9270 9963
9c37b5ae 9964 if (cu->language == language_cplus)
94af9270 9965 {
60430eff
DJ
9966 /* Assume that an artificial first parameter is
9967 "this", but do not crash if it is not. RealView
9968 marks unnamed (and thus unused) parameters as
9969 artificial; there is no way to differentiate
9970 the two cases. */
1f704f76 9971 if (type->num_fields () > 0
94af9270 9972 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
9973 && type->field (0).type ()->code () == TYPE_CODE_PTR
9974 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 9975 buf.puts (" const");
94af9270
KS
9976 }
9977 }
9978
d7e74731 9979 const std::string &intermediate_name = buf.string ();
94af9270
KS
9980
9981 if (cu->language == language_cplus)
34a68019 9982 canonical_name
322a8516 9983 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 9984 objfile);
34a68019
TT
9985
9986 /* If we only computed INTERMEDIATE_NAME, or if
9987 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 9988 intern it. */
322a8516 9989 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 9990 name = objfile->intern (intermediate_name);
34a68019
TT
9991 else
9992 name = canonical_name;
94af9270
KS
9993 }
9994 }
9995
9996 return name;
9997}
9998
0114d602
DJ
9999/* Return the fully qualified name of DIE, based on its DW_AT_name.
10000 If scope qualifiers are appropriate they will be added. The result
34a68019 10001 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10002 not have a name. NAME may either be from a previous call to
10003 dwarf2_name or NULL.
10004
9c37b5ae 10005 The output string will be canonicalized (if C++). */
0114d602
DJ
10006
10007static const char *
15d034d0 10008dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10009{
94af9270
KS
10010 return dwarf2_compute_name (name, die, cu, 0);
10011}
0114d602 10012
94af9270
KS
10013/* Construct a physname for the given DIE in CU. NAME may either be
10014 from a previous call to dwarf2_name or NULL. The result will be
10015 allocated on the objfile_objstack or NULL if the DIE does not have a
10016 name.
0114d602 10017
9c37b5ae 10018 The output string will be canonicalized (if C++). */
0114d602 10019
94af9270 10020static const char *
15d034d0 10021dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10022{
5e22e966 10023 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10024 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10025 int need_copy = 1;
10026
10027 /* In this case dwarf2_compute_name is just a shortcut not building anything
10028 on its own. */
10029 if (!die_needs_namespace (die, cu))
10030 return dwarf2_compute_name (name, die, cu, 1);
10031
906bb4c5
TT
10032 if (cu->language != language_rust)
10033 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10034
10035 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10036 has computed. */
791afaa2 10037 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10038 if (mangled != NULL)
900e11f9 10039 {
900e11f9 10040
d3355e4d 10041 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10042 {
10043 /* Do nothing (do not demangle the symbol name). */
10044 }
a766d390
DE
10045 else
10046 {
0eb876f5
JB
10047 /* Use DMGL_RET_DROP for C++ template functions to suppress
10048 their return type. It is easier for GDB users to search
10049 for such functions as `name(params)' than `long name(params)'.
10050 In such case the minimal symbol names do not match the full
10051 symbol names but for template functions there is never a need
10052 to look up their definition from their declaration so
10053 the only disadvantage remains the minimal symbol variant
10054 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10055 demangled.reset (gdb_demangle (mangled,
10056 (DMGL_PARAMS | DMGL_ANSI
10057 | DMGL_RET_DROP)));
a766d390 10058 }
900e11f9 10059 if (demangled)
791afaa2 10060 canon = demangled.get ();
900e11f9
JK
10061 else
10062 {
10063 canon = mangled;
10064 need_copy = 0;
10065 }
10066 }
10067
10068 if (canon == NULL || check_physname)
10069 {
10070 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10071
10072 if (canon != NULL && strcmp (physname, canon) != 0)
10073 {
10074 /* It may not mean a bug in GDB. The compiler could also
10075 compute DW_AT_linkage_name incorrectly. But in such case
10076 GDB would need to be bug-to-bug compatible. */
10077
b98664d3 10078 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10079 "(from linkage <%s>) - DIE at %s [in module %s]"),
10080 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10081 objfile_name (objfile));
900e11f9
JK
10082
10083 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10084 is available here - over computed PHYSNAME. It is safer
10085 against both buggy GDB and buggy compilers. */
10086
10087 retval = canon;
10088 }
10089 else
10090 {
10091 retval = physname;
10092 need_copy = 0;
10093 }
10094 }
10095 else
10096 retval = canon;
10097
10098 if (need_copy)
be1e3d3e 10099 retval = objfile->intern (retval);
900e11f9 10100
900e11f9 10101 return retval;
0114d602
DJ
10102}
10103
74921315
KS
10104/* Inspect DIE in CU for a namespace alias. If one exists, record
10105 a new symbol for it.
10106
10107 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10108
10109static int
10110read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10111{
10112 struct attribute *attr;
10113
10114 /* If the die does not have a name, this is not a namespace
10115 alias. */
10116 attr = dwarf2_attr (die, DW_AT_name, cu);
10117 if (attr != NULL)
10118 {
10119 int num;
10120 struct die_info *d = die;
10121 struct dwarf2_cu *imported_cu = cu;
10122
10123 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10124 keep inspecting DIEs until we hit the underlying import. */
10125#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10126 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10127 {
10128 attr = dwarf2_attr (d, DW_AT_import, cu);
10129 if (attr == NULL)
10130 break;
10131
10132 d = follow_die_ref (d, attr, &imported_cu);
10133 if (d->tag != DW_TAG_imported_declaration)
10134 break;
10135 }
10136
10137 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10138 {
b98664d3 10139 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10140 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10141 return 0;
10142 }
10143
10144 if (attr != NULL)
10145 {
10146 struct type *type;
0826b30a 10147 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10148
aa66c379 10149 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10150 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10151 {
10152 /* This declaration is a global namespace alias. Add
10153 a symbol for it whose type is the aliased namespace. */
10154 new_symbol (die, type, cu);
10155 return 1;
10156 }
10157 }
10158 }
10159
10160 return 0;
10161}
10162
22cee43f 10163/* Return the using directives repository (global or local?) to use in the
804d2729 10164 current context for CU.
22cee43f
PMR
10165
10166 For Ada, imported declarations can materialize renamings, which *may* be
10167 global. However it is impossible (for now?) in DWARF to distinguish
10168 "external" imported declarations and "static" ones. As all imported
10169 declarations seem to be static in all other languages, make them all CU-wide
10170 global only in Ada. */
10171
10172static struct using_direct **
804d2729 10173using_directives (struct dwarf2_cu *cu)
22cee43f 10174{
c24bdb02
KS
10175 if (cu->language == language_ada
10176 && cu->get_builder ()->outermost_context_p ())
10177 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10178 else
c24bdb02 10179 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10180}
10181
27aa8d6a
SW
10182/* Read the import statement specified by the given die and record it. */
10183
10184static void
10185read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10186{
5e22e966 10187 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10188 struct attribute *import_attr;
32019081 10189 struct die_info *imported_die, *child_die;
de4affc9 10190 struct dwarf2_cu *imported_cu;
27aa8d6a 10191 const char *imported_name;
794684b6 10192 const char *imported_name_prefix;
13387711
SW
10193 const char *canonical_name;
10194 const char *import_alias;
10195 const char *imported_declaration = NULL;
794684b6 10196 const char *import_prefix;
eb1e02fd 10197 std::vector<const char *> excludes;
13387711 10198
27aa8d6a
SW
10199 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10200 if (import_attr == NULL)
10201 {
b98664d3 10202 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10203 dwarf_tag_name (die->tag));
10204 return;
10205 }
10206
de4affc9
CC
10207 imported_cu = cu;
10208 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10209 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10210 if (imported_name == NULL)
10211 {
10212 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10213
dda83cd7
SM
10214 The import in the following code:
10215 namespace A
10216 {
10217 typedef int B;
10218 }
10219
10220 int main ()
10221 {
10222 using A::B;
10223 B b;
10224 return b;
10225 }
10226
10227 ...
10228 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10229 <52> DW_AT_decl_file : 1
10230 <53> DW_AT_decl_line : 6
10231 <54> DW_AT_import : <0x75>
10232 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10233 <59> DW_AT_name : B
10234 <5b> DW_AT_decl_file : 1
10235 <5c> DW_AT_decl_line : 2
10236 <5d> DW_AT_type : <0x6e>
10237 ...
10238 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10239 <76> DW_AT_byte_size : 4
10240 <77> DW_AT_encoding : 5 (signed)
10241
10242 imports the wrong die ( 0x75 instead of 0x58 ).
10243 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10244 return;
10245 }
10246
82856980
SW
10247 /* Figure out the local name after import. */
10248 import_alias = dwarf2_name (die, cu);
27aa8d6a 10249
794684b6
SW
10250 /* Figure out where the statement is being imported to. */
10251 import_prefix = determine_prefix (die, cu);
10252
10253 /* Figure out what the scope of the imported die is and prepend it
10254 to the name of the imported die. */
de4affc9 10255 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10256
f55ee35c
JK
10257 if (imported_die->tag != DW_TAG_namespace
10258 && imported_die->tag != DW_TAG_module)
794684b6 10259 {
13387711
SW
10260 imported_declaration = imported_name;
10261 canonical_name = imported_name_prefix;
794684b6 10262 }
13387711 10263 else if (strlen (imported_name_prefix) > 0)
12aaed36 10264 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10265 imported_name_prefix,
10266 (cu->language == language_d ? "." : "::"),
10267 imported_name, (char *) NULL);
13387711
SW
10268 else
10269 canonical_name = imported_name;
794684b6 10270
32019081
JK
10271 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10272 for (child_die = die->child; child_die && child_die->tag;
436c571c 10273 child_die = child_die->sibling)
32019081
JK
10274 {
10275 /* DWARF-4: A Fortran use statement with a “rename list” may be
10276 represented by an imported module entry with an import attribute
10277 referring to the module and owned entries corresponding to those
10278 entities that are renamed as part of being imported. */
10279
10280 if (child_die->tag != DW_TAG_imported_declaration)
10281 {
b98664d3 10282 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10283 "- DIE at %s [in module %s]"),
10284 sect_offset_str (child_die->sect_off),
10285 objfile_name (objfile));
32019081
JK
10286 continue;
10287 }
10288
10289 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10290 if (import_attr == NULL)
10291 {
b98664d3 10292 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10293 dwarf_tag_name (child_die->tag));
10294 continue;
10295 }
10296
10297 imported_cu = cu;
10298 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10299 &imported_cu);
10300 imported_name = dwarf2_name (imported_die, imported_cu);
10301 if (imported_name == NULL)
10302 {
b98664d3 10303 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10304 "imported name - DIE at %s [in module %s]"),
10305 sect_offset_str (child_die->sect_off),
10306 objfile_name (objfile));
32019081
JK
10307 continue;
10308 }
10309
eb1e02fd 10310 excludes.push_back (imported_name);
32019081
JK
10311
10312 process_die (child_die, cu);
10313 }
10314
804d2729 10315 add_using_directive (using_directives (cu),
22cee43f
PMR
10316 import_prefix,
10317 canonical_name,
10318 import_alias,
10319 imported_declaration,
10320 excludes,
10321 0,
10322 &objfile->objfile_obstack);
27aa8d6a
SW
10323}
10324
5230b05a
WT
10325/* ICC<14 does not output the required DW_AT_declaration on incomplete
10326 types, but gives them a size of zero. Starting with version 14,
10327 ICC is compatible with GCC. */
10328
9068261f 10329static bool
5230b05a
WT
10330producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10331{
10332 if (!cu->checked_producer)
10333 check_producer (cu);
10334
10335 return cu->producer_is_icc_lt_14;
10336}
10337
eb77c9df
AB
10338/* ICC generates a DW_AT_type for C void functions. This was observed on
10339 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10340 which says that void functions should not have a DW_AT_type. */
10341
10342static bool
10343producer_is_icc (struct dwarf2_cu *cu)
10344{
10345 if (!cu->checked_producer)
10346 check_producer (cu);
10347
10348 return cu->producer_is_icc;
10349}
10350
1b80a9fa
JK
10351/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10352 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10353 this, it was first present in GCC release 4.3.0. */
10354
9068261f 10355static bool
1b80a9fa
JK
10356producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10357{
10358 if (!cu->checked_producer)
10359 check_producer (cu);
10360
10361 return cu->producer_is_gcc_lt_4_3;
10362}
10363
d721ba37
PA
10364static file_and_directory
10365find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10366{
d721ba37
PA
10367 file_and_directory res;
10368
9291a0cd
TT
10369 /* Find the filename. Do not use dwarf2_name here, since the filename
10370 is not a source language identifier. */
d721ba37
PA
10371 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10372 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10373
d721ba37
PA
10374 if (res.comp_dir == NULL
10375 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10376 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10377 {
d721ba37
PA
10378 res.comp_dir_storage = ldirname (res.name);
10379 if (!res.comp_dir_storage.empty ())
10380 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10381 }
d721ba37 10382 if (res.comp_dir != NULL)
9291a0cd
TT
10383 {
10384 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10385 directory, get rid of it. */
d721ba37 10386 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10387
d721ba37
PA
10388 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10389 res.comp_dir = cp + 1;
9291a0cd
TT
10390 }
10391
d721ba37
PA
10392 if (res.name == NULL)
10393 res.name = "<unknown>";
10394
10395 return res;
9291a0cd
TT
10396}
10397
f4dc4d17
DE
10398/* Handle DW_AT_stmt_list for a compilation unit.
10399 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10400 COMP_DIR is the compilation directory. LOWPC is passed to
10401 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10402
10403static void
10404handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10405 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10406{
976ca316 10407 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10408 struct attribute *attr;
527f3840
JK
10409 struct line_header line_header_local;
10410 hashval_t line_header_local_hash;
527f3840
JK
10411 void **slot;
10412 int decode_mapping;
2ab95328 10413
f4dc4d17
DE
10414 gdb_assert (! cu->per_cu->is_debug_types);
10415
2ab95328 10416 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 10417 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
10418 return;
10419
d4df075e 10420 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
10421
10422 /* The line header hash table is only created if needed (it exists to
10423 prevent redundant reading of the line table for partial_units).
10424 If we're given a partial_unit, we'll need it. If we're given a
10425 compile_unit, then use the line header hash table if it's already
10426 created, but don't create one just yet. */
10427
976ca316 10428 if (per_objfile->line_header_hash == NULL
527f3840 10429 && die->tag == DW_TAG_partial_unit)
2ab95328 10430 {
976ca316 10431 per_objfile->line_header_hash
d15acc42
TT
10432 .reset (htab_create_alloc (127, line_header_hash_voidp,
10433 line_header_eq_voidp,
10434 free_line_header_voidp,
10435 xcalloc, xfree));
527f3840 10436 }
2ab95328 10437
9c541725 10438 line_header_local.sect_off = line_offset;
527f3840
JK
10439 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10440 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10441 if (per_objfile->line_header_hash != NULL)
527f3840 10442 {
976ca316 10443 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10444 &line_header_local,
10445 line_header_local_hash, NO_INSERT);
10446
10447 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10448 is not present in *SLOT (since if there is something in *SLOT then
10449 it will be for a partial_unit). */
10450 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10451 {
527f3840 10452 gdb_assert (*slot != NULL);
9a3c8263 10453 cu->line_header = (struct line_header *) *slot;
527f3840 10454 return;
dee91e82 10455 }
2ab95328 10456 }
527f3840
JK
10457
10458 /* dwarf_decode_line_header does not yet provide sufficient information.
10459 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10460 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10461 if (lh == NULL)
527f3840 10462 return;
4c8aa72d
PA
10463
10464 cu->line_header = lh.release ();
10465 cu->line_header_die_owner = die;
527f3840 10466
976ca316 10467 if (per_objfile->line_header_hash == NULL)
527f3840
JK
10468 slot = NULL;
10469 else
10470 {
976ca316 10471 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10472 &line_header_local,
10473 line_header_local_hash, INSERT);
10474 gdb_assert (slot != NULL);
10475 }
10476 if (slot != NULL && *slot == NULL)
10477 {
10478 /* This newly decoded line number information unit will be owned
10479 by line_header_hash hash table. */
10480 *slot = cu->line_header;
4c8aa72d 10481 cu->line_header_die_owner = NULL;
527f3840
JK
10482 }
10483 else
10484 {
10485 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 10486 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
10487 line_header for this CU - it may happen at most once for each line
10488 number information unit. And if we're not using line_header_hash
10489 then this is what we want as well. */
10490 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10491 }
10492 decode_mapping = (die->tag != DW_TAG_partial_unit);
10493 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10494 decode_mapping);
fff8551c 10495
2ab95328
TT
10496}
10497
95554aad 10498/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10499
c906108c 10500static void
e7c27a73 10501read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10502{
976ca316
SM
10503 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10504 struct objfile *objfile = per_objfile->objfile;
08feed99 10505 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10506 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10507 CORE_ADDR highpc = ((CORE_ADDR) 0);
10508 struct attribute *attr;
c906108c 10509 struct die_info *child_die;
e142c38c 10510 CORE_ADDR baseaddr;
6e70227d 10511
380618d6 10512 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10513 baseaddr = objfile->text_section_offset ();
c906108c 10514
fae299cd 10515 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10516
10517 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10518 from finish_block. */
2acceee2 10519 if (lowpc == ((CORE_ADDR) -1))
c906108c 10520 lowpc = highpc;
3e29f34a 10521 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10522
d721ba37 10523 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10524
f4b8a18d
KW
10525 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10526 standardised yet. As a workaround for the language detection we fall
10527 back to the DW_AT_producer string. */
10528 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10529 cu->language = language_opencl;
10530
3019eac3
DE
10531 /* Similar hack for Go. */
10532 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10533 set_cu_language (DW_LANG_Go, cu);
10534
c24bdb02 10535 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10536
10537 /* Decode line number information if present. We do this before
10538 processing child DIEs, so that the line header table is available
10539 for DW_AT_decl_file. */
d721ba37 10540 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10541
10542 /* Process all dies in compilation unit. */
10543 if (die->child != NULL)
10544 {
10545 child_die = die->child;
10546 while (child_die && child_die->tag)
10547 {
10548 process_die (child_die, cu);
436c571c 10549 child_die = child_die->sibling;
3019eac3
DE
10550 }
10551 }
10552
10553 /* Decode macro information, if present. Dwarf 2 macro information
10554 refers to information in the line number info statement program
10555 header, so we can only read it if we've read the header
10556 successfully. */
0af92d60
JK
10557 attr = dwarf2_attr (die, DW_AT_macros, cu);
10558 if (attr == NULL)
10559 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 10560 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
10561 {
10562 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10563 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10564
529908cb 10565 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
10566 }
10567 else
10568 {
10569 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 10570 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 10571 {
529908cb 10572 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 10573
43f3e411 10574 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10575 }
10576 }
3019eac3
DE
10577}
10578
c24bdb02
KS
10579void
10580dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10581{
f4dc4d17
DE
10582 struct type_unit_group *tu_group;
10583 int first_time;
3019eac3 10584 struct attribute *attr;
9c541725 10585 unsigned int i;
0186c6a7 10586 struct signatured_type *sig_type;
3019eac3 10587
f4dc4d17 10588 gdb_assert (per_cu->is_debug_types);
0186c6a7 10589 sig_type = (struct signatured_type *) per_cu;
3019eac3 10590
c24bdb02 10591 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10592
f4dc4d17 10593 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10594 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10595 if (sig_type->type_unit_group == NULL)
c24bdb02 10596 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10597 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10598
10599 /* If we've already processed this stmt_list there's no real need to
10600 do it again, we could fake it and just recreate the part we need
10601 (file name,index -> symtab mapping). If data shows this optimization
10602 is useful we can do it then. */
8adb8487
TT
10603 type_unit_group_unshareable *tug_unshare
10604 = per_objfile->get_type_unit_group_unshareable (tu_group);
10605 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
10606
10607 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10608 debug info. */
fff8551c 10609 line_header_up lh;
d4df075e 10610 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 10611 {
d4df075e 10612 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 10613 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10614 }
10615 if (lh == NULL)
10616 {
10617 if (first_time)
c24bdb02 10618 start_symtab ("", NULL, 0);
f4dc4d17
DE
10619 else
10620 {
8adb8487 10621 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 10622 gdb_assert (m_builder == nullptr);
8adb8487 10623 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10624 m_builder.reset (new struct buildsym_compunit
10625 (COMPUNIT_OBJFILE (cust), "",
10626 COMPUNIT_DIRNAME (cust),
10627 compunit_language (cust),
10628 0, cust));
770479f2 10629 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10630 }
f4dc4d17 10631 return;
3019eac3
DE
10632 }
10633
c24bdb02
KS
10634 line_header = lh.release ();
10635 line_header_die_owner = die;
3019eac3 10636
f4dc4d17
DE
10637 if (first_time)
10638 {
c24bdb02 10639 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10640
1fd60fc0
DE
10641 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10642 still initializing it, and our caller (a few levels up)
10643 process_full_type_unit still needs to know if this is the first
10644 time. */
10645
8adb8487 10646 tug_unshare->symtabs
4ac93832
TT
10647 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10648 struct symtab *, line_header->file_names_size ());
3019eac3 10649
7ba99d21
AT
10650 auto &file_names = line_header->file_names ();
10651 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10652 {
7ba99d21 10653 file_entry &fe = file_names[i];
c24bdb02
KS
10654 dwarf2_start_subfile (this, fe.name,
10655 fe.include_dir (line_header));
10656 buildsym_compunit *b = get_builder ();
10657 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10658 {
4c8aa72d
PA
10659 /* NOTE: start_subfile will recognize when it's been
10660 passed a file it has already seen. So we can't
10661 assume there's a simple mapping from
10662 cu->line_header->file_names to subfiles, plus
10663 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10664 b->get_current_subfile ()->symtab
10665 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10666 }
10667
c24bdb02 10668 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 10669 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
10670 }
10671 }
10672 else
3019eac3 10673 {
c24bdb02 10674 gdb_assert (m_builder == nullptr);
8adb8487 10675 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10676 m_builder.reset (new struct buildsym_compunit
10677 (COMPUNIT_OBJFILE (cust), "",
10678 COMPUNIT_DIRNAME (cust),
10679 compunit_language (cust),
10680 0, cust));
770479f2 10681 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10682
7ba99d21
AT
10683 auto &file_names = line_header->file_names ();
10684 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10685 {
7ba99d21 10686 file_entry &fe = file_names[i];
8adb8487 10687 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 10688 }
3019eac3
DE
10689 }
10690
f4dc4d17
DE
10691 /* The main symtab is allocated last. Type units don't have DW_AT_name
10692 so they don't have a "real" (so to speak) symtab anyway.
10693 There is later code that will assign the main symtab to all symbols
10694 that don't have one. We need to handle the case of a symbol with a
10695 missing symtab (DW_AT_decl_file) anyway. */
10696}
3019eac3 10697
f4dc4d17
DE
10698/* Process DW_TAG_type_unit.
10699 For TUs we want to skip the first top level sibling if it's not the
10700 actual type being defined by this TU. In this case the first top
10701 level sibling is there to provide context only. */
3019eac3 10702
f4dc4d17
DE
10703static void
10704read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10705{
10706 struct die_info *child_die;
3019eac3 10707
f4dc4d17
DE
10708 prepare_one_comp_unit (cu, die, language_minimal);
10709
10710 /* Initialize (or reinitialize) the machinery for building symtabs.
10711 We do this before processing child DIEs, so that the line header table
10712 is available for DW_AT_decl_file. */
c24bdb02 10713 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10714
10715 if (die->child != NULL)
10716 {
10717 child_die = die->child;
10718 while (child_die && child_die->tag)
10719 {
10720 process_die (child_die, cu);
436c571c 10721 child_die = child_die->sibling;
f4dc4d17
DE
10722 }
10723 }
3019eac3
DE
10724}
10725\f
80626a55
DE
10726/* DWO/DWP files.
10727
10728 http://gcc.gnu.org/wiki/DebugFission
10729 http://gcc.gnu.org/wiki/DebugFissionDWP
10730
10731 To simplify handling of both DWO files ("object" files with the DWARF info)
10732 and DWP files (a file with the DWOs packaged up into one file), we treat
10733 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10734
10735static hashval_t
10736hash_dwo_file (const void *item)
10737{
9a3c8263 10738 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10739 hashval_t hash;
3019eac3 10740
a2ce51a0
DE
10741 hash = htab_hash_string (dwo_file->dwo_name);
10742 if (dwo_file->comp_dir != NULL)
10743 hash += htab_hash_string (dwo_file->comp_dir);
10744 return hash;
3019eac3
DE
10745}
10746
10747static int
10748eq_dwo_file (const void *item_lhs, const void *item_rhs)
10749{
9a3c8263
SM
10750 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10751 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10752
a2ce51a0
DE
10753 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10754 return 0;
10755 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10756 return lhs->comp_dir == rhs->comp_dir;
10757 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10758}
10759
10760/* Allocate a hash table for DWO files. */
10761
51ac9db5 10762static htab_up
298e9637 10763allocate_dwo_file_hash_table ()
3019eac3 10764{
51ac9db5
SM
10765 auto delete_dwo_file = [] (void *item)
10766 {
10767 struct dwo_file *dwo_file = (struct dwo_file *) item;
10768
10769 delete dwo_file;
10770 };
10771
bc68fb19
TT
10772 return htab_up (htab_create_alloc (41,
10773 hash_dwo_file,
10774 eq_dwo_file,
10775 delete_dwo_file,
10776 xcalloc, xfree));
3019eac3
DE
10777}
10778
80626a55
DE
10779/* Lookup DWO file DWO_NAME. */
10780
10781static void **
976ca316 10782lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
10783 const char *dwo_name,
10784 const char *comp_dir)
80626a55
DE
10785{
10786 struct dwo_file find_entry;
10787 void **slot;
10788
976ca316
SM
10789 if (per_objfile->per_bfd->dwo_files == NULL)
10790 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 10791
0ac5b59e
DE
10792 find_entry.dwo_name = dwo_name;
10793 find_entry.comp_dir = comp_dir;
976ca316 10794 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 10795 INSERT);
80626a55
DE
10796
10797 return slot;
10798}
10799
3019eac3
DE
10800static hashval_t
10801hash_dwo_unit (const void *item)
10802{
9a3c8263 10803 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
10804
10805 /* This drops the top 32 bits of the id, but is ok for a hash. */
10806 return dwo_unit->signature;
10807}
10808
10809static int
10810eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10811{
9a3c8263
SM
10812 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10813 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
10814
10815 /* The signature is assumed to be unique within the DWO file.
10816 So while object file CU dwo_id's always have the value zero,
10817 that's OK, assuming each object file DWO file has only one CU,
10818 and that's the rule for now. */
10819 return lhs->signature == rhs->signature;
10820}
10821
10822/* Allocate a hash table for DWO CUs,TUs.
10823 There is one of these tables for each of CUs,TUs for each DWO file. */
10824
b0b6a987 10825static htab_up
298e9637 10826allocate_dwo_unit_table ()
3019eac3
DE
10827{
10828 /* Start out with a pretty small number.
10829 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
10830 return htab_up (htab_create_alloc (3,
10831 hash_dwo_unit,
10832 eq_dwo_unit,
10833 NULL, xcalloc, xfree));
3019eac3
DE
10834}
10835
19c3d4c9 10836/* die_reader_func for create_dwo_cu. */
3019eac3
DE
10837
10838static void
19c3d4c9
DE
10839create_dwo_cu_reader (const struct die_reader_specs *reader,
10840 const gdb_byte *info_ptr,
10841 struct die_info *comp_unit_die,
c0ab21c2
TT
10842 struct dwo_file *dwo_file,
10843 struct dwo_unit *dwo_unit)
3019eac3
DE
10844{
10845 struct dwarf2_cu *cu = reader->cu;
9c541725 10846 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 10847 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 10848
a084a2a6
AT
10849 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10850 if (!signature.has_value ())
3019eac3 10851 {
b98664d3 10852 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 10853 " its dwo_id [in module %s]"),
9d8780f0 10854 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
10855 return;
10856 }
10857
3019eac3 10858 dwo_unit->dwo_file = dwo_file;
a084a2a6 10859 dwo_unit->signature = *signature;
8a0459fd 10860 dwo_unit->section = section;
9c541725 10861 dwo_unit->sect_off = sect_off;
3019eac3
DE
10862 dwo_unit->length = cu->per_cu->length;
10863
6f738b01
SM
10864 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10865 sect_offset_str (sect_off),
10866 hex_string (dwo_unit->signature));
3019eac3
DE
10867}
10868
33c5cd75 10869/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 10870 Note: This function processes DWO files only, not DWP files. */
3019eac3 10871
33c5cd75 10872static void
976ca316 10873create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 10874 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 10875 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 10876{
976ca316
SM
10877 struct objfile *objfile = per_objfile->objfile;
10878 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 10879 const gdb_byte *info_ptr, *end_ptr;
3019eac3 10880
96b79293 10881 section.read (objfile);
33c5cd75 10882 info_ptr = section.buffer;
3019eac3
DE
10883
10884 if (info_ptr == NULL)
33c5cd75 10885 return;
3019eac3 10886
6f738b01
SM
10887 dwarf_read_debug_printf ("Reading %s for %s:",
10888 section.get_name (),
10889 section.get_file_name ());
3019eac3 10890
33c5cd75 10891 end_ptr = info_ptr + section.size;
3019eac3
DE
10892 while (info_ptr < end_ptr)
10893 {
10894 struct dwarf2_per_cu_data per_cu;
c0ab21c2 10895 struct dwo_unit read_unit {};
33c5cd75
DB
10896 struct dwo_unit *dwo_unit;
10897 void **slot;
10898 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 10899
1859c670 10900 per_cu.per_bfd = per_bfd;
3019eac3 10901 per_cu.is_debug_types = 0;
33c5cd75
DB
10902 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10903 per_cu.section = &section;
10904
976ca316 10905 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
10906 if (!reader.dummy_p)
10907 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 10908 &dwo_file, &read_unit);
33c5cd75
DB
10909 info_ptr += per_cu.length;
10910
10911 // If the unit could not be parsed, skip it.
c0ab21c2 10912 if (read_unit.dwo_file == NULL)
33c5cd75 10913 continue;
3019eac3 10914
33c5cd75 10915 if (cus_htab == NULL)
298e9637 10916 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 10917
1859c670 10918 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 10919 struct dwo_unit);
c0ab21c2 10920 *dwo_unit = read_unit;
b0b6a987 10921 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
10922 gdb_assert (slot != NULL);
10923 if (*slot != NULL)
19c3d4c9 10924 {
33c5cd75
DB
10925 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10926 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 10927
b98664d3 10928 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
10929 " the entry at offset %s, signature %s"),
10930 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 10931 hex_string (dwo_unit->signature));
19c3d4c9 10932 }
33c5cd75 10933 *slot = (void *)dwo_unit;
3019eac3 10934 }
3019eac3
DE
10935}
10936
80626a55
DE
10937/* DWP file .debug_{cu,tu}_index section format:
10938 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
10939 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10940
10941 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10942 officially standard DWP format was published with DWARF v5 and is called
10943 Version 5. There are no versions 3 or 4.
80626a55 10944
d2415c6c
DE
10945 DWP Version 1:
10946
80626a55
DE
10947 Both index sections have the same format, and serve to map a 64-bit
10948 signature to a set of section numbers. Each section begins with a header,
10949 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10950 indexes, and a pool of 32-bit section numbers. The index sections will be
10951 aligned at 8-byte boundaries in the file.
10952
d2415c6c
DE
10953 The index section header consists of:
10954
10955 V, 32 bit version number
10956 -, 32 bits unused
10957 N, 32 bit number of compilation units or type units in the index
10958 M, 32 bit number of slots in the hash table
80626a55 10959
d2415c6c 10960 Numbers are recorded using the byte order of the application binary.
80626a55 10961
d2415c6c
DE
10962 The hash table begins at offset 16 in the section, and consists of an array
10963 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10964 order of the application binary). Unused slots in the hash table are 0.
10965 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 10966
d2415c6c
DE
10967 The parallel table begins immediately after the hash table
10968 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10969 array of 32-bit indexes (using the byte order of the application binary),
10970 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10971 table contains a 32-bit index into the pool of section numbers. For unused
10972 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 10973
73869dc2
DE
10974 The pool of section numbers begins immediately following the hash table
10975 (at offset 16 + 12 * M from the beginning of the section). The pool of
10976 section numbers consists of an array of 32-bit words (using the byte order
10977 of the application binary). Each item in the array is indexed starting
10978 from 0. The hash table entry provides the index of the first section
10979 number in the set. Additional section numbers in the set follow, and the
10980 set is terminated by a 0 entry (section number 0 is not used in ELF).
10981
10982 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10983 section must be the first entry in the set, and the .debug_abbrev.dwo must
10984 be the second entry. Other members of the set may follow in any order.
10985
10986 ---
10987
d2854d8d 10988 DWP Versions 2 and 5:
73869dc2 10989
d2854d8d 10990 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
10991 and the entries in the index tables are now offsets into these sections.
10992 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10993 section.
10994
10995 Index Section Contents:
10996 Header
10997 Hash Table of Signatures dwp_hash_table.hash_table
10998 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
10999 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11000 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11001
11002 The index section header consists of:
11003
11004 V, 32 bit version number
11005 L, 32 bit number of columns in the table of section offsets
11006 N, 32 bit number of compilation units or type units in the index
11007 M, 32 bit number of slots in the hash table
11008
11009 Numbers are recorded using the byte order of the application binary.
11010
11011 The hash table has the same format as version 1.
11012 The parallel table of indices has the same format as version 1,
11013 except that the entries are origin-1 indices into the table of sections
11014 offsets and the table of section sizes.
11015
11016 The table of offsets begins immediately following the parallel table
11017 (at offset 16 + 12 * M from the beginning of the section). The table is
11018 a two-dimensional array of 32-bit words (using the byte order of the
11019 application binary), with L columns and N+1 rows, in row-major order.
11020 Each row in the array is indexed starting from 0. The first row provides
11021 a key to the remaining rows: each column in this row provides an identifier
11022 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11023 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11024
11025 DW_SECT_INFO 1 .debug_info.dwo
11026 DW_SECT_TYPES 2 .debug_types.dwo
11027 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11028 DW_SECT_LINE 4 .debug_line.dwo
11029 DW_SECT_LOC 5 .debug_loc.dwo
11030 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11031 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11032 DW_SECT_MACRO 8 .debug_macro.dwo
11033
d2854d8d
CT
11034 The section identifiers for Version 5 are:
11035
11036 DW_SECT_INFO_V5 1 .debug_info.dwo
11037 DW_SECT_RESERVED_V5 2 --
11038 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11039 DW_SECT_LINE_V5 4 .debug_line.dwo
11040 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11041 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11042 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11043 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11044
73869dc2
DE
11045 The offsets provided by the CU and TU index sections are the base offsets
11046 for the contributions made by each CU or TU to the corresponding section
11047 in the package file. Each CU and TU header contains an abbrev_offset
11048 field, used to find the abbreviations table for that CU or TU within the
11049 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11050 be interpreted as relative to the base offset given in the index section.
11051 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11052 should be interpreted as relative to the base offset for .debug_line.dwo,
11053 and offsets into other debug sections obtained from DWARF attributes should
11054 also be interpreted as relative to the corresponding base offset.
11055
11056 The table of sizes begins immediately following the table of offsets.
11057 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11058 with L columns and N rows, in row-major order. Each row in the array is
11059 indexed starting from 1 (row 0 is shared by the two tables).
11060
11061 ---
11062
11063 Hash table lookup is handled the same in version 1 and 2:
11064
11065 We assume that N and M will not exceed 2^32 - 1.
11066 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11067
d2415c6c
DE
11068 Given a 64-bit compilation unit signature or a type signature S, an entry
11069 in the hash table is located as follows:
80626a55 11070
d2415c6c
DE
11071 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11072 the low-order k bits all set to 1.
80626a55 11073
d2415c6c 11074 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11075
d2415c6c
DE
11076 3) If the hash table entry at index H matches the signature, use that
11077 entry. If the hash table entry at index H is unused (all zeroes),
11078 terminate the search: the signature is not present in the table.
80626a55 11079
d2415c6c 11080 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11081
d2415c6c 11082 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11083 to stop at an unused slot or find the match. */
80626a55
DE
11084
11085/* Create a hash table to map DWO IDs to their CU/TU entry in
11086 .debug_{info,types}.dwo in DWP_FILE.
11087 Returns NULL if there isn't one.
11088 Note: This function processes DWP files only, not DWO files. */
11089
11090static struct dwp_hash_table *
976ca316 11091create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11092 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11093{
976ca316 11094 struct objfile *objfile = per_objfile->objfile;
400174b1 11095 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11096 const gdb_byte *index_ptr, *index_end;
80626a55 11097 struct dwarf2_section_info *index;
73869dc2 11098 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11099 struct dwp_hash_table *htab;
11100
11101 if (is_debug_types)
11102 index = &dwp_file->sections.tu_index;
11103 else
11104 index = &dwp_file->sections.cu_index;
11105
96b79293 11106 if (index->empty ())
80626a55 11107 return NULL;
96b79293 11108 index->read (objfile);
80626a55
DE
11109
11110 index_ptr = index->buffer;
11111 index_end = index_ptr + index->size;
11112
d2854d8d
CT
11113 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11114 For now it's safe to just read 4 bytes (particularly as it's difficult to
11115 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11116 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11117 index_ptr += 4;
d2854d8d 11118 if (version == 2 || version == 5)
73869dc2
DE
11119 nr_columns = read_4_bytes (dbfd, index_ptr);
11120 else
11121 nr_columns = 0;
11122 index_ptr += 4;
80626a55
DE
11123 nr_units = read_4_bytes (dbfd, index_ptr);
11124 index_ptr += 4;
11125 nr_slots = read_4_bytes (dbfd, index_ptr);
11126 index_ptr += 4;
11127
d2854d8d 11128 if (version != 1 && version != 2 && version != 5)
80626a55 11129 {
21aa081e 11130 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11131 " [in module %s]"),
21aa081e 11132 pulongest (version), dwp_file->name);
80626a55
DE
11133 }
11134 if (nr_slots != (nr_slots & -nr_slots))
11135 {
21aa081e 11136 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11137 " is not power of 2 [in module %s]"),
21aa081e 11138 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11139 }
11140
976ca316 11141 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11142 htab->version = version;
11143 htab->nr_columns = nr_columns;
80626a55
DE
11144 htab->nr_units = nr_units;
11145 htab->nr_slots = nr_slots;
11146 htab->hash_table = index_ptr;
11147 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11148
11149 /* Exit early if the table is empty. */
11150 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11151 || (version == 2 && nr_columns == 0)
11152 || (version == 5 && nr_columns == 0))
73869dc2
DE
11153 {
11154 /* All must be zero. */
11155 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11156 || (version == 2 && nr_columns != 0)
11157 || (version == 5 && nr_columns != 0))
73869dc2 11158 {
b98664d3 11159 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11160 " all zero [in modules %s]"),
11161 dwp_file->name);
11162 }
11163 return htab;
11164 }
11165
11166 if (version == 1)
11167 {
11168 htab->section_pool.v1.indices =
11169 htab->unit_table + sizeof (uint32_t) * nr_slots;
11170 /* It's harder to decide whether the section is too small in v1.
11171 V1 is deprecated anyway so we punt. */
11172 }
d2854d8d 11173 else if (version == 2)
73869dc2
DE
11174 {
11175 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11176 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11177 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11178 /* Reverse map for error checking. */
11179 int ids_seen[DW_SECT_MAX + 1];
11180 int i;
11181
11182 if (nr_columns < 2)
11183 {
11184 error (_("Dwarf Error: bad DWP hash table, too few columns"
11185 " in section table [in module %s]"),
11186 dwp_file->name);
11187 }
11188 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11189 {
11190 error (_("Dwarf Error: bad DWP hash table, too many columns"
11191 " in section table [in module %s]"),
11192 dwp_file->name);
11193 }
04fd5eed
GB
11194 memset (ids, 255, sizeof_ids);
11195 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11196 for (i = 0; i < nr_columns; ++i)
11197 {
11198 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11199
11200 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11201 {
11202 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11203 " in section table [in module %s]"),
11204 id, dwp_file->name);
11205 }
11206 if (ids_seen[id] != -1)
11207 {
11208 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11209 " id %d in section table [in module %s]"),
11210 id, dwp_file->name);
11211 }
11212 ids_seen[id] = i;
11213 ids[i] = id;
11214 }
11215 /* Must have exactly one info or types section. */
11216 if (((ids_seen[DW_SECT_INFO] != -1)
11217 + (ids_seen[DW_SECT_TYPES] != -1))
11218 != 1)
11219 {
11220 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11221 " DWO info/types section [in module %s]"),
11222 dwp_file->name);
11223 }
11224 /* Must have an abbrev section. */
11225 if (ids_seen[DW_SECT_ABBREV] == -1)
11226 {
11227 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11228 " section [in module %s]"),
11229 dwp_file->name);
11230 }
11231 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11232 htab->section_pool.v2.sizes =
11233 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11234 * nr_units * nr_columns);
11235 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11236 * nr_units * nr_columns))
11237 > index_end)
11238 {
11239 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11240 " [in module %s]"),
11241 dwp_file->name);
11242 }
11243 }
d2854d8d
CT
11244 else /* version == 5 */
11245 {
11246 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11247 int *ids = htab->section_pool.v5.section_ids;
11248 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11249 /* Reverse map for error checking. */
11250 int ids_seen[DW_SECT_MAX_V5 + 1];
11251
11252 if (nr_columns < 2)
11253 {
11254 error (_("Dwarf Error: bad DWP hash table, too few columns"
11255 " in section table [in module %s]"),
11256 dwp_file->name);
11257 }
11258 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11259 {
11260 error (_("Dwarf Error: bad DWP hash table, too many columns"
11261 " in section table [in module %s]"),
11262 dwp_file->name);
11263 }
11264 memset (ids, 255, sizeof_ids);
11265 memset (ids_seen, 255, sizeof (ids_seen));
11266 for (int i = 0; i < nr_columns; ++i)
11267 {
11268 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11269
11270 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11271 {
11272 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11273 " in section table [in module %s]"),
11274 id, dwp_file->name);
11275 }
11276 if (ids_seen[id] != -1)
11277 {
11278 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11279 " id %d in section table [in module %s]"),
11280 id, dwp_file->name);
11281 }
11282 ids_seen[id] = i;
11283 ids[i] = id;
11284 }
11285 /* Must have seen an info section. */
11286 if (ids_seen[DW_SECT_INFO_V5] == -1)
11287 {
11288 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11289 " DWO info/types section [in module %s]"),
11290 dwp_file->name);
11291 }
11292 /* Must have an abbrev section. */
11293 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11294 {
11295 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11296 " section [in module %s]"),
11297 dwp_file->name);
11298 }
11299 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11300 htab->section_pool.v5.sizes
11301 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11302 * nr_units * nr_columns);
11303 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11304 * nr_units * nr_columns))
11305 > index_end)
11306 {
11307 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11308 " [in module %s]"),
11309 dwp_file->name);
11310 }
11311 }
80626a55
DE
11312
11313 return htab;
11314}
11315
11316/* Update SECTIONS with the data from SECTP.
11317
5bb6e9dd
TT
11318 This function is like the other "locate" section routines, but in
11319 this context the sections to read comes from the DWP V1 hash table,
11320 not the full ELF section table.
80626a55
DE
11321
11322 The result is non-zero for success, or zero if an error was found. */
11323
11324static int
73869dc2
DE
11325locate_v1_virtual_dwo_sections (asection *sectp,
11326 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11327{
11328 const struct dwop_section_names *names = &dwop_section_names;
11329
fbedd546 11330 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
11331 {
11332 /* There can be only one. */
049412e3 11333 if (sections->abbrev.s.section != NULL)
80626a55 11334 return 0;
049412e3 11335 sections->abbrev.s.section = sectp;
fd361982 11336 sections->abbrev.size = bfd_section_size (sectp);
80626a55 11337 }
fbedd546
TT
11338 else if (names->info_dwo.matches (sectp->name)
11339 || names->types_dwo.matches (sectp->name))
80626a55
DE
11340 {
11341 /* There can be only one. */
049412e3 11342 if (sections->info_or_types.s.section != NULL)
80626a55 11343 return 0;
049412e3 11344 sections->info_or_types.s.section = sectp;
fd361982 11345 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 11346 }
fbedd546 11347 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
11348 {
11349 /* There can be only one. */
049412e3 11350 if (sections->line.s.section != NULL)
80626a55 11351 return 0;
049412e3 11352 sections->line.s.section = sectp;
fd361982 11353 sections->line.size = bfd_section_size (sectp);
80626a55 11354 }
fbedd546 11355 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
11356 {
11357 /* There can be only one. */
049412e3 11358 if (sections->loc.s.section != NULL)
80626a55 11359 return 0;
049412e3 11360 sections->loc.s.section = sectp;
fd361982 11361 sections->loc.size = bfd_section_size (sectp);
80626a55 11362 }
fbedd546 11363 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
11364 {
11365 /* There can be only one. */
049412e3 11366 if (sections->macinfo.s.section != NULL)
80626a55 11367 return 0;
049412e3 11368 sections->macinfo.s.section = sectp;
fd361982 11369 sections->macinfo.size = bfd_section_size (sectp);
80626a55 11370 }
fbedd546 11371 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
11372 {
11373 /* There can be only one. */
049412e3 11374 if (sections->macro.s.section != NULL)
80626a55 11375 return 0;
049412e3 11376 sections->macro.s.section = sectp;
fd361982 11377 sections->macro.size = bfd_section_size (sectp);
80626a55 11378 }
fbedd546 11379 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
11380 {
11381 /* There can be only one. */
049412e3 11382 if (sections->str_offsets.s.section != NULL)
80626a55 11383 return 0;
049412e3 11384 sections->str_offsets.s.section = sectp;
fd361982 11385 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11386 }
11387 else
11388 {
11389 /* No other kind of section is valid. */
11390 return 0;
11391 }
11392
11393 return 1;
11394}
11395
73869dc2
DE
11396/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11397 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11398 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11399 This is for DWP version 1 files. */
80626a55
DE
11400
11401static struct dwo_unit *
976ca316 11402create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11403 struct dwp_file *dwp_file,
73869dc2
DE
11404 uint32_t unit_index,
11405 const char *comp_dir,
11406 ULONGEST signature, int is_debug_types)
80626a55 11407{
73869dc2
DE
11408 const struct dwp_hash_table *dwp_htab =
11409 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11410 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11411 const char *kind = is_debug_types ? "TU" : "CU";
11412 struct dwo_file *dwo_file;
11413 struct dwo_unit *dwo_unit;
73869dc2 11414 struct virtual_v1_dwo_sections sections;
80626a55 11415 void **dwo_file_slot;
80626a55
DE
11416 int i;
11417
73869dc2
DE
11418 gdb_assert (dwp_file->version == 1);
11419
6f738b01
SM
11420 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11421 kind, pulongest (unit_index), hex_string (signature),
11422 dwp_file->name);
80626a55 11423
19ac8c2e 11424 /* Fetch the sections of this DWO unit.
80626a55
DE
11425 Put a limit on the number of sections we look for so that bad data
11426 doesn't cause us to loop forever. */
11427
73869dc2 11428#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11429 (1 /* .debug_info or .debug_types */ \
11430 + 1 /* .debug_abbrev */ \
11431 + 1 /* .debug_line */ \
11432 + 1 /* .debug_loc */ \
11433 + 1 /* .debug_str_offsets */ \
19ac8c2e 11434 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11435 + 1 /* trailing zero */)
11436
11437 memset (&sections, 0, sizeof (sections));
80626a55 11438
73869dc2 11439 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11440 {
11441 asection *sectp;
11442 uint32_t section_nr =
11443 read_4_bytes (dbfd,
73869dc2
DE
11444 dwp_htab->section_pool.v1.indices
11445 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11446
11447 if (section_nr == 0)
11448 break;
11449 if (section_nr >= dwp_file->num_sections)
11450 {
11451 error (_("Dwarf Error: bad DWP hash table, section number too large"
11452 " [in module %s]"),
11453 dwp_file->name);
11454 }
11455
11456 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11457 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11458 {
11459 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11460 " [in module %s]"),
11461 dwp_file->name);
11462 }
11463 }
11464
11465 if (i < 2
96b79293
TT
11466 || sections.info_or_types.empty ()
11467 || sections.abbrev.empty ())
80626a55
DE
11468 {
11469 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11470 " [in module %s]"),
11471 dwp_file->name);
11472 }
73869dc2 11473 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11474 {
11475 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11476 " [in module %s]"),
11477 dwp_file->name);
11478 }
11479
11480 /* It's easier for the rest of the code if we fake a struct dwo_file and
11481 have dwo_unit "live" in that. At least for now.
11482
11483 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11484 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11485 file, we can combine them back into a virtual DWO file to save space
11486 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11487 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11488
791afaa2
TT
11489 std::string virtual_dwo_name =
11490 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11491 sections.abbrev.get_id (),
11492 sections.line.get_id (),
11493 sections.loc.get_id (),
11494 sections.str_offsets.get_id ());
80626a55 11495 /* Can we use an existing virtual DWO file? */
976ca316 11496 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11497 comp_dir);
80626a55
DE
11498 /* Create one if necessary. */
11499 if (*dwo_file_slot == NULL)
11500 {
6f738b01
SM
11501 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11502 virtual_dwo_name.c_str ());
11503
51ac9db5 11504 dwo_file = new struct dwo_file;
976ca316 11505 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11506 dwo_file->comp_dir = comp_dir;
80626a55
DE
11507 dwo_file->sections.abbrev = sections.abbrev;
11508 dwo_file->sections.line = sections.line;
11509 dwo_file->sections.loc = sections.loc;
11510 dwo_file->sections.macinfo = sections.macinfo;
11511 dwo_file->sections.macro = sections.macro;
11512 dwo_file->sections.str_offsets = sections.str_offsets;
11513 /* The "str" section is global to the entire DWP file. */
11514 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11515 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11516 there's no need to record it in dwo_file.
11517 Also, we can't simply record type sections in dwo_file because
11518 we record a pointer into the vector in dwo_unit. As we collect more
11519 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11520 for it, invalidating all copies of pointers into the previous
11521 contents. */
80626a55
DE
11522 *dwo_file_slot = dwo_file;
11523 }
11524 else
11525 {
6f738b01
SM
11526 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11527 virtual_dwo_name.c_str ());
11528
9a3c8263 11529 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11530 }
80626a55 11531
976ca316 11532 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11533 dwo_unit->dwo_file = dwo_file;
11534 dwo_unit->signature = signature;
8d749320 11535 dwo_unit->section =
976ca316 11536 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11537 *dwo_unit->section = sections.info_or_types;
57d63ce2 11538 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11539
11540 return dwo_unit;
11541}
11542
d2854d8d
CT
11543/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11544 simplify them. Given a pointer to the containing section SECTION, and
11545 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11546 virtual section of just that piece. */
73869dc2
DE
11547
11548static struct dwarf2_section_info
d2854d8d
CT
11549create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11550 struct dwarf2_section_info *section,
11551 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
11552{
11553 struct dwarf2_section_info result;
11554 asection *sectp;
11555
11556 gdb_assert (section != NULL);
11557 gdb_assert (!section->is_virtual);
11558
11559 memset (&result, 0, sizeof (result));
11560 result.s.containing_section = section;
dc4ccb6f 11561 result.is_virtual = true;
73869dc2
DE
11562
11563 if (size == 0)
11564 return result;
11565
96b79293 11566 sectp = section->get_bfd_section ();
73869dc2
DE
11567
11568 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11569 bounds of the real section. This is a pretty-rare event, so just
11570 flag an error (easier) instead of a warning and trying to cope. */
11571 if (sectp == NULL
fd361982 11572 || offset + size > bfd_section_size (sectp))
73869dc2 11573 {
d2854d8d 11574 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 11575 " in section %s [in module %s]"),
fd361982 11576 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 11577 objfile_name (per_objfile->objfile));
73869dc2
DE
11578 }
11579
11580 result.virtual_offset = offset;
11581 result.size = size;
11582 return result;
11583}
11584
11585/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11586 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11587 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11588 This is for DWP version 2 files. */
11589
11590static struct dwo_unit *
976ca316 11591create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 11592 struct dwp_file *dwp_file,
73869dc2
DE
11593 uint32_t unit_index,
11594 const char *comp_dir,
11595 ULONGEST signature, int is_debug_types)
11596{
73869dc2
DE
11597 const struct dwp_hash_table *dwp_htab =
11598 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11599 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11600 const char *kind = is_debug_types ? "TU" : "CU";
11601 struct dwo_file *dwo_file;
11602 struct dwo_unit *dwo_unit;
d2854d8d 11603 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 11604 void **dwo_file_slot;
73869dc2
DE
11605 int i;
11606
11607 gdb_assert (dwp_file->version == 2);
11608
6f738b01
SM
11609 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11610 kind, pulongest (unit_index), hex_string (signature),
11611 dwp_file->name);
73869dc2
DE
11612
11613 /* Fetch the section offsets of this DWO unit. */
11614
11615 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11616
11617 for (i = 0; i < dwp_htab->nr_columns; ++i)
11618 {
11619 uint32_t offset = read_4_bytes (dbfd,
11620 dwp_htab->section_pool.v2.offsets
11621 + (((unit_index - 1) * dwp_htab->nr_columns
11622 + i)
11623 * sizeof (uint32_t)));
11624 uint32_t size = read_4_bytes (dbfd,
11625 dwp_htab->section_pool.v2.sizes
11626 + (((unit_index - 1) * dwp_htab->nr_columns
11627 + i)
11628 * sizeof (uint32_t)));
11629
11630 switch (dwp_htab->section_pool.v2.section_ids[i])
11631 {
11632 case DW_SECT_INFO:
11633 case DW_SECT_TYPES:
11634 sections.info_or_types_offset = offset;
11635 sections.info_or_types_size = size;
11636 break;
11637 case DW_SECT_ABBREV:
11638 sections.abbrev_offset = offset;
11639 sections.abbrev_size = size;
11640 break;
11641 case DW_SECT_LINE:
11642 sections.line_offset = offset;
11643 sections.line_size = size;
11644 break;
11645 case DW_SECT_LOC:
11646 sections.loc_offset = offset;
11647 sections.loc_size = size;
11648 break;
11649 case DW_SECT_STR_OFFSETS:
11650 sections.str_offsets_offset = offset;
11651 sections.str_offsets_size = size;
11652 break;
11653 case DW_SECT_MACINFO:
11654 sections.macinfo_offset = offset;
11655 sections.macinfo_size = size;
11656 break;
11657 case DW_SECT_MACRO:
11658 sections.macro_offset = offset;
11659 sections.macro_size = size;
11660 break;
11661 }
11662 }
11663
11664 /* It's easier for the rest of the code if we fake a struct dwo_file and
11665 have dwo_unit "live" in that. At least for now.
11666
11667 The DWP file can be made up of a random collection of CUs and TUs.
11668 However, for each CU + set of TUs that came from the same original DWO
11669 file, we can combine them back into a virtual DWO file to save space
11670 (fewer struct dwo_file objects to allocate). Remember that for really
11671 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11672
791afaa2
TT
11673 std::string virtual_dwo_name =
11674 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11675 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11676 (long) (sections.line_size ? sections.line_offset : 0),
11677 (long) (sections.loc_size ? sections.loc_offset : 0),
11678 (long) (sections.str_offsets_size
11679 ? sections.str_offsets_offset : 0));
73869dc2 11680 /* Can we use an existing virtual DWO file? */
976ca316 11681 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11682 comp_dir);
73869dc2
DE
11683 /* Create one if necessary. */
11684 if (*dwo_file_slot == NULL)
11685 {
6f738b01
SM
11686 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11687 virtual_dwo_name.c_str ());
11688
51ac9db5 11689 dwo_file = new struct dwo_file;
976ca316 11690 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
11691 dwo_file->comp_dir = comp_dir;
11692 dwo_file->sections.abbrev =
d2854d8d
CT
11693 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11694 sections.abbrev_offset,
11695 sections.abbrev_size);
73869dc2 11696 dwo_file->sections.line =
d2854d8d
CT
11697 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11698 sections.line_offset,
11699 sections.line_size);
73869dc2 11700 dwo_file->sections.loc =
d2854d8d
CT
11701 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11702 sections.loc_offset, sections.loc_size);
73869dc2 11703 dwo_file->sections.macinfo =
d2854d8d
CT
11704 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11705 sections.macinfo_offset,
11706 sections.macinfo_size);
73869dc2 11707 dwo_file->sections.macro =
d2854d8d
CT
11708 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11709 sections.macro_offset,
11710 sections.macro_size);
73869dc2 11711 dwo_file->sections.str_offsets =
d2854d8d
CT
11712 create_dwp_v2_or_v5_section (per_objfile,
11713 &dwp_file->sections.str_offsets,
11714 sections.str_offsets_offset,
11715 sections.str_offsets_size);
73869dc2
DE
11716 /* The "str" section is global to the entire DWP file. */
11717 dwo_file->sections.str = dwp_file->sections.str;
11718 /* The info or types section is assigned below to dwo_unit,
11719 there's no need to record it in dwo_file.
11720 Also, we can't simply record type sections in dwo_file because
11721 we record a pointer into the vector in dwo_unit. As we collect more
11722 types we'll grow the vector and eventually have to reallocate space
11723 for it, invalidating all copies of pointers into the previous
11724 contents. */
11725 *dwo_file_slot = dwo_file;
11726 }
11727 else
11728 {
6f738b01
SM
11729 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11730 virtual_dwo_name.c_str ());
11731
9a3c8263 11732 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11733 }
73869dc2 11734
976ca316 11735 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
11736 dwo_unit->dwo_file = dwo_file;
11737 dwo_unit->signature = signature;
8d749320 11738 dwo_unit->section =
976ca316 11739 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 11740 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 11741 (per_objfile,
d2854d8d
CT
11742 is_debug_types
11743 ? &dwp_file->sections.types
11744 : &dwp_file->sections.info,
11745 sections.info_or_types_offset,
11746 sections.info_or_types_size);
11747 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11748
11749 return dwo_unit;
11750}
11751
11752/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11753 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11754 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11755 This is for DWP version 5 files. */
11756
11757static struct dwo_unit *
11758create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11759 struct dwp_file *dwp_file,
11760 uint32_t unit_index,
11761 const char *comp_dir,
11762 ULONGEST signature, int is_debug_types)
11763{
11764 const struct dwp_hash_table *dwp_htab
11765 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11766 bfd *dbfd = dwp_file->dbfd.get ();
11767 const char *kind = is_debug_types ? "TU" : "CU";
11768 struct dwo_file *dwo_file;
11769 struct dwo_unit *dwo_unit;
11770 struct virtual_v2_or_v5_dwo_sections sections {};
11771 void **dwo_file_slot;
11772
11773 gdb_assert (dwp_file->version == 5);
11774
6f738b01
SM
11775 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
11776 kind, pulongest (unit_index), hex_string (signature),
11777 dwp_file->name);
d2854d8d
CT
11778
11779 /* Fetch the section offsets of this DWO unit. */
11780
11781 /* memset (&sections, 0, sizeof (sections)); */
11782
11783 for (int i = 0; i < dwp_htab->nr_columns; ++i)
11784 {
11785 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
11786 dwp_htab->section_pool.v5.offsets
11787 + (((unit_index - 1)
11788 * dwp_htab->nr_columns
11789 + i)
11790 * sizeof (uint32_t)));
d2854d8d 11791 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
11792 dwp_htab->section_pool.v5.sizes
11793 + (((unit_index - 1) * dwp_htab->nr_columns
11794 + i)
11795 * sizeof (uint32_t)));
d2854d8d
CT
11796
11797 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
11798 {
11799 case DW_SECT_ABBREV_V5:
11800 sections.abbrev_offset = offset;
11801 sections.abbrev_size = size;
11802 break;
11803 case DW_SECT_INFO_V5:
11804 sections.info_or_types_offset = offset;
11805 sections.info_or_types_size = size;
11806 break;
11807 case DW_SECT_LINE_V5:
11808 sections.line_offset = offset;
11809 sections.line_size = size;
11810 break;
11811 case DW_SECT_LOCLISTS_V5:
11812 sections.loclists_offset = offset;
11813 sections.loclists_size = size;
11814 break;
11815 case DW_SECT_MACRO_V5:
11816 sections.macro_offset = offset;
11817 sections.macro_size = size;
11818 break;
11819 case DW_SECT_RNGLISTS_V5:
11820 sections.rnglists_offset = offset;
11821 sections.rnglists_size = size;
11822 break;
11823 case DW_SECT_STR_OFFSETS_V5:
11824 sections.str_offsets_offset = offset;
11825 sections.str_offsets_size = size;
11826 break;
11827 case DW_SECT_RESERVED_V5:
11828 default:
11829 break;
11830 }
d2854d8d
CT
11831 }
11832
11833 /* It's easier for the rest of the code if we fake a struct dwo_file and
11834 have dwo_unit "live" in that. At least for now.
11835
11836 The DWP file can be made up of a random collection of CUs and TUs.
11837 However, for each CU + set of TUs that came from the same original DWO
11838 file, we can combine them back into a virtual DWO file to save space
11839 (fewer struct dwo_file objects to allocate). Remember that for really
11840 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11841
11842 std::string virtual_dwo_name =
11843 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
11844 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11845 (long) (sections.line_size ? sections.line_offset : 0),
11846 (long) (sections.loclists_size ? sections.loclists_offset : 0),
11847 (long) (sections.str_offsets_size
11848 ? sections.str_offsets_offset : 0),
11849 (long) (sections.macro_size ? sections.macro_offset : 0),
11850 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
11851 /* Can we use an existing virtual DWO file? */
11852 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
11853 virtual_dwo_name.c_str (),
11854 comp_dir);
d2854d8d
CT
11855 /* Create one if necessary. */
11856 if (*dwo_file_slot == NULL)
11857 {
6f738b01
SM
11858 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11859 virtual_dwo_name.c_str ());
11860
d2854d8d
CT
11861 dwo_file = new struct dwo_file;
11862 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11863 dwo_file->comp_dir = comp_dir;
11864 dwo_file->sections.abbrev =
dda83cd7
SM
11865 create_dwp_v2_or_v5_section (per_objfile,
11866 &dwp_file->sections.abbrev,
11867 sections.abbrev_offset,
11868 sections.abbrev_size);
d2854d8d 11869 dwo_file->sections.line =
dda83cd7
SM
11870 create_dwp_v2_or_v5_section (per_objfile,
11871 &dwp_file->sections.line,
11872 sections.line_offset, sections.line_size);
d2854d8d 11873 dwo_file->sections.macro =
dda83cd7
SM
11874 create_dwp_v2_or_v5_section (per_objfile,
11875 &dwp_file->sections.macro,
11876 sections.macro_offset,
11877 sections.macro_size);
d2854d8d 11878 dwo_file->sections.loclists =
dda83cd7
SM
11879 create_dwp_v2_or_v5_section (per_objfile,
11880 &dwp_file->sections.loclists,
11881 sections.loclists_offset,
11882 sections.loclists_size);
d2854d8d 11883 dwo_file->sections.rnglists =
dda83cd7
SM
11884 create_dwp_v2_or_v5_section (per_objfile,
11885 &dwp_file->sections.rnglists,
11886 sections.rnglists_offset,
11887 sections.rnglists_size);
d2854d8d 11888 dwo_file->sections.str_offsets =
dda83cd7
SM
11889 create_dwp_v2_or_v5_section (per_objfile,
11890 &dwp_file->sections.str_offsets,
11891 sections.str_offsets_offset,
11892 sections.str_offsets_size);
d2854d8d
CT
11893 /* The "str" section is global to the entire DWP file. */
11894 dwo_file->sections.str = dwp_file->sections.str;
11895 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
11896 there's no need to record it in dwo_file.
11897 Also, we can't simply record type sections in dwo_file because
11898 we record a pointer into the vector in dwo_unit. As we collect more
11899 types we'll grow the vector and eventually have to reallocate space
11900 for it, invalidating all copies of pointers into the previous
11901 contents. */
d2854d8d
CT
11902 *dwo_file_slot = dwo_file;
11903 }
11904 else
11905 {
6f738b01
SM
11906 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11907 virtual_dwo_name.c_str ());
11908
d2854d8d
CT
11909 dwo_file = (struct dwo_file *) *dwo_file_slot;
11910 }
11911
11912 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11913 dwo_unit->dwo_file = dwo_file;
11914 dwo_unit->signature = signature;
11915 dwo_unit->section
11916 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11917 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
11918 &dwp_file->sections.info,
11919 sections.info_or_types_offset,
11920 sections.info_or_types_size);
73869dc2
DE
11921 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11922
11923 return dwo_unit;
11924}
11925
57d63ce2
DE
11926/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11927 Returns NULL if the signature isn't found. */
80626a55
DE
11928
11929static struct dwo_unit *
976ca316 11930lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 11931 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11932 ULONGEST signature, int is_debug_types)
80626a55 11933{
57d63ce2
DE
11934 const struct dwp_hash_table *dwp_htab =
11935 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11936 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11937 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11938 uint32_t hash = signature & mask;
11939 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11940 unsigned int i;
11941 void **slot;
870f88f7 11942 struct dwo_unit find_dwo_cu;
80626a55
DE
11943
11944 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11945 find_dwo_cu.signature = signature;
19ac8c2e 11946 slot = htab_find_slot (is_debug_types
48b490f2
TT
11947 ? dwp_file->loaded_tus.get ()
11948 : dwp_file->loaded_cus.get (),
19ac8c2e 11949 &find_dwo_cu, INSERT);
80626a55
DE
11950
11951 if (*slot != NULL)
9a3c8263 11952 return (struct dwo_unit *) *slot;
80626a55
DE
11953
11954 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11955 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11956 {
11957 ULONGEST signature_in_table;
11958
11959 signature_in_table =
57d63ce2 11960 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11961 if (signature_in_table == signature)
11962 {
57d63ce2
DE
11963 uint32_t unit_index =
11964 read_4_bytes (dbfd,
11965 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11966
73869dc2
DE
11967 if (dwp_file->version == 1)
11968 {
976ca316
SM
11969 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
11970 unit_index, comp_dir,
11971 signature, is_debug_types);
73869dc2 11972 }
d2854d8d 11973 else if (dwp_file->version == 2)
73869dc2 11974 {
976ca316
SM
11975 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
11976 unit_index, comp_dir,
11977 signature, is_debug_types);
73869dc2 11978 }
d2854d8d
CT
11979 else /* version == 5 */
11980 {
11981 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11982 unit_index, comp_dir,
11983 signature, is_debug_types);
11984 }
9a3c8263 11985 return (struct dwo_unit *) *slot;
80626a55
DE
11986 }
11987 if (signature_in_table == 0)
11988 return NULL;
11989 hash = (hash + hash2) & mask;
11990 }
11991
11992 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11993 " [in module %s]"),
11994 dwp_file->name);
11995}
11996
ab5088bf 11997/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11998 Open the file specified by FILE_NAME and hand it off to BFD for
11999 preliminary analysis. Return a newly initialized bfd *, which
12000 includes a canonicalized copy of FILE_NAME.
80626a55 12001 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12002 SEARCH_CWD is true if the current directory is to be searched.
12003 It will be searched before debug-file-directory.
13aaf454
DE
12004 If successful, the file is added to the bfd include table of the
12005 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12006 If unable to find/open the file, return NULL.
3019eac3
DE
12007 NOTE: This function is derived from symfile_bfd_open. */
12008
192b62ce 12009static gdb_bfd_ref_ptr
976ca316 12010try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12011 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12012{
24b9144d 12013 int desc;
9c02c129
DE
12014 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12015 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12016 to debug_file_directory. */
e0cc99a6 12017 const char *search_path;
9c02c129
DE
12018 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12019
e0cc99a6 12020 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12021 if (search_cwd)
12022 {
12023 if (*debug_file_directory != '\0')
e0cc99a6
TT
12024 {
12025 search_path_holder.reset (concat (".", dirname_separator_string,
12026 debug_file_directory,
12027 (char *) NULL));
12028 search_path = search_path_holder.get ();
12029 }
6ac97d4c 12030 else
e0cc99a6 12031 search_path = ".";
6ac97d4c 12032 }
9c02c129 12033 else
e0cc99a6 12034 search_path = debug_file_directory;
3019eac3 12035
56d467f4
CT
12036 /* Add the path for the executable binary to the list of search paths. */
12037 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12038 search_path_holder.reset (concat (objfile_dir.c_str (),
12039 dirname_separator_string,
12040 search_path, nullptr));
12041 search_path = search_path_holder.get ();
12042
24b9144d 12043 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12044 if (is_dwp)
12045 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12046
12047 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12048 desc = openp (search_path, flags, file_name,
3019eac3
DE
12049 O_RDONLY | O_BINARY, &absolute_name);
12050 if (desc < 0)
12051 return NULL;
12052
e0cc99a6
TT
12053 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12054 gnutarget, desc));
9c02c129
DE
12055 if (sym_bfd == NULL)
12056 return NULL;
192b62ce 12057 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12058
192b62ce
TT
12059 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12060 return NULL;
3019eac3 12061
13aaf454
DE
12062 /* Success. Record the bfd as having been included by the objfile's bfd.
12063 This is important because things like demangled_names_hash lives in the
12064 objfile's per_bfd space and may have references to things like symbol
12065 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12066 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12067
3019eac3
DE
12068 return sym_bfd;
12069}
12070
ab5088bf 12071/* Try to open DWO file FILE_NAME.
3019eac3
DE
12072 COMP_DIR is the DW_AT_comp_dir attribute.
12073 The result is the bfd handle of the file.
12074 If there is a problem finding or opening the file, return NULL.
12075 Upon success, the canonicalized path of the file is stored in the bfd,
12076 same as symfile_bfd_open. */
12077
192b62ce 12078static gdb_bfd_ref_ptr
976ca316 12079open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12080 const char *file_name, const char *comp_dir)
3019eac3 12081{
80626a55 12082 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12083 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12084 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12085
12086 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12087
12088 if (comp_dir != NULL)
12089 {
43816ebc
TT
12090 gdb::unique_xmalloc_ptr<char> path_to_try
12091 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12092
12093 /* NOTE: If comp_dir is a relative path, this will also try the
12094 search path, which seems useful. */
976ca316 12095 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12096 0 /*is_dwp*/,
192b62ce 12097 1 /*search_cwd*/));
3019eac3
DE
12098 if (abfd != NULL)
12099 return abfd;
12100 }
12101
12102 /* That didn't work, try debug-file-directory, which, despite its name,
12103 is a list of paths. */
12104
12105 if (*debug_file_directory == '\0')
12106 return NULL;
12107
976ca316 12108 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12109 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12110}
12111
80626a55
DE
12112/* This function is mapped across the sections and remembers the offset and
12113 size of each of the DWO debugging sections we are interested in. */
12114
12115static void
5bb6e9dd
TT
12116dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12117 dwo_sections *dwo_sections)
80626a55 12118{
80626a55
DE
12119 const struct dwop_section_names *names = &dwop_section_names;
12120
fbedd546 12121 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12122 {
049412e3 12123 dwo_sections->abbrev.s.section = sectp;
fd361982 12124 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12125 }
fbedd546 12126 else if (names->info_dwo.matches (sectp->name))
80626a55 12127 {
049412e3 12128 dwo_sections->info.s.section = sectp;
fd361982 12129 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12130 }
fbedd546 12131 else if (names->line_dwo.matches (sectp->name))
80626a55 12132 {
049412e3 12133 dwo_sections->line.s.section = sectp;
fd361982 12134 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12135 }
fbedd546 12136 else if (names->loc_dwo.matches (sectp->name))
80626a55 12137 {
049412e3 12138 dwo_sections->loc.s.section = sectp;
fd361982 12139 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12140 }
fbedd546 12141 else if (names->loclists_dwo.matches (sectp->name))
41144253 12142 {
12143 dwo_sections->loclists.s.section = sectp;
12144 dwo_sections->loclists.size = bfd_section_size (sectp);
12145 }
fbedd546 12146 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12147 {
049412e3 12148 dwo_sections->macinfo.s.section = sectp;
fd361982 12149 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12150 }
fbedd546 12151 else if (names->macro_dwo.matches (sectp->name))
80626a55 12152 {
049412e3 12153 dwo_sections->macro.s.section = sectp;
fd361982 12154 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12155 }
fbedd546 12156 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12157 {
12158 dwo_sections->rnglists.s.section = sectp;
12159 dwo_sections->rnglists.size = bfd_section_size (sectp);
12160 }
fbedd546 12161 else if (names->str_dwo.matches (sectp->name))
80626a55 12162 {
049412e3 12163 dwo_sections->str.s.section = sectp;
fd361982 12164 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12165 }
fbedd546 12166 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12167 {
049412e3 12168 dwo_sections->str_offsets.s.section = sectp;
fd361982 12169 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12170 }
fbedd546 12171 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12172 {
12173 struct dwarf2_section_info type_section;
12174
12175 memset (&type_section, 0, sizeof (type_section));
049412e3 12176 type_section.s.section = sectp;
fd361982 12177 type_section.size = bfd_section_size (sectp);
fd5866f6 12178 dwo_sections->types.push_back (type_section);
80626a55
DE
12179 }
12180}
12181
ab5088bf 12182/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12183 by PER_CU. This is for the non-DWP case.
80626a55 12184 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12185
12186static struct dwo_file *
4ab09049
SM
12187open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12188 const char *comp_dir)
3019eac3 12189{
976ca316 12190 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12191
976ca316 12192 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12193 if (dbfd == NULL)
12194 {
6f738b01
SM
12195 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12196
80626a55
DE
12197 return NULL;
12198 }
263db9a1 12199
51ac9db5 12200 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12201 dwo_file->dwo_name = dwo_name;
12202 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12203 dwo_file->dbfd = std::move (dbfd);
3019eac3 12204
5bb6e9dd
TT
12205 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12206 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12207 &dwo_file->sections);
3019eac3 12208
976ca316
SM
12209 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12210 dwo_file->cus);
3019eac3 12211
d2854d8d
CT
12212 if (cu->per_cu->dwarf_version < 5)
12213 {
12214 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12215 dwo_file->sections.types, dwo_file->tus);
12216 }
12217 else
12218 {
12219 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12220 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12221 rcuh_kind::COMPILE);
d2854d8d 12222 }
3019eac3 12223
6f738b01 12224 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12225
263db9a1 12226 return dwo_file.release ();
3019eac3
DE
12227}
12228
80626a55 12229/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12230 size of each of the DWP debugging sections common to version 1 and 2 that
12231 we are interested in. */
3019eac3 12232
80626a55 12233static void
73869dc2 12234dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12235 dwp_file *dwp_file)
3019eac3 12236{
80626a55
DE
12237 const struct dwop_section_names *names = &dwop_section_names;
12238 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12239
80626a55 12240 /* Record the ELF section number for later lookup: this is what the
73869dc2 12241 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12242 gdb_assert (elf_section_nr < dwp_file->num_sections);
12243 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12244
80626a55 12245 /* Look for specific sections that we need. */
fbedd546 12246 if (names->str_dwo.matches (sectp->name))
80626a55 12247 {
049412e3 12248 dwp_file->sections.str.s.section = sectp;
fd361982 12249 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12250 }
fbedd546 12251 else if (names->cu_index.matches (sectp->name))
80626a55 12252 {
049412e3 12253 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12254 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12255 }
fbedd546 12256 else if (names->tu_index.matches (sectp->name))
80626a55 12257 {
049412e3 12258 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12259 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12260 }
12261}
3019eac3 12262
73869dc2
DE
12263/* This function is mapped across the sections and remembers the offset and
12264 size of each of the DWP version 2 debugging sections that we are interested
12265 in. This is split into a separate function because we don't know if we
d2854d8d 12266 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12267
12268static void
12269dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12270{
9a3c8263 12271 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12272 const struct dwop_section_names *names = &dwop_section_names;
12273 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12274
12275 /* Record the ELF section number for later lookup: this is what the
12276 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12277 gdb_assert (elf_section_nr < dwp_file->num_sections);
12278 dwp_file->elf_sections[elf_section_nr] = sectp;
12279
12280 /* Look for specific sections that we need. */
fbedd546 12281 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12282 {
049412e3 12283 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12284 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12285 }
fbedd546 12286 else if (names->info_dwo.matches (sectp->name))
73869dc2 12287 {
049412e3 12288 dwp_file->sections.info.s.section = sectp;
fd361982 12289 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12290 }
fbedd546 12291 else if (names->line_dwo.matches (sectp->name))
73869dc2 12292 {
049412e3 12293 dwp_file->sections.line.s.section = sectp;
fd361982 12294 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 12295 }
fbedd546 12296 else if (names->loc_dwo.matches (sectp->name))
73869dc2 12297 {
049412e3 12298 dwp_file->sections.loc.s.section = sectp;
fd361982 12299 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 12300 }
fbedd546 12301 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 12302 {
049412e3 12303 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12304 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 12305 }
fbedd546 12306 else if (names->macro_dwo.matches (sectp->name))
73869dc2 12307 {
049412e3 12308 dwp_file->sections.macro.s.section = sectp;
fd361982 12309 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 12310 }
fbedd546 12311 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 12312 {
049412e3 12313 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12314 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 12315 }
fbedd546 12316 else if (names->types_dwo.matches (sectp->name))
73869dc2 12317 {
049412e3 12318 dwp_file->sections.types.s.section = sectp;
fd361982 12319 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12320 }
12321}
12322
d2854d8d
CT
12323/* This function is mapped across the sections and remembers the offset and
12324 size of each of the DWP version 5 debugging sections that we are interested
12325 in. This is split into a separate function because we don't know if we
12326 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12327
12328static void
12329dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12330{
12331 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12332 const struct dwop_section_names *names = &dwop_section_names;
12333 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12334
12335 /* Record the ELF section number for later lookup: this is what the
12336 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12337 gdb_assert (elf_section_nr < dwp_file->num_sections);
12338 dwp_file->elf_sections[elf_section_nr] = sectp;
12339
12340 /* Look for specific sections that we need. */
fbedd546 12341 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
12342 {
12343 dwp_file->sections.abbrev.s.section = sectp;
12344 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12345 }
fbedd546 12346 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
12347 {
12348 dwp_file->sections.info.s.section = sectp;
12349 dwp_file->sections.info.size = bfd_section_size (sectp);
12350 }
fbedd546 12351 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
12352 {
12353 dwp_file->sections.line.s.section = sectp;
12354 dwp_file->sections.line.size = bfd_section_size (sectp);
12355 }
fbedd546 12356 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
12357 {
12358 dwp_file->sections.loclists.s.section = sectp;
12359 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12360 }
fbedd546 12361 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
12362 {
12363 dwp_file->sections.macro.s.section = sectp;
12364 dwp_file->sections.macro.size = bfd_section_size (sectp);
12365 }
fbedd546 12366 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
12367 {
12368 dwp_file->sections.rnglists.s.section = sectp;
12369 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12370 }
fbedd546 12371 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
12372 {
12373 dwp_file->sections.str_offsets.s.section = sectp;
12374 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12375 }
12376}
12377
80626a55 12378/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12379
80626a55
DE
12380static hashval_t
12381hash_dwp_loaded_cutus (const void *item)
12382{
9a3c8263 12383 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12384
80626a55
DE
12385 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12386 return dwo_unit->signature;
3019eac3
DE
12387}
12388
80626a55 12389/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12390
80626a55
DE
12391static int
12392eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12393{
9a3c8263
SM
12394 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12395 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12396
80626a55
DE
12397 return dua->signature == dub->signature;
12398}
3019eac3 12399
80626a55 12400/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12401
48b490f2 12402static htab_up
298e9637 12403allocate_dwp_loaded_cutus_table ()
80626a55 12404{
48b490f2
TT
12405 return htab_up (htab_create_alloc (3,
12406 hash_dwp_loaded_cutus,
12407 eq_dwp_loaded_cutus,
12408 NULL, xcalloc, xfree));
80626a55 12409}
3019eac3 12410
ab5088bf
DE
12411/* Try to open DWP file FILE_NAME.
12412 The result is the bfd handle of the file.
12413 If there is a problem finding or opening the file, return NULL.
12414 Upon success, the canonicalized path of the file is stored in the bfd,
12415 same as symfile_bfd_open. */
12416
192b62ce 12417static gdb_bfd_ref_ptr
976ca316 12418open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12419{
976ca316 12420 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12421 1 /*is_dwp*/,
192b62ce 12422 1 /*search_cwd*/));
6ac97d4c
DE
12423 if (abfd != NULL)
12424 return abfd;
12425
12426 /* Work around upstream bug 15652.
12427 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12428 [Whether that's a "bug" is debatable, but it is getting in our way.]
12429 We have no real idea where the dwp file is, because gdb's realpath-ing
12430 of the executable's path may have discarded the needed info.
12431 [IWBN if the dwp file name was recorded in the executable, akin to
12432 .gnu_debuglink, but that doesn't exist yet.]
12433 Strip the directory from FILE_NAME and search again. */
12434 if (*debug_file_directory != '\0')
12435 {
12436 /* Don't implicitly search the current directory here.
12437 If the user wants to search "." to handle this case,
12438 it must be added to debug-file-directory. */
976ca316
SM
12439 return try_open_dwop_file (per_objfile, lbasename (file_name),
12440 1 /*is_dwp*/,
6ac97d4c
DE
12441 0 /*search_cwd*/);
12442 }
12443
12444 return NULL;
ab5088bf
DE
12445}
12446
80626a55
DE
12447/* Initialize the use of the DWP file for the current objfile.
12448 By convention the name of the DWP file is ${objfile}.dwp.
12449 The result is NULL if it can't be found. */
a766d390 12450
400174b1 12451static std::unique_ptr<struct dwp_file>
976ca316 12452open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12453{
976ca316 12454 struct objfile *objfile = per_objfile->objfile;
80626a55 12455
82bf32bc
JK
12456 /* Try to find first .dwp for the binary file before any symbolic links
12457 resolving. */
6c447423
DE
12458
12459 /* If the objfile is a debug file, find the name of the real binary
12460 file and get the name of dwp file from there. */
d721ba37 12461 std::string dwp_name;
6c447423
DE
12462 if (objfile->separate_debug_objfile_backlink != NULL)
12463 {
12464 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12465 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12466
d721ba37 12467 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12468 }
12469 else
d721ba37
PA
12470 dwp_name = objfile->original_name;
12471
12472 dwp_name += ".dwp";
80626a55 12473
976ca316 12474 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12475 if (dbfd == NULL
12476 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12477 {
12478 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12479 dwp_name = objfile_name (objfile);
12480 dwp_name += ".dwp";
976ca316 12481 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12482 }
12483
80626a55
DE
12484 if (dbfd == NULL)
12485 {
6f738b01
SM
12486 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12487
400174b1 12488 return std::unique_ptr<dwp_file> ();
3019eac3 12489 }
400174b1
TT
12490
12491 const char *name = bfd_get_filename (dbfd.get ());
12492 std::unique_ptr<struct dwp_file> dwp_file
12493 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12494
0a0f4c01 12495 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12496 dwp_file->elf_sections =
976ca316 12497 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12498 dwp_file->num_sections, asection *);
12499
5bb6e9dd
TT
12500 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12501 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12502 dwp_file.get ());
80626a55 12503
976ca316 12504 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12505
976ca316 12506 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12507
73869dc2 12508 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12509 if (dwp_file->cus && dwp_file->tus
12510 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12511 {
12512 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12513 pretty bizarre. We use pulongest here because that's the established
4d65956b 12514 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12515 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12516 " TU version %s [in DWP file %s]"),
12517 pulongest (dwp_file->cus->version),
d721ba37 12518 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12519 }
08302ed2
DE
12520
12521 if (dwp_file->cus)
12522 dwp_file->version = dwp_file->cus->version;
12523 else if (dwp_file->tus)
12524 dwp_file->version = dwp_file->tus->version;
12525 else
12526 dwp_file->version = 2;
73869dc2 12527
5bb6e9dd
TT
12528 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12529 {
12530 if (dwp_file->version == 2)
12531 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12532 dwp_file.get ());
12533 else
12534 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12535 dwp_file.get ());
12536 }
73869dc2 12537
298e9637
SM
12538 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12539 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12540
6f738b01
SM
12541 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12542 dwarf_read_debug_printf (" %s CUs, %s TUs",
12543 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12544 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12545
12546 return dwp_file;
3019eac3 12547}
c906108c 12548
ab5088bf
DE
12549/* Wrapper around open_and_init_dwp_file, only open it once. */
12550
12551static struct dwp_file *
976ca316 12552get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12553{
976ca316 12554 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12555 {
976ca316
SM
12556 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12557 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12558 }
976ca316 12559 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12560}
12561
80626a55
DE
12562/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12563 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12564 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12565 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12566 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12567
12568 This is called, for example, when wanting to read a variable with a
12569 complex location. Therefore we don't want to do file i/o for every call.
12570 Therefore we don't want to look for a DWO file on every call.
12571 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12572 then we check if we've already seen DWO_NAME, and only THEN do we check
12573 for a DWO file.
12574
1c658ad5 12575 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12576 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12577
3019eac3 12578static struct dwo_unit *
4ab09049 12579lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12580 ULONGEST signature, int is_debug_types)
3019eac3 12581{
976ca316
SM
12582 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12583 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12584 const char *kind = is_debug_types ? "TU" : "CU";
12585 void **dwo_file_slot;
3019eac3 12586 struct dwo_file *dwo_file;
80626a55 12587 struct dwp_file *dwp_file;
cb1df416 12588
6a506a2d
DE
12589 /* First see if there's a DWP file.
12590 If we have a DWP file but didn't find the DWO inside it, don't
12591 look for the original DWO file. It makes gdb behave differently
12592 depending on whether one is debugging in the build tree. */
cf2c3c16 12593
976ca316 12594 dwp_file = get_dwp_file (per_objfile);
80626a55 12595 if (dwp_file != NULL)
cf2c3c16 12596 {
80626a55
DE
12597 const struct dwp_hash_table *dwp_htab =
12598 is_debug_types ? dwp_file->tus : dwp_file->cus;
12599
12600 if (dwp_htab != NULL)
12601 {
12602 struct dwo_unit *dwo_cutu =
976ca316
SM
12603 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12604 is_debug_types);
80626a55
DE
12605
12606 if (dwo_cutu != NULL)
12607 {
6f738b01
SM
12608 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12609 kind, hex_string (signature),
12610 host_address_to_string (dwo_cutu));
12611
80626a55
DE
12612 return dwo_cutu;
12613 }
12614 }
12615 }
6a506a2d 12616 else
80626a55 12617 {
6a506a2d 12618 /* No DWP file, look for the DWO file. */
80626a55 12619
976ca316 12620 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 12621 if (*dwo_file_slot == NULL)
80626a55 12622 {
6a506a2d 12623 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12624 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12625 }
6a506a2d 12626 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12627 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12628
6a506a2d 12629 if (dwo_file != NULL)
19c3d4c9 12630 {
6a506a2d
DE
12631 struct dwo_unit *dwo_cutu = NULL;
12632
12633 if (is_debug_types && dwo_file->tus)
12634 {
12635 struct dwo_unit find_dwo_cutu;
12636
12637 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12638 find_dwo_cutu.signature = signature;
9a3c8263 12639 dwo_cutu
b0b6a987
TT
12640 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12641 &find_dwo_cutu);
6a506a2d 12642 }
33c5cd75 12643 else if (!is_debug_types && dwo_file->cus)
80626a55 12644 {
33c5cd75
DB
12645 struct dwo_unit find_dwo_cutu;
12646
12647 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12648 find_dwo_cutu.signature = signature;
b0b6a987 12649 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12650 &find_dwo_cutu);
6a506a2d
DE
12651 }
12652
12653 if (dwo_cutu != NULL)
12654 {
6f738b01
SM
12655 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12656 kind, dwo_name, hex_string (signature),
12657 host_address_to_string (dwo_cutu));
12658
6a506a2d 12659 return dwo_cutu;
80626a55
DE
12660 }
12661 }
2e276125 12662 }
9cdd5dbd 12663
80626a55
DE
12664 /* We didn't find it. This could mean a dwo_id mismatch, or
12665 someone deleted the DWO/DWP file, or the search path isn't set up
12666 correctly to find the file. */
12667
6f738b01
SM
12668 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12669 kind, dwo_name, hex_string (signature));
3019eac3 12670
6656a72d
DE
12671 /* This is a warning and not a complaint because it can be caused by
12672 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12673 {
12674 /* Print the name of the DWP file if we looked there, helps the user
12675 better diagnose the problem. */
791afaa2 12676 std::string dwp_text;
43942612
DE
12677
12678 if (dwp_file != NULL)
791afaa2
TT
12679 dwp_text = string_printf (" [in DWP file %s]",
12680 lbasename (dwp_file->name));
43942612 12681
9d8780f0 12682 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12683 " [in module %s]"),
4ab09049
SM
12684 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12685 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12686 }
3019eac3 12687 return NULL;
5fb290d7
DJ
12688}
12689
80626a55
DE
12690/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12691 See lookup_dwo_cutu_unit for details. */
12692
12693static struct dwo_unit *
4ab09049 12694lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12695 ULONGEST signature)
12696{
4ab09049
SM
12697 gdb_assert (!cu->per_cu->is_debug_types);
12698
12699 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12700}
12701
12702/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12703 See lookup_dwo_cutu_unit for details. */
12704
12705static struct dwo_unit *
4ab09049 12706lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12707{
4ab09049
SM
12708 gdb_assert (cu->per_cu->is_debug_types);
12709
12710 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12711
12712 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12713}
12714
89e63ee4
DE
12715/* Traversal function for queue_and_load_all_dwo_tus. */
12716
12717static int
12718queue_and_load_dwo_tu (void **slot, void *info)
12719{
12720 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12721 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12722 ULONGEST signature = dwo_unit->signature;
d460f660 12723 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12724
12725 if (sig_type != NULL)
12726 {
89e63ee4
DE
12727 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12728 a real dependency of PER_CU on SIG_TYPE. That is detected later
12729 while processing PER_CU. */
0d305d5c
TT
12730 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12731 cu->language))
12732 load_full_type_unit (sig_type, cu->per_objfile);
12733 cu->per_cu->imported_symtabs_push (sig_type);
89e63ee4
DE
12734 }
12735
12736 return 1;
12737}
12738
1b555f17 12739/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12740 The DWO may have the only definition of the type, though it may not be
12741 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12742 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12743
12744static void
1b555f17 12745queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12746{
12747 struct dwo_unit *dwo_unit;
12748 struct dwo_file *dwo_file;
12749
1b555f17
SM
12750 gdb_assert (cu != nullptr);
12751 gdb_assert (!cu->per_cu->is_debug_types);
12752 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12753
1b555f17 12754 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12755 gdb_assert (dwo_unit != NULL);
12756
12757 dwo_file = dwo_unit->dwo_file;
12758 if (dwo_file->tus != NULL)
1b555f17 12759 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
12760}
12761
3019eac3 12762/* Read in various DIEs. */
348e048f 12763
d389af10 12764/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12765 Inherit only the children of the DW_AT_abstract_origin DIE not being
12766 already referenced by DW_AT_abstract_origin from the children of the
12767 current DIE. */
d389af10
JK
12768
12769static void
12770inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12771{
12772 struct die_info *child_die;
791afaa2 12773 sect_offset *offsetp;
d389af10
JK
12774 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12775 struct die_info *origin_die;
12776 /* Iterator of the ORIGIN_DIE children. */
12777 struct die_info *origin_child_die;
d389af10 12778 struct attribute *attr;
cd02d79d
PA
12779 struct dwarf2_cu *origin_cu;
12780 struct pending **origin_previous_list_in_scope;
d389af10
JK
12781
12782 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12783 if (!attr)
12784 return;
12785
cd02d79d
PA
12786 /* Note that following die references may follow to a die in a
12787 different cu. */
12788
12789 origin_cu = cu;
12790 origin_die = follow_die_ref (die, attr, &origin_cu);
12791
12792 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12793 symbols in. */
12794 origin_previous_list_in_scope = origin_cu->list_in_scope;
12795 origin_cu->list_in_scope = cu->list_in_scope;
12796
edb3359d
DJ
12797 if (die->tag != origin_die->tag
12798 && !(die->tag == DW_TAG_inlined_subroutine
12799 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12800 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12801 sect_offset_str (die->sect_off),
12802 sect_offset_str (origin_die->sect_off));
d389af10 12803
34dc0f95
TBA
12804 /* Find if the concrete and abstract trees are structurally the
12805 same. This is a shallow traversal and it is not bullet-proof;
12806 the compiler can trick the debugger into believing that the trees
12807 are isomorphic, whereas they actually are not. However, the
12808 likelyhood of this happening is pretty low, and a full-fledged
12809 check would be an overkill. */
12810 bool are_isomorphic = true;
12811 die_info *concrete_child = die->child;
12812 die_info *abstract_child = origin_die->child;
12813 while (concrete_child != nullptr || abstract_child != nullptr)
12814 {
12815 if (concrete_child == nullptr
12816 || abstract_child == nullptr
12817 || concrete_child->tag != abstract_child->tag)
12818 {
12819 are_isomorphic = false;
12820 break;
12821 }
12822
12823 concrete_child = concrete_child->sibling;
12824 abstract_child = abstract_child->sibling;
12825 }
12826
12827 /* Walk the origin's children in parallel to the concrete children.
12828 This helps match an origin child in case the debug info misses
12829 DW_AT_abstract_origin attributes. Keep in mind that the abstract
12830 origin tree may not have the same tree structure as the concrete
12831 DIE, though. */
12832 die_info *corresponding_abstract_child
12833 = are_isomorphic ? origin_die->child : nullptr;
12834
791afaa2 12835 std::vector<sect_offset> offsets;
d389af10 12836
3ea89b92
PMR
12837 for (child_die = die->child;
12838 child_die && child_die->tag;
436c571c 12839 child_die = child_die->sibling)
3ea89b92
PMR
12840 {
12841 struct die_info *child_origin_die;
12842 struct dwarf2_cu *child_origin_cu;
12843
12844 /* We are trying to process concrete instance entries:
216f72a1 12845 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12846 it's not relevant to our analysis here. i.e. detecting DIEs that are
12847 present in the abstract instance but not referenced in the concrete
12848 one. */
216f72a1 12849 if (child_die->tag == DW_TAG_call_site
dda83cd7 12850 || child_die->tag == DW_TAG_GNU_call_site)
34dc0f95
TBA
12851 {
12852 if (are_isomorphic)
12853 corresponding_abstract_child
12854 = corresponding_abstract_child->sibling;
12855 continue;
12856 }
3ea89b92 12857
c38f313d
DJ
12858 /* For each CHILD_DIE, find the corresponding child of
12859 ORIGIN_DIE. If there is more than one layer of
12860 DW_AT_abstract_origin, follow them all; there shouldn't be,
12861 but GCC versions at least through 4.4 generate this (GCC PR
12862 40573). */
3ea89b92
PMR
12863 child_origin_die = child_die;
12864 child_origin_cu = cu;
c38f313d
DJ
12865 while (1)
12866 {
cd02d79d
PA
12867 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12868 child_origin_cu);
c38f313d
DJ
12869 if (attr == NULL)
12870 break;
cd02d79d
PA
12871 child_origin_die = follow_die_ref (child_origin_die, attr,
12872 &child_origin_cu);
c38f313d
DJ
12873 }
12874
34dc0f95
TBA
12875 /* If missing DW_AT_abstract_origin, try the corresponding child
12876 of the origin. Clang emits such lexical scopes. */
12877 if (child_origin_die == child_die
12878 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
12879 && are_isomorphic
12880 && child_die->tag == DW_TAG_lexical_block)
12881 child_origin_die = corresponding_abstract_child;
12882
d389af10
JK
12883 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12884 counterpart may exist. */
c38f313d 12885 if (child_origin_die != child_die)
d389af10 12886 {
edb3359d
DJ
12887 if (child_die->tag != child_origin_die->tag
12888 && !(child_die->tag == DW_TAG_inlined_subroutine
12889 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12890 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12891 "different tags"),
9d8780f0
SM
12892 sect_offset_str (child_die->sect_off),
12893 sect_offset_str (child_origin_die->sect_off));
c38f313d 12894 if (child_origin_die->parent != origin_die)
b98664d3 12895 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12896 "different parents"),
9d8780f0
SM
12897 sect_offset_str (child_die->sect_off),
12898 sect_offset_str (child_origin_die->sect_off));
c38f313d 12899 else
791afaa2 12900 offsets.push_back (child_origin_die->sect_off);
d389af10 12901 }
34dc0f95
TBA
12902
12903 if (are_isomorphic)
12904 corresponding_abstract_child = corresponding_abstract_child->sibling;
d389af10 12905 }
791afaa2
TT
12906 std::sort (offsets.begin (), offsets.end ());
12907 sect_offset *offsets_end = offsets.data () + offsets.size ();
12908 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12909 if (offsetp[-1] == *offsetp)
b98664d3 12910 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12911 "to DIE %s as their abstract origin"),
12912 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12913
791afaa2 12914 offsetp = offsets.data ();
d389af10
JK
12915 origin_child_die = origin_die->child;
12916 while (origin_child_die && origin_child_die->tag)
12917 {
12918 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12919 while (offsetp < offsets_end
9c541725 12920 && *offsetp < origin_child_die->sect_off)
d389af10 12921 offsetp++;
b64f50a1 12922 if (offsetp >= offsets_end
9c541725 12923 || *offsetp > origin_child_die->sect_off)
d389af10 12924 {
adde2bff
DE
12925 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12926 Check whether we're already processing ORIGIN_CHILD_DIE.
12927 This can happen with mutually referenced abstract_origins.
12928 PR 16581. */
12929 if (!origin_child_die->in_process)
12930 process_die (origin_child_die, origin_cu);
d389af10 12931 }
436c571c 12932 origin_child_die = origin_child_die->sibling;
d389af10 12933 }
cd02d79d 12934 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12935
12936 if (cu != origin_cu)
12937 compute_delayed_physnames (origin_cu);
d389af10
JK
12938}
12939
c906108c 12940static void
e7c27a73 12941read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12942{
5e22e966 12943 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 12944 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 12945 struct context_stack *newobj;
c906108c
SS
12946 CORE_ADDR lowpc;
12947 CORE_ADDR highpc;
12948 struct die_info *child_die;
edb3359d 12949 struct attribute *attr, *call_line, *call_file;
15d034d0 12950 const char *name;
e142c38c 12951 CORE_ADDR baseaddr;
801e3a5b 12952 struct block *block;
edb3359d 12953 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12954 std::vector<struct symbol *> template_args;
34eaf542 12955 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12956
12957 if (inlined_func)
12958 {
12959 /* If we do not have call site information, we can't show the
12960 caller of this inlined function. That's too confusing, so
12961 only use the scope for local variables. */
12962 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12963 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12964 if (call_line == NULL || call_file == NULL)
12965 {
12966 read_lexical_block_scope (die, cu);
12967 return;
12968 }
12969 }
c906108c 12970
b3b3bada 12971 baseaddr = objfile->text_section_offset ();
e142c38c 12972
94af9270 12973 name = dwarf2_name (die, cu);
c906108c 12974
e8d05480
JB
12975 /* Ignore functions with missing or empty names. These are actually
12976 illegal according to the DWARF standard. */
12977 if (name == NULL)
12978 {
b98664d3 12979 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12980 sect_offset_str (die->sect_off));
e8d05480
JB
12981 return;
12982 }
12983
12984 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12985 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12986 <= PC_BOUNDS_INVALID)
e8d05480 12987 {
ae4d0c03 12988 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 12989 if (attr == nullptr || !attr->as_boolean ())
b98664d3 12990 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12991 "for subprogram DIE at %s"),
12992 sect_offset_str (die->sect_off));
e8d05480
JB
12993 return;
12994 }
c906108c 12995
3e29f34a
MR
12996 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12997 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12998
34eaf542
TT
12999 /* If we have any template arguments, then we must allocate a
13000 different sort of symbol. */
436c571c 13001 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13002 {
13003 if (child_die->tag == DW_TAG_template_type_param
13004 || child_die->tag == DW_TAG_template_value_param)
13005 {
8c14c3a3 13006 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13007 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13008 break;
13009 }
13010 }
13011
702cf3f5 13012 gdb_assert (cu->get_builder () != nullptr);
c24bdb02 13013 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13014 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13015 (struct symbol *) templ_func);
4c2df51b 13016
81873cc8 13017 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13018 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13019 cu->language);
13020
4cecd739
DJ
13021 /* If there is a location expression for DW_AT_frame_base, record
13022 it. */
e142c38c 13023 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13024 if (attr != nullptr)
fe978cb0 13025 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13026
63e43d3a
PMR
13027 /* If there is a location for the static link, record it. */
13028 newobj->static_link = NULL;
13029 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13030 if (attr != nullptr)
63e43d3a 13031 {
224c3ddb
SM
13032 newobj->static_link
13033 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13034 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13035 cu->addr_type ());
63e43d3a
PMR
13036 }
13037
c24bdb02 13038 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13039
639d11d3 13040 if (die->child != NULL)
c906108c 13041 {
639d11d3 13042 child_die = die->child;
c906108c
SS
13043 while (child_die && child_die->tag)
13044 {
34eaf542
TT
13045 if (child_die->tag == DW_TAG_template_type_param
13046 || child_die->tag == DW_TAG_template_value_param)
13047 {
13048 struct symbol *arg = new_symbol (child_die, NULL, cu);
13049
f1078f66 13050 if (arg != NULL)
2f4732b0 13051 template_args.push_back (arg);
34eaf542
TT
13052 }
13053 else
13054 process_die (child_die, cu);
436c571c 13055 child_die = child_die->sibling;
c906108c
SS
13056 }
13057 }
13058
d389af10
JK
13059 inherit_abstract_dies (die, cu);
13060
4a811a97
UW
13061 /* If we have a DW_AT_specification, we might need to import using
13062 directives from the context of the specification DIE. See the
13063 comment in determine_prefix. */
13064 if (cu->language == language_cplus
13065 && dwarf2_attr (die, DW_AT_specification, cu))
13066 {
13067 struct dwarf2_cu *spec_cu = cu;
13068 struct die_info *spec_die = die_specification (die, &spec_cu);
13069
13070 while (spec_die)
13071 {
13072 child_die = spec_die->child;
13073 while (child_die && child_die->tag)
13074 {
13075 if (child_die->tag == DW_TAG_imported_module)
13076 process_die (child_die, spec_cu);
436c571c 13077 child_die = child_die->sibling;
4a811a97
UW
13078 }
13079
13080 /* In some cases, GCC generates specification DIEs that
13081 themselves contain DW_AT_specification attributes. */
13082 spec_die = die_specification (spec_die, &spec_cu);
13083 }
13084 }
13085
c24bdb02 13086 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13087 /* Make a block for the local symbols within. */
c24bdb02 13088 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13089 cstk.static_link, lowpc, highpc);
801e3a5b 13090
df8a16a1 13091 /* For C++, set the block's scope. */
45280282
IB
13092 if ((cu->language == language_cplus
13093 || cu->language == language_fortran
c44af4eb
TT
13094 || cu->language == language_d
13095 || cu->language == language_rust)
4d4ec4e5 13096 && cu->processing_has_namespace_info)
195a3f6c
TT
13097 block_set_scope (block, determine_prefix (die, cu),
13098 &objfile->objfile_obstack);
df8a16a1 13099
801e3a5b
JB
13100 /* If we have address ranges, record them. */
13101 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13102
a60f3166 13103 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13104
34eaf542 13105 /* Attach template arguments to function. */
2f4732b0 13106 if (!template_args.empty ())
34eaf542
TT
13107 {
13108 gdb_assert (templ_func != NULL);
13109
2f4732b0 13110 templ_func->n_template_arguments = template_args.size ();
34eaf542 13111 templ_func->template_arguments
dda83cd7 13112 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13113 templ_func->n_template_arguments);
34eaf542 13114 memcpy (templ_func->template_arguments,
2f4732b0 13115 template_args.data (),
34eaf542 13116 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13117
13118 /* Make sure that the symtab is set on the new symbols. Even
13119 though they don't appear in this symtab directly, other parts
13120 of gdb assume that symbols do, and this is reasonably
13121 true. */
8634679f 13122 for (symbol *sym : template_args)
3e1d3d8c 13123 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13124 }
13125
208d8187
JB
13126 /* In C++, we can have functions nested inside functions (e.g., when
13127 a function declares a class that has methods). This means that
13128 when we finish processing a function scope, we may need to go
13129 back to building a containing block's symbol lists. */
c24bdb02
KS
13130 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13131 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13132
921e78cf
JB
13133 /* If we've finished processing a top-level function, subsequent
13134 symbols go in the file symbol list. */
c24bdb02
KS
13135 if (cu->get_builder ()->outermost_context_p ())
13136 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13137}
13138
13139/* Process all the DIES contained within a lexical block scope. Start
13140 a new scope, process the dies, and then close the scope. */
13141
13142static void
e7c27a73 13143read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13144{
5e22e966 13145 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13146 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13147 CORE_ADDR lowpc, highpc;
13148 struct die_info *child_die;
e142c38c
DJ
13149 CORE_ADDR baseaddr;
13150
b3b3bada 13151 baseaddr = objfile->text_section_offset ();
c906108c
SS
13152
13153 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13154 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13155 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13156 be nasty. Might be easier to properly extend generic blocks to
af34e669 13157 describe ranges. */
e385593e
JK
13158 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13159 {
13160 case PC_BOUNDS_NOT_PRESENT:
13161 /* DW_TAG_lexical_block has no attributes, process its children as if
13162 there was no wrapping by that DW_TAG_lexical_block.
13163 GCC does no longer produces such DWARF since GCC r224161. */
13164 for (child_die = die->child;
13165 child_die != NULL && child_die->tag;
436c571c 13166 child_die = child_die->sibling)
4f7bc5ed
TT
13167 {
13168 /* We might already be processing this DIE. This can happen
13169 in an unusual circumstance -- where a subroutine A
13170 appears lexically in another subroutine B, but A actually
13171 inlines B. The recursion is broken here, rather than in
13172 inherit_abstract_dies, because it seems better to simply
13173 drop concrete children here. */
13174 if (!child_die->in_process)
13175 process_die (child_die, cu);
13176 }
e385593e
JK
13177 return;
13178 case PC_BOUNDS_INVALID:
13179 return;
13180 }
3e29f34a
MR
13181 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13182 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13183
c24bdb02 13184 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13185 if (die->child != NULL)
c906108c 13186 {
639d11d3 13187 child_die = die->child;
c906108c
SS
13188 while (child_die && child_die->tag)
13189 {
e7c27a73 13190 process_die (child_die, cu);
436c571c 13191 child_die = child_die->sibling;
c906108c
SS
13192 }
13193 }
3ea89b92 13194 inherit_abstract_dies (die, cu);
c24bdb02 13195 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13196
c24bdb02
KS
13197 if (*cu->get_builder ()->get_local_symbols () != NULL
13198 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13199 {
801e3a5b 13200 struct block *block
dda83cd7 13201 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13202 cstk.start_addr, highpc);
801e3a5b
JB
13203
13204 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13205 do here, means that recording a parent's ranges entails
13206 walking across all its children's ranges as they appear in
13207 the address map, which is quadratic behavior.
13208
13209 It would be nicer to record the parent's ranges before
13210 traversing its children, simply overriding whatever you find
13211 there. But since we don't even decide whether to create a
13212 block until after we've traversed its children, that's hard
13213 to do. */
801e3a5b 13214 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13215 }
c24bdb02
KS
13216 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13217 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13218}
13219
216f72a1 13220/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13221
13222static void
13223read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13224{
5e22e966 13225 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13226 struct objfile *objfile = per_objfile->objfile;
08feed99 13227 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13228 CORE_ADDR pc, baseaddr;
13229 struct attribute *attr;
13230 struct call_site *call_site, call_site_local;
13231 void **slot;
13232 int nparams;
13233 struct die_info *child_die;
13234
b3b3bada 13235 baseaddr = objfile->text_section_offset ();
96408a79 13236
216f72a1
JK
13237 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13238 if (attr == NULL)
13239 {
13240 /* This was a pre-DWARF-5 GNU extension alias
13241 for DW_AT_call_return_pc. */
13242 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13243 }
96408a79
SA
13244 if (!attr)
13245 {
b98664d3 13246 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13247 "DIE %s [in module %s]"),
13248 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13249 return;
13250 }
95f982e5 13251 pc = attr->as_address () + baseaddr;
3e29f34a 13252 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13253
13254 if (cu->call_site_htab == NULL)
13255 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13256 NULL, &objfile->objfile_obstack,
13257 hashtab_obstack_allocate, NULL);
13258 call_site_local.pc = pc;
13259 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13260 if (*slot != NULL)
13261 {
b98664d3 13262 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13263 "DIE %s [in module %s]"),
13264 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13265 objfile_name (objfile));
96408a79
SA
13266 return;
13267 }
13268
13269 /* Count parameters at the caller. */
13270
13271 nparams = 0;
13272 for (child_die = die->child; child_die && child_die->tag;
436c571c 13273 child_die = child_die->sibling)
96408a79 13274 {
216f72a1 13275 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13276 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13277 {
b98664d3 13278 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13279 "DW_TAG_call_site child DIE %s [in module %s]"),
13280 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13281 objfile_name (objfile));
96408a79
SA
13282 continue;
13283 }
13284
13285 nparams++;
13286 }
13287
224c3ddb
SM
13288 call_site
13289 = ((struct call_site *)
13290 obstack_alloc (&objfile->objfile_obstack,
13291 sizeof (*call_site)
13292 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13293 *slot = call_site;
13294 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13295 call_site->pc = pc;
13296
216f72a1
JK
13297 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13298 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13299 {
13300 struct die_info *func_die;
13301
13302 /* Skip also over DW_TAG_inlined_subroutine. */
13303 for (func_die = die->parent;
13304 func_die && func_die->tag != DW_TAG_subprogram
13305 && func_die->tag != DW_TAG_subroutine_type;
13306 func_die = func_die->parent);
13307
216f72a1
JK
13308 /* DW_AT_call_all_calls is a superset
13309 of DW_AT_call_all_tail_calls. */
96408a79 13310 if (func_die
dda83cd7
SM
13311 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13312 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13313 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13314 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13315 {
13316 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13317 not complete. But keep CALL_SITE for look ups via call_site_htab,
13318 both the initial caller containing the real return address PC and
13319 the final callee containing the current PC of a chain of tail
13320 calls do not need to have the tail call list complete. But any
13321 function candidate for a virtual tail call frame searched via
13322 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13323 determined unambiguously. */
13324 }
13325 else
13326 {
13327 struct type *func_type = NULL;
13328
13329 if (func_die)
13330 func_type = get_die_type (func_die, cu);
13331 if (func_type != NULL)
13332 {
78134374 13333 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13334
13335 /* Enlist this call site to the function. */
13336 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13337 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13338 }
13339 else
b98664d3 13340 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13341 "DIE %s [in module %s]"),
13342 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13343 }
13344 }
13345
216f72a1
JK
13346 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13347 if (attr == NULL)
13348 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13349 if (attr == NULL)
13350 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13351 if (attr == NULL)
216f72a1
JK
13352 {
13353 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13354 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13355 }
96408a79 13356 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13357 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13358 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13359 else if (attr->form_is_block ())
96408a79
SA
13360 {
13361 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13362 struct dwarf_block *block = attr->as_block ();
96408a79 13363
8d749320 13364 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13365 dlbaton->data = block->data;
13366 dlbaton->size = block->size;
a50264ba 13367 dlbaton->per_objfile = per_objfile;
96408a79
SA
13368 dlbaton->per_cu = cu->per_cu;
13369
13370 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13371 }
cd6c91b4 13372 else if (attr->form_is_ref ())
96408a79 13373 {
96408a79
SA
13374 struct dwarf2_cu *target_cu = cu;
13375 struct die_info *target_die;
13376
ac9ec31b 13377 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13378 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13379 if (die_is_declaration (target_die, target_cu))
13380 {
7d45c7c3 13381 const char *target_physname;
9112db09
JK
13382
13383 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13384 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13385 if (target_physname == NULL)
9112db09 13386 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13387 if (target_physname == NULL)
b98664d3 13388 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13389 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 13390 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13391 else
7d455152 13392 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13393 }
13394 else
13395 {
13396 CORE_ADDR lowpc;
13397
13398 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13399 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13400 <= PC_BOUNDS_INVALID)
b98664d3 13401 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13402 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 13403 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13404 else
3e29f34a
MR
13405 {
13406 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13407 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13408 }
96408a79
SA
13409 }
13410 }
13411 else
b98664d3 13412 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13413 "block nor reference, for DIE %s [in module %s]"),
13414 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13415
13416 call_site->per_cu = cu->per_cu;
9f47c707 13417 call_site->per_objfile = per_objfile;
96408a79
SA
13418
13419 for (child_die = die->child;
13420 child_die && child_die->tag;
436c571c 13421 child_die = child_die->sibling)
96408a79 13422 {
96408a79 13423 struct call_site_parameter *parameter;
1788b2d3 13424 struct attribute *loc, *origin;
96408a79 13425
216f72a1 13426 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13427 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13428 {
13429 /* Already printed the complaint above. */
13430 continue;
13431 }
13432
13433 gdb_assert (call_site->parameter_count < nparams);
13434 parameter = &call_site->parameter[call_site->parameter_count];
13435
1788b2d3
JK
13436 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13437 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13438 register is contained in DW_AT_call_value. */
96408a79 13439
24c5c679 13440 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13441 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13442 if (origin == NULL)
13443 {
13444 /* This was a pre-DWARF-5 GNU extension alias
13445 for DW_AT_call_parameter. */
13446 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13447 }
cd6c91b4 13448 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13449 {
1788b2d3 13450 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13451
0826b30a 13452 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13453 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13454 {
13455 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13456 binding can be done only inside one CU. Such referenced DIE
13457 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13458 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13459 "DW_TAG_call_site child DIE %s [in module %s]"),
13460 sect_offset_str (child_die->sect_off),
9c541725 13461 objfile_name (objfile));
d76b7dbc
JK
13462 continue;
13463 }
9c541725
PA
13464 parameter->u.param_cu_off
13465 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13466 }
4fc6c0d5 13467 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13468 {
b98664d3 13469 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13470 "DW_TAG_call_site child DIE %s [in module %s]"),
13471 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13472 continue;
13473 }
24c5c679 13474 else
96408a79 13475 {
9d2246fc
TT
13476 struct dwarf_block *block = loc->as_block ();
13477
24c5c679 13478 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 13479 (block->data, &block->data[block->size]);
24c5c679
JK
13480 if (parameter->u.dwarf_reg != -1)
13481 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
13482 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13483 &block->data[block->size],
24c5c679
JK
13484 &parameter->u.fb_offset))
13485 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13486 else
13487 {
b98664d3 13488 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13489 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13490 "DW_TAG_call_site child DIE %s "
24c5c679 13491 "[in module %s]"),
9d8780f0 13492 sect_offset_str (child_die->sect_off),
9c541725 13493 objfile_name (objfile));
24c5c679
JK
13494 continue;
13495 }
96408a79
SA
13496 }
13497
216f72a1
JK
13498 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13499 if (attr == NULL)
13500 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13501 if (attr == NULL || !attr->form_is_block ())
96408a79 13502 {
b98664d3 13503 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13504 "DW_TAG_call_site child DIE %s [in module %s]"),
13505 sect_offset_str (child_die->sect_off),
9c541725 13506 objfile_name (objfile));
96408a79
SA
13507 continue;
13508 }
9d2246fc
TT
13509
13510 struct dwarf_block *block = attr->as_block ();
13511 parameter->value = block->data;
13512 parameter->value_size = block->size;
96408a79
SA
13513
13514 /* Parameters are not pre-cleared by memset above. */
13515 parameter->data_value = NULL;
13516 parameter->data_value_size = 0;
13517 call_site->parameter_count++;
13518
216f72a1
JK
13519 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13520 if (attr == NULL)
13521 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13522 if (attr != nullptr)
96408a79 13523 {
4fc6c0d5 13524 if (!attr->form_is_block ())
b98664d3 13525 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13526 "DW_TAG_call_site child DIE %s [in module %s]"),
13527 sect_offset_str (child_die->sect_off),
9c541725 13528 objfile_name (objfile));
96408a79
SA
13529 else
13530 {
9d2246fc
TT
13531 block = attr->as_block ();
13532 parameter->data_value = block->data;
13533 parameter->data_value_size = block->size;
96408a79
SA
13534 }
13535 }
13536 }
13537}
13538
71a3c369
TT
13539/* Helper function for read_variable. If DIE represents a virtual
13540 table, then return the type of the concrete object that is
13541 associated with the virtual table. Otherwise, return NULL. */
13542
13543static struct type *
13544rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13545{
13546 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13547 if (attr == NULL)
13548 return NULL;
13549
13550 /* Find the type DIE. */
13551 struct die_info *type_die = NULL;
13552 struct dwarf2_cu *type_cu = cu;
13553
cd6c91b4 13554 if (attr->form_is_ref ())
71a3c369
TT
13555 type_die = follow_die_ref (die, attr, &type_cu);
13556 if (type_die == NULL)
13557 return NULL;
13558
13559 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13560 return NULL;
13561 return die_containing_type (type_die, type_cu);
13562}
13563
13564/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13565
13566static void
13567read_variable (struct die_info *die, struct dwarf2_cu *cu)
13568{
13569 struct rust_vtable_symbol *storage = NULL;
13570
13571 if (cu->language == language_rust)
13572 {
13573 struct type *containing_type = rust_containing_type (die, cu);
13574
13575 if (containing_type != NULL)
13576 {
5e22e966 13577 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13578
8c14c3a3 13579 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13580 storage->concrete_type = containing_type;
cf724bc9 13581 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13582 }
13583 }
13584
e4a62c65
TV
13585 struct symbol *res = new_symbol (die, NULL, cu, storage);
13586 struct attribute *abstract_origin
13587 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13588 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13589 if (res == NULL && loc && abstract_origin)
13590 {
13591 /* We have a variable without a name, but with a location and an abstract
13592 origin. This may be a concrete instance of an abstract variable
13593 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13594 later. */
13595 struct dwarf2_cu *origin_cu = cu;
13596 struct die_info *origin_die
13597 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13598 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13599 per_objfile->per_bfd->abstract_to_concrete
13600 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13601 }
71a3c369
TT
13602}
13603
43988095
JK
13604/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13605 reading .debug_rnglists.
13606 Callback's type should be:
13607 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13608 Return true if the attributes are present and valid, otherwise,
13609 return false. */
13610
13611template <typename Callback>
13612static bool
13613dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 13614 dwarf_tag tag, Callback &&callback)
43988095 13615{
976ca316
SM
13616 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13617 struct objfile *objfile = per_objfile->objfile;
43988095 13618 bfd *obfd = objfile->obfd;
43988095 13619 /* Base address selection entry. */
2b24b6e4 13620 gdb::optional<CORE_ADDR> base;
43988095 13621 const gdb_byte *buffer;
43988095
JK
13622 CORE_ADDR baseaddr;
13623 bool overflow = false;
d0ce17d8
CT
13624 ULONGEST addr_index;
13625 struct dwarf2_section_info *rnglists_section;
43988095 13626
43988095 13627 base = cu->base_address;
d0ce17d8
CT
13628 rnglists_section = cu_debug_rnglists_section (cu, tag);
13629 rnglists_section->read (objfile);
43988095 13630
d0ce17d8 13631 if (offset >= rnglists_section->size)
43988095 13632 {
b98664d3 13633 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13634 offset);
13635 return false;
13636 }
d0ce17d8 13637 buffer = rnglists_section->buffer + offset;
43988095 13638
b3b3bada 13639 baseaddr = objfile->text_section_offset ();
43988095
JK
13640
13641 while (1)
13642 {
7814882a
JK
13643 /* Initialize it due to a false compiler warning. */
13644 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
13645 const gdb_byte *buf_end = (rnglists_section->buffer
13646 + rnglists_section->size);
43988095
JK
13647 unsigned int bytes_read;
13648
13649 if (buffer == buf_end)
13650 {
13651 overflow = true;
13652 break;
13653 }
13654 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13655 switch (rlet)
13656 {
13657 case DW_RLE_end_of_list:
13658 break;
13659 case DW_RLE_base_address:
13660 if (buffer + cu->header.addr_size > buf_end)
13661 {
13662 overflow = true;
13663 break;
13664 }
c8a7a66f 13665 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13666 buffer += bytes_read;
13667 break;
dda83cd7
SM
13668 case DW_RLE_base_addressx:
13669 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13670 buffer += bytes_read;
13671 base = read_addr_index (cu, addr_index);
13672 break;
43988095
JK
13673 case DW_RLE_start_length:
13674 if (buffer + cu->header.addr_size > buf_end)
13675 {
13676 overflow = true;
13677 break;
13678 }
c8a7a66f
TT
13679 range_beginning = cu->header.read_address (obfd, buffer,
13680 &bytes_read);
43988095
JK
13681 buffer += bytes_read;
13682 range_end = (range_beginning
13683 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13684 buffer += bytes_read;
13685 if (buffer > buf_end)
13686 {
13687 overflow = true;
13688 break;
13689 }
13690 break;
d0ce17d8 13691 case DW_RLE_startx_length:
dda83cd7
SM
13692 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13693 buffer += bytes_read;
13694 range_beginning = read_addr_index (cu, addr_index);
13695 if (buffer > buf_end)
13696 {
13697 overflow = true;
13698 break;
13699 }
13700 range_end = (range_beginning
13701 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13702 buffer += bytes_read;
13703 break;
43988095
JK
13704 case DW_RLE_offset_pair:
13705 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13706 buffer += bytes_read;
13707 if (buffer > buf_end)
13708 {
13709 overflow = true;
13710 break;
13711 }
13712 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13713 buffer += bytes_read;
13714 if (buffer > buf_end)
13715 {
13716 overflow = true;
13717 break;
13718 }
13719 break;
13720 case DW_RLE_start_end:
13721 if (buffer + 2 * cu->header.addr_size > buf_end)
13722 {
13723 overflow = true;
13724 break;
13725 }
c8a7a66f
TT
13726 range_beginning = cu->header.read_address (obfd, buffer,
13727 &bytes_read);
43988095 13728 buffer += bytes_read;
c8a7a66f 13729 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13730 buffer += bytes_read;
13731 break;
d0ce17d8 13732 case DW_RLE_startx_endx:
dda83cd7
SM
13733 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13734 buffer += bytes_read;
13735 range_beginning = read_addr_index (cu, addr_index);
13736 if (buffer > buf_end)
13737 {
13738 overflow = true;
13739 break;
13740 }
13741 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13742 buffer += bytes_read;
13743 range_end = read_addr_index (cu, addr_index);
13744 break;
43988095 13745 default:
b98664d3 13746 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13747 return false;
13748 }
13749 if (rlet == DW_RLE_end_of_list || overflow)
13750 break;
13751 if (rlet == DW_RLE_base_address)
13752 continue;
13753
43988095
JK
13754 if (range_beginning > range_end)
13755 {
13756 /* Inverted range entries are invalid. */
b98664d3 13757 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13758 return false;
13759 }
13760
13761 /* Empty range entries have no effect. */
13762 if (range_beginning == range_end)
13763 continue;
13764
d0ce17d8
CT
13765 /* Only DW_RLE_offset_pair needs the base address added. */
13766 if (rlet == DW_RLE_offset_pair)
13767 {
13768 if (!base.has_value ())
13769 {
13770 /* We have no valid base address for the DW_RLE_offset_pair. */
13771 complaint (_("Invalid .debug_rnglists data (no base address for "
13772 "DW_RLE_offset_pair)"));
13773 return false;
13774 }
13775
13776 range_beginning += *base;
13777 range_end += *base;
13778 }
43988095
JK
13779
13780 /* A not-uncommon case of bad debug info.
13781 Don't pollute the addrmap with bad data. */
13782 if (range_beginning + baseaddr == 0
976ca316 13783 && !per_objfile->per_bfd->has_section_at_zero)
43988095 13784 {
b98664d3 13785 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13786 " [in module %s]"), objfile_name (objfile));
13787 continue;
13788 }
13789
13790 callback (range_beginning, range_end);
13791 }
13792
13793 if (overflow)
13794 {
b98664d3 13795 complaint (_("Offset %d is not terminated "
43988095
JK
13796 "for DW_AT_ranges attribute"),
13797 offset);
13798 return false;
13799 }
13800
13801 return true;
13802}
13803
13804/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13805 Callback's type should be:
13806 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13807 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13808
43988095 13809template <typename Callback>
43039443 13810static int
d0ce17d8 13811dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 13812 Callback &&callback)
43039443 13813{
5e22e966
SM
13814 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13815 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13816 struct comp_unit_head *cu_header = &cu->header;
13817 bfd *obfd = objfile->obfd;
13818 unsigned int addr_size = cu_header->addr_size;
13819 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13820 /* Base address selection entry. */
2b24b6e4 13821 gdb::optional<CORE_ADDR> base;
43039443 13822 unsigned int dummy;
d521ce57 13823 const gdb_byte *buffer;
ff013f42 13824 CORE_ADDR baseaddr;
43039443 13825
43988095 13826 if (cu_header->version >= 5)
d0ce17d8 13827 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 13828
d00adf39 13829 base = cu->base_address;
43039443 13830
5e22e966
SM
13831 per_objfile->per_bfd->ranges.read (objfile);
13832 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13833 {
b98664d3 13834 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13835 offset);
13836 return 0;
13837 }
5e22e966 13838 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13839
b3b3bada 13840 baseaddr = objfile->text_section_offset ();
ff013f42 13841
43039443
JK
13842 while (1)
13843 {
13844 CORE_ADDR range_beginning, range_end;
13845
c8a7a66f 13846 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13847 buffer += addr_size;
c8a7a66f 13848 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13849 buffer += addr_size;
13850 offset += 2 * addr_size;
13851
13852 /* An end of list marker is a pair of zero addresses. */
13853 if (range_beginning == 0 && range_end == 0)
13854 /* Found the end of list entry. */
13855 break;
13856
13857 /* Each base address selection entry is a pair of 2 values.
13858 The first is the largest possible address, the second is
13859 the base address. Check for a base address here. */
13860 if ((range_beginning & mask) == mask)
13861 {
28d2bfb9
AB
13862 /* If we found the largest possible address, then we already
13863 have the base address in range_end. */
13864 base = range_end;
43039443
JK
13865 continue;
13866 }
13867
2b24b6e4 13868 if (!base.has_value ())
43039443
JK
13869 {
13870 /* We have no valid base address for the ranges
13871 data. */
b98664d3 13872 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13873 return 0;
13874 }
13875
9277c30c
UW
13876 if (range_beginning > range_end)
13877 {
13878 /* Inverted range entries are invalid. */
b98664d3 13879 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13880 return 0;
13881 }
13882
13883 /* Empty range entries have no effect. */
13884 if (range_beginning == range_end)
13885 continue;
13886
2b24b6e4
TT
13887 range_beginning += *base;
13888 range_end += *base;
43039443 13889
01093045
DE
13890 /* A not-uncommon case of bad debug info.
13891 Don't pollute the addrmap with bad data. */
13892 if (range_beginning + baseaddr == 0
5e22e966 13893 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13894 {
b98664d3 13895 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13896 " [in module %s]"), objfile_name (objfile));
01093045
DE
13897 continue;
13898 }
13899
5f46c5a5
JK
13900 callback (range_beginning, range_end);
13901 }
13902
13903 return 1;
13904}
13905
13906/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13907 Return 1 if the attributes are present and valid, otherwise, return 0.
efd7398e 13908 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
5f46c5a5
JK
13909
13910static int
13911dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13912 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 13913 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 13914{
5e22e966 13915 struct objfile *objfile = cu->per_objfile->objfile;
84685904 13916 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 13917 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13918 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13919 int low_set = 0;
13920 CORE_ADDR low = 0;
13921 CORE_ADDR high = 0;
13922 int retval;
13923
d0ce17d8 13924 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
13925 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13926 {
9277c30c 13927 if (ranges_pst != NULL)
3e29f34a
MR
13928 {
13929 CORE_ADDR lowpc;
13930 CORE_ADDR highpc;
13931
79748972
TT
13932 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13933 range_beginning + baseaddr)
13934 - baseaddr);
13935 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13936 range_end + baseaddr)
13937 - baseaddr);
84685904 13938 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 13939 lowpc, highpc - 1, ranges_pst);
3e29f34a 13940 }
ff013f42 13941
43039443
JK
13942 /* FIXME: This is recording everything as a low-high
13943 segment of consecutive addresses. We should have a
13944 data structure for discontiguous block ranges
13945 instead. */
13946 if (! low_set)
13947 {
13948 low = range_beginning;
13949 high = range_end;
13950 low_set = 1;
13951 }
13952 else
13953 {
13954 if (range_beginning < low)
13955 low = range_beginning;
13956 if (range_end > high)
13957 high = range_end;
13958 }
5f46c5a5
JK
13959 });
13960 if (!retval)
13961 return 0;
43039443
JK
13962
13963 if (! low_set)
13964 /* If the first entry is an end-of-list marker, the range
13965 describes an empty scope, i.e. no instructions. */
13966 return 0;
13967
13968 if (low_return)
13969 *low_return = low;
13970 if (high_return)
13971 *high_return = high;
13972 return 1;
13973}
13974
3a2b436a
JK
13975/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13976 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13977 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13978
3a2b436a 13979static enum pc_bounds_kind
af34e669 13980dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13981 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13982 dwarf2_psymtab *pst)
c906108c 13983{
976ca316 13984 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 13985 struct attribute *attr;
91da1414 13986 struct attribute *attr_high;
af34e669
DJ
13987 CORE_ADDR low = 0;
13988 CORE_ADDR high = 0;
e385593e 13989 enum pc_bounds_kind ret;
c906108c 13990
91da1414
MW
13991 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13992 if (attr_high)
af34e669 13993 {
e142c38c 13994 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13995 if (attr != nullptr)
dda83cd7 13996 {
95f982e5
TT
13997 low = attr->as_address ();
13998 high = attr_high->as_address ();
cd6c91b4 13999 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14000 high += low;
91da1414 14001 }
af34e669
DJ
14002 else
14003 /* Found high w/o low attribute. */
e385593e 14004 return PC_BOUNDS_INVALID;
af34e669
DJ
14005
14006 /* Found consecutive range of addresses. */
3a2b436a 14007 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14008 }
c906108c 14009 else
af34e669 14010 {
e142c38c 14011 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14012 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14013 {
2b0c7f41
SM
14014 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14015 on DWARF version). */
14016 ULONGEST ranges_offset = attr->as_unsigned ();
14017
14018 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14019 this value. */
14020 if (die->tag != DW_TAG_compile_unit)
14021 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14022
af34e669 14023 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14024 .debug_ranges section. */
d0ce17d8
CT
14025 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14026 die->tag))
e385593e 14027 return PC_BOUNDS_INVALID;
43039443 14028 /* Found discontinuous range of addresses. */
3a2b436a 14029 ret = PC_BOUNDS_RANGES;
af34e669 14030 }
e385593e
JK
14031 else
14032 return PC_BOUNDS_NOT_PRESENT;
af34e669 14033 }
c906108c 14034
48fbe735 14035 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14036 if (high <= low)
e385593e 14037 return PC_BOUNDS_INVALID;
c906108c
SS
14038
14039 /* When using the GNU linker, .gnu.linkonce. sections are used to
14040 eliminate duplicate copies of functions and vtables and such.
14041 The linker will arbitrarily choose one and discard the others.
14042 The AT_*_pc values for such functions refer to local labels in
14043 these sections. If the section from that file was discarded, the
14044 labels are not in the output, so the relocs get a value of 0.
14045 If this is a discarded function, mark the pc bounds as invalid,
14046 so that GDB will ignore it. */
976ca316 14047 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14048 return PC_BOUNDS_INVALID;
c906108c
SS
14049
14050 *lowpc = low;
96408a79
SA
14051 if (highpc)
14052 *highpc = high;
af34e669 14053 return ret;
c906108c
SS
14054}
14055
b084d499
JB
14056/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14057 its low and high PC addresses. Do nothing if these addresses could not
14058 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14059 and HIGHPC to the high address if greater than HIGHPC. */
14060
14061static void
14062dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14063 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14064 struct dwarf2_cu *cu)
b084d499
JB
14065{
14066 CORE_ADDR low, high;
14067 struct die_info *child = die->child;
14068
e385593e 14069 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14070 {
325fac50
PA
14071 *lowpc = std::min (*lowpc, low);
14072 *highpc = std::max (*highpc, high);
b084d499
JB
14073 }
14074
14075 /* If the language does not allow nested subprograms (either inside
14076 subprograms or lexical blocks), we're done. */
14077 if (cu->language != language_ada)
14078 return;
6e70227d 14079
b084d499
JB
14080 /* Check all the children of the given DIE. If it contains nested
14081 subprograms, then check their pc bounds. Likewise, we need to
14082 check lexical blocks as well, as they may also contain subprogram
14083 definitions. */
14084 while (child && child->tag)
14085 {
14086 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14087 || child->tag == DW_TAG_lexical_block)
14088 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14089 child = child->sibling;
b084d499
JB
14090 }
14091}
14092
fae299cd
DC
14093/* Get the low and high pc's represented by the scope DIE, and store
14094 them in *LOWPC and *HIGHPC. If the correct values can't be
14095 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14096
14097static void
14098get_scope_pc_bounds (struct die_info *die,
14099 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14100 struct dwarf2_cu *cu)
14101{
14102 CORE_ADDR best_low = (CORE_ADDR) -1;
14103 CORE_ADDR best_high = (CORE_ADDR) 0;
14104 CORE_ADDR current_low, current_high;
14105
3a2b436a 14106 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14107 >= PC_BOUNDS_RANGES)
fae299cd
DC
14108 {
14109 best_low = current_low;
14110 best_high = current_high;
14111 }
14112 else
14113 {
14114 struct die_info *child = die->child;
14115
14116 while (child && child->tag)
14117 {
14118 switch (child->tag) {
14119 case DW_TAG_subprogram:
dda83cd7 14120 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14121 break;
14122 case DW_TAG_namespace:
f55ee35c 14123 case DW_TAG_module:
fae299cd
DC
14124 /* FIXME: carlton/2004-01-16: Should we do this for
14125 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14126 that current GCC's always emit the DIEs corresponding
14127 to definitions of methods of classes as children of a
14128 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14129 the DIEs giving the declarations, which could be
14130 anywhere). But I don't see any reason why the
14131 standards says that they have to be there. */
14132 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14133
14134 if (current_low != ((CORE_ADDR) -1))
14135 {
325fac50
PA
14136 best_low = std::min (best_low, current_low);
14137 best_high = std::max (best_high, current_high);
fae299cd
DC
14138 }
14139 break;
14140 default:
0963b4bd 14141 /* Ignore. */
fae299cd
DC
14142 break;
14143 }
14144
436c571c 14145 child = child->sibling;
fae299cd
DC
14146 }
14147 }
14148
14149 *lowpc = best_low;
14150 *highpc = best_high;
14151}
14152
801e3a5b
JB
14153/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14154 in DIE. */
380bca97 14155
801e3a5b
JB
14156static void
14157dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14158 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14159{
5e22e966 14160 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14161 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14162 struct attribute *attr;
91da1414 14163 struct attribute *attr_high;
801e3a5b 14164
91da1414
MW
14165 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14166 if (attr_high)
801e3a5b 14167 {
801e3a5b 14168 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14169 if (attr != nullptr)
dda83cd7 14170 {
95f982e5
TT
14171 CORE_ADDR low = attr->as_address ();
14172 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14173
cd6c91b4 14174 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14175 high += low;
9a619af0 14176
3e29f34a
MR
14177 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14178 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14179 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14180 }
801e3a5b
JB
14181 }
14182
14183 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14184 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14185 {
2b0c7f41
SM
14186 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14187 on DWARF version). */
14188 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14189
2b0c7f41
SM
14190 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14191 this value. */
14192 if (die->tag != DW_TAG_compile_unit)
14193 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14194
2d5f09ec 14195 std::vector<blockrange> blockvec;
2b0c7f41 14196 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14197 [&] (CORE_ADDR start, CORE_ADDR end)
14198 {
58fdfd2c
JK
14199 start += baseaddr;
14200 end += baseaddr;
5f46c5a5
JK
14201 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14202 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14203 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14204 blockvec.emplace_back (start, end);
5f46c5a5 14205 });
2d5f09ec
KB
14206
14207 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14208 }
14209}
14210
685b1105
JK
14211/* Check whether the producer field indicates either of GCC < 4.6, or the
14212 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14213
685b1105
JK
14214static void
14215check_producer (struct dwarf2_cu *cu)
60d5a603 14216{
38360086 14217 int major, minor;
60d5a603
JK
14218
14219 if (cu->producer == NULL)
14220 {
14221 /* For unknown compilers expect their behavior is DWARF version
14222 compliant.
14223
14224 GCC started to support .debug_types sections by -gdwarf-4 since
14225 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14226 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14227 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14228 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14229 }
b1ffba5a 14230 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14231 {
38360086
MW
14232 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14233 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14234 }
5230b05a 14235 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14236 {
14237 cu->producer_is_icc = true;
14238 cu->producer_is_icc_lt_14 = major < 14;
14239 }
c258c396
JD
14240 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14241 cu->producer_is_codewarrior = true;
685b1105
JK
14242 else
14243 {
14244 /* For other non-GCC compilers, expect their behavior is DWARF version
14245 compliant. */
60d5a603
JK
14246 }
14247
9068261f 14248 cu->checked_producer = true;
685b1105 14249}
ba919b58 14250
685b1105
JK
14251/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14252 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14253 during 4.6.0 experimental. */
14254
9068261f 14255static bool
685b1105
JK
14256producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14257{
14258 if (!cu->checked_producer)
14259 check_producer (cu);
14260
14261 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14262}
14263
c258c396
JD
14264
14265/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14266 with incorrect is_stmt attributes. */
14267
14268static bool
14269producer_is_codewarrior (struct dwarf2_cu *cu)
14270{
14271 if (!cu->checked_producer)
14272 check_producer (cu);
14273
14274 return cu->producer_is_codewarrior;
14275}
14276
bf23a268
TT
14277/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14278 If that attribute is not available, return the appropriate
14279 default. */
60d5a603
JK
14280
14281static enum dwarf_access_attribute
bf23a268 14282dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14283{
bf23a268
TT
14284 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14285 if (attr != nullptr)
14286 {
14287 LONGEST value = attr->constant_value (-1);
14288 if (value == DW_ACCESS_public
14289 || value == DW_ACCESS_protected
14290 || value == DW_ACCESS_private)
14291 return (dwarf_access_attribute) value;
14292 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14293 plongest (value));
14294 }
14295
60d5a603
JK
14296 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14297 {
14298 /* The default DWARF 2 accessibility for members is public, the default
14299 accessibility for inheritance is private. */
14300
14301 if (die->tag != DW_TAG_inheritance)
14302 return DW_ACCESS_public;
14303 else
14304 return DW_ACCESS_private;
14305 }
14306 else
14307 {
14308 /* DWARF 3+ defines the default accessibility a different way. The same
14309 rules apply now for DW_TAG_inheritance as for the members and it only
14310 depends on the container kind. */
14311
14312 if (die->parent->tag == DW_TAG_class_type)
14313 return DW_ACCESS_private;
14314 else
14315 return DW_ACCESS_public;
14316 }
14317}
14318
74ac6d43
TT
14319/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14320 offset. If the attribute was not found return 0, otherwise return
14321 1. If it was found but could not properly be handled, set *OFFSET
14322 to 0. */
14323
14324static int
14325handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14326 LONGEST *offset)
14327{
14328 struct attribute *attr;
14329
14330 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14331 if (attr != NULL)
14332 {
14333 *offset = 0;
14334
14335 /* Note that we do not check for a section offset first here.
14336 This is because DW_AT_data_member_location is new in DWARF 4,
14337 so if we see it, we can assume that a constant form is really
14338 a constant and not a section offset. */
cd6c91b4 14339 if (attr->form_is_constant ())
0826b30a 14340 *offset = attr->constant_value (0);
cd6c91b4 14341 else if (attr->form_is_section_offset ())
74ac6d43 14342 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14343 else if (attr->form_is_block ())
9d2246fc 14344 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14345 else
14346 dwarf2_complex_location_expr_complaint ();
14347
14348 return 1;
14349 }
14350
14351 return 0;
14352}
14353
7d79de9a
TT
14354/* Look for DW_AT_data_member_location and store the results in FIELD. */
14355
14356static void
14357handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14358 struct field *field)
14359{
14360 struct attribute *attr;
14361
14362 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14363 if (attr != NULL)
14364 {
14365 if (attr->form_is_constant ())
14366 {
14367 LONGEST offset = attr->constant_value (0);
14368 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14369 }
14370 else if (attr->form_is_section_offset ())
14371 dwarf2_complex_location_expr_complaint ();
14372 else if (attr->form_is_block ())
14373 {
14374 bool handled;
9d2246fc 14375 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14376 if (handled)
14377 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14378 else
14379 {
5e22e966
SM
14380 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14381 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14382 struct dwarf2_locexpr_baton *dlbaton
14383 = XOBNEW (&objfile->objfile_obstack,
14384 struct dwarf2_locexpr_baton);
9d2246fc
TT
14385 dlbaton->data = attr->as_block ()->data;
14386 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14387 /* When using this baton, we want to compute the address
14388 of the field, not the value. This is why
14389 is_reference is set to false here. */
14390 dlbaton->is_reference = false;
5e22e966 14391 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14392 dlbaton->per_cu = cu->per_cu;
14393
14394 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14395 }
14396 }
14397 else
14398 dwarf2_complex_location_expr_complaint ();
14399 }
14400}
14401
c906108c
SS
14402/* Add an aggregate field to the field list. */
14403
14404static void
107d2387 14405dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14406 struct dwarf2_cu *cu)
6e70227d 14407{
5e22e966 14408 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14409 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14410 struct nextfield *new_field;
14411 struct attribute *attr;
14412 struct field *fp;
15d034d0 14413 const char *fieldname = "";
c906108c 14414
7d0ccb61
DJ
14415 if (die->tag == DW_TAG_inheritance)
14416 {
be2daae6
TT
14417 fip->baseclasses.emplace_back ();
14418 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14419 }
14420 else
14421 {
be2daae6
TT
14422 fip->fields.emplace_back ();
14423 new_field = &fip->fields.back ();
7d0ccb61 14424 }
be2daae6 14425
9c6a1327
TT
14426 new_field->offset = die->sect_off;
14427
bf23a268 14428 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 14429 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 14430 fip->non_public_fields = true;
60d5a603 14431
e142c38c 14432 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14433 if (attr != nullptr)
23dca5c3 14434 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
14435 else
14436 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14437
14438 fp = &new_field->field;
a9a9bd0f 14439
e142c38c 14440 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14441 {
a9a9bd0f 14442 /* Data member other than a C++ static data member. */
6e70227d 14443
c906108c 14444 /* Get type of field. */
5d14b6e5 14445 fp->set_type (die_type (die, cu));
c906108c 14446
d6a843b5 14447 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14448
c906108c 14449 /* Get bit size of field (zero if none). */
e142c38c 14450 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14451 if (attr != nullptr)
c906108c 14452 {
529908cb 14453 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
14454 }
14455 else
14456 {
14457 FIELD_BITSIZE (*fp) = 0;
14458 }
14459
14460 /* Get bit offset of field. */
7d79de9a 14461 handle_data_member_location (die, cu, fp);
e142c38c 14462 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 14463 if (attr != nullptr && attr->form_is_constant ())
c906108c 14464 {
d5a22e77 14465 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14466 {
14467 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
14468 additional bit offset from the MSB of the containing
14469 anonymous object to the MSB of the field. We don't
14470 have to do anything special since we don't need to
14471 know the size of the anonymous object. */
529908cb 14472 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 14473 + attr->constant_value (0)));
c906108c
SS
14474 }
14475 else
14476 {
14477 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
14478 MSB of the anonymous object, subtract off the number of
14479 bits from the MSB of the field to the MSB of the
14480 object, and then subtract off the number of bits of
14481 the field itself. The result is the bit offset of
14482 the LSB of the field. */
c906108c 14483 int anonymous_size;
7c184d33 14484 int bit_offset = attr->constant_value (0);
c906108c 14485
e142c38c 14486 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 14487 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
14488 {
14489 /* The size of the anonymous object containing
14490 the bit field is explicit, so use the
14491 indicated size (in bytes). */
7c184d33 14492 anonymous_size = attr->constant_value (0);
c906108c
SS
14493 }
14494 else
14495 {
14496 /* The size of the anonymous object containing
14497 the bit field must be inferred from the type
14498 attribute of the data member containing the
14499 bit field. */
5d14b6e5 14500 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14501 }
f41f5e61
PA
14502 SET_FIELD_BITPOS (*fp,
14503 (FIELD_BITPOS (*fp)
14504 + anonymous_size * bits_per_byte
14505 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14506 }
14507 }
da5b30da
AA
14508 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14509 if (attr != NULL)
14510 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14511 + attr->constant_value (0)));
c906108c
SS
14512
14513 /* Get name of field. */
39cbfefa
DJ
14514 fieldname = dwarf2_name (die, cu);
14515 if (fieldname == NULL)
14516 fieldname = "";
d8151005
DJ
14517
14518 /* The name is already allocated along with this objfile, so we don't
14519 need to duplicate it for the type. */
14520 fp->name = fieldname;
c906108c
SS
14521
14522 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 14523 pointer or virtual base class pointer) to private. */
e142c38c 14524 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14525 {
d48cc9dd 14526 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 14527 new_field->accessibility = DW_ACCESS_private;
264fc0e2 14528 fip->non_public_fields = true;
c906108c
SS
14529 }
14530 }
a9a9bd0f 14531 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14532 {
a9a9bd0f
DC
14533 /* C++ static member. */
14534
14535 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14536 is a declaration, but all versions of G++ as of this writing
14537 (so through at least 3.2.1) incorrectly generate
14538 DW_TAG_variable tags. */
6e70227d 14539
ff355380 14540 const char *physname;
c906108c 14541
a9a9bd0f 14542 /* Get name of field. */
39cbfefa
DJ
14543 fieldname = dwarf2_name (die, cu);
14544 if (fieldname == NULL)
c906108c
SS
14545 return;
14546
254e6b9e 14547 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14548 if (attr
14549 /* Only create a symbol if this is an external value.
14550 new_symbol checks this and puts the value in the global symbol
14551 table, which we want. If it is not external, new_symbol
14552 will try to put the value in cu->list_in_scope which is wrong. */
14553 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14554 {
14555 /* A static const member, not much different than an enum as far as
14556 we're concerned, except that we can support more types. */
14557 new_symbol (die, NULL, cu);
14558 }
14559
2df3850c 14560 /* Get physical name. */
ff355380 14561 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14562
d8151005
DJ
14563 /* The name is already allocated along with this objfile, so we don't
14564 need to duplicate it for the type. */
14565 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 14566 fp->set_type (die_type (die, cu));
d8151005 14567 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14568 }
14569 else if (die->tag == DW_TAG_inheritance)
14570 {
74ac6d43 14571 /* C++ base class field. */
7d79de9a 14572 handle_data_member_location (die, cu, fp);
c906108c 14573 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
14574 fp->set_type (die_type (die, cu));
14575 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 14576 }
2ddeaf8a
TT
14577 else
14578 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14579}
14580
883fd55a
KS
14581/* Can the type given by DIE define another type? */
14582
14583static bool
14584type_can_define_types (const struct die_info *die)
14585{
14586 switch (die->tag)
14587 {
14588 case DW_TAG_typedef:
14589 case DW_TAG_class_type:
14590 case DW_TAG_structure_type:
14591 case DW_TAG_union_type:
14592 case DW_TAG_enumeration_type:
14593 return true;
14594
14595 default:
14596 return false;
14597 }
14598}
14599
14600/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14601
14602static void
883fd55a
KS
14603dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14604 struct dwarf2_cu *cu)
6e70227d 14605{
be2daae6
TT
14606 struct decl_field fp;
14607 memset (&fp, 0, sizeof (fp));
98751a41 14608
883fd55a 14609 gdb_assert (type_can_define_types (die));
98751a41 14610
883fd55a 14611 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14612 fp.name = dwarf2_name (die, cu);
14613 fp.type = read_type_die (die, cu);
98751a41 14614
c191a687 14615 /* Save accessibility. */
bf23a268 14616 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
14617 switch (accessibility)
14618 {
14619 case DW_ACCESS_public:
14620 /* The assumed value if neither private nor protected. */
14621 break;
14622 case DW_ACCESS_private:
be2daae6 14623 fp.is_private = 1;
c191a687
KS
14624 break;
14625 case DW_ACCESS_protected:
be2daae6 14626 fp.is_protected = 1;
c191a687 14627 break;
c191a687
KS
14628 }
14629
883fd55a 14630 if (die->tag == DW_TAG_typedef)
be2daae6 14631 fip->typedef_field_list.push_back (fp);
883fd55a 14632 else
be2daae6 14633 fip->nested_types_list.push_back (fp);
98751a41
JK
14634}
14635
9c6a1327
TT
14636/* A convenience typedef that's used when finding the discriminant
14637 field for a variant part. */
1b95cdb7
SM
14638typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14639 offset_map_type;
9c6a1327
TT
14640
14641/* Compute the discriminant range for a given variant. OBSTACK is
14642 where the results will be stored. VARIANT is the variant to
14643 process. IS_UNSIGNED indicates whether the discriminant is signed
14644 or unsigned. */
14645
14646static const gdb::array_view<discriminant_range>
14647convert_variant_range (struct obstack *obstack, const variant_field &variant,
14648 bool is_unsigned)
14649{
14650 std::vector<discriminant_range> ranges;
14651
14652 if (variant.default_branch)
14653 return {};
14654
14655 if (variant.discr_list_data == nullptr)
14656 {
14657 discriminant_range r
14658 = {variant.discriminant_value, variant.discriminant_value};
14659 ranges.push_back (r);
14660 }
14661 else
14662 {
14663 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14664 variant.discr_list_data->size);
14665 while (!data.empty ())
14666 {
14667 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14668 {
14669 complaint (_("invalid discriminant marker: %d"), data[0]);
14670 break;
14671 }
14672 bool is_range = data[0] == DW_DSC_range;
14673 data = data.slice (1);
14674
14675 ULONGEST low, high;
14676 unsigned int bytes_read;
14677
14678 if (data.empty ())
14679 {
14680 complaint (_("DW_AT_discr_list missing low value"));
14681 break;
14682 }
14683 if (is_unsigned)
14684 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14685 else
14686 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14687 &bytes_read);
14688 data = data.slice (bytes_read);
14689
14690 if (is_range)
14691 {
14692 if (data.empty ())
14693 {
14694 complaint (_("DW_AT_discr_list missing high value"));
14695 break;
14696 }
14697 if (is_unsigned)
14698 high = read_unsigned_leb128 (nullptr, data.data (),
14699 &bytes_read);
14700 else
14701 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14702 &bytes_read);
14703 data = data.slice (bytes_read);
14704 }
14705 else
14706 high = low;
14707
14708 ranges.push_back ({ low, high });
14709 }
14710 }
14711
14712 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14713 ranges.size ());
14714 std::copy (ranges.begin (), ranges.end (), result);
14715 return gdb::array_view<discriminant_range> (result, ranges.size ());
14716}
14717
14718static const gdb::array_view<variant_part> create_variant_parts
14719 (struct obstack *obstack,
14720 const offset_map_type &offset_map,
14721 struct field_info *fi,
14722 const std::vector<variant_part_builder> &variant_parts);
14723
14724/* Fill in a "struct variant" for a given variant field. RESULT is
14725 the variant to fill in. OBSTACK is where any needed allocations
14726 will be done. OFFSET_MAP holds the mapping from section offsets to
14727 fields for the type. FI describes the fields of the type we're
14728 processing. FIELD is the variant field we're converting. */
14729
14730static void
14731create_one_variant (variant &result, struct obstack *obstack,
14732 const offset_map_type &offset_map,
14733 struct field_info *fi, const variant_field &field)
14734{
14735 result.discriminants = convert_variant_range (obstack, field, false);
14736 result.first_field = field.first_field + fi->baseclasses.size ();
14737 result.last_field = field.last_field + fi->baseclasses.size ();
14738 result.parts = create_variant_parts (obstack, offset_map, fi,
14739 field.variant_parts);
14740}
14741
14742/* Fill in a "struct variant_part" for a given variant part. RESULT
14743 is the variant part to fill in. OBSTACK is where any needed
14744 allocations will be done. OFFSET_MAP holds the mapping from
14745 section offsets to fields for the type. FI describes the fields of
14746 the type we're processing. BUILDER is the variant part to be
14747 converted. */
14748
14749static void
14750create_one_variant_part (variant_part &result,
14751 struct obstack *obstack,
14752 const offset_map_type &offset_map,
14753 struct field_info *fi,
14754 const variant_part_builder &builder)
14755{
14756 auto iter = offset_map.find (builder.discriminant_offset);
14757 if (iter == offset_map.end ())
14758 {
14759 result.discriminant_index = -1;
14760 /* Doesn't matter. */
14761 result.is_unsigned = false;
14762 }
14763 else
14764 {
14765 result.discriminant_index = iter->second;
14766 result.is_unsigned
c6d940a9 14767 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
14768 }
14769
14770 size_t n = builder.variants.size ();
14771 variant *output = new (obstack) variant[n];
14772 for (size_t i = 0; i < n; ++i)
14773 create_one_variant (output[i], obstack, offset_map, fi,
14774 builder.variants[i]);
14775
14776 result.variants = gdb::array_view<variant> (output, n);
14777}
14778
14779/* Create a vector of variant parts that can be attached to a type.
14780 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14781 holds the mapping from section offsets to fields for the type. FI
14782 describes the fields of the type we're processing. VARIANT_PARTS
14783 is the vector to convert. */
14784
14785static const gdb::array_view<variant_part>
14786create_variant_parts (struct obstack *obstack,
14787 const offset_map_type &offset_map,
14788 struct field_info *fi,
14789 const std::vector<variant_part_builder> &variant_parts)
14790{
14791 if (variant_parts.empty ())
14792 return {};
14793
14794 size_t n = variant_parts.size ();
14795 variant_part *result = new (obstack) variant_part[n];
14796 for (size_t i = 0; i < n; ++i)
14797 create_one_variant_part (result[i], obstack, offset_map, fi,
14798 variant_parts[i]);
14799
14800 return gdb::array_view<variant_part> (result, n);
14801}
14802
14803/* Compute the variant part vector for FIP, attaching it to TYPE when
14804 done. */
14805
14806static void
14807add_variant_property (struct field_info *fip, struct type *type,
14808 struct dwarf2_cu *cu)
14809{
14810 /* Map section offsets of fields to their field index. Note the
14811 field index here does not take the number of baseclasses into
14812 account. */
14813 offset_map_type offset_map;
14814 for (int i = 0; i < fip->fields.size (); ++i)
14815 offset_map[fip->fields[i].offset] = i;
14816
5e22e966 14817 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14818 gdb::array_view<variant_part> parts
14819 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14820 fip->variant_parts);
14821
14822 struct dynamic_prop prop;
8c2e4e06
SM
14823 prop.set_variant_parts ((gdb::array_view<variant_part> *)
14824 obstack_copy (&objfile->objfile_obstack, &parts,
14825 sizeof (parts)));
9c6a1327 14826
5c54719c 14827 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14828}
14829
c906108c
SS
14830/* Create the vector of fields, and attach it to the type. */
14831
14832static void
fba45db2 14833dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14834 struct dwarf2_cu *cu)
c906108c 14835{
317f7127 14836 int nfields = fip->nfields ();
c906108c
SS
14837
14838 /* Record the field count, allocate space for the array of fields,
14839 and create blank accessibility bitfields if necessary. */
5e33d5f4 14840 type->set_num_fields (nfields);
3cabb6b0
SM
14841 type->set_fields
14842 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14843
b4ba55a1 14844 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14845 {
14846 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14847
14848 TYPE_FIELD_PRIVATE_BITS (type) =
14849 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14850 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14851
14852 TYPE_FIELD_PROTECTED_BITS (type) =
14853 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14854 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14855
774b6a14
TT
14856 TYPE_FIELD_IGNORE_BITS (type) =
14857 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14858 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14859 }
14860
14861 /* If the type has baseclasses, allocate and clear a bit vector for
14862 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14863 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14864 {
be2daae6 14865 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14866 unsigned char *pointer;
c906108c
SS
14867
14868 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14869 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14870 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14871 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14872 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14873 }
14874
9c6a1327
TT
14875 if (!fip->variant_parts.empty ())
14876 add_variant_property (fip, type, cu);
2ddeaf8a 14877
be2daae6
TT
14878 /* Copy the saved-up fields into the field vector. */
14879 for (int i = 0; i < nfields; ++i)
c906108c 14880 {
be2daae6
TT
14881 struct nextfield &field
14882 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14883 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14884
ceacbf6e 14885 type->field (i) = field.field;
be2daae6 14886 switch (field.accessibility)
c906108c 14887 {
c5aa993b 14888 case DW_ACCESS_private:
b4ba55a1 14889 if (cu->language != language_ada)
be2daae6 14890 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14891 break;
c906108c 14892
c5aa993b 14893 case DW_ACCESS_protected:
b4ba55a1 14894 if (cu->language != language_ada)
be2daae6 14895 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14896 break;
c906108c 14897
c5aa993b
JM
14898 case DW_ACCESS_public:
14899 break;
c906108c 14900
c5aa993b
JM
14901 default:
14902 /* Unknown accessibility. Complain and treat it as public. */
14903 {
b98664d3 14904 complaint (_("unsupported accessibility %d"),
be2daae6 14905 field.accessibility);
c5aa993b
JM
14906 }
14907 break;
c906108c 14908 }
be2daae6 14909 if (i < fip->baseclasses.size ())
c906108c 14910 {
be2daae6 14911 switch (field.virtuality)
c906108c 14912 {
c5aa993b
JM
14913 case DW_VIRTUALITY_virtual:
14914 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14915 if (cu->language == language_ada)
a73c6dcd 14916 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14917 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14918 break;
c906108c
SS
14919 }
14920 }
c906108c
SS
14921 }
14922}
14923
7d27a96d
TT
14924/* Return true if this member function is a constructor, false
14925 otherwise. */
14926
14927static int
14928dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14929{
14930 const char *fieldname;
fe978cb0 14931 const char *type_name;
7d27a96d
TT
14932 int len;
14933
14934 if (die->parent == NULL)
14935 return 0;
14936
14937 if (die->parent->tag != DW_TAG_structure_type
14938 && die->parent->tag != DW_TAG_union_type
14939 && die->parent->tag != DW_TAG_class_type)
14940 return 0;
14941
14942 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14943 type_name = dwarf2_name (die->parent, cu);
14944 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14945 return 0;
14946
14947 len = strlen (fieldname);
fe978cb0
PA
14948 return (strncmp (fieldname, type_name, len) == 0
14949 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14950}
14951
c906108c
SS
14952/* Add a member function to the proper fieldlist. */
14953
14954static void
107d2387 14955dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14956 struct type *type, struct dwarf2_cu *cu)
c906108c 14957{
5e22e966 14958 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 14959 struct attribute *attr;
c906108c 14960 int i;
be2daae6 14961 struct fnfieldlist *flp = nullptr;
c906108c 14962 struct fn_field *fnp;
15d034d0 14963 const char *fieldname;
f792889a 14964 struct type *this_type;
c906108c 14965
b4ba55a1 14966 if (cu->language == language_ada)
a73c6dcd 14967 error (_("unexpected member function in Ada type"));
b4ba55a1 14968
2df3850c 14969 /* Get name of member function. */
39cbfefa
DJ
14970 fieldname = dwarf2_name (die, cu);
14971 if (fieldname == NULL)
2df3850c 14972 return;
c906108c 14973
c906108c 14974 /* Look up member function name in fieldlist. */
be2daae6 14975 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14976 {
27bfe10e 14977 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14978 {
14979 flp = &fip->fnfieldlists[i];
14980 break;
14981 }
c906108c
SS
14982 }
14983
be2daae6
TT
14984 /* Create a new fnfieldlist if necessary. */
14985 if (flp == nullptr)
c906108c 14986 {
be2daae6
TT
14987 fip->fnfieldlists.emplace_back ();
14988 flp = &fip->fnfieldlists.back ();
c906108c 14989 flp->name = fieldname;
be2daae6 14990 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14991 }
14992
be2daae6
TT
14993 /* Create a new member function field and add it to the vector of
14994 fnfieldlists. */
14995 flp->fnfields.emplace_back ();
14996 fnp = &flp->fnfields.back ();
3da10d80
KS
14997
14998 /* Delay processing of the physname until later. */
9c37b5ae 14999 if (cu->language == language_cplus)
be2daae6
TT
15000 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15001 die, cu);
3da10d80
KS
15002 else
15003 {
1d06ead6 15004 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15005 fnp->physname = physname ? physname : "";
15006 }
15007
c906108c 15008 fnp->type = alloc_type (objfile);
f792889a 15009 this_type = read_type_die (die, cu);
78134374 15010 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15011 {
1f704f76 15012 int nparams = this_type->num_fields ();
c906108c 15013
f792889a 15014 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15015 of the method itself (TYPE_CODE_METHOD). */
15016 smash_to_method_type (fnp->type, type,
f792889a 15017 TYPE_TARGET_TYPE (this_type),
80fc5e77 15018 this_type->fields (),
1f704f76 15019 this_type->num_fields (),
a409645d 15020 this_type->has_varargs ());
c906108c
SS
15021
15022 /* Handle static member functions.
dda83cd7
SM
15023 Dwarf2 has no clean way to discern C++ static and non-static
15024 member functions. G++ helps GDB by marking the first
15025 parameter for non-static member functions (which is the this
15026 pointer) as artificial. We obtain this information from
15027 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15028 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15029 fnp->voffset = VOFFSET_STATIC;
15030 }
15031 else
b98664d3 15032 complaint (_("member function type missing for '%s'"),
3da10d80 15033 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15034
15035 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15036 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15037 fnp->fcontext = die_containing_type (die, cu);
c906108c 15038
3e43a32a
MS
15039 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15040 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15041
15042 /* Get accessibility. */
bf23a268 15043 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15044 switch (accessibility)
c906108c 15045 {
60d5a603
JK
15046 case DW_ACCESS_private:
15047 fnp->is_private = 1;
15048 break;
15049 case DW_ACCESS_protected:
15050 fnp->is_protected = 1;
15051 break;
c906108c
SS
15052 }
15053
b02dede2 15054 /* Check for artificial methods. */
e142c38c 15055 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15056 if (attr && attr->as_boolean ())
b02dede2
DJ
15057 fnp->is_artificial = 1;
15058
e35000a7
TBA
15059 /* Check for defaulted methods. */
15060 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15061 if (attr != nullptr)
15062 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15063
15064 /* Check for deleted methods. */
15065 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15066 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15067 fnp->is_deleted = 1;
15068
7d27a96d
TT
15069 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15070
0d564a31 15071 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15072 function. For older versions of GCC, this is an offset in the
15073 appropriate virtual table, as specified by DW_AT_containing_type.
15074 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15075 to the object address. */
15076
e142c38c 15077 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15078 if (attr != nullptr)
8e19ed76 15079 {
9d2246fc 15080 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15081 {
9d2246fc
TT
15082 struct dwarf_block *block = attr->as_block ();
15083
15084 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15085 {
15086 /* Old-style GCC. */
9d2246fc 15087 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15088 }
9d2246fc
TT
15089 else if (block->data[0] == DW_OP_deref
15090 || (block->size > 1
15091 && block->data[0] == DW_OP_deref_size
15092 && block->data[1] == cu->header.addr_size))
aec5aa8b 15093 {
9d2246fc 15094 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15095 if ((fnp->voffset % cu->header.addr_size) != 0)
15096 dwarf2_complex_location_expr_complaint ();
15097 else
15098 fnp->voffset /= cu->header.addr_size;
15099 fnp->voffset += 2;
15100 }
15101 else
15102 dwarf2_complex_location_expr_complaint ();
15103
15104 if (!fnp->fcontext)
7e993ebf
KS
15105 {
15106 /* If there is no `this' field and no DW_AT_containing_type,
15107 we cannot actually find a base class context for the
15108 vtable! */
1f704f76 15109 if (this_type->num_fields () == 0
7e993ebf
KS
15110 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15111 {
b98664d3 15112 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15113 "function \"%s\" (offset %s)"),
15114 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15115 }
15116 else
15117 {
15118 fnp->fcontext
940da03e 15119 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15120 }
15121 }
aec5aa8b 15122 }
cd6c91b4 15123 else if (attr->form_is_section_offset ())
dda83cd7 15124 {
4d3c2250 15125 dwarf2_complex_location_expr_complaint ();
dda83cd7 15126 }
8e19ed76 15127 else
dda83cd7 15128 {
4d3c2250
KB
15129 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15130 fieldname);
dda83cd7 15131 }
0d564a31 15132 }
d48cc9dd
DJ
15133 else
15134 {
15135 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15136 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15137 {
15138 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15139 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15140 "but the vtable offset is not specified"),
9d8780f0 15141 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15142 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15143 TYPE_CPLUS_DYNAMIC (type) = 1;
15144 }
15145 }
c906108c
SS
15146}
15147
15148/* Create the vector of member function fields, and attach it to the type. */
15149
15150static void
fba45db2 15151dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15152 struct dwarf2_cu *cu)
c906108c 15153{
b4ba55a1 15154 if (cu->language == language_ada)
a73c6dcd 15155 error (_("unexpected member functions in Ada type"));
b4ba55a1 15156
c906108c
SS
15157 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15158 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15159 TYPE_ALLOC (type,
15160 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15161
be2daae6 15162 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15163 {
be2daae6 15164 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15165 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15166
be2daae6
TT
15167 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15168 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15169 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15170 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15171
15172 for (int k = 0; k < nf.fnfields.size (); ++k)
15173 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15174 }
15175
be2daae6 15176 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15177}
15178
1168df01
JB
15179/* Returns non-zero if NAME is the name of a vtable member in CU's
15180 language, zero otherwise. */
15181static int
15182is_vtable_name (const char *name, struct dwarf2_cu *cu)
15183{
15184 static const char vptr[] = "_vptr";
15185
9c37b5ae
TT
15186 /* Look for the C++ form of the vtable. */
15187 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15188 return 1;
15189
15190 return 0;
15191}
15192
c0dd20ea 15193/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15194 functions, with the ABI-specified layout. If TYPE describes
15195 such a structure, smash it into a member function type.
61049d3b
DJ
15196
15197 GCC shouldn't do this; it should just output pointer to member DIEs.
15198 This is GCC PR debug/28767. */
c0dd20ea 15199
0b92b5bb
TT
15200static void
15201quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15202{
09e2d7c7 15203 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15204
15205 /* Check for a structure with no name and two children. */
1f704f76 15206 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15207 return;
c0dd20ea
DJ
15208
15209 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15210 if (TYPE_FIELD_NAME (type, 0) == NULL
15211 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15212 || TYPE_FIELD_NAME (type, 1) == NULL
15213 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15214 return;
c0dd20ea
DJ
15215
15216 /* Find the type of the method. */
940da03e 15217 pfn_type = type->field (0).type ();
c0dd20ea 15218 if (pfn_type == NULL
78134374
SM
15219 || pfn_type->code () != TYPE_CODE_PTR
15220 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15221 return;
c0dd20ea
DJ
15222
15223 /* Look for the "this" argument. */
15224 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15225 if (pfn_type->num_fields () == 0
940da03e
SM
15226 /* || pfn_type->field (0).type () == NULL */
15227 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15228 return;
c0dd20ea 15229
940da03e 15230 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15231 new_type = alloc_type (objfile);
09e2d7c7 15232 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15233 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15234 pfn_type->has_varargs ());
0b92b5bb 15235 smash_to_methodptr_type (type, new_type);
c0dd20ea 15236}
1168df01 15237
e26624c6
TT
15238/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15239 requires rewriting, then copy it and return the updated copy.
15240 Otherwise return nullptr. */
15241
15242static struct type *
15243rewrite_array_type (struct type *type)
15244{
15245 if (type->code () != TYPE_CODE_ARRAY)
15246 return nullptr;
15247
15248 struct type *index_type = type->index_type ();
15249 range_bounds *current_bounds = index_type->bounds ();
15250
15251 /* Handle multi-dimensional arrays. */
15252 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15253 if (new_target == nullptr)
15254 {
15255 /* Maybe we don't need to rewrite this array. */
15256 if (current_bounds->low.kind () == PROP_CONST
15257 && current_bounds->high.kind () == PROP_CONST)
15258 return nullptr;
15259 }
15260
15261 /* Either the target type was rewritten, or the bounds have to be
15262 updated. Either way we want to copy the type and update
15263 everything. */
15264 struct type *copy = copy_type (type);
15265 int nfields = copy->num_fields ();
15266 field *new_fields
15267 = ((struct field *) TYPE_ZALLOC (copy,
15268 nfields * sizeof (struct field)));
15269 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15270 copy->set_fields (new_fields);
15271 if (new_target != nullptr)
15272 TYPE_TARGET_TYPE (copy) = new_target;
15273
15274 struct type *index_copy = copy_type (index_type);
15275 range_bounds *bounds
15276 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15277 sizeof (range_bounds));
15278 *bounds = *current_bounds;
15279 bounds->low.set_const_val (1);
15280 bounds->high.set_const_val (0);
15281 index_copy->set_bounds (bounds);
15282 copy->set_index_type (index_copy);
15283
15284 return copy;
15285}
15286
57567375
TT
15287/* While some versions of GCC will generate complicated DWARF for an
15288 array (see quirk_ada_thick_pointer), more recent versions were
15289 modified to emit an explicit thick pointer structure. However, in
15290 this case, the array still has DWARF expressions for its ranges,
15291 and these must be ignored. */
15292
15293static void
15294quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15295 struct type *type)
15296{
15297 gdb_assert (cu->language == language_ada);
15298
15299 /* Check for a structure with two children. */
15300 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15301 return;
15302
15303 /* Check for P_ARRAY and P_BOUNDS members. */
15304 if (TYPE_FIELD_NAME (type, 0) == NULL
15305 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15306 || TYPE_FIELD_NAME (type, 1) == NULL
15307 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15308 return;
15309
15310 /* Make sure we're looking at a pointer to an array. */
15311 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15312 return;
57567375 15313
e26624c6
TT
15314 /* The Ada code already knows how to handle these types, so all that
15315 we need to do is turn the bounds into static bounds. However, we
15316 don't want to rewrite existing array or index types in-place,
15317 because those may be referenced in other contexts where this
15318 rewriting is undesirable. */
15319 struct type *new_ary_type
15320 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15321 if (new_ary_type != nullptr)
15322 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15323}
15324
2b4424c3
TT
15325/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15326 appropriate error checking and issuing complaints if there is a
15327 problem. */
15328
15329static ULONGEST
15330get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15331{
15332 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15333
15334 if (attr == nullptr)
15335 return 0;
15336
cd6c91b4 15337 if (!attr->form_is_constant ())
2b4424c3 15338 {
b98664d3 15339 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15340 " - DIE at %s [in module %s]"),
15341 sect_offset_str (die->sect_off),
5e22e966 15342 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15343 return 0;
15344 }
15345
529908cb
TT
15346 LONGEST val = attr->constant_value (0);
15347 if (val < 0)
2b4424c3 15348 {
529908cb
TT
15349 complaint (_("DW_AT_alignment value must not be negative"
15350 " - DIE at %s [in module %s]"),
15351 sect_offset_str (die->sect_off),
15352 objfile_name (cu->per_objfile->objfile));
15353 return 0;
2b4424c3 15354 }
529908cb 15355 ULONGEST align = val;
2b4424c3
TT
15356
15357 if (align == 0)
15358 {
b98664d3 15359 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15360 " - DIE at %s [in module %s]"),
15361 sect_offset_str (die->sect_off),
5e22e966 15362 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15363 return 0;
15364 }
15365 if ((align & (align - 1)) != 0)
15366 {
b98664d3 15367 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15368 " - DIE at %s [in module %s]"),
15369 sect_offset_str (die->sect_off),
5e22e966 15370 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15371 return 0;
15372 }
15373
15374 return align;
15375}
15376
15377/* If the DIE has a DW_AT_alignment attribute, use its value to set
15378 the alignment for TYPE. */
15379
15380static void
15381maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15382 struct type *type)
15383{
15384 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15385 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15386 " - DIE at %s [in module %s]"),
15387 sect_offset_str (die->sect_off),
5e22e966 15388 objfile_name (cu->per_objfile->objfile));
2b4424c3 15389}
685b1105 15390
e35000a7
TBA
15391/* Check if the given VALUE is a valid enum dwarf_calling_convention
15392 constant for a type, according to DWARF5 spec, Table 5.5. */
15393
15394static bool
15395is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15396{
15397 switch (value)
15398 {
15399 case DW_CC_normal:
15400 case DW_CC_pass_by_reference:
15401 case DW_CC_pass_by_value:
15402 return true;
15403
15404 default:
15405 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15406 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15407 return false;
15408 }
15409}
15410
d0922fcf
TBA
15411/* Check if the given VALUE is a valid enum dwarf_calling_convention
15412 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15413 also according to GNU-specific values (see include/dwarf2.h). */
15414
15415static bool
15416is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15417{
15418 switch (value)
15419 {
15420 case DW_CC_normal:
15421 case DW_CC_program:
15422 case DW_CC_nocall:
15423 return true;
15424
15425 case DW_CC_GNU_renesas_sh:
15426 case DW_CC_GNU_borland_fastcall_i386:
15427 case DW_CC_GDB_IBM_OpenCL:
15428 return true;
15429
15430 default:
15431 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15432 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15433 return false;
15434 }
15435}
15436
c906108c 15437/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15438 (definition) to create a type for the structure or union. Fill in
15439 the type's name and general properties; the members will not be
83655187
DE
15440 processed until process_structure_scope. A symbol table entry for
15441 the type will also not be done until process_structure_scope (assuming
15442 the type has a name).
c906108c 15443
c767944b
DJ
15444 NOTE: we need to call these functions regardless of whether or not the
15445 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15446 structure or union. This gets the type entered into our set of
83655187 15447 user defined types. */
c906108c 15448
f792889a 15449static struct type *
134d01f1 15450read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15451{
5e22e966 15452 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15453 struct type *type;
15454 struct attribute *attr;
15d034d0 15455 const char *name;
c906108c 15456
348e048f
DE
15457 /* If the definition of this type lives in .debug_types, read that type.
15458 Don't follow DW_AT_specification though, that will take us back up
15459 the chain and we want to go down. */
052c8bb8 15460 attr = die->attr (DW_AT_signature);
435d3d88 15461 if (attr != nullptr)
348e048f 15462 {
ac9ec31b 15463 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15464
ac9ec31b 15465 /* The type's CU may not be the same as CU.
02142a6c 15466 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15467 return set_die_type (die, type, cu);
15468 }
15469
c0dd20ea 15470 type = alloc_type (objfile);
c906108c 15471 INIT_CPLUS_SPECIFIC (type);
93311388 15472
39cbfefa
DJ
15473 name = dwarf2_name (die, cu);
15474 if (name != NULL)
c906108c 15475 {
987504bb 15476 if (cu->language == language_cplus
c44af4eb
TT
15477 || cu->language == language_d
15478 || cu->language == language_rust)
63d06c5c 15479 {
15d034d0 15480 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15481
15482 /* dwarf2_full_name might have already finished building the DIE's
15483 type. If so, there is no need to continue. */
15484 if (get_die_type (die, cu) != NULL)
15485 return get_die_type (die, cu);
15486
d0e39ea2 15487 type->set_name (full_name);
63d06c5c
DC
15488 }
15489 else
15490 {
d8151005
DJ
15491 /* The name is already allocated along with this objfile, so
15492 we don't need to duplicate it for the type. */
d0e39ea2 15493 type->set_name (name);
63d06c5c 15494 }
c906108c
SS
15495 }
15496
15497 if (die->tag == DW_TAG_structure_type)
15498 {
67607e24 15499 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15500 }
15501 else if (die->tag == DW_TAG_union_type)
15502 {
67607e24 15503 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15504 }
15505 else
15506 {
67607e24 15507 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15508 }
15509
0cc2414c 15510 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
aa70e35c 15511 type->set_is_declared_class (true);
0cc2414c 15512
e35000a7
TBA
15513 /* Store the calling convention in the type if it's available in
15514 the die. Otherwise the calling convention remains set to
15515 the default value DW_CC_normal. */
15516 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15517 if (attr != nullptr
529908cb 15518 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
15519 {
15520 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15521 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 15522 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
15523 }
15524
e142c38c 15525 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15526 if (attr != nullptr)
c906108c 15527 {
cd6c91b4 15528 if (attr->form_is_constant ())
dda83cd7 15529 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
15530 else
15531 {
f8e89861 15532 struct dynamic_prop prop;
293e7e51 15533 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15534 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 15535 TYPE_LENGTH (type) = 0;
155bfbd3 15536 }
c906108c
SS
15537 }
15538 else
15539 {
15540 TYPE_LENGTH (type) = 0;
15541 }
15542
2b4424c3
TT
15543 maybe_set_alignment (cu, die, type);
15544
5230b05a 15545 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15546 {
5230b05a
WT
15547 /* ICC<14 does not output the required DW_AT_declaration on
15548 incomplete types, but gives them a size of zero. */
b4b73759 15549 type->set_is_stub (true);
685b1105
JK
15550 }
15551 else
9baccff6 15552 type->set_stub_is_supported (true);
685b1105 15553
dc718098 15554 if (die_is_declaration (die, cu))
b4b73759 15555 type->set_is_stub (true);
a6c727b2
DJ
15556 else if (attr == NULL && die->child == NULL
15557 && producer_is_realview (cu->producer))
15558 /* RealView does not output the required DW_AT_declaration
15559 on incomplete types. */
b4b73759 15560 type->set_is_stub (true);
dc718098 15561
c906108c
SS
15562 /* We need to add the type field to the die immediately so we don't
15563 infinitely recurse when dealing with pointers to the structure
0963b4bd 15564 type within the structure itself. */
1c379e20 15565 set_die_type (die, type, cu);
c906108c 15566
7e314c57
JK
15567 /* set_die_type should be already done. */
15568 set_descriptive_type (type, die, cu);
15569
c767944b
DJ
15570 return type;
15571}
15572
9c6a1327
TT
15573static void handle_struct_member_die
15574 (struct die_info *child_die,
15575 struct type *type,
15576 struct field_info *fi,
15577 std::vector<struct symbol *> *template_args,
15578 struct dwarf2_cu *cu);
15579
15580/* A helper for handle_struct_member_die that handles
15581 DW_TAG_variant_part. */
15582
15583static void
15584handle_variant_part (struct die_info *die, struct type *type,
15585 struct field_info *fi,
15586 std::vector<struct symbol *> *template_args,
15587 struct dwarf2_cu *cu)
15588{
15589 variant_part_builder *new_part;
15590 if (fi->current_variant_part == nullptr)
15591 {
15592 fi->variant_parts.emplace_back ();
15593 new_part = &fi->variant_parts.back ();
15594 }
15595 else if (!fi->current_variant_part->processing_variant)
15596 {
15597 complaint (_("nested DW_TAG_variant_part seen "
15598 "- DIE at %s [in module %s]"),
15599 sect_offset_str (die->sect_off),
5e22e966 15600 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15601 return;
15602 }
15603 else
15604 {
15605 variant_field &current = fi->current_variant_part->variants.back ();
15606 current.variant_parts.emplace_back ();
15607 new_part = &current.variant_parts.back ();
15608 }
15609
15610 /* When we recurse, we want callees to add to this new variant
15611 part. */
15612 scoped_restore save_current_variant_part
15613 = make_scoped_restore (&fi->current_variant_part, new_part);
15614
15615 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15616 if (discr == NULL)
15617 {
15618 /* It's a univariant form, an extension we support. */
15619 }
15620 else if (discr->form_is_ref ())
15621 {
15622 struct dwarf2_cu *target_cu = cu;
15623 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15624
15625 new_part->discriminant_offset = target_die->sect_off;
15626 }
15627 else
15628 {
15629 complaint (_("DW_AT_discr does not have DIE reference form"
15630 " - DIE at %s [in module %s]"),
15631 sect_offset_str (die->sect_off),
5e22e966 15632 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15633 }
15634
15635 for (die_info *child_die = die->child;
15636 child_die != NULL;
15637 child_die = child_die->sibling)
15638 handle_struct_member_die (child_die, type, fi, template_args, cu);
15639}
15640
15641/* A helper for handle_struct_member_die that handles
15642 DW_TAG_variant. */
15643
15644static void
15645handle_variant (struct die_info *die, struct type *type,
15646 struct field_info *fi,
15647 std::vector<struct symbol *> *template_args,
15648 struct dwarf2_cu *cu)
15649{
15650 if (fi->current_variant_part == nullptr)
15651 {
15652 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15653 "- DIE at %s [in module %s]"),
15654 sect_offset_str (die->sect_off),
5e22e966 15655 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15656 return;
15657 }
15658 if (fi->current_variant_part->processing_variant)
15659 {
15660 complaint (_("nested DW_TAG_variant seen "
15661 "- DIE at %s [in module %s]"),
15662 sect_offset_str (die->sect_off),
5e22e966 15663 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15664 return;
15665 }
15666
15667 scoped_restore save_processing_variant
15668 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15669 true);
15670
15671 fi->current_variant_part->variants.emplace_back ();
15672 variant_field &variant = fi->current_variant_part->variants.back ();
15673 variant.first_field = fi->fields.size ();
15674
15675 /* In a variant we want to get the discriminant and also add a
15676 field for our sole member child. */
15677 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 15678 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
15679 {
15680 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 15681 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
15682 variant.default_branch = true;
15683 else
9d2246fc 15684 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
15685 }
15686 else
cae21f8e 15687 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
15688
15689 for (die_info *variant_child = die->child;
15690 variant_child != NULL;
15691 variant_child = variant_child->sibling)
15692 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15693
15694 variant.last_field = fi->fields.size ();
15695}
15696
2ddeaf8a
TT
15697/* A helper for process_structure_scope that handles a single member
15698 DIE. */
15699
15700static void
15701handle_struct_member_die (struct die_info *child_die, struct type *type,
15702 struct field_info *fi,
15703 std::vector<struct symbol *> *template_args,
15704 struct dwarf2_cu *cu)
15705{
15706 if (child_die->tag == DW_TAG_member
9c6a1327 15707 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15708 {
15709 /* NOTE: carlton/2002-11-05: A C++ static data member
15710 should be a DW_TAG_member that is a declaration, but
15711 all versions of G++ as of this writing (so through at
15712 least 3.2.1) incorrectly generate DW_TAG_variable
15713 tags for them instead. */
15714 dwarf2_add_field (fi, child_die, cu);
15715 }
15716 else if (child_die->tag == DW_TAG_subprogram)
15717 {
15718 /* Rust doesn't have member functions in the C++ sense.
15719 However, it does emit ordinary functions as children
15720 of a struct DIE. */
15721 if (cu->language == language_rust)
15722 read_func_scope (child_die, cu);
15723 else
15724 {
15725 /* C++ member function. */
15726 dwarf2_add_member_fn (fi, child_die, type, cu);
15727 }
15728 }
15729 else if (child_die->tag == DW_TAG_inheritance)
15730 {
15731 /* C++ base class field. */
15732 dwarf2_add_field (fi, child_die, cu);
15733 }
15734 else if (type_can_define_types (child_die))
15735 dwarf2_add_type_defn (fi, child_die, cu);
15736 else if (child_die->tag == DW_TAG_template_type_param
15737 || child_die->tag == DW_TAG_template_value_param)
15738 {
15739 struct symbol *arg = new_symbol (child_die, NULL, cu);
15740
15741 if (arg != NULL)
15742 template_args->push_back (arg);
15743 }
9c6a1327
TT
15744 else if (child_die->tag == DW_TAG_variant_part)
15745 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15746 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15747 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15748}
15749
c767944b
DJ
15750/* Finish creating a structure or union type, including filling in
15751 its members and creating a symbol for it. */
15752
15753static void
15754process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15755{
5e22e966 15756 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15757 struct die_info *child_die;
c767944b
DJ
15758 struct type *type;
15759
15760 type = get_die_type (die, cu);
15761 if (type == NULL)
15762 type = read_structure_type (die, cu);
15763
3e1d3d8c 15764 bool has_template_parameters = false;
e142c38c 15765 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15766 {
15767 struct field_info fi;
2f4732b0 15768 std::vector<struct symbol *> template_args;
c906108c 15769
639d11d3 15770 child_die = die->child;
c906108c
SS
15771
15772 while (child_die && child_die->tag)
15773 {
2ddeaf8a 15774 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15775 child_die = child_die->sibling;
c906108c
SS
15776 }
15777
34eaf542 15778 /* Attach template arguments to type. */
2f4732b0 15779 if (!template_args.empty ())
34eaf542 15780 {
3e1d3d8c 15781 has_template_parameters = true;
34eaf542 15782 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15783 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15784 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15785 = XOBNEWVEC (&objfile->objfile_obstack,
15786 struct symbol *,
15787 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15788 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15789 template_args.data (),
34eaf542
TT
15790 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15791 * sizeof (struct symbol *)));
34eaf542
TT
15792 }
15793
c906108c 15794 /* Attach fields and member functions to the type. */
317f7127 15795 if (fi.nfields () > 0)
e7c27a73 15796 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15797 if (!fi.fnfieldlists.empty ())
c906108c 15798 {
e7c27a73 15799 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15800
c5aa993b 15801 /* Get the type which refers to the base class (possibly this
c906108c 15802 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15803 class from the DW_AT_containing_type attribute. This use of
15804 DW_AT_containing_type is a GNU extension. */
c906108c 15805
e142c38c 15806 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15807 {
e7c27a73 15808 struct type *t = die_containing_type (die, cu);
c906108c 15809
ae6ae975 15810 set_type_vptr_basetype (type, t);
c906108c
SS
15811 if (type == t)
15812 {
c906108c
SS
15813 int i;
15814
15815 /* Our own class provides vtbl ptr. */
1f704f76 15816 for (i = t->num_fields () - 1;
c906108c
SS
15817 i >= TYPE_N_BASECLASSES (t);
15818 --i)
15819 {
0d5cff50 15820 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15821
dda83cd7 15822 if (is_vtable_name (fieldname, cu))
c906108c 15823 {
ae6ae975 15824 set_type_vptr_fieldno (type, i);
c906108c
SS
15825 break;
15826 }
15827 }
15828
15829 /* Complain if virtual function table field not found. */
15830 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15831 complaint (_("virtual function table pointer "
3e43a32a 15832 "not found when defining class '%s'"),
7d93a1e0 15833 type->name () ? type->name () : "");
c906108c
SS
15834 }
15835 else
15836 {
ae6ae975 15837 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15838 }
15839 }
f6235d4c 15840 else if (cu->producer
61012eef 15841 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15842 {
15843 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
15844 of the containing type, but the vtable pointer is
15845 always named __vfp. */
f6235d4c
EZ
15846
15847 int i;
15848
1f704f76 15849 for (i = type->num_fields () - 1;
f6235d4c
EZ
15850 i >= TYPE_N_BASECLASSES (type);
15851 --i)
15852 {
15853 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15854 {
ae6ae975
DE
15855 set_type_vptr_fieldno (type, i);
15856 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15857 break;
15858 }
15859 }
15860 }
c906108c 15861 }
98751a41
JK
15862
15863 /* Copy fi.typedef_field_list linked list elements content into the
15864 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15865 if (!fi.typedef_field_list.empty ())
98751a41 15866 {
be2daae6 15867 int count = fi.typedef_field_list.size ();
98751a41 15868
a0d7a4ff 15869 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15870 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15871 = ((struct decl_field *)
be2daae6
TT
15872 TYPE_ALLOC (type,
15873 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15874 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15875
be2daae6
TT
15876 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15877 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15878 }
c767944b 15879
883fd55a
KS
15880 /* Copy fi.nested_types_list linked list elements content into the
15881 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15882 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15883 {
be2daae6 15884 int count = fi.nested_types_list.size ();
883fd55a
KS
15885
15886 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15887 TYPE_NESTED_TYPES_ARRAY (type)
15888 = ((struct decl_field *)
be2daae6
TT
15889 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15890 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15891
be2daae6
TT
15892 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15893 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15894 }
c906108c 15895 }
63d06c5c 15896
bb5ed363 15897 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15898 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15899 cu->rust_unions.push_back (type);
57567375
TT
15900 else if (cu->language == language_ada)
15901 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 15902
90aeadfc
DC
15903 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15904 snapshots) has been known to create a die giving a declaration
15905 for a class that has, as a child, a die giving a definition for a
15906 nested class. So we have to process our children even if the
15907 current die is a declaration. Normally, of course, a declaration
15908 won't have any children at all. */
134d01f1 15909
ca040673
DE
15910 child_die = die->child;
15911
90aeadfc
DC
15912 while (child_die != NULL && child_die->tag)
15913 {
15914 if (child_die->tag == DW_TAG_member
15915 || child_die->tag == DW_TAG_variable
34eaf542
TT
15916 || child_die->tag == DW_TAG_inheritance
15917 || child_die->tag == DW_TAG_template_value_param
15918 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15919 {
90aeadfc 15920 /* Do nothing. */
134d01f1 15921 }
90aeadfc
DC
15922 else
15923 process_die (child_die, cu);
134d01f1 15924
436c571c 15925 child_die = child_die->sibling;
134d01f1
DJ
15926 }
15927
fa4028e9
JB
15928 /* Do not consider external references. According to the DWARF standard,
15929 these DIEs are identified by the fact that they have no byte_size
15930 attribute, and a declaration attribute. */
15931 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15932 || !die_is_declaration (die, cu)
15933 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15934 {
15935 struct symbol *sym = new_symbol (die, type, cu);
15936
15937 if (has_template_parameters)
15938 {
a776957c
TT
15939 struct symtab *symtab;
15940 if (sym != nullptr)
15941 symtab = symbol_symtab (sym);
15942 else if (cu->line_header != nullptr)
15943 {
15944 /* Any related symtab will do. */
15945 symtab
7ba99d21 15946 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15947 }
15948 else
15949 {
15950 symtab = nullptr;
15951 complaint (_("could not find suitable "
15952 "symtab for template parameter"
15953 " - DIE at %s [in module %s]"),
15954 sect_offset_str (die->sect_off),
15955 objfile_name (objfile));
15956 }
15957
15958 if (symtab != nullptr)
15959 {
15960 /* Make sure that the symtab is set on the new symbols.
15961 Even though they don't appear in this symtab directly,
15962 other parts of gdb assume that symbols do, and this is
15963 reasonably true. */
15964 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15965 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15966 }
3e1d3d8c
TT
15967 }
15968 }
134d01f1
DJ
15969}
15970
ed6acedd
TT
15971/* Assuming DIE is an enumeration type, and TYPE is its associated
15972 type, update TYPE using some information only available in DIE's
15973 children. In particular, the fields are computed. */
55426c9d
JB
15974
15975static void
15976update_enumeration_type_from_children (struct die_info *die,
15977 struct type *type,
15978 struct dwarf2_cu *cu)
15979{
60f7655a 15980 struct die_info *child_die;
55426c9d
JB
15981 int unsigned_enum = 1;
15982 int flag_enum = 1;
55426c9d 15983
8268c778 15984 auto_obstack obstack;
ed6acedd 15985 std::vector<struct field> fields;
55426c9d 15986
60f7655a
DE
15987 for (child_die = die->child;
15988 child_die != NULL && child_die->tag;
436c571c 15989 child_die = child_die->sibling)
55426c9d
JB
15990 {
15991 struct attribute *attr;
15992 LONGEST value;
15993 const gdb_byte *bytes;
15994 struct dwarf2_locexpr_baton *baton;
15995 const char *name;
60f7655a 15996
55426c9d
JB
15997 if (child_die->tag != DW_TAG_enumerator)
15998 continue;
15999
16000 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16001 if (attr == NULL)
16002 continue;
16003
16004 name = dwarf2_name (child_die, cu);
16005 if (name == NULL)
16006 name = "<anonymous enumerator>";
16007
16008 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16009 &value, &bytes, &baton);
16010 if (value < 0)
16011 {
16012 unsigned_enum = 0;
16013 flag_enum = 0;
16014 }
55426c9d 16015 else
edd45eb0
SM
16016 {
16017 if (count_one_bits_ll (value) >= 2)
16018 flag_enum = 0;
edd45eb0 16019 }
55426c9d 16020
ed6acedd
TT
16021 fields.emplace_back ();
16022 struct field &field = fields.back ();
16023 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16024 SET_FIELD_ENUMVAL (field, value);
16025 }
16026
16027 if (!fields.empty ())
16028 {
5e33d5f4 16029 type->set_num_fields (fields.size ());
3cabb6b0
SM
16030 type->set_fields
16031 ((struct field *)
16032 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16033 memcpy (type->fields (), fields.data (),
ed6acedd 16034 sizeof (struct field) * fields.size ());
55426c9d
JB
16035 }
16036
16037 if (unsigned_enum)
653223d3
SM
16038 type->set_is_unsigned (true);
16039
55426c9d 16040 if (flag_enum)
9902b327 16041 type->set_is_flag_enum (true);
55426c9d
JB
16042}
16043
134d01f1
DJ
16044/* Given a DW_AT_enumeration_type die, set its type. We do not
16045 complete the type's fields yet, or create any symbols. */
c906108c 16046
f792889a 16047static struct type *
134d01f1 16048read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16049{
5e22e966 16050 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16051 struct type *type;
c906108c 16052 struct attribute *attr;
0114d602 16053 const char *name;
134d01f1 16054
348e048f
DE
16055 /* If the definition of this type lives in .debug_types, read that type.
16056 Don't follow DW_AT_specification though, that will take us back up
16057 the chain and we want to go down. */
052c8bb8 16058 attr = die->attr (DW_AT_signature);
435d3d88 16059 if (attr != nullptr)
348e048f 16060 {
ac9ec31b 16061 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16062
ac9ec31b 16063 /* The type's CU may not be the same as CU.
02142a6c 16064 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16065 return set_die_type (die, type, cu);
16066 }
16067
c906108c
SS
16068 type = alloc_type (objfile);
16069
67607e24 16070 type->set_code (TYPE_CODE_ENUM);
94af9270 16071 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16072 if (name != NULL)
d0e39ea2 16073 type->set_name (name);
c906108c 16074
0626fc76
TT
16075 attr = dwarf2_attr (die, DW_AT_type, cu);
16076 if (attr != NULL)
16077 {
16078 struct type *underlying_type = die_type (die, cu);
16079
16080 TYPE_TARGET_TYPE (type) = underlying_type;
16081 }
16082
e142c38c 16083 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16084 if (attr != nullptr)
c906108c 16085 {
529908cb 16086 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16087 }
16088 else
16089 {
16090 TYPE_LENGTH (type) = 0;
16091 }
16092
2b4424c3
TT
16093 maybe_set_alignment (cu, die, type);
16094
137033e9
JB
16095 /* The enumeration DIE can be incomplete. In Ada, any type can be
16096 declared as private in the package spec, and then defined only
16097 inside the package body. Such types are known as Taft Amendment
16098 Types. When another package uses such a type, an incomplete DIE
16099 may be generated by the compiler. */
02eb380e 16100 if (die_is_declaration (die, cu))
b4b73759 16101 type->set_is_stub (true);
02eb380e 16102
0626fc76
TT
16103 /* If this type has an underlying type that is not a stub, then we
16104 may use its attributes. We always use the "unsigned" attribute
16105 in this situation, because ordinarily we guess whether the type
16106 is unsigned -- but the guess can be wrong and the underlying type
16107 can tell us the reality. However, we defer to a local size
16108 attribute if one exists, because this lets the compiler override
16109 the underlying type if needed. */
e46d3488 16110 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16111 {
9e7c9a03
HD
16112 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16113 underlying_type = check_typedef (underlying_type);
653223d3
SM
16114
16115 type->set_is_unsigned (underlying_type->is_unsigned ());
16116
0626fc76 16117 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16118 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16119
2b4424c3 16120 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16121 && TYPE_RAW_ALIGN (underlying_type) != 0)
16122 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16123 }
16124
aa70e35c 16125 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
3d567982 16126
ed6acedd
TT
16127 set_die_type (die, type, cu);
16128
16129 /* Finish the creation of this type by using the enum's children.
16130 Note that, as usual, this must come after set_die_type to avoid
16131 infinite recursion when trying to compute the names of the
16132 enumerators. */
16133 update_enumeration_type_from_children (die, type, cu);
16134
16135 return type;
134d01f1
DJ
16136}
16137
16138/* Given a pointer to a die which begins an enumeration, process all
16139 the dies that define the members of the enumeration, and create the
16140 symbol for the enumeration type.
16141
16142 NOTE: We reverse the order of the element list. */
16143
16144static void
16145process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16146{
f792889a 16147 struct type *this_type;
134d01f1 16148
f792889a
DJ
16149 this_type = get_die_type (die, cu);
16150 if (this_type == NULL)
16151 this_type = read_enumeration_type (die, cu);
9dc481d3 16152
639d11d3 16153 if (die->child != NULL)
c906108c 16154 {
9dc481d3 16155 struct die_info *child_die;
15d034d0 16156 const char *name;
9dc481d3 16157
639d11d3 16158 child_die = die->child;
c906108c
SS
16159 while (child_die && child_die->tag)
16160 {
16161 if (child_die->tag != DW_TAG_enumerator)
16162 {
e7c27a73 16163 process_die (child_die, cu);
c906108c
SS
16164 }
16165 else
16166 {
39cbfefa
DJ
16167 name = dwarf2_name (child_die, cu);
16168 if (name)
ed6acedd 16169 new_symbol (child_die, this_type, cu);
c906108c
SS
16170 }
16171
436c571c 16172 child_die = child_die->sibling;
c906108c 16173 }
c906108c 16174 }
134d01f1 16175
6c83ed52
TT
16176 /* If we are reading an enum from a .debug_types unit, and the enum
16177 is a declaration, and the enum is not the signatured type in the
16178 unit, then we do not want to add a symbol for it. Adding a
16179 symbol would in some cases obscure the true definition of the
16180 enum, giving users an incomplete type when the definition is
16181 actually available. Note that we do not want to do this for all
16182 enums which are just declarations, because C++0x allows forward
16183 enum declarations. */
3019eac3 16184 if (cu->per_cu->is_debug_types
6c83ed52
TT
16185 && die_is_declaration (die, cu))
16186 {
52dc124a 16187 struct signatured_type *sig_type;
6c83ed52 16188
c0f78cd4 16189 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16190 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16191 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16192 return;
16193 }
16194
f792889a 16195 new_symbol (die, this_type, cu);
c906108c
SS
16196}
16197
57567375
TT
16198/* Helper function for quirk_ada_thick_pointer that examines a bounds
16199 expression for an index type and finds the corresponding field
16200 offset in the hidden "P_BOUNDS" structure. Returns true on success
16201 and updates *FIELD, false if it fails to recognize an
16202 expression. */
16203
16204static bool
16205recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16206 int *bounds_offset, struct field *field,
16207 struct dwarf2_cu *cu)
16208{
16209 struct attribute *attr = dwarf2_attr (die, name, cu);
16210 if (attr == nullptr || !attr->form_is_block ())
16211 return false;
16212
16213 const struct dwarf_block *block = attr->as_block ();
16214 const gdb_byte *start = block->data;
16215 const gdb_byte *end = block->data + block->size;
16216
16217 /* The expression to recognize generally looks like:
16218
16219 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16220 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16221
16222 However, the second "plus_uconst" may be missing:
16223
16224 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16225 DW_OP_deref_size: 4)
16226
16227 This happens when the field is at the start of the structure.
16228
16229 Also, the final deref may not be sized:
16230
16231 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16232 DW_OP_deref)
16233
16234 This happens when the size of the index type happens to be the
16235 same as the architecture's word size. This can occur with or
16236 without the second plus_uconst. */
16237
16238 if (end - start < 2)
16239 return false;
16240 if (*start++ != DW_OP_push_object_address)
16241 return false;
16242 if (*start++ != DW_OP_plus_uconst)
16243 return false;
16244
16245 uint64_t this_bound_off;
16246 start = gdb_read_uleb128 (start, end, &this_bound_off);
16247 if (start == nullptr || (int) this_bound_off != this_bound_off)
16248 return false;
16249 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16250 is consistent among all bounds. */
16251 if (*bounds_offset == -1)
16252 *bounds_offset = this_bound_off;
16253 else if (*bounds_offset != this_bound_off)
16254 return false;
16255
16256 if (start == end || *start++ != DW_OP_deref)
16257 return false;
16258
16259 int offset = 0;
16260 if (start ==end)
16261 return false;
16262 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16263 {
16264 /* This means an offset of 0. */
16265 }
16266 else if (*start++ != DW_OP_plus_uconst)
16267 return false;
16268 else
16269 {
16270 /* The size is the parameter to DW_OP_plus_uconst. */
16271 uint64_t val;
16272 start = gdb_read_uleb128 (start, end, &val);
16273 if (start == nullptr)
16274 return false;
16275 if ((int) val != val)
16276 return false;
16277 offset = val;
16278 }
16279
16280 if (start == end)
16281 return false;
16282
16283 uint64_t size;
16284 if (*start == DW_OP_deref_size)
16285 {
16286 start = gdb_read_uleb128 (start + 1, end, &size);
16287 if (start == nullptr)
16288 return false;
16289 }
16290 else if (*start == DW_OP_deref)
16291 {
16292 size = cu->header.addr_size;
16293 ++start;
16294 }
16295 else
16296 return false;
16297
16298 SET_FIELD_BITPOS (*field, 8 * offset);
16299 if (size != TYPE_LENGTH (field->type ()))
16300 FIELD_BITSIZE (*field) = 8 * size;
16301
16302 return true;
16303}
16304
16305/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16306 some kinds of Ada arrays:
16307
16308 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16309 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16310 <11e0> DW_AT_data_location: 2 byte block: 97 6
16311 (DW_OP_push_object_address; DW_OP_deref)
16312 <11e3> DW_AT_type : <0x1173>
16313 <11e7> DW_AT_sibling : <0x1201>
16314 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16315 <11ec> DW_AT_type : <0x1206>
16316 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16317 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16318 DW_OP_deref_size: 4)
16319 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16320 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16321 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16322
16323 This actually represents a "thick pointer", which is a structure
16324 with two elements: one that is a pointer to the array data, and one
16325 that is a pointer to another structure; this second structure holds
16326 the array bounds.
16327
16328 This returns a new type on success, or nullptr if this didn't
16329 recognize the type. */
16330
16331static struct type *
16332quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16333 struct type *type)
16334{
16335 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16336 /* So far we've only seen this with block form. */
16337 if (attr == nullptr || !attr->form_is_block ())
16338 return nullptr;
16339
16340 /* Note that this will fail if the structure layout is changed by
16341 the compiler. However, we have no good way to recognize some
16342 other layout, because we don't know what expression the compiler
16343 might choose to emit should this happen. */
16344 struct dwarf_block *blk = attr->as_block ();
16345 if (blk->size != 2
16346 || blk->data[0] != DW_OP_push_object_address
16347 || blk->data[1] != DW_OP_deref)
16348 return nullptr;
16349
16350 int bounds_offset = -1;
16351 int max_align = -1;
16352 std::vector<struct field> range_fields;
16353 for (struct die_info *child_die = die->child;
16354 child_die;
16355 child_die = child_die->sibling)
16356 {
16357 if (child_die->tag == DW_TAG_subrange_type)
16358 {
16359 struct type *underlying = read_subrange_index_type (child_die, cu);
16360
16361 int this_align = type_align (underlying);
16362 if (this_align > max_align)
16363 max_align = this_align;
16364
16365 range_fields.emplace_back ();
16366 range_fields.emplace_back ();
16367
16368 struct field &lower = range_fields[range_fields.size () - 2];
16369 struct field &upper = range_fields[range_fields.size () - 1];
16370
16371 lower.set_type (underlying);
16372 FIELD_ARTIFICIAL (lower) = 1;
16373
16374 upper.set_type (underlying);
16375 FIELD_ARTIFICIAL (upper) = 1;
16376
16377 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16378 &bounds_offset, &lower, cu)
16379 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16380 &bounds_offset, &upper, cu))
16381 return nullptr;
16382 }
16383 }
16384
16385 /* This shouldn't really happen, but double-check that we found
16386 where the bounds are stored. */
16387 if (bounds_offset == -1)
16388 return nullptr;
16389
16390 struct objfile *objfile = cu->per_objfile->objfile;
16391 for (int i = 0; i < range_fields.size (); i += 2)
16392 {
16393 char name[20];
16394
16395 /* Set the name of each field in the bounds. */
16396 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16397 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16398 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16399 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16400 }
16401
16402 struct type *bounds = alloc_type (objfile);
16403 bounds->set_code (TYPE_CODE_STRUCT);
16404
16405 bounds->set_num_fields (range_fields.size ());
16406 bounds->set_fields
16407 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16408 * sizeof (struct field))));
16409 memcpy (bounds->fields (), range_fields.data (),
16410 bounds->num_fields () * sizeof (struct field));
16411
16412 int last_fieldno = range_fields.size () - 1;
16413 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16414 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16415 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16416
16417 /* Rewrite the existing array type in place. Specifically, we
16418 remove any dynamic properties we might have read, and we replace
16419 the index types. */
16420 struct type *iter = type;
16421 for (int i = 0; i < range_fields.size (); i += 2)
16422 {
16423 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16424 iter->main_type->dyn_prop_list = nullptr;
16425 iter->set_index_type
16426 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16427 iter = TYPE_TARGET_TYPE (iter);
16428 }
16429
16430 struct type *result = alloc_type (objfile);
16431 result->set_code (TYPE_CODE_STRUCT);
16432
16433 result->set_num_fields (2);
16434 result->set_fields
16435 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16436 * sizeof (struct field))));
16437
16438 /* The names are chosen to coincide with what the compiler does with
16439 -fgnat-encodings=all, which the Ada code in gdb already
16440 understands. */
16441 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16442 result->field (0).set_type (lookup_pointer_type (type));
16443
16444 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16445 result->field (1).set_type (lookup_pointer_type (bounds));
16446 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16447
16448 result->set_name (type->name ());
16449 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16450 + TYPE_LENGTH (result->field (1).type ()));
16451
16452 return result;
16453}
16454
c906108c
SS
16455/* Extract all information from a DW_TAG_array_type DIE and put it in
16456 the DIE's type field. For now, this only handles one dimensional
16457 arrays. */
16458
f792889a 16459static struct type *
e7c27a73 16460read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16461{
5e22e966 16462 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16463 struct die_info *child_die;
7e314c57 16464 struct type *type;
c906108c 16465 struct type *element_type, *range_type, *index_type;
c906108c 16466 struct attribute *attr;
15d034d0 16467 const char *name;
a405673c 16468 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16469 unsigned int bit_stride = 0;
c906108c 16470
e7c27a73 16471 element_type = die_type (die, cu);
c906108c 16472
7e314c57
JK
16473 /* The die_type call above may have already set the type for this DIE. */
16474 type = get_die_type (die, cu);
16475 if (type)
16476 return type;
16477
dc53a7ad
JB
16478 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16479 if (attr != NULL)
a405673c
JB
16480 {
16481 int stride_ok;
293e7e51 16482 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16483
16484 byte_stride_prop
16485 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16486 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16487 prop_type);
a405673c
JB
16488 if (!stride_ok)
16489 {
b98664d3 16490 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16491 " - DIE at %s [in module %s]"),
16492 sect_offset_str (die->sect_off),
5e22e966 16493 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16494 /* Ignore this attribute. We will likely not be able to print
16495 arrays of this type correctly, but there is little we can do
16496 to help if we cannot read the attribute's value. */
16497 byte_stride_prop = NULL;
16498 }
16499 }
dc53a7ad
JB
16500
16501 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16502 if (attr != NULL)
529908cb 16503 bit_stride = attr->constant_value (0);
dc53a7ad 16504
c906108c
SS
16505 /* Irix 6.2 native cc creates array types without children for
16506 arrays with unspecified length. */
639d11d3 16507 if (die->child == NULL)
c906108c 16508 {
46bf5051 16509 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16510 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16511 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16512 byte_stride_prop, bit_stride);
f792889a 16513 return set_die_type (die, type, cu);
c906108c
SS
16514 }
16515
791afaa2 16516 std::vector<struct type *> range_types;
639d11d3 16517 child_die = die->child;
c906108c
SS
16518 while (child_die && child_die->tag)
16519 {
16520 if (child_die->tag == DW_TAG_subrange_type)
16521 {
f792889a 16522 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16523
dda83cd7
SM
16524 if (child_type != NULL)
16525 {
0963b4bd 16526 /* The range type was succesfully read. Save it for the
dda83cd7 16527 array type creation. */
791afaa2 16528 range_types.push_back (child_type);
dda83cd7 16529 }
c906108c 16530 }
436c571c 16531 child_die = child_die->sibling;
c906108c
SS
16532 }
16533
cf2b2075
TV
16534 if (range_types.empty ())
16535 {
16536 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16537 sect_offset_str (die->sect_off),
16538 objfile_name (cu->per_objfile->objfile));
16539 return NULL;
16540 }
16541
c906108c
SS
16542 /* Dwarf2 dimensions are output from left to right, create the
16543 necessary array types in backwards order. */
7ca2d3a3 16544
c906108c 16545 type = element_type;
7ca2d3a3
DL
16546
16547 if (read_array_order (die, cu) == DW_ORD_col_major)
16548 {
16549 int i = 0;
9a619af0 16550
791afaa2 16551 while (i < range_types.size ())
10f6a3ad
TT
16552 {
16553 type = create_array_type_with_stride (NULL, type, range_types[i++],
16554 byte_stride_prop, bit_stride);
16555 bit_stride = 0;
16556 byte_stride_prop = nullptr;
16557 }
7ca2d3a3
DL
16558 }
16559 else
16560 {
791afaa2 16561 size_t ndim = range_types.size ();
7ca2d3a3 16562 while (ndim-- > 0)
10f6a3ad
TT
16563 {
16564 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16565 byte_stride_prop, bit_stride);
16566 bit_stride = 0;
16567 byte_stride_prop = nullptr;
16568 }
7ca2d3a3 16569 }
c906108c 16570
cf2b2075
TV
16571 gdb_assert (type != element_type);
16572
f5f8a009
EZ
16573 /* Understand Dwarf2 support for vector types (like they occur on
16574 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16575 array type. This is not part of the Dwarf2/3 standard yet, but a
16576 custom vendor extension. The main difference between a regular
16577 array and the vector variant is that vectors are passed by value
16578 to functions. */
e142c38c 16579 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16580 if (attr != nullptr)
ea37ba09 16581 make_vector_type (type);
f5f8a009 16582
dbc98a8b
KW
16583 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16584 implementation may choose to implement triple vectors using this
16585 attribute. */
16586 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 16587 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 16588 {
529908cb
TT
16589 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16590 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 16591 else
b98664d3 16592 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16593 "than the total size of elements"));
dbc98a8b
KW
16594 }
16595
39cbfefa
DJ
16596 name = dwarf2_name (die, cu);
16597 if (name)
d0e39ea2 16598 type->set_name (name);
6e70227d 16599
2b4424c3
TT
16600 maybe_set_alignment (cu, die, type);
16601
57567375
TT
16602 struct type *replacement_type = nullptr;
16603 if (cu->language == language_ada)
16604 {
16605 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16606 if (replacement_type != nullptr)
16607 type = replacement_type;
16608 }
16609
0963b4bd 16610 /* Install the type in the die. */
57567375 16611 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
16612
16613 /* set_die_type should be already done. */
b4ba55a1
JB
16614 set_descriptive_type (type, die, cu);
16615
7e314c57 16616 return type;
c906108c
SS
16617}
16618
7ca2d3a3 16619static enum dwarf_array_dim_ordering
6e70227d 16620read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16621{
16622 struct attribute *attr;
16623
16624 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16625
435d3d88 16626 if (attr != nullptr)
1bc397c5
TT
16627 {
16628 LONGEST val = attr->constant_value (-1);
16629 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16630 return (enum dwarf_array_dim_ordering) val;
16631 }
7ca2d3a3 16632
0963b4bd
MS
16633 /* GNU F77 is a special case, as at 08/2004 array type info is the
16634 opposite order to the dwarf2 specification, but data is still
16635 laid out as per normal fortran.
7ca2d3a3 16636
0963b4bd
MS
16637 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16638 version checking. */
7ca2d3a3 16639
905e0470
PM
16640 if (cu->language == language_fortran
16641 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16642 {
16643 return DW_ORD_row_major;
16644 }
16645
3a3440fb 16646 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
16647 {
16648 case array_column_major:
16649 return DW_ORD_col_major;
16650 case array_row_major:
16651 default:
16652 return DW_ORD_row_major;
16653 };
16654}
16655
72019c9c 16656/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16657 the DIE's type field. */
72019c9c 16658
f792889a 16659static struct type *
72019c9c
GM
16660read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16661{
7e314c57
JK
16662 struct type *domain_type, *set_type;
16663 struct attribute *attr;
f792889a 16664
7e314c57
JK
16665 domain_type = die_type (die, cu);
16666
16667 /* The die_type call above may have already set the type for this DIE. */
16668 set_type = get_die_type (die, cu);
16669 if (set_type)
16670 return set_type;
16671
16672 set_type = create_set_type (NULL, domain_type);
16673
16674 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
16675 if (attr != nullptr && attr->form_is_unsigned ())
16676 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 16677
2b4424c3
TT
16678 maybe_set_alignment (cu, die, set_type);
16679
f792889a 16680 return set_die_type (die, set_type, cu);
72019c9c 16681}
7ca2d3a3 16682
0971de02
TT
16683/* A helper for read_common_block that creates a locexpr baton.
16684 SYM is the symbol which we are marking as computed.
16685 COMMON_DIE is the DIE for the common block.
16686 COMMON_LOC is the location expression attribute for the common
16687 block itself.
16688 MEMBER_LOC is the location expression attribute for the particular
16689 member of the common block that we are processing.
16690 CU is the CU from which the above come. */
16691
16692static void
16693mark_common_block_symbol_computed (struct symbol *sym,
16694 struct die_info *common_die,
16695 struct attribute *common_loc,
16696 struct attribute *member_loc,
16697 struct dwarf2_cu *cu)
16698{
5e22e966 16699 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16700 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16701 struct dwarf2_locexpr_baton *baton;
16702 gdb_byte *ptr;
16703 unsigned int cu_off;
08feed99 16704 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16705 LONGEST offset = 0;
16706
16707 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16708 gdb_assert (common_loc->form_is_block ());
16709 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16710 || member_loc->form_is_constant ());
0971de02 16711
8d749320 16712 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16713 baton->per_objfile = per_objfile;
0971de02
TT
16714 baton->per_cu = cu->per_cu;
16715 gdb_assert (baton->per_cu);
16716
16717 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16718
cd6c91b4 16719 if (member_loc->form_is_constant ())
0971de02 16720 {
0826b30a 16721 offset = member_loc->constant_value (0);
0971de02
TT
16722 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16723 }
16724 else
9d2246fc 16725 baton->size += member_loc->as_block ()->size;
0971de02 16726
224c3ddb 16727 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16728 baton->data = ptr;
16729
16730 *ptr++ = DW_OP_call4;
9c541725 16731 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16732 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16733 ptr += 4;
16734
cd6c91b4 16735 if (member_loc->form_is_constant ())
0971de02
TT
16736 {
16737 *ptr++ = DW_OP_addr;
16738 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16739 ptr += cu->header.addr_size;
16740 }
16741 else
16742 {
16743 /* We have to copy the data here, because DW_OP_call4 will only
16744 use a DW_AT_location attribute. */
9d2246fc
TT
16745 struct dwarf_block *block = member_loc->as_block ();
16746 memcpy (ptr, block->data, block->size);
16747 ptr += block->size;
0971de02
TT
16748 }
16749
16750 *ptr++ = DW_OP_plus;
16751 gdb_assert (ptr - baton->data == baton->size);
16752
0971de02 16753 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16754 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16755}
16756
4357ac6c
TT
16757/* Create appropriate locally-scoped variables for all the
16758 DW_TAG_common_block entries. Also create a struct common_block
16759 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16760 is used to separate the common blocks name namespace from regular
4357ac6c 16761 variable names. */
c906108c
SS
16762
16763static void
e7c27a73 16764read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16765{
0971de02
TT
16766 struct attribute *attr;
16767
16768 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16769 if (attr != nullptr)
0971de02
TT
16770 {
16771 /* Support the .debug_loc offsets. */
4fc6c0d5 16772 if (attr->form_is_block ())
dda83cd7 16773 {
0971de02 16774 /* Ok. */
dda83cd7 16775 }
cd6c91b4 16776 else if (attr->form_is_section_offset ())
dda83cd7 16777 {
0971de02
TT
16778 dwarf2_complex_location_expr_complaint ();
16779 attr = NULL;
dda83cd7 16780 }
0971de02 16781 else
dda83cd7 16782 {
0971de02
TT
16783 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16784 "common block member");
16785 attr = NULL;
dda83cd7 16786 }
0971de02
TT
16787 }
16788
639d11d3 16789 if (die->child != NULL)
c906108c 16790 {
5e22e966 16791 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16792 struct die_info *child_die;
16793 size_t n_entries = 0, size;
16794 struct common_block *common_block;
16795 struct symbol *sym;
74ac6d43 16796
4357ac6c
TT
16797 for (child_die = die->child;
16798 child_die && child_die->tag;
436c571c 16799 child_die = child_die->sibling)
4357ac6c
TT
16800 ++n_entries;
16801
16802 size = (sizeof (struct common_block)
16803 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16804 common_block
16805 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16806 size);
4357ac6c
TT
16807 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16808 common_block->n_entries = 0;
16809
16810 for (child_die = die->child;
16811 child_die && child_die->tag;
436c571c 16812 child_die = child_die->sibling)
4357ac6c
TT
16813 {
16814 /* Create the symbol in the DW_TAG_common_block block in the current
16815 symbol scope. */
e7c27a73 16816 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16817 if (sym != NULL)
16818 {
16819 struct attribute *member_loc;
16820
16821 common_block->contents[common_block->n_entries++] = sym;
16822
16823 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16824 cu);
16825 if (member_loc)
16826 {
16827 /* GDB has handled this for a long time, but it is
16828 not specified by DWARF. It seems to have been
16829 emitted by gfortran at least as recently as:
16830 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16831 complaint (_("Variable in common block has "
0971de02 16832 "DW_AT_data_member_location "
9d8780f0
SM
16833 "- DIE at %s [in module %s]"),
16834 sect_offset_str (child_die->sect_off),
518817b3 16835 objfile_name (objfile));
0971de02 16836
cd6c91b4 16837 if (member_loc->form_is_section_offset ())
0971de02 16838 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16839 else if (member_loc->form_is_constant ()
4fc6c0d5 16840 || member_loc->form_is_block ())
0971de02 16841 {
435d3d88 16842 if (attr != nullptr)
0971de02
TT
16843 mark_common_block_symbol_computed (sym, die, attr,
16844 member_loc, cu);
16845 }
16846 else
16847 dwarf2_complex_location_expr_complaint ();
16848 }
16849 }
c906108c 16850 }
4357ac6c
TT
16851
16852 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16853 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16854 }
16855}
16856
0114d602 16857/* Create a type for a C++ namespace. */
d9fa45fe 16858
0114d602
DJ
16859static struct type *
16860read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16861{
5e22e966 16862 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16863 const char *previous_prefix, *name;
9219021c 16864 int is_anonymous;
0114d602
DJ
16865 struct type *type;
16866
16867 /* For extensions, reuse the type of the original namespace. */
16868 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16869 {
16870 struct die_info *ext_die;
16871 struct dwarf2_cu *ext_cu = cu;
9a619af0 16872
0114d602
DJ
16873 ext_die = dwarf2_extension (die, &ext_cu);
16874 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16875
16876 /* EXT_CU may not be the same as CU.
02142a6c 16877 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16878 return set_die_type (die, type, cu);
16879 }
9219021c 16880
e142c38c 16881 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16882
16883 /* Now build the name of the current namespace. */
16884
0114d602
DJ
16885 previous_prefix = determine_prefix (die, cu);
16886 if (previous_prefix[0] != '\0')
16887 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16888 previous_prefix, name, 0, cu);
0114d602
DJ
16889
16890 /* Create the type. */
19f392bc 16891 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16892
60531b24 16893 return set_die_type (die, type, cu);
0114d602
DJ
16894}
16895
22cee43f 16896/* Read a namespace scope. */
0114d602
DJ
16897
16898static void
16899read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16900{
5e22e966 16901 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16902 int is_anonymous;
9219021c 16903
5c4e30ca
DC
16904 /* Add a symbol associated to this if we haven't seen the namespace
16905 before. Also, add a using directive if it's an anonymous
16906 namespace. */
9219021c 16907
f2f0e013 16908 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16909 {
16910 struct type *type;
16911
0114d602 16912 type = read_type_die (die, cu);
e7c27a73 16913 new_symbol (die, type, cu);
5c4e30ca 16914
e8e80198 16915 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16916 if (is_anonymous)
0114d602
DJ
16917 {
16918 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16919
eb1e02fd 16920 std::vector<const char *> excludes;
804d2729 16921 add_using_directive (using_directives (cu),
7d93a1e0 16922 previous_prefix, type->name (), NULL,
eb1e02fd 16923 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16924 }
5c4e30ca 16925 }
9219021c 16926
639d11d3 16927 if (die->child != NULL)
d9fa45fe 16928 {
639d11d3 16929 struct die_info *child_die = die->child;
6e70227d 16930
d9fa45fe
DC
16931 while (child_die && child_die->tag)
16932 {
e7c27a73 16933 process_die (child_die, cu);
436c571c 16934 child_die = child_die->sibling;
d9fa45fe
DC
16935 }
16936 }
38d518c9
EZ
16937}
16938
f55ee35c
JK
16939/* Read a Fortran module as type. This DIE can be only a declaration used for
16940 imported module. Still we need that type as local Fortran "use ... only"
16941 declaration imports depend on the created type in determine_prefix. */
16942
16943static struct type *
16944read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16945{
5e22e966 16946 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16947 const char *module_name;
f55ee35c
JK
16948 struct type *type;
16949
16950 module_name = dwarf2_name (die, cu);
19f392bc 16951 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16952
f55ee35c
JK
16953 return set_die_type (die, type, cu);
16954}
16955
5d7cb8df
JK
16956/* Read a Fortran module. */
16957
16958static void
16959read_module (struct die_info *die, struct dwarf2_cu *cu)
16960{
16961 struct die_info *child_die = die->child;
530e8392
KB
16962 struct type *type;
16963
16964 type = read_type_die (die, cu);
16965 new_symbol (die, type, cu);
5d7cb8df 16966
5d7cb8df
JK
16967 while (child_die && child_die->tag)
16968 {
16969 process_die (child_die, cu);
436c571c 16970 child_die = child_die->sibling;
5d7cb8df
JK
16971 }
16972}
16973
38d518c9
EZ
16974/* Return the name of the namespace represented by DIE. Set
16975 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16976 namespace. */
16977
16978static const char *
e142c38c 16979namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16980{
16981 struct die_info *current_die;
16982 const char *name = NULL;
16983
16984 /* Loop through the extensions until we find a name. */
16985
16986 for (current_die = die;
16987 current_die != NULL;
f2f0e013 16988 current_die = dwarf2_extension (die, &cu))
38d518c9 16989 {
96553a0c
DE
16990 /* We don't use dwarf2_name here so that we can detect the absence
16991 of a name -> anonymous namespace. */
7d45c7c3 16992 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16993
38d518c9
EZ
16994 if (name != NULL)
16995 break;
16996 }
16997
16998 /* Is it an anonymous namespace? */
16999
17000 *is_anonymous = (name == NULL);
17001 if (*is_anonymous)
2b1dbab0 17002 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17003
17004 return name;
d9fa45fe
DC
17005}
17006
c906108c
SS
17007/* Extract all information from a DW_TAG_pointer_type DIE and add to
17008 the user defined type vector. */
17009
f792889a 17010static struct type *
e7c27a73 17011read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17012{
5e22e966 17013 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17014 struct comp_unit_head *cu_header = &cu->header;
c906108c 17015 struct type *type;
8b2dbe47
KB
17016 struct attribute *attr_byte_size;
17017 struct attribute *attr_address_class;
17018 int byte_size, addr_class;
7e314c57
JK
17019 struct type *target_type;
17020
17021 target_type = die_type (die, cu);
c906108c 17022
7e314c57
JK
17023 /* The die_type call above may have already set the type for this DIE. */
17024 type = get_die_type (die, cu);
17025 if (type)
17026 return type;
17027
17028 type = lookup_pointer_type (target_type);
8b2dbe47 17029
e142c38c 17030 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17031 if (attr_byte_size)
529908cb 17032 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17033 else
8b2dbe47
KB
17034 byte_size = cu_header->addr_size;
17035
e142c38c 17036 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17037 if (attr_address_class)
529908cb 17038 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17039 else
17040 addr_class = DW_ADDR_none;
17041
2b4424c3
TT
17042 ULONGEST alignment = get_alignment (cu, die);
17043
17044 /* If the pointer size, alignment, or address class is different
17045 than the default, create a type variant marked as such and set
17046 the length accordingly. */
17047 if (TYPE_LENGTH (type) != byte_size
17048 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17049 && alignment != TYPE_RAW_ALIGN (type))
17050 || addr_class != DW_ADDR_none)
c906108c 17051 {
5e2b427d 17052 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17053 {
314ad88d
PA
17054 type_instance_flags type_flags
17055 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17056 addr_class);
876cecd0
TT
17057 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17058 == 0);
8b2dbe47
KB
17059 type = make_type_with_address_space (type, type_flags);
17060 }
17061 else if (TYPE_LENGTH (type) != byte_size)
17062 {
b98664d3 17063 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17064 }
2b4424c3
TT
17065 else if (TYPE_RAW_ALIGN (type) != alignment)
17066 {
b98664d3 17067 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17068 " - DIE at %s [in module %s]"),
17069 sect_offset_str (die->sect_off),
5e22e966 17070 objfile_name (cu->per_objfile->objfile));
2b4424c3 17071 }
6e70227d 17072 else
9a619af0
MS
17073 {
17074 /* Should we also complain about unhandled address classes? */
17075 }
c906108c 17076 }
8b2dbe47
KB
17077
17078 TYPE_LENGTH (type) = byte_size;
2b4424c3 17079 set_type_align (type, alignment);
f792889a 17080 return set_die_type (die, type, cu);
c906108c
SS
17081}
17082
17083/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17084 the user defined type vector. */
17085
f792889a 17086static struct type *
e7c27a73 17087read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17088{
17089 struct type *type;
17090 struct type *to_type;
17091 struct type *domain;
17092
e7c27a73
DJ
17093 to_type = die_type (die, cu);
17094 domain = die_containing_type (die, cu);
0d5de010 17095
7e314c57
JK
17096 /* The calls above may have already set the type for this DIE. */
17097 type = get_die_type (die, cu);
17098 if (type)
17099 return type;
17100
78134374 17101 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17102 type = lookup_methodptr_type (to_type);
78134374 17103 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17104 {
5e22e966 17105 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17106
17107 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17108 to_type->fields (), to_type->num_fields (),
a409645d 17109 to_type->has_varargs ());
7078baeb
TT
17110 type = lookup_methodptr_type (new_type);
17111 }
0d5de010
DJ
17112 else
17113 type = lookup_memberptr_type (to_type, domain);
c906108c 17114
f792889a 17115 return set_die_type (die, type, cu);
c906108c
SS
17116}
17117
4297a3f0 17118/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17119 the user defined type vector. */
17120
f792889a 17121static struct type *
4297a3f0 17122read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17123 enum type_code refcode)
c906108c 17124{
e7c27a73 17125 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17126 struct type *type, *target_type;
c906108c
SS
17127 struct attribute *attr;
17128
4297a3f0
AV
17129 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17130
7e314c57
JK
17131 target_type = die_type (die, cu);
17132
17133 /* The die_type call above may have already set the type for this DIE. */
17134 type = get_die_type (die, cu);
17135 if (type)
17136 return type;
17137
4297a3f0 17138 type = lookup_reference_type (target_type, refcode);
e142c38c 17139 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17140 if (attr != nullptr)
c906108c 17141 {
529908cb 17142 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17143 }
17144 else
17145 {
107d2387 17146 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17147 }
2b4424c3 17148 maybe_set_alignment (cu, die, type);
f792889a 17149 return set_die_type (die, type, cu);
c906108c
SS
17150}
17151
cf363f18
MW
17152/* Add the given cv-qualifiers to the element type of the array. GCC
17153 outputs DWARF type qualifiers that apply to an array, not the
17154 element type. But GDB relies on the array element type to carry
17155 the cv-qualifiers. This mimics section 6.7.3 of the C99
17156 specification. */
17157
17158static struct type *
17159add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17160 struct type *base_type, int cnst, int voltl)
17161{
17162 struct type *el_type, *inner_array;
17163
17164 base_type = copy_type (base_type);
17165 inner_array = base_type;
17166
78134374 17167 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17168 {
17169 TYPE_TARGET_TYPE (inner_array) =
17170 copy_type (TYPE_TARGET_TYPE (inner_array));
17171 inner_array = TYPE_TARGET_TYPE (inner_array);
17172 }
17173
17174 el_type = TYPE_TARGET_TYPE (inner_array);
17175 cnst |= TYPE_CONST (el_type);
17176 voltl |= TYPE_VOLATILE (el_type);
17177 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17178
17179 return set_die_type (die, base_type, cu);
17180}
17181
f792889a 17182static struct type *
e7c27a73 17183read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17184{
f792889a 17185 struct type *base_type, *cv_type;
c906108c 17186
e7c27a73 17187 base_type = die_type (die, cu);
7e314c57
JK
17188
17189 /* The die_type call above may have already set the type for this DIE. */
17190 cv_type = get_die_type (die, cu);
17191 if (cv_type)
17192 return cv_type;
17193
2f608a3a
KW
17194 /* In case the const qualifier is applied to an array type, the element type
17195 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17196 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17197 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17198
f792889a
DJ
17199 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17200 return set_die_type (die, cv_type, cu);
c906108c
SS
17201}
17202
f792889a 17203static struct type *
e7c27a73 17204read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17205{
f792889a 17206 struct type *base_type, *cv_type;
c906108c 17207
e7c27a73 17208 base_type = die_type (die, cu);
7e314c57
JK
17209
17210 /* The die_type call above may have already set the type for this DIE. */
17211 cv_type = get_die_type (die, cu);
17212 if (cv_type)
17213 return cv_type;
17214
cf363f18
MW
17215 /* In case the volatile qualifier is applied to an array type, the
17216 element type is so qualified, not the array type (section 6.7.3
17217 of C99). */
78134374 17218 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17219 return add_array_cv_type (die, cu, base_type, 0, 1);
17220
f792889a
DJ
17221 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17222 return set_die_type (die, cv_type, cu);
c906108c
SS
17223}
17224
06d66ee9
TT
17225/* Handle DW_TAG_restrict_type. */
17226
17227static struct type *
17228read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17229{
17230 struct type *base_type, *cv_type;
17231
17232 base_type = die_type (die, cu);
17233
17234 /* The die_type call above may have already set the type for this DIE. */
17235 cv_type = get_die_type (die, cu);
17236 if (cv_type)
17237 return cv_type;
17238
17239 cv_type = make_restrict_type (base_type);
17240 return set_die_type (die, cv_type, cu);
17241}
17242
a2c2acaf
MW
17243/* Handle DW_TAG_atomic_type. */
17244
17245static struct type *
17246read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17247{
17248 struct type *base_type, *cv_type;
17249
17250 base_type = die_type (die, cu);
17251
17252 /* The die_type call above may have already set the type for this DIE. */
17253 cv_type = get_die_type (die, cu);
17254 if (cv_type)
17255 return cv_type;
17256
17257 cv_type = make_atomic_type (base_type);
17258 return set_die_type (die, cv_type, cu);
17259}
17260
c906108c
SS
17261/* Extract all information from a DW_TAG_string_type DIE and add to
17262 the user defined type vector. It isn't really a user defined type,
17263 but it behaves like one, with other DIE's using an AT_user_def_type
17264 attribute to reference it. */
17265
f792889a 17266static struct type *
e7c27a73 17267read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17268{
5e22e966 17269 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17270 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17271 struct type *type, *range_type, *index_type, *char_type;
17272 struct attribute *attr;
216a7e6b
AB
17273 struct dynamic_prop prop;
17274 bool length_is_constant = true;
17275 LONGEST length;
17276
17277 /* There are a couple of places where bit sizes might be made use of
17278 when parsing a DW_TAG_string_type, however, no producer that we know
17279 of make use of these. Handling bit sizes that are a multiple of the
17280 byte size is easy enough, but what about other bit sizes? Lets deal
17281 with that problem when we have to. Warn about these attributes being
17282 unsupported, then parse the type and ignore them like we always
17283 have. */
17284 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17285 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17286 {
17287 static bool warning_printed = false;
17288 if (!warning_printed)
17289 {
17290 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17291 "currently supported on DW_TAG_string_type."));
17292 warning_printed = true;
17293 }
17294 }
c906108c 17295
e142c38c 17296 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17297 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17298 {
17299 /* The string length describes the location at which the length of
17300 the string can be found. The size of the length field can be
17301 specified with one of the attributes below. */
17302 struct type *prop_type;
17303 struct attribute *len
17304 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17305 if (len == nullptr)
17306 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17307 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17308 {
17309 /* Pass 0 as the default as we know this attribute is constant
17310 and the default value will not be returned. */
0826b30a 17311 LONGEST sz = len->constant_value (0);
293e7e51 17312 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17313 }
17314 else
17315 {
17316 /* If the size is not specified then we assume it is the size of
17317 an address on this target. */
293e7e51 17318 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17319 }
17320
17321 /* Convert the attribute into a dynamic property. */
17322 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17323 length = 1;
17324 else
17325 length_is_constant = false;
17326 }
17327 else if (attr != nullptr)
17328 {
17329 /* This DW_AT_string_length just contains the length with no
17330 indirection. There's no need to create a dynamic property in this
17331 case. Pass 0 for the default value as we know it will not be
17332 returned in this case. */
0826b30a 17333 length = attr->constant_value (0);
216a7e6b
AB
17334 }
17335 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17336 {
216a7e6b 17337 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17338 length = attr->constant_value (1);
c906108c
SS
17339 }
17340 else
17341 {
216a7e6b
AB
17342 /* Use 1 as a fallback length if we have nothing else. */
17343 length = 1;
c906108c 17344 }
6ccb9162 17345
46bf5051 17346 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17347 if (length_is_constant)
17348 range_type = create_static_range_type (NULL, index_type, 1, length);
17349 else
17350 {
17351 struct dynamic_prop low_bound;
17352
8c2e4e06 17353 low_bound.set_const_val (1);
216a7e6b
AB
17354 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17355 }
3b7538c0
UW
17356 char_type = language_string_char_type (cu->language_defn, gdbarch);
17357 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17358
f792889a 17359 return set_die_type (die, type, cu);
c906108c
SS
17360}
17361
4d804846
JB
17362/* Assuming that DIE corresponds to a function, returns nonzero
17363 if the function is prototyped. */
17364
17365static int
17366prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17367{
17368 struct attribute *attr;
17369
17370 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17371 if (attr && attr->as_boolean ())
4d804846
JB
17372 return 1;
17373
17374 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17375 is only meaningful for C, but the concept also extends to other
4d804846
JB
17376 languages that allow unprototyped functions (Eg: Objective C).
17377 For all other languages, assume that functions are always
17378 prototyped. */
17379 if (cu->language != language_c
17380 && cu->language != language_objc
17381 && cu->language != language_opencl)
17382 return 1;
17383
17384 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17385 prototyped and unprototyped functions; default to prototyped,
17386 since that is more common in modern code (and RealView warns
17387 about unprototyped functions). */
17388 if (producer_is_realview (cu->producer))
17389 return 1;
17390
17391 return 0;
17392}
17393
c906108c
SS
17394/* Handle DIES due to C code like:
17395
17396 struct foo
c5aa993b
JM
17397 {
17398 int (*funcp)(int a, long l);
17399 int b;
17400 };
c906108c 17401
0963b4bd 17402 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17403
f792889a 17404static struct type *
e7c27a73 17405read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17406{
5e22e966 17407 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17408 struct type *type; /* Type that this function returns. */
17409 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17410 struct attribute *attr;
17411
e7c27a73 17412 type = die_type (die, cu);
7e314c57
JK
17413
17414 /* The die_type call above may have already set the type for this DIE. */
17415 ftype = get_die_type (die, cu);
17416 if (ftype)
17417 return ftype;
17418
0c8b41f1 17419 ftype = lookup_function_type (type);
c906108c 17420
4d804846 17421 if (prototyped_function_p (die, cu))
27e69b7a 17422 ftype->set_is_prototyped (true);
c906108c 17423
c055b101
CV
17424 /* Store the calling convention in the type if it's available in
17425 the subroutine die. Otherwise set the calling convention to
17426 the default value DW_CC_normal. */
17427 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 17428 if (attr != nullptr
529908cb 17429 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 17430 TYPE_CALLING_CONVENTION (ftype)
529908cb 17431 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
17432 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17433 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17434 else
17435 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17436
743649fd
MW
17437 /* Record whether the function returns normally to its caller or not
17438 if the DWARF producer set that information. */
17439 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 17440 if (attr && attr->as_boolean ())
743649fd
MW
17441 TYPE_NO_RETURN (ftype) = 1;
17442
76c10ea2
GM
17443 /* We need to add the subroutine type to the die immediately so
17444 we don't infinitely recurse when dealing with parameters
0963b4bd 17445 declared as the same subroutine type. */
76c10ea2 17446 set_die_type (die, ftype, cu);
6e70227d 17447
639d11d3 17448 if (die->child != NULL)
c906108c 17449 {
bb5ed363 17450 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17451 struct die_info *child_die;
8072405b 17452 int nparams, iparams;
c906108c
SS
17453
17454 /* Count the number of parameters.
dda83cd7
SM
17455 FIXME: GDB currently ignores vararg functions, but knows about
17456 vararg member functions. */
8072405b 17457 nparams = 0;
639d11d3 17458 child_die = die->child;
c906108c
SS
17459 while (child_die && child_die->tag)
17460 {
17461 if (child_die->tag == DW_TAG_formal_parameter)
17462 nparams++;
17463 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
17464 ftype->set_has_varargs (true);
17465
436c571c 17466 child_die = child_die->sibling;
c906108c
SS
17467 }
17468
17469 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17470 ftype->set_num_fields (nparams);
3cabb6b0
SM
17471 ftype->set_fields
17472 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17473
8072405b
JK
17474 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17475 even if we error out during the parameters reading below. */
17476 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17477 ftype->field (iparams).set_type (void_type);
8072405b
JK
17478
17479 iparams = 0;
639d11d3 17480 child_die = die->child;
c906108c
SS
17481 while (child_die && child_die->tag)
17482 {
17483 if (child_die->tag == DW_TAG_formal_parameter)
17484 {
3ce3b1ba
PA
17485 struct type *arg_type;
17486
17487 /* DWARF version 2 has no clean way to discern C++
17488 static and non-static member functions. G++ helps
17489 GDB by marking the first parameter for non-static
17490 member functions (which is the this pointer) as
17491 artificial. We pass this information to
17492 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17493
17494 DWARF version 3 added DW_AT_object_pointer, which GCC
17495 4.5 does not yet generate. */
e142c38c 17496 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17497 if (attr != nullptr)
c45bc3f8 17498 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 17499 else
9c37b5ae 17500 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17501 arg_type = die_type (child_die, cu);
17502
17503 /* RealView does not mark THIS as const, which the testsuite
17504 expects. GCC marks THIS as const in method definitions,
17505 but not in the class specifications (GCC PR 43053). */
17506 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17507 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17508 {
17509 int is_this = 0;
17510 struct dwarf2_cu *arg_cu = cu;
17511 const char *name = dwarf2_name (child_die, cu);
17512
17513 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17514 if (attr != nullptr)
3ce3b1ba
PA
17515 {
17516 /* If the compiler emits this, use it. */
17517 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17518 is_this = 1;
17519 }
17520 else if (name && strcmp (name, "this") == 0)
17521 /* Function definitions will have the argument names. */
17522 is_this = 1;
17523 else if (name == NULL && iparams == 0)
17524 /* Declarations may not have the names, so like
17525 elsewhere in GDB, assume an artificial first
17526 argument is "this". */
17527 is_this = 1;
17528
17529 if (is_this)
17530 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17531 arg_type, 0);
17532 }
17533
5d14b6e5 17534 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17535 iparams++;
17536 }
436c571c 17537 child_die = child_die->sibling;
c906108c
SS
17538 }
17539 }
17540
76c10ea2 17541 return ftype;
c906108c
SS
17542}
17543
f792889a 17544static struct type *
e7c27a73 17545read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17546{
5e22e966 17547 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17548 const char *name = NULL;
3c8e0968 17549 struct type *this_type, *target_type;
c906108c 17550
94af9270 17551 name = dwarf2_full_name (NULL, die, cu);
19f392bc 17552 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 17553 this_type->set_target_is_stub (true);
f792889a 17554 set_die_type (die, this_type, cu);
3c8e0968
DE
17555 target_type = die_type (die, cu);
17556 if (target_type != this_type)
17557 TYPE_TARGET_TYPE (this_type) = target_type;
17558 else
17559 {
17560 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17561 spec and cause infinite loops in GDB. */
b98664d3 17562 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17563 "- DIE at %s [in module %s]"),
17564 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17565 TYPE_TARGET_TYPE (this_type) = NULL;
17566 }
e4003a34
TV
17567 if (name == NULL)
17568 {
17569 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17570 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17571 Handle these by just returning the target type, rather than
17572 constructing an anonymous typedef type and trying to handle this
17573 elsewhere. */
17574 set_die_type (die, target_type, cu);
17575 return target_type;
17576 }
f792889a 17577 return this_type;
c906108c
SS
17578}
17579
a625a8c9
TT
17580/* Helper for get_dwarf2_rational_constant that computes the value of
17581 a given gmp_mpz given an attribute. */
17582
17583static void
17584get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17585{
17586 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17587 location expression that pushes an implicit value. */
17588 if (attr->form == DW_FORM_exprloc)
17589 {
17590 dwarf_block *blk = attr->as_block ();
17591 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17592 {
17593 uint64_t len;
17594 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17595 blk->data + blk->size,
17596 &len);
17597 if (ptr - blk->data + len <= blk->size)
17598 {
17599 mpz_import (value->val, len,
17600 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17601 1, 0, 0, ptr);
17602 return;
17603 }
17604 }
17605
17606 /* On failure set it to 1. */
17607 *value = gdb_mpz (1);
17608 }
17609 else if (attr->form_is_block ())
17610 {
17611 dwarf_block *blk = attr->as_block ();
17612 mpz_import (value->val, blk->size,
17613 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17614 1, 0, 0, blk->data);
17615 }
17616 else
17617 *value = gdb_mpz (attr->constant_value (1));
17618}
17619
09584414
JB
17620/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17621 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17622
17623 If the numerator and/or numerator attribute is missing,
17624 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17625 untouched. */
17626
17627static void
17628get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 17629 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
17630{
17631 struct attribute *num_attr, *denom_attr;
17632
17633 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17634 if (num_attr == nullptr)
17635 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17636 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17637
17638 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17639 if (denom_attr == nullptr)
17640 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17641 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17642
17643 if (num_attr == nullptr || denom_attr == nullptr)
17644 return;
17645
a625a8c9
TT
17646 get_mpz (cu, numerator, num_attr);
17647 get_mpz (cu, denominator, denom_attr);
09584414
JB
17648}
17649
17650/* Same as get_dwarf2_rational_constant, but extracting an unsigned
17651 rational constant, rather than a signed one.
17652
17653 If the rational constant has a negative value, a complaint
17654 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17655
17656static void
17657get_dwarf2_unsigned_rational_constant (struct die_info *die,
17658 struct dwarf2_cu *cu,
5cde1d82
TT
17659 gdb_mpz *numerator,
17660 gdb_mpz *denominator)
09584414 17661{
5cde1d82
TT
17662 gdb_mpz num (1);
17663 gdb_mpz denom (1);
09584414
JB
17664
17665 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 17666 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 17667 {
5cde1d82
TT
17668 mpz_neg (num.val, num.val);
17669 mpz_neg (denom.val, denom.val);
09584414 17670 }
5cde1d82 17671 else if (mpz_sgn (num.val) == -1)
09584414
JB
17672 {
17673 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17674 " in DIE at %s"),
17675 sect_offset_str (die->sect_off));
17676 return;
17677 }
5cde1d82 17678 else if (mpz_sgn (denom.val) == -1)
09584414
JB
17679 {
17680 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17681 " in DIE at %s"),
17682 sect_offset_str (die->sect_off));
17683 return;
17684 }
17685
5cde1d82
TT
17686 *numerator = std::move (num);
17687 *denominator = std::move (denom);
09584414
JB
17688}
17689
bbcdf9ab
TT
17690/* Assuming that ENCODING is a string whose contents starting at the
17691 K'th character is "_nn" where "nn" is a decimal number, scan that
17692 number and set RESULT to the value. K is updated to point to the
17693 character immediately following the number.
17694
17695 If the string does not conform to the format described above, false
17696 is returned, and K may or may not be changed. */
17697
17698static bool
17699ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17700{
17701 /* The next character should be an underscore ('_') followed
17702 by a digit. */
17703 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17704 return false;
17705
17706 /* Skip the underscore. */
17707 k++;
17708 int start = k;
17709
17710 /* Determine the number of digits for our number. */
17711 while (isdigit (encoding[k]))
17712 k++;
17713 if (k == start)
17714 return false;
17715
17716 std::string copy (&encoding[start], k - start);
17717 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17718 return false;
17719
17720 return true;
17721}
17722
17723/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17724 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17725 DENOM, update OFFSET, and return true on success. Return false on
17726 failure. */
17727
17728static bool
17729ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17730 gdb_mpz *num, gdb_mpz *denom)
17731{
17732 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17733 return false;
17734 return ada_get_gnat_encoded_number (encoding, offset, denom);
17735}
17736
09584414 17737/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
17738 of the corresponding TYPE by setting its type-specific data. CU is
17739 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17740 encodings. It is nullptr if the GNAT encoding should be
17741 ignored. */
09584414
JB
17742
17743static void
bbcdf9ab
TT
17744finish_fixed_point_type (struct type *type, const char *suffix,
17745 struct die_info *die, struct dwarf2_cu *cu)
09584414 17746{
09584414
JB
17747 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17748 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17749
bbcdf9ab
TT
17750 /* If GNAT encodings are preferred, don't examine the
17751 attributes. */
17752 struct attribute *attr = nullptr;
17753 if (suffix == nullptr)
17754 {
17755 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17756 if (attr == nullptr)
17757 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17758 if (attr == nullptr)
17759 attr = dwarf2_attr (die, DW_AT_small, cu);
17760 }
09584414 17761
5cde1d82
TT
17762 /* Numerator and denominator of our fixed-point type's scaling factor.
17763 The default is a scaling factor of 1, which we use as a fallback
17764 when we are not able to decode it (problem with the debugging info,
17765 unsupported forms, bug in GDB, etc...). Using that as the default
17766 allows us to at least print the unscaled value, which might still
17767 be useful to a user. */
17768 gdb_mpz scale_num (1);
17769 gdb_mpz scale_denom (1);
17770
09584414
JB
17771 if (attr == nullptr)
17772 {
bbcdf9ab
TT
17773 int offset = 0;
17774 if (suffix != nullptr
17775 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17776 &scale_denom)
17777 /* The number might be encoded as _nn_dd_nn_dd, where the
17778 second ratio is the 'small value. In this situation, we
17779 want the second value. */
17780 && (suffix[offset] != '_'
17781 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
17782 &scale_denom)))
17783 {
17784 /* Found it. */
17785 }
17786 else
17787 {
17788 /* Scaling factor not found. Assume a scaling factor of 1,
17789 and hope for the best. At least the user will be able to
17790 see the encoded value. */
17791 scale_num = 1;
17792 scale_denom = 1;
17793 complaint (_("no scale found for fixed-point type (DIE at %s)"),
17794 sect_offset_str (die->sect_off));
17795 }
09584414
JB
17796 }
17797 else if (attr->name == DW_AT_binary_scale)
17798 {
17799 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 17800 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 17801
5cde1d82 17802 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
17803 }
17804 else if (attr->name == DW_AT_decimal_scale)
17805 {
17806 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 17807 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 17808
5cde1d82 17809 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
17810 }
17811 else if (attr->name == DW_AT_small)
17812 {
17813 struct die_info *scale_die;
17814 struct dwarf2_cu *scale_cu = cu;
17815
17816 scale_die = follow_die_ref (die, attr, &scale_cu);
17817 if (scale_die->tag == DW_TAG_constant)
17818 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17819 &scale_num, &scale_denom);
17820 else
17821 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17822 " (DIE at %s)"),
17823 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17824 }
17825 else
17826 {
17827 complaint (_("unsupported scale attribute %s for fixed-point type"
17828 " (DIE at %s)"),
17829 dwarf_attr_name (attr->name),
17830 sect_offset_str (die->sect_off));
17831 }
17832
2a12c336 17833 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
17834 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17835 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
17836 mpq_canonicalize (scaling_factor.val);
17837}
17838
bbcdf9ab
TT
17839/* The gnat-encoding suffix for fixed point. */
17840
17841#define GNAT_FIXED_POINT_SUFFIX "___XF_"
17842
17843/* If NAME encodes an Ada fixed-point type, return a pointer to the
17844 "XF" suffix of the name. The text after this is what encodes the
17845 'small and 'delta information. Otherwise, return nullptr. */
17846
17847static const char *
17848gnat_encoded_fixed_point_type_info (const char *name)
17849{
17850 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17851}
17852
9b790ce7
UW
17853/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17854 (which may be different from NAME) to the architecture back-end to allow
17855 it to guess the correct format if necessary. */
17856
17857static struct type *
17858dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17859 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17860{
08feed99 17861 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17862 const struct floatformat **format;
17863 struct type *type;
17864
17865 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17866 if (format)
103a685e 17867 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17868 else
77b7c781 17869 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17870
17871 return type;
17872}
17873
eb77c9df
AB
17874/* Allocate an integer type of size BITS and name NAME. */
17875
17876static struct type *
17877dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17878 int bits, int unsigned_p, const char *name)
17879{
17880 struct type *type;
17881
17882 /* Versions of Intel's C Compiler generate an integer type called "void"
17883 instead of using DW_TAG_unspecified_type. This has been seen on
17884 at least versions 14, 17, and 18. */
35ee2dc2
AB
17885 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17886 && strcmp (name, "void") == 0)
eb77c9df
AB
17887 type = objfile_type (objfile)->builtin_void;
17888 else
17889 type = init_integer_type (objfile, bits, unsigned_p, name);
17890
17891 return type;
17892}
17893
09584414
JB
17894/* Return true if DIE has a DW_AT_small attribute whose value is
17895 a constant rational, where both the numerator and denominator
17896 are equal to zero.
17897
17898 CU is the DIE's Compilation Unit. */
17899
17900static bool
17901has_zero_over_zero_small_attribute (struct die_info *die,
17902 struct dwarf2_cu *cu)
17903{
17904 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
17905 if (attr == nullptr)
17906 return false;
17907
17908 struct dwarf2_cu *scale_cu = cu;
17909 struct die_info *scale_die
17910 = follow_die_ref (die, attr, &scale_cu);
17911
17912 if (scale_die->tag != DW_TAG_constant)
17913 return false;
17914
5cde1d82 17915 gdb_mpz num (1), denom (1);
09584414 17916 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 17917 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
17918}
17919
8bdc1658
AB
17920/* Initialise and return a floating point type of size BITS suitable for
17921 use as a component of a complex number. The NAME_HINT is passed through
17922 when initialising the floating point type and is the name of the complex
17923 type.
17924
17925 As DWARF doesn't currently provide an explicit name for the components
17926 of a complex number, but it can be helpful to have these components
17927 named, we try to select a suitable name based on the size of the
17928 component. */
17929static struct type *
17930dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17931 struct objfile *objfile,
103a685e
TT
17932 int bits, const char *name_hint,
17933 enum bfd_endian byte_order)
8bdc1658 17934{
08feed99 17935 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17936 struct type *tt = nullptr;
17937
35add35e
AB
17938 /* Try to find a suitable floating point builtin type of size BITS.
17939 We're going to use the name of this type as the name for the complex
17940 target type that we are about to create. */
1db455a7 17941 switch (cu->language)
8bdc1658 17942 {
1db455a7
AB
17943 case language_fortran:
17944 switch (bits)
17945 {
17946 case 32:
17947 tt = builtin_f_type (gdbarch)->builtin_real;
17948 break;
17949 case 64:
17950 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17951 break;
17952 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17953 case 128:
17954 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17955 break;
17956 }
8bdc1658 17957 break;
1db455a7
AB
17958 default:
17959 switch (bits)
17960 {
17961 case 32:
17962 tt = builtin_type (gdbarch)->builtin_float;
17963 break;
17964 case 64:
17965 tt = builtin_type (gdbarch)->builtin_double;
17966 break;
17967 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17968 case 128:
17969 tt = builtin_type (gdbarch)->builtin_long_double;
17970 break;
17971 }
8bdc1658
AB
17972 break;
17973 }
17974
35add35e
AB
17975 /* If the type we found doesn't match the size we were looking for, then
17976 pretend we didn't find a type at all, the complex target type we
17977 create will then be nameless. */
a12e5744 17978 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17979 tt = nullptr;
17980
7d93a1e0 17981 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17982 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17983}
17984
c906108c
SS
17985/* Find a representation of a given base type and install
17986 it in the TYPE field of the die. */
17987
f792889a 17988static struct type *
e7c27a73 17989read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17990{
5e22e966 17991 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17992 struct type *type;
17993 struct attribute *attr;
19f392bc 17994 int encoding = 0, bits = 0;
15d034d0 17995 const char *name;
34877895 17996 gdbarch *arch;
c906108c 17997
e142c38c 17998 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
17999 if (attr != nullptr && attr->form_is_constant ())
18000 encoding = attr->constant_value (0);
e142c38c 18001 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18002 if (attr != nullptr)
529908cb 18003 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18004 name = dwarf2_name (die, cu);
6ccb9162 18005 if (!name)
34877895 18006 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18007
08feed99 18008 arch = objfile->arch ();
103a685e
TT
18009 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18010
34877895 18011 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18012 if (attr != nullptr && attr->form_is_constant ())
103a685e 18013 {
529908cb 18014 int endianity = attr->constant_value (0);
103a685e
TT
18015
18016 switch (endianity)
18017 {
18018 case DW_END_big:
18019 byte_order = BFD_ENDIAN_BIG;
18020 break;
18021 case DW_END_little:
18022 byte_order = BFD_ENDIAN_LITTLE;
18023 break;
18024 default:
18025 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18026 break;
18027 }
18028 }
6ccb9162 18029
09584414
JB
18030 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18031 && cu->language == language_ada
18032 && has_zero_over_zero_small_attribute (die, cu))
18033 {
18034 /* brobecker/2018-02-24: This is a fixed point type for which
18035 the scaling factor is represented as fraction whose value
18036 does not make sense (zero divided by zero), so we should
18037 normally never see these. However, there is a small category
18038 of fixed point types for which GNAT is unable to provide
18039 the scaling factor via the standard DWARF mechanisms, and
18040 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18041 This is likely what this DIE is about. */
09584414
JB
18042 encoding = (encoding == DW_ATE_signed_fixed
18043 ? DW_ATE_signed
18044 : DW_ATE_unsigned);
18045 }
18046
bbcdf9ab
TT
18047 /* With GNAT encodings, fixed-point information will be encoded in
18048 the type name. Note that this can also occur with the above
18049 zero-over-zero case, which is why this is a separate "if" rather
18050 than an "else if". */
18051 const char *gnat_encoding_suffix = nullptr;
18052 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18053 && cu->language == language_ada
18054 && name != nullptr)
18055 {
18056 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18057 if (gnat_encoding_suffix != nullptr)
18058 {
18059 gdb_assert (startswith (gnat_encoding_suffix,
18060 GNAT_FIXED_POINT_SUFFIX));
18061 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18062 name, gnat_encoding_suffix - name);
18063 /* Use -1 here so that SUFFIX points at the "_" after the
18064 "XF". */
18065 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18066
18067 encoding = (encoding == DW_ATE_signed
18068 ? DW_ATE_signed_fixed
18069 : DW_ATE_unsigned_fixed);
18070 }
18071 }
18072
6ccb9162 18073 switch (encoding)
c906108c 18074 {
6ccb9162
UW
18075 case DW_ATE_address:
18076 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18077 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18078 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18079 break;
18080 case DW_ATE_boolean:
19f392bc 18081 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18082 break;
18083 case DW_ATE_complex_float:
103a685e
TT
18084 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18085 byte_order);
78134374 18086 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18087 {
18088 if (name == nullptr)
18089 {
18090 struct obstack *obstack
5e22e966 18091 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18092 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18093 nullptr);
18094 }
18095 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18096 }
18097 else
18098 type = init_complex_type (name, type);
6ccb9162
UW
18099 break;
18100 case DW_ATE_decimal_float:
19f392bc 18101 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18102 break;
18103 case DW_ATE_float:
103a685e 18104 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18105 break;
18106 case DW_ATE_signed:
eb77c9df 18107 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18108 break;
18109 case DW_ATE_unsigned:
3b2b8fea
TT
18110 if (cu->language == language_fortran
18111 && name
61012eef 18112 && startswith (name, "character("))
19f392bc
UW
18113 type = init_character_type (objfile, bits, 1, name);
18114 else
eb77c9df 18115 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18116 break;
18117 case DW_ATE_signed_char:
6e70227d 18118 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18119 || cu->language == language_pascal
18120 || cu->language == language_fortran)
19f392bc
UW
18121 type = init_character_type (objfile, bits, 0, name);
18122 else
eb77c9df 18123 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18124 break;
18125 case DW_ATE_unsigned_char:
868a0084 18126 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18127 || cu->language == language_pascal
c44af4eb
TT
18128 || cu->language == language_fortran
18129 || cu->language == language_rust)
19f392bc
UW
18130 type = init_character_type (objfile, bits, 1, name);
18131 else
eb77c9df 18132 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18133 break;
75079b2b 18134 case DW_ATE_UTF:
53e710ac 18135 {
53e710ac
PA
18136 if (bits == 16)
18137 type = builtin_type (arch)->builtin_char16;
18138 else if (bits == 32)
18139 type = builtin_type (arch)->builtin_char32;
18140 else
18141 {
b98664d3 18142 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18143 bits);
eb77c9df 18144 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18145 }
18146 return set_die_type (die, type, cu);
18147 }
75079b2b 18148 break;
09584414
JB
18149 case DW_ATE_signed_fixed:
18150 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18151 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18152 break;
18153 case DW_ATE_unsigned_fixed:
18154 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18155 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18156 break;
75079b2b 18157
6ccb9162 18158 default:
b98664d3 18159 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18160 dwarf_type_encoding_name (encoding));
77b7c781 18161 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18162 break;
c906108c 18163 }
6ccb9162 18164
0114d602 18165 if (name && strcmp (name, "char") == 0)
15152a54 18166 type->set_has_no_signedness (true);
0114d602 18167
2b4424c3
TT
18168 maybe_set_alignment (cu, die, type);
18169
db558e34 18170 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18171
20a5fcbd
TT
18172 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18173 {
18174 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18175 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18176 {
529908cb 18177 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18178 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18179 /* Only use the attributes if they make sense together. */
18180 if (attr == nullptr
529908cb
TT
18181 || (attr->as_unsigned () + real_bit_size
18182 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18183 {
18184 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18185 = real_bit_size;
18186 if (attr != nullptr)
18187 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18188 = attr->as_unsigned ();
20a5fcbd
TT
18189 }
18190 }
18191 }
18192
f792889a 18193 return set_die_type (die, type, cu);
c906108c
SS
18194}
18195
80180f79
SA
18196/* Parse dwarf attribute if it's a block, reference or constant and put the
18197 resulting value of the attribute into struct bound_prop.
18198 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18199
18200static int
18201attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18202 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18203 struct type *default_type)
80180f79
SA
18204{
18205 struct dwarf2_property_baton *baton;
5e22e966 18206 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18207 struct objfile *objfile = per_objfile->objfile;
18208 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18209
9a49df9d
AB
18210 gdb_assert (default_type != NULL);
18211
80180f79
SA
18212 if (attr == NULL || prop == NULL)
18213 return 0;
18214
4fc6c0d5 18215 if (attr->form_is_block ())
80180f79 18216 {
8d749320 18217 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18218 baton->property_type = default_type;
80180f79 18219 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18220 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18221
18222 struct dwarf_block *block = attr->as_block ();
18223 baton->locexpr.size = block->size;
18224 baton->locexpr.data = block->data;
216a7e6b
AB
18225 switch (attr->name)
18226 {
18227 case DW_AT_string_length:
18228 baton->locexpr.is_reference = true;
18229 break;
18230 default:
18231 baton->locexpr.is_reference = false;
18232 break;
18233 }
8c2e4e06
SM
18234
18235 prop->set_locexpr (baton);
18236 gdb_assert (prop->baton () != NULL);
80180f79 18237 }
cd6c91b4 18238 else if (attr->form_is_ref ())
80180f79
SA
18239 {
18240 struct dwarf2_cu *target_cu = cu;
18241 struct die_info *target_die;
18242 struct attribute *target_attr;
18243
18244 target_die = follow_die_ref (die, attr, &target_cu);
18245 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18246 if (target_attr == NULL)
18247 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18248 target_cu);
80180f79
SA
18249 if (target_attr == NULL)
18250 return 0;
18251
df25ebbd 18252 switch (target_attr->name)
80180f79 18253 {
df25ebbd 18254 case DW_AT_location:
cd6c91b4 18255 if (target_attr->form_is_section_offset ())
df25ebbd 18256 {
8d749320 18257 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18258 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18259 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18260 prop->set_loclist (baton);
18261 gdb_assert (prop->baton () != NULL);
df25ebbd 18262 }
4fc6c0d5 18263 else if (target_attr->form_is_block ())
df25ebbd 18264 {
8d749320 18265 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18266 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18267 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18268 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18269 struct dwarf_block *block = target_attr->as_block ();
18270 baton->locexpr.size = block->size;
18271 baton->locexpr.data = block->data;
9a49df9d 18272 baton->locexpr.is_reference = true;
8c2e4e06
SM
18273 prop->set_locexpr (baton);
18274 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18275 }
18276 else
18277 {
18278 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18279 "dynamic property");
18280 return 0;
18281 }
18282 break;
18283 case DW_AT_data_member_location:
18284 {
18285 LONGEST offset;
18286
18287 if (!handle_data_member_location (target_die, target_cu,
18288 &offset))
18289 return 0;
18290
8d749320 18291 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18292 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18293 target_cu);
df25ebbd
JB
18294 baton->offset_info.offset = offset;
18295 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18296 prop->set_addr_offset (baton);
df25ebbd
JB
18297 break;
18298 }
80180f79
SA
18299 }
18300 }
cd6c91b4 18301 else if (attr->form_is_constant ())
8c2e4e06 18302 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18303 else
18304 {
18305 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18306 dwarf2_name (die, cu));
18307 return 0;
18308 }
18309
18310 return 1;
18311}
18312
09ba997f 18313/* See read.h. */
9a49df9d 18314
09ba997f 18315struct type *
293e7e51 18316dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18317{
9a49df9d
AB
18318 struct type *int_type;
18319
18320 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18321#define TRY_TYPE(F) \
18322 int_type = (unsigned_p \
18323 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18324 : objfile_type (objfile)->builtin_ ## F); \
18325 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18326 return int_type
18327
18328 TRY_TYPE (char);
18329 TRY_TYPE (short);
18330 TRY_TYPE (int);
18331 TRY_TYPE (long);
18332 TRY_TYPE (long_long);
18333
18334#undef TRY_TYPE
18335
18336 gdb_assert_not_reached ("unable to find suitable integer type");
18337}
18338
b86352cf
AB
18339/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18340 present (which is valid) then compute the default type based on the
18341 compilation units address size. */
18342
18343static struct type *
18344read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18345{
18346 struct type *index_type = die_type (die, cu);
18347
18348 /* Dwarf-2 specifications explicitly allows to create subrange types
18349 without specifying a base type.
18350 In that case, the base type must be set to the type of
18351 the lower bound, upper bound or count, in that order, if any of these
18352 three attributes references an object that has a type.
18353 If no base type is found, the Dwarf-2 specifications say that
18354 a signed integer type of size equal to the size of an address should
18355 be used.
18356 For the following C code: `extern char gdb_int [];'
18357 GCC produces an empty range DIE.
18358 FIXME: muller/2010-05-28: Possible references to object for low bound,
18359 high bound or count are not yet handled by this code. */
78134374 18360 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18361 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18362
18363 return index_type;
18364}
18365
a02abb62
JB
18366/* Read the given DW_AT_subrange DIE. */
18367
f792889a 18368static struct type *
a02abb62
JB
18369read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18370{
4c9ad8c2 18371 struct type *base_type, *orig_base_type;
a02abb62
JB
18372 struct type *range_type;
18373 struct attribute *attr;
729efb13 18374 struct dynamic_prop low, high;
4fae6e18 18375 int low_default_is_valid;
c451ebe5 18376 int high_bound_is_count = 0;
15d034d0 18377 const char *name;
d359392f 18378 ULONGEST negative_mask;
e77813c8 18379
b86352cf
AB
18380 orig_base_type = read_subrange_index_type (die, cu);
18381
4c9ad8c2
TT
18382 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18383 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18384 creating the range type, but we use the result of check_typedef
18385 when examining properties of the type. */
18386 base_type = check_typedef (orig_base_type);
a02abb62 18387
7e314c57
JK
18388 /* The die_type call above may have already set the type for this DIE. */
18389 range_type = get_die_type (die, cu);
18390 if (range_type)
18391 return range_type;
18392
8c2e4e06 18393 high.set_const_val (0);
729efb13 18394
4fae6e18
JK
18395 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18396 omitting DW_AT_lower_bound. */
18397 switch (cu->language)
6e70227d 18398 {
4fae6e18
JK
18399 case language_c:
18400 case language_cplus:
8c2e4e06 18401 low.set_const_val (0);
4fae6e18
JK
18402 low_default_is_valid = 1;
18403 break;
18404 case language_fortran:
8c2e4e06 18405 low.set_const_val (1);
4fae6e18
JK
18406 low_default_is_valid = 1;
18407 break;
18408 case language_d:
4fae6e18 18409 case language_objc:
c44af4eb 18410 case language_rust:
8c2e4e06 18411 low.set_const_val (0);
4fae6e18
JK
18412 low_default_is_valid = (cu->header.version >= 4);
18413 break;
18414 case language_ada:
18415 case language_m2:
18416 case language_pascal:
8c2e4e06 18417 low.set_const_val (1);
4fae6e18
JK
18418 low_default_is_valid = (cu->header.version >= 4);
18419 break;
18420 default:
8c2e4e06 18421 low.set_const_val (0);
4fae6e18
JK
18422 low_default_is_valid = 0;
18423 break;
a02abb62
JB
18424 }
18425
e142c38c 18426 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18427 if (attr != nullptr)
9a49df9d 18428 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18429 else if (!low_default_is_valid)
b98664d3 18430 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18431 "- DIE at %s [in module %s]"),
18432 sect_offset_str (die->sect_off),
5e22e966 18433 objfile_name (cu->per_objfile->objfile));
a02abb62 18434
506f5c41
TV
18435 struct attribute *attr_ub, *attr_count;
18436 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18437 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18438 {
506f5c41 18439 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18440 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18441 {
c451ebe5 18442 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18443 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18444 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18445 else
18446 high_bound_is_count = 1;
c2ff108b 18447 }
506f5c41
TV
18448 else
18449 {
18450 if (attr_ub != NULL)
18451 complaint (_("Unresolved DW_AT_upper_bound "
18452 "- DIE at %s [in module %s]"),
18453 sect_offset_str (die->sect_off),
5e22e966 18454 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18455 if (attr_count != NULL)
18456 complaint (_("Unresolved DW_AT_count "
18457 "- DIE at %s [in module %s]"),
18458 sect_offset_str (die->sect_off),
5e22e966 18459 objfile_name (cu->per_objfile->objfile));
506f5c41 18460 }
e77813c8 18461 }
a02abb62 18462
4e962e74
TT
18463 LONGEST bias = 0;
18464 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18465 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18466 bias = bias_attr->constant_value (0);
4e962e74 18467
dbb9c2b1
JB
18468 /* Normally, the DWARF producers are expected to use a signed
18469 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18470 But this is unfortunately not always the case, as witnessed
18471 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18472 is used instead. To work around that ambiguity, we treat
18473 the bounds as signed, and thus sign-extend their values, when
18474 the base type is signed. */
6e70227d 18475 negative_mask =
d359392f 18476 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18477 if (low.kind () == PROP_CONST
c6d940a9 18478 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18479 low.set_const_val (low.const_val () | negative_mask);
18480 if (high.kind () == PROP_CONST
c6d940a9 18481 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18482 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18483
5bbd8269
AB
18484 /* Check for bit and byte strides. */
18485 struct dynamic_prop byte_stride_prop;
18486 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18487 if (attr_byte_stride != nullptr)
18488 {
293e7e51 18489 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18490 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18491 prop_type);
18492 }
18493
18494 struct dynamic_prop bit_stride_prop;
18495 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18496 if (attr_bit_stride != nullptr)
18497 {
18498 /* It only makes sense to have either a bit or byte stride. */
18499 if (attr_byte_stride != nullptr)
18500 {
18501 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18502 "- DIE at %s [in module %s]"),
18503 sect_offset_str (die->sect_off),
5e22e966 18504 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
18505 attr_bit_stride = nullptr;
18506 }
18507 else
18508 {
293e7e51 18509 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18510 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18511 prop_type);
18512 }
18513 }
18514
18515 if (attr_byte_stride != nullptr
18516 || attr_bit_stride != nullptr)
18517 {
18518 bool byte_stride_p = (attr_byte_stride != nullptr);
18519 struct dynamic_prop *stride
18520 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18521
18522 range_type
18523 = create_range_type_with_stride (NULL, orig_base_type, &low,
18524 &high, bias, stride, byte_stride_p);
18525 }
18526 else
18527 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18528
c451ebe5 18529 if (high_bound_is_count)
599088e3 18530 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 18531
c2ff108b
JK
18532 /* Ada expects an empty array on no boundary attributes. */
18533 if (attr == NULL && cu->language != language_ada)
8c2e4e06 18534 range_type->bounds ()->high.set_undefined ();
c2ff108b 18535
39cbfefa
DJ
18536 name = dwarf2_name (die, cu);
18537 if (name)
d0e39ea2 18538 range_type->set_name (name);
6e70227d 18539
e142c38c 18540 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18541 if (attr != nullptr)
529908cb 18542 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 18543
2b4424c3
TT
18544 maybe_set_alignment (cu, die, range_type);
18545
7e314c57
JK
18546 set_die_type (die, range_type, cu);
18547
18548 /* set_die_type should be already done. */
b4ba55a1
JB
18549 set_descriptive_type (range_type, die, cu);
18550
7e314c57 18551 return range_type;
a02abb62 18552}
6e70227d 18553
f792889a 18554static struct type *
81a17f79
JB
18555read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18556{
18557 struct type *type;
81a17f79 18558
5e22e966 18559 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 18560 type->set_name (dwarf2_name (die, cu));
81a17f79 18561
74a2f8ff 18562 /* In Ada, an unspecified type is typically used when the description
85102364 18563 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18564 such a type, we treat it as a stub, and try to resolve it later on,
18565 when needed. */
18566 if (cu->language == language_ada)
b4b73759 18567 type->set_is_stub (true);
74a2f8ff 18568
f792889a 18569 return set_die_type (die, type, cu);
81a17f79 18570}
a02abb62 18571
639d11d3
DC
18572/* Read a single die and all its descendents. Set the die's sibling
18573 field to NULL; set other fields in the die correctly, and set all
18574 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18575 location of the info_ptr after reading all of those dies. PARENT
18576 is the parent of the die in question. */
18577
18578static struct die_info *
dee91e82 18579read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18580 const gdb_byte *info_ptr,
18581 const gdb_byte **new_info_ptr,
dee91e82 18582 struct die_info *parent)
639d11d3
DC
18583{
18584 struct die_info *die;
d521ce57 18585 const gdb_byte *cur_ptr;
639d11d3 18586
3e225074 18587 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18588 if (die == NULL)
18589 {
18590 *new_info_ptr = cur_ptr;
18591 return NULL;
18592 }
93311388 18593 store_in_ref_table (die, reader->cu);
639d11d3 18594
3e225074 18595 if (die->has_children)
bf6af496 18596 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18597 else
18598 {
18599 die->child = NULL;
18600 *new_info_ptr = cur_ptr;
18601 }
18602
18603 die->sibling = NULL;
18604 die->parent = parent;
18605 return die;
18606}
18607
18608/* Read a die, all of its descendents, and all of its siblings; set
18609 all of the fields of all of the dies correctly. Arguments are as
18610 in read_die_and_children. */
18611
18612static struct die_info *
bf6af496 18613read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18614 const gdb_byte *info_ptr,
18615 const gdb_byte **new_info_ptr,
bf6af496 18616 struct die_info *parent)
639d11d3
DC
18617{
18618 struct die_info *first_die, *last_sibling;
d521ce57 18619 const gdb_byte *cur_ptr;
639d11d3 18620
c906108c 18621 cur_ptr = info_ptr;
639d11d3
DC
18622 first_die = last_sibling = NULL;
18623
18624 while (1)
c906108c 18625 {
639d11d3 18626 struct die_info *die
dee91e82 18627 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18628
1d325ec1 18629 if (die == NULL)
c906108c 18630 {
639d11d3
DC
18631 *new_info_ptr = cur_ptr;
18632 return first_die;
c906108c 18633 }
1d325ec1
DJ
18634
18635 if (!first_die)
18636 first_die = die;
c906108c 18637 else
1d325ec1
DJ
18638 last_sibling->sibling = die;
18639
18640 last_sibling = die;
c906108c 18641 }
c906108c
SS
18642}
18643
bf6af496
DE
18644/* Read a die, all of its descendents, and all of its siblings; set
18645 all of the fields of all of the dies correctly. Arguments are as
18646 in read_die_and_children.
18647 This the main entry point for reading a DIE and all its children. */
18648
18649static struct die_info *
18650read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18651 const gdb_byte *info_ptr,
18652 const gdb_byte **new_info_ptr,
bf6af496
DE
18653 struct die_info *parent)
18654{
18655 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18656 new_info_ptr, parent);
18657
b4f54984 18658 if (dwarf_die_debug)
bf6af496
DE
18659 {
18660 fprintf_unfiltered (gdb_stdlog,
18661 "Read die from %s@0x%x of %s:\n",
96b79293 18662 reader->die_section->get_name (),
bf6af496
DE
18663 (unsigned) (info_ptr - reader->die_section->buffer),
18664 bfd_get_filename (reader->abfd));
b4f54984 18665 dump_die (die, dwarf_die_debug);
bf6af496
DE
18666 }
18667
18668 return die;
18669}
18670
3019eac3
DE
18671/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18672 attributes.
18673 The caller is responsible for filling in the extra attributes
18674 and updating (*DIEP)->num_attrs.
18675 Set DIEP to point to a newly allocated die with its information,
3e225074 18676 except for its child, sibling, and parent fields. */
93311388 18677
d521ce57 18678static const gdb_byte *
3019eac3 18679read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18680 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18681 int num_extra_attrs)
93311388 18682{
b64f50a1 18683 unsigned int abbrev_number, bytes_read, i;
7c32eebb 18684 const struct abbrev_info *abbrev;
93311388
DE
18685 struct die_info *die;
18686 struct dwarf2_cu *cu = reader->cu;
18687 bfd *abfd = reader->abfd;
18688
9c541725 18689 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18690 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18691 info_ptr += bytes_read;
18692 if (!abbrev_number)
18693 {
18694 *diep = NULL;
93311388
DE
18695 return info_ptr;
18696 }
18697
685af9cd 18698 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18699 if (!abbrev)
348e048f
DE
18700 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18701 abbrev_number,
18702 bfd_get_filename (abfd));
18703
3019eac3 18704 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18705 die->sect_off = sect_off;
93311388
DE
18706 die->tag = abbrev->tag;
18707 die->abbrev = abbrev_number;
3e225074 18708 die->has_children = abbrev->has_children;
93311388 18709
3019eac3
DE
18710 /* Make the result usable.
18711 The caller needs to update num_attrs after adding the extra
18712 attributes. */
93311388
DE
18713 die->num_attrs = abbrev->num_attrs;
18714
7a5f294d 18715 bool any_need_reprocess = false;
93311388 18716 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 18717 {
7a5f294d
TT
18718 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18719 info_ptr);
18720 if (die->attrs[i].requires_reprocessing_p ())
18721 any_need_reprocess = true;
18a8505e
AT
18722 }
18723
052c8bb8 18724 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
18725 if (attr != nullptr && attr->form_is_unsigned ())
18726 cu->str_offsets_base = attr->as_unsigned ();
93311388 18727
41144253 18728 attr = die->attr (DW_AT_loclists_base);
18729 if (attr != nullptr)
529908cb 18730 cu->loclist_base = attr->as_unsigned ();
41144253 18731
a39fdb41 18732 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18733 if (maybe_addr_base.has_value ())
18734 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
18735
18736 attr = die->attr (DW_AT_rnglists_base);
18737 if (attr != nullptr)
2b0c7f41 18738 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 18739
7a5f294d
TT
18740 if (any_need_reprocess)
18741 {
18742 for (i = 0; i < abbrev->num_attrs; ++i)
18743 {
18744 if (die->attrs[i].requires_reprocessing_p ())
18745 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18746 }
18747 }
93311388 18748 *diep = die;
93311388
DE
18749 return info_ptr;
18750}
18751
3019eac3
DE
18752/* Read a die and all its attributes.
18753 Set DIEP to point to a newly allocated die with its information,
3e225074 18754 except for its child, sibling, and parent fields. */
3019eac3 18755
d521ce57 18756static const gdb_byte *
3019eac3 18757read_full_die (const struct die_reader_specs *reader,
3e225074 18758 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18759{
d521ce57 18760 const gdb_byte *result;
bf6af496 18761
3e225074 18762 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18763
b4f54984 18764 if (dwarf_die_debug)
bf6af496
DE
18765 {
18766 fprintf_unfiltered (gdb_stdlog,
18767 "Read die from %s@0x%x of %s:\n",
96b79293 18768 reader->die_section->get_name (),
bf6af496
DE
18769 (unsigned) (info_ptr - reader->die_section->buffer),
18770 bfd_get_filename (reader->abfd));
b4f54984 18771 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18772 }
18773
18774 return result;
3019eac3 18775}
433df2d4 18776\f
c906108c 18777
72bf9492
DJ
18778/* Returns nonzero if TAG represents a type that we might generate a partial
18779 symbol for. */
18780
18781static int
d8f62e84 18782is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
18783{
18784 switch (tag)
18785 {
18786#if 0
18787 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
18788 that we don't at present. Note that normally this does not
18789 matter, mainly because C compilers don't give names to these
18790 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
18791 case DW_TAG_file_type:
18792 case DW_TAG_ptr_to_member_type:
18793 case DW_TAG_set_type:
18794 case DW_TAG_string_type:
18795 case DW_TAG_subroutine_type:
18796#endif
d8f62e84
TT
18797
18798 /* GNAT may emit an array with a name, but no typedef, so we
18799 need to make a symbol in this case. */
18800 case DW_TAG_array_type:
18801 return lang == language_ada;
18802
72bf9492
DJ
18803 case DW_TAG_base_type:
18804 case DW_TAG_class_type:
680b30c7 18805 case DW_TAG_interface_type:
72bf9492
DJ
18806 case DW_TAG_enumeration_type:
18807 case DW_TAG_structure_type:
18808 case DW_TAG_subrange_type:
18809 case DW_TAG_typedef:
18810 case DW_TAG_union_type:
18811 return 1;
18812 default:
18813 return 0;
18814 }
18815}
18816
18817/* Load all DIEs that are interesting for partial symbols into memory. */
18818
18819static struct partial_die_info *
dee91e82 18820load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18821 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18822{
dee91e82 18823 struct dwarf2_cu *cu = reader->cu;
5e22e966 18824 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18825 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18826 unsigned int bytes_read;
5afb4e99 18827 unsigned int load_all = 0;
72bf9492
DJ
18828 int nesting_level = 1;
18829
18830 parent_die = NULL;
18831 last_die = NULL;
18832
7adf1e79
DE
18833 gdb_assert (cu->per_cu != NULL);
18834 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18835 load_all = 1;
18836
72bf9492
DJ
18837 cu->partial_dies
18838 = htab_create_alloc_ex (cu->header.length / 12,
18839 partial_die_hash,
18840 partial_die_eq,
18841 NULL,
18842 &cu->comp_unit_obstack,
18843 hashtab_obstack_allocate,
18844 dummy_obstack_deallocate);
18845
72bf9492
DJ
18846 while (1)
18847 {
7c32eebb
TT
18848 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18849 &bytes_read);
72bf9492
DJ
18850
18851 /* A NULL abbrev means the end of a series of children. */
18852 if (abbrev == NULL)
18853 {
18854 if (--nesting_level == 0)
cd9983dd
YQ
18855 return first_die;
18856
72bf9492
DJ
18857 info_ptr += bytes_read;
18858 last_die = parent_die;
18859 parent_die = parent_die->die_parent;
18860 continue;
18861 }
18862
98bfdba5
PA
18863 /* Check for template arguments. We never save these; if
18864 they're seen, we just mark the parent, and go on our way. */
18865 if (parent_die != NULL
18866 && cu->language == language_cplus
18867 && (abbrev->tag == DW_TAG_template_type_param
18868 || abbrev->tag == DW_TAG_template_value_param))
18869 {
18870 parent_die->has_template_arguments = 1;
18871
18872 if (!load_all)
18873 {
18874 /* We don't need a partial DIE for the template argument. */
dee91e82 18875 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18876 continue;
18877 }
18878 }
18879
0d99eb77 18880 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18881 Skip their other children. */
18882 if (!load_all
18883 && cu->language == language_cplus
18884 && parent_die != NULL
f9b5d5ea
TV
18885 && parent_die->tag == DW_TAG_subprogram
18886 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 18887 {
dee91e82 18888 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18889 continue;
18890 }
18891
5afb4e99
DJ
18892 /* Check whether this DIE is interesting enough to save. Normally
18893 we would not be interested in members here, but there may be
18894 later variables referencing them via DW_AT_specification (for
18895 static members). */
18896 if (!load_all
d8f62e84 18897 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 18898 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18899 && abbrev->tag != DW_TAG_enumerator
18900 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18901 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18902 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18903 && abbrev->tag != DW_TAG_variable
5afb4e99 18904 && abbrev->tag != DW_TAG_namespace
f55ee35c 18905 && abbrev->tag != DW_TAG_module
95554aad 18906 && abbrev->tag != DW_TAG_member
74921315
KS
18907 && abbrev->tag != DW_TAG_imported_unit
18908 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18909 {
18910 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18911 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18912 continue;
18913 }
18914
6f06d47b
YQ
18915 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18916 abbrev);
cd9983dd 18917
48fbe735 18918 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18919
18920 /* This two-pass algorithm for processing partial symbols has a
18921 high cost in cache pressure. Thus, handle some simple cases
18922 here which cover the majority of C partial symbols. DIEs
18923 which neither have specification tags in them, nor could have
18924 specification tags elsewhere pointing at them, can simply be
18925 processed and discarded.
18926
18927 This segment is also optional; scan_partial_symbols and
18928 add_partial_symbol will handle these DIEs if we chain
18929 them in normally. When compilers which do not emit large
18930 quantities of duplicate debug information are more common,
18931 this code can probably be removed. */
18932
18933 /* Any complete simple types at the top level (pretty much all
18934 of them, for a language without namespaces), can be processed
18935 directly. */
18936 if (parent_die == NULL
cd9983dd
YQ
18937 && pdi.has_specification == 0
18938 && pdi.is_declaration == 0
18939 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18940 || pdi.tag == DW_TAG_base_type
d8f62e84 18941 || pdi.tag == DW_TAG_array_type
cd9983dd 18942 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18943 {
7d00ffec 18944 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
18945 add_partial_symbol (&pdi, cu);
18946
cd9983dd 18947 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18948 continue;
18949 }
18950
d8228535
JK
18951 /* The exception for DW_TAG_typedef with has_children above is
18952 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18953 type_name_or_error will error on such types later.
d8228535
JK
18954
18955 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18956 it could not find the child DIEs referenced later, this is checked
18957 above. In correct DWARF DW_TAG_typedef should have no children. */
18958
cd9983dd 18959 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18960 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18961 "- DIE at %s [in module %s]"),
cd9983dd 18962 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18963
72bf9492
DJ
18964 /* If we're at the second level, and we're an enumerator, and
18965 our parent has no specification (meaning possibly lives in a
18966 namespace elsewhere), then we can add the partial symbol now
18967 instead of queueing it. */
cd9983dd 18968 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18969 && parent_die != NULL
18970 && parent_die->die_parent == NULL
18971 && parent_die->tag == DW_TAG_enumeration_type
18972 && parent_die->has_specification == 0)
18973 {
7d00ffec 18974 if (pdi.raw_name == NULL)
b98664d3 18975 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18976 else if (building_psymtab)
f0fbb768 18977 add_partial_symbol (&pdi, cu);
72bf9492 18978
cd9983dd 18979 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18980 continue;
18981 }
18982
cd9983dd 18983 struct partial_die_info *part_die
6f06d47b 18984 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18985
72bf9492
DJ
18986 /* We'll save this DIE so link it in. */
18987 part_die->die_parent = parent_die;
18988 part_die->die_sibling = NULL;
18989 part_die->die_child = NULL;
18990
18991 if (last_die && last_die == parent_die)
18992 last_die->die_child = part_die;
18993 else if (last_die)
18994 last_die->die_sibling = part_die;
18995
18996 last_die = part_die;
18997
18998 if (first_die == NULL)
18999 first_die = part_die;
19000
19001 /* Maybe add the DIE to the hash table. Not all DIEs that we
19002 find interesting need to be in the hash table, because we
19003 also have the parent/sibling/child chains; only those that we
19004 might refer to by offset later during partial symbol reading.
19005
19006 For now this means things that might have be the target of a
19007 DW_AT_specification, DW_AT_abstract_origin, or
19008 DW_AT_extension. DW_AT_extension will refer only to
19009 namespaces; DW_AT_abstract_origin refers to functions (and
19010 many things under the function DIE, but we do not recurse
19011 into function DIEs during partial symbol reading) and
19012 possibly variables as well; DW_AT_specification refers to
19013 declarations. Declarations ought to have the DW_AT_declaration
19014 flag. It happens that GCC forgets to put it in sometimes, but
19015 only for functions, not for types.
19016
19017 Adding more things than necessary to the hash table is harmless
19018 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19019 wasted time in find_partial_die, when we reread the compilation
19020 unit with load_all_dies set. */
72bf9492 19021
5afb4e99 19022 if (load_all
72929c62 19023 || abbrev->tag == DW_TAG_constant
5afb4e99 19024 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19025 || abbrev->tag == DW_TAG_variable
19026 || abbrev->tag == DW_TAG_namespace
19027 || part_die->is_declaration)
19028 {
19029 void **slot;
19030
19031 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19032 to_underlying (part_die->sect_off),
19033 INSERT);
72bf9492
DJ
19034 *slot = part_die;
19035 }
19036
72bf9492 19037 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19038 we have no reason to follow the children of structures; for other
98bfdba5
PA
19039 languages we have to, so that we can get at method physnames
19040 to infer fully qualified class names, for DW_AT_specification,
19041 and for C++ template arguments. For C++, we also look one level
19042 inside functions to find template arguments (if the name of the
19043 function does not already contain the template arguments).
bc30ff58 19044
0a4b0913
AB
19045 For Ada and Fortran, we need to scan the children of subprograms
19046 and lexical blocks as well because these languages allow the
19047 definition of nested entities that could be interesting for the
19048 debugger, such as nested subprograms for instance. */
72bf9492 19049 if (last_die->has_children
5afb4e99
DJ
19050 && (load_all
19051 || last_die->tag == DW_TAG_namespace
f55ee35c 19052 || last_die->tag == DW_TAG_module
72bf9492 19053 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19054 || (cu->language == language_cplus
19055 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19056 && (last_die->raw_name == NULL
19057 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19058 || (cu->language != language_c
19059 && (last_die->tag == DW_TAG_class_type
680b30c7 19060 || last_die->tag == DW_TAG_interface_type
72bf9492 19061 || last_die->tag == DW_TAG_structure_type
bc30ff58 19062 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19063 || ((cu->language == language_ada
19064 || cu->language == language_fortran)
bc30ff58
JB
19065 && (last_die->tag == DW_TAG_subprogram
19066 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19067 {
19068 nesting_level++;
19069 parent_die = last_die;
19070 continue;
19071 }
19072
19073 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19074 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19075
19076 /* Back to the top, do it again. */
19077 }
19078}
19079
6f06d47b 19080partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19081 const struct abbrev_info *abbrev)
6f06d47b
YQ
19082 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19083{
19084}
19085
7d00ffec
TT
19086/* See class definition. */
19087
19088const char *
19089partial_die_info::name (dwarf2_cu *cu)
19090{
19091 if (!canonical_name && raw_name != nullptr)
19092 {
19093 struct objfile *objfile = cu->per_objfile->objfile;
19094 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19095 canonical_name = 1;
19096 }
19097
19098 return raw_name;
19099}
19100
35cc7ed7
YQ
19101/* Read a minimal amount of information into the minimal die structure.
19102 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19103
48fbe735
YQ
19104const gdb_byte *
19105partial_die_info::read (const struct die_reader_specs *reader,
19106 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19107{
dee91e82 19108 struct dwarf2_cu *cu = reader->cu;
976ca316 19109 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19110 unsigned int i;
c5aa993b 19111 int has_low_pc_attr = 0;
c906108c 19112 int has_high_pc_attr = 0;
91da1414 19113 int high_pc_relative = 0;
c906108c 19114
fd0a254f 19115 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19116 {
e7da7f8f 19117 attribute attr;
7a5f294d 19118 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19119 /* String and address offsets that need to do the reprocessing have
dda83cd7 19120 already been read at this point, so there is no need to wait until
18a8505e 19121 the loop terminates to do the reprocessing. */
7a5f294d 19122 if (attr.requires_reprocessing_p ())
d0ce17d8 19123 read_attribute_reprocess (reader, &attr, tag);
c906108c 19124 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19125 partial symbol table. */
c906108c
SS
19126 switch (attr.name)
19127 {
19128 case DW_AT_name:
48fbe735 19129 switch (tag)
71c25dea
TT
19130 {
19131 case DW_TAG_compile_unit:
95554aad 19132 case DW_TAG_partial_unit:
348e048f 19133 case DW_TAG_type_unit:
71c25dea
TT
19134 /* Compilation units have a DW_AT_name that is a filename, not
19135 a source language identifier. */
19136 case DW_TAG_enumeration_type:
19137 case DW_TAG_enumerator:
19138 /* These tags always have simple identifiers already; no need
19139 to canonicalize them. */
7d00ffec 19140 canonical_name = 1;
2c830f54 19141 raw_name = attr.as_string ();
71c25dea
TT
19142 break;
19143 default:
7d00ffec 19144 canonical_name = 0;
2c830f54 19145 raw_name = attr.as_string ();
71c25dea
TT
19146 break;
19147 }
c906108c 19148 break;
31ef98ae 19149 case DW_AT_linkage_name:
c906108c 19150 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19151 /* Note that both forms of linkage name might appear. We
19152 assume they will be the same, and we only store the last
19153 one we see. */
95f982e5 19154 linkage_name = attr.as_string ();
c906108c
SS
19155 break;
19156 case DW_AT_low_pc:
19157 has_low_pc_attr = 1;
95f982e5 19158 lowpc = attr.as_address ();
c906108c
SS
19159 break;
19160 case DW_AT_high_pc:
19161 has_high_pc_attr = 1;
95f982e5 19162 highpc = attr.as_address ();
cd6c91b4 19163 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19164 high_pc_relative = 1;
c906108c
SS
19165 break;
19166 case DW_AT_location:
dda83cd7
SM
19167 /* Support the .debug_loc offsets. */
19168 if (attr.form_is_block ())
19169 {
9d2246fc 19170 d.locdesc = attr.as_block ();
dda83cd7
SM
19171 }
19172 else if (attr.form_is_section_offset ())
19173 {
4d3c2250 19174 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19175 }
19176 else
19177 {
4d3c2250
KB
19178 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19179 "partial symbol information");
dda83cd7 19180 }
c906108c 19181 break;
c906108c 19182 case DW_AT_external:
c45bc3f8 19183 is_external = attr.as_boolean ();
c906108c
SS
19184 break;
19185 case DW_AT_declaration:
c45bc3f8 19186 is_declaration = attr.as_boolean ();
c906108c
SS
19187 break;
19188 case DW_AT_type:
48fbe735 19189 has_type = 1;
c906108c
SS
19190 break;
19191 case DW_AT_abstract_origin:
19192 case DW_AT_specification:
72bf9492 19193 case DW_AT_extension:
48fbe735 19194 has_specification = 1;
0826b30a 19195 spec_offset = attr.get_ref_die_offset ();
48fbe735 19196 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19197 || cu->per_cu->is_dwz);
c906108c
SS
19198 break;
19199 case DW_AT_sibling:
19200 /* Ignore absolute siblings, they might point outside of
19201 the current compile unit. */
19202 if (attr.form == DW_FORM_ref_addr)
b98664d3 19203 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19204 else
b9502d3f 19205 {
48fbe735 19206 const gdb_byte *buffer = reader->buffer;
0826b30a 19207 sect_offset off = attr.get_ref_die_offset ();
9c541725 19208 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19209
19210 if (sibling_ptr < info_ptr)
b98664d3 19211 complaint (_("DW_AT_sibling points backwards"));
22869d73 19212 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19213 reader->die_section->overflow_complaint ();
b9502d3f 19214 else
48fbe735 19215 sibling = sibling_ptr;
b9502d3f 19216 }
c906108c 19217 break;
dda83cd7
SM
19218 case DW_AT_byte_size:
19219 has_byte_size = 1;
19220 break;
19221 case DW_AT_const_value:
19222 has_const_value = 1;
19223 break;
68511cec
CES
19224 case DW_AT_calling_convention:
19225 /* DWARF doesn't provide a way to identify a program's source-level
19226 entry point. DW_AT_calling_convention attributes are only meant
19227 to describe functions' calling conventions.
19228
19229 However, because it's a necessary piece of information in
0c1b455e
TT
19230 Fortran, and before DWARF 4 DW_CC_program was the only
19231 piece of debugging information whose definition refers to
19232 a 'main program' at all, several compilers marked Fortran
19233 main programs with DW_CC_program --- even when those
19234 functions use the standard calling conventions.
19235
19236 Although DWARF now specifies a way to provide this
19237 information, we support this practice for backward
19238 compatibility. */
529908cb 19239 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19240 && cu->language == language_fortran)
48fbe735 19241 main_subprogram = 1;
68511cec 19242 break;
481860b3 19243 case DW_AT_inline:
529908cb
TT
19244 {
19245 LONGEST value = attr.constant_value (-1);
19246 if (value == DW_INL_inlined
19247 || value == DW_INL_declared_inlined)
19248 may_be_inlined = 1;
19249 }
481860b3 19250 break;
95554aad
TT
19251
19252 case DW_AT_import:
48fbe735 19253 if (tag == DW_TAG_imported_unit)
36586728 19254 {
0826b30a 19255 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19256 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19257 || cu->per_cu->is_dwz);
19258 }
95554aad
TT
19259 break;
19260
0c1b455e 19261 case DW_AT_main_subprogram:
c45bc3f8 19262 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19263 break;
19264
05caa1d2
TT
19265 case DW_AT_ranges:
19266 {
2b0c7f41
SM
19267 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19268 on DWARF version). */
19269 ULONGEST ranges_offset = attr.as_unsigned ();
19270
19271 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19272 this value. */
19273 if (tag != DW_TAG_compile_unit)
19274 ranges_offset += cu->gnu_ranges_base;
19275
05caa1d2 19276 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19277 nullptr, tag))
05caa1d2
TT
19278 has_pc_info = 1;
19279 }
19280 break;
19281
c906108c
SS
19282 default:
19283 break;
19284 }
19285 }
19286
10d06d82
TT
19287 /* For Ada, if both the name and the linkage name appear, we prefer
19288 the latter. This lets "catch exception" work better, regardless
19289 of the order in which the name and linkage name were emitted.
19290 Really, though, this is just a workaround for the fact that gdb
19291 doesn't store both the name and the linkage name. */
19292 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19293 raw_name = linkage_name;
10d06d82 19294
91da1414 19295 if (high_pc_relative)
48fbe735 19296 highpc += lowpc;
91da1414 19297
9373cf26
JK
19298 if (has_low_pc_attr && has_high_pc_attr)
19299 {
19300 /* When using the GNU linker, .gnu.linkonce. sections are used to
19301 eliminate duplicate copies of functions and vtables and such.
19302 The linker will arbitrarily choose one and discard the others.
19303 The AT_*_pc values for such functions refer to local labels in
19304 these sections. If the section from that file was discarded, the
19305 labels are not in the output, so the relocs get a value of 0.
19306 If this is a discarded function, mark the pc bounds as invalid,
19307 so that GDB will ignore it. */
976ca316 19308 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19309 {
976ca316 19310 struct objfile *objfile = per_objfile->objfile;
08feed99 19311 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19312
b98664d3 19313 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19314 "for DIE at %s [in module %s]"),
48fbe735
YQ
19315 paddress (gdbarch, lowpc),
19316 sect_offset_str (sect_off),
9d8780f0 19317 objfile_name (objfile));
9373cf26
JK
19318 }
19319 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19320 else if (lowpc >= highpc)
9373cf26 19321 {
976ca316 19322 struct objfile *objfile = per_objfile->objfile;
08feed99 19323 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19324
b98664d3 19325 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19326 "for DIE at %s [in module %s]"),
48fbe735
YQ
19327 paddress (gdbarch, lowpc),
19328 paddress (gdbarch, highpc),
19329 sect_offset_str (sect_off),
9c541725 19330 objfile_name (objfile));
9373cf26
JK
19331 }
19332 else
48fbe735 19333 has_pc_info = 1;
9373cf26 19334 }
85cbf3d3 19335
c906108c
SS
19336 return info_ptr;
19337}
19338
72bf9492
DJ
19339/* Find a cached partial DIE at OFFSET in CU. */
19340
d590ff25
YQ
19341struct partial_die_info *
19342dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19343{
19344 struct partial_die_info *lookup_die = NULL;
6f06d47b 19345 struct partial_die_info part_die (sect_off);
72bf9492 19346
9a3c8263 19347 lookup_die = ((struct partial_die_info *)
d590ff25 19348 htab_find_with_hash (partial_dies, &part_die,
9c541725 19349 to_underlying (sect_off)));
72bf9492 19350
72bf9492
DJ
19351 return lookup_die;
19352}
19353
348e048f
DE
19354/* Find a partial DIE at OFFSET, which may or may not be in CU,
19355 except in the case of .debug_types DIEs which do not reference
19356 outside their CU (they do however referencing other types via
55f1336d 19357 DW_FORM_ref_sig8). */
72bf9492 19358
122cf0f2 19359static const struct cu_partial_die_info
9c541725 19360find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19361{
976ca316
SM
19362 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19363 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19364 struct partial_die_info *pd = NULL;
72bf9492 19365
36586728 19366 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19367 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19368 {
d590ff25 19369 pd = cu->find_partial_die (sect_off);
5afb4e99 19370 if (pd != NULL)
fb816e8b 19371 return { cu, pd };
0d99eb77
DE
19372 /* We missed recording what we needed.
19373 Load all dies and try again. */
5afb4e99 19374 }
0d99eb77
DE
19375 else
19376 {
19377 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19378 if (cu->per_cu->is_debug_types)
0d99eb77 19379 {
9d8780f0
SM
19380 error (_("Dwarf Error: Type Unit at offset %s contains"
19381 " external reference to offset %s [in module %s].\n"),
19382 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19383 bfd_get_filename (objfile->obfd));
19384 }
7188ed02
SM
19385 dwarf2_per_cu_data *per_cu
19386 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19387 per_objfile);
72bf9492 19388
976ca316 19389 cu = per_objfile->get_cu (per_cu);
7188ed02 19390 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19391 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19392
976ca316 19393 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19394
19395 cu->last_used = 0;
19396 pd = cu->find_partial_die (sect_off);
0d99eb77 19397 }
5afb4e99 19398
dee91e82
DE
19399 /* If we didn't find it, and not all dies have been loaded,
19400 load them all and try again. */
19401
7188ed02 19402 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 19403 {
7188ed02 19404 cu->per_cu->load_all_dies = 1;
fd820528
DE
19405
19406 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19407 THIS_CU->cu may already be in use. So we can't just free it and
19408 replace its DIEs with the ones we read in. Instead, we leave those
19409 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19410 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19411 set. */
976ca316 19412 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19413
7188ed02 19414 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19415 }
19416
19417 if (pd == NULL)
521894aa 19418 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 19419 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19420 return { cu, pd };
72bf9492
DJ
19421}
19422
abc72ce4
DE
19423/* See if we can figure out if the class lives in a namespace. We do
19424 this by looking for a member function; its demangled name will
19425 contain namespace info, if there is any. */
19426
19427static void
19428guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19429 struct dwarf2_cu *cu)
19430{
19431 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19432 what template types look like, because the demangler
19433 frequently doesn't give the same name as the debug info. We
19434 could fix this by only using the demangled name to get the
19435 prefix (but see comment in read_structure_type). */
19436
19437 struct partial_die_info *real_pdi;
19438 struct partial_die_info *child_pdi;
19439
19440 /* If this DIE (this DIE's specification, if any) has a parent, then
19441 we should not do this. We'll prepend the parent's fully qualified
19442 name when we create the partial symbol. */
19443
19444 real_pdi = struct_pdi;
19445 while (real_pdi->has_specification)
fb816e8b 19446 {
122cf0f2
AB
19447 auto res = find_partial_die (real_pdi->spec_offset,
19448 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19449 real_pdi = res.pdi;
19450 cu = res.cu;
19451 }
abc72ce4
DE
19452
19453 if (real_pdi->die_parent != NULL)
19454 return;
19455
19456 for (child_pdi = struct_pdi->die_child;
19457 child_pdi != NULL;
19458 child_pdi = child_pdi->die_sibling)
19459 {
19460 if (child_pdi->tag == DW_TAG_subprogram
19461 && child_pdi->linkage_name != NULL)
19462 {
43816ebc 19463 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19464 (cu->language_defn->class_name_from_physname
19465 (child_pdi->linkage_name));
abc72ce4
DE
19466 if (actual_class_name != NULL)
19467 {
5e22e966 19468 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19469 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19470 struct_pdi->canonical_name = 1;
abc72ce4
DE
19471 }
19472 break;
19473 }
19474 }
19475}
19476
25c11aca
TV
19477/* Return true if a DIE with TAG may have the DW_AT_const_value
19478 attribute. */
19479
19480static bool
19481can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19482{
19483 switch (tag)
19484 {
19485 case DW_TAG_constant:
19486 case DW_TAG_enumerator:
19487 case DW_TAG_formal_parameter:
19488 case DW_TAG_template_value_param:
19489 case DW_TAG_variable:
19490 return true;
19491 }
19492
19493 return false;
19494}
19495
52356b79
YQ
19496void
19497partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19498{
abc72ce4
DE
19499 /* Once we've fixed up a die, there's no point in doing so again.
19500 This also avoids a memory leak if we were to call
19501 guess_partial_die_structure_name multiple times. */
52356b79 19502 if (fixup_called)
abc72ce4
DE
19503 return;
19504
72bf9492
DJ
19505 /* If we found a reference attribute and the DIE has no name, try
19506 to find a name in the referred to DIE. */
19507
7d00ffec 19508 if (raw_name == NULL && has_specification)
72bf9492
DJ
19509 {
19510 struct partial_die_info *spec_die;
72bf9492 19511
122cf0f2 19512 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19513 spec_die = res.pdi;
19514 cu = res.cu;
72bf9492 19515
52356b79 19516 spec_die->fixup (cu);
72bf9492 19517
7d00ffec 19518 if (spec_die->raw_name)
72bf9492 19519 {
7d00ffec
TT
19520 raw_name = spec_die->raw_name;
19521 canonical_name = spec_die->canonical_name;
72bf9492
DJ
19522
19523 /* Copy DW_AT_external attribute if it is set. */
19524 if (spec_die->is_external)
52356b79 19525 is_external = spec_die->is_external;
72bf9492
DJ
19526 }
19527 }
19528
25c11aca
TV
19529 if (!has_const_value && has_specification
19530 && can_have_DW_AT_const_value_p (tag))
19531 {
19532 struct partial_die_info *spec_die;
19533
19534 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19535 spec_die = res.pdi;
19536 cu = res.cu;
19537
19538 spec_die->fixup (cu);
19539
19540 if (spec_die->has_const_value)
19541 {
19542 /* Copy DW_AT_const_value attribute if it is set. */
19543 has_const_value = spec_die->has_const_value;
19544 }
19545 }
19546
72bf9492 19547 /* Set default names for some unnamed DIEs. */
72bf9492 19548
7d00ffec
TT
19549 if (raw_name == NULL && tag == DW_TAG_namespace)
19550 {
19551 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19552 canonical_name = 1;
19553 }
72bf9492 19554
abc72ce4
DE
19555 /* If there is no parent die to provide a namespace, and there are
19556 children, see if we can determine the namespace from their linkage
122d1940 19557 name. */
abc72ce4 19558 if (cu->language == language_cplus
5e22e966 19559 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
19560 && die_parent == NULL
19561 && has_children
19562 && (tag == DW_TAG_class_type
19563 || tag == DW_TAG_structure_type
19564 || tag == DW_TAG_union_type))
19565 guess_partial_die_structure_name (this, cu);
abc72ce4 19566
53832f31
TT
19567 /* GCC might emit a nameless struct or union that has a linkage
19568 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 19569 if (raw_name == NULL
52356b79
YQ
19570 && (tag == DW_TAG_class_type
19571 || tag == DW_TAG_interface_type
19572 || tag == DW_TAG_structure_type
19573 || tag == DW_TAG_union_type)
19574 && linkage_name != NULL)
53832f31 19575 {
43816ebc
TT
19576 gdb::unique_xmalloc_ptr<char> demangled
19577 (gdb_demangle (linkage_name, DMGL_TYPES));
19578 if (demangled != nullptr)
53832f31 19579 {
96408a79
SA
19580 const char *base;
19581
19582 /* Strip any leading namespaces/classes, keep only the base name.
19583 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19584 base = strrchr (demangled.get (), ':');
19585 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19586 base++;
19587 else
43816ebc 19588 base = demangled.get ();
96408a79 19589
5e22e966 19590 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19591 raw_name = objfile->intern (base);
19592 canonical_name = 1;
53832f31
TT
19593 }
19594 }
19595
52356b79 19596 fixup_called = 1;
72bf9492
DJ
19597}
19598
d0ce17d8 19599/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
19600 contents from the given SECTION in the HEADER.
19601
19602 HEADER_OFFSET is the offset of the header in the section. */
41144253 19603static void
d0ce17d8 19604read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
19605 struct dwarf2_section_info *section,
19606 sect_offset header_offset)
41144253 19607{
19608 unsigned int bytes_read;
19609 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
19610 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19611
41144253 19612 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19613 info_ptr += bytes_read;
a1c40103 19614
41144253 19615 header->version = read_2_bytes (abfd, info_ptr);
19616 info_ptr += 2;
a1c40103 19617
41144253 19618 header->addr_size = read_1_byte (abfd, info_ptr);
19619 info_ptr += 1;
a1c40103 19620
41144253 19621 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19622 info_ptr += 1;
a1c40103 19623
41144253 19624 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19625}
19626
19627/* Return the DW_AT_loclists_base value for the CU. */
19628static ULONGEST
19629lookup_loclist_base (struct dwarf2_cu *cu)
19630{
19631 /* For the .dwo unit, the loclist_base points to the first offset following
19632 the header. The header consists of the following entities-
19633 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19634 bit format)
19635 2. version (2 bytes)
19636 3. address size (1 byte)
19637 4. segment selector size (1 byte)
19638 5. offset entry count (4 bytes)
19639 These sizes are derived as per the DWARFv5 standard. */
19640 if (cu->dwo_unit != nullptr)
19641 {
19642 if (cu->header.initial_length_size == 4)
19643 return LOCLIST_HEADER_SIZE32;
19644 return LOCLIST_HEADER_SIZE64;
19645 }
19646 return cu->loclist_base;
19647}
19648
19649/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19650 array of offsets in the .debug_loclists section. */
e57933dc
SM
19651
19652static sect_offset
41144253 19653read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19654{
976ca316
SM
19655 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19656 struct objfile *objfile = per_objfile->objfile;
41144253 19657 bfd *abfd = objfile->obfd;
a1c40103
SM
19658 ULONGEST loclist_header_size =
19659 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19660 : LOCLIST_HEADER_SIZE64);
41144253 19661 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
19662
19663 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19664 ULONGEST start_offset =
19665 loclist_base + loclist_index * cu->header.offset_size;
19666
a1c40103 19667 /* Get loclists section. */
41144253 19668 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19669
a1c40103 19670 /* Read the loclists section content. */
41144253 19671 section->read (objfile);
19672 if (section->buffer == NULL)
a0c1eeba
SM
19673 error (_("DW_FORM_loclistx used without .debug_loclists "
19674 "section [in module %s]"), objfile_name (objfile));
19675
a1c40103
SM
19676 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19677 so if loclist_base is smaller than the header size, we have a problem. */
19678 if (loclist_base < loclist_header_size)
19679 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19680 objfile_name (objfile));
19681
19682 /* Read the header of the loclists contribution. */
d0ce17d8 19683 struct loclists_rnglists_header header;
a1c40103
SM
19684 read_loclists_rnglists_header (&header, section,
19685 (sect_offset) (loclist_base - loclist_header_size));
19686
19687 /* Verify the loclist index is valid. */
41144253 19688 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
19689 error (_("DW_FORM_loclistx pointing outside of "
19690 ".debug_loclists offset array [in module %s]"),
19691 objfile_name (objfile));
19692
05787bad
SM
19693 /* Validate that reading won't go beyond the end of the section. */
19694 if (start_offset + cu->header.offset_size > section->size)
19695 error (_("Reading DW_FORM_loclistx index beyond end of"
19696 ".debug_loclists section [in module %s]"),
19697 objfile_name (objfile));
19698
19699 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 19700
19701 if (cu->header.offset_size == 4)
e57933dc 19702 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 19703 else
e57933dc 19704 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 19705}
19706
d0ce17d8
CT
19707/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19708 array of offsets in the .debug_rnglists section. */
e57933dc
SM
19709
19710static sect_offset
d0ce17d8
CT
19711read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19712 dwarf_tag tag)
19713{
19714 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19715 struct objfile *objfile = dwarf2_per_objfile->objfile;
19716 bfd *abfd = objfile->obfd;
19717 ULONGEST rnglist_header_size =
19718 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19719 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
19720
19721 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19722 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19723 doesn't apply. */
d0ce17d8 19724 ULONGEST rnglist_base =
2b0c7f41 19725 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
19726
19727 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
19728 ULONGEST start_offset =
19729 rnglist_base + rnglist_index * cu->header.offset_size;
19730
19731 /* Get rnglists section. */
19732 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19733
19734 /* Read the rnglists section content. */
19735 section->read (objfile);
19736 if (section->buffer == nullptr)
19737 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19738 "[in module %s]"),
19739 objfile_name (objfile));
19740
a1c40103
SM
19741 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19742 so if rnglist_base is smaller than the header size, we have a problem. */
19743 if (rnglist_base < rnglist_header_size)
19744 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19745 objfile_name (objfile));
19746
19747 /* Read the header of the rnglists contribution. */
d0ce17d8 19748 struct loclists_rnglists_header header;
a1c40103
SM
19749 read_loclists_rnglists_header (&header, section,
19750 (sect_offset) (rnglist_base - rnglist_header_size));
19751
19752 /* Verify the rnglist index is valid. */
d0ce17d8
CT
19753 if (rnglist_index >= header.offset_entry_count)
19754 error (_("DW_FORM_rnglistx index pointing outside of "
19755 ".debug_rnglists offset array [in module %s]"),
19756 objfile_name (objfile));
19757
d0ce17d8 19758 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 19759 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
19760 error (_("Reading DW_FORM_rnglistx index beyond end of"
19761 ".debug_rnglists section [in module %s]"),
19762 objfile_name (objfile));
19763
19764 const gdb_byte *info_ptr = section->buffer + start_offset;
19765
19766 if (cu->header.offset_size == 4)
e57933dc 19767 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 19768 else
e57933dc 19769 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
19770}
19771
18a8505e
AT
19772/* Process the attributes that had to be skipped in the first round. These
19773 attributes are the ones that need str_offsets_base or addr_base attributes.
19774 They could not have been processed in the first round, because at the time
19775 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19776static void
19777read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 19778 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
19779{
19780 struct dwarf2_cu *cu = reader->cu;
19781 switch (attr->form)
19782 {
19783 case DW_FORM_addrx:
19784 case DW_FORM_GNU_addr_index:
36d378cf
TT
19785 attr->set_address (read_addr_index (cu,
19786 attr->as_unsigned_reprocess ()));
dda83cd7 19787 break;
41144253 19788 case DW_FORM_loclistx:
b1829e1b 19789 {
e57933dc 19790 sect_offset loclists_sect_off
b1829e1b
SM
19791 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19792
e57933dc 19793 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
19794 }
19795 break;
d0ce17d8 19796 case DW_FORM_rnglistx:
b1829e1b 19797 {
e57933dc 19798 sect_offset rnglists_sect_off
b1829e1b
SM
19799 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19800
e57933dc 19801 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 19802 }
dda83cd7 19803 break;
18a8505e
AT
19804 case DW_FORM_strx:
19805 case DW_FORM_strx1:
19806 case DW_FORM_strx2:
19807 case DW_FORM_strx3:
19808 case DW_FORM_strx4:
19809 case DW_FORM_GNU_str_index:
19810 {
fe56917a 19811 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 19812 gdb_assert (!attr->canonical_string_p ());
18a8505e 19813 if (reader->dwo_file != NULL)
c6481205
TT
19814 attr->set_string_noncanonical (read_dwo_str_index (reader,
19815 str_index));
18a8505e 19816 else
c6481205
TT
19817 attr->set_string_noncanonical (read_stub_str_index (cu,
19818 str_index));
18a8505e
AT
19819 break;
19820 }
19821 default:
19822 gdb_assert_not_reached (_("Unexpected DWARF form."));
19823 }
19824}
19825
a8329558 19826/* Read an attribute value described by an attribute form. */
c906108c 19827
d521ce57 19828static const gdb_byte *
dee91e82
DE
19829read_attribute_value (const struct die_reader_specs *reader,
19830 struct attribute *attr, unsigned form,
7a5f294d 19831 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19832{
dee91e82 19833 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
19834 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19835 struct objfile *objfile = per_objfile->objfile;
dee91e82 19836 bfd *abfd = reader->abfd;
e7c27a73 19837 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19838 unsigned int bytes_read;
19839 struct dwarf_block *blk;
19840
aead7601 19841 attr->form = (enum dwarf_form) form;
a8329558 19842 switch (form)
c906108c 19843 {
c906108c 19844 case DW_FORM_ref_addr:
7c290a04
TT
19845 if (cu_header->version == 2)
19846 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 19847 &bytes_read));
ae411497 19848 else
7c290a04 19849 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19850 &bytes_read));
ae411497
TT
19851 info_ptr += bytes_read;
19852 break;
36586728 19853 case DW_FORM_GNU_ref_alt:
7c290a04 19854 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19855 &bytes_read));
36586728
TT
19856 info_ptr += bytes_read;
19857 break;
ae411497 19858 case DW_FORM_addr:
08feed99
TT
19859 {
19860 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 19861 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
19862 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19863 attr->set_address (addr);
08feed99
TT
19864 info_ptr += bytes_read;
19865 }
c906108c
SS
19866 break;
19867 case DW_FORM_block2:
7b5a2f43 19868 blk = dwarf_alloc_block (cu);
c906108c
SS
19869 blk->size = read_2_bytes (abfd, info_ptr);
19870 info_ptr += 2;
19871 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19872 info_ptr += blk->size;
9d2246fc 19873 attr->set_block (blk);
c906108c
SS
19874 break;
19875 case DW_FORM_block4:
7b5a2f43 19876 blk = dwarf_alloc_block (cu);
c906108c
SS
19877 blk->size = read_4_bytes (abfd, info_ptr);
19878 info_ptr += 4;
19879 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19880 info_ptr += blk->size;
9d2246fc 19881 attr->set_block (blk);
c906108c
SS
19882 break;
19883 case DW_FORM_data2:
414ad644 19884 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
19885 info_ptr += 2;
19886 break;
19887 case DW_FORM_data4:
414ad644 19888 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
19889 info_ptr += 4;
19890 break;
19891 case DW_FORM_data8:
414ad644 19892 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
19893 info_ptr += 8;
19894 break;
0224619f
JK
19895 case DW_FORM_data16:
19896 blk = dwarf_alloc_block (cu);
19897 blk->size = 16;
19898 blk->data = read_n_bytes (abfd, info_ptr, 16);
19899 info_ptr += 16;
9d2246fc 19900 attr->set_block (blk);
0224619f 19901 break;
2dc7f7b3 19902 case DW_FORM_sec_offset:
7c290a04 19903 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 19904 &bytes_read));
2dc7f7b3
TT
19905 info_ptr += bytes_read;
19906 break;
41144253 19907 case DW_FORM_loclistx:
19908 {
7a5f294d
TT
19909 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19910 &bytes_read));
19911 info_ptr += bytes_read;
41144253 19912 }
19913 break;
c906108c 19914 case DW_FORM_string:
c6481205
TT
19915 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19916 &bytes_read));
c906108c
SS
19917 info_ptr += bytes_read;
19918 break;
4bdf3d34 19919 case DW_FORM_strp:
36586728
TT
19920 if (!cu->per_cu->is_dwz)
19921 {
c6481205
TT
19922 attr->set_string_noncanonical
19923 (read_indirect_string (per_objfile,
19924 abfd, info_ptr, cu_header,
19925 &bytes_read));
36586728
TT
19926 info_ptr += bytes_read;
19927 break;
19928 }
19929 /* FALLTHROUGH */
43988095
JK
19930 case DW_FORM_line_strp:
19931 if (!cu->per_cu->is_dwz)
19932 {
c6481205
TT
19933 attr->set_string_noncanonical
19934 (per_objfile->read_line_string (info_ptr, cu_header,
19935 &bytes_read));
43988095
JK
19936 info_ptr += bytes_read;
19937 break;
19938 }
19939 /* FALLTHROUGH */
36586728
TT
19940 case DW_FORM_GNU_strp_alt:
19941 {
a7308ce0 19942 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
19943 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19944 &bytes_read);
36586728 19945
c6481205
TT
19946 attr->set_string_noncanonical
19947 (dwz->read_string (objfile, str_offset));
36586728
TT
19948 info_ptr += bytes_read;
19949 }
4bdf3d34 19950 break;
2dc7f7b3 19951 case DW_FORM_exprloc:
c906108c 19952 case DW_FORM_block:
7b5a2f43 19953 blk = dwarf_alloc_block (cu);
c906108c
SS
19954 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19955 info_ptr += bytes_read;
19956 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19957 info_ptr += blk->size;
9d2246fc 19958 attr->set_block (blk);
c906108c
SS
19959 break;
19960 case DW_FORM_block1:
7b5a2f43 19961 blk = dwarf_alloc_block (cu);
c906108c
SS
19962 blk->size = read_1_byte (abfd, info_ptr);
19963 info_ptr += 1;
19964 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19965 info_ptr += blk->size;
9d2246fc 19966 attr->set_block (blk);
c906108c
SS
19967 break;
19968 case DW_FORM_data1:
c906108c 19969 case DW_FORM_flag:
414ad644 19970 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
19971 info_ptr += 1;
19972 break;
2dc7f7b3 19973 case DW_FORM_flag_present:
414ad644 19974 attr->set_unsigned (1);
2dc7f7b3 19975 break;
c906108c 19976 case DW_FORM_sdata:
1bc397c5 19977 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19978 info_ptr += bytes_read;
19979 break;
18a8505e 19980 case DW_FORM_rnglistx:
7a5f294d
TT
19981 {
19982 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19983 &bytes_read));
19984 info_ptr += bytes_read;
19985 }
19986 break;
d0ce17d8 19987 case DW_FORM_udata:
414ad644 19988 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19989 info_ptr += bytes_read;
19990 break;
19991 case DW_FORM_ref1:
7c290a04 19992 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 19993 + read_1_byte (abfd, info_ptr)));
c906108c
SS
19994 info_ptr += 1;
19995 break;
19996 case DW_FORM_ref2:
7c290a04 19997 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 19998 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
19999 info_ptr += 2;
20000 break;
20001 case DW_FORM_ref4:
7c290a04 20002 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20003 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20004 info_ptr += 4;
20005 break;
613e1657 20006 case DW_FORM_ref8:
7c290a04 20007 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20008 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20009 info_ptr += 8;
20010 break;
55f1336d 20011 case DW_FORM_ref_sig8:
630ed6b9 20012 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20013 info_ptr += 8;
20014 break;
c906108c 20015 case DW_FORM_ref_udata:
7c290a04 20016 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
20017 + read_unsigned_leb128 (abfd, info_ptr,
20018 &bytes_read)));
c906108c
SS
20019 info_ptr += bytes_read;
20020 break;
c906108c 20021 case DW_FORM_indirect:
a8329558
KW
20022 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20023 info_ptr += bytes_read;
43988095
JK
20024 if (form == DW_FORM_implicit_const)
20025 {
20026 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20027 info_ptr += bytes_read;
20028 }
20029 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20030 info_ptr);
43988095
JK
20031 break;
20032 case DW_FORM_implicit_const:
1bc397c5 20033 attr->set_signed (implicit_const);
a8329558 20034 break;
336d760d 20035 case DW_FORM_addrx:
3019eac3 20036 case DW_FORM_GNU_addr_index:
fe56917a
TT
20037 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20038 &bytes_read));
3019eac3
DE
20039 info_ptr += bytes_read;
20040 break;
cf532bd1 20041 case DW_FORM_strx:
15f18d14
AT
20042 case DW_FORM_strx1:
20043 case DW_FORM_strx2:
20044 case DW_FORM_strx3:
20045 case DW_FORM_strx4:
3019eac3 20046 case DW_FORM_GNU_str_index:
3019eac3 20047 {
15f18d14
AT
20048 ULONGEST str_index;
20049 if (form == DW_FORM_strx1)
20050 {
20051 str_index = read_1_byte (abfd, info_ptr);
20052 info_ptr += 1;
20053 }
20054 else if (form == DW_FORM_strx2)
20055 {
20056 str_index = read_2_bytes (abfd, info_ptr);
20057 info_ptr += 2;
20058 }
20059 else if (form == DW_FORM_strx3)
20060 {
20061 str_index = read_3_bytes (abfd, info_ptr);
20062 info_ptr += 3;
20063 }
20064 else if (form == DW_FORM_strx4)
20065 {
20066 str_index = read_4_bytes (abfd, info_ptr);
20067 info_ptr += 4;
20068 }
20069 else
20070 {
20071 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20072 info_ptr += bytes_read;
20073 }
fe56917a 20074 attr->set_unsigned_reprocess (str_index);
7a5f294d 20075 }
3019eac3 20076 break;
c906108c 20077 default:
8a3fe4f8 20078 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20079 dwarf_form_name (form),
20080 bfd_get_filename (abfd));
c906108c 20081 }
28e94949 20082
36586728 20083 /* Super hack. */
cd6c91b4 20084 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20085 attr->form = DW_FORM_GNU_ref_alt;
20086
28e94949
JB
20087 /* We have seen instances where the compiler tried to emit a byte
20088 size attribute of -1 which ended up being encoded as an unsigned
20089 0xffffffff. Although 0xffffffff is technically a valid size value,
20090 an object of this size seems pretty unlikely so we can relatively
20091 safely treat these cases as if the size attribute was invalid and
20092 treat them as zero by default. */
20093 if (attr->name == DW_AT_byte_size
20094 && form == DW_FORM_data4
529908cb 20095 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20096 {
20097 complaint
dda83cd7
SM
20098 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20099 hex_string (attr->as_unsigned ()));
414ad644 20100 attr->set_unsigned (0);
01c66ae6 20101 }
28e94949 20102
c906108c
SS
20103 return info_ptr;
20104}
20105
a8329558
KW
20106/* Read an attribute described by an abbreviated attribute. */
20107
d521ce57 20108static const gdb_byte *
dee91e82 20109read_attribute (const struct die_reader_specs *reader,
4444f407 20110 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20111 const gdb_byte *info_ptr)
a8329558
KW
20112{
20113 attr->name = abbrev->name;
c6481205 20114 attr->string_is_canonical = 0;
fe56917a 20115 attr->requires_reprocessing = 0;
43988095 20116 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20117 abbrev->implicit_const, info_ptr);
a8329558
KW
20118}
20119
43988095
JK
20120/* Return pointer to string at .debug_str offset STR_OFFSET. */
20121
20122static const char *
976ca316 20123read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20124 LONGEST str_offset)
43988095 20125{
976ca316
SM
20126 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20127 str_offset, "DW_FORM_strp");
c906108c
SS
20128}
20129
43988095
JK
20130/* Return pointer to string at .debug_str offset as read from BUF.
20131 BUF is assumed to be in a compilation unit described by CU_HEADER.
20132 Return *BYTES_READ_PTR count of bytes read from BUF. */
20133
d521ce57 20134static const char *
976ca316 20135read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20136 const gdb_byte *buf,
cf2c3c16
TT
20137 const struct comp_unit_head *cu_header,
20138 unsigned int *bytes_read_ptr)
20139{
8266302d 20140 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20141
976ca316 20142 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20143}
20144
86c0bb4c 20145/* See read.h. */
43988095 20146
86c0bb4c
TT
20147const char *
20148dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20149 const struct comp_unit_head *cu_header,
20150 unsigned int *bytes_read_ptr)
43988095 20151{
86c0bb4c 20152 bfd *abfd = objfile->obfd;
8266302d 20153 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20154
5989a64e 20155 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20156}
20157
3019eac3 20158/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20159 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20160 ADDR_SIZE is the size of addresses from the CU header. */
20161
20162static CORE_ADDR
976ca316
SM
20163read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20164 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20165{
976ca316 20166 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20167 bfd *abfd = objfile->obfd;
20168 const gdb_byte *info_ptr;
18a8505e 20169 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20170
976ca316
SM
20171 per_objfile->per_bfd->addr.read (objfile);
20172 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20173 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20174 objfile_name (objfile));
18a8505e 20175 if (addr_base_or_zero + addr_index * addr_size
976ca316 20176 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20177 error (_("DW_FORM_addr_index pointing outside of "
20178 ".debug_addr section [in module %s]"),
4262abfb 20179 objfile_name (objfile));
976ca316
SM
20180 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20181 + addr_index * addr_size);
3019eac3
DE
20182 if (addr_size == 4)
20183 return bfd_get_32 (abfd, info_ptr);
20184 else
20185 return bfd_get_64 (abfd, info_ptr);
20186}
20187
20188/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20189
20190static CORE_ADDR
20191read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20192{
5e22e966 20193 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20194 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20195}
20196
20197/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20198
20199static CORE_ADDR
d521ce57 20200read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20201 unsigned int *bytes_read)
20202{
5e22e966 20203 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20204 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20205
20206 return read_addr_index (cu, addr_index);
20207}
20208
450a1bfc 20209/* See read.h. */
3019eac3
DE
20210
20211CORE_ADDR
82ca3f51 20212dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20213 dwarf2_per_objfile *per_objfile,
82ca3f51 20214 unsigned int addr_index)
3019eac3 20215{
976ca316 20216 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20217 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20218 int addr_size;
20219
3019eac3
DE
20220 /* We need addr_base and addr_size.
20221 If we don't have PER_CU->cu, we have to get it.
20222 Nasty, but the alternative is storing the needed info in PER_CU,
20223 which at this point doesn't seem justified: it's not clear how frequently
20224 it would get used and it would increase the size of every PER_CU.
20225 Entry points like dwarf2_per_cu_addr_size do a similar thing
20226 so we're not in uncharted territory here.
20227 Alas we need to be a bit more complicated as addr_base is contained
20228 in the DIE.
20229
20230 We don't need to read the entire CU(/TU).
20231 We just need the header and top level die.
a1b64ce1 20232
3019eac3 20233 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20234 For now we skip this optimization. */
3019eac3
DE
20235
20236 if (cu != NULL)
20237 {
20238 addr_base = cu->addr_base;
20239 addr_size = cu->header.addr_size;
20240 }
20241 else
20242 {
976ca316 20243 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20244 addr_base = reader.cu->addr_base;
20245 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20246 }
20247
976ca316 20248 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20249}
20250
18a8505e
AT
20251/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20252 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20253 DWO file. */
3019eac3 20254
d521ce57 20255static const char *
18a8505e
AT
20256read_str_index (struct dwarf2_cu *cu,
20257 struct dwarf2_section_info *str_section,
20258 struct dwarf2_section_info *str_offsets_section,
20259 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20260{
976ca316
SM
20261 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20262 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20263 const char *objf_name = objfile_name (objfile);
3019eac3 20264 bfd *abfd = objfile->obfd;
d521ce57 20265 const gdb_byte *info_ptr;
3019eac3 20266 ULONGEST str_offset;
cf532bd1 20267 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20268
96b79293
TT
20269 str_section->read (objfile);
20270 str_offsets_section->read (objfile);
73869dc2 20271 if (str_section->buffer == NULL)
18a8505e 20272 error (_("%s used without %s section"
9d8780f0 20273 " in CU at offset %s [in module %s]"),
96b79293 20274 form_name, str_section->get_name (),
dda83cd7 20275 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20276 if (str_offsets_section->buffer == NULL)
18a8505e 20277 error (_("%s used without %s section"
9d8780f0 20278 " in CU at offset %s [in module %s]"),
96b79293 20279 form_name, str_section->get_name (),
dda83cd7 20280 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20281 info_ptr = (str_offsets_section->buffer
18a8505e 20282 + str_offsets_base
3019eac3
DE
20283 + str_index * cu->header.offset_size);
20284 if (cu->header.offset_size == 4)
20285 str_offset = bfd_get_32 (abfd, info_ptr);
20286 else
20287 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20288 if (str_offset >= str_section->size)
57d63ce2 20289 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20290 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20291 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20292 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20293}
20294
18a8505e
AT
20295/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20296
20297static const char *
20298read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20299{
20300 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20301 ? reader->cu->header.addr_size : 0;
20302 return read_str_index (reader->cu,
20303 &reader->dwo_file->sections.str,
20304 &reader->dwo_file->sections.str_offsets,
20305 str_offsets_base, str_index);
20306}
20307
20308/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20309
20310static const char *
20311read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20312{
5e22e966 20313 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20314 const char *objf_name = objfile_name (objfile);
20315 static const char form_name[] = "DW_FORM_GNU_str_index";
20316 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20317
20318 if (!cu->str_offsets_base.has_value ())
20319 error (_("%s used in Fission stub without %s"
20320 " in CU at offset 0x%lx [in module %s]"),
20321 form_name, str_offsets_attr_name,
20322 (long) cu->header.offset_size, objf_name);
20323
20324 return read_str_index (cu,
5e22e966
SM
20325 &cu->per_objfile->per_bfd->str,
20326 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20327 *cu->str_offsets_base, str_index);
20328}
20329
3019eac3
DE
20330/* Return the length of an LEB128 number in BUF. */
20331
20332static int
20333leb128_size (const gdb_byte *buf)
20334{
20335 const gdb_byte *begin = buf;
20336 gdb_byte byte;
20337
20338 while (1)
20339 {
20340 byte = *buf++;
20341 if ((byte & 128) == 0)
20342 return buf - begin;
20343 }
20344}
20345
c906108c 20346static void
e142c38c 20347set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20348{
20349 switch (lang)
20350 {
20351 case DW_LANG_C89:
76bee0cc 20352 case DW_LANG_C99:
0cfd832f 20353 case DW_LANG_C11:
c906108c 20354 case DW_LANG_C:
d1be3247 20355 case DW_LANG_UPC:
e142c38c 20356 cu->language = language_c;
c906108c 20357 break;
9c37b5ae 20358 case DW_LANG_Java:
c906108c 20359 case DW_LANG_C_plus_plus:
0cfd832f
MW
20360 case DW_LANG_C_plus_plus_11:
20361 case DW_LANG_C_plus_plus_14:
e142c38c 20362 cu->language = language_cplus;
c906108c 20363 break;
6aecb9c2
JB
20364 case DW_LANG_D:
20365 cu->language = language_d;
20366 break;
c906108c
SS
20367 case DW_LANG_Fortran77:
20368 case DW_LANG_Fortran90:
b21b22e0 20369 case DW_LANG_Fortran95:
f7de9aab
MW
20370 case DW_LANG_Fortran03:
20371 case DW_LANG_Fortran08:
e142c38c 20372 cu->language = language_fortran;
c906108c 20373 break;
a766d390
DE
20374 case DW_LANG_Go:
20375 cu->language = language_go;
20376 break;
c906108c 20377 case DW_LANG_Mips_Assembler:
e142c38c 20378 cu->language = language_asm;
c906108c
SS
20379 break;
20380 case DW_LANG_Ada83:
8aaf0b47 20381 case DW_LANG_Ada95:
bc5f45f8
JB
20382 cu->language = language_ada;
20383 break;
72019c9c
GM
20384 case DW_LANG_Modula2:
20385 cu->language = language_m2;
20386 break;
fe8e67fd
PM
20387 case DW_LANG_Pascal83:
20388 cu->language = language_pascal;
20389 break;
22566fbd
DJ
20390 case DW_LANG_ObjC:
20391 cu->language = language_objc;
20392 break;
c44af4eb
TT
20393 case DW_LANG_Rust:
20394 case DW_LANG_Rust_old:
20395 cu->language = language_rust;
20396 break;
c906108c
SS
20397 case DW_LANG_Cobol74:
20398 case DW_LANG_Cobol85:
c906108c 20399 default:
e142c38c 20400 cu->language = language_minimal;
c906108c
SS
20401 break;
20402 }
e142c38c 20403 cu->language_defn = language_def (cu->language);
c906108c
SS
20404}
20405
20406/* Return the named attribute or NULL if not there. */
20407
20408static struct attribute *
e142c38c 20409dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20410{
a48e046c 20411 for (;;)
c906108c 20412 {
a48e046c
TT
20413 unsigned int i;
20414 struct attribute *spec = NULL;
20415
20416 for (i = 0; i < die->num_attrs; ++i)
20417 {
20418 if (die->attrs[i].name == name)
20419 return &die->attrs[i];
20420 if (die->attrs[i].name == DW_AT_specification
20421 || die->attrs[i].name == DW_AT_abstract_origin)
20422 spec = &die->attrs[i];
20423 }
20424
20425 if (!spec)
20426 break;
c906108c 20427
f2f0e013 20428 die = follow_die_ref (die, spec, &cu);
f2f0e013 20429 }
c5aa993b 20430
c906108c
SS
20431 return NULL;
20432}
20433
7d45c7c3
KB
20434/* Return the string associated with a string-typed attribute, or NULL if it
20435 is either not found or is of an incorrect type. */
20436
20437static const char *
20438dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20439{
20440 struct attribute *attr;
20441 const char *str = NULL;
20442
20443 attr = dwarf2_attr (die, name, cu);
20444
20445 if (attr != NULL)
20446 {
95f982e5 20447 str = attr->as_string ();
e61108c9 20448 if (str == nullptr)
dda83cd7 20449 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20450 "DIE at %s in module %s"),
20451 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20452 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20453 }
20454
20455 return str;
20456}
20457
a084a2a6 20458/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20459 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20460static const char *
20461dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20462{
20463 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20464 if (dwo_name == nullptr)
20465 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20466 return dwo_name;
20467}
20468
05cf31d1
JB
20469/* Return non-zero iff the attribute NAME is defined for the given DIE,
20470 and holds a non-zero value. This function should only be used for
2dc7f7b3 20471 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20472
20473static int
20474dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20475{
20476 struct attribute *attr = dwarf2_attr (die, name, cu);
20477
c45bc3f8 20478 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
20479}
20480
3ca72b44 20481static int
e142c38c 20482die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20483{
05cf31d1
JB
20484 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20485 which value is non-zero. However, we have to be careful with
20486 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20487 (via dwarf2_flag_true_p) follows this attribute. So we may
20488 end up accidently finding a declaration attribute that belongs
20489 to a different DIE referenced by the specification attribute,
20490 even though the given DIE does not have a declaration attribute. */
20491 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20492 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20493}
20494
63d06c5c 20495/* Return the die giving the specification for DIE, if there is
f2f0e013 20496 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20497 containing the return value on output. If there is no
20498 specification, but there is an abstract origin, that is
20499 returned. */
63d06c5c
DC
20500
20501static struct die_info *
f2f0e013 20502die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20503{
f2f0e013
DJ
20504 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20505 *spec_cu);
63d06c5c 20506
edb3359d
DJ
20507 if (spec_attr == NULL)
20508 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20509
63d06c5c
DC
20510 if (spec_attr == NULL)
20511 return NULL;
20512 else
f2f0e013 20513 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20514}
c906108c 20515
527f3840
JK
20516/* Stub for free_line_header to match void * callback types. */
20517
20518static void
20519free_line_header_voidp (void *arg)
20520{
9a3c8263 20521 struct line_header *lh = (struct line_header *) arg;
527f3840 20522
fff8551c 20523 delete lh;
527f3840
JK
20524}
20525
83769d0b 20526/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20527
20528static struct dwarf2_section_info *
20529get_debug_line_section (struct dwarf2_cu *cu)
20530{
20531 struct dwarf2_section_info *section;
976ca316 20532 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
20533
20534 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20535 DWO file. */
20536 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20537 section = &cu->dwo_unit->dwo_file->sections.line;
20538 else if (cu->per_cu->is_dwz)
20539 {
a7308ce0 20540 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
20541
20542 section = &dwz->line;
20543 }
20544 else
976ca316 20545 section = &per_objfile->per_bfd->line;
36586728
TT
20546
20547 return section;
20548}
20549
debd256d 20550/* Read the statement program header starting at OFFSET in
3019eac3 20551 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20552 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20553 Returns NULL if there is a problem reading the header, e.g., if it
20554 has a version we don't understand.
debd256d
JB
20555
20556 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20557 the returned object point into the dwarf line section buffer,
20558 and must not be freed. */
ae2de4f8 20559
fff8551c 20560static line_header_up
9c541725 20561dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20562{
3019eac3 20563 struct dwarf2_section_info *section;
976ca316 20564 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 20565
36586728 20566 section = get_debug_line_section (cu);
976ca316 20567 section->read (per_objfile->objfile);
3019eac3 20568 if (section->buffer == NULL)
debd256d 20569 {
3019eac3 20570 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20571 complaint (_("missing .debug_line.dwo section"));
3019eac3 20572 else
b98664d3 20573 complaint (_("missing .debug_line section"));
debd256d
JB
20574 return 0;
20575 }
20576
0df7ad3a 20577 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 20578 per_objfile, section, &cu->header);
debd256d 20579}
c906108c 20580
c6da4cef 20581/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20582 Return the file name of the psymtab for the given file_entry.
c6da4cef 20583 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20584 If space for the result is malloc'd, *NAME_HOLDER will be set.
20585 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20586
d521ce57 20587static const char *
7ba99d21 20588psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20589 const dwarf2_psymtab *pst,
c89b44cd
TT
20590 const char *comp_dir,
20591 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20592{
d521ce57
TT
20593 const char *include_name = fe.name;
20594 const char *include_name_to_compare = include_name;
72b9f47f 20595 const char *pst_filename;
c6da4cef
DE
20596 int file_is_pst;
20597
8c43009f 20598 const char *dir_name = fe.include_dir (lh);
c6da4cef 20599
c89b44cd 20600 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20601 if (!IS_ABSOLUTE_PATH (include_name)
20602 && (dir_name != NULL || comp_dir != NULL))
20603 {
20604 /* Avoid creating a duplicate psymtab for PST.
20605 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20606 Before we do the comparison, however, we need to account
20607 for DIR_NAME and COMP_DIR.
20608 First prepend dir_name (if non-NULL). If we still don't
20609 have an absolute path prepend comp_dir (if non-NULL).
20610 However, the directory we record in the include-file's
20611 psymtab does not contain COMP_DIR (to match the
20612 corresponding symtab(s)).
20613
20614 Example:
20615
20616 bash$ cd /tmp
20617 bash$ gcc -g ./hello.c
20618 include_name = "hello.c"
20619 dir_name = "."
20620 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20621 DW_AT_name = "./hello.c"
20622
20623 */
c6da4cef
DE
20624
20625 if (dir_name != NULL)
20626 {
c89b44cd
TT
20627 name_holder->reset (concat (dir_name, SLASH_STRING,
20628 include_name, (char *) NULL));
20629 include_name = name_holder->get ();
c6da4cef 20630 include_name_to_compare = include_name;
c6da4cef
DE
20631 }
20632 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20633 {
c89b44cd
TT
20634 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20635 include_name, (char *) NULL));
20636 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20637 }
20638 }
20639
20640 pst_filename = pst->filename;
c89b44cd 20641 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20642 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20643 {
c89b44cd
TT
20644 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20645 pst_filename, (char *) NULL));
20646 pst_filename = copied_name.get ();
c6da4cef
DE
20647 }
20648
1e3fad37 20649 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20650
c6da4cef
DE
20651 if (file_is_pst)
20652 return NULL;
20653 return include_name;
20654}
20655
d9b3de22
DE
20656/* State machine to track the state of the line number program. */
20657
6f77053d 20658class lnp_state_machine
d9b3de22 20659{
6f77053d
PA
20660public:
20661 /* Initialize a machine state for the start of a line number
20662 program. */
804d2729
TT
20663 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20664 bool record_lines_p);
6f77053d 20665
8c43009f
PA
20666 file_entry *current_file ()
20667 {
20668 /* lh->file_names is 0-based, but the file name numbers in the
20669 statement program are 1-based. */
6f77053d
PA
20670 return m_line_header->file_name_at (m_file);
20671 }
20672
20673 /* Record the line in the state machine. END_SEQUENCE is true if
20674 we're processing the end of a sequence. */
20675 void record_line (bool end_sequence);
20676
a8caed5d 20677 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 20678 nop-out rest of the lines in this sequence. */
6f77053d
PA
20679 void check_line_address (struct dwarf2_cu *cu,
20680 const gdb_byte *line_ptr,
7ab6656f 20681 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20682
20683 void handle_set_discriminator (unsigned int discriminator)
20684 {
20685 m_discriminator = discriminator;
20686 m_line_has_non_zero_discriminator |= discriminator != 0;
20687 }
20688
20689 /* Handle DW_LNE_set_address. */
20690 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20691 {
20692 m_op_index = 0;
20693 address += baseaddr;
20694 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20695 }
20696
20697 /* Handle DW_LNS_advance_pc. */
20698 void handle_advance_pc (CORE_ADDR adjust);
20699
20700 /* Handle a special opcode. */
20701 void handle_special_opcode (unsigned char op_code);
20702
20703 /* Handle DW_LNS_advance_line. */
20704 void handle_advance_line (int line_delta)
20705 {
20706 advance_line (line_delta);
20707 }
20708
20709 /* Handle DW_LNS_set_file. */
20710 void handle_set_file (file_name_index file);
20711
20712 /* Handle DW_LNS_negate_stmt. */
20713 void handle_negate_stmt ()
20714 {
20715 m_is_stmt = !m_is_stmt;
20716 }
20717
20718 /* Handle DW_LNS_const_add_pc. */
20719 void handle_const_add_pc ();
20720
20721 /* Handle DW_LNS_fixed_advance_pc. */
20722 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20723 {
20724 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20725 m_op_index = 0;
20726 }
20727
20728 /* Handle DW_LNS_copy. */
20729 void handle_copy ()
20730 {
20731 record_line (false);
20732 m_discriminator = 0;
20733 }
20734
20735 /* Handle DW_LNE_end_sequence. */
20736 void handle_end_sequence ()
20737 {
804d2729 20738 m_currently_recording_lines = true;
6f77053d
PA
20739 }
20740
20741private:
20742 /* Advance the line by LINE_DELTA. */
20743 void advance_line (int line_delta)
20744 {
20745 m_line += line_delta;
20746
20747 if (line_delta != 0)
20748 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20749 }
20750
804d2729
TT
20751 struct dwarf2_cu *m_cu;
20752
6f77053d
PA
20753 gdbarch *m_gdbarch;
20754
20755 /* True if we're recording lines.
20756 Otherwise we're building partial symtabs and are just interested in
20757 finding include files mentioned by the line number program. */
20758 bool m_record_lines_p;
20759
8c43009f 20760 /* The line number header. */
6f77053d 20761 line_header *m_line_header;
8c43009f 20762
6f77053d
PA
20763 /* These are part of the standard DWARF line number state machine,
20764 and initialized according to the DWARF spec. */
d9b3de22 20765
6f77053d 20766 unsigned char m_op_index = 0;
7ba99d21
AT
20767 /* The line table index of the current file. */
20768 file_name_index m_file = 1;
6f77053d
PA
20769 unsigned int m_line = 1;
20770
20771 /* These are initialized in the constructor. */
20772
20773 CORE_ADDR m_address;
20774 bool m_is_stmt;
20775 unsigned int m_discriminator;
d9b3de22
DE
20776
20777 /* Additional bits of state we need to track. */
20778
20779 /* The last file that we called dwarf2_start_subfile for.
20780 This is only used for TLLs. */
6f77053d 20781 unsigned int m_last_file = 0;
d9b3de22 20782 /* The last file a line number was recorded for. */
6f77053d 20783 struct subfile *m_last_subfile = NULL;
d9b3de22 20784
1313c56e
AB
20785 /* The address of the last line entry. */
20786 CORE_ADDR m_last_address;
20787
20788 /* Set to true when a previous line at the same address (using
20789 m_last_address) had m_is_stmt true. This is reset to false when a
20790 line entry at a new address (m_address different to m_last_address) is
20791 processed. */
20792 bool m_stmt_at_address = false;
20793
804d2729
TT
20794 /* When true, record the lines we decode. */
20795 bool m_currently_recording_lines = false;
d9b3de22
DE
20796
20797 /* The last line number that was recorded, used to coalesce
20798 consecutive entries for the same line. This can happen, for
20799 example, when discriminators are present. PR 17276. */
6f77053d
PA
20800 unsigned int m_last_line = 0;
20801 bool m_line_has_non_zero_discriminator = false;
8c43009f 20802};
d9b3de22 20803
6f77053d
PA
20804void
20805lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20806{
20807 CORE_ADDR addr_adj = (((m_op_index + adjust)
20808 / m_line_header->maximum_ops_per_instruction)
20809 * m_line_header->minimum_instruction_length);
20810 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20811 m_op_index = ((m_op_index + adjust)
20812 % m_line_header->maximum_ops_per_instruction);
20813}
d9b3de22 20814
6f77053d
PA
20815void
20816lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20817{
6f77053d 20818 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20819 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20820 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20821 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20822 / m_line_header->maximum_ops_per_instruction)
20823 * m_line_header->minimum_instruction_length);
20824 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20825 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20826 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20827
258bf0ee 20828 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20829 advance_line (line_delta);
20830 record_line (false);
20831 m_discriminator = 0;
20832}
d9b3de22 20833
6f77053d
PA
20834void
20835lnp_state_machine::handle_set_file (file_name_index file)
20836{
20837 m_file = file;
20838
20839 const file_entry *fe = current_file ();
20840 if (fe == NULL)
20841 dwarf2_debug_line_missing_file_complaint ();
20842 else if (m_record_lines_p)
20843 {
20844 const char *dir = fe->include_dir (m_line_header);
20845
c24bdb02 20846 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20847 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20848 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20849 }
20850}
20851
20852void
20853lnp_state_machine::handle_const_add_pc ()
20854{
20855 CORE_ADDR adjust
20856 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20857
20858 CORE_ADDR addr_adj
20859 = (((m_op_index + adjust)
20860 / m_line_header->maximum_ops_per_instruction)
20861 * m_line_header->minimum_instruction_length);
20862
20863 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20864 m_op_index = ((m_op_index + adjust)
20865 % m_line_header->maximum_ops_per_instruction);
20866}
d9b3de22 20867
a05a36a5
DE
20868/* Return non-zero if we should add LINE to the line number table.
20869 LINE is the line to add, LAST_LINE is the last line that was added,
20870 LAST_SUBFILE is the subfile for LAST_LINE.
20871 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20872 had a non-zero discriminator.
20873
20874 We have to be careful in the presence of discriminators.
20875 E.g., for this line:
20876
20877 for (i = 0; i < 100000; i++);
20878
20879 clang can emit four line number entries for that one line,
20880 each with a different discriminator.
20881 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20882
20883 However, we want gdb to coalesce all four entries into one.
20884 Otherwise the user could stepi into the middle of the line and
20885 gdb would get confused about whether the pc really was in the
20886 middle of the line.
20887
20888 Things are further complicated by the fact that two consecutive
20889 line number entries for the same line is a heuristic used by gcc
20890 to denote the end of the prologue. So we can't just discard duplicate
20891 entries, we have to be selective about it. The heuristic we use is
20892 that we only collapse consecutive entries for the same line if at least
20893 one of those entries has a non-zero discriminator. PR 17276.
20894
20895 Note: Addresses in the line number state machine can never go backwards
20896 within one sequence, thus this coalescing is ok. */
20897
20898static int
804d2729
TT
20899dwarf_record_line_p (struct dwarf2_cu *cu,
20900 unsigned int line, unsigned int last_line,
a05a36a5
DE
20901 int line_has_non_zero_discriminator,
20902 struct subfile *last_subfile)
20903{
c24bdb02 20904 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20905 return 1;
20906 if (line != last_line)
20907 return 1;
20908 /* Same line for the same file that we've seen already.
20909 As a last check, for pr 17276, only record the line if the line
20910 has never had a non-zero discriminator. */
20911 if (!line_has_non_zero_discriminator)
20912 return 1;
20913 return 0;
20914}
20915
804d2729
TT
20916/* Use the CU's builder to record line number LINE beginning at
20917 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20918
20919static void
d9b3de22 20920dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20921 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20922 struct dwarf2_cu *cu)
252a6764
DE
20923{
20924 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20925
27e0867f
DE
20926 if (dwarf_line_debug)
20927 {
20928 fprintf_unfiltered (gdb_stdlog,
20929 "Recording line %u, file %s, address %s\n",
20930 line, lbasename (subfile->name),
20931 paddress (gdbarch, address));
20932 }
20933
804d2729 20934 if (cu != nullptr)
8c95582d 20935 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20936}
20937
20938/* Subroutine of dwarf_decode_lines_1 to simplify it.
20939 Mark the end of a set of line number records.
d9b3de22 20940 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20941 If SUBFILE is NULL the request is ignored. */
20942
20943static void
20944dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20945 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20946{
27e0867f
DE
20947 if (subfile == NULL)
20948 return;
20949
20950 if (dwarf_line_debug)
20951 {
20952 fprintf_unfiltered (gdb_stdlog,
20953 "Finishing current line, file %s, address %s\n",
20954 lbasename (subfile->name),
20955 paddress (gdbarch, address));
20956 }
20957
8c95582d 20958 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20959}
20960
6f77053d
PA
20961void
20962lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20963{
d9b3de22
DE
20964 if (dwarf_line_debug)
20965 {
20966 fprintf_unfiltered (gdb_stdlog,
20967 "Processing actual line %u: file %u,"
94a72be7 20968 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20969 m_line, m_file,
6f77053d 20970 paddress (m_gdbarch, m_address),
94a72be7
AB
20971 m_is_stmt, m_discriminator,
20972 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20973 }
20974
6f77053d 20975 file_entry *fe = current_file ();
8c43009f
PA
20976
20977 if (fe == NULL)
d9b3de22
DE
20978 dwarf2_debug_line_missing_file_complaint ();
20979 /* For now we ignore lines not starting on an instruction boundary.
20980 But not when processing end_sequence for compatibility with the
20981 previous version of the code. */
6f77053d 20982 else if (m_op_index == 0 || end_sequence)
d9b3de22 20983 {
8c43009f 20984 fe->included_p = 1;
8c95582d 20985 if (m_record_lines_p)
d9b3de22 20986 {
1313c56e
AB
20987 /* When we switch files we insert an end maker in the first file,
20988 switch to the second file and add a new line entry. The
20989 problem is that the end marker inserted in the first file will
20990 discard any previous line entries at the same address. If the
20991 line entries in the first file are marked as is-stmt, while
20992 the new line in the second file is non-stmt, then this means
20993 the end marker will discard is-stmt lines so we can have a
20994 non-stmt line. This means that there are less addresses at
20995 which the user can insert a breakpoint.
20996
20997 To improve this we track the last address in m_last_address,
20998 and whether we have seen an is-stmt at this address. Then
20999 when switching files, if we have seen a stmt at the current
21000 address, and we are switching to create a non-stmt line, then
21001 discard the new line. */
21002 bool file_changed
21003 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21004 bool ignore_this_line
dda83cd7
SM
21005 = ((file_changed && !end_sequence && m_last_address == m_address
21006 && !m_is_stmt && m_stmt_at_address)
21007 || (!end_sequence && m_line == 0));
1313c56e
AB
21008
21009 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21010 {
804d2729
TT
21011 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21012 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21013 }
21014
1313c56e 21015 if (!end_sequence && !ignore_this_line)
d9b3de22 21016 {
8c95582d
AB
21017 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21018
804d2729 21019 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21020 m_line_has_non_zero_discriminator,
21021 m_last_subfile))
d9b3de22 21022 {
c24bdb02 21023 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21024 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21025 builder->get_current_subfile (),
8c95582d 21026 m_line, m_address, is_stmt,
804d2729 21027 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21028 }
c24bdb02 21029 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21030 m_last_line = m_line;
d9b3de22
DE
21031 }
21032 }
21033 }
1313c56e
AB
21034
21035 /* Track whether we have seen any m_is_stmt true at m_address in case we
21036 have multiple line table entries all at m_address. */
21037 if (m_last_address != m_address)
21038 {
21039 m_stmt_at_address = false;
21040 m_last_address = m_address;
21041 }
21042 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21043}
21044
804d2729
TT
21045lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21046 line_header *lh, bool record_lines_p)
d9b3de22 21047{
804d2729 21048 m_cu = cu;
6f77053d
PA
21049 m_gdbarch = arch;
21050 m_record_lines_p = record_lines_p;
21051 m_line_header = lh;
d9b3de22 21052
804d2729 21053 m_currently_recording_lines = true;
d9b3de22 21054
d9b3de22
DE
21055 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21056 was a line entry for it so that the backend has a chance to adjust it
21057 and also record it in case it needs it. This is currently used by MIPS
21058 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21059 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21060 m_is_stmt = lh->default_is_stmt;
21061 m_discriminator = 0;
1313c56e
AB
21062
21063 m_last_address = m_address;
21064 m_stmt_at_address = false;
252a6764
DE
21065}
21066
6f77053d
PA
21067void
21068lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21069 const gdb_byte *line_ptr,
7ab6656f 21070 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21071{
a8caed5d
FS
21072 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21073 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21074 located at 0x0. In this case, additionally check that if
21075 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21076
a8caed5d
FS
21077 if ((address == 0 && address < unrelocated_lowpc)
21078 || address == (CORE_ADDR) -1)
924c2928
DE
21079 {
21080 /* This line table is for a function which has been
21081 GCd by the linker. Ignore it. PR gdb/12528 */
21082
5e22e966 21083 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21084 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21085
b98664d3 21086 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21087 line_offset, objfile_name (objfile));
804d2729
TT
21088 m_currently_recording_lines = false;
21089 /* Note: m_currently_recording_lines is left as false until we see
21090 DW_LNE_end_sequence. */
924c2928
DE
21091 }
21092}
21093
f3f5162e 21094/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21095 Process the line number information in LH.
21096 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21097 program in order to set included_p for every referenced header. */
debd256d 21098
c906108c 21099static void
43f3e411
DE
21100dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21101 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21102{
d521ce57
TT
21103 const gdb_byte *line_ptr, *extended_end;
21104 const gdb_byte *line_end;
a8c50c1f 21105 unsigned int bytes_read, extended_len;
699ca60a 21106 unsigned char op_code, extended_op;
e142c38c 21107 CORE_ADDR baseaddr;
5e22e966 21108 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21109 bfd *abfd = objfile->obfd;
08feed99 21110 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21111 /* True if we're recording line info (as opposed to building partial
21112 symtabs and just interested in finding include files mentioned by
21113 the line number program). */
21114 bool record_lines_p = !decode_for_pst_p;
e142c38c 21115
b3b3bada 21116 baseaddr = objfile->text_section_offset ();
c906108c 21117
debd256d
JB
21118 line_ptr = lh->statement_program_start;
21119 line_end = lh->statement_program_end;
c906108c
SS
21120
21121 /* Read the statement sequences until there's nothing left. */
21122 while (line_ptr < line_end)
21123 {
6f77053d
PA
21124 /* The DWARF line number program state machine. Reset the state
21125 machine at the start of each sequence. */
804d2729 21126 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21127 bool end_sequence = false;
d9b3de22 21128
8c43009f 21129 if (record_lines_p)
c906108c 21130 {
8c43009f
PA
21131 /* Start a subfile for the current file of the state
21132 machine. */
21133 const file_entry *fe = state_machine.current_file ();
21134
21135 if (fe != NULL)
804d2729 21136 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21137 }
21138
a738430d 21139 /* Decode the table. */
d9b3de22 21140 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21141 {
21142 op_code = read_1_byte (abfd, line_ptr);
21143 line_ptr += 1;
9aa1fe7e 21144
debd256d 21145 if (op_code >= lh->opcode_base)
6e70227d 21146 {
8e07a239 21147 /* Special opcode. */
6f77053d 21148 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21149 }
21150 else switch (op_code)
c906108c
SS
21151 {
21152 case DW_LNS_extended_op:
3e43a32a
MS
21153 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21154 &bytes_read);
473b7be6 21155 line_ptr += bytes_read;
a8c50c1f 21156 extended_end = line_ptr + extended_len;
c906108c
SS
21157 extended_op = read_1_byte (abfd, line_ptr);
21158 line_ptr += 1;
8f34b746
TV
21159 if (DW_LNE_lo_user <= extended_op
21160 && extended_op <= DW_LNE_hi_user)
21161 {
21162 /* Vendor extension, ignore. */
21163 line_ptr = extended_end;
21164 break;
21165 }
c906108c
SS
21166 switch (extended_op)
21167 {
21168 case DW_LNE_end_sequence:
6f77053d
PA
21169 state_machine.handle_end_sequence ();
21170 end_sequence = true;
c906108c
SS
21171 break;
21172 case DW_LNE_set_address:
d9b3de22
DE
21173 {
21174 CORE_ADDR address
c8a7a66f 21175 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21176 line_ptr += bytes_read;
6f77053d
PA
21177
21178 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21179 lowpc - baseaddr, address);
6f77053d 21180 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21181 }
c906108c
SS
21182 break;
21183 case DW_LNE_define_file:
dda83cd7
SM
21184 {
21185 const char *cur_file;
ecfb656c
PA
21186 unsigned int mod_time, length;
21187 dir_index dindex;
6e70227d 21188
dda83cd7 21189 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21190 &bytes_read);
dda83cd7
SM
21191 line_ptr += bytes_read;
21192 dindex = (dir_index)
21193 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21194 line_ptr += bytes_read;
21195 mod_time =
21196 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21197 line_ptr += bytes_read;
21198 length =
21199 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21200 line_ptr += bytes_read;
21201 lh->add_file_name (cur_file, dindex, mod_time, length);
21202 }
c906108c 21203 break;
d0c6ba3d 21204 case DW_LNE_set_discriminator:
6f77053d
PA
21205 {
21206 /* The discriminator is not interesting to the
21207 debugger; just ignore it. We still need to
21208 check its value though:
21209 if there are consecutive entries for the same
21210 (non-prologue) line we want to coalesce them.
21211 PR 17276. */
21212 unsigned int discr
21213 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21214 line_ptr += bytes_read;
21215
21216 state_machine.handle_set_discriminator (discr);
21217 }
d0c6ba3d 21218 break;
c906108c 21219 default:
b98664d3 21220 complaint (_("mangled .debug_line section"));
debd256d 21221 return;
c906108c 21222 }
a8c50c1f
DJ
21223 /* Make sure that we parsed the extended op correctly. If e.g.
21224 we expected a different address size than the producer used,
21225 we may have read the wrong number of bytes. */
21226 if (line_ptr != extended_end)
21227 {
b98664d3 21228 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21229 return;
21230 }
c906108c
SS
21231 break;
21232 case DW_LNS_copy:
6f77053d 21233 state_machine.handle_copy ();
c906108c
SS
21234 break;
21235 case DW_LNS_advance_pc:
2dc7f7b3
TT
21236 {
21237 CORE_ADDR adjust
21238 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21239 line_ptr += bytes_read;
6f77053d
PA
21240
21241 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21242 }
c906108c
SS
21243 break;
21244 case DW_LNS_advance_line:
a05a36a5
DE
21245 {
21246 int line_delta
21247 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21248 line_ptr += bytes_read;
6f77053d
PA
21249
21250 state_machine.handle_advance_line (line_delta);
a05a36a5 21251 }
c906108c
SS
21252 break;
21253 case DW_LNS_set_file:
d9b3de22 21254 {
6f77053d 21255 file_name_index file
ecfb656c
PA
21256 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21257 &bytes_read);
d9b3de22 21258 line_ptr += bytes_read;
8c43009f 21259
6f77053d 21260 state_machine.handle_set_file (file);
d9b3de22 21261 }
c906108c
SS
21262 break;
21263 case DW_LNS_set_column:
0ad93d4f 21264 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21265 line_ptr += bytes_read;
21266 break;
21267 case DW_LNS_negate_stmt:
6f77053d 21268 state_machine.handle_negate_stmt ();
c906108c
SS
21269 break;
21270 case DW_LNS_set_basic_block:
c906108c 21271 break;
c2c6d25f
JM
21272 /* Add to the address register of the state machine the
21273 address increment value corresponding to special opcode
a738430d
MK
21274 255. I.e., this value is scaled by the minimum
21275 instruction length since special opcode 255 would have
b021a221 21276 scaled the increment. */
c906108c 21277 case DW_LNS_const_add_pc:
6f77053d 21278 state_machine.handle_const_add_pc ();
c906108c
SS
21279 break;
21280 case DW_LNS_fixed_advance_pc:
3e29f34a 21281 {
6f77053d 21282 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21283 line_ptr += 2;
6f77053d
PA
21284
21285 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21286 }
c906108c 21287 break;
9aa1fe7e 21288 default:
a738430d
MK
21289 {
21290 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21291 int i;
a738430d 21292
debd256d 21293 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21294 {
21295 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21296 line_ptr += bytes_read;
21297 }
21298 }
c906108c
SS
21299 }
21300 }
d9b3de22
DE
21301
21302 if (!end_sequence)
21303 dwarf2_debug_line_missing_end_sequence_complaint ();
21304
21305 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21306 in which case we still finish recording the last line). */
6f77053d 21307 state_machine.record_line (true);
c906108c 21308 }
f3f5162e
DE
21309}
21310
21311/* Decode the Line Number Program (LNP) for the given line_header
21312 structure and CU. The actual information extracted and the type
21313 of structures created from the LNP depends on the value of PST.
21314
21315 1. If PST is NULL, then this procedure uses the data from the program
21316 to create all necessary symbol tables, and their linetables.
21317
21318 2. If PST is not NULL, this procedure reads the program to determine
21319 the list of files included by the unit represented by PST, and
21320 builds all the associated partial symbol tables.
21321
21322 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21323 It is used for relative paths in the line table.
21324 NOTE: When processing partial symtabs (pst != NULL),
21325 comp_dir == pst->dirname.
21326
21327 NOTE: It is important that psymtabs have the same file name (via strcmp)
21328 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21329 symtab we don't use it in the name of the psymtabs we create.
21330 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21331 A good testcase for this is mb-inline.exp.
21332
527f3840
JK
21333 LOWPC is the lowest address in CU (or 0 if not known).
21334
21335 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21336 for its PC<->lines mapping information. Otherwise only the filename
21337 table is read in. */
f3f5162e
DE
21338
21339static void
21340dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21341 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21342 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21343{
5e22e966 21344 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21345 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21346
527f3840
JK
21347 if (decode_mapping)
21348 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21349
21350 if (decode_for_pst_p)
21351 {
aaa75496 21352 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21353 create the psymtab of each included file. */
7ba99d21 21354 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21355 if (file_entry.included_p == 1)
21356 {
c89b44cd 21357 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21358 const char *include_name =
7ba99d21
AT
21359 psymtab_include_file_name (lh, file_entry, pst,
21360 comp_dir, &name_holder);
c6da4cef 21361 if (include_name != NULL)
7b249e47
TT
21362 dwarf2_create_include_psymtab
21363 (cu->per_objfile->per_bfd, include_name, pst,
21364 cu->per_objfile->per_bfd->partial_symtabs.get (),
0072c873 21365 objfile->per_bfd);
dda83cd7 21366 }
aaa75496 21367 }
cb1df416
DJ
21368 else
21369 {
21370 /* Make sure a symtab is created for every file, even files
21371 which contain only variables (i.e. no code with associated
21372 line numbers). */
c24bdb02
KS
21373 buildsym_compunit *builder = cu->get_builder ();
21374 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21375
7ba99d21 21376 for (auto &fe : lh->file_names ())
cb1df416 21377 {
804d2729 21378 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21379 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21380 {
c24bdb02 21381 builder->get_current_subfile ()->symtab
804d2729 21382 = allocate_symtab (cust,
c24bdb02 21383 builder->get_current_subfile ()->name);
43f3e411 21384 }
c24bdb02 21385 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21386 }
21387 }
c906108c
SS
21388}
21389
21390/* Start a subfile for DWARF. FILENAME is the name of the file and
21391 DIRNAME the name of the source directory which contains FILENAME
4d663531 21392 or NULL if not known.
c906108c
SS
21393 This routine tries to keep line numbers from identical absolute and
21394 relative file names in a common subfile.
21395
21396 Using the `list' example from the GDB testsuite, which resides in
21397 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21398 of /srcdir/list0.c yields the following debugging information for list0.c:
21399
c5aa993b 21400 DW_AT_name: /srcdir/list0.c
4d663531 21401 DW_AT_comp_dir: /compdir
357e46e7 21402 files.files[0].name: list0.h
c5aa993b 21403 files.files[0].dir: /srcdir
357e46e7 21404 files.files[1].name: list0.c
c5aa993b 21405 files.files[1].dir: /srcdir
c906108c
SS
21406
21407 The line number information for list0.c has to end up in a single
4f1520fb
FR
21408 subfile, so that `break /srcdir/list0.c:1' works as expected.
21409 start_subfile will ensure that this happens provided that we pass the
21410 concatenation of files.files[1].dir and files.files[1].name as the
21411 subfile's name. */
c906108c
SS
21412
21413static void
804d2729
TT
21414dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21415 const char *dirname)
c906108c 21416{
43816ebc 21417 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21418
4d663531 21419 /* In order not to lose the line information directory,
4f1520fb
FR
21420 we concatenate it to the filename when it makes sense.
21421 Note that the Dwarf3 standard says (speaking of filenames in line
21422 information): ``The directory index is ignored for file names
21423 that represent full path names''. Thus ignoring dirname in the
21424 `else' branch below isn't an issue. */
c906108c 21425
d5166ae1 21426 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21427 {
43816ebc
TT
21428 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21429 filename = copy.get ();
d521ce57 21430 }
c906108c 21431
c24bdb02 21432 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21433}
21434
4c2df51b
DJ
21435static void
21436var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21437 struct dwarf2_cu *cu)
4c2df51b 21438{
5e22e966 21439 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21440 struct comp_unit_head *cu_header = &cu->header;
21441
4c2df51b
DJ
21442 /* NOTE drow/2003-01-30: There used to be a comment and some special
21443 code here to turn a symbol with DW_AT_external and a
21444 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21445 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21446 with some versions of binutils) where shared libraries could have
21447 relocations against symbols in their debug information - the
21448 minimal symbol would have the right address, but the debug info
21449 would not. It's no longer necessary, because we will explicitly
21450 apply relocations when we read in the debug information now. */
21451
21452 /* A DW_AT_location attribute with no contents indicates that a
21453 variable has been optimized away. */
9d2246fc 21454 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21455 {
f1e6e072 21456 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21457 return;
21458 }
21459
21460 /* Handle one degenerate form of location expression specially, to
21461 preserve GDB's previous behavior when section offsets are
336d760d
AT
21462 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21463 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21464
9d2246fc
TT
21465 if (attr->form_is_block ())
21466 {
21467 struct dwarf_block *block = attr->as_block ();
21468
21469 if ((block->data[0] == DW_OP_addr
21470 && block->size == 1 + cu_header->addr_size)
21471 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 21472 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
21473 && (block->size
21474 == 1 + leb128_size (&block->data[1]))))
21475 {
21476 unsigned int dummy;
21477
21478 if (block->data[0] == DW_OP_addr)
21479 SET_SYMBOL_VALUE_ADDRESS
21480 (sym, cu->header.read_address (objfile->obfd,
21481 block->data + 1,
38583298 21482 &dummy));
9d2246fc
TT
21483 else
21484 SET_SYMBOL_VALUE_ADDRESS
21485 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21486 &dummy));
21487 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21488 fixup_symbol_section (sym, objfile);
21489 SET_SYMBOL_VALUE_ADDRESS
21490 (sym,
21491 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 21492 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
21493 return;
21494 }
4c2df51b
DJ
21495 }
21496
21497 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21498 expression evaluator, and use LOC_COMPUTED only when necessary
21499 (i.e. when the value of a register or memory location is
21500 referenced, or a thread-local block, etc.). Then again, it might
21501 not be worthwhile. I'm assuming that it isn't unless performance
21502 or memory numbers show me otherwise. */
21503
f1e6e072 21504 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21505
f1e6e072 21506 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21507 cu->has_loclist = true;
4c2df51b
DJ
21508}
21509
c906108c
SS
21510/* Given a pointer to a DWARF information entry, figure out if we need
21511 to make a symbol table entry for it, and if so, create a new entry
21512 and return a pointer to it.
21513 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21514 used the passed type.
21515 If SPACE is not NULL, use it to hold the new symbol. If it is
21516 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21517
21518static struct symbol *
5e2db402
TT
21519new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21520 struct symbol *space)
c906108c 21521{
976ca316
SM
21522 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21523 struct objfile *objfile = per_objfile->objfile;
08feed99 21524 struct gdbarch *gdbarch = objfile->arch ();
c906108c 21525 struct symbol *sym = NULL;
15d034d0 21526 const char *name;
c906108c
SS
21527 struct attribute *attr = NULL;
21528 struct attribute *attr2 = NULL;
e142c38c 21529 CORE_ADDR baseaddr;
e37fd15a
SW
21530 struct pending **list_to_add = NULL;
21531
edb3359d 21532 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21533
b3b3bada 21534 baseaddr = objfile->text_section_offset ();
c906108c 21535
94af9270 21536 name = dwarf2_name (die, cu);
c906108c
SS
21537 if (name)
21538 {
34eaf542 21539 int suppress_add = 0;
94af9270 21540
34eaf542
TT
21541 if (space)
21542 sym = space;
21543 else
8c14c3a3 21544 sym = new (&objfile->objfile_obstack) symbol;
c906108c 21545 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21546
21547 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21548 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
21549 /* Fortran does not have mangling standard and the mangling does differ
21550 between gfortran, iFort etc. */
bcfe6157
TT
21551 const char *physname
21552 = (cu->language == language_fortran
21553 ? dwarf2_full_name (name, die, cu)
21554 : dwarf2_physname (name, die, cu));
21555 const char *linkagename = dw2_linkage_name (die, cu);
21556
21557 if (linkagename == nullptr || cu->language == language_ada)
21558 sym->set_linkage_name (physname);
21559 else
21560 {
21561 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21562 sym->set_linkage_name (linkagename);
21563 }
f55ee35c 21564
c906108c 21565 /* Default assumptions.
dda83cd7 21566 Use the passed type or decode it from the die. */
176620f1 21567 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21568 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21569 if (type != NULL)
21570 SYMBOL_TYPE (sym) = type;
21571 else
e7c27a73 21572 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21573 attr = dwarf2_attr (die,
21574 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21575 cu);
435d3d88 21576 if (attr != nullptr)
529908cb 21577 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 21578
edb3359d
DJ
21579 attr = dwarf2_attr (die,
21580 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21581 cu);
aa659cfa 21582 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 21583 {
529908cb 21584 file_name_index file_index
aa659cfa 21585 = (file_name_index) attr->as_nonnegative ();
8c43009f 21586 struct file_entry *fe;
9a619af0 21587
ecfb656c
PA
21588 if (cu->line_header != NULL)
21589 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21590 else
21591 fe = NULL;
21592
21593 if (fe == NULL)
b98664d3 21594 complaint (_("file index out of range"));
8c43009f
PA
21595 else
21596 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21597 }
21598
c906108c
SS
21599 switch (die->tag)
21600 {
21601 case DW_TAG_label:
e142c38c 21602 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21603 if (attr != nullptr)
3e29f34a
MR
21604 {
21605 CORE_ADDR addr;
21606
95f982e5 21607 addr = attr->as_address ();
3e29f34a 21608 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21609 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 21610 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 21611 }
8f5c6526
TV
21612 else
21613 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
21614 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21615 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 21616 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21617 break;
21618 case DW_TAG_subprogram:
21619 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21620 finish_block. */
f1e6e072 21621 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21622 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 21623 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
21624 || cu->language == language_ada
21625 || cu->language == language_fortran)
c906108c 21626 {
dda83cd7
SM
21627 /* Subprograms marked external are stored as a global symbol.
21628 Ada and Fortran subprograms, whether marked external or
21629 not, are always stored as a global symbol, because we want
21630 to be able to access them globally. For instance, we want
21631 to be able to break on a nested subprogram without having
21632 to specify the context. */
c24bdb02 21633 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21634 }
21635 else
21636 {
e37fd15a 21637 list_to_add = cu->list_in_scope;
c906108c
SS
21638 }
21639 break;
edb3359d
DJ
21640 case DW_TAG_inlined_subroutine:
21641 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21642 finish_block. */
f1e6e072 21643 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21644 SYMBOL_INLINED (sym) = 1;
481860b3 21645 list_to_add = cu->list_in_scope;
edb3359d 21646 break;
34eaf542
TT
21647 case DW_TAG_template_value_param:
21648 suppress_add = 1;
21649 /* Fall through. */
72929c62 21650 case DW_TAG_constant:
c906108c 21651 case DW_TAG_variable:
254e6b9e 21652 case DW_TAG_member:
0963b4bd
MS
21653 /* Compilation with minimal debug info may result in
21654 variables with missing type entries. Change the
21655 misleading `void' type to something sensible. */
78134374 21656 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 21657 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21658
e142c38c 21659 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21660 /* In the case of DW_TAG_member, we should only be called for
21661 static const members. */
21662 if (die->tag == DW_TAG_member)
21663 {
3863f96c
DE
21664 /* dwarf2_add_field uses die_is_declaration,
21665 so we do the same. */
254e6b9e
DE
21666 gdb_assert (die_is_declaration (die, cu));
21667 gdb_assert (attr);
21668 }
435d3d88 21669 if (attr != nullptr)
c906108c 21670 {
e7c27a73 21671 dwarf2_const_value (attr, sym, cu);
e142c38c 21672 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21673 if (!suppress_add)
34eaf542 21674 {
c45bc3f8 21675 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 21676 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21677 else
e37fd15a 21678 list_to_add = cu->list_in_scope;
34eaf542 21679 }
c906108c
SS
21680 break;
21681 }
e142c38c 21682 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21683 if (attr != nullptr)
c906108c 21684 {
e7c27a73 21685 var_decode_location (attr, sym, cu);
e142c38c 21686 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21687
21688 /* Fortran explicitly imports any global symbols to the local
21689 scope by DW_TAG_common_block. */
21690 if (cu->language == language_fortran && die->parent
21691 && die->parent->tag == DW_TAG_common_block)
21692 attr2 = NULL;
21693
caac4577
JG
21694 if (SYMBOL_CLASS (sym) == LOC_STATIC
21695 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 21696 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
21697 {
21698 /* When a static variable is eliminated by the linker,
21699 the corresponding debug information is not stripped
21700 out, but the variable address is set to null;
21701 do not add such variables into symbol table. */
21702 }
c45bc3f8 21703 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 21704 {
4b610737
TT
21705 if (SYMBOL_CLASS (sym) == LOC_STATIC
21706 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 21707 && per_objfile->per_bfd->can_copy)
4b610737
TT
21708 {
21709 /* A global static variable might be subject to
21710 copy relocation. We first check for a local
21711 minsym, though, because maybe the symbol was
21712 marked hidden, in which case this would not
21713 apply. */
21714 bound_minimal_symbol found
21715 = (lookup_minimal_symbol_linkage
987012b8 21716 (sym->linkage_name (), objfile));
4b610737
TT
21717 if (found.minsym != nullptr)
21718 sym->maybe_copied = 1;
21719 }
f55ee35c 21720
1c809c68
TT
21721 /* A variable with DW_AT_external is never static,
21722 but it may be block-scoped. */
804d2729 21723 list_to_add
c24bdb02
KS
21724 = ((cu->list_in_scope
21725 == cu->get_builder ()->get_file_symbols ())
21726 ? cu->get_builder ()->get_global_symbols ()
804d2729 21727 : cu->list_in_scope);
1c809c68 21728 }
c906108c 21729 else
e37fd15a 21730 list_to_add = cu->list_in_scope;
c906108c
SS
21731 }
21732 else
21733 {
21734 /* We do not know the address of this symbol.
dda83cd7
SM
21735 If it is an external symbol and we have type information
21736 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21737 The address of the variable will then be determined from
21738 the minimal symbol table whenever the variable is
21739 referenced. */
e142c38c 21740 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21741
21742 /* Fortran explicitly imports any global symbols to the local
21743 scope by DW_TAG_common_block. */
21744 if (cu->language == language_fortran && die->parent
21745 && die->parent->tag == DW_TAG_common_block)
21746 {
21747 /* SYMBOL_CLASS doesn't matter here because
21748 read_common_block is going to reset it. */
21749 if (!suppress_add)
21750 list_to_add = cu->list_in_scope;
21751 }
c45bc3f8 21752 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 21753 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21754 {
0fe7935b
DJ
21755 /* A variable with DW_AT_external is never static, but it
21756 may be block-scoped. */
804d2729 21757 list_to_add
c24bdb02
KS
21758 = ((cu->list_in_scope
21759 == cu->get_builder ()->get_file_symbols ())
21760 ? cu->get_builder ()->get_global_symbols ()
804d2729 21761 : cu->list_in_scope);
0fe7935b 21762
f1e6e072 21763 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21764 }
442ddf59
JK
21765 else if (!die_is_declaration (die, cu))
21766 {
21767 /* Use the default LOC_OPTIMIZED_OUT class. */
21768 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21769 if (!suppress_add)
21770 list_to_add = cu->list_in_scope;
442ddf59 21771 }
c906108c
SS
21772 }
21773 break;
21774 case DW_TAG_formal_parameter:
a60f3166 21775 {
3db19b2d
TV
21776 /* If we are inside a function, mark this as an argument. If
21777 not, we might be looking at an argument to an inlined function
21778 when we do not have enough information to show inlined frames;
21779 pretend it's a local variable in that case so that the user can
21780 still see it. */
21781 struct context_stack *curr
21782 = cu->get_builder ()->get_current_context_stack ();
21783 if (curr != nullptr && curr->name != nullptr)
21784 SYMBOL_IS_ARGUMENT (sym) = 1;
a60f3166 21785 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21786 if (attr != nullptr)
a60f3166
TT
21787 {
21788 var_decode_location (attr, sym, cu);
21789 }
21790 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21791 if (attr != nullptr)
a60f3166
TT
21792 {
21793 dwarf2_const_value (attr, sym, cu);
21794 }
f346a30d 21795
a60f3166
TT
21796 list_to_add = cu->list_in_scope;
21797 }
c906108c
SS
21798 break;
21799 case DW_TAG_unspecified_parameters:
21800 /* From varargs functions; gdb doesn't seem to have any
21801 interest in this information, so just ignore it for now.
21802 (FIXME?) */
21803 break;
34eaf542
TT
21804 case DW_TAG_template_type_param:
21805 suppress_add = 1;
21806 /* Fall through. */
c906108c 21807 case DW_TAG_class_type:
680b30c7 21808 case DW_TAG_interface_type:
c906108c
SS
21809 case DW_TAG_structure_type:
21810 case DW_TAG_union_type:
72019c9c 21811 case DW_TAG_set_type:
c906108c 21812 case DW_TAG_enumeration_type:
f1e6e072 21813 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21814 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21815
63d06c5c 21816 {
9c37b5ae 21817 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21818 really ever be static objects: otherwise, if you try
21819 to, say, break of a class's method and you're in a file
21820 which doesn't mention that class, it won't work unless
21821 the check for all static symbols in lookup_symbol_aux
21822 saves you. See the OtherFileClass tests in
21823 gdb.c++/namespace.exp. */
21824
e37fd15a 21825 if (!suppress_add)
34eaf542 21826 {
c24bdb02 21827 buildsym_compunit *builder = cu->get_builder ();
804d2729 21828 list_to_add
c24bdb02 21829 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21830 && cu->language == language_cplus
c24bdb02 21831 ? builder->get_global_symbols ()
804d2729 21832 : cu->list_in_scope);
63d06c5c 21833
64382290 21834 /* The semantics of C++ state that "struct foo {
9c37b5ae 21835 ... }" also defines a typedef for "foo". */
64382290 21836 if (cu->language == language_cplus
45280282 21837 || cu->language == language_ada
c44af4eb
TT
21838 || cu->language == language_d
21839 || cu->language == language_rust)
64382290
TT
21840 {
21841 /* The symbol's name is already allocated along
21842 with this objfile, so we don't need to
21843 duplicate it for the type. */
7d93a1e0 21844 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21845 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21846 }
63d06c5c
DC
21847 }
21848 }
c906108c
SS
21849 break;
21850 case DW_TAG_typedef:
f1e6e072 21851 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21852 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21853 list_to_add = cu->list_in_scope;
63d06c5c 21854 break;
d8f62e84 21855 case DW_TAG_array_type:
c906108c 21856 case DW_TAG_base_type:
dda83cd7 21857 case DW_TAG_subrange_type:
f1e6e072 21858 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21859 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21860 list_to_add = cu->list_in_scope;
c906108c
SS
21861 break;
21862 case DW_TAG_enumerator:
e142c38c 21863 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21864 if (attr != nullptr)
c906108c 21865 {
e7c27a73 21866 dwarf2_const_value (attr, sym, cu);
c906108c 21867 }
63d06c5c
DC
21868 {
21869 /* NOTE: carlton/2003-11-10: See comment above in the
21870 DW_TAG_class_type, etc. block. */
21871
804d2729 21872 list_to_add
c24bdb02 21873 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21874 && cu->language == language_cplus
c24bdb02 21875 ? cu->get_builder ()->get_global_symbols ()
804d2729 21876 : cu->list_in_scope);
63d06c5c 21877 }
c906108c 21878 break;
74921315 21879 case DW_TAG_imported_declaration:
5c4e30ca 21880 case DW_TAG_namespace:
f1e6e072 21881 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21882 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21883 break;
530e8392
KB
21884 case DW_TAG_module:
21885 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21886 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21887 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21888 break;
4357ac6c 21889 case DW_TAG_common_block:
f1e6e072 21890 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21891 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21892 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21893 break;
c906108c
SS
21894 default:
21895 /* Not a tag we recognize. Hopefully we aren't processing
21896 trash data, but since we must specifically ignore things
21897 we don't recognize, there is nothing else we should do at
0963b4bd 21898 this point. */
b98664d3 21899 complaint (_("unsupported tag: '%s'"),
4d3c2250 21900 dwarf_tag_name (die->tag));
c906108c
SS
21901 break;
21902 }
df8a16a1 21903
e37fd15a
SW
21904 if (suppress_add)
21905 {
21906 sym->hash_next = objfile->template_symbols;
21907 objfile->template_symbols = sym;
21908 list_to_add = NULL;
21909 }
21910
21911 if (list_to_add != NULL)
d3cb6808 21912 add_symbol_to_list (sym, list_to_add);
e37fd15a 21913
df8a16a1
DJ
21914 /* For the benefit of old versions of GCC, check for anonymous
21915 namespaces based on the demangled name. */
4d4ec4e5 21916 if (!cu->processing_has_namespace_info
94af9270 21917 && cu->language == language_cplus)
c24bdb02 21918 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21919 }
21920 return (sym);
21921}
21922
98bfdba5
PA
21923/* Given an attr with a DW_FORM_dataN value in host byte order,
21924 zero-extend it as appropriate for the symbol's type. The DWARF
21925 standard (v4) is not entirely clear about the meaning of using
21926 DW_FORM_dataN for a constant with a signed type, where the type is
21927 wider than the data. The conclusion of a discussion on the DWARF
21928 list was that this is unspecified. We choose to always zero-extend
21929 because that is the interpretation long in use by GCC. */
c906108c 21930
98bfdba5 21931static gdb_byte *
ff39bb5e 21932dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21933 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21934{
5e22e966 21935 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21936 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21937 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 21938 LONGEST l = attr->constant_value (0);
98bfdba5
PA
21939
21940 if (bits < sizeof (*value) * 8)
21941 {
21942 l &= ((LONGEST) 1 << bits) - 1;
21943 *value = l;
21944 }
21945 else if (bits == sizeof (*value) * 8)
21946 *value = l;
21947 else
21948 {
224c3ddb 21949 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21950 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21951 return bytes;
21952 }
21953
21954 return NULL;
21955}
21956
21957/* Read a constant value from an attribute. Either set *VALUE, or if
21958 the value does not fit in *VALUE, set *BYTES - either already
21959 allocated on the objfile obstack, or newly allocated on OBSTACK,
21960 or, set *BATON, if we translated the constant to a location
21961 expression. */
21962
21963static void
ff39bb5e 21964dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21965 const char *name, struct obstack *obstack,
21966 struct dwarf2_cu *cu,
d521ce57 21967 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21968 struct dwarf2_locexpr_baton **baton)
21969{
5e22e966 21970 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21971 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21972 struct comp_unit_head *cu_header = &cu->header;
c906108c 21973 struct dwarf_block *blk;
98bfdba5
PA
21974 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21975 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21976
21977 *value = 0;
21978 *bytes = NULL;
21979 *baton = NULL;
c906108c
SS
21980
21981 switch (attr->form)
21982 {
21983 case DW_FORM_addr:
336d760d 21984 case DW_FORM_addrx:
3019eac3 21985 case DW_FORM_GNU_addr_index:
ac56253d 21986 {
ac56253d
TT
21987 gdb_byte *data;
21988
98bfdba5
PA
21989 if (TYPE_LENGTH (type) != cu_header->addr_size)
21990 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21991 cu_header->addr_size,
98bfdba5 21992 TYPE_LENGTH (type));
ac56253d
TT
21993 /* Symbols of this form are reasonably rare, so we just
21994 piggyback on the existing location code rather than writing
21995 a new implementation of symbol_computed_ops. */
8d749320 21996 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21997 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21998 (*baton)->per_cu = cu->per_cu;
21999 gdb_assert ((*baton)->per_cu);
ac56253d 22000
98bfdba5 22001 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22002 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22003 (*baton)->data = data;
ac56253d
TT
22004
22005 data[0] = DW_OP_addr;
22006 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22007 byte_order, attr->as_address ());
ac56253d 22008 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22009 }
c906108c 22010 break;
4ac36638 22011 case DW_FORM_string:
93b5768b 22012 case DW_FORM_strp:
cf532bd1 22013 case DW_FORM_strx:
3019eac3 22014 case DW_FORM_GNU_str_index:
36586728 22015 case DW_FORM_GNU_strp_alt:
c6481205 22016 /* The string is already allocated on the objfile obstack, point
98bfdba5 22017 directly to it. */
2c830f54 22018 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22019 break;
c906108c
SS
22020 case DW_FORM_block1:
22021 case DW_FORM_block2:
22022 case DW_FORM_block4:
22023 case DW_FORM_block:
2dc7f7b3 22024 case DW_FORM_exprloc:
0224619f 22025 case DW_FORM_data16:
9d2246fc 22026 blk = attr->as_block ();
98bfdba5
PA
22027 if (TYPE_LENGTH (type) != blk->size)
22028 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22029 TYPE_LENGTH (type));
22030 *bytes = blk->data;
c906108c 22031 break;
2df3850c
JM
22032
22033 /* The DW_AT_const_value attributes are supposed to carry the
22034 symbol's value "represented as it would be on the target
22035 architecture." By the time we get here, it's already been
22036 converted to host endianness, so we just need to sign- or
22037 zero-extend it as appropriate. */
22038 case DW_FORM_data1:
3aef2284 22039 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22040 break;
c906108c 22041 case DW_FORM_data2:
3aef2284 22042 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22043 break;
c906108c 22044 case DW_FORM_data4:
3aef2284 22045 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22046 break;
c906108c 22047 case DW_FORM_data8:
3aef2284 22048 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22049 break;
22050
c906108c 22051 case DW_FORM_sdata:
663c44ac 22052 case DW_FORM_implicit_const:
1bc397c5 22053 *value = attr->as_signed ();
2df3850c
JM
22054 break;
22055
c906108c 22056 case DW_FORM_udata:
529908cb 22057 *value = attr->as_unsigned ();
c906108c 22058 break;
2df3850c 22059
c906108c 22060 default:
b98664d3 22061 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22062 dwarf_form_name (attr->form));
98bfdba5 22063 *value = 0;
c906108c
SS
22064 break;
22065 }
22066}
22067
2df3850c 22068
98bfdba5
PA
22069/* Copy constant value from an attribute to a symbol. */
22070
2df3850c 22071static void
ff39bb5e 22072dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22073 struct dwarf2_cu *cu)
2df3850c 22074{
5e22e966 22075 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22076 LONGEST value;
d521ce57 22077 const gdb_byte *bytes;
98bfdba5 22078 struct dwarf2_locexpr_baton *baton;
2df3850c 22079
98bfdba5 22080 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22081 sym->print_name (),
98bfdba5
PA
22082 &objfile->objfile_obstack, cu,
22083 &value, &bytes, &baton);
2df3850c 22084
98bfdba5
PA
22085 if (baton != NULL)
22086 {
98bfdba5 22087 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22088 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22089 }
22090 else if (bytes != NULL)
22091 {
22092 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22093 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22094 }
22095 else
22096 {
22097 SYMBOL_VALUE (sym) = value;
f1e6e072 22098 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22099 }
2df3850c
JM
22100}
22101
c906108c
SS
22102/* Return the type of the die in question using its DW_AT_type attribute. */
22103
22104static struct type *
e7c27a73 22105die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22106{
c906108c 22107 struct attribute *type_attr;
c906108c 22108
e142c38c 22109 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22110 if (!type_attr)
22111 {
5e22e966 22112 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22113 /* A missing DW_AT_type represents a void type. */
518817b3 22114 return objfile_type (objfile)->builtin_void;
c906108c 22115 }
348e048f 22116
673bfd45 22117 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22118}
22119
b4ba55a1
JB
22120/* True iff CU's producer generates GNAT Ada auxiliary information
22121 that allows to find parallel types through that information instead
22122 of having to do expensive parallel lookups by type name. */
22123
22124static int
22125need_gnat_info (struct dwarf2_cu *cu)
22126{
de4cb04a
JB
22127 /* Assume that the Ada compiler was GNAT, which always produces
22128 the auxiliary information. */
22129 return (cu->language == language_ada);
b4ba55a1
JB
22130}
22131
b4ba55a1
JB
22132/* Return the auxiliary type of the die in question using its
22133 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22134 attribute is not present. */
22135
22136static struct type *
22137die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22138{
b4ba55a1 22139 struct attribute *type_attr;
b4ba55a1
JB
22140
22141 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22142 if (!type_attr)
22143 return NULL;
22144
673bfd45 22145 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22146}
22147
22148/* If DIE has a descriptive_type attribute, then set the TYPE's
22149 descriptive type accordingly. */
22150
22151static void
22152set_descriptive_type (struct type *type, struct die_info *die,
22153 struct dwarf2_cu *cu)
22154{
22155 struct type *descriptive_type = die_descriptive_type (die, cu);
22156
22157 if (descriptive_type)
22158 {
22159 ALLOCATE_GNAT_AUX_TYPE (type);
22160 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22161 }
22162}
22163
c906108c
SS
22164/* Return the containing type of the die in question using its
22165 DW_AT_containing_type attribute. */
22166
22167static struct type *
e7c27a73 22168die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22169{
c906108c 22170 struct attribute *type_attr;
5e22e966 22171 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22172
e142c38c 22173 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22174 if (!type_attr)
22175 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22176 "[in module %s]"), objfile_name (objfile));
33ac96f0 22177
673bfd45 22178 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22179}
22180
ac9ec31b
DE
22181/* Return an error marker type to use for the ill formed type in DIE/CU. */
22182
22183static struct type *
22184build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22185{
976ca316
SM
22186 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22187 struct objfile *objfile = per_objfile->objfile;
528e1572 22188 char *saved;
ac9ec31b 22189
528e1572
SM
22190 std::string message
22191 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22192 objfile_name (objfile),
22193 sect_offset_str (cu->header.sect_off),
22194 sect_offset_str (die->sect_off));
efba19b0 22195 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22196
19f392bc 22197 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22198}
22199
673bfd45 22200/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22201 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22202 DW_AT_containing_type.
673bfd45
DE
22203 If there is no type substitute an error marker. */
22204
c906108c 22205static struct type *
ff39bb5e 22206lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22207 struct dwarf2_cu *cu)
c906108c 22208{
976ca316
SM
22209 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22210 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22211 struct type *this_type;
22212
ac9ec31b
DE
22213 gdb_assert (attr->name == DW_AT_type
22214 || attr->name == DW_AT_GNAT_descriptive_type
22215 || attr->name == DW_AT_containing_type);
22216
673bfd45
DE
22217 /* First see if we have it cached. */
22218
36586728
TT
22219 if (attr->form == DW_FORM_GNU_ref_alt)
22220 {
22221 struct dwarf2_per_cu_data *per_cu;
0826b30a 22222 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22223
976ca316
SM
22224 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22225 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22226 }
cd6c91b4 22227 else if (attr->form_is_ref ())
673bfd45 22228 {
0826b30a 22229 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22230
976ca316 22231 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22232 }
55f1336d 22233 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22234 {
630ed6b9 22235 ULONGEST signature = attr->as_signature ();
673bfd45 22236
ac9ec31b 22237 return get_signatured_type (die, signature, cu);
673bfd45
DE
22238 }
22239 else
22240 {
b98664d3 22241 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22242 " at %s [in module %s]"),
22243 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22244 objfile_name (objfile));
ac9ec31b 22245 return build_error_marker_type (cu, die);
673bfd45
DE
22246 }
22247
22248 /* If not cached we need to read it in. */
22249
22250 if (this_type == NULL)
22251 {
ac9ec31b 22252 struct die_info *type_die = NULL;
673bfd45
DE
22253 struct dwarf2_cu *type_cu = cu;
22254
cd6c91b4 22255 if (attr->form_is_ref ())
ac9ec31b
DE
22256 type_die = follow_die_ref (die, attr, &type_cu);
22257 if (type_die == NULL)
22258 return build_error_marker_type (cu, die);
22259 /* If we find the type now, it's probably because the type came
3019eac3
DE
22260 from an inter-CU reference and the type's CU got expanded before
22261 ours. */
ac9ec31b 22262 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22263 }
22264
22265 /* If we still don't have a type use an error marker. */
22266
22267 if (this_type == NULL)
ac9ec31b 22268 return build_error_marker_type (cu, die);
673bfd45 22269
f792889a 22270 return this_type;
c906108c
SS
22271}
22272
673bfd45
DE
22273/* Return the type in DIE, CU.
22274 Returns NULL for invalid types.
22275
02142a6c 22276 This first does a lookup in die_type_hash,
673bfd45
DE
22277 and only reads the die in if necessary.
22278
22279 NOTE: This can be called when reading in partial or full symbols. */
22280
f792889a 22281static struct type *
e7c27a73 22282read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22283{
f792889a
DJ
22284 struct type *this_type;
22285
22286 this_type = get_die_type (die, cu);
22287 if (this_type)
22288 return this_type;
22289
673bfd45
DE
22290 return read_type_die_1 (die, cu);
22291}
22292
22293/* Read the type in DIE, CU.
22294 Returns NULL for invalid types. */
22295
22296static struct type *
22297read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22298{
22299 struct type *this_type = NULL;
22300
c906108c
SS
22301 switch (die->tag)
22302 {
22303 case DW_TAG_class_type:
680b30c7 22304 case DW_TAG_interface_type:
c906108c
SS
22305 case DW_TAG_structure_type:
22306 case DW_TAG_union_type:
f792889a 22307 this_type = read_structure_type (die, cu);
c906108c
SS
22308 break;
22309 case DW_TAG_enumeration_type:
f792889a 22310 this_type = read_enumeration_type (die, cu);
c906108c
SS
22311 break;
22312 case DW_TAG_subprogram:
22313 case DW_TAG_subroutine_type:
edb3359d 22314 case DW_TAG_inlined_subroutine:
f792889a 22315 this_type = read_subroutine_type (die, cu);
c906108c
SS
22316 break;
22317 case DW_TAG_array_type:
f792889a 22318 this_type = read_array_type (die, cu);
c906108c 22319 break;
72019c9c 22320 case DW_TAG_set_type:
f792889a 22321 this_type = read_set_type (die, cu);
72019c9c 22322 break;
c906108c 22323 case DW_TAG_pointer_type:
f792889a 22324 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22325 break;
22326 case DW_TAG_ptr_to_member_type:
f792889a 22327 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22328 break;
22329 case DW_TAG_reference_type:
4297a3f0
AV
22330 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22331 break;
22332 case DW_TAG_rvalue_reference_type:
22333 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22334 break;
22335 case DW_TAG_const_type:
f792889a 22336 this_type = read_tag_const_type (die, cu);
c906108c
SS
22337 break;
22338 case DW_TAG_volatile_type:
f792889a 22339 this_type = read_tag_volatile_type (die, cu);
c906108c 22340 break;
06d66ee9
TT
22341 case DW_TAG_restrict_type:
22342 this_type = read_tag_restrict_type (die, cu);
22343 break;
c906108c 22344 case DW_TAG_string_type:
f792889a 22345 this_type = read_tag_string_type (die, cu);
c906108c
SS
22346 break;
22347 case DW_TAG_typedef:
f792889a 22348 this_type = read_typedef (die, cu);
c906108c 22349 break;
a02abb62 22350 case DW_TAG_subrange_type:
f792889a 22351 this_type = read_subrange_type (die, cu);
a02abb62 22352 break;
c906108c 22353 case DW_TAG_base_type:
f792889a 22354 this_type = read_base_type (die, cu);
c906108c 22355 break;
81a17f79 22356 case DW_TAG_unspecified_type:
f792889a 22357 this_type = read_unspecified_type (die, cu);
81a17f79 22358 break;
0114d602
DJ
22359 case DW_TAG_namespace:
22360 this_type = read_namespace_type (die, cu);
22361 break;
f55ee35c
JK
22362 case DW_TAG_module:
22363 this_type = read_module_type (die, cu);
22364 break;
a2c2acaf
MW
22365 case DW_TAG_atomic_type:
22366 this_type = read_tag_atomic_type (die, cu);
22367 break;
c906108c 22368 default:
b98664d3 22369 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22370 dwarf_tag_name (die->tag));
c906108c
SS
22371 break;
22372 }
63d06c5c 22373
f792889a 22374 return this_type;
63d06c5c
DC
22375}
22376
abc72ce4
DE
22377/* See if we can figure out if the class lives in a namespace. We do
22378 this by looking for a member function; its demangled name will
22379 contain namespace info, if there is any.
22380 Return the computed name or NULL.
22381 Space for the result is allocated on the objfile's obstack.
22382 This is the full-die version of guess_partial_die_structure_name.
22383 In this case we know DIE has no useful parent. */
22384
43816ebc 22385static const char *
abc72ce4
DE
22386guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22387{
22388 struct die_info *spec_die;
22389 struct dwarf2_cu *spec_cu;
22390 struct die_info *child;
5e22e966 22391 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22392
22393 spec_cu = cu;
22394 spec_die = die_specification (die, &spec_cu);
22395 if (spec_die != NULL)
22396 {
22397 die = spec_die;
22398 cu = spec_cu;
22399 }
22400
22401 for (child = die->child;
22402 child != NULL;
22403 child = child->sibling)
22404 {
22405 if (child->tag == DW_TAG_subprogram)
22406 {
73b9be8b 22407 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22408
7d45c7c3 22409 if (linkage_name != NULL)
abc72ce4 22410 {
43816ebc 22411 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22412 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22413 const char *name = NULL;
abc72ce4
DE
22414
22415 if (actual_name != NULL)
22416 {
15d034d0 22417 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22418
22419 if (die_name != NULL
43816ebc 22420 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22421 {
22422 /* Strip off the class name from the full name.
22423 We want the prefix. */
22424 int die_name_len = strlen (die_name);
43816ebc
TT
22425 int actual_name_len = strlen (actual_name.get ());
22426 const char *ptr = actual_name.get ();
abc72ce4
DE
22427
22428 /* Test for '::' as a sanity check. */
22429 if (actual_name_len > die_name_len + 2
43816ebc 22430 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22431 name = obstack_strndup (
e3b94546 22432 &objfile->per_bfd->storage_obstack,
43816ebc 22433 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22434 }
22435 }
abc72ce4
DE
22436 return name;
22437 }
22438 }
22439 }
22440
22441 return NULL;
22442}
22443
96408a79
SA
22444/* GCC might emit a nameless typedef that has a linkage name. Determine the
22445 prefix part in such case. See
22446 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22447
a121b7c1 22448static const char *
96408a79
SA
22449anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22450{
22451 struct attribute *attr;
e6a959d6 22452 const char *base;
96408a79
SA
22453
22454 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22455 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22456 return NULL;
22457
7d45c7c3 22458 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22459 return NULL;
22460
73b9be8b 22461 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22462 const char *attr_name = attr->as_string ();
22463 if (attr == NULL || attr_name == NULL)
96408a79
SA
22464 return NULL;
22465
22466 /* dwarf2_name had to be already called. */
3b64bf15 22467 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22468
22469 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22470 base = strrchr (attr_name, ':');
22471 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22472 return "";
22473
5e22e966 22474 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22475 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22476 attr_name,
22477 &base[-1] - attr_name);
96408a79
SA
22478}
22479
fdde2d81 22480/* Return the name of the namespace/class that DIE is defined within,
0114d602 22481 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22482
0114d602
DJ
22483 For example, if we're within the method foo() in the following
22484 code:
22485
22486 namespace N {
22487 class C {
22488 void foo () {
22489 }
22490 };
22491 }
22492
22493 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22494
0d5cff50 22495static const char *
e142c38c 22496determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22497{
976ca316 22498 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
22499 struct die_info *parent, *spec_die;
22500 struct dwarf2_cu *spec_cu;
22501 struct type *parent_type;
a121b7c1 22502 const char *retval;
63d06c5c 22503
9c37b5ae 22504 if (cu->language != language_cplus
c44af4eb
TT
22505 && cu->language != language_fortran && cu->language != language_d
22506 && cu->language != language_rust)
0114d602
DJ
22507 return "";
22508
96408a79
SA
22509 retval = anonymous_struct_prefix (die, cu);
22510 if (retval)
22511 return retval;
22512
0114d602
DJ
22513 /* We have to be careful in the presence of DW_AT_specification.
22514 For example, with GCC 3.4, given the code
22515
22516 namespace N {
22517 void foo() {
22518 // Definition of N::foo.
22519 }
22520 }
22521
22522 then we'll have a tree of DIEs like this:
22523
22524 1: DW_TAG_compile_unit
22525 2: DW_TAG_namespace // N
22526 3: DW_TAG_subprogram // declaration of N::foo
22527 4: DW_TAG_subprogram // definition of N::foo
22528 DW_AT_specification // refers to die #3
22529
22530 Thus, when processing die #4, we have to pretend that we're in
22531 the context of its DW_AT_specification, namely the contex of die
22532 #3. */
22533 spec_cu = cu;
22534 spec_die = die_specification (die, &spec_cu);
22535 if (spec_die == NULL)
22536 parent = die->parent;
22537 else
63d06c5c 22538 {
0114d602
DJ
22539 parent = spec_die->parent;
22540 cu = spec_cu;
63d06c5c 22541 }
0114d602
DJ
22542
22543 if (parent == NULL)
22544 return "";
98bfdba5
PA
22545 else if (parent->building_fullname)
22546 {
22547 const char *name;
22548 const char *parent_name;
22549
22550 /* It has been seen on RealView 2.2 built binaries,
22551 DW_TAG_template_type_param types actually _defined_ as
22552 children of the parent class:
22553
22554 enum E {};
22555 template class <class Enum> Class{};
22556 Class<enum E> class_e;
22557
dda83cd7
SM
22558 1: DW_TAG_class_type (Class)
22559 2: DW_TAG_enumeration_type (E)
22560 3: DW_TAG_enumerator (enum1:0)
22561 3: DW_TAG_enumerator (enum2:1)
22562 ...
22563 2: DW_TAG_template_type_param
22564 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
22565
22566 Besides being broken debug info, it can put GDB into an
22567 infinite loop. Consider:
22568
22569 When we're building the full name for Class<E>, we'll start
22570 at Class, and go look over its template type parameters,
22571 finding E. We'll then try to build the full name of E, and
22572 reach here. We're now trying to build the full name of E,
22573 and look over the parent DIE for containing scope. In the
22574 broken case, if we followed the parent DIE of E, we'd again
22575 find Class, and once again go look at its template type
22576 arguments, etc., etc. Simply don't consider such parent die
22577 as source-level parent of this die (it can't be, the language
22578 doesn't allow it), and break the loop here. */
22579 name = dwarf2_name (die, cu);
22580 parent_name = dwarf2_name (parent, cu);
b98664d3 22581 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22582 name ? name : "<unknown>",
22583 parent_name ? parent_name : "<unknown>");
22584 return "";
22585 }
63d06c5c 22586 else
0114d602
DJ
22587 switch (parent->tag)
22588 {
63d06c5c 22589 case DW_TAG_namespace:
0114d602 22590 parent_type = read_type_die (parent, cu);
acebe513
UW
22591 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22592 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22593 Work around this problem here. */
22594 if (cu->language == language_cplus
7d93a1e0 22595 && strcmp (parent_type->name (), "::") == 0)
acebe513 22596 return "";
0114d602 22597 /* We give a name to even anonymous namespaces. */
7d93a1e0 22598 return parent_type->name ();
63d06c5c 22599 case DW_TAG_class_type:
680b30c7 22600 case DW_TAG_interface_type:
63d06c5c 22601 case DW_TAG_structure_type:
0114d602 22602 case DW_TAG_union_type:
f55ee35c 22603 case DW_TAG_module:
0114d602 22604 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
22605 if (parent_type->name () != NULL)
22606 return parent_type->name ();
0114d602
DJ
22607 else
22608 /* An anonymous structure is only allowed non-static data
22609 members; no typedefs, no member functions, et cetera.
22610 So it does not need a prefix. */
22611 return "";
abc72ce4 22612 case DW_TAG_compile_unit:
95554aad 22613 case DW_TAG_partial_unit:
abc72ce4
DE
22614 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22615 if (cu->language == language_cplus
976ca316 22616 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
22617 && die->child != NULL
22618 && (die->tag == DW_TAG_class_type
22619 || die->tag == DW_TAG_structure_type
22620 || die->tag == DW_TAG_union_type))
22621 {
43816ebc 22622 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22623 if (name != NULL)
22624 return name;
22625 }
22626 return "";
0a4b0913
AB
22627 case DW_TAG_subprogram:
22628 /* Nested subroutines in Fortran get a prefix with the name
22629 of the parent's subroutine. */
22630 if (cu->language == language_fortran)
22631 {
22632 if ((die->tag == DW_TAG_subprogram)
22633 && (dwarf2_name (parent, cu) != NULL))
22634 return dwarf2_name (parent, cu);
22635 }
22636 return determine_prefix (parent, cu);
3d567982
TT
22637 case DW_TAG_enumeration_type:
22638 parent_type = read_type_die (parent, cu);
3bc440a2 22639 if (parent_type->is_declared_class ())
3d567982 22640 {
7d93a1e0
SM
22641 if (parent_type->name () != NULL)
22642 return parent_type->name ();
3d567982
TT
22643 return "";
22644 }
22645 /* Fall through. */
63d06c5c 22646 default:
8176b9b8 22647 return determine_prefix (parent, cu);
63d06c5c 22648 }
63d06c5c
DC
22649}
22650
3e43a32a
MS
22651/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22652 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22653 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22654 an obconcat, otherwise allocate storage for the result. The CU argument is
22655 used to determine the language and hence, the appropriate separator. */
987504bb 22656
f55ee35c 22657#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22658
22659static char *
f55ee35c 22660typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 22661 int physname, struct dwarf2_cu *cu)
63d06c5c 22662{
f55ee35c 22663 const char *lead = "";
5c315b68 22664 const char *sep;
63d06c5c 22665
3e43a32a
MS
22666 if (suffix == NULL || suffix[0] == '\0'
22667 || prefix == NULL || prefix[0] == '\0')
987504bb 22668 sep = "";
45280282
IB
22669 else if (cu->language == language_d)
22670 {
22671 /* For D, the 'main' function could be defined in any module, but it
22672 should never be prefixed. */
22673 if (strcmp (suffix, "D main") == 0)
22674 {
22675 prefix = "";
22676 sep = "";
22677 }
22678 else
22679 sep = ".";
22680 }
f55ee35c
JK
22681 else if (cu->language == language_fortran && physname)
22682 {
22683 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22684 DW_AT_MIPS_linkage_name is preferred and used instead. */
22685
22686 lead = "__";
22687 sep = "_MOD_";
22688 }
987504bb
JJ
22689 else
22690 sep = "::";
63d06c5c 22691
6dd47d34
DE
22692 if (prefix == NULL)
22693 prefix = "";
22694 if (suffix == NULL)
22695 suffix = "";
22696
987504bb
JJ
22697 if (obs == NULL)
22698 {
3e43a32a 22699 char *retval
224c3ddb
SM
22700 = ((char *)
22701 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22702
f55ee35c
JK
22703 strcpy (retval, lead);
22704 strcat (retval, prefix);
6dd47d34
DE
22705 strcat (retval, sep);
22706 strcat (retval, suffix);
63d06c5c
DC
22707 return retval;
22708 }
987504bb
JJ
22709 else
22710 {
22711 /* We have an obstack. */
f55ee35c 22712 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22713 }
63d06c5c
DC
22714}
22715
71c25dea
TT
22716/* Get name of a die, return NULL if not found. */
22717
15d034d0
TT
22718static const char *
22719dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 22720 struct objfile *objfile)
71c25dea
TT
22721{
22722 if (name && cu->language == language_cplus)
22723 {
596dc4ad
TT
22724 gdb::unique_xmalloc_ptr<char> canon_name
22725 = cp_canonicalize_string (name);
71c25dea 22726
596dc4ad
TT
22727 if (canon_name != nullptr)
22728 name = objfile->intern (canon_name.get ());
71c25dea
TT
22729 }
22730
22731 return name;
c906108c
SS
22732}
22733
96553a0c
DE
22734/* Get name of a die, return NULL if not found.
22735 Anonymous namespaces are converted to their magic string. */
9219021c 22736
15d034d0 22737static const char *
e142c38c 22738dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22739{
22740 struct attribute *attr;
5e22e966 22741 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 22742
e142c38c 22743 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
22744 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22745 if (attr_name == nullptr
96553a0c 22746 && die->tag != DW_TAG_namespace
53832f31
TT
22747 && die->tag != DW_TAG_class_type
22748 && die->tag != DW_TAG_interface_type
22749 && die->tag != DW_TAG_structure_type
22750 && die->tag != DW_TAG_union_type)
71c25dea
TT
22751 return NULL;
22752
22753 switch (die->tag)
22754 {
22755 case DW_TAG_compile_unit:
95554aad 22756 case DW_TAG_partial_unit:
71c25dea
TT
22757 /* Compilation units have a DW_AT_name that is a filename, not
22758 a source language identifier. */
22759 case DW_TAG_enumeration_type:
22760 case DW_TAG_enumerator:
22761 /* These tags always have simple identifiers already; no need
22762 to canonicalize them. */
2c830f54 22763 return attr_name;
907af001 22764
96553a0c 22765 case DW_TAG_namespace:
2c830f54
TT
22766 if (attr_name != nullptr)
22767 return attr_name;
96553a0c
DE
22768 return CP_ANONYMOUS_NAMESPACE_STR;
22769
907af001
UW
22770 case DW_TAG_class_type:
22771 case DW_TAG_interface_type:
22772 case DW_TAG_structure_type:
22773 case DW_TAG_union_type:
22774 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22775 structures or unions. These were of the form "._%d" in GCC 4.1,
22776 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22777 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
22778 if (attr_name != nullptr
22779 && (startswith (attr_name, "._")
22780 || startswith (attr_name, "<anonymous")))
907af001 22781 return NULL;
53832f31
TT
22782
22783 /* GCC might emit a nameless typedef that has a linkage name. See
22784 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 22785 if (!attr || attr_name == NULL)
53832f31 22786 {
73b9be8b 22787 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 22788 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 22789 if (attr == NULL || attr_name == NULL)
53832f31
TT
22790 return NULL;
22791
2c830f54 22792 /* Avoid demangling attr_name the second time on a second
df5c6c50 22793 call for the same DIE. */
3b64bf15 22794 if (!attr->canonical_string_p ())
53832f31 22795 {
43816ebc 22796 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 22797 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
22798 if (demangled == nullptr)
22799 return nullptr;
43816ebc 22800
c6481205 22801 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 22802 attr_name = attr->as_string ();
53832f31 22803 }
67430cd0 22804
2c830f54
TT
22805 /* Strip any leading namespaces/classes, keep only the
22806 base name. DW_AT_name for named DIEs does not
22807 contain the prefixes. */
22808 const char *base = strrchr (attr_name, ':');
22809 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
22810 return &base[1];
22811 else
2c830f54 22812 return attr_name;
53832f31 22813 }
907af001
UW
22814 break;
22815
71c25dea 22816 default:
907af001
UW
22817 break;
22818 }
22819
3b64bf15 22820 if (!attr->canonical_string_p ())
c6481205
TT
22821 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22822 objfile));
2c830f54 22823 return attr->as_string ();
9219021c
DC
22824}
22825
22826/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22827 is none. *EXT_CU is the CU containing DIE on input, and the CU
22828 containing the return value on output. */
9219021c
DC
22829
22830static struct die_info *
f2f0e013 22831dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22832{
22833 struct attribute *attr;
9219021c 22834
f2f0e013 22835 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22836 if (attr == NULL)
22837 return NULL;
22838
f2f0e013 22839 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22840}
22841
f9aca02d 22842static void
d97bc12b 22843dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22844{
22845 unsigned int i;
22846
d97bc12b 22847 print_spaces (indent, f);
9d8780f0 22848 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22849 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22850 sect_offset_str (die->sect_off));
d97bc12b
DE
22851
22852 if (die->parent != NULL)
22853 {
22854 print_spaces (indent, f);
9d8780f0
SM
22855 fprintf_unfiltered (f, " parent at offset: %s\n",
22856 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22857 }
22858
22859 print_spaces (indent, f);
22860 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22861 dwarf_bool_name (die->child != NULL));
c906108c 22862
d97bc12b
DE
22863 print_spaces (indent, f);
22864 fprintf_unfiltered (f, " attributes:\n");
22865
c906108c
SS
22866 for (i = 0; i < die->num_attrs; ++i)
22867 {
d97bc12b
DE
22868 print_spaces (indent, f);
22869 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22870 dwarf_attr_name (die->attrs[i].name),
22871 dwarf_form_name (die->attrs[i].form));
d97bc12b 22872
c906108c
SS
22873 switch (die->attrs[i].form)
22874 {
c906108c 22875 case DW_FORM_addr:
336d760d 22876 case DW_FORM_addrx:
3019eac3 22877 case DW_FORM_GNU_addr_index:
d97bc12b 22878 fprintf_unfiltered (f, "address: ");
36d378cf 22879 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
22880 break;
22881 case DW_FORM_block2:
22882 case DW_FORM_block4:
22883 case DW_FORM_block:
22884 case DW_FORM_block1:
56eb65bd 22885 fprintf_unfiltered (f, "block: size %s",
9d2246fc 22886 pulongest (die->attrs[i].as_block ()->size));
c906108c 22887 break;
2dc7f7b3 22888 case DW_FORM_exprloc:
56eb65bd 22889 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 22890 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 22891 break;
0224619f
JK
22892 case DW_FORM_data16:
22893 fprintf_unfiltered (f, "constant of 16 bytes");
22894 break;
4568ecf9
DE
22895 case DW_FORM_ref_addr:
22896 fprintf_unfiltered (f, "ref address: ");
529908cb 22897 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 22898 break;
36586728
TT
22899 case DW_FORM_GNU_ref_alt:
22900 fprintf_unfiltered (f, "alt ref address: ");
529908cb 22901 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 22902 break;
10b3939b
DJ
22903 case DW_FORM_ref1:
22904 case DW_FORM_ref2:
22905 case DW_FORM_ref4:
4568ecf9
DE
22906 case DW_FORM_ref8:
22907 case DW_FORM_ref_udata:
d97bc12b 22908 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 22909 (long) (die->attrs[i].as_unsigned ()));
10b3939b 22910 break;
c906108c
SS
22911 case DW_FORM_data1:
22912 case DW_FORM_data2:
22913 case DW_FORM_data4:
ce5d95e1 22914 case DW_FORM_data8:
c906108c 22915 case DW_FORM_udata:
43bbcdc2 22916 fprintf_unfiltered (f, "constant: %s",
529908cb 22917 pulongest (die->attrs[i].as_unsigned ()));
c906108c 22918 break;
2dc7f7b3
TT
22919 case DW_FORM_sec_offset:
22920 fprintf_unfiltered (f, "section offset: %s",
529908cb 22921 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 22922 break;
55f1336d 22923 case DW_FORM_ref_sig8:
ac9ec31b 22924 fprintf_unfiltered (f, "signature: %s",
630ed6b9 22925 hex_string (die->attrs[i].as_signature ()));
348e048f 22926 break;
c906108c 22927 case DW_FORM_string:
4bdf3d34 22928 case DW_FORM_strp:
43988095 22929 case DW_FORM_line_strp:
cf532bd1 22930 case DW_FORM_strx:
3019eac3 22931 case DW_FORM_GNU_str_index:
36586728 22932 case DW_FORM_GNU_strp_alt:
8285870a 22933 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
22934 die->attrs[i].as_string ()
22935 ? die->attrs[i].as_string () : "",
22936 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
22937 break;
22938 case DW_FORM_flag:
c45bc3f8 22939 if (die->attrs[i].as_boolean ())
d97bc12b 22940 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22941 else
d97bc12b 22942 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22943 break;
2dc7f7b3
TT
22944 case DW_FORM_flag_present:
22945 fprintf_unfiltered (f, "flag: TRUE");
22946 break;
a8329558 22947 case DW_FORM_indirect:
0963b4bd
MS
22948 /* The reader will have reduced the indirect form to
22949 the "base form" so this form should not occur. */
5f48f8f3 22950 fprintf_unfiltered (f,
3e43a32a 22951 "unexpected attribute form: DW_FORM_indirect");
a8329558 22952 break;
1bc397c5 22953 case DW_FORM_sdata:
663c44ac
JK
22954 case DW_FORM_implicit_const:
22955 fprintf_unfiltered (f, "constant: %s",
1bc397c5 22956 plongest (die->attrs[i].as_signed ()));
663c44ac 22957 break;
c906108c 22958 default:
d97bc12b 22959 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22960 die->attrs[i].form);
d97bc12b 22961 break;
c906108c 22962 }
d97bc12b 22963 fprintf_unfiltered (f, "\n");
c906108c
SS
22964 }
22965}
22966
f9aca02d 22967static void
d97bc12b 22968dump_die_for_error (struct die_info *die)
c906108c 22969{
d97bc12b
DE
22970 dump_die_shallow (gdb_stderr, 0, die);
22971}
22972
22973static void
22974dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22975{
22976 int indent = level * 4;
22977
22978 gdb_assert (die != NULL);
22979
22980 if (level >= max_level)
22981 return;
22982
22983 dump_die_shallow (f, indent, die);
22984
22985 if (die->child != NULL)
c906108c 22986 {
d97bc12b
DE
22987 print_spaces (indent, f);
22988 fprintf_unfiltered (f, " Children:");
22989 if (level + 1 < max_level)
22990 {
22991 fprintf_unfiltered (f, "\n");
22992 dump_die_1 (f, level + 1, max_level, die->child);
22993 }
22994 else
22995 {
3e43a32a
MS
22996 fprintf_unfiltered (f,
22997 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22998 }
22999 }
23000
23001 if (die->sibling != NULL && level > 0)
23002 {
23003 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23004 }
23005}
23006
d97bc12b
DE
23007/* This is called from the pdie macro in gdbinit.in.
23008 It's not static so gcc will keep a copy callable from gdb. */
23009
23010void
23011dump_die (struct die_info *die, int max_level)
23012{
23013 dump_die_1 (gdb_stdlog, 0, max_level, die);
23014}
23015
f9aca02d 23016static void
51545339 23017store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23018{
51545339 23019 void **slot;
c906108c 23020
9c541725
PA
23021 slot = htab_find_slot_with_hash (cu->die_hash, die,
23022 to_underlying (die->sect_off),
b64f50a1 23023 INSERT);
51545339
DJ
23024
23025 *slot = die;
c906108c
SS
23026}
23027
348e048f
DE
23028/* Follow reference or signature attribute ATTR of SRC_DIE.
23029 On entry *REF_CU is the CU of SRC_DIE.
23030 On exit *REF_CU is the CU of the result. */
23031
23032static struct die_info *
ff39bb5e 23033follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23034 struct dwarf2_cu **ref_cu)
23035{
23036 struct die_info *die;
23037
cd6c91b4 23038 if (attr->form_is_ref ())
348e048f 23039 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23040 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23041 die = follow_die_sig (src_die, attr, ref_cu);
23042 else
23043 {
23044 dump_die_for_error (src_die);
23045 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23046 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23047 }
23048
23049 return die;
03dd20cc
DJ
23050}
23051
5c631832 23052/* Follow reference OFFSET.
673bfd45
DE
23053 On entry *REF_CU is the CU of the source die referencing OFFSET.
23054 On exit *REF_CU is the CU of the result.
23055 Returns NULL if OFFSET is invalid. */
f504f079 23056
f9aca02d 23057static struct die_info *
9c541725 23058follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23059 struct dwarf2_cu **ref_cu)
c906108c 23060{
10b3939b 23061 struct die_info temp_die;
f2f0e013 23062 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23063 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23064
348e048f
DE
23065 gdb_assert (cu->per_cu != NULL);
23066
98bfdba5
PA
23067 target_cu = cu;
23068
17e593e9
SM
23069 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23070 "source CU contains target offset: %d",
23071 sect_offset_str (cu->per_cu->sect_off),
23072 sect_offset_str (sect_off),
23073 cu->header.offset_in_cu_p (sect_off));
23074
3019eac3 23075 if (cu->per_cu->is_debug_types)
348e048f
DE
23076 {
23077 /* .debug_types CUs cannot reference anything outside their CU.
23078 If they need to, they have to reference a signatured type via
55f1336d 23079 DW_FORM_ref_sig8. */
4057dfde 23080 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23081 return NULL;
348e048f 23082 }
36586728 23083 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23084 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23085 {
23086 struct dwarf2_per_cu_data *per_cu;
9a619af0 23087
9c541725 23088 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23089 per_objfile);
03dd20cc 23090
17e593e9
SM
23091 dwarf_read_debug_printf_v ("target CU offset: %s, "
23092 "target CU DIEs loaded: %d",
23093 sect_offset_str (per_cu->sect_off),
23094 per_objfile->get_cu (per_cu) != nullptr);
23095
616c069a
SM
23096 /* If necessary, add it to the queue and load its DIEs.
23097
23098 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23099 it doesn't mean they are currently loaded. Since we require them
23100 to be loaded, we must check for ourselves. */
23101 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23102 || per_objfile->get_cu (per_cu) == nullptr)
4a636814
SM
23103 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23104 false, cu->language);
03dd20cc 23105
976ca316 23106 target_cu = per_objfile->get_cu (per_cu);
616c069a 23107 gdb_assert (target_cu != nullptr);
10b3939b 23108 }
98bfdba5
PA
23109 else if (cu->dies == NULL)
23110 {
23111 /* We're loading full DIEs during partial symbol reading. */
976ca316 23112 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23113 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23114 language_minimal);
98bfdba5 23115 }
c906108c 23116
f2f0e013 23117 *ref_cu = target_cu;
9c541725 23118 temp_die.sect_off = sect_off;
c24bdb02 23119
3db19b2d
TV
23120 if (target_cu != cu)
23121 target_cu->ancestor = cu;
23122
9a3c8263 23123 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23124 &temp_die,
23125 to_underlying (sect_off));
5c631832 23126}
10b3939b 23127
5c631832
JK
23128/* Follow reference attribute ATTR of SRC_DIE.
23129 On entry *REF_CU is the CU of SRC_DIE.
23130 On exit *REF_CU is the CU of the result. */
23131
23132static struct die_info *
ff39bb5e 23133follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23134 struct dwarf2_cu **ref_cu)
23135{
0826b30a 23136 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23137 struct dwarf2_cu *cu = *ref_cu;
23138 struct die_info *die;
23139
9c541725 23140 die = follow_die_offset (sect_off,
36586728
TT
23141 (attr->form == DW_FORM_GNU_ref_alt
23142 || cu->per_cu->is_dwz),
23143 ref_cu);
5c631832 23144 if (!die)
9d8780f0
SM
23145 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23146 "at %s [in module %s]"),
23147 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23148 objfile_name (cu->per_objfile->objfile));
348e048f 23149
5c631832
JK
23150 return die;
23151}
23152
d4c9a4f8 23153/* See read.h. */
5c631832
JK
23154
23155struct dwarf2_locexpr_baton
9c541725 23156dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23157 dwarf2_per_cu_data *per_cu,
976ca316 23158 dwarf2_per_objfile *per_objfile,
041d9819
SM
23159 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23160 bool resolve_abstract_p)
5c631832 23161{
5c631832
JK
23162 struct die_info *die;
23163 struct attribute *attr;
23164 struct dwarf2_locexpr_baton retval;
976ca316 23165 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23166
976ca316 23167 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23168 if (cu == nullptr)
976ca316 23169 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23170
23171 if (cu == nullptr)
cc12ce38
DE
23172 {
23173 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23174 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23175 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23176 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23177 }
918dd910 23178
9c541725 23179 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23180 if (!die)
9d8780f0
SM
23181 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23182 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23183
23184 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23185 if (!attr && resolve_abstract_p
976ca316
SM
23186 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23187 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23188 {
041d9819 23189 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23190 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23191 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23192
3360b6e7 23193 for (const auto &cand_off
976ca316 23194 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23195 {
3360b6e7
TV
23196 struct dwarf2_cu *cand_cu = cu;
23197 struct die_info *cand
23198 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23199 if (!cand
23200 || !cand->parent
e4a62c65
TV
23201 || cand->parent->tag != DW_TAG_subprogram)
23202 continue;
23203
23204 CORE_ADDR pc_low, pc_high;
23205 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23206 if (pc_low == ((CORE_ADDR) -1))
23207 continue;
23208 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23209 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23210 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23211 continue;
23212
23213 die = cand;
23214 attr = dwarf2_attr (die, DW_AT_location, cu);
23215 break;
23216 }
23217 }
23218
5c631832
JK
23219 if (!attr)
23220 {
e103e986
JK
23221 /* DWARF: "If there is no such attribute, then there is no effect.".
23222 DATA is ignored if SIZE is 0. */
5c631832 23223
e103e986 23224 retval.data = NULL;
5c631832
JK
23225 retval.size = 0;
23226 }
cd6c91b4 23227 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23228 {
23229 struct dwarf2_loclist_baton loclist_baton;
041d9819 23230 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23231 size_t size;
23232
23233 fill_in_loclist_baton (cu, &loclist_baton, attr);
23234
23235 retval.data = dwarf2_find_location_expression (&loclist_baton,
23236 &size, pc);
23237 retval.size = size;
23238 }
5c631832
JK
23239 else
23240 {
4fc6c0d5 23241 if (!attr->form_is_block ())
9d8780f0 23242 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23243 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23244 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23245
9d2246fc
TT
23246 struct dwarf_block *block = attr->as_block ();
23247 retval.data = block->data;
23248 retval.size = block->size;
5c631832 23249 }
976ca316 23250 retval.per_objfile = per_objfile;
5c631832 23251 retval.per_cu = cu->per_cu;
918dd910 23252
976ca316 23253 per_objfile->age_comp_units ();
918dd910 23254
5c631832 23255 return retval;
348e048f
DE
23256}
23257
d4c9a4f8 23258/* See read.h. */
8b9737bf
TT
23259
23260struct dwarf2_locexpr_baton
23261dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23262 dwarf2_per_cu_data *per_cu,
14095eb3 23263 dwarf2_per_objfile *per_objfile,
041d9819 23264 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23265{
9c541725 23266 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23267
14095eb3 23268 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23269 get_frame_pc);
8b9737bf
TT
23270}
23271
b6807d98
TT
23272/* Write a constant of a given type as target-ordered bytes into
23273 OBSTACK. */
23274
23275static const gdb_byte *
23276write_constant_as_bytes (struct obstack *obstack,
23277 enum bfd_endian byte_order,
23278 struct type *type,
23279 ULONGEST value,
23280 LONGEST *len)
23281{
23282 gdb_byte *result;
23283
23284 *len = TYPE_LENGTH (type);
224c3ddb 23285 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23286 store_unsigned_integer (result, *len, byte_order, value);
23287
23288 return result;
23289}
23290
d4c9a4f8 23291/* See read.h. */
b6807d98
TT
23292
23293const gdb_byte *
9c541725 23294dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23295 dwarf2_per_cu_data *per_cu,
14095eb3 23296 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23297 obstack *obstack,
b6807d98
TT
23298 LONGEST *len)
23299{
b6807d98
TT
23300 struct die_info *die;
23301 struct attribute *attr;
23302 const gdb_byte *result = NULL;
23303 struct type *type;
23304 LONGEST value;
23305 enum bfd_endian byte_order;
14095eb3 23306 struct objfile *objfile = per_objfile->objfile;
b6807d98 23307
7188ed02 23308 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23309 if (cu == nullptr)
23310 cu = load_cu (per_cu, per_objfile, false);
23311
23312 if (cu == nullptr)
cc12ce38
DE
23313 {
23314 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23315 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23316 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23317 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23318 }
b6807d98 23319
9c541725 23320 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23321 if (!die)
9d8780f0
SM
23322 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23323 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23324
23325 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23326 if (attr == NULL)
23327 return NULL;
23328
e3b94546 23329 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23330 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23331
23332 switch (attr->form)
23333 {
23334 case DW_FORM_addr:
336d760d 23335 case DW_FORM_addrx:
b6807d98
TT
23336 case DW_FORM_GNU_addr_index:
23337 {
23338 gdb_byte *tem;
23339
23340 *len = cu->header.addr_size;
224c3ddb 23341 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23342 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23343 result = tem;
23344 }
23345 break;
23346 case DW_FORM_string:
23347 case DW_FORM_strp:
cf532bd1 23348 case DW_FORM_strx:
b6807d98
TT
23349 case DW_FORM_GNU_str_index:
23350 case DW_FORM_GNU_strp_alt:
c6481205 23351 /* The string is already allocated on the objfile obstack, point
b6807d98 23352 directly to it. */
2c830f54
TT
23353 {
23354 const char *attr_name = attr->as_string ();
23355 result = (const gdb_byte *) attr_name;
23356 *len = strlen (attr_name);
23357 }
b6807d98
TT
23358 break;
23359 case DW_FORM_block1:
23360 case DW_FORM_block2:
23361 case DW_FORM_block4:
23362 case DW_FORM_block:
23363 case DW_FORM_exprloc:
0224619f 23364 case DW_FORM_data16:
9d2246fc
TT
23365 {
23366 struct dwarf_block *block = attr->as_block ();
23367 result = block->data;
23368 *len = block->size;
23369 }
b6807d98
TT
23370 break;
23371
23372 /* The DW_AT_const_value attributes are supposed to carry the
23373 symbol's value "represented as it would be on the target
23374 architecture." By the time we get here, it's already been
23375 converted to host endianness, so we just need to sign- or
23376 zero-extend it as appropriate. */
23377 case DW_FORM_data1:
23378 type = die_type (die, cu);
23379 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23380 if (result == NULL)
23381 result = write_constant_as_bytes (obstack, byte_order,
23382 type, value, len);
23383 break;
23384 case DW_FORM_data2:
23385 type = die_type (die, cu);
23386 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23387 if (result == NULL)
23388 result = write_constant_as_bytes (obstack, byte_order,
23389 type, value, len);
23390 break;
23391 case DW_FORM_data4:
23392 type = die_type (die, cu);
23393 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23394 if (result == NULL)
23395 result = write_constant_as_bytes (obstack, byte_order,
23396 type, value, len);
23397 break;
23398 case DW_FORM_data8:
23399 type = die_type (die, cu);
23400 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23401 if (result == NULL)
23402 result = write_constant_as_bytes (obstack, byte_order,
23403 type, value, len);
23404 break;
23405
23406 case DW_FORM_sdata:
663c44ac 23407 case DW_FORM_implicit_const:
b6807d98
TT
23408 type = die_type (die, cu);
23409 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23410 type, attr->as_signed (), len);
b6807d98
TT
23411 break;
23412
23413 case DW_FORM_udata:
23414 type = die_type (die, cu);
23415 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23416 type, attr->as_unsigned (), len);
b6807d98
TT
23417 break;
23418
23419 default:
b98664d3 23420 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23421 dwarf_form_name (attr->form));
23422 break;
23423 }
23424
23425 return result;
23426}
23427
d4c9a4f8 23428/* See read.h. */
7942e96e
AA
23429
23430struct type *
9c541725 23431dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
23432 dwarf2_per_cu_data *per_cu,
23433 dwarf2_per_objfile *per_objfile)
7942e96e 23434{
7942e96e
AA
23435 struct die_info *die;
23436
7188ed02 23437 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23438 if (cu == nullptr)
23439 cu = load_cu (per_cu, per_objfile, false);
23440
23441 if (cu == nullptr)
23442 return nullptr;
7942e96e 23443
9c541725 23444 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23445 if (!die)
23446 return NULL;
23447
23448 return die_type (die, cu);
23449}
23450
8cb5117c 23451/* See read.h. */
8a9b8146
TT
23452
23453struct type *
b64f50a1 23454dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23455 dwarf2_per_cu_data *per_cu,
23456 dwarf2_per_objfile *per_objfile)
8a9b8146 23457{
9c541725 23458 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23459 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23460}
23461
ac9ec31b 23462/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23463 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23464 On exit *REF_CU is the CU of the result.
23465 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23466
23467static struct die_info *
ac9ec31b
DE
23468follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23469 struct dwarf2_cu **ref_cu)
348e048f 23470{
348e048f 23471 struct die_info temp_die;
3db19b2d 23472 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 23473 struct die_info *die;
976ca316 23474 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23475
348e048f 23476
ac9ec31b
DE
23477 /* While it might be nice to assert sig_type->type == NULL here,
23478 we can get here for DW_AT_imported_declaration where we need
23479 the DIE not the type. */
348e048f 23480
616c069a 23481 /* If necessary, add it to the queue and load its DIEs.
348e048f 23482
616c069a
SM
23483 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23484 it doesn't mean they are currently loaded. Since we require them
23485 to be loaded, we must check for ourselves. */
0d305d5c 23486 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
616c069a 23487 language_minimal)
0d305d5c 23488 || per_objfile->get_cu (sig_type) == nullptr)
976ca316 23489 read_signatured_type (sig_type, per_objfile);
348e048f 23490
0d305d5c 23491 sig_cu = per_objfile->get_cu (sig_type);
69d751e3 23492 gdb_assert (sig_cu != NULL);
9c541725
PA
23493 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23494 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23495 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23496 to_underlying (temp_die.sect_off));
348e048f
DE
23497 if (die)
23498 {
796a7ff8
DE
23499 /* For .gdb_index version 7 keep track of included TUs.
23500 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
23501 if (per_objfile->per_bfd->index_table != NULL
23502 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 23503 {
ae640021 23504 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23505 }
23506
348e048f 23507 *ref_cu = sig_cu;
3db19b2d
TV
23508 if (sig_cu != cu)
23509 sig_cu->ancestor = cu;
c24bdb02 23510
348e048f
DE
23511 return die;
23512 }
23513
ac9ec31b
DE
23514 return NULL;
23515}
23516
23517/* Follow signatured type referenced by ATTR in SRC_DIE.
23518 On entry *REF_CU is the CU of SRC_DIE.
23519 On exit *REF_CU is the CU of the result.
23520 The result is the DIE of the type.
23521 If the referenced type cannot be found an error is thrown. */
23522
23523static struct die_info *
ff39bb5e 23524follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23525 struct dwarf2_cu **ref_cu)
23526{
630ed6b9 23527 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
23528 struct signatured_type *sig_type;
23529 struct die_info *die;
23530
23531 gdb_assert (attr->form == DW_FORM_ref_sig8);
23532
a2ce51a0 23533 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23534 /* sig_type will be NULL if the signatured type is missing from
23535 the debug info. */
23536 if (sig_type == NULL)
23537 {
23538 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
23539 " from DIE at %s [in module %s]"),
23540 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23541 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23542 }
23543
23544 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23545 if (die == NULL)
23546 {
23547 dump_die_for_error (src_die);
23548 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23549 " from DIE at %s [in module %s]"),
23550 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23551 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23552 }
23553
23554 return die;
23555}
23556
23557/* Get the type specified by SIGNATURE referenced in DIE/CU,
23558 reading in and processing the type unit if necessary. */
23559
23560static struct type *
23561get_signatured_type (struct die_info *die, ULONGEST signature,
23562 struct dwarf2_cu *cu)
23563{
976ca316 23564 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
23565 struct signatured_type *sig_type;
23566 struct dwarf2_cu *type_cu;
23567 struct die_info *type_die;
23568 struct type *type;
23569
a2ce51a0 23570 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23571 /* sig_type will be NULL if the signatured type is missing from
23572 the debug info. */
23573 if (sig_type == NULL)
23574 {
b98664d3 23575 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23576 " from DIE at %s [in module %s]"),
23577 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23578 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23579 return build_error_marker_type (cu, die);
23580 }
23581
23582 /* If we already know the type we're done. */
976ca316 23583 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
23584 if (type != nullptr)
23585 return type;
ac9ec31b
DE
23586
23587 type_cu = cu;
23588 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23589 if (type_die != NULL)
23590 {
23591 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23592 is created. This is important, for example, because for c++ classes
23593 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23594 type = read_type_die (type_die, type_cu);
23595 if (type == NULL)
23596 {
b98664d3 23597 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23598 " referenced from DIE at %s [in module %s]"),
23599 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23600 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23601 type = build_error_marker_type (cu, die);
23602 }
23603 }
23604 else
23605 {
b98664d3 23606 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23607 " from DIE at %s [in module %s]"),
23608 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23609 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23610 type = build_error_marker_type (cu, die);
23611 }
e286671b 23612
976ca316 23613 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
23614
23615 return type;
23616}
23617
23618/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23619 reading in and processing the type unit if necessary. */
23620
23621static struct type *
ff39bb5e 23622get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23623 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23624{
23625 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 23626 if (attr->form_is_ref ())
ac9ec31b
DE
23627 {
23628 struct dwarf2_cu *type_cu = cu;
23629 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23630
23631 return read_type_die (type_die, type_cu);
23632 }
23633 else if (attr->form == DW_FORM_ref_sig8)
23634 {
630ed6b9 23635 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
23636 }
23637 else
23638 {
976ca316 23639 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23640
b98664d3 23641 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23642 " at %s [in module %s]"),
23643 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 23644 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23645 return build_error_marker_type (cu, die);
23646 }
348e048f
DE
23647}
23648
e5fe5e75 23649/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23650
23651static void
ab432490
SM
23652load_full_type_unit (dwarf2_per_cu_data *per_cu,
23653 dwarf2_per_objfile *per_objfile)
348e048f 23654{
52dc124a 23655 struct signatured_type *sig_type;
348e048f 23656
f4dc4d17 23657 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 23658 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 23659
6721b2ec
DE
23660 /* We have the per_cu, but we need the signatured_type.
23661 Fortunately this is an easy translation. */
23662 gdb_assert (per_cu->is_debug_types);
23663 sig_type = (struct signatured_type *) per_cu;
348e048f 23664
7188ed02 23665 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 23666
ab432490 23667 read_signatured_type (sig_type, per_objfile);
348e048f 23668
7188ed02 23669 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
23670}
23671
3019eac3
DE
23672/* Read in a signatured type and build its CU and DIEs.
23673 If the type is a stub for the real type in a DWO file,
23674 read in the real type from the DWO file as well. */
dee91e82
DE
23675
23676static void
ab432490
SM
23677read_signatured_type (signatured_type *sig_type,
23678 dwarf2_per_objfile *per_objfile)
dee91e82 23679{
0d305d5c
TT
23680 gdb_assert (sig_type->is_debug_types);
23681 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
348e048f 23682
0d305d5c 23683 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
23684
23685 if (!reader.dummy_p)
23686 {
23687 struct dwarf2_cu *cu = reader.cu;
23688 const gdb_byte *info_ptr = reader.info_ptr;
23689
23690 gdb_assert (cu->die_hash == NULL);
23691 cu->die_hash =
23692 htab_create_alloc_ex (cu->header.length / 12,
23693 die_hash,
23694 die_eq,
23695 NULL,
23696 &cu->comp_unit_obstack,
23697 hashtab_obstack_allocate,
23698 dummy_obstack_deallocate);
23699
3e225074 23700 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
23701 reader.comp_unit_die->child
23702 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23703 reader.comp_unit_die);
23704 cu->dies = reader.comp_unit_die;
23705 /* comp_unit_die is not stored in die_hash, no need. */
23706
23707 /* We try not to read any attributes in this function, because
23708 not all CUs needed for references have been loaded yet, and
23709 symbol table processing isn't initialized. But we have to
23710 set the CU language, or we won't be able to build types
23711 correctly. Similarly, if we do not read the producer, we can
23712 not apply producer-specific interpretation. */
23713 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
23714
23715 reader.keep ();
c0ab21c2
TT
23716 }
23717
0d305d5c 23718 sig_type->tu_read = 1;
c906108c
SS
23719}
23720
c906108c
SS
23721/* Decode simple location descriptions.
23722 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
23723 the location and return the value. If COMPUTED is non-null, it is
23724 set to true to indicate that decoding was successful, and false
23725 otherwise. If COMPUTED is null, then this function may emit a
23726 complaint. */
c906108c
SS
23727
23728static CORE_ADDR
7d79de9a 23729decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 23730{
5e22e966 23731 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
23732 size_t i;
23733 size_t size = blk->size;
d521ce57 23734 const gdb_byte *data = blk->data;
21ae7a4d
JK
23735 CORE_ADDR stack[64];
23736 int stacki;
23737 unsigned int bytes_read, unsnd;
23738 gdb_byte op;
c906108c 23739
7d79de9a
TT
23740 if (computed != nullptr)
23741 *computed = false;
23742
21ae7a4d
JK
23743 i = 0;
23744 stacki = 0;
23745 stack[stacki] = 0;
23746 stack[++stacki] = 0;
23747
23748 while (i < size)
23749 {
23750 op = data[i++];
23751 switch (op)
23752 {
23753 case DW_OP_lit0:
23754 case DW_OP_lit1:
23755 case DW_OP_lit2:
23756 case DW_OP_lit3:
23757 case DW_OP_lit4:
23758 case DW_OP_lit5:
23759 case DW_OP_lit6:
23760 case DW_OP_lit7:
23761 case DW_OP_lit8:
23762 case DW_OP_lit9:
23763 case DW_OP_lit10:
23764 case DW_OP_lit11:
23765 case DW_OP_lit12:
23766 case DW_OP_lit13:
23767 case DW_OP_lit14:
23768 case DW_OP_lit15:
23769 case DW_OP_lit16:
23770 case DW_OP_lit17:
23771 case DW_OP_lit18:
23772 case DW_OP_lit19:
23773 case DW_OP_lit20:
23774 case DW_OP_lit21:
23775 case DW_OP_lit22:
23776 case DW_OP_lit23:
23777 case DW_OP_lit24:
23778 case DW_OP_lit25:
23779 case DW_OP_lit26:
23780 case DW_OP_lit27:
23781 case DW_OP_lit28:
23782 case DW_OP_lit29:
23783 case DW_OP_lit30:
23784 case DW_OP_lit31:
23785 stack[++stacki] = op - DW_OP_lit0;
23786 break;
f1bea926 23787
21ae7a4d
JK
23788 case DW_OP_reg0:
23789 case DW_OP_reg1:
23790 case DW_OP_reg2:
23791 case DW_OP_reg3:
23792 case DW_OP_reg4:
23793 case DW_OP_reg5:
23794 case DW_OP_reg6:
23795 case DW_OP_reg7:
23796 case DW_OP_reg8:
23797 case DW_OP_reg9:
23798 case DW_OP_reg10:
23799 case DW_OP_reg11:
23800 case DW_OP_reg12:
23801 case DW_OP_reg13:
23802 case DW_OP_reg14:
23803 case DW_OP_reg15:
23804 case DW_OP_reg16:
23805 case DW_OP_reg17:
23806 case DW_OP_reg18:
23807 case DW_OP_reg19:
23808 case DW_OP_reg20:
23809 case DW_OP_reg21:
23810 case DW_OP_reg22:
23811 case DW_OP_reg23:
23812 case DW_OP_reg24:
23813 case DW_OP_reg25:
23814 case DW_OP_reg26:
23815 case DW_OP_reg27:
23816 case DW_OP_reg28:
23817 case DW_OP_reg29:
23818 case DW_OP_reg30:
23819 case DW_OP_reg31:
23820 stack[++stacki] = op - DW_OP_reg0;
23821 if (i < size)
7d79de9a
TT
23822 {
23823 if (computed == nullptr)
23824 dwarf2_complex_location_expr_complaint ();
23825 else
23826 return 0;
23827 }
21ae7a4d 23828 break;
c906108c 23829
21ae7a4d
JK
23830 case DW_OP_regx:
23831 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23832 i += bytes_read;
23833 stack[++stacki] = unsnd;
23834 if (i < size)
7d79de9a
TT
23835 {
23836 if (computed == nullptr)
23837 dwarf2_complex_location_expr_complaint ();
23838 else
23839 return 0;
23840 }
21ae7a4d 23841 break;
c906108c 23842
21ae7a4d 23843 case DW_OP_addr:
c8a7a66f
TT
23844 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23845 &bytes_read);
21ae7a4d
JK
23846 i += bytes_read;
23847 break;
d53d4ac5 23848
21ae7a4d
JK
23849 case DW_OP_const1u:
23850 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23851 i += 1;
23852 break;
23853
23854 case DW_OP_const1s:
23855 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23856 i += 1;
23857 break;
23858
23859 case DW_OP_const2u:
23860 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23861 i += 2;
23862 break;
23863
23864 case DW_OP_const2s:
23865 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23866 i += 2;
23867 break;
d53d4ac5 23868
21ae7a4d
JK
23869 case DW_OP_const4u:
23870 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23871 i += 4;
23872 break;
23873
23874 case DW_OP_const4s:
23875 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23876 i += 4;
23877 break;
23878
585861ea
JK
23879 case DW_OP_const8u:
23880 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23881 i += 8;
23882 break;
23883
21ae7a4d
JK
23884 case DW_OP_constu:
23885 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23886 &bytes_read);
23887 i += bytes_read;
23888 break;
23889
23890 case DW_OP_consts:
23891 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23892 i += bytes_read;
23893 break;
23894
23895 case DW_OP_dup:
23896 stack[stacki + 1] = stack[stacki];
23897 stacki++;
23898 break;
23899
23900 case DW_OP_plus:
23901 stack[stacki - 1] += stack[stacki];
23902 stacki--;
23903 break;
23904
23905 case DW_OP_plus_uconst:
23906 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23907 &bytes_read);
23908 i += bytes_read;
23909 break;
23910
23911 case DW_OP_minus:
23912 stack[stacki - 1] -= stack[stacki];
23913 stacki--;
23914 break;
23915
23916 case DW_OP_deref:
23917 /* If we're not the last op, then we definitely can't encode
23918 this using GDB's address_class enum. This is valid for partial
23919 global symbols, although the variable's address will be bogus
23920 in the psymtab. */
23921 if (i < size)
7d79de9a
TT
23922 {
23923 if (computed == nullptr)
23924 dwarf2_complex_location_expr_complaint ();
23925 else
23926 return 0;
23927 }
21ae7a4d
JK
23928 break;
23929
dda83cd7 23930 case DW_OP_GNU_push_tls_address:
4aa4e28b 23931 case DW_OP_form_tls_address:
21ae7a4d
JK
23932 /* The top of the stack has the offset from the beginning
23933 of the thread control block at which the variable is located. */
23934 /* Nothing should follow this operator, so the top of stack would
23935 be returned. */
23936 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23937 address will be bogus in the psymtab. Make it always at least
23938 non-zero to not look as a variable garbage collected by linker
23939 which have DW_OP_addr 0. */
21ae7a4d 23940 if (i < size)
7d79de9a
TT
23941 {
23942 if (computed == nullptr)
23943 dwarf2_complex_location_expr_complaint ();
23944 else
23945 return 0;
23946 }
585861ea 23947 stack[stacki]++;
dda83cd7 23948 break;
21ae7a4d
JK
23949
23950 case DW_OP_GNU_uninit:
7d79de9a
TT
23951 if (computed != nullptr)
23952 return 0;
21ae7a4d
JK
23953 break;
23954
336d760d 23955 case DW_OP_addrx:
3019eac3 23956 case DW_OP_GNU_addr_index:
49f6c839 23957 case DW_OP_GNU_const_index:
3019eac3
DE
23958 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23959 &bytes_read);
23960 i += bytes_read;
23961 break;
23962
21ae7a4d 23963 default:
7d79de9a
TT
23964 if (computed == nullptr)
23965 {
23966 const char *name = get_DW_OP_name (op);
21ae7a4d 23967
7d79de9a
TT
23968 if (name)
23969 complaint (_("unsupported stack op: '%s'"),
23970 name);
23971 else
23972 complaint (_("unsupported stack op: '%02x'"),
23973 op);
23974 }
21ae7a4d
JK
23975
23976 return (stack[stacki]);
d53d4ac5 23977 }
3c6e0cb3 23978
21ae7a4d 23979 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 23980 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
23981 if (stacki >= ARRAY_SIZE (stack) - 1)
23982 {
7d79de9a
TT
23983 if (computed == nullptr)
23984 complaint (_("location description stack overflow"));
21ae7a4d
JK
23985 return 0;
23986 }
23987
23988 if (stacki <= 0)
23989 {
7d79de9a
TT
23990 if (computed == nullptr)
23991 complaint (_("location description stack underflow"));
21ae7a4d
JK
23992 return 0;
23993 }
23994 }
7d79de9a
TT
23995
23996 if (computed != nullptr)
23997 *computed = true;
21ae7a4d 23998 return (stack[stacki]);
c906108c
SS
23999}
24000
24001/* memory allocation interface */
24002
c906108c 24003static struct dwarf_block *
7b5a2f43 24004dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24005{
8d749320 24006 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24007}
24008
c906108c 24009static struct die_info *
b60c80d6 24010dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24011{
24012 struct die_info *die;
b60c80d6
DJ
24013 size_t size = sizeof (struct die_info);
24014
24015 if (num_attrs > 1)
24016 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24017
b60c80d6 24018 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24019 memset (die, 0, sizeof (struct die_info));
24020 return (die);
24021}
2e276125
JB
24022
24023\f
a036ba48 24024
c90ec28a 24025/* Macro support. */
cf2c3c16 24026
9eac9650
TT
24027/* An overload of dwarf_decode_macros that finds the correct section
24028 and ensures it is read in before calling the other overload. */
24029
24030static void
24031dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24032 int section_is_gnu)
24033{
976ca316
SM
24034 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24035 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24036 const struct line_header *lh = cu->line_header;
9eac9650
TT
24037 unsigned int offset_size = cu->header.offset_size;
24038 struct dwarf2_section_info *section;
24039 const char *section_name;
24040
24041 if (cu->dwo_unit != nullptr)
24042 {
24043 if (section_is_gnu)
24044 {
24045 section = &cu->dwo_unit->dwo_file->sections.macro;
24046 section_name = ".debug_macro.dwo";
24047 }
24048 else
24049 {
24050 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24051 section_name = ".debug_macinfo.dwo";
24052 }
24053 }
24054 else
24055 {
24056 if (section_is_gnu)
24057 {
976ca316 24058 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24059 section_name = ".debug_macro";
24060 }
24061 else
24062 {
976ca316 24063 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24064 section_name = ".debug_macinfo";
24065 }
24066 }
24067
24068 section->read (objfile);
24069 if (section->buffer == nullptr)
24070 {
24071 complaint (_("missing %s section"), section_name);
24072 return;
24073 }
24074
24075 buildsym_compunit *builder = cu->get_builder ();
24076
048fde1e 24077 struct dwarf2_section_info *str_offsets_section;
24078 struct dwarf2_section_info *str_section;
24079 ULONGEST str_offsets_base;
24080
24081 if (cu->dwo_unit != nullptr)
24082 {
24083 str_offsets_section = &cu->dwo_unit->dwo_file
24084 ->sections.str_offsets;
24085 str_section = &cu->dwo_unit->dwo_file->sections.str;
24086 str_offsets_base = cu->header.addr_size;
24087 }
24088 else
24089 {
24090 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24091 str_section = &per_objfile->per_bfd->str;
24092 str_offsets_base = *cu->str_offsets_base;
24093 }
24094
976ca316 24095 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24096 offset_size, offset, str_section, str_offsets_section,
24097 str_offsets_base, section_is_gnu);
9eac9650
TT
24098}
24099
3019eac3
DE
24100/* Return the .debug_loc section to use for CU.
24101 For DWO files use .debug_loc.dwo. */
24102
24103static struct dwarf2_section_info *
24104cu_debug_loc_section (struct dwarf2_cu *cu)
24105{
976ca316 24106 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24107
3019eac3 24108 if (cu->dwo_unit)
43988095
JK
24109 {
24110 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24111
43988095
JK
24112 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24113 }
976ca316
SM
24114 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24115 : &per_objfile->per_bfd->loc);
3019eac3
DE
24116}
24117
d0ce17d8
CT
24118/* Return the .debug_rnglists section to use for CU. */
24119static struct dwarf2_section_info *
24120cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24121{
24122 if (cu->header.version < 5)
24123 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24124 cu->header.version);
24125 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24126
24127 /* Make sure we read the .debug_rnglists section from the file that
24128 contains the DW_AT_ranges attribute we are reading. Normally that
24129 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24130 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24131 program. */
24132 if (cu->dwo_unit != nullptr
24133 && tag != DW_TAG_compile_unit
24134 && tag != DW_TAG_skeleton_unit)
24135 {
24136 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24137
24138 if (sections->rnglists.size > 0)
24139 return &sections->rnglists;
24140 else
24141 error (_(".debug_rnglists section is missing from .dwo file."));
24142 }
24143 return &dwarf2_per_objfile->per_bfd->rnglists;
24144}
24145
8cf6f0b1
TT
24146/* A helper function that fills in a dwarf2_loclist_baton. */
24147
24148static void
24149fill_in_loclist_baton (struct dwarf2_cu *cu,
24150 struct dwarf2_loclist_baton *baton,
ff39bb5e 24151 const struct attribute *attr)
8cf6f0b1 24152{
976ca316 24153 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24154 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24155
976ca316 24156 section->read (per_objfile->objfile);
8cf6f0b1 24157
976ca316 24158 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24159 baton->per_cu = cu->per_cu;
24160 gdb_assert (baton->per_cu);
24161 /* We don't know how long the location list is, but make sure we
24162 don't run off the edge of the section. */
d4df075e
TT
24163 baton->size = section->size - attr->as_unsigned ();
24164 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24165 if (cu->base_address.has_value ())
24166 baton->base_address = *cu->base_address;
24167 else
24168 baton->base_address = 0;
f664829e 24169 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24170}
24171
4c2df51b 24172static void
ff39bb5e 24173dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24174 struct dwarf2_cu *cu, int is_block)
4c2df51b 24175{
976ca316
SM
24176 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24177 struct objfile *objfile = per_objfile->objfile;
3019eac3 24178 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24179
cd6c91b4 24180 if (attr->form_is_section_offset ()
3019eac3 24181 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24182 the section. If so, fall through to the complaint in the
24183 other branch. */
d4df075e 24184 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24185 {
0d53c4c4 24186 struct dwarf2_loclist_baton *baton;
4c2df51b 24187
8d749320 24188 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24189
8cf6f0b1 24190 fill_in_loclist_baton (cu, baton, attr);
be391dca 24191
2b24b6e4 24192 if (!cu->base_address.has_value ())
b98664d3 24193 complaint (_("Location list used without "
3e43a32a 24194 "specifying the CU base address."));
4c2df51b 24195
f1e6e072
TT
24196 SYMBOL_ACLASS_INDEX (sym) = (is_block
24197 ? dwarf2_loclist_block_index
24198 : dwarf2_loclist_index);
0d53c4c4
DJ
24199 SYMBOL_LOCATION_BATON (sym) = baton;
24200 }
24201 else
24202 {
24203 struct dwarf2_locexpr_baton *baton;
24204
8d749320 24205 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24206 baton->per_objfile = per_objfile;
ae0d2f24
UW
24207 baton->per_cu = cu->per_cu;
24208 gdb_assert (baton->per_cu);
0d53c4c4 24209
4fc6c0d5 24210 if (attr->form_is_block ())
0d53c4c4
DJ
24211 {
24212 /* Note that we're just copying the block's data pointer
24213 here, not the actual data. We're still pointing into the
6502dd73
DJ
24214 info_buffer for SYM's objfile; right now we never release
24215 that buffer, but when we do clean up properly this may
24216 need to change. */
9d2246fc
TT
24217 struct dwarf_block *block = attr->as_block ();
24218 baton->size = block->size;
24219 baton->data = block->data;
0d53c4c4
DJ
24220 }
24221 else
24222 {
24223 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24224 sym->natural_name ());
0d53c4c4 24225 baton->size = 0;
0d53c4c4 24226 }
6e70227d 24227
f1e6e072
TT
24228 SYMBOL_ACLASS_INDEX (sym) = (is_block
24229 ? dwarf2_locexpr_block_index
24230 : dwarf2_locexpr_index);
0d53c4c4
DJ
24231 SYMBOL_LOCATION_BATON (sym) = baton;
24232 }
4c2df51b 24233}
6502dd73 24234
2e6a9f79 24235/* See read.h. */
96408a79 24236
2e6a9f79
SM
24237const comp_unit_head *
24238dwarf2_per_cu_data::get_header () const
96408a79 24239{
2e6a9f79
SM
24240 if (!m_header_read_in)
24241 {
24242 const gdb_byte *info_ptr
24243 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24244
2e6a9f79 24245 memset (&m_header, 0, sizeof (m_header));
96408a79 24246
2e6a9f79
SM
24247 read_comp_unit_head (&m_header, info_ptr, this->section,
24248 rcuh_kind::COMPILE);
a9f172c6
TT
24249
24250 m_header_read_in = true;
2e6a9f79 24251 }
96408a79 24252
2e6a9f79 24253 return &m_header;
96408a79
SA
24254}
24255
09ba997f 24256/* See read.h. */
ae0d2f24 24257
98714339 24258int
09ba997f 24259dwarf2_per_cu_data::addr_size () const
ae0d2f24 24260{
2e6a9f79 24261 return this->get_header ()->addr_size;
ae0d2f24
UW
24262}
24263
09ba997f 24264/* See read.h. */
9eae7c52
TT
24265
24266int
09ba997f 24267dwarf2_per_cu_data::offset_size () const
9eae7c52 24268{
2e6a9f79 24269 return this->get_header ()->offset_size;
96408a79
SA
24270}
24271
09ba997f 24272/* See read.h. */
96408a79
SA
24273
24274int
09ba997f 24275dwarf2_per_cu_data::ref_addr_size () const
96408a79 24276{
2e6a9f79 24277 const comp_unit_head *header = this->get_header ();
96408a79 24278
2e6a9f79
SM
24279 if (header->version == 2)
24280 return header->addr_size;
96408a79 24281 else
2e6a9f79 24282 return header->offset_size;
181cebd4
JK
24283}
24284
22b6cd70
TT
24285/* A helper function for dwarf2_find_containing_comp_unit that returns
24286 the index of the result, and that searches a vector. It will
24287 return a result even if the offset in question does not actually
24288 occur in any CU. This is separate so that it can be unit
24289 tested. */
ae038cb0 24290
22b6cd70
TT
24291static int
24292dwarf2_find_containing_comp_unit
24293 (sect_offset sect_off,
24294 unsigned int offset_in_dwz,
473ab964 24295 const std::vector<dwarf2_per_cu_data_up> &all_comp_units)
ae038cb0 24296{
ae038cb0
DJ
24297 int low, high;
24298
ae038cb0 24299 low = 0;
22b6cd70 24300 high = all_comp_units.size () - 1;
ae038cb0
DJ
24301 while (high > low)
24302 {
36586728 24303 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24304 int mid = low + (high - low) / 2;
9a619af0 24305
0d305d5c 24306 mid_cu = all_comp_units[mid].get ();
36586728 24307 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24308 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24309 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24310 high = mid;
24311 else
24312 low = mid + 1;
24313 }
24314 gdb_assert (low == high);
22b6cd70
TT
24315 return low;
24316}
24317
24318/* Locate the .debug_info compilation unit from CU's objfile which contains
24319 the DIE at OFFSET. Raises an error on failure. */
24320
24321static struct dwarf2_per_cu_data *
24322dwarf2_find_containing_comp_unit (sect_offset sect_off,
24323 unsigned int offset_in_dwz,
976ca316 24324 dwarf2_per_objfile *per_objfile)
22b6cd70 24325{
976ca316
SM
24326 int low = dwarf2_find_containing_comp_unit
24327 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
0d305d5c
TT
24328 dwarf2_per_cu_data *this_cu
24329 = per_objfile->per_bfd->all_comp_units[low].get ();
22b6cd70 24330
45b8ae0c 24331 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24332 {
36586728 24333 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24334 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24335 "offset %s [in module %s]"),
24336 sect_offset_str (sect_off),
976ca316 24337 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24338
976ca316 24339 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24340 <= sect_off);
0d305d5c 24341 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
ae038cb0
DJ
24342 }
24343 else
24344 {
976ca316 24345 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24346 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24347 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24348 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24349 return this_cu;
24350 }
24351}
24352
22b6cd70
TT
24353#if GDB_SELF_TEST
24354
24355namespace selftests {
24356namespace find_containing_comp_unit {
24357
24358static void
24359run_test ()
24360{
473ab964 24361 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
0d305d5c 24362 dwarf2_per_cu_data *one_ptr = one.get ();
473ab964 24363 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
0d305d5c 24364 dwarf2_per_cu_data *two_ptr = two.get ();
473ab964 24365 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
0d305d5c 24366 dwarf2_per_cu_data *three_ptr = three.get ();
473ab964 24367 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
0d305d5c
TT
24368 dwarf2_per_cu_data *four_ptr = four.get ();
24369
24370 one->length = 5;
24371 two->sect_off = sect_offset (one->length);
24372 two->length = 7;
24373
24374 three->length = 5;
24375 three->is_dwz = 1;
24376 four->sect_off = sect_offset (three->length);
24377 four->length = 7;
24378 four->is_dwz = 1;
24379
473ab964 24380 std::vector<dwarf2_per_cu_data_up> units;
0d305d5c
TT
24381 units.push_back (std::move (one));
24382 units.push_back (std::move (two));
24383 units.push_back (std::move (three));
24384 units.push_back (std::move (four));
22b6cd70
TT
24385
24386 int result;
24387
24388 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
0d305d5c 24389 SELF_CHECK (units[result].get () == one_ptr);
22b6cd70 24390 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
0d305d5c 24391 SELF_CHECK (units[result].get () == one_ptr);
22b6cd70 24392 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
0d305d5c 24393 SELF_CHECK (units[result].get () == two_ptr);
22b6cd70
TT
24394
24395 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
0d305d5c 24396 SELF_CHECK (units[result].get () == three_ptr);
22b6cd70 24397 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
0d305d5c 24398 SELF_CHECK (units[result].get () == three_ptr);
22b6cd70 24399 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
0d305d5c 24400 SELF_CHECK (units[result].get () == four_ptr);
22b6cd70
TT
24401}
24402
24403}
24404}
24405
24406#endif /* GDB_SELF_TEST */
24407
9816fde3
JK
24408/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24409
24410static void
95554aad
TT
24411prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24412 enum language pretend_language)
9816fde3
JK
24413{
24414 struct attribute *attr;
24415
24416 /* Set the language we're debugging. */
24417 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24418 if (attr != nullptr)
529908cb 24419 set_cu_language (attr->constant_value (0), cu);
9816fde3 24420 else
9cded63f 24421 {
95554aad 24422 cu->language = pretend_language;
9cded63f
TT
24423 cu->language_defn = language_def (cu->language);
24424 }
dee91e82 24425
7d45c7c3 24426 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24427}
24428
7188ed02 24429/* See read.h. */
ae038cb0 24430
7188ed02
SM
24431dwarf2_cu *
24432dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24433{
7188ed02
SM
24434 auto it = m_dwarf2_cus.find (per_cu);
24435 if (it == m_dwarf2_cus.end ())
24436 return nullptr;
ae038cb0 24437
7188ed02
SM
24438 return it->second;
24439}
24440
24441/* See read.h. */
24442
24443void
24444dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24445{
24446 gdb_assert (this->get_cu (per_cu) == nullptr);
24447
24448 m_dwarf2_cus[per_cu] = cu;
24449}
24450
24451/* See read.h. */
24452
24453void
24454dwarf2_per_objfile::age_comp_units ()
24455{
17e593e9
SM
24456 dwarf_read_debug_printf_v ("running");
24457
08ac5771
SM
24458 /* This is not expected to be called in the middle of CU expansion. There is
24459 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24460 loaded in memory. Calling age_comp_units while the queue is in use could
24461 make us free the DIEs for a CU that is in the queue and therefore break
24462 that invariant. */
24463 gdb_assert (!this->per_bfd->queue.has_value ());
24464
7188ed02
SM
24465 /* Start by clearing all marks. */
24466 for (auto pair : m_dwarf2_cus)
347212b8 24467 pair.second->clear_mark ();
7188ed02
SM
24468
24469 /* Traverse all CUs, mark them and their dependencies if used recently
24470 enough. */
24471 for (auto pair : m_dwarf2_cus)
ae038cb0 24472 {
7188ed02
SM
24473 dwarf2_cu *cu = pair.second;
24474
24475 cu->last_used++;
24476 if (cu->last_used <= dwarf_max_cache_age)
347212b8 24477 cu->mark ();
ae038cb0
DJ
24478 }
24479
7188ed02
SM
24480 /* Delete all CUs still not marked. */
24481 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 24482 {
7188ed02 24483 dwarf2_cu *cu = it->second;
ae038cb0 24484
347212b8 24485 if (!cu->is_marked ())
ae038cb0 24486 {
17e593e9
SM
24487 dwarf_read_debug_printf_v ("deleting old CU %s",
24488 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
24489 delete cu;
24490 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
24491 }
24492 else
7188ed02 24493 it++;
ae038cb0
DJ
24494 }
24495}
24496
7188ed02 24497/* See read.h. */
ae038cb0 24498
7188ed02
SM
24499void
24500dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24501{
7188ed02
SM
24502 auto it = m_dwarf2_cus.find (per_cu);
24503 if (it == m_dwarf2_cus.end ())
24504 return;
ae038cb0 24505
7188ed02 24506 delete it->second;
ae038cb0 24507
7188ed02
SM
24508 m_dwarf2_cus.erase (it);
24509}
ae038cb0 24510
7188ed02
SM
24511dwarf2_per_objfile::~dwarf2_per_objfile ()
24512{
24513 remove_all_cus ();
ae038cb0
DJ
24514}
24515
dee91e82
DE
24516/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24517 We store these in a hash table separate from the DIEs, and preserve them
24518 when the DIEs are flushed out of cache.
24519
24520 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24521 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24522 or the type may come from a DWO file. Furthermore, while it's more logical
24523 to use per_cu->section+offset, with Fission the section with the data is in
24524 the DWO file but we don't know that section at the point we need it.
24525 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24526 because we can enter the lookup routine, get_die_type_at_offset, from
24527 outside this file, and thus won't necessarily have PER_CU->cu.
24528 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24529
dee91e82 24530struct dwarf2_per_cu_offset_and_type
1c379e20 24531{
dee91e82 24532 const struct dwarf2_per_cu_data *per_cu;
9c541725 24533 sect_offset sect_off;
1c379e20
DJ
24534 struct type *type;
24535};
24536
dee91e82 24537/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24538
24539static hashval_t
dee91e82 24540per_cu_offset_and_type_hash (const void *item)
1c379e20 24541{
9a3c8263
SM
24542 const struct dwarf2_per_cu_offset_and_type *ofs
24543 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24544
9c541725 24545 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24546}
24547
dee91e82 24548/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24549
24550static int
dee91e82 24551per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24552{
9a3c8263
SM
24553 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24554 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24555 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24556 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24557
dee91e82 24558 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24559 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24560}
24561
24562/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24563 table if necessary. For convenience, return TYPE.
24564
24565 The DIEs reading must have careful ordering to:
85102364 24566 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24567 reading current DIE.
24568 * Not trying to dereference contents of still incompletely read in types
24569 while reading in other DIEs.
24570 * Enable referencing still incompletely read in types just by a pointer to
24571 the type without accessing its fields.
24572
24573 Therefore caller should follow these rules:
24574 * Try to fetch any prerequisite types we may need to build this DIE type
24575 before building the type and calling set_die_type.
e71ec853 24576 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24577 possible before fetching more types to complete the current type.
24578 * Make the type as complete as possible before fetching more types. */
1c379e20 24579
f792889a 24580static struct type *
57567375
TT
24581set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24582 bool skip_data_location)
1c379e20 24583{
976ca316 24584 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 24585 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 24586 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
24587 struct attribute *attr;
24588 struct dynamic_prop prop;
1c379e20 24589
b4ba55a1
JB
24590 /* For Ada types, make sure that the gnat-specific data is always
24591 initialized (if not already set). There are a few types where
24592 we should not be doing so, because the type-specific area is
24593 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24594 where the type-specific area is used to store the floatformat).
24595 But this is not a problem, because the gnat-specific information
24596 is actually not needed for these types. */
24597 if (need_gnat_info (cu)
78134374
SM
24598 && type->code () != TYPE_CODE_FUNC
24599 && type->code () != TYPE_CODE_FLT
24600 && type->code () != TYPE_CODE_METHODPTR
24601 && type->code () != TYPE_CODE_MEMBERPTR
24602 && type->code () != TYPE_CODE_METHOD
09584414 24603 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
24604 && !HAVE_GNAT_AUX_INFO (type))
24605 INIT_GNAT_SPECIFIC (type);
24606
3f2f83dd
KB
24607 /* Read DW_AT_allocated and set in type. */
24608 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 24609 if (attr != NULL)
3f2f83dd 24610 {
293e7e51 24611 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24612 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24613 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 24614 }
3f2f83dd
KB
24615
24616 /* Read DW_AT_associated and set in type. */
24617 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 24618 if (attr != NULL)
3f2f83dd 24619 {
293e7e51 24620 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24621 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24622 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 24623 }
3f2f83dd 24624
3cdcd0ce 24625 /* Read DW_AT_data_location and set in type. */
57567375
TT
24626 if (!skip_data_location)
24627 {
24628 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24629 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24630 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24631 }
3cdcd0ce 24632
976ca316
SM
24633 if (per_objfile->die_type_hash == NULL)
24634 per_objfile->die_type_hash
0335378b
TT
24635 = htab_up (htab_create_alloc (127,
24636 per_cu_offset_and_type_hash,
24637 per_cu_offset_and_type_eq,
24638 NULL, xcalloc, xfree));
1c379e20 24639
dee91e82 24640 ofs.per_cu = cu->per_cu;
9c541725 24641 ofs.sect_off = die->sect_off;
1c379e20 24642 ofs.type = type;
dee91e82 24643 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 24644 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24645 if (*slot)
b98664d3 24646 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24647 sect_offset_str (die->sect_off));
8d749320
SM
24648 *slot = XOBNEW (&objfile->objfile_obstack,
24649 struct dwarf2_per_cu_offset_and_type);
1c379e20 24650 **slot = ofs;
f792889a 24651 return type;
1c379e20
DJ
24652}
24653
9c541725 24654/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24655 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24656
24657static struct type *
9c541725 24658get_die_type_at_offset (sect_offset sect_off,
aa66c379 24659 dwarf2_per_cu_data *per_cu,
976ca316 24660 dwarf2_per_objfile *per_objfile)
1c379e20 24661{
dee91e82 24662 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 24663
976ca316 24664 if (per_objfile->die_type_hash == NULL)
f792889a 24665 return NULL;
1c379e20 24666
dee91e82 24667 ofs.per_cu = per_cu;
9c541725 24668 ofs.sect_off = sect_off;
9a3c8263 24669 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 24670 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24671 if (slot)
24672 return slot->type;
24673 else
24674 return NULL;
24675}
24676
02142a6c 24677/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24678 or return NULL if DIE does not have a saved type. */
24679
24680static struct type *
24681get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24682{
aa66c379 24683 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
24684}
24685
72bf9492
DJ
24686/* Trivial hash function for partial_die_info: the hash value of a DIE
24687 is its offset in .debug_info for this objfile. */
24688
24689static hashval_t
24690partial_die_hash (const void *item)
24691{
9a3c8263
SM
24692 const struct partial_die_info *part_die
24693 = (const struct partial_die_info *) item;
9a619af0 24694
9c541725 24695 return to_underlying (part_die->sect_off);
72bf9492
DJ
24696}
24697
24698/* Trivial comparison function for partial_die_info structures: two DIEs
24699 are equal if they have the same offset. */
24700
24701static int
24702partial_die_eq (const void *item_lhs, const void *item_rhs)
24703{
9a3c8263
SM
24704 const struct partial_die_info *part_die_lhs
24705 = (const struct partial_die_info *) item_lhs;
24706 const struct partial_die_info *part_die_rhs
24707 = (const struct partial_die_info *) item_rhs;
9a619af0 24708
9c541725 24709 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24710}
24711
3c3bb058
AB
24712struct cmd_list_element *set_dwarf_cmdlist;
24713struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 24714
9291a0cd 24715static void
cd4fb1b2
SM
24716show_check_physname (struct ui_file *file, int from_tty,
24717 struct cmd_list_element *c, const char *value)
9291a0cd 24718{
cd4fb1b2
SM
24719 fprintf_filtered (file,
24720 _("Whether to check \"physname\" is %s.\n"),
24721 value);
9291a0cd
TT
24722}
24723
6c265988 24724void _initialize_dwarf2_read ();
cd4fb1b2 24725void
6c265988 24726_initialize_dwarf2_read ()
9291a0cd 24727{
0743fc83 24728 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 24729Set DWARF specific variables.\n\
590042fc 24730Configure DWARF variables such as the cache size."),
2f822da5 24731 &set_dwarf_cmdlist,
0743fc83 24732 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24733
0743fc83 24734 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
24735Show DWARF specific variables.\n\
24736Show DWARF variables such as the cache size."),
2f822da5 24737 &show_dwarf_cmdlist,
0743fc83 24738 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24739
cd4fb1b2
SM
24740 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24741 &dwarf_max_cache_age, _("\
24742Set the upper bound on the age of cached DWARF compilation units."), _("\
24743Show the upper bound on the age of cached DWARF compilation units."), _("\
24744A higher limit means that cached compilation units will be stored\n\
24745in memory longer, and more total memory will be used. Zero disables\n\
24746caching, which can slow down startup."),
24747 NULL,
24748 show_dwarf_max_cache_age,
24749 &set_dwarf_cmdlist,
24750 &show_dwarf_cmdlist);
156942c7 24751
cd4fb1b2
SM
24752 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24753Set debugging of the DWARF reader."), _("\
24754Show debugging of the DWARF reader."), _("\
24755When enabled (non-zero), debugging messages are printed during DWARF\n\
24756reading and symtab expansion. A value of 1 (one) provides basic\n\
24757information. A value greater than 1 provides more verbose information."),
24758 NULL,
24759 NULL,
24760 &setdebuglist, &showdebuglist);
9291a0cd 24761
cd4fb1b2
SM
24762 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24763Set debugging of the DWARF DIE reader."), _("\
24764Show debugging of the DWARF DIE reader."), _("\
24765When enabled (non-zero), DIEs are dumped after they are read in.\n\
24766The value is the maximum depth to print."),
24767 NULL,
24768 NULL,
24769 &setdebuglist, &showdebuglist);
9291a0cd 24770
cd4fb1b2
SM
24771 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24772Set debugging of the dwarf line reader."), _("\
24773Show debugging of the dwarf line reader."), _("\
24774When enabled (non-zero), line number entries are dumped as they are read in.\n\
24775A value of 1 (one) provides basic information.\n\
24776A value greater than 1 provides more verbose information."),
24777 NULL,
24778 NULL,
24779 &setdebuglist, &showdebuglist);
437afbb8 24780
cd4fb1b2
SM
24781 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24782Set cross-checking of \"physname\" code against demangler."), _("\
24783Show cross-checking of \"physname\" code against demangler."), _("\
24784When enabled, GDB's internal \"physname\" code is checked against\n\
24785the demangler."),
24786 NULL, show_check_physname,
24787 &setdebuglist, &showdebuglist);
900e11f9 24788
e615022a
DE
24789 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24790 no_class, &use_deprecated_index_sections, _("\
24791Set whether to use deprecated gdb_index sections."), _("\
24792Show whether to use deprecated gdb_index sections."), _("\
24793When enabled, deprecated .gdb_index sections are used anyway.\n\
24794Normally they are ignored either because of a missing feature or\n\
24795performance issue.\n\
24796Warning: This option must be enabled before gdb reads the file."),
24797 NULL,
24798 NULL,
24799 &setlist, &showlist);
24800
f1e6e072
TT
24801 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24802 &dwarf2_locexpr_funcs);
24803 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24804 &dwarf2_loclist_funcs);
24805
24806 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24807 &dwarf2_block_frame_base_locexpr_funcs);
24808 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24809 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24810
24811#if GDB_SELF_TEST
24812 selftests::register_test ("dw2_expand_symtabs_matching",
24813 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24814 selftests::register_test ("dwarf2_find_containing_comp_unit",
24815 selftests::find_containing_comp_unit::run_test);
c62446b1 24816#endif
6502dd73 24817}
This page took 6.377606 seconds and 4 git commands to generate.