[gdb/symtab] Fix infinite recursion in dwarf2_cu::get_builder()
[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"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
9fda78b6 40#include "dwarf2/dwz.h"
c90ec28a 41#include "dwarf2/macro.h"
c2d50fd0 42#include "dwarf2/die.h"
c2a62a3d 43#include "dwarf2/sect-names.h"
2b2558bf 44#include "dwarf2/stringify.h"
70182375 45#include "dwarf2/public.h"
4de283e4
TT
46#include "bfd.h"
47#include "elf-bfd.h"
48#include "symtab.h"
49#include "gdbtypes.h"
50#include "objfiles.h"
d55e5aa6 51#include "dwarf2.h"
4de283e4
TT
52#include "buildsym.h"
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
3da10d80
KS
463/* Type used for delaying computation of method physnames.
464 See comments for compute_delayed_physnames. */
465struct delayed_method_info
466{
467 /* The type to which the method is attached, i.e., its parent class. */
468 struct type *type;
469
470 /* The index of the method in the type's function fieldlists. */
471 int fnfield_index;
472
473 /* The index of the method in the fieldlist. */
474 int index;
475
476 /* The name of the DIE. */
477 const char *name;
478
479 /* The DIE associated with this method. */
480 struct die_info *die;
481};
482
e7c27a73
DJ
483/* Internal state when decoding a particular compilation unit. */
484struct dwarf2_cu
485{
9e021579
SM
486 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
487 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
488
489 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
490
c24bdb02
KS
491 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
492 Create the set of symtabs used by this TU, or if this TU is sharing
493 symtabs with another TU and the symtabs have already been created
494 then restore those symtabs in the line header.
495 We don't need the pc/line-number mapping for type units. */
496 void setup_type_unit_groups (struct die_info *die);
497
498 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
499 buildsym_compunit constructor. */
500 struct compunit_symtab *start_symtab (const char *name,
501 const char *comp_dir,
502 CORE_ADDR low_pc);
503
504 /* Reset the builder. */
505 void reset_builder () { m_builder.reset (); }
506
293e7e51
SM
507 /* Return a type that is a generic pointer type, the size of which
508 matches the address size given in the compilation unit header for
509 this CU. */
510 struct type *addr_type () const;
511
512 /* Find an integer type the same size as the address size given in
513 the compilation unit header for this CU. UNSIGNED_P controls if
514 the integer is unsigned or not. */
515 struct type *addr_sized_int_type (bool unsigned_p) const;
516
d00adf39 517 /* The header of the compilation unit. */
fcd3b13d 518 struct comp_unit_head header {};
e142c38c 519
d00adf39 520 /* Base address of this compilation unit. */
2b24b6e4 521 gdb::optional<CORE_ADDR> base_address;
d00adf39 522
e142c38c 523 /* The language we are debugging. */
fcd3b13d
SM
524 enum language language = language_unknown;
525 const struct language_defn *language_defn = nullptr;
e142c38c 526
fcd3b13d 527 const char *producer = nullptr;
b0f35d58 528
c24bdb02 529private:
804d2729
TT
530 /* The symtab builder for this CU. This is only non-NULL when full
531 symbols are being read. */
c24bdb02 532 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 533
c24bdb02 534public:
e142c38c
DJ
535 /* The generic symbol table building routines have separate lists for
536 file scope symbols and all all other scopes (local scopes). So
537 we need to select the right one to pass to add_symbol_to_list().
538 We do it by keeping a pointer to the correct list in list_in_scope.
539
540 FIXME: The original dwarf code just treated the file scope as the
541 first local scope, and all other local scopes as nested local
542 scopes, and worked fine. Check to see if we really need to
543 distinguish these in buildsym.c. */
fcd3b13d 544 struct pending **list_in_scope = nullptr;
e142c38c 545
b64f50a1
JK
546 /* Hash table holding all the loaded partial DIEs
547 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 548 htab_t partial_dies = nullptr;
72bf9492
DJ
549
550 /* Storage for things with the same lifetime as this read-in compilation
551 unit, including partial DIEs. */
fcd3b13d 552 auto_obstack comp_unit_obstack;
72bf9492 553
69d751e3 554 /* Backlink to our per_cu entry. */
ae038cb0
DJ
555 struct dwarf2_per_cu_data *per_cu;
556
9e021579 557 /* The dwarf2_per_objfile that owns this. */
976ca316 558 dwarf2_per_objfile *per_objfile;
9e021579 559
ae038cb0 560 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 561 int last_used = 0;
ae038cb0 562
b64f50a1
JK
563 /* A hash table of DIE cu_offset for following references with
564 die_info->offset.sect_off as hash. */
fcd3b13d 565 htab_t die_hash = nullptr;
10b3939b
DJ
566
567 /* Full DIEs if read in. */
fcd3b13d 568 struct die_info *dies = nullptr;
10b3939b
DJ
569
570 /* A set of pointers to dwarf2_per_cu_data objects for compilation
571 units referenced by this one. Only set during full symbol processing;
572 partial symbol tables do not have dependencies. */
fcd3b13d 573 htab_t dependencies = nullptr;
10b3939b 574
cb1df416 575 /* Header data from the line table, during full symbol processing. */
fcd3b13d 576 struct line_header *line_header = nullptr;
4c8aa72d 577 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 578 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
579 this is the DW_TAG_compile_unit die for this CU. We'll hold on
580 to the line header as long as this DIE is being processed. See
581 process_die_scope. */
fcd3b13d 582 die_info *line_header_die_owner = nullptr;
cb1df416 583
3da10d80
KS
584 /* A list of methods which need to have physnames computed
585 after all type information has been read. */
c89b44cd 586 std::vector<delayed_method_info> method_list;
3da10d80 587
96408a79 588 /* To be copied to symtab->call_site_htab. */
fcd3b13d 589 htab_t call_site_htab = nullptr;
96408a79 590
034e5797
DE
591 /* Non-NULL if this CU came from a DWO file.
592 There is an invariant here that is important to remember:
593 Except for attributes copied from the top level DIE in the "main"
594 (or "stub") file in preparation for reading the DWO file
18a8505e 595 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
596 Either there isn't a DWO file (in which case this is NULL and the point
597 is moot), or there is and either we're not going to read it (in which
598 case this is NULL) or there is and we are reading it (in which case this
599 is non-NULL). */
fcd3b13d 600 struct dwo_unit *dwo_unit = nullptr;
3019eac3 601
18a8505e 602 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 603 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 604 gdb::optional<ULONGEST> addr_base;
3019eac3 605
2b0c7f41
SM
606 /* The DW_AT_GNU_ranges_base attribute, if present.
607
608 This is only relevant in the context of pre-DWARF 5 split units. In this
609 context, there is a .debug_ranges section in the linked executable,
610 containing all the ranges data for all the compilation units. Each
611 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
612 indicates the base of its contribution to that section. The DW_AT_ranges
613 attributes in the split-unit are of the form DW_FORM_sec_offset and point
614 into the .debug_ranges section of the linked file. However, they are not
615 "true" DW_FORM_sec_offset, because they are relative to the base of their
616 compilation unit's contribution, rather than relative to the beginning of
617 the section. The DW_AT_GNU_ranges_base value must be added to it to make
618 it relative to the beginning of the section.
619
620 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
621 case, so this value can be added without needing to know whether we are in
622 this case or not.
623
624 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
625 skeleton/stub, it must not have the base added, as it already points to the
626 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
627 have a DW_AT_ranges attribute, we can use the
628
629 die->tag != DW_AT_compile_unit
630
631 to determine whether the base should be added or not. */
632 ULONGEST gnu_ranges_base = 0;
633
634 /* The DW_AT_rnglists_base attribute, if present.
635
636 This is used when processing attributes of form DW_FORM_rnglistx in
637 non-split units. Attributes of this form found in a split unit don't
638 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
639 section. */
640 ULONGEST rnglists_base = 0;
2e3cf129 641
41144253 642 /* The DW_AT_loclists_base attribute if present. */
643 ULONGEST loclist_base = 0;
644
c9317f21
TT
645 /* When reading debug info generated by older versions of rustc, we
646 have to rewrite some union types to be struct types with a
647 variant part. This rewriting must be done after the CU is fully
648 read in, because otherwise at the point of rewriting some struct
649 type might not have been fully processed. So, we keep a list of
650 all such types here and process them after expansion. */
651 std::vector<struct type *> rust_unions;
652
18a8505e
AT
653 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
654 files, the value is implicitly zero. For DWARF 5 version DWO files, the
655 value is often implicit and is the size of the header of
656 .debug_str_offsets section (8 or 4, depending on the address size). */
657 gdb::optional<ULONGEST> str_offsets_base;
658
ae038cb0 659 /* Mark used when releasing cached dies. */
9068261f 660 bool mark : 1;
ae038cb0 661
8be455d7
JK
662 /* This CU references .debug_loc. See the symtab->locations_valid field.
663 This test is imperfect as there may exist optimized debug code not using
664 any location list and still facing inlining issues if handled as
665 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 666 bool has_loclist : 1;
ba919b58 667
9068261f 668 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
669 if all the producer_is_* fields are valid. This information is cached
670 because profiling CU expansion showed excessive time spent in
671 producer_is_gxx_lt_4_6. */
9068261f
AB
672 bool checked_producer : 1;
673 bool producer_is_gxx_lt_4_6 : 1;
674 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 675 bool producer_is_icc : 1;
9068261f 676 bool producer_is_icc_lt_14 : 1;
c258c396 677 bool producer_is_codewarrior : 1;
4d4ec4e5 678
9068261f 679 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
680 debugging info for C++ namespaces. GCC 3.3.x did not produce
681 this information, but later versions do. */
682
9068261f 683 bool processing_has_namespace_info : 1;
d590ff25
YQ
684
685 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02 686
c24bdb02
KS
687 /* Get the buildsym_compunit for this CU. */
688 buildsym_compunit *get_builder ()
689 {
690 /* If this CU has a builder associated with it, use that. */
691 if (m_builder != nullptr)
692 return m_builder.get ();
693
c24bdb02
KS
694 return nullptr;
695 }
e7c27a73
DJ
696};
697
094b34ac
DE
698/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
699 This includes type_unit_group and quick_file_names. */
700
701struct stmt_list_hash
702{
703 /* The DWO unit this table is from or NULL if there is none. */
704 struct dwo_unit *dwo_unit;
705
706 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 707 sect_offset line_sect_off;
094b34ac
DE
708};
709
5989a64e 710/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
711 an object of this type. This contains elements of type unit groups
712 that can be shared across objfiles. The non-shareable parts are in
713 type_unit_group_unshareable. */
f4dc4d17 714
0d305d5c 715struct type_unit_group : public dwarf2_per_cu_data
f4dc4d17 716{
0186c6a7
DE
717 /* The TUs that share this DW_AT_stmt_list entry.
718 This is added to while parsing type units to build partial symtabs,
719 and is deleted afterwards and not used again. */
0d305d5c 720 std::vector<signatured_type *> *tus = nullptr;
f4dc4d17 721
094b34ac 722 /* The data used to construct the hash key. */
0d305d5c 723 struct stmt_list_hash hash {};
f4dc4d17
DE
724};
725
73869dc2 726/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
727
728struct dwo_sections
729{
730 struct dwarf2_section_info abbrev;
3019eac3
DE
731 struct dwarf2_section_info line;
732 struct dwarf2_section_info loc;
43988095 733 struct dwarf2_section_info loclists;
09262596
DE
734 struct dwarf2_section_info macinfo;
735 struct dwarf2_section_info macro;
d0ce17d8 736 struct dwarf2_section_info rnglists;
3019eac3
DE
737 struct dwarf2_section_info str;
738 struct dwarf2_section_info str_offsets;
80626a55
DE
739 /* In the case of a virtual DWO file, these two are unused. */
740 struct dwarf2_section_info info;
fd5866f6 741 std::vector<dwarf2_section_info> types;
3019eac3
DE
742};
743
c88ee1f0 744/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
745
746struct dwo_unit
747{
748 /* Backlink to the containing struct dwo_file. */
749 struct dwo_file *dwo_file;
750
751 /* The "id" that distinguishes this CU/TU.
752 .debug_info calls this "dwo_id", .debug_types calls this "signature".
753 Since signatures came first, we stick with it for consistency. */
754 ULONGEST signature;
755
756 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 757 struct dwarf2_section_info *section;
3019eac3 758
9c541725
PA
759 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
760 sect_offset sect_off;
3019eac3
DE
761 unsigned int length;
762
763 /* For types, offset in the type's DIE of the type defined by this TU. */
764 cu_offset type_offset_in_tu;
765};
766
73869dc2
DE
767/* include/dwarf2.h defines the DWP section codes.
768 It defines a max value but it doesn't define a min value, which we
769 use for error checking, so provide one. */
770
771enum dwp_v2_section_ids
772{
773 DW_SECT_MIN = 1
774};
775
80626a55 776/* Data for one DWO file.
57d63ce2
DE
777
778 This includes virtual DWO files (a virtual DWO file is a DWO file as it
779 appears in a DWP file). DWP files don't really have DWO files per se -
780 comdat folding of types "loses" the DWO file they came from, and from
781 a high level view DWP files appear to contain a mass of random types.
782 However, to maintain consistency with the non-DWP case we pretend DWP
783 files contain virtual DWO files, and we assign each TU with one virtual
784 DWO file (generally based on the line and abbrev section offsets -
785 a heuristic that seems to work in practice). */
3019eac3
DE
786
787struct dwo_file
788{
51ac9db5
SM
789 dwo_file () = default;
790 DISABLE_COPY_AND_ASSIGN (dwo_file);
791
18a8505e 792 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
793 For virtual DWO files the name is constructed from the section offsets
794 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
795 from related CU+TUs. */
51ac9db5 796 const char *dwo_name = nullptr;
0ac5b59e
DE
797
798 /* The DW_AT_comp_dir attribute. */
51ac9db5 799 const char *comp_dir = nullptr;
3019eac3 800
80626a55
DE
801 /* The bfd, when the file is open. Otherwise this is NULL.
802 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 803 gdb_bfd_ref_ptr dbfd;
3019eac3 804
73869dc2 805 /* The sections that make up this DWO file.
d2854d8d 806 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
73869dc2 807 sections (for lack of a better name). */
51ac9db5 808 struct dwo_sections sections {};
3019eac3 809
33c5cd75
DB
810 /* The CUs in the file.
811 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
812 an extension to handle LLVM's Link Time Optimization output (where
813 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 814 htab_up cus;
3019eac3
DE
815
816 /* Table of TUs in the file.
817 Each element is a struct dwo_unit. */
b0b6a987 818 htab_up tus;
3019eac3
DE
819};
820
80626a55
DE
821/* These sections are what may appear in a DWP file. */
822
823struct dwp_sections
824{
d2854d8d 825 /* These are used by all DWP versions (1, 2 and 5). */
80626a55
DE
826 struct dwarf2_section_info str;
827 struct dwarf2_section_info cu_index;
828 struct dwarf2_section_info tu_index;
73869dc2 829
d2854d8d 830 /* These are only used by DWP version 2 and version 5 files.
73869dc2
DE
831 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
832 sections are referenced by section number, and are not recorded here.
d2854d8d
CT
833 In DWP version 2 or 5 there is at most one copy of all these sections,
834 each section being (effectively) comprised of the concatenation of all of
835 the individual sections that exist in the version 1 format.
73869dc2
DE
836 To keep the code simple we treat each of these concatenated pieces as a
837 section itself (a virtual section?). */
838 struct dwarf2_section_info abbrev;
839 struct dwarf2_section_info info;
840 struct dwarf2_section_info line;
841 struct dwarf2_section_info loc;
d2854d8d 842 struct dwarf2_section_info loclists;
73869dc2
DE
843 struct dwarf2_section_info macinfo;
844 struct dwarf2_section_info macro;
d2854d8d 845 struct dwarf2_section_info rnglists;
73869dc2
DE
846 struct dwarf2_section_info str_offsets;
847 struct dwarf2_section_info types;
80626a55
DE
848};
849
73869dc2
DE
850/* These sections are what may appear in a virtual DWO file in DWP version 1.
851 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 852
73869dc2 853struct virtual_v1_dwo_sections
80626a55
DE
854{
855 struct dwarf2_section_info abbrev;
856 struct dwarf2_section_info line;
857 struct dwarf2_section_info loc;
858 struct dwarf2_section_info macinfo;
859 struct dwarf2_section_info macro;
860 struct dwarf2_section_info str_offsets;
861 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 862 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
863 struct dwarf2_section_info info_or_types;
864};
865
d2854d8d 866/* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
73869dc2
DE
867 In version 2, the sections of the DWO files are concatenated together
868 and stored in one section of that name. Thus each ELF section contains
869 several "virtual" sections. */
870
d2854d8d 871struct virtual_v2_or_v5_dwo_sections
73869dc2
DE
872{
873 bfd_size_type abbrev_offset;
874 bfd_size_type abbrev_size;
875
876 bfd_size_type line_offset;
877 bfd_size_type line_size;
878
879 bfd_size_type loc_offset;
880 bfd_size_type loc_size;
881
d2854d8d
CT
882 bfd_size_type loclists_offset;
883 bfd_size_type loclists_size;
884
73869dc2
DE
885 bfd_size_type macinfo_offset;
886 bfd_size_type macinfo_size;
887
888 bfd_size_type macro_offset;
889 bfd_size_type macro_size;
890
d2854d8d
CT
891 bfd_size_type rnglists_offset;
892 bfd_size_type rnglists_size;
893
73869dc2
DE
894 bfd_size_type str_offsets_offset;
895 bfd_size_type str_offsets_size;
896
897 /* Each DWP hash table entry records one CU or one TU.
898 That is recorded here, and copied to dwo_unit.section. */
899 bfd_size_type info_or_types_offset;
900 bfd_size_type info_or_types_size;
901};
902
80626a55
DE
903/* Contents of DWP hash tables. */
904
905struct dwp_hash_table
906{
73869dc2 907 uint32_t version, nr_columns;
80626a55 908 uint32_t nr_units, nr_slots;
73869dc2
DE
909 const gdb_byte *hash_table, *unit_table;
910 union
911 {
912 struct
913 {
914 const gdb_byte *indices;
915 } v1;
916 struct
917 {
918 /* This is indexed by column number and gives the id of the section
919 in that column. */
920#define MAX_NR_V2_DWO_SECTIONS \
921 (1 /* .debug_info or .debug_types */ \
922 + 1 /* .debug_abbrev */ \
923 + 1 /* .debug_line */ \
924 + 1 /* .debug_loc */ \
925 + 1 /* .debug_str_offsets */ \
926 + 1 /* .debug_macro or .debug_macinfo */)
927 int section_ids[MAX_NR_V2_DWO_SECTIONS];
928 const gdb_byte *offsets;
929 const gdb_byte *sizes;
930 } v2;
d2854d8d
CT
931 struct
932 {
933 /* This is indexed by column number and gives the id of the section
934 in that column. */
935#define MAX_NR_V5_DWO_SECTIONS \
936 (1 /* .debug_info */ \
937 + 1 /* .debug_abbrev */ \
938 + 1 /* .debug_line */ \
939 + 1 /* .debug_loclists */ \
940 + 1 /* .debug_str_offsets */ \
941 + 1 /* .debug_macro */ \
942 + 1 /* .debug_rnglists */)
943 int section_ids[MAX_NR_V5_DWO_SECTIONS];
944 const gdb_byte *offsets;
945 const gdb_byte *sizes;
946 } v5;
73869dc2 947 } section_pool;
80626a55
DE
948};
949
950/* Data for one DWP file. */
951
952struct dwp_file
953{
400174b1
TT
954 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
955 : name (name_),
956 dbfd (std::move (abfd))
957 {
958 }
959
80626a55
DE
960 /* Name of the file. */
961 const char *name;
962
73869dc2 963 /* File format version. */
400174b1 964 int version = 0;
73869dc2 965
93417882 966 /* The bfd. */
400174b1 967 gdb_bfd_ref_ptr dbfd;
80626a55
DE
968
969 /* Section info for this file. */
400174b1 970 struct dwp_sections sections {};
80626a55 971
57d63ce2 972 /* Table of CUs in the file. */
400174b1 973 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
974
975 /* Table of TUs in the file. */
400174b1 976 const struct dwp_hash_table *tus = nullptr;
80626a55 977
19ac8c2e 978 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
979 htab_up loaded_cus;
980 htab_up loaded_tus;
80626a55 981
73869dc2
DE
982 /* Table to map ELF section numbers to their sections.
983 This is only needed for the DWP V1 file format. */
400174b1
TT
984 unsigned int num_sections = 0;
985 asection **elf_sections = nullptr;
80626a55
DE
986};
987
0963b4bd
MS
988/* Struct used to pass misc. parameters to read_die_and_children, et
989 al. which are used for both .debug_info and .debug_types dies.
990 All parameters here are unchanging for the life of the call. This
dee91e82 991 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
992
993struct die_reader_specs
994{
a32a8923 995 /* The bfd of die_section. */
0280fdcc 996 bfd *abfd;
93311388
DE
997
998 /* The CU of the DIE we are parsing. */
999 struct dwarf2_cu *cu;
1000
80626a55 1001 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1002 struct dwo_file *dwo_file;
1003
dee91e82 1004 /* The section the die comes from.
3019eac3 1005 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1006 struct dwarf2_section_info *die_section;
1007
1008 /* die_section->buffer. */
d521ce57 1009 const gdb_byte *buffer;
f664829e
DE
1010
1011 /* The end of the buffer. */
1012 const gdb_byte *buffer_end;
a2ce51a0 1013
685af9cd
TT
1014 /* The abbreviation table to use when reading the DIEs. */
1015 struct abbrev_table *abbrev_table;
93311388
DE
1016};
1017
c0ab21c2
TT
1018/* A subclass of die_reader_specs that holds storage and has complex
1019 constructor and destructor behavior. */
1020
1021class cutu_reader : public die_reader_specs
1022{
1023public:
1024
ab432490
SM
1025 cutu_reader (dwarf2_per_cu_data *this_cu,
1026 dwarf2_per_objfile *per_objfile,
c0ab21c2 1027 struct abbrev_table *abbrev_table,
2e671100 1028 dwarf2_cu *existing_cu,
c0ab21c2
TT
1029 bool skip_partial);
1030
1031 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 1032 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
1033 struct dwarf2_cu *parent_cu = nullptr,
1034 struct dwo_file *dwo_file = nullptr);
1035
c0ab21c2
TT
1036 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1037
1038 const gdb_byte *info_ptr = nullptr;
1039 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
1040 bool dummy_p = false;
1041
6751ebae
TT
1042 /* Release the new CU, putting it on the chain. This cannot be done
1043 for dummy CUs. */
1044 void keep ();
1045
c0ab21c2 1046private:
9e021579
SM
1047 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1048 dwarf2_per_objfile *per_objfile,
2e671100 1049 dwarf2_cu *existing_cu);
c0ab21c2
TT
1050
1051 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
1052 std::unique_ptr<dwarf2_cu> m_new_cu;
1053
1054 /* The ordinary abbreviation table. */
1055 abbrev_table_up m_abbrev_table_holder;
1056
1057 /* The DWO abbreviation table. */
1058 abbrev_table_up m_dwo_abbrev_table;
1059};
dee91e82 1060
c906108c 1061/* When we construct a partial symbol table entry we only
0963b4bd 1062 need this much information. */
6f06d47b 1063struct partial_die_info : public allocate_on_obstack
c906108c 1064 {
7c32eebb 1065 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
6f06d47b
YQ
1066
1067 /* Disable assign but still keep copy ctor, which is needed
1068 load_partial_dies. */
1069 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1070
52356b79
YQ
1071 /* Adjust the partial die before generating a symbol for it. This
1072 function may set the is_external flag or change the DIE's
1073 name. */
1074 void fixup (struct dwarf2_cu *cu);
1075
48fbe735
YQ
1076 /* Read a minimal amount of information into the minimal die
1077 structure. */
1078 const gdb_byte *read (const struct die_reader_specs *reader,
1079 const struct abbrev_info &abbrev,
1080 const gdb_byte *info_ptr);
1081
7d00ffec
TT
1082 /* Compute the name of this partial DIE. This memoizes the
1083 result, so it is safe to call multiple times. */
1084 const char *name (dwarf2_cu *cu);
1085
72bf9492 1086 /* Offset of this DIE. */
6f06d47b 1087 const sect_offset sect_off;
72bf9492
DJ
1088
1089 /* DWARF-2 tag for this DIE. */
6f06d47b 1090 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1091
72bf9492 1092 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1093 const unsigned int has_children : 1;
1094
72bf9492
DJ
1095 unsigned int is_external : 1;
1096 unsigned int is_declaration : 1;
1097 unsigned int has_type : 1;
1098 unsigned int has_specification : 1;
1099 unsigned int has_pc_info : 1;
481860b3 1100 unsigned int may_be_inlined : 1;
72bf9492 1101
0c1b455e
TT
1102 /* This DIE has been marked DW_AT_main_subprogram. */
1103 unsigned int main_subprogram : 1;
1104
72bf9492
DJ
1105 /* Flag set if the SCOPE field of this structure has been
1106 computed. */
1107 unsigned int scope_set : 1;
1108
fa4028e9
JB
1109 /* Flag set if the DIE has a byte_size attribute. */
1110 unsigned int has_byte_size : 1;
1111
ff908ebf
AW
1112 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1113 unsigned int has_const_value : 1;
1114
98bfdba5
PA
1115 /* Flag set if any of the DIE's children are template arguments. */
1116 unsigned int has_template_arguments : 1;
1117
52356b79 1118 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1119 unsigned int fixup_called : 1;
1120
36586728
TT
1121 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1122 unsigned int is_dwz : 1;
1123
1124 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1125 unsigned int spec_is_dwz : 1;
1126
7d00ffec
TT
1127 unsigned int canonical_name : 1;
1128
72bf9492 1129 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1130 sometimes a default name for unnamed DIEs. */
7d00ffec 1131 const char *raw_name = nullptr;
72bf9492 1132
abc72ce4 1133 /* The linkage name, if present. */
6f06d47b 1134 const char *linkage_name = nullptr;
abc72ce4 1135
72bf9492
DJ
1136 /* The scope to prepend to our children. This is generally
1137 allocated on the comp_unit_obstack, so will disappear
1138 when this compilation unit leaves the cache. */
6f06d47b 1139 const char *scope = nullptr;
72bf9492 1140
95554aad
TT
1141 /* Some data associated with the partial DIE. The tag determines
1142 which field is live. */
1143 union
1144 {
1145 /* The location description associated with this DIE, if any. */
1146 struct dwarf_block *locdesc;
1147 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1148 sect_offset sect_off;
6f06d47b 1149 } d {};
72bf9492
DJ
1150
1151 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1152 CORE_ADDR lowpc = 0;
1153 CORE_ADDR highpc = 0;
72bf9492 1154
93311388 1155 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1156 DW_AT_sibling, if any. */
48fbe735
YQ
1157 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1158 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1159 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1160
1161 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1162 DW_AT_specification (or DW_AT_abstract_origin or
1163 DW_AT_extension). */
6f06d47b 1164 sect_offset spec_offset {};
72bf9492
DJ
1165
1166 /* Pointers to this DIE's parent, first child, and next sibling,
1167 if any. */
6f06d47b
YQ
1168 struct partial_die_info *die_parent = nullptr;
1169 struct partial_die_info *die_child = nullptr;
1170 struct partial_die_info *die_sibling = nullptr;
1171
1172 friend struct partial_die_info *
1173 dwarf2_cu::find_partial_die (sect_offset sect_off);
1174
1175 private:
1176 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1177 partial_die_info (sect_offset sect_off)
1178 : partial_die_info (sect_off, DW_TAG_padding, 0)
1179 {
1180 }
1181
1182 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1183 int has_children_)
1184 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1185 {
1186 is_external = 0;
1187 is_declaration = 0;
1188 has_type = 0;
1189 has_specification = 0;
1190 has_pc_info = 0;
1191 may_be_inlined = 0;
1192 main_subprogram = 0;
1193 scope_set = 0;
1194 has_byte_size = 0;
1195 has_const_value = 0;
1196 has_template_arguments = 0;
1197 fixup_called = 0;
1198 is_dwz = 0;
1199 spec_is_dwz = 0;
7d00ffec 1200 canonical_name = 0;
6f06d47b 1201 }
c906108c
SS
1202 };
1203
c906108c
SS
1204/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1205 but this would require a corresponding change in unpack_field_as_long
1206 and friends. */
1207static int bits_per_byte = 8;
1208
9c6a1327
TT
1209struct variant_part_builder;
1210
1211/* When reading a variant, we track a bit more information about the
1212 field, and store it in an object of this type. */
2ddeaf8a
TT
1213
1214struct variant_field
1215{
9c6a1327
TT
1216 int first_field = -1;
1217 int last_field = -1;
1218
1219 /* A variant can contain other variant parts. */
1220 std::vector<variant_part_builder> variant_parts;
1221
2ddeaf8a
TT
1222 /* If we see a DW_TAG_variant, then this will be set if this is the
1223 default branch. */
9c6a1327
TT
1224 bool default_branch = false;
1225 /* If we see a DW_AT_discr_value, then this will be the discriminant
1226 value. */
1227 ULONGEST discriminant_value = 0;
1228 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1229 data. */
1230 struct dwarf_block *discr_list_data = nullptr;
1231};
1232
1233/* This represents a DW_TAG_variant_part. */
1234
1235struct variant_part_builder
1236{
1237 /* The offset of the discriminant field. */
1238 sect_offset discriminant_offset {};
1239
1240 /* Variants that are direct children of this variant part. */
1241 std::vector<variant_field> variants;
1242
1243 /* True if we're currently reading a variant. */
1244 bool processing_variant = false;
2ddeaf8a
TT
1245};
1246
52059ffd
TT
1247struct nextfield
1248{
be2daae6
TT
1249 int accessibility = 0;
1250 int virtuality = 0;
9c6a1327
TT
1251 /* Variant parts need to find the discriminant, which is a DIE
1252 reference. We track the section offset of each field to make
1253 this link. */
1254 sect_offset offset;
be2daae6 1255 struct field field {};
52059ffd
TT
1256};
1257
1258struct fnfieldlist
1259{
be2daae6
TT
1260 const char *name = nullptr;
1261 std::vector<struct fn_field> fnfields;
52059ffd
TT
1262};
1263
c906108c
SS
1264/* The routines that read and process dies for a C struct or C++ class
1265 pass lists of data member fields and lists of member function fields
1266 in an instance of a field_info structure, as defined below. */
1267struct field_info
2de01bdb
SM
1268{
1269 /* List of data member and baseclasses fields. */
1270 std::vector<struct nextfield> fields;
1271 std::vector<struct nextfield> baseclasses;
1272
1273 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1274 bool non_public_fields = false;
2de01bdb
SM
1275
1276 /* Member function fieldlist array, contains name of possibly overloaded
1277 member function, number of overloaded member functions and a pointer
1278 to the head of the member function field chain. */
1279 std::vector<struct fnfieldlist> fnfieldlists;
1280
1281 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1282 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1283 std::vector<struct decl_field> typedef_field_list;
1284
1285 /* Nested types defined by this class and the number of elements in this
1286 list. */
1287 std::vector<struct decl_field> nested_types_list;
1288
1289 /* If non-null, this is the variant part we are currently
1290 reading. */
1291 variant_part_builder *current_variant_part = nullptr;
1292 /* This holds all the top-level variant parts attached to the type
1293 we're reading. */
1294 std::vector<variant_part_builder> variant_parts;
1295
1296 /* Return the total number of fields (including baseclasses). */
1297 int nfields () const
c5aa993b 1298 {
2de01bdb
SM
1299 return fields.size () + baseclasses.size ();
1300 }
1301};
c906108c 1302
ae038cb0
DJ
1303/* Loaded secondary compilation units are kept in memory until they
1304 have not been referenced for the processing of this many
1305 compilation units. Set this to zero to disable caching. Cache
1306 sizes of up to at least twenty will improve startup time for
1307 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1308static int dwarf_max_cache_age = 5;
920d2a44 1309static void
b4f54984
DE
1310show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1311 struct cmd_list_element *c, const char *value)
920d2a44 1312{
3e43a32a 1313 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1314 "DWARF compilation units is %s.\n"),
920d2a44
AC
1315 value);
1316}
4390d890 1317\f
c906108c
SS
1318/* local function prototypes */
1319
918dd910
JK
1320static void dwarf2_find_base_address (struct die_info *die,
1321 struct dwarf2_cu *cu);
1322
891813be 1323static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1324 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1325 const char *name);
0018ea6f 1326
f1902523
JK
1327static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1328 const gdb_byte *info_ptr,
3e225074 1329 struct die_info *type_unit_die);
f1902523 1330
976ca316 1331static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1332
72bf9492
DJ
1333static void scan_partial_symbols (struct partial_die_info *,
1334 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1335 int, struct dwarf2_cu *);
c906108c 1336
72bf9492
DJ
1337static void add_partial_symbol (struct partial_die_info *,
1338 struct dwarf2_cu *);
63d06c5c 1339
72bf9492
DJ
1340static void add_partial_namespace (struct partial_die_info *pdi,
1341 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1342 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1343
5d7cb8df 1344static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1345 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1346 struct dwarf2_cu *cu);
1347
72bf9492
DJ
1348static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1349 struct dwarf2_cu *cu);
91c24f0a 1350
bc30ff58
JB
1351static void add_partial_subprogram (struct partial_die_info *pdi,
1352 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1353 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1354
d521ce57 1355static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1356
dee91e82 1357static struct partial_die_info *load_partial_dies
d521ce57 1358 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1359
fb816e8b
TV
1360/* A pair of partial_die_info and compilation unit. */
1361struct cu_partial_die_info
1362{
1363 /* The compilation unit of the partial_die_info. */
1364 struct dwarf2_cu *cu;
1365 /* A partial_die_info. */
1366 struct partial_die_info *pdi;
122cf0f2
AB
1367
1368 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1369 : cu (cu),
1370 pdi (pdi)
405feb71 1371 { /* Nothing. */ }
122cf0f2
AB
1372
1373private:
1374 cu_partial_die_info () = delete;
fb816e8b
TV
1375};
1376
122cf0f2
AB
1377static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1378 struct dwarf2_cu *);
72bf9492 1379
d521ce57 1380static const gdb_byte *read_attribute (const struct die_reader_specs *,
4444f407
TT
1381 struct attribute *,
1382 const struct attr_abbrev *,
7a5f294d 1383 const gdb_byte *);
18a8505e
AT
1384
1385static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1386 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1387
1388static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1389
976ca316
SM
1390static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1391 dwarf2_section_info *, sect_offset);
f4dc4d17 1392
ed2dc618 1393static const char *read_indirect_string
976ca316 1394 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1395 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1396
ed2dc618 1397static const char *read_indirect_string_at_offset
976ca316 1398 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1399
d521ce57
TT
1400static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1401 const gdb_byte *,
3019eac3
DE
1402 unsigned int *);
1403
18a8505e
AT
1404static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1405 ULONGEST str_index);
1406
1407static const char *read_stub_str_index (struct dwarf2_cu *cu,
1408 ULONGEST str_index);
3019eac3 1409
e142c38c 1410static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1411
e142c38c
DJ
1412static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1413 struct dwarf2_cu *);
c906108c 1414
7d45c7c3 1415static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
dda83cd7 1416 struct dwarf2_cu *cu);
7d45c7c3 1417
a084a2a6
AT
1418static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1419
05cf31d1 1420static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
dda83cd7 1421 struct dwarf2_cu *cu);
05cf31d1 1422
e142c38c 1423static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1424
e142c38c 1425static struct die_info *die_specification (struct die_info *die,
f2f0e013 1426 struct dwarf2_cu **);
63d06c5c 1427
9c541725 1428static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1429 struct dwarf2_cu *cu);
debd256d 1430
f3f5162e 1431static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1432 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1433 CORE_ADDR, int decode_mapping);
c906108c 1434
804d2729
TT
1435static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1436 const char *);
c906108c 1437
a14ed312 1438static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1439 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1440
ff39bb5e 1441static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1442 struct dwarf2_cu *);
c906108c 1443
ff39bb5e 1444static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1445 struct type *type,
1446 const char *name,
1447 struct obstack *obstack,
12df843f 1448 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1449 const gdb_byte **bytes,
98bfdba5 1450 struct dwarf2_locexpr_baton **baton);
2df3850c 1451
57567375
TT
1452static struct type *read_subrange_index_type (struct die_info *die,
1453 struct dwarf2_cu *cu);
1454
e7c27a73 1455static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1456
b4ba55a1
JB
1457static int need_gnat_info (struct dwarf2_cu *);
1458
3e43a32a
MS
1459static struct type *die_descriptive_type (struct die_info *,
1460 struct dwarf2_cu *);
b4ba55a1
JB
1461
1462static void set_descriptive_type (struct type *, struct die_info *,
1463 struct dwarf2_cu *);
1464
e7c27a73
DJ
1465static struct type *die_containing_type (struct die_info *,
1466 struct dwarf2_cu *);
c906108c 1467
ff39bb5e 1468static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1469 struct dwarf2_cu *);
c906108c 1470
f792889a 1471static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1472
673bfd45
DE
1473static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1474
0d5cff50 1475static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1476
6e70227d 1477static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1478 const char *suffix, int physname,
1479 struct dwarf2_cu *cu);
63d06c5c 1480
e7c27a73 1481static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1482
348e048f
DE
1483static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1484
e7c27a73 1485static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1486
e7c27a73 1487static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1488
96408a79
SA
1489static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1490
71a3c369
TT
1491static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1492
41144253 1493/* Return the .debug_loclists section to use for cu. */
1494static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1495
d0ce17d8
CT
1496/* Return the .debug_rnglists section to use for cu. */
1497static struct dwarf2_section_info *cu_debug_rnglists_section
1498 (struct dwarf2_cu *cu, dwarf_tag tag);
1499
3a2b436a 1500/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1501 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1502enum pc_bounds_kind
1503{
e385593e 1504 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1505 PC_BOUNDS_NOT_PRESENT,
1506
e385593e
JK
1507 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1508 were present but they do not form a valid range of PC addresses. */
1509 PC_BOUNDS_INVALID,
1510
3a2b436a
JK
1511 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1512 PC_BOUNDS_RANGES,
1513
1514 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1515 PC_BOUNDS_HIGH_LOW,
1516};
1517
1518static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1519 CORE_ADDR *, CORE_ADDR *,
1520 struct dwarf2_cu *,
891813be 1521 dwarf2_psymtab *);
c906108c 1522
fae299cd
DC
1523static void get_scope_pc_bounds (struct die_info *,
1524 CORE_ADDR *, CORE_ADDR *,
1525 struct dwarf2_cu *);
1526
801e3a5b 1527static void dwarf2_record_block_ranges (struct die_info *, struct block *,
dda83cd7 1528 CORE_ADDR, struct dwarf2_cu *);
801e3a5b 1529
a14ed312 1530static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1531 struct dwarf2_cu *);
c906108c 1532
a14ed312 1533static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1534 struct type *, struct dwarf2_cu *);
c906108c 1535
a14ed312 1536static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1537 struct die_info *, struct type *,
e7c27a73 1538 struct dwarf2_cu *);
c906108c 1539
a14ed312 1540static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1541 struct type *,
1542 struct dwarf2_cu *);
c906108c 1543
134d01f1 1544static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1545
e7c27a73 1546static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1547
e7c27a73 1548static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1549
5d7cb8df
JK
1550static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1551
804d2729 1552static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1553
27aa8d6a
SW
1554static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1555
74921315
KS
1556static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1557
f55ee35c
JK
1558static struct type *read_module_type (struct die_info *die,
1559 struct dwarf2_cu *cu);
1560
38d518c9 1561static const char *namespace_name (struct die_info *die,
e142c38c 1562 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1563
134d01f1 1564static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1565
7d79de9a
TT
1566static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1567 bool * = nullptr);
c906108c 1568
6e70227d 1569static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1570 struct dwarf2_cu *);
1571
bf6af496 1572static struct die_info *read_die_and_siblings_1
d521ce57 1573 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1574 struct die_info *);
639d11d3 1575
dee91e82 1576static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1577 const gdb_byte *info_ptr,
1578 const gdb_byte **new_info_ptr,
639d11d3
DC
1579 struct die_info *parent);
1580
d521ce57
TT
1581static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1582 struct die_info **, const gdb_byte *,
3e225074 1583 int);
3019eac3 1584
d521ce57 1585static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1586 struct die_info **, const gdb_byte *);
93311388 1587
e7c27a73 1588static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1589
15d034d0 1590static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1591 struct objfile *);
71c25dea 1592
15d034d0 1593static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1594
15d034d0 1595static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1596 struct die_info *die,
1597 struct dwarf2_cu *cu);
1598
ca69b9e6
DE
1599static const char *dwarf2_physname (const char *name, struct die_info *die,
1600 struct dwarf2_cu *cu);
1601
e142c38c 1602static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1603 struct dwarf2_cu **);
9219021c 1604
d97bc12b
DE
1605static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1606
1607static void dump_die_for_error (struct die_info *);
1608
1609static void dump_die_1 (struct ui_file *, int level, int max_level,
1610 struct die_info *);
c906108c 1611
d97bc12b 1612/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1613
51545339 1614static void store_in_ref_table (struct die_info *,
10b3939b 1615 struct dwarf2_cu *);
c906108c 1616
348e048f 1617static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1618 const struct attribute *,
348e048f
DE
1619 struct dwarf2_cu **);
1620
10b3939b 1621static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1622 const struct attribute *,
f2f0e013 1623 struct dwarf2_cu **);
c906108c 1624
348e048f 1625static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1626 const struct attribute *,
348e048f
DE
1627 struct dwarf2_cu **);
1628
ac9ec31b
DE
1629static struct type *get_signatured_type (struct die_info *, ULONGEST,
1630 struct dwarf2_cu *);
1631
1632static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1633 const struct attribute *,
ac9ec31b
DE
1634 struct dwarf2_cu *);
1635
ab432490
SM
1636static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1637 dwarf2_per_objfile *per_objfile);
348e048f 1638
ab432490
SM
1639static void read_signatured_type (signatured_type *sig_type,
1640 dwarf2_per_objfile *per_objfile);
348e048f 1641
63e43d3a
PMR
1642static int attr_to_dynamic_prop (const struct attribute *attr,
1643 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1644 struct dynamic_prop *prop, struct type *type);
63e43d3a 1645
c906108c
SS
1646/* memory allocation interface */
1647
7b5a2f43 1648static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1649
b60c80d6 1650static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1651
43f3e411 1652static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1653
8cf6f0b1
TT
1654static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1655 struct dwarf2_loclist_baton *baton,
ff39bb5e 1656 const struct attribute *attr);
8cf6f0b1 1657
ff39bb5e 1658static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1659 struct symbol *sym,
f1e6e072
TT
1660 struct dwarf2_cu *cu,
1661 int is_block);
4c2df51b 1662
d521ce57
TT
1663static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1664 const gdb_byte *info_ptr,
7c32eebb 1665 const struct abbrev_info *abbrev);
4bb7a0a7 1666
72bf9492
DJ
1667static hashval_t partial_die_hash (const void *item);
1668
1669static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1670
ae038cb0 1671static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1672 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1673 dwarf2_per_objfile *per_objfile);
ae038cb0 1674
9816fde3 1675static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1676 struct die_info *comp_unit_die,
1677 enum language pretend_language);
93311388 1678
f792889a 1679static struct type *set_die_type (struct die_info *, struct type *,
57567375 1680 struct dwarf2_cu *, bool = false);
1c379e20 1681
976ca316 1682static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1683
ab432490
SM
1684static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1685 dwarf2_per_objfile *per_objfile,
4a636814 1686 dwarf2_cu *existing_cu,
ab432490
SM
1687 bool skip_partial,
1688 enum language pretend_language);
10b3939b 1689
8fc0b21d 1690static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1691 enum language pretend_language);
10b3939b 1692
8fc0b21d 1693static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1694 enum language pretend_language);
f4dc4d17 1695
10b3939b
DJ
1696static void dwarf2_add_dependence (struct dwarf2_cu *,
1697 struct dwarf2_per_cu_data *);
1698
ae038cb0
DJ
1699static void dwarf2_mark (struct dwarf2_cu *);
1700
b64f50a1 1701static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1702 dwarf2_per_cu_data *per_cu,
1703 dwarf2_per_objfile *per_objfile);
673bfd45 1704
f792889a 1705static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1706
120ce1b5
SM
1707static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1708 dwarf2_per_objfile *per_objfile,
95554aad
TT
1709 enum language pretend_language);
1710
976ca316 1711static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1712
b303c6f6
AB
1713/* Class, the destructor of which frees all allocated queue entries. This
1714 will only have work to do if an error was thrown while processing the
1715 dwarf. If no error was thrown then the queue entries should have all
1716 been processed, and freed, as we went along. */
1717
1718class dwarf2_queue_guard
1719{
1720public:
39856def
TT
1721 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1722 : m_per_objfile (per_objfile)
1723 {
08ac5771
SM
1724 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1725
1726 m_per_objfile->per_bfd->queue.emplace ();
39856def 1727 }
b303c6f6
AB
1728
1729 /* Free any entries remaining on the queue. There should only be
1730 entries left if we hit an error while processing the dwarf. */
1731 ~dwarf2_queue_guard ()
1732 {
08ac5771
SM
1733 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1734
1735 m_per_objfile->per_bfd->queue.reset ();
39856def 1736 }
b303c6f6 1737
39856def 1738 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1739
39856def
TT
1740private:
1741 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1742};
1743
39856def
TT
1744dwarf2_queue_item::~dwarf2_queue_item ()
1745{
1746 /* Anything still marked queued is likely to be in an
1747 inconsistent state, so discard it. */
1748 if (per_cu->queued)
1749 {
7188ed02 1750 per_objfile->remove_cu (per_cu);
39856def
TT
1751 per_cu->queued = 0;
1752 }
1753}
1754
d721ba37
PA
1755/* The return type of find_file_and_directory. Note, the enclosed
1756 string pointers are only valid while this object is valid. */
1757
1758struct file_and_directory
1759{
1760 /* The filename. This is never NULL. */
1761 const char *name;
1762
1763 /* The compilation directory. NULL if not known. If we needed to
1764 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1765 points directly to the DW_AT_comp_dir string attribute owned by
1766 the obstack that owns the DIE. */
1767 const char *comp_dir;
1768
1769 /* If we needed to build a new string for comp_dir, this is what
1770 owns the storage. */
1771 std::string comp_dir_storage;
1772};
1773
1774static file_and_directory find_file_and_directory (struct die_info *die,
1775 struct dwarf2_cu *cu);
9291a0cd 1776
298e9637 1777static htab_up allocate_signatured_type_table ();
1fd400ff 1778
298e9637 1779static htab_up allocate_dwo_unit_table ();
3019eac3 1780
57d63ce2 1781static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1782 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1783 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1784
976ca316 1785static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1786
3019eac3 1787static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1788 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1789 ULONGEST signature);
3019eac3
DE
1790
1791static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1792 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1793
1b555f17 1794static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1795
263db9a1
TT
1796/* A unique pointer to a dwo_file. */
1797
51ac9db5 1798typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1799
976ca316 1800static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1801
1b80a9fa 1802static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1803
1804static void free_line_header_voidp (void *arg);
4390d890
DE
1805\f
1806/* Various complaints about symbol reading that don't abort the process. */
1807
4390d890
DE
1808static void
1809dwarf2_debug_line_missing_file_complaint (void)
1810{
b98664d3 1811 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1812}
1813
1814static void
1815dwarf2_debug_line_missing_end_sequence_complaint (void)
1816{
b98664d3 1817 complaint (_(".debug_line section has line "
4390d890
DE
1818 "program sequence without an end"));
1819}
1820
1821static void
1822dwarf2_complex_location_expr_complaint (void)
1823{
b98664d3 1824 complaint (_("location expression too complex"));
4390d890
DE
1825}
1826
1827static void
1828dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1829 int arg3)
1830{
b98664d3 1831 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1832 arg1, arg2, arg3);
1833}
1834
4390d890
DE
1835static void
1836dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1837{
b98664d3 1838 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1839 arg1, arg2);
1840}
527f3840
JK
1841
1842/* Hash function for line_header_hash. */
1843
1844static hashval_t
1845line_header_hash (const struct line_header *ofs)
1846{
9c541725 1847 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1848}
1849
1850/* Hash function for htab_create_alloc_ex for line_header_hash. */
1851
1852static hashval_t
1853line_header_hash_voidp (const void *item)
1854{
9a3c8263 1855 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1856
1857 return line_header_hash (ofs);
1858}
1859
1860/* Equality function for line_header_hash. */
1861
1862static int
1863line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1864{
9a3c8263
SM
1865 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1866 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1867
9c541725 1868 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1869 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1870}
1871
4390d890 1872\f
9291a0cd 1873
330cdd98
PA
1874/* See declaration. */
1875
5989a64e
SM
1876dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1877 bool can_copy_)
c3699833
SM
1878 : obfd (obfd),
1879 can_copy (can_copy_)
330cdd98
PA
1880{
1881 if (names == NULL)
1882 names = &dwarf2_elf_names;
1883
330cdd98
PA
1884 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1885 locate_sections (obfd, sec, *names);
1886}
1887
5989a64e 1888dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1889{
0d305d5c 1890 for (auto &per_cu : all_comp_units)
ae640021 1891 per_cu->imported_symtabs_free ();
fc8e7e75 1892
5989a64e 1893 /* Everything else should be on this->obstack. */
330cdd98
PA
1894}
1895
7188ed02 1896/* See read.h. */
330cdd98
PA
1897
1898void
7188ed02 1899dwarf2_per_objfile::remove_all_cus ()
330cdd98 1900{
08ac5771
SM
1901 gdb_assert (!this->per_bfd->queue.has_value ());
1902
7188ed02
SM
1903 for (auto pair : m_dwarf2_cus)
1904 delete pair.second;
330cdd98 1905
7188ed02 1906 m_dwarf2_cus.clear ();
330cdd98
PA
1907}
1908
11ed8cad
TT
1909/* A helper class that calls free_cached_comp_units on
1910 destruction. */
1911
1912class free_cached_comp_units
1913{
1914public:
1915
1916 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1917 : m_per_objfile (per_objfile)
1918 {
1919 }
1920
1921 ~free_cached_comp_units ()
1922 {
7188ed02 1923 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1924 }
1925
1926 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1927
1928private:
1929
1930 dwarf2_per_objfile *m_per_objfile;
1931};
1932
af758d11
SM
1933/* See read.h. */
1934
1935bool
1936dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1937{
1938 gdb_assert (per_cu->index < this->m_symtabs.size ());
1939
1940 return this->m_symtabs[per_cu->index] != nullptr;
1941}
1942
1943/* See read.h. */
1944
1945compunit_symtab *
1946dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1947{
1948 gdb_assert (per_cu->index < this->m_symtabs.size ());
1949
1950 return this->m_symtabs[per_cu->index];
1951}
1952
1953/* See read.h. */
1954
1955void
1956dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1957 compunit_symtab *symtab)
1958{
1959 gdb_assert (per_cu->index < this->m_symtabs.size ());
1960 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1961
1962 this->m_symtabs[per_cu->index] = symtab;
1963}
1964
c906108c 1965/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1966 information and return true if we have enough to do something.
1967 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1968 ELF names are used. CAN_COPY is true for formats where symbol
1969 interposition is possible and so symbol values must follow copy
1970 relocation rules. */
c906108c
SS
1971
1972int
251d32d9 1973dwarf2_has_info (struct objfile *objfile,
dda83cd7 1974 const struct dwarf2_debug_sections *names,
4b610737 1975 bool can_copy)
c906108c 1976{
97cbe998
SDJ
1977 if (objfile->flags & OBJF_READNEVER)
1978 return 0;
1979
976ca316 1980 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1981
976ca316 1982 if (per_objfile == NULL)
5989a64e 1983 {
17ee85fc
TT
1984 dwarf2_per_bfd *per_bfd;
1985
8a91fbdf
SM
1986 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1987 BFD doesn't require relocations.
1988
1989 We don't share with objfiles for which -readnow was requested,
1990 because it would complicate things when loading the same BFD with
1991 -readnow and then without -readnow. */
1992 if (!gdb_bfd_requires_relocations (objfile->obfd)
1993 && (objfile->flags & OBJF_READNOW) == 0)
17ee85fc
TT
1994 {
1995 /* See if one has been created for this BFD yet. */
1996 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1997
1998 if (per_bfd == nullptr)
1999 {
2000 /* No, create it now. */
2001 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
2002 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
2003 }
2004 }
2005 else
2006 {
2007 /* No sharing possible, create one specifically for this objfile. */
2008 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
2009 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
2010 }
5989a64e 2011
976ca316 2012 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 2013 }
5bfd760d 2014
976ca316
SM
2015 return (!per_objfile->per_bfd->info.is_virtual
2016 && per_objfile->per_bfd->info.s.section != NULL
2017 && !per_objfile->per_bfd->abbrev.is_virtual
2018 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
2019}
2020
330cdd98 2021/* See declaration. */
c906108c 2022
330cdd98 2023void
5989a64e
SM
2024dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
2025 const dwarf2_debug_sections &names)
c906108c 2026{
fd361982 2027 flagword aflag = bfd_section_flags (sectp);
251d32d9 2028
dc7650b8
JK
2029 if ((aflag & SEC_HAS_CONTENTS) == 0)
2030 {
2031 }
950b7495
KS
2032 else if (elf_section_data (sectp)->this_hdr.sh_size
2033 > bfd_get_file_size (abfd))
2034 {
2035 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2036 warning (_("Discarding section %s which has a section size (%s"
2037 ") larger than the file size [in module %s]"),
2038 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2039 bfd_get_filename (abfd));
2040 }
fbedd546 2041 else if (names.info.matches (sectp->name))
c906108c 2042 {
330cdd98 2043 this->info.s.section = sectp;
fd361982 2044 this->info.size = bfd_section_size (sectp);
c906108c 2045 }
fbedd546 2046 else if (names.abbrev.matches (sectp->name))
c906108c 2047 {
330cdd98 2048 this->abbrev.s.section = sectp;
fd361982 2049 this->abbrev.size = bfd_section_size (sectp);
c906108c 2050 }
fbedd546 2051 else if (names.line.matches (sectp->name))
c906108c 2052 {
330cdd98 2053 this->line.s.section = sectp;
fd361982 2054 this->line.size = bfd_section_size (sectp);
c906108c 2055 }
fbedd546 2056 else if (names.loc.matches (sectp->name))
c906108c 2057 {
330cdd98 2058 this->loc.s.section = sectp;
fd361982 2059 this->loc.size = bfd_section_size (sectp);
c906108c 2060 }
fbedd546 2061 else if (names.loclists.matches (sectp->name))
43988095 2062 {
330cdd98 2063 this->loclists.s.section = sectp;
fd361982 2064 this->loclists.size = bfd_section_size (sectp);
43988095 2065 }
fbedd546 2066 else if (names.macinfo.matches (sectp->name))
c906108c 2067 {
330cdd98 2068 this->macinfo.s.section = sectp;
fd361982 2069 this->macinfo.size = bfd_section_size (sectp);
c906108c 2070 }
fbedd546 2071 else if (names.macro.matches (sectp->name))
cf2c3c16 2072 {
330cdd98 2073 this->macro.s.section = sectp;
fd361982 2074 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2075 }
fbedd546 2076 else if (names.str.matches (sectp->name))
c906108c 2077 {
330cdd98 2078 this->str.s.section = sectp;
fd361982 2079 this->str.size = bfd_section_size (sectp);
c906108c 2080 }
fbedd546 2081 else if (names.str_offsets.matches (sectp->name))
18a8505e
AT
2082 {
2083 this->str_offsets.s.section = sectp;
2084 this->str_offsets.size = bfd_section_size (sectp);
2085 }
fbedd546 2086 else if (names.line_str.matches (sectp->name))
43988095 2087 {
330cdd98 2088 this->line_str.s.section = sectp;
fd361982 2089 this->line_str.size = bfd_section_size (sectp);
43988095 2090 }
fbedd546 2091 else if (names.addr.matches (sectp->name))
3019eac3 2092 {
330cdd98 2093 this->addr.s.section = sectp;
fd361982 2094 this->addr.size = bfd_section_size (sectp);
3019eac3 2095 }
fbedd546 2096 else if (names.frame.matches (sectp->name))
b6af0555 2097 {
330cdd98 2098 this->frame.s.section = sectp;
fd361982 2099 this->frame.size = bfd_section_size (sectp);
b6af0555 2100 }
fbedd546 2101 else if (names.eh_frame.matches (sectp->name))
b6af0555 2102 {
330cdd98 2103 this->eh_frame.s.section = sectp;
fd361982 2104 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2105 }
fbedd546 2106 else if (names.ranges.matches (sectp->name))
af34e669 2107 {
330cdd98 2108 this->ranges.s.section = sectp;
fd361982 2109 this->ranges.size = bfd_section_size (sectp);
af34e669 2110 }
fbedd546 2111 else if (names.rnglists.matches (sectp->name))
43988095 2112 {
330cdd98 2113 this->rnglists.s.section = sectp;
fd361982 2114 this->rnglists.size = bfd_section_size (sectp);
43988095 2115 }
fbedd546 2116 else if (names.types.matches (sectp->name))
348e048f 2117 {
8b70b953
TT
2118 struct dwarf2_section_info type_section;
2119
2120 memset (&type_section, 0, sizeof (type_section));
049412e3 2121 type_section.s.section = sectp;
fd361982 2122 type_section.size = bfd_section_size (sectp);
8b70b953 2123
fd5866f6 2124 this->types.push_back (type_section);
348e048f 2125 }
fbedd546 2126 else if (names.gdb_index.matches (sectp->name))
9291a0cd 2127 {
330cdd98 2128 this->gdb_index.s.section = sectp;
fd361982 2129 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2130 }
fbedd546 2131 else if (names.debug_names.matches (sectp->name))
927aa2e7
JK
2132 {
2133 this->debug_names.s.section = sectp;
fd361982 2134 this->debug_names.size = bfd_section_size (sectp);
927aa2e7 2135 }
fbedd546 2136 else if (names.debug_aranges.matches (sectp->name))
927aa2e7
JK
2137 {
2138 this->debug_aranges.s.section = sectp;
fd361982 2139 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2140 }
dce234bc 2141
fd361982
AM
2142 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2143 && bfd_section_vma (sectp) == 0)
330cdd98 2144 this->has_section_at_zero = true;
c906108c
SS
2145}
2146
dce234bc 2147/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2148 SECTION_NAME. */
af34e669 2149
dce234bc 2150void
3017a003 2151dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
2152 enum dwarf2_section_enum sect,
2153 asection **sectp, const gdb_byte **bufp,
2154 bfd_size_type *sizep)
dce234bc 2155{
976ca316 2156 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2157 struct dwarf2_section_info *info;
a3b2a86b
TT
2158
2159 /* We may see an objfile without any DWARF, in which case we just
2160 return nothing. */
976ca316 2161 if (per_objfile == NULL)
a3b2a86b
TT
2162 {
2163 *sectp = NULL;
2164 *bufp = NULL;
2165 *sizep = 0;
2166 return;
2167 }
3017a003
TG
2168 switch (sect)
2169 {
2170 case DWARF2_DEBUG_FRAME:
976ca316 2171 info = &per_objfile->per_bfd->frame;
3017a003
TG
2172 break;
2173 case DWARF2_EH_FRAME:
976ca316 2174 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2175 break;
2176 default:
2177 gdb_assert_not_reached ("unexpected section");
2178 }
dce234bc 2179
96b79293 2180 info->read (objfile);
dce234bc 2181
96b79293 2182 *sectp = info->get_bfd_section ();
dce234bc
PP
2183 *bufp = info->buffer;
2184 *sizep = info->size;
2185}
2186
9291a0cd 2187\f
39298a5d 2188/* DWARF quick_symbol_functions support. */
7b9f3c50
DE
2189
2190/* TUs can share .debug_line entries, and there can be a lot more TUs than
2191 unique line tables, so we maintain a separate table of all .debug_line
2192 derived entries to support the sharing.
2193 All the quick functions need is the list of file names. We discard the
2194 line_header when we're done and don't need to record it here. */
2195struct quick_file_names
2196{
094b34ac
DE
2197 /* The data used to construct the hash key. */
2198 struct stmt_list_hash hash;
7b9f3c50
DE
2199
2200 /* The number of entries in file_names, real_names. */
2201 unsigned int num_file_names;
2202
2203 /* The file names from the line table, after being run through
2204 file_full_name. */
2205 const char **file_names;
2206
2207 /* The file names from the line table after being run through
2208 gdb_realpath. These are computed lazily. */
2209 const char **real_names;
2210};
2211
2212/* When using the index (and thus not using psymtabs), each CU has an
2213 object of this type. This is used to hold information needed by
2214 the various "quick" methods. */
2215struct dwarf2_per_cu_quick_data
2216{
2217 /* The file table. This can be NULL if there was no file table
2218 or it's currently not read in.
5989a64e 2219 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2220 struct quick_file_names *file_names;
2221
7b9f3c50
DE
2222 /* A temporary mark bit used when iterating over all CUs in
2223 expand_symtabs_matching. */
2224 unsigned int mark : 1;
2225
2226 /* True if we've tried to read the file table and found there isn't one.
2227 There will be no point in trying to read it again next time. */
2228 unsigned int no_file_data : 1;
2229};
2230
edc02ceb
TT
2231/* A subclass of psymbol_functions that arranges to read the DWARF
2232 partial symbols when needed. */
2233struct lazy_dwarf_reader : public psymbol_functions
2234{
2235 using psymbol_functions::psymbol_functions;
2236
2237 bool can_lazily_read_symbols () override
2238 {
2239 return true;
2240 }
2241
2242 void read_partial_symbols (struct objfile *objfile) override
2243 {
2244 if (dwarf2_has_info (objfile, nullptr))
2245 dwarf2_build_psymtabs (objfile, this);
2246 }
2247};
2248
2249static quick_symbol_functions_up
2250make_lazy_dwarf_reader ()
2251{
2252 return quick_symbol_functions_up (new lazy_dwarf_reader);
2253}
2254
39298a5d
TT
2255struct dwarf2_base_index_functions : public quick_symbol_functions
2256{
2257 bool has_symbols (struct objfile *objfile) override;
2258
2259 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2260
2261 void forget_cached_source_info (struct objfile *objfile) override;
2262
39298a5d
TT
2263 enum language lookup_global_symbol_language (struct objfile *objfile,
2264 const char *name,
2265 domain_enum domain,
2266 bool *symbol_found_p) override
2267 {
2268 *symbol_found_p = false;
2269 return language_unknown;
2270 }
2271
4829711b 2272 void print_stats (struct objfile *objfile, bool print_bcache) override;
39298a5d
TT
2273
2274 void expand_all_symtabs (struct objfile *objfile) override;
2275
39298a5d
TT
2276 struct compunit_symtab *find_pc_sect_compunit_symtab
2277 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2278 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2279
2280 struct compunit_symtab *find_compunit_symtab_by_address
2281 (struct objfile *objfile, CORE_ADDR address) override
2282 {
2283 return nullptr;
2284 }
2285
2286 void map_symbol_filenames (struct objfile *objfile,
f4655dee
TT
2287 gdb::function_view<symbol_filename_ftype> fun,
2288 bool need_fullname) override;
39298a5d
TT
2289};
2290
2291struct dwarf2_gdb_index : public dwarf2_base_index_functions
2292{
39298a5d
TT
2293 void dump (struct objfile *objfile) override;
2294
0b7b2c2a 2295 void expand_matching_symbols
39298a5d
TT
2296 (struct objfile *,
2297 const lookup_name_info &lookup_name,
2298 domain_enum domain,
2299 int global,
39298a5d
TT
2300 symbol_compare_ftype *ordered_compare) override;
2301
df35e626 2302 bool expand_symtabs_matching
39298a5d
TT
2303 (struct objfile *objfile,
2304 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2305 const lookup_name_info *lookup_name,
2306 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2307 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2308 block_search_flags search_flags,
3bfa51a7 2309 domain_enum domain,
39298a5d
TT
2310 enum search_domain kind) override;
2311};
2312
2313struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2314{
39298a5d
TT
2315 void dump (struct objfile *objfile) override;
2316
0b7b2c2a 2317 void expand_matching_symbols
39298a5d
TT
2318 (struct objfile *,
2319 const lookup_name_info &lookup_name,
2320 domain_enum domain,
2321 int global,
39298a5d
TT
2322 symbol_compare_ftype *ordered_compare) override;
2323
df35e626 2324 bool expand_symtabs_matching
39298a5d
TT
2325 (struct objfile *objfile,
2326 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2327 const lookup_name_info *lookup_name,
2328 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2329 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2330 block_search_flags search_flags,
3bfa51a7 2331 domain_enum domain,
39298a5d
TT
2332 enum search_domain kind) override;
2333};
2334
edc02ceb 2335static quick_symbol_functions_up
39298a5d
TT
2336make_dwarf_gdb_index ()
2337{
2338 return quick_symbol_functions_up (new dwarf2_gdb_index);
2339}
2340
edc02ceb 2341static quick_symbol_functions_up
39298a5d
TT
2342make_dwarf_debug_names ()
2343{
2344 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2345}
2346
094b34ac
DE
2347/* Utility hash function for a stmt_list_hash. */
2348
2349static hashval_t
2350hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2351{
2352 hashval_t v = 0;
2353
2354 if (stmt_list_hash->dwo_unit != NULL)
2355 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2356 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2357 return v;
2358}
2359
2360/* Utility equality function for a stmt_list_hash. */
2361
2362static int
2363eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2364 const struct stmt_list_hash *rhs)
2365{
2366 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2367 return 0;
2368 if (lhs->dwo_unit != NULL
2369 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2370 return 0;
2371
9c541725 2372 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2373}
2374
7b9f3c50
DE
2375/* Hash function for a quick_file_names. */
2376
2377static hashval_t
2378hash_file_name_entry (const void *e)
2379{
9a3c8263
SM
2380 const struct quick_file_names *file_data
2381 = (const struct quick_file_names *) e;
7b9f3c50 2382
094b34ac 2383 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2384}
2385
2386/* Equality function for a quick_file_names. */
2387
2388static int
2389eq_file_name_entry (const void *a, const void *b)
2390{
9a3c8263
SM
2391 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2392 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2393
094b34ac 2394 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2395}
2396
2397/* Delete function for a quick_file_names. */
2398
2399static void
2400delete_file_name_entry (void *e)
2401{
9a3c8263 2402 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2403 int i;
2404
2405 for (i = 0; i < file_data->num_file_names; ++i)
2406 {
2407 xfree ((void*) file_data->file_names[i]);
2408 if (file_data->real_names)
2409 xfree ((void*) file_data->real_names[i]);
2410 }
2411
45940949
TT
2412 /* The space for the struct itself lives on the obstack, so we don't
2413 free it here. */
7b9f3c50
DE
2414}
2415
2416/* Create a quick_file_names hash table. */
2417
5895093f 2418static htab_up
7b9f3c50
DE
2419create_quick_file_names_table (unsigned int nr_initial_entries)
2420{
5895093f
TT
2421 return htab_up (htab_create_alloc (nr_initial_entries,
2422 hash_file_name_entry, eq_file_name_entry,
2423 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2424}
9291a0cd 2425
ab432490
SM
2426/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2427 function is unrelated to symtabs, symtab would have to be created afterwards.
2428 You should call age_cached_comp_units after processing the CU. */
918dd910 2429
1b555f17 2430static dwarf2_cu *
ab432490
SM
2431load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2432 bool skip_partial)
918dd910 2433{
3019eac3 2434 if (per_cu->is_debug_types)
ab432490 2435 load_full_type_unit (per_cu, per_objfile);
918dd910 2436 else
4a636814
SM
2437 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2438 skip_partial, language_minimal);
918dd910 2439
7188ed02
SM
2440 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2441 if (cu == nullptr)
1b555f17 2442 return nullptr; /* Dummy CU. */
2dc860c0 2443
7188ed02 2444 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2445
7188ed02 2446 return cu;
918dd910
JK
2447}
2448
1350c3b4 2449/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2450
9291a0cd 2451static void
97a1449a 2452dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2453 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2454{
f4dc4d17
DE
2455 /* Skip type_unit_groups, reading the type units they contain
2456 is handled elsewhere. */
197400e8 2457 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2458 return;
2459
08ac5771
SM
2460 {
2461 /* The destructor of dwarf2_queue_guard frees any entries left on
2462 the queue. After this point we're guaranteed to leave this function
2463 with the dwarf queue empty. */
2464 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2465
08ac5771
SM
2466 if (!per_objfile->symtab_set_p (per_cu))
2467 {
2468 queue_comp_unit (per_cu, per_objfile, language_minimal);
2469 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2470
2471 /* If we just loaded a CU from a DWO, and we're working with an index
2472 that may badly handle TUs, load all the TUs in that DWO as well.
2473 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2474 if (!per_cu->is_debug_types
2475 && cu != NULL
2476 && cu->dwo_unit != NULL
2477 && per_objfile->per_bfd->index_table != NULL
2478 && per_objfile->per_bfd->index_table->version <= 7
2479 /* DWP files aren't supported yet. */
2480 && get_dwp_file (per_objfile) == NULL)
2481 queue_and_load_all_dwo_tus (cu);
2482 }
9291a0cd 2483
08ac5771
SM
2484 process_queue (per_objfile);
2485 }
9291a0cd
TT
2486
2487 /* Age the cache, releasing compilation units that have not
2488 been used recently. */
976ca316 2489 per_objfile->age_comp_units ();
9291a0cd
TT
2490}
2491
97a1449a
SM
2492/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2493 the per-objfile for which this symtab is instantiated.
2494
2495 Returns the resulting symbol table. */
2fdf6df6 2496
43f3e411 2497static struct compunit_symtab *
97a1449a 2498dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2499 dwarf2_per_objfile *per_objfile,
97a1449a 2500 bool skip_partial)
9291a0cd 2501{
976ca316 2502 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2503
976ca316 2504 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2505 {
976ca316 2506 free_cached_comp_units freer (per_objfile);
c83dd867 2507 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2508 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2509 process_cu_includes (per_objfile);
9291a0cd 2510 }
f194fefb 2511
976ca316 2512 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2513}
2514
d3473f0c
TT
2515/* See read.h. */
2516
0d305d5c 2517std::unique_ptr<dwarf2_per_cu_data>
5989a64e 2518dwarf2_per_bfd::allocate_per_cu ()
d3473f0c 2519{
0d305d5c 2520 std::unique_ptr<dwarf2_per_cu_data> result (new dwarf2_per_cu_data);
1859c670 2521 result->per_bfd = this;
d3473f0c
TT
2522 result->index = m_num_psymtabs++;
2523 return result;
2524}
2525
2526/* See read.h. */
2527
0d305d5c 2528std::unique_ptr<signatured_type>
5989a64e 2529dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c 2530{
0d305d5c
TT
2531 std::unique_ptr<signatured_type> result (new signatured_type);
2532 result->per_bfd = this;
2533 result->index = m_num_psymtabs++;
91eea9cc 2534 tu_stats.nr_tus++;
d3473f0c
TT
2535 return result;
2536}
2537
168c9250 2538/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2539 obstack, and constructed with the specified field values. */
4b514bc8 2540
0d305d5c 2541static std::unique_ptr<dwarf2_per_cu_data>
168c9250
SM
2542create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2543 struct dwarf2_section_info *section,
2544 int is_dwz,
2545 sect_offset sect_off, ULONGEST length)
4b514bc8 2546{
0d305d5c 2547 std::unique_ptr<dwarf2_per_cu_data> the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2548 the_cu->sect_off = sect_off;
2549 the_cu->length = length;
4b514bc8 2550 the_cu->section = section;
168c9250 2551 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2552 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2553 the_cu->is_dwz = is_dwz;
2554 return the_cu;
2555}
2556
2ec9a5e0
TT
2557/* A helper for create_cus_from_index that handles a given list of
2558 CUs. */
2fdf6df6 2559
74a0d9f6 2560static void
168c9250 2561create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2562 const gdb_byte *cu_list, offset_type n_elements,
2563 struct dwarf2_section_info *section,
b76e467d 2564 int is_dwz)
9291a0cd 2565{
12359b5e 2566 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2567 {
74a0d9f6 2568 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2569
2570 sect_offset sect_off
2571 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2572 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2573 cu_list += 2 * 8;
2574
0d305d5c 2575 std::unique_ptr<dwarf2_per_cu_data> per_cu
168c9250
SM
2576 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2577 length);
0d305d5c 2578 per_bfd->all_comp_units.push_back (std::move (per_cu));
9291a0cd 2579 }
9291a0cd
TT
2580}
2581
2ec9a5e0 2582/* Read the CU list from the mapped index, and use it to create all
168c9250 2583 the CU objects for PER_BFD. */
2ec9a5e0 2584
74a0d9f6 2585static void
168c9250 2586create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2587 const gdb_byte *cu_list, offset_type cu_list_elements,
2588 const gdb_byte *dwz_list, offset_type dwz_elements)
2589{
168c9250
SM
2590 gdb_assert (per_bfd->all_comp_units.empty ());
2591 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2592
168c9250
SM
2593 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2594 &per_bfd->info, 0);
2ec9a5e0
TT
2595
2596 if (dwz_elements == 0)
74a0d9f6 2597 return;
2ec9a5e0 2598
168c9250
SM
2599 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2600 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2601 &dwz->info, 1);
2ec9a5e0
TT
2602}
2603
1fd400ff 2604/* Create the signatured type hash table from the index. */
673bfd45 2605
74a0d9f6 2606static void
12359b5e 2607create_signatured_type_table_from_index
168c9250
SM
2608 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2609 const gdb_byte *bytes, offset_type elements)
1fd400ff 2610{
298e9637 2611 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2612
12359b5e 2613 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2614 {
0d305d5c 2615 std::unique_ptr<signatured_type> sig_type;
9c541725 2616 ULONGEST signature;
1fd400ff 2617 void **slot;
9c541725 2618 cu_offset type_offset_in_tu;
1fd400ff 2619
74a0d9f6 2620 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2621 sect_offset sect_off
2622 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2623 type_offset_in_tu
2624 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2625 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2626 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2627 bytes += 3 * 8;
2628
168c9250 2629 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2630 sig_type->signature = signature;
9c541725 2631 sig_type->type_offset_in_tu = type_offset_in_tu;
0d305d5c
TT
2632 sig_type->is_debug_types = 1;
2633 sig_type->section = section;
2634 sig_type->sect_off = sect_off;
2635 sig_type->v.quick
168c9250 2636 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2637 struct dwarf2_per_cu_quick_data);
2638
0d305d5c
TT
2639 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2640 *slot = sig_type.get ();
1fd400ff 2641
91eea9cc 2642 per_bfd->all_comp_units.push_back (std::move (sig_type));
1fd400ff
TT
2643 }
2644
168c9250 2645 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2646}
2647
927aa2e7
JK
2648/* Create the signatured type hash table from .debug_names. */
2649
2650static void
2651create_signatured_type_table_from_debug_names
976ca316 2652 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2653 const mapped_debug_names &map,
2654 struct dwarf2_section_info *section,
2655 struct dwarf2_section_info *abbrev_section)
2656{
976ca316 2657 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2658
96b79293
TT
2659 section->read (objfile);
2660 abbrev_section->read (objfile);
927aa2e7 2661
298e9637 2662 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2663
2664 for (uint32_t i = 0; i < map.tu_count; ++i)
2665 {
0d305d5c 2666 std::unique_ptr<signatured_type> sig_type;
927aa2e7 2667 void **slot;
927aa2e7
JK
2668
2669 sect_offset sect_off
2670 = (sect_offset) (extract_unsigned_integer
2671 (map.tu_table_reordered + i * map.offset_size,
2672 map.offset_size,
2673 map.dwarf5_byte_order));
2674
2675 comp_unit_head cu_header;
976ca316 2676 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2677 abbrev_section,
927aa2e7
JK
2678 section->buffer + to_underlying (sect_off),
2679 rcuh_kind::TYPE);
2680
976ca316 2681 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2682 sig_type->signature = cu_header.signature;
2683 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
0d305d5c
TT
2684 sig_type->is_debug_types = 1;
2685 sig_type->section = section;
2686 sig_type->sect_off = sect_off;
2687 sig_type->v.quick
976ca316 2688 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2689 struct dwarf2_per_cu_quick_data);
2690
0d305d5c
TT
2691 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2692 *slot = sig_type.get ();
927aa2e7 2693
91eea9cc 2694 per_objfile->per_bfd->all_comp_units.push_back (std::move (sig_type));
927aa2e7
JK
2695 }
2696
976ca316 2697 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2698}
2699
9291a0cd 2700/* Read the address map data from the mapped index, and use it to
efd7398e 2701 populate the psymtabs_addrmap. */
2fdf6df6 2702
9291a0cd 2703static void
976ca316 2704create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2705 struct mapped_index *index)
9291a0cd 2706{
976ca316 2707 struct objfile *objfile = per_objfile->objfile;
efd7398e 2708 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
08feed99 2709 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2710 const gdb_byte *iter, *end;
9291a0cd 2711 struct addrmap *mutable_map;
9291a0cd
TT
2712 CORE_ADDR baseaddr;
2713
8268c778
PA
2714 auto_obstack temp_obstack;
2715
9291a0cd
TT
2716 mutable_map = addrmap_create_mutable (&temp_obstack);
2717
f00a2de2
PA
2718 iter = index->address_table.data ();
2719 end = iter + index->address_table.size ();
9291a0cd 2720
b3b3bada 2721 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2722
2723 while (iter < end)
2724 {
2725 ULONGEST hi, lo, cu_index;
2726 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2727 iter += 8;
2728 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2729 iter += 8;
2730 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2731 iter += 4;
f652bce2 2732
24a55014 2733 if (lo > hi)
f652bce2 2734 {
b98664d3 2735 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2736 hex_string (lo), hex_string (hi));
24a55014 2737 continue;
f652bce2 2738 }
24a55014 2739
efd7398e 2740 if (cu_index >= per_bfd->all_comp_units.size ())
f652bce2 2741 {
b98664d3 2742 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2743 (unsigned) cu_index);
24a55014 2744 continue;
f652bce2 2745 }
24a55014 2746
79748972
TT
2747 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2748 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2749 addrmap_set_empty (mutable_map, lo, hi - 1,
efd7398e 2750 per_bfd->get_cu (cu_index));
9291a0cd
TT
2751 }
2752
efd7398e
TT
2753 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2754 &per_bfd->obstack);
9291a0cd
TT
2755}
2756
927aa2e7 2757/* Read the address map data from DWARF-5 .debug_aranges, and use it to
efd7398e 2758 populate the psymtabs_addrmap. */
927aa2e7
JK
2759
2760static void
976ca316 2761create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2762 struct dwarf2_section_info *section)
2763{
976ca316 2764 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2765 bfd *abfd = objfile->obfd;
08feed99 2766 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2767 const CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e 2768 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
2769
2770 auto_obstack temp_obstack;
2771 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2772
2773 std::unordered_map<sect_offset,
2774 dwarf2_per_cu_data *,
2775 gdb::hash_enum<sect_offset>>
2776 debug_info_offset_to_per_cu;
0d305d5c 2777 for (const auto &per_cu : per_bfd->all_comp_units)
927aa2e7 2778 {
927aa2e7 2779 const auto insertpair
0d305d5c
TT
2780 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2781 per_cu.get ());
927aa2e7
JK
2782 if (!insertpair.second)
2783 {
2784 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2785 "debug_info_offset %s, ignoring .debug_aranges."),
2786 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2787 return;
2788 }
2789 }
2790
96b79293 2791 section->read (objfile);
927aa2e7
JK
2792
2793 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2794
2795 const gdb_byte *addr = section->buffer;
2796
2797 while (addr < section->buffer + section->size)
2798 {
2799 const gdb_byte *const entry_addr = addr;
2800 unsigned int bytes_read;
2801
2802 const LONGEST entry_length = read_initial_length (abfd, addr,
2803 &bytes_read);
2804 addr += bytes_read;
2805
2806 const gdb_byte *const entry_end = addr + entry_length;
2807 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2808 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2809 if (addr + entry_length > section->buffer + section->size)
2810 {
47e3f474 2811 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2812 "length %s exceeds section length %s, "
927aa2e7 2813 "ignoring .debug_aranges."),
47e3f474
TV
2814 objfile_name (objfile),
2815 plongest (entry_addr - section->buffer),
927aa2e7
JK
2816 plongest (bytes_read + entry_length),
2817 pulongest (section->size));
2818 return;
2819 }
2820
2821 /* The version number. */
2822 const uint16_t version = read_2_bytes (abfd, addr);
2823 addr += 2;
2824 if (version != 2)
2825 {
47e3f474 2826 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2827 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2828 objfile_name (objfile),
2829 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2830 return;
2831 }
2832
2833 const uint64_t debug_info_offset
2834 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2835 addr += offset_size;
2836 const auto per_cu_it
2837 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2838 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2839 {
47e3f474 2840 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2841 "debug_info_offset %s does not exists, "
2842 "ignoring .debug_aranges."),
47e3f474
TV
2843 objfile_name (objfile),
2844 plongest (entry_addr - section->buffer),
927aa2e7
JK
2845 pulongest (debug_info_offset));
2846 return;
2847 }
2848 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2849
2850 const uint8_t address_size = *addr++;
2851 if (address_size < 1 || address_size > 8)
2852 {
47e3f474 2853 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2854 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2855 objfile_name (objfile),
2856 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2857 return;
2858 }
2859
2860 const uint8_t segment_selector_size = *addr++;
2861 if (segment_selector_size != 0)
2862 {
47e3f474 2863 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2864 "segment_selector_size %u is not supported, "
2865 "ignoring .debug_aranges."),
47e3f474
TV
2866 objfile_name (objfile),
2867 plongest (entry_addr - section->buffer),
927aa2e7
JK
2868 segment_selector_size);
2869 return;
2870 }
2871
2872 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2873 size. It is undocumented by the DWARF standard but GCC does
2874 use it. */
927aa2e7
JK
2875 for (size_t padding = ((-(addr - section->buffer))
2876 & (2 * address_size - 1));
dda83cd7 2877 padding > 0; padding--)
927aa2e7
JK
2878 if (*addr++ != 0)
2879 {
47e3f474 2880 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2881 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2882 objfile_name (objfile),
2883 plongest (entry_addr - section->buffer));
927aa2e7
JK
2884 return;
2885 }
2886
2887 for (;;)
2888 {
2889 if (addr + 2 * address_size > entry_end)
2890 {
47e3f474 2891 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2892 "address list is not properly terminated, "
2893 "ignoring .debug_aranges."),
47e3f474
TV
2894 objfile_name (objfile),
2895 plongest (entry_addr - section->buffer));
927aa2e7
JK
2896 return;
2897 }
2898 ULONGEST start = extract_unsigned_integer (addr, address_size,
2899 dwarf5_byte_order);
2900 addr += address_size;
2901 ULONGEST length = extract_unsigned_integer (addr, address_size,
2902 dwarf5_byte_order);
2903 addr += address_size;
2904 if (start == 0 && length == 0)
2905 break;
efd7398e 2906 if (start == 0 && !per_bfd->has_section_at_zero)
927aa2e7
JK
2907 {
2908 /* Symbol was eliminated due to a COMDAT group. */
2909 continue;
2910 }
2911 ULONGEST end = start + length;
79748972
TT
2912 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2913 - baseaddr);
2914 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2915 - baseaddr);
927aa2e7
JK
2916 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2917 }
2918 }
2919
efd7398e
TT
2920 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2921 &per_bfd->obstack);
927aa2e7
JK
2922}
2923
4485a1c1
SM
2924/* A helper function that reads the .gdb_index from BUFFER and fills
2925 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2926 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2927 ok to use deprecated sections.
2928
2929 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2930 out parameters that are filled in with information about the CU and
2931 TU lists in the section.
2932
4485a1c1 2933 Returns true if all went well, false otherwise. */
2fdf6df6 2934
d33bc52e 2935static bool
3810f182 2936read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2937 bool deprecated_ok,
2938 gdb::array_view<const gdb_byte> buffer,
2939 struct mapped_index *map,
2940 const gdb_byte **cu_list,
2941 offset_type *cu_list_elements,
2942 const gdb_byte **types_list,
2943 offset_type *types_list_elements)
2944{
2945 const gdb_byte *addr = &buffer[0];
42c2c694 2946 offset_view metadata (buffer);
82430852 2947
9291a0cd 2948 /* Version check. */
42c2c694 2949 offset_type version = metadata[0];
987d643c 2950 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2951 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2952 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2953 indices. */
831adc1f 2954 if (version < 4)
481860b3
GB
2955 {
2956 static int warning_printed = 0;
2957 if (!warning_printed)
2958 {
2959 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2960 filename);
481860b3
GB
2961 warning_printed = 1;
2962 }
2963 return 0;
2964 }
2965 /* Index version 4 uses a different hash function than index version
2966 5 and later.
2967
2968 Versions earlier than 6 did not emit psymbols for inlined
2969 functions. Using these files will cause GDB not to be able to
2970 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2971 indices unless the user has done
2972 "set use-deprecated-index-sections on". */
2ec9a5e0 2973 if (version < 6 && !deprecated_ok)
481860b3
GB
2974 {
2975 static int warning_printed = 0;
2976 if (!warning_printed)
2977 {
e615022a
DE
2978 warning (_("\
2979Skipping deprecated .gdb_index section in %s.\n\
2980Do \"set use-deprecated-index-sections on\" before the file is read\n\
2981to use the section anyway."),
2ec9a5e0 2982 filename);
481860b3
GB
2983 warning_printed = 1;
2984 }
2985 return 0;
2986 }
796a7ff8 2987 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2988 of the TU (for symbols coming from TUs),
2989 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2990 Plus gold-generated indices can have duplicate entries for global symbols,
2991 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2992 These are just performance bugs, and we can't distinguish gdb-generated
2993 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2994
481860b3 2995 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2996 longer backward compatible. */
796a7ff8 2997 if (version > 8)
594e8718 2998 return 0;
9291a0cd 2999
559a7a62 3000 map->version = version;
9291a0cd 3001
42c2c694
TT
3002 int i = 1;
3003 *cu_list = addr + metadata[i];
3004 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
1fd400ff
TT
3005 ++i;
3006
42c2c694
TT
3007 *types_list = addr + metadata[i];
3008 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
987d643c 3009 ++i;
1fd400ff 3010
42c2c694
TT
3011 const gdb_byte *address_table = addr + metadata[i];
3012 const gdb_byte *address_table_end = addr + metadata[i + 1];
f00a2de2
PA
3013 map->address_table
3014 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3015 ++i;
3016
42c2c694
TT
3017 const gdb_byte *symbol_table = addr + metadata[i];
3018 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
f00a2de2 3019 map->symbol_table
42c2c694
TT
3020 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
3021 symbol_table_end));
9291a0cd 3022
f00a2de2 3023 ++i;
42c2c694 3024 map->constant_pool = buffer.slice (metadata[i]);
1fd400ff 3025
2ec9a5e0
TT
3026 return 1;
3027}
3028
4485a1c1
SM
3029/* Callback types for dwarf2_read_gdb_index. */
3030
3031typedef gdb::function_view
5989a64e 3032 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3033 get_gdb_index_contents_ftype;
3034typedef gdb::function_view
3035 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3036 get_gdb_index_contents_dwz_ftype;
3037
927aa2e7 3038/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3039 elements of all the CUs and return 1. Otherwise, return 0. */
3040
3041static int
4485a1c1 3042dwarf2_read_gdb_index
976ca316 3043 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3044 get_gdb_index_contents_ftype get_gdb_index_contents,
3045 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3046{
2ec9a5e0
TT
3047 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3048 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3049 struct dwz_file *dwz;
976ca316 3050 struct objfile *objfile = per_objfile->objfile;
f8c41851 3051 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3052
4485a1c1 3053 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3054 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3055
3056 if (main_index_contents.empty ())
3057 return 0;
3058
3063847f 3059 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3060 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3061 use_deprecated_index_sections,
3062 main_index_contents, map.get (), &cu_list,
3063 &cu_list_elements, &types_list,
3064 &types_list_elements))
2ec9a5e0
TT
3065 return 0;
3066
0fefef59 3067 /* Don't use the index if it's empty. */
3063847f 3068 if (map->symbol_table.empty ())
0fefef59
DE
3069 return 0;
3070
2ec9a5e0
TT
3071 /* If there is a .dwz file, read it so we can get its CU list as
3072 well. */
f8c41851 3073 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3074 if (dwz != NULL)
2ec9a5e0 3075 {
2ec9a5e0
TT
3076 struct mapped_index dwz_map;
3077 const gdb_byte *dwz_types_ignore;
3078 offset_type dwz_types_elements_ignore;
3079
4485a1c1
SM
3080 gdb::array_view<const gdb_byte> dwz_index_content
3081 = get_gdb_index_contents_dwz (objfile, dwz);
3082
3083 if (dwz_index_content.empty ())
3084 return 0;
3085
3810f182 3086 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3087 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3088 &dwz_list, &dwz_list_elements,
3089 &dwz_types_ignore,
3090 &dwz_types_elements_ignore))
2ec9a5e0
TT
3091 {
3092 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3093 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3094 return 0;
3095 }
3096 }
3097
f8c41851
SM
3098 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3099 dwz_list_elements);
1fd400ff 3100
8b70b953
TT
3101 if (types_list_elements)
3102 {
8b70b953
TT
3103 /* We can only handle a single .debug_types when we have an
3104 index. */
f8c41851 3105 if (per_bfd->types.size () != 1)
8b70b953
TT
3106 return 0;
3107
f8c41851 3108 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3109
f8c41851 3110 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3111 types_list_elements);
8b70b953 3112 }
9291a0cd 3113
976ca316 3114 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3115
f8c41851
SM
3116 per_bfd->index_table = std::move (map);
3117 per_bfd->using_index = 1;
3118 per_bfd->quick_file_names_table =
3119 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3120
9291a0cd
TT
3121 return 1;
3122}
3123
dee91e82 3124/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3125
dee91e82
DE
3126static void
3127dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 3128 struct die_info *comp_unit_die)
9291a0cd 3129{
dee91e82 3130 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3131 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3132 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3133 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3134 struct attribute *attr;
7b9f3c50
DE
3135 void **slot;
3136 struct quick_file_names *qfn;
9291a0cd 3137
0186c6a7
DE
3138 gdb_assert (! this_cu->is_debug_types);
3139
07261596
TT
3140 /* Our callers never want to match partial units -- instead they
3141 will match the enclosing full CU. */
3142 if (comp_unit_die->tag == DW_TAG_partial_unit)
3143 {
3144 this_cu->v.quick->no_file_data = 1;
3145 return;
3146 }
3147
0186c6a7 3148 lh_cu = this_cu;
7b9f3c50 3149 slot = NULL;
dee91e82 3150
fff8551c 3151 line_header_up lh;
9c541725 3152 sect_offset line_offset {};
fff8551c 3153
dee91e82 3154 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3155 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3156 {
7b9f3c50
DE
3157 struct quick_file_names find_entry;
3158
d4df075e 3159 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3160
3161 /* We may have already read in this line header (TU line header sharing).
3162 If we have we're done. */
094b34ac 3163 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3164 find_entry.hash.line_sect_off = line_offset;
976ca316 3165 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3166 &find_entry, INSERT);
3167 if (*slot != NULL)
3168 {
9a3c8263 3169 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3170 return;
7b9f3c50
DE
3171 }
3172
3019eac3 3173 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3174 }
3175 if (lh == NULL)
3176 {
094b34ac 3177 lh_cu->v.quick->no_file_data = 1;
dee91e82 3178 return;
9291a0cd
TT
3179 }
3180
976ca316 3181 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3182 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3183 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3184 gdb_assert (slot != NULL);
3185 *slot = qfn;
9291a0cd 3186
d721ba37 3187 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3188
aa391654
TT
3189 int offset = 0;
3190 if (strcmp (fnd.name, "<unknown>") != 0)
3191 ++offset;
3192
7ba99d21 3193 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3194 qfn->file_names =
976ca316 3195 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3196 qfn->num_file_names);
aa391654
TT
3197 if (offset != 0)
3198 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3199 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3200 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3201 fnd.comp_dir).release ();
7b9f3c50 3202 qfn->real_names = NULL;
9291a0cd 3203
094b34ac 3204 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3205}
3206
3207/* A helper for the "quick" functions which attempts to read the line
3208 table for THIS_CU. */
3209
3210static struct quick_file_names *
ab432490
SM
3211dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3212 dwarf2_per_objfile *per_objfile)
dee91e82 3213{
0186c6a7
DE
3214 /* This should never be called for TUs. */
3215 gdb_assert (! this_cu->is_debug_types);
3216 /* Nor type unit groups. */
197400e8 3217 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3218
dee91e82
DE
3219 if (this_cu->v.quick->file_names != NULL)
3220 return this_cu->v.quick->file_names;
3221 /* If we know there is no line data, no point in looking again. */
3222 if (this_cu->v.quick->no_file_data)
3223 return NULL;
3224
ab432490 3225 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3226 if (!reader.dummy_p)
eb5dd737 3227 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
3228
3229 if (this_cu->v.quick->no_file_data)
3230 return NULL;
3231 return this_cu->v.quick->file_names;
9291a0cd
TT
3232}
3233
3234/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3235 real path for a given file name from the line table. */
2fdf6df6 3236
9291a0cd 3237static const char *
976ca316 3238dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3239 struct quick_file_names *qfn, int index)
9291a0cd 3240{
7b9f3c50 3241 if (qfn->real_names == NULL)
976ca316 3242 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3243 qfn->num_file_names, const char *);
9291a0cd 3244
7b9f3c50 3245 if (qfn->real_names[index] == NULL)
14278e1f 3246 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3247
7b9f3c50 3248 return qfn->real_names[index];
9291a0cd
TT
3249}
3250
39298a5d
TT
3251struct symtab *
3252dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
9291a0cd 3253{
976ca316 3254 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
0d305d5c
TT
3255 dwarf2_per_cu_data *dwarf_cu
3256 = per_objfile->per_bfd->all_comp_units.back ().get ();
976ca316 3257 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3258
43f3e411
DE
3259 if (cust == NULL)
3260 return NULL;
ed2dc618 3261
43f3e411 3262 return compunit_primary_filetab (cust);
9291a0cd
TT
3263}
3264
7b9f3c50
DE
3265/* Traversal function for dw2_forget_cached_source_info. */
3266
3267static int
3268dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3269{
7b9f3c50 3270 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3271
7b9f3c50 3272 if (file_data->real_names)
9291a0cd 3273 {
7b9f3c50 3274 int i;
9291a0cd 3275
7b9f3c50 3276 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3277 {
7b9f3c50
DE
3278 xfree ((void*) file_data->real_names[i]);
3279 file_data->real_names[i] = NULL;
9291a0cd
TT
3280 }
3281 }
7b9f3c50
DE
3282
3283 return 1;
3284}
3285
39298a5d
TT
3286void
3287dwarf2_base_index_functions::forget_cached_source_info
3288 (struct objfile *objfile)
7b9f3c50 3289{
976ca316 3290 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3291
976ca316 3292 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3293 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3294}
3295
da51c347
DE
3296/* Struct used to manage iterating over all CUs looking for a symbol. */
3297
3298struct dw2_symtab_iterator
9291a0cd 3299{
ed2dc618 3300 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3301 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3302 /* If set, only look for symbols that match that block. Valid values are
3303 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3304 gdb::optional<block_enum> block_index;
da51c347
DE
3305 /* The kind of symbol we're looking for. */
3306 domain_enum domain;
3307 /* The list of CUs from the index entry of the symbol,
3308 or NULL if not found. */
42c2c694 3309 offset_view vec;
da51c347
DE
3310 /* The next element in VEC to look at. */
3311 int next;
3312 /* The number of elements in VEC, or zero if there is no match. */
3313 int length;
8943b874
DE
3314 /* Have we seen a global version of the symbol?
3315 If so we can ignore all further global instances.
3316 This is to work around gold/15646, inefficient gold-generated
3317 indices. */
3318 int global_seen;
da51c347 3319};
9291a0cd 3320
7089bd88 3321/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2fdf6df6 3322
9291a0cd 3323static void
7089bd88
TT
3324dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3325 dwarf2_per_objfile *per_objfile,
3326 gdb::optional<block_enum> block_index,
3327 domain_enum domain, offset_type namei)
da51c347 3328{
976ca316 3329 iter->per_objfile = per_objfile;
da51c347
DE
3330 iter->block_index = block_index;
3331 iter->domain = domain;
3332 iter->next = 0;
8943b874 3333 iter->global_seen = 0;
42c2c694 3334 iter->vec = {};
e5f3ece2 3335 iter->length = 0;
e5f3ece2
TV
3336
3337 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3338 /* index is NULL if OBJF_READNOW. */
3339 if (index == NULL)
3340 return;
3341
3342 gdb_assert (!index->symbol_name_slot_invalid (namei));
42c2c694 3343 offset_type vec_idx = index->symbol_vec_index (namei);
e5f3ece2 3344
42c2c694
TT
3345 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3346 iter->length = iter->vec[0];
da51c347
DE
3347}
3348
3349/* Return the next matching CU or NULL if there are no more. */
3350
3351static struct dwarf2_per_cu_data *
3352dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3353{
976ca316 3354 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3355
da51c347
DE
3356 for ( ; iter->next < iter->length; ++iter->next)
3357 {
42c2c694 3358 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
da51c347 3359 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3360 gdb_index_symbol_kind symbol_kind =
3361 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3362 /* Only check the symbol attributes if they're present.
3363 Indices prior to version 7 don't record them,
3364 and indices >= 7 may elide them for certain symbols
3365 (gold does this). */
3366 int attrs_valid =
976ca316 3367 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3368 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3369
3190f0c6 3370 /* Don't crash on bad data. */
91eea9cc 3371 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3190f0c6 3372 {
b98664d3 3373 complaint (_(".gdb_index entry has bad CU index"
976ca316 3374 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3375 continue;
3376 }
3377
91eea9cc 3378 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3190f0c6 3379
da51c347 3380 /* Skip if already read in. */
976ca316 3381 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3382 continue;
3383
8943b874
DE
3384 /* Check static vs global. */
3385 if (attrs_valid)
3386 {
2b79f376
SM
3387 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3388
3389 if (iter->block_index.has_value ())
3390 {
3391 bool want_static = *iter->block_index == STATIC_BLOCK;
3392
3393 if (is_static != want_static)
3394 continue;
3395 }
3396
8943b874 3397 /* Work around gold/15646. */
f030440d
TV
3398 if (!is_static
3399 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3400 {
3401 if (iter->global_seen)
3402 continue;
3403
3404 iter->global_seen = 1;
3405 }
8943b874 3406 }
da51c347
DE
3407
3408 /* Only check the symbol's kind if it has one. */
3409 if (attrs_valid)
3410 {
3411 switch (iter->domain)
3412 {
3413 case VAR_DOMAIN:
3414 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3415 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3416 /* Some types are also in VAR_DOMAIN. */
3417 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3418 continue;
3419 break;
3420 case STRUCT_DOMAIN:
3421 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3422 continue;
3423 break;
3424 case LABEL_DOMAIN:
3425 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3426 continue;
3427 break;
59c35742
AB
3428 case MODULE_DOMAIN:
3429 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3430 continue;
3431 break;
da51c347
DE
3432 default:
3433 break;
3434 }
3435 }
3436
3437 ++iter->next;
3438 return per_cu;
3439 }
3440
3441 return NULL;
3442}
3443
39298a5d 3444void
4829711b
TT
3445dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3446 bool print_bcache)
9291a0cd 3447{
4829711b
TT
3448 if (print_bcache)
3449 return;
3450
976ca316 3451 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
91eea9cc 3452 int total = per_objfile->per_bfd->all_comp_units.size ();
ed2dc618 3453 int count = 0;
9291a0cd 3454
ed2dc618 3455 for (int i = 0; i < total; ++i)
9291a0cd 3456 {
91eea9cc 3457 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
9291a0cd 3458
976ca316 3459 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3460 ++count;
3461 }
e4a48d9d 3462 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3463 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3464}
3465
779bd270
DE
3466/* This dumps minimal information about the index.
3467 It is called via "mt print objfiles".
3468 One use is to verify .gdb_index has been loaded by the
3469 gdb.dwarf2/gdb-index.exp testcase. */
3470
39298a5d
TT
3471void
3472dwarf2_gdb_index::dump (struct objfile *objfile)
9291a0cd 3473{
976ca316 3474 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3475
976ca316 3476 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3477 printf_filtered (".gdb_index:");
976ca316 3478 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3479 {
3480 printf_filtered (" version %d\n",
976ca316 3481 per_objfile->per_bfd->index_table->version);
779bd270
DE
3482 }
3483 else
3484 printf_filtered (" faked for \"readnow\"\n");
3485 printf_filtered ("\n");
9291a0cd
TT
3486}
3487
39298a5d
TT
3488void
3489dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
9291a0cd 3490{
976ca316 3491 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
91eea9cc 3492 int total_units = per_objfile->per_bfd->all_comp_units.size ();
9291a0cd 3493
ed2dc618 3494 for (int i = 0; i < total_units; ++i)
9291a0cd 3495 {
91eea9cc 3496 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
9291a0cd 3497
58f0c718
TT
3498 /* We don't want to directly expand a partial CU, because if we
3499 read it with the wrong language, then assertion failures can
3500 be triggered later on. See PR symtab/23010. So, tell
3501 dw2_instantiate_symtab to skip partial CUs -- any important
3502 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3503 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3504 }
3505}
3506
df35e626 3507static bool
9a0bacfb
TV
3508dw2_expand_symtabs_matching_symbol
3509 (mapped_index_base &index,
3510 const lookup_name_info &lookup_name_in,
3511 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3512 gdb::function_view<bool (offset_type)> match_callback,
3513 dwarf2_per_objfile *per_objfile);
9a0bacfb 3514
df35e626 3515static bool
9a0bacfb 3516dw2_expand_symtabs_matching_one
97a1449a
SM
3517 (dwarf2_per_cu_data *per_cu,
3518 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3519 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3520 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3521
af82f89d 3522void
0b7b2c2a 3523dwarf2_gdb_index::expand_matching_symbols
199b4314 3524 (struct objfile *objfile,
b054970d 3525 const lookup_name_info &name, domain_enum domain,
199b4314 3526 int global,
199b4314 3527 symbol_compare_ftype *ordered_compare)
9291a0cd 3528{
1aa98955 3529 /* Used for Ada. */
976ca316 3530 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3531
9a0bacfb
TV
3532 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3533
976ca316 3534 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3535 {
976ca316 3536 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3537
9a0bacfb
TV
3538 const char *match_name = name.ada ().lookup_name ().c_str ();
3539 auto matcher = [&] (const char *symname)
3540 {
3541 if (ordered_compare == nullptr)
3542 return true;
3543 return ordered_compare (symname, match_name) == 0;
3544 };
3545
b64f703b 3546 dw2_expand_symtabs_matching_symbol (index, name, matcher,
9a0bacfb
TV
3547 [&] (offset_type namei)
3548 {
3549 struct dw2_symtab_iterator iter;
3550 struct dwarf2_per_cu_data *per_cu;
3551
976ca316 3552 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3553 namei);
9a0bacfb 3554 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3555 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3556 nullptr);
9a0bacfb 3557 return true;
976ca316 3558 }, per_objfile);
9a0bacfb
TV
3559 }
3560 else
3561 {
3562 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3563 proceed assuming all symtabs have been read in. */
3564 }
9291a0cd
TT
3565}
3566
e1ef7d7a
PA
3567/* Starting from a search name, return the string that finds the upper
3568 bound of all strings that start with SEARCH_NAME in a sorted name
3569 list. Returns the empty string to indicate that the upper bound is
3570 the end of the list. */
3571
3572static std::string
3573make_sort_after_prefix_name (const char *search_name)
3574{
3575 /* When looking to complete "func", we find the upper bound of all
3576 symbols that start with "func" by looking for where we'd insert
3577 the closest string that would follow "func" in lexicographical
3578 order. Usually, that's "func"-with-last-character-incremented,
3579 i.e. "fund". Mind non-ASCII characters, though. Usually those
3580 will be UTF-8 multi-byte sequences, but we can't be certain.
3581 Especially mind the 0xff character, which is a valid character in
3582 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3583 rule out compilers allowing it in identifiers. Note that
3584 conveniently, strcmp/strcasecmp are specified to compare
3585 characters interpreted as unsigned char. So what we do is treat
3586 the whole string as a base 256 number composed of a sequence of
3587 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3588 to 0, and carries 1 to the following more-significant position.
3589 If the very first character in SEARCH_NAME ends up incremented
3590 and carries/overflows, then the upper bound is the end of the
3591 list. The string after the empty string is also the empty
3592 string.
3593
3594 Some examples of this operation:
3595
3596 SEARCH_NAME => "+1" RESULT
3597
3598 "abc" => "abd"
3599 "ab\xff" => "ac"
3600 "\xff" "a" "\xff" => "\xff" "b"
3601 "\xff" => ""
3602 "\xff\xff" => ""
3603 "" => ""
3604
3605 Then, with these symbols for example:
3606
3607 func
3608 func1
3609 fund
3610
3611 completing "func" looks for symbols between "func" and
3612 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3613 which finds "func" and "func1", but not "fund".
3614
3615 And with:
3616
3617 funcÿ (Latin1 'ÿ' [0xff])
3618 funcÿ1
3619 fund
3620
3621 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3622 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3623
3624 And with:
3625
3626 ÿÿ (Latin1 'ÿ' [0xff])
3627 ÿÿ1
3628
3629 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3630 the end of the list.
3631 */
3632 std::string after = search_name;
3633 while (!after.empty () && (unsigned char) after.back () == 0xff)
3634 after.pop_back ();
3635 if (!after.empty ())
3636 after.back () = (unsigned char) after.back () + 1;
3637 return after;
3638}
3639
5c58de74 3640/* See declaration. */
61d96d7e 3641
5c58de74
PA
3642std::pair<std::vector<name_component>::const_iterator,
3643 std::vector<name_component>::const_iterator>
44ed8f3e 3644mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3645 (const lookup_name_info &lookup_name_without_params, language lang,
3646 dwarf2_per_objfile *per_objfile) const
3f563c84 3647{
5c58de74
PA
3648 auto *name_cmp
3649 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3650
3b00ef10 3651 const char *lang_name
e0802d59 3652 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3653
3f563c84
PA
3654 /* Comparison function object for lower_bound that matches against a
3655 given symbol name. */
3656 auto lookup_compare_lower = [&] (const name_component &elem,
3657 const char *name)
3658 {
fcf23d5b 3659 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3660 const char *elem_name = elem_qualified + elem.name_offset;
3661 return name_cmp (elem_name, name) < 0;
3662 };
3663
3664 /* Comparison function object for upper_bound that matches against a
3665 given symbol name. */
3666 auto lookup_compare_upper = [&] (const char *name,
3667 const name_component &elem)
3668 {
fcf23d5b 3669 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3670 const char *elem_name = elem_qualified + elem.name_offset;
3671 return name_cmp (name, elem_name) < 0;
3672 };
3673
5c58de74
PA
3674 auto begin = this->name_components.begin ();
3675 auto end = this->name_components.end ();
3f563c84
PA
3676
3677 /* Find the lower bound. */
3678 auto lower = [&] ()
3679 {
3b00ef10 3680 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3681 return begin;
3682 else
3b00ef10 3683 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3684 } ();
3685
3686 /* Find the upper bound. */
3687 auto upper = [&] ()
3688 {
5c58de74 3689 if (lookup_name_without_params.completion_mode ())
3f563c84 3690 {
e1ef7d7a
PA
3691 /* In completion mode, we want UPPER to point past all
3692 symbols names that have the same prefix. I.e., with
3693 these symbols, and completing "func":
3694
3695 function << lower bound
3696 function1
3697 other_function << upper bound
3698
3699 We find the upper bound by looking for the insertion
3700 point of "func"-with-last-character-incremented,
3701 i.e. "fund". */
3b00ef10 3702 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3703 if (after.empty ())
3f563c84 3704 return end;
e6b2f5ef
PA
3705 return std::lower_bound (lower, end, after.c_str (),
3706 lookup_compare_lower);
3f563c84
PA
3707 }
3708 else
3b00ef10 3709 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3710 } ();
3711
5c58de74
PA
3712 return {lower, upper};
3713}
3714
3715/* See declaration. */
3716
3717void
fcf23d5b 3718mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3719{
3720 if (!this->name_components.empty ())
3721 return;
3722
3723 this->name_components_casing = case_sensitivity;
3724 auto *name_cmp
3725 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3726
3727 /* The code below only knows how to break apart components of C++
3728 symbol names (and other languages that use '::' as
3b00ef10 3729 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3730 auto count = this->symbol_name_count ();
3731 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3732 {
44ed8f3e 3733 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3734 continue;
3735
fcf23d5b 3736 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3737
3738 /* Add each name component to the name component table. */
3739 unsigned int previous_len = 0;
3b00ef10
TT
3740
3741 if (strstr (name, "::") != nullptr)
3742 {
3743 for (unsigned int current_len = cp_find_first_component (name);
3744 name[current_len] != '\0';
3745 current_len += cp_find_first_component (name + current_len))
3746 {
3747 gdb_assert (name[current_len] == ':');
3748 this->name_components.push_back ({previous_len, idx});
3749 /* Skip the '::'. */
3750 current_len += 2;
3751 previous_len = current_len;
3752 }
3753 }
3754 else
5c58de74 3755 {
3b00ef10
TT
3756 /* Handle the Ada encoded (aka mangled) form here. */
3757 for (const char *iter = strstr (name, "__");
3758 iter != nullptr;
3759 iter = strstr (iter, "__"))
3760 {
3761 this->name_components.push_back ({previous_len, idx});
3762 iter += 2;
3763 previous_len = iter - name;
3764 }
5c58de74 3765 }
3b00ef10 3766
5c58de74
PA
3767 this->name_components.push_back ({previous_len, idx});
3768 }
3769
3770 /* Sort name_components elements by name. */
3771 auto name_comp_compare = [&] (const name_component &left,
3772 const name_component &right)
3773 {
fcf23d5b
SM
3774 const char *left_qualified
3775 = this->symbol_name_at (left.idx, per_objfile);
3776 const char *right_qualified
3777 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
3778
3779 const char *left_name = left_qualified + left.name_offset;
3780 const char *right_name = right_qualified + right.name_offset;
3781
3782 return name_cmp (left_name, right_name) < 0;
3783 };
3784
3785 std::sort (this->name_components.begin (),
3786 this->name_components.end (),
3787 name_comp_compare);
3788}
3789
3790/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3791 mapped_index_base instead of the containing objfile. This is split
3792 to a separate function in order to be able to unit test the
3793 name_components matching using a mock mapped_index_base. For each
5c58de74 3794 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3795 symbol's index in the mapped_index_base symbol table. */
5c58de74 3796
df35e626 3797static bool
5c58de74 3798dw2_expand_symtabs_matching_symbol
44ed8f3e 3799 (mapped_index_base &index,
5c58de74
PA
3800 const lookup_name_info &lookup_name_in,
3801 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3802 gdb::function_view<bool (offset_type)> match_callback,
3803 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3804{
3805 lookup_name_info lookup_name_without_params
3806 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3807
3808 /* Build the symbol name component sorted vector, if we haven't
3809 yet. */
fcf23d5b 3810 index.build_name_components (per_objfile);
5c58de74 3811
3f563c84
PA
3812 /* The same symbol may appear more than once in the range though.
3813 E.g., if we're looking for symbols that complete "w", and we have
3814 a symbol named "w1::w2", we'll find the two name components for
3815 that same symbol in the range. To be sure we only call the
3816 callback once per symbol, we first collect the symbol name
3817 indexes that matched in a temporary vector and ignore
3818 duplicates. */
3819 std::vector<offset_type> matches;
3f563c84 3820
3b00ef10
TT
3821 struct name_and_matcher
3822 {
3823 symbol_name_matcher_ftype *matcher;
ecc6c606 3824 const char *name;
3b00ef10
TT
3825
3826 bool operator== (const name_and_matcher &other) const
3f563c84 3827 {
ecc6c606 3828 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3829 }
3830 };
3831
3832 /* A vector holding all the different symbol name matchers, for all
3833 languages. */
3834 std::vector<name_and_matcher> matchers;
3835
3836 for (int i = 0; i < nr_languages; i++)
3837 {
3838 enum language lang_e = (enum language) i;
3839
3840 const language_defn *lang = language_def (lang_e);
3841 symbol_name_matcher_ftype *name_matcher
c9debfb9 3842 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 3843
3b00ef10 3844 name_and_matcher key {
dda83cd7 3845 name_matcher,
3b00ef10
TT
3846 lookup_name_without_params.language_lookup_name (lang_e)
3847 };
3848
3849 /* Don't insert the same comparison routine more than once.
3850 Note that we do this linear walk. This is not a problem in
3851 practice because the number of supported languages is
3852 low. */
3853 if (std::find (matchers.begin (), matchers.end (), key)
3854 != matchers.end ())
9291a0cd 3855 continue;
3b00ef10
TT
3856 matchers.push_back (std::move (key));
3857
3858 auto bounds
3859 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 3860 lang_e, per_objfile);
3b00ef10
TT
3861
3862 /* Now for each symbol name in range, check to see if we have a name
3863 match, and if so, call the MATCH_CALLBACK callback. */
3864
3865 for (; bounds.first != bounds.second; ++bounds.first)
3866 {
fcf23d5b
SM
3867 const char *qualified
3868 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
3869
3870 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3871 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3872 continue;
9291a0cd 3873
3b00ef10
TT
3874 matches.push_back (bounds.first->idx);
3875 }
3f563c84
PA
3876 }
3877
3878 std::sort (matches.begin (), matches.end ());
3879
3880 /* Finally call the callback, once per match. */
3881 ULONGEST prev = -1;
df35e626 3882 bool result = true;
3f563c84
PA
3883 for (offset_type idx : matches)
3884 {
3885 if (prev != idx)
3886 {
3b00ef10 3887 if (!match_callback (idx))
df35e626
TT
3888 {
3889 result = false;
3890 break;
3891 }
3f563c84
PA
3892 prev = idx;
3893 }
3894 }
3895
3896 /* Above we use a type wider than idx's for 'prev', since 0 and
3897 (offset_type)-1 are both possible values. */
3898 static_assert (sizeof (prev) > sizeof (offset_type), "");
df35e626
TT
3899
3900 return result;
3f563c84
PA
3901}
3902
c62446b1
PA
3903#if GDB_SELF_TEST
3904
3905namespace selftests { namespace dw2_expand_symtabs_matching {
3906
a3c5fafd
PA
3907/* A mock .gdb_index/.debug_names-like name index table, enough to
3908 exercise dw2_expand_symtabs_matching_symbol, which works with the
3909 mapped_index_base interface. Builds an index from the symbol list
3910 passed as parameter to the constructor. */
3911class mock_mapped_index : public mapped_index_base
c62446b1
PA
3912{
3913public:
a3c5fafd
PA
3914 mock_mapped_index (gdb::array_view<const char *> symbols)
3915 : m_symbol_table (symbols)
c62446b1
PA
3916 {}
3917
a3c5fafd 3918 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 3919
a3c5fafd 3920 /* Return the number of names in the symbol table. */
632e107b 3921 size_t symbol_name_count () const override
c62446b1 3922 {
a3c5fafd 3923 return m_symbol_table.size ();
c62446b1
PA
3924 }
3925
a3c5fafd 3926 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
3927 const char *symbol_name_at
3928 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
3929 {
3930 return m_symbol_table[idx];
3931 }
c62446b1 3932
a3c5fafd
PA
3933private:
3934 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
3935};
3936
3937/* Convenience function that converts a NULL pointer to a "<null>"
3938 string, to pass to print routines. */
3939
3940static const char *
3941string_or_null (const char *str)
3942{
3943 return str != NULL ? str : "<null>";
3944}
3945
3946/* Check if a lookup_name_info built from
3947 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3948 index. EXPECTED_LIST is the list of expected matches, in expected
3949 matching order. If no match expected, then an empty list is
3950 specified. Returns true on success. On failure prints a warning
3951 indicating the file:line that failed, and returns false. */
3952
3953static bool
3954check_match (const char *file, int line,
3955 mock_mapped_index &mock_index,
3956 const char *name, symbol_name_match_type match_type,
3957 bool completion_mode,
fcf23d5b
SM
3958 std::initializer_list<const char *> expected_list,
3959 dwarf2_per_objfile *per_objfile)
c62446b1
PA
3960{
3961 lookup_name_info lookup_name (name, match_type, completion_mode);
3962
3963 bool matched = true;
3964
3965 auto mismatch = [&] (const char *expected_str,
3966 const char *got)
3967 {
3968 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3969 "expected=\"%s\", got=\"%s\"\n"),
3970 file, line,
3971 (match_type == symbol_name_match_type::FULL
3972 ? "FULL" : "WILD"),
3973 name, string_or_null (expected_str), string_or_null (got));
3974 matched = false;
3975 };
3976
3977 auto expected_it = expected_list.begin ();
3978 auto expected_end = expected_list.end ();
3979
a3c5fafd 3980 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
b64f703b 3981 nullptr,
c62446b1
PA
3982 [&] (offset_type idx)
3983 {
fcf23d5b 3984 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
3985 const char *expected_str
3986 = expected_it == expected_end ? NULL : *expected_it++;
3987
3988 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3989 mismatch (expected_str, matched_name);
3b00ef10 3990 return true;
fcf23d5b 3991 }, per_objfile);
c62446b1
PA
3992
3993 const char *expected_str
3994 = expected_it == expected_end ? NULL : *expected_it++;
3995 if (expected_str != NULL)
3996 mismatch (expected_str, NULL);
3997
3998 return matched;
3999}
4000
4001/* The symbols added to the mock mapped_index for testing (in
4002 canonical form). */
4003static const char *test_symbols[] = {
4004 "function",
4005 "std::bar",
4006 "std::zfunction",
4007 "std::zfunction2",
4008 "w1::w2",
4009 "ns::foo<char*>",
4010 "ns::foo<int>",
4011 "ns::foo<long>",
a20714ff
PA
4012 "ns2::tmpl<int>::foo2",
4013 "(anonymous namespace)::A::B::C",
c62446b1 4014
e1ef7d7a
PA
4015 /* These are used to check that the increment-last-char in the
4016 matching algorithm for completion doesn't match "t1_fund" when
4017 completing "t1_func". */
4018 "t1_func",
4019 "t1_func1",
4020 "t1_fund",
4021 "t1_fund1",
4022
4023 /* A UTF-8 name with multi-byte sequences to make sure that
4024 cp-name-parser understands this as a single identifier ("função"
4025 is "function" in PT). */
4026 u8"u8função",
4027
4028 /* \377 (0xff) is Latin1 'ÿ'. */
4029 "yfunc\377",
4030
4031 /* \377 (0xff) is Latin1 'ÿ'. */
4032 "\377",
4033 "\377\377123",
4034
c62446b1
PA
4035 /* A name with all sorts of complications. Starts with "z" to make
4036 it easier for the completion tests below. */
4037#define Z_SYM_NAME \
4038 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4039 "::tuple<(anonymous namespace)::ui*, " \
4040 "std::default_delete<(anonymous namespace)::ui>, void>"
4041
4042 Z_SYM_NAME
4043};
4044
a3c5fafd
PA
4045/* Returns true if the mapped_index_base::find_name_component_bounds
4046 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4047 in completion mode. */
5c58de74
PA
4048
4049static bool
a3c5fafd 4050check_find_bounds_finds (mapped_index_base &index,
5c58de74 4051 const char *search_name,
fcf23d5b
SM
4052 gdb::array_view<const char *> expected_syms,
4053 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4054{
4055 lookup_name_info lookup_name (search_name,
4056 symbol_name_match_type::FULL, true);
4057
3b00ef10 4058 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4059 language_cplus,
4060 per_objfile);
5c58de74
PA
4061
4062 size_t distance = std::distance (bounds.first, bounds.second);
4063 if (distance != expected_syms.size ())
4064 return false;
4065
4066 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4067 {
4068 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4069 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4070 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4071 return false;
4072 }
4073
4074 return true;
4075}
4076
4077/* Test the lower-level mapped_index::find_name_component_bounds
4078 method. */
4079
c62446b1 4080static void
5c58de74
PA
4081test_mapped_index_find_name_component_bounds ()
4082{
4083 mock_mapped_index mock_index (test_symbols);
4084
fcf23d5b 4085 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4086
4087 /* Test the lower-level mapped_index::find_name_component_bounds
4088 method in completion mode. */
4089 {
4090 static const char *expected_syms[] = {
4091 "t1_func",
4092 "t1_func1",
5c58de74
PA
4093 };
4094
fcf23d5b
SM
4095 SELF_CHECK (check_find_bounds_finds
4096 (mock_index, "t1_func", expected_syms,
4097 NULL /* per_objfile */));
5c58de74
PA
4098 }
4099
4100 /* Check that the increment-last-char in the name matching algorithm
4101 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4102 {
4103 static const char *expected_syms1[] = {
4104 "\377",
4105 "\377\377123",
4106 };
fcf23d5b
SM
4107 SELF_CHECK (check_find_bounds_finds
4108 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4109
4110 static const char *expected_syms2[] = {
4111 "\377\377123",
4112 };
fcf23d5b
SM
4113 SELF_CHECK (check_find_bounds_finds
4114 (mock_index, "\377\377", expected_syms2,
4115 NULL /* per_objfile */));
5c58de74
PA
4116 }
4117}
4118
4119/* Test dw2_expand_symtabs_matching_symbol. */
4120
4121static void
4122test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4123{
4124 mock_mapped_index mock_index (test_symbols);
4125
4126 /* We let all tests run until the end even if some fails, for debug
4127 convenience. */
4128 bool any_mismatch = false;
4129
4130 /* Create the expected symbols list (an initializer_list). Needed
4131 because lists have commas, and we need to pass them to CHECK,
4132 which is a macro. */
4133#define EXPECT(...) { __VA_ARGS__ }
4134
4135 /* Wrapper for check_match that passes down the current
4136 __FILE__/__LINE__. */
4137#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4138 any_mismatch |= !check_match (__FILE__, __LINE__, \
4139 mock_index, \
4140 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4141 EXPECTED_LIST, NULL)
c62446b1
PA
4142
4143 /* Identity checks. */
4144 for (const char *sym : test_symbols)
4145 {
4146 /* Should be able to match all existing symbols. */
4147 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4148 EXPECT (sym));
4149
4150 /* Should be able to match all existing symbols with
4151 parameters. */
4152 std::string with_params = std::string (sym) + "(int)";
4153 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4154 EXPECT (sym));
4155
4156 /* Should be able to match all existing symbols with
4157 parameters and qualifiers. */
4158 with_params = std::string (sym) + " ( int ) const";
4159 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4160 EXPECT (sym));
4161
4162 /* This should really find sym, but cp-name-parser.y doesn't
4163 know about lvalue/rvalue qualifiers yet. */
4164 with_params = std::string (sym) + " ( int ) &&";
4165 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4166 {});
4167 }
4168
e1ef7d7a
PA
4169 /* Check that the name matching algorithm for completion doesn't get
4170 confused with Latin1 'ÿ' / 0xff. */
4171 {
4172 static const char str[] = "\377";
4173 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4174 EXPECT ("\377", "\377\377123"));
4175 }
4176
4177 /* Check that the increment-last-char in the matching algorithm for
4178 completion doesn't match "t1_fund" when completing "t1_func". */
4179 {
4180 static const char str[] = "t1_func";
4181 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4182 EXPECT ("t1_func", "t1_func1"));
4183 }
4184
c62446b1
PA
4185 /* Check that completion mode works at each prefix of the expected
4186 symbol name. */
4187 {
4188 static const char str[] = "function(int)";
4189 size_t len = strlen (str);
4190 std::string lookup;
4191
4192 for (size_t i = 1; i < len; i++)
4193 {
4194 lookup.assign (str, i);
4195 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4196 EXPECT ("function"));
4197 }
4198 }
4199
4200 /* While "w" is a prefix of both components, the match function
4201 should still only be called once. */
4202 {
4203 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4204 EXPECT ("w1::w2"));
a20714ff
PA
4205 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4206 EXPECT ("w1::w2"));
c62446b1
PA
4207 }
4208
4209 /* Same, with a "complicated" symbol. */
4210 {
4211 static const char str[] = Z_SYM_NAME;
4212 size_t len = strlen (str);
4213 std::string lookup;
4214
4215 for (size_t i = 1; i < len; i++)
4216 {
4217 lookup.assign (str, i);
4218 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4219 EXPECT (Z_SYM_NAME));
4220 }
4221 }
4222
4223 /* In FULL mode, an incomplete symbol doesn't match. */
4224 {
4225 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4226 {});
4227 }
4228
4229 /* A complete symbol with parameters matches any overload, since the
4230 index has no overload info. */
4231 {
4232 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4233 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4234 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4235 EXPECT ("std::zfunction", "std::zfunction2"));
4236 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4237 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4238 }
4239
4240 /* Check that whitespace is ignored appropriately. A symbol with a
4241 template argument list. */
4242 {
4243 static const char expected[] = "ns::foo<int>";
4244 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4245 EXPECT (expected));
a20714ff
PA
4246 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4247 EXPECT (expected));
c62446b1
PA
4248 }
4249
4250 /* Check that whitespace is ignored appropriately. A symbol with a
4251 template argument list that includes a pointer. */
4252 {
4253 static const char expected[] = "ns::foo<char*>";
4254 /* Try both completion and non-completion modes. */
4255 static const bool completion_mode[2] = {false, true};
4256 for (size_t i = 0; i < 2; i++)
4257 {
4258 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4259 completion_mode[i], EXPECT (expected));
a20714ff
PA
4260 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4261 completion_mode[i], EXPECT (expected));
c62446b1
PA
4262
4263 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4264 completion_mode[i], EXPECT (expected));
a20714ff
PA
4265 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4266 completion_mode[i], EXPECT (expected));
c62446b1
PA
4267 }
4268 }
4269
4270 {
4271 /* Check method qualifiers are ignored. */
4272 static const char expected[] = "ns::foo<char*>";
4273 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4274 symbol_name_match_type::FULL, true, EXPECT (expected));
4275 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4276 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4277 CHECK_MATCH ("foo < char * > ( int ) const",
4278 symbol_name_match_type::WILD, true, EXPECT (expected));
4279 CHECK_MATCH ("foo < char * > ( int ) &&",
4280 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4281 }
4282
4283 /* Test lookup names that don't match anything. */
4284 {
a20714ff
PA
4285 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4286 {});
4287
c62446b1
PA
4288 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4289 {});
4290 }
4291
a20714ff
PA
4292 /* Some wild matching tests, exercising "(anonymous namespace)",
4293 which should not be confused with a parameter list. */
4294 {
4295 static const char *syms[] = {
4296 "A::B::C",
4297 "B::C",
4298 "C",
4299 "A :: B :: C ( int )",
4300 "B :: C ( int )",
4301 "C ( int )",
4302 };
4303
4304 for (const char *s : syms)
4305 {
4306 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4307 EXPECT ("(anonymous namespace)::A::B::C"));
4308 }
4309 }
4310
4311 {
4312 static const char expected[] = "ns2::tmpl<int>::foo2";
4313 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4314 EXPECT (expected));
4315 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4316 EXPECT (expected));
4317 }
4318
c62446b1
PA
4319 SELF_CHECK (!any_mismatch);
4320
4321#undef EXPECT
4322#undef CHECK_MATCH
4323}
4324
5c58de74
PA
4325static void
4326run_test ()
4327{
4328 test_mapped_index_find_name_component_bounds ();
4329 test_dw2_expand_symtabs_matching_symbol ();
4330}
4331
c62446b1
PA
4332}} // namespace selftests::dw2_expand_symtabs_matching
4333
4334#endif /* GDB_SELF_TEST */
4335
4b514bc8
JK
4336/* If FILE_MATCHER is NULL or if PER_CU has
4337 dwarf2_per_cu_quick_data::MARK set (see
4338 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4339 EXPANSION_NOTIFY on it. */
4340
df35e626 4341static bool
4b514bc8 4342dw2_expand_symtabs_matching_one
97a1449a
SM
4343 (dwarf2_per_cu_data *per_cu,
4344 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4345 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4346 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4347{
4348 if (file_matcher == NULL || per_cu->v.quick->mark)
4349 {
af758d11 4350 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4351
97a1449a
SM
4352 compunit_symtab *symtab
4353 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4354 gdb_assert (symtab != nullptr);
4b514bc8 4355
af758d11 4356 if (expansion_notify != NULL && symtab_was_null)
df35e626 4357 return expansion_notify (symtab);
4b514bc8 4358 }
df35e626 4359 return true;
4b514bc8
JK
4360}
4361
3f563c84
PA
4362/* Helper for dw2_expand_matching symtabs. Called on each symbol
4363 matched, to expand corresponding CUs that were marked. IDX is the
4364 index of the symbol name that matched. */
4365
df35e626 4366static bool
3f563c84 4367dw2_expand_marked_cus
976ca316 4368 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4369 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4370 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4371 block_search_flags search_flags,
3f563c84
PA
4372 search_domain kind)
4373{
42c2c694 4374 offset_type vec_len, vec_idx;
3f563c84 4375 bool global_seen = false;
976ca316 4376 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4377
42c2c694
TT
4378 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4379 vec_len = vec[0];
61920122
PA
4380 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4381 {
42c2c694 4382 offset_type cu_index_and_attrs = vec[vec_idx + 1];
61920122
PA
4383 /* This value is only valid for index versions >= 7. */
4384 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4385 gdb_index_symbol_kind symbol_kind =
4386 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4387 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4388 /* Only check the symbol attributes if they're present.
4389 Indices prior to version 7 don't record them,
4390 and indices >= 7 may elide them for certain symbols
4391 (gold does this). */
4392 int attrs_valid =
4393 (index.version >= 7
4394 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4395
4396 /* Work around gold/15646. */
f030440d
TV
4397 if (attrs_valid
4398 && !is_static
4399 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4400 {
f030440d 4401 if (global_seen)
61920122 4402 continue;
f030440d
TV
4403
4404 global_seen = true;
61920122 4405 }
3190f0c6 4406
61920122
PA
4407 /* Only check the symbol's kind if it has one. */
4408 if (attrs_valid)
4409 {
03a8ea51
TT
4410 if (is_static)
4411 {
4412 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4413 continue;
4414 }
4415 else
4416 {
4417 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4418 continue;
4419 }
4420
61920122 4421 switch (kind)
8943b874 4422 {
61920122
PA
4423 case VARIABLES_DOMAIN:
4424 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4425 continue;
4426 break;
4427 case FUNCTIONS_DOMAIN:
4428 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4429 continue;
61920122
PA
4430 break;
4431 case TYPES_DOMAIN:
4432 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4433 continue;
4434 break;
59c35742
AB
4435 case MODULES_DOMAIN:
4436 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4437 continue;
4438 break;
61920122
PA
4439 default:
4440 break;
8943b874 4441 }
61920122 4442 }
8943b874 4443
61920122 4444 /* Don't crash on bad data. */
91eea9cc 4445 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
61920122 4446 {
b98664d3 4447 complaint (_(".gdb_index entry has bad CU index"
976ca316 4448 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4449 continue;
4450 }
4451
91eea9cc 4452 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
df35e626
TT
4453 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4454 expansion_notify))
4455 return false;
61920122 4456 }
df35e626
TT
4457
4458 return true;
61920122
PA
4459}
4460
4b514bc8
JK
4461/* If FILE_MATCHER is non-NULL, set all the
4462 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4463 that match FILE_MATCHER. */
4464
61920122 4465static void
4b514bc8 4466dw_expand_symtabs_matching_file_matcher
976ca316 4467 (dwarf2_per_objfile *per_objfile,
ed2dc618 4468 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4469{
4b514bc8 4470 if (file_matcher == NULL)
61920122
PA
4471 return;
4472
4b514bc8
JK
4473 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4474 htab_eq_pointer,
4475 NULL, xcalloc, xfree));
4476 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4477 htab_eq_pointer,
4478 NULL, xcalloc, xfree));
61920122 4479
4b514bc8
JK
4480 /* The rule is CUs specify all the files, including those used by
4481 any TU, so there's no need to scan TUs here. */
61920122 4482
0d305d5c 4483 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4484 {
927aa2e7
JK
4485 QUIT;
4486
91eea9cc
TT
4487 if (per_cu->is_debug_types)
4488 continue;
927aa2e7
JK
4489 per_cu->v.quick->mark = 0;
4490
4491 /* We only need to look at symtabs not already expanded. */
0d305d5c 4492 if (per_objfile->symtab_set_p (per_cu.get ()))
927aa2e7
JK
4493 continue;
4494
0d305d5c
TT
4495 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4496 per_objfile);
927aa2e7
JK
4497 if (file_data == NULL)
4498 continue;
4499
4500 if (htab_find (visited_not_found.get (), file_data) != NULL)
4501 continue;
4502 else if (htab_find (visited_found.get (), file_data) != NULL)
4503 {
4504 per_cu->v.quick->mark = 1;
4505 continue;
4506 }
4507
b76e467d 4508 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4509 {
4510 const char *this_real_name;
4511
4512 if (file_matcher (file_data->file_names[j], false))
4513 {
4514 per_cu->v.quick->mark = 1;
4515 break;
4516 }
4517
4518 /* Before we invoke realpath, which can get expensive when many
4519 files are involved, do a quick comparison of the basenames. */
4520 if (!basenames_may_differ
4521 && !file_matcher (lbasename (file_data->file_names[j]),
4522 true))
4523 continue;
4524
976ca316 4525 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4526 if (file_matcher (this_real_name, false))
4527 {
4528 per_cu->v.quick->mark = 1;
4529 break;
4530 }
4531 }
4532
b76e467d
SM
4533 void **slot = htab_find_slot (per_cu->v.quick->mark
4534 ? visited_found.get ()
4535 : visited_not_found.get (),
4536 file_data, INSERT);
927aa2e7
JK
4537 *slot = file_data;
4538 }
4539}
4540
df35e626 4541bool
af82f89d
TT
4542dwarf2_gdb_index::expand_symtabs_matching
4543 (struct objfile *objfile,
4544 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4545 const lookup_name_info *lookup_name,
4546 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4547 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4548 block_search_flags search_flags,
3bfa51a7 4549 domain_enum domain,
af82f89d 4550 enum search_domain kind)
927aa2e7 4551{
976ca316 4552 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4553
4554 /* index_table is NULL if OBJF_READNOW. */
976ca316 4555 if (!per_objfile->per_bfd->index_table)
df35e626 4556 return true;
927aa2e7 4557
976ca316 4558 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4559
c1a66c06
TV
4560 if (symbol_matcher == NULL && lookup_name == NULL)
4561 {
0d305d5c 4562 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4563 {
4564 QUIT;
4565
0d305d5c 4566 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
df35e626
TT
4567 file_matcher,
4568 expansion_notify))
4569 return false;
c1a66c06 4570 }
df35e626 4571 return true;
c1a66c06
TV
4572 }
4573
976ca316 4574 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4575
df35e626
TT
4576 bool result
4577 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4578 symbol_matcher,
4579 [&] (offset_type idx)
927aa2e7 4580 {
df35e626 4581 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
03a8ea51 4582 expansion_notify, search_flags, kind))
df35e626 4583 return false;
3b00ef10 4584 return true;
976ca316 4585 }, per_objfile);
df35e626
TT
4586
4587 return result;
927aa2e7
JK
4588}
4589
4590/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4591 symtab. */
4592
4593static struct compunit_symtab *
4594recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4595 CORE_ADDR pc)
4596{
4597 int i;
4598
4599 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4600 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4601 return cust;
4602
4603 if (cust->includes == NULL)
4604 return NULL;
4605
4606 for (i = 0; cust->includes[i]; ++i)
4607 {
4608 struct compunit_symtab *s = cust->includes[i];
4609
4610 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4611 if (s != NULL)
4612 return s;
4613 }
4614
4615 return NULL;
4616}
4617
39298a5d
TT
4618struct compunit_symtab *
4619dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4620 (struct objfile *objfile,
4621 struct bound_minimal_symbol msymbol,
4622 CORE_ADDR pc,
4623 struct obj_section *section,
4624 int warn_if_readin)
927aa2e7
JK
4625{
4626 struct dwarf2_per_cu_data *data;
4627 struct compunit_symtab *result;
4628
efd7398e
TT
4629 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4630 if (per_objfile->per_bfd->index_addrmap == nullptr)
927aa2e7
JK
4631 return NULL;
4632
b3b3bada 4633 CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e
TT
4634 data = ((struct dwarf2_per_cu_data *)
4635 addrmap_find (per_objfile->per_bfd->index_addrmap,
4636 pc - baseaddr));
927aa2e7
JK
4637 if (!data)
4638 return NULL;
4639
af758d11 4640 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4641 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4642 paddress (objfile->arch (), pc));
927aa2e7 4643
97a1449a
SM
4644 result = recursively_find_pc_sect_compunit_symtab
4645 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4646
927aa2e7
JK
4647 gdb_assert (result != NULL);
4648 return result;
4649}
4650
39298a5d 4651void
f4655dee
TT
4652dwarf2_base_index_functions::map_symbol_filenames
4653 (struct objfile *objfile,
4654 gdb::function_view<symbol_filename_ftype> fun,
4655 bool need_fullname)
927aa2e7 4656{
976ca316 4657 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4658
9fc29955
AB
4659 /* Use caches to ensure we only call FUN once for each filename. */
4660 filename_seen_cache filenames_cache;
4661 std::unordered_set<quick_file_names *> qfn_cache;
927aa2e7 4662
9fc29955
AB
4663 /* The rule is CUs specify all the files, including those used by any TU,
4664 so there's no need to scan TUs here. We can ignore file names coming
4665 from already-expanded CUs. It is possible that an expanded CU might
4666 reuse the file names data from a currently unexpanded CU, in this
4667 case we don't want to report the files from the unexpanded CU. */
927aa2e7 4668
0d305d5c 4669 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
9fc29955 4670 {
0d305d5c 4671 if (per_objfile->symtab_set_p (per_cu.get ()))
927aa2e7 4672 {
9fc29955
AB
4673 if (per_cu->v.quick->file_names != nullptr)
4674 qfn_cache.insert (per_cu->v.quick->file_names);
927aa2e7 4675 }
9fc29955 4676 }
927aa2e7 4677
0d305d5c 4678 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
9fc29955
AB
4679 {
4680 /* We only need to look at symtabs not already expanded. */
0d305d5c 4681 if (per_objfile->symtab_set_p (per_cu.get ()))
9fc29955 4682 continue;
927aa2e7 4683
0d305d5c
TT
4684 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4685 per_objfile);
9fc29955
AB
4686 if (file_data == nullptr
4687 || qfn_cache.find (file_data) != qfn_cache.end ())
4688 continue;
927aa2e7 4689
9fc29955
AB
4690 for (int j = 0; j < file_data->num_file_names; ++j)
4691 {
4692 const char *filename = file_data->file_names[j];
4693 filenames_cache.seen (filename);
927aa2e7
JK
4694 }
4695 }
4696
9fc29955 4697 filenames_cache.traverse ([&] (const char *filename)
927aa2e7
JK
4698 {
4699 gdb::unique_xmalloc_ptr<char> this_real_name;
4700
4701 if (need_fullname)
4702 this_real_name = gdb_realpath (filename);
f4655dee 4703 fun (filename, this_real_name.get ());
927aa2e7
JK
4704 });
4705}
4706
39298a5d
TT
4707bool
4708dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
927aa2e7 4709{
fae2120b 4710 return true;
927aa2e7
JK
4711}
4712
927aa2e7
JK
4713/* DWARF-5 debug_names reader. */
4714
4715/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4716static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4717
4718/* A helper function that reads the .debug_names section in SECTION
4719 and fills in MAP. FILENAME is the name of the file containing the
4720 section; it is used for error reporting.
4721
4722 Returns true if all went well, false otherwise. */
4723
4724static bool
4725read_debug_names_from_section (struct objfile *objfile,
4726 const char *filename,
4727 struct dwarf2_section_info *section,
4728 mapped_debug_names &map)
4729{
96b79293 4730 if (section->empty ())
927aa2e7
JK
4731 return false;
4732
4733 /* Older elfutils strip versions could keep the section in the main
4734 executable while splitting it for the separate debug info file. */
96b79293 4735 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4736 return false;
4737
96b79293 4738 section->read (objfile);
927aa2e7 4739
08feed99 4740 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4741
4742 const gdb_byte *addr = section->buffer;
4743
96b79293 4744 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4745
4746 unsigned int bytes_read;
4747 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4748 addr += bytes_read;
4749
4750 map.dwarf5_is_dwarf64 = bytes_read != 4;
4751 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4752 if (bytes_read + length != section->size)
4753 {
4754 /* There may be multiple per-CU indices. */
4755 warning (_("Section .debug_names in %s length %s does not match "
4756 "section length %s, ignoring .debug_names."),
4757 filename, plongest (bytes_read + length),
4758 pulongest (section->size));
4759 return false;
4760 }
4761
4762 /* The version number. */
4763 uint16_t version = read_2_bytes (abfd, addr);
4764 addr += 2;
4765 if (version != 5)
4766 {
4767 warning (_("Section .debug_names in %s has unsupported version %d, "
4768 "ignoring .debug_names."),
4769 filename, version);
4770 return false;
4771 }
4772
4773 /* Padding. */
4774 uint16_t padding = read_2_bytes (abfd, addr);
4775 addr += 2;
4776 if (padding != 0)
4777 {
4778 warning (_("Section .debug_names in %s has unsupported padding %d, "
4779 "ignoring .debug_names."),
4780 filename, padding);
4781 return false;
4782 }
4783
4784 /* comp_unit_count - The number of CUs in the CU list. */
4785 map.cu_count = read_4_bytes (abfd, addr);
4786 addr += 4;
4787
4788 /* local_type_unit_count - The number of TUs in the local TU
4789 list. */
4790 map.tu_count = read_4_bytes (abfd, addr);
4791 addr += 4;
4792
4793 /* foreign_type_unit_count - The number of TUs in the foreign TU
4794 list. */
4795 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4796 addr += 4;
4797 if (foreign_tu_count != 0)
4798 {
4799 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4800 "ignoring .debug_names."),
4801 filename, static_cast<unsigned long> (foreign_tu_count));
4802 return false;
4803 }
4804
4805 /* bucket_count - The number of hash buckets in the hash lookup
4806 table. */
4807 map.bucket_count = read_4_bytes (abfd, addr);
4808 addr += 4;
4809
4810 /* name_count - The number of unique names in the index. */
4811 map.name_count = read_4_bytes (abfd, addr);
4812 addr += 4;
4813
4814 /* abbrev_table_size - The size in bytes of the abbreviations
4815 table. */
4816 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4817 addr += 4;
4818
4819 /* augmentation_string_size - The size in bytes of the augmentation
4820 string. This value is rounded up to a multiple of 4. */
4821 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4822 addr += 4;
4823 map.augmentation_is_gdb = ((augmentation_string_size
4824 == sizeof (dwarf5_augmentation))
4825 && memcmp (addr, dwarf5_augmentation,
4826 sizeof (dwarf5_augmentation)) == 0);
4827 augmentation_string_size += (-augmentation_string_size) & 3;
4828 addr += augmentation_string_size;
4829
4830 /* List of CUs */
4831 map.cu_table_reordered = addr;
4832 addr += map.cu_count * map.offset_size;
4833
4834 /* List of Local TUs */
4835 map.tu_table_reordered = addr;
4836 addr += map.tu_count * map.offset_size;
4837
4838 /* Hash Lookup Table */
4839 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4840 addr += map.bucket_count * 4;
4841 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4842 addr += map.name_count * 4;
4843
4844 /* Name Table */
4845 map.name_table_string_offs_reordered = addr;
4846 addr += map.name_count * map.offset_size;
4847 map.name_table_entry_offs_reordered = addr;
4848 addr += map.name_count * map.offset_size;
4849
4850 const gdb_byte *abbrev_table_start = addr;
4851 for (;;)
4852 {
927aa2e7
JK
4853 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4854 addr += bytes_read;
4855 if (index_num == 0)
4856 break;
4857
4858 const auto insertpair
4859 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4860 if (!insertpair.second)
4861 {
4862 warning (_("Section .debug_names in %s has duplicate index %s, "
4863 "ignoring .debug_names."),
4864 filename, pulongest (index_num));
4865 return false;
4866 }
4867 mapped_debug_names::index_val &indexval = insertpair.first->second;
4868 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4869 addr += bytes_read;
4870
4871 for (;;)
4872 {
4873 mapped_debug_names::index_val::attr attr;
4874 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4875 addr += bytes_read;
4876 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4877 addr += bytes_read;
4878 if (attr.form == DW_FORM_implicit_const)
4879 {
4880 attr.implicit_const = read_signed_leb128 (abfd, addr,
4881 &bytes_read);
4882 addr += bytes_read;
4883 }
4884 if (attr.dw_idx == 0 && attr.form == 0)
4885 break;
4886 indexval.attr_vec.push_back (std::move (attr));
4887 }
4888 }
4889 if (addr != abbrev_table_start + abbrev_table_size)
4890 {
4891 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4892 "of size %s vs. written as %u, ignoring .debug_names."),
4893 filename, plongest (addr - abbrev_table_start),
4894 abbrev_table_size);
927aa2e7
JK
4895 return false;
4896 }
4897 map.entry_pool = addr;
4898
4899 return true;
4900}
4901
4902/* A helper for create_cus_from_debug_names that handles the MAP's CU
4903 list. */
4904
4905static void
168c9250 4906create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4907 const mapped_debug_names &map,
4908 dwarf2_section_info &section,
b76e467d 4909 bool is_dwz)
927aa2e7 4910{
3ee6bb11
TV
4911 if (!map.augmentation_is_gdb)
4912 {
fc9a13fb
TV
4913 for (uint32_t i = 0; i < map.cu_count; ++i)
4914 {
4915 sect_offset sect_off
4916 = (sect_offset) (extract_unsigned_integer
4917 (map.cu_table_reordered + i * map.offset_size,
4918 map.offset_size,
4919 map.dwarf5_byte_order));
4920 /* We don't know the length of the CU, because the CU list in a
4921 .debug_names index can be incomplete, so we can't use the start
4922 of the next CU as end of this CU. We create the CUs here with
4923 length 0, and in cutu_reader::cutu_reader we'll fill in the
4924 actual length. */
0d305d5c 4925 std::unique_ptr<dwarf2_per_cu_data> per_cu
fc9a13fb
TV
4926 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4927 sect_off, 0);
0d305d5c 4928 per_bfd->all_comp_units.push_back (std::move (per_cu));
fc9a13fb 4929 }
d3b54e63 4930 return;
3ee6bb11
TV
4931 }
4932
927aa2e7
JK
4933 sect_offset sect_off_prev;
4934 for (uint32_t i = 0; i <= map.cu_count; ++i)
4935 {
4936 sect_offset sect_off_next;
4937 if (i < map.cu_count)
4938 {
4939 sect_off_next
4940 = (sect_offset) (extract_unsigned_integer
4941 (map.cu_table_reordered + i * map.offset_size,
4942 map.offset_size,
4943 map.dwarf5_byte_order));
4944 }
4945 else
4946 sect_off_next = (sect_offset) section.size;
4947 if (i >= 1)
4948 {
4949 const ULONGEST length = sect_off_next - sect_off_prev;
0d305d5c 4950 std::unique_ptr<dwarf2_per_cu_data> per_cu
168c9250 4951 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 4952 sect_off_prev, length);
0d305d5c 4953 per_bfd->all_comp_units.push_back (std::move (per_cu));
927aa2e7
JK
4954 }
4955 sect_off_prev = sect_off_next;
4956 }
4957}
4958
4959/* Read the CU list from the mapped index, and use it to create all
ed2dc618 4960 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
4961
4962static void
168c9250 4963create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4964 const mapped_debug_names &map,
4965 const mapped_debug_names &dwz_map)
4966{
168c9250
SM
4967 gdb_assert (per_bfd->all_comp_units.empty ());
4968 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 4969
168c9250 4970 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 4971 false /* is_dwz */);
927aa2e7
JK
4972
4973 if (dwz_map.cu_count == 0)
4974 return;
4975
168c9250
SM
4976 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4977 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 4978 true /* is_dwz */);
927aa2e7
JK
4979}
4980
4981/* Read .debug_names. If everything went ok, initialize the "quick"
4982 elements of all the CUs and return true. Otherwise, return false. */
4983
4984static bool
976ca316 4985dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 4986{
fcf23d5b
SM
4987 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4988 mapped_debug_names dwz_map;
976ca316 4989 struct objfile *objfile = per_objfile->objfile;
f8c41851 4990 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
4991
4992 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
bab287cd 4993 &per_bfd->debug_names, *map))
927aa2e7
JK
4994 return false;
4995
4996 /* Don't use the index if it's empty. */
22ca247e 4997 if (map->name_count == 0)
927aa2e7
JK
4998 return false;
4999
5000 /* If there is a .dwz file, read it so we can get its CU list as
5001 well. */
f8c41851 5002 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5003 if (dwz != NULL)
5004 {
5005 if (!read_debug_names_from_section (objfile,
00f93c44 5006 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5007 &dwz->debug_names, dwz_map))
5008 {
5009 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5010 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5011 return false;
5012 }
5013 }
5014
f8c41851 5015 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5016
22ca247e 5017 if (map->tu_count != 0)
927aa2e7
JK
5018 {
5019 /* We can only handle a single .debug_types when we have an
5020 index. */
f8c41851 5021 if (per_bfd->types.size () != 1)
927aa2e7
JK
5022 return false;
5023
f8c41851 5024 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5025
5026 create_signatured_type_table_from_debug_names
f8c41851 5027 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5028 }
5029
f8c41851 5030 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5031
f8c41851
SM
5032 per_bfd->debug_names_table = std::move (map);
5033 per_bfd->using_index = 1;
5034 per_bfd->quick_file_names_table =
bab287cd 5035 create_quick_file_names_table (per_bfd->all_comp_units.size ());
927aa2e7
JK
5036
5037 return true;
5038}
5039
927aa2e7
JK
5040/* Type used to manage iterating over all CUs looking for a symbol for
5041 .debug_names. */
5042
5043class dw2_debug_names_iterator
5044{
5045public:
927aa2e7 5046 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5047 block_search_flags block_index,
2b79f376 5048 domain_enum domain,
fcf23d5b 5049 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5050 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5051 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5052 m_per_objfile (per_objfile)
927aa2e7
JK
5053 {}
5054
5055 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5056 search_domain search, uint32_t namei,
3bfa51a7
TT
5057 dwarf2_per_objfile *per_objfile,
5058 domain_enum domain = UNDEF_DOMAIN)
927aa2e7 5059 : m_map (map),
3bfa51a7 5060 m_domain (domain),
927aa2e7 5061 m_search (search),
fcf23d5b
SM
5062 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5063 m_per_objfile (per_objfile)
927aa2e7
JK
5064 {}
5065
3b00ef10 5066 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5067 block_search_flags block_index, domain_enum domain,
fcf23d5b 5068 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5069 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5070 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5071 m_per_objfile (per_objfile)
3b00ef10
TT
5072 {}
5073
927aa2e7
JK
5074 /* Return the next matching CU or NULL if there are no more. */
5075 dwarf2_per_cu_data *next ();
5076
5077private:
5078 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5079 const char *name,
5080 dwarf2_per_objfile *per_objfile);
927aa2e7 5081 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5082 uint32_t namei,
5083 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5084
5085 /* The internalized form of .debug_names. */
5086 const mapped_debug_names &m_map;
5087
03a8ea51
TT
5088 /* Restrict the search to these blocks. */
5089 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
5090 | SEARCH_STATIC_BLOCK);
927aa2e7
JK
5091
5092 /* The kind of symbol we're looking for. */
5093 const domain_enum m_domain = UNDEF_DOMAIN;
5094 const search_domain m_search = ALL_DOMAIN;
5095
5096 /* The list of CUs from the index entry of the symbol, or NULL if
5097 not found. */
5098 const gdb_byte *m_addr;
fcf23d5b
SM
5099
5100 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5101};
5102
5103const char *
fcf23d5b 5104mapped_debug_names::namei_to_name
976ca316 5105 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5106{
5107 const ULONGEST namei_string_offs
5108 = extract_unsigned_integer ((name_table_string_offs_reordered
5109 + namei * offset_size),
5110 offset_size,
5111 dwarf5_byte_order);
976ca316 5112 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5113}
5114
5115/* Find a slot in .debug_names for the object named NAME. If NAME is
5116 found, return pointer to its pool data. If NAME cannot be found,
5117 return NULL. */
5118
5119const gdb_byte *
5120dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5121 (const mapped_debug_names &map, const char *name,
5122 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5123{
5124 int (*cmp) (const char *, const char *);
5125
54ee4252 5126 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5127 if (current_language->la_language == language_cplus
5128 || current_language->la_language == language_fortran
5129 || current_language->la_language == language_d)
5130 {
5131 /* NAME is already canonical. Drop any qualifiers as
5132 .debug_names does not contain any. */
5133
5134 if (strchr (name, '(') != NULL)
5135 {
54ee4252 5136 without_params = cp_remove_params (name);
927aa2e7 5137 if (without_params != NULL)
54ee4252 5138 name = without_params.get ();
927aa2e7
JK
5139 }
5140 }
5141
5142 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5143
5144 const uint32_t full_hash = dwarf5_djb_hash (name);
5145 uint32_t namei
5146 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5147 (map.bucket_table_reordered
5148 + (full_hash % map.bucket_count)), 4,
5149 map.dwarf5_byte_order);
5150 if (namei == 0)
5151 return NULL;
5152 --namei;
5153 if (namei >= map.name_count)
5154 {
b98664d3 5155 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5156 "[in module %s]"),
5157 namei, map.name_count,
fcf23d5b 5158 objfile_name (per_objfile->objfile));
927aa2e7
JK
5159 return NULL;
5160 }
5161
5162 for (;;)
5163 {
5164 const uint32_t namei_full_hash
5165 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5166 (map.hash_table_reordered + namei), 4,
5167 map.dwarf5_byte_order);
5168 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5169 return NULL;
5170
5171 if (full_hash == namei_full_hash)
5172 {
fcf23d5b 5173 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5174
5175#if 0 /* An expensive sanity check. */
5176 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5177 {
b98664d3 5178 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5179 "[in module %s]"),
5180 namei, objfile_name (dwarf2_per_objfile->objfile));
5181 return NULL;
5182 }
5183#endif
5184
5185 if (cmp (namei_string, name) == 0)
5186 {
5187 const ULONGEST namei_entry_offs
5188 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5189 + namei * map.offset_size),
5190 map.offset_size, map.dwarf5_byte_order);
5191 return map.entry_pool + namei_entry_offs;
5192 }
5193 }
5194
5195 ++namei;
5196 if (namei >= map.name_count)
5197 return NULL;
5198 }
5199}
5200
5201const gdb_byte *
5202dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5203 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5204{
5205 if (namei >= map.name_count)
5206 {
b98664d3 5207 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5208 "[in module %s]"),
5209 namei, map.name_count,
fcf23d5b 5210 objfile_name (per_objfile->objfile));
927aa2e7
JK
5211 return NULL;
5212 }
5213
5214 const ULONGEST namei_entry_offs
5215 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5216 + namei * map.offset_size),
5217 map.offset_size, map.dwarf5_byte_order);
5218 return map.entry_pool + namei_entry_offs;
5219}
5220
5221/* See dw2_debug_names_iterator. */
5222
5223dwarf2_per_cu_data *
5224dw2_debug_names_iterator::next ()
5225{
5226 if (m_addr == NULL)
5227 return NULL;
5228
fcf23d5b
SM
5229 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5230 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5231 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5232
5233 again:
5234
5235 unsigned int bytes_read;
5236 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5237 m_addr += bytes_read;
5238 if (abbrev == 0)
5239 return NULL;
5240
5241 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5242 if (indexval_it == m_map.abbrev_map.cend ())
5243 {
b98664d3 5244 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5245 "[in module %s]"),
ed2dc618 5246 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5247 return NULL;
5248 }
5249 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5250 enum class symbol_linkage {
5251 unknown,
5252 static_,
5253 extern_,
23c13d42 5254 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5255 dwarf2_per_cu_data *per_cu = NULL;
5256 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5257 {
5258 ULONGEST ull;
5259 switch (attr.form)
5260 {
5261 case DW_FORM_implicit_const:
5262 ull = attr.implicit_const;
5263 break;
5264 case DW_FORM_flag_present:
5265 ull = 1;
5266 break;
5267 case DW_FORM_udata:
5268 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5269 m_addr += bytes_read;
5270 break;
6dc55ce9 5271 case DW_FORM_ref4:
5272 ull = read_4_bytes (abfd, m_addr);
5273 m_addr += 4;
5274 break;
5275 case DW_FORM_ref8:
5276 ull = read_8_bytes (abfd, m_addr);
5277 m_addr += 8;
5278 break;
5279 case DW_FORM_ref_sig8:
5280 ull = read_8_bytes (abfd, m_addr);
5281 m_addr += 8;
5282 break;
927aa2e7 5283 default:
b98664d3 5284 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5285 dwarf_form_name (attr.form),
ed2dc618 5286 objfile_name (objfile));
927aa2e7
JK
5287 return NULL;
5288 }
5289 switch (attr.dw_idx)
5290 {
5291 case DW_IDX_compile_unit:
5292 /* Don't crash on bad data. */
bab287cd 5293 if (ull >= per_bfd->all_comp_units.size ())
927aa2e7 5294 {
b98664d3 5295 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5296 " [in module %s]"),
5297 pulongest (ull),
fcf23d5b 5298 objfile_name (objfile));
927aa2e7
JK
5299 continue;
5300 }
91eea9cc 5301 per_cu = per_bfd->get_cu (ull);
927aa2e7 5302 break;
8af5c486
JK
5303 case DW_IDX_type_unit:
5304 /* Don't crash on bad data. */
91eea9cc 5305 if (ull >= per_bfd->tu_stats.nr_tus)
8af5c486 5306 {
b98664d3 5307 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5308 " [in module %s]"),
5309 pulongest (ull),
fcf23d5b 5310 objfile_name (objfile));
8af5c486
JK
5311 continue;
5312 }
91eea9cc 5313 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
8af5c486 5314 break;
6dc55ce9 5315 case DW_IDX_die_offset:
5316 /* In a per-CU index (as opposed to a per-module index), index
5317 entries without CU attribute implicitly refer to the single CU. */
5318 if (per_cu == NULL)
fcf23d5b 5319 per_cu = per_bfd->get_cu (0);
6dc55ce9 5320 break;
927aa2e7
JK
5321 case DW_IDX_GNU_internal:
5322 if (!m_map.augmentation_is_gdb)
5323 break;
23c13d42 5324 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5325 break;
5326 case DW_IDX_GNU_external:
5327 if (!m_map.augmentation_is_gdb)
5328 break;
23c13d42 5329 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5330 break;
5331 }
5332 }
5333
5334 /* Skip if already read in. */
fcf23d5b 5335 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5336 goto again;
5337
5338 /* Check static vs global. */
03a8ea51 5339 if (symbol_linkage_ != symbol_linkage::unknown)
927aa2e7 5340 {
03a8ea51
TT
5341 if (symbol_linkage_ == symbol_linkage::static_)
5342 {
5343 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5344 goto again;
5345 }
5346 else
5347 {
5348 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5349 goto again;
5350 }
927aa2e7
JK
5351 }
5352
5353 /* Match dw2_symtab_iter_next, symbol_kind
5354 and debug_names::psymbol_tag. */
5355 switch (m_domain)
5356 {
5357 case VAR_DOMAIN:
5358 switch (indexval.dwarf_tag)
5359 {
5360 case DW_TAG_variable:
5361 case DW_TAG_subprogram:
5362 /* Some types are also in VAR_DOMAIN. */
5363 case DW_TAG_typedef:
5364 case DW_TAG_structure_type:
5365 break;
5366 default:
5367 goto again;
5368 }
5369 break;
5370 case STRUCT_DOMAIN:
5371 switch (indexval.dwarf_tag)
5372 {
5373 case DW_TAG_typedef:
5374 case DW_TAG_structure_type:
5375 break;
5376 default:
5377 goto again;
5378 }
5379 break;
5380 case LABEL_DOMAIN:
5381 switch (indexval.dwarf_tag)
5382 {
5383 case 0:
5384 case DW_TAG_variable:
5385 break;
5386 default:
5387 goto again;
5388 }
5389 break;
59c35742
AB
5390 case MODULE_DOMAIN:
5391 switch (indexval.dwarf_tag)
5392 {
5393 case DW_TAG_module:
5394 break;
5395 default:
5396 goto again;
5397 }
5398 break;
927aa2e7
JK
5399 default:
5400 break;
5401 }
5402
5403 /* Match dw2_expand_symtabs_matching, symbol_kind and
5404 debug_names::psymbol_tag. */
5405 switch (m_search)
4b514bc8 5406 {
927aa2e7
JK
5407 case VARIABLES_DOMAIN:
5408 switch (indexval.dwarf_tag)
4b514bc8 5409 {
927aa2e7
JK
5410 case DW_TAG_variable:
5411 break;
5412 default:
5413 goto again;
4b514bc8 5414 }
927aa2e7
JK
5415 break;
5416 case FUNCTIONS_DOMAIN:
5417 switch (indexval.dwarf_tag)
4b514bc8 5418 {
927aa2e7
JK
5419 case DW_TAG_subprogram:
5420 break;
5421 default:
5422 goto again;
4b514bc8 5423 }
927aa2e7
JK
5424 break;
5425 case TYPES_DOMAIN:
5426 switch (indexval.dwarf_tag)
5427 {
5428 case DW_TAG_typedef:
5429 case DW_TAG_structure_type:
5430 break;
5431 default:
5432 goto again;
5433 }
5434 break;
59c35742
AB
5435 case MODULES_DOMAIN:
5436 switch (indexval.dwarf_tag)
5437 {
5438 case DW_TAG_module:
5439 break;
5440 default:
5441 goto again;
5442 }
927aa2e7
JK
5443 default:
5444 break;
4b514bc8 5445 }
927aa2e7
JK
5446
5447 return per_cu;
4b514bc8 5448}
61920122 5449
927aa2e7
JK
5450/* This dumps minimal information about .debug_names. It is called
5451 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5452 uses this to verify that .debug_names has been loaded. */
9291a0cd 5453
39298a5d
TT
5454void
5455dwarf2_debug_names_index::dump (struct objfile *objfile)
927aa2e7 5456{
976ca316 5457 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5458
976ca316 5459 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5460 printf_filtered (".debug_names:");
976ca316 5461 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5462 printf_filtered (" exists\n");
5463 else
5464 printf_filtered (" faked for \"readnow\"\n");
5465 printf_filtered ("\n");
9291a0cd
TT
5466}
5467
39298a5d 5468void
0b7b2c2a 5469dwarf2_debug_names_index::expand_matching_symbols
3b00ef10
TT
5470 (struct objfile *objfile,
5471 const lookup_name_info &name, domain_enum domain,
5472 int global,
3b00ef10
TT
5473 symbol_compare_ftype *ordered_compare)
5474{
976ca316 5475 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5476
5477 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5478 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5479 return;
5480
976ca316 5481 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
03a8ea51
TT
5482 const block_search_flags block_flags
5483 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
3b00ef10
TT
5484
5485 const char *match_name = name.ada ().lookup_name ().c_str ();
5486 auto matcher = [&] (const char *symname)
5487 {
5488 if (ordered_compare == nullptr)
5489 return true;
5490 return ordered_compare (symname, match_name) == 0;
5491 };
5492
b64f703b 5493 dw2_expand_symtabs_matching_symbol (map, name, matcher,
3b00ef10
TT
5494 [&] (offset_type namei)
5495 {
5496 /* The name was matched, now expand corresponding CUs that were
5497 marked. */
03a8ea51 5498 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
976ca316 5499 per_objfile);
3b00ef10
TT
5500
5501 struct dwarf2_per_cu_data *per_cu;
5502 while ((per_cu = iter.next ()) != NULL)
976ca316 5503 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5504 nullptr);
3b00ef10 5505 return true;
976ca316 5506 }, per_objfile);
3b00ef10
TT
5507}
5508
df35e626 5509bool
39298a5d 5510dwarf2_debug_names_index::expand_symtabs_matching
927aa2e7
JK
5511 (struct objfile *objfile,
5512 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5513 const lookup_name_info *lookup_name,
927aa2e7
JK
5514 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5515 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 5516 block_search_flags search_flags,
3bfa51a7 5517 domain_enum domain,
927aa2e7
JK
5518 enum search_domain kind)
5519{
976ca316 5520 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5521
927aa2e7 5522 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5523 if (!per_objfile->per_bfd->debug_names_table)
df35e626 5524 return true;
9291a0cd 5525
976ca316 5526 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5527
c1a66c06
TV
5528 if (symbol_matcher == NULL && lookup_name == NULL)
5529 {
0d305d5c 5530 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5531 {
5532 QUIT;
5533
0d305d5c 5534 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
df35e626
TT
5535 file_matcher,
5536 expansion_notify))
5537 return false;
c1a66c06 5538 }
df35e626 5539 return true;
c1a66c06
TV
5540 }
5541
976ca316 5542 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5543
df35e626
TT
5544 bool result
5545 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5546 symbol_matcher,
5547 [&] (offset_type namei)
927aa2e7 5548 {
927aa2e7
JK
5549 /* The name was matched, now expand corresponding CUs that were
5550 marked. */
3bfa51a7 5551 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
bbf2f4df 5552
927aa2e7
JK
5553 struct dwarf2_per_cu_data *per_cu;
5554 while ((per_cu = iter.next ()) != NULL)
df35e626
TT
5555 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5556 file_matcher,
5557 expansion_notify))
5558 return false;
3b00ef10 5559 return true;
976ca316 5560 }, per_objfile);
df35e626
TT
5561
5562 return result;
9291a0cd
TT
5563}
5564
4485a1c1 5565/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5566 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5567
5568template <typename T>
5569static gdb::array_view<const gdb_byte>
5570get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5571{
5572 dwarf2_section_info *section = &section_owner->gdb_index;
5573
96b79293 5574 if (section->empty ())
4485a1c1
SM
5575 return {};
5576
5577 /* Older elfutils strip versions could keep the section in the main
5578 executable while splitting it for the separate debug info file. */
96b79293 5579 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5580 return {};
5581
96b79293 5582 section->read (obj);
4485a1c1 5583
8bebfcda
PA
5584 /* dwarf2_section_info::size is a bfd_size_type, while
5585 gdb::array_view works with size_t. On 32-bit hosts, with
5586 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5587 is 32-bit. So we need an explicit narrowing conversion here.
5588 This is fine, because it's impossible to allocate or mmap an
5589 array/buffer larger than what size_t can represent. */
5590 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5591}
5592
87d6a7aa
SM
5593/* Lookup the index cache for the contents of the index associated to
5594 DWARF2_OBJ. */
5595
5596static gdb::array_view<const gdb_byte>
5989a64e 5597get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5598{
5599 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5600 if (build_id == nullptr)
5601 return {};
5602
5603 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5604 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5605}
5606
5607/* Same as the above, but for DWZ. */
5608
5609static gdb::array_view<const gdb_byte>
5610get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5611{
5612 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5613 if (build_id == nullptr)
5614 return {};
5615
5616 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5617}
5618
edc02ceb 5619/* See dwarf2/public.h. */
9291a0cd 5620
edc02ceb
TT
5621void
5622dwarf2_initialize_objfile (struct objfile *objfile)
9291a0cd 5623{
976ca316
SM
5624 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5625 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5626
850ed749
SM
5627 dwarf_read_debug_printf ("called");
5628
9291a0cd
TT
5629 /* If we're about to read full symbols, don't bother with the
5630 indices. In this case we also don't care if some other debug
5631 format is making psymtabs, because they are all about to be
5632 expanded anyway. */
5633 if ((objfile->flags & OBJF_READNOW))
5634 {
850ed749
SM
5635 dwarf_read_debug_printf ("readnow requested");
5636
17ee85fc
TT
5637 /* When using READNOW, the using_index flag (set below) indicates that
5638 PER_BFD was already initialized, when we loaded some other objfile. */
5639 if (per_bfd->using_index)
5640 {
850ed749 5641 dwarf_read_debug_printf ("using_index already set");
976ca316 5642 per_objfile->resize_symtabs ();
edc02ceb
TT
5643 objfile->qf.push_front (make_dwarf_gdb_index ());
5644 return;
17ee85fc
TT
5645 }
5646
5647 per_bfd->using_index = 1;
976ca316 5648 create_all_comp_units (per_objfile);
17ee85fc
TT
5649 per_bfd->quick_file_names_table
5650 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5651 per_objfile->resize_symtabs ();
9291a0cd 5652
91eea9cc 5653 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
9291a0cd 5654 {
91eea9cc 5655 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
9291a0cd 5656
17ee85fc 5657 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5658 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5659 }
5660
edc02ceb
TT
5661 /* Arrange for gdb to see the "quick" functions. However, these
5662 functions will be no-ops because we will have expanded all
5663 symtabs. */
5664 objfile->qf.push_front (make_dwarf_gdb_index ());
5665 return;
9291a0cd
TT
5666 }
5667
17ee85fc
TT
5668 /* Was a debug names index already read when we processed an objfile sharing
5669 PER_BFD? */
5670 if (per_bfd->debug_names_table != nullptr)
5671 {
850ed749 5672 dwarf_read_debug_printf ("re-using shared debug names table");
976ca316 5673 per_objfile->resize_symtabs ();
edc02ceb
TT
5674 objfile->qf.push_front (make_dwarf_debug_names ());
5675 return;
17ee85fc
TT
5676 }
5677
5678 /* Was a GDB index already read when we processed an objfile sharing
5679 PER_BFD? */
5680 if (per_bfd->index_table != nullptr)
5681 {
850ed749 5682 dwarf_read_debug_printf ("re-using shared index table");
976ca316 5683 per_objfile->resize_symtabs ();
edc02ceb
TT
5684 objfile->qf.push_front (make_dwarf_gdb_index ());
5685 return;
17ee85fc
TT
5686 }
5687
efb763a5
SM
5688 /* There might already be partial symtabs built for this BFD. This happens
5689 when loading the same binary twice with the index-cache enabled. If so,
5690 don't try to read an index. The objfile / per_objfile initialization will
5691 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5692 code path. */
5693 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
5694 {
5695 dwarf_read_debug_printf ("re-using shared partial symtabs");
edc02ceb
TT
5696 objfile->qf.push_front (make_lazy_dwarf_reader ());
5697 return;
850ed749 5698 }
efb763a5 5699
976ca316 5700 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 5701 {
850ed749 5702 dwarf_read_debug_printf ("found debug names");
976ca316 5703 per_objfile->resize_symtabs ();
edc02ceb
TT
5704 objfile->qf.push_front (make_dwarf_debug_names ());
5705 return;
3c0aa29a 5706 }
927aa2e7 5707
976ca316 5708 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 5709 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5710 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 5711 {
850ed749 5712 dwarf_read_debug_printf ("found gdb index from file");
976ca316 5713 per_objfile->resize_symtabs ();
edc02ceb
TT
5714 objfile->qf.push_front (make_dwarf_gdb_index ());
5715 return;
3c0aa29a 5716 }
9291a0cd 5717
87d6a7aa 5718 /* ... otherwise, try to find the index in the index cache. */
976ca316 5719 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
5720 get_gdb_index_contents_from_cache,
5721 get_gdb_index_contents_from_cache_dwz))
5722 {
850ed749 5723 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa 5724 global_index_cache.hit ();
976ca316 5725 per_objfile->resize_symtabs ();
edc02ceb
TT
5726 objfile->qf.push_front (make_dwarf_gdb_index ());
5727 return;
87d6a7aa
SM
5728 }
5729
5730 global_index_cache.miss ();
edc02ceb 5731 objfile->qf.push_front (make_lazy_dwarf_reader ());
9291a0cd
TT
5732}
5733
5734\f
5735
dce234bc
PP
5736/* Build a partial symbol table. */
5737
5738void
eb36a3eb 5739dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
dce234bc 5740{
976ca316
SM
5741 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5742 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
5743
5744 if (per_bfd->partial_symtabs != nullptr)
5745 {
5746 /* Partial symbols were already read, so now we can simply
5747 attach them. */
eb36a3eb
TT
5748 if (psf == nullptr)
5749 {
5750 psf = new psymbol_functions (per_bfd->partial_symtabs);
5751 objfile->qf.emplace_front (psf);
5752 }
5753 else
5754 psf->set_partial_symtabs (per_bfd->partial_symtabs);
976ca316 5755 per_objfile->resize_symtabs ();
17ee85fc
TT
5756 return;
5757 }
c9bf0622 5758
eb36a3eb
TT
5759 if (psf == nullptr)
5760 {
5761 psf = new psymbol_functions;
5762 objfile->qf.emplace_front (psf);
5763 }
5764 const std::shared_ptr<psymtab_storage> &partial_symtabs
5765 = psf->get_partial_symtabs ();
5766
51962708
TT
5767 /* Set the local reference to partial symtabs, so that we don't try
5768 to read them again if reading another objfile with the same BFD.
5769 If we can't in fact share, this won't make a difference anyway as
5770 the dwarf2_per_bfd object won't be shared. */
eb36a3eb 5771 per_bfd->partial_symtabs = partial_symtabs;
51962708 5772
a70b8144 5773 try
c9bf0622
TT
5774 {
5775 /* This isn't really ideal: all the data we allocate on the
5776 objfile's obstack is still uselessly kept around. However,
5777 freeing it seems unsafe. */
eb36a3eb 5778 psymtab_discarder psymtabs (partial_symtabs.get ());
976ca316 5779 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 5780 psymtabs.keep ();
87d6a7aa 5781
976ca316 5782 per_objfile->resize_symtabs ();
af758d11 5783
87d6a7aa 5784 /* (maybe) store an index in the cache. */
976ca316 5785 global_index_cache.store (per_objfile);
c9bf0622 5786 }
230d2906 5787 catch (const gdb_exception_error &except)
492d29ea
PA
5788 {
5789 exception_print (gdb_stderr, except);
5790 }
c906108c 5791}
c906108c 5792
3b80fe9b
DE
5793/* Find the base address of the compilation unit for range lists and
5794 location lists. It will normally be specified by DW_AT_low_pc.
5795 In DWARF-3 draft 4, the base address could be overridden by
5796 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5797 compilation units with discontinuous ranges. */
5798
5799static void
5800dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5801{
5802 struct attribute *attr;
5803
2b24b6e4 5804 cu->base_address.reset ();
3b80fe9b
DE
5805
5806 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5807 if (attr != nullptr)
95f982e5 5808 cu->base_address = attr->as_address ();
3b80fe9b
DE
5809 else
5810 {
5811 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5812 if (attr != nullptr)
95f982e5 5813 cu->base_address = attr->as_address ();
3b80fe9b
DE
5814 }
5815}
5816
36586728
TT
5817/* Helper function that returns the proper abbrev section for
5818 THIS_CU. */
5819
5820static struct dwarf2_section_info *
5821get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5822{
5823 struct dwarf2_section_info *abbrev;
c3699833 5824 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
5825
5826 if (this_cu->is_dwz)
a7308ce0 5827 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 5828 else
c3699833 5829 abbrev = &per_bfd->abbrev;
36586728
TT
5830
5831 return abbrev;
5832}
5833
f4dc4d17
DE
5834/* Fetch the abbreviation table offset from a comp or type unit header. */
5835
5836static sect_offset
976ca316 5837read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 5838 struct dwarf2_section_info *section,
9c541725 5839 sect_offset sect_off)
f4dc4d17 5840{
96b79293 5841 bfd *abfd = section->get_bfd_owner ();
d521ce57 5842 const gdb_byte *info_ptr;
ac298888 5843 unsigned int initial_length_size, offset_size;
43988095 5844 uint16_t version;
f4dc4d17 5845
976ca316 5846 section->read (per_objfile->objfile);
9c541725 5847 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5848 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5849 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5850 info_ptr += initial_length_size;
5851
5852 version = read_2_bytes (abfd, info_ptr);
5853 info_ptr += 2;
5854 if (version >= 5)
5855 {
5856 /* Skip unit type and address size. */
5857 info_ptr += 2;
5858 }
5859
24aa364d 5860 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5861}
5862
b83470bf
TT
5863/* A partial symtab that is used only for include files. */
5864struct dwarf2_include_psymtab : public partial_symtab
5865{
7b249e47
TT
5866 dwarf2_include_psymtab (const char *filename,
5867 psymtab_storage *partial_symtabs,
0072c873
SM
5868 objfile_per_bfd_storage *objfile_per_bfd)
5869 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
b83470bf
TT
5870 {
5871 }
5872
5873 void read_symtab (struct objfile *objfile) override
5874 {
194d088f
TV
5875 /* It's an include file, no symbols to read for it.
5876 Everything is in the includer symtab. */
5877
5878 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5879 expansion of the includer psymtab. We use the dependencies[0] field to
5880 model the includer. But if we go the regular route of calling
5881 expand_psymtab here, and having expand_psymtab call expand_dependencies
5882 to expand the includer, we'll only use expand_psymtab on the includer
5883 (making it a non-toplevel psymtab), while if we expand the includer via
5884 another path, we'll use read_symtab (making it a toplevel psymtab).
5885 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5886 psymtab, and trigger read_symtab on the includer here directly. */
5887 includer ()->read_symtab (objfile);
b83470bf
TT
5888 }
5889
5890 void expand_psymtab (struct objfile *objfile) override
5891 {
194d088f
TV
5892 /* This is not called by read_symtab, and should not be called by any
5893 expand_dependencies. */
5894 gdb_assert (false);
b83470bf
TT
5895 }
5896
5717c425 5897 bool readin_p (struct objfile *objfile) const override
b83470bf 5898 {
5717c425 5899 return includer ()->readin_p (objfile);
b83470bf
TT
5900 }
5901
5717c425 5902 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
5903 {
5904 return nullptr;
5905 }
5906
5907private:
194d088f
TV
5908 partial_symtab *includer () const
5909 {
5910 /* An include psymtab has exactly one dependency: the psymtab that
5911 includes it. */
5912 gdb_assert (this->number_of_dependencies == 1);
5913 return this->dependencies[0];
5914 }
b83470bf
TT
5915};
5916
aaa75496
JB
5917/* Allocate a new partial symtab for file named NAME and mark this new
5918 partial symtab as being an include of PST. */
5919
5920static void
84685904 5921dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
7b249e47
TT
5922 const char *name,
5923 dwarf2_psymtab *pst,
5924 psymtab_storage *partial_symtabs,
0072c873 5925 objfile_per_bfd_storage *objfile_per_bfd)
aaa75496 5926{
7b249e47 5927 dwarf2_include_psymtab *subpst
0072c873 5928 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
aaa75496 5929
fbd9ab74 5930 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 5931 subpst->dirname = pst->dirname;
fbd9ab74 5932
84685904 5933 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5934 subpst->dependencies[0] = pst;
5935 subpst->number_of_dependencies = 1;
aaa75496
JB
5936}
5937
5938/* Read the Line Number Program data and extract the list of files
5939 included by the source file represented by PST. Build an include
d85a05f0 5940 partial symtab for each of these included files. */
aaa75496
JB
5941
5942static void
5943dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5944 struct die_info *die,
891813be 5945 dwarf2_psymtab *pst)
aaa75496 5946{
fff8551c 5947 line_header_up lh;
d85a05f0 5948 struct attribute *attr;
aaa75496 5949
d85a05f0 5950 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
5951 if (attr != nullptr && attr->form_is_unsigned ())
5952 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
5953 if (lh == NULL)
5954 return; /* No linetable, so no includes. */
5955
79748972
TT
5956 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5957 that we pass in the raw text_low here; that is ok because we're
5958 only decoding the line table to make include partial symtabs, and
5959 so the addresses aren't really used. */
4ae976d1 5960 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5961 pst->raw_text_low (), 1);
aaa75496
JB
5962}
5963
348e048f 5964static hashval_t
52dc124a 5965hash_signatured_type (const void *item)
348e048f 5966{
9a3c8263
SM
5967 const struct signatured_type *sig_type
5968 = (const struct signatured_type *) item;
9a619af0 5969
348e048f 5970 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5971 return sig_type->signature;
348e048f
DE
5972}
5973
5974static int
52dc124a 5975eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5976{
9a3c8263
SM
5977 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5978 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5979
348e048f
DE
5980 return lhs->signature == rhs->signature;
5981}
5982
1fd400ff
TT
5983/* Allocate a hash table for signatured types. */
5984
b0b6a987 5985static htab_up
298e9637 5986allocate_signatured_type_table ()
1fd400ff 5987{
b0b6a987
TT
5988 return htab_up (htab_create_alloc (41,
5989 hash_signatured_type,
5990 eq_signatured_type,
5991 NULL, xcalloc, xfree));
1fd400ff
TT
5992}
5993
78d4d2c5 5994/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5995 and fill them into TYPES_HTAB. It will process only type units,
5996 therefore DW_UT_type. */
c88ee1f0 5997
78d4d2c5 5998static void
976ca316 5999create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6000 struct dwo_file *dwo_file,
b0b6a987 6001 dwarf2_section_info *section, htab_up &types_htab,
43988095 6002 rcuh_kind section_kind)
348e048f 6003{
976ca316 6004 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6005 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6006 bfd *abfd;
6007 const gdb_byte *info_ptr, *end_ptr;
348e048f 6008
b8efb248 6009 abbrev_section = &dwo_file->sections.abbrev;
4bdcc0c1 6010
4800761a 6011 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
6012 section->get_name (),
6013 abbrev_section->get_file_name ());
09406207 6014
96b79293 6015 section->read (objfile);
78d4d2c5 6016 info_ptr = section->buffer;
348e048f 6017
78d4d2c5
JK
6018 if (info_ptr == NULL)
6019 return;
348e048f 6020
78d4d2c5
JK
6021 /* We can't set abfd until now because the section may be empty or
6022 not present, in which case the bfd is unknown. */
96b79293 6023 abfd = section->get_bfd_owner ();
348e048f 6024
c0ab21c2
TT
6025 /* We don't use cutu_reader here because we don't need to read
6026 any dies: the signature is in the header. */
3019eac3 6027
78d4d2c5
JK
6028 end_ptr = info_ptr + section->size;
6029 while (info_ptr < end_ptr)
6030 {
0d305d5c 6031 std::unique_ptr<signatured_type> sig_type;
78d4d2c5
JK
6032 struct dwo_unit *dwo_tu;
6033 void **slot;
6034 const gdb_byte *ptr = info_ptr;
6035 struct comp_unit_head header;
6036 unsigned int length;
8b70b953 6037
9c541725 6038 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6039
a49dd8dd
JK
6040 /* Initialize it due to a false compiler warning. */
6041 header.signature = -1;
9c541725 6042 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6043
78d4d2c5
JK
6044 /* We need to read the type's signature in order to build the hash
6045 table, but we don't need anything else just yet. */
348e048f 6046
976ca316 6047 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6048 abbrev_section, ptr, section_kind);
348e048f 6049
4057dfde 6050 length = header.get_length ();
6caca83c 6051
78d4d2c5
JK
6052 /* Skip dummy type units. */
6053 if (ptr >= info_ptr + length
43988095 6054 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6055 || (header.unit_type != DW_UT_type
6056 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6057 {
6058 info_ptr += length;
6059 continue;
6060 }
dee91e82 6061
78d4d2c5 6062 if (types_htab == NULL)
b8efb248 6063 types_htab = allocate_dwo_unit_table ();
8b70b953 6064
b8efb248
TT
6065 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6066 dwo_tu->dwo_file = dwo_file;
6067 dwo_tu->signature = header.signature;
6068 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6069 dwo_tu->section = section;
6070 dwo_tu->sect_off = sect_off;
6071 dwo_tu->length = length;
78d4d2c5 6072
b8efb248 6073 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
78d4d2c5
JK
6074 gdb_assert (slot != NULL);
6075 if (*slot != NULL)
b8efb248
TT
6076 complaint (_("debug type entry at offset %s is duplicate to"
6077 " the entry at offset %s, signature %s"),
6078 sect_offset_str (sect_off),
6079 sect_offset_str (dwo_tu->sect_off),
6080 hex_string (header.signature));
6081 *slot = dwo_tu;
3019eac3 6082
6f738b01
SM
6083 dwarf_read_debug_printf_v (" offset %s, signature %s",
6084 sect_offset_str (sect_off),
6085 hex_string (header.signature));
3019eac3 6086
78d4d2c5
JK
6087 info_ptr += length;
6088 }
6089}
3019eac3 6090
78d4d2c5
JK
6091/* Create the hash table of all entries in the .debug_types
6092 (or .debug_types.dwo) section(s).
b8efb248 6093 DWO_FILE is a pointer to the DWO file object.
b3c8eb43 6094
78d4d2c5 6095 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6096
78d4d2c5 6097 Note: This function processes DWO files only, not DWP files. */
348e048f 6098
78d4d2c5 6099static void
976ca316 6100create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6101 struct dwo_file *dwo_file,
fd5866f6 6102 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6103 htab_up &types_htab)
78d4d2c5 6104{
fd5866f6 6105 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6106 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6107 rcuh_kind::TYPE);
3019eac3
DE
6108}
6109
5989a64e 6110/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6111 If SLOT is non-NULL, it is the entry to use in the hash table.
6112 Otherwise we find one. */
6113
6114static struct signatured_type *
976ca316 6115add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6116{
91eea9cc
TT
6117 if (per_objfile->per_bfd->all_comp_units.size ()
6118 == per_objfile->per_bfd->all_comp_units.capacity ())
976ca316 6119 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6120
0d305d5c
TT
6121 std::unique_ptr<signatured_type> sig_type_holder
6122 = per_objfile->per_bfd->allocate_signatured_type ();
6123 signatured_type *sig_type = sig_type_holder.get ();
b2bdb8cf 6124
976ca316 6125 per_objfile->resize_symtabs ();
af758d11 6126
91eea9cc 6127 per_objfile->per_bfd->all_comp_units.push_back (std::move (sig_type_holder));
6aa5f3a6 6128 sig_type->signature = sig;
0d305d5c 6129 sig_type->is_debug_types = 1;
976ca316 6130 if (per_objfile->per_bfd->using_index)
6aa5f3a6 6131 {
0d305d5c 6132 sig_type->v.quick =
976ca316 6133 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6134 struct dwarf2_per_cu_quick_data);
6135 }
6136
6137 if (slot == NULL)
6138 {
976ca316 6139 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6140 sig_type, INSERT);
6141 }
6142 gdb_assert (*slot == NULL);
6143 *slot = sig_type;
6144 /* The rest of sig_type must be filled in by the caller. */
6145 return sig_type;
6146}
6147
a2ce51a0
DE
6148/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6149 Fill in SIG_ENTRY with DWO_ENTRY. */
6150
6151static void
976ca316 6152fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6153 struct signatured_type *sig_entry,
6154 struct dwo_unit *dwo_entry)
6155{
976ca316 6156 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6157
7ee85ab1 6158 /* Make sure we're not clobbering something we don't expect to. */
0d305d5c
TT
6159 gdb_assert (! sig_entry->queued);
6160 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
1859c670 6161 if (per_bfd->using_index)
6aa5f3a6 6162 {
0d305d5c
TT
6163 gdb_assert (sig_entry->v.quick != NULL);
6164 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6aa5f3a6
DE
6165 }
6166 else
0d305d5c 6167 gdb_assert (sig_entry->v.psymtab == NULL);
a2ce51a0 6168 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6169 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6170 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6171 gdb_assert (sig_entry->dwo_unit == NULL);
6172
0d305d5c
TT
6173 sig_entry->section = dwo_entry->section;
6174 sig_entry->sect_off = dwo_entry->sect_off;
6175 sig_entry->length = dwo_entry->length;
6176 sig_entry->reading_dwo_directly = 1;
6177 sig_entry->per_bfd = per_bfd;
a2ce51a0
DE
6178 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6179 sig_entry->dwo_unit = dwo_entry;
6180}
6181
6182/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6183 If we haven't read the TU yet, create the signatured_type data structure
6184 for a TU to be read in directly from a DWO file, bypassing the stub.
6185 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6186 using .gdb_index, then when reading a CU we want to stay in the DWO file
6187 containing that CU. Otherwise we could end up reading several other DWO
6188 files (due to comdat folding) to process the transitive closure of all the
6189 mentioned TUs, and that can be slow. The current DWO file will have every
6190 type signature that it needs.
a2ce51a0
DE
6191 We only do this for .gdb_index because in the psymtab case we already have
6192 to read all the DWOs to build the type unit groups. */
6193
6194static struct signatured_type *
6195lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6196{
976ca316 6197 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6198 struct dwo_file *dwo_file;
6199 struct dwo_unit find_dwo_entry, *dwo_entry;
6200 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6201 void **slot;
a2ce51a0 6202
976ca316 6203 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6204
6aa5f3a6
DE
6205 /* If TU skeletons have been removed then we may not have read in any
6206 TUs yet. */
976ca316
SM
6207 if (per_objfile->per_bfd->signatured_types == NULL)
6208 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6209
6210 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6211 Use the global signatured_types array to do our own comdat-folding
6212 of types. If this is the first time we're reading this TU, and
6213 the TU has an entry in .gdb_index, replace the recorded data from
6214 .gdb_index with this TU. */
a2ce51a0 6215
a2ce51a0 6216 find_sig_entry.signature = sig;
976ca316 6217 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6218 &find_sig_entry, INSERT);
9a3c8263 6219 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6220
6221 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6222 read. Don't reassign the global entry to point to this DWO if that's
6223 the case. Also note that if the TU is already being read, it may not
6224 have come from a DWO, the program may be a mix of Fission-compiled
6225 code and non-Fission-compiled code. */
6226
6227 /* Have we already tried to read this TU?
6228 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6229 needn't exist in the global table yet). */
0d305d5c 6230 if (sig_entry != NULL && sig_entry->tu_read)
a2ce51a0
DE
6231 return sig_entry;
6232
6aa5f3a6
DE
6233 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6234 dwo_unit of the TU itself. */
6235 dwo_file = cu->dwo_unit->dwo_file;
6236
a2ce51a0
DE
6237 /* Ok, this is the first time we're reading this TU. */
6238 if (dwo_file->tus == NULL)
6239 return NULL;
6240 find_dwo_entry.signature = sig;
b0b6a987
TT
6241 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6242 &find_dwo_entry);
a2ce51a0
DE
6243 if (dwo_entry == NULL)
6244 return NULL;
6245
6aa5f3a6
DE
6246 /* If the global table doesn't have an entry for this TU, add one. */
6247 if (sig_entry == NULL)
976ca316 6248 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6249
976ca316 6250 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
0d305d5c 6251 sig_entry->tu_read = 1;
a2ce51a0
DE
6252 return sig_entry;
6253}
6254
a2ce51a0
DE
6255/* Subroutine of lookup_signatured_type.
6256 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6257 then try the DWP file. If the TU stub (skeleton) has been removed then
6258 it won't be in .gdb_index. */
a2ce51a0
DE
6259
6260static struct signatured_type *
6261lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6262{
976ca316
SM
6263 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6264 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6265 struct dwo_unit *dwo_entry;
6266 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6267 void **slot;
a2ce51a0 6268
976ca316 6269 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6270 gdb_assert (dwp_file != NULL);
6271
6aa5f3a6
DE
6272 /* If TU skeletons have been removed then we may not have read in any
6273 TUs yet. */
976ca316
SM
6274 if (per_objfile->per_bfd->signatured_types == NULL)
6275 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6276
6aa5f3a6 6277 find_sig_entry.signature = sig;
976ca316 6278 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6279 &find_sig_entry, INSERT);
9a3c8263 6280 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6281
6282 /* Have we already tried to read this TU?
6283 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6284 needn't exist in the global table yet). */
6285 if (sig_entry != NULL)
6286 return sig_entry;
6287
a2ce51a0
DE
6288 if (dwp_file->tus == NULL)
6289 return NULL;
976ca316
SM
6290 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6291 1 /* is_debug_types */);
a2ce51a0
DE
6292 if (dwo_entry == NULL)
6293 return NULL;
6294
976ca316
SM
6295 sig_entry = add_type_unit (per_objfile, sig, slot);
6296 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6297
a2ce51a0
DE
6298 return sig_entry;
6299}
6300
380bca97 6301/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6302 Returns NULL if signature SIG is not present in the table.
6303 It is up to the caller to complain about this. */
348e048f
DE
6304
6305static struct signatured_type *
a2ce51a0 6306lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6307{
976ca316 6308 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6309
976ca316 6310 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6311 {
6312 /* We're in a DWO/DWP file, and we're using .gdb_index.
6313 These cases require special processing. */
976ca316 6314 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6315 return lookup_dwo_signatured_type (cu, sig);
6316 else
6317 return lookup_dwp_signatured_type (cu, sig);
6318 }
6319 else
6320 {
6321 struct signatured_type find_entry, *entry;
348e048f 6322
976ca316 6323 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6324 return NULL;
6325 find_entry.signature = sig;
9a3c8263 6326 entry = ((struct signatured_type *)
976ca316 6327 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6328 &find_entry));
a2ce51a0
DE
6329 return entry;
6330 }
348e048f 6331}
18a8505e 6332
42e7ad6c 6333/* Low level DIE reading support. */
348e048f 6334
d85a05f0
DJ
6335/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6336
6337static void
6338init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6339 struct dwarf2_cu *cu,
3019eac3 6340 struct dwarf2_section_info *section,
685af9cd
TT
6341 struct dwo_file *dwo_file,
6342 struct abbrev_table *abbrev_table)
d85a05f0 6343{
fceca515 6344 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6345 reader->abfd = section->get_bfd_owner ();
d85a05f0 6346 reader->cu = cu;
3019eac3 6347 reader->dwo_file = dwo_file;
dee91e82
DE
6348 reader->die_section = section;
6349 reader->buffer = section->buffer;
f664829e 6350 reader->buffer_end = section->buffer + section->size;
685af9cd 6351 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6352}
6353
c0ab21c2 6354/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6355 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6356 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6357 already.
6358
6359 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6360 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6361 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6362 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6363 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6364 STUB_COMP_DIR may be non-NULL.
3e225074 6365 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6366 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6367 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6368 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6369 kept around for at least as long as *RESULT_READER.
6370
b0c7bfa9
DE
6371 The result is non-zero if a valid (non-dummy) DIE was found. */
6372
6373static int
4ab09049 6374read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6375 struct dwo_unit *dwo_unit,
b0c7bfa9 6376 struct die_info *stub_comp_unit_die,
a2ce51a0 6377 const char *stub_comp_dir,
b0c7bfa9 6378 struct die_reader_specs *result_reader,
d521ce57 6379 const gdb_byte **result_info_ptr,
b0c7bfa9 6380 struct die_info **result_comp_unit_die,
685af9cd 6381 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6382{
976ca316 6383 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6384 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6385 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6386 bfd *abfd;
d521ce57 6387 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6388 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6389 int i,num_extra_attrs;
6390 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6391 struct die_info *comp_unit_die;
6392
b0aeadb3
DE
6393 /* At most one of these may be provided. */
6394 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6395
b0c7bfa9
DE
6396 /* These attributes aren't processed until later:
6397 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6398 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6399 referenced later. However, these attributes are found in the stub
6400 which we won't have later. In order to not impose this complication
6401 on the rest of the code, we read them here and copy them to the
6402 DWO CU/TU die. */
b0c7bfa9
DE
6403
6404 stmt_list = NULL;
6405 low_pc = NULL;
6406 high_pc = NULL;
6407 ranges = NULL;
6408 comp_dir = NULL;
6409
6410 if (stub_comp_unit_die != NULL)
6411 {
6412 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6413 DWO file. */
4ab09049 6414 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6415 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6416 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6417 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6418 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6419 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6420
a39fdb41 6421 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6422
2b0c7f41
SM
6423 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6424 We need the value before we can process DW_AT_ranges values from the
6425 DWO. */
6426 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6427
6428 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6429 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6430 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6431 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6432 section. */
6433 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6434 }
a2ce51a0
DE
6435 else if (stub_comp_dir != NULL)
6436 {
6437 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6438 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6439 comp_dir->name = DW_AT_comp_dir;
6440 comp_dir->form = DW_FORM_string;
c6481205 6441 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6442 }
b0c7bfa9
DE
6443
6444 /* Set up for reading the DWO CU/TU. */
6445 cu->dwo_unit = dwo_unit;
685af9cd 6446 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6447 section->read (objfile);
6448 abfd = section->get_bfd_owner ();
9c541725
PA
6449 begin_info_ptr = info_ptr = (section->buffer
6450 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6451 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6452
4ab09049 6453 if (per_cu->is_debug_types)
b0c7bfa9 6454 {
4ab09049 6455 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6456
976ca316
SM
6457 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6458 section, dwo_abbrev_section,
43988095 6459 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6460 /* This is not an assert because it can be caused by bad debug info. */
43988095 6461 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6462 {
6463 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6464 " TU at offset %s [in module %s]"),
a2ce51a0 6465 hex_string (sig_type->signature),
43988095 6466 hex_string (cu->header.signature),
9d8780f0 6467 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6468 bfd_get_filename (abfd));
6469 }
9c541725 6470 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6471 /* For DWOs coming from DWP files, we don't know the CU length
6472 nor the type's offset in the TU until now. */
4057dfde 6473 dwo_unit->length = cu->header.get_length ();
9c541725 6474 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6475
6476 /* Establish the type offset that can be used to lookup the type.
6477 For DWO files, we don't know it until now. */
9c541725
PA
6478 sig_type->type_offset_in_section
6479 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6480 }
6481 else
6482 {
976ca316
SM
6483 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6484 section, dwo_abbrev_section,
43988095 6485 info_ptr, rcuh_kind::COMPILE);
9c541725 6486 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6487 /* For DWOs coming from DWP files, we don't know the CU length
6488 until now. */
4057dfde 6489 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6490 }
6491
606decb2 6492 dwo_abbrev_section->read (objfile);
685af9cd 6493 *result_dwo_abbrev_table
606decb2 6494 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6495 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6496 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6497
6498 /* Read in the die, but leave space to copy over the attributes
6499 from the stub. This has the benefit of simplifying the rest of
6500 the code - all the work to maintain the illusion of a single
6501 DW_TAG_{compile,type}_unit DIE is done here. */
6502 num_extra_attrs = ((stmt_list != NULL)
6503 + (low_pc != NULL)
6504 + (high_pc != NULL)
6505 + (ranges != NULL)
6506 + (comp_dir != NULL));
6507 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6508 num_extra_attrs);
b0c7bfa9
DE
6509
6510 /* Copy over the attributes from the stub to the DIE we just read in. */
6511 comp_unit_die = *result_comp_unit_die;
6512 i = comp_unit_die->num_attrs;
6513 if (stmt_list != NULL)
6514 comp_unit_die->attrs[i++] = *stmt_list;
6515 if (low_pc != NULL)
6516 comp_unit_die->attrs[i++] = *low_pc;
6517 if (high_pc != NULL)
6518 comp_unit_die->attrs[i++] = *high_pc;
6519 if (ranges != NULL)
6520 comp_unit_die->attrs[i++] = *ranges;
6521 if (comp_dir != NULL)
6522 comp_unit_die->attrs[i++] = *comp_dir;
6523 comp_unit_die->num_attrs += num_extra_attrs;
6524
b4f54984 6525 if (dwarf_die_debug)
bf6af496
DE
6526 {
6527 fprintf_unfiltered (gdb_stdlog,
6528 "Read die from %s@0x%x of %s:\n",
96b79293 6529 section->get_name (),
bf6af496
DE
6530 (unsigned) (begin_info_ptr - section->buffer),
6531 bfd_get_filename (abfd));
b4f54984 6532 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6533 }
6534
b0c7bfa9
DE
6535 /* Skip dummy compilation units. */
6536 if (info_ptr >= begin_info_ptr + dwo_unit->length
6537 || peek_abbrev_code (abfd, info_ptr) == 0)
6538 return 0;
6539
6540 *result_info_ptr = info_ptr;
6541 return 1;
6542}
6543
a084a2a6
AT
6544/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6545 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6546 signature is part of the header. */
6547static gdb::optional<ULONGEST>
6548lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6549{
6550 if (cu->header.version >= 5)
6551 return cu->header.signature;
6552 struct attribute *attr;
6553 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 6554 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 6555 return gdb::optional<ULONGEST> ();
529908cb 6556 return attr->as_unsigned ();
a084a2a6
AT
6557}
6558
c0ab21c2 6559/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6560 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6561 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6562
6563static struct dwo_unit *
4ab09049 6564lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6565{
4ab09049 6566 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6567 struct dwo_unit *dwo_unit;
c0ab21c2 6568 const char *comp_dir;
b0c7bfa9 6569
a2ce51a0
DE
6570 gdb_assert (cu != NULL);
6571
b0c7bfa9 6572 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6573 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6574 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6575
4ab09049
SM
6576 if (per_cu->is_debug_types)
6577 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6578 else
6579 {
a084a2a6 6580 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6581
a084a2a6 6582 if (!signature.has_value ())
b0c7bfa9
DE
6583 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6584 " [in module %s]"),
4ab09049
SM
6585 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6586
6587 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6588 }
6589
b0c7bfa9
DE
6590 return dwo_unit;
6591}
6592
c0ab21c2 6593/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6594 See it for a description of the parameters.
fcd3b13d 6595 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6596
c0ab21c2 6597void
9e021579
SM
6598cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6599 dwarf2_per_objfile *per_objfile,
2e671100 6600 dwarf2_cu *existing_cu)
a2ce51a0 6601{
a2ce51a0 6602 struct signatured_type *sig_type;
a2ce51a0
DE
6603
6604 /* Verify we can do the following downcast, and that we have the
6605 data we need. */
6606 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6607 sig_type = (struct signatured_type *) this_cu;
6608 gdb_assert (sig_type->dwo_unit != NULL);
6609
2e671100
SM
6610 dwarf2_cu *cu;
6611
6612 if (existing_cu != nullptr)
6aa5f3a6 6613 {
2e671100
SM
6614 cu = existing_cu;
6615 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6616 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6617 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6618 }
6619 else
6620 {
7188ed02 6621 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6622 in per_objfile yet. */
7188ed02 6623 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6624 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6625 cu = m_new_cu.get ();
6aa5f3a6
DE
6626 }
6627
6628 /* A future optimization, if needed, would be to use an existing
6629 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6630 could share abbrev tables. */
a2ce51a0 6631
2e671100 6632 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6633 NULL /* stub_comp_unit_die */,
6634 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6635 this, &info_ptr,
3e225074 6636 &comp_unit_die,
c0ab21c2 6637 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6638 {
6639 /* Dummy die. */
c0ab21c2 6640 dummy_p = true;
a2ce51a0 6641 }
a2ce51a0
DE
6642}
6643
fd820528 6644/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6645 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6646
f4dc4d17
DE
6647 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6648 Otherwise the table specified in the comp unit header is read in and used.
6649 This is an optimization for when we already have the abbrev table.
6650
2e671100
SM
6651 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6652 allocated. */
aaa75496 6653
ab432490 6654cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6655 dwarf2_per_objfile *per_objfile,
c0ab21c2 6656 struct abbrev_table *abbrev_table,
2e671100 6657 dwarf2_cu *existing_cu,
c0ab21c2
TT
6658 bool skip_partial)
6659 : die_reader_specs {},
6751ebae 6660 m_this_cu (this_cu)
c906108c 6661{
976ca316 6662 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6663 struct dwarf2_section_info *section = this_cu->section;
96b79293 6664 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 6665 const gdb_byte *begin_info_ptr;
dee91e82 6666 struct signatured_type *sig_type = NULL;
4bdcc0c1 6667 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6668 /* Non-zero if CU currently points to a DWO file and we need to
6669 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6670 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6671 int rereading_dwo_cu = 0;
c906108c 6672
b4f54984 6673 if (dwarf_die_debug)
9d8780f0 6674 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6675 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6676 sect_offset_str (this_cu->sect_off));
09406207 6677
a2ce51a0
DE
6678 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6679 file (instead of going through the stub), short-circuit all of this. */
6680 if (this_cu->reading_dwo_directly)
6681 {
6682 /* Narrow down the scope of possibilities to have to understand. */
6683 gdb_assert (this_cu->is_debug_types);
6684 gdb_assert (abbrev_table == NULL);
976ca316 6685 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
6686 return;
6687 }
6688
dee91e82 6689 /* This is cheap if the section is already read in. */
96b79293 6690 section->read (objfile);
dee91e82 6691
9c541725 6692 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6693
6694 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 6695
2e671100
SM
6696 dwarf2_cu *cu;
6697
6698 if (existing_cu != nullptr)
dee91e82 6699 {
2e671100 6700 cu = existing_cu;
42e7ad6c
DE
6701 /* If this CU is from a DWO file we need to start over, we need to
6702 refetch the attributes from the skeleton CU.
6703 This could be optimized by retrieving those attributes from when we
6704 were here the first time: the previous comp_unit_die was stored in
6705 comp_unit_obstack. But there's no data yet that we need this
6706 optimization. */
6707 if (cu->dwo_unit != NULL)
6708 rereading_dwo_cu = 1;
dee91e82
DE
6709 }
6710 else
6711 {
7188ed02 6712 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6713 in per_objfile yet. */
976ca316
SM
6714 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6715 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 6716 cu = m_new_cu.get ();
42e7ad6c 6717 }
dee91e82 6718
b0c7bfa9 6719 /* Get the header. */
9c541725 6720 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6721 {
6722 /* We already have the header, there's no need to read it in again. */
9c541725 6723 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6724 }
6725 else
6726 {
3019eac3 6727 if (this_cu->is_debug_types)
dee91e82 6728 {
976ca316
SM
6729 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6730 section, abbrev_section,
6731 info_ptr, rcuh_kind::TYPE);
dee91e82 6732
42e7ad6c
DE
6733 /* Since per_cu is the first member of struct signatured_type,
6734 we can go from a pointer to one to a pointer to the other. */
6735 sig_type = (struct signatured_type *) this_cu;
43988095 6736 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6737 gdb_assert (sig_type->type_offset_in_tu
6738 == cu->header.type_cu_offset_in_tu);
6739 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6740
42e7ad6c
DE
6741 /* LENGTH has not been set yet for type units if we're
6742 using .gdb_index. */
4057dfde 6743 this_cu->length = cu->header.get_length ();
3019eac3
DE
6744
6745 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6746 sig_type->type_offset_in_section =
6747 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6748
6749 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6750 }
6751 else
6752 {
976ca316
SM
6753 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6754 section, abbrev_section,
43988095
JK
6755 info_ptr,
6756 rcuh_kind::COMPILE);
dee91e82 6757
9c541725 6758 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
6759 if (this_cu->length == 0)
6760 this_cu->length = cu->header.get_length ();
6761 else
6762 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6763 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6764 }
6765 }
10b3939b 6766
6caca83c 6767 /* Skip dummy compilation units. */
dee91e82 6768 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6769 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6770 {
6771 dummy_p = true;
6772 return;
6773 }
6caca83c 6774
433df2d4
DE
6775 /* If we don't have them yet, read the abbrevs for this compilation unit.
6776 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6777 done. */
f4dc4d17 6778 if (abbrev_table != NULL)
685af9cd
TT
6779 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6780 else
f4dc4d17 6781 {
606decb2 6782 abbrev_section->read (objfile);
c0ab21c2 6783 m_abbrev_table_holder
606decb2 6784 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 6785 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6786 }
af703f96 6787
dee91e82 6788 /* Read the top level CU/TU die. */
c0ab21c2 6789 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6790 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6791
58f0c718 6792 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6793 {
6794 dummy_p = true;
6795 return;
6796 }
58f0c718 6797
b0c7bfa9 6798 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6799 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6800 table from the DWO file and pass the ownership over to us. It will be
6801 referenced from READER, so we must make sure to free it after we're done
6802 with READER.
6803
b0c7bfa9
DE
6804 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6805 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6806 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6807 if (dwo_name != nullptr)
3019eac3 6808 {
3019eac3 6809 struct dwo_unit *dwo_unit;
b0c7bfa9 6810 struct die_info *dwo_comp_unit_die;
3019eac3 6811
3e225074 6812 if (comp_unit_die->has_children)
6a506a2d 6813 {
b98664d3 6814 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6815 " has children (offset %s) [in module %s]"),
6816 sect_offset_str (this_cu->sect_off),
6817 bfd_get_filename (abfd));
6a506a2d 6818 }
4ab09049 6819 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 6820 if (dwo_unit != NULL)
3019eac3 6821 {
4ab09049 6822 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 6823 comp_unit_die, NULL,
c0ab21c2 6824 this, &info_ptr,
3e225074 6825 &dwo_comp_unit_die,
c0ab21c2 6826 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6827 {
6828 /* Dummy die. */
c0ab21c2 6829 dummy_p = true;
6a506a2d
DE
6830 return;
6831 }
6832 comp_unit_die = dwo_comp_unit_die;
6833 }
6834 else
6835 {
6836 /* Yikes, we couldn't find the rest of the DIE, we only have
6837 the stub. A complaint has already been logged. There's
6838 not much more we can do except pass on the stub DIE to
6839 die_reader_func. We don't want to throw an error on bad
6840 debug info. */
3019eac3
DE
6841 }
6842 }
c0ab21c2 6843}
3019eac3 6844
6751ebae
TT
6845void
6846cutu_reader::keep ()
c0ab21c2 6847{
b0c7bfa9 6848 /* Done, clean up. */
6751ebae
TT
6849 gdb_assert (!dummy_p);
6850 if (m_new_cu != NULL)
348e048f 6851 {
7188ed02 6852 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 6853 now. */
7188ed02
SM
6854 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6855 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 6856 }
dee91e82
DE
6857}
6858
18a8505e
AT
6859/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6860 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6861 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6862
6863 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6864 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6865
6866 We fill in THIS_CU->length.
6867
dee91e82 6868 THIS_CU->cu is always freed when done.
3019eac3 6869 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6870 to care whether it refers to the "main" CU or the DWO CU.
6871
6872 When parent_cu is passed, it is used to provide a default value for
6873 str_offsets_base and addr_base from the parent. */
dee91e82 6874
ab432490 6875cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6876 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
6877 struct dwarf2_cu *parent_cu,
6878 struct dwo_file *dwo_file)
6879 : die_reader_specs {},
6880 m_this_cu (this_cu)
dee91e82 6881{
976ca316 6882 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6883 struct dwarf2_section_info *section = this_cu->section;
96b79293 6884 bfd *abfd = section->get_bfd_owner ();
33e80786 6885 struct dwarf2_section_info *abbrev_section;
d521ce57 6886 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6887
b4f54984 6888 if (dwarf_die_debug)
9d8780f0 6889 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6890 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6891 sect_offset_str (this_cu->sect_off));
09406207 6892
976ca316 6893 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 6894
33e80786
DE
6895 abbrev_section = (dwo_file != NULL
6896 ? &dwo_file->sections.abbrev
6897 : get_abbrev_section_for_cu (this_cu));
6898
dee91e82 6899 /* This is cheap if the section is already read in. */
96b79293 6900 section->read (objfile);
dee91e82 6901
976ca316 6902 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 6903
9c541725 6904 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
6905 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6906 section, abbrev_section, info_ptr,
43988095
JK
6907 (this_cu->is_debug_types
6908 ? rcuh_kind::TYPE
6909 : rcuh_kind::COMPILE));
dee91e82 6910
18a8505e
AT
6911 if (parent_cu != nullptr)
6912 {
c0ab21c2
TT
6913 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6914 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 6915 }
4057dfde 6916 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
6917
6918 /* Skip dummy compilation units. */
6919 if (info_ptr >= begin_info_ptr + this_cu->length
6920 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6921 {
6922 dummy_p = true;
6923 return;
6924 }
72bf9492 6925
606decb2 6926 abbrev_section->read (objfile);
c0ab21c2 6927 m_abbrev_table_holder
606decb2 6928 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 6929
c0ab21c2
TT
6930 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6931 m_abbrev_table_holder.get ());
3e225074 6932 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
6933}
6934
0018ea6f
DE
6935\f
6936/* Type Unit Groups.
dee91e82 6937
0018ea6f
DE
6938 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6939 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6940 so that all types coming from the same compilation (.o file) are grouped
6941 together. A future step could be to put the types in the same symtab as
6942 the CU the types ultimately came from. */
ff013f42 6943
f4dc4d17
DE
6944static hashval_t
6945hash_type_unit_group (const void *item)
6946{
9a3c8263
SM
6947 const struct type_unit_group *tu_group
6948 = (const struct type_unit_group *) item;
f4dc4d17 6949
094b34ac 6950 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6951}
348e048f
DE
6952
6953static int
f4dc4d17 6954eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6955{
9a3c8263
SM
6956 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6957 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6958
094b34ac 6959 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6960}
348e048f 6961
f4dc4d17
DE
6962/* Allocate a hash table for type unit groups. */
6963
eaa5fa8b 6964static htab_up
298e9637 6965allocate_type_unit_groups_table ()
f4dc4d17 6966{
eaa5fa8b
TT
6967 return htab_up (htab_create_alloc (3,
6968 hash_type_unit_group,
6969 eq_type_unit_group,
0d305d5c
TT
6970 [] (void *arg)
6971 {
6972 type_unit_group *grp
6973 = (type_unit_group *) arg;
6974 delete grp;
6975 },
6976 xcalloc, xfree));
f4dc4d17 6977}
dee91e82 6978
f4dc4d17
DE
6979/* Type units that don't have DW_AT_stmt_list are grouped into their own
6980 partial symtabs. We combine several TUs per psymtab to not let the size
6981 of any one psymtab grow too big. */
6982#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6983#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6984
094b34ac 6985/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6986 Create the type_unit_group object used to hold one or more TUs. */
6987
0d305d5c 6988static std::unique_ptr<type_unit_group>
094b34ac 6989create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 6990{
976ca316
SM
6991 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6992 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 6993
0d305d5c
TT
6994 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6995 tu_group->per_bfd = per_bfd;
f4dc4d17 6996
1859c670 6997 if (per_bfd->using_index)
094b34ac 6998 {
0d305d5c
TT
6999 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7000 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7001 }
7002 else
7003 {
9c541725 7004 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7005 dwarf2_psymtab *pst;
528e1572 7006 std::string name;
094b34ac
DE
7007
7008 /* Give the symtab a useful name for debug purposes. */
7009 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7010 name = string_printf ("<type_units_%d>",
7011 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7012 else
528e1572 7013 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7014
0d305d5c
TT
7015 pst = create_partial_symtab (tu_group.get (), per_objfile,
7016 name.c_str ());
6d94535f 7017 pst->anonymous = true;
094b34ac 7018 }
f4dc4d17 7019
094b34ac 7020 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7021 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7022
7023 return tu_group;
7024}
7025
094b34ac
DE
7026/* Look up the type_unit_group for type unit CU, and create it if necessary.
7027 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7028
7029static struct type_unit_group *
ff39bb5e 7030get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7031{
976ca316
SM
7032 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7033 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7034 struct type_unit_group *tu_group;
7035 void **slot;
7036 unsigned int line_offset;
7037 struct type_unit_group type_unit_group_for_lookup;
7038
976ca316
SM
7039 if (per_objfile->per_bfd->type_unit_groups == NULL)
7040 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7041
7042 /* Do we need to create a new group, or can we use an existing one? */
7043
529908cb 7044 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7045 {
529908cb 7046 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7047 ++tu_stats->nr_symtab_sharers;
7048 }
7049 else
7050 {
7051 /* Ugh, no stmt_list. Rare, but we have to handle it.
7052 We can do various things here like create one group per TU or
7053 spread them over multiple groups to split up the expansion work.
7054 To avoid worst case scenarios (too many groups or too large groups)
7055 we, umm, group them in bunches. */
7056 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7057 | (tu_stats->nr_stmt_less_type_units
7058 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7059 ++tu_stats->nr_stmt_less_type_units;
7060 }
7061
094b34ac 7062 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7063 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7064 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17 7065 &type_unit_group_for_lookup, INSERT);
0d305d5c 7066 if (*slot == nullptr)
f4dc4d17 7067 {
9c541725 7068 sect_offset line_offset_struct = (sect_offset) line_offset;
0d305d5c
TT
7069 std::unique_ptr<type_unit_group> grp
7070 = create_type_unit_group (cu, line_offset_struct);
7071 *slot = grp.release ();
f4dc4d17
DE
7072 ++tu_stats->nr_symtabs;
7073 }
7074
0d305d5c
TT
7075 tu_group = (struct type_unit_group *) *slot;
7076 gdb_assert (tu_group != nullptr);
f4dc4d17
DE
7077 return tu_group;
7078}
0018ea6f
DE
7079\f
7080/* Partial symbol tables. */
7081
7082/* Create a psymtab named NAME and assign it to PER_CU.
7083
7084 The caller must fill in the following details:
7085 dirname, textlow, texthigh. */
7086
891813be 7087static dwarf2_psymtab *
7aa104c4
SM
7088create_partial_symtab (dwarf2_per_cu_data *per_cu,
7089 dwarf2_per_objfile *per_objfile,
7090 const char *name)
0018ea6f 7091{
0072c873
SM
7092 dwarf2_psymtab *pst
7093 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7094 per_objfile->objfile->per_bfd, per_cu);
0018ea6f 7095
6d94535f 7096 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7097
7098 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7099 per_cu->v.psymtab = pst;
7100
7101 return pst;
7102}
7103
c0ab21c2 7104/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7105
7106static void
7107process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7108 const gdb_byte *info_ptr,
0018ea6f 7109 struct die_info *comp_unit_die,
c0ab21c2 7110 enum language pretend_language)
0018ea6f
DE
7111{
7112 struct dwarf2_cu *cu = reader->cu;
7aa104c4 7113 dwarf2_per_objfile *per_objfile = cu->per_objfile;
84685904 7114 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7aa104c4 7115 struct objfile *objfile = per_objfile->objfile;
08feed99 7116 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7117 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7118 CORE_ADDR baseaddr;
7119 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7120 dwarf2_psymtab *pst;
3a2b436a 7121 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7122 const char *filename;
0018ea6f 7123
0018ea6f
DE
7124 gdb_assert (! per_cu->is_debug_types);
7125
c0ab21c2 7126 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7127
0018ea6f 7128 /* Allocate a new partial symbol table structure. */
2e927613
TV
7129 gdb::unique_xmalloc_ptr<char> debug_filename;
7130 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7131 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7132 if (filename == NULL)
0018ea6f 7133 filename = "";
2e927613
TV
7134 else if (strcmp (filename, artificial) == 0)
7135 {
7136 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7137 sect_offset_str (per_cu->sect_off),
7138 (char *) NULL));
2e927613
TV
7139 filename = debug_filename.get ();
7140 }
0018ea6f 7141
7aa104c4 7142 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7143
7144 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7145 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7146
b3b3bada 7147 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7148
7149 dwarf2_find_base_address (comp_unit_die, cu);
7150
7151 /* Possibly set the default values of LOWPC and HIGHPC from
7152 `DW_AT_ranges'. */
3a2b436a
JK
7153 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7154 &best_highpc, cu, pst);
7155 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7156 {
7157 CORE_ADDR low
7158 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7159 - baseaddr);
7160 CORE_ADDR high
7161 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7162 - baseaddr - 1);
7163 /* Store the contiguous range if it is not empty; it can be
7164 empty for CUs with no code. */
84685904 7165 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 7166 low, high, pst);
79748972 7167 }
0018ea6f
DE
7168
7169 /* Check if comp unit has_children.
7170 If so, read the rest of the partial symbols from this comp unit.
7171 If not, there's no more debug_info for this comp unit. */
3e225074 7172 if (comp_unit_die->has_children)
0018ea6f
DE
7173 {
7174 struct partial_die_info *first_die;
7175 CORE_ADDR lowpc, highpc;
7176
7177 lowpc = ((CORE_ADDR) -1);
7178 highpc = ((CORE_ADDR) 0);
7179
7180 first_die = load_partial_dies (reader, info_ptr, 1);
7181
7182 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7183 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7184
7185 /* If we didn't find a lowpc, set it to highpc to avoid
7186 complaints from `maint check'. */
7187 if (lowpc == ((CORE_ADDR) -1))
7188 lowpc = highpc;
7189
7190 /* If the compilation unit didn't have an explicit address range,
7191 then use the information extracted from its child dies. */
e385593e 7192 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7193 {
7194 best_lowpc = lowpc;
7195 best_highpc = highpc;
7196 }
7197 }
4ae976d1 7198 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7199 best_lowpc + baseaddr)
7200 - baseaddr);
4ae976d1 7201 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7202 best_highpc + baseaddr)
7203 - baseaddr);
0018ea6f 7204
ae7754b2 7205 pst->end ();
0018ea6f 7206
ae640021 7207 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7208 {
7209 int i;
ae640021 7210 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7211
7212 /* Fill in 'dependencies' here; we fill in 'users' in a
7213 post-pass. */
7214 pst->number_of_dependencies = len;
a9342b62 7215 pst->dependencies
84685904 7216 = per_bfd->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7217 for (i = 0; i < len; ++i)
7218 {
7219 pst->dependencies[i]
7220 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7221 }
0018ea6f 7222
ae640021 7223 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7224 }
7225
7226 /* Get the list of files included in the current compilation unit,
7227 and build a psymtab for each of them. */
7228 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7229
6f738b01
SM
7230 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7231 ", %d global, %d static syms",
7232 per_cu->is_debug_types ? "type" : "comp",
7233 sect_offset_str (per_cu->sect_off),
7234 paddress (gdbarch, pst->text_low (objfile)),
7235 paddress (gdbarch, pst->text_high (objfile)),
7236 (int) pst->global_psymbols.size (),
7237 (int) pst->static_psymbols.size ());
0018ea6f
DE
7238}
7239
7240/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7241 Process compilation unit THIS_CU for a psymtab. */
7242
7243static void
ab432490
SM
7244process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7245 dwarf2_per_objfile *per_objfile,
135f5437 7246 bool want_partial_unit,
b93601f3 7247 enum language pretend_language)
0018ea6f
DE
7248{
7249 /* If this compilation unit was already read in, free the
7250 cached copy in order to read it in again. This is
7251 necessary because we skipped some symbols when we first
7252 read in the compilation unit (see load_partial_dies).
7253 This problem could be avoided, but the benefit is unclear. */
7188ed02 7254 per_objfile->remove_cu (this_cu);
0018ea6f 7255
2e671100 7256 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7257
58990295
TV
7258 switch (reader.comp_unit_die->tag)
7259 {
7260 case DW_TAG_compile_unit:
7261 this_cu->unit_type = DW_UT_compile;
7262 break;
7263 case DW_TAG_partial_unit:
7264 this_cu->unit_type = DW_UT_partial;
7265 break;
e77b0004
TV
7266 case DW_TAG_type_unit:
7267 this_cu->unit_type = DW_UT_type;
7268 break;
58990295 7269 default:
702cf3f5
AB
7270 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7271 dwarf_tag_name (reader.comp_unit_die->tag),
7272 sect_offset_str (reader.cu->per_cu->sect_off),
7273 objfile_name (per_objfile->objfile));
58990295
TV
7274 }
7275
c0ab21c2 7276 if (reader.dummy_p)
f1902523 7277 {
c0ab21c2 7278 /* Nothing. */
f1902523 7279 }
c0ab21c2 7280 else if (this_cu->is_debug_types)
3e225074
TT
7281 build_type_psymtabs_reader (&reader, reader.info_ptr,
7282 reader.comp_unit_die);
135f5437
TT
7283 else if (want_partial_unit
7284 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7285 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7286 reader.comp_unit_die,
c0ab21c2 7287 pretend_language);
0018ea6f 7288
7188ed02 7289 this_cu->lang = reader.cu->language;
58990295 7290
0018ea6f 7291 /* Age out any secondary CUs. */
7188ed02 7292 per_objfile->age_comp_units ();
0018ea6f 7293}
f4dc4d17
DE
7294
7295/* Reader function for build_type_psymtabs. */
7296
7297static void
7298build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7299 const gdb_byte *info_ptr,
3e225074 7300 struct die_info *type_unit_die)
f4dc4d17 7301{
976ca316 7302 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7303 struct dwarf2_cu *cu = reader->cu;
7304 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7305 struct signatured_type *sig_type;
f4dc4d17
DE
7306 struct type_unit_group *tu_group;
7307 struct attribute *attr;
7308 struct partial_die_info *first_die;
7309 CORE_ADDR lowpc, highpc;
891813be 7310 dwarf2_psymtab *pst;
f4dc4d17 7311
0186c6a7
DE
7312 gdb_assert (per_cu->is_debug_types);
7313 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7314
3e225074 7315 if (! type_unit_die->has_children)
f4dc4d17
DE
7316 return;
7317
052c8bb8 7318 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7319 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7320
df07e2c7 7321 if (tu_group->tus == nullptr)
a8b3b8e9 7322 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7323 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7324
7325 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7326 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7327 pst->anonymous = true;
f4dc4d17
DE
7328
7329 first_die = load_partial_dies (reader, info_ptr, 1);
7330
7331 lowpc = (CORE_ADDR) -1;
7332 highpc = (CORE_ADDR) 0;
7333 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7334
ae7754b2 7335 pst->end ();
f4dc4d17
DE
7336}
7337
73051182
DE
7338/* Struct used to sort TUs by their abbreviation table offset. */
7339
7340struct tu_abbrev_offset
7341{
b2bdb8cf
SM
7342 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7343 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7344 {}
7345
7346 signatured_type *sig_type;
73051182
DE
7347 sect_offset abbrev_offset;
7348};
7349
b8efb248 7350/* Helper routine for build_type_psymtabs, passed to std::sort. */
73051182 7351
484cf504
TT
7352static bool
7353sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7354 const struct tu_abbrev_offset &b)
73051182 7355{
484cf504 7356 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7357}
7358
7359/* Efficiently read all the type units.
73051182
DE
7360
7361 The efficiency is because we sort TUs by the abbrev table they use and
7362 only read each abbrev table once. In one program there are 200K TUs
7363 sharing 8K abbrev tables.
7364
7365 The main purpose of this function is to support building the
5989a64e 7366 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7367 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7368 can collapse the search space by grouping them by stmt_list.
7369 The savings can be significant, in the same program from above the 200K TUs
7370 share 8K stmt_list tables.
7371
7372 FUNC is expected to call get_type_unit_group, which will create the
7373 struct type_unit_group if necessary and add it to
5989a64e 7374 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7375
7376static void
b8efb248 7377build_type_psymtabs (dwarf2_per_objfile *per_objfile)
73051182 7378{
976ca316 7379 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7380 abbrev_table_up abbrev_table;
73051182 7381 sect_offset abbrev_offset;
73051182
DE
7382
7383 /* It's up to the caller to not call us multiple times. */
976ca316 7384 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7385
91eea9cc 7386 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
73051182
DE
7387 return;
7388
7389 /* TUs typically share abbrev tables, and there can be way more TUs than
7390 abbrev tables. Sort by abbrev table to reduce the number of times we
7391 read each abbrev table in.
7392 Alternatives are to punt or to maintain a cache of abbrev tables.
7393 This is simpler and efficient enough for now.
7394
7395 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7396 symtab to use). Typically TUs with the same abbrev offset have the same
7397 stmt_list value too so in practice this should work well.
7398
7399 The basic algorithm here is:
7400
7401 sort TUs by abbrev table
7402 for each TU with same abbrev table:
7403 read abbrev table if first user
7404 read TU top level DIE
7405 [IWBN if DWO skeletons had DW_AT_stmt_list]
7406 call FUNC */
7407
6f738b01 7408 dwarf_read_debug_printf ("Building type unit groups ...");
73051182 7409
91eea9cc 7410 /* Sort in a separate table to maintain the order of all_comp_units
73051182 7411 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7412 std::vector<tu_abbrev_offset> sorted_by_abbrev;
91eea9cc 7413 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
b2bdb8cf 7414
91eea9cc
TT
7415 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7416 {
7417 if (cu->is_debug_types)
7418 {
7419 auto sig_type = static_cast<signatured_type *> (cu.get ());
7420 sorted_by_abbrev.emplace_back
7421 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7422 sig_type->sect_off));
7423 }
7424 }
73051182 7425
484cf504
TT
7426 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7427 sort_tu_by_abbrev_offset);
73051182 7428
9c541725 7429 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7430
b2bdb8cf 7431 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7432 {
73051182
DE
7433 /* Switch to the next abbrev table if necessary. */
7434 if (abbrev_table == NULL
b2bdb8cf 7435 || tu.abbrev_offset != abbrev_offset)
73051182 7436 {
b2bdb8cf 7437 abbrev_offset = tu.abbrev_offset;
606decb2 7438 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7439 abbrev_table =
606decb2 7440 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7441 ++tu_stats->nr_uniq_abbrev_tables;
7442 }
7443
0d305d5c 7444 cutu_reader reader (tu.sig_type, per_objfile,
2e671100 7445 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7446 if (!reader.dummy_p)
7447 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7448 reader.comp_unit_die);
73051182 7449 }
6aa5f3a6 7450}
73051182 7451
6aa5f3a6
DE
7452/* Print collected type unit statistics. */
7453
7454static void
976ca316 7455print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7456{
976ca316 7457 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7458
6f738b01 7459 dwarf_read_debug_printf ("Type unit statistics:");
91eea9cc 7460 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
6f738b01
SM
7461 dwarf_read_debug_printf (" %d uniq abbrev tables",
7462 tu_stats->nr_uniq_abbrev_tables);
7463 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7464 tu_stats->nr_symtabs);
7465 dwarf_read_debug_printf (" %d symtab sharers",
7466 tu_stats->nr_symtab_sharers);
7467 dwarf_read_debug_printf (" %d type units without a stmt_list",
7468 tu_stats->nr_stmt_less_type_units);
7469 dwarf_read_debug_printf (" %d all_type_units reallocs",
7470 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7471}
7472
f4dc4d17
DE
7473/* Traversal function for build_type_psymtabs. */
7474
7475static int
7476build_type_psymtab_dependencies (void **slot, void *info)
7477{
976ca316 7478 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
84685904 7479 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 7480 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
0d305d5c 7481 dwarf2_psymtab *pst = tu_group->v.psymtab;
df07e2c7 7482 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7483 int i;
7484
7485 gdb_assert (len > 0);
0d305d5c 7486 gdb_assert (tu_group->type_unit_group_p ());
f4dc4d17
DE
7487
7488 pst->number_of_dependencies = len;
84685904 7489 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
df07e2c7 7490 for (i = 0; i < len; ++i)
f4dc4d17 7491 {
df07e2c7 7492 struct signatured_type *iter = tu_group->tus->at (i);
0d305d5c
TT
7493 gdb_assert (iter->is_debug_types);
7494 pst->dependencies[i] = iter->v.psymtab;
796a7ff8 7495 iter->type_unit_group = tu_group;
f4dc4d17
DE
7496 }
7497
df07e2c7
AB
7498 delete tu_group->tus;
7499 tu_group->tus = nullptr;
348e048f
DE
7500
7501 return 1;
7502}
7503
6aa5f3a6
DE
7504/* Traversal function for process_skeletonless_type_unit.
7505 Read a TU in a DWO file and build partial symbols for it. */
7506
7507static int
7508process_skeletonless_type_unit (void **slot, void *info)
7509{
7510 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7511 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7512 struct signatured_type find_entry, *entry;
7513
7514 /* If this TU doesn't exist in the global table, add it and read it in. */
7515
976ca316
SM
7516 if (per_objfile->per_bfd->signatured_types == NULL)
7517 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7518
7519 find_entry.signature = dwo_unit->signature;
976ca316 7520 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7521 &find_entry, INSERT);
6aa5f3a6
DE
7522 /* If we've already seen this type there's nothing to do. What's happening
7523 is we're doing our own version of comdat-folding here. */
7524 if (*slot != NULL)
7525 return 1;
7526
b8efb248 7527 /* This does the job that create_all_comp_units would have done for
6aa5f3a6 7528 this TU. */
976ca316
SM
7529 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7530 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7531 *slot = entry;
7532
b8efb248 7533 /* This does the job that build_type_psymtabs would have done. */
0d305d5c 7534 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7535 if (!reader.dummy_p)
7536 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7537 reader.comp_unit_die);
6aa5f3a6
DE
7538
7539 return 1;
7540}
7541
7542/* Traversal function for process_skeletonless_type_units. */
7543
7544static int
7545process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7546{
7547 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7548
7549 if (dwo_file->tus != NULL)
b0b6a987
TT
7550 htab_traverse_noresize (dwo_file->tus.get (),
7551 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7552
7553 return 1;
7554}
7555
7556/* Scan all TUs of DWO files, verifying we've processed them.
7557 This is needed in case a TU was emitted without its skeleton.
7558 Note: This can't be done until we know what all the DWO files are. */
7559
7560static void
976ca316 7561process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7562{
7563 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7564 if (get_dwp_file (per_objfile) == NULL
7565 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7566 {
976ca316 7567 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7568 process_dwo_file_for_skeletonless_type_units,
976ca316 7569 per_objfile);
6aa5f3a6 7570 }
348e048f
DE
7571}
7572
ed2dc618 7573/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7574
7575static void
976ca316 7576set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7577{
0d305d5c 7578 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7579 {
891813be 7580 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7581
36586728
TT
7582 if (pst == NULL)
7583 continue;
7584
b76e467d 7585 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7586 {
7587 /* Set the 'user' field only if it is not already set. */
7588 if (pst->dependencies[j]->user == NULL)
7589 pst->dependencies[j]->user = pst;
7590 }
7591 }
7592}
7593
93311388
DE
7594/* Build the partial symbol table by doing a quick pass through the
7595 .debug_info and .debug_abbrev sections. */
72bf9492 7596
93311388 7597static void
976ca316 7598dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 7599{
976ca316 7600 struct objfile *objfile = per_objfile->objfile;
84685904 7601 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
93311388 7602
6f738b01
SM
7603 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7604 objfile_name (objfile));
45cfd468 7605
76935768 7606 scoped_restore restore_reading_psyms
bab287cd 7607 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
98bfdba5 7608
84685904 7609 per_bfd->info.read (objfile);
91c24f0a 7610
93311388
DE
7611 /* Any cached compilation units will be linked by the per-objfile
7612 read_in_chain. Make sure to free them when we're done. */
976ca316 7613 free_cached_comp_units freer (per_objfile);
72bf9492 7614
976ca316 7615 create_all_comp_units (per_objfile);
b8efb248 7616 build_type_psymtabs (per_objfile);
c906108c 7617
60606b2c
TT
7618 /* Create a temporary address map on a temporary obstack. We later
7619 copy this to the final obstack. */
8268c778 7620 auto_obstack temp_obstack;
791afaa2
TT
7621
7622 scoped_restore save_psymtabs_addrmap
84685904 7623 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
791afaa2 7624 addrmap_create_mutable (&temp_obstack));
72bf9492 7625
0d305d5c 7626 for (const auto &per_cu : per_bfd->all_comp_units)
3d5afab3
TV
7627 {
7628 if (per_cu->v.psymtab != NULL)
7629 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7630 continue;
0d305d5c 7631 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
ab432490 7632 language_minimal);
3d5afab3 7633 }
ff013f42 7634
6aa5f3a6 7635 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 7636 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
7637
7638 /* Now that all TUs have been processed we can fill in the dependencies. */
84685904 7639 if (per_bfd->type_unit_groups != NULL)
6aa5f3a6 7640 {
84685904 7641 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
976ca316 7642 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
7643 }
7644
6f738b01 7645 if (dwarf_read_debug > 0)
976ca316 7646 print_tu_stats (per_objfile);
6aa5f3a6 7647
976ca316 7648 set_partial_user (per_objfile);
95554aad 7649
84685904
TT
7650 per_bfd->partial_symtabs->psymtabs_addrmap
7651 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7652 per_bfd->partial_symtabs->obstack ());
791afaa2
TT
7653 /* At this point we want to keep the address map. */
7654 save_psymtabs_addrmap.release ();
ff013f42 7655
6f738b01
SM
7656 dwarf_read_debug_printf ("Done building psymtabs of %s",
7657 objfile_name (objfile));
ae038cb0
DJ
7658}
7659
dee91e82
DE
7660/* Load the partial DIEs for a secondary CU into memory.
7661 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7662
dee91e82 7663static void
ab432490 7664load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
7665 dwarf2_per_objfile *per_objfile,
7666 dwarf2_cu *existing_cu)
dee91e82 7667{
2e671100 7668 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
7669
7670 if (!reader.dummy_p)
7671 {
7672 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7673 language_minimal);
7674
7675 /* Check if comp unit has_children.
7676 If so, read the rest of the partial symbols from this comp unit.
7677 If not, there's no more debug_info for this comp unit. */
3e225074 7678 if (reader.comp_unit_die->has_children)
c0ab21c2 7679 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7680
7681 reader.keep ();
c0ab21c2 7682 }
ae038cb0
DJ
7683}
7684
ae038cb0 7685static void
976ca316 7686read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 7687 struct dwarf2_section_info *section,
f1902523 7688 struct dwarf2_section_info *abbrev_section,
b8efb248
TT
7689 unsigned int is_dwz,
7690 htab_up &types_htab,
7691 rcuh_kind section_kind)
ae038cb0 7692{
d521ce57 7693 const gdb_byte *info_ptr;
976ca316 7694 struct objfile *objfile = per_objfile->objfile;
be391dca 7695
6f738b01
SM
7696 dwarf_read_debug_printf ("Reading %s for %s",
7697 section->get_name (),
7698 section->get_file_name ());
bf6af496 7699
96b79293 7700 section->read (objfile);
ae038cb0 7701
36586728 7702 info_ptr = section->buffer;
6e70227d 7703
36586728 7704 while (info_ptr < section->buffer + section->size)
ae038cb0 7705 {
0d305d5c 7706 std::unique_ptr<dwarf2_per_cu_data> this_cu;
ae038cb0 7707
9c541725 7708 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7709
f1902523 7710 comp_unit_head cu_header;
976ca316 7711 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 7712 abbrev_section, info_ptr,
b8efb248 7713 section_kind);
ae038cb0
DJ
7714
7715 /* Save the compilation unit for later lookup. */
f1902523 7716 if (cu_header.unit_type != DW_UT_type)
976ca316 7717 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
7718 else
7719 {
b8efb248
TT
7720 if (types_htab == nullptr)
7721 types_htab = allocate_signatured_type_table ();
7722
976ca316 7723 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b8efb248 7724 signatured_type *sig_ptr = sig_type.get ();
f1902523
JK
7725 sig_type->signature = cu_header.signature;
7726 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
0d305d5c 7727 this_cu = std::move (sig_type);
b8efb248
TT
7728
7729 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7730 gdb_assert (slot != nullptr);
7731 if (*slot != nullptr)
7732 complaint (_("debug type entry at offset %s is duplicate to"
7733 " the entry at offset %s, signature %s"),
7734 sect_offset_str (sect_off),
7735 sect_offset_str (sig_ptr->sect_off),
7736 hex_string (sig_ptr->signature));
7737 *slot = sig_ptr;
f1902523
JK
7738 }
7739 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7740 this_cu->sect_off = sect_off;
f1902523 7741 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7742 this_cu->is_dwz = is_dwz;
8a0459fd 7743 this_cu->section = section;
ae038cb0 7744
ae038cb0 7745 info_ptr = info_ptr + this_cu->length;
0d305d5c 7746 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
ae038cb0 7747 }
36586728
TT
7748}
7749
7750/* Create a list of all compilation units in OBJFILE.
7751 This is only done for -readnow and building partial symtabs. */
7752
7753static void
976ca316 7754create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 7755{
b8efb248
TT
7756 htab_up types_htab;
7757
976ca316 7758 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
b8efb248
TT
7759 &per_objfile->per_bfd->abbrev, 0,
7760 types_htab, rcuh_kind::COMPILE);
7761 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7762 read_comp_units_from_section (per_objfile, &section,
7763 &per_objfile->per_bfd->abbrev, 0,
7764 types_htab, rcuh_kind::TYPE);
36586728 7765
976ca316 7766 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 7767 if (dwz != NULL)
b8efb248
TT
7768 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7769 types_htab, rcuh_kind::COMPILE);
7770
7771 per_objfile->per_bfd->signatured_types = std::move (types_htab);
c906108c
SS
7772}
7773
5734ee8b 7774/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7775 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7776 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7777 DW_AT_ranges). See the comments of add_partial_subprogram on how
7778 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7779
72bf9492
DJ
7780static void
7781scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7782 CORE_ADDR *highpc, int set_addrmap,
7783 struct dwarf2_cu *cu)
c906108c 7784{
72bf9492 7785 struct partial_die_info *pdi;
c906108c 7786
91c24f0a
DC
7787 /* Now, march along the PDI's, descending into ones which have
7788 interesting children but skipping the children of the other ones,
7789 until we reach the end of the compilation unit. */
c906108c 7790
72bf9492 7791 pdi = first_die;
91c24f0a 7792
72bf9492
DJ
7793 while (pdi != NULL)
7794 {
52356b79 7795 pdi->fixup (cu);
c906108c 7796
f55ee35c 7797 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7798 children, so we need to look at them. Ditto for anonymous
7799 enums. */
933c6fe4 7800
7d00ffec 7801 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7802 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7803 || pdi->tag == DW_TAG_imported_unit
7804 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7805 {
72bf9492 7806 switch (pdi->tag)
c906108c
SS
7807 {
7808 case DW_TAG_subprogram:
b1dc1806 7809 case DW_TAG_inlined_subroutine:
cdc07690 7810 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
7811 if (cu->language == language_cplus)
7812 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7813 set_addrmap, cu);
c906108c 7814 break;
72929c62 7815 case DW_TAG_constant:
c906108c
SS
7816 case DW_TAG_variable:
7817 case DW_TAG_typedef:
91c24f0a 7818 case DW_TAG_union_type:
317d2668
TV
7819 if (!pdi->is_declaration
7820 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 7821 {
72bf9492 7822 add_partial_symbol (pdi, cu);
63d06c5c
DC
7823 }
7824 break;
c906108c 7825 case DW_TAG_class_type:
680b30c7 7826 case DW_TAG_interface_type:
c906108c 7827 case DW_TAG_structure_type:
72bf9492 7828 if (!pdi->is_declaration)
c906108c 7829 {
72bf9492 7830 add_partial_symbol (pdi, cu);
c906108c 7831 }
b7fee5a3
KS
7832 if ((cu->language == language_rust
7833 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7834 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7835 set_addrmap, cu);
c906108c 7836 break;
91c24f0a 7837 case DW_TAG_enumeration_type:
72bf9492
DJ
7838 if (!pdi->is_declaration)
7839 add_partial_enumeration (pdi, cu);
c906108c
SS
7840 break;
7841 case DW_TAG_base_type:
dda83cd7 7842 case DW_TAG_subrange_type:
c906108c 7843 /* File scope base type definitions are added to the partial
dda83cd7 7844 symbol table. */
72bf9492 7845 add_partial_symbol (pdi, cu);
c906108c 7846 break;
d9fa45fe 7847 case DW_TAG_namespace:
cdc07690 7848 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7849 break;
5d7cb8df 7850 case DW_TAG_module:
59c35742
AB
7851 if (!pdi->is_declaration)
7852 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7853 break;
95554aad
TT
7854 case DW_TAG_imported_unit:
7855 {
7856 struct dwarf2_per_cu_data *per_cu;
7857
f4dc4d17
DE
7858 /* For now we don't handle imported units in type units. */
7859 if (cu->per_cu->is_debug_types)
7860 {
7861 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7862 " supported in type units [in module %s]"),
5e22e966 7863 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
7864 }
7865
e3b94546 7866 per_cu = dwarf2_find_containing_comp_unit
5e22e966 7867 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
7868
7869 /* Go read the partial unit, if needed. */
7870 if (per_cu->v.psymtab == NULL)
ab432490
SM
7871 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7872 cu->language);
95554aad 7873
ae640021 7874 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7875 }
7876 break;
74921315
KS
7877 case DW_TAG_imported_declaration:
7878 add_partial_symbol (pdi, cu);
7879 break;
c906108c
SS
7880 default:
7881 break;
7882 }
7883 }
7884
72bf9492
DJ
7885 /* If the die has a sibling, skip to the sibling. */
7886
7887 pdi = pdi->die_sibling;
7888 }
7889}
7890
7891/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7892
72bf9492 7893 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7894 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7895 Enumerators are an exception; they use the scope of their parent
7896 enumeration type, i.e. the name of the enumeration type is not
7897 prepended to the enumerator.
91c24f0a 7898
72bf9492
DJ
7899 There are two complexities. One is DW_AT_specification; in this
7900 case "parent" means the parent of the target of the specification,
7901 instead of the direct parent of the DIE. The other is compilers
7902 which do not emit DW_TAG_namespace; in this case we try to guess
7903 the fully qualified name of structure types from their members'
7904 linkage names. This must be done using the DIE's children rather
7905 than the children of any DW_AT_specification target. We only need
7906 to do this for structures at the top level, i.e. if the target of
7907 any DW_AT_specification (if any; otherwise the DIE itself) does not
7908 have a parent. */
7909
7910/* Compute the scope prefix associated with PDI's parent, in
7911 compilation unit CU. The result will be allocated on CU's
7912 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7913 field. NULL is returned if no prefix is necessary. */
15d034d0 7914static const char *
72bf9492
DJ
7915partial_die_parent_scope (struct partial_die_info *pdi,
7916 struct dwarf2_cu *cu)
7917{
15d034d0 7918 const char *grandparent_scope;
72bf9492 7919 struct partial_die_info *parent, *real_pdi;
91c24f0a 7920
72bf9492
DJ
7921 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7922 then this means the parent of the specification DIE. */
7923
7924 real_pdi = pdi;
72bf9492 7925 while (real_pdi->has_specification)
fb816e8b 7926 {
122cf0f2
AB
7927 auto res = find_partial_die (real_pdi->spec_offset,
7928 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
7929 real_pdi = res.pdi;
7930 cu = res.cu;
7931 }
72bf9492
DJ
7932
7933 parent = real_pdi->die_parent;
7934 if (parent == NULL)
7935 return NULL;
7936
7937 if (parent->scope_set)
7938 return parent->scope;
7939
52356b79 7940 parent->fixup (cu);
72bf9492 7941
10b3939b 7942 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7943
acebe513
UW
7944 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7945 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7946 Work around this problem here. */
7947 if (cu->language == language_cplus
6e70227d 7948 && parent->tag == DW_TAG_namespace
7d00ffec 7949 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
7950 && grandparent_scope == NULL)
7951 {
7952 parent->scope = NULL;
7953 parent->scope_set = 1;
7954 return NULL;
7955 }
7956
0a4b0913 7957 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
7958 if (pdi->tag == DW_TAG_enumerator)
7959 /* Enumerators should not get the name of the enumeration as a prefix. */
7960 parent->scope = grandparent_scope;
7961 else if (parent->tag == DW_TAG_namespace
f55ee35c 7962 || parent->tag == DW_TAG_module
72bf9492
DJ
7963 || parent->tag == DW_TAG_structure_type
7964 || parent->tag == DW_TAG_class_type
680b30c7 7965 || parent->tag == DW_TAG_interface_type
ceeb3d5a 7966 || parent->tag == DW_TAG_union_type
0a4b0913
AB
7967 || parent->tag == DW_TAG_enumeration_type
7968 || (cu->language == language_fortran
7969 && parent->tag == DW_TAG_subprogram
7970 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
7971 {
7972 if (grandparent_scope == NULL)
7d00ffec 7973 parent->scope = parent->name (cu);
72bf9492 7974 else
3e43a32a
MS
7975 parent->scope = typename_concat (&cu->comp_unit_obstack,
7976 grandparent_scope,
7d00ffec 7977 parent->name (cu), 0, cu);
72bf9492 7978 }
72bf9492
DJ
7979 else
7980 {
7981 /* FIXME drow/2004-04-01: What should we be doing with
7982 function-local names? For partial symbols, we should probably be
7983 ignoring them. */
fa9c3fa0
TT
7984 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7985 dwarf_tag_name (parent->tag),
7986 sect_offset_str (pdi->sect_off));
72bf9492 7987 parent->scope = grandparent_scope;
c906108c
SS
7988 }
7989
72bf9492
DJ
7990 parent->scope_set = 1;
7991 return parent->scope;
7992}
7993
7994/* Return the fully scoped name associated with PDI, from compilation unit
7995 CU. The result will be allocated with malloc. */
4568ecf9 7996
43816ebc 7997static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
7998partial_die_full_name (struct partial_die_info *pdi,
7999 struct dwarf2_cu *cu)
8000{
15d034d0 8001 const char *parent_scope;
72bf9492 8002
98bfdba5
PA
8003 /* If this is a template instantiation, we can not work out the
8004 template arguments from partial DIEs. So, unfortunately, we have
8005 to go through the full DIEs. At least any work we do building
8006 types here will be reused if full symbols are loaded later. */
8007 if (pdi->has_template_arguments)
8008 {
52356b79 8009 pdi->fixup (cu);
98bfdba5 8010
7d00ffec 8011 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8012 {
8013 struct die_info *die;
8014 struct attribute attr;
8015 struct dwarf2_cu *ref_cu = cu;
8016
b64f50a1 8017 /* DW_FORM_ref_addr is using section offset. */
b4069958 8018 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8019 attr.form = DW_FORM_ref_addr;
9c541725 8020 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8021 die = follow_die_ref (NULL, &attr, &ref_cu);
8022
43816ebc 8023 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8024 }
8025 }
8026
72bf9492
DJ
8027 parent_scope = partial_die_parent_scope (pdi, cu);
8028 if (parent_scope == NULL)
8029 return NULL;
8030 else
43816ebc 8031 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8032 pdi->name (cu),
8033 0, cu));
c906108c
SS
8034}
8035
8036static void
72bf9492 8037add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8038{
976ca316
SM
8039 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8040 struct objfile *objfile = per_objfile->objfile;
08feed99 8041 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8042 CORE_ADDR addr = 0;
15d034d0 8043 const char *actual_name = NULL;
e142c38c
DJ
8044 CORE_ADDR baseaddr;
8045
b3b3bada 8046 baseaddr = objfile->text_section_offset ();
c906108c 8047
43816ebc
TT
8048 gdb::unique_xmalloc_ptr<char> built_actual_name
8049 = partial_die_full_name (pdi, cu);
15d034d0 8050 if (built_actual_name != NULL)
43816ebc 8051 actual_name = built_actual_name.get ();
63d06c5c 8052
72bf9492 8053 if (actual_name == NULL)
7d00ffec 8054 actual_name = pdi->name (cu);
72bf9492 8055
76e288d1
TT
8056 partial_symbol psymbol;
8057 memset (&psymbol, 0, sizeof (psymbol));
8058 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 8059 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
8060
8061 /* The code below indicates that the psymbol should be installed by
8062 setting this. */
8063 gdb::optional<psymbol_placement> where;
8064
c906108c
SS
8065 switch (pdi->tag)
8066 {
b1dc1806 8067 case DW_TAG_inlined_subroutine:
c906108c 8068 case DW_TAG_subprogram:
79748972
TT
8069 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8070 - baseaddr);
0a4b0913
AB
8071 if (pdi->is_external
8072 || cu->language == language_ada
8073 || (cu->language == language_fortran
8074 && pdi->die_parent != NULL
8075 && pdi->die_parent->tag == DW_TAG_subprogram))
8076 {
dda83cd7
SM
8077 /* Normally, only "external" DIEs are part of the global scope.
8078 But in Ada and Fortran, we want to be able to access nested
8079 procedures globally. So all Ada and Fortran subprograms are
8080 stored in the global scope. */
76e288d1 8081 where = psymbol_placement::GLOBAL;
c906108c
SS
8082 }
8083 else
76e288d1
TT
8084 where = psymbol_placement::STATIC;
8085
8086 psymbol.domain = VAR_DOMAIN;
8087 psymbol.aclass = LOC_BLOCK;
a52d653e 8088 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 8089 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8090
8091 if (pdi->main_subprogram && actual_name != NULL)
8092 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8093 break;
72929c62 8094 case DW_TAG_constant:
76e288d1
TT
8095 psymbol.domain = VAR_DOMAIN;
8096 psymbol.aclass = LOC_STATIC;
8097 where = (pdi->is_external
8098 ? psymbol_placement::GLOBAL
8099 : psymbol_placement::STATIC);
72929c62 8100 break;
c906108c 8101 case DW_TAG_variable:
95554aad
TT
8102 if (pdi->d.locdesc)
8103 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8104
95554aad 8105 if (pdi->d.locdesc
caac4577 8106 && addr == 0
976ca316 8107 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8108 {
8109 /* A global or static variable may also have been stripped
8110 out by the linker if unused, in which case its address
8111 will be nullified; do not add such variables into partial
8112 symbol table then. */
8113 }
8114 else if (pdi->is_external)
c906108c
SS
8115 {
8116 /* Global Variable.
8117 Don't enter into the minimal symbol tables as there is
8118 a minimal symbol table entry from the ELF symbols already.
8119 Enter into partial symbol table if it has a location
8120 descriptor or a type.
8121 If the location descriptor is missing, new_symbol will create
8122 a LOC_UNRESOLVED symbol, the address of the variable will then
8123 be determined from the minimal symbol table whenever the variable
8124 is referenced.
8125 The address for the partial symbol table entry is not
8126 used by GDB, but it comes in handy for debugging partial symbol
8127 table building. */
8128
95554aad 8129 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8130 {
8131 psymbol.domain = VAR_DOMAIN;
8132 psymbol.aclass = LOC_STATIC;
a52d653e 8133 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8134 psymbol.ginfo.value.address = addr;
8135 where = psymbol_placement::GLOBAL;
8136 }
c906108c
SS
8137 }
8138 else
8139 {
ff908ebf
AW
8140 int has_loc = pdi->d.locdesc != NULL;
8141
8142 /* Static Variable. Skip symbols whose value we cannot know (those
8143 without location descriptors or constant values). */
8144 if (!has_loc && !pdi->has_const_value)
43816ebc 8145 return;
ff908ebf 8146
76e288d1
TT
8147 psymbol.domain = VAR_DOMAIN;
8148 psymbol.aclass = LOC_STATIC;
a52d653e 8149 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8150 if (has_loc)
8151 psymbol.ginfo.value.address = addr;
8152 where = psymbol_placement::STATIC;
c906108c
SS
8153 }
8154 break;
d8f62e84 8155 case DW_TAG_array_type:
c906108c
SS
8156 case DW_TAG_typedef:
8157 case DW_TAG_base_type:
a02abb62 8158 case DW_TAG_subrange_type:
76e288d1
TT
8159 psymbol.domain = VAR_DOMAIN;
8160 psymbol.aclass = LOC_TYPEDEF;
8161 where = psymbol_placement::STATIC;
c906108c 8162 break;
74921315 8163 case DW_TAG_imported_declaration:
72bf9492 8164 case DW_TAG_namespace:
76e288d1
TT
8165 psymbol.domain = VAR_DOMAIN;
8166 psymbol.aclass = LOC_TYPEDEF;
8167 where = psymbol_placement::GLOBAL;
72bf9492 8168 break;
530e8392 8169 case DW_TAG_module:
a5fd13a9 8170 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8171 available without any name. If so, we skip the module as it
8172 doesn't bring any value. */
a5fd13a9 8173 if (actual_name != nullptr)
76e288d1
TT
8174 {
8175 psymbol.domain = MODULE_DOMAIN;
8176 psymbol.aclass = LOC_TYPEDEF;
8177 where = psymbol_placement::GLOBAL;
8178 }
530e8392 8179 break;
c906108c 8180 case DW_TAG_class_type:
680b30c7 8181 case DW_TAG_interface_type:
c906108c
SS
8182 case DW_TAG_structure_type:
8183 case DW_TAG_union_type:
8184 case DW_TAG_enumeration_type:
fa4028e9 8185 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8186 about "Structure, Union, and Class Type Entries": "An incomplete
8187 structure, union or class type is represented by a structure,
8188 union or class entry that does not have a byte size attribute
8189 and that has a DW_AT_declaration attribute." */
fa4028e9 8190 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8191 return;
fa4028e9 8192
63d06c5c
DC
8193 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8194 static vs. global. */
76e288d1
TT
8195 psymbol.domain = STRUCT_DOMAIN;
8196 psymbol.aclass = LOC_TYPEDEF;
8197 where = (cu->language == language_cplus
8198 ? psymbol_placement::GLOBAL
8199 : psymbol_placement::STATIC);
c906108c
SS
8200 break;
8201 case DW_TAG_enumerator:
76e288d1
TT
8202 psymbol.domain = VAR_DOMAIN;
8203 psymbol.aclass = LOC_CONST;
8204 where = (cu->language == language_cplus
8205 ? psymbol_placement::GLOBAL
8206 : psymbol_placement::STATIC);
c906108c
SS
8207 break;
8208 default:
8209 break;
8210 }
76e288d1
TT
8211
8212 if (where.has_value ())
8213 {
f049a313
TT
8214 if (built_actual_name != nullptr)
8215 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8216 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8217 psymbol.ginfo.set_linkage_name (actual_name);
8218 else
8219 {
8220 psymbol.ginfo.set_demangled_name (actual_name,
8221 &objfile->objfile_obstack);
8222 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8223 }
7b249e47
TT
8224 cu->per_cu->v.psymtab->add_psymbol
8225 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8226 objfile);
76e288d1 8227 }
c906108c
SS
8228}
8229
5c4e30ca
DC
8230/* Read a partial die corresponding to a namespace; also, add a symbol
8231 corresponding to that namespace to the symbol table. NAMESPACE is
8232 the name of the enclosing namespace. */
91c24f0a 8233
72bf9492
DJ
8234static void
8235add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8236 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8237 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8238{
72bf9492 8239 /* Add a symbol for the namespace. */
e7c27a73 8240
72bf9492 8241 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8242
8243 /* Now scan partial symbols in that namespace. */
8244
91c24f0a 8245 if (pdi->has_children)
cdc07690 8246 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8247}
8248
5d7cb8df
JK
8249/* Read a partial die corresponding to a Fortran module. */
8250
8251static void
8252add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8253 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8254{
530e8392
KB
8255 /* Add a symbol for the namespace. */
8256
8257 add_partial_symbol (pdi, cu);
8258
f55ee35c 8259 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8260
8261 if (pdi->has_children)
cdc07690 8262 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8263}
8264
b1dc1806
XR
8265/* Read a partial die corresponding to a subprogram or an inlined
8266 subprogram and create a partial symbol for that subprogram.
8267 When the CU language allows it, this routine also defines a partial
8268 symbol for each nested subprogram that this subprogram contains.
8269 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8270 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8271
cdc07690
YQ
8272 PDI may also be a lexical block, in which case we simply search
8273 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8274 Again, this is only performed when the CU language allows this
8275 type of definitions. */
8276
8277static void
8278add_partial_subprogram (struct partial_die_info *pdi,
8279 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8280 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8281{
b1dc1806 8282 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8283 {
8284 if (pdi->has_pc_info)
dda83cd7
SM
8285 {
8286 if (pdi->lowpc < *lowpc)
8287 *lowpc = pdi->lowpc;
8288 if (pdi->highpc > *highpc)
8289 *highpc = pdi->highpc;
cdc07690 8290 if (set_addrmap)
5734ee8b 8291 {
5e22e966 8292 struct objfile *objfile = cu->per_objfile->objfile;
84685904 8293 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 8294 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8295 CORE_ADDR baseaddr;
b926417a
TT
8296 CORE_ADDR this_highpc;
8297 CORE_ADDR this_lowpc;
5734ee8b 8298
b3b3bada 8299 baseaddr = objfile->text_section_offset ();
b926417a
TT
8300 this_lowpc
8301 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8302 pdi->lowpc + baseaddr)
8303 - baseaddr);
8304 this_highpc
8305 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8306 pdi->highpc + baseaddr)
8307 - baseaddr);
84685904 8308 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
b926417a 8309 this_lowpc, this_highpc - 1,
9291a0cd 8310 cu->per_cu->v.psymtab);
5734ee8b 8311 }
dda83cd7 8312 }
481860b3
GB
8313
8314 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8315 {
dda83cd7 8316 if (!pdi->is_declaration)
e8d05480
JB
8317 /* Ignore subprogram DIEs that do not have a name, they are
8318 illegal. Do not emit a complaint at this point, we will
8319 do so when we convert this psymtab into a symtab. */
7d00ffec 8320 if (pdi->name (cu))
e8d05480 8321 add_partial_symbol (pdi, cu);
dda83cd7 8322 }
bc30ff58 8323 }
6e70227d 8324
bc30ff58
JB
8325 if (! pdi->has_children)
8326 return;
8327
0a4b0913 8328 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8329 {
8330 pdi = pdi->die_child;
8331 while (pdi != NULL)
8332 {
52356b79 8333 pdi->fixup (cu);
bc30ff58 8334 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8335 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8336 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8337 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8338 pdi = pdi->die_sibling;
8339 }
8340 }
8341}
8342
91c24f0a
DC
8343/* Read a partial die corresponding to an enumeration type. */
8344
72bf9492
DJ
8345static void
8346add_partial_enumeration (struct partial_die_info *enum_pdi,
8347 struct dwarf2_cu *cu)
91c24f0a 8348{
72bf9492 8349 struct partial_die_info *pdi;
91c24f0a 8350
7d00ffec 8351 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8352 add_partial_symbol (enum_pdi, cu);
8353
8354 pdi = enum_pdi->die_child;
8355 while (pdi)
91c24f0a 8356 {
7d00ffec 8357 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8358 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8359 else
72bf9492
DJ
8360 add_partial_symbol (pdi, cu);
8361 pdi = pdi->die_sibling;
91c24f0a 8362 }
91c24f0a
DC
8363}
8364
6caca83c
CC
8365/* Return the initial uleb128 in the die at INFO_PTR. */
8366
8367static unsigned int
d521ce57 8368peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8369{
8370 unsigned int bytes_read;
8371
8372 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8373}
8374
685af9cd
TT
8375/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8376 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8377
4bb7a0a7
DJ
8378 Return the corresponding abbrev, or NULL if the number is zero (indicating
8379 an empty DIE). In either case *BYTES_READ will be set to the length of
8380 the initial number. */
8381
7c32eebb 8382static const struct abbrev_info *
685af9cd
TT
8383peek_die_abbrev (const die_reader_specs &reader,
8384 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8385{
685af9cd 8386 dwarf2_cu *cu = reader.cu;
0280fdcc 8387 bfd *abfd = reader.abfd;
685af9cd
TT
8388 unsigned int abbrev_number
8389 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8390
8391 if (abbrev_number == 0)
8392 return NULL;
8393
7c32eebb
TT
8394 const abbrev_info *abbrev
8395 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8396 if (!abbrev)
8397 {
422b9917 8398 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8399 " at offset %s [in module %s]"),
422b9917 8400 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8401 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8402 }
8403
8404 return abbrev;
8405}
8406
93311388
DE
8407/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8408 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8409 DIE. Any children of the skipped DIEs will also be skipped. */
8410
d521ce57
TT
8411static const gdb_byte *
8412skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8413{
4bb7a0a7
DJ
8414 while (1)
8415 {
685af9cd 8416 unsigned int bytes_read;
7c32eebb
TT
8417 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8418 &bytes_read);
685af9cd 8419
4bb7a0a7
DJ
8420 if (abbrev == NULL)
8421 return info_ptr + bytes_read;
8422 else
dee91e82 8423 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8424 }
8425}
8426
93311388
DE
8427/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8428 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8429 abbrev corresponding to that skipped uleb128 should be passed in
8430 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8431 children. */
8432
d521ce57
TT
8433static const gdb_byte *
8434skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8435 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8436{
8437 unsigned int bytes_read;
8438 struct attribute attr;
dee91e82
DE
8439 bfd *abfd = reader->abfd;
8440 struct dwarf2_cu *cu = reader->cu;
d521ce57 8441 const gdb_byte *buffer = reader->buffer;
f664829e 8442 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8443 unsigned int form, i;
8444
8445 for (i = 0; i < abbrev->num_attrs; i++)
8446 {
8447 /* The only abbrev we care about is DW_AT_sibling. */
8448 if (abbrev->attrs[i].name == DW_AT_sibling)
8449 {
7a5f294d 8450 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8451 if (attr.form == DW_FORM_ref_addr)
b98664d3 8452 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8453 else
b9502d3f 8454 {
0826b30a 8455 sect_offset off = attr.get_ref_die_offset ();
9c541725 8456 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8457
8458 if (sibling_ptr < info_ptr)
b98664d3 8459 complaint (_("DW_AT_sibling points backwards"));
22869d73 8460 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8461 reader->die_section->overflow_complaint ();
b9502d3f
WN
8462 else
8463 return sibling_ptr;
8464 }
4bb7a0a7
DJ
8465 }
8466
8467 /* If it isn't DW_AT_sibling, skip this attribute. */
8468 form = abbrev->attrs[i].form;
8469 skip_attribute:
8470 switch (form)
8471 {
4bb7a0a7 8472 case DW_FORM_ref_addr:
ae411497
TT
8473 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8474 and later it is offset sized. */
8475 if (cu->header.version == 2)
8476 info_ptr += cu->header.addr_size;
8477 else
8478 info_ptr += cu->header.offset_size;
8479 break;
36586728
TT
8480 case DW_FORM_GNU_ref_alt:
8481 info_ptr += cu->header.offset_size;
8482 break;
ae411497 8483 case DW_FORM_addr:
4bb7a0a7
DJ
8484 info_ptr += cu->header.addr_size;
8485 break;
8486 case DW_FORM_data1:
8487 case DW_FORM_ref1:
8488 case DW_FORM_flag:
8fe0f950 8489 case DW_FORM_strx1:
4bb7a0a7
DJ
8490 info_ptr += 1;
8491 break;
2dc7f7b3 8492 case DW_FORM_flag_present:
43988095 8493 case DW_FORM_implicit_const:
2dc7f7b3 8494 break;
4bb7a0a7
DJ
8495 case DW_FORM_data2:
8496 case DW_FORM_ref2:
8fe0f950 8497 case DW_FORM_strx2:
4bb7a0a7
DJ
8498 info_ptr += 2;
8499 break;
8fe0f950
AT
8500 case DW_FORM_strx3:
8501 info_ptr += 3;
8502 break;
4bb7a0a7
DJ
8503 case DW_FORM_data4:
8504 case DW_FORM_ref4:
8fe0f950 8505 case DW_FORM_strx4:
4bb7a0a7
DJ
8506 info_ptr += 4;
8507 break;
8508 case DW_FORM_data8:
8509 case DW_FORM_ref8:
55f1336d 8510 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8511 info_ptr += 8;
8512 break;
0224619f
JK
8513 case DW_FORM_data16:
8514 info_ptr += 16;
8515 break;
4bb7a0a7 8516 case DW_FORM_string:
9b1c24c8 8517 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8518 info_ptr += bytes_read;
8519 break;
2dc7f7b3 8520 case DW_FORM_sec_offset:
4bb7a0a7 8521 case DW_FORM_strp:
36586728 8522 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8523 info_ptr += cu->header.offset_size;
8524 break;
2dc7f7b3 8525 case DW_FORM_exprloc:
4bb7a0a7
DJ
8526 case DW_FORM_block:
8527 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8528 info_ptr += bytes_read;
8529 break;
8530 case DW_FORM_block1:
8531 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8532 break;
8533 case DW_FORM_block2:
8534 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8535 break;
8536 case DW_FORM_block4:
8537 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8538 break;
336d760d 8539 case DW_FORM_addrx:
cf532bd1 8540 case DW_FORM_strx:
4bb7a0a7
DJ
8541 case DW_FORM_sdata:
8542 case DW_FORM_udata:
8543 case DW_FORM_ref_udata:
3019eac3
DE
8544 case DW_FORM_GNU_addr_index:
8545 case DW_FORM_GNU_str_index:
18a8505e 8546 case DW_FORM_rnglistx:
41144253 8547 case DW_FORM_loclistx:
d521ce57 8548 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8549 break;
8550 case DW_FORM_indirect:
8551 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8552 info_ptr += bytes_read;
8553 /* We need to continue parsing from here, so just go back to
8554 the top. */
8555 goto skip_attribute;
8556
8557 default:
3e43a32a
MS
8558 error (_("Dwarf Error: Cannot handle %s "
8559 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8560 dwarf_form_name (form),
8561 bfd_get_filename (abfd));
8562 }
8563 }
8564
8565 if (abbrev->has_children)
dee91e82 8566 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8567 else
8568 return info_ptr;
8569}
8570
93311388 8571/* Locate ORIG_PDI's sibling.
dee91e82 8572 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8573
d521ce57 8574static const gdb_byte *
dee91e82
DE
8575locate_pdi_sibling (const struct die_reader_specs *reader,
8576 struct partial_die_info *orig_pdi,
d521ce57 8577 const gdb_byte *info_ptr)
91c24f0a
DC
8578{
8579 /* Do we know the sibling already? */
72bf9492 8580
91c24f0a
DC
8581 if (orig_pdi->sibling)
8582 return orig_pdi->sibling;
8583
8584 /* Are there any children to deal with? */
8585
8586 if (!orig_pdi->has_children)
8587 return info_ptr;
8588
4bb7a0a7 8589 /* Skip the children the long way. */
91c24f0a 8590
dee91e82 8591 return skip_children (reader, info_ptr);
91c24f0a
DC
8592}
8593
257e7a09 8594/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8595 not NULL. */
c906108c 8596
891813be
TT
8597void
8598dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8599{
976ca316 8600 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8601
976ca316 8602 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8603
077cbab2
TT
8604 /* If this psymtab is constructed from a debug-only objfile, the
8605 has_section_at_zero flag will not necessarily be correct. We
8606 can get the correct value for this flag by looking at the data
8607 associated with the (presumably stripped) associated objfile. */
8608 if (objfile->separate_debug_objfile_backlink)
c906108c 8609 {
976ca316 8610 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8611 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8612
976ca316
SM
8613 per_objfile->per_bfd->has_section_at_zero
8614 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8615 }
98bfdba5 8616
8566b89b 8617 expand_psymtab (objfile);
95554aad 8618
976ca316 8619 process_cu_includes (per_objfile);
c906108c 8620}
9cdd5dbd
DE
8621\f
8622/* Reading in full CUs. */
c906108c 8623
10b3939b
DJ
8624/* Add PER_CU to the queue. */
8625
8626static void
120ce1b5
SM
8627queue_comp_unit (dwarf2_per_cu_data *per_cu,
8628 dwarf2_per_objfile *per_objfile,
95554aad 8629 enum language pretend_language)
10b3939b 8630{
10b3939b 8631 per_cu->queued = 1;
08ac5771
SM
8632
8633 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8634 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8635}
8636
616c069a
SM
8637/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8638
89e63ee4
DE
8639 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8640 dependency.
69d751e3 8641
616c069a
SM
8642 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8643 DIEs, false otherwise.
8644
8645 Explanation: there is an invariant that if a CU is queued for expansion
8646 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8647 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8648 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8649 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8650 invariant is respected.
8651
8652 The caller is therefore not required to load the CU's DIEs (we return false)
8653 if:
8654
8655 - the CU is already expanded, and therefore does not get enqueued
8656 - the CU gets enqueued for expansion, but its DIEs are already loaded
8657
8658 Note that the caller should not use this function's return value as an
8659 indicator of whether the CU's DIEs are loaded right now, it should check
8660 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
8661
8662static int
89e63ee4 8663maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8664 dwarf2_per_cu_data *per_cu,
8665 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8666 enum language pretend_language)
8667{
8668 /* We may arrive here during partial symbol reading, if we need full
8669 DIEs to process an unusual case (e.g. template arguments). Do
8670 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8671 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 8672 {
7188ed02
SM
8673 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8674
8675 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
8676 return 1;
8677 return 0;
8678 }
8679
8680 /* Mark the dependence relation so that we don't flush PER_CU
8681 too early. */
89e63ee4
DE
8682 if (dependent_cu != NULL)
8683 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8684
8685 /* If it's already on the queue, we have nothing to do. */
8686 if (per_cu->queued)
de53369b
SM
8687 {
8688 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8689 loaded. */
8690 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
8691
8692 /* If the CU is queued for expansion, it should not already be
8693 expanded. */
8694 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8695
8696 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
8697 return 0;
8698 }
0907af0c 8699
616c069a
SM
8700 bool queued = false;
8701 if (!per_objfile->symtab_set_p (per_cu))
8702 {
8703 /* Add it to the queue. */
8704 queue_comp_unit (per_cu, per_objfile, pretend_language);
8705 queued = true;
8706 }
8707
0907af0c
DE
8708 /* If the compilation unit is already loaded, just mark it as
8709 used. */
7188ed02
SM
8710 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8711 if (cu != nullptr)
616c069a 8712 cu->last_used = 0;
0907af0c 8713
616c069a
SM
8714 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8715 and the DIEs are not already loaded. */
8716 return queued && cu == nullptr;
0907af0c
DE
8717}
8718
10b3939b
DJ
8719/* Process the queue. */
8720
8721static void
976ca316 8722process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 8723{
6f738b01
SM
8724 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8725 objfile_name (per_objfile->objfile));
45cfd468 8726
03dd20cc
DJ
8727 /* The queue starts out with one item, but following a DIE reference
8728 may load a new CU, adding it to the end of the queue. */
08ac5771 8729 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 8730 {
08ac5771 8731 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 8732 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 8733
976ca316 8734 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 8735 {
976ca316 8736 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 8737
7188ed02
SM
8738 /* Skip dummy CUs. */
8739 if (cu != nullptr)
73be47f5 8740 {
7188ed02
SM
8741 unsigned int debug_print_threshold;
8742 char buf[100];
8743
8744 if (per_cu->is_debug_types)
8745 {
8746 struct signatured_type *sig_type =
8747 (struct signatured_type *) per_cu;
8748
8749 sprintf (buf, "TU %s at offset %s",
8750 hex_string (sig_type->signature),
8751 sect_offset_str (per_cu->sect_off));
8752 /* There can be 100s of TUs.
8753 Only print them in verbose mode. */
8754 debug_print_threshold = 2;
8755 }
8756 else
8757 {
8758 sprintf (buf, "CU at offset %s",
8759 sect_offset_str (per_cu->sect_off));
8760 debug_print_threshold = 1;
8761 }
247f5c4f 8762
7188ed02 8763 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8764 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 8765
7188ed02
SM
8766 if (per_cu->is_debug_types)
8767 process_full_type_unit (cu, item.pretend_language);
8768 else
8769 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 8770
7188ed02 8771 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8772 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 8773 }
f4dc4d17 8774 }
10b3939b 8775
7188ed02 8776 per_cu->queued = 0;
08ac5771 8777 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
8778 }
8779
6f738b01
SM
8780 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8781 objfile_name (per_objfile->objfile));
10b3939b
DJ
8782}
8783
10b3939b
DJ
8784/* Read in full symbols for PST, and anything it depends on. */
8785
8566b89b
TT
8786void
8787dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8788{
af758d11 8789 gdb_assert (!readin_p (objfile));
95554aad 8790
17ee85fc
TT
8791 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8792 free_cached_comp_units freer (per_objfile);
48993951 8793 expand_dependencies (objfile);
aaa75496 8794
97a1449a 8795 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 8796 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
8797}
8798
af758d11
SM
8799/* See psympriv.h. */
8800
8801bool
8802dwarf2_psymtab::readin_p (struct objfile *objfile) const
8803{
8804 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8805 return per_objfile->symtab_set_p (per_cu_data);
8806}
8807
8808/* See psympriv.h. */
8809
8810compunit_symtab *
8811dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8812{
8813 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8814 return per_objfile->get_symtab (per_cu_data);
8815}
8816
dee91e82
DE
8817/* Trivial hash function for die_info: the hash value of a DIE
8818 is its offset in .debug_info for this objfile. */
10b3939b 8819
dee91e82
DE
8820static hashval_t
8821die_hash (const void *item)
10b3939b 8822{
9a3c8263 8823 const struct die_info *die = (const struct die_info *) item;
6502dd73 8824
9c541725 8825 return to_underlying (die->sect_off);
dee91e82 8826}
63d06c5c 8827
dee91e82
DE
8828/* Trivial comparison function for die_info structures: two DIEs
8829 are equal if they have the same offset. */
98bfdba5 8830
dee91e82
DE
8831static int
8832die_eq (const void *item_lhs, const void *item_rhs)
8833{
9a3c8263
SM
8834 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8835 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8836
9c541725 8837 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8838}
c906108c 8839
4a636814
SM
8840/* Load the DIEs associated with PER_CU into memory.
8841
8842 In some cases, the caller, while reading partial symbols, will need to load
8843 the full symbols for the CU for some reason. It will already have a
8844 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8845 rather than creating a new one. */
c906108c 8846
dee91e82 8847static void
ab432490
SM
8848load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8849 dwarf2_per_objfile *per_objfile,
4a636814 8850 dwarf2_cu *existing_cu,
c0ab21c2
TT
8851 bool skip_partial,
8852 enum language pretend_language)
dee91e82 8853{
c0ab21c2
TT
8854 gdb_assert (! this_cu->is_debug_types);
8855
7188ed02 8856 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
8857 if (reader.dummy_p)
8858 return;
8859
8860 struct dwarf2_cu *cu = reader.cu;
8861 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8862
dee91e82
DE
8863 gdb_assert (cu->die_hash == NULL);
8864 cu->die_hash =
8865 htab_create_alloc_ex (cu->header.length / 12,
8866 die_hash,
8867 die_eq,
8868 NULL,
8869 &cu->comp_unit_obstack,
8870 hashtab_obstack_allocate,
8871 dummy_obstack_deallocate);
e142c38c 8872
3e225074 8873 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8874 reader.comp_unit_die->child
8875 = read_die_and_siblings (&reader, reader.info_ptr,
8876 &info_ptr, reader.comp_unit_die);
8877 cu->dies = reader.comp_unit_die;
dee91e82 8878 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8879
8880 /* We try not to read any attributes in this function, because not
9cdd5dbd 8881 all CUs needed for references have been loaded yet, and symbol
10b3939b 8882 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8883 or we won't be able to build types correctly.
8884 Similarly, if we do not read the producer, we can not apply
8885 producer-specific interpretation. */
c0ab21c2 8886 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8887
8888 reader.keep ();
10b3939b
DJ
8889}
8890
3da10d80
KS
8891/* Add a DIE to the delayed physname list. */
8892
8893static void
8894add_to_method_list (struct type *type, int fnfield_index, int index,
8895 const char *name, struct die_info *die,
8896 struct dwarf2_cu *cu)
8897{
8898 struct delayed_method_info mi;
8899 mi.type = type;
8900 mi.fnfield_index = fnfield_index;
8901 mi.index = index;
8902 mi.name = name;
8903 mi.die = die;
c89b44cd 8904 cu->method_list.push_back (mi);
3da10d80
KS
8905}
8906
3693fdb3
PA
8907/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8908 "const" / "volatile". If so, decrements LEN by the length of the
8909 modifier and return true. Otherwise return false. */
8910
8911template<size_t N>
8912static bool
8913check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8914{
8915 size_t mod_len = sizeof (mod) - 1;
8916 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8917 {
8918 len -= mod_len;
8919 return true;
8920 }
8921 return false;
8922}
8923
3da10d80
KS
8924/* Compute the physnames of any methods on the CU's method list.
8925
8926 The computation of method physnames is delayed in order to avoid the
8927 (bad) condition that one of the method's formal parameters is of an as yet
8928 incomplete type. */
8929
8930static void
8931compute_delayed_physnames (struct dwarf2_cu *cu)
8932{
3693fdb3 8933 /* Only C++ delays computing physnames. */
c89b44cd 8934 if (cu->method_list.empty ())
3693fdb3
PA
8935 return;
8936 gdb_assert (cu->language == language_cplus);
8937
52941706 8938 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8939 {
1d06ead6 8940 const char *physname;
3da10d80 8941 struct fn_fieldlist *fn_flp
c89b44cd
TT
8942 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8943 physname = dwarf2_physname (mi.name, mi.die, cu);
8944 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8945 = physname ? physname : "";
3693fdb3
PA
8946
8947 /* Since there's no tag to indicate whether a method is a
8948 const/volatile overload, extract that information out of the
8949 demangled name. */
8950 if (physname != NULL)
8951 {
8952 size_t len = strlen (physname);
8953
8954 while (1)
8955 {
8956 if (physname[len] == ')') /* shortcut */
8957 break;
8958 else if (check_modifier (physname, len, " const"))
c89b44cd 8959 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 8960 else if (check_modifier (physname, len, " volatile"))
c89b44cd 8961 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
8962 else
8963 break;
8964 }
8965 }
3da10d80 8966 }
c89b44cd
TT
8967
8968 /* The list is no longer needed. */
8969 cu->method_list.clear ();
3da10d80
KS
8970}
8971
a766d390
DE
8972/* Go objects should be embedded in a DW_TAG_module DIE,
8973 and it's not clear if/how imported objects will appear.
8974 To keep Go support simple until that's worked out,
8975 go back through what we've read and create something usable.
8976 We could do this while processing each DIE, and feels kinda cleaner,
8977 but that way is more invasive.
8978 This is to, for example, allow the user to type "p var" or "b main"
8979 without having to specify the package name, and allow lookups
8980 of module.object to work in contexts that use the expression
8981 parser. */
8982
8983static void
8984fixup_go_packaging (struct dwarf2_cu *cu)
8985{
421d1616 8986 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
8987 struct pending *list;
8988 int i;
8989
c24bdb02 8990 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
8991 list != NULL;
8992 list = list->next)
a766d390
DE
8993 {
8994 for (i = 0; i < list->nsyms; ++i)
8995 {
8996 struct symbol *sym = list->symbol[i];
8997
c1b5c1eb 8998 if (sym->language () == language_go
a766d390
DE
8999 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9000 {
421d1616
TT
9001 gdb::unique_xmalloc_ptr<char> this_package_name
9002 (go_symbol_package_name (sym));
a766d390
DE
9003
9004 if (this_package_name == NULL)
9005 continue;
9006 if (package_name == NULL)
421d1616 9007 package_name = std::move (this_package_name);
a766d390
DE
9008 else
9009 {
5e22e966 9010 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9011 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9012 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9013 (symbol_symtab (sym) != NULL
9014 ? symtab_to_filename_for_display
9015 (symbol_symtab (sym))
e3b94546 9016 : objfile_name (objfile)),
421d1616 9017 this_package_name.get (), package_name.get ());
a766d390
DE
9018 }
9019 }
9020 }
9021 }
9022
9023 if (package_name != NULL)
9024 {
5e22e966 9025 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9026 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9027 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9028 saved_package_name);
a766d390
DE
9029 struct symbol *sym;
9030
8c14c3a3 9031 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9032 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9033 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9034 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9035 e.g., "main" finds the "main" module and not C's main(). */
9036 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9037 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9038 SYMBOL_TYPE (sym) = type;
9039
c24bdb02 9040 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9041 }
9042}
9043
c9317f21
TT
9044/* Allocate a fully-qualified name consisting of the two parts on the
9045 obstack. */
9046
9047static const char *
9048rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9049{
9050 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9051}
9052
9c6a1327
TT
9053/* A helper that allocates a variant part to attach to a Rust enum
9054 type. OBSTACK is where the results should be allocated. TYPE is
9055 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9056 discriminant. It must be the index of one of the fields of TYPE,
9057 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9058 DEFAULT_INDEX is the index of the default field; or -1 if there is
9059 no default. RANGES is indexed by "effective" field number (the
9060 field index, but omitting the discriminant and default fields) and
9061 must hold the discriminant values used by the variants. Note that
9062 RANGES must have a lifetime at least as long as OBSTACK -- either
9063 already allocated on it, or static. */
c9317f21 9064
9c6a1327
TT
9065static void
9066alloc_rust_variant (struct obstack *obstack, struct type *type,
9067 int discriminant_index, int default_index,
9068 gdb::array_view<discriminant_range> ranges)
9069{
57d02173
TT
9070 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9071 gdb_assert (discriminant_index == -1
9072 || (discriminant_index >= 0
9073 && discriminant_index < type->num_fields ()));
c9317f21 9074 gdb_assert (default_index == -1
1f704f76 9075 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9076
9c6a1327 9077 /* We have one variant for each non-discriminant field. */
57d02173
TT
9078 int n_variants = type->num_fields ();
9079 if (discriminant_index != -1)
9080 --n_variants;
c9317f21 9081
9c6a1327
TT
9082 variant *variants = new (obstack) variant[n_variants];
9083 int var_idx = 0;
9084 int range_idx = 0;
1f704f76 9085 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9086 {
9087 if (i == discriminant_index)
9088 continue;
c9317f21 9089
9c6a1327
TT
9090 variants[var_idx].first_field = i;
9091 variants[var_idx].last_field = i + 1;
9092
9093 /* The default field does not need a range, but other fields do.
9094 We skipped the discriminant above. */
9095 if (i != default_index)
9096 {
9097 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9098 ++range_idx;
9099 }
c9317f21 9100
9c6a1327
TT
9101 ++var_idx;
9102 }
9103
9104 gdb_assert (range_idx == ranges.size ());
9105 gdb_assert (var_idx == n_variants);
9106
9107 variant_part *part = new (obstack) variant_part;
9108 part->discriminant_index = discriminant_index;
57d02173
TT
9109 /* If there is no discriminant, then whether it is signed is of no
9110 consequence. */
9111 part->is_unsigned
9112 = (discriminant_index == -1
9113 ? false
c6d940a9 9114 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9115 part->variants = gdb::array_view<variant> (variants, n_variants);
9116
9117 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9118 gdb::array_view<variant_part> *prop_value
9119 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9120
9c6a1327 9121 struct dynamic_prop prop;
8c2e4e06 9122 prop.set_variant_parts (prop_value);
9c6a1327 9123
5c54719c 9124 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9125}
9126
9127/* Some versions of rustc emitted enums in an unusual way.
9128
9129 Ordinary enums were emitted as unions. The first element of each
9130 structure in the union was named "RUST$ENUM$DISR". This element
9131 held the discriminant.
9132
9133 These versions of Rust also implemented the "non-zero"
9134 optimization. When the enum had two values, and one is empty and
9135 the other holds a pointer that cannot be zero, the pointer is used
9136 as the discriminant, with a zero value meaning the empty variant.
9137 Here, the union's first member is of the form
9138 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9139 where the fieldnos are the indices of the fields that should be
9140 traversed in order to find the field (which may be several fields deep)
9141 and the variantname is the name of the variant of the case when the
9142 field is zero.
9143
9144 This function recognizes whether TYPE is of one of these forms,
9145 and, if so, smashes it to be a variant type. */
9146
9147static void
9148quirk_rust_enum (struct type *type, struct objfile *objfile)
9149{
78134374 9150 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9151
9152 /* We don't need to deal with empty enums. */
1f704f76 9153 if (type->num_fields () == 0)
c9317f21
TT
9154 return;
9155
9156#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9157 if (type->num_fields () == 1
c9317f21
TT
9158 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9159 {
9160 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9161
9162 /* Decode the field name to find the offset of the
9163 discriminant. */
9164 ULONGEST bit_offset = 0;
940da03e 9165 struct type *field_type = type->field (0).type ();
c9317f21
TT
9166 while (name[0] >= '0' && name[0] <= '9')
9167 {
9168 char *tail;
9169 unsigned long index = strtoul (name, &tail, 10);
9170 name = tail;
9171 if (*name != '$'
1f704f76 9172 || index >= field_type->num_fields ()
c9317f21
TT
9173 || (TYPE_FIELD_LOC_KIND (field_type, index)
9174 != FIELD_LOC_KIND_BITPOS))
9175 {
b98664d3 9176 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9177 "[in module %s]"),
9178 TYPE_FIELD_NAME (type, 0),
9179 objfile_name (objfile));
9180 return;
9181 }
9182 ++name;
9183
9184 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9185 field_type = field_type->field (index).type ();
c9317f21
TT
9186 }
9187
9c6a1327
TT
9188 /* Smash this type to be a structure type. We have to do this
9189 because the type has already been recorded. */
67607e24 9190 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9191 type->set_num_fields (3);
9c6a1327 9192 /* Save the field we care about. */
ceacbf6e 9193 struct field saved_field = type->field (0);
3cabb6b0
SM
9194 type->set_fields
9195 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9196
9c6a1327 9197 /* Put the discriminant at index 0. */
5d14b6e5 9198 type->field (0).set_type (field_type);
9c6a1327
TT
9199 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9200 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9201 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9202
9203 /* The order of fields doesn't really matter, so put the real
9204 field at index 1 and the data-less field at index 2. */
ceacbf6e 9205 type->field (1) = saved_field;
9c6a1327 9206 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9207 = rust_last_path_segment (type->field (1).type ()->name ());
9208 type->field (1).type ()->set_name
7d93a1e0 9209 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9210 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9211
9212 const char *dataless_name
7d93a1e0 9213 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9214 name);
9215 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9216 dataless_name);
5d14b6e5 9217 type->field (2).set_type (dataless_type);
c9317f21
TT
9218 /* NAME points into the original discriminant name, which
9219 already has the correct lifetime. */
9c6a1327 9220 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9221 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9222
9c6a1327
TT
9223 /* Indicate that this is a variant type. */
9224 static discriminant_range ranges[1] = { { 0, 0 } };
9225 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9226 }
77c2dba3
TT
9227 /* A union with a single anonymous field is probably an old-style
9228 univariant enum. */
1f704f76 9229 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9230 {
c9317f21
TT
9231 /* Smash this type to be a structure type. We have to do this
9232 because the type has already been recorded. */
67607e24 9233 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9234
940da03e 9235 struct type *field_type = type->field (0).type ();
c9317f21 9236 const char *variant_name
7d93a1e0 9237 = rust_last_path_segment (field_type->name ());
9c6a1327 9238 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9239 field_type->set_name
9240 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9241 type->name (), variant_name));
57d02173
TT
9242
9243 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9244 }
9245 else
9246 {
9247 struct type *disr_type = nullptr;
1f704f76 9248 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9249 {
940da03e 9250 disr_type = type->field (i).type ();
c9317f21 9251
78134374 9252 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9253 {
9254 /* All fields of a true enum will be structs. */
9255 return;
9256 }
1f704f76 9257 else if (disr_type->num_fields () == 0)
c9317f21
TT
9258 {
9259 /* Could be data-less variant, so keep going. */
a037790e 9260 disr_type = nullptr;
c9317f21
TT
9261 }
9262 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9263 "RUST$ENUM$DISR") != 0)
9264 {
9265 /* Not a Rust enum. */
9266 return;
9267 }
9268 else
9269 {
9270 /* Found one. */
9271 break;
9272 }
9273 }
9274
9275 /* If we got here without a discriminant, then it's probably
9276 just a union. */
9277 if (disr_type == nullptr)
9278 return;
9279
9280 /* Smash this type to be a structure type. We have to do this
9281 because the type has already been recorded. */
67607e24 9282 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9283
9c6a1327 9284 /* Make space for the discriminant field. */
ceacbf6e 9285 struct field *disr_field = &disr_type->field (0);
9c6a1327 9286 field *new_fields
1f704f76 9287 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9288 * sizeof (struct field)));
80fc5e77 9289 memcpy (new_fields + 1, type->fields (),
1f704f76 9290 type->num_fields () * sizeof (struct field));
3cabb6b0 9291 type->set_fields (new_fields);
1f704f76 9292 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9293
9294 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9295 type->field (0) = *disr_field;
9c6a1327
TT
9296 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9297 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9298
9299 /* We need a way to find the correct discriminant given a
9300 variant name. For convenience we build a map here. */
b6cdac4b 9301 struct type *enum_type = disr_field->type ();
c9317f21 9302 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9303 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9304 {
9305 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9306 {
9307 const char *name
9308 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9309 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9310 }
9311 }
9312
1f704f76 9313 int n_fields = type->num_fields ();
9c6a1327
TT
9314 /* We don't need a range entry for the discriminant, but we do
9315 need one for every other field, as there is no default
9316 variant. */
9317 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9318 discriminant_range,
9319 n_fields - 1);
c9317f21
TT
9320 /* Skip the discriminant here. */
9321 for (int i = 1; i < n_fields; ++i)
9322 {
9323 /* Find the final word in the name of this variant's type.
9324 That name can be used to look up the correct
9325 discriminant. */
9326 const char *variant_name
940da03e 9327 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9328
9329 auto iter = discriminant_map.find (variant_name);
9330 if (iter != discriminant_map.end ())
9c6a1327 9331 {
57d02173
TT
9332 ranges[i - 1].low = iter->second;
9333 ranges[i - 1].high = iter->second;
9c6a1327 9334 }
c9317f21 9335
57d02173
TT
9336 /* In Rust, each element should have the size of the
9337 enclosing enum. */
9338 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9339
bedda9ac 9340 /* Remove the discriminant field, if it exists. */
940da03e 9341 struct type *sub_type = type->field (i).type ();
1f704f76 9342 if (sub_type->num_fields () > 0)
bedda9ac 9343 {
5e33d5f4 9344 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9345 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9346 }
9c6a1327 9347 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9348 sub_type->set_name
9349 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9350 type->name (), variant_name));
c9317f21 9351 }
9c6a1327
TT
9352
9353 /* Indicate that this is a variant type. */
a1520ad8 9354 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9355 gdb::array_view<discriminant_range> (ranges,
9356 n_fields - 1));
c9317f21
TT
9357 }
9358}
9359
9360/* Rewrite some Rust unions to be structures with variants parts. */
9361
9362static void
9363rust_union_quirks (struct dwarf2_cu *cu)
9364{
9365 gdb_assert (cu->language == language_rust);
52941706 9366 for (type *type_ : cu->rust_unions)
5e22e966 9367 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9368 /* We don't need this any more. */
9369 cu->rust_unions.clear ();
c9317f21
TT
9370}
9371
8adb8487
TT
9372/* See read.h. */
9373
9374type_unit_group_unshareable *
9375dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9376{
9377 auto iter = this->m_type_units.find (tu_group);
9378 if (iter != this->m_type_units.end ())
9379 return iter->second.get ();
9380
9381 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9382 type_unit_group_unshareable *result = uniq.get ();
9383 this->m_type_units[tu_group] = std::move (uniq);
9384 return result;
9385}
9386
e286671b
TT
9387struct type *
9388dwarf2_per_objfile::get_type_for_signatured_type
9389 (signatured_type *sig_type) const
9390{
9391 auto iter = this->m_type_map.find (sig_type);
9392 if (iter == this->m_type_map.end ())
9393 return nullptr;
9394
9395 return iter->second;
9396}
9397
9398void dwarf2_per_objfile::set_type_for_signatured_type
9399 (signatured_type *sig_type, struct type *type)
9400{
9401 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9402
9403 this->m_type_map[sig_type] = type;
9404}
9405
95554aad
TT
9406/* A helper function for computing the list of all symbol tables
9407 included by PER_CU. */
9408
9409static void
4c39bc03 9410recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9411 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9412 dwarf2_per_cu_data *per_cu,
9413 dwarf2_per_objfile *per_objfile,
43f3e411 9414 struct compunit_symtab *immediate_parent)
95554aad 9415{
af758d11 9416 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9417 if (*slot != NULL)
9418 {
9419 /* This inclusion and its children have been processed. */
9420 return;
9421 }
9422
9423 *slot = per_cu;
af758d11 9424
95554aad 9425 /* Only add a CU if it has a symbol table. */
43182c09 9426 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9427 if (cust != NULL)
ec94af83
DE
9428 {
9429 /* If this is a type unit only add its symbol table if we haven't
9430 seen it yet (type unit per_cu's can share symtabs). */
9431 if (per_cu->is_debug_types)
9432 {
43f3e411 9433 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9434 if (*slot == NULL)
9435 {
43f3e411 9436 *slot = cust;
4c39bc03 9437 result->push_back (cust);
43f3e411
DE
9438 if (cust->user == NULL)
9439 cust->user = immediate_parent;
ec94af83
DE
9440 }
9441 }
9442 else
f9125b6c 9443 {
4c39bc03 9444 result->push_back (cust);
43f3e411
DE
9445 if (cust->user == NULL)
9446 cust->user = immediate_parent;
f9125b6c 9447 }
ec94af83 9448 }
95554aad 9449
ae640021
AB
9450 if (!per_cu->imported_symtabs_empty ())
9451 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9452 {
9453 recursively_compute_inclusions (result, all_children,
43182c09
SM
9454 all_type_symtabs, ptr, per_objfile,
9455 cust);
ae640021 9456 }
95554aad
TT
9457}
9458
43f3e411 9459/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9460 PER_CU. */
9461
9462static void
43182c09
SM
9463compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9464 dwarf2_per_objfile *per_objfile)
95554aad 9465{
f4dc4d17
DE
9466 gdb_assert (! per_cu->is_debug_types);
9467
ae640021 9468 if (!per_cu->imported_symtabs_empty ())
95554aad 9469 {
ae640021 9470 int len;
4c39bc03 9471 std::vector<compunit_symtab *> result_symtabs;
43182c09 9472 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9473
9474 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9475 if (cust == NULL)
95554aad
TT
9476 return;
9477
280a9412
TT
9478 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9479 htab_eq_pointer,
9480 NULL, xcalloc, xfree));
9481 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9482 htab_eq_pointer,
9483 NULL, xcalloc, xfree));
95554aad 9484
ae640021 9485 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9486 {
280a9412
TT
9487 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9488 all_type_symtabs.get (), ptr,
9489 per_objfile, cust);
ec94af83 9490 }
95554aad 9491
ec94af83 9492 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9493 len = result_symtabs.size ();
43f3e411 9494 cust->includes
f6e649dd 9495 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9496 struct compunit_symtab *, len + 1);
4c39bc03
TT
9497 memcpy (cust->includes, result_symtabs.data (),
9498 len * sizeof (compunit_symtab *));
43f3e411 9499 cust->includes[len] = NULL;
95554aad
TT
9500 }
9501}
9502
9503/* Compute the 'includes' field for the symtabs of all the CUs we just
9504 read. */
9505
9506static void
976ca316 9507process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9508{
976ca316 9509 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9510 {
9511 if (! iter->is_debug_types)
976ca316 9512 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9513 }
95554aad 9514
976ca316 9515 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9516}
9517
8fc0b21d 9518/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9519 already been loaded into memory. */
9520
9521static void
8fc0b21d 9522process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9523{
976ca316
SM
9524 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9525 struct objfile *objfile = per_objfile->objfile;
08feed99 9526 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9527 CORE_ADDR lowpc, highpc;
43f3e411 9528 struct compunit_symtab *cust;
10b3939b 9529 CORE_ADDR baseaddr;
4359dff1 9530 struct block *static_block;
3e29f34a 9531 CORE_ADDR addr;
10b3939b 9532
b3b3bada 9533 baseaddr = objfile->text_section_offset ();
10b3939b 9534
c89b44cd
TT
9535 /* Clear the list here in case something was left over. */
9536 cu->method_list.clear ();
10b3939b 9537
95554aad
TT
9538 cu->language = pretend_language;
9539 cu->language_defn = language_def (cu->language);
9540
1c47ec3e
TV
9541 dwarf2_find_base_address (cu->dies, cu);
9542
702cf3f5
AB
9543 /* Before we start reading the top-level DIE, ensure it has a valid tag
9544 type. */
9545 switch (cu->dies->tag)
9546 {
9547 case DW_TAG_compile_unit:
9548 case DW_TAG_partial_unit:
9549 case DW_TAG_type_unit:
9550 break;
9551 default:
9552 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9553 dwarf_tag_name (cu->dies->tag),
9554 sect_offset_str (cu->per_cu->sect_off),
9555 objfile_name (per_objfile->objfile));
9556 }
9557
c906108c 9558 /* Do line number decoding in read_file_scope () */
10b3939b 9559 process_die (cu->dies, cu);
c906108c 9560
a766d390
DE
9561 /* For now fudge the Go package. */
9562 if (cu->language == language_go)
9563 fixup_go_packaging (cu);
9564
5f48f8f3 9565 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9566 should be complete, and it should now be safe to compute all of the
9567 physnames. */
9568 compute_delayed_physnames (cu);
3da10d80 9569
c9317f21
TT
9570 if (cu->language == language_rust)
9571 rust_union_quirks (cu);
9572
fae299cd
DC
9573 /* Some compilers don't define a DW_AT_high_pc attribute for the
9574 compilation unit. If the DW_AT_high_pc is missing, synthesize
9575 it, by scanning the DIE's below the compilation unit. */
10b3939b 9576 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9577
3e29f34a 9578 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9579 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9580
9581 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9582 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9583 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9584 addrmap to help ensure it has an accurate map of pc values belonging to
9585 this comp unit. */
9586 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9587
c24bdb02 9588 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9589 SECT_OFF_TEXT (objfile),
9590 0);
c906108c 9591
43f3e411 9592 if (cust != NULL)
c906108c 9593 {
df15bd07 9594 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9595
8be455d7
JK
9596 /* Set symtab language to language from DW_AT_language. If the
9597 compilation is from a C file generated by language preprocessors, do
9598 not set the language if it was already deduced by start_subfile. */
43f3e411 9599 if (!(cu->language == language_c
40e3ad0e 9600 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9601 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9602
9603 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9604 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9605 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9606 there were bugs in prologue debug info, fixed later in GCC-4.5
9607 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9608
9609 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9610 needed, it would be wrong due to missing DW_AT_producer there.
9611
9612 Still one can confuse GDB by using non-standard GCC compilation
9613 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9614 */
ab260dad 9615 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9616 cust->locations_valid = 1;
e0d00bc7
JK
9617
9618 if (gcc_4_minor >= 5)
43f3e411 9619 cust->epilogue_unwind_valid = 1;
96408a79 9620
43f3e411 9621 cust->call_site_htab = cu->call_site_htab;
c906108c 9622 }
9291a0cd 9623
976ca316 9624 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9625
95554aad 9626 /* Push it for inclusion processing later. */
976ca316 9627 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9628
9629 /* Not needed any more. */
c24bdb02 9630 cu->reset_builder ();
f4dc4d17 9631}
45cfd468 9632
8fc0b21d 9633/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9634 already been loaded into memory. */
9635
9636static void
8fc0b21d 9637process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9638 enum language pretend_language)
9639{
976ca316
SM
9640 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9641 struct objfile *objfile = per_objfile->objfile;
43f3e411 9642 struct compunit_symtab *cust;
0186c6a7
DE
9643 struct signatured_type *sig_type;
9644
8fc0b21d
SM
9645 gdb_assert (cu->per_cu->is_debug_types);
9646 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9647
c89b44cd
TT
9648 /* Clear the list here in case something was left over. */
9649 cu->method_list.clear ();
f4dc4d17 9650
f4dc4d17
DE
9651 cu->language = pretend_language;
9652 cu->language_defn = language_def (cu->language);
9653
9654 /* The symbol tables are set up in read_type_unit_scope. */
9655 process_die (cu->dies, cu);
9656
9657 /* For now fudge the Go package. */
9658 if (cu->language == language_go)
9659 fixup_go_packaging (cu);
9660
5f48f8f3 9661 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9662 should be complete, and it should now be safe to compute all of the
9663 physnames. */
9664 compute_delayed_physnames (cu);
f4dc4d17 9665
c9317f21
TT
9666 if (cu->language == language_rust)
9667 rust_union_quirks (cu);
9668
f4dc4d17
DE
9669 /* TUs share symbol tables.
9670 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9671 of it with end_expandable_symtab. Otherwise, complete the addition of
9672 this TU's symbols to the existing symtab. */
8adb8487 9673 type_unit_group_unshareable *tug_unshare =
976ca316 9674 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9675 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9676 {
c24bdb02
KS
9677 buildsym_compunit *builder = cu->get_builder ();
9678 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9679 tug_unshare->compunit_symtab = cust;
f4dc4d17 9680
43f3e411 9681 if (cust != NULL)
f4dc4d17
DE
9682 {
9683 /* Set symtab language to language from DW_AT_language. If the
9684 compilation is from a C file generated by language preprocessors,
9685 do not set the language if it was already deduced by
9686 start_subfile. */
43f3e411
DE
9687 if (!(cu->language == language_c
9688 && COMPUNIT_FILETABS (cust)->language != language_c))
9689 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9690 }
9691 }
9692 else
9693 {
c24bdb02 9694 cu->get_builder ()->augment_type_symtab ();
8adb8487 9695 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9696 }
9697
976ca316 9698 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9699
9700 /* Not needed any more. */
c24bdb02 9701 cu->reset_builder ();
c906108c
SS
9702}
9703
95554aad
TT
9704/* Process an imported unit DIE. */
9705
9706static void
9707process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9708{
9709 struct attribute *attr;
9710
f4dc4d17
DE
9711 /* For now we don't handle imported units in type units. */
9712 if (cu->per_cu->is_debug_types)
9713 {
9714 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9715 " supported in type units [in module %s]"),
5e22e966 9716 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9717 }
9718
95554aad
TT
9719 attr = dwarf2_attr (die, DW_AT_import, cu);
9720 if (attr != NULL)
9721 {
0826b30a 9722 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9723 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9724 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9725 dwarf2_per_cu_data *per_cu
ab432490 9726 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9727
58990295
TV
9728 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9729 into another compilation unit, at root level. Regard this as a hint,
9730 and ignore it. */
9731 if (die->parent && die->parent->parent == NULL
9732 && per_cu->unit_type == DW_UT_compile
9733 && per_cu->lang == language_cplus)
9734 return;
9735
69d751e3 9736 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 9737 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
9738 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9739 false, cu->language);
95554aad 9740
ae640021 9741 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9742 }
9743}
9744
4c8aa72d
PA
9745/* RAII object that represents a process_die scope: i.e.,
9746 starts/finishes processing a DIE. */
9747class process_die_scope
adde2bff 9748{
4c8aa72d
PA
9749public:
9750 process_die_scope (die_info *die, dwarf2_cu *cu)
9751 : m_die (die), m_cu (cu)
9752 {
9753 /* We should only be processing DIEs not already in process. */
9754 gdb_assert (!m_die->in_process);
9755 m_die->in_process = true;
9756 }
8c3cb9fa 9757
4c8aa72d
PA
9758 ~process_die_scope ()
9759 {
9760 m_die->in_process = false;
9761
9762 /* If we're done processing the DIE for the CU that owns the line
9763 header, we don't need the line header anymore. */
9764 if (m_cu->line_header_die_owner == m_die)
9765 {
9766 delete m_cu->line_header;
9767 m_cu->line_header = NULL;
9768 m_cu->line_header_die_owner = NULL;
9769 }
9770 }
9771
9772private:
9773 die_info *m_die;
9774 dwarf2_cu *m_cu;
9775};
adde2bff 9776
c906108c
SS
9777/* Process a die and its children. */
9778
9779static void
e7c27a73 9780process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9781{
4c8aa72d 9782 process_die_scope scope (die, cu);
adde2bff 9783
c906108c
SS
9784 switch (die->tag)
9785 {
9786 case DW_TAG_padding:
9787 break;
9788 case DW_TAG_compile_unit:
95554aad 9789 case DW_TAG_partial_unit:
e7c27a73 9790 read_file_scope (die, cu);
c906108c 9791 break;
348e048f
DE
9792 case DW_TAG_type_unit:
9793 read_type_unit_scope (die, cu);
9794 break;
c906108c 9795 case DW_TAG_subprogram:
0a4b0913
AB
9796 /* Nested subprograms in Fortran get a prefix. */
9797 if (cu->language == language_fortran
9798 && die->parent != NULL
9799 && die->parent->tag == DW_TAG_subprogram)
9800 cu->processing_has_namespace_info = true;
9801 /* Fall through. */
c906108c 9802 case DW_TAG_inlined_subroutine:
edb3359d 9803 read_func_scope (die, cu);
c906108c
SS
9804 break;
9805 case DW_TAG_lexical_block:
14898363
L
9806 case DW_TAG_try_block:
9807 case DW_TAG_catch_block:
e7c27a73 9808 read_lexical_block_scope (die, cu);
c906108c 9809 break;
216f72a1 9810 case DW_TAG_call_site:
96408a79
SA
9811 case DW_TAG_GNU_call_site:
9812 read_call_site_scope (die, cu);
9813 break;
c906108c 9814 case DW_TAG_class_type:
680b30c7 9815 case DW_TAG_interface_type:
c906108c
SS
9816 case DW_TAG_structure_type:
9817 case DW_TAG_union_type:
134d01f1 9818 process_structure_scope (die, cu);
c906108c
SS
9819 break;
9820 case DW_TAG_enumeration_type:
134d01f1 9821 process_enumeration_scope (die, cu);
c906108c 9822 break;
134d01f1 9823
f792889a
DJ
9824 /* These dies have a type, but processing them does not create
9825 a symbol or recurse to process the children. Therefore we can
9826 read them on-demand through read_type_die. */
c906108c 9827 case DW_TAG_subroutine_type:
72019c9c 9828 case DW_TAG_set_type:
c906108c 9829 case DW_TAG_pointer_type:
c906108c 9830 case DW_TAG_ptr_to_member_type:
c906108c 9831 case DW_TAG_reference_type:
4297a3f0 9832 case DW_TAG_rvalue_reference_type:
c906108c 9833 case DW_TAG_string_type:
c906108c 9834 break;
134d01f1 9835
d8f62e84
TT
9836 case DW_TAG_array_type:
9837 /* We only need to handle this case for Ada -- in other
9838 languages, it's normal for the compiler to emit a typedef
9839 instead. */
9840 if (cu->language != language_ada)
9841 break;
9842 /* FALLTHROUGH */
c906108c 9843 case DW_TAG_base_type:
a02abb62 9844 case DW_TAG_subrange_type:
cb249c71 9845 case DW_TAG_typedef:
134d01f1 9846 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 9847 DW_AT_name. */
f792889a 9848 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9849 break;
c906108c 9850 case DW_TAG_common_block:
e7c27a73 9851 read_common_block (die, cu);
c906108c
SS
9852 break;
9853 case DW_TAG_common_inclusion:
9854 break;
d9fa45fe 9855 case DW_TAG_namespace:
9068261f 9856 cu->processing_has_namespace_info = true;
e7c27a73 9857 read_namespace (die, cu);
d9fa45fe 9858 break;
5d7cb8df 9859 case DW_TAG_module:
9068261f 9860 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9861 read_module (die, cu);
9862 break;
d9fa45fe 9863 case DW_TAG_imported_declaration:
9068261f 9864 cu->processing_has_namespace_info = true;
74921315
KS
9865 if (read_namespace_alias (die, cu))
9866 break;
86a73007
TT
9867 /* The declaration is not a global namespace alias. */
9868 /* Fall through. */
d9fa45fe 9869 case DW_TAG_imported_module:
9068261f 9870 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9871 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9872 || cu->language != language_fortran))
b98664d3 9873 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9874 dwarf_tag_name (die->tag));
9875 read_import_statement (die, cu);
d9fa45fe 9876 break;
95554aad
TT
9877
9878 case DW_TAG_imported_unit:
9879 process_imported_unit_die (die, cu);
9880 break;
9881
71a3c369
TT
9882 case DW_TAG_variable:
9883 read_variable (die, cu);
9884 break;
9885
c906108c 9886 default:
e7c27a73 9887 new_symbol (die, NULL, cu);
c906108c
SS
9888 break;
9889 }
9890}
ca69b9e6
DE
9891\f
9892/* DWARF name computation. */
c906108c 9893
94af9270
KS
9894/* A helper function for dwarf2_compute_name which determines whether DIE
9895 needs to have the name of the scope prepended to the name listed in the
9896 die. */
9897
9898static int
9899die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9900{
1c809c68
TT
9901 struct attribute *attr;
9902
94af9270
KS
9903 switch (die->tag)
9904 {
9905 case DW_TAG_namespace:
9906 case DW_TAG_typedef:
9907 case DW_TAG_class_type:
9908 case DW_TAG_interface_type:
9909 case DW_TAG_structure_type:
9910 case DW_TAG_union_type:
9911 case DW_TAG_enumeration_type:
9912 case DW_TAG_enumerator:
9913 case DW_TAG_subprogram:
08a76f8a 9914 case DW_TAG_inlined_subroutine:
94af9270 9915 case DW_TAG_member:
74921315 9916 case DW_TAG_imported_declaration:
94af9270
KS
9917 return 1;
9918
9919 case DW_TAG_variable:
c2b0a229 9920 case DW_TAG_constant:
94af9270
KS
9921 /* We only need to prefix "globally" visible variables. These include
9922 any variable marked with DW_AT_external or any variable that
9923 lives in a namespace. [Variables in anonymous namespaces
9924 require prefixing, but they are not DW_AT_external.] */
9925
9926 if (dwarf2_attr (die, DW_AT_specification, cu))
9927 {
9928 struct dwarf2_cu *spec_cu = cu;
9a619af0 9929
94af9270
KS
9930 return die_needs_namespace (die_specification (die, &spec_cu),
9931 spec_cu);
9932 }
9933
1c809c68 9934 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9935 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9936 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9937 return 0;
9938 /* A variable in a lexical block of some kind does not need a
9939 namespace, even though in C++ such variables may be external
9940 and have a mangled name. */
9941 if (die->parent->tag == DW_TAG_lexical_block
9942 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9943 || die->parent->tag == DW_TAG_catch_block
9944 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9945 return 0;
9946 return 1;
94af9270
KS
9947
9948 default:
9949 return 0;
9950 }
9951}
9952
73b9be8b
KS
9953/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9954 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9955 defined for the given DIE. */
9956
9957static struct attribute *
9958dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9959{
9960 struct attribute *attr;
9961
9962 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9963 if (attr == NULL)
9964 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9965
9966 return attr;
9967}
9968
9969/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9970 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9971 defined for the given DIE. */
9972
9973static const char *
9974dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9975{
9976 const char *linkage_name;
9977
9978 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9979 if (linkage_name == NULL)
9980 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9981
787de330
TT
9982 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9983 See https://github.com/rust-lang/rust/issues/32925. */
9984 if (cu->language == language_rust && linkage_name != NULL
9985 && strchr (linkage_name, '{') != NULL)
9986 linkage_name = NULL;
9987
73b9be8b
KS
9988 return linkage_name;
9989}
9990
94af9270 9991/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9992 compute the physname for the object, which include a method's:
9c37b5ae 9993 - formal parameters (C++),
a766d390 9994 - receiver type (Go),
a766d390
DE
9995
9996 The term "physname" is a bit confusing.
9997 For C++, for example, it is the demangled name.
9998 For Go, for example, it's the mangled name.
94af9270 9999
af6b7be1
JB
10000 For Ada, return the DIE's linkage name rather than the fully qualified
10001 name. PHYSNAME is ignored..
10002
5989a64e 10003 The result is allocated on the objfile->per_bfd's obstack and
45940949 10004 canonicalized. */
94af9270
KS
10005
10006static const char *
15d034d0
TT
10007dwarf2_compute_name (const char *name,
10008 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10009 int physname)
10010{
5e22e966 10011 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10012
94af9270
KS
10013 if (name == NULL)
10014 name = dwarf2_name (die, cu);
10015
2ee7123e
DE
10016 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10017 but otherwise compute it by typename_concat inside GDB.
10018 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10019 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10020 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10021 will set the demangled name to the result of dwarf2_full_name, and it is
10022 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10023 if (cu->language == language_ada
10024 || (cu->language == language_fortran && physname))
10025 {
10026 /* For Ada unit, we prefer the linkage name over the name, as
10027 the former contains the exported name, which the user expects
10028 to be able to reference. Ideally, we want the user to be able
10029 to reference this entity using either natural or linkage name,
10030 but we haven't started looking at this enhancement yet. */
73b9be8b 10031 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10032
2ee7123e
DE
10033 if (linkage_name != NULL)
10034 return linkage_name;
f55ee35c
JK
10035 }
10036
94af9270
KS
10037 /* These are the only languages we know how to qualify names in. */
10038 if (name != NULL
9c37b5ae 10039 && (cu->language == language_cplus
c44af4eb
TT
10040 || cu->language == language_fortran || cu->language == language_d
10041 || cu->language == language_rust))
94af9270
KS
10042 {
10043 if (die_needs_namespace (die, cu))
10044 {
0d5cff50 10045 const char *prefix;
34a68019 10046 const char *canonical_name = NULL;
94af9270 10047
d7e74731
PA
10048 string_file buf;
10049
94af9270 10050 prefix = determine_prefix (die, cu);
94af9270
KS
10051 if (*prefix != '\0')
10052 {
43816ebc
TT
10053 gdb::unique_xmalloc_ptr<char> prefixed_name
10054 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10055
43816ebc 10056 buf.puts (prefixed_name.get ());
94af9270
KS
10057 }
10058 else
d7e74731 10059 buf.puts (name);
94af9270 10060
98bfdba5
PA
10061 /* Template parameters may be specified in the DIE's DW_AT_name, or
10062 as children with DW_TAG_template_type_param or
10063 DW_TAG_value_type_param. If the latter, add them to the name
10064 here. If the name already has template parameters, then
10065 skip this step; some versions of GCC emit both, and
10066 it is more efficient to use the pre-computed name.
10067
10068 Something to keep in mind about this process: it is very
10069 unlikely, or in some cases downright impossible, to produce
10070 something that will match the mangled name of a function.
10071 If the definition of the function has the same debug info,
10072 we should be able to match up with it anyway. But fallbacks
10073 using the minimal symbol, for instance to find a method
10074 implemented in a stripped copy of libstdc++, will not work.
10075 If we do not have debug info for the definition, we will have to
10076 match them up some other way.
10077
10078 When we do name matching there is a related problem with function
10079 templates; two instantiated function templates are allowed to
10080 differ only by their return types, which we do not add here. */
10081
10082 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10083 {
10084 struct attribute *attr;
10085 struct die_info *child;
10086 int first = 1;
2c75ccb2 10087 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
10088
10089 die->building_fullname = 1;
10090
10091 for (child = die->child; child != NULL; child = child->sibling)
10092 {
10093 struct type *type;
12df843f 10094 LONGEST value;
d521ce57 10095 const gdb_byte *bytes;
98bfdba5
PA
10096 struct dwarf2_locexpr_baton *baton;
10097 struct value *v;
10098
10099 if (child->tag != DW_TAG_template_type_param
10100 && child->tag != DW_TAG_template_value_param)
10101 continue;
10102
10103 if (first)
10104 {
d7e74731 10105 buf.puts ("<");
98bfdba5
PA
10106 first = 0;
10107 }
10108 else
d7e74731 10109 buf.puts (", ");
98bfdba5
PA
10110
10111 attr = dwarf2_attr (child, DW_AT_type, cu);
10112 if (attr == NULL)
10113 {
b98664d3 10114 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10115 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10116 continue;
10117 }
10118 type = die_type (child, cu);
10119
10120 if (child->tag == DW_TAG_template_type_param)
10121 {
2c75ccb2
AB
10122 cplus_lang->print_type (type, "", &buf, -1, 0,
10123 &type_print_raw_options);
98bfdba5
PA
10124 continue;
10125 }
10126
10127 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10128 if (attr == NULL)
10129 {
b98664d3 10130 complaint (_("template parameter missing "
3e43a32a 10131 "DW_AT_const_value"));
d7e74731 10132 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10133 continue;
10134 }
10135
10136 dwarf2_const_value_attr (attr, type, name,
10137 &cu->comp_unit_obstack, cu,
10138 &value, &bytes, &baton);
10139
20ce4123 10140 if (type->has_no_signedness ())
98bfdba5
PA
10141 /* GDB prints characters as NUMBER 'CHAR'. If that's
10142 changed, this can use value_print instead. */
2c75ccb2 10143 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
10144 else
10145 {
10146 struct value_print_options opts;
10147
10148 if (baton != NULL)
10149 v = dwarf2_evaluate_loc_desc (type, NULL,
10150 baton->data,
10151 baton->size,
9f47c707
SM
10152 baton->per_cu,
10153 baton->per_objfile);
98bfdba5
PA
10154 else if (bytes != NULL)
10155 {
10156 v = allocate_value (type);
10157 memcpy (value_contents_writeable (v), bytes,
10158 TYPE_LENGTH (type));
10159 }
10160 else
10161 v = value_from_longest (type, value);
10162
3e43a32a
MS
10163 /* Specify decimal so that we do not depend on
10164 the radix. */
98bfdba5
PA
10165 get_formatted_print_options (&opts, 'd');
10166 opts.raw = 1;
d7e74731 10167 value_print (v, &buf, &opts);
98bfdba5 10168 release_value (v);
98bfdba5
PA
10169 }
10170 }
10171
10172 die->building_fullname = 0;
10173
10174 if (!first)
10175 {
10176 /* Close the argument list, with a space if necessary
10177 (nested templates). */
d7e74731
PA
10178 if (!buf.empty () && buf.string ().back () == '>')
10179 buf.puts (" >");
98bfdba5 10180 else
d7e74731 10181 buf.puts (">");
98bfdba5
PA
10182 }
10183 }
10184
9c37b5ae 10185 /* For C++ methods, append formal parameter type
94af9270 10186 information, if PHYSNAME. */
6e70227d 10187
94af9270 10188 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10189 && cu->language == language_cplus)
94af9270
KS
10190 {
10191 struct type *type = read_type_die (die, cu);
10192
d7e74731 10193 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10194 &type_print_raw_options);
94af9270 10195
9c37b5ae 10196 if (cu->language == language_cplus)
94af9270 10197 {
60430eff
DJ
10198 /* Assume that an artificial first parameter is
10199 "this", but do not crash if it is not. RealView
10200 marks unnamed (and thus unused) parameters as
10201 artificial; there is no way to differentiate
10202 the two cases. */
1f704f76 10203 if (type->num_fields () > 0
94af9270 10204 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10205 && type->field (0).type ()->code () == TYPE_CODE_PTR
10206 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10207 buf.puts (" const");
94af9270
KS
10208 }
10209 }
10210
d7e74731 10211 const std::string &intermediate_name = buf.string ();
94af9270
KS
10212
10213 if (cu->language == language_cplus)
34a68019 10214 canonical_name
322a8516 10215 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10216 objfile);
34a68019
TT
10217
10218 /* If we only computed INTERMEDIATE_NAME, or if
10219 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10220 intern it. */
322a8516 10221 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10222 name = objfile->intern (intermediate_name);
34a68019
TT
10223 else
10224 name = canonical_name;
94af9270
KS
10225 }
10226 }
10227
10228 return name;
10229}
10230
0114d602
DJ
10231/* Return the fully qualified name of DIE, based on its DW_AT_name.
10232 If scope qualifiers are appropriate they will be added. The result
34a68019 10233 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10234 not have a name. NAME may either be from a previous call to
10235 dwarf2_name or NULL.
10236
9c37b5ae 10237 The output string will be canonicalized (if C++). */
0114d602
DJ
10238
10239static const char *
15d034d0 10240dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10241{
94af9270
KS
10242 return dwarf2_compute_name (name, die, cu, 0);
10243}
0114d602 10244
94af9270
KS
10245/* Construct a physname for the given DIE in CU. NAME may either be
10246 from a previous call to dwarf2_name or NULL. The result will be
10247 allocated on the objfile_objstack or NULL if the DIE does not have a
10248 name.
0114d602 10249
9c37b5ae 10250 The output string will be canonicalized (if C++). */
0114d602 10251
94af9270 10252static const char *
15d034d0 10253dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10254{
5e22e966 10255 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10256 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10257 int need_copy = 1;
10258
10259 /* In this case dwarf2_compute_name is just a shortcut not building anything
10260 on its own. */
10261 if (!die_needs_namespace (die, cu))
10262 return dwarf2_compute_name (name, die, cu, 1);
10263
906bb4c5
TT
10264 if (cu->language != language_rust)
10265 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10266
10267 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10268 has computed. */
791afaa2 10269 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10270 if (mangled != NULL)
900e11f9 10271 {
900e11f9 10272
d3355e4d 10273 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10274 {
10275 /* Do nothing (do not demangle the symbol name). */
10276 }
a766d390
DE
10277 else
10278 {
0eb876f5
JB
10279 /* Use DMGL_RET_DROP for C++ template functions to suppress
10280 their return type. It is easier for GDB users to search
10281 for such functions as `name(params)' than `long name(params)'.
10282 In such case the minimal symbol names do not match the full
10283 symbol names but for template functions there is never a need
10284 to look up their definition from their declaration so
10285 the only disadvantage remains the minimal symbol variant
10286 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10287 demangled.reset (gdb_demangle (mangled,
10288 (DMGL_PARAMS | DMGL_ANSI
10289 | DMGL_RET_DROP)));
a766d390 10290 }
900e11f9 10291 if (demangled)
791afaa2 10292 canon = demangled.get ();
900e11f9
JK
10293 else
10294 {
10295 canon = mangled;
10296 need_copy = 0;
10297 }
10298 }
10299
10300 if (canon == NULL || check_physname)
10301 {
10302 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10303
10304 if (canon != NULL && strcmp (physname, canon) != 0)
10305 {
10306 /* It may not mean a bug in GDB. The compiler could also
10307 compute DW_AT_linkage_name incorrectly. But in such case
10308 GDB would need to be bug-to-bug compatible. */
10309
b98664d3 10310 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10311 "(from linkage <%s>) - DIE at %s [in module %s]"),
10312 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10313 objfile_name (objfile));
900e11f9
JK
10314
10315 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10316 is available here - over computed PHYSNAME. It is safer
10317 against both buggy GDB and buggy compilers. */
10318
10319 retval = canon;
10320 }
10321 else
10322 {
10323 retval = physname;
10324 need_copy = 0;
10325 }
10326 }
10327 else
10328 retval = canon;
10329
10330 if (need_copy)
be1e3d3e 10331 retval = objfile->intern (retval);
900e11f9 10332
900e11f9 10333 return retval;
0114d602
DJ
10334}
10335
74921315
KS
10336/* Inspect DIE in CU for a namespace alias. If one exists, record
10337 a new symbol for it.
10338
10339 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10340
10341static int
10342read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10343{
10344 struct attribute *attr;
10345
10346 /* If the die does not have a name, this is not a namespace
10347 alias. */
10348 attr = dwarf2_attr (die, DW_AT_name, cu);
10349 if (attr != NULL)
10350 {
10351 int num;
10352 struct die_info *d = die;
10353 struct dwarf2_cu *imported_cu = cu;
10354
10355 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10356 keep inspecting DIEs until we hit the underlying import. */
10357#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10358 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10359 {
10360 attr = dwarf2_attr (d, DW_AT_import, cu);
10361 if (attr == NULL)
10362 break;
10363
10364 d = follow_die_ref (d, attr, &imported_cu);
10365 if (d->tag != DW_TAG_imported_declaration)
10366 break;
10367 }
10368
10369 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10370 {
b98664d3 10371 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10372 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10373 return 0;
10374 }
10375
10376 if (attr != NULL)
10377 {
10378 struct type *type;
0826b30a 10379 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10380
aa66c379 10381 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10382 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10383 {
10384 /* This declaration is a global namespace alias. Add
10385 a symbol for it whose type is the aliased namespace. */
10386 new_symbol (die, type, cu);
10387 return 1;
10388 }
10389 }
10390 }
10391
10392 return 0;
10393}
10394
22cee43f 10395/* Return the using directives repository (global or local?) to use in the
804d2729 10396 current context for CU.
22cee43f
PMR
10397
10398 For Ada, imported declarations can materialize renamings, which *may* be
10399 global. However it is impossible (for now?) in DWARF to distinguish
10400 "external" imported declarations and "static" ones. As all imported
10401 declarations seem to be static in all other languages, make them all CU-wide
10402 global only in Ada. */
10403
10404static struct using_direct **
804d2729 10405using_directives (struct dwarf2_cu *cu)
22cee43f 10406{
c24bdb02
KS
10407 if (cu->language == language_ada
10408 && cu->get_builder ()->outermost_context_p ())
10409 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10410 else
c24bdb02 10411 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10412}
10413
27aa8d6a
SW
10414/* Read the import statement specified by the given die and record it. */
10415
10416static void
10417read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10418{
5e22e966 10419 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10420 struct attribute *import_attr;
32019081 10421 struct die_info *imported_die, *child_die;
de4affc9 10422 struct dwarf2_cu *imported_cu;
27aa8d6a 10423 const char *imported_name;
794684b6 10424 const char *imported_name_prefix;
13387711
SW
10425 const char *canonical_name;
10426 const char *import_alias;
10427 const char *imported_declaration = NULL;
794684b6 10428 const char *import_prefix;
eb1e02fd 10429 std::vector<const char *> excludes;
13387711 10430
27aa8d6a
SW
10431 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10432 if (import_attr == NULL)
10433 {
b98664d3 10434 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10435 dwarf_tag_name (die->tag));
10436 return;
10437 }
10438
de4affc9
CC
10439 imported_cu = cu;
10440 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10441 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10442 if (imported_name == NULL)
10443 {
10444 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10445
dda83cd7
SM
10446 The import in the following code:
10447 namespace A
10448 {
10449 typedef int B;
10450 }
10451
10452 int main ()
10453 {
10454 using A::B;
10455 B b;
10456 return b;
10457 }
10458
10459 ...
10460 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10461 <52> DW_AT_decl_file : 1
10462 <53> DW_AT_decl_line : 6
10463 <54> DW_AT_import : <0x75>
10464 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10465 <59> DW_AT_name : B
10466 <5b> DW_AT_decl_file : 1
10467 <5c> DW_AT_decl_line : 2
10468 <5d> DW_AT_type : <0x6e>
10469 ...
10470 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10471 <76> DW_AT_byte_size : 4
10472 <77> DW_AT_encoding : 5 (signed)
10473
10474 imports the wrong die ( 0x75 instead of 0x58 ).
10475 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10476 return;
10477 }
10478
82856980
SW
10479 /* Figure out the local name after import. */
10480 import_alias = dwarf2_name (die, cu);
27aa8d6a 10481
794684b6
SW
10482 /* Figure out where the statement is being imported to. */
10483 import_prefix = determine_prefix (die, cu);
10484
10485 /* Figure out what the scope of the imported die is and prepend it
10486 to the name of the imported die. */
de4affc9 10487 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10488
f55ee35c
JK
10489 if (imported_die->tag != DW_TAG_namespace
10490 && imported_die->tag != DW_TAG_module)
794684b6 10491 {
13387711
SW
10492 imported_declaration = imported_name;
10493 canonical_name = imported_name_prefix;
794684b6 10494 }
13387711 10495 else if (strlen (imported_name_prefix) > 0)
12aaed36 10496 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10497 imported_name_prefix,
10498 (cu->language == language_d ? "." : "::"),
10499 imported_name, (char *) NULL);
13387711
SW
10500 else
10501 canonical_name = imported_name;
794684b6 10502
32019081
JK
10503 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10504 for (child_die = die->child; child_die && child_die->tag;
436c571c 10505 child_die = child_die->sibling)
32019081
JK
10506 {
10507 /* DWARF-4: A Fortran use statement with a “rename list” may be
10508 represented by an imported module entry with an import attribute
10509 referring to the module and owned entries corresponding to those
10510 entities that are renamed as part of being imported. */
10511
10512 if (child_die->tag != DW_TAG_imported_declaration)
10513 {
b98664d3 10514 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10515 "- DIE at %s [in module %s]"),
10516 sect_offset_str (child_die->sect_off),
10517 objfile_name (objfile));
32019081
JK
10518 continue;
10519 }
10520
10521 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10522 if (import_attr == NULL)
10523 {
b98664d3 10524 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10525 dwarf_tag_name (child_die->tag));
10526 continue;
10527 }
10528
10529 imported_cu = cu;
10530 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10531 &imported_cu);
10532 imported_name = dwarf2_name (imported_die, imported_cu);
10533 if (imported_name == NULL)
10534 {
b98664d3 10535 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10536 "imported name - DIE at %s [in module %s]"),
10537 sect_offset_str (child_die->sect_off),
10538 objfile_name (objfile));
32019081
JK
10539 continue;
10540 }
10541
eb1e02fd 10542 excludes.push_back (imported_name);
32019081
JK
10543
10544 process_die (child_die, cu);
10545 }
10546
804d2729 10547 add_using_directive (using_directives (cu),
22cee43f
PMR
10548 import_prefix,
10549 canonical_name,
10550 import_alias,
10551 imported_declaration,
10552 excludes,
10553 0,
10554 &objfile->objfile_obstack);
27aa8d6a
SW
10555}
10556
5230b05a
WT
10557/* ICC<14 does not output the required DW_AT_declaration on incomplete
10558 types, but gives them a size of zero. Starting with version 14,
10559 ICC is compatible with GCC. */
10560
9068261f 10561static bool
5230b05a
WT
10562producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10563{
10564 if (!cu->checked_producer)
10565 check_producer (cu);
10566
10567 return cu->producer_is_icc_lt_14;
10568}
10569
eb77c9df
AB
10570/* ICC generates a DW_AT_type for C void functions. This was observed on
10571 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10572 which says that void functions should not have a DW_AT_type. */
10573
10574static bool
10575producer_is_icc (struct dwarf2_cu *cu)
10576{
10577 if (!cu->checked_producer)
10578 check_producer (cu);
10579
10580 return cu->producer_is_icc;
10581}
10582
1b80a9fa
JK
10583/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10584 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10585 this, it was first present in GCC release 4.3.0. */
10586
9068261f 10587static bool
1b80a9fa
JK
10588producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10589{
10590 if (!cu->checked_producer)
10591 check_producer (cu);
10592
10593 return cu->producer_is_gcc_lt_4_3;
10594}
10595
d721ba37
PA
10596static file_and_directory
10597find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10598{
d721ba37
PA
10599 file_and_directory res;
10600
9291a0cd
TT
10601 /* Find the filename. Do not use dwarf2_name here, since the filename
10602 is not a source language identifier. */
d721ba37
PA
10603 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10604 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10605
d721ba37
PA
10606 if (res.comp_dir == NULL
10607 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10608 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10609 {
d721ba37
PA
10610 res.comp_dir_storage = ldirname (res.name);
10611 if (!res.comp_dir_storage.empty ())
10612 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10613 }
d721ba37 10614 if (res.comp_dir != NULL)
9291a0cd
TT
10615 {
10616 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10617 directory, get rid of it. */
d721ba37 10618 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10619
d721ba37
PA
10620 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10621 res.comp_dir = cp + 1;
9291a0cd
TT
10622 }
10623
d721ba37
PA
10624 if (res.name == NULL)
10625 res.name = "<unknown>";
10626
10627 return res;
9291a0cd
TT
10628}
10629
f4dc4d17
DE
10630/* Handle DW_AT_stmt_list for a compilation unit.
10631 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10632 COMP_DIR is the compilation directory. LOWPC is passed to
10633 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10634
10635static void
10636handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10637 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10638{
976ca316 10639 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10640 struct attribute *attr;
527f3840
JK
10641 struct line_header line_header_local;
10642 hashval_t line_header_local_hash;
527f3840
JK
10643 void **slot;
10644 int decode_mapping;
2ab95328 10645
f4dc4d17
DE
10646 gdb_assert (! cu->per_cu->is_debug_types);
10647
2ab95328 10648 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 10649 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
10650 return;
10651
d4df075e 10652 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
10653
10654 /* The line header hash table is only created if needed (it exists to
10655 prevent redundant reading of the line table for partial_units).
10656 If we're given a partial_unit, we'll need it. If we're given a
10657 compile_unit, then use the line header hash table if it's already
10658 created, but don't create one just yet. */
10659
976ca316 10660 if (per_objfile->line_header_hash == NULL
527f3840 10661 && die->tag == DW_TAG_partial_unit)
2ab95328 10662 {
976ca316 10663 per_objfile->line_header_hash
d15acc42
TT
10664 .reset (htab_create_alloc (127, line_header_hash_voidp,
10665 line_header_eq_voidp,
10666 free_line_header_voidp,
10667 xcalloc, xfree));
527f3840 10668 }
2ab95328 10669
9c541725 10670 line_header_local.sect_off = line_offset;
527f3840
JK
10671 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10672 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10673 if (per_objfile->line_header_hash != NULL)
527f3840 10674 {
976ca316 10675 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10676 &line_header_local,
10677 line_header_local_hash, NO_INSERT);
10678
10679 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10680 is not present in *SLOT (since if there is something in *SLOT then
10681 it will be for a partial_unit). */
10682 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10683 {
527f3840 10684 gdb_assert (*slot != NULL);
9a3c8263 10685 cu->line_header = (struct line_header *) *slot;
527f3840 10686 return;
dee91e82 10687 }
2ab95328 10688 }
527f3840
JK
10689
10690 /* dwarf_decode_line_header does not yet provide sufficient information.
10691 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10692 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10693 if (lh == NULL)
527f3840 10694 return;
4c8aa72d
PA
10695
10696 cu->line_header = lh.release ();
10697 cu->line_header_die_owner = die;
527f3840 10698
976ca316 10699 if (per_objfile->line_header_hash == NULL)
527f3840
JK
10700 slot = NULL;
10701 else
10702 {
976ca316 10703 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10704 &line_header_local,
10705 line_header_local_hash, INSERT);
10706 gdb_assert (slot != NULL);
10707 }
10708 if (slot != NULL && *slot == NULL)
10709 {
10710 /* This newly decoded line number information unit will be owned
10711 by line_header_hash hash table. */
10712 *slot = cu->line_header;
4c8aa72d 10713 cu->line_header_die_owner = NULL;
527f3840
JK
10714 }
10715 else
10716 {
10717 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 10718 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
10719 line_header for this CU - it may happen at most once for each line
10720 number information unit. And if we're not using line_header_hash
10721 then this is what we want as well. */
10722 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10723 }
10724 decode_mapping = (die->tag != DW_TAG_partial_unit);
10725 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10726 decode_mapping);
fff8551c 10727
2ab95328
TT
10728}
10729
95554aad 10730/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10731
c906108c 10732static void
e7c27a73 10733read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10734{
976ca316
SM
10735 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10736 struct objfile *objfile = per_objfile->objfile;
08feed99 10737 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10738 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10739 CORE_ADDR highpc = ((CORE_ADDR) 0);
10740 struct attribute *attr;
c906108c 10741 struct die_info *child_die;
e142c38c 10742 CORE_ADDR baseaddr;
6e70227d 10743
380618d6 10744 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10745 baseaddr = objfile->text_section_offset ();
c906108c 10746
fae299cd 10747 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10748
10749 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10750 from finish_block. */
2acceee2 10751 if (lowpc == ((CORE_ADDR) -1))
c906108c 10752 lowpc = highpc;
3e29f34a 10753 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10754
d721ba37 10755 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10756
f4b8a18d
KW
10757 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10758 standardised yet. As a workaround for the language detection we fall
10759 back to the DW_AT_producer string. */
10760 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10761 cu->language = language_opencl;
10762
3019eac3
DE
10763 /* Similar hack for Go. */
10764 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10765 set_cu_language (DW_LANG_Go, cu);
10766
c24bdb02 10767 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10768
10769 /* Decode line number information if present. We do this before
10770 processing child DIEs, so that the line header table is available
10771 for DW_AT_decl_file. */
d721ba37 10772 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10773
10774 /* Process all dies in compilation unit. */
10775 if (die->child != NULL)
10776 {
10777 child_die = die->child;
10778 while (child_die && child_die->tag)
10779 {
10780 process_die (child_die, cu);
436c571c 10781 child_die = child_die->sibling;
3019eac3
DE
10782 }
10783 }
10784
10785 /* Decode macro information, if present. Dwarf 2 macro information
10786 refers to information in the line number info statement program
10787 header, so we can only read it if we've read the header
10788 successfully. */
0af92d60
JK
10789 attr = dwarf2_attr (die, DW_AT_macros, cu);
10790 if (attr == NULL)
10791 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 10792 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
10793 {
10794 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10795 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10796
529908cb 10797 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
10798 }
10799 else
10800 {
10801 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 10802 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 10803 {
529908cb 10804 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 10805
43f3e411 10806 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10807 }
10808 }
3019eac3
DE
10809}
10810
c24bdb02
KS
10811void
10812dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10813{
f4dc4d17
DE
10814 struct type_unit_group *tu_group;
10815 int first_time;
3019eac3 10816 struct attribute *attr;
9c541725 10817 unsigned int i;
0186c6a7 10818 struct signatured_type *sig_type;
3019eac3 10819
f4dc4d17 10820 gdb_assert (per_cu->is_debug_types);
0186c6a7 10821 sig_type = (struct signatured_type *) per_cu;
3019eac3 10822
c24bdb02 10823 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10824
f4dc4d17 10825 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10826 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10827 if (sig_type->type_unit_group == NULL)
c24bdb02 10828 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10829 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10830
10831 /* If we've already processed this stmt_list there's no real need to
10832 do it again, we could fake it and just recreate the part we need
10833 (file name,index -> symtab mapping). If data shows this optimization
10834 is useful we can do it then. */
8adb8487
TT
10835 type_unit_group_unshareable *tug_unshare
10836 = per_objfile->get_type_unit_group_unshareable (tu_group);
10837 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
10838
10839 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10840 debug info. */
fff8551c 10841 line_header_up lh;
d4df075e 10842 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 10843 {
d4df075e 10844 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 10845 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10846 }
10847 if (lh == NULL)
10848 {
10849 if (first_time)
c24bdb02 10850 start_symtab ("", NULL, 0);
f4dc4d17
DE
10851 else
10852 {
8adb8487 10853 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 10854 gdb_assert (m_builder == nullptr);
8adb8487 10855 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10856 m_builder.reset (new struct buildsym_compunit
10857 (COMPUNIT_OBJFILE (cust), "",
10858 COMPUNIT_DIRNAME (cust),
10859 compunit_language (cust),
10860 0, cust));
770479f2 10861 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10862 }
f4dc4d17 10863 return;
3019eac3
DE
10864 }
10865
c24bdb02
KS
10866 line_header = lh.release ();
10867 line_header_die_owner = die;
3019eac3 10868
f4dc4d17
DE
10869 if (first_time)
10870 {
c24bdb02 10871 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10872
1fd60fc0
DE
10873 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10874 still initializing it, and our caller (a few levels up)
10875 process_full_type_unit still needs to know if this is the first
10876 time. */
10877
8adb8487 10878 tug_unshare->symtabs
4ac93832
TT
10879 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10880 struct symtab *, line_header->file_names_size ());
3019eac3 10881
7ba99d21
AT
10882 auto &file_names = line_header->file_names ();
10883 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10884 {
7ba99d21 10885 file_entry &fe = file_names[i];
c24bdb02
KS
10886 dwarf2_start_subfile (this, fe.name,
10887 fe.include_dir (line_header));
10888 buildsym_compunit *b = get_builder ();
10889 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10890 {
4c8aa72d
PA
10891 /* NOTE: start_subfile will recognize when it's been
10892 passed a file it has already seen. So we can't
10893 assume there's a simple mapping from
10894 cu->line_header->file_names to subfiles, plus
10895 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10896 b->get_current_subfile ()->symtab
10897 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10898 }
10899
c24bdb02 10900 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 10901 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
10902 }
10903 }
10904 else
3019eac3 10905 {
c24bdb02 10906 gdb_assert (m_builder == nullptr);
8adb8487 10907 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10908 m_builder.reset (new struct buildsym_compunit
10909 (COMPUNIT_OBJFILE (cust), "",
10910 COMPUNIT_DIRNAME (cust),
10911 compunit_language (cust),
10912 0, cust));
770479f2 10913 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10914
7ba99d21
AT
10915 auto &file_names = line_header->file_names ();
10916 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10917 {
7ba99d21 10918 file_entry &fe = file_names[i];
8adb8487 10919 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 10920 }
3019eac3
DE
10921 }
10922
f4dc4d17
DE
10923 /* The main symtab is allocated last. Type units don't have DW_AT_name
10924 so they don't have a "real" (so to speak) symtab anyway.
10925 There is later code that will assign the main symtab to all symbols
10926 that don't have one. We need to handle the case of a symbol with a
10927 missing symtab (DW_AT_decl_file) anyway. */
10928}
3019eac3 10929
f4dc4d17
DE
10930/* Process DW_TAG_type_unit.
10931 For TUs we want to skip the first top level sibling if it's not the
10932 actual type being defined by this TU. In this case the first top
10933 level sibling is there to provide context only. */
3019eac3 10934
f4dc4d17
DE
10935static void
10936read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10937{
10938 struct die_info *child_die;
3019eac3 10939
f4dc4d17
DE
10940 prepare_one_comp_unit (cu, die, language_minimal);
10941
10942 /* Initialize (or reinitialize) the machinery for building symtabs.
10943 We do this before processing child DIEs, so that the line header table
10944 is available for DW_AT_decl_file. */
c24bdb02 10945 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10946
10947 if (die->child != NULL)
10948 {
10949 child_die = die->child;
10950 while (child_die && child_die->tag)
10951 {
10952 process_die (child_die, cu);
436c571c 10953 child_die = child_die->sibling;
f4dc4d17
DE
10954 }
10955 }
3019eac3
DE
10956}
10957\f
80626a55
DE
10958/* DWO/DWP files.
10959
10960 http://gcc.gnu.org/wiki/DebugFission
10961 http://gcc.gnu.org/wiki/DebugFissionDWP
10962
10963 To simplify handling of both DWO files ("object" files with the DWARF info)
10964 and DWP files (a file with the DWOs packaged up into one file), we treat
10965 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10966
10967static hashval_t
10968hash_dwo_file (const void *item)
10969{
9a3c8263 10970 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10971 hashval_t hash;
3019eac3 10972
a2ce51a0
DE
10973 hash = htab_hash_string (dwo_file->dwo_name);
10974 if (dwo_file->comp_dir != NULL)
10975 hash += htab_hash_string (dwo_file->comp_dir);
10976 return hash;
3019eac3
DE
10977}
10978
10979static int
10980eq_dwo_file (const void *item_lhs, const void *item_rhs)
10981{
9a3c8263
SM
10982 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10983 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10984
a2ce51a0
DE
10985 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10986 return 0;
10987 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10988 return lhs->comp_dir == rhs->comp_dir;
10989 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10990}
10991
10992/* Allocate a hash table for DWO files. */
10993
51ac9db5 10994static htab_up
298e9637 10995allocate_dwo_file_hash_table ()
3019eac3 10996{
51ac9db5
SM
10997 auto delete_dwo_file = [] (void *item)
10998 {
10999 struct dwo_file *dwo_file = (struct dwo_file *) item;
11000
11001 delete dwo_file;
11002 };
11003
bc68fb19
TT
11004 return htab_up (htab_create_alloc (41,
11005 hash_dwo_file,
11006 eq_dwo_file,
11007 delete_dwo_file,
11008 xcalloc, xfree));
3019eac3
DE
11009}
11010
80626a55
DE
11011/* Lookup DWO file DWO_NAME. */
11012
11013static void **
976ca316 11014lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11015 const char *dwo_name,
11016 const char *comp_dir)
80626a55
DE
11017{
11018 struct dwo_file find_entry;
11019 void **slot;
11020
976ca316
SM
11021 if (per_objfile->per_bfd->dwo_files == NULL)
11022 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11023
0ac5b59e
DE
11024 find_entry.dwo_name = dwo_name;
11025 find_entry.comp_dir = comp_dir;
976ca316 11026 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11027 INSERT);
80626a55
DE
11028
11029 return slot;
11030}
11031
3019eac3
DE
11032static hashval_t
11033hash_dwo_unit (const void *item)
11034{
9a3c8263 11035 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11036
11037 /* This drops the top 32 bits of the id, but is ok for a hash. */
11038 return dwo_unit->signature;
11039}
11040
11041static int
11042eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11043{
9a3c8263
SM
11044 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11045 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11046
11047 /* The signature is assumed to be unique within the DWO file.
11048 So while object file CU dwo_id's always have the value zero,
11049 that's OK, assuming each object file DWO file has only one CU,
11050 and that's the rule for now. */
11051 return lhs->signature == rhs->signature;
11052}
11053
11054/* Allocate a hash table for DWO CUs,TUs.
11055 There is one of these tables for each of CUs,TUs for each DWO file. */
11056
b0b6a987 11057static htab_up
298e9637 11058allocate_dwo_unit_table ()
3019eac3
DE
11059{
11060 /* Start out with a pretty small number.
11061 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11062 return htab_up (htab_create_alloc (3,
11063 hash_dwo_unit,
11064 eq_dwo_unit,
11065 NULL, xcalloc, xfree));
3019eac3
DE
11066}
11067
19c3d4c9 11068/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11069
11070static void
19c3d4c9
DE
11071create_dwo_cu_reader (const struct die_reader_specs *reader,
11072 const gdb_byte *info_ptr,
11073 struct die_info *comp_unit_die,
c0ab21c2
TT
11074 struct dwo_file *dwo_file,
11075 struct dwo_unit *dwo_unit)
3019eac3
DE
11076{
11077 struct dwarf2_cu *cu = reader->cu;
9c541725 11078 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11079 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11080
a084a2a6
AT
11081 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11082 if (!signature.has_value ())
3019eac3 11083 {
b98664d3 11084 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11085 " its dwo_id [in module %s]"),
9d8780f0 11086 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11087 return;
11088 }
11089
3019eac3 11090 dwo_unit->dwo_file = dwo_file;
a084a2a6 11091 dwo_unit->signature = *signature;
8a0459fd 11092 dwo_unit->section = section;
9c541725 11093 dwo_unit->sect_off = sect_off;
3019eac3
DE
11094 dwo_unit->length = cu->per_cu->length;
11095
6f738b01
SM
11096 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11097 sect_offset_str (sect_off),
11098 hex_string (dwo_unit->signature));
3019eac3
DE
11099}
11100
33c5cd75 11101/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11102 Note: This function processes DWO files only, not DWP files. */
3019eac3 11103
33c5cd75 11104static void
976ca316 11105create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11106 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11107 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11108{
976ca316
SM
11109 struct objfile *objfile = per_objfile->objfile;
11110 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11111 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11112
96b79293 11113 section.read (objfile);
33c5cd75 11114 info_ptr = section.buffer;
3019eac3
DE
11115
11116 if (info_ptr == NULL)
33c5cd75 11117 return;
3019eac3 11118
6f738b01
SM
11119 dwarf_read_debug_printf ("Reading %s for %s:",
11120 section.get_name (),
11121 section.get_file_name ());
3019eac3 11122
33c5cd75 11123 end_ptr = info_ptr + section.size;
3019eac3
DE
11124 while (info_ptr < end_ptr)
11125 {
11126 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11127 struct dwo_unit read_unit {};
33c5cd75
DB
11128 struct dwo_unit *dwo_unit;
11129 void **slot;
11130 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11131
1859c670 11132 per_cu.per_bfd = per_bfd;
3019eac3 11133 per_cu.is_debug_types = 0;
33c5cd75
DB
11134 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11135 per_cu.section = &section;
11136
976ca316 11137 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11138 if (!reader.dummy_p)
11139 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11140 &dwo_file, &read_unit);
33c5cd75
DB
11141 info_ptr += per_cu.length;
11142
11143 // If the unit could not be parsed, skip it.
c0ab21c2 11144 if (read_unit.dwo_file == NULL)
33c5cd75 11145 continue;
3019eac3 11146
33c5cd75 11147 if (cus_htab == NULL)
298e9637 11148 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11149
1859c670 11150 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11151 struct dwo_unit);
c0ab21c2 11152 *dwo_unit = read_unit;
b0b6a987 11153 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11154 gdb_assert (slot != NULL);
11155 if (*slot != NULL)
19c3d4c9 11156 {
33c5cd75
DB
11157 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11158 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11159
b98664d3 11160 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11161 " the entry at offset %s, signature %s"),
11162 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11163 hex_string (dwo_unit->signature));
19c3d4c9 11164 }
33c5cd75 11165 *slot = (void *)dwo_unit;
3019eac3 11166 }
3019eac3
DE
11167}
11168
80626a55
DE
11169/* DWP file .debug_{cu,tu}_index section format:
11170 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11171 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11172
11173 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11174 officially standard DWP format was published with DWARF v5 and is called
11175 Version 5. There are no versions 3 or 4.
80626a55 11176
d2415c6c
DE
11177 DWP Version 1:
11178
80626a55
DE
11179 Both index sections have the same format, and serve to map a 64-bit
11180 signature to a set of section numbers. Each section begins with a header,
11181 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11182 indexes, and a pool of 32-bit section numbers. The index sections will be
11183 aligned at 8-byte boundaries in the file.
11184
d2415c6c
DE
11185 The index section header consists of:
11186
11187 V, 32 bit version number
11188 -, 32 bits unused
11189 N, 32 bit number of compilation units or type units in the index
11190 M, 32 bit number of slots in the hash table
80626a55 11191
d2415c6c 11192 Numbers are recorded using the byte order of the application binary.
80626a55 11193
d2415c6c
DE
11194 The hash table begins at offset 16 in the section, and consists of an array
11195 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11196 order of the application binary). Unused slots in the hash table are 0.
11197 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11198
d2415c6c
DE
11199 The parallel table begins immediately after the hash table
11200 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11201 array of 32-bit indexes (using the byte order of the application binary),
11202 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11203 table contains a 32-bit index into the pool of section numbers. For unused
11204 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11205
73869dc2
DE
11206 The pool of section numbers begins immediately following the hash table
11207 (at offset 16 + 12 * M from the beginning of the section). The pool of
11208 section numbers consists of an array of 32-bit words (using the byte order
11209 of the application binary). Each item in the array is indexed starting
11210 from 0. The hash table entry provides the index of the first section
11211 number in the set. Additional section numbers in the set follow, and the
11212 set is terminated by a 0 entry (section number 0 is not used in ELF).
11213
11214 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11215 section must be the first entry in the set, and the .debug_abbrev.dwo must
11216 be the second entry. Other members of the set may follow in any order.
11217
11218 ---
11219
d2854d8d 11220 DWP Versions 2 and 5:
73869dc2 11221
d2854d8d 11222 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11223 and the entries in the index tables are now offsets into these sections.
11224 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11225 section.
11226
11227 Index Section Contents:
11228 Header
11229 Hash Table of Signatures dwp_hash_table.hash_table
11230 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11231 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11232 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11233
11234 The index section header consists of:
11235
11236 V, 32 bit version number
11237 L, 32 bit number of columns in the table of section offsets
11238 N, 32 bit number of compilation units or type units in the index
11239 M, 32 bit number of slots in the hash table
11240
11241 Numbers are recorded using the byte order of the application binary.
11242
11243 The hash table has the same format as version 1.
11244 The parallel table of indices has the same format as version 1,
11245 except that the entries are origin-1 indices into the table of sections
11246 offsets and the table of section sizes.
11247
11248 The table of offsets begins immediately following the parallel table
11249 (at offset 16 + 12 * M from the beginning of the section). The table is
11250 a two-dimensional array of 32-bit words (using the byte order of the
11251 application binary), with L columns and N+1 rows, in row-major order.
11252 Each row in the array is indexed starting from 0. The first row provides
11253 a key to the remaining rows: each column in this row provides an identifier
11254 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11255 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11256
11257 DW_SECT_INFO 1 .debug_info.dwo
11258 DW_SECT_TYPES 2 .debug_types.dwo
11259 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11260 DW_SECT_LINE 4 .debug_line.dwo
11261 DW_SECT_LOC 5 .debug_loc.dwo
11262 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11263 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11264 DW_SECT_MACRO 8 .debug_macro.dwo
11265
d2854d8d
CT
11266 The section identifiers for Version 5 are:
11267
11268 DW_SECT_INFO_V5 1 .debug_info.dwo
11269 DW_SECT_RESERVED_V5 2 --
11270 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11271 DW_SECT_LINE_V5 4 .debug_line.dwo
11272 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11273 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11274 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11275 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11276
73869dc2
DE
11277 The offsets provided by the CU and TU index sections are the base offsets
11278 for the contributions made by each CU or TU to the corresponding section
11279 in the package file. Each CU and TU header contains an abbrev_offset
11280 field, used to find the abbreviations table for that CU or TU within the
11281 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11282 be interpreted as relative to the base offset given in the index section.
11283 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11284 should be interpreted as relative to the base offset for .debug_line.dwo,
11285 and offsets into other debug sections obtained from DWARF attributes should
11286 also be interpreted as relative to the corresponding base offset.
11287
11288 The table of sizes begins immediately following the table of offsets.
11289 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11290 with L columns and N rows, in row-major order. Each row in the array is
11291 indexed starting from 1 (row 0 is shared by the two tables).
11292
11293 ---
11294
11295 Hash table lookup is handled the same in version 1 and 2:
11296
11297 We assume that N and M will not exceed 2^32 - 1.
11298 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11299
d2415c6c
DE
11300 Given a 64-bit compilation unit signature or a type signature S, an entry
11301 in the hash table is located as follows:
80626a55 11302
d2415c6c
DE
11303 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11304 the low-order k bits all set to 1.
80626a55 11305
d2415c6c 11306 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11307
d2415c6c
DE
11308 3) If the hash table entry at index H matches the signature, use that
11309 entry. If the hash table entry at index H is unused (all zeroes),
11310 terminate the search: the signature is not present in the table.
80626a55 11311
d2415c6c 11312 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11313
d2415c6c 11314 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11315 to stop at an unused slot or find the match. */
80626a55
DE
11316
11317/* Create a hash table to map DWO IDs to their CU/TU entry in
11318 .debug_{info,types}.dwo in DWP_FILE.
11319 Returns NULL if there isn't one.
11320 Note: This function processes DWP files only, not DWO files. */
11321
11322static struct dwp_hash_table *
976ca316 11323create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11324 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11325{
976ca316 11326 struct objfile *objfile = per_objfile->objfile;
400174b1 11327 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11328 const gdb_byte *index_ptr, *index_end;
80626a55 11329 struct dwarf2_section_info *index;
73869dc2 11330 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11331 struct dwp_hash_table *htab;
11332
11333 if (is_debug_types)
11334 index = &dwp_file->sections.tu_index;
11335 else
11336 index = &dwp_file->sections.cu_index;
11337
96b79293 11338 if (index->empty ())
80626a55 11339 return NULL;
96b79293 11340 index->read (objfile);
80626a55
DE
11341
11342 index_ptr = index->buffer;
11343 index_end = index_ptr + index->size;
11344
d2854d8d
CT
11345 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11346 For now it's safe to just read 4 bytes (particularly as it's difficult to
11347 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11348 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11349 index_ptr += 4;
d2854d8d 11350 if (version == 2 || version == 5)
73869dc2
DE
11351 nr_columns = read_4_bytes (dbfd, index_ptr);
11352 else
11353 nr_columns = 0;
11354 index_ptr += 4;
80626a55
DE
11355 nr_units = read_4_bytes (dbfd, index_ptr);
11356 index_ptr += 4;
11357 nr_slots = read_4_bytes (dbfd, index_ptr);
11358 index_ptr += 4;
11359
d2854d8d 11360 if (version != 1 && version != 2 && version != 5)
80626a55 11361 {
21aa081e 11362 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11363 " [in module %s]"),
21aa081e 11364 pulongest (version), dwp_file->name);
80626a55
DE
11365 }
11366 if (nr_slots != (nr_slots & -nr_slots))
11367 {
21aa081e 11368 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11369 " is not power of 2 [in module %s]"),
21aa081e 11370 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11371 }
11372
976ca316 11373 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11374 htab->version = version;
11375 htab->nr_columns = nr_columns;
80626a55
DE
11376 htab->nr_units = nr_units;
11377 htab->nr_slots = nr_slots;
11378 htab->hash_table = index_ptr;
11379 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11380
11381 /* Exit early if the table is empty. */
11382 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11383 || (version == 2 && nr_columns == 0)
11384 || (version == 5 && nr_columns == 0))
73869dc2
DE
11385 {
11386 /* All must be zero. */
11387 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11388 || (version == 2 && nr_columns != 0)
11389 || (version == 5 && nr_columns != 0))
73869dc2 11390 {
b98664d3 11391 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11392 " all zero [in modules %s]"),
11393 dwp_file->name);
11394 }
11395 return htab;
11396 }
11397
11398 if (version == 1)
11399 {
11400 htab->section_pool.v1.indices =
11401 htab->unit_table + sizeof (uint32_t) * nr_slots;
11402 /* It's harder to decide whether the section is too small in v1.
11403 V1 is deprecated anyway so we punt. */
11404 }
d2854d8d 11405 else if (version == 2)
73869dc2
DE
11406 {
11407 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11408 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11409 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11410 /* Reverse map for error checking. */
11411 int ids_seen[DW_SECT_MAX + 1];
11412 int i;
11413
11414 if (nr_columns < 2)
11415 {
11416 error (_("Dwarf Error: bad DWP hash table, too few columns"
11417 " in section table [in module %s]"),
11418 dwp_file->name);
11419 }
11420 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11421 {
11422 error (_("Dwarf Error: bad DWP hash table, too many columns"
11423 " in section table [in module %s]"),
11424 dwp_file->name);
11425 }
04fd5eed
GB
11426 memset (ids, 255, sizeof_ids);
11427 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11428 for (i = 0; i < nr_columns; ++i)
11429 {
11430 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11431
11432 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11433 {
11434 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11435 " in section table [in module %s]"),
11436 id, dwp_file->name);
11437 }
11438 if (ids_seen[id] != -1)
11439 {
11440 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11441 " id %d in section table [in module %s]"),
11442 id, dwp_file->name);
11443 }
11444 ids_seen[id] = i;
11445 ids[i] = id;
11446 }
11447 /* Must have exactly one info or types section. */
11448 if (((ids_seen[DW_SECT_INFO] != -1)
11449 + (ids_seen[DW_SECT_TYPES] != -1))
11450 != 1)
11451 {
11452 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11453 " DWO info/types section [in module %s]"),
11454 dwp_file->name);
11455 }
11456 /* Must have an abbrev section. */
11457 if (ids_seen[DW_SECT_ABBREV] == -1)
11458 {
11459 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11460 " section [in module %s]"),
11461 dwp_file->name);
11462 }
11463 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11464 htab->section_pool.v2.sizes =
11465 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11466 * nr_units * nr_columns);
11467 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11468 * nr_units * nr_columns))
11469 > index_end)
11470 {
11471 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11472 " [in module %s]"),
11473 dwp_file->name);
11474 }
11475 }
d2854d8d
CT
11476 else /* version == 5 */
11477 {
11478 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11479 int *ids = htab->section_pool.v5.section_ids;
11480 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11481 /* Reverse map for error checking. */
11482 int ids_seen[DW_SECT_MAX_V5 + 1];
11483
11484 if (nr_columns < 2)
11485 {
11486 error (_("Dwarf Error: bad DWP hash table, too few columns"
11487 " in section table [in module %s]"),
11488 dwp_file->name);
11489 }
11490 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11491 {
11492 error (_("Dwarf Error: bad DWP hash table, too many columns"
11493 " in section table [in module %s]"),
11494 dwp_file->name);
11495 }
11496 memset (ids, 255, sizeof_ids);
11497 memset (ids_seen, 255, sizeof (ids_seen));
11498 for (int i = 0; i < nr_columns; ++i)
11499 {
11500 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11501
11502 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11503 {
11504 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11505 " in section table [in module %s]"),
11506 id, dwp_file->name);
11507 }
11508 if (ids_seen[id] != -1)
11509 {
11510 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11511 " id %d in section table [in module %s]"),
11512 id, dwp_file->name);
11513 }
11514 ids_seen[id] = i;
11515 ids[i] = id;
11516 }
11517 /* Must have seen an info section. */
11518 if (ids_seen[DW_SECT_INFO_V5] == -1)
11519 {
11520 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11521 " DWO info/types section [in module %s]"),
11522 dwp_file->name);
11523 }
11524 /* Must have an abbrev section. */
11525 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11526 {
11527 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11528 " section [in module %s]"),
11529 dwp_file->name);
11530 }
11531 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11532 htab->section_pool.v5.sizes
11533 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11534 * nr_units * nr_columns);
11535 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11536 * nr_units * nr_columns))
11537 > index_end)
11538 {
11539 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11540 " [in module %s]"),
11541 dwp_file->name);
11542 }
11543 }
80626a55
DE
11544
11545 return htab;
11546}
11547
11548/* Update SECTIONS with the data from SECTP.
11549
5bb6e9dd
TT
11550 This function is like the other "locate" section routines, but in
11551 this context the sections to read comes from the DWP V1 hash table,
11552 not the full ELF section table.
80626a55
DE
11553
11554 The result is non-zero for success, or zero if an error was found. */
11555
11556static int
73869dc2
DE
11557locate_v1_virtual_dwo_sections (asection *sectp,
11558 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11559{
11560 const struct dwop_section_names *names = &dwop_section_names;
11561
fbedd546 11562 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
11563 {
11564 /* There can be only one. */
049412e3 11565 if (sections->abbrev.s.section != NULL)
80626a55 11566 return 0;
049412e3 11567 sections->abbrev.s.section = sectp;
fd361982 11568 sections->abbrev.size = bfd_section_size (sectp);
80626a55 11569 }
fbedd546
TT
11570 else if (names->info_dwo.matches (sectp->name)
11571 || names->types_dwo.matches (sectp->name))
80626a55
DE
11572 {
11573 /* There can be only one. */
049412e3 11574 if (sections->info_or_types.s.section != NULL)
80626a55 11575 return 0;
049412e3 11576 sections->info_or_types.s.section = sectp;
fd361982 11577 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 11578 }
fbedd546 11579 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
11580 {
11581 /* There can be only one. */
049412e3 11582 if (sections->line.s.section != NULL)
80626a55 11583 return 0;
049412e3 11584 sections->line.s.section = sectp;
fd361982 11585 sections->line.size = bfd_section_size (sectp);
80626a55 11586 }
fbedd546 11587 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
11588 {
11589 /* There can be only one. */
049412e3 11590 if (sections->loc.s.section != NULL)
80626a55 11591 return 0;
049412e3 11592 sections->loc.s.section = sectp;
fd361982 11593 sections->loc.size = bfd_section_size (sectp);
80626a55 11594 }
fbedd546 11595 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
11596 {
11597 /* There can be only one. */
049412e3 11598 if (sections->macinfo.s.section != NULL)
80626a55 11599 return 0;
049412e3 11600 sections->macinfo.s.section = sectp;
fd361982 11601 sections->macinfo.size = bfd_section_size (sectp);
80626a55 11602 }
fbedd546 11603 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
11604 {
11605 /* There can be only one. */
049412e3 11606 if (sections->macro.s.section != NULL)
80626a55 11607 return 0;
049412e3 11608 sections->macro.s.section = sectp;
fd361982 11609 sections->macro.size = bfd_section_size (sectp);
80626a55 11610 }
fbedd546 11611 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
11612 {
11613 /* There can be only one. */
049412e3 11614 if (sections->str_offsets.s.section != NULL)
80626a55 11615 return 0;
049412e3 11616 sections->str_offsets.s.section = sectp;
fd361982 11617 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11618 }
11619 else
11620 {
11621 /* No other kind of section is valid. */
11622 return 0;
11623 }
11624
11625 return 1;
11626}
11627
73869dc2
DE
11628/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11629 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11630 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11631 This is for DWP version 1 files. */
80626a55
DE
11632
11633static struct dwo_unit *
976ca316 11634create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11635 struct dwp_file *dwp_file,
73869dc2
DE
11636 uint32_t unit_index,
11637 const char *comp_dir,
11638 ULONGEST signature, int is_debug_types)
80626a55 11639{
73869dc2
DE
11640 const struct dwp_hash_table *dwp_htab =
11641 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11642 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11643 const char *kind = is_debug_types ? "TU" : "CU";
11644 struct dwo_file *dwo_file;
11645 struct dwo_unit *dwo_unit;
73869dc2 11646 struct virtual_v1_dwo_sections sections;
80626a55 11647 void **dwo_file_slot;
80626a55
DE
11648 int i;
11649
73869dc2
DE
11650 gdb_assert (dwp_file->version == 1);
11651
6f738b01
SM
11652 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11653 kind, pulongest (unit_index), hex_string (signature),
11654 dwp_file->name);
80626a55 11655
19ac8c2e 11656 /* Fetch the sections of this DWO unit.
80626a55
DE
11657 Put a limit on the number of sections we look for so that bad data
11658 doesn't cause us to loop forever. */
11659
73869dc2 11660#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11661 (1 /* .debug_info or .debug_types */ \
11662 + 1 /* .debug_abbrev */ \
11663 + 1 /* .debug_line */ \
11664 + 1 /* .debug_loc */ \
11665 + 1 /* .debug_str_offsets */ \
19ac8c2e 11666 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11667 + 1 /* trailing zero */)
11668
11669 memset (&sections, 0, sizeof (sections));
80626a55 11670
73869dc2 11671 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11672 {
11673 asection *sectp;
11674 uint32_t section_nr =
11675 read_4_bytes (dbfd,
73869dc2
DE
11676 dwp_htab->section_pool.v1.indices
11677 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11678
11679 if (section_nr == 0)
11680 break;
11681 if (section_nr >= dwp_file->num_sections)
11682 {
11683 error (_("Dwarf Error: bad DWP hash table, section number too large"
11684 " [in module %s]"),
11685 dwp_file->name);
11686 }
11687
11688 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11689 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11690 {
11691 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11692 " [in module %s]"),
11693 dwp_file->name);
11694 }
11695 }
11696
11697 if (i < 2
96b79293
TT
11698 || sections.info_or_types.empty ()
11699 || sections.abbrev.empty ())
80626a55
DE
11700 {
11701 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11702 " [in module %s]"),
11703 dwp_file->name);
11704 }
73869dc2 11705 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11706 {
11707 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11708 " [in module %s]"),
11709 dwp_file->name);
11710 }
11711
11712 /* It's easier for the rest of the code if we fake a struct dwo_file and
11713 have dwo_unit "live" in that. At least for now.
11714
11715 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11716 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11717 file, we can combine them back into a virtual DWO file to save space
11718 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11719 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11720
791afaa2
TT
11721 std::string virtual_dwo_name =
11722 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11723 sections.abbrev.get_id (),
11724 sections.line.get_id (),
11725 sections.loc.get_id (),
11726 sections.str_offsets.get_id ());
80626a55 11727 /* Can we use an existing virtual DWO file? */
976ca316 11728 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11729 comp_dir);
80626a55
DE
11730 /* Create one if necessary. */
11731 if (*dwo_file_slot == NULL)
11732 {
6f738b01
SM
11733 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11734 virtual_dwo_name.c_str ());
11735
51ac9db5 11736 dwo_file = new struct dwo_file;
976ca316 11737 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11738 dwo_file->comp_dir = comp_dir;
80626a55
DE
11739 dwo_file->sections.abbrev = sections.abbrev;
11740 dwo_file->sections.line = sections.line;
11741 dwo_file->sections.loc = sections.loc;
11742 dwo_file->sections.macinfo = sections.macinfo;
11743 dwo_file->sections.macro = sections.macro;
11744 dwo_file->sections.str_offsets = sections.str_offsets;
11745 /* The "str" section is global to the entire DWP file. */
11746 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11747 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11748 there's no need to record it in dwo_file.
11749 Also, we can't simply record type sections in dwo_file because
11750 we record a pointer into the vector in dwo_unit. As we collect more
11751 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11752 for it, invalidating all copies of pointers into the previous
11753 contents. */
80626a55
DE
11754 *dwo_file_slot = dwo_file;
11755 }
11756 else
11757 {
6f738b01
SM
11758 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11759 virtual_dwo_name.c_str ());
11760
9a3c8263 11761 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11762 }
80626a55 11763
976ca316 11764 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11765 dwo_unit->dwo_file = dwo_file;
11766 dwo_unit->signature = signature;
8d749320 11767 dwo_unit->section =
976ca316 11768 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11769 *dwo_unit->section = sections.info_or_types;
57d63ce2 11770 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11771
11772 return dwo_unit;
11773}
11774
d2854d8d
CT
11775/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11776 simplify them. Given a pointer to the containing section SECTION, and
11777 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11778 virtual section of just that piece. */
73869dc2
DE
11779
11780static struct dwarf2_section_info
d2854d8d
CT
11781create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11782 struct dwarf2_section_info *section,
11783 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
11784{
11785 struct dwarf2_section_info result;
11786 asection *sectp;
11787
11788 gdb_assert (section != NULL);
11789 gdb_assert (!section->is_virtual);
11790
11791 memset (&result, 0, sizeof (result));
11792 result.s.containing_section = section;
dc4ccb6f 11793 result.is_virtual = true;
73869dc2
DE
11794
11795 if (size == 0)
11796 return result;
11797
96b79293 11798 sectp = section->get_bfd_section ();
73869dc2
DE
11799
11800 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11801 bounds of the real section. This is a pretty-rare event, so just
11802 flag an error (easier) instead of a warning and trying to cope. */
11803 if (sectp == NULL
fd361982 11804 || offset + size > bfd_section_size (sectp))
73869dc2 11805 {
d2854d8d 11806 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 11807 " in section %s [in module %s]"),
fd361982 11808 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 11809 objfile_name (per_objfile->objfile));
73869dc2
DE
11810 }
11811
11812 result.virtual_offset = offset;
11813 result.size = size;
11814 return result;
11815}
11816
11817/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11818 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11819 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11820 This is for DWP version 2 files. */
11821
11822static struct dwo_unit *
976ca316 11823create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 11824 struct dwp_file *dwp_file,
73869dc2
DE
11825 uint32_t unit_index,
11826 const char *comp_dir,
11827 ULONGEST signature, int is_debug_types)
11828{
73869dc2
DE
11829 const struct dwp_hash_table *dwp_htab =
11830 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11831 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11832 const char *kind = is_debug_types ? "TU" : "CU";
11833 struct dwo_file *dwo_file;
11834 struct dwo_unit *dwo_unit;
d2854d8d 11835 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 11836 void **dwo_file_slot;
73869dc2
DE
11837 int i;
11838
11839 gdb_assert (dwp_file->version == 2);
11840
6f738b01
SM
11841 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11842 kind, pulongest (unit_index), hex_string (signature),
11843 dwp_file->name);
73869dc2
DE
11844
11845 /* Fetch the section offsets of this DWO unit. */
11846
11847 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11848
11849 for (i = 0; i < dwp_htab->nr_columns; ++i)
11850 {
11851 uint32_t offset = read_4_bytes (dbfd,
11852 dwp_htab->section_pool.v2.offsets
11853 + (((unit_index - 1) * dwp_htab->nr_columns
11854 + i)
11855 * sizeof (uint32_t)));
11856 uint32_t size = read_4_bytes (dbfd,
11857 dwp_htab->section_pool.v2.sizes
11858 + (((unit_index - 1) * dwp_htab->nr_columns
11859 + i)
11860 * sizeof (uint32_t)));
11861
11862 switch (dwp_htab->section_pool.v2.section_ids[i])
11863 {
11864 case DW_SECT_INFO:
11865 case DW_SECT_TYPES:
11866 sections.info_or_types_offset = offset;
11867 sections.info_or_types_size = size;
11868 break;
11869 case DW_SECT_ABBREV:
11870 sections.abbrev_offset = offset;
11871 sections.abbrev_size = size;
11872 break;
11873 case DW_SECT_LINE:
11874 sections.line_offset = offset;
11875 sections.line_size = size;
11876 break;
11877 case DW_SECT_LOC:
11878 sections.loc_offset = offset;
11879 sections.loc_size = size;
11880 break;
11881 case DW_SECT_STR_OFFSETS:
11882 sections.str_offsets_offset = offset;
11883 sections.str_offsets_size = size;
11884 break;
11885 case DW_SECT_MACINFO:
11886 sections.macinfo_offset = offset;
11887 sections.macinfo_size = size;
11888 break;
11889 case DW_SECT_MACRO:
11890 sections.macro_offset = offset;
11891 sections.macro_size = size;
11892 break;
11893 }
11894 }
11895
11896 /* It's easier for the rest of the code if we fake a struct dwo_file and
11897 have dwo_unit "live" in that. At least for now.
11898
11899 The DWP file can be made up of a random collection of CUs and TUs.
11900 However, for each CU + set of TUs that came from the same original DWO
11901 file, we can combine them back into a virtual DWO file to save space
11902 (fewer struct dwo_file objects to allocate). Remember that for really
11903 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11904
791afaa2
TT
11905 std::string virtual_dwo_name =
11906 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11907 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11908 (long) (sections.line_size ? sections.line_offset : 0),
11909 (long) (sections.loc_size ? sections.loc_offset : 0),
11910 (long) (sections.str_offsets_size
11911 ? sections.str_offsets_offset : 0));
73869dc2 11912 /* Can we use an existing virtual DWO file? */
976ca316 11913 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11914 comp_dir);
73869dc2
DE
11915 /* Create one if necessary. */
11916 if (*dwo_file_slot == NULL)
11917 {
6f738b01
SM
11918 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11919 virtual_dwo_name.c_str ());
11920
51ac9db5 11921 dwo_file = new struct dwo_file;
976ca316 11922 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
11923 dwo_file->comp_dir = comp_dir;
11924 dwo_file->sections.abbrev =
d2854d8d
CT
11925 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11926 sections.abbrev_offset,
11927 sections.abbrev_size);
73869dc2 11928 dwo_file->sections.line =
d2854d8d
CT
11929 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11930 sections.line_offset,
11931 sections.line_size);
73869dc2 11932 dwo_file->sections.loc =
d2854d8d
CT
11933 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11934 sections.loc_offset, sections.loc_size);
73869dc2 11935 dwo_file->sections.macinfo =
d2854d8d
CT
11936 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11937 sections.macinfo_offset,
11938 sections.macinfo_size);
73869dc2 11939 dwo_file->sections.macro =
d2854d8d
CT
11940 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11941 sections.macro_offset,
11942 sections.macro_size);
73869dc2 11943 dwo_file->sections.str_offsets =
d2854d8d
CT
11944 create_dwp_v2_or_v5_section (per_objfile,
11945 &dwp_file->sections.str_offsets,
11946 sections.str_offsets_offset,
11947 sections.str_offsets_size);
73869dc2
DE
11948 /* The "str" section is global to the entire DWP file. */
11949 dwo_file->sections.str = dwp_file->sections.str;
11950 /* The info or types section is assigned below to dwo_unit,
11951 there's no need to record it in dwo_file.
11952 Also, we can't simply record type sections in dwo_file because
11953 we record a pointer into the vector in dwo_unit. As we collect more
11954 types we'll grow the vector and eventually have to reallocate space
11955 for it, invalidating all copies of pointers into the previous
11956 contents. */
11957 *dwo_file_slot = dwo_file;
11958 }
11959 else
11960 {
6f738b01
SM
11961 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11962 virtual_dwo_name.c_str ());
11963
9a3c8263 11964 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11965 }
73869dc2 11966
976ca316 11967 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
11968 dwo_unit->dwo_file = dwo_file;
11969 dwo_unit->signature = signature;
8d749320 11970 dwo_unit->section =
976ca316 11971 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 11972 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 11973 (per_objfile,
d2854d8d
CT
11974 is_debug_types
11975 ? &dwp_file->sections.types
11976 : &dwp_file->sections.info,
11977 sections.info_or_types_offset,
11978 sections.info_or_types_size);
11979 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11980
11981 return dwo_unit;
11982}
11983
11984/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11985 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11986 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11987 This is for DWP version 5 files. */
11988
11989static struct dwo_unit *
11990create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11991 struct dwp_file *dwp_file,
11992 uint32_t unit_index,
11993 const char *comp_dir,
11994 ULONGEST signature, int is_debug_types)
11995{
11996 const struct dwp_hash_table *dwp_htab
11997 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11998 bfd *dbfd = dwp_file->dbfd.get ();
11999 const char *kind = is_debug_types ? "TU" : "CU";
12000 struct dwo_file *dwo_file;
12001 struct dwo_unit *dwo_unit;
12002 struct virtual_v2_or_v5_dwo_sections sections {};
12003 void **dwo_file_slot;
12004
12005 gdb_assert (dwp_file->version == 5);
12006
6f738b01
SM
12007 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12008 kind, pulongest (unit_index), hex_string (signature),
12009 dwp_file->name);
d2854d8d
CT
12010
12011 /* Fetch the section offsets of this DWO unit. */
12012
12013 /* memset (&sections, 0, sizeof (sections)); */
12014
12015 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12016 {
12017 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12018 dwp_htab->section_pool.v5.offsets
12019 + (((unit_index - 1)
12020 * dwp_htab->nr_columns
12021 + i)
12022 * sizeof (uint32_t)));
d2854d8d 12023 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12024 dwp_htab->section_pool.v5.sizes
12025 + (((unit_index - 1) * dwp_htab->nr_columns
12026 + i)
12027 * sizeof (uint32_t)));
d2854d8d
CT
12028
12029 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12030 {
12031 case DW_SECT_ABBREV_V5:
12032 sections.abbrev_offset = offset;
12033 sections.abbrev_size = size;
12034 break;
12035 case DW_SECT_INFO_V5:
12036 sections.info_or_types_offset = offset;
12037 sections.info_or_types_size = size;
12038 break;
12039 case DW_SECT_LINE_V5:
12040 sections.line_offset = offset;
12041 sections.line_size = size;
12042 break;
12043 case DW_SECT_LOCLISTS_V5:
12044 sections.loclists_offset = offset;
12045 sections.loclists_size = size;
12046 break;
12047 case DW_SECT_MACRO_V5:
12048 sections.macro_offset = offset;
12049 sections.macro_size = size;
12050 break;
12051 case DW_SECT_RNGLISTS_V5:
12052 sections.rnglists_offset = offset;
12053 sections.rnglists_size = size;
12054 break;
12055 case DW_SECT_STR_OFFSETS_V5:
12056 sections.str_offsets_offset = offset;
12057 sections.str_offsets_size = size;
12058 break;
12059 case DW_SECT_RESERVED_V5:
12060 default:
12061 break;
12062 }
d2854d8d
CT
12063 }
12064
12065 /* It's easier for the rest of the code if we fake a struct dwo_file and
12066 have dwo_unit "live" in that. At least for now.
12067
12068 The DWP file can be made up of a random collection of CUs and TUs.
12069 However, for each CU + set of TUs that came from the same original DWO
12070 file, we can combine them back into a virtual DWO file to save space
12071 (fewer struct dwo_file objects to allocate). Remember that for really
12072 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12073
12074 std::string virtual_dwo_name =
12075 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12076 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12077 (long) (sections.line_size ? sections.line_offset : 0),
12078 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12079 (long) (sections.str_offsets_size
12080 ? sections.str_offsets_offset : 0),
12081 (long) (sections.macro_size ? sections.macro_offset : 0),
12082 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12083 /* Can we use an existing virtual DWO file? */
12084 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12085 virtual_dwo_name.c_str (),
12086 comp_dir);
d2854d8d
CT
12087 /* Create one if necessary. */
12088 if (*dwo_file_slot == NULL)
12089 {
6f738b01
SM
12090 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12091 virtual_dwo_name.c_str ());
12092
d2854d8d
CT
12093 dwo_file = new struct dwo_file;
12094 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12095 dwo_file->comp_dir = comp_dir;
12096 dwo_file->sections.abbrev =
dda83cd7
SM
12097 create_dwp_v2_or_v5_section (per_objfile,
12098 &dwp_file->sections.abbrev,
12099 sections.abbrev_offset,
12100 sections.abbrev_size);
d2854d8d 12101 dwo_file->sections.line =
dda83cd7
SM
12102 create_dwp_v2_or_v5_section (per_objfile,
12103 &dwp_file->sections.line,
12104 sections.line_offset, sections.line_size);
d2854d8d 12105 dwo_file->sections.macro =
dda83cd7
SM
12106 create_dwp_v2_or_v5_section (per_objfile,
12107 &dwp_file->sections.macro,
12108 sections.macro_offset,
12109 sections.macro_size);
d2854d8d 12110 dwo_file->sections.loclists =
dda83cd7
SM
12111 create_dwp_v2_or_v5_section (per_objfile,
12112 &dwp_file->sections.loclists,
12113 sections.loclists_offset,
12114 sections.loclists_size);
d2854d8d 12115 dwo_file->sections.rnglists =
dda83cd7
SM
12116 create_dwp_v2_or_v5_section (per_objfile,
12117 &dwp_file->sections.rnglists,
12118 sections.rnglists_offset,
12119 sections.rnglists_size);
d2854d8d 12120 dwo_file->sections.str_offsets =
dda83cd7
SM
12121 create_dwp_v2_or_v5_section (per_objfile,
12122 &dwp_file->sections.str_offsets,
12123 sections.str_offsets_offset,
12124 sections.str_offsets_size);
d2854d8d
CT
12125 /* The "str" section is global to the entire DWP file. */
12126 dwo_file->sections.str = dwp_file->sections.str;
12127 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12128 there's no need to record it in dwo_file.
12129 Also, we can't simply record type sections in dwo_file because
12130 we record a pointer into the vector in dwo_unit. As we collect more
12131 types we'll grow the vector and eventually have to reallocate space
12132 for it, invalidating all copies of pointers into the previous
12133 contents. */
d2854d8d
CT
12134 *dwo_file_slot = dwo_file;
12135 }
12136 else
12137 {
6f738b01
SM
12138 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12139 virtual_dwo_name.c_str ());
12140
d2854d8d
CT
12141 dwo_file = (struct dwo_file *) *dwo_file_slot;
12142 }
12143
12144 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12145 dwo_unit->dwo_file = dwo_file;
12146 dwo_unit->signature = signature;
12147 dwo_unit->section
12148 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12149 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12150 &dwp_file->sections.info,
12151 sections.info_or_types_offset,
12152 sections.info_or_types_size);
73869dc2
DE
12153 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12154
12155 return dwo_unit;
12156}
12157
57d63ce2
DE
12158/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12159 Returns NULL if the signature isn't found. */
80626a55
DE
12160
12161static struct dwo_unit *
976ca316 12162lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12163 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12164 ULONGEST signature, int is_debug_types)
80626a55 12165{
57d63ce2
DE
12166 const struct dwp_hash_table *dwp_htab =
12167 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12168 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12169 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12170 uint32_t hash = signature & mask;
12171 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12172 unsigned int i;
12173 void **slot;
870f88f7 12174 struct dwo_unit find_dwo_cu;
80626a55
DE
12175
12176 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12177 find_dwo_cu.signature = signature;
19ac8c2e 12178 slot = htab_find_slot (is_debug_types
48b490f2
TT
12179 ? dwp_file->loaded_tus.get ()
12180 : dwp_file->loaded_cus.get (),
19ac8c2e 12181 &find_dwo_cu, INSERT);
80626a55
DE
12182
12183 if (*slot != NULL)
9a3c8263 12184 return (struct dwo_unit *) *slot;
80626a55
DE
12185
12186 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12187 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12188 {
12189 ULONGEST signature_in_table;
12190
12191 signature_in_table =
57d63ce2 12192 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12193 if (signature_in_table == signature)
12194 {
57d63ce2
DE
12195 uint32_t unit_index =
12196 read_4_bytes (dbfd,
12197 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12198
73869dc2
DE
12199 if (dwp_file->version == 1)
12200 {
976ca316
SM
12201 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12202 unit_index, comp_dir,
12203 signature, is_debug_types);
73869dc2 12204 }
d2854d8d 12205 else if (dwp_file->version == 2)
73869dc2 12206 {
976ca316
SM
12207 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12208 unit_index, comp_dir,
12209 signature, is_debug_types);
73869dc2 12210 }
d2854d8d
CT
12211 else /* version == 5 */
12212 {
12213 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12214 unit_index, comp_dir,
12215 signature, is_debug_types);
12216 }
9a3c8263 12217 return (struct dwo_unit *) *slot;
80626a55
DE
12218 }
12219 if (signature_in_table == 0)
12220 return NULL;
12221 hash = (hash + hash2) & mask;
12222 }
12223
12224 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12225 " [in module %s]"),
12226 dwp_file->name);
12227}
12228
ab5088bf 12229/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12230 Open the file specified by FILE_NAME and hand it off to BFD for
12231 preliminary analysis. Return a newly initialized bfd *, which
12232 includes a canonicalized copy of FILE_NAME.
80626a55 12233 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12234 SEARCH_CWD is true if the current directory is to be searched.
12235 It will be searched before debug-file-directory.
13aaf454
DE
12236 If successful, the file is added to the bfd include table of the
12237 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12238 If unable to find/open the file, return NULL.
3019eac3
DE
12239 NOTE: This function is derived from symfile_bfd_open. */
12240
192b62ce 12241static gdb_bfd_ref_ptr
976ca316 12242try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12243 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12244{
24b9144d 12245 int desc;
9c02c129
DE
12246 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12247 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12248 to debug_file_directory. */
e0cc99a6 12249 const char *search_path;
9c02c129
DE
12250 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12251
e0cc99a6 12252 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12253 if (search_cwd)
12254 {
12255 if (*debug_file_directory != '\0')
e0cc99a6
TT
12256 {
12257 search_path_holder.reset (concat (".", dirname_separator_string,
12258 debug_file_directory,
12259 (char *) NULL));
12260 search_path = search_path_holder.get ();
12261 }
6ac97d4c 12262 else
e0cc99a6 12263 search_path = ".";
6ac97d4c 12264 }
9c02c129 12265 else
e0cc99a6 12266 search_path = debug_file_directory;
3019eac3 12267
56d467f4
CT
12268 /* Add the path for the executable binary to the list of search paths. */
12269 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12270 search_path_holder.reset (concat (objfile_dir.c_str (),
12271 dirname_separator_string,
12272 search_path, nullptr));
12273 search_path = search_path_holder.get ();
12274
24b9144d 12275 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12276 if (is_dwp)
12277 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12278
12279 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12280 desc = openp (search_path, flags, file_name,
3019eac3
DE
12281 O_RDONLY | O_BINARY, &absolute_name);
12282 if (desc < 0)
12283 return NULL;
12284
e0cc99a6
TT
12285 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12286 gnutarget, desc));
9c02c129
DE
12287 if (sym_bfd == NULL)
12288 return NULL;
192b62ce 12289 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12290
192b62ce
TT
12291 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12292 return NULL;
3019eac3 12293
13aaf454
DE
12294 /* Success. Record the bfd as having been included by the objfile's bfd.
12295 This is important because things like demangled_names_hash lives in the
12296 objfile's per_bfd space and may have references to things like symbol
12297 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12298 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12299
3019eac3
DE
12300 return sym_bfd;
12301}
12302
ab5088bf 12303/* Try to open DWO file FILE_NAME.
3019eac3
DE
12304 COMP_DIR is the DW_AT_comp_dir attribute.
12305 The result is the bfd handle of the file.
12306 If there is a problem finding or opening the file, return NULL.
12307 Upon success, the canonicalized path of the file is stored in the bfd,
12308 same as symfile_bfd_open. */
12309
192b62ce 12310static gdb_bfd_ref_ptr
976ca316 12311open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12312 const char *file_name, const char *comp_dir)
3019eac3 12313{
80626a55 12314 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12315 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12316 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12317
12318 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12319
12320 if (comp_dir != NULL)
12321 {
43816ebc
TT
12322 gdb::unique_xmalloc_ptr<char> path_to_try
12323 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12324
12325 /* NOTE: If comp_dir is a relative path, this will also try the
12326 search path, which seems useful. */
976ca316 12327 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12328 0 /*is_dwp*/,
192b62ce 12329 1 /*search_cwd*/));
3019eac3
DE
12330 if (abfd != NULL)
12331 return abfd;
12332 }
12333
12334 /* That didn't work, try debug-file-directory, which, despite its name,
12335 is a list of paths. */
12336
12337 if (*debug_file_directory == '\0')
12338 return NULL;
12339
976ca316 12340 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12341 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12342}
12343
80626a55
DE
12344/* This function is mapped across the sections and remembers the offset and
12345 size of each of the DWO debugging sections we are interested in. */
12346
12347static void
5bb6e9dd
TT
12348dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12349 dwo_sections *dwo_sections)
80626a55 12350{
80626a55
DE
12351 const struct dwop_section_names *names = &dwop_section_names;
12352
fbedd546 12353 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12354 {
049412e3 12355 dwo_sections->abbrev.s.section = sectp;
fd361982 12356 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12357 }
fbedd546 12358 else if (names->info_dwo.matches (sectp->name))
80626a55 12359 {
049412e3 12360 dwo_sections->info.s.section = sectp;
fd361982 12361 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12362 }
fbedd546 12363 else if (names->line_dwo.matches (sectp->name))
80626a55 12364 {
049412e3 12365 dwo_sections->line.s.section = sectp;
fd361982 12366 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12367 }
fbedd546 12368 else if (names->loc_dwo.matches (sectp->name))
80626a55 12369 {
049412e3 12370 dwo_sections->loc.s.section = sectp;
fd361982 12371 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12372 }
fbedd546 12373 else if (names->loclists_dwo.matches (sectp->name))
41144253 12374 {
12375 dwo_sections->loclists.s.section = sectp;
12376 dwo_sections->loclists.size = bfd_section_size (sectp);
12377 }
fbedd546 12378 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12379 {
049412e3 12380 dwo_sections->macinfo.s.section = sectp;
fd361982 12381 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12382 }
fbedd546 12383 else if (names->macro_dwo.matches (sectp->name))
80626a55 12384 {
049412e3 12385 dwo_sections->macro.s.section = sectp;
fd361982 12386 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12387 }
fbedd546 12388 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12389 {
12390 dwo_sections->rnglists.s.section = sectp;
12391 dwo_sections->rnglists.size = bfd_section_size (sectp);
12392 }
fbedd546 12393 else if (names->str_dwo.matches (sectp->name))
80626a55 12394 {
049412e3 12395 dwo_sections->str.s.section = sectp;
fd361982 12396 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12397 }
fbedd546 12398 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12399 {
049412e3 12400 dwo_sections->str_offsets.s.section = sectp;
fd361982 12401 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12402 }
fbedd546 12403 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12404 {
12405 struct dwarf2_section_info type_section;
12406
12407 memset (&type_section, 0, sizeof (type_section));
049412e3 12408 type_section.s.section = sectp;
fd361982 12409 type_section.size = bfd_section_size (sectp);
fd5866f6 12410 dwo_sections->types.push_back (type_section);
80626a55
DE
12411 }
12412}
12413
ab5088bf 12414/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12415 by PER_CU. This is for the non-DWP case.
80626a55 12416 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12417
12418static struct dwo_file *
4ab09049
SM
12419open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12420 const char *comp_dir)
3019eac3 12421{
976ca316 12422 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12423
976ca316 12424 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12425 if (dbfd == NULL)
12426 {
6f738b01
SM
12427 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12428
80626a55
DE
12429 return NULL;
12430 }
263db9a1 12431
51ac9db5 12432 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12433 dwo_file->dwo_name = dwo_name;
12434 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12435 dwo_file->dbfd = std::move (dbfd);
3019eac3 12436
5bb6e9dd
TT
12437 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12438 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12439 &dwo_file->sections);
3019eac3 12440
976ca316
SM
12441 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12442 dwo_file->cus);
3019eac3 12443
d2854d8d
CT
12444 if (cu->per_cu->dwarf_version < 5)
12445 {
12446 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12447 dwo_file->sections.types, dwo_file->tus);
12448 }
12449 else
12450 {
12451 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12452 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12453 rcuh_kind::COMPILE);
d2854d8d 12454 }
3019eac3 12455
6f738b01 12456 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12457
263db9a1 12458 return dwo_file.release ();
3019eac3
DE
12459}
12460
80626a55 12461/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12462 size of each of the DWP debugging sections common to version 1 and 2 that
12463 we are interested in. */
3019eac3 12464
80626a55 12465static void
73869dc2 12466dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12467 dwp_file *dwp_file)
3019eac3 12468{
80626a55
DE
12469 const struct dwop_section_names *names = &dwop_section_names;
12470 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12471
80626a55 12472 /* Record the ELF section number for later lookup: this is what the
73869dc2 12473 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12474 gdb_assert (elf_section_nr < dwp_file->num_sections);
12475 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12476
80626a55 12477 /* Look for specific sections that we need. */
fbedd546 12478 if (names->str_dwo.matches (sectp->name))
80626a55 12479 {
049412e3 12480 dwp_file->sections.str.s.section = sectp;
fd361982 12481 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12482 }
fbedd546 12483 else if (names->cu_index.matches (sectp->name))
80626a55 12484 {
049412e3 12485 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12486 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12487 }
fbedd546 12488 else if (names->tu_index.matches (sectp->name))
80626a55 12489 {
049412e3 12490 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12491 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12492 }
12493}
3019eac3 12494
73869dc2
DE
12495/* This function is mapped across the sections and remembers the offset and
12496 size of each of the DWP version 2 debugging sections that we are interested
12497 in. This is split into a separate function because we don't know if we
d2854d8d 12498 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12499
12500static void
12501dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12502{
9a3c8263 12503 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12504 const struct dwop_section_names *names = &dwop_section_names;
12505 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12506
12507 /* Record the ELF section number for later lookup: this is what the
12508 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12509 gdb_assert (elf_section_nr < dwp_file->num_sections);
12510 dwp_file->elf_sections[elf_section_nr] = sectp;
12511
12512 /* Look for specific sections that we need. */
fbedd546 12513 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12514 {
049412e3 12515 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12516 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12517 }
fbedd546 12518 else if (names->info_dwo.matches (sectp->name))
73869dc2 12519 {
049412e3 12520 dwp_file->sections.info.s.section = sectp;
fd361982 12521 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12522 }
fbedd546 12523 else if (names->line_dwo.matches (sectp->name))
73869dc2 12524 {
049412e3 12525 dwp_file->sections.line.s.section = sectp;
fd361982 12526 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 12527 }
fbedd546 12528 else if (names->loc_dwo.matches (sectp->name))
73869dc2 12529 {
049412e3 12530 dwp_file->sections.loc.s.section = sectp;
fd361982 12531 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 12532 }
fbedd546 12533 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 12534 {
049412e3 12535 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12536 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 12537 }
fbedd546 12538 else if (names->macro_dwo.matches (sectp->name))
73869dc2 12539 {
049412e3 12540 dwp_file->sections.macro.s.section = sectp;
fd361982 12541 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 12542 }
fbedd546 12543 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 12544 {
049412e3 12545 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12546 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 12547 }
fbedd546 12548 else if (names->types_dwo.matches (sectp->name))
73869dc2 12549 {
049412e3 12550 dwp_file->sections.types.s.section = sectp;
fd361982 12551 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12552 }
12553}
12554
d2854d8d
CT
12555/* This function is mapped across the sections and remembers the offset and
12556 size of each of the DWP version 5 debugging sections that we are interested
12557 in. This is split into a separate function because we don't know if we
12558 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12559
12560static void
12561dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12562{
12563 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12564 const struct dwop_section_names *names = &dwop_section_names;
12565 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12566
12567 /* Record the ELF section number for later lookup: this is what the
12568 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12569 gdb_assert (elf_section_nr < dwp_file->num_sections);
12570 dwp_file->elf_sections[elf_section_nr] = sectp;
12571
12572 /* Look for specific sections that we need. */
fbedd546 12573 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
12574 {
12575 dwp_file->sections.abbrev.s.section = sectp;
12576 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12577 }
fbedd546 12578 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
12579 {
12580 dwp_file->sections.info.s.section = sectp;
12581 dwp_file->sections.info.size = bfd_section_size (sectp);
12582 }
fbedd546 12583 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
12584 {
12585 dwp_file->sections.line.s.section = sectp;
12586 dwp_file->sections.line.size = bfd_section_size (sectp);
12587 }
fbedd546 12588 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
12589 {
12590 dwp_file->sections.loclists.s.section = sectp;
12591 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12592 }
fbedd546 12593 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
12594 {
12595 dwp_file->sections.macro.s.section = sectp;
12596 dwp_file->sections.macro.size = bfd_section_size (sectp);
12597 }
fbedd546 12598 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
12599 {
12600 dwp_file->sections.rnglists.s.section = sectp;
12601 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12602 }
fbedd546 12603 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
12604 {
12605 dwp_file->sections.str_offsets.s.section = sectp;
12606 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12607 }
12608}
12609
80626a55 12610/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12611
80626a55
DE
12612static hashval_t
12613hash_dwp_loaded_cutus (const void *item)
12614{
9a3c8263 12615 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12616
80626a55
DE
12617 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12618 return dwo_unit->signature;
3019eac3
DE
12619}
12620
80626a55 12621/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12622
80626a55
DE
12623static int
12624eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12625{
9a3c8263
SM
12626 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12627 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12628
80626a55
DE
12629 return dua->signature == dub->signature;
12630}
3019eac3 12631
80626a55 12632/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12633
48b490f2 12634static htab_up
298e9637 12635allocate_dwp_loaded_cutus_table ()
80626a55 12636{
48b490f2
TT
12637 return htab_up (htab_create_alloc (3,
12638 hash_dwp_loaded_cutus,
12639 eq_dwp_loaded_cutus,
12640 NULL, xcalloc, xfree));
80626a55 12641}
3019eac3 12642
ab5088bf
DE
12643/* Try to open DWP file FILE_NAME.
12644 The result is the bfd handle of the file.
12645 If there is a problem finding or opening the file, return NULL.
12646 Upon success, the canonicalized path of the file is stored in the bfd,
12647 same as symfile_bfd_open. */
12648
192b62ce 12649static gdb_bfd_ref_ptr
976ca316 12650open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12651{
976ca316 12652 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12653 1 /*is_dwp*/,
192b62ce 12654 1 /*search_cwd*/));
6ac97d4c
DE
12655 if (abfd != NULL)
12656 return abfd;
12657
12658 /* Work around upstream bug 15652.
12659 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12660 [Whether that's a "bug" is debatable, but it is getting in our way.]
12661 We have no real idea where the dwp file is, because gdb's realpath-ing
12662 of the executable's path may have discarded the needed info.
12663 [IWBN if the dwp file name was recorded in the executable, akin to
12664 .gnu_debuglink, but that doesn't exist yet.]
12665 Strip the directory from FILE_NAME and search again. */
12666 if (*debug_file_directory != '\0')
12667 {
12668 /* Don't implicitly search the current directory here.
12669 If the user wants to search "." to handle this case,
12670 it must be added to debug-file-directory. */
976ca316
SM
12671 return try_open_dwop_file (per_objfile, lbasename (file_name),
12672 1 /*is_dwp*/,
6ac97d4c
DE
12673 0 /*search_cwd*/);
12674 }
12675
12676 return NULL;
ab5088bf
DE
12677}
12678
80626a55
DE
12679/* Initialize the use of the DWP file for the current objfile.
12680 By convention the name of the DWP file is ${objfile}.dwp.
12681 The result is NULL if it can't be found. */
a766d390 12682
400174b1 12683static std::unique_ptr<struct dwp_file>
976ca316 12684open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12685{
976ca316 12686 struct objfile *objfile = per_objfile->objfile;
80626a55 12687
82bf32bc
JK
12688 /* Try to find first .dwp for the binary file before any symbolic links
12689 resolving. */
6c447423
DE
12690
12691 /* If the objfile is a debug file, find the name of the real binary
12692 file and get the name of dwp file from there. */
d721ba37 12693 std::string dwp_name;
6c447423
DE
12694 if (objfile->separate_debug_objfile_backlink != NULL)
12695 {
12696 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12697 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12698
d721ba37 12699 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12700 }
12701 else
d721ba37
PA
12702 dwp_name = objfile->original_name;
12703
12704 dwp_name += ".dwp";
80626a55 12705
976ca316 12706 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12707 if (dbfd == NULL
12708 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12709 {
12710 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12711 dwp_name = objfile_name (objfile);
12712 dwp_name += ".dwp";
976ca316 12713 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12714 }
12715
80626a55
DE
12716 if (dbfd == NULL)
12717 {
6f738b01
SM
12718 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12719
400174b1 12720 return std::unique_ptr<dwp_file> ();
3019eac3 12721 }
400174b1
TT
12722
12723 const char *name = bfd_get_filename (dbfd.get ());
12724 std::unique_ptr<struct dwp_file> dwp_file
12725 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12726
0a0f4c01 12727 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12728 dwp_file->elf_sections =
976ca316 12729 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12730 dwp_file->num_sections, asection *);
12731
5bb6e9dd
TT
12732 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12733 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12734 dwp_file.get ());
80626a55 12735
976ca316 12736 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12737
976ca316 12738 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12739
73869dc2 12740 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12741 if (dwp_file->cus && dwp_file->tus
12742 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12743 {
12744 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12745 pretty bizarre. We use pulongest here because that's the established
4d65956b 12746 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12747 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12748 " TU version %s [in DWP file %s]"),
12749 pulongest (dwp_file->cus->version),
d721ba37 12750 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12751 }
08302ed2
DE
12752
12753 if (dwp_file->cus)
12754 dwp_file->version = dwp_file->cus->version;
12755 else if (dwp_file->tus)
12756 dwp_file->version = dwp_file->tus->version;
12757 else
12758 dwp_file->version = 2;
73869dc2 12759
5bb6e9dd
TT
12760 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12761 {
12762 if (dwp_file->version == 2)
12763 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12764 dwp_file.get ());
12765 else
12766 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12767 dwp_file.get ());
12768 }
73869dc2 12769
298e9637
SM
12770 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12771 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12772
6f738b01
SM
12773 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12774 dwarf_read_debug_printf (" %s CUs, %s TUs",
12775 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12776 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12777
12778 return dwp_file;
3019eac3 12779}
c906108c 12780
ab5088bf
DE
12781/* Wrapper around open_and_init_dwp_file, only open it once. */
12782
12783static struct dwp_file *
976ca316 12784get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12785{
976ca316 12786 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12787 {
976ca316
SM
12788 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12789 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12790 }
976ca316 12791 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12792}
12793
80626a55
DE
12794/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12795 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12796 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12797 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12798 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12799
12800 This is called, for example, when wanting to read a variable with a
12801 complex location. Therefore we don't want to do file i/o for every call.
12802 Therefore we don't want to look for a DWO file on every call.
12803 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12804 then we check if we've already seen DWO_NAME, and only THEN do we check
12805 for a DWO file.
12806
1c658ad5 12807 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12808 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12809
3019eac3 12810static struct dwo_unit *
4ab09049 12811lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12812 ULONGEST signature, int is_debug_types)
3019eac3 12813{
976ca316
SM
12814 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12815 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12816 const char *kind = is_debug_types ? "TU" : "CU";
12817 void **dwo_file_slot;
3019eac3 12818 struct dwo_file *dwo_file;
80626a55 12819 struct dwp_file *dwp_file;
cb1df416 12820
6a506a2d
DE
12821 /* First see if there's a DWP file.
12822 If we have a DWP file but didn't find the DWO inside it, don't
12823 look for the original DWO file. It makes gdb behave differently
12824 depending on whether one is debugging in the build tree. */
cf2c3c16 12825
976ca316 12826 dwp_file = get_dwp_file (per_objfile);
80626a55 12827 if (dwp_file != NULL)
cf2c3c16 12828 {
80626a55
DE
12829 const struct dwp_hash_table *dwp_htab =
12830 is_debug_types ? dwp_file->tus : dwp_file->cus;
12831
12832 if (dwp_htab != NULL)
12833 {
12834 struct dwo_unit *dwo_cutu =
976ca316
SM
12835 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12836 is_debug_types);
80626a55
DE
12837
12838 if (dwo_cutu != NULL)
12839 {
6f738b01
SM
12840 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12841 kind, hex_string (signature),
12842 host_address_to_string (dwo_cutu));
12843
80626a55
DE
12844 return dwo_cutu;
12845 }
12846 }
12847 }
6a506a2d 12848 else
80626a55 12849 {
6a506a2d 12850 /* No DWP file, look for the DWO file. */
80626a55 12851
976ca316 12852 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 12853 if (*dwo_file_slot == NULL)
80626a55 12854 {
6a506a2d 12855 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12856 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12857 }
6a506a2d 12858 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12859 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12860
6a506a2d 12861 if (dwo_file != NULL)
19c3d4c9 12862 {
6a506a2d
DE
12863 struct dwo_unit *dwo_cutu = NULL;
12864
12865 if (is_debug_types && dwo_file->tus)
12866 {
12867 struct dwo_unit find_dwo_cutu;
12868
12869 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12870 find_dwo_cutu.signature = signature;
9a3c8263 12871 dwo_cutu
b0b6a987
TT
12872 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12873 &find_dwo_cutu);
6a506a2d 12874 }
33c5cd75 12875 else if (!is_debug_types && dwo_file->cus)
80626a55 12876 {
33c5cd75
DB
12877 struct dwo_unit find_dwo_cutu;
12878
12879 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12880 find_dwo_cutu.signature = signature;
b0b6a987 12881 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12882 &find_dwo_cutu);
6a506a2d
DE
12883 }
12884
12885 if (dwo_cutu != NULL)
12886 {
6f738b01
SM
12887 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12888 kind, dwo_name, hex_string (signature),
12889 host_address_to_string (dwo_cutu));
12890
6a506a2d 12891 return dwo_cutu;
80626a55
DE
12892 }
12893 }
2e276125 12894 }
9cdd5dbd 12895
80626a55
DE
12896 /* We didn't find it. This could mean a dwo_id mismatch, or
12897 someone deleted the DWO/DWP file, or the search path isn't set up
12898 correctly to find the file. */
12899
6f738b01
SM
12900 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12901 kind, dwo_name, hex_string (signature));
3019eac3 12902
6656a72d
DE
12903 /* This is a warning and not a complaint because it can be caused by
12904 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12905 {
12906 /* Print the name of the DWP file if we looked there, helps the user
12907 better diagnose the problem. */
791afaa2 12908 std::string dwp_text;
43942612
DE
12909
12910 if (dwp_file != NULL)
791afaa2
TT
12911 dwp_text = string_printf (" [in DWP file %s]",
12912 lbasename (dwp_file->name));
43942612 12913
9d8780f0 12914 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12915 " [in module %s]"),
4ab09049
SM
12916 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12917 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12918 }
3019eac3 12919 return NULL;
5fb290d7
DJ
12920}
12921
80626a55
DE
12922/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12923 See lookup_dwo_cutu_unit for details. */
12924
12925static struct dwo_unit *
4ab09049 12926lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12927 ULONGEST signature)
12928{
4ab09049
SM
12929 gdb_assert (!cu->per_cu->is_debug_types);
12930
12931 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12932}
12933
12934/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12935 See lookup_dwo_cutu_unit for details. */
12936
12937static struct dwo_unit *
4ab09049 12938lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12939{
4ab09049
SM
12940 gdb_assert (cu->per_cu->is_debug_types);
12941
12942 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12943
12944 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12945}
12946
89e63ee4
DE
12947/* Traversal function for queue_and_load_all_dwo_tus. */
12948
12949static int
12950queue_and_load_dwo_tu (void **slot, void *info)
12951{
12952 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12953 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12954 ULONGEST signature = dwo_unit->signature;
d460f660 12955 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12956
12957 if (sig_type != NULL)
12958 {
89e63ee4
DE
12959 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12960 a real dependency of PER_CU on SIG_TYPE. That is detected later
12961 while processing PER_CU. */
0d305d5c
TT
12962 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12963 cu->language))
12964 load_full_type_unit (sig_type, cu->per_objfile);
12965 cu->per_cu->imported_symtabs_push (sig_type);
89e63ee4
DE
12966 }
12967
12968 return 1;
12969}
12970
1b555f17 12971/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12972 The DWO may have the only definition of the type, though it may not be
12973 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12974 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12975
12976static void
1b555f17 12977queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12978{
12979 struct dwo_unit *dwo_unit;
12980 struct dwo_file *dwo_file;
12981
1b555f17
SM
12982 gdb_assert (cu != nullptr);
12983 gdb_assert (!cu->per_cu->is_debug_types);
12984 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12985
1b555f17 12986 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12987 gdb_assert (dwo_unit != NULL);
12988
12989 dwo_file = dwo_unit->dwo_file;
12990 if (dwo_file->tus != NULL)
1b555f17 12991 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
12992}
12993
3019eac3 12994/* Read in various DIEs. */
348e048f 12995
d389af10 12996/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12997 Inherit only the children of the DW_AT_abstract_origin DIE not being
12998 already referenced by DW_AT_abstract_origin from the children of the
12999 current DIE. */
d389af10
JK
13000
13001static void
13002inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13003{
13004 struct die_info *child_die;
791afaa2 13005 sect_offset *offsetp;
d389af10
JK
13006 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13007 struct die_info *origin_die;
13008 /* Iterator of the ORIGIN_DIE children. */
13009 struct die_info *origin_child_die;
d389af10 13010 struct attribute *attr;
cd02d79d
PA
13011 struct dwarf2_cu *origin_cu;
13012 struct pending **origin_previous_list_in_scope;
d389af10
JK
13013
13014 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13015 if (!attr)
13016 return;
13017
cd02d79d
PA
13018 /* Note that following die references may follow to a die in a
13019 different cu. */
13020
13021 origin_cu = cu;
13022 origin_die = follow_die_ref (die, attr, &origin_cu);
13023
13024 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13025 symbols in. */
13026 origin_previous_list_in_scope = origin_cu->list_in_scope;
13027 origin_cu->list_in_scope = cu->list_in_scope;
13028
edb3359d
DJ
13029 if (die->tag != origin_die->tag
13030 && !(die->tag == DW_TAG_inlined_subroutine
13031 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13032 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13033 sect_offset_str (die->sect_off),
13034 sect_offset_str (origin_die->sect_off));
d389af10 13035
34dc0f95
TBA
13036 /* Find if the concrete and abstract trees are structurally the
13037 same. This is a shallow traversal and it is not bullet-proof;
13038 the compiler can trick the debugger into believing that the trees
13039 are isomorphic, whereas they actually are not. However, the
13040 likelyhood of this happening is pretty low, and a full-fledged
13041 check would be an overkill. */
13042 bool are_isomorphic = true;
13043 die_info *concrete_child = die->child;
13044 die_info *abstract_child = origin_die->child;
13045 while (concrete_child != nullptr || abstract_child != nullptr)
13046 {
13047 if (concrete_child == nullptr
13048 || abstract_child == nullptr
13049 || concrete_child->tag != abstract_child->tag)
13050 {
13051 are_isomorphic = false;
13052 break;
13053 }
13054
13055 concrete_child = concrete_child->sibling;
13056 abstract_child = abstract_child->sibling;
13057 }
13058
13059 /* Walk the origin's children in parallel to the concrete children.
13060 This helps match an origin child in case the debug info misses
13061 DW_AT_abstract_origin attributes. Keep in mind that the abstract
13062 origin tree may not have the same tree structure as the concrete
13063 DIE, though. */
13064 die_info *corresponding_abstract_child
13065 = are_isomorphic ? origin_die->child : nullptr;
13066
791afaa2 13067 std::vector<sect_offset> offsets;
d389af10 13068
3ea89b92
PMR
13069 for (child_die = die->child;
13070 child_die && child_die->tag;
436c571c 13071 child_die = child_die->sibling)
3ea89b92
PMR
13072 {
13073 struct die_info *child_origin_die;
13074 struct dwarf2_cu *child_origin_cu;
13075
13076 /* We are trying to process concrete instance entries:
216f72a1 13077 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13078 it's not relevant to our analysis here. i.e. detecting DIEs that are
13079 present in the abstract instance but not referenced in the concrete
13080 one. */
216f72a1 13081 if (child_die->tag == DW_TAG_call_site
dda83cd7 13082 || child_die->tag == DW_TAG_GNU_call_site)
34dc0f95
TBA
13083 {
13084 if (are_isomorphic)
13085 corresponding_abstract_child
13086 = corresponding_abstract_child->sibling;
13087 continue;
13088 }
3ea89b92 13089
c38f313d
DJ
13090 /* For each CHILD_DIE, find the corresponding child of
13091 ORIGIN_DIE. If there is more than one layer of
13092 DW_AT_abstract_origin, follow them all; there shouldn't be,
13093 but GCC versions at least through 4.4 generate this (GCC PR
13094 40573). */
3ea89b92
PMR
13095 child_origin_die = child_die;
13096 child_origin_cu = cu;
c38f313d
DJ
13097 while (1)
13098 {
cd02d79d
PA
13099 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13100 child_origin_cu);
c38f313d
DJ
13101 if (attr == NULL)
13102 break;
cd02d79d
PA
13103 child_origin_die = follow_die_ref (child_origin_die, attr,
13104 &child_origin_cu);
c38f313d
DJ
13105 }
13106
34dc0f95
TBA
13107 /* If missing DW_AT_abstract_origin, try the corresponding child
13108 of the origin. Clang emits such lexical scopes. */
13109 if (child_origin_die == child_die
13110 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
13111 && are_isomorphic
13112 && child_die->tag == DW_TAG_lexical_block)
13113 child_origin_die = corresponding_abstract_child;
13114
d389af10
JK
13115 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13116 counterpart may exist. */
c38f313d 13117 if (child_origin_die != child_die)
d389af10 13118 {
edb3359d
DJ
13119 if (child_die->tag != child_origin_die->tag
13120 && !(child_die->tag == DW_TAG_inlined_subroutine
13121 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13122 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13123 "different tags"),
9d8780f0
SM
13124 sect_offset_str (child_die->sect_off),
13125 sect_offset_str (child_origin_die->sect_off));
c38f313d 13126 if (child_origin_die->parent != origin_die)
b98664d3 13127 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13128 "different parents"),
9d8780f0
SM
13129 sect_offset_str (child_die->sect_off),
13130 sect_offset_str (child_origin_die->sect_off));
c38f313d 13131 else
791afaa2 13132 offsets.push_back (child_origin_die->sect_off);
d389af10 13133 }
34dc0f95
TBA
13134
13135 if (are_isomorphic)
13136 corresponding_abstract_child = corresponding_abstract_child->sibling;
d389af10 13137 }
791afaa2
TT
13138 std::sort (offsets.begin (), offsets.end ());
13139 sect_offset *offsets_end = offsets.data () + offsets.size ();
13140 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13141 if (offsetp[-1] == *offsetp)
b98664d3 13142 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13143 "to DIE %s as their abstract origin"),
13144 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13145
791afaa2 13146 offsetp = offsets.data ();
d389af10
JK
13147 origin_child_die = origin_die->child;
13148 while (origin_child_die && origin_child_die->tag)
13149 {
13150 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13151 while (offsetp < offsets_end
9c541725 13152 && *offsetp < origin_child_die->sect_off)
d389af10 13153 offsetp++;
b64f50a1 13154 if (offsetp >= offsets_end
9c541725 13155 || *offsetp > origin_child_die->sect_off)
d389af10 13156 {
adde2bff
DE
13157 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13158 Check whether we're already processing ORIGIN_CHILD_DIE.
13159 This can happen with mutually referenced abstract_origins.
13160 PR 16581. */
13161 if (!origin_child_die->in_process)
13162 process_die (origin_child_die, origin_cu);
d389af10 13163 }
436c571c 13164 origin_child_die = origin_child_die->sibling;
d389af10 13165 }
cd02d79d 13166 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13167
13168 if (cu != origin_cu)
13169 compute_delayed_physnames (origin_cu);
d389af10
JK
13170}
13171
c906108c 13172static void
e7c27a73 13173read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13174{
5e22e966 13175 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13176 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13177 struct context_stack *newobj;
c906108c
SS
13178 CORE_ADDR lowpc;
13179 CORE_ADDR highpc;
13180 struct die_info *child_die;
edb3359d 13181 struct attribute *attr, *call_line, *call_file;
15d034d0 13182 const char *name;
e142c38c 13183 CORE_ADDR baseaddr;
801e3a5b 13184 struct block *block;
edb3359d 13185 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13186 std::vector<struct symbol *> template_args;
34eaf542 13187 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13188
13189 if (inlined_func)
13190 {
13191 /* If we do not have call site information, we can't show the
13192 caller of this inlined function. That's too confusing, so
13193 only use the scope for local variables. */
13194 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13195 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13196 if (call_line == NULL || call_file == NULL)
13197 {
13198 read_lexical_block_scope (die, cu);
13199 return;
13200 }
13201 }
c906108c 13202
b3b3bada 13203 baseaddr = objfile->text_section_offset ();
e142c38c 13204
94af9270 13205 name = dwarf2_name (die, cu);
c906108c 13206
e8d05480
JB
13207 /* Ignore functions with missing or empty names. These are actually
13208 illegal according to the DWARF standard. */
13209 if (name == NULL)
13210 {
b98664d3 13211 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13212 sect_offset_str (die->sect_off));
e8d05480
JB
13213 return;
13214 }
13215
13216 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13217 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13218 <= PC_BOUNDS_INVALID)
e8d05480 13219 {
ae4d0c03 13220 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13221 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13222 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13223 "for subprogram DIE at %s"),
13224 sect_offset_str (die->sect_off));
e8d05480
JB
13225 return;
13226 }
c906108c 13227
3e29f34a
MR
13228 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13229 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13230
34eaf542
TT
13231 /* If we have any template arguments, then we must allocate a
13232 different sort of symbol. */
436c571c 13233 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13234 {
13235 if (child_die->tag == DW_TAG_template_type_param
13236 || child_die->tag == DW_TAG_template_value_param)
13237 {
8c14c3a3 13238 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13239 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13240 break;
13241 }
13242 }
13243
702cf3f5 13244 gdb_assert (cu->get_builder () != nullptr);
c24bdb02 13245 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13246 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13247 (struct symbol *) templ_func);
4c2df51b 13248
81873cc8 13249 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13250 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13251 cu->language);
13252
4cecd739
DJ
13253 /* If there is a location expression for DW_AT_frame_base, record
13254 it. */
e142c38c 13255 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13256 if (attr != nullptr)
fe978cb0 13257 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13258
63e43d3a
PMR
13259 /* If there is a location for the static link, record it. */
13260 newobj->static_link = NULL;
13261 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13262 if (attr != nullptr)
63e43d3a 13263 {
224c3ddb
SM
13264 newobj->static_link
13265 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13266 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13267 cu->addr_type ());
63e43d3a
PMR
13268 }
13269
c24bdb02 13270 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13271
639d11d3 13272 if (die->child != NULL)
c906108c 13273 {
639d11d3 13274 child_die = die->child;
c906108c
SS
13275 while (child_die && child_die->tag)
13276 {
34eaf542
TT
13277 if (child_die->tag == DW_TAG_template_type_param
13278 || child_die->tag == DW_TAG_template_value_param)
13279 {
13280 struct symbol *arg = new_symbol (child_die, NULL, cu);
13281
f1078f66 13282 if (arg != NULL)
2f4732b0 13283 template_args.push_back (arg);
34eaf542
TT
13284 }
13285 else
13286 process_die (child_die, cu);
436c571c 13287 child_die = child_die->sibling;
c906108c
SS
13288 }
13289 }
13290
d389af10
JK
13291 inherit_abstract_dies (die, cu);
13292
4a811a97
UW
13293 /* If we have a DW_AT_specification, we might need to import using
13294 directives from the context of the specification DIE. See the
13295 comment in determine_prefix. */
13296 if (cu->language == language_cplus
13297 && dwarf2_attr (die, DW_AT_specification, cu))
13298 {
13299 struct dwarf2_cu *spec_cu = cu;
13300 struct die_info *spec_die = die_specification (die, &spec_cu);
13301
13302 while (spec_die)
13303 {
13304 child_die = spec_die->child;
13305 while (child_die && child_die->tag)
13306 {
13307 if (child_die->tag == DW_TAG_imported_module)
13308 process_die (child_die, spec_cu);
436c571c 13309 child_die = child_die->sibling;
4a811a97
UW
13310 }
13311
13312 /* In some cases, GCC generates specification DIEs that
13313 themselves contain DW_AT_specification attributes. */
13314 spec_die = die_specification (spec_die, &spec_cu);
13315 }
13316 }
13317
c24bdb02 13318 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13319 /* Make a block for the local symbols within. */
c24bdb02 13320 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13321 cstk.static_link, lowpc, highpc);
801e3a5b 13322
df8a16a1 13323 /* For C++, set the block's scope. */
45280282
IB
13324 if ((cu->language == language_cplus
13325 || cu->language == language_fortran
c44af4eb
TT
13326 || cu->language == language_d
13327 || cu->language == language_rust)
4d4ec4e5 13328 && cu->processing_has_namespace_info)
195a3f6c
TT
13329 block_set_scope (block, determine_prefix (die, cu),
13330 &objfile->objfile_obstack);
df8a16a1 13331
801e3a5b
JB
13332 /* If we have address ranges, record them. */
13333 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13334
a60f3166 13335 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13336
34eaf542 13337 /* Attach template arguments to function. */
2f4732b0 13338 if (!template_args.empty ())
34eaf542
TT
13339 {
13340 gdb_assert (templ_func != NULL);
13341
2f4732b0 13342 templ_func->n_template_arguments = template_args.size ();
34eaf542 13343 templ_func->template_arguments
dda83cd7 13344 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13345 templ_func->n_template_arguments);
34eaf542 13346 memcpy (templ_func->template_arguments,
2f4732b0 13347 template_args.data (),
34eaf542 13348 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13349
13350 /* Make sure that the symtab is set on the new symbols. Even
13351 though they don't appear in this symtab directly, other parts
13352 of gdb assume that symbols do, and this is reasonably
13353 true. */
8634679f 13354 for (symbol *sym : template_args)
3e1d3d8c 13355 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13356 }
13357
208d8187
JB
13358 /* In C++, we can have functions nested inside functions (e.g., when
13359 a function declares a class that has methods). This means that
13360 when we finish processing a function scope, we may need to go
13361 back to building a containing block's symbol lists. */
c24bdb02
KS
13362 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13363 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13364
921e78cf
JB
13365 /* If we've finished processing a top-level function, subsequent
13366 symbols go in the file symbol list. */
c24bdb02
KS
13367 if (cu->get_builder ()->outermost_context_p ())
13368 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13369}
13370
13371/* Process all the DIES contained within a lexical block scope. Start
13372 a new scope, process the dies, and then close the scope. */
13373
13374static void
e7c27a73 13375read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13376{
5e22e966 13377 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13378 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13379 CORE_ADDR lowpc, highpc;
13380 struct die_info *child_die;
e142c38c
DJ
13381 CORE_ADDR baseaddr;
13382
b3b3bada 13383 baseaddr = objfile->text_section_offset ();
c906108c
SS
13384
13385 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13386 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13387 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13388 be nasty. Might be easier to properly extend generic blocks to
af34e669 13389 describe ranges. */
e385593e
JK
13390 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13391 {
13392 case PC_BOUNDS_NOT_PRESENT:
13393 /* DW_TAG_lexical_block has no attributes, process its children as if
13394 there was no wrapping by that DW_TAG_lexical_block.
13395 GCC does no longer produces such DWARF since GCC r224161. */
13396 for (child_die = die->child;
13397 child_die != NULL && child_die->tag;
436c571c 13398 child_die = child_die->sibling)
4f7bc5ed
TT
13399 {
13400 /* We might already be processing this DIE. This can happen
13401 in an unusual circumstance -- where a subroutine A
13402 appears lexically in another subroutine B, but A actually
13403 inlines B. The recursion is broken here, rather than in
13404 inherit_abstract_dies, because it seems better to simply
13405 drop concrete children here. */
13406 if (!child_die->in_process)
13407 process_die (child_die, cu);
13408 }
e385593e
JK
13409 return;
13410 case PC_BOUNDS_INVALID:
13411 return;
13412 }
3e29f34a
MR
13413 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13414 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13415
c24bdb02 13416 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13417 if (die->child != NULL)
c906108c 13418 {
639d11d3 13419 child_die = die->child;
c906108c
SS
13420 while (child_die && child_die->tag)
13421 {
e7c27a73 13422 process_die (child_die, cu);
436c571c 13423 child_die = child_die->sibling;
c906108c
SS
13424 }
13425 }
3ea89b92 13426 inherit_abstract_dies (die, cu);
c24bdb02 13427 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13428
c24bdb02
KS
13429 if (*cu->get_builder ()->get_local_symbols () != NULL
13430 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13431 {
801e3a5b 13432 struct block *block
dda83cd7 13433 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13434 cstk.start_addr, highpc);
801e3a5b
JB
13435
13436 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13437 do here, means that recording a parent's ranges entails
13438 walking across all its children's ranges as they appear in
13439 the address map, which is quadratic behavior.
13440
13441 It would be nicer to record the parent's ranges before
13442 traversing its children, simply overriding whatever you find
13443 there. But since we don't even decide whether to create a
13444 block until after we've traversed its children, that's hard
13445 to do. */
801e3a5b 13446 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13447 }
c24bdb02
KS
13448 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13449 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13450}
13451
216f72a1 13452/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13453
13454static void
13455read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13456{
5e22e966 13457 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13458 struct objfile *objfile = per_objfile->objfile;
08feed99 13459 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13460 CORE_ADDR pc, baseaddr;
13461 struct attribute *attr;
13462 struct call_site *call_site, call_site_local;
13463 void **slot;
13464 int nparams;
13465 struct die_info *child_die;
13466
b3b3bada 13467 baseaddr = objfile->text_section_offset ();
96408a79 13468
216f72a1
JK
13469 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13470 if (attr == NULL)
13471 {
13472 /* This was a pre-DWARF-5 GNU extension alias
13473 for DW_AT_call_return_pc. */
13474 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13475 }
96408a79
SA
13476 if (!attr)
13477 {
b98664d3 13478 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13479 "DIE %s [in module %s]"),
13480 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13481 return;
13482 }
95f982e5 13483 pc = attr->as_address () + baseaddr;
3e29f34a 13484 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13485
13486 if (cu->call_site_htab == NULL)
13487 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13488 NULL, &objfile->objfile_obstack,
13489 hashtab_obstack_allocate, NULL);
13490 call_site_local.pc = pc;
13491 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13492 if (*slot != NULL)
13493 {
b98664d3 13494 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13495 "DIE %s [in module %s]"),
13496 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13497 objfile_name (objfile));
96408a79
SA
13498 return;
13499 }
13500
13501 /* Count parameters at the caller. */
13502
13503 nparams = 0;
13504 for (child_die = die->child; child_die && child_die->tag;
436c571c 13505 child_die = child_die->sibling)
96408a79 13506 {
216f72a1 13507 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13508 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13509 {
b98664d3 13510 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13511 "DW_TAG_call_site child DIE %s [in module %s]"),
13512 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13513 objfile_name (objfile));
96408a79
SA
13514 continue;
13515 }
13516
13517 nparams++;
13518 }
13519
224c3ddb
SM
13520 call_site
13521 = ((struct call_site *)
13522 obstack_alloc (&objfile->objfile_obstack,
13523 sizeof (*call_site)
13524 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13525 *slot = call_site;
13526 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13527 call_site->pc = pc;
13528
216f72a1
JK
13529 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13530 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13531 {
13532 struct die_info *func_die;
13533
13534 /* Skip also over DW_TAG_inlined_subroutine. */
13535 for (func_die = die->parent;
13536 func_die && func_die->tag != DW_TAG_subprogram
13537 && func_die->tag != DW_TAG_subroutine_type;
13538 func_die = func_die->parent);
13539
216f72a1
JK
13540 /* DW_AT_call_all_calls is a superset
13541 of DW_AT_call_all_tail_calls. */
96408a79 13542 if (func_die
dda83cd7
SM
13543 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13544 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13545 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13546 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13547 {
13548 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13549 not complete. But keep CALL_SITE for look ups via call_site_htab,
13550 both the initial caller containing the real return address PC and
13551 the final callee containing the current PC of a chain of tail
13552 calls do not need to have the tail call list complete. But any
13553 function candidate for a virtual tail call frame searched via
13554 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13555 determined unambiguously. */
13556 }
13557 else
13558 {
13559 struct type *func_type = NULL;
13560
13561 if (func_die)
13562 func_type = get_die_type (func_die, cu);
13563 if (func_type != NULL)
13564 {
78134374 13565 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13566
13567 /* Enlist this call site to the function. */
13568 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13569 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13570 }
13571 else
b98664d3 13572 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13573 "DIE %s [in module %s]"),
13574 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13575 }
13576 }
13577
216f72a1
JK
13578 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13579 if (attr == NULL)
13580 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13581 if (attr == NULL)
13582 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13583 if (attr == NULL)
216f72a1
JK
13584 {
13585 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13586 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13587 }
96408a79 13588 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13589 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13590 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13591 else if (attr->form_is_block ())
96408a79
SA
13592 {
13593 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13594 struct dwarf_block *block = attr->as_block ();
96408a79 13595
8d749320 13596 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13597 dlbaton->data = block->data;
13598 dlbaton->size = block->size;
a50264ba 13599 dlbaton->per_objfile = per_objfile;
96408a79
SA
13600 dlbaton->per_cu = cu->per_cu;
13601
13602 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13603 }
cd6c91b4 13604 else if (attr->form_is_ref ())
96408a79 13605 {
96408a79
SA
13606 struct dwarf2_cu *target_cu = cu;
13607 struct die_info *target_die;
13608
ac9ec31b 13609 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13610 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13611 if (die_is_declaration (target_die, target_cu))
13612 {
7d45c7c3 13613 const char *target_physname;
9112db09
JK
13614
13615 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13616 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13617 if (target_physname == NULL)
9112db09 13618 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13619 if (target_physname == NULL)
b98664d3 13620 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13621 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 13622 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13623 else
7d455152 13624 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13625 }
13626 else
13627 {
13628 CORE_ADDR lowpc;
13629
13630 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13631 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13632 <= PC_BOUNDS_INVALID)
b98664d3 13633 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13634 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 13635 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13636 else
3e29f34a
MR
13637 {
13638 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13639 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13640 }
96408a79
SA
13641 }
13642 }
13643 else
b98664d3 13644 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13645 "block nor reference, for DIE %s [in module %s]"),
13646 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13647
13648 call_site->per_cu = cu->per_cu;
9f47c707 13649 call_site->per_objfile = per_objfile;
96408a79
SA
13650
13651 for (child_die = die->child;
13652 child_die && child_die->tag;
436c571c 13653 child_die = child_die->sibling)
96408a79 13654 {
96408a79 13655 struct call_site_parameter *parameter;
1788b2d3 13656 struct attribute *loc, *origin;
96408a79 13657
216f72a1 13658 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13659 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13660 {
13661 /* Already printed the complaint above. */
13662 continue;
13663 }
13664
13665 gdb_assert (call_site->parameter_count < nparams);
13666 parameter = &call_site->parameter[call_site->parameter_count];
13667
1788b2d3
JK
13668 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13669 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13670 register is contained in DW_AT_call_value. */
96408a79 13671
24c5c679 13672 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13673 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13674 if (origin == NULL)
13675 {
13676 /* This was a pre-DWARF-5 GNU extension alias
13677 for DW_AT_call_parameter. */
13678 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13679 }
cd6c91b4 13680 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13681 {
1788b2d3 13682 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13683
0826b30a 13684 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13685 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13686 {
13687 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13688 binding can be done only inside one CU. Such referenced DIE
13689 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13690 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13691 "DW_TAG_call_site child DIE %s [in module %s]"),
13692 sect_offset_str (child_die->sect_off),
9c541725 13693 objfile_name (objfile));
d76b7dbc
JK
13694 continue;
13695 }
9c541725
PA
13696 parameter->u.param_cu_off
13697 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13698 }
4fc6c0d5 13699 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13700 {
b98664d3 13701 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13702 "DW_TAG_call_site child DIE %s [in module %s]"),
13703 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13704 continue;
13705 }
24c5c679 13706 else
96408a79 13707 {
9d2246fc
TT
13708 struct dwarf_block *block = loc->as_block ();
13709
24c5c679 13710 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 13711 (block->data, &block->data[block->size]);
24c5c679
JK
13712 if (parameter->u.dwarf_reg != -1)
13713 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
13714 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13715 &block->data[block->size],
24c5c679
JK
13716 &parameter->u.fb_offset))
13717 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13718 else
13719 {
b98664d3 13720 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13721 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13722 "DW_TAG_call_site child DIE %s "
24c5c679 13723 "[in module %s]"),
9d8780f0 13724 sect_offset_str (child_die->sect_off),
9c541725 13725 objfile_name (objfile));
24c5c679
JK
13726 continue;
13727 }
96408a79
SA
13728 }
13729
216f72a1
JK
13730 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13731 if (attr == NULL)
13732 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13733 if (attr == NULL || !attr->form_is_block ())
96408a79 13734 {
b98664d3 13735 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13736 "DW_TAG_call_site child DIE %s [in module %s]"),
13737 sect_offset_str (child_die->sect_off),
9c541725 13738 objfile_name (objfile));
96408a79
SA
13739 continue;
13740 }
9d2246fc
TT
13741
13742 struct dwarf_block *block = attr->as_block ();
13743 parameter->value = block->data;
13744 parameter->value_size = block->size;
96408a79
SA
13745
13746 /* Parameters are not pre-cleared by memset above. */
13747 parameter->data_value = NULL;
13748 parameter->data_value_size = 0;
13749 call_site->parameter_count++;
13750
216f72a1
JK
13751 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13752 if (attr == NULL)
13753 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13754 if (attr != nullptr)
96408a79 13755 {
4fc6c0d5 13756 if (!attr->form_is_block ())
b98664d3 13757 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13758 "DW_TAG_call_site child DIE %s [in module %s]"),
13759 sect_offset_str (child_die->sect_off),
9c541725 13760 objfile_name (objfile));
96408a79
SA
13761 else
13762 {
9d2246fc
TT
13763 block = attr->as_block ();
13764 parameter->data_value = block->data;
13765 parameter->data_value_size = block->size;
96408a79
SA
13766 }
13767 }
13768 }
13769}
13770
71a3c369
TT
13771/* Helper function for read_variable. If DIE represents a virtual
13772 table, then return the type of the concrete object that is
13773 associated with the virtual table. Otherwise, return NULL. */
13774
13775static struct type *
13776rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13777{
13778 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13779 if (attr == NULL)
13780 return NULL;
13781
13782 /* Find the type DIE. */
13783 struct die_info *type_die = NULL;
13784 struct dwarf2_cu *type_cu = cu;
13785
cd6c91b4 13786 if (attr->form_is_ref ())
71a3c369
TT
13787 type_die = follow_die_ref (die, attr, &type_cu);
13788 if (type_die == NULL)
13789 return NULL;
13790
13791 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13792 return NULL;
13793 return die_containing_type (type_die, type_cu);
13794}
13795
13796/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13797
13798static void
13799read_variable (struct die_info *die, struct dwarf2_cu *cu)
13800{
13801 struct rust_vtable_symbol *storage = NULL;
13802
13803 if (cu->language == language_rust)
13804 {
13805 struct type *containing_type = rust_containing_type (die, cu);
13806
13807 if (containing_type != NULL)
13808 {
5e22e966 13809 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13810
8c14c3a3 13811 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13812 storage->concrete_type = containing_type;
cf724bc9 13813 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13814 }
13815 }
13816
e4a62c65
TV
13817 struct symbol *res = new_symbol (die, NULL, cu, storage);
13818 struct attribute *abstract_origin
13819 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13820 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13821 if (res == NULL && loc && abstract_origin)
13822 {
13823 /* We have a variable without a name, but with a location and an abstract
13824 origin. This may be a concrete instance of an abstract variable
13825 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13826 later. */
13827 struct dwarf2_cu *origin_cu = cu;
13828 struct die_info *origin_die
13829 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13830 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13831 per_objfile->per_bfd->abstract_to_concrete
13832 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13833 }
71a3c369
TT
13834}
13835
43988095
JK
13836/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13837 reading .debug_rnglists.
13838 Callback's type should be:
13839 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13840 Return true if the attributes are present and valid, otherwise,
13841 return false. */
13842
13843template <typename Callback>
13844static bool
13845dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 13846 dwarf_tag tag, Callback &&callback)
43988095 13847{
976ca316
SM
13848 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13849 struct objfile *objfile = per_objfile->objfile;
43988095 13850 bfd *obfd = objfile->obfd;
43988095 13851 /* Base address selection entry. */
2b24b6e4 13852 gdb::optional<CORE_ADDR> base;
43988095 13853 const gdb_byte *buffer;
43988095
JK
13854 CORE_ADDR baseaddr;
13855 bool overflow = false;
d0ce17d8
CT
13856 ULONGEST addr_index;
13857 struct dwarf2_section_info *rnglists_section;
43988095 13858
43988095 13859 base = cu->base_address;
d0ce17d8
CT
13860 rnglists_section = cu_debug_rnglists_section (cu, tag);
13861 rnglists_section->read (objfile);
43988095 13862
d0ce17d8 13863 if (offset >= rnglists_section->size)
43988095 13864 {
b98664d3 13865 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13866 offset);
13867 return false;
13868 }
d0ce17d8 13869 buffer = rnglists_section->buffer + offset;
43988095 13870
b3b3bada 13871 baseaddr = objfile->text_section_offset ();
43988095
JK
13872
13873 while (1)
13874 {
7814882a
JK
13875 /* Initialize it due to a false compiler warning. */
13876 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
13877 const gdb_byte *buf_end = (rnglists_section->buffer
13878 + rnglists_section->size);
43988095
JK
13879 unsigned int bytes_read;
13880
13881 if (buffer == buf_end)
13882 {
13883 overflow = true;
13884 break;
13885 }
13886 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13887 switch (rlet)
13888 {
13889 case DW_RLE_end_of_list:
13890 break;
13891 case DW_RLE_base_address:
13892 if (buffer + cu->header.addr_size > buf_end)
13893 {
13894 overflow = true;
13895 break;
13896 }
c8a7a66f 13897 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13898 buffer += bytes_read;
13899 break;
dda83cd7
SM
13900 case DW_RLE_base_addressx:
13901 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13902 buffer += bytes_read;
13903 base = read_addr_index (cu, addr_index);
13904 break;
43988095
JK
13905 case DW_RLE_start_length:
13906 if (buffer + cu->header.addr_size > buf_end)
13907 {
13908 overflow = true;
13909 break;
13910 }
c8a7a66f
TT
13911 range_beginning = cu->header.read_address (obfd, buffer,
13912 &bytes_read);
43988095
JK
13913 buffer += bytes_read;
13914 range_end = (range_beginning
13915 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13916 buffer += bytes_read;
13917 if (buffer > buf_end)
13918 {
13919 overflow = true;
13920 break;
13921 }
13922 break;
d0ce17d8 13923 case DW_RLE_startx_length:
dda83cd7
SM
13924 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13925 buffer += bytes_read;
13926 range_beginning = read_addr_index (cu, addr_index);
13927 if (buffer > buf_end)
13928 {
13929 overflow = true;
13930 break;
13931 }
13932 range_end = (range_beginning
13933 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13934 buffer += bytes_read;
13935 break;
43988095
JK
13936 case DW_RLE_offset_pair:
13937 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13938 buffer += bytes_read;
13939 if (buffer > buf_end)
13940 {
13941 overflow = true;
13942 break;
13943 }
13944 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13945 buffer += bytes_read;
13946 if (buffer > buf_end)
13947 {
13948 overflow = true;
13949 break;
13950 }
13951 break;
13952 case DW_RLE_start_end:
13953 if (buffer + 2 * cu->header.addr_size > buf_end)
13954 {
13955 overflow = true;
13956 break;
13957 }
c8a7a66f
TT
13958 range_beginning = cu->header.read_address (obfd, buffer,
13959 &bytes_read);
43988095 13960 buffer += bytes_read;
c8a7a66f 13961 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13962 buffer += bytes_read;
13963 break;
d0ce17d8 13964 case DW_RLE_startx_endx:
dda83cd7
SM
13965 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13966 buffer += bytes_read;
13967 range_beginning = read_addr_index (cu, addr_index);
13968 if (buffer > buf_end)
13969 {
13970 overflow = true;
13971 break;
13972 }
13973 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13974 buffer += bytes_read;
13975 range_end = read_addr_index (cu, addr_index);
13976 break;
43988095 13977 default:
b98664d3 13978 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13979 return false;
13980 }
13981 if (rlet == DW_RLE_end_of_list || overflow)
13982 break;
13983 if (rlet == DW_RLE_base_address)
13984 continue;
13985
43988095
JK
13986 if (range_beginning > range_end)
13987 {
13988 /* Inverted range entries are invalid. */
b98664d3 13989 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13990 return false;
13991 }
13992
13993 /* Empty range entries have no effect. */
13994 if (range_beginning == range_end)
13995 continue;
13996
d0ce17d8
CT
13997 /* Only DW_RLE_offset_pair needs the base address added. */
13998 if (rlet == DW_RLE_offset_pair)
13999 {
14000 if (!base.has_value ())
14001 {
14002 /* We have no valid base address for the DW_RLE_offset_pair. */
14003 complaint (_("Invalid .debug_rnglists data (no base address for "
14004 "DW_RLE_offset_pair)"));
14005 return false;
14006 }
14007
14008 range_beginning += *base;
14009 range_end += *base;
14010 }
43988095
JK
14011
14012 /* A not-uncommon case of bad debug info.
14013 Don't pollute the addrmap with bad data. */
14014 if (range_beginning + baseaddr == 0
976ca316 14015 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14016 {
b98664d3 14017 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14018 " [in module %s]"), objfile_name (objfile));
14019 continue;
14020 }
14021
14022 callback (range_beginning, range_end);
14023 }
14024
14025 if (overflow)
14026 {
b98664d3 14027 complaint (_("Offset %d is not terminated "
43988095
JK
14028 "for DW_AT_ranges attribute"),
14029 offset);
14030 return false;
14031 }
14032
14033 return true;
14034}
14035
14036/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14037 Callback's type should be:
14038 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14039 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14040
43988095 14041template <typename Callback>
43039443 14042static int
d0ce17d8 14043dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14044 Callback &&callback)
43039443 14045{
5e22e966
SM
14046 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14047 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14048 struct comp_unit_head *cu_header = &cu->header;
14049 bfd *obfd = objfile->obfd;
14050 unsigned int addr_size = cu_header->addr_size;
14051 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14052 /* Base address selection entry. */
2b24b6e4 14053 gdb::optional<CORE_ADDR> base;
43039443 14054 unsigned int dummy;
d521ce57 14055 const gdb_byte *buffer;
ff013f42 14056 CORE_ADDR baseaddr;
43039443 14057
43988095 14058 if (cu_header->version >= 5)
d0ce17d8 14059 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14060
d00adf39 14061 base = cu->base_address;
43039443 14062
5e22e966
SM
14063 per_objfile->per_bfd->ranges.read (objfile);
14064 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14065 {
b98664d3 14066 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14067 offset);
14068 return 0;
14069 }
5e22e966 14070 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14071
b3b3bada 14072 baseaddr = objfile->text_section_offset ();
ff013f42 14073
43039443
JK
14074 while (1)
14075 {
14076 CORE_ADDR range_beginning, range_end;
14077
c8a7a66f 14078 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14079 buffer += addr_size;
c8a7a66f 14080 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14081 buffer += addr_size;
14082 offset += 2 * addr_size;
14083
14084 /* An end of list marker is a pair of zero addresses. */
14085 if (range_beginning == 0 && range_end == 0)
14086 /* Found the end of list entry. */
14087 break;
14088
14089 /* Each base address selection entry is a pair of 2 values.
14090 The first is the largest possible address, the second is
14091 the base address. Check for a base address here. */
14092 if ((range_beginning & mask) == mask)
14093 {
28d2bfb9
AB
14094 /* If we found the largest possible address, then we already
14095 have the base address in range_end. */
14096 base = range_end;
43039443
JK
14097 continue;
14098 }
14099
2b24b6e4 14100 if (!base.has_value ())
43039443
JK
14101 {
14102 /* We have no valid base address for the ranges
14103 data. */
b98664d3 14104 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14105 return 0;
14106 }
14107
9277c30c
UW
14108 if (range_beginning > range_end)
14109 {
14110 /* Inverted range entries are invalid. */
b98664d3 14111 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14112 return 0;
14113 }
14114
14115 /* Empty range entries have no effect. */
14116 if (range_beginning == range_end)
14117 continue;
14118
2b24b6e4
TT
14119 range_beginning += *base;
14120 range_end += *base;
43039443 14121
01093045
DE
14122 /* A not-uncommon case of bad debug info.
14123 Don't pollute the addrmap with bad data. */
14124 if (range_beginning + baseaddr == 0
5e22e966 14125 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14126 {
b98664d3 14127 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14128 " [in module %s]"), objfile_name (objfile));
01093045
DE
14129 continue;
14130 }
14131
5f46c5a5
JK
14132 callback (range_beginning, range_end);
14133 }
14134
14135 return 1;
14136}
14137
14138/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14139 Return 1 if the attributes are present and valid, otherwise, return 0.
efd7398e 14140 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
5f46c5a5
JK
14141
14142static int
14143dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14144 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14145 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14146{
5e22e966 14147 struct objfile *objfile = cu->per_objfile->objfile;
84685904 14148 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 14149 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14150 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14151 int low_set = 0;
14152 CORE_ADDR low = 0;
14153 CORE_ADDR high = 0;
14154 int retval;
14155
d0ce17d8 14156 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14157 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14158 {
9277c30c 14159 if (ranges_pst != NULL)
3e29f34a
MR
14160 {
14161 CORE_ADDR lowpc;
14162 CORE_ADDR highpc;
14163
79748972
TT
14164 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14165 range_beginning + baseaddr)
14166 - baseaddr);
14167 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14168 range_end + baseaddr)
14169 - baseaddr);
84685904 14170 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 14171 lowpc, highpc - 1, ranges_pst);
3e29f34a 14172 }
ff013f42 14173
43039443
JK
14174 /* FIXME: This is recording everything as a low-high
14175 segment of consecutive addresses. We should have a
14176 data structure for discontiguous block ranges
14177 instead. */
14178 if (! low_set)
14179 {
14180 low = range_beginning;
14181 high = range_end;
14182 low_set = 1;
14183 }
14184 else
14185 {
14186 if (range_beginning < low)
14187 low = range_beginning;
14188 if (range_end > high)
14189 high = range_end;
14190 }
5f46c5a5
JK
14191 });
14192 if (!retval)
14193 return 0;
43039443
JK
14194
14195 if (! low_set)
14196 /* If the first entry is an end-of-list marker, the range
14197 describes an empty scope, i.e. no instructions. */
14198 return 0;
14199
14200 if (low_return)
14201 *low_return = low;
14202 if (high_return)
14203 *high_return = high;
14204 return 1;
14205}
14206
3a2b436a
JK
14207/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14208 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14209 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14210
3a2b436a 14211static enum pc_bounds_kind
af34e669 14212dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14213 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14214 dwarf2_psymtab *pst)
c906108c 14215{
976ca316 14216 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14217 struct attribute *attr;
91da1414 14218 struct attribute *attr_high;
af34e669
DJ
14219 CORE_ADDR low = 0;
14220 CORE_ADDR high = 0;
e385593e 14221 enum pc_bounds_kind ret;
c906108c 14222
91da1414
MW
14223 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14224 if (attr_high)
af34e669 14225 {
e142c38c 14226 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14227 if (attr != nullptr)
dda83cd7 14228 {
95f982e5
TT
14229 low = attr->as_address ();
14230 high = attr_high->as_address ();
cd6c91b4 14231 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14232 high += low;
91da1414 14233 }
af34e669
DJ
14234 else
14235 /* Found high w/o low attribute. */
e385593e 14236 return PC_BOUNDS_INVALID;
af34e669
DJ
14237
14238 /* Found consecutive range of addresses. */
3a2b436a 14239 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14240 }
c906108c 14241 else
af34e669 14242 {
e142c38c 14243 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14244 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14245 {
2b0c7f41
SM
14246 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14247 on DWARF version). */
14248 ULONGEST ranges_offset = attr->as_unsigned ();
14249
14250 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14251 this value. */
14252 if (die->tag != DW_TAG_compile_unit)
14253 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14254
af34e669 14255 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14256 .debug_ranges section. */
d0ce17d8
CT
14257 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14258 die->tag))
e385593e 14259 return PC_BOUNDS_INVALID;
43039443 14260 /* Found discontinuous range of addresses. */
3a2b436a 14261 ret = PC_BOUNDS_RANGES;
af34e669 14262 }
e385593e
JK
14263 else
14264 return PC_BOUNDS_NOT_PRESENT;
af34e669 14265 }
c906108c 14266
48fbe735 14267 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14268 if (high <= low)
e385593e 14269 return PC_BOUNDS_INVALID;
c906108c
SS
14270
14271 /* When using the GNU linker, .gnu.linkonce. sections are used to
14272 eliminate duplicate copies of functions and vtables and such.
14273 The linker will arbitrarily choose one and discard the others.
14274 The AT_*_pc values for such functions refer to local labels in
14275 these sections. If the section from that file was discarded, the
14276 labels are not in the output, so the relocs get a value of 0.
14277 If this is a discarded function, mark the pc bounds as invalid,
14278 so that GDB will ignore it. */
976ca316 14279 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14280 return PC_BOUNDS_INVALID;
c906108c
SS
14281
14282 *lowpc = low;
96408a79
SA
14283 if (highpc)
14284 *highpc = high;
af34e669 14285 return ret;
c906108c
SS
14286}
14287
b084d499
JB
14288/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14289 its low and high PC addresses. Do nothing if these addresses could not
14290 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14291 and HIGHPC to the high address if greater than HIGHPC. */
14292
14293static void
14294dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14295 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14296 struct dwarf2_cu *cu)
b084d499
JB
14297{
14298 CORE_ADDR low, high;
14299 struct die_info *child = die->child;
14300
e385593e 14301 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14302 {
325fac50
PA
14303 *lowpc = std::min (*lowpc, low);
14304 *highpc = std::max (*highpc, high);
b084d499
JB
14305 }
14306
14307 /* If the language does not allow nested subprograms (either inside
14308 subprograms or lexical blocks), we're done. */
14309 if (cu->language != language_ada)
14310 return;
6e70227d 14311
b084d499
JB
14312 /* Check all the children of the given DIE. If it contains nested
14313 subprograms, then check their pc bounds. Likewise, we need to
14314 check lexical blocks as well, as they may also contain subprogram
14315 definitions. */
14316 while (child && child->tag)
14317 {
14318 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14319 || child->tag == DW_TAG_lexical_block)
14320 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14321 child = child->sibling;
b084d499
JB
14322 }
14323}
14324
fae299cd
DC
14325/* Get the low and high pc's represented by the scope DIE, and store
14326 them in *LOWPC and *HIGHPC. If the correct values can't be
14327 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14328
14329static void
14330get_scope_pc_bounds (struct die_info *die,
14331 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14332 struct dwarf2_cu *cu)
14333{
14334 CORE_ADDR best_low = (CORE_ADDR) -1;
14335 CORE_ADDR best_high = (CORE_ADDR) 0;
14336 CORE_ADDR current_low, current_high;
14337
3a2b436a 14338 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14339 >= PC_BOUNDS_RANGES)
fae299cd
DC
14340 {
14341 best_low = current_low;
14342 best_high = current_high;
14343 }
14344 else
14345 {
14346 struct die_info *child = die->child;
14347
14348 while (child && child->tag)
14349 {
14350 switch (child->tag) {
14351 case DW_TAG_subprogram:
dda83cd7 14352 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14353 break;
14354 case DW_TAG_namespace:
f55ee35c 14355 case DW_TAG_module:
fae299cd
DC
14356 /* FIXME: carlton/2004-01-16: Should we do this for
14357 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14358 that current GCC's always emit the DIEs corresponding
14359 to definitions of methods of classes as children of a
14360 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14361 the DIEs giving the declarations, which could be
14362 anywhere). But I don't see any reason why the
14363 standards says that they have to be there. */
14364 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14365
14366 if (current_low != ((CORE_ADDR) -1))
14367 {
325fac50
PA
14368 best_low = std::min (best_low, current_low);
14369 best_high = std::max (best_high, current_high);
fae299cd
DC
14370 }
14371 break;
14372 default:
0963b4bd 14373 /* Ignore. */
fae299cd
DC
14374 break;
14375 }
14376
436c571c 14377 child = child->sibling;
fae299cd
DC
14378 }
14379 }
14380
14381 *lowpc = best_low;
14382 *highpc = best_high;
14383}
14384
801e3a5b
JB
14385/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14386 in DIE. */
380bca97 14387
801e3a5b
JB
14388static void
14389dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14390 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14391{
5e22e966 14392 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14393 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14394 struct attribute *attr;
91da1414 14395 struct attribute *attr_high;
801e3a5b 14396
91da1414
MW
14397 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14398 if (attr_high)
801e3a5b 14399 {
801e3a5b 14400 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14401 if (attr != nullptr)
dda83cd7 14402 {
95f982e5
TT
14403 CORE_ADDR low = attr->as_address ();
14404 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14405
cd6c91b4 14406 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14407 high += low;
9a619af0 14408
3e29f34a
MR
14409 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14410 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14411 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14412 }
801e3a5b
JB
14413 }
14414
14415 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14416 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14417 {
2b0c7f41
SM
14418 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14419 on DWARF version). */
14420 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14421
2b0c7f41
SM
14422 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14423 this value. */
14424 if (die->tag != DW_TAG_compile_unit)
14425 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14426
2d5f09ec 14427 std::vector<blockrange> blockvec;
2b0c7f41 14428 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14429 [&] (CORE_ADDR start, CORE_ADDR end)
14430 {
58fdfd2c
JK
14431 start += baseaddr;
14432 end += baseaddr;
5f46c5a5
JK
14433 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14434 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14435 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14436 blockvec.emplace_back (start, end);
5f46c5a5 14437 });
2d5f09ec
KB
14438
14439 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14440 }
14441}
14442
685b1105
JK
14443/* Check whether the producer field indicates either of GCC < 4.6, or the
14444 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14445
685b1105
JK
14446static void
14447check_producer (struct dwarf2_cu *cu)
60d5a603 14448{
38360086 14449 int major, minor;
60d5a603
JK
14450
14451 if (cu->producer == NULL)
14452 {
14453 /* For unknown compilers expect their behavior is DWARF version
14454 compliant.
14455
14456 GCC started to support .debug_types sections by -gdwarf-4 since
14457 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14458 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14459 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14460 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14461 }
b1ffba5a 14462 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14463 {
38360086
MW
14464 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14465 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14466 }
5230b05a 14467 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14468 {
14469 cu->producer_is_icc = true;
14470 cu->producer_is_icc_lt_14 = major < 14;
14471 }
c258c396
JD
14472 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14473 cu->producer_is_codewarrior = true;
685b1105
JK
14474 else
14475 {
14476 /* For other non-GCC compilers, expect their behavior is DWARF version
14477 compliant. */
60d5a603
JK
14478 }
14479
9068261f 14480 cu->checked_producer = true;
685b1105 14481}
ba919b58 14482
685b1105
JK
14483/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14484 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14485 during 4.6.0 experimental. */
14486
9068261f 14487static bool
685b1105
JK
14488producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14489{
14490 if (!cu->checked_producer)
14491 check_producer (cu);
14492
14493 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14494}
14495
c258c396
JD
14496
14497/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14498 with incorrect is_stmt attributes. */
14499
14500static bool
14501producer_is_codewarrior (struct dwarf2_cu *cu)
14502{
14503 if (!cu->checked_producer)
14504 check_producer (cu);
14505
14506 return cu->producer_is_codewarrior;
14507}
14508
bf23a268
TT
14509/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14510 If that attribute is not available, return the appropriate
14511 default. */
60d5a603
JK
14512
14513static enum dwarf_access_attribute
bf23a268 14514dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14515{
bf23a268
TT
14516 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14517 if (attr != nullptr)
14518 {
14519 LONGEST value = attr->constant_value (-1);
14520 if (value == DW_ACCESS_public
14521 || value == DW_ACCESS_protected
14522 || value == DW_ACCESS_private)
14523 return (dwarf_access_attribute) value;
14524 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14525 plongest (value));
14526 }
14527
60d5a603
JK
14528 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14529 {
14530 /* The default DWARF 2 accessibility for members is public, the default
14531 accessibility for inheritance is private. */
14532
14533 if (die->tag != DW_TAG_inheritance)
14534 return DW_ACCESS_public;
14535 else
14536 return DW_ACCESS_private;
14537 }
14538 else
14539 {
14540 /* DWARF 3+ defines the default accessibility a different way. The same
14541 rules apply now for DW_TAG_inheritance as for the members and it only
14542 depends on the container kind. */
14543
14544 if (die->parent->tag == DW_TAG_class_type)
14545 return DW_ACCESS_private;
14546 else
14547 return DW_ACCESS_public;
14548 }
14549}
14550
74ac6d43
TT
14551/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14552 offset. If the attribute was not found return 0, otherwise return
14553 1. If it was found but could not properly be handled, set *OFFSET
14554 to 0. */
14555
14556static int
14557handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14558 LONGEST *offset)
14559{
14560 struct attribute *attr;
14561
14562 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14563 if (attr != NULL)
14564 {
14565 *offset = 0;
14566
14567 /* Note that we do not check for a section offset first here.
14568 This is because DW_AT_data_member_location is new in DWARF 4,
14569 so if we see it, we can assume that a constant form is really
14570 a constant and not a section offset. */
cd6c91b4 14571 if (attr->form_is_constant ())
0826b30a 14572 *offset = attr->constant_value (0);
cd6c91b4 14573 else if (attr->form_is_section_offset ())
74ac6d43 14574 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14575 else if (attr->form_is_block ())
9d2246fc 14576 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14577 else
14578 dwarf2_complex_location_expr_complaint ();
14579
14580 return 1;
14581 }
14582
14583 return 0;
14584}
14585
7d79de9a
TT
14586/* Look for DW_AT_data_member_location and store the results in FIELD. */
14587
14588static void
14589handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14590 struct field *field)
14591{
14592 struct attribute *attr;
14593
14594 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14595 if (attr != NULL)
14596 {
14597 if (attr->form_is_constant ())
14598 {
14599 LONGEST offset = attr->constant_value (0);
14600 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14601 }
14602 else if (attr->form_is_section_offset ())
14603 dwarf2_complex_location_expr_complaint ();
14604 else if (attr->form_is_block ())
14605 {
14606 bool handled;
9d2246fc 14607 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14608 if (handled)
14609 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14610 else
14611 {
5e22e966
SM
14612 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14613 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14614 struct dwarf2_locexpr_baton *dlbaton
14615 = XOBNEW (&objfile->objfile_obstack,
14616 struct dwarf2_locexpr_baton);
9d2246fc
TT
14617 dlbaton->data = attr->as_block ()->data;
14618 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14619 /* When using this baton, we want to compute the address
14620 of the field, not the value. This is why
14621 is_reference is set to false here. */
14622 dlbaton->is_reference = false;
5e22e966 14623 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14624 dlbaton->per_cu = cu->per_cu;
14625
14626 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14627 }
14628 }
14629 else
14630 dwarf2_complex_location_expr_complaint ();
14631 }
14632}
14633
c906108c
SS
14634/* Add an aggregate field to the field list. */
14635
14636static void
107d2387 14637dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14638 struct dwarf2_cu *cu)
6e70227d 14639{
5e22e966 14640 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14641 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14642 struct nextfield *new_field;
14643 struct attribute *attr;
14644 struct field *fp;
15d034d0 14645 const char *fieldname = "";
c906108c 14646
7d0ccb61
DJ
14647 if (die->tag == DW_TAG_inheritance)
14648 {
be2daae6
TT
14649 fip->baseclasses.emplace_back ();
14650 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14651 }
14652 else
14653 {
be2daae6
TT
14654 fip->fields.emplace_back ();
14655 new_field = &fip->fields.back ();
7d0ccb61 14656 }
be2daae6 14657
9c6a1327
TT
14658 new_field->offset = die->sect_off;
14659
bf23a268 14660 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 14661 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 14662 fip->non_public_fields = true;
60d5a603 14663
e142c38c 14664 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14665 if (attr != nullptr)
23dca5c3 14666 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
14667 else
14668 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14669
14670 fp = &new_field->field;
a9a9bd0f 14671
e142c38c 14672 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14673 {
a9a9bd0f 14674 /* Data member other than a C++ static data member. */
6e70227d 14675
c906108c 14676 /* Get type of field. */
5d14b6e5 14677 fp->set_type (die_type (die, cu));
c906108c 14678
d6a843b5 14679 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14680
c906108c 14681 /* Get bit size of field (zero if none). */
e142c38c 14682 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14683 if (attr != nullptr)
c906108c 14684 {
529908cb 14685 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
14686 }
14687 else
14688 {
14689 FIELD_BITSIZE (*fp) = 0;
14690 }
14691
14692 /* Get bit offset of field. */
7d79de9a 14693 handle_data_member_location (die, cu, fp);
e142c38c 14694 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 14695 if (attr != nullptr && attr->form_is_constant ())
c906108c 14696 {
d5a22e77 14697 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14698 {
14699 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
14700 additional bit offset from the MSB of the containing
14701 anonymous object to the MSB of the field. We don't
14702 have to do anything special since we don't need to
14703 know the size of the anonymous object. */
529908cb 14704 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 14705 + attr->constant_value (0)));
c906108c
SS
14706 }
14707 else
14708 {
14709 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
14710 MSB of the anonymous object, subtract off the number of
14711 bits from the MSB of the field to the MSB of the
14712 object, and then subtract off the number of bits of
14713 the field itself. The result is the bit offset of
14714 the LSB of the field. */
c906108c 14715 int anonymous_size;
7c184d33 14716 int bit_offset = attr->constant_value (0);
c906108c 14717
e142c38c 14718 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 14719 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
14720 {
14721 /* The size of the anonymous object containing
14722 the bit field is explicit, so use the
14723 indicated size (in bytes). */
7c184d33 14724 anonymous_size = attr->constant_value (0);
c906108c
SS
14725 }
14726 else
14727 {
14728 /* The size of the anonymous object containing
14729 the bit field must be inferred from the type
14730 attribute of the data member containing the
14731 bit field. */
5d14b6e5 14732 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14733 }
f41f5e61
PA
14734 SET_FIELD_BITPOS (*fp,
14735 (FIELD_BITPOS (*fp)
14736 + anonymous_size * bits_per_byte
14737 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14738 }
14739 }
da5b30da
AA
14740 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14741 if (attr != NULL)
14742 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14743 + attr->constant_value (0)));
c906108c
SS
14744
14745 /* Get name of field. */
39cbfefa
DJ
14746 fieldname = dwarf2_name (die, cu);
14747 if (fieldname == NULL)
14748 fieldname = "";
d8151005
DJ
14749
14750 /* The name is already allocated along with this objfile, so we don't
14751 need to duplicate it for the type. */
14752 fp->name = fieldname;
c906108c
SS
14753
14754 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 14755 pointer or virtual base class pointer) to private. */
e142c38c 14756 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14757 {
d48cc9dd 14758 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 14759 new_field->accessibility = DW_ACCESS_private;
264fc0e2 14760 fip->non_public_fields = true;
c906108c
SS
14761 }
14762 }
a9a9bd0f 14763 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14764 {
a9a9bd0f
DC
14765 /* C++ static member. */
14766
14767 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14768 is a declaration, but all versions of G++ as of this writing
14769 (so through at least 3.2.1) incorrectly generate
14770 DW_TAG_variable tags. */
6e70227d 14771
ff355380 14772 const char *physname;
c906108c 14773
a9a9bd0f 14774 /* Get name of field. */
39cbfefa
DJ
14775 fieldname = dwarf2_name (die, cu);
14776 if (fieldname == NULL)
c906108c
SS
14777 return;
14778
254e6b9e 14779 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14780 if (attr
14781 /* Only create a symbol if this is an external value.
14782 new_symbol checks this and puts the value in the global symbol
14783 table, which we want. If it is not external, new_symbol
14784 will try to put the value in cu->list_in_scope which is wrong. */
14785 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14786 {
14787 /* A static const member, not much different than an enum as far as
14788 we're concerned, except that we can support more types. */
14789 new_symbol (die, NULL, cu);
14790 }
14791
2df3850c 14792 /* Get physical name. */
ff355380 14793 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14794
d8151005
DJ
14795 /* The name is already allocated along with this objfile, so we don't
14796 need to duplicate it for the type. */
14797 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 14798 fp->set_type (die_type (die, cu));
d8151005 14799 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14800 }
14801 else if (die->tag == DW_TAG_inheritance)
14802 {
74ac6d43 14803 /* C++ base class field. */
7d79de9a 14804 handle_data_member_location (die, cu, fp);
c906108c 14805 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
14806 fp->set_type (die_type (die, cu));
14807 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 14808 }
2ddeaf8a
TT
14809 else
14810 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14811}
14812
883fd55a
KS
14813/* Can the type given by DIE define another type? */
14814
14815static bool
14816type_can_define_types (const struct die_info *die)
14817{
14818 switch (die->tag)
14819 {
14820 case DW_TAG_typedef:
14821 case DW_TAG_class_type:
14822 case DW_TAG_structure_type:
14823 case DW_TAG_union_type:
14824 case DW_TAG_enumeration_type:
14825 return true;
14826
14827 default:
14828 return false;
14829 }
14830}
14831
14832/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14833
14834static void
883fd55a
KS
14835dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14836 struct dwarf2_cu *cu)
6e70227d 14837{
be2daae6
TT
14838 struct decl_field fp;
14839 memset (&fp, 0, sizeof (fp));
98751a41 14840
883fd55a 14841 gdb_assert (type_can_define_types (die));
98751a41 14842
883fd55a 14843 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14844 fp.name = dwarf2_name (die, cu);
14845 fp.type = read_type_die (die, cu);
98751a41 14846
c191a687 14847 /* Save accessibility. */
bf23a268 14848 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
14849 switch (accessibility)
14850 {
14851 case DW_ACCESS_public:
14852 /* The assumed value if neither private nor protected. */
14853 break;
14854 case DW_ACCESS_private:
be2daae6 14855 fp.is_private = 1;
c191a687
KS
14856 break;
14857 case DW_ACCESS_protected:
be2daae6 14858 fp.is_protected = 1;
c191a687 14859 break;
c191a687
KS
14860 }
14861
883fd55a 14862 if (die->tag == DW_TAG_typedef)
be2daae6 14863 fip->typedef_field_list.push_back (fp);
883fd55a 14864 else
be2daae6 14865 fip->nested_types_list.push_back (fp);
98751a41
JK
14866}
14867
9c6a1327
TT
14868/* A convenience typedef that's used when finding the discriminant
14869 field for a variant part. */
1b95cdb7
SM
14870typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14871 offset_map_type;
9c6a1327
TT
14872
14873/* Compute the discriminant range for a given variant. OBSTACK is
14874 where the results will be stored. VARIANT is the variant to
14875 process. IS_UNSIGNED indicates whether the discriminant is signed
14876 or unsigned. */
14877
14878static const gdb::array_view<discriminant_range>
14879convert_variant_range (struct obstack *obstack, const variant_field &variant,
14880 bool is_unsigned)
14881{
14882 std::vector<discriminant_range> ranges;
14883
14884 if (variant.default_branch)
14885 return {};
14886
14887 if (variant.discr_list_data == nullptr)
14888 {
14889 discriminant_range r
14890 = {variant.discriminant_value, variant.discriminant_value};
14891 ranges.push_back (r);
14892 }
14893 else
14894 {
14895 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14896 variant.discr_list_data->size);
14897 while (!data.empty ())
14898 {
14899 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14900 {
14901 complaint (_("invalid discriminant marker: %d"), data[0]);
14902 break;
14903 }
14904 bool is_range = data[0] == DW_DSC_range;
14905 data = data.slice (1);
14906
14907 ULONGEST low, high;
14908 unsigned int bytes_read;
14909
14910 if (data.empty ())
14911 {
14912 complaint (_("DW_AT_discr_list missing low value"));
14913 break;
14914 }
14915 if (is_unsigned)
14916 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14917 else
14918 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14919 &bytes_read);
14920 data = data.slice (bytes_read);
14921
14922 if (is_range)
14923 {
14924 if (data.empty ())
14925 {
14926 complaint (_("DW_AT_discr_list missing high value"));
14927 break;
14928 }
14929 if (is_unsigned)
14930 high = read_unsigned_leb128 (nullptr, data.data (),
14931 &bytes_read);
14932 else
14933 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14934 &bytes_read);
14935 data = data.slice (bytes_read);
14936 }
14937 else
14938 high = low;
14939
14940 ranges.push_back ({ low, high });
14941 }
14942 }
14943
14944 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14945 ranges.size ());
14946 std::copy (ranges.begin (), ranges.end (), result);
14947 return gdb::array_view<discriminant_range> (result, ranges.size ());
14948}
14949
14950static const gdb::array_view<variant_part> create_variant_parts
14951 (struct obstack *obstack,
14952 const offset_map_type &offset_map,
14953 struct field_info *fi,
14954 const std::vector<variant_part_builder> &variant_parts);
14955
14956/* Fill in a "struct variant" for a given variant field. RESULT is
14957 the variant to fill in. OBSTACK is where any needed allocations
14958 will be done. OFFSET_MAP holds the mapping from section offsets to
14959 fields for the type. FI describes the fields of the type we're
14960 processing. FIELD is the variant field we're converting. */
14961
14962static void
14963create_one_variant (variant &result, struct obstack *obstack,
14964 const offset_map_type &offset_map,
14965 struct field_info *fi, const variant_field &field)
14966{
14967 result.discriminants = convert_variant_range (obstack, field, false);
14968 result.first_field = field.first_field + fi->baseclasses.size ();
14969 result.last_field = field.last_field + fi->baseclasses.size ();
14970 result.parts = create_variant_parts (obstack, offset_map, fi,
14971 field.variant_parts);
14972}
14973
14974/* Fill in a "struct variant_part" for a given variant part. RESULT
14975 is the variant part to fill in. OBSTACK is where any needed
14976 allocations will be done. OFFSET_MAP holds the mapping from
14977 section offsets to fields for the type. FI describes the fields of
14978 the type we're processing. BUILDER is the variant part to be
14979 converted. */
14980
14981static void
14982create_one_variant_part (variant_part &result,
14983 struct obstack *obstack,
14984 const offset_map_type &offset_map,
14985 struct field_info *fi,
14986 const variant_part_builder &builder)
14987{
14988 auto iter = offset_map.find (builder.discriminant_offset);
14989 if (iter == offset_map.end ())
14990 {
14991 result.discriminant_index = -1;
14992 /* Doesn't matter. */
14993 result.is_unsigned = false;
14994 }
14995 else
14996 {
14997 result.discriminant_index = iter->second;
14998 result.is_unsigned
c6d940a9 14999 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15000 }
15001
15002 size_t n = builder.variants.size ();
15003 variant *output = new (obstack) variant[n];
15004 for (size_t i = 0; i < n; ++i)
15005 create_one_variant (output[i], obstack, offset_map, fi,
15006 builder.variants[i]);
15007
15008 result.variants = gdb::array_view<variant> (output, n);
15009}
15010
15011/* Create a vector of variant parts that can be attached to a type.
15012 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15013 holds the mapping from section offsets to fields for the type. FI
15014 describes the fields of the type we're processing. VARIANT_PARTS
15015 is the vector to convert. */
15016
15017static const gdb::array_view<variant_part>
15018create_variant_parts (struct obstack *obstack,
15019 const offset_map_type &offset_map,
15020 struct field_info *fi,
15021 const std::vector<variant_part_builder> &variant_parts)
15022{
15023 if (variant_parts.empty ())
15024 return {};
15025
15026 size_t n = variant_parts.size ();
15027 variant_part *result = new (obstack) variant_part[n];
15028 for (size_t i = 0; i < n; ++i)
15029 create_one_variant_part (result[i], obstack, offset_map, fi,
15030 variant_parts[i]);
15031
15032 return gdb::array_view<variant_part> (result, n);
15033}
15034
15035/* Compute the variant part vector for FIP, attaching it to TYPE when
15036 done. */
15037
15038static void
15039add_variant_property (struct field_info *fip, struct type *type,
15040 struct dwarf2_cu *cu)
15041{
15042 /* Map section offsets of fields to their field index. Note the
15043 field index here does not take the number of baseclasses into
15044 account. */
15045 offset_map_type offset_map;
15046 for (int i = 0; i < fip->fields.size (); ++i)
15047 offset_map[fip->fields[i].offset] = i;
15048
5e22e966 15049 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15050 gdb::array_view<variant_part> parts
15051 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15052 fip->variant_parts);
15053
15054 struct dynamic_prop prop;
8c2e4e06
SM
15055 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15056 obstack_copy (&objfile->objfile_obstack, &parts,
15057 sizeof (parts)));
9c6a1327 15058
5c54719c 15059 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15060}
15061
c906108c
SS
15062/* Create the vector of fields, and attach it to the type. */
15063
15064static void
fba45db2 15065dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15066 struct dwarf2_cu *cu)
c906108c 15067{
317f7127 15068 int nfields = fip->nfields ();
c906108c
SS
15069
15070 /* Record the field count, allocate space for the array of fields,
15071 and create blank accessibility bitfields if necessary. */
5e33d5f4 15072 type->set_num_fields (nfields);
3cabb6b0
SM
15073 type->set_fields
15074 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15075
b4ba55a1 15076 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15077 {
15078 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15079
15080 TYPE_FIELD_PRIVATE_BITS (type) =
15081 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15082 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15083
15084 TYPE_FIELD_PROTECTED_BITS (type) =
15085 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15086 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15087
774b6a14
TT
15088 TYPE_FIELD_IGNORE_BITS (type) =
15089 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15090 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15091 }
15092
15093 /* If the type has baseclasses, allocate and clear a bit vector for
15094 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15095 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15096 {
be2daae6 15097 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15098 unsigned char *pointer;
c906108c
SS
15099
15100 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15101 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15102 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15103 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15104 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15105 }
15106
9c6a1327
TT
15107 if (!fip->variant_parts.empty ())
15108 add_variant_property (fip, type, cu);
2ddeaf8a 15109
be2daae6
TT
15110 /* Copy the saved-up fields into the field vector. */
15111 for (int i = 0; i < nfields; ++i)
c906108c 15112 {
be2daae6
TT
15113 struct nextfield &field
15114 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15115 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15116
ceacbf6e 15117 type->field (i) = field.field;
be2daae6 15118 switch (field.accessibility)
c906108c 15119 {
c5aa993b 15120 case DW_ACCESS_private:
b4ba55a1 15121 if (cu->language != language_ada)
be2daae6 15122 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15123 break;
c906108c 15124
c5aa993b 15125 case DW_ACCESS_protected:
b4ba55a1 15126 if (cu->language != language_ada)
be2daae6 15127 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15128 break;
c906108c 15129
c5aa993b
JM
15130 case DW_ACCESS_public:
15131 break;
c906108c 15132
c5aa993b
JM
15133 default:
15134 /* Unknown accessibility. Complain and treat it as public. */
15135 {
b98664d3 15136 complaint (_("unsupported accessibility %d"),
be2daae6 15137 field.accessibility);
c5aa993b
JM
15138 }
15139 break;
c906108c 15140 }
be2daae6 15141 if (i < fip->baseclasses.size ())
c906108c 15142 {
be2daae6 15143 switch (field.virtuality)
c906108c 15144 {
c5aa993b
JM
15145 case DW_VIRTUALITY_virtual:
15146 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15147 if (cu->language == language_ada)
a73c6dcd 15148 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15149 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15150 break;
c906108c
SS
15151 }
15152 }
c906108c
SS
15153 }
15154}
15155
7d27a96d
TT
15156/* Return true if this member function is a constructor, false
15157 otherwise. */
15158
15159static int
15160dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15161{
15162 const char *fieldname;
fe978cb0 15163 const char *type_name;
7d27a96d
TT
15164 int len;
15165
15166 if (die->parent == NULL)
15167 return 0;
15168
15169 if (die->parent->tag != DW_TAG_structure_type
15170 && die->parent->tag != DW_TAG_union_type
15171 && die->parent->tag != DW_TAG_class_type)
15172 return 0;
15173
15174 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15175 type_name = dwarf2_name (die->parent, cu);
15176 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15177 return 0;
15178
15179 len = strlen (fieldname);
fe978cb0
PA
15180 return (strncmp (fieldname, type_name, len) == 0
15181 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15182}
15183
c906108c
SS
15184/* Add a member function to the proper fieldlist. */
15185
15186static void
107d2387 15187dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15188 struct type *type, struct dwarf2_cu *cu)
c906108c 15189{
5e22e966 15190 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15191 struct attribute *attr;
c906108c 15192 int i;
be2daae6 15193 struct fnfieldlist *flp = nullptr;
c906108c 15194 struct fn_field *fnp;
15d034d0 15195 const char *fieldname;
f792889a 15196 struct type *this_type;
c906108c 15197
b4ba55a1 15198 if (cu->language == language_ada)
a73c6dcd 15199 error (_("unexpected member function in Ada type"));
b4ba55a1 15200
2df3850c 15201 /* Get name of member function. */
39cbfefa
DJ
15202 fieldname = dwarf2_name (die, cu);
15203 if (fieldname == NULL)
2df3850c 15204 return;
c906108c 15205
c906108c 15206 /* Look up member function name in fieldlist. */
be2daae6 15207 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15208 {
27bfe10e 15209 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15210 {
15211 flp = &fip->fnfieldlists[i];
15212 break;
15213 }
c906108c
SS
15214 }
15215
be2daae6
TT
15216 /* Create a new fnfieldlist if necessary. */
15217 if (flp == nullptr)
c906108c 15218 {
be2daae6
TT
15219 fip->fnfieldlists.emplace_back ();
15220 flp = &fip->fnfieldlists.back ();
c906108c 15221 flp->name = fieldname;
be2daae6 15222 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15223 }
15224
be2daae6
TT
15225 /* Create a new member function field and add it to the vector of
15226 fnfieldlists. */
15227 flp->fnfields.emplace_back ();
15228 fnp = &flp->fnfields.back ();
3da10d80
KS
15229
15230 /* Delay processing of the physname until later. */
9c37b5ae 15231 if (cu->language == language_cplus)
be2daae6
TT
15232 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15233 die, cu);
3da10d80
KS
15234 else
15235 {
1d06ead6 15236 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15237 fnp->physname = physname ? physname : "";
15238 }
15239
c906108c 15240 fnp->type = alloc_type (objfile);
f792889a 15241 this_type = read_type_die (die, cu);
78134374 15242 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15243 {
1f704f76 15244 int nparams = this_type->num_fields ();
c906108c 15245
f792889a 15246 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15247 of the method itself (TYPE_CODE_METHOD). */
15248 smash_to_method_type (fnp->type, type,
f792889a 15249 TYPE_TARGET_TYPE (this_type),
80fc5e77 15250 this_type->fields (),
1f704f76 15251 this_type->num_fields (),
a409645d 15252 this_type->has_varargs ());
c906108c
SS
15253
15254 /* Handle static member functions.
dda83cd7
SM
15255 Dwarf2 has no clean way to discern C++ static and non-static
15256 member functions. G++ helps GDB by marking the first
15257 parameter for non-static member functions (which is the this
15258 pointer) as artificial. We obtain this information from
15259 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15260 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15261 fnp->voffset = VOFFSET_STATIC;
15262 }
15263 else
b98664d3 15264 complaint (_("member function type missing for '%s'"),
3da10d80 15265 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15266
15267 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15268 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15269 fnp->fcontext = die_containing_type (die, cu);
c906108c 15270
3e43a32a
MS
15271 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15272 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15273
15274 /* Get accessibility. */
bf23a268 15275 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15276 switch (accessibility)
c906108c 15277 {
60d5a603
JK
15278 case DW_ACCESS_private:
15279 fnp->is_private = 1;
15280 break;
15281 case DW_ACCESS_protected:
15282 fnp->is_protected = 1;
15283 break;
c906108c
SS
15284 }
15285
b02dede2 15286 /* Check for artificial methods. */
e142c38c 15287 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15288 if (attr && attr->as_boolean ())
b02dede2
DJ
15289 fnp->is_artificial = 1;
15290
e35000a7
TBA
15291 /* Check for defaulted methods. */
15292 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15293 if (attr != nullptr)
15294 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15295
15296 /* Check for deleted methods. */
15297 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15298 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15299 fnp->is_deleted = 1;
15300
7d27a96d
TT
15301 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15302
0d564a31 15303 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15304 function. For older versions of GCC, this is an offset in the
15305 appropriate virtual table, as specified by DW_AT_containing_type.
15306 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15307 to the object address. */
15308
e142c38c 15309 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15310 if (attr != nullptr)
8e19ed76 15311 {
9d2246fc 15312 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15313 {
9d2246fc
TT
15314 struct dwarf_block *block = attr->as_block ();
15315
15316 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15317 {
15318 /* Old-style GCC. */
9d2246fc 15319 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15320 }
9d2246fc
TT
15321 else if (block->data[0] == DW_OP_deref
15322 || (block->size > 1
15323 && block->data[0] == DW_OP_deref_size
15324 && block->data[1] == cu->header.addr_size))
aec5aa8b 15325 {
9d2246fc 15326 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15327 if ((fnp->voffset % cu->header.addr_size) != 0)
15328 dwarf2_complex_location_expr_complaint ();
15329 else
15330 fnp->voffset /= cu->header.addr_size;
15331 fnp->voffset += 2;
15332 }
15333 else
15334 dwarf2_complex_location_expr_complaint ();
15335
15336 if (!fnp->fcontext)
7e993ebf
KS
15337 {
15338 /* If there is no `this' field and no DW_AT_containing_type,
15339 we cannot actually find a base class context for the
15340 vtable! */
1f704f76 15341 if (this_type->num_fields () == 0
7e993ebf
KS
15342 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15343 {
b98664d3 15344 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15345 "function \"%s\" (offset %s)"),
15346 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15347 }
15348 else
15349 {
15350 fnp->fcontext
940da03e 15351 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15352 }
15353 }
aec5aa8b 15354 }
cd6c91b4 15355 else if (attr->form_is_section_offset ())
dda83cd7 15356 {
4d3c2250 15357 dwarf2_complex_location_expr_complaint ();
dda83cd7 15358 }
8e19ed76 15359 else
dda83cd7 15360 {
4d3c2250
KB
15361 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15362 fieldname);
dda83cd7 15363 }
0d564a31 15364 }
d48cc9dd
DJ
15365 else
15366 {
15367 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15368 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15369 {
15370 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15371 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15372 "but the vtable offset is not specified"),
9d8780f0 15373 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15374 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15375 TYPE_CPLUS_DYNAMIC (type) = 1;
15376 }
15377 }
c906108c
SS
15378}
15379
15380/* Create the vector of member function fields, and attach it to the type. */
15381
15382static void
fba45db2 15383dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15384 struct dwarf2_cu *cu)
c906108c 15385{
b4ba55a1 15386 if (cu->language == language_ada)
a73c6dcd 15387 error (_("unexpected member functions in Ada type"));
b4ba55a1 15388
c906108c
SS
15389 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15390 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15391 TYPE_ALLOC (type,
15392 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15393
be2daae6 15394 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15395 {
be2daae6 15396 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15397 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15398
be2daae6
TT
15399 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15400 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15401 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15402 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15403
15404 for (int k = 0; k < nf.fnfields.size (); ++k)
15405 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15406 }
15407
be2daae6 15408 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15409}
15410
1168df01
JB
15411/* Returns non-zero if NAME is the name of a vtable member in CU's
15412 language, zero otherwise. */
15413static int
15414is_vtable_name (const char *name, struct dwarf2_cu *cu)
15415{
15416 static const char vptr[] = "_vptr";
15417
9c37b5ae
TT
15418 /* Look for the C++ form of the vtable. */
15419 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15420 return 1;
15421
15422 return 0;
15423}
15424
c0dd20ea 15425/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15426 functions, with the ABI-specified layout. If TYPE describes
15427 such a structure, smash it into a member function type.
61049d3b
DJ
15428
15429 GCC shouldn't do this; it should just output pointer to member DIEs.
15430 This is GCC PR debug/28767. */
c0dd20ea 15431
0b92b5bb
TT
15432static void
15433quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15434{
09e2d7c7 15435 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15436
15437 /* Check for a structure with no name and two children. */
1f704f76 15438 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15439 return;
c0dd20ea
DJ
15440
15441 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15442 if (TYPE_FIELD_NAME (type, 0) == NULL
15443 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15444 || TYPE_FIELD_NAME (type, 1) == NULL
15445 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15446 return;
c0dd20ea
DJ
15447
15448 /* Find the type of the method. */
940da03e 15449 pfn_type = type->field (0).type ();
c0dd20ea 15450 if (pfn_type == NULL
78134374
SM
15451 || pfn_type->code () != TYPE_CODE_PTR
15452 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15453 return;
c0dd20ea
DJ
15454
15455 /* Look for the "this" argument. */
15456 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15457 if (pfn_type->num_fields () == 0
940da03e
SM
15458 /* || pfn_type->field (0).type () == NULL */
15459 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15460 return;
c0dd20ea 15461
940da03e 15462 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15463 new_type = alloc_type (objfile);
09e2d7c7 15464 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15465 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15466 pfn_type->has_varargs ());
0b92b5bb 15467 smash_to_methodptr_type (type, new_type);
c0dd20ea 15468}
1168df01 15469
e26624c6
TT
15470/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15471 requires rewriting, then copy it and return the updated copy.
15472 Otherwise return nullptr. */
15473
15474static struct type *
15475rewrite_array_type (struct type *type)
15476{
15477 if (type->code () != TYPE_CODE_ARRAY)
15478 return nullptr;
15479
15480 struct type *index_type = type->index_type ();
15481 range_bounds *current_bounds = index_type->bounds ();
15482
15483 /* Handle multi-dimensional arrays. */
15484 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15485 if (new_target == nullptr)
15486 {
15487 /* Maybe we don't need to rewrite this array. */
15488 if (current_bounds->low.kind () == PROP_CONST
15489 && current_bounds->high.kind () == PROP_CONST)
15490 return nullptr;
15491 }
15492
15493 /* Either the target type was rewritten, or the bounds have to be
15494 updated. Either way we want to copy the type and update
15495 everything. */
15496 struct type *copy = copy_type (type);
15497 int nfields = copy->num_fields ();
15498 field *new_fields
15499 = ((struct field *) TYPE_ZALLOC (copy,
15500 nfields * sizeof (struct field)));
15501 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15502 copy->set_fields (new_fields);
15503 if (new_target != nullptr)
15504 TYPE_TARGET_TYPE (copy) = new_target;
15505
15506 struct type *index_copy = copy_type (index_type);
15507 range_bounds *bounds
15508 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15509 sizeof (range_bounds));
15510 *bounds = *current_bounds;
15511 bounds->low.set_const_val (1);
15512 bounds->high.set_const_val (0);
15513 index_copy->set_bounds (bounds);
15514 copy->set_index_type (index_copy);
15515
15516 return copy;
15517}
15518
57567375
TT
15519/* While some versions of GCC will generate complicated DWARF for an
15520 array (see quirk_ada_thick_pointer), more recent versions were
15521 modified to emit an explicit thick pointer structure. However, in
15522 this case, the array still has DWARF expressions for its ranges,
15523 and these must be ignored. */
15524
15525static void
15526quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15527 struct type *type)
15528{
15529 gdb_assert (cu->language == language_ada);
15530
15531 /* Check for a structure with two children. */
15532 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15533 return;
15534
15535 /* Check for P_ARRAY and P_BOUNDS members. */
15536 if (TYPE_FIELD_NAME (type, 0) == NULL
15537 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15538 || TYPE_FIELD_NAME (type, 1) == NULL
15539 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15540 return;
15541
15542 /* Make sure we're looking at a pointer to an array. */
15543 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15544 return;
57567375 15545
e26624c6
TT
15546 /* The Ada code already knows how to handle these types, so all that
15547 we need to do is turn the bounds into static bounds. However, we
15548 don't want to rewrite existing array or index types in-place,
15549 because those may be referenced in other contexts where this
15550 rewriting is undesirable. */
15551 struct type *new_ary_type
15552 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15553 if (new_ary_type != nullptr)
15554 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15555}
15556
2b4424c3
TT
15557/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15558 appropriate error checking and issuing complaints if there is a
15559 problem. */
15560
15561static ULONGEST
15562get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15563{
15564 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15565
15566 if (attr == nullptr)
15567 return 0;
15568
cd6c91b4 15569 if (!attr->form_is_constant ())
2b4424c3 15570 {
b98664d3 15571 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15572 " - DIE at %s [in module %s]"),
15573 sect_offset_str (die->sect_off),
5e22e966 15574 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15575 return 0;
15576 }
15577
529908cb
TT
15578 LONGEST val = attr->constant_value (0);
15579 if (val < 0)
2b4424c3 15580 {
529908cb
TT
15581 complaint (_("DW_AT_alignment value must not be negative"
15582 " - DIE at %s [in module %s]"),
15583 sect_offset_str (die->sect_off),
15584 objfile_name (cu->per_objfile->objfile));
15585 return 0;
2b4424c3 15586 }
529908cb 15587 ULONGEST align = val;
2b4424c3
TT
15588
15589 if (align == 0)
15590 {
b98664d3 15591 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15592 " - DIE at %s [in module %s]"),
15593 sect_offset_str (die->sect_off),
5e22e966 15594 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15595 return 0;
15596 }
15597 if ((align & (align - 1)) != 0)
15598 {
b98664d3 15599 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15600 " - DIE at %s [in module %s]"),
15601 sect_offset_str (die->sect_off),
5e22e966 15602 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15603 return 0;
15604 }
15605
15606 return align;
15607}
15608
15609/* If the DIE has a DW_AT_alignment attribute, use its value to set
15610 the alignment for TYPE. */
15611
15612static void
15613maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15614 struct type *type)
15615{
15616 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15617 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15618 " - DIE at %s [in module %s]"),
15619 sect_offset_str (die->sect_off),
5e22e966 15620 objfile_name (cu->per_objfile->objfile));
2b4424c3 15621}
685b1105 15622
e35000a7
TBA
15623/* Check if the given VALUE is a valid enum dwarf_calling_convention
15624 constant for a type, according to DWARF5 spec, Table 5.5. */
15625
15626static bool
15627is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15628{
15629 switch (value)
15630 {
15631 case DW_CC_normal:
15632 case DW_CC_pass_by_reference:
15633 case DW_CC_pass_by_value:
15634 return true;
15635
15636 default:
15637 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15638 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15639 return false;
15640 }
15641}
15642
d0922fcf
TBA
15643/* Check if the given VALUE is a valid enum dwarf_calling_convention
15644 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15645 also according to GNU-specific values (see include/dwarf2.h). */
15646
15647static bool
15648is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15649{
15650 switch (value)
15651 {
15652 case DW_CC_normal:
15653 case DW_CC_program:
15654 case DW_CC_nocall:
15655 return true;
15656
15657 case DW_CC_GNU_renesas_sh:
15658 case DW_CC_GNU_borland_fastcall_i386:
15659 case DW_CC_GDB_IBM_OpenCL:
15660 return true;
15661
15662 default:
15663 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15664 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15665 return false;
15666 }
15667}
15668
c906108c 15669/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15670 (definition) to create a type for the structure or union. Fill in
15671 the type's name and general properties; the members will not be
83655187
DE
15672 processed until process_structure_scope. A symbol table entry for
15673 the type will also not be done until process_structure_scope (assuming
15674 the type has a name).
c906108c 15675
c767944b
DJ
15676 NOTE: we need to call these functions regardless of whether or not the
15677 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15678 structure or union. This gets the type entered into our set of
83655187 15679 user defined types. */
c906108c 15680
f792889a 15681static struct type *
134d01f1 15682read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15683{
5e22e966 15684 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15685 struct type *type;
15686 struct attribute *attr;
15d034d0 15687 const char *name;
c906108c 15688
348e048f
DE
15689 /* If the definition of this type lives in .debug_types, read that type.
15690 Don't follow DW_AT_specification though, that will take us back up
15691 the chain and we want to go down. */
052c8bb8 15692 attr = die->attr (DW_AT_signature);
435d3d88 15693 if (attr != nullptr)
348e048f 15694 {
ac9ec31b 15695 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15696
ac9ec31b 15697 /* The type's CU may not be the same as CU.
02142a6c 15698 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15699 return set_die_type (die, type, cu);
15700 }
15701
c0dd20ea 15702 type = alloc_type (objfile);
c906108c 15703 INIT_CPLUS_SPECIFIC (type);
93311388 15704
39cbfefa
DJ
15705 name = dwarf2_name (die, cu);
15706 if (name != NULL)
c906108c 15707 {
987504bb 15708 if (cu->language == language_cplus
c44af4eb
TT
15709 || cu->language == language_d
15710 || cu->language == language_rust)
63d06c5c 15711 {
15d034d0 15712 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15713
15714 /* dwarf2_full_name might have already finished building the DIE's
15715 type. If so, there is no need to continue. */
15716 if (get_die_type (die, cu) != NULL)
15717 return get_die_type (die, cu);
15718
d0e39ea2 15719 type->set_name (full_name);
63d06c5c
DC
15720 }
15721 else
15722 {
d8151005
DJ
15723 /* The name is already allocated along with this objfile, so
15724 we don't need to duplicate it for the type. */
d0e39ea2 15725 type->set_name (name);
63d06c5c 15726 }
c906108c
SS
15727 }
15728
15729 if (die->tag == DW_TAG_structure_type)
15730 {
67607e24 15731 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15732 }
15733 else if (die->tag == DW_TAG_union_type)
15734 {
67607e24 15735 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15736 }
15737 else
15738 {
67607e24 15739 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15740 }
15741
0cc2414c 15742 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
aa70e35c 15743 type->set_is_declared_class (true);
0cc2414c 15744
e35000a7
TBA
15745 /* Store the calling convention in the type if it's available in
15746 the die. Otherwise the calling convention remains set to
15747 the default value DW_CC_normal. */
15748 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15749 if (attr != nullptr
529908cb 15750 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
15751 {
15752 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15753 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 15754 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
15755 }
15756
e142c38c 15757 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15758 if (attr != nullptr)
c906108c 15759 {
cd6c91b4 15760 if (attr->form_is_constant ())
dda83cd7 15761 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
15762 else
15763 {
f8e89861 15764 struct dynamic_prop prop;
293e7e51 15765 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15766 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 15767 TYPE_LENGTH (type) = 0;
155bfbd3 15768 }
c906108c
SS
15769 }
15770 else
15771 {
15772 TYPE_LENGTH (type) = 0;
15773 }
15774
2b4424c3
TT
15775 maybe_set_alignment (cu, die, type);
15776
5230b05a 15777 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15778 {
5230b05a
WT
15779 /* ICC<14 does not output the required DW_AT_declaration on
15780 incomplete types, but gives them a size of zero. */
b4b73759 15781 type->set_is_stub (true);
685b1105
JK
15782 }
15783 else
9baccff6 15784 type->set_stub_is_supported (true);
685b1105 15785
dc718098 15786 if (die_is_declaration (die, cu))
b4b73759 15787 type->set_is_stub (true);
a6c727b2
DJ
15788 else if (attr == NULL && die->child == NULL
15789 && producer_is_realview (cu->producer))
15790 /* RealView does not output the required DW_AT_declaration
15791 on incomplete types. */
b4b73759 15792 type->set_is_stub (true);
dc718098 15793
c906108c
SS
15794 /* We need to add the type field to the die immediately so we don't
15795 infinitely recurse when dealing with pointers to the structure
0963b4bd 15796 type within the structure itself. */
1c379e20 15797 set_die_type (die, type, cu);
c906108c 15798
7e314c57
JK
15799 /* set_die_type should be already done. */
15800 set_descriptive_type (type, die, cu);
15801
c767944b
DJ
15802 return type;
15803}
15804
9c6a1327
TT
15805static void handle_struct_member_die
15806 (struct die_info *child_die,
15807 struct type *type,
15808 struct field_info *fi,
15809 std::vector<struct symbol *> *template_args,
15810 struct dwarf2_cu *cu);
15811
15812/* A helper for handle_struct_member_die that handles
15813 DW_TAG_variant_part. */
15814
15815static void
15816handle_variant_part (struct die_info *die, struct type *type,
15817 struct field_info *fi,
15818 std::vector<struct symbol *> *template_args,
15819 struct dwarf2_cu *cu)
15820{
15821 variant_part_builder *new_part;
15822 if (fi->current_variant_part == nullptr)
15823 {
15824 fi->variant_parts.emplace_back ();
15825 new_part = &fi->variant_parts.back ();
15826 }
15827 else if (!fi->current_variant_part->processing_variant)
15828 {
15829 complaint (_("nested DW_TAG_variant_part seen "
15830 "- DIE at %s [in module %s]"),
15831 sect_offset_str (die->sect_off),
5e22e966 15832 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15833 return;
15834 }
15835 else
15836 {
15837 variant_field &current = fi->current_variant_part->variants.back ();
15838 current.variant_parts.emplace_back ();
15839 new_part = &current.variant_parts.back ();
15840 }
15841
15842 /* When we recurse, we want callees to add to this new variant
15843 part. */
15844 scoped_restore save_current_variant_part
15845 = make_scoped_restore (&fi->current_variant_part, new_part);
15846
15847 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15848 if (discr == NULL)
15849 {
15850 /* It's a univariant form, an extension we support. */
15851 }
15852 else if (discr->form_is_ref ())
15853 {
15854 struct dwarf2_cu *target_cu = cu;
15855 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15856
15857 new_part->discriminant_offset = target_die->sect_off;
15858 }
15859 else
15860 {
15861 complaint (_("DW_AT_discr does not have DIE reference form"
15862 " - DIE at %s [in module %s]"),
15863 sect_offset_str (die->sect_off),
5e22e966 15864 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15865 }
15866
15867 for (die_info *child_die = die->child;
15868 child_die != NULL;
15869 child_die = child_die->sibling)
15870 handle_struct_member_die (child_die, type, fi, template_args, cu);
15871}
15872
15873/* A helper for handle_struct_member_die that handles
15874 DW_TAG_variant. */
15875
15876static void
15877handle_variant (struct die_info *die, struct type *type,
15878 struct field_info *fi,
15879 std::vector<struct symbol *> *template_args,
15880 struct dwarf2_cu *cu)
15881{
15882 if (fi->current_variant_part == nullptr)
15883 {
15884 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15885 "- DIE at %s [in module %s]"),
15886 sect_offset_str (die->sect_off),
5e22e966 15887 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15888 return;
15889 }
15890 if (fi->current_variant_part->processing_variant)
15891 {
15892 complaint (_("nested DW_TAG_variant seen "
15893 "- DIE at %s [in module %s]"),
15894 sect_offset_str (die->sect_off),
5e22e966 15895 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15896 return;
15897 }
15898
15899 scoped_restore save_processing_variant
15900 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15901 true);
15902
15903 fi->current_variant_part->variants.emplace_back ();
15904 variant_field &variant = fi->current_variant_part->variants.back ();
15905 variant.first_field = fi->fields.size ();
15906
15907 /* In a variant we want to get the discriminant and also add a
15908 field for our sole member child. */
15909 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 15910 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
15911 {
15912 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 15913 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
15914 variant.default_branch = true;
15915 else
9d2246fc 15916 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
15917 }
15918 else
cae21f8e 15919 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
15920
15921 for (die_info *variant_child = die->child;
15922 variant_child != NULL;
15923 variant_child = variant_child->sibling)
15924 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15925
15926 variant.last_field = fi->fields.size ();
15927}
15928
2ddeaf8a
TT
15929/* A helper for process_structure_scope that handles a single member
15930 DIE. */
15931
15932static void
15933handle_struct_member_die (struct die_info *child_die, struct type *type,
15934 struct field_info *fi,
15935 std::vector<struct symbol *> *template_args,
15936 struct dwarf2_cu *cu)
15937{
15938 if (child_die->tag == DW_TAG_member
9c6a1327 15939 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15940 {
15941 /* NOTE: carlton/2002-11-05: A C++ static data member
15942 should be a DW_TAG_member that is a declaration, but
15943 all versions of G++ as of this writing (so through at
15944 least 3.2.1) incorrectly generate DW_TAG_variable
15945 tags for them instead. */
15946 dwarf2_add_field (fi, child_die, cu);
15947 }
15948 else if (child_die->tag == DW_TAG_subprogram)
15949 {
15950 /* Rust doesn't have member functions in the C++ sense.
15951 However, it does emit ordinary functions as children
15952 of a struct DIE. */
15953 if (cu->language == language_rust)
15954 read_func_scope (child_die, cu);
15955 else
15956 {
15957 /* C++ member function. */
15958 dwarf2_add_member_fn (fi, child_die, type, cu);
15959 }
15960 }
15961 else if (child_die->tag == DW_TAG_inheritance)
15962 {
15963 /* C++ base class field. */
15964 dwarf2_add_field (fi, child_die, cu);
15965 }
15966 else if (type_can_define_types (child_die))
15967 dwarf2_add_type_defn (fi, child_die, cu);
15968 else if (child_die->tag == DW_TAG_template_type_param
15969 || child_die->tag == DW_TAG_template_value_param)
15970 {
15971 struct symbol *arg = new_symbol (child_die, NULL, cu);
15972
15973 if (arg != NULL)
15974 template_args->push_back (arg);
15975 }
9c6a1327
TT
15976 else if (child_die->tag == DW_TAG_variant_part)
15977 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15978 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15979 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15980}
15981
c767944b
DJ
15982/* Finish creating a structure or union type, including filling in
15983 its members and creating a symbol for it. */
15984
15985static void
15986process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15987{
5e22e966 15988 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15989 struct die_info *child_die;
c767944b
DJ
15990 struct type *type;
15991
15992 type = get_die_type (die, cu);
15993 if (type == NULL)
15994 type = read_structure_type (die, cu);
15995
3e1d3d8c 15996 bool has_template_parameters = false;
e142c38c 15997 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15998 {
15999 struct field_info fi;
2f4732b0 16000 std::vector<struct symbol *> template_args;
c906108c 16001
639d11d3 16002 child_die = die->child;
c906108c
SS
16003
16004 while (child_die && child_die->tag)
16005 {
2ddeaf8a 16006 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16007 child_die = child_die->sibling;
c906108c
SS
16008 }
16009
34eaf542 16010 /* Attach template arguments to type. */
2f4732b0 16011 if (!template_args.empty ())
34eaf542 16012 {
3e1d3d8c 16013 has_template_parameters = true;
34eaf542 16014 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16015 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16016 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16017 = XOBNEWVEC (&objfile->objfile_obstack,
16018 struct symbol *,
16019 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16020 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16021 template_args.data (),
34eaf542
TT
16022 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16023 * sizeof (struct symbol *)));
34eaf542
TT
16024 }
16025
c906108c 16026 /* Attach fields and member functions to the type. */
317f7127 16027 if (fi.nfields () > 0)
e7c27a73 16028 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16029 if (!fi.fnfieldlists.empty ())
c906108c 16030 {
e7c27a73 16031 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16032
c5aa993b 16033 /* Get the type which refers to the base class (possibly this
c906108c 16034 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16035 class from the DW_AT_containing_type attribute. This use of
16036 DW_AT_containing_type is a GNU extension. */
c906108c 16037
e142c38c 16038 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16039 {
e7c27a73 16040 struct type *t = die_containing_type (die, cu);
c906108c 16041
ae6ae975 16042 set_type_vptr_basetype (type, t);
c906108c
SS
16043 if (type == t)
16044 {
c906108c
SS
16045 int i;
16046
16047 /* Our own class provides vtbl ptr. */
1f704f76 16048 for (i = t->num_fields () - 1;
c906108c
SS
16049 i >= TYPE_N_BASECLASSES (t);
16050 --i)
16051 {
0d5cff50 16052 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16053
dda83cd7 16054 if (is_vtable_name (fieldname, cu))
c906108c 16055 {
ae6ae975 16056 set_type_vptr_fieldno (type, i);
c906108c
SS
16057 break;
16058 }
16059 }
16060
16061 /* Complain if virtual function table field not found. */
16062 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16063 complaint (_("virtual function table pointer "
3e43a32a 16064 "not found when defining class '%s'"),
7d93a1e0 16065 type->name () ? type->name () : "");
c906108c
SS
16066 }
16067 else
16068 {
ae6ae975 16069 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16070 }
16071 }
f6235d4c 16072 else if (cu->producer
61012eef 16073 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16074 {
16075 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16076 of the containing type, but the vtable pointer is
16077 always named __vfp. */
f6235d4c
EZ
16078
16079 int i;
16080
1f704f76 16081 for (i = type->num_fields () - 1;
f6235d4c
EZ
16082 i >= TYPE_N_BASECLASSES (type);
16083 --i)
16084 {
16085 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16086 {
ae6ae975
DE
16087 set_type_vptr_fieldno (type, i);
16088 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16089 break;
16090 }
16091 }
16092 }
c906108c 16093 }
98751a41
JK
16094
16095 /* Copy fi.typedef_field_list linked list elements content into the
16096 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16097 if (!fi.typedef_field_list.empty ())
98751a41 16098 {
be2daae6 16099 int count = fi.typedef_field_list.size ();
98751a41 16100
a0d7a4ff 16101 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16102 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16103 = ((struct decl_field *)
be2daae6
TT
16104 TYPE_ALLOC (type,
16105 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16106 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16107
be2daae6
TT
16108 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16109 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16110 }
c767944b 16111
883fd55a
KS
16112 /* Copy fi.nested_types_list linked list elements content into the
16113 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16114 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16115 {
be2daae6 16116 int count = fi.nested_types_list.size ();
883fd55a
KS
16117
16118 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16119 TYPE_NESTED_TYPES_ARRAY (type)
16120 = ((struct decl_field *)
be2daae6
TT
16121 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16122 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16123
be2daae6
TT
16124 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16125 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16126 }
c906108c 16127 }
63d06c5c 16128
bb5ed363 16129 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16130 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16131 cu->rust_unions.push_back (type);
57567375
TT
16132 else if (cu->language == language_ada)
16133 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16134
90aeadfc
DC
16135 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16136 snapshots) has been known to create a die giving a declaration
16137 for a class that has, as a child, a die giving a definition for a
16138 nested class. So we have to process our children even if the
16139 current die is a declaration. Normally, of course, a declaration
16140 won't have any children at all. */
134d01f1 16141
ca040673
DE
16142 child_die = die->child;
16143
90aeadfc
DC
16144 while (child_die != NULL && child_die->tag)
16145 {
16146 if (child_die->tag == DW_TAG_member
16147 || child_die->tag == DW_TAG_variable
34eaf542
TT
16148 || child_die->tag == DW_TAG_inheritance
16149 || child_die->tag == DW_TAG_template_value_param
16150 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16151 {
90aeadfc 16152 /* Do nothing. */
134d01f1 16153 }
90aeadfc
DC
16154 else
16155 process_die (child_die, cu);
134d01f1 16156
436c571c 16157 child_die = child_die->sibling;
134d01f1
DJ
16158 }
16159
fa4028e9
JB
16160 /* Do not consider external references. According to the DWARF standard,
16161 these DIEs are identified by the fact that they have no byte_size
16162 attribute, and a declaration attribute. */
16163 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16164 || !die_is_declaration (die, cu)
16165 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16166 {
16167 struct symbol *sym = new_symbol (die, type, cu);
16168
16169 if (has_template_parameters)
16170 {
a776957c
TT
16171 struct symtab *symtab;
16172 if (sym != nullptr)
16173 symtab = symbol_symtab (sym);
16174 else if (cu->line_header != nullptr)
16175 {
16176 /* Any related symtab will do. */
16177 symtab
7ba99d21 16178 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16179 }
16180 else
16181 {
16182 symtab = nullptr;
16183 complaint (_("could not find suitable "
16184 "symtab for template parameter"
16185 " - DIE at %s [in module %s]"),
16186 sect_offset_str (die->sect_off),
16187 objfile_name (objfile));
16188 }
16189
16190 if (symtab != nullptr)
16191 {
16192 /* Make sure that the symtab is set on the new symbols.
16193 Even though they don't appear in this symtab directly,
16194 other parts of gdb assume that symbols do, and this is
16195 reasonably true. */
16196 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16197 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16198 }
3e1d3d8c
TT
16199 }
16200 }
134d01f1
DJ
16201}
16202
ed6acedd
TT
16203/* Assuming DIE is an enumeration type, and TYPE is its associated
16204 type, update TYPE using some information only available in DIE's
16205 children. In particular, the fields are computed. */
55426c9d
JB
16206
16207static void
16208update_enumeration_type_from_children (struct die_info *die,
16209 struct type *type,
16210 struct dwarf2_cu *cu)
16211{
60f7655a 16212 struct die_info *child_die;
55426c9d
JB
16213 int unsigned_enum = 1;
16214 int flag_enum = 1;
55426c9d 16215
8268c778 16216 auto_obstack obstack;
ed6acedd 16217 std::vector<struct field> fields;
55426c9d 16218
60f7655a
DE
16219 for (child_die = die->child;
16220 child_die != NULL && child_die->tag;
436c571c 16221 child_die = child_die->sibling)
55426c9d
JB
16222 {
16223 struct attribute *attr;
16224 LONGEST value;
16225 const gdb_byte *bytes;
16226 struct dwarf2_locexpr_baton *baton;
16227 const char *name;
60f7655a 16228
55426c9d
JB
16229 if (child_die->tag != DW_TAG_enumerator)
16230 continue;
16231
16232 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16233 if (attr == NULL)
16234 continue;
16235
16236 name = dwarf2_name (child_die, cu);
16237 if (name == NULL)
16238 name = "<anonymous enumerator>";
16239
16240 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16241 &value, &bytes, &baton);
16242 if (value < 0)
16243 {
16244 unsigned_enum = 0;
16245 flag_enum = 0;
16246 }
55426c9d 16247 else
edd45eb0
SM
16248 {
16249 if (count_one_bits_ll (value) >= 2)
16250 flag_enum = 0;
edd45eb0 16251 }
55426c9d 16252
ed6acedd
TT
16253 fields.emplace_back ();
16254 struct field &field = fields.back ();
16255 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16256 SET_FIELD_ENUMVAL (field, value);
16257 }
16258
16259 if (!fields.empty ())
16260 {
5e33d5f4 16261 type->set_num_fields (fields.size ());
3cabb6b0
SM
16262 type->set_fields
16263 ((struct field *)
16264 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16265 memcpy (type->fields (), fields.data (),
ed6acedd 16266 sizeof (struct field) * fields.size ());
55426c9d
JB
16267 }
16268
16269 if (unsigned_enum)
653223d3
SM
16270 type->set_is_unsigned (true);
16271
55426c9d 16272 if (flag_enum)
9902b327 16273 type->set_is_flag_enum (true);
55426c9d
JB
16274}
16275
134d01f1
DJ
16276/* Given a DW_AT_enumeration_type die, set its type. We do not
16277 complete the type's fields yet, or create any symbols. */
c906108c 16278
f792889a 16279static struct type *
134d01f1 16280read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16281{
5e22e966 16282 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16283 struct type *type;
c906108c 16284 struct attribute *attr;
0114d602 16285 const char *name;
134d01f1 16286
348e048f
DE
16287 /* If the definition of this type lives in .debug_types, read that type.
16288 Don't follow DW_AT_specification though, that will take us back up
16289 the chain and we want to go down. */
052c8bb8 16290 attr = die->attr (DW_AT_signature);
435d3d88 16291 if (attr != nullptr)
348e048f 16292 {
ac9ec31b 16293 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16294
ac9ec31b 16295 /* The type's CU may not be the same as CU.
02142a6c 16296 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16297 return set_die_type (die, type, cu);
16298 }
16299
c906108c
SS
16300 type = alloc_type (objfile);
16301
67607e24 16302 type->set_code (TYPE_CODE_ENUM);
94af9270 16303 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16304 if (name != NULL)
d0e39ea2 16305 type->set_name (name);
c906108c 16306
0626fc76
TT
16307 attr = dwarf2_attr (die, DW_AT_type, cu);
16308 if (attr != NULL)
16309 {
16310 struct type *underlying_type = die_type (die, cu);
16311
16312 TYPE_TARGET_TYPE (type) = underlying_type;
16313 }
16314
e142c38c 16315 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16316 if (attr != nullptr)
c906108c 16317 {
529908cb 16318 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16319 }
16320 else
16321 {
16322 TYPE_LENGTH (type) = 0;
16323 }
16324
2b4424c3
TT
16325 maybe_set_alignment (cu, die, type);
16326
137033e9
JB
16327 /* The enumeration DIE can be incomplete. In Ada, any type can be
16328 declared as private in the package spec, and then defined only
16329 inside the package body. Such types are known as Taft Amendment
16330 Types. When another package uses such a type, an incomplete DIE
16331 may be generated by the compiler. */
02eb380e 16332 if (die_is_declaration (die, cu))
b4b73759 16333 type->set_is_stub (true);
02eb380e 16334
0626fc76
TT
16335 /* If this type has an underlying type that is not a stub, then we
16336 may use its attributes. We always use the "unsigned" attribute
16337 in this situation, because ordinarily we guess whether the type
16338 is unsigned -- but the guess can be wrong and the underlying type
16339 can tell us the reality. However, we defer to a local size
16340 attribute if one exists, because this lets the compiler override
16341 the underlying type if needed. */
e46d3488 16342 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16343 {
9e7c9a03
HD
16344 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16345 underlying_type = check_typedef (underlying_type);
653223d3
SM
16346
16347 type->set_is_unsigned (underlying_type->is_unsigned ());
16348
0626fc76 16349 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16350 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16351
2b4424c3 16352 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16353 && TYPE_RAW_ALIGN (underlying_type) != 0)
16354 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16355 }
16356
aa70e35c 16357 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
3d567982 16358
ed6acedd
TT
16359 set_die_type (die, type, cu);
16360
16361 /* Finish the creation of this type by using the enum's children.
16362 Note that, as usual, this must come after set_die_type to avoid
16363 infinite recursion when trying to compute the names of the
16364 enumerators. */
16365 update_enumeration_type_from_children (die, type, cu);
16366
16367 return type;
134d01f1
DJ
16368}
16369
16370/* Given a pointer to a die which begins an enumeration, process all
16371 the dies that define the members of the enumeration, and create the
16372 symbol for the enumeration type.
16373
16374 NOTE: We reverse the order of the element list. */
16375
16376static void
16377process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16378{
f792889a 16379 struct type *this_type;
134d01f1 16380
f792889a
DJ
16381 this_type = get_die_type (die, cu);
16382 if (this_type == NULL)
16383 this_type = read_enumeration_type (die, cu);
9dc481d3 16384
639d11d3 16385 if (die->child != NULL)
c906108c 16386 {
9dc481d3 16387 struct die_info *child_die;
15d034d0 16388 const char *name;
9dc481d3 16389
639d11d3 16390 child_die = die->child;
c906108c
SS
16391 while (child_die && child_die->tag)
16392 {
16393 if (child_die->tag != DW_TAG_enumerator)
16394 {
e7c27a73 16395 process_die (child_die, cu);
c906108c
SS
16396 }
16397 else
16398 {
39cbfefa
DJ
16399 name = dwarf2_name (child_die, cu);
16400 if (name)
ed6acedd 16401 new_symbol (child_die, this_type, cu);
c906108c
SS
16402 }
16403
436c571c 16404 child_die = child_die->sibling;
c906108c 16405 }
c906108c 16406 }
134d01f1 16407
6c83ed52
TT
16408 /* If we are reading an enum from a .debug_types unit, and the enum
16409 is a declaration, and the enum is not the signatured type in the
16410 unit, then we do not want to add a symbol for it. Adding a
16411 symbol would in some cases obscure the true definition of the
16412 enum, giving users an incomplete type when the definition is
16413 actually available. Note that we do not want to do this for all
16414 enums which are just declarations, because C++0x allows forward
16415 enum declarations. */
3019eac3 16416 if (cu->per_cu->is_debug_types
6c83ed52
TT
16417 && die_is_declaration (die, cu))
16418 {
52dc124a 16419 struct signatured_type *sig_type;
6c83ed52 16420
c0f78cd4 16421 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16422 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16423 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16424 return;
16425 }
16426
f792889a 16427 new_symbol (die, this_type, cu);
c906108c
SS
16428}
16429
57567375
TT
16430/* Helper function for quirk_ada_thick_pointer that examines a bounds
16431 expression for an index type and finds the corresponding field
16432 offset in the hidden "P_BOUNDS" structure. Returns true on success
16433 and updates *FIELD, false if it fails to recognize an
16434 expression. */
16435
16436static bool
16437recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16438 int *bounds_offset, struct field *field,
16439 struct dwarf2_cu *cu)
16440{
16441 struct attribute *attr = dwarf2_attr (die, name, cu);
16442 if (attr == nullptr || !attr->form_is_block ())
16443 return false;
16444
16445 const struct dwarf_block *block = attr->as_block ();
16446 const gdb_byte *start = block->data;
16447 const gdb_byte *end = block->data + block->size;
16448
16449 /* The expression to recognize generally looks like:
16450
16451 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16452 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16453
16454 However, the second "plus_uconst" may be missing:
16455
16456 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16457 DW_OP_deref_size: 4)
16458
16459 This happens when the field is at the start of the structure.
16460
16461 Also, the final deref may not be sized:
16462
16463 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16464 DW_OP_deref)
16465
16466 This happens when the size of the index type happens to be the
16467 same as the architecture's word size. This can occur with or
16468 without the second plus_uconst. */
16469
16470 if (end - start < 2)
16471 return false;
16472 if (*start++ != DW_OP_push_object_address)
16473 return false;
16474 if (*start++ != DW_OP_plus_uconst)
16475 return false;
16476
16477 uint64_t this_bound_off;
16478 start = gdb_read_uleb128 (start, end, &this_bound_off);
16479 if (start == nullptr || (int) this_bound_off != this_bound_off)
16480 return false;
16481 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16482 is consistent among all bounds. */
16483 if (*bounds_offset == -1)
16484 *bounds_offset = this_bound_off;
16485 else if (*bounds_offset != this_bound_off)
16486 return false;
16487
16488 if (start == end || *start++ != DW_OP_deref)
16489 return false;
16490
16491 int offset = 0;
16492 if (start ==end)
16493 return false;
16494 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16495 {
16496 /* This means an offset of 0. */
16497 }
16498 else if (*start++ != DW_OP_plus_uconst)
16499 return false;
16500 else
16501 {
16502 /* The size is the parameter to DW_OP_plus_uconst. */
16503 uint64_t val;
16504 start = gdb_read_uleb128 (start, end, &val);
16505 if (start == nullptr)
16506 return false;
16507 if ((int) val != val)
16508 return false;
16509 offset = val;
16510 }
16511
16512 if (start == end)
16513 return false;
16514
16515 uint64_t size;
16516 if (*start == DW_OP_deref_size)
16517 {
16518 start = gdb_read_uleb128 (start + 1, end, &size);
16519 if (start == nullptr)
16520 return false;
16521 }
16522 else if (*start == DW_OP_deref)
16523 {
16524 size = cu->header.addr_size;
16525 ++start;
16526 }
16527 else
16528 return false;
16529
16530 SET_FIELD_BITPOS (*field, 8 * offset);
16531 if (size != TYPE_LENGTH (field->type ()))
16532 FIELD_BITSIZE (*field) = 8 * size;
16533
16534 return true;
16535}
16536
16537/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16538 some kinds of Ada arrays:
16539
16540 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16541 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16542 <11e0> DW_AT_data_location: 2 byte block: 97 6
16543 (DW_OP_push_object_address; DW_OP_deref)
16544 <11e3> DW_AT_type : <0x1173>
16545 <11e7> DW_AT_sibling : <0x1201>
16546 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16547 <11ec> DW_AT_type : <0x1206>
16548 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16549 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16550 DW_OP_deref_size: 4)
16551 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16552 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16553 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16554
16555 This actually represents a "thick pointer", which is a structure
16556 with two elements: one that is a pointer to the array data, and one
16557 that is a pointer to another structure; this second structure holds
16558 the array bounds.
16559
16560 This returns a new type on success, or nullptr if this didn't
16561 recognize the type. */
16562
16563static struct type *
16564quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16565 struct type *type)
16566{
16567 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16568 /* So far we've only seen this with block form. */
16569 if (attr == nullptr || !attr->form_is_block ())
16570 return nullptr;
16571
16572 /* Note that this will fail if the structure layout is changed by
16573 the compiler. However, we have no good way to recognize some
16574 other layout, because we don't know what expression the compiler
16575 might choose to emit should this happen. */
16576 struct dwarf_block *blk = attr->as_block ();
16577 if (blk->size != 2
16578 || blk->data[0] != DW_OP_push_object_address
16579 || blk->data[1] != DW_OP_deref)
16580 return nullptr;
16581
16582 int bounds_offset = -1;
16583 int max_align = -1;
16584 std::vector<struct field> range_fields;
16585 for (struct die_info *child_die = die->child;
16586 child_die;
16587 child_die = child_die->sibling)
16588 {
16589 if (child_die->tag == DW_TAG_subrange_type)
16590 {
16591 struct type *underlying = read_subrange_index_type (child_die, cu);
16592
16593 int this_align = type_align (underlying);
16594 if (this_align > max_align)
16595 max_align = this_align;
16596
16597 range_fields.emplace_back ();
16598 range_fields.emplace_back ();
16599
16600 struct field &lower = range_fields[range_fields.size () - 2];
16601 struct field &upper = range_fields[range_fields.size () - 1];
16602
16603 lower.set_type (underlying);
16604 FIELD_ARTIFICIAL (lower) = 1;
16605
16606 upper.set_type (underlying);
16607 FIELD_ARTIFICIAL (upper) = 1;
16608
16609 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16610 &bounds_offset, &lower, cu)
16611 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16612 &bounds_offset, &upper, cu))
16613 return nullptr;
16614 }
16615 }
16616
16617 /* This shouldn't really happen, but double-check that we found
16618 where the bounds are stored. */
16619 if (bounds_offset == -1)
16620 return nullptr;
16621
16622 struct objfile *objfile = cu->per_objfile->objfile;
16623 for (int i = 0; i < range_fields.size (); i += 2)
16624 {
16625 char name[20];
16626
16627 /* Set the name of each field in the bounds. */
16628 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16629 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16630 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16631 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16632 }
16633
16634 struct type *bounds = alloc_type (objfile);
16635 bounds->set_code (TYPE_CODE_STRUCT);
16636
16637 bounds->set_num_fields (range_fields.size ());
16638 bounds->set_fields
16639 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16640 * sizeof (struct field))));
16641 memcpy (bounds->fields (), range_fields.data (),
16642 bounds->num_fields () * sizeof (struct field));
16643
16644 int last_fieldno = range_fields.size () - 1;
16645 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16646 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16647 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16648
16649 /* Rewrite the existing array type in place. Specifically, we
16650 remove any dynamic properties we might have read, and we replace
16651 the index types. */
16652 struct type *iter = type;
16653 for (int i = 0; i < range_fields.size (); i += 2)
16654 {
16655 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16656 iter->main_type->dyn_prop_list = nullptr;
16657 iter->set_index_type
16658 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16659 iter = TYPE_TARGET_TYPE (iter);
16660 }
16661
16662 struct type *result = alloc_type (objfile);
16663 result->set_code (TYPE_CODE_STRUCT);
16664
16665 result->set_num_fields (2);
16666 result->set_fields
16667 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16668 * sizeof (struct field))));
16669
16670 /* The names are chosen to coincide with what the compiler does with
16671 -fgnat-encodings=all, which the Ada code in gdb already
16672 understands. */
16673 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16674 result->field (0).set_type (lookup_pointer_type (type));
16675
16676 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16677 result->field (1).set_type (lookup_pointer_type (bounds));
16678 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16679
16680 result->set_name (type->name ());
16681 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16682 + TYPE_LENGTH (result->field (1).type ()));
16683
16684 return result;
16685}
16686
c906108c
SS
16687/* Extract all information from a DW_TAG_array_type DIE and put it in
16688 the DIE's type field. For now, this only handles one dimensional
16689 arrays. */
16690
f792889a 16691static struct type *
e7c27a73 16692read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16693{
5e22e966 16694 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16695 struct die_info *child_die;
7e314c57 16696 struct type *type;
c906108c 16697 struct type *element_type, *range_type, *index_type;
c906108c 16698 struct attribute *attr;
15d034d0 16699 const char *name;
a405673c 16700 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16701 unsigned int bit_stride = 0;
c906108c 16702
e7c27a73 16703 element_type = die_type (die, cu);
c906108c 16704
7e314c57
JK
16705 /* The die_type call above may have already set the type for this DIE. */
16706 type = get_die_type (die, cu);
16707 if (type)
16708 return type;
16709
dc53a7ad
JB
16710 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16711 if (attr != NULL)
a405673c
JB
16712 {
16713 int stride_ok;
293e7e51 16714 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16715
16716 byte_stride_prop
16717 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16718 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16719 prop_type);
a405673c
JB
16720 if (!stride_ok)
16721 {
b98664d3 16722 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16723 " - DIE at %s [in module %s]"),
16724 sect_offset_str (die->sect_off),
5e22e966 16725 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16726 /* Ignore this attribute. We will likely not be able to print
16727 arrays of this type correctly, but there is little we can do
16728 to help if we cannot read the attribute's value. */
16729 byte_stride_prop = NULL;
16730 }
16731 }
dc53a7ad
JB
16732
16733 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16734 if (attr != NULL)
529908cb 16735 bit_stride = attr->constant_value (0);
dc53a7ad 16736
c906108c
SS
16737 /* Irix 6.2 native cc creates array types without children for
16738 arrays with unspecified length. */
639d11d3 16739 if (die->child == NULL)
c906108c 16740 {
46bf5051 16741 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16742 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16743 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16744 byte_stride_prop, bit_stride);
f792889a 16745 return set_die_type (die, type, cu);
c906108c
SS
16746 }
16747
791afaa2 16748 std::vector<struct type *> range_types;
639d11d3 16749 child_die = die->child;
c906108c
SS
16750 while (child_die && child_die->tag)
16751 {
16752 if (child_die->tag == DW_TAG_subrange_type)
16753 {
f792889a 16754 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16755
dda83cd7
SM
16756 if (child_type != NULL)
16757 {
0963b4bd 16758 /* The range type was succesfully read. Save it for the
dda83cd7 16759 array type creation. */
791afaa2 16760 range_types.push_back (child_type);
dda83cd7 16761 }
c906108c 16762 }
436c571c 16763 child_die = child_die->sibling;
c906108c
SS
16764 }
16765
cf2b2075
TV
16766 if (range_types.empty ())
16767 {
16768 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16769 sect_offset_str (die->sect_off),
16770 objfile_name (cu->per_objfile->objfile));
16771 return NULL;
16772 }
16773
c906108c
SS
16774 /* Dwarf2 dimensions are output from left to right, create the
16775 necessary array types in backwards order. */
7ca2d3a3 16776
c906108c 16777 type = element_type;
7ca2d3a3
DL
16778
16779 if (read_array_order (die, cu) == DW_ORD_col_major)
16780 {
16781 int i = 0;
9a619af0 16782
791afaa2 16783 while (i < range_types.size ())
10f6a3ad
TT
16784 {
16785 type = create_array_type_with_stride (NULL, type, range_types[i++],
16786 byte_stride_prop, bit_stride);
16787 bit_stride = 0;
16788 byte_stride_prop = nullptr;
16789 }
7ca2d3a3
DL
16790 }
16791 else
16792 {
791afaa2 16793 size_t ndim = range_types.size ();
7ca2d3a3 16794 while (ndim-- > 0)
10f6a3ad
TT
16795 {
16796 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16797 byte_stride_prop, bit_stride);
16798 bit_stride = 0;
16799 byte_stride_prop = nullptr;
16800 }
7ca2d3a3 16801 }
c906108c 16802
cf2b2075
TV
16803 gdb_assert (type != element_type);
16804
f5f8a009
EZ
16805 /* Understand Dwarf2 support for vector types (like they occur on
16806 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16807 array type. This is not part of the Dwarf2/3 standard yet, but a
16808 custom vendor extension. The main difference between a regular
16809 array and the vector variant is that vectors are passed by value
16810 to functions. */
e142c38c 16811 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16812 if (attr != nullptr)
ea37ba09 16813 make_vector_type (type);
f5f8a009 16814
dbc98a8b
KW
16815 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16816 implementation may choose to implement triple vectors using this
16817 attribute. */
16818 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 16819 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 16820 {
529908cb
TT
16821 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16822 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 16823 else
b98664d3 16824 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16825 "than the total size of elements"));
dbc98a8b
KW
16826 }
16827
39cbfefa
DJ
16828 name = dwarf2_name (die, cu);
16829 if (name)
d0e39ea2 16830 type->set_name (name);
6e70227d 16831
2b4424c3
TT
16832 maybe_set_alignment (cu, die, type);
16833
57567375
TT
16834 struct type *replacement_type = nullptr;
16835 if (cu->language == language_ada)
16836 {
16837 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16838 if (replacement_type != nullptr)
16839 type = replacement_type;
16840 }
16841
0963b4bd 16842 /* Install the type in the die. */
57567375 16843 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
16844
16845 /* set_die_type should be already done. */
b4ba55a1
JB
16846 set_descriptive_type (type, die, cu);
16847
7e314c57 16848 return type;
c906108c
SS
16849}
16850
7ca2d3a3 16851static enum dwarf_array_dim_ordering
6e70227d 16852read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16853{
16854 struct attribute *attr;
16855
16856 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16857
435d3d88 16858 if (attr != nullptr)
1bc397c5
TT
16859 {
16860 LONGEST val = attr->constant_value (-1);
16861 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16862 return (enum dwarf_array_dim_ordering) val;
16863 }
7ca2d3a3 16864
0963b4bd
MS
16865 /* GNU F77 is a special case, as at 08/2004 array type info is the
16866 opposite order to the dwarf2 specification, but data is still
16867 laid out as per normal fortran.
7ca2d3a3 16868
0963b4bd
MS
16869 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16870 version checking. */
7ca2d3a3 16871
905e0470
PM
16872 if (cu->language == language_fortran
16873 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16874 {
16875 return DW_ORD_row_major;
16876 }
16877
3a3440fb 16878 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
16879 {
16880 case array_column_major:
16881 return DW_ORD_col_major;
16882 case array_row_major:
16883 default:
16884 return DW_ORD_row_major;
16885 };
16886}
16887
72019c9c 16888/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16889 the DIE's type field. */
72019c9c 16890
f792889a 16891static struct type *
72019c9c
GM
16892read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16893{
7e314c57
JK
16894 struct type *domain_type, *set_type;
16895 struct attribute *attr;
f792889a 16896
7e314c57
JK
16897 domain_type = die_type (die, cu);
16898
16899 /* The die_type call above may have already set the type for this DIE. */
16900 set_type = get_die_type (die, cu);
16901 if (set_type)
16902 return set_type;
16903
16904 set_type = create_set_type (NULL, domain_type);
16905
16906 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
16907 if (attr != nullptr && attr->form_is_unsigned ())
16908 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 16909
2b4424c3
TT
16910 maybe_set_alignment (cu, die, set_type);
16911
f792889a 16912 return set_die_type (die, set_type, cu);
72019c9c 16913}
7ca2d3a3 16914
0971de02
TT
16915/* A helper for read_common_block that creates a locexpr baton.
16916 SYM is the symbol which we are marking as computed.
16917 COMMON_DIE is the DIE for the common block.
16918 COMMON_LOC is the location expression attribute for the common
16919 block itself.
16920 MEMBER_LOC is the location expression attribute for the particular
16921 member of the common block that we are processing.
16922 CU is the CU from which the above come. */
16923
16924static void
16925mark_common_block_symbol_computed (struct symbol *sym,
16926 struct die_info *common_die,
16927 struct attribute *common_loc,
16928 struct attribute *member_loc,
16929 struct dwarf2_cu *cu)
16930{
5e22e966 16931 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16932 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16933 struct dwarf2_locexpr_baton *baton;
16934 gdb_byte *ptr;
16935 unsigned int cu_off;
08feed99 16936 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16937 LONGEST offset = 0;
16938
16939 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16940 gdb_assert (common_loc->form_is_block ());
16941 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16942 || member_loc->form_is_constant ());
0971de02 16943
8d749320 16944 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16945 baton->per_objfile = per_objfile;
0971de02
TT
16946 baton->per_cu = cu->per_cu;
16947 gdb_assert (baton->per_cu);
16948
16949 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16950
cd6c91b4 16951 if (member_loc->form_is_constant ())
0971de02 16952 {
0826b30a 16953 offset = member_loc->constant_value (0);
0971de02
TT
16954 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16955 }
16956 else
9d2246fc 16957 baton->size += member_loc->as_block ()->size;
0971de02 16958
224c3ddb 16959 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16960 baton->data = ptr;
16961
16962 *ptr++ = DW_OP_call4;
9c541725 16963 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16964 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16965 ptr += 4;
16966
cd6c91b4 16967 if (member_loc->form_is_constant ())
0971de02
TT
16968 {
16969 *ptr++ = DW_OP_addr;
16970 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16971 ptr += cu->header.addr_size;
16972 }
16973 else
16974 {
16975 /* We have to copy the data here, because DW_OP_call4 will only
16976 use a DW_AT_location attribute. */
9d2246fc
TT
16977 struct dwarf_block *block = member_loc->as_block ();
16978 memcpy (ptr, block->data, block->size);
16979 ptr += block->size;
0971de02
TT
16980 }
16981
16982 *ptr++ = DW_OP_plus;
16983 gdb_assert (ptr - baton->data == baton->size);
16984
0971de02 16985 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16986 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16987}
16988
4357ac6c
TT
16989/* Create appropriate locally-scoped variables for all the
16990 DW_TAG_common_block entries. Also create a struct common_block
16991 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16992 is used to separate the common blocks name namespace from regular
4357ac6c 16993 variable names. */
c906108c
SS
16994
16995static void
e7c27a73 16996read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16997{
0971de02
TT
16998 struct attribute *attr;
16999
17000 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17001 if (attr != nullptr)
0971de02
TT
17002 {
17003 /* Support the .debug_loc offsets. */
4fc6c0d5 17004 if (attr->form_is_block ())
dda83cd7 17005 {
0971de02 17006 /* Ok. */
dda83cd7 17007 }
cd6c91b4 17008 else if (attr->form_is_section_offset ())
dda83cd7 17009 {
0971de02
TT
17010 dwarf2_complex_location_expr_complaint ();
17011 attr = NULL;
dda83cd7 17012 }
0971de02 17013 else
dda83cd7 17014 {
0971de02
TT
17015 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17016 "common block member");
17017 attr = NULL;
dda83cd7 17018 }
0971de02
TT
17019 }
17020
639d11d3 17021 if (die->child != NULL)
c906108c 17022 {
5e22e966 17023 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17024 struct die_info *child_die;
17025 size_t n_entries = 0, size;
17026 struct common_block *common_block;
17027 struct symbol *sym;
74ac6d43 17028
4357ac6c
TT
17029 for (child_die = die->child;
17030 child_die && child_die->tag;
436c571c 17031 child_die = child_die->sibling)
4357ac6c
TT
17032 ++n_entries;
17033
17034 size = (sizeof (struct common_block)
17035 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17036 common_block
17037 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17038 size);
4357ac6c
TT
17039 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17040 common_block->n_entries = 0;
17041
17042 for (child_die = die->child;
17043 child_die && child_die->tag;
436c571c 17044 child_die = child_die->sibling)
4357ac6c
TT
17045 {
17046 /* Create the symbol in the DW_TAG_common_block block in the current
17047 symbol scope. */
e7c27a73 17048 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17049 if (sym != NULL)
17050 {
17051 struct attribute *member_loc;
17052
17053 common_block->contents[common_block->n_entries++] = sym;
17054
17055 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17056 cu);
17057 if (member_loc)
17058 {
17059 /* GDB has handled this for a long time, but it is
17060 not specified by DWARF. It seems to have been
17061 emitted by gfortran at least as recently as:
17062 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17063 complaint (_("Variable in common block has "
0971de02 17064 "DW_AT_data_member_location "
9d8780f0
SM
17065 "- DIE at %s [in module %s]"),
17066 sect_offset_str (child_die->sect_off),
518817b3 17067 objfile_name (objfile));
0971de02 17068
cd6c91b4 17069 if (member_loc->form_is_section_offset ())
0971de02 17070 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17071 else if (member_loc->form_is_constant ()
4fc6c0d5 17072 || member_loc->form_is_block ())
0971de02 17073 {
435d3d88 17074 if (attr != nullptr)
0971de02
TT
17075 mark_common_block_symbol_computed (sym, die, attr,
17076 member_loc, cu);
17077 }
17078 else
17079 dwarf2_complex_location_expr_complaint ();
17080 }
17081 }
c906108c 17082 }
4357ac6c
TT
17083
17084 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17085 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17086 }
17087}
17088
0114d602 17089/* Create a type for a C++ namespace. */
d9fa45fe 17090
0114d602
DJ
17091static struct type *
17092read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17093{
5e22e966 17094 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17095 const char *previous_prefix, *name;
9219021c 17096 int is_anonymous;
0114d602
DJ
17097 struct type *type;
17098
17099 /* For extensions, reuse the type of the original namespace. */
17100 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17101 {
17102 struct die_info *ext_die;
17103 struct dwarf2_cu *ext_cu = cu;
9a619af0 17104
0114d602
DJ
17105 ext_die = dwarf2_extension (die, &ext_cu);
17106 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17107
17108 /* EXT_CU may not be the same as CU.
02142a6c 17109 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17110 return set_die_type (die, type, cu);
17111 }
9219021c 17112
e142c38c 17113 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17114
17115 /* Now build the name of the current namespace. */
17116
0114d602
DJ
17117 previous_prefix = determine_prefix (die, cu);
17118 if (previous_prefix[0] != '\0')
17119 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17120 previous_prefix, name, 0, cu);
0114d602
DJ
17121
17122 /* Create the type. */
19f392bc 17123 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17124
60531b24 17125 return set_die_type (die, type, cu);
0114d602
DJ
17126}
17127
22cee43f 17128/* Read a namespace scope. */
0114d602
DJ
17129
17130static void
17131read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17132{
5e22e966 17133 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17134 int is_anonymous;
9219021c 17135
5c4e30ca
DC
17136 /* Add a symbol associated to this if we haven't seen the namespace
17137 before. Also, add a using directive if it's an anonymous
17138 namespace. */
9219021c 17139
f2f0e013 17140 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17141 {
17142 struct type *type;
17143
0114d602 17144 type = read_type_die (die, cu);
e7c27a73 17145 new_symbol (die, type, cu);
5c4e30ca 17146
e8e80198 17147 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17148 if (is_anonymous)
0114d602
DJ
17149 {
17150 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17151
eb1e02fd 17152 std::vector<const char *> excludes;
804d2729 17153 add_using_directive (using_directives (cu),
7d93a1e0 17154 previous_prefix, type->name (), NULL,
eb1e02fd 17155 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17156 }
5c4e30ca 17157 }
9219021c 17158
639d11d3 17159 if (die->child != NULL)
d9fa45fe 17160 {
639d11d3 17161 struct die_info *child_die = die->child;
6e70227d 17162
d9fa45fe
DC
17163 while (child_die && child_die->tag)
17164 {
e7c27a73 17165 process_die (child_die, cu);
436c571c 17166 child_die = child_die->sibling;
d9fa45fe
DC
17167 }
17168 }
38d518c9
EZ
17169}
17170
f55ee35c
JK
17171/* Read a Fortran module as type. This DIE can be only a declaration used for
17172 imported module. Still we need that type as local Fortran "use ... only"
17173 declaration imports depend on the created type in determine_prefix. */
17174
17175static struct type *
17176read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17177{
5e22e966 17178 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17179 const char *module_name;
f55ee35c
JK
17180 struct type *type;
17181
17182 module_name = dwarf2_name (die, cu);
19f392bc 17183 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17184
f55ee35c
JK
17185 return set_die_type (die, type, cu);
17186}
17187
5d7cb8df
JK
17188/* Read a Fortran module. */
17189
17190static void
17191read_module (struct die_info *die, struct dwarf2_cu *cu)
17192{
17193 struct die_info *child_die = die->child;
530e8392
KB
17194 struct type *type;
17195
17196 type = read_type_die (die, cu);
17197 new_symbol (die, type, cu);
5d7cb8df 17198
5d7cb8df
JK
17199 while (child_die && child_die->tag)
17200 {
17201 process_die (child_die, cu);
436c571c 17202 child_die = child_die->sibling;
5d7cb8df
JK
17203 }
17204}
17205
38d518c9
EZ
17206/* Return the name of the namespace represented by DIE. Set
17207 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17208 namespace. */
17209
17210static const char *
e142c38c 17211namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17212{
17213 struct die_info *current_die;
17214 const char *name = NULL;
17215
17216 /* Loop through the extensions until we find a name. */
17217
17218 for (current_die = die;
17219 current_die != NULL;
f2f0e013 17220 current_die = dwarf2_extension (die, &cu))
38d518c9 17221 {
96553a0c
DE
17222 /* We don't use dwarf2_name here so that we can detect the absence
17223 of a name -> anonymous namespace. */
7d45c7c3 17224 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17225
38d518c9
EZ
17226 if (name != NULL)
17227 break;
17228 }
17229
17230 /* Is it an anonymous namespace? */
17231
17232 *is_anonymous = (name == NULL);
17233 if (*is_anonymous)
2b1dbab0 17234 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17235
17236 return name;
d9fa45fe
DC
17237}
17238
c906108c
SS
17239/* Extract all information from a DW_TAG_pointer_type DIE and add to
17240 the user defined type vector. */
17241
f792889a 17242static struct type *
e7c27a73 17243read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17244{
5e22e966 17245 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17246 struct comp_unit_head *cu_header = &cu->header;
c906108c 17247 struct type *type;
8b2dbe47
KB
17248 struct attribute *attr_byte_size;
17249 struct attribute *attr_address_class;
17250 int byte_size, addr_class;
7e314c57
JK
17251 struct type *target_type;
17252
17253 target_type = die_type (die, cu);
c906108c 17254
7e314c57
JK
17255 /* The die_type call above may have already set the type for this DIE. */
17256 type = get_die_type (die, cu);
17257 if (type)
17258 return type;
17259
17260 type = lookup_pointer_type (target_type);
8b2dbe47 17261
e142c38c 17262 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17263 if (attr_byte_size)
529908cb 17264 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17265 else
8b2dbe47
KB
17266 byte_size = cu_header->addr_size;
17267
e142c38c 17268 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17269 if (attr_address_class)
529908cb 17270 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17271 else
17272 addr_class = DW_ADDR_none;
17273
2b4424c3
TT
17274 ULONGEST alignment = get_alignment (cu, die);
17275
17276 /* If the pointer size, alignment, or address class is different
17277 than the default, create a type variant marked as such and set
17278 the length accordingly. */
17279 if (TYPE_LENGTH (type) != byte_size
17280 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17281 && alignment != TYPE_RAW_ALIGN (type))
17282 || addr_class != DW_ADDR_none)
c906108c 17283 {
5e2b427d 17284 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17285 {
314ad88d
PA
17286 type_instance_flags type_flags
17287 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17288 addr_class);
876cecd0
TT
17289 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17290 == 0);
8b2dbe47
KB
17291 type = make_type_with_address_space (type, type_flags);
17292 }
17293 else if (TYPE_LENGTH (type) != byte_size)
17294 {
b98664d3 17295 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17296 }
2b4424c3
TT
17297 else if (TYPE_RAW_ALIGN (type) != alignment)
17298 {
b98664d3 17299 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17300 " - DIE at %s [in module %s]"),
17301 sect_offset_str (die->sect_off),
5e22e966 17302 objfile_name (cu->per_objfile->objfile));
2b4424c3 17303 }
6e70227d 17304 else
9a619af0
MS
17305 {
17306 /* Should we also complain about unhandled address classes? */
17307 }
c906108c 17308 }
8b2dbe47
KB
17309
17310 TYPE_LENGTH (type) = byte_size;
2b4424c3 17311 set_type_align (type, alignment);
f792889a 17312 return set_die_type (die, type, cu);
c906108c
SS
17313}
17314
17315/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17316 the user defined type vector. */
17317
f792889a 17318static struct type *
e7c27a73 17319read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17320{
17321 struct type *type;
17322 struct type *to_type;
17323 struct type *domain;
17324
e7c27a73
DJ
17325 to_type = die_type (die, cu);
17326 domain = die_containing_type (die, cu);
0d5de010 17327
7e314c57
JK
17328 /* The calls above may have already set the type for this DIE. */
17329 type = get_die_type (die, cu);
17330 if (type)
17331 return type;
17332
78134374 17333 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17334 type = lookup_methodptr_type (to_type);
78134374 17335 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17336 {
5e22e966 17337 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17338
17339 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17340 to_type->fields (), to_type->num_fields (),
a409645d 17341 to_type->has_varargs ());
7078baeb
TT
17342 type = lookup_methodptr_type (new_type);
17343 }
0d5de010
DJ
17344 else
17345 type = lookup_memberptr_type (to_type, domain);
c906108c 17346
f792889a 17347 return set_die_type (die, type, cu);
c906108c
SS
17348}
17349
4297a3f0 17350/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17351 the user defined type vector. */
17352
f792889a 17353static struct type *
4297a3f0 17354read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17355 enum type_code refcode)
c906108c 17356{
e7c27a73 17357 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17358 struct type *type, *target_type;
c906108c
SS
17359 struct attribute *attr;
17360
4297a3f0
AV
17361 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17362
7e314c57
JK
17363 target_type = die_type (die, cu);
17364
17365 /* The die_type call above may have already set the type for this DIE. */
17366 type = get_die_type (die, cu);
17367 if (type)
17368 return type;
17369
4297a3f0 17370 type = lookup_reference_type (target_type, refcode);
e142c38c 17371 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17372 if (attr != nullptr)
c906108c 17373 {
529908cb 17374 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17375 }
17376 else
17377 {
107d2387 17378 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17379 }
2b4424c3 17380 maybe_set_alignment (cu, die, type);
f792889a 17381 return set_die_type (die, type, cu);
c906108c
SS
17382}
17383
cf363f18
MW
17384/* Add the given cv-qualifiers to the element type of the array. GCC
17385 outputs DWARF type qualifiers that apply to an array, not the
17386 element type. But GDB relies on the array element type to carry
17387 the cv-qualifiers. This mimics section 6.7.3 of the C99
17388 specification. */
17389
17390static struct type *
17391add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17392 struct type *base_type, int cnst, int voltl)
17393{
17394 struct type *el_type, *inner_array;
17395
17396 base_type = copy_type (base_type);
17397 inner_array = base_type;
17398
78134374 17399 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17400 {
17401 TYPE_TARGET_TYPE (inner_array) =
17402 copy_type (TYPE_TARGET_TYPE (inner_array));
17403 inner_array = TYPE_TARGET_TYPE (inner_array);
17404 }
17405
17406 el_type = TYPE_TARGET_TYPE (inner_array);
17407 cnst |= TYPE_CONST (el_type);
17408 voltl |= TYPE_VOLATILE (el_type);
17409 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17410
17411 return set_die_type (die, base_type, cu);
17412}
17413
f792889a 17414static struct type *
e7c27a73 17415read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17416{
f792889a 17417 struct type *base_type, *cv_type;
c906108c 17418
e7c27a73 17419 base_type = die_type (die, cu);
7e314c57
JK
17420
17421 /* The die_type call above may have already set the type for this DIE. */
17422 cv_type = get_die_type (die, cu);
17423 if (cv_type)
17424 return cv_type;
17425
2f608a3a
KW
17426 /* In case the const qualifier is applied to an array type, the element type
17427 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17428 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17429 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17430
f792889a
DJ
17431 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17432 return set_die_type (die, cv_type, cu);
c906108c
SS
17433}
17434
f792889a 17435static struct type *
e7c27a73 17436read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17437{
f792889a 17438 struct type *base_type, *cv_type;
c906108c 17439
e7c27a73 17440 base_type = die_type (die, cu);
7e314c57
JK
17441
17442 /* The die_type call above may have already set the type for this DIE. */
17443 cv_type = get_die_type (die, cu);
17444 if (cv_type)
17445 return cv_type;
17446
cf363f18
MW
17447 /* In case the volatile qualifier is applied to an array type, the
17448 element type is so qualified, not the array type (section 6.7.3
17449 of C99). */
78134374 17450 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17451 return add_array_cv_type (die, cu, base_type, 0, 1);
17452
f792889a
DJ
17453 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17454 return set_die_type (die, cv_type, cu);
c906108c
SS
17455}
17456
06d66ee9
TT
17457/* Handle DW_TAG_restrict_type. */
17458
17459static struct type *
17460read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17461{
17462 struct type *base_type, *cv_type;
17463
17464 base_type = die_type (die, cu);
17465
17466 /* The die_type call above may have already set the type for this DIE. */
17467 cv_type = get_die_type (die, cu);
17468 if (cv_type)
17469 return cv_type;
17470
17471 cv_type = make_restrict_type (base_type);
17472 return set_die_type (die, cv_type, cu);
17473}
17474
a2c2acaf
MW
17475/* Handle DW_TAG_atomic_type. */
17476
17477static struct type *
17478read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17479{
17480 struct type *base_type, *cv_type;
17481
17482 base_type = die_type (die, cu);
17483
17484 /* The die_type call above may have already set the type for this DIE. */
17485 cv_type = get_die_type (die, cu);
17486 if (cv_type)
17487 return cv_type;
17488
17489 cv_type = make_atomic_type (base_type);
17490 return set_die_type (die, cv_type, cu);
17491}
17492
c906108c
SS
17493/* Extract all information from a DW_TAG_string_type DIE and add to
17494 the user defined type vector. It isn't really a user defined type,
17495 but it behaves like one, with other DIE's using an AT_user_def_type
17496 attribute to reference it. */
17497
f792889a 17498static struct type *
e7c27a73 17499read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17500{
5e22e966 17501 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17502 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17503 struct type *type, *range_type, *index_type, *char_type;
17504 struct attribute *attr;
216a7e6b
AB
17505 struct dynamic_prop prop;
17506 bool length_is_constant = true;
17507 LONGEST length;
17508
17509 /* There are a couple of places where bit sizes might be made use of
17510 when parsing a DW_TAG_string_type, however, no producer that we know
17511 of make use of these. Handling bit sizes that are a multiple of the
17512 byte size is easy enough, but what about other bit sizes? Lets deal
17513 with that problem when we have to. Warn about these attributes being
17514 unsupported, then parse the type and ignore them like we always
17515 have. */
17516 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17517 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17518 {
17519 static bool warning_printed = false;
17520 if (!warning_printed)
17521 {
17522 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17523 "currently supported on DW_TAG_string_type."));
17524 warning_printed = true;
17525 }
17526 }
c906108c 17527
e142c38c 17528 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17529 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17530 {
17531 /* The string length describes the location at which the length of
17532 the string can be found. The size of the length field can be
17533 specified with one of the attributes below. */
17534 struct type *prop_type;
17535 struct attribute *len
17536 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17537 if (len == nullptr)
17538 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17539 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17540 {
17541 /* Pass 0 as the default as we know this attribute is constant
17542 and the default value will not be returned. */
0826b30a 17543 LONGEST sz = len->constant_value (0);
293e7e51 17544 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17545 }
17546 else
17547 {
17548 /* If the size is not specified then we assume it is the size of
17549 an address on this target. */
293e7e51 17550 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17551 }
17552
17553 /* Convert the attribute into a dynamic property. */
17554 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17555 length = 1;
17556 else
17557 length_is_constant = false;
17558 }
17559 else if (attr != nullptr)
17560 {
17561 /* This DW_AT_string_length just contains the length with no
17562 indirection. There's no need to create a dynamic property in this
17563 case. Pass 0 for the default value as we know it will not be
17564 returned in this case. */
0826b30a 17565 length = attr->constant_value (0);
216a7e6b
AB
17566 }
17567 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17568 {
216a7e6b 17569 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17570 length = attr->constant_value (1);
c906108c
SS
17571 }
17572 else
17573 {
216a7e6b
AB
17574 /* Use 1 as a fallback length if we have nothing else. */
17575 length = 1;
c906108c 17576 }
6ccb9162 17577
46bf5051 17578 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17579 if (length_is_constant)
17580 range_type = create_static_range_type (NULL, index_type, 1, length);
17581 else
17582 {
17583 struct dynamic_prop low_bound;
17584
8c2e4e06 17585 low_bound.set_const_val (1);
216a7e6b
AB
17586 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17587 }
3b7538c0
UW
17588 char_type = language_string_char_type (cu->language_defn, gdbarch);
17589 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17590
f792889a 17591 return set_die_type (die, type, cu);
c906108c
SS
17592}
17593
4d804846
JB
17594/* Assuming that DIE corresponds to a function, returns nonzero
17595 if the function is prototyped. */
17596
17597static int
17598prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17599{
17600 struct attribute *attr;
17601
17602 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17603 if (attr && attr->as_boolean ())
4d804846
JB
17604 return 1;
17605
17606 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17607 is only meaningful for C, but the concept also extends to other
4d804846
JB
17608 languages that allow unprototyped functions (Eg: Objective C).
17609 For all other languages, assume that functions are always
17610 prototyped. */
17611 if (cu->language != language_c
17612 && cu->language != language_objc
17613 && cu->language != language_opencl)
17614 return 1;
17615
17616 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17617 prototyped and unprototyped functions; default to prototyped,
17618 since that is more common in modern code (and RealView warns
17619 about unprototyped functions). */
17620 if (producer_is_realview (cu->producer))
17621 return 1;
17622
17623 return 0;
17624}
17625
c906108c
SS
17626/* Handle DIES due to C code like:
17627
17628 struct foo
c5aa993b
JM
17629 {
17630 int (*funcp)(int a, long l);
17631 int b;
17632 };
c906108c 17633
0963b4bd 17634 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17635
f792889a 17636static struct type *
e7c27a73 17637read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17638{
5e22e966 17639 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17640 struct type *type; /* Type that this function returns. */
17641 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17642 struct attribute *attr;
17643
e7c27a73 17644 type = die_type (die, cu);
7e314c57
JK
17645
17646 /* The die_type call above may have already set the type for this DIE. */
17647 ftype = get_die_type (die, cu);
17648 if (ftype)
17649 return ftype;
17650
0c8b41f1 17651 ftype = lookup_function_type (type);
c906108c 17652
4d804846 17653 if (prototyped_function_p (die, cu))
27e69b7a 17654 ftype->set_is_prototyped (true);
c906108c 17655
c055b101
CV
17656 /* Store the calling convention in the type if it's available in
17657 the subroutine die. Otherwise set the calling convention to
17658 the default value DW_CC_normal. */
17659 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 17660 if (attr != nullptr
529908cb 17661 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 17662 TYPE_CALLING_CONVENTION (ftype)
529908cb 17663 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
17664 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17665 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17666 else
17667 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17668
743649fd
MW
17669 /* Record whether the function returns normally to its caller or not
17670 if the DWARF producer set that information. */
17671 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 17672 if (attr && attr->as_boolean ())
743649fd
MW
17673 TYPE_NO_RETURN (ftype) = 1;
17674
76c10ea2
GM
17675 /* We need to add the subroutine type to the die immediately so
17676 we don't infinitely recurse when dealing with parameters
0963b4bd 17677 declared as the same subroutine type. */
76c10ea2 17678 set_die_type (die, ftype, cu);
6e70227d 17679
639d11d3 17680 if (die->child != NULL)
c906108c 17681 {
bb5ed363 17682 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17683 struct die_info *child_die;
8072405b 17684 int nparams, iparams;
c906108c
SS
17685
17686 /* Count the number of parameters.
dda83cd7
SM
17687 FIXME: GDB currently ignores vararg functions, but knows about
17688 vararg member functions. */
8072405b 17689 nparams = 0;
639d11d3 17690 child_die = die->child;
c906108c
SS
17691 while (child_die && child_die->tag)
17692 {
17693 if (child_die->tag == DW_TAG_formal_parameter)
17694 nparams++;
17695 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
17696 ftype->set_has_varargs (true);
17697
436c571c 17698 child_die = child_die->sibling;
c906108c
SS
17699 }
17700
17701 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17702 ftype->set_num_fields (nparams);
3cabb6b0
SM
17703 ftype->set_fields
17704 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17705
8072405b
JK
17706 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17707 even if we error out during the parameters reading below. */
17708 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17709 ftype->field (iparams).set_type (void_type);
8072405b
JK
17710
17711 iparams = 0;
639d11d3 17712 child_die = die->child;
c906108c
SS
17713 while (child_die && child_die->tag)
17714 {
17715 if (child_die->tag == DW_TAG_formal_parameter)
17716 {
3ce3b1ba
PA
17717 struct type *arg_type;
17718
17719 /* DWARF version 2 has no clean way to discern C++
17720 static and non-static member functions. G++ helps
17721 GDB by marking the first parameter for non-static
17722 member functions (which is the this pointer) as
17723 artificial. We pass this information to
17724 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17725
17726 DWARF version 3 added DW_AT_object_pointer, which GCC
17727 4.5 does not yet generate. */
e142c38c 17728 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17729 if (attr != nullptr)
c45bc3f8 17730 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 17731 else
9c37b5ae 17732 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17733 arg_type = die_type (child_die, cu);
17734
17735 /* RealView does not mark THIS as const, which the testsuite
17736 expects. GCC marks THIS as const in method definitions,
17737 but not in the class specifications (GCC PR 43053). */
17738 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17739 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17740 {
17741 int is_this = 0;
17742 struct dwarf2_cu *arg_cu = cu;
17743 const char *name = dwarf2_name (child_die, cu);
17744
17745 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17746 if (attr != nullptr)
3ce3b1ba
PA
17747 {
17748 /* If the compiler emits this, use it. */
17749 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17750 is_this = 1;
17751 }
17752 else if (name && strcmp (name, "this") == 0)
17753 /* Function definitions will have the argument names. */
17754 is_this = 1;
17755 else if (name == NULL && iparams == 0)
17756 /* Declarations may not have the names, so like
17757 elsewhere in GDB, assume an artificial first
17758 argument is "this". */
17759 is_this = 1;
17760
17761 if (is_this)
17762 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17763 arg_type, 0);
17764 }
17765
5d14b6e5 17766 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17767 iparams++;
17768 }
436c571c 17769 child_die = child_die->sibling;
c906108c
SS
17770 }
17771 }
17772
76c10ea2 17773 return ftype;
c906108c
SS
17774}
17775
f792889a 17776static struct type *
e7c27a73 17777read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17778{
5e22e966 17779 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17780 const char *name = NULL;
3c8e0968 17781 struct type *this_type, *target_type;
c906108c 17782
94af9270 17783 name = dwarf2_full_name (NULL, die, cu);
19f392bc 17784 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 17785 this_type->set_target_is_stub (true);
f792889a 17786 set_die_type (die, this_type, cu);
3c8e0968
DE
17787 target_type = die_type (die, cu);
17788 if (target_type != this_type)
17789 TYPE_TARGET_TYPE (this_type) = target_type;
17790 else
17791 {
17792 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17793 spec and cause infinite loops in GDB. */
b98664d3 17794 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17795 "- DIE at %s [in module %s]"),
17796 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17797 TYPE_TARGET_TYPE (this_type) = NULL;
17798 }
e4003a34
TV
17799 if (name == NULL)
17800 {
17801 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17802 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17803 Handle these by just returning the target type, rather than
17804 constructing an anonymous typedef type and trying to handle this
17805 elsewhere. */
17806 set_die_type (die, target_type, cu);
17807 return target_type;
17808 }
f792889a 17809 return this_type;
c906108c
SS
17810}
17811
a625a8c9
TT
17812/* Helper for get_dwarf2_rational_constant that computes the value of
17813 a given gmp_mpz given an attribute. */
17814
17815static void
17816get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17817{
17818 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17819 location expression that pushes an implicit value. */
17820 if (attr->form == DW_FORM_exprloc)
17821 {
17822 dwarf_block *blk = attr->as_block ();
17823 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17824 {
17825 uint64_t len;
17826 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17827 blk->data + blk->size,
17828 &len);
17829 if (ptr - blk->data + len <= blk->size)
17830 {
17831 mpz_import (value->val, len,
17832 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17833 1, 0, 0, ptr);
17834 return;
17835 }
17836 }
17837
17838 /* On failure set it to 1. */
17839 *value = gdb_mpz (1);
17840 }
17841 else if (attr->form_is_block ())
17842 {
17843 dwarf_block *blk = attr->as_block ();
17844 mpz_import (value->val, blk->size,
17845 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17846 1, 0, 0, blk->data);
17847 }
17848 else
17849 *value = gdb_mpz (attr->constant_value (1));
17850}
17851
09584414
JB
17852/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17853 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17854
17855 If the numerator and/or numerator attribute is missing,
17856 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17857 untouched. */
17858
17859static void
17860get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 17861 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
17862{
17863 struct attribute *num_attr, *denom_attr;
17864
17865 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17866 if (num_attr == nullptr)
17867 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17868 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17869
17870 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17871 if (denom_attr == nullptr)
17872 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17873 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17874
17875 if (num_attr == nullptr || denom_attr == nullptr)
17876 return;
17877
a625a8c9
TT
17878 get_mpz (cu, numerator, num_attr);
17879 get_mpz (cu, denominator, denom_attr);
09584414
JB
17880}
17881
17882/* Same as get_dwarf2_rational_constant, but extracting an unsigned
17883 rational constant, rather than a signed one.
17884
17885 If the rational constant has a negative value, a complaint
17886 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17887
17888static void
17889get_dwarf2_unsigned_rational_constant (struct die_info *die,
17890 struct dwarf2_cu *cu,
5cde1d82
TT
17891 gdb_mpz *numerator,
17892 gdb_mpz *denominator)
09584414 17893{
5cde1d82
TT
17894 gdb_mpz num (1);
17895 gdb_mpz denom (1);
09584414
JB
17896
17897 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 17898 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 17899 {
5cde1d82
TT
17900 mpz_neg (num.val, num.val);
17901 mpz_neg (denom.val, denom.val);
09584414 17902 }
5cde1d82 17903 else if (mpz_sgn (num.val) == -1)
09584414
JB
17904 {
17905 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17906 " in DIE at %s"),
17907 sect_offset_str (die->sect_off));
17908 return;
17909 }
5cde1d82 17910 else if (mpz_sgn (denom.val) == -1)
09584414
JB
17911 {
17912 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17913 " in DIE at %s"),
17914 sect_offset_str (die->sect_off));
17915 return;
17916 }
17917
5cde1d82
TT
17918 *numerator = std::move (num);
17919 *denominator = std::move (denom);
09584414
JB
17920}
17921
bbcdf9ab
TT
17922/* Assuming that ENCODING is a string whose contents starting at the
17923 K'th character is "_nn" where "nn" is a decimal number, scan that
17924 number and set RESULT to the value. K is updated to point to the
17925 character immediately following the number.
17926
17927 If the string does not conform to the format described above, false
17928 is returned, and K may or may not be changed. */
17929
17930static bool
17931ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17932{
17933 /* The next character should be an underscore ('_') followed
17934 by a digit. */
17935 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17936 return false;
17937
17938 /* Skip the underscore. */
17939 k++;
17940 int start = k;
17941
17942 /* Determine the number of digits for our number. */
17943 while (isdigit (encoding[k]))
17944 k++;
17945 if (k == start)
17946 return false;
17947
17948 std::string copy (&encoding[start], k - start);
17949 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17950 return false;
17951
17952 return true;
17953}
17954
17955/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17956 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17957 DENOM, update OFFSET, and return true on success. Return false on
17958 failure. */
17959
17960static bool
17961ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17962 gdb_mpz *num, gdb_mpz *denom)
17963{
17964 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17965 return false;
17966 return ada_get_gnat_encoded_number (encoding, offset, denom);
17967}
17968
09584414 17969/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
17970 of the corresponding TYPE by setting its type-specific data. CU is
17971 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17972 encodings. It is nullptr if the GNAT encoding should be
17973 ignored. */
09584414
JB
17974
17975static void
bbcdf9ab
TT
17976finish_fixed_point_type (struct type *type, const char *suffix,
17977 struct die_info *die, struct dwarf2_cu *cu)
09584414 17978{
09584414
JB
17979 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17980 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17981
bbcdf9ab
TT
17982 /* If GNAT encodings are preferred, don't examine the
17983 attributes. */
17984 struct attribute *attr = nullptr;
17985 if (suffix == nullptr)
17986 {
17987 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17988 if (attr == nullptr)
17989 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17990 if (attr == nullptr)
17991 attr = dwarf2_attr (die, DW_AT_small, cu);
17992 }
09584414 17993
5cde1d82
TT
17994 /* Numerator and denominator of our fixed-point type's scaling factor.
17995 The default is a scaling factor of 1, which we use as a fallback
17996 when we are not able to decode it (problem with the debugging info,
17997 unsupported forms, bug in GDB, etc...). Using that as the default
17998 allows us to at least print the unscaled value, which might still
17999 be useful to a user. */
18000 gdb_mpz scale_num (1);
18001 gdb_mpz scale_denom (1);
18002
09584414
JB
18003 if (attr == nullptr)
18004 {
bbcdf9ab
TT
18005 int offset = 0;
18006 if (suffix != nullptr
18007 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18008 &scale_denom)
18009 /* The number might be encoded as _nn_dd_nn_dd, where the
18010 second ratio is the 'small value. In this situation, we
18011 want the second value. */
18012 && (suffix[offset] != '_'
18013 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18014 &scale_denom)))
18015 {
18016 /* Found it. */
18017 }
18018 else
18019 {
18020 /* Scaling factor not found. Assume a scaling factor of 1,
18021 and hope for the best. At least the user will be able to
18022 see the encoded value. */
18023 scale_num = 1;
18024 scale_denom = 1;
18025 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18026 sect_offset_str (die->sect_off));
18027 }
09584414
JB
18028 }
18029 else if (attr->name == DW_AT_binary_scale)
18030 {
18031 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18032 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18033
5cde1d82 18034 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
18035 }
18036 else if (attr->name == DW_AT_decimal_scale)
18037 {
18038 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18039 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18040
5cde1d82 18041 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
18042 }
18043 else if (attr->name == DW_AT_small)
18044 {
18045 struct die_info *scale_die;
18046 struct dwarf2_cu *scale_cu = cu;
18047
18048 scale_die = follow_die_ref (die, attr, &scale_cu);
18049 if (scale_die->tag == DW_TAG_constant)
18050 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18051 &scale_num, &scale_denom);
18052 else
18053 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18054 " (DIE at %s)"),
18055 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18056 }
18057 else
18058 {
18059 complaint (_("unsupported scale attribute %s for fixed-point type"
18060 " (DIE at %s)"),
18061 dwarf_attr_name (attr->name),
18062 sect_offset_str (die->sect_off));
18063 }
18064
2a12c336 18065 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
18066 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18067 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
18068 mpq_canonicalize (scaling_factor.val);
18069}
18070
bbcdf9ab
TT
18071/* The gnat-encoding suffix for fixed point. */
18072
18073#define GNAT_FIXED_POINT_SUFFIX "___XF_"
18074
18075/* If NAME encodes an Ada fixed-point type, return a pointer to the
18076 "XF" suffix of the name. The text after this is what encodes the
18077 'small and 'delta information. Otherwise, return nullptr. */
18078
18079static const char *
18080gnat_encoded_fixed_point_type_info (const char *name)
18081{
18082 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18083}
18084
9b790ce7
UW
18085/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18086 (which may be different from NAME) to the architecture back-end to allow
18087 it to guess the correct format if necessary. */
18088
18089static struct type *
18090dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18091 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18092{
08feed99 18093 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18094 const struct floatformat **format;
18095 struct type *type;
18096
18097 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18098 if (format)
103a685e 18099 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18100 else
77b7c781 18101 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18102
18103 return type;
18104}
18105
eb77c9df
AB
18106/* Allocate an integer type of size BITS and name NAME. */
18107
18108static struct type *
18109dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18110 int bits, int unsigned_p, const char *name)
18111{
18112 struct type *type;
18113
18114 /* Versions of Intel's C Compiler generate an integer type called "void"
18115 instead of using DW_TAG_unspecified_type. This has been seen on
18116 at least versions 14, 17, and 18. */
35ee2dc2
AB
18117 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18118 && strcmp (name, "void") == 0)
eb77c9df
AB
18119 type = objfile_type (objfile)->builtin_void;
18120 else
18121 type = init_integer_type (objfile, bits, unsigned_p, name);
18122
18123 return type;
18124}
18125
09584414
JB
18126/* Return true if DIE has a DW_AT_small attribute whose value is
18127 a constant rational, where both the numerator and denominator
18128 are equal to zero.
18129
18130 CU is the DIE's Compilation Unit. */
18131
18132static bool
18133has_zero_over_zero_small_attribute (struct die_info *die,
18134 struct dwarf2_cu *cu)
18135{
18136 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18137 if (attr == nullptr)
18138 return false;
18139
18140 struct dwarf2_cu *scale_cu = cu;
18141 struct die_info *scale_die
18142 = follow_die_ref (die, attr, &scale_cu);
18143
18144 if (scale_die->tag != DW_TAG_constant)
18145 return false;
18146
5cde1d82 18147 gdb_mpz num (1), denom (1);
09584414 18148 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 18149 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
18150}
18151
8bdc1658
AB
18152/* Initialise and return a floating point type of size BITS suitable for
18153 use as a component of a complex number. The NAME_HINT is passed through
18154 when initialising the floating point type and is the name of the complex
18155 type.
18156
18157 As DWARF doesn't currently provide an explicit name for the components
18158 of a complex number, but it can be helpful to have these components
18159 named, we try to select a suitable name based on the size of the
18160 component. */
18161static struct type *
18162dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18163 struct objfile *objfile,
103a685e
TT
18164 int bits, const char *name_hint,
18165 enum bfd_endian byte_order)
8bdc1658 18166{
08feed99 18167 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18168 struct type *tt = nullptr;
18169
35add35e
AB
18170 /* Try to find a suitable floating point builtin type of size BITS.
18171 We're going to use the name of this type as the name for the complex
18172 target type that we are about to create. */
1db455a7 18173 switch (cu->language)
8bdc1658 18174 {
1db455a7
AB
18175 case language_fortran:
18176 switch (bits)
18177 {
18178 case 32:
18179 tt = builtin_f_type (gdbarch)->builtin_real;
18180 break;
18181 case 64:
18182 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18183 break;
18184 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18185 case 128:
18186 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18187 break;
18188 }
8bdc1658 18189 break;
1db455a7
AB
18190 default:
18191 switch (bits)
18192 {
18193 case 32:
18194 tt = builtin_type (gdbarch)->builtin_float;
18195 break;
18196 case 64:
18197 tt = builtin_type (gdbarch)->builtin_double;
18198 break;
18199 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18200 case 128:
18201 tt = builtin_type (gdbarch)->builtin_long_double;
18202 break;
18203 }
8bdc1658
AB
18204 break;
18205 }
18206
35add35e
AB
18207 /* If the type we found doesn't match the size we were looking for, then
18208 pretend we didn't find a type at all, the complex target type we
18209 create will then be nameless. */
a12e5744 18210 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18211 tt = nullptr;
18212
7d93a1e0 18213 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18214 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18215}
18216
c906108c
SS
18217/* Find a representation of a given base type and install
18218 it in the TYPE field of the die. */
18219
f792889a 18220static struct type *
e7c27a73 18221read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18222{
5e22e966 18223 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18224 struct type *type;
18225 struct attribute *attr;
19f392bc 18226 int encoding = 0, bits = 0;
15d034d0 18227 const char *name;
34877895 18228 gdbarch *arch;
c906108c 18229
e142c38c 18230 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18231 if (attr != nullptr && attr->form_is_constant ())
18232 encoding = attr->constant_value (0);
e142c38c 18233 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18234 if (attr != nullptr)
529908cb 18235 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18236 name = dwarf2_name (die, cu);
6ccb9162 18237 if (!name)
34877895 18238 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18239
08feed99 18240 arch = objfile->arch ();
103a685e
TT
18241 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18242
34877895 18243 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18244 if (attr != nullptr && attr->form_is_constant ())
103a685e 18245 {
529908cb 18246 int endianity = attr->constant_value (0);
103a685e
TT
18247
18248 switch (endianity)
18249 {
18250 case DW_END_big:
18251 byte_order = BFD_ENDIAN_BIG;
18252 break;
18253 case DW_END_little:
18254 byte_order = BFD_ENDIAN_LITTLE;
18255 break;
18256 default:
18257 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18258 break;
18259 }
18260 }
6ccb9162 18261
09584414
JB
18262 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18263 && cu->language == language_ada
18264 && has_zero_over_zero_small_attribute (die, cu))
18265 {
18266 /* brobecker/2018-02-24: This is a fixed point type for which
18267 the scaling factor is represented as fraction whose value
18268 does not make sense (zero divided by zero), so we should
18269 normally never see these. However, there is a small category
18270 of fixed point types for which GNAT is unable to provide
18271 the scaling factor via the standard DWARF mechanisms, and
18272 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18273 This is likely what this DIE is about. */
09584414
JB
18274 encoding = (encoding == DW_ATE_signed_fixed
18275 ? DW_ATE_signed
18276 : DW_ATE_unsigned);
18277 }
18278
bbcdf9ab
TT
18279 /* With GNAT encodings, fixed-point information will be encoded in
18280 the type name. Note that this can also occur with the above
18281 zero-over-zero case, which is why this is a separate "if" rather
18282 than an "else if". */
18283 const char *gnat_encoding_suffix = nullptr;
18284 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18285 && cu->language == language_ada
18286 && name != nullptr)
18287 {
18288 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18289 if (gnat_encoding_suffix != nullptr)
18290 {
18291 gdb_assert (startswith (gnat_encoding_suffix,
18292 GNAT_FIXED_POINT_SUFFIX));
18293 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18294 name, gnat_encoding_suffix - name);
18295 /* Use -1 here so that SUFFIX points at the "_" after the
18296 "XF". */
18297 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18298
18299 encoding = (encoding == DW_ATE_signed
18300 ? DW_ATE_signed_fixed
18301 : DW_ATE_unsigned_fixed);
18302 }
18303 }
18304
6ccb9162 18305 switch (encoding)
c906108c 18306 {
6ccb9162
UW
18307 case DW_ATE_address:
18308 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18309 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18310 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18311 break;
18312 case DW_ATE_boolean:
19f392bc 18313 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18314 break;
18315 case DW_ATE_complex_float:
103a685e
TT
18316 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18317 byte_order);
78134374 18318 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18319 {
18320 if (name == nullptr)
18321 {
18322 struct obstack *obstack
5e22e966 18323 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18324 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18325 nullptr);
18326 }
18327 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18328 }
18329 else
18330 type = init_complex_type (name, type);
6ccb9162
UW
18331 break;
18332 case DW_ATE_decimal_float:
19f392bc 18333 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18334 break;
18335 case DW_ATE_float:
103a685e 18336 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18337 break;
18338 case DW_ATE_signed:
eb77c9df 18339 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18340 break;
18341 case DW_ATE_unsigned:
3b2b8fea
TT
18342 if (cu->language == language_fortran
18343 && name
61012eef 18344 && startswith (name, "character("))
19f392bc
UW
18345 type = init_character_type (objfile, bits, 1, name);
18346 else
eb77c9df 18347 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18348 break;
18349 case DW_ATE_signed_char:
6e70227d 18350 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18351 || cu->language == language_pascal
18352 || cu->language == language_fortran)
19f392bc
UW
18353 type = init_character_type (objfile, bits, 0, name);
18354 else
eb77c9df 18355 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18356 break;
18357 case DW_ATE_unsigned_char:
868a0084 18358 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18359 || cu->language == language_pascal
c44af4eb
TT
18360 || cu->language == language_fortran
18361 || cu->language == language_rust)
19f392bc
UW
18362 type = init_character_type (objfile, bits, 1, name);
18363 else
eb77c9df 18364 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18365 break;
75079b2b 18366 case DW_ATE_UTF:
53e710ac 18367 {
53e710ac
PA
18368 if (bits == 16)
18369 type = builtin_type (arch)->builtin_char16;
18370 else if (bits == 32)
18371 type = builtin_type (arch)->builtin_char32;
18372 else
18373 {
b98664d3 18374 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18375 bits);
eb77c9df 18376 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18377 }
18378 return set_die_type (die, type, cu);
18379 }
75079b2b 18380 break;
09584414
JB
18381 case DW_ATE_signed_fixed:
18382 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18383 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18384 break;
18385 case DW_ATE_unsigned_fixed:
18386 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18387 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18388 break;
75079b2b 18389
6ccb9162 18390 default:
b98664d3 18391 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18392 dwarf_type_encoding_name (encoding));
77b7c781 18393 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18394 break;
c906108c 18395 }
6ccb9162 18396
0114d602 18397 if (name && strcmp (name, "char") == 0)
15152a54 18398 type->set_has_no_signedness (true);
0114d602 18399
2b4424c3
TT
18400 maybe_set_alignment (cu, die, type);
18401
db558e34 18402 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18403
20a5fcbd
TT
18404 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18405 {
18406 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18407 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18408 {
529908cb 18409 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18410 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18411 /* Only use the attributes if they make sense together. */
18412 if (attr == nullptr
529908cb
TT
18413 || (attr->as_unsigned () + real_bit_size
18414 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18415 {
18416 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18417 = real_bit_size;
18418 if (attr != nullptr)
18419 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18420 = attr->as_unsigned ();
20a5fcbd
TT
18421 }
18422 }
18423 }
18424
f792889a 18425 return set_die_type (die, type, cu);
c906108c
SS
18426}
18427
80180f79
SA
18428/* Parse dwarf attribute if it's a block, reference or constant and put the
18429 resulting value of the attribute into struct bound_prop.
18430 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18431
18432static int
18433attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18434 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18435 struct type *default_type)
80180f79
SA
18436{
18437 struct dwarf2_property_baton *baton;
5e22e966 18438 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18439 struct objfile *objfile = per_objfile->objfile;
18440 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18441
9a49df9d
AB
18442 gdb_assert (default_type != NULL);
18443
80180f79
SA
18444 if (attr == NULL || prop == NULL)
18445 return 0;
18446
4fc6c0d5 18447 if (attr->form_is_block ())
80180f79 18448 {
8d749320 18449 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18450 baton->property_type = default_type;
80180f79 18451 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18452 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18453
18454 struct dwarf_block *block = attr->as_block ();
18455 baton->locexpr.size = block->size;
18456 baton->locexpr.data = block->data;
216a7e6b
AB
18457 switch (attr->name)
18458 {
18459 case DW_AT_string_length:
18460 baton->locexpr.is_reference = true;
18461 break;
18462 default:
18463 baton->locexpr.is_reference = false;
18464 break;
18465 }
8c2e4e06
SM
18466
18467 prop->set_locexpr (baton);
18468 gdb_assert (prop->baton () != NULL);
80180f79 18469 }
cd6c91b4 18470 else if (attr->form_is_ref ())
80180f79
SA
18471 {
18472 struct dwarf2_cu *target_cu = cu;
18473 struct die_info *target_die;
18474 struct attribute *target_attr;
18475
18476 target_die = follow_die_ref (die, attr, &target_cu);
18477 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18478 if (target_attr == NULL)
18479 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18480 target_cu);
80180f79
SA
18481 if (target_attr == NULL)
18482 return 0;
18483
df25ebbd 18484 switch (target_attr->name)
80180f79 18485 {
df25ebbd 18486 case DW_AT_location:
cd6c91b4 18487 if (target_attr->form_is_section_offset ())
df25ebbd 18488 {
8d749320 18489 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18490 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18491 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18492 prop->set_loclist (baton);
18493 gdb_assert (prop->baton () != NULL);
df25ebbd 18494 }
4fc6c0d5 18495 else if (target_attr->form_is_block ())
df25ebbd 18496 {
8d749320 18497 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18498 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18499 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18500 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18501 struct dwarf_block *block = target_attr->as_block ();
18502 baton->locexpr.size = block->size;
18503 baton->locexpr.data = block->data;
9a49df9d 18504 baton->locexpr.is_reference = true;
8c2e4e06
SM
18505 prop->set_locexpr (baton);
18506 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18507 }
18508 else
18509 {
18510 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18511 "dynamic property");
18512 return 0;
18513 }
18514 break;
18515 case DW_AT_data_member_location:
18516 {
18517 LONGEST offset;
18518
18519 if (!handle_data_member_location (target_die, target_cu,
18520 &offset))
18521 return 0;
18522
8d749320 18523 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18524 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18525 target_cu);
df25ebbd
JB
18526 baton->offset_info.offset = offset;
18527 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18528 prop->set_addr_offset (baton);
df25ebbd
JB
18529 break;
18530 }
80180f79
SA
18531 }
18532 }
cd6c91b4 18533 else if (attr->form_is_constant ())
8c2e4e06 18534 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18535 else
18536 {
18537 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18538 dwarf2_name (die, cu));
18539 return 0;
18540 }
18541
18542 return 1;
18543}
18544
09ba997f 18545/* See read.h. */
9a49df9d 18546
09ba997f 18547struct type *
293e7e51 18548dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18549{
9a49df9d
AB
18550 struct type *int_type;
18551
18552 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18553#define TRY_TYPE(F) \
18554 int_type = (unsigned_p \
18555 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18556 : objfile_type (objfile)->builtin_ ## F); \
18557 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18558 return int_type
18559
18560 TRY_TYPE (char);
18561 TRY_TYPE (short);
18562 TRY_TYPE (int);
18563 TRY_TYPE (long);
18564 TRY_TYPE (long_long);
18565
18566#undef TRY_TYPE
18567
18568 gdb_assert_not_reached ("unable to find suitable integer type");
18569}
18570
09ba997f 18571/* See read.h. */
11a8b164 18572
09ba997f 18573struct type *
293e7e51 18574dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18575{
293e7e51
SM
18576 int addr_size = this->per_cu->addr_size ();
18577 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18578}
18579
b86352cf
AB
18580/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18581 present (which is valid) then compute the default type based on the
18582 compilation units address size. */
18583
18584static struct type *
18585read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18586{
18587 struct type *index_type = die_type (die, cu);
18588
18589 /* Dwarf-2 specifications explicitly allows to create subrange types
18590 without specifying a base type.
18591 In that case, the base type must be set to the type of
18592 the lower bound, upper bound or count, in that order, if any of these
18593 three attributes references an object that has a type.
18594 If no base type is found, the Dwarf-2 specifications say that
18595 a signed integer type of size equal to the size of an address should
18596 be used.
18597 For the following C code: `extern char gdb_int [];'
18598 GCC produces an empty range DIE.
18599 FIXME: muller/2010-05-28: Possible references to object for low bound,
18600 high bound or count are not yet handled by this code. */
78134374 18601 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18602 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18603
18604 return index_type;
18605}
18606
a02abb62
JB
18607/* Read the given DW_AT_subrange DIE. */
18608
f792889a 18609static struct type *
a02abb62
JB
18610read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18611{
4c9ad8c2 18612 struct type *base_type, *orig_base_type;
a02abb62
JB
18613 struct type *range_type;
18614 struct attribute *attr;
729efb13 18615 struct dynamic_prop low, high;
4fae6e18 18616 int low_default_is_valid;
c451ebe5 18617 int high_bound_is_count = 0;
15d034d0 18618 const char *name;
d359392f 18619 ULONGEST negative_mask;
e77813c8 18620
b86352cf
AB
18621 orig_base_type = read_subrange_index_type (die, cu);
18622
4c9ad8c2
TT
18623 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18624 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18625 creating the range type, but we use the result of check_typedef
18626 when examining properties of the type. */
18627 base_type = check_typedef (orig_base_type);
a02abb62 18628
7e314c57
JK
18629 /* The die_type call above may have already set the type for this DIE. */
18630 range_type = get_die_type (die, cu);
18631 if (range_type)
18632 return range_type;
18633
8c2e4e06 18634 high.set_const_val (0);
729efb13 18635
4fae6e18
JK
18636 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18637 omitting DW_AT_lower_bound. */
18638 switch (cu->language)
6e70227d 18639 {
4fae6e18
JK
18640 case language_c:
18641 case language_cplus:
8c2e4e06 18642 low.set_const_val (0);
4fae6e18
JK
18643 low_default_is_valid = 1;
18644 break;
18645 case language_fortran:
8c2e4e06 18646 low.set_const_val (1);
4fae6e18
JK
18647 low_default_is_valid = 1;
18648 break;
18649 case language_d:
4fae6e18 18650 case language_objc:
c44af4eb 18651 case language_rust:
8c2e4e06 18652 low.set_const_val (0);
4fae6e18
JK
18653 low_default_is_valid = (cu->header.version >= 4);
18654 break;
18655 case language_ada:
18656 case language_m2:
18657 case language_pascal:
8c2e4e06 18658 low.set_const_val (1);
4fae6e18
JK
18659 low_default_is_valid = (cu->header.version >= 4);
18660 break;
18661 default:
8c2e4e06 18662 low.set_const_val (0);
4fae6e18
JK
18663 low_default_is_valid = 0;
18664 break;
a02abb62
JB
18665 }
18666
e142c38c 18667 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18668 if (attr != nullptr)
9a49df9d 18669 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18670 else if (!low_default_is_valid)
b98664d3 18671 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18672 "- DIE at %s [in module %s]"),
18673 sect_offset_str (die->sect_off),
5e22e966 18674 objfile_name (cu->per_objfile->objfile));
a02abb62 18675
506f5c41
TV
18676 struct attribute *attr_ub, *attr_count;
18677 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18678 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18679 {
506f5c41 18680 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18681 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18682 {
c451ebe5 18683 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18684 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18685 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18686 else
18687 high_bound_is_count = 1;
c2ff108b 18688 }
506f5c41
TV
18689 else
18690 {
18691 if (attr_ub != NULL)
18692 complaint (_("Unresolved DW_AT_upper_bound "
18693 "- DIE at %s [in module %s]"),
18694 sect_offset_str (die->sect_off),
5e22e966 18695 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18696 if (attr_count != NULL)
18697 complaint (_("Unresolved DW_AT_count "
18698 "- DIE at %s [in module %s]"),
18699 sect_offset_str (die->sect_off),
5e22e966 18700 objfile_name (cu->per_objfile->objfile));
506f5c41 18701 }
e77813c8 18702 }
a02abb62 18703
4e962e74
TT
18704 LONGEST bias = 0;
18705 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18706 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18707 bias = bias_attr->constant_value (0);
4e962e74 18708
dbb9c2b1
JB
18709 /* Normally, the DWARF producers are expected to use a signed
18710 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18711 But this is unfortunately not always the case, as witnessed
18712 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18713 is used instead. To work around that ambiguity, we treat
18714 the bounds as signed, and thus sign-extend their values, when
18715 the base type is signed. */
6e70227d 18716 negative_mask =
d359392f 18717 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18718 if (low.kind () == PROP_CONST
c6d940a9 18719 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18720 low.set_const_val (low.const_val () | negative_mask);
18721 if (high.kind () == PROP_CONST
c6d940a9 18722 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18723 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18724
5bbd8269
AB
18725 /* Check for bit and byte strides. */
18726 struct dynamic_prop byte_stride_prop;
18727 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18728 if (attr_byte_stride != nullptr)
18729 {
293e7e51 18730 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18731 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18732 prop_type);
18733 }
18734
18735 struct dynamic_prop bit_stride_prop;
18736 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18737 if (attr_bit_stride != nullptr)
18738 {
18739 /* It only makes sense to have either a bit or byte stride. */
18740 if (attr_byte_stride != nullptr)
18741 {
18742 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18743 "- DIE at %s [in module %s]"),
18744 sect_offset_str (die->sect_off),
5e22e966 18745 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
18746 attr_bit_stride = nullptr;
18747 }
18748 else
18749 {
293e7e51 18750 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18751 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18752 prop_type);
18753 }
18754 }
18755
18756 if (attr_byte_stride != nullptr
18757 || attr_bit_stride != nullptr)
18758 {
18759 bool byte_stride_p = (attr_byte_stride != nullptr);
18760 struct dynamic_prop *stride
18761 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18762
18763 range_type
18764 = create_range_type_with_stride (NULL, orig_base_type, &low,
18765 &high, bias, stride, byte_stride_p);
18766 }
18767 else
18768 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18769
c451ebe5 18770 if (high_bound_is_count)
599088e3 18771 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 18772
c2ff108b
JK
18773 /* Ada expects an empty array on no boundary attributes. */
18774 if (attr == NULL && cu->language != language_ada)
8c2e4e06 18775 range_type->bounds ()->high.set_undefined ();
c2ff108b 18776
39cbfefa
DJ
18777 name = dwarf2_name (die, cu);
18778 if (name)
d0e39ea2 18779 range_type->set_name (name);
6e70227d 18780
e142c38c 18781 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18782 if (attr != nullptr)
529908cb 18783 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 18784
2b4424c3
TT
18785 maybe_set_alignment (cu, die, range_type);
18786
7e314c57
JK
18787 set_die_type (die, range_type, cu);
18788
18789 /* set_die_type should be already done. */
b4ba55a1
JB
18790 set_descriptive_type (range_type, die, cu);
18791
7e314c57 18792 return range_type;
a02abb62 18793}
6e70227d 18794
f792889a 18795static struct type *
81a17f79
JB
18796read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18797{
18798 struct type *type;
81a17f79 18799
5e22e966 18800 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 18801 type->set_name (dwarf2_name (die, cu));
81a17f79 18802
74a2f8ff 18803 /* In Ada, an unspecified type is typically used when the description
85102364 18804 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18805 such a type, we treat it as a stub, and try to resolve it later on,
18806 when needed. */
18807 if (cu->language == language_ada)
b4b73759 18808 type->set_is_stub (true);
74a2f8ff 18809
f792889a 18810 return set_die_type (die, type, cu);
81a17f79 18811}
a02abb62 18812
639d11d3
DC
18813/* Read a single die and all its descendents. Set the die's sibling
18814 field to NULL; set other fields in the die correctly, and set all
18815 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18816 location of the info_ptr after reading all of those dies. PARENT
18817 is the parent of the die in question. */
18818
18819static struct die_info *
dee91e82 18820read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18821 const gdb_byte *info_ptr,
18822 const gdb_byte **new_info_ptr,
dee91e82 18823 struct die_info *parent)
639d11d3
DC
18824{
18825 struct die_info *die;
d521ce57 18826 const gdb_byte *cur_ptr;
639d11d3 18827
3e225074 18828 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18829 if (die == NULL)
18830 {
18831 *new_info_ptr = cur_ptr;
18832 return NULL;
18833 }
93311388 18834 store_in_ref_table (die, reader->cu);
639d11d3 18835
3e225074 18836 if (die->has_children)
bf6af496 18837 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18838 else
18839 {
18840 die->child = NULL;
18841 *new_info_ptr = cur_ptr;
18842 }
18843
18844 die->sibling = NULL;
18845 die->parent = parent;
18846 return die;
18847}
18848
18849/* Read a die, all of its descendents, and all of its siblings; set
18850 all of the fields of all of the dies correctly. Arguments are as
18851 in read_die_and_children. */
18852
18853static struct die_info *
bf6af496 18854read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18855 const gdb_byte *info_ptr,
18856 const gdb_byte **new_info_ptr,
bf6af496 18857 struct die_info *parent)
639d11d3
DC
18858{
18859 struct die_info *first_die, *last_sibling;
d521ce57 18860 const gdb_byte *cur_ptr;
639d11d3 18861
c906108c 18862 cur_ptr = info_ptr;
639d11d3
DC
18863 first_die = last_sibling = NULL;
18864
18865 while (1)
c906108c 18866 {
639d11d3 18867 struct die_info *die
dee91e82 18868 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18869
1d325ec1 18870 if (die == NULL)
c906108c 18871 {
639d11d3
DC
18872 *new_info_ptr = cur_ptr;
18873 return first_die;
c906108c 18874 }
1d325ec1
DJ
18875
18876 if (!first_die)
18877 first_die = die;
c906108c 18878 else
1d325ec1
DJ
18879 last_sibling->sibling = die;
18880
18881 last_sibling = die;
c906108c 18882 }
c906108c
SS
18883}
18884
bf6af496
DE
18885/* Read a die, all of its descendents, and all of its siblings; set
18886 all of the fields of all of the dies correctly. Arguments are as
18887 in read_die_and_children.
18888 This the main entry point for reading a DIE and all its children. */
18889
18890static struct die_info *
18891read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18892 const gdb_byte *info_ptr,
18893 const gdb_byte **new_info_ptr,
bf6af496
DE
18894 struct die_info *parent)
18895{
18896 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18897 new_info_ptr, parent);
18898
b4f54984 18899 if (dwarf_die_debug)
bf6af496
DE
18900 {
18901 fprintf_unfiltered (gdb_stdlog,
18902 "Read die from %s@0x%x of %s:\n",
96b79293 18903 reader->die_section->get_name (),
bf6af496
DE
18904 (unsigned) (info_ptr - reader->die_section->buffer),
18905 bfd_get_filename (reader->abfd));
b4f54984 18906 dump_die (die, dwarf_die_debug);
bf6af496
DE
18907 }
18908
18909 return die;
18910}
18911
3019eac3
DE
18912/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18913 attributes.
18914 The caller is responsible for filling in the extra attributes
18915 and updating (*DIEP)->num_attrs.
18916 Set DIEP to point to a newly allocated die with its information,
3e225074 18917 except for its child, sibling, and parent fields. */
93311388 18918
d521ce57 18919static const gdb_byte *
3019eac3 18920read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18921 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18922 int num_extra_attrs)
93311388 18923{
b64f50a1 18924 unsigned int abbrev_number, bytes_read, i;
7c32eebb 18925 const struct abbrev_info *abbrev;
93311388
DE
18926 struct die_info *die;
18927 struct dwarf2_cu *cu = reader->cu;
18928 bfd *abfd = reader->abfd;
18929
9c541725 18930 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18931 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18932 info_ptr += bytes_read;
18933 if (!abbrev_number)
18934 {
18935 *diep = NULL;
93311388
DE
18936 return info_ptr;
18937 }
18938
685af9cd 18939 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18940 if (!abbrev)
348e048f
DE
18941 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18942 abbrev_number,
18943 bfd_get_filename (abfd));
18944
3019eac3 18945 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18946 die->sect_off = sect_off;
93311388
DE
18947 die->tag = abbrev->tag;
18948 die->abbrev = abbrev_number;
3e225074 18949 die->has_children = abbrev->has_children;
93311388 18950
3019eac3
DE
18951 /* Make the result usable.
18952 The caller needs to update num_attrs after adding the extra
18953 attributes. */
93311388
DE
18954 die->num_attrs = abbrev->num_attrs;
18955
7a5f294d 18956 bool any_need_reprocess = false;
93311388 18957 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 18958 {
7a5f294d
TT
18959 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18960 info_ptr);
18961 if (die->attrs[i].requires_reprocessing_p ())
18962 any_need_reprocess = true;
18a8505e
AT
18963 }
18964
052c8bb8 18965 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
18966 if (attr != nullptr && attr->form_is_unsigned ())
18967 cu->str_offsets_base = attr->as_unsigned ();
93311388 18968
41144253 18969 attr = die->attr (DW_AT_loclists_base);
18970 if (attr != nullptr)
529908cb 18971 cu->loclist_base = attr->as_unsigned ();
41144253 18972
a39fdb41 18973 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18974 if (maybe_addr_base.has_value ())
18975 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
18976
18977 attr = die->attr (DW_AT_rnglists_base);
18978 if (attr != nullptr)
2b0c7f41 18979 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 18980
7a5f294d
TT
18981 if (any_need_reprocess)
18982 {
18983 for (i = 0; i < abbrev->num_attrs; ++i)
18984 {
18985 if (die->attrs[i].requires_reprocessing_p ())
18986 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18987 }
18988 }
93311388 18989 *diep = die;
93311388
DE
18990 return info_ptr;
18991}
18992
3019eac3
DE
18993/* Read a die and all its attributes.
18994 Set DIEP to point to a newly allocated die with its information,
3e225074 18995 except for its child, sibling, and parent fields. */
3019eac3 18996
d521ce57 18997static const gdb_byte *
3019eac3 18998read_full_die (const struct die_reader_specs *reader,
3e225074 18999 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19000{
d521ce57 19001 const gdb_byte *result;
bf6af496 19002
3e225074 19003 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19004
b4f54984 19005 if (dwarf_die_debug)
bf6af496
DE
19006 {
19007 fprintf_unfiltered (gdb_stdlog,
19008 "Read die from %s@0x%x of %s:\n",
96b79293 19009 reader->die_section->get_name (),
bf6af496
DE
19010 (unsigned) (info_ptr - reader->die_section->buffer),
19011 bfd_get_filename (reader->abfd));
b4f54984 19012 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19013 }
19014
19015 return result;
3019eac3 19016}
433df2d4 19017\f
c906108c 19018
72bf9492
DJ
19019/* Returns nonzero if TAG represents a type that we might generate a partial
19020 symbol for. */
19021
19022static int
d8f62e84 19023is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19024{
19025 switch (tag)
19026 {
19027#if 0
19028 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19029 that we don't at present. Note that normally this does not
19030 matter, mainly because C compilers don't give names to these
19031 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19032 case DW_TAG_file_type:
19033 case DW_TAG_ptr_to_member_type:
19034 case DW_TAG_set_type:
19035 case DW_TAG_string_type:
19036 case DW_TAG_subroutine_type:
19037#endif
d8f62e84
TT
19038
19039 /* GNAT may emit an array with a name, but no typedef, so we
19040 need to make a symbol in this case. */
19041 case DW_TAG_array_type:
19042 return lang == language_ada;
19043
72bf9492
DJ
19044 case DW_TAG_base_type:
19045 case DW_TAG_class_type:
680b30c7 19046 case DW_TAG_interface_type:
72bf9492
DJ
19047 case DW_TAG_enumeration_type:
19048 case DW_TAG_structure_type:
19049 case DW_TAG_subrange_type:
19050 case DW_TAG_typedef:
19051 case DW_TAG_union_type:
19052 return 1;
19053 default:
19054 return 0;
19055 }
19056}
19057
19058/* Load all DIEs that are interesting for partial symbols into memory. */
19059
19060static struct partial_die_info *
dee91e82 19061load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19062 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19063{
dee91e82 19064 struct dwarf2_cu *cu = reader->cu;
5e22e966 19065 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19066 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19067 unsigned int bytes_read;
5afb4e99 19068 unsigned int load_all = 0;
72bf9492
DJ
19069 int nesting_level = 1;
19070
19071 parent_die = NULL;
19072 last_die = NULL;
19073
7adf1e79
DE
19074 gdb_assert (cu->per_cu != NULL);
19075 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19076 load_all = 1;
19077
72bf9492
DJ
19078 cu->partial_dies
19079 = htab_create_alloc_ex (cu->header.length / 12,
19080 partial_die_hash,
19081 partial_die_eq,
19082 NULL,
19083 &cu->comp_unit_obstack,
19084 hashtab_obstack_allocate,
19085 dummy_obstack_deallocate);
19086
72bf9492
DJ
19087 while (1)
19088 {
7c32eebb
TT
19089 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19090 &bytes_read);
72bf9492
DJ
19091
19092 /* A NULL abbrev means the end of a series of children. */
19093 if (abbrev == NULL)
19094 {
19095 if (--nesting_level == 0)
cd9983dd
YQ
19096 return first_die;
19097
72bf9492
DJ
19098 info_ptr += bytes_read;
19099 last_die = parent_die;
19100 parent_die = parent_die->die_parent;
19101 continue;
19102 }
19103
98bfdba5
PA
19104 /* Check for template arguments. We never save these; if
19105 they're seen, we just mark the parent, and go on our way. */
19106 if (parent_die != NULL
19107 && cu->language == language_cplus
19108 && (abbrev->tag == DW_TAG_template_type_param
19109 || abbrev->tag == DW_TAG_template_value_param))
19110 {
19111 parent_die->has_template_arguments = 1;
19112
19113 if (!load_all)
19114 {
19115 /* We don't need a partial DIE for the template argument. */
dee91e82 19116 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19117 continue;
19118 }
19119 }
19120
0d99eb77 19121 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19122 Skip their other children. */
19123 if (!load_all
19124 && cu->language == language_cplus
19125 && parent_die != NULL
f9b5d5ea
TV
19126 && parent_die->tag == DW_TAG_subprogram
19127 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19128 {
dee91e82 19129 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19130 continue;
19131 }
19132
5afb4e99
DJ
19133 /* Check whether this DIE is interesting enough to save. Normally
19134 we would not be interested in members here, but there may be
19135 later variables referencing them via DW_AT_specification (for
19136 static members). */
19137 if (!load_all
d8f62e84 19138 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19139 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19140 && abbrev->tag != DW_TAG_enumerator
19141 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19142 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19143 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19144 && abbrev->tag != DW_TAG_variable
5afb4e99 19145 && abbrev->tag != DW_TAG_namespace
f55ee35c 19146 && abbrev->tag != DW_TAG_module
95554aad 19147 && abbrev->tag != DW_TAG_member
74921315
KS
19148 && abbrev->tag != DW_TAG_imported_unit
19149 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19150 {
19151 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19152 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19153 continue;
19154 }
19155
6f06d47b
YQ
19156 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19157 abbrev);
cd9983dd 19158
48fbe735 19159 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19160
19161 /* This two-pass algorithm for processing partial symbols has a
19162 high cost in cache pressure. Thus, handle some simple cases
19163 here which cover the majority of C partial symbols. DIEs
19164 which neither have specification tags in them, nor could have
19165 specification tags elsewhere pointing at them, can simply be
19166 processed and discarded.
19167
19168 This segment is also optional; scan_partial_symbols and
19169 add_partial_symbol will handle these DIEs if we chain
19170 them in normally. When compilers which do not emit large
19171 quantities of duplicate debug information are more common,
19172 this code can probably be removed. */
19173
19174 /* Any complete simple types at the top level (pretty much all
19175 of them, for a language without namespaces), can be processed
19176 directly. */
19177 if (parent_die == NULL
cd9983dd
YQ
19178 && pdi.has_specification == 0
19179 && pdi.is_declaration == 0
19180 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19181 || pdi.tag == DW_TAG_base_type
d8f62e84 19182 || pdi.tag == DW_TAG_array_type
cd9983dd 19183 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19184 {
7d00ffec 19185 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19186 add_partial_symbol (&pdi, cu);
19187
cd9983dd 19188 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19189 continue;
19190 }
19191
d8228535
JK
19192 /* The exception for DW_TAG_typedef with has_children above is
19193 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19194 type_name_or_error will error on such types later.
d8228535
JK
19195
19196 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19197 it could not find the child DIEs referenced later, this is checked
19198 above. In correct DWARF DW_TAG_typedef should have no children. */
19199
cd9983dd 19200 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19201 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19202 "- DIE at %s [in module %s]"),
cd9983dd 19203 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19204
72bf9492
DJ
19205 /* If we're at the second level, and we're an enumerator, and
19206 our parent has no specification (meaning possibly lives in a
19207 namespace elsewhere), then we can add the partial symbol now
19208 instead of queueing it. */
cd9983dd 19209 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19210 && parent_die != NULL
19211 && parent_die->die_parent == NULL
19212 && parent_die->tag == DW_TAG_enumeration_type
19213 && parent_die->has_specification == 0)
19214 {
7d00ffec 19215 if (pdi.raw_name == NULL)
b98664d3 19216 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19217 else if (building_psymtab)
f0fbb768 19218 add_partial_symbol (&pdi, cu);
72bf9492 19219
cd9983dd 19220 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19221 continue;
19222 }
19223
cd9983dd 19224 struct partial_die_info *part_die
6f06d47b 19225 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19226
72bf9492
DJ
19227 /* We'll save this DIE so link it in. */
19228 part_die->die_parent = parent_die;
19229 part_die->die_sibling = NULL;
19230 part_die->die_child = NULL;
19231
19232 if (last_die && last_die == parent_die)
19233 last_die->die_child = part_die;
19234 else if (last_die)
19235 last_die->die_sibling = part_die;
19236
19237 last_die = part_die;
19238
19239 if (first_die == NULL)
19240 first_die = part_die;
19241
19242 /* Maybe add the DIE to the hash table. Not all DIEs that we
19243 find interesting need to be in the hash table, because we
19244 also have the parent/sibling/child chains; only those that we
19245 might refer to by offset later during partial symbol reading.
19246
19247 For now this means things that might have be the target of a
19248 DW_AT_specification, DW_AT_abstract_origin, or
19249 DW_AT_extension. DW_AT_extension will refer only to
19250 namespaces; DW_AT_abstract_origin refers to functions (and
19251 many things under the function DIE, but we do not recurse
19252 into function DIEs during partial symbol reading) and
19253 possibly variables as well; DW_AT_specification refers to
19254 declarations. Declarations ought to have the DW_AT_declaration
19255 flag. It happens that GCC forgets to put it in sometimes, but
19256 only for functions, not for types.
19257
19258 Adding more things than necessary to the hash table is harmless
19259 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19260 wasted time in find_partial_die, when we reread the compilation
19261 unit with load_all_dies set. */
72bf9492 19262
5afb4e99 19263 if (load_all
72929c62 19264 || abbrev->tag == DW_TAG_constant
5afb4e99 19265 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19266 || abbrev->tag == DW_TAG_variable
19267 || abbrev->tag == DW_TAG_namespace
19268 || part_die->is_declaration)
19269 {
19270 void **slot;
19271
19272 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19273 to_underlying (part_die->sect_off),
19274 INSERT);
72bf9492
DJ
19275 *slot = part_die;
19276 }
19277
72bf9492 19278 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19279 we have no reason to follow the children of structures; for other
98bfdba5
PA
19280 languages we have to, so that we can get at method physnames
19281 to infer fully qualified class names, for DW_AT_specification,
19282 and for C++ template arguments. For C++, we also look one level
19283 inside functions to find template arguments (if the name of the
19284 function does not already contain the template arguments).
bc30ff58 19285
0a4b0913
AB
19286 For Ada and Fortran, we need to scan the children of subprograms
19287 and lexical blocks as well because these languages allow the
19288 definition of nested entities that could be interesting for the
19289 debugger, such as nested subprograms for instance. */
72bf9492 19290 if (last_die->has_children
5afb4e99
DJ
19291 && (load_all
19292 || last_die->tag == DW_TAG_namespace
f55ee35c 19293 || last_die->tag == DW_TAG_module
72bf9492 19294 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19295 || (cu->language == language_cplus
19296 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19297 && (last_die->raw_name == NULL
19298 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19299 || (cu->language != language_c
19300 && (last_die->tag == DW_TAG_class_type
680b30c7 19301 || last_die->tag == DW_TAG_interface_type
72bf9492 19302 || last_die->tag == DW_TAG_structure_type
bc30ff58 19303 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19304 || ((cu->language == language_ada
19305 || cu->language == language_fortran)
bc30ff58
JB
19306 && (last_die->tag == DW_TAG_subprogram
19307 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19308 {
19309 nesting_level++;
19310 parent_die = last_die;
19311 continue;
19312 }
19313
19314 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19315 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19316
19317 /* Back to the top, do it again. */
19318 }
19319}
19320
6f06d47b 19321partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19322 const struct abbrev_info *abbrev)
6f06d47b
YQ
19323 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19324{
19325}
19326
7d00ffec
TT
19327/* See class definition. */
19328
19329const char *
19330partial_die_info::name (dwarf2_cu *cu)
19331{
19332 if (!canonical_name && raw_name != nullptr)
19333 {
19334 struct objfile *objfile = cu->per_objfile->objfile;
19335 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19336 canonical_name = 1;
19337 }
19338
19339 return raw_name;
19340}
19341
35cc7ed7
YQ
19342/* Read a minimal amount of information into the minimal die structure.
19343 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19344
48fbe735
YQ
19345const gdb_byte *
19346partial_die_info::read (const struct die_reader_specs *reader,
19347 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19348{
dee91e82 19349 struct dwarf2_cu *cu = reader->cu;
976ca316 19350 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19351 unsigned int i;
c5aa993b 19352 int has_low_pc_attr = 0;
c906108c 19353 int has_high_pc_attr = 0;
91da1414 19354 int high_pc_relative = 0;
c906108c 19355
fd0a254f 19356 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19357 {
e7da7f8f 19358 attribute attr;
7a5f294d 19359 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19360 /* String and address offsets that need to do the reprocessing have
dda83cd7 19361 already been read at this point, so there is no need to wait until
18a8505e 19362 the loop terminates to do the reprocessing. */
7a5f294d 19363 if (attr.requires_reprocessing_p ())
d0ce17d8 19364 read_attribute_reprocess (reader, &attr, tag);
c906108c 19365 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19366 partial symbol table. */
c906108c
SS
19367 switch (attr.name)
19368 {
19369 case DW_AT_name:
48fbe735 19370 switch (tag)
71c25dea
TT
19371 {
19372 case DW_TAG_compile_unit:
95554aad 19373 case DW_TAG_partial_unit:
348e048f 19374 case DW_TAG_type_unit:
71c25dea
TT
19375 /* Compilation units have a DW_AT_name that is a filename, not
19376 a source language identifier. */
19377 case DW_TAG_enumeration_type:
19378 case DW_TAG_enumerator:
19379 /* These tags always have simple identifiers already; no need
19380 to canonicalize them. */
7d00ffec 19381 canonical_name = 1;
2c830f54 19382 raw_name = attr.as_string ();
71c25dea
TT
19383 break;
19384 default:
7d00ffec 19385 canonical_name = 0;
2c830f54 19386 raw_name = attr.as_string ();
71c25dea
TT
19387 break;
19388 }
c906108c 19389 break;
31ef98ae 19390 case DW_AT_linkage_name:
c906108c 19391 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19392 /* Note that both forms of linkage name might appear. We
19393 assume they will be the same, and we only store the last
19394 one we see. */
95f982e5 19395 linkage_name = attr.as_string ();
c906108c
SS
19396 break;
19397 case DW_AT_low_pc:
19398 has_low_pc_attr = 1;
95f982e5 19399 lowpc = attr.as_address ();
c906108c
SS
19400 break;
19401 case DW_AT_high_pc:
19402 has_high_pc_attr = 1;
95f982e5 19403 highpc = attr.as_address ();
cd6c91b4 19404 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19405 high_pc_relative = 1;
c906108c
SS
19406 break;
19407 case DW_AT_location:
dda83cd7
SM
19408 /* Support the .debug_loc offsets. */
19409 if (attr.form_is_block ())
19410 {
9d2246fc 19411 d.locdesc = attr.as_block ();
dda83cd7
SM
19412 }
19413 else if (attr.form_is_section_offset ())
19414 {
4d3c2250 19415 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19416 }
19417 else
19418 {
4d3c2250
KB
19419 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19420 "partial symbol information");
dda83cd7 19421 }
c906108c 19422 break;
c906108c 19423 case DW_AT_external:
c45bc3f8 19424 is_external = attr.as_boolean ();
c906108c
SS
19425 break;
19426 case DW_AT_declaration:
c45bc3f8 19427 is_declaration = attr.as_boolean ();
c906108c
SS
19428 break;
19429 case DW_AT_type:
48fbe735 19430 has_type = 1;
c906108c
SS
19431 break;
19432 case DW_AT_abstract_origin:
19433 case DW_AT_specification:
72bf9492 19434 case DW_AT_extension:
48fbe735 19435 has_specification = 1;
0826b30a 19436 spec_offset = attr.get_ref_die_offset ();
48fbe735 19437 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19438 || cu->per_cu->is_dwz);
c906108c
SS
19439 break;
19440 case DW_AT_sibling:
19441 /* Ignore absolute siblings, they might point outside of
19442 the current compile unit. */
19443 if (attr.form == DW_FORM_ref_addr)
b98664d3 19444 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19445 else
b9502d3f 19446 {
48fbe735 19447 const gdb_byte *buffer = reader->buffer;
0826b30a 19448 sect_offset off = attr.get_ref_die_offset ();
9c541725 19449 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19450
19451 if (sibling_ptr < info_ptr)
b98664d3 19452 complaint (_("DW_AT_sibling points backwards"));
22869d73 19453 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19454 reader->die_section->overflow_complaint ();
b9502d3f 19455 else
48fbe735 19456 sibling = sibling_ptr;
b9502d3f 19457 }
c906108c 19458 break;
dda83cd7
SM
19459 case DW_AT_byte_size:
19460 has_byte_size = 1;
19461 break;
19462 case DW_AT_const_value:
19463 has_const_value = 1;
19464 break;
68511cec
CES
19465 case DW_AT_calling_convention:
19466 /* DWARF doesn't provide a way to identify a program's source-level
19467 entry point. DW_AT_calling_convention attributes are only meant
19468 to describe functions' calling conventions.
19469
19470 However, because it's a necessary piece of information in
0c1b455e
TT
19471 Fortran, and before DWARF 4 DW_CC_program was the only
19472 piece of debugging information whose definition refers to
19473 a 'main program' at all, several compilers marked Fortran
19474 main programs with DW_CC_program --- even when those
19475 functions use the standard calling conventions.
19476
19477 Although DWARF now specifies a way to provide this
19478 information, we support this practice for backward
19479 compatibility. */
529908cb 19480 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19481 && cu->language == language_fortran)
48fbe735 19482 main_subprogram = 1;
68511cec 19483 break;
481860b3 19484 case DW_AT_inline:
529908cb
TT
19485 {
19486 LONGEST value = attr.constant_value (-1);
19487 if (value == DW_INL_inlined
19488 || value == DW_INL_declared_inlined)
19489 may_be_inlined = 1;
19490 }
481860b3 19491 break;
95554aad
TT
19492
19493 case DW_AT_import:
48fbe735 19494 if (tag == DW_TAG_imported_unit)
36586728 19495 {
0826b30a 19496 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19497 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19498 || cu->per_cu->is_dwz);
19499 }
95554aad
TT
19500 break;
19501
0c1b455e 19502 case DW_AT_main_subprogram:
c45bc3f8 19503 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19504 break;
19505
05caa1d2
TT
19506 case DW_AT_ranges:
19507 {
2b0c7f41
SM
19508 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19509 on DWARF version). */
19510 ULONGEST ranges_offset = attr.as_unsigned ();
19511
19512 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19513 this value. */
19514 if (tag != DW_TAG_compile_unit)
19515 ranges_offset += cu->gnu_ranges_base;
19516
05caa1d2 19517 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19518 nullptr, tag))
05caa1d2
TT
19519 has_pc_info = 1;
19520 }
19521 break;
19522
c906108c
SS
19523 default:
19524 break;
19525 }
19526 }
19527
10d06d82
TT
19528 /* For Ada, if both the name and the linkage name appear, we prefer
19529 the latter. This lets "catch exception" work better, regardless
19530 of the order in which the name and linkage name were emitted.
19531 Really, though, this is just a workaround for the fact that gdb
19532 doesn't store both the name and the linkage name. */
19533 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19534 raw_name = linkage_name;
10d06d82 19535
91da1414 19536 if (high_pc_relative)
48fbe735 19537 highpc += lowpc;
91da1414 19538
9373cf26
JK
19539 if (has_low_pc_attr && has_high_pc_attr)
19540 {
19541 /* When using the GNU linker, .gnu.linkonce. sections are used to
19542 eliminate duplicate copies of functions and vtables and such.
19543 The linker will arbitrarily choose one and discard the others.
19544 The AT_*_pc values for such functions refer to local labels in
19545 these sections. If the section from that file was discarded, the
19546 labels are not in the output, so the relocs get a value of 0.
19547 If this is a discarded function, mark the pc bounds as invalid,
19548 so that GDB will ignore it. */
976ca316 19549 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19550 {
976ca316 19551 struct objfile *objfile = per_objfile->objfile;
08feed99 19552 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19553
b98664d3 19554 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19555 "for DIE at %s [in module %s]"),
48fbe735
YQ
19556 paddress (gdbarch, lowpc),
19557 sect_offset_str (sect_off),
9d8780f0 19558 objfile_name (objfile));
9373cf26
JK
19559 }
19560 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19561 else if (lowpc >= highpc)
9373cf26 19562 {
976ca316 19563 struct objfile *objfile = per_objfile->objfile;
08feed99 19564 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19565
b98664d3 19566 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19567 "for DIE at %s [in module %s]"),
48fbe735
YQ
19568 paddress (gdbarch, lowpc),
19569 paddress (gdbarch, highpc),
19570 sect_offset_str (sect_off),
9c541725 19571 objfile_name (objfile));
9373cf26
JK
19572 }
19573 else
48fbe735 19574 has_pc_info = 1;
9373cf26 19575 }
85cbf3d3 19576
c906108c
SS
19577 return info_ptr;
19578}
19579
72bf9492
DJ
19580/* Find a cached partial DIE at OFFSET in CU. */
19581
d590ff25
YQ
19582struct partial_die_info *
19583dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19584{
19585 struct partial_die_info *lookup_die = NULL;
6f06d47b 19586 struct partial_die_info part_die (sect_off);
72bf9492 19587
9a3c8263 19588 lookup_die = ((struct partial_die_info *)
d590ff25 19589 htab_find_with_hash (partial_dies, &part_die,
9c541725 19590 to_underlying (sect_off)));
72bf9492 19591
72bf9492
DJ
19592 return lookup_die;
19593}
19594
348e048f
DE
19595/* Find a partial DIE at OFFSET, which may or may not be in CU,
19596 except in the case of .debug_types DIEs which do not reference
19597 outside their CU (they do however referencing other types via
55f1336d 19598 DW_FORM_ref_sig8). */
72bf9492 19599
122cf0f2 19600static const struct cu_partial_die_info
9c541725 19601find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19602{
976ca316
SM
19603 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19604 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19605 struct partial_die_info *pd = NULL;
72bf9492 19606
36586728 19607 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19608 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19609 {
d590ff25 19610 pd = cu->find_partial_die (sect_off);
5afb4e99 19611 if (pd != NULL)
fb816e8b 19612 return { cu, pd };
0d99eb77
DE
19613 /* We missed recording what we needed.
19614 Load all dies and try again. */
5afb4e99 19615 }
0d99eb77
DE
19616 else
19617 {
19618 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19619 if (cu->per_cu->is_debug_types)
0d99eb77 19620 {
9d8780f0
SM
19621 error (_("Dwarf Error: Type Unit at offset %s contains"
19622 " external reference to offset %s [in module %s].\n"),
19623 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19624 bfd_get_filename (objfile->obfd));
19625 }
7188ed02
SM
19626 dwarf2_per_cu_data *per_cu
19627 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19628 per_objfile);
72bf9492 19629
976ca316 19630 cu = per_objfile->get_cu (per_cu);
7188ed02 19631 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19632 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19633
976ca316 19634 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19635
19636 cu->last_used = 0;
19637 pd = cu->find_partial_die (sect_off);
0d99eb77 19638 }
5afb4e99 19639
dee91e82
DE
19640 /* If we didn't find it, and not all dies have been loaded,
19641 load them all and try again. */
19642
7188ed02 19643 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 19644 {
7188ed02 19645 cu->per_cu->load_all_dies = 1;
fd820528
DE
19646
19647 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19648 THIS_CU->cu may already be in use. So we can't just free it and
19649 replace its DIEs with the ones we read in. Instead, we leave those
19650 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19651 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19652 set. */
976ca316 19653 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19654
7188ed02 19655 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19656 }
19657
19658 if (pd == NULL)
521894aa 19659 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 19660 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19661 return { cu, pd };
72bf9492
DJ
19662}
19663
abc72ce4
DE
19664/* See if we can figure out if the class lives in a namespace. We do
19665 this by looking for a member function; its demangled name will
19666 contain namespace info, if there is any. */
19667
19668static void
19669guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19670 struct dwarf2_cu *cu)
19671{
19672 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19673 what template types look like, because the demangler
19674 frequently doesn't give the same name as the debug info. We
19675 could fix this by only using the demangled name to get the
19676 prefix (but see comment in read_structure_type). */
19677
19678 struct partial_die_info *real_pdi;
19679 struct partial_die_info *child_pdi;
19680
19681 /* If this DIE (this DIE's specification, if any) has a parent, then
19682 we should not do this. We'll prepend the parent's fully qualified
19683 name when we create the partial symbol. */
19684
19685 real_pdi = struct_pdi;
19686 while (real_pdi->has_specification)
fb816e8b 19687 {
122cf0f2
AB
19688 auto res = find_partial_die (real_pdi->spec_offset,
19689 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19690 real_pdi = res.pdi;
19691 cu = res.cu;
19692 }
abc72ce4
DE
19693
19694 if (real_pdi->die_parent != NULL)
19695 return;
19696
19697 for (child_pdi = struct_pdi->die_child;
19698 child_pdi != NULL;
19699 child_pdi = child_pdi->die_sibling)
19700 {
19701 if (child_pdi->tag == DW_TAG_subprogram
19702 && child_pdi->linkage_name != NULL)
19703 {
43816ebc 19704 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19705 (cu->language_defn->class_name_from_physname
19706 (child_pdi->linkage_name));
abc72ce4
DE
19707 if (actual_class_name != NULL)
19708 {
5e22e966 19709 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19710 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19711 struct_pdi->canonical_name = 1;
abc72ce4
DE
19712 }
19713 break;
19714 }
19715 }
19716}
19717
25c11aca
TV
19718/* Return true if a DIE with TAG may have the DW_AT_const_value
19719 attribute. */
19720
19721static bool
19722can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19723{
19724 switch (tag)
19725 {
19726 case DW_TAG_constant:
19727 case DW_TAG_enumerator:
19728 case DW_TAG_formal_parameter:
19729 case DW_TAG_template_value_param:
19730 case DW_TAG_variable:
19731 return true;
19732 }
19733
19734 return false;
19735}
19736
52356b79
YQ
19737void
19738partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19739{
abc72ce4
DE
19740 /* Once we've fixed up a die, there's no point in doing so again.
19741 This also avoids a memory leak if we were to call
19742 guess_partial_die_structure_name multiple times. */
52356b79 19743 if (fixup_called)
abc72ce4
DE
19744 return;
19745
72bf9492
DJ
19746 /* If we found a reference attribute and the DIE has no name, try
19747 to find a name in the referred to DIE. */
19748
7d00ffec 19749 if (raw_name == NULL && has_specification)
72bf9492
DJ
19750 {
19751 struct partial_die_info *spec_die;
72bf9492 19752
122cf0f2 19753 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19754 spec_die = res.pdi;
19755 cu = res.cu;
72bf9492 19756
52356b79 19757 spec_die->fixup (cu);
72bf9492 19758
7d00ffec 19759 if (spec_die->raw_name)
72bf9492 19760 {
7d00ffec
TT
19761 raw_name = spec_die->raw_name;
19762 canonical_name = spec_die->canonical_name;
72bf9492
DJ
19763
19764 /* Copy DW_AT_external attribute if it is set. */
19765 if (spec_die->is_external)
52356b79 19766 is_external = spec_die->is_external;
72bf9492
DJ
19767 }
19768 }
19769
25c11aca
TV
19770 if (!has_const_value && has_specification
19771 && can_have_DW_AT_const_value_p (tag))
19772 {
19773 struct partial_die_info *spec_die;
19774
19775 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19776 spec_die = res.pdi;
19777 cu = res.cu;
19778
19779 spec_die->fixup (cu);
19780
19781 if (spec_die->has_const_value)
19782 {
19783 /* Copy DW_AT_const_value attribute if it is set. */
19784 has_const_value = spec_die->has_const_value;
19785 }
19786 }
19787
72bf9492 19788 /* Set default names for some unnamed DIEs. */
72bf9492 19789
7d00ffec
TT
19790 if (raw_name == NULL && tag == DW_TAG_namespace)
19791 {
19792 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19793 canonical_name = 1;
19794 }
72bf9492 19795
abc72ce4
DE
19796 /* If there is no parent die to provide a namespace, and there are
19797 children, see if we can determine the namespace from their linkage
122d1940 19798 name. */
abc72ce4 19799 if (cu->language == language_cplus
5e22e966 19800 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
19801 && die_parent == NULL
19802 && has_children
19803 && (tag == DW_TAG_class_type
19804 || tag == DW_TAG_structure_type
19805 || tag == DW_TAG_union_type))
19806 guess_partial_die_structure_name (this, cu);
abc72ce4 19807
53832f31
TT
19808 /* GCC might emit a nameless struct or union that has a linkage
19809 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 19810 if (raw_name == NULL
52356b79
YQ
19811 && (tag == DW_TAG_class_type
19812 || tag == DW_TAG_interface_type
19813 || tag == DW_TAG_structure_type
19814 || tag == DW_TAG_union_type)
19815 && linkage_name != NULL)
53832f31 19816 {
43816ebc
TT
19817 gdb::unique_xmalloc_ptr<char> demangled
19818 (gdb_demangle (linkage_name, DMGL_TYPES));
19819 if (demangled != nullptr)
53832f31 19820 {
96408a79
SA
19821 const char *base;
19822
19823 /* Strip any leading namespaces/classes, keep only the base name.
19824 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19825 base = strrchr (demangled.get (), ':');
19826 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19827 base++;
19828 else
43816ebc 19829 base = demangled.get ();
96408a79 19830
5e22e966 19831 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19832 raw_name = objfile->intern (base);
19833 canonical_name = 1;
53832f31
TT
19834 }
19835 }
19836
52356b79 19837 fixup_called = 1;
72bf9492
DJ
19838}
19839
d0ce17d8 19840/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
19841 contents from the given SECTION in the HEADER.
19842
19843 HEADER_OFFSET is the offset of the header in the section. */
41144253 19844static void
d0ce17d8 19845read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
19846 struct dwarf2_section_info *section,
19847 sect_offset header_offset)
41144253 19848{
19849 unsigned int bytes_read;
19850 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
19851 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19852
41144253 19853 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19854 info_ptr += bytes_read;
a1c40103 19855
41144253 19856 header->version = read_2_bytes (abfd, info_ptr);
19857 info_ptr += 2;
a1c40103 19858
41144253 19859 header->addr_size = read_1_byte (abfd, info_ptr);
19860 info_ptr += 1;
a1c40103 19861
41144253 19862 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19863 info_ptr += 1;
a1c40103 19864
41144253 19865 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19866}
19867
19868/* Return the DW_AT_loclists_base value for the CU. */
19869static ULONGEST
19870lookup_loclist_base (struct dwarf2_cu *cu)
19871{
19872 /* For the .dwo unit, the loclist_base points to the first offset following
19873 the header. The header consists of the following entities-
19874 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19875 bit format)
19876 2. version (2 bytes)
19877 3. address size (1 byte)
19878 4. segment selector size (1 byte)
19879 5. offset entry count (4 bytes)
19880 These sizes are derived as per the DWARFv5 standard. */
19881 if (cu->dwo_unit != nullptr)
19882 {
19883 if (cu->header.initial_length_size == 4)
19884 return LOCLIST_HEADER_SIZE32;
19885 return LOCLIST_HEADER_SIZE64;
19886 }
19887 return cu->loclist_base;
19888}
19889
19890/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19891 array of offsets in the .debug_loclists section. */
e57933dc
SM
19892
19893static sect_offset
41144253 19894read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19895{
976ca316
SM
19896 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19897 struct objfile *objfile = per_objfile->objfile;
41144253 19898 bfd *abfd = objfile->obfd;
a1c40103
SM
19899 ULONGEST loclist_header_size =
19900 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19901 : LOCLIST_HEADER_SIZE64);
41144253 19902 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
19903
19904 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19905 ULONGEST start_offset =
19906 loclist_base + loclist_index * cu->header.offset_size;
19907
a1c40103 19908 /* Get loclists section. */
41144253 19909 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19910
a1c40103 19911 /* Read the loclists section content. */
41144253 19912 section->read (objfile);
19913 if (section->buffer == NULL)
a0c1eeba
SM
19914 error (_("DW_FORM_loclistx used without .debug_loclists "
19915 "section [in module %s]"), objfile_name (objfile));
19916
a1c40103
SM
19917 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19918 so if loclist_base is smaller than the header size, we have a problem. */
19919 if (loclist_base < loclist_header_size)
19920 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19921 objfile_name (objfile));
19922
19923 /* Read the header of the loclists contribution. */
d0ce17d8 19924 struct loclists_rnglists_header header;
a1c40103
SM
19925 read_loclists_rnglists_header (&header, section,
19926 (sect_offset) (loclist_base - loclist_header_size));
19927
19928 /* Verify the loclist index is valid. */
41144253 19929 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
19930 error (_("DW_FORM_loclistx pointing outside of "
19931 ".debug_loclists offset array [in module %s]"),
19932 objfile_name (objfile));
19933
05787bad
SM
19934 /* Validate that reading won't go beyond the end of the section. */
19935 if (start_offset + cu->header.offset_size > section->size)
19936 error (_("Reading DW_FORM_loclistx index beyond end of"
19937 ".debug_loclists section [in module %s]"),
19938 objfile_name (objfile));
19939
19940 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 19941
19942 if (cu->header.offset_size == 4)
e57933dc 19943 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 19944 else
e57933dc 19945 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 19946}
19947
d0ce17d8
CT
19948/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19949 array of offsets in the .debug_rnglists section. */
e57933dc
SM
19950
19951static sect_offset
d0ce17d8
CT
19952read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19953 dwarf_tag tag)
19954{
19955 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19956 struct objfile *objfile = dwarf2_per_objfile->objfile;
19957 bfd *abfd = objfile->obfd;
19958 ULONGEST rnglist_header_size =
19959 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19960 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
19961
19962 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19963 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19964 doesn't apply. */
d0ce17d8 19965 ULONGEST rnglist_base =
2b0c7f41 19966 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
19967
19968 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
19969 ULONGEST start_offset =
19970 rnglist_base + rnglist_index * cu->header.offset_size;
19971
19972 /* Get rnglists section. */
19973 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19974
19975 /* Read the rnglists section content. */
19976 section->read (objfile);
19977 if (section->buffer == nullptr)
19978 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19979 "[in module %s]"),
19980 objfile_name (objfile));
19981
a1c40103
SM
19982 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19983 so if rnglist_base is smaller than the header size, we have a problem. */
19984 if (rnglist_base < rnglist_header_size)
19985 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19986 objfile_name (objfile));
19987
19988 /* Read the header of the rnglists contribution. */
d0ce17d8 19989 struct loclists_rnglists_header header;
a1c40103
SM
19990 read_loclists_rnglists_header (&header, section,
19991 (sect_offset) (rnglist_base - rnglist_header_size));
19992
19993 /* Verify the rnglist index is valid. */
d0ce17d8
CT
19994 if (rnglist_index >= header.offset_entry_count)
19995 error (_("DW_FORM_rnglistx index pointing outside of "
19996 ".debug_rnglists offset array [in module %s]"),
19997 objfile_name (objfile));
19998
d0ce17d8 19999 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 20000 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
20001 error (_("Reading DW_FORM_rnglistx index beyond end of"
20002 ".debug_rnglists section [in module %s]"),
20003 objfile_name (objfile));
20004
20005 const gdb_byte *info_ptr = section->buffer + start_offset;
20006
20007 if (cu->header.offset_size == 4)
e57933dc 20008 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 20009 else
e57933dc 20010 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
20011}
20012
18a8505e
AT
20013/* Process the attributes that had to be skipped in the first round. These
20014 attributes are the ones that need str_offsets_base or addr_base attributes.
20015 They could not have been processed in the first round, because at the time
20016 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20017static void
20018read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20019 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20020{
20021 struct dwarf2_cu *cu = reader->cu;
20022 switch (attr->form)
20023 {
20024 case DW_FORM_addrx:
20025 case DW_FORM_GNU_addr_index:
36d378cf
TT
20026 attr->set_address (read_addr_index (cu,
20027 attr->as_unsigned_reprocess ()));
dda83cd7 20028 break;
41144253 20029 case DW_FORM_loclistx:
b1829e1b 20030 {
e57933dc 20031 sect_offset loclists_sect_off
b1829e1b
SM
20032 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20033
e57933dc 20034 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
20035 }
20036 break;
d0ce17d8 20037 case DW_FORM_rnglistx:
b1829e1b 20038 {
e57933dc 20039 sect_offset rnglists_sect_off
b1829e1b
SM
20040 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20041
e57933dc 20042 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 20043 }
dda83cd7 20044 break;
18a8505e
AT
20045 case DW_FORM_strx:
20046 case DW_FORM_strx1:
20047 case DW_FORM_strx2:
20048 case DW_FORM_strx3:
20049 case DW_FORM_strx4:
20050 case DW_FORM_GNU_str_index:
20051 {
fe56917a 20052 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20053 gdb_assert (!attr->canonical_string_p ());
18a8505e 20054 if (reader->dwo_file != NULL)
c6481205
TT
20055 attr->set_string_noncanonical (read_dwo_str_index (reader,
20056 str_index));
18a8505e 20057 else
c6481205
TT
20058 attr->set_string_noncanonical (read_stub_str_index (cu,
20059 str_index));
18a8505e
AT
20060 break;
20061 }
20062 default:
20063 gdb_assert_not_reached (_("Unexpected DWARF form."));
20064 }
20065}
20066
a8329558 20067/* Read an attribute value described by an attribute form. */
c906108c 20068
d521ce57 20069static const gdb_byte *
dee91e82
DE
20070read_attribute_value (const struct die_reader_specs *reader,
20071 struct attribute *attr, unsigned form,
7a5f294d 20072 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20073{
dee91e82 20074 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20075 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20076 struct objfile *objfile = per_objfile->objfile;
dee91e82 20077 bfd *abfd = reader->abfd;
e7c27a73 20078 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20079 unsigned int bytes_read;
20080 struct dwarf_block *blk;
20081
aead7601 20082 attr->form = (enum dwarf_form) form;
a8329558 20083 switch (form)
c906108c 20084 {
c906108c 20085 case DW_FORM_ref_addr:
7c290a04
TT
20086 if (cu_header->version == 2)
20087 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 20088 &bytes_read));
ae411497 20089 else
7c290a04 20090 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20091 &bytes_read));
ae411497
TT
20092 info_ptr += bytes_read;
20093 break;
36586728 20094 case DW_FORM_GNU_ref_alt:
7c290a04 20095 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20096 &bytes_read));
36586728
TT
20097 info_ptr += bytes_read;
20098 break;
ae411497 20099 case DW_FORM_addr:
08feed99
TT
20100 {
20101 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 20102 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
20103 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20104 attr->set_address (addr);
08feed99
TT
20105 info_ptr += bytes_read;
20106 }
c906108c
SS
20107 break;
20108 case DW_FORM_block2:
7b5a2f43 20109 blk = dwarf_alloc_block (cu);
c906108c
SS
20110 blk->size = read_2_bytes (abfd, info_ptr);
20111 info_ptr += 2;
20112 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20113 info_ptr += blk->size;
9d2246fc 20114 attr->set_block (blk);
c906108c
SS
20115 break;
20116 case DW_FORM_block4:
7b5a2f43 20117 blk = dwarf_alloc_block (cu);
c906108c
SS
20118 blk->size = read_4_bytes (abfd, info_ptr);
20119 info_ptr += 4;
20120 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20121 info_ptr += blk->size;
9d2246fc 20122 attr->set_block (blk);
c906108c
SS
20123 break;
20124 case DW_FORM_data2:
414ad644 20125 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20126 info_ptr += 2;
20127 break;
20128 case DW_FORM_data4:
414ad644 20129 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20130 info_ptr += 4;
20131 break;
20132 case DW_FORM_data8:
414ad644 20133 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20134 info_ptr += 8;
20135 break;
0224619f
JK
20136 case DW_FORM_data16:
20137 blk = dwarf_alloc_block (cu);
20138 blk->size = 16;
20139 blk->data = read_n_bytes (abfd, info_ptr, 16);
20140 info_ptr += 16;
9d2246fc 20141 attr->set_block (blk);
0224619f 20142 break;
2dc7f7b3 20143 case DW_FORM_sec_offset:
7c290a04 20144 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20145 &bytes_read));
2dc7f7b3
TT
20146 info_ptr += bytes_read;
20147 break;
41144253 20148 case DW_FORM_loclistx:
20149 {
7a5f294d
TT
20150 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20151 &bytes_read));
20152 info_ptr += bytes_read;
41144253 20153 }
20154 break;
c906108c 20155 case DW_FORM_string:
c6481205
TT
20156 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20157 &bytes_read));
c906108c
SS
20158 info_ptr += bytes_read;
20159 break;
4bdf3d34 20160 case DW_FORM_strp:
36586728
TT
20161 if (!cu->per_cu->is_dwz)
20162 {
c6481205
TT
20163 attr->set_string_noncanonical
20164 (read_indirect_string (per_objfile,
20165 abfd, info_ptr, cu_header,
20166 &bytes_read));
36586728
TT
20167 info_ptr += bytes_read;
20168 break;
20169 }
20170 /* FALLTHROUGH */
43988095
JK
20171 case DW_FORM_line_strp:
20172 if (!cu->per_cu->is_dwz)
20173 {
c6481205
TT
20174 attr->set_string_noncanonical
20175 (per_objfile->read_line_string (info_ptr, cu_header,
20176 &bytes_read));
43988095
JK
20177 info_ptr += bytes_read;
20178 break;
20179 }
20180 /* FALLTHROUGH */
36586728
TT
20181 case DW_FORM_GNU_strp_alt:
20182 {
a7308ce0 20183 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
20184 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20185 &bytes_read);
36586728 20186
c6481205
TT
20187 attr->set_string_noncanonical
20188 (dwz->read_string (objfile, str_offset));
36586728
TT
20189 info_ptr += bytes_read;
20190 }
4bdf3d34 20191 break;
2dc7f7b3 20192 case DW_FORM_exprloc:
c906108c 20193 case DW_FORM_block:
7b5a2f43 20194 blk = dwarf_alloc_block (cu);
c906108c
SS
20195 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20196 info_ptr += bytes_read;
20197 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20198 info_ptr += blk->size;
9d2246fc 20199 attr->set_block (blk);
c906108c
SS
20200 break;
20201 case DW_FORM_block1:
7b5a2f43 20202 blk = dwarf_alloc_block (cu);
c906108c
SS
20203 blk->size = read_1_byte (abfd, info_ptr);
20204 info_ptr += 1;
20205 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20206 info_ptr += blk->size;
9d2246fc 20207 attr->set_block (blk);
c906108c
SS
20208 break;
20209 case DW_FORM_data1:
c906108c 20210 case DW_FORM_flag:
414ad644 20211 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20212 info_ptr += 1;
20213 break;
2dc7f7b3 20214 case DW_FORM_flag_present:
414ad644 20215 attr->set_unsigned (1);
2dc7f7b3 20216 break;
c906108c 20217 case DW_FORM_sdata:
1bc397c5 20218 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20219 info_ptr += bytes_read;
20220 break;
18a8505e 20221 case DW_FORM_rnglistx:
7a5f294d
TT
20222 {
20223 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20224 &bytes_read));
20225 info_ptr += bytes_read;
20226 }
20227 break;
d0ce17d8 20228 case DW_FORM_udata:
414ad644 20229 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20230 info_ptr += bytes_read;
20231 break;
20232 case DW_FORM_ref1:
7c290a04 20233 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20234 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20235 info_ptr += 1;
20236 break;
20237 case DW_FORM_ref2:
7c290a04 20238 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20239 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20240 info_ptr += 2;
20241 break;
20242 case DW_FORM_ref4:
7c290a04 20243 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20244 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20245 info_ptr += 4;
20246 break;
613e1657 20247 case DW_FORM_ref8:
7c290a04 20248 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20249 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20250 info_ptr += 8;
20251 break;
55f1336d 20252 case DW_FORM_ref_sig8:
630ed6b9 20253 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20254 info_ptr += 8;
20255 break;
c906108c 20256 case DW_FORM_ref_udata:
7c290a04 20257 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
20258 + read_unsigned_leb128 (abfd, info_ptr,
20259 &bytes_read)));
c906108c
SS
20260 info_ptr += bytes_read;
20261 break;
c906108c 20262 case DW_FORM_indirect:
a8329558
KW
20263 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20264 info_ptr += bytes_read;
43988095
JK
20265 if (form == DW_FORM_implicit_const)
20266 {
20267 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20268 info_ptr += bytes_read;
20269 }
20270 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20271 info_ptr);
43988095
JK
20272 break;
20273 case DW_FORM_implicit_const:
1bc397c5 20274 attr->set_signed (implicit_const);
a8329558 20275 break;
336d760d 20276 case DW_FORM_addrx:
3019eac3 20277 case DW_FORM_GNU_addr_index:
fe56917a
TT
20278 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20279 &bytes_read));
3019eac3
DE
20280 info_ptr += bytes_read;
20281 break;
cf532bd1 20282 case DW_FORM_strx:
15f18d14
AT
20283 case DW_FORM_strx1:
20284 case DW_FORM_strx2:
20285 case DW_FORM_strx3:
20286 case DW_FORM_strx4:
3019eac3 20287 case DW_FORM_GNU_str_index:
3019eac3 20288 {
15f18d14
AT
20289 ULONGEST str_index;
20290 if (form == DW_FORM_strx1)
20291 {
20292 str_index = read_1_byte (abfd, info_ptr);
20293 info_ptr += 1;
20294 }
20295 else if (form == DW_FORM_strx2)
20296 {
20297 str_index = read_2_bytes (abfd, info_ptr);
20298 info_ptr += 2;
20299 }
20300 else if (form == DW_FORM_strx3)
20301 {
20302 str_index = read_3_bytes (abfd, info_ptr);
20303 info_ptr += 3;
20304 }
20305 else if (form == DW_FORM_strx4)
20306 {
20307 str_index = read_4_bytes (abfd, info_ptr);
20308 info_ptr += 4;
20309 }
20310 else
20311 {
20312 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20313 info_ptr += bytes_read;
20314 }
fe56917a 20315 attr->set_unsigned_reprocess (str_index);
7a5f294d 20316 }
3019eac3 20317 break;
c906108c 20318 default:
8a3fe4f8 20319 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20320 dwarf_form_name (form),
20321 bfd_get_filename (abfd));
c906108c 20322 }
28e94949 20323
36586728 20324 /* Super hack. */
cd6c91b4 20325 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20326 attr->form = DW_FORM_GNU_ref_alt;
20327
28e94949
JB
20328 /* We have seen instances where the compiler tried to emit a byte
20329 size attribute of -1 which ended up being encoded as an unsigned
20330 0xffffffff. Although 0xffffffff is technically a valid size value,
20331 an object of this size seems pretty unlikely so we can relatively
20332 safely treat these cases as if the size attribute was invalid and
20333 treat them as zero by default. */
20334 if (attr->name == DW_AT_byte_size
20335 && form == DW_FORM_data4
529908cb 20336 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20337 {
20338 complaint
dda83cd7
SM
20339 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20340 hex_string (attr->as_unsigned ()));
414ad644 20341 attr->set_unsigned (0);
01c66ae6 20342 }
28e94949 20343
c906108c
SS
20344 return info_ptr;
20345}
20346
a8329558
KW
20347/* Read an attribute described by an abbreviated attribute. */
20348
d521ce57 20349static const gdb_byte *
dee91e82 20350read_attribute (const struct die_reader_specs *reader,
4444f407 20351 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20352 const gdb_byte *info_ptr)
a8329558
KW
20353{
20354 attr->name = abbrev->name;
c6481205 20355 attr->string_is_canonical = 0;
fe56917a 20356 attr->requires_reprocessing = 0;
43988095 20357 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20358 abbrev->implicit_const, info_ptr);
a8329558
KW
20359}
20360
43988095
JK
20361/* Return pointer to string at .debug_str offset STR_OFFSET. */
20362
20363static const char *
976ca316 20364read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20365 LONGEST str_offset)
43988095 20366{
976ca316
SM
20367 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20368 str_offset, "DW_FORM_strp");
c906108c
SS
20369}
20370
43988095
JK
20371/* Return pointer to string at .debug_str offset as read from BUF.
20372 BUF is assumed to be in a compilation unit described by CU_HEADER.
20373 Return *BYTES_READ_PTR count of bytes read from BUF. */
20374
d521ce57 20375static const char *
976ca316 20376read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20377 const gdb_byte *buf,
cf2c3c16
TT
20378 const struct comp_unit_head *cu_header,
20379 unsigned int *bytes_read_ptr)
20380{
8266302d 20381 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20382
976ca316 20383 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20384}
20385
86c0bb4c 20386/* See read.h. */
43988095 20387
86c0bb4c
TT
20388const char *
20389dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20390 const struct comp_unit_head *cu_header,
20391 unsigned int *bytes_read_ptr)
43988095 20392{
86c0bb4c 20393 bfd *abfd = objfile->obfd;
8266302d 20394 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20395
5989a64e 20396 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20397}
20398
3019eac3 20399/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20400 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20401 ADDR_SIZE is the size of addresses from the CU header. */
20402
20403static CORE_ADDR
976ca316
SM
20404read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20405 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20406{
976ca316 20407 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20408 bfd *abfd = objfile->obfd;
20409 const gdb_byte *info_ptr;
18a8505e 20410 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20411
976ca316
SM
20412 per_objfile->per_bfd->addr.read (objfile);
20413 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20414 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20415 objfile_name (objfile));
18a8505e 20416 if (addr_base_or_zero + addr_index * addr_size
976ca316 20417 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20418 error (_("DW_FORM_addr_index pointing outside of "
20419 ".debug_addr section [in module %s]"),
4262abfb 20420 objfile_name (objfile));
976ca316
SM
20421 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20422 + addr_index * addr_size);
3019eac3
DE
20423 if (addr_size == 4)
20424 return bfd_get_32 (abfd, info_ptr);
20425 else
20426 return bfd_get_64 (abfd, info_ptr);
20427}
20428
20429/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20430
20431static CORE_ADDR
20432read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20433{
5e22e966 20434 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20435 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20436}
20437
20438/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20439
20440static CORE_ADDR
d521ce57 20441read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20442 unsigned int *bytes_read)
20443{
5e22e966 20444 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20445 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20446
20447 return read_addr_index (cu, addr_index);
20448}
20449
450a1bfc 20450/* See read.h. */
3019eac3
DE
20451
20452CORE_ADDR
82ca3f51 20453dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20454 dwarf2_per_objfile *per_objfile,
82ca3f51 20455 unsigned int addr_index)
3019eac3 20456{
976ca316 20457 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20458 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20459 int addr_size;
20460
3019eac3
DE
20461 /* We need addr_base and addr_size.
20462 If we don't have PER_CU->cu, we have to get it.
20463 Nasty, but the alternative is storing the needed info in PER_CU,
20464 which at this point doesn't seem justified: it's not clear how frequently
20465 it would get used and it would increase the size of every PER_CU.
20466 Entry points like dwarf2_per_cu_addr_size do a similar thing
20467 so we're not in uncharted territory here.
20468 Alas we need to be a bit more complicated as addr_base is contained
20469 in the DIE.
20470
20471 We don't need to read the entire CU(/TU).
20472 We just need the header and top level die.
a1b64ce1 20473
3019eac3 20474 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20475 For now we skip this optimization. */
3019eac3
DE
20476
20477 if (cu != NULL)
20478 {
20479 addr_base = cu->addr_base;
20480 addr_size = cu->header.addr_size;
20481 }
20482 else
20483 {
976ca316 20484 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20485 addr_base = reader.cu->addr_base;
20486 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20487 }
20488
976ca316 20489 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20490}
20491
18a8505e
AT
20492/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20493 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20494 DWO file. */
3019eac3 20495
d521ce57 20496static const char *
18a8505e
AT
20497read_str_index (struct dwarf2_cu *cu,
20498 struct dwarf2_section_info *str_section,
20499 struct dwarf2_section_info *str_offsets_section,
20500 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20501{
976ca316
SM
20502 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20503 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20504 const char *objf_name = objfile_name (objfile);
3019eac3 20505 bfd *abfd = objfile->obfd;
d521ce57 20506 const gdb_byte *info_ptr;
3019eac3 20507 ULONGEST str_offset;
cf532bd1 20508 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20509
96b79293
TT
20510 str_section->read (objfile);
20511 str_offsets_section->read (objfile);
73869dc2 20512 if (str_section->buffer == NULL)
18a8505e 20513 error (_("%s used without %s section"
9d8780f0 20514 " in CU at offset %s [in module %s]"),
96b79293 20515 form_name, str_section->get_name (),
dda83cd7 20516 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20517 if (str_offsets_section->buffer == NULL)
18a8505e 20518 error (_("%s used without %s section"
9d8780f0 20519 " in CU at offset %s [in module %s]"),
96b79293 20520 form_name, str_section->get_name (),
dda83cd7 20521 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20522 info_ptr = (str_offsets_section->buffer
18a8505e 20523 + str_offsets_base
3019eac3
DE
20524 + str_index * cu->header.offset_size);
20525 if (cu->header.offset_size == 4)
20526 str_offset = bfd_get_32 (abfd, info_ptr);
20527 else
20528 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20529 if (str_offset >= str_section->size)
57d63ce2 20530 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20531 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20532 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20533 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20534}
20535
18a8505e
AT
20536/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20537
20538static const char *
20539read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20540{
20541 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20542 ? reader->cu->header.addr_size : 0;
20543 return read_str_index (reader->cu,
20544 &reader->dwo_file->sections.str,
20545 &reader->dwo_file->sections.str_offsets,
20546 str_offsets_base, str_index);
20547}
20548
20549/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20550
20551static const char *
20552read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20553{
5e22e966 20554 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20555 const char *objf_name = objfile_name (objfile);
20556 static const char form_name[] = "DW_FORM_GNU_str_index";
20557 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20558
20559 if (!cu->str_offsets_base.has_value ())
20560 error (_("%s used in Fission stub without %s"
20561 " in CU at offset 0x%lx [in module %s]"),
20562 form_name, str_offsets_attr_name,
20563 (long) cu->header.offset_size, objf_name);
20564
20565 return read_str_index (cu,
5e22e966
SM
20566 &cu->per_objfile->per_bfd->str,
20567 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20568 *cu->str_offsets_base, str_index);
20569}
20570
3019eac3
DE
20571/* Return the length of an LEB128 number in BUF. */
20572
20573static int
20574leb128_size (const gdb_byte *buf)
20575{
20576 const gdb_byte *begin = buf;
20577 gdb_byte byte;
20578
20579 while (1)
20580 {
20581 byte = *buf++;
20582 if ((byte & 128) == 0)
20583 return buf - begin;
20584 }
20585}
20586
c906108c 20587static void
e142c38c 20588set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20589{
20590 switch (lang)
20591 {
20592 case DW_LANG_C89:
76bee0cc 20593 case DW_LANG_C99:
0cfd832f 20594 case DW_LANG_C11:
c906108c 20595 case DW_LANG_C:
d1be3247 20596 case DW_LANG_UPC:
e142c38c 20597 cu->language = language_c;
c906108c 20598 break;
9c37b5ae 20599 case DW_LANG_Java:
c906108c 20600 case DW_LANG_C_plus_plus:
0cfd832f
MW
20601 case DW_LANG_C_plus_plus_11:
20602 case DW_LANG_C_plus_plus_14:
e142c38c 20603 cu->language = language_cplus;
c906108c 20604 break;
6aecb9c2
JB
20605 case DW_LANG_D:
20606 cu->language = language_d;
20607 break;
c906108c
SS
20608 case DW_LANG_Fortran77:
20609 case DW_LANG_Fortran90:
b21b22e0 20610 case DW_LANG_Fortran95:
f7de9aab
MW
20611 case DW_LANG_Fortran03:
20612 case DW_LANG_Fortran08:
e142c38c 20613 cu->language = language_fortran;
c906108c 20614 break;
a766d390
DE
20615 case DW_LANG_Go:
20616 cu->language = language_go;
20617 break;
c906108c 20618 case DW_LANG_Mips_Assembler:
e142c38c 20619 cu->language = language_asm;
c906108c
SS
20620 break;
20621 case DW_LANG_Ada83:
8aaf0b47 20622 case DW_LANG_Ada95:
bc5f45f8
JB
20623 cu->language = language_ada;
20624 break;
72019c9c
GM
20625 case DW_LANG_Modula2:
20626 cu->language = language_m2;
20627 break;
fe8e67fd
PM
20628 case DW_LANG_Pascal83:
20629 cu->language = language_pascal;
20630 break;
22566fbd
DJ
20631 case DW_LANG_ObjC:
20632 cu->language = language_objc;
20633 break;
c44af4eb
TT
20634 case DW_LANG_Rust:
20635 case DW_LANG_Rust_old:
20636 cu->language = language_rust;
20637 break;
c906108c
SS
20638 case DW_LANG_Cobol74:
20639 case DW_LANG_Cobol85:
c906108c 20640 default:
e142c38c 20641 cu->language = language_minimal;
c906108c
SS
20642 break;
20643 }
e142c38c 20644 cu->language_defn = language_def (cu->language);
c906108c
SS
20645}
20646
20647/* Return the named attribute or NULL if not there. */
20648
20649static struct attribute *
e142c38c 20650dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20651{
a48e046c 20652 for (;;)
c906108c 20653 {
a48e046c
TT
20654 unsigned int i;
20655 struct attribute *spec = NULL;
20656
20657 for (i = 0; i < die->num_attrs; ++i)
20658 {
20659 if (die->attrs[i].name == name)
20660 return &die->attrs[i];
20661 if (die->attrs[i].name == DW_AT_specification
20662 || die->attrs[i].name == DW_AT_abstract_origin)
20663 spec = &die->attrs[i];
20664 }
20665
20666 if (!spec)
20667 break;
c906108c 20668
f2f0e013 20669 die = follow_die_ref (die, spec, &cu);
f2f0e013 20670 }
c5aa993b 20671
c906108c
SS
20672 return NULL;
20673}
20674
7d45c7c3
KB
20675/* Return the string associated with a string-typed attribute, or NULL if it
20676 is either not found or is of an incorrect type. */
20677
20678static const char *
20679dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20680{
20681 struct attribute *attr;
20682 const char *str = NULL;
20683
20684 attr = dwarf2_attr (die, name, cu);
20685
20686 if (attr != NULL)
20687 {
95f982e5 20688 str = attr->as_string ();
e61108c9 20689 if (str == nullptr)
dda83cd7 20690 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20691 "DIE at %s in module %s"),
20692 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20693 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20694 }
20695
20696 return str;
20697}
20698
a084a2a6 20699/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20700 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20701static const char *
20702dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20703{
20704 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20705 if (dwo_name == nullptr)
20706 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20707 return dwo_name;
20708}
20709
05cf31d1
JB
20710/* Return non-zero iff the attribute NAME is defined for the given DIE,
20711 and holds a non-zero value. This function should only be used for
2dc7f7b3 20712 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20713
20714static int
20715dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20716{
20717 struct attribute *attr = dwarf2_attr (die, name, cu);
20718
c45bc3f8 20719 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
20720}
20721
3ca72b44 20722static int
e142c38c 20723die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20724{
05cf31d1
JB
20725 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20726 which value is non-zero. However, we have to be careful with
20727 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20728 (via dwarf2_flag_true_p) follows this attribute. So we may
20729 end up accidently finding a declaration attribute that belongs
20730 to a different DIE referenced by the specification attribute,
20731 even though the given DIE does not have a declaration attribute. */
20732 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20733 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20734}
20735
63d06c5c 20736/* Return the die giving the specification for DIE, if there is
f2f0e013 20737 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20738 containing the return value on output. If there is no
20739 specification, but there is an abstract origin, that is
20740 returned. */
63d06c5c
DC
20741
20742static struct die_info *
f2f0e013 20743die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20744{
f2f0e013
DJ
20745 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20746 *spec_cu);
63d06c5c 20747
edb3359d
DJ
20748 if (spec_attr == NULL)
20749 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20750
63d06c5c
DC
20751 if (spec_attr == NULL)
20752 return NULL;
20753 else
f2f0e013 20754 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20755}
c906108c 20756
527f3840
JK
20757/* Stub for free_line_header to match void * callback types. */
20758
20759static void
20760free_line_header_voidp (void *arg)
20761{
9a3c8263 20762 struct line_header *lh = (struct line_header *) arg;
527f3840 20763
fff8551c 20764 delete lh;
527f3840
JK
20765}
20766
83769d0b 20767/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20768
20769static struct dwarf2_section_info *
20770get_debug_line_section (struct dwarf2_cu *cu)
20771{
20772 struct dwarf2_section_info *section;
976ca316 20773 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
20774
20775 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20776 DWO file. */
20777 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20778 section = &cu->dwo_unit->dwo_file->sections.line;
20779 else if (cu->per_cu->is_dwz)
20780 {
a7308ce0 20781 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
20782
20783 section = &dwz->line;
20784 }
20785 else
976ca316 20786 section = &per_objfile->per_bfd->line;
36586728
TT
20787
20788 return section;
20789}
20790
debd256d 20791/* Read the statement program header starting at OFFSET in
3019eac3 20792 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20793 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20794 Returns NULL if there is a problem reading the header, e.g., if it
20795 has a version we don't understand.
debd256d
JB
20796
20797 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20798 the returned object point into the dwarf line section buffer,
20799 and must not be freed. */
ae2de4f8 20800
fff8551c 20801static line_header_up
9c541725 20802dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20803{
3019eac3 20804 struct dwarf2_section_info *section;
976ca316 20805 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 20806
36586728 20807 section = get_debug_line_section (cu);
976ca316 20808 section->read (per_objfile->objfile);
3019eac3 20809 if (section->buffer == NULL)
debd256d 20810 {
3019eac3 20811 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20812 complaint (_("missing .debug_line.dwo section"));
3019eac3 20813 else
b98664d3 20814 complaint (_("missing .debug_line section"));
debd256d
JB
20815 return 0;
20816 }
20817
0df7ad3a 20818 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 20819 per_objfile, section, &cu->header);
debd256d 20820}
c906108c 20821
c6da4cef 20822/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20823 Return the file name of the psymtab for the given file_entry.
c6da4cef 20824 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20825 If space for the result is malloc'd, *NAME_HOLDER will be set.
20826 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20827
d521ce57 20828static const char *
7ba99d21 20829psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20830 const dwarf2_psymtab *pst,
c89b44cd
TT
20831 const char *comp_dir,
20832 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20833{
d521ce57
TT
20834 const char *include_name = fe.name;
20835 const char *include_name_to_compare = include_name;
72b9f47f 20836 const char *pst_filename;
c6da4cef
DE
20837 int file_is_pst;
20838
8c43009f 20839 const char *dir_name = fe.include_dir (lh);
c6da4cef 20840
c89b44cd 20841 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20842 if (!IS_ABSOLUTE_PATH (include_name)
20843 && (dir_name != NULL || comp_dir != NULL))
20844 {
20845 /* Avoid creating a duplicate psymtab for PST.
20846 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20847 Before we do the comparison, however, we need to account
20848 for DIR_NAME and COMP_DIR.
20849 First prepend dir_name (if non-NULL). If we still don't
20850 have an absolute path prepend comp_dir (if non-NULL).
20851 However, the directory we record in the include-file's
20852 psymtab does not contain COMP_DIR (to match the
20853 corresponding symtab(s)).
20854
20855 Example:
20856
20857 bash$ cd /tmp
20858 bash$ gcc -g ./hello.c
20859 include_name = "hello.c"
20860 dir_name = "."
20861 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20862 DW_AT_name = "./hello.c"
20863
20864 */
c6da4cef
DE
20865
20866 if (dir_name != NULL)
20867 {
c89b44cd
TT
20868 name_holder->reset (concat (dir_name, SLASH_STRING,
20869 include_name, (char *) NULL));
20870 include_name = name_holder->get ();
c6da4cef 20871 include_name_to_compare = include_name;
c6da4cef
DE
20872 }
20873 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20874 {
c89b44cd
TT
20875 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20876 include_name, (char *) NULL));
20877 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20878 }
20879 }
20880
20881 pst_filename = pst->filename;
c89b44cd 20882 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20883 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20884 {
c89b44cd
TT
20885 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20886 pst_filename, (char *) NULL));
20887 pst_filename = copied_name.get ();
c6da4cef
DE
20888 }
20889
1e3fad37 20890 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20891
c6da4cef
DE
20892 if (file_is_pst)
20893 return NULL;
20894 return include_name;
20895}
20896
d9b3de22
DE
20897/* State machine to track the state of the line number program. */
20898
6f77053d 20899class lnp_state_machine
d9b3de22 20900{
6f77053d
PA
20901public:
20902 /* Initialize a machine state for the start of a line number
20903 program. */
804d2729
TT
20904 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20905 bool record_lines_p);
6f77053d 20906
8c43009f
PA
20907 file_entry *current_file ()
20908 {
20909 /* lh->file_names is 0-based, but the file name numbers in the
20910 statement program are 1-based. */
6f77053d
PA
20911 return m_line_header->file_name_at (m_file);
20912 }
20913
20914 /* Record the line in the state machine. END_SEQUENCE is true if
20915 we're processing the end of a sequence. */
20916 void record_line (bool end_sequence);
20917
a8caed5d 20918 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 20919 nop-out rest of the lines in this sequence. */
6f77053d
PA
20920 void check_line_address (struct dwarf2_cu *cu,
20921 const gdb_byte *line_ptr,
7ab6656f 20922 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20923
20924 void handle_set_discriminator (unsigned int discriminator)
20925 {
20926 m_discriminator = discriminator;
20927 m_line_has_non_zero_discriminator |= discriminator != 0;
20928 }
20929
20930 /* Handle DW_LNE_set_address. */
20931 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20932 {
20933 m_op_index = 0;
20934 address += baseaddr;
20935 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20936 }
20937
20938 /* Handle DW_LNS_advance_pc. */
20939 void handle_advance_pc (CORE_ADDR adjust);
20940
20941 /* Handle a special opcode. */
20942 void handle_special_opcode (unsigned char op_code);
20943
20944 /* Handle DW_LNS_advance_line. */
20945 void handle_advance_line (int line_delta)
20946 {
20947 advance_line (line_delta);
20948 }
20949
20950 /* Handle DW_LNS_set_file. */
20951 void handle_set_file (file_name_index file);
20952
20953 /* Handle DW_LNS_negate_stmt. */
20954 void handle_negate_stmt ()
20955 {
20956 m_is_stmt = !m_is_stmt;
20957 }
20958
20959 /* Handle DW_LNS_const_add_pc. */
20960 void handle_const_add_pc ();
20961
20962 /* Handle DW_LNS_fixed_advance_pc. */
20963 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20964 {
20965 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20966 m_op_index = 0;
20967 }
20968
20969 /* Handle DW_LNS_copy. */
20970 void handle_copy ()
20971 {
20972 record_line (false);
20973 m_discriminator = 0;
20974 }
20975
20976 /* Handle DW_LNE_end_sequence. */
20977 void handle_end_sequence ()
20978 {
804d2729 20979 m_currently_recording_lines = true;
6f77053d
PA
20980 }
20981
20982private:
20983 /* Advance the line by LINE_DELTA. */
20984 void advance_line (int line_delta)
20985 {
20986 m_line += line_delta;
20987
20988 if (line_delta != 0)
20989 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20990 }
20991
804d2729
TT
20992 struct dwarf2_cu *m_cu;
20993
6f77053d
PA
20994 gdbarch *m_gdbarch;
20995
20996 /* True if we're recording lines.
20997 Otherwise we're building partial symtabs and are just interested in
20998 finding include files mentioned by the line number program. */
20999 bool m_record_lines_p;
21000
8c43009f 21001 /* The line number header. */
6f77053d 21002 line_header *m_line_header;
8c43009f 21003
6f77053d
PA
21004 /* These are part of the standard DWARF line number state machine,
21005 and initialized according to the DWARF spec. */
d9b3de22 21006
6f77053d 21007 unsigned char m_op_index = 0;
7ba99d21
AT
21008 /* The line table index of the current file. */
21009 file_name_index m_file = 1;
6f77053d
PA
21010 unsigned int m_line = 1;
21011
21012 /* These are initialized in the constructor. */
21013
21014 CORE_ADDR m_address;
21015 bool m_is_stmt;
21016 unsigned int m_discriminator;
d9b3de22
DE
21017
21018 /* Additional bits of state we need to track. */
21019
21020 /* The last file that we called dwarf2_start_subfile for.
21021 This is only used for TLLs. */
6f77053d 21022 unsigned int m_last_file = 0;
d9b3de22 21023 /* The last file a line number was recorded for. */
6f77053d 21024 struct subfile *m_last_subfile = NULL;
d9b3de22 21025
1313c56e
AB
21026 /* The address of the last line entry. */
21027 CORE_ADDR m_last_address;
21028
21029 /* Set to true when a previous line at the same address (using
21030 m_last_address) had m_is_stmt true. This is reset to false when a
21031 line entry at a new address (m_address different to m_last_address) is
21032 processed. */
21033 bool m_stmt_at_address = false;
21034
804d2729
TT
21035 /* When true, record the lines we decode. */
21036 bool m_currently_recording_lines = false;
d9b3de22
DE
21037
21038 /* The last line number that was recorded, used to coalesce
21039 consecutive entries for the same line. This can happen, for
21040 example, when discriminators are present. PR 17276. */
6f77053d
PA
21041 unsigned int m_last_line = 0;
21042 bool m_line_has_non_zero_discriminator = false;
8c43009f 21043};
d9b3de22 21044
6f77053d
PA
21045void
21046lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21047{
21048 CORE_ADDR addr_adj = (((m_op_index + adjust)
21049 / m_line_header->maximum_ops_per_instruction)
21050 * m_line_header->minimum_instruction_length);
21051 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21052 m_op_index = ((m_op_index + adjust)
21053 % m_line_header->maximum_ops_per_instruction);
21054}
d9b3de22 21055
6f77053d
PA
21056void
21057lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21058{
6f77053d 21059 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21060 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21061 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21062 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21063 / m_line_header->maximum_ops_per_instruction)
21064 * m_line_header->minimum_instruction_length);
21065 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21066 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21067 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21068
258bf0ee 21069 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21070 advance_line (line_delta);
21071 record_line (false);
21072 m_discriminator = 0;
21073}
d9b3de22 21074
6f77053d
PA
21075void
21076lnp_state_machine::handle_set_file (file_name_index file)
21077{
21078 m_file = file;
21079
21080 const file_entry *fe = current_file ();
21081 if (fe == NULL)
21082 dwarf2_debug_line_missing_file_complaint ();
21083 else if (m_record_lines_p)
21084 {
21085 const char *dir = fe->include_dir (m_line_header);
21086
c24bdb02 21087 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21088 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21089 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21090 }
21091}
21092
21093void
21094lnp_state_machine::handle_const_add_pc ()
21095{
21096 CORE_ADDR adjust
21097 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21098
21099 CORE_ADDR addr_adj
21100 = (((m_op_index + adjust)
21101 / m_line_header->maximum_ops_per_instruction)
21102 * m_line_header->minimum_instruction_length);
21103
21104 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21105 m_op_index = ((m_op_index + adjust)
21106 % m_line_header->maximum_ops_per_instruction);
21107}
d9b3de22 21108
a05a36a5
DE
21109/* Return non-zero if we should add LINE to the line number table.
21110 LINE is the line to add, LAST_LINE is the last line that was added,
21111 LAST_SUBFILE is the subfile for LAST_LINE.
21112 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21113 had a non-zero discriminator.
21114
21115 We have to be careful in the presence of discriminators.
21116 E.g., for this line:
21117
21118 for (i = 0; i < 100000; i++);
21119
21120 clang can emit four line number entries for that one line,
21121 each with a different discriminator.
21122 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21123
21124 However, we want gdb to coalesce all four entries into one.
21125 Otherwise the user could stepi into the middle of the line and
21126 gdb would get confused about whether the pc really was in the
21127 middle of the line.
21128
21129 Things are further complicated by the fact that two consecutive
21130 line number entries for the same line is a heuristic used by gcc
21131 to denote the end of the prologue. So we can't just discard duplicate
21132 entries, we have to be selective about it. The heuristic we use is
21133 that we only collapse consecutive entries for the same line if at least
21134 one of those entries has a non-zero discriminator. PR 17276.
21135
21136 Note: Addresses in the line number state machine can never go backwards
21137 within one sequence, thus this coalescing is ok. */
21138
21139static int
804d2729
TT
21140dwarf_record_line_p (struct dwarf2_cu *cu,
21141 unsigned int line, unsigned int last_line,
a05a36a5
DE
21142 int line_has_non_zero_discriminator,
21143 struct subfile *last_subfile)
21144{
c24bdb02 21145 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21146 return 1;
21147 if (line != last_line)
21148 return 1;
21149 /* Same line for the same file that we've seen already.
21150 As a last check, for pr 17276, only record the line if the line
21151 has never had a non-zero discriminator. */
21152 if (!line_has_non_zero_discriminator)
21153 return 1;
21154 return 0;
21155}
21156
804d2729
TT
21157/* Use the CU's builder to record line number LINE beginning at
21158 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21159
21160static void
d9b3de22 21161dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21162 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21163 struct dwarf2_cu *cu)
252a6764
DE
21164{
21165 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21166
27e0867f
DE
21167 if (dwarf_line_debug)
21168 {
21169 fprintf_unfiltered (gdb_stdlog,
21170 "Recording line %u, file %s, address %s\n",
21171 line, lbasename (subfile->name),
21172 paddress (gdbarch, address));
21173 }
21174
804d2729 21175 if (cu != nullptr)
8c95582d 21176 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21177}
21178
21179/* Subroutine of dwarf_decode_lines_1 to simplify it.
21180 Mark the end of a set of line number records.
d9b3de22 21181 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21182 If SUBFILE is NULL the request is ignored. */
21183
21184static void
21185dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21186 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21187{
27e0867f
DE
21188 if (subfile == NULL)
21189 return;
21190
21191 if (dwarf_line_debug)
21192 {
21193 fprintf_unfiltered (gdb_stdlog,
21194 "Finishing current line, file %s, address %s\n",
21195 lbasename (subfile->name),
21196 paddress (gdbarch, address));
21197 }
21198
8c95582d 21199 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21200}
21201
6f77053d
PA
21202void
21203lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21204{
d9b3de22
DE
21205 if (dwarf_line_debug)
21206 {
21207 fprintf_unfiltered (gdb_stdlog,
21208 "Processing actual line %u: file %u,"
94a72be7 21209 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21210 m_line, m_file,
6f77053d 21211 paddress (m_gdbarch, m_address),
94a72be7
AB
21212 m_is_stmt, m_discriminator,
21213 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21214 }
21215
6f77053d 21216 file_entry *fe = current_file ();
8c43009f
PA
21217
21218 if (fe == NULL)
d9b3de22
DE
21219 dwarf2_debug_line_missing_file_complaint ();
21220 /* For now we ignore lines not starting on an instruction boundary.
21221 But not when processing end_sequence for compatibility with the
21222 previous version of the code. */
6f77053d 21223 else if (m_op_index == 0 || end_sequence)
d9b3de22 21224 {
8c43009f 21225 fe->included_p = 1;
8c95582d 21226 if (m_record_lines_p)
d9b3de22 21227 {
1313c56e
AB
21228 /* When we switch files we insert an end maker in the first file,
21229 switch to the second file and add a new line entry. The
21230 problem is that the end marker inserted in the first file will
21231 discard any previous line entries at the same address. If the
21232 line entries in the first file are marked as is-stmt, while
21233 the new line in the second file is non-stmt, then this means
21234 the end marker will discard is-stmt lines so we can have a
21235 non-stmt line. This means that there are less addresses at
21236 which the user can insert a breakpoint.
21237
21238 To improve this we track the last address in m_last_address,
21239 and whether we have seen an is-stmt at this address. Then
21240 when switching files, if we have seen a stmt at the current
21241 address, and we are switching to create a non-stmt line, then
21242 discard the new line. */
21243 bool file_changed
21244 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21245 bool ignore_this_line
dda83cd7
SM
21246 = ((file_changed && !end_sequence && m_last_address == m_address
21247 && !m_is_stmt && m_stmt_at_address)
21248 || (!end_sequence && m_line == 0));
1313c56e
AB
21249
21250 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21251 {
804d2729
TT
21252 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21253 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21254 }
21255
1313c56e 21256 if (!end_sequence && !ignore_this_line)
d9b3de22 21257 {
8c95582d
AB
21258 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21259
804d2729 21260 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21261 m_line_has_non_zero_discriminator,
21262 m_last_subfile))
d9b3de22 21263 {
c24bdb02 21264 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21265 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21266 builder->get_current_subfile (),
8c95582d 21267 m_line, m_address, is_stmt,
804d2729 21268 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21269 }
c24bdb02 21270 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21271 m_last_line = m_line;
d9b3de22
DE
21272 }
21273 }
21274 }
1313c56e
AB
21275
21276 /* Track whether we have seen any m_is_stmt true at m_address in case we
21277 have multiple line table entries all at m_address. */
21278 if (m_last_address != m_address)
21279 {
21280 m_stmt_at_address = false;
21281 m_last_address = m_address;
21282 }
21283 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21284}
21285
804d2729
TT
21286lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21287 line_header *lh, bool record_lines_p)
d9b3de22 21288{
804d2729 21289 m_cu = cu;
6f77053d
PA
21290 m_gdbarch = arch;
21291 m_record_lines_p = record_lines_p;
21292 m_line_header = lh;
d9b3de22 21293
804d2729 21294 m_currently_recording_lines = true;
d9b3de22 21295
d9b3de22
DE
21296 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21297 was a line entry for it so that the backend has a chance to adjust it
21298 and also record it in case it needs it. This is currently used by MIPS
21299 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21300 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21301 m_is_stmt = lh->default_is_stmt;
21302 m_discriminator = 0;
1313c56e
AB
21303
21304 m_last_address = m_address;
21305 m_stmt_at_address = false;
252a6764
DE
21306}
21307
6f77053d
PA
21308void
21309lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21310 const gdb_byte *line_ptr,
7ab6656f 21311 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21312{
a8caed5d
FS
21313 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21314 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21315 located at 0x0. In this case, additionally check that if
21316 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21317
a8caed5d
FS
21318 if ((address == 0 && address < unrelocated_lowpc)
21319 || address == (CORE_ADDR) -1)
924c2928
DE
21320 {
21321 /* This line table is for a function which has been
21322 GCd by the linker. Ignore it. PR gdb/12528 */
21323
5e22e966 21324 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21325 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21326
b98664d3 21327 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21328 line_offset, objfile_name (objfile));
804d2729
TT
21329 m_currently_recording_lines = false;
21330 /* Note: m_currently_recording_lines is left as false until we see
21331 DW_LNE_end_sequence. */
924c2928
DE
21332 }
21333}
21334
f3f5162e 21335/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21336 Process the line number information in LH.
21337 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21338 program in order to set included_p for every referenced header. */
debd256d 21339
c906108c 21340static void
43f3e411
DE
21341dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21342 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21343{
d521ce57
TT
21344 const gdb_byte *line_ptr, *extended_end;
21345 const gdb_byte *line_end;
a8c50c1f 21346 unsigned int bytes_read, extended_len;
699ca60a 21347 unsigned char op_code, extended_op;
e142c38c 21348 CORE_ADDR baseaddr;
5e22e966 21349 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21350 bfd *abfd = objfile->obfd;
08feed99 21351 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21352 /* True if we're recording line info (as opposed to building partial
21353 symtabs and just interested in finding include files mentioned by
21354 the line number program). */
21355 bool record_lines_p = !decode_for_pst_p;
e142c38c 21356
b3b3bada 21357 baseaddr = objfile->text_section_offset ();
c906108c 21358
debd256d
JB
21359 line_ptr = lh->statement_program_start;
21360 line_end = lh->statement_program_end;
c906108c
SS
21361
21362 /* Read the statement sequences until there's nothing left. */
21363 while (line_ptr < line_end)
21364 {
6f77053d
PA
21365 /* The DWARF line number program state machine. Reset the state
21366 machine at the start of each sequence. */
804d2729 21367 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21368 bool end_sequence = false;
d9b3de22 21369
8c43009f 21370 if (record_lines_p)
c906108c 21371 {
8c43009f
PA
21372 /* Start a subfile for the current file of the state
21373 machine. */
21374 const file_entry *fe = state_machine.current_file ();
21375
21376 if (fe != NULL)
804d2729 21377 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21378 }
21379
a738430d 21380 /* Decode the table. */
d9b3de22 21381 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21382 {
21383 op_code = read_1_byte (abfd, line_ptr);
21384 line_ptr += 1;
9aa1fe7e 21385
debd256d 21386 if (op_code >= lh->opcode_base)
6e70227d 21387 {
8e07a239 21388 /* Special opcode. */
6f77053d 21389 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21390 }
21391 else switch (op_code)
c906108c
SS
21392 {
21393 case DW_LNS_extended_op:
3e43a32a
MS
21394 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21395 &bytes_read);
473b7be6 21396 line_ptr += bytes_read;
a8c50c1f 21397 extended_end = line_ptr + extended_len;
c906108c
SS
21398 extended_op = read_1_byte (abfd, line_ptr);
21399 line_ptr += 1;
8f34b746
TV
21400 if (DW_LNE_lo_user <= extended_op
21401 && extended_op <= DW_LNE_hi_user)
21402 {
21403 /* Vendor extension, ignore. */
21404 line_ptr = extended_end;
21405 break;
21406 }
c906108c
SS
21407 switch (extended_op)
21408 {
21409 case DW_LNE_end_sequence:
6f77053d
PA
21410 state_machine.handle_end_sequence ();
21411 end_sequence = true;
c906108c
SS
21412 break;
21413 case DW_LNE_set_address:
d9b3de22
DE
21414 {
21415 CORE_ADDR address
c8a7a66f 21416 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21417 line_ptr += bytes_read;
6f77053d
PA
21418
21419 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21420 lowpc - baseaddr, address);
6f77053d 21421 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21422 }
c906108c
SS
21423 break;
21424 case DW_LNE_define_file:
dda83cd7
SM
21425 {
21426 const char *cur_file;
ecfb656c
PA
21427 unsigned int mod_time, length;
21428 dir_index dindex;
6e70227d 21429
dda83cd7 21430 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21431 &bytes_read);
dda83cd7
SM
21432 line_ptr += bytes_read;
21433 dindex = (dir_index)
21434 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21435 line_ptr += bytes_read;
21436 mod_time =
21437 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21438 line_ptr += bytes_read;
21439 length =
21440 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21441 line_ptr += bytes_read;
21442 lh->add_file_name (cur_file, dindex, mod_time, length);
21443 }
c906108c 21444 break;
d0c6ba3d 21445 case DW_LNE_set_discriminator:
6f77053d
PA
21446 {
21447 /* The discriminator is not interesting to the
21448 debugger; just ignore it. We still need to
21449 check its value though:
21450 if there are consecutive entries for the same
21451 (non-prologue) line we want to coalesce them.
21452 PR 17276. */
21453 unsigned int discr
21454 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21455 line_ptr += bytes_read;
21456
21457 state_machine.handle_set_discriminator (discr);
21458 }
d0c6ba3d 21459 break;
c906108c 21460 default:
b98664d3 21461 complaint (_("mangled .debug_line section"));
debd256d 21462 return;
c906108c 21463 }
a8c50c1f
DJ
21464 /* Make sure that we parsed the extended op correctly. If e.g.
21465 we expected a different address size than the producer used,
21466 we may have read the wrong number of bytes. */
21467 if (line_ptr != extended_end)
21468 {
b98664d3 21469 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21470 return;
21471 }
c906108c
SS
21472 break;
21473 case DW_LNS_copy:
6f77053d 21474 state_machine.handle_copy ();
c906108c
SS
21475 break;
21476 case DW_LNS_advance_pc:
2dc7f7b3
TT
21477 {
21478 CORE_ADDR adjust
21479 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21480 line_ptr += bytes_read;
6f77053d
PA
21481
21482 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21483 }
c906108c
SS
21484 break;
21485 case DW_LNS_advance_line:
a05a36a5
DE
21486 {
21487 int line_delta
21488 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21489 line_ptr += bytes_read;
6f77053d
PA
21490
21491 state_machine.handle_advance_line (line_delta);
a05a36a5 21492 }
c906108c
SS
21493 break;
21494 case DW_LNS_set_file:
d9b3de22 21495 {
6f77053d 21496 file_name_index file
ecfb656c
PA
21497 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21498 &bytes_read);
d9b3de22 21499 line_ptr += bytes_read;
8c43009f 21500
6f77053d 21501 state_machine.handle_set_file (file);
d9b3de22 21502 }
c906108c
SS
21503 break;
21504 case DW_LNS_set_column:
0ad93d4f 21505 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21506 line_ptr += bytes_read;
21507 break;
21508 case DW_LNS_negate_stmt:
6f77053d 21509 state_machine.handle_negate_stmt ();
c906108c
SS
21510 break;
21511 case DW_LNS_set_basic_block:
c906108c 21512 break;
c2c6d25f
JM
21513 /* Add to the address register of the state machine the
21514 address increment value corresponding to special opcode
a738430d
MK
21515 255. I.e., this value is scaled by the minimum
21516 instruction length since special opcode 255 would have
b021a221 21517 scaled the increment. */
c906108c 21518 case DW_LNS_const_add_pc:
6f77053d 21519 state_machine.handle_const_add_pc ();
c906108c
SS
21520 break;
21521 case DW_LNS_fixed_advance_pc:
3e29f34a 21522 {
6f77053d 21523 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21524 line_ptr += 2;
6f77053d
PA
21525
21526 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21527 }
c906108c 21528 break;
9aa1fe7e 21529 default:
a738430d
MK
21530 {
21531 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21532 int i;
a738430d 21533
debd256d 21534 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21535 {
21536 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21537 line_ptr += bytes_read;
21538 }
21539 }
c906108c
SS
21540 }
21541 }
d9b3de22
DE
21542
21543 if (!end_sequence)
21544 dwarf2_debug_line_missing_end_sequence_complaint ();
21545
21546 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21547 in which case we still finish recording the last line). */
6f77053d 21548 state_machine.record_line (true);
c906108c 21549 }
f3f5162e
DE
21550}
21551
21552/* Decode the Line Number Program (LNP) for the given line_header
21553 structure and CU. The actual information extracted and the type
21554 of structures created from the LNP depends on the value of PST.
21555
21556 1. If PST is NULL, then this procedure uses the data from the program
21557 to create all necessary symbol tables, and their linetables.
21558
21559 2. If PST is not NULL, this procedure reads the program to determine
21560 the list of files included by the unit represented by PST, and
21561 builds all the associated partial symbol tables.
21562
21563 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21564 It is used for relative paths in the line table.
21565 NOTE: When processing partial symtabs (pst != NULL),
21566 comp_dir == pst->dirname.
21567
21568 NOTE: It is important that psymtabs have the same file name (via strcmp)
21569 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21570 symtab we don't use it in the name of the psymtabs we create.
21571 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21572 A good testcase for this is mb-inline.exp.
21573
527f3840
JK
21574 LOWPC is the lowest address in CU (or 0 if not known).
21575
21576 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21577 for its PC<->lines mapping information. Otherwise only the filename
21578 table is read in. */
f3f5162e
DE
21579
21580static void
21581dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21582 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21583 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21584{
5e22e966 21585 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21586 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21587
527f3840
JK
21588 if (decode_mapping)
21589 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21590
21591 if (decode_for_pst_p)
21592 {
aaa75496 21593 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21594 create the psymtab of each included file. */
7ba99d21 21595 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21596 if (file_entry.included_p == 1)
21597 {
c89b44cd 21598 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21599 const char *include_name =
7ba99d21
AT
21600 psymtab_include_file_name (lh, file_entry, pst,
21601 comp_dir, &name_holder);
c6da4cef 21602 if (include_name != NULL)
7b249e47
TT
21603 dwarf2_create_include_psymtab
21604 (cu->per_objfile->per_bfd, include_name, pst,
21605 cu->per_objfile->per_bfd->partial_symtabs.get (),
0072c873 21606 objfile->per_bfd);
dda83cd7 21607 }
aaa75496 21608 }
cb1df416
DJ
21609 else
21610 {
21611 /* Make sure a symtab is created for every file, even files
21612 which contain only variables (i.e. no code with associated
21613 line numbers). */
c24bdb02
KS
21614 buildsym_compunit *builder = cu->get_builder ();
21615 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21616
7ba99d21 21617 for (auto &fe : lh->file_names ())
cb1df416 21618 {
804d2729 21619 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21620 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21621 {
c24bdb02 21622 builder->get_current_subfile ()->symtab
804d2729 21623 = allocate_symtab (cust,
c24bdb02 21624 builder->get_current_subfile ()->name);
43f3e411 21625 }
c24bdb02 21626 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21627 }
21628 }
c906108c
SS
21629}
21630
21631/* Start a subfile for DWARF. FILENAME is the name of the file and
21632 DIRNAME the name of the source directory which contains FILENAME
4d663531 21633 or NULL if not known.
c906108c
SS
21634 This routine tries to keep line numbers from identical absolute and
21635 relative file names in a common subfile.
21636
21637 Using the `list' example from the GDB testsuite, which resides in
21638 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21639 of /srcdir/list0.c yields the following debugging information for list0.c:
21640
c5aa993b 21641 DW_AT_name: /srcdir/list0.c
4d663531 21642 DW_AT_comp_dir: /compdir
357e46e7 21643 files.files[0].name: list0.h
c5aa993b 21644 files.files[0].dir: /srcdir
357e46e7 21645 files.files[1].name: list0.c
c5aa993b 21646 files.files[1].dir: /srcdir
c906108c
SS
21647
21648 The line number information for list0.c has to end up in a single
4f1520fb
FR
21649 subfile, so that `break /srcdir/list0.c:1' works as expected.
21650 start_subfile will ensure that this happens provided that we pass the
21651 concatenation of files.files[1].dir and files.files[1].name as the
21652 subfile's name. */
c906108c
SS
21653
21654static void
804d2729
TT
21655dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21656 const char *dirname)
c906108c 21657{
43816ebc 21658 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21659
4d663531 21660 /* In order not to lose the line information directory,
4f1520fb
FR
21661 we concatenate it to the filename when it makes sense.
21662 Note that the Dwarf3 standard says (speaking of filenames in line
21663 information): ``The directory index is ignored for file names
21664 that represent full path names''. Thus ignoring dirname in the
21665 `else' branch below isn't an issue. */
c906108c 21666
d5166ae1 21667 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21668 {
43816ebc
TT
21669 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21670 filename = copy.get ();
d521ce57 21671 }
c906108c 21672
c24bdb02 21673 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21674}
21675
804d2729
TT
21676/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21677 buildsym_compunit constructor. */
f4dc4d17 21678
c24bdb02
KS
21679struct compunit_symtab *
21680dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21681 CORE_ADDR low_pc)
f4dc4d17 21682{
c24bdb02 21683 gdb_assert (m_builder == nullptr);
43f3e411 21684
c24bdb02 21685 m_builder.reset (new struct buildsym_compunit
f6e649dd 21686 (this->per_objfile->objfile,
c24bdb02 21687 name, comp_dir, language, low_pc));
93b8bea4 21688
c24bdb02 21689 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21690
c24bdb02
KS
21691 get_builder ()->record_debugformat ("DWARF 2");
21692 get_builder ()->record_producer (producer);
f4dc4d17 21693
c24bdb02 21694 processing_has_namespace_info = false;
43f3e411 21695
c24bdb02 21696 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21697}
21698
4c2df51b
DJ
21699static void
21700var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21701 struct dwarf2_cu *cu)
4c2df51b 21702{
5e22e966 21703 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21704 struct comp_unit_head *cu_header = &cu->header;
21705
4c2df51b
DJ
21706 /* NOTE drow/2003-01-30: There used to be a comment and some special
21707 code here to turn a symbol with DW_AT_external and a
21708 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21709 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21710 with some versions of binutils) where shared libraries could have
21711 relocations against symbols in their debug information - the
21712 minimal symbol would have the right address, but the debug info
21713 would not. It's no longer necessary, because we will explicitly
21714 apply relocations when we read in the debug information now. */
21715
21716 /* A DW_AT_location attribute with no contents indicates that a
21717 variable has been optimized away. */
9d2246fc 21718 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21719 {
f1e6e072 21720 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21721 return;
21722 }
21723
21724 /* Handle one degenerate form of location expression specially, to
21725 preserve GDB's previous behavior when section offsets are
336d760d
AT
21726 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21727 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21728
9d2246fc
TT
21729 if (attr->form_is_block ())
21730 {
21731 struct dwarf_block *block = attr->as_block ();
21732
21733 if ((block->data[0] == DW_OP_addr
21734 && block->size == 1 + cu_header->addr_size)
21735 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 21736 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
21737 && (block->size
21738 == 1 + leb128_size (&block->data[1]))))
21739 {
21740 unsigned int dummy;
21741
21742 if (block->data[0] == DW_OP_addr)
21743 SET_SYMBOL_VALUE_ADDRESS
21744 (sym, cu->header.read_address (objfile->obfd,
21745 block->data + 1,
38583298 21746 &dummy));
9d2246fc
TT
21747 else
21748 SET_SYMBOL_VALUE_ADDRESS
21749 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21750 &dummy));
21751 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21752 fixup_symbol_section (sym, objfile);
21753 SET_SYMBOL_VALUE_ADDRESS
21754 (sym,
21755 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 21756 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
21757 return;
21758 }
4c2df51b
DJ
21759 }
21760
21761 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21762 expression evaluator, and use LOC_COMPUTED only when necessary
21763 (i.e. when the value of a register or memory location is
21764 referenced, or a thread-local block, etc.). Then again, it might
21765 not be worthwhile. I'm assuming that it isn't unless performance
21766 or memory numbers show me otherwise. */
21767
f1e6e072 21768 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21769
f1e6e072 21770 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21771 cu->has_loclist = true;
4c2df51b
DJ
21772}
21773
c906108c
SS
21774/* Given a pointer to a DWARF information entry, figure out if we need
21775 to make a symbol table entry for it, and if so, create a new entry
21776 and return a pointer to it.
21777 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21778 used the passed type.
21779 If SPACE is not NULL, use it to hold the new symbol. If it is
21780 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21781
21782static struct symbol *
5e2db402
TT
21783new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21784 struct symbol *space)
c906108c 21785{
976ca316
SM
21786 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21787 struct objfile *objfile = per_objfile->objfile;
08feed99 21788 struct gdbarch *gdbarch = objfile->arch ();
c906108c 21789 struct symbol *sym = NULL;
15d034d0 21790 const char *name;
c906108c
SS
21791 struct attribute *attr = NULL;
21792 struct attribute *attr2 = NULL;
e142c38c 21793 CORE_ADDR baseaddr;
e37fd15a
SW
21794 struct pending **list_to_add = NULL;
21795
edb3359d 21796 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21797
b3b3bada 21798 baseaddr = objfile->text_section_offset ();
c906108c 21799
94af9270 21800 name = dwarf2_name (die, cu);
c906108c
SS
21801 if (name)
21802 {
34eaf542 21803 int suppress_add = 0;
94af9270 21804
34eaf542
TT
21805 if (space)
21806 sym = space;
21807 else
8c14c3a3 21808 sym = new (&objfile->objfile_obstack) symbol;
c906108c 21809 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21810
21811 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21812 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
21813 /* Fortran does not have mangling standard and the mangling does differ
21814 between gfortran, iFort etc. */
bcfe6157
TT
21815 const char *physname
21816 = (cu->language == language_fortran
21817 ? dwarf2_full_name (name, die, cu)
21818 : dwarf2_physname (name, die, cu));
21819 const char *linkagename = dw2_linkage_name (die, cu);
21820
21821 if (linkagename == nullptr || cu->language == language_ada)
21822 sym->set_linkage_name (physname);
21823 else
21824 {
21825 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21826 sym->set_linkage_name (linkagename);
21827 }
f55ee35c 21828
c906108c 21829 /* Default assumptions.
dda83cd7 21830 Use the passed type or decode it from the die. */
176620f1 21831 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21832 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21833 if (type != NULL)
21834 SYMBOL_TYPE (sym) = type;
21835 else
e7c27a73 21836 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21837 attr = dwarf2_attr (die,
21838 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21839 cu);
435d3d88 21840 if (attr != nullptr)
529908cb 21841 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 21842
edb3359d
DJ
21843 attr = dwarf2_attr (die,
21844 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21845 cu);
aa659cfa 21846 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 21847 {
529908cb 21848 file_name_index file_index
aa659cfa 21849 = (file_name_index) attr->as_nonnegative ();
8c43009f 21850 struct file_entry *fe;
9a619af0 21851
ecfb656c
PA
21852 if (cu->line_header != NULL)
21853 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21854 else
21855 fe = NULL;
21856
21857 if (fe == NULL)
b98664d3 21858 complaint (_("file index out of range"));
8c43009f
PA
21859 else
21860 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21861 }
21862
c906108c
SS
21863 switch (die->tag)
21864 {
21865 case DW_TAG_label:
e142c38c 21866 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21867 if (attr != nullptr)
3e29f34a
MR
21868 {
21869 CORE_ADDR addr;
21870
95f982e5 21871 addr = attr->as_address ();
3e29f34a 21872 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21873 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 21874 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 21875 }
8f5c6526
TV
21876 else
21877 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
21878 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21879 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 21880 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21881 break;
21882 case DW_TAG_subprogram:
21883 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21884 finish_block. */
f1e6e072 21885 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21886 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 21887 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
21888 || cu->language == language_ada
21889 || cu->language == language_fortran)
c906108c 21890 {
dda83cd7
SM
21891 /* Subprograms marked external are stored as a global symbol.
21892 Ada and Fortran subprograms, whether marked external or
21893 not, are always stored as a global symbol, because we want
21894 to be able to access them globally. For instance, we want
21895 to be able to break on a nested subprogram without having
21896 to specify the context. */
c24bdb02 21897 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21898 }
21899 else
21900 {
e37fd15a 21901 list_to_add = cu->list_in_scope;
c906108c
SS
21902 }
21903 break;
edb3359d
DJ
21904 case DW_TAG_inlined_subroutine:
21905 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21906 finish_block. */
f1e6e072 21907 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21908 SYMBOL_INLINED (sym) = 1;
481860b3 21909 list_to_add = cu->list_in_scope;
edb3359d 21910 break;
34eaf542
TT
21911 case DW_TAG_template_value_param:
21912 suppress_add = 1;
21913 /* Fall through. */
72929c62 21914 case DW_TAG_constant:
c906108c 21915 case DW_TAG_variable:
254e6b9e 21916 case DW_TAG_member:
0963b4bd
MS
21917 /* Compilation with minimal debug info may result in
21918 variables with missing type entries. Change the
21919 misleading `void' type to something sensible. */
78134374 21920 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 21921 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21922
e142c38c 21923 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21924 /* In the case of DW_TAG_member, we should only be called for
21925 static const members. */
21926 if (die->tag == DW_TAG_member)
21927 {
3863f96c
DE
21928 /* dwarf2_add_field uses die_is_declaration,
21929 so we do the same. */
254e6b9e
DE
21930 gdb_assert (die_is_declaration (die, cu));
21931 gdb_assert (attr);
21932 }
435d3d88 21933 if (attr != nullptr)
c906108c 21934 {
e7c27a73 21935 dwarf2_const_value (attr, sym, cu);
e142c38c 21936 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21937 if (!suppress_add)
34eaf542 21938 {
c45bc3f8 21939 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 21940 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21941 else
e37fd15a 21942 list_to_add = cu->list_in_scope;
34eaf542 21943 }
c906108c
SS
21944 break;
21945 }
e142c38c 21946 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21947 if (attr != nullptr)
c906108c 21948 {
e7c27a73 21949 var_decode_location (attr, sym, cu);
e142c38c 21950 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21951
21952 /* Fortran explicitly imports any global symbols to the local
21953 scope by DW_TAG_common_block. */
21954 if (cu->language == language_fortran && die->parent
21955 && die->parent->tag == DW_TAG_common_block)
21956 attr2 = NULL;
21957
caac4577
JG
21958 if (SYMBOL_CLASS (sym) == LOC_STATIC
21959 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 21960 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
21961 {
21962 /* When a static variable is eliminated by the linker,
21963 the corresponding debug information is not stripped
21964 out, but the variable address is set to null;
21965 do not add such variables into symbol table. */
21966 }
c45bc3f8 21967 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 21968 {
4b610737
TT
21969 if (SYMBOL_CLASS (sym) == LOC_STATIC
21970 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 21971 && per_objfile->per_bfd->can_copy)
4b610737
TT
21972 {
21973 /* A global static variable might be subject to
21974 copy relocation. We first check for a local
21975 minsym, though, because maybe the symbol was
21976 marked hidden, in which case this would not
21977 apply. */
21978 bound_minimal_symbol found
21979 = (lookup_minimal_symbol_linkage
987012b8 21980 (sym->linkage_name (), objfile));
4b610737
TT
21981 if (found.minsym != nullptr)
21982 sym->maybe_copied = 1;
21983 }
f55ee35c 21984
1c809c68
TT
21985 /* A variable with DW_AT_external is never static,
21986 but it may be block-scoped. */
804d2729 21987 list_to_add
c24bdb02
KS
21988 = ((cu->list_in_scope
21989 == cu->get_builder ()->get_file_symbols ())
21990 ? cu->get_builder ()->get_global_symbols ()
804d2729 21991 : cu->list_in_scope);
1c809c68 21992 }
c906108c 21993 else
e37fd15a 21994 list_to_add = cu->list_in_scope;
c906108c
SS
21995 }
21996 else
21997 {
21998 /* We do not know the address of this symbol.
dda83cd7
SM
21999 If it is an external symbol and we have type information
22000 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22001 The address of the variable will then be determined from
22002 the minimal symbol table whenever the variable is
22003 referenced. */
e142c38c 22004 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22005
22006 /* Fortran explicitly imports any global symbols to the local
22007 scope by DW_TAG_common_block. */
22008 if (cu->language == language_fortran && die->parent
22009 && die->parent->tag == DW_TAG_common_block)
22010 {
22011 /* SYMBOL_CLASS doesn't matter here because
22012 read_common_block is going to reset it. */
22013 if (!suppress_add)
22014 list_to_add = cu->list_in_scope;
22015 }
c45bc3f8 22016 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22017 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22018 {
0fe7935b
DJ
22019 /* A variable with DW_AT_external is never static, but it
22020 may be block-scoped. */
804d2729 22021 list_to_add
c24bdb02
KS
22022 = ((cu->list_in_scope
22023 == cu->get_builder ()->get_file_symbols ())
22024 ? cu->get_builder ()->get_global_symbols ()
804d2729 22025 : cu->list_in_scope);
0fe7935b 22026
f1e6e072 22027 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22028 }
442ddf59
JK
22029 else if (!die_is_declaration (die, cu))
22030 {
22031 /* Use the default LOC_OPTIMIZED_OUT class. */
22032 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22033 if (!suppress_add)
22034 list_to_add = cu->list_in_scope;
442ddf59 22035 }
c906108c
SS
22036 }
22037 break;
22038 case DW_TAG_formal_parameter:
a60f3166 22039 {
4cf88725 22040 SYMBOL_IS_ARGUMENT (sym) = 1;
a60f3166 22041 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22042 if (attr != nullptr)
a60f3166
TT
22043 {
22044 var_decode_location (attr, sym, cu);
22045 }
22046 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22047 if (attr != nullptr)
a60f3166
TT
22048 {
22049 dwarf2_const_value (attr, sym, cu);
22050 }
f346a30d 22051
a60f3166
TT
22052 list_to_add = cu->list_in_scope;
22053 }
c906108c
SS
22054 break;
22055 case DW_TAG_unspecified_parameters:
22056 /* From varargs functions; gdb doesn't seem to have any
22057 interest in this information, so just ignore it for now.
22058 (FIXME?) */
22059 break;
34eaf542
TT
22060 case DW_TAG_template_type_param:
22061 suppress_add = 1;
22062 /* Fall through. */
c906108c 22063 case DW_TAG_class_type:
680b30c7 22064 case DW_TAG_interface_type:
c906108c
SS
22065 case DW_TAG_structure_type:
22066 case DW_TAG_union_type:
72019c9c 22067 case DW_TAG_set_type:
c906108c 22068 case DW_TAG_enumeration_type:
f1e6e072 22069 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22070 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22071
63d06c5c 22072 {
9c37b5ae 22073 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22074 really ever be static objects: otherwise, if you try
22075 to, say, break of a class's method and you're in a file
22076 which doesn't mention that class, it won't work unless
22077 the check for all static symbols in lookup_symbol_aux
22078 saves you. See the OtherFileClass tests in
22079 gdb.c++/namespace.exp. */
22080
e37fd15a 22081 if (!suppress_add)
34eaf542 22082 {
c24bdb02 22083 buildsym_compunit *builder = cu->get_builder ();
804d2729 22084 list_to_add
c24bdb02 22085 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22086 && cu->language == language_cplus
c24bdb02 22087 ? builder->get_global_symbols ()
804d2729 22088 : cu->list_in_scope);
63d06c5c 22089
64382290 22090 /* The semantics of C++ state that "struct foo {
9c37b5ae 22091 ... }" also defines a typedef for "foo". */
64382290 22092 if (cu->language == language_cplus
45280282 22093 || cu->language == language_ada
c44af4eb
TT
22094 || cu->language == language_d
22095 || cu->language == language_rust)
64382290
TT
22096 {
22097 /* The symbol's name is already allocated along
22098 with this objfile, so we don't need to
22099 duplicate it for the type. */
7d93a1e0 22100 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22101 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22102 }
63d06c5c
DC
22103 }
22104 }
c906108c
SS
22105 break;
22106 case DW_TAG_typedef:
f1e6e072 22107 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22108 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22109 list_to_add = cu->list_in_scope;
63d06c5c 22110 break;
d8f62e84 22111 case DW_TAG_array_type:
c906108c 22112 case DW_TAG_base_type:
dda83cd7 22113 case DW_TAG_subrange_type:
f1e6e072 22114 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22115 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22116 list_to_add = cu->list_in_scope;
c906108c
SS
22117 break;
22118 case DW_TAG_enumerator:
e142c38c 22119 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22120 if (attr != nullptr)
c906108c 22121 {
e7c27a73 22122 dwarf2_const_value (attr, sym, cu);
c906108c 22123 }
63d06c5c
DC
22124 {
22125 /* NOTE: carlton/2003-11-10: See comment above in the
22126 DW_TAG_class_type, etc. block. */
22127
804d2729 22128 list_to_add
c24bdb02 22129 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22130 && cu->language == language_cplus
c24bdb02 22131 ? cu->get_builder ()->get_global_symbols ()
804d2729 22132 : cu->list_in_scope);
63d06c5c 22133 }
c906108c 22134 break;
74921315 22135 case DW_TAG_imported_declaration:
5c4e30ca 22136 case DW_TAG_namespace:
f1e6e072 22137 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22138 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22139 break;
530e8392
KB
22140 case DW_TAG_module:
22141 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22142 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22143 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22144 break;
4357ac6c 22145 case DW_TAG_common_block:
f1e6e072 22146 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22147 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22148 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22149 break;
c906108c
SS
22150 default:
22151 /* Not a tag we recognize. Hopefully we aren't processing
22152 trash data, but since we must specifically ignore things
22153 we don't recognize, there is nothing else we should do at
0963b4bd 22154 this point. */
b98664d3 22155 complaint (_("unsupported tag: '%s'"),
4d3c2250 22156 dwarf_tag_name (die->tag));
c906108c
SS
22157 break;
22158 }
df8a16a1 22159
e37fd15a
SW
22160 if (suppress_add)
22161 {
22162 sym->hash_next = objfile->template_symbols;
22163 objfile->template_symbols = sym;
22164 list_to_add = NULL;
22165 }
22166
22167 if (list_to_add != NULL)
d3cb6808 22168 add_symbol_to_list (sym, list_to_add);
e37fd15a 22169
df8a16a1
DJ
22170 /* For the benefit of old versions of GCC, check for anonymous
22171 namespaces based on the demangled name. */
4d4ec4e5 22172 if (!cu->processing_has_namespace_info
94af9270 22173 && cu->language == language_cplus)
c24bdb02 22174 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22175 }
22176 return (sym);
22177}
22178
98bfdba5
PA
22179/* Given an attr with a DW_FORM_dataN value in host byte order,
22180 zero-extend it as appropriate for the symbol's type. The DWARF
22181 standard (v4) is not entirely clear about the meaning of using
22182 DW_FORM_dataN for a constant with a signed type, where the type is
22183 wider than the data. The conclusion of a discussion on the DWARF
22184 list was that this is unspecified. We choose to always zero-extend
22185 because that is the interpretation long in use by GCC. */
c906108c 22186
98bfdba5 22187static gdb_byte *
ff39bb5e 22188dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22189 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22190{
5e22e966 22191 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22192 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22193 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22194 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22195
22196 if (bits < sizeof (*value) * 8)
22197 {
22198 l &= ((LONGEST) 1 << bits) - 1;
22199 *value = l;
22200 }
22201 else if (bits == sizeof (*value) * 8)
22202 *value = l;
22203 else
22204 {
224c3ddb 22205 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22206 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22207 return bytes;
22208 }
22209
22210 return NULL;
22211}
22212
22213/* Read a constant value from an attribute. Either set *VALUE, or if
22214 the value does not fit in *VALUE, set *BYTES - either already
22215 allocated on the objfile obstack, or newly allocated on OBSTACK,
22216 or, set *BATON, if we translated the constant to a location
22217 expression. */
22218
22219static void
ff39bb5e 22220dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22221 const char *name, struct obstack *obstack,
22222 struct dwarf2_cu *cu,
d521ce57 22223 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22224 struct dwarf2_locexpr_baton **baton)
22225{
5e22e966 22226 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22227 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22228 struct comp_unit_head *cu_header = &cu->header;
c906108c 22229 struct dwarf_block *blk;
98bfdba5
PA
22230 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22231 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22232
22233 *value = 0;
22234 *bytes = NULL;
22235 *baton = NULL;
c906108c
SS
22236
22237 switch (attr->form)
22238 {
22239 case DW_FORM_addr:
336d760d 22240 case DW_FORM_addrx:
3019eac3 22241 case DW_FORM_GNU_addr_index:
ac56253d 22242 {
ac56253d
TT
22243 gdb_byte *data;
22244
98bfdba5
PA
22245 if (TYPE_LENGTH (type) != cu_header->addr_size)
22246 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22247 cu_header->addr_size,
98bfdba5 22248 TYPE_LENGTH (type));
ac56253d
TT
22249 /* Symbols of this form are reasonably rare, so we just
22250 piggyback on the existing location code rather than writing
22251 a new implementation of symbol_computed_ops. */
8d749320 22252 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22253 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22254 (*baton)->per_cu = cu->per_cu;
22255 gdb_assert ((*baton)->per_cu);
ac56253d 22256
98bfdba5 22257 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22258 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22259 (*baton)->data = data;
ac56253d
TT
22260
22261 data[0] = DW_OP_addr;
22262 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22263 byte_order, attr->as_address ());
ac56253d 22264 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22265 }
c906108c 22266 break;
4ac36638 22267 case DW_FORM_string:
93b5768b 22268 case DW_FORM_strp:
cf532bd1 22269 case DW_FORM_strx:
3019eac3 22270 case DW_FORM_GNU_str_index:
36586728 22271 case DW_FORM_GNU_strp_alt:
c6481205 22272 /* The string is already allocated on the objfile obstack, point
98bfdba5 22273 directly to it. */
2c830f54 22274 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22275 break;
c906108c
SS
22276 case DW_FORM_block1:
22277 case DW_FORM_block2:
22278 case DW_FORM_block4:
22279 case DW_FORM_block:
2dc7f7b3 22280 case DW_FORM_exprloc:
0224619f 22281 case DW_FORM_data16:
9d2246fc 22282 blk = attr->as_block ();
98bfdba5
PA
22283 if (TYPE_LENGTH (type) != blk->size)
22284 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22285 TYPE_LENGTH (type));
22286 *bytes = blk->data;
c906108c 22287 break;
2df3850c
JM
22288
22289 /* The DW_AT_const_value attributes are supposed to carry the
22290 symbol's value "represented as it would be on the target
22291 architecture." By the time we get here, it's already been
22292 converted to host endianness, so we just need to sign- or
22293 zero-extend it as appropriate. */
22294 case DW_FORM_data1:
3aef2284 22295 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22296 break;
c906108c 22297 case DW_FORM_data2:
3aef2284 22298 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22299 break;
c906108c 22300 case DW_FORM_data4:
3aef2284 22301 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22302 break;
c906108c 22303 case DW_FORM_data8:
3aef2284 22304 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22305 break;
22306
c906108c 22307 case DW_FORM_sdata:
663c44ac 22308 case DW_FORM_implicit_const:
1bc397c5 22309 *value = attr->as_signed ();
2df3850c
JM
22310 break;
22311
c906108c 22312 case DW_FORM_udata:
529908cb 22313 *value = attr->as_unsigned ();
c906108c 22314 break;
2df3850c 22315
c906108c 22316 default:
b98664d3 22317 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22318 dwarf_form_name (attr->form));
98bfdba5 22319 *value = 0;
c906108c
SS
22320 break;
22321 }
22322}
22323
2df3850c 22324
98bfdba5
PA
22325/* Copy constant value from an attribute to a symbol. */
22326
2df3850c 22327static void
ff39bb5e 22328dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22329 struct dwarf2_cu *cu)
2df3850c 22330{
5e22e966 22331 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22332 LONGEST value;
d521ce57 22333 const gdb_byte *bytes;
98bfdba5 22334 struct dwarf2_locexpr_baton *baton;
2df3850c 22335
98bfdba5 22336 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22337 sym->print_name (),
98bfdba5
PA
22338 &objfile->objfile_obstack, cu,
22339 &value, &bytes, &baton);
2df3850c 22340
98bfdba5
PA
22341 if (baton != NULL)
22342 {
98bfdba5 22343 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22344 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22345 }
22346 else if (bytes != NULL)
22347 {
22348 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22349 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22350 }
22351 else
22352 {
22353 SYMBOL_VALUE (sym) = value;
f1e6e072 22354 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22355 }
2df3850c
JM
22356}
22357
c906108c
SS
22358/* Return the type of the die in question using its DW_AT_type attribute. */
22359
22360static struct type *
e7c27a73 22361die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22362{
c906108c 22363 struct attribute *type_attr;
c906108c 22364
e142c38c 22365 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22366 if (!type_attr)
22367 {
5e22e966 22368 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22369 /* A missing DW_AT_type represents a void type. */
518817b3 22370 return objfile_type (objfile)->builtin_void;
c906108c 22371 }
348e048f 22372
673bfd45 22373 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22374}
22375
b4ba55a1
JB
22376/* True iff CU's producer generates GNAT Ada auxiliary information
22377 that allows to find parallel types through that information instead
22378 of having to do expensive parallel lookups by type name. */
22379
22380static int
22381need_gnat_info (struct dwarf2_cu *cu)
22382{
de4cb04a
JB
22383 /* Assume that the Ada compiler was GNAT, which always produces
22384 the auxiliary information. */
22385 return (cu->language == language_ada);
b4ba55a1
JB
22386}
22387
b4ba55a1
JB
22388/* Return the auxiliary type of the die in question using its
22389 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22390 attribute is not present. */
22391
22392static struct type *
22393die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22394{
b4ba55a1 22395 struct attribute *type_attr;
b4ba55a1
JB
22396
22397 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22398 if (!type_attr)
22399 return NULL;
22400
673bfd45 22401 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22402}
22403
22404/* If DIE has a descriptive_type attribute, then set the TYPE's
22405 descriptive type accordingly. */
22406
22407static void
22408set_descriptive_type (struct type *type, struct die_info *die,
22409 struct dwarf2_cu *cu)
22410{
22411 struct type *descriptive_type = die_descriptive_type (die, cu);
22412
22413 if (descriptive_type)
22414 {
22415 ALLOCATE_GNAT_AUX_TYPE (type);
22416 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22417 }
22418}
22419
c906108c
SS
22420/* Return the containing type of the die in question using its
22421 DW_AT_containing_type attribute. */
22422
22423static struct type *
e7c27a73 22424die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22425{
c906108c 22426 struct attribute *type_attr;
5e22e966 22427 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22428
e142c38c 22429 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22430 if (!type_attr)
22431 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22432 "[in module %s]"), objfile_name (objfile));
33ac96f0 22433
673bfd45 22434 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22435}
22436
ac9ec31b
DE
22437/* Return an error marker type to use for the ill formed type in DIE/CU. */
22438
22439static struct type *
22440build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22441{
976ca316
SM
22442 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22443 struct objfile *objfile = per_objfile->objfile;
528e1572 22444 char *saved;
ac9ec31b 22445
528e1572
SM
22446 std::string message
22447 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22448 objfile_name (objfile),
22449 sect_offset_str (cu->header.sect_off),
22450 sect_offset_str (die->sect_off));
efba19b0 22451 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22452
19f392bc 22453 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22454}
22455
673bfd45 22456/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22457 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22458 DW_AT_containing_type.
673bfd45
DE
22459 If there is no type substitute an error marker. */
22460
c906108c 22461static struct type *
ff39bb5e 22462lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22463 struct dwarf2_cu *cu)
c906108c 22464{
976ca316
SM
22465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22466 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22467 struct type *this_type;
22468
ac9ec31b
DE
22469 gdb_assert (attr->name == DW_AT_type
22470 || attr->name == DW_AT_GNAT_descriptive_type
22471 || attr->name == DW_AT_containing_type);
22472
673bfd45
DE
22473 /* First see if we have it cached. */
22474
36586728
TT
22475 if (attr->form == DW_FORM_GNU_ref_alt)
22476 {
22477 struct dwarf2_per_cu_data *per_cu;
0826b30a 22478 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22479
976ca316
SM
22480 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22481 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22482 }
cd6c91b4 22483 else if (attr->form_is_ref ())
673bfd45 22484 {
0826b30a 22485 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22486
976ca316 22487 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22488 }
55f1336d 22489 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22490 {
630ed6b9 22491 ULONGEST signature = attr->as_signature ();
673bfd45 22492
ac9ec31b 22493 return get_signatured_type (die, signature, cu);
673bfd45
DE
22494 }
22495 else
22496 {
b98664d3 22497 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22498 " at %s [in module %s]"),
22499 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22500 objfile_name (objfile));
ac9ec31b 22501 return build_error_marker_type (cu, die);
673bfd45
DE
22502 }
22503
22504 /* If not cached we need to read it in. */
22505
22506 if (this_type == NULL)
22507 {
ac9ec31b 22508 struct die_info *type_die = NULL;
673bfd45
DE
22509 struct dwarf2_cu *type_cu = cu;
22510
cd6c91b4 22511 if (attr->form_is_ref ())
ac9ec31b
DE
22512 type_die = follow_die_ref (die, attr, &type_cu);
22513 if (type_die == NULL)
22514 return build_error_marker_type (cu, die);
22515 /* If we find the type now, it's probably because the type came
3019eac3
DE
22516 from an inter-CU reference and the type's CU got expanded before
22517 ours. */
ac9ec31b 22518 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22519 }
22520
22521 /* If we still don't have a type use an error marker. */
22522
22523 if (this_type == NULL)
ac9ec31b 22524 return build_error_marker_type (cu, die);
673bfd45 22525
f792889a 22526 return this_type;
c906108c
SS
22527}
22528
673bfd45
DE
22529/* Return the type in DIE, CU.
22530 Returns NULL for invalid types.
22531
02142a6c 22532 This first does a lookup in die_type_hash,
673bfd45
DE
22533 and only reads the die in if necessary.
22534
22535 NOTE: This can be called when reading in partial or full symbols. */
22536
f792889a 22537static struct type *
e7c27a73 22538read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22539{
f792889a
DJ
22540 struct type *this_type;
22541
22542 this_type = get_die_type (die, cu);
22543 if (this_type)
22544 return this_type;
22545
673bfd45
DE
22546 return read_type_die_1 (die, cu);
22547}
22548
22549/* Read the type in DIE, CU.
22550 Returns NULL for invalid types. */
22551
22552static struct type *
22553read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22554{
22555 struct type *this_type = NULL;
22556
c906108c
SS
22557 switch (die->tag)
22558 {
22559 case DW_TAG_class_type:
680b30c7 22560 case DW_TAG_interface_type:
c906108c
SS
22561 case DW_TAG_structure_type:
22562 case DW_TAG_union_type:
f792889a 22563 this_type = read_structure_type (die, cu);
c906108c
SS
22564 break;
22565 case DW_TAG_enumeration_type:
f792889a 22566 this_type = read_enumeration_type (die, cu);
c906108c
SS
22567 break;
22568 case DW_TAG_subprogram:
22569 case DW_TAG_subroutine_type:
edb3359d 22570 case DW_TAG_inlined_subroutine:
f792889a 22571 this_type = read_subroutine_type (die, cu);
c906108c
SS
22572 break;
22573 case DW_TAG_array_type:
f792889a 22574 this_type = read_array_type (die, cu);
c906108c 22575 break;
72019c9c 22576 case DW_TAG_set_type:
f792889a 22577 this_type = read_set_type (die, cu);
72019c9c 22578 break;
c906108c 22579 case DW_TAG_pointer_type:
f792889a 22580 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22581 break;
22582 case DW_TAG_ptr_to_member_type:
f792889a 22583 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22584 break;
22585 case DW_TAG_reference_type:
4297a3f0
AV
22586 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22587 break;
22588 case DW_TAG_rvalue_reference_type:
22589 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22590 break;
22591 case DW_TAG_const_type:
f792889a 22592 this_type = read_tag_const_type (die, cu);
c906108c
SS
22593 break;
22594 case DW_TAG_volatile_type:
f792889a 22595 this_type = read_tag_volatile_type (die, cu);
c906108c 22596 break;
06d66ee9
TT
22597 case DW_TAG_restrict_type:
22598 this_type = read_tag_restrict_type (die, cu);
22599 break;
c906108c 22600 case DW_TAG_string_type:
f792889a 22601 this_type = read_tag_string_type (die, cu);
c906108c
SS
22602 break;
22603 case DW_TAG_typedef:
f792889a 22604 this_type = read_typedef (die, cu);
c906108c 22605 break;
a02abb62 22606 case DW_TAG_subrange_type:
f792889a 22607 this_type = read_subrange_type (die, cu);
a02abb62 22608 break;
c906108c 22609 case DW_TAG_base_type:
f792889a 22610 this_type = read_base_type (die, cu);
c906108c 22611 break;
81a17f79 22612 case DW_TAG_unspecified_type:
f792889a 22613 this_type = read_unspecified_type (die, cu);
81a17f79 22614 break;
0114d602
DJ
22615 case DW_TAG_namespace:
22616 this_type = read_namespace_type (die, cu);
22617 break;
f55ee35c
JK
22618 case DW_TAG_module:
22619 this_type = read_module_type (die, cu);
22620 break;
a2c2acaf
MW
22621 case DW_TAG_atomic_type:
22622 this_type = read_tag_atomic_type (die, cu);
22623 break;
c906108c 22624 default:
b98664d3 22625 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22626 dwarf_tag_name (die->tag));
c906108c
SS
22627 break;
22628 }
63d06c5c 22629
f792889a 22630 return this_type;
63d06c5c
DC
22631}
22632
abc72ce4
DE
22633/* See if we can figure out if the class lives in a namespace. We do
22634 this by looking for a member function; its demangled name will
22635 contain namespace info, if there is any.
22636 Return the computed name or NULL.
22637 Space for the result is allocated on the objfile's obstack.
22638 This is the full-die version of guess_partial_die_structure_name.
22639 In this case we know DIE has no useful parent. */
22640
43816ebc 22641static const char *
abc72ce4
DE
22642guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22643{
22644 struct die_info *spec_die;
22645 struct dwarf2_cu *spec_cu;
22646 struct die_info *child;
5e22e966 22647 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22648
22649 spec_cu = cu;
22650 spec_die = die_specification (die, &spec_cu);
22651 if (spec_die != NULL)
22652 {
22653 die = spec_die;
22654 cu = spec_cu;
22655 }
22656
22657 for (child = die->child;
22658 child != NULL;
22659 child = child->sibling)
22660 {
22661 if (child->tag == DW_TAG_subprogram)
22662 {
73b9be8b 22663 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22664
7d45c7c3 22665 if (linkage_name != NULL)
abc72ce4 22666 {
43816ebc 22667 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22668 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22669 const char *name = NULL;
abc72ce4
DE
22670
22671 if (actual_name != NULL)
22672 {
15d034d0 22673 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22674
22675 if (die_name != NULL
43816ebc 22676 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22677 {
22678 /* Strip off the class name from the full name.
22679 We want the prefix. */
22680 int die_name_len = strlen (die_name);
43816ebc
TT
22681 int actual_name_len = strlen (actual_name.get ());
22682 const char *ptr = actual_name.get ();
abc72ce4
DE
22683
22684 /* Test for '::' as a sanity check. */
22685 if (actual_name_len > die_name_len + 2
43816ebc 22686 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22687 name = obstack_strndup (
e3b94546 22688 &objfile->per_bfd->storage_obstack,
43816ebc 22689 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22690 }
22691 }
abc72ce4
DE
22692 return name;
22693 }
22694 }
22695 }
22696
22697 return NULL;
22698}
22699
96408a79
SA
22700/* GCC might emit a nameless typedef that has a linkage name. Determine the
22701 prefix part in such case. See
22702 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22703
a121b7c1 22704static const char *
96408a79
SA
22705anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22706{
22707 struct attribute *attr;
e6a959d6 22708 const char *base;
96408a79
SA
22709
22710 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22711 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22712 return NULL;
22713
7d45c7c3 22714 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22715 return NULL;
22716
73b9be8b 22717 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22718 const char *attr_name = attr->as_string ();
22719 if (attr == NULL || attr_name == NULL)
96408a79
SA
22720 return NULL;
22721
22722 /* dwarf2_name had to be already called. */
3b64bf15 22723 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22724
22725 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22726 base = strrchr (attr_name, ':');
22727 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22728 return "";
22729
5e22e966 22730 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22731 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22732 attr_name,
22733 &base[-1] - attr_name);
96408a79
SA
22734}
22735
fdde2d81 22736/* Return the name of the namespace/class that DIE is defined within,
0114d602 22737 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22738
0114d602
DJ
22739 For example, if we're within the method foo() in the following
22740 code:
22741
22742 namespace N {
22743 class C {
22744 void foo () {
22745 }
22746 };
22747 }
22748
22749 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22750
0d5cff50 22751static const char *
e142c38c 22752determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22753{
976ca316 22754 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
22755 struct die_info *parent, *spec_die;
22756 struct dwarf2_cu *spec_cu;
22757 struct type *parent_type;
a121b7c1 22758 const char *retval;
63d06c5c 22759
9c37b5ae 22760 if (cu->language != language_cplus
c44af4eb
TT
22761 && cu->language != language_fortran && cu->language != language_d
22762 && cu->language != language_rust)
0114d602
DJ
22763 return "";
22764
96408a79
SA
22765 retval = anonymous_struct_prefix (die, cu);
22766 if (retval)
22767 return retval;
22768
0114d602
DJ
22769 /* We have to be careful in the presence of DW_AT_specification.
22770 For example, with GCC 3.4, given the code
22771
22772 namespace N {
22773 void foo() {
22774 // Definition of N::foo.
22775 }
22776 }
22777
22778 then we'll have a tree of DIEs like this:
22779
22780 1: DW_TAG_compile_unit
22781 2: DW_TAG_namespace // N
22782 3: DW_TAG_subprogram // declaration of N::foo
22783 4: DW_TAG_subprogram // definition of N::foo
22784 DW_AT_specification // refers to die #3
22785
22786 Thus, when processing die #4, we have to pretend that we're in
22787 the context of its DW_AT_specification, namely the contex of die
22788 #3. */
22789 spec_cu = cu;
22790 spec_die = die_specification (die, &spec_cu);
22791 if (spec_die == NULL)
22792 parent = die->parent;
22793 else
63d06c5c 22794 {
0114d602
DJ
22795 parent = spec_die->parent;
22796 cu = spec_cu;
63d06c5c 22797 }
0114d602
DJ
22798
22799 if (parent == NULL)
22800 return "";
98bfdba5
PA
22801 else if (parent->building_fullname)
22802 {
22803 const char *name;
22804 const char *parent_name;
22805
22806 /* It has been seen on RealView 2.2 built binaries,
22807 DW_TAG_template_type_param types actually _defined_ as
22808 children of the parent class:
22809
22810 enum E {};
22811 template class <class Enum> Class{};
22812 Class<enum E> class_e;
22813
dda83cd7
SM
22814 1: DW_TAG_class_type (Class)
22815 2: DW_TAG_enumeration_type (E)
22816 3: DW_TAG_enumerator (enum1:0)
22817 3: DW_TAG_enumerator (enum2:1)
22818 ...
22819 2: DW_TAG_template_type_param
22820 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
22821
22822 Besides being broken debug info, it can put GDB into an
22823 infinite loop. Consider:
22824
22825 When we're building the full name for Class<E>, we'll start
22826 at Class, and go look over its template type parameters,
22827 finding E. We'll then try to build the full name of E, and
22828 reach here. We're now trying to build the full name of E,
22829 and look over the parent DIE for containing scope. In the
22830 broken case, if we followed the parent DIE of E, we'd again
22831 find Class, and once again go look at its template type
22832 arguments, etc., etc. Simply don't consider such parent die
22833 as source-level parent of this die (it can't be, the language
22834 doesn't allow it), and break the loop here. */
22835 name = dwarf2_name (die, cu);
22836 parent_name = dwarf2_name (parent, cu);
b98664d3 22837 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22838 name ? name : "<unknown>",
22839 parent_name ? parent_name : "<unknown>");
22840 return "";
22841 }
63d06c5c 22842 else
0114d602
DJ
22843 switch (parent->tag)
22844 {
63d06c5c 22845 case DW_TAG_namespace:
0114d602 22846 parent_type = read_type_die (parent, cu);
acebe513
UW
22847 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22848 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22849 Work around this problem here. */
22850 if (cu->language == language_cplus
7d93a1e0 22851 && strcmp (parent_type->name (), "::") == 0)
acebe513 22852 return "";
0114d602 22853 /* We give a name to even anonymous namespaces. */
7d93a1e0 22854 return parent_type->name ();
63d06c5c 22855 case DW_TAG_class_type:
680b30c7 22856 case DW_TAG_interface_type:
63d06c5c 22857 case DW_TAG_structure_type:
0114d602 22858 case DW_TAG_union_type:
f55ee35c 22859 case DW_TAG_module:
0114d602 22860 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
22861 if (parent_type->name () != NULL)
22862 return parent_type->name ();
0114d602
DJ
22863 else
22864 /* An anonymous structure is only allowed non-static data
22865 members; no typedefs, no member functions, et cetera.
22866 So it does not need a prefix. */
22867 return "";
abc72ce4 22868 case DW_TAG_compile_unit:
95554aad 22869 case DW_TAG_partial_unit:
abc72ce4
DE
22870 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22871 if (cu->language == language_cplus
976ca316 22872 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
22873 && die->child != NULL
22874 && (die->tag == DW_TAG_class_type
22875 || die->tag == DW_TAG_structure_type
22876 || die->tag == DW_TAG_union_type))
22877 {
43816ebc 22878 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22879 if (name != NULL)
22880 return name;
22881 }
22882 return "";
0a4b0913
AB
22883 case DW_TAG_subprogram:
22884 /* Nested subroutines in Fortran get a prefix with the name
22885 of the parent's subroutine. */
22886 if (cu->language == language_fortran)
22887 {
22888 if ((die->tag == DW_TAG_subprogram)
22889 && (dwarf2_name (parent, cu) != NULL))
22890 return dwarf2_name (parent, cu);
22891 }
22892 return determine_prefix (parent, cu);
3d567982
TT
22893 case DW_TAG_enumeration_type:
22894 parent_type = read_type_die (parent, cu);
3bc440a2 22895 if (parent_type->is_declared_class ())
3d567982 22896 {
7d93a1e0
SM
22897 if (parent_type->name () != NULL)
22898 return parent_type->name ();
3d567982
TT
22899 return "";
22900 }
22901 /* Fall through. */
63d06c5c 22902 default:
8176b9b8 22903 return determine_prefix (parent, cu);
63d06c5c 22904 }
63d06c5c
DC
22905}
22906
3e43a32a
MS
22907/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22908 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22909 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22910 an obconcat, otherwise allocate storage for the result. The CU argument is
22911 used to determine the language and hence, the appropriate separator. */
987504bb 22912
f55ee35c 22913#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22914
22915static char *
f55ee35c 22916typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 22917 int physname, struct dwarf2_cu *cu)
63d06c5c 22918{
f55ee35c 22919 const char *lead = "";
5c315b68 22920 const char *sep;
63d06c5c 22921
3e43a32a
MS
22922 if (suffix == NULL || suffix[0] == '\0'
22923 || prefix == NULL || prefix[0] == '\0')
987504bb 22924 sep = "";
45280282
IB
22925 else if (cu->language == language_d)
22926 {
22927 /* For D, the 'main' function could be defined in any module, but it
22928 should never be prefixed. */
22929 if (strcmp (suffix, "D main") == 0)
22930 {
22931 prefix = "";
22932 sep = "";
22933 }
22934 else
22935 sep = ".";
22936 }
f55ee35c
JK
22937 else if (cu->language == language_fortran && physname)
22938 {
22939 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22940 DW_AT_MIPS_linkage_name is preferred and used instead. */
22941
22942 lead = "__";
22943 sep = "_MOD_";
22944 }
987504bb
JJ
22945 else
22946 sep = "::";
63d06c5c 22947
6dd47d34
DE
22948 if (prefix == NULL)
22949 prefix = "";
22950 if (suffix == NULL)
22951 suffix = "";
22952
987504bb
JJ
22953 if (obs == NULL)
22954 {
3e43a32a 22955 char *retval
224c3ddb
SM
22956 = ((char *)
22957 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22958
f55ee35c
JK
22959 strcpy (retval, lead);
22960 strcat (retval, prefix);
6dd47d34
DE
22961 strcat (retval, sep);
22962 strcat (retval, suffix);
63d06c5c
DC
22963 return retval;
22964 }
987504bb
JJ
22965 else
22966 {
22967 /* We have an obstack. */
f55ee35c 22968 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22969 }
63d06c5c
DC
22970}
22971
71c25dea
TT
22972/* Get name of a die, return NULL if not found. */
22973
15d034d0
TT
22974static const char *
22975dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 22976 struct objfile *objfile)
71c25dea
TT
22977{
22978 if (name && cu->language == language_cplus)
22979 {
596dc4ad
TT
22980 gdb::unique_xmalloc_ptr<char> canon_name
22981 = cp_canonicalize_string (name);
71c25dea 22982
596dc4ad
TT
22983 if (canon_name != nullptr)
22984 name = objfile->intern (canon_name.get ());
71c25dea
TT
22985 }
22986
22987 return name;
c906108c
SS
22988}
22989
96553a0c
DE
22990/* Get name of a die, return NULL if not found.
22991 Anonymous namespaces are converted to their magic string. */
9219021c 22992
15d034d0 22993static const char *
e142c38c 22994dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22995{
22996 struct attribute *attr;
5e22e966 22997 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 22998
e142c38c 22999 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23000 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23001 if (attr_name == nullptr
96553a0c 23002 && die->tag != DW_TAG_namespace
53832f31
TT
23003 && die->tag != DW_TAG_class_type
23004 && die->tag != DW_TAG_interface_type
23005 && die->tag != DW_TAG_structure_type
23006 && die->tag != DW_TAG_union_type)
71c25dea
TT
23007 return NULL;
23008
23009 switch (die->tag)
23010 {
23011 case DW_TAG_compile_unit:
95554aad 23012 case DW_TAG_partial_unit:
71c25dea
TT
23013 /* Compilation units have a DW_AT_name that is a filename, not
23014 a source language identifier. */
23015 case DW_TAG_enumeration_type:
23016 case DW_TAG_enumerator:
23017 /* These tags always have simple identifiers already; no need
23018 to canonicalize them. */
2c830f54 23019 return attr_name;
907af001 23020
96553a0c 23021 case DW_TAG_namespace:
2c830f54
TT
23022 if (attr_name != nullptr)
23023 return attr_name;
96553a0c
DE
23024 return CP_ANONYMOUS_NAMESPACE_STR;
23025
907af001
UW
23026 case DW_TAG_class_type:
23027 case DW_TAG_interface_type:
23028 case DW_TAG_structure_type:
23029 case DW_TAG_union_type:
23030 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23031 structures or unions. These were of the form "._%d" in GCC 4.1,
23032 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23033 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23034 if (attr_name != nullptr
23035 && (startswith (attr_name, "._")
23036 || startswith (attr_name, "<anonymous")))
907af001 23037 return NULL;
53832f31
TT
23038
23039 /* GCC might emit a nameless typedef that has a linkage name. See
23040 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23041 if (!attr || attr_name == NULL)
53832f31 23042 {
73b9be8b 23043 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23044 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23045 if (attr == NULL || attr_name == NULL)
53832f31
TT
23046 return NULL;
23047
2c830f54 23048 /* Avoid demangling attr_name the second time on a second
df5c6c50 23049 call for the same DIE. */
3b64bf15 23050 if (!attr->canonical_string_p ())
53832f31 23051 {
43816ebc 23052 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23053 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23054 if (demangled == nullptr)
23055 return nullptr;
43816ebc 23056
c6481205 23057 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23058 attr_name = attr->as_string ();
53832f31 23059 }
67430cd0 23060
2c830f54
TT
23061 /* Strip any leading namespaces/classes, keep only the
23062 base name. DW_AT_name for named DIEs does not
23063 contain the prefixes. */
23064 const char *base = strrchr (attr_name, ':');
23065 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23066 return &base[1];
23067 else
2c830f54 23068 return attr_name;
53832f31 23069 }
907af001
UW
23070 break;
23071
71c25dea 23072 default:
907af001
UW
23073 break;
23074 }
23075
3b64bf15 23076 if (!attr->canonical_string_p ())
c6481205
TT
23077 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23078 objfile));
2c830f54 23079 return attr->as_string ();
9219021c
DC
23080}
23081
23082/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23083 is none. *EXT_CU is the CU containing DIE on input, and the CU
23084 containing the return value on output. */
9219021c
DC
23085
23086static struct die_info *
f2f0e013 23087dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23088{
23089 struct attribute *attr;
9219021c 23090
f2f0e013 23091 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23092 if (attr == NULL)
23093 return NULL;
23094
f2f0e013 23095 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23096}
23097
f9aca02d 23098static void
d97bc12b 23099dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23100{
23101 unsigned int i;
23102
d97bc12b 23103 print_spaces (indent, f);
9d8780f0 23104 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23105 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23106 sect_offset_str (die->sect_off));
d97bc12b
DE
23107
23108 if (die->parent != NULL)
23109 {
23110 print_spaces (indent, f);
9d8780f0
SM
23111 fprintf_unfiltered (f, " parent at offset: %s\n",
23112 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23113 }
23114
23115 print_spaces (indent, f);
23116 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23117 dwarf_bool_name (die->child != NULL));
c906108c 23118
d97bc12b
DE
23119 print_spaces (indent, f);
23120 fprintf_unfiltered (f, " attributes:\n");
23121
c906108c
SS
23122 for (i = 0; i < die->num_attrs; ++i)
23123 {
d97bc12b
DE
23124 print_spaces (indent, f);
23125 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23126 dwarf_attr_name (die->attrs[i].name),
23127 dwarf_form_name (die->attrs[i].form));
d97bc12b 23128
c906108c
SS
23129 switch (die->attrs[i].form)
23130 {
c906108c 23131 case DW_FORM_addr:
336d760d 23132 case DW_FORM_addrx:
3019eac3 23133 case DW_FORM_GNU_addr_index:
d97bc12b 23134 fprintf_unfiltered (f, "address: ");
36d378cf 23135 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23136 break;
23137 case DW_FORM_block2:
23138 case DW_FORM_block4:
23139 case DW_FORM_block:
23140 case DW_FORM_block1:
56eb65bd 23141 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23142 pulongest (die->attrs[i].as_block ()->size));
c906108c 23143 break;
2dc7f7b3 23144 case DW_FORM_exprloc:
56eb65bd 23145 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23146 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23147 break;
0224619f
JK
23148 case DW_FORM_data16:
23149 fprintf_unfiltered (f, "constant of 16 bytes");
23150 break;
4568ecf9
DE
23151 case DW_FORM_ref_addr:
23152 fprintf_unfiltered (f, "ref address: ");
529908cb 23153 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23154 break;
36586728
TT
23155 case DW_FORM_GNU_ref_alt:
23156 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23157 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23158 break;
10b3939b
DJ
23159 case DW_FORM_ref1:
23160 case DW_FORM_ref2:
23161 case DW_FORM_ref4:
4568ecf9
DE
23162 case DW_FORM_ref8:
23163 case DW_FORM_ref_udata:
d97bc12b 23164 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23165 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23166 break;
c906108c
SS
23167 case DW_FORM_data1:
23168 case DW_FORM_data2:
23169 case DW_FORM_data4:
ce5d95e1 23170 case DW_FORM_data8:
c906108c 23171 case DW_FORM_udata:
43bbcdc2 23172 fprintf_unfiltered (f, "constant: %s",
529908cb 23173 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23174 break;
2dc7f7b3
TT
23175 case DW_FORM_sec_offset:
23176 fprintf_unfiltered (f, "section offset: %s",
529908cb 23177 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23178 break;
55f1336d 23179 case DW_FORM_ref_sig8:
ac9ec31b 23180 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23181 hex_string (die->attrs[i].as_signature ()));
348e048f 23182 break;
c906108c 23183 case DW_FORM_string:
4bdf3d34 23184 case DW_FORM_strp:
43988095 23185 case DW_FORM_line_strp:
cf532bd1 23186 case DW_FORM_strx:
3019eac3 23187 case DW_FORM_GNU_str_index:
36586728 23188 case DW_FORM_GNU_strp_alt:
8285870a 23189 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23190 die->attrs[i].as_string ()
23191 ? die->attrs[i].as_string () : "",
23192 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23193 break;
23194 case DW_FORM_flag:
c45bc3f8 23195 if (die->attrs[i].as_boolean ())
d97bc12b 23196 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23197 else
d97bc12b 23198 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23199 break;
2dc7f7b3
TT
23200 case DW_FORM_flag_present:
23201 fprintf_unfiltered (f, "flag: TRUE");
23202 break;
a8329558 23203 case DW_FORM_indirect:
0963b4bd
MS
23204 /* The reader will have reduced the indirect form to
23205 the "base form" so this form should not occur. */
5f48f8f3 23206 fprintf_unfiltered (f,
3e43a32a 23207 "unexpected attribute form: DW_FORM_indirect");
a8329558 23208 break;
1bc397c5 23209 case DW_FORM_sdata:
663c44ac
JK
23210 case DW_FORM_implicit_const:
23211 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23212 plongest (die->attrs[i].as_signed ()));
663c44ac 23213 break;
c906108c 23214 default:
d97bc12b 23215 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23216 die->attrs[i].form);
d97bc12b 23217 break;
c906108c 23218 }
d97bc12b 23219 fprintf_unfiltered (f, "\n");
c906108c
SS
23220 }
23221}
23222
f9aca02d 23223static void
d97bc12b 23224dump_die_for_error (struct die_info *die)
c906108c 23225{
d97bc12b
DE
23226 dump_die_shallow (gdb_stderr, 0, die);
23227}
23228
23229static void
23230dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23231{
23232 int indent = level * 4;
23233
23234 gdb_assert (die != NULL);
23235
23236 if (level >= max_level)
23237 return;
23238
23239 dump_die_shallow (f, indent, die);
23240
23241 if (die->child != NULL)
c906108c 23242 {
d97bc12b
DE
23243 print_spaces (indent, f);
23244 fprintf_unfiltered (f, " Children:");
23245 if (level + 1 < max_level)
23246 {
23247 fprintf_unfiltered (f, "\n");
23248 dump_die_1 (f, level + 1, max_level, die->child);
23249 }
23250 else
23251 {
3e43a32a
MS
23252 fprintf_unfiltered (f,
23253 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23254 }
23255 }
23256
23257 if (die->sibling != NULL && level > 0)
23258 {
23259 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23260 }
23261}
23262
d97bc12b
DE
23263/* This is called from the pdie macro in gdbinit.in.
23264 It's not static so gcc will keep a copy callable from gdb. */
23265
23266void
23267dump_die (struct die_info *die, int max_level)
23268{
23269 dump_die_1 (gdb_stdlog, 0, max_level, die);
23270}
23271
f9aca02d 23272static void
51545339 23273store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23274{
51545339 23275 void **slot;
c906108c 23276
9c541725
PA
23277 slot = htab_find_slot_with_hash (cu->die_hash, die,
23278 to_underlying (die->sect_off),
b64f50a1 23279 INSERT);
51545339
DJ
23280
23281 *slot = die;
c906108c
SS
23282}
23283
348e048f
DE
23284/* Follow reference or signature attribute ATTR of SRC_DIE.
23285 On entry *REF_CU is the CU of SRC_DIE.
23286 On exit *REF_CU is the CU of the result. */
23287
23288static struct die_info *
ff39bb5e 23289follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23290 struct dwarf2_cu **ref_cu)
23291{
23292 struct die_info *die;
23293
cd6c91b4 23294 if (attr->form_is_ref ())
348e048f 23295 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23296 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23297 die = follow_die_sig (src_die, attr, ref_cu);
23298 else
23299 {
23300 dump_die_for_error (src_die);
23301 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23302 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23303 }
23304
23305 return die;
03dd20cc
DJ
23306}
23307
5c631832 23308/* Follow reference OFFSET.
673bfd45
DE
23309 On entry *REF_CU is the CU of the source die referencing OFFSET.
23310 On exit *REF_CU is the CU of the result.
23311 Returns NULL if OFFSET is invalid. */
f504f079 23312
f9aca02d 23313static struct die_info *
9c541725 23314follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23315 struct dwarf2_cu **ref_cu)
c906108c 23316{
10b3939b 23317 struct die_info temp_die;
f2f0e013 23318 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23319 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23320
348e048f
DE
23321 gdb_assert (cu->per_cu != NULL);
23322
98bfdba5
PA
23323 target_cu = cu;
23324
17e593e9
SM
23325 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23326 "source CU contains target offset: %d",
23327 sect_offset_str (cu->per_cu->sect_off),
23328 sect_offset_str (sect_off),
23329 cu->header.offset_in_cu_p (sect_off));
23330
3019eac3 23331 if (cu->per_cu->is_debug_types)
348e048f
DE
23332 {
23333 /* .debug_types CUs cannot reference anything outside their CU.
23334 If they need to, they have to reference a signatured type via
55f1336d 23335 DW_FORM_ref_sig8. */
4057dfde 23336 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23337 return NULL;
348e048f 23338 }
36586728 23339 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23340 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23341 {
23342 struct dwarf2_per_cu_data *per_cu;
9a619af0 23343
9c541725 23344 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23345 per_objfile);
03dd20cc 23346
17e593e9
SM
23347 dwarf_read_debug_printf_v ("target CU offset: %s, "
23348 "target CU DIEs loaded: %d",
23349 sect_offset_str (per_cu->sect_off),
23350 per_objfile->get_cu (per_cu) != nullptr);
23351
616c069a
SM
23352 /* If necessary, add it to the queue and load its DIEs.
23353
23354 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23355 it doesn't mean they are currently loaded. Since we require them
23356 to be loaded, we must check for ourselves. */
23357 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23358 || per_objfile->get_cu (per_cu) == nullptr)
4a636814
SM
23359 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23360 false, cu->language);
03dd20cc 23361
976ca316 23362 target_cu = per_objfile->get_cu (per_cu);
616c069a 23363 gdb_assert (target_cu != nullptr);
10b3939b 23364 }
98bfdba5
PA
23365 else if (cu->dies == NULL)
23366 {
23367 /* We're loading full DIEs during partial symbol reading. */
976ca316 23368 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23369 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23370 language_minimal);
98bfdba5 23371 }
c906108c 23372
f2f0e013 23373 *ref_cu = target_cu;
9c541725 23374 temp_die.sect_off = sect_off;
c24bdb02 23375
9a3c8263 23376 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23377 &temp_die,
23378 to_underlying (sect_off));
5c631832 23379}
10b3939b 23380
5c631832
JK
23381/* Follow reference attribute ATTR of SRC_DIE.
23382 On entry *REF_CU is the CU of SRC_DIE.
23383 On exit *REF_CU is the CU of the result. */
23384
23385static struct die_info *
ff39bb5e 23386follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23387 struct dwarf2_cu **ref_cu)
23388{
0826b30a 23389 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23390 struct dwarf2_cu *cu = *ref_cu;
23391 struct die_info *die;
23392
9c541725 23393 die = follow_die_offset (sect_off,
36586728
TT
23394 (attr->form == DW_FORM_GNU_ref_alt
23395 || cu->per_cu->is_dwz),
23396 ref_cu);
5c631832 23397 if (!die)
9d8780f0
SM
23398 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23399 "at %s [in module %s]"),
23400 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23401 objfile_name (cu->per_objfile->objfile));
348e048f 23402
5c631832
JK
23403 return die;
23404}
23405
d4c9a4f8 23406/* See read.h. */
5c631832
JK
23407
23408struct dwarf2_locexpr_baton
9c541725 23409dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23410 dwarf2_per_cu_data *per_cu,
976ca316 23411 dwarf2_per_objfile *per_objfile,
041d9819
SM
23412 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23413 bool resolve_abstract_p)
5c631832 23414{
5c631832
JK
23415 struct die_info *die;
23416 struct attribute *attr;
23417 struct dwarf2_locexpr_baton retval;
976ca316 23418 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23419
976ca316 23420 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23421 if (cu == nullptr)
976ca316 23422 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23423
23424 if (cu == nullptr)
cc12ce38
DE
23425 {
23426 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23427 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23428 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23429 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23430 }
918dd910 23431
9c541725 23432 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23433 if (!die)
9d8780f0
SM
23434 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23435 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23436
23437 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23438 if (!attr && resolve_abstract_p
976ca316
SM
23439 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23440 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23441 {
041d9819 23442 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23443 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23444 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23445
3360b6e7 23446 for (const auto &cand_off
976ca316 23447 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23448 {
3360b6e7
TV
23449 struct dwarf2_cu *cand_cu = cu;
23450 struct die_info *cand
23451 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23452 if (!cand
23453 || !cand->parent
e4a62c65
TV
23454 || cand->parent->tag != DW_TAG_subprogram)
23455 continue;
23456
23457 CORE_ADDR pc_low, pc_high;
23458 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23459 if (pc_low == ((CORE_ADDR) -1))
23460 continue;
23461 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23462 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23463 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23464 continue;
23465
23466 die = cand;
23467 attr = dwarf2_attr (die, DW_AT_location, cu);
23468 break;
23469 }
23470 }
23471
5c631832
JK
23472 if (!attr)
23473 {
e103e986
JK
23474 /* DWARF: "If there is no such attribute, then there is no effect.".
23475 DATA is ignored if SIZE is 0. */
5c631832 23476
e103e986 23477 retval.data = NULL;
5c631832
JK
23478 retval.size = 0;
23479 }
cd6c91b4 23480 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23481 {
23482 struct dwarf2_loclist_baton loclist_baton;
041d9819 23483 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23484 size_t size;
23485
23486 fill_in_loclist_baton (cu, &loclist_baton, attr);
23487
23488 retval.data = dwarf2_find_location_expression (&loclist_baton,
23489 &size, pc);
23490 retval.size = size;
23491 }
5c631832
JK
23492 else
23493 {
4fc6c0d5 23494 if (!attr->form_is_block ())
9d8780f0 23495 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23496 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23497 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23498
9d2246fc
TT
23499 struct dwarf_block *block = attr->as_block ();
23500 retval.data = block->data;
23501 retval.size = block->size;
5c631832 23502 }
976ca316 23503 retval.per_objfile = per_objfile;
5c631832 23504 retval.per_cu = cu->per_cu;
918dd910 23505
976ca316 23506 per_objfile->age_comp_units ();
918dd910 23507
5c631832 23508 return retval;
348e048f
DE
23509}
23510
d4c9a4f8 23511/* See read.h. */
8b9737bf
TT
23512
23513struct dwarf2_locexpr_baton
23514dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23515 dwarf2_per_cu_data *per_cu,
14095eb3 23516 dwarf2_per_objfile *per_objfile,
041d9819 23517 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23518{
9c541725 23519 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23520
14095eb3 23521 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23522 get_frame_pc);
8b9737bf
TT
23523}
23524
b6807d98
TT
23525/* Write a constant of a given type as target-ordered bytes into
23526 OBSTACK. */
23527
23528static const gdb_byte *
23529write_constant_as_bytes (struct obstack *obstack,
23530 enum bfd_endian byte_order,
23531 struct type *type,
23532 ULONGEST value,
23533 LONGEST *len)
23534{
23535 gdb_byte *result;
23536
23537 *len = TYPE_LENGTH (type);
224c3ddb 23538 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23539 store_unsigned_integer (result, *len, byte_order, value);
23540
23541 return result;
23542}
23543
d4c9a4f8 23544/* See read.h. */
b6807d98
TT
23545
23546const gdb_byte *
9c541725 23547dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23548 dwarf2_per_cu_data *per_cu,
14095eb3 23549 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23550 obstack *obstack,
b6807d98
TT
23551 LONGEST *len)
23552{
b6807d98
TT
23553 struct die_info *die;
23554 struct attribute *attr;
23555 const gdb_byte *result = NULL;
23556 struct type *type;
23557 LONGEST value;
23558 enum bfd_endian byte_order;
14095eb3 23559 struct objfile *objfile = per_objfile->objfile;
b6807d98 23560
7188ed02 23561 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23562 if (cu == nullptr)
23563 cu = load_cu (per_cu, per_objfile, false);
23564
23565 if (cu == nullptr)
cc12ce38
DE
23566 {
23567 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23568 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23569 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23570 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23571 }
b6807d98 23572
9c541725 23573 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23574 if (!die)
9d8780f0
SM
23575 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23576 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23577
23578 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23579 if (attr == NULL)
23580 return NULL;
23581
e3b94546 23582 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23583 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23584
23585 switch (attr->form)
23586 {
23587 case DW_FORM_addr:
336d760d 23588 case DW_FORM_addrx:
b6807d98
TT
23589 case DW_FORM_GNU_addr_index:
23590 {
23591 gdb_byte *tem;
23592
23593 *len = cu->header.addr_size;
224c3ddb 23594 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23595 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23596 result = tem;
23597 }
23598 break;
23599 case DW_FORM_string:
23600 case DW_FORM_strp:
cf532bd1 23601 case DW_FORM_strx:
b6807d98
TT
23602 case DW_FORM_GNU_str_index:
23603 case DW_FORM_GNU_strp_alt:
c6481205 23604 /* The string is already allocated on the objfile obstack, point
b6807d98 23605 directly to it. */
2c830f54
TT
23606 {
23607 const char *attr_name = attr->as_string ();
23608 result = (const gdb_byte *) attr_name;
23609 *len = strlen (attr_name);
23610 }
b6807d98
TT
23611 break;
23612 case DW_FORM_block1:
23613 case DW_FORM_block2:
23614 case DW_FORM_block4:
23615 case DW_FORM_block:
23616 case DW_FORM_exprloc:
0224619f 23617 case DW_FORM_data16:
9d2246fc
TT
23618 {
23619 struct dwarf_block *block = attr->as_block ();
23620 result = block->data;
23621 *len = block->size;
23622 }
b6807d98
TT
23623 break;
23624
23625 /* The DW_AT_const_value attributes are supposed to carry the
23626 symbol's value "represented as it would be on the target
23627 architecture." By the time we get here, it's already been
23628 converted to host endianness, so we just need to sign- or
23629 zero-extend it as appropriate. */
23630 case DW_FORM_data1:
23631 type = die_type (die, cu);
23632 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23633 if (result == NULL)
23634 result = write_constant_as_bytes (obstack, byte_order,
23635 type, value, len);
23636 break;
23637 case DW_FORM_data2:
23638 type = die_type (die, cu);
23639 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23640 if (result == NULL)
23641 result = write_constant_as_bytes (obstack, byte_order,
23642 type, value, len);
23643 break;
23644 case DW_FORM_data4:
23645 type = die_type (die, cu);
23646 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23647 if (result == NULL)
23648 result = write_constant_as_bytes (obstack, byte_order,
23649 type, value, len);
23650 break;
23651 case DW_FORM_data8:
23652 type = die_type (die, cu);
23653 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23654 if (result == NULL)
23655 result = write_constant_as_bytes (obstack, byte_order,
23656 type, value, len);
23657 break;
23658
23659 case DW_FORM_sdata:
663c44ac 23660 case DW_FORM_implicit_const:
b6807d98
TT
23661 type = die_type (die, cu);
23662 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23663 type, attr->as_signed (), len);
b6807d98
TT
23664 break;
23665
23666 case DW_FORM_udata:
23667 type = die_type (die, cu);
23668 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23669 type, attr->as_unsigned (), len);
b6807d98
TT
23670 break;
23671
23672 default:
b98664d3 23673 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23674 dwarf_form_name (attr->form));
23675 break;
23676 }
23677
23678 return result;
23679}
23680
d4c9a4f8 23681/* See read.h. */
7942e96e
AA
23682
23683struct type *
9c541725 23684dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
23685 dwarf2_per_cu_data *per_cu,
23686 dwarf2_per_objfile *per_objfile)
7942e96e 23687{
7942e96e
AA
23688 struct die_info *die;
23689
7188ed02 23690 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23691 if (cu == nullptr)
23692 cu = load_cu (per_cu, per_objfile, false);
23693
23694 if (cu == nullptr)
23695 return nullptr;
7942e96e 23696
9c541725 23697 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23698 if (!die)
23699 return NULL;
23700
23701 return die_type (die, cu);
23702}
23703
8cb5117c 23704/* See read.h. */
8a9b8146
TT
23705
23706struct type *
b64f50a1 23707dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23708 dwarf2_per_cu_data *per_cu,
23709 dwarf2_per_objfile *per_objfile)
8a9b8146 23710{
9c541725 23711 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23712 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23713}
23714
ac9ec31b 23715/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23716 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23717 On exit *REF_CU is the CU of the result.
23718 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23719
23720static struct die_info *
ac9ec31b
DE
23721follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23722 struct dwarf2_cu **ref_cu)
348e048f 23723{
348e048f 23724 struct die_info temp_die;
4cf88725 23725 struct dwarf2_cu *sig_cu;
348e048f 23726 struct die_info *die;
976ca316 23727 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23728
348e048f 23729
ac9ec31b
DE
23730 /* While it might be nice to assert sig_type->type == NULL here,
23731 we can get here for DW_AT_imported_declaration where we need
23732 the DIE not the type. */
348e048f 23733
616c069a 23734 /* If necessary, add it to the queue and load its DIEs.
348e048f 23735
616c069a
SM
23736 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23737 it doesn't mean they are currently loaded. Since we require them
23738 to be loaded, we must check for ourselves. */
0d305d5c 23739 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
616c069a 23740 language_minimal)
0d305d5c 23741 || per_objfile->get_cu (sig_type) == nullptr)
976ca316 23742 read_signatured_type (sig_type, per_objfile);
348e048f 23743
0d305d5c 23744 sig_cu = per_objfile->get_cu (sig_type);
69d751e3 23745 gdb_assert (sig_cu != NULL);
9c541725
PA
23746 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23747 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23748 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23749 to_underlying (temp_die.sect_off));
348e048f
DE
23750 if (die)
23751 {
796a7ff8
DE
23752 /* For .gdb_index version 7 keep track of included TUs.
23753 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
23754 if (per_objfile->per_bfd->index_table != NULL
23755 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 23756 {
ae640021 23757 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23758 }
23759
348e048f 23760 *ref_cu = sig_cu;
c24bdb02 23761
348e048f
DE
23762 return die;
23763 }
23764
ac9ec31b
DE
23765 return NULL;
23766}
23767
23768/* Follow signatured type referenced by ATTR in SRC_DIE.
23769 On entry *REF_CU is the CU of SRC_DIE.
23770 On exit *REF_CU is the CU of the result.
23771 The result is the DIE of the type.
23772 If the referenced type cannot be found an error is thrown. */
23773
23774static struct die_info *
ff39bb5e 23775follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23776 struct dwarf2_cu **ref_cu)
23777{
630ed6b9 23778 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
23779 struct signatured_type *sig_type;
23780 struct die_info *die;
23781
23782 gdb_assert (attr->form == DW_FORM_ref_sig8);
23783
a2ce51a0 23784 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23785 /* sig_type will be NULL if the signatured type is missing from
23786 the debug info. */
23787 if (sig_type == NULL)
23788 {
23789 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
23790 " from DIE at %s [in module %s]"),
23791 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23792 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23793 }
23794
23795 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23796 if (die == NULL)
23797 {
23798 dump_die_for_error (src_die);
23799 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23800 " from DIE at %s [in module %s]"),
23801 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23802 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23803 }
23804
23805 return die;
23806}
23807
23808/* Get the type specified by SIGNATURE referenced in DIE/CU,
23809 reading in and processing the type unit if necessary. */
23810
23811static struct type *
23812get_signatured_type (struct die_info *die, ULONGEST signature,
23813 struct dwarf2_cu *cu)
23814{
976ca316 23815 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
23816 struct signatured_type *sig_type;
23817 struct dwarf2_cu *type_cu;
23818 struct die_info *type_die;
23819 struct type *type;
23820
a2ce51a0 23821 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23822 /* sig_type will be NULL if the signatured type is missing from
23823 the debug info. */
23824 if (sig_type == NULL)
23825 {
b98664d3 23826 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23827 " from DIE at %s [in module %s]"),
23828 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23829 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23830 return build_error_marker_type (cu, die);
23831 }
23832
23833 /* If we already know the type we're done. */
976ca316 23834 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
23835 if (type != nullptr)
23836 return type;
ac9ec31b
DE
23837
23838 type_cu = cu;
23839 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23840 if (type_die != NULL)
23841 {
23842 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23843 is created. This is important, for example, because for c++ classes
23844 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23845 type = read_type_die (type_die, type_cu);
23846 if (type == NULL)
23847 {
b98664d3 23848 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23849 " referenced from DIE at %s [in module %s]"),
23850 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23851 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23852 type = build_error_marker_type (cu, die);
23853 }
23854 }
23855 else
23856 {
b98664d3 23857 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23858 " from DIE at %s [in module %s]"),
23859 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23860 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23861 type = build_error_marker_type (cu, die);
23862 }
e286671b 23863
976ca316 23864 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
23865
23866 return type;
23867}
23868
23869/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23870 reading in and processing the type unit if necessary. */
23871
23872static struct type *
ff39bb5e 23873get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23874 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23875{
23876 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 23877 if (attr->form_is_ref ())
ac9ec31b
DE
23878 {
23879 struct dwarf2_cu *type_cu = cu;
23880 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23881
23882 return read_type_die (type_die, type_cu);
23883 }
23884 else if (attr->form == DW_FORM_ref_sig8)
23885 {
630ed6b9 23886 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
23887 }
23888 else
23889 {
976ca316 23890 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23891
b98664d3 23892 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23893 " at %s [in module %s]"),
23894 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 23895 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23896 return build_error_marker_type (cu, die);
23897 }
348e048f
DE
23898}
23899
e5fe5e75 23900/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23901
23902static void
ab432490
SM
23903load_full_type_unit (dwarf2_per_cu_data *per_cu,
23904 dwarf2_per_objfile *per_objfile)
348e048f 23905{
52dc124a 23906 struct signatured_type *sig_type;
348e048f 23907
f4dc4d17 23908 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 23909 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 23910
6721b2ec
DE
23911 /* We have the per_cu, but we need the signatured_type.
23912 Fortunately this is an easy translation. */
23913 gdb_assert (per_cu->is_debug_types);
23914 sig_type = (struct signatured_type *) per_cu;
348e048f 23915
7188ed02 23916 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 23917
ab432490 23918 read_signatured_type (sig_type, per_objfile);
348e048f 23919
7188ed02 23920 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
23921}
23922
3019eac3
DE
23923/* Read in a signatured type and build its CU and DIEs.
23924 If the type is a stub for the real type in a DWO file,
23925 read in the real type from the DWO file as well. */
dee91e82
DE
23926
23927static void
ab432490
SM
23928read_signatured_type (signatured_type *sig_type,
23929 dwarf2_per_objfile *per_objfile)
dee91e82 23930{
0d305d5c
TT
23931 gdb_assert (sig_type->is_debug_types);
23932 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
348e048f 23933
0d305d5c 23934 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
23935
23936 if (!reader.dummy_p)
23937 {
23938 struct dwarf2_cu *cu = reader.cu;
23939 const gdb_byte *info_ptr = reader.info_ptr;
23940
23941 gdb_assert (cu->die_hash == NULL);
23942 cu->die_hash =
23943 htab_create_alloc_ex (cu->header.length / 12,
23944 die_hash,
23945 die_eq,
23946 NULL,
23947 &cu->comp_unit_obstack,
23948 hashtab_obstack_allocate,
23949 dummy_obstack_deallocate);
23950
3e225074 23951 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
23952 reader.comp_unit_die->child
23953 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23954 reader.comp_unit_die);
23955 cu->dies = reader.comp_unit_die;
23956 /* comp_unit_die is not stored in die_hash, no need. */
23957
23958 /* We try not to read any attributes in this function, because
23959 not all CUs needed for references have been loaded yet, and
23960 symbol table processing isn't initialized. But we have to
23961 set the CU language, or we won't be able to build types
23962 correctly. Similarly, if we do not read the producer, we can
23963 not apply producer-specific interpretation. */
23964 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
23965
23966 reader.keep ();
c0ab21c2
TT
23967 }
23968
0d305d5c 23969 sig_type->tu_read = 1;
c906108c
SS
23970}
23971
c906108c
SS
23972/* Decode simple location descriptions.
23973 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
23974 the location and return the value. If COMPUTED is non-null, it is
23975 set to true to indicate that decoding was successful, and false
23976 otherwise. If COMPUTED is null, then this function may emit a
23977 complaint. */
c906108c
SS
23978
23979static CORE_ADDR
7d79de9a 23980decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 23981{
5e22e966 23982 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
23983 size_t i;
23984 size_t size = blk->size;
d521ce57 23985 const gdb_byte *data = blk->data;
21ae7a4d
JK
23986 CORE_ADDR stack[64];
23987 int stacki;
23988 unsigned int bytes_read, unsnd;
23989 gdb_byte op;
c906108c 23990
7d79de9a
TT
23991 if (computed != nullptr)
23992 *computed = false;
23993
21ae7a4d
JK
23994 i = 0;
23995 stacki = 0;
23996 stack[stacki] = 0;
23997 stack[++stacki] = 0;
23998
23999 while (i < size)
24000 {
24001 op = data[i++];
24002 switch (op)
24003 {
24004 case DW_OP_lit0:
24005 case DW_OP_lit1:
24006 case DW_OP_lit2:
24007 case DW_OP_lit3:
24008 case DW_OP_lit4:
24009 case DW_OP_lit5:
24010 case DW_OP_lit6:
24011 case DW_OP_lit7:
24012 case DW_OP_lit8:
24013 case DW_OP_lit9:
24014 case DW_OP_lit10:
24015 case DW_OP_lit11:
24016 case DW_OP_lit12:
24017 case DW_OP_lit13:
24018 case DW_OP_lit14:
24019 case DW_OP_lit15:
24020 case DW_OP_lit16:
24021 case DW_OP_lit17:
24022 case DW_OP_lit18:
24023 case DW_OP_lit19:
24024 case DW_OP_lit20:
24025 case DW_OP_lit21:
24026 case DW_OP_lit22:
24027 case DW_OP_lit23:
24028 case DW_OP_lit24:
24029 case DW_OP_lit25:
24030 case DW_OP_lit26:
24031 case DW_OP_lit27:
24032 case DW_OP_lit28:
24033 case DW_OP_lit29:
24034 case DW_OP_lit30:
24035 case DW_OP_lit31:
24036 stack[++stacki] = op - DW_OP_lit0;
24037 break;
f1bea926 24038
21ae7a4d
JK
24039 case DW_OP_reg0:
24040 case DW_OP_reg1:
24041 case DW_OP_reg2:
24042 case DW_OP_reg3:
24043 case DW_OP_reg4:
24044 case DW_OP_reg5:
24045 case DW_OP_reg6:
24046 case DW_OP_reg7:
24047 case DW_OP_reg8:
24048 case DW_OP_reg9:
24049 case DW_OP_reg10:
24050 case DW_OP_reg11:
24051 case DW_OP_reg12:
24052 case DW_OP_reg13:
24053 case DW_OP_reg14:
24054 case DW_OP_reg15:
24055 case DW_OP_reg16:
24056 case DW_OP_reg17:
24057 case DW_OP_reg18:
24058 case DW_OP_reg19:
24059 case DW_OP_reg20:
24060 case DW_OP_reg21:
24061 case DW_OP_reg22:
24062 case DW_OP_reg23:
24063 case DW_OP_reg24:
24064 case DW_OP_reg25:
24065 case DW_OP_reg26:
24066 case DW_OP_reg27:
24067 case DW_OP_reg28:
24068 case DW_OP_reg29:
24069 case DW_OP_reg30:
24070 case DW_OP_reg31:
24071 stack[++stacki] = op - DW_OP_reg0;
24072 if (i < size)
7d79de9a
TT
24073 {
24074 if (computed == nullptr)
24075 dwarf2_complex_location_expr_complaint ();
24076 else
24077 return 0;
24078 }
21ae7a4d 24079 break;
c906108c 24080
21ae7a4d
JK
24081 case DW_OP_regx:
24082 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24083 i += bytes_read;
24084 stack[++stacki] = unsnd;
24085 if (i < size)
7d79de9a
TT
24086 {
24087 if (computed == nullptr)
24088 dwarf2_complex_location_expr_complaint ();
24089 else
24090 return 0;
24091 }
21ae7a4d 24092 break;
c906108c 24093
21ae7a4d 24094 case DW_OP_addr:
c8a7a66f
TT
24095 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24096 &bytes_read);
21ae7a4d
JK
24097 i += bytes_read;
24098 break;
d53d4ac5 24099
21ae7a4d
JK
24100 case DW_OP_const1u:
24101 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24102 i += 1;
24103 break;
24104
24105 case DW_OP_const1s:
24106 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24107 i += 1;
24108 break;
24109
24110 case DW_OP_const2u:
24111 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24112 i += 2;
24113 break;
24114
24115 case DW_OP_const2s:
24116 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24117 i += 2;
24118 break;
d53d4ac5 24119
21ae7a4d
JK
24120 case DW_OP_const4u:
24121 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24122 i += 4;
24123 break;
24124
24125 case DW_OP_const4s:
24126 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24127 i += 4;
24128 break;
24129
585861ea
JK
24130 case DW_OP_const8u:
24131 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24132 i += 8;
24133 break;
24134
21ae7a4d
JK
24135 case DW_OP_constu:
24136 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24137 &bytes_read);
24138 i += bytes_read;
24139 break;
24140
24141 case DW_OP_consts:
24142 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24143 i += bytes_read;
24144 break;
24145
24146 case DW_OP_dup:
24147 stack[stacki + 1] = stack[stacki];
24148 stacki++;
24149 break;
24150
24151 case DW_OP_plus:
24152 stack[stacki - 1] += stack[stacki];
24153 stacki--;
24154 break;
24155
24156 case DW_OP_plus_uconst:
24157 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24158 &bytes_read);
24159 i += bytes_read;
24160 break;
24161
24162 case DW_OP_minus:
24163 stack[stacki - 1] -= stack[stacki];
24164 stacki--;
24165 break;
24166
24167 case DW_OP_deref:
24168 /* If we're not the last op, then we definitely can't encode
24169 this using GDB's address_class enum. This is valid for partial
24170 global symbols, although the variable's address will be bogus
24171 in the psymtab. */
24172 if (i < size)
7d79de9a
TT
24173 {
24174 if (computed == nullptr)
24175 dwarf2_complex_location_expr_complaint ();
24176 else
24177 return 0;
24178 }
21ae7a4d
JK
24179 break;
24180
dda83cd7 24181 case DW_OP_GNU_push_tls_address:
4aa4e28b 24182 case DW_OP_form_tls_address:
21ae7a4d
JK
24183 /* The top of the stack has the offset from the beginning
24184 of the thread control block at which the variable is located. */
24185 /* Nothing should follow this operator, so the top of stack would
24186 be returned. */
24187 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24188 address will be bogus in the psymtab. Make it always at least
24189 non-zero to not look as a variable garbage collected by linker
24190 which have DW_OP_addr 0. */
21ae7a4d 24191 if (i < size)
7d79de9a
TT
24192 {
24193 if (computed == nullptr)
24194 dwarf2_complex_location_expr_complaint ();
24195 else
24196 return 0;
24197 }
585861ea 24198 stack[stacki]++;
dda83cd7 24199 break;
21ae7a4d
JK
24200
24201 case DW_OP_GNU_uninit:
7d79de9a
TT
24202 if (computed != nullptr)
24203 return 0;
21ae7a4d
JK
24204 break;
24205
336d760d 24206 case DW_OP_addrx:
3019eac3 24207 case DW_OP_GNU_addr_index:
49f6c839 24208 case DW_OP_GNU_const_index:
3019eac3
DE
24209 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24210 &bytes_read);
24211 i += bytes_read;
24212 break;
24213
21ae7a4d 24214 default:
7d79de9a
TT
24215 if (computed == nullptr)
24216 {
24217 const char *name = get_DW_OP_name (op);
21ae7a4d 24218
7d79de9a
TT
24219 if (name)
24220 complaint (_("unsupported stack op: '%s'"),
24221 name);
24222 else
24223 complaint (_("unsupported stack op: '%02x'"),
24224 op);
24225 }
21ae7a4d
JK
24226
24227 return (stack[stacki]);
d53d4ac5 24228 }
3c6e0cb3 24229
21ae7a4d 24230 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24231 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24232 if (stacki >= ARRAY_SIZE (stack) - 1)
24233 {
7d79de9a
TT
24234 if (computed == nullptr)
24235 complaint (_("location description stack overflow"));
21ae7a4d
JK
24236 return 0;
24237 }
24238
24239 if (stacki <= 0)
24240 {
7d79de9a
TT
24241 if (computed == nullptr)
24242 complaint (_("location description stack underflow"));
21ae7a4d
JK
24243 return 0;
24244 }
24245 }
7d79de9a
TT
24246
24247 if (computed != nullptr)
24248 *computed = true;
21ae7a4d 24249 return (stack[stacki]);
c906108c
SS
24250}
24251
24252/* memory allocation interface */
24253
c906108c 24254static struct dwarf_block *
7b5a2f43 24255dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24256{
8d749320 24257 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24258}
24259
c906108c 24260static struct die_info *
b60c80d6 24261dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24262{
24263 struct die_info *die;
b60c80d6
DJ
24264 size_t size = sizeof (struct die_info);
24265
24266 if (num_attrs > 1)
24267 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24268
b60c80d6 24269 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24270 memset (die, 0, sizeof (struct die_info));
24271 return (die);
24272}
2e276125
JB
24273
24274\f
a036ba48 24275
c90ec28a 24276/* Macro support. */
cf2c3c16 24277
9eac9650
TT
24278/* An overload of dwarf_decode_macros that finds the correct section
24279 and ensures it is read in before calling the other overload. */
24280
24281static void
24282dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24283 int section_is_gnu)
24284{
976ca316
SM
24285 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24286 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24287 const struct line_header *lh = cu->line_header;
9eac9650
TT
24288 unsigned int offset_size = cu->header.offset_size;
24289 struct dwarf2_section_info *section;
24290 const char *section_name;
24291
24292 if (cu->dwo_unit != nullptr)
24293 {
24294 if (section_is_gnu)
24295 {
24296 section = &cu->dwo_unit->dwo_file->sections.macro;
24297 section_name = ".debug_macro.dwo";
24298 }
24299 else
24300 {
24301 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24302 section_name = ".debug_macinfo.dwo";
24303 }
24304 }
24305 else
24306 {
24307 if (section_is_gnu)
24308 {
976ca316 24309 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24310 section_name = ".debug_macro";
24311 }
24312 else
24313 {
976ca316 24314 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24315 section_name = ".debug_macinfo";
24316 }
24317 }
24318
24319 section->read (objfile);
24320 if (section->buffer == nullptr)
24321 {
24322 complaint (_("missing %s section"), section_name);
24323 return;
24324 }
24325
24326 buildsym_compunit *builder = cu->get_builder ();
24327
048fde1e 24328 struct dwarf2_section_info *str_offsets_section;
24329 struct dwarf2_section_info *str_section;
24330 ULONGEST str_offsets_base;
24331
24332 if (cu->dwo_unit != nullptr)
24333 {
24334 str_offsets_section = &cu->dwo_unit->dwo_file
24335 ->sections.str_offsets;
24336 str_section = &cu->dwo_unit->dwo_file->sections.str;
24337 str_offsets_base = cu->header.addr_size;
24338 }
24339 else
24340 {
24341 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24342 str_section = &per_objfile->per_bfd->str;
24343 str_offsets_base = *cu->str_offsets_base;
24344 }
24345
976ca316 24346 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24347 offset_size, offset, str_section, str_offsets_section,
24348 str_offsets_base, section_is_gnu);
9eac9650
TT
24349}
24350
3019eac3
DE
24351/* Return the .debug_loc section to use for CU.
24352 For DWO files use .debug_loc.dwo. */
24353
24354static struct dwarf2_section_info *
24355cu_debug_loc_section (struct dwarf2_cu *cu)
24356{
976ca316 24357 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24358
3019eac3 24359 if (cu->dwo_unit)
43988095
JK
24360 {
24361 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24362
43988095
JK
24363 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24364 }
976ca316
SM
24365 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24366 : &per_objfile->per_bfd->loc);
3019eac3
DE
24367}
24368
d0ce17d8
CT
24369/* Return the .debug_rnglists section to use for CU. */
24370static struct dwarf2_section_info *
24371cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24372{
24373 if (cu->header.version < 5)
24374 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24375 cu->header.version);
24376 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24377
24378 /* Make sure we read the .debug_rnglists section from the file that
24379 contains the DW_AT_ranges attribute we are reading. Normally that
24380 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24381 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24382 program. */
24383 if (cu->dwo_unit != nullptr
24384 && tag != DW_TAG_compile_unit
24385 && tag != DW_TAG_skeleton_unit)
24386 {
24387 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24388
24389 if (sections->rnglists.size > 0)
24390 return &sections->rnglists;
24391 else
24392 error (_(".debug_rnglists section is missing from .dwo file."));
24393 }
24394 return &dwarf2_per_objfile->per_bfd->rnglists;
24395}
24396
8cf6f0b1
TT
24397/* A helper function that fills in a dwarf2_loclist_baton. */
24398
24399static void
24400fill_in_loclist_baton (struct dwarf2_cu *cu,
24401 struct dwarf2_loclist_baton *baton,
ff39bb5e 24402 const struct attribute *attr)
8cf6f0b1 24403{
976ca316 24404 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24405 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24406
976ca316 24407 section->read (per_objfile->objfile);
8cf6f0b1 24408
976ca316 24409 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24410 baton->per_cu = cu->per_cu;
24411 gdb_assert (baton->per_cu);
24412 /* We don't know how long the location list is, but make sure we
24413 don't run off the edge of the section. */
d4df075e
TT
24414 baton->size = section->size - attr->as_unsigned ();
24415 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24416 if (cu->base_address.has_value ())
24417 baton->base_address = *cu->base_address;
24418 else
24419 baton->base_address = 0;
f664829e 24420 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24421}
24422
4c2df51b 24423static void
ff39bb5e 24424dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24425 struct dwarf2_cu *cu, int is_block)
4c2df51b 24426{
976ca316
SM
24427 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24428 struct objfile *objfile = per_objfile->objfile;
3019eac3 24429 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24430
cd6c91b4 24431 if (attr->form_is_section_offset ()
3019eac3 24432 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24433 the section. If so, fall through to the complaint in the
24434 other branch. */
d4df075e 24435 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24436 {
0d53c4c4 24437 struct dwarf2_loclist_baton *baton;
4c2df51b 24438
8d749320 24439 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24440
8cf6f0b1 24441 fill_in_loclist_baton (cu, baton, attr);
be391dca 24442
2b24b6e4 24443 if (!cu->base_address.has_value ())
b98664d3 24444 complaint (_("Location list used without "
3e43a32a 24445 "specifying the CU base address."));
4c2df51b 24446
f1e6e072
TT
24447 SYMBOL_ACLASS_INDEX (sym) = (is_block
24448 ? dwarf2_loclist_block_index
24449 : dwarf2_loclist_index);
0d53c4c4
DJ
24450 SYMBOL_LOCATION_BATON (sym) = baton;
24451 }
24452 else
24453 {
24454 struct dwarf2_locexpr_baton *baton;
24455
8d749320 24456 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24457 baton->per_objfile = per_objfile;
ae0d2f24
UW
24458 baton->per_cu = cu->per_cu;
24459 gdb_assert (baton->per_cu);
0d53c4c4 24460
4fc6c0d5 24461 if (attr->form_is_block ())
0d53c4c4
DJ
24462 {
24463 /* Note that we're just copying the block's data pointer
24464 here, not the actual data. We're still pointing into the
6502dd73
DJ
24465 info_buffer for SYM's objfile; right now we never release
24466 that buffer, but when we do clean up properly this may
24467 need to change. */
9d2246fc
TT
24468 struct dwarf_block *block = attr->as_block ();
24469 baton->size = block->size;
24470 baton->data = block->data;
0d53c4c4
DJ
24471 }
24472 else
24473 {
24474 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24475 sym->natural_name ());
0d53c4c4 24476 baton->size = 0;
0d53c4c4 24477 }
6e70227d 24478
f1e6e072
TT
24479 SYMBOL_ACLASS_INDEX (sym) = (is_block
24480 ? dwarf2_locexpr_block_index
24481 : dwarf2_locexpr_index);
0d53c4c4
DJ
24482 SYMBOL_LOCATION_BATON (sym) = baton;
24483 }
4c2df51b 24484}
6502dd73 24485
2e6a9f79 24486/* See read.h. */
96408a79 24487
2e6a9f79
SM
24488const comp_unit_head *
24489dwarf2_per_cu_data::get_header () const
96408a79 24490{
2e6a9f79
SM
24491 if (!m_header_read_in)
24492 {
24493 const gdb_byte *info_ptr
24494 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24495
2e6a9f79 24496 memset (&m_header, 0, sizeof (m_header));
96408a79 24497
2e6a9f79
SM
24498 read_comp_unit_head (&m_header, info_ptr, this->section,
24499 rcuh_kind::COMPILE);
a9f172c6
TT
24500
24501 m_header_read_in = true;
2e6a9f79 24502 }
96408a79 24503
2e6a9f79 24504 return &m_header;
96408a79
SA
24505}
24506
09ba997f 24507/* See read.h. */
ae0d2f24 24508
98714339 24509int
09ba997f 24510dwarf2_per_cu_data::addr_size () const
ae0d2f24 24511{
2e6a9f79 24512 return this->get_header ()->addr_size;
ae0d2f24
UW
24513}
24514
09ba997f 24515/* See read.h. */
9eae7c52
TT
24516
24517int
09ba997f 24518dwarf2_per_cu_data::offset_size () const
9eae7c52 24519{
2e6a9f79 24520 return this->get_header ()->offset_size;
96408a79
SA
24521}
24522
09ba997f 24523/* See read.h. */
96408a79
SA
24524
24525int
09ba997f 24526dwarf2_per_cu_data::ref_addr_size () const
96408a79 24527{
2e6a9f79 24528 const comp_unit_head *header = this->get_header ();
96408a79 24529
2e6a9f79
SM
24530 if (header->version == 2)
24531 return header->addr_size;
96408a79 24532 else
2e6a9f79 24533 return header->offset_size;
181cebd4
JK
24534}
24535
09ba997f 24536/* See read.h. */
9aa1f1e3 24537
09ba997f 24538struct type *
293e7e51 24539dwarf2_cu::addr_type () const
9a49df9d 24540{
293e7e51 24541 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24542 struct type *void_type = objfile_type (objfile)->builtin_void;
24543 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24544 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24545
24546 if (TYPE_LENGTH (addr_type) == addr_size)
24547 return addr_type;
24548
c6d940a9 24549 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24550 return addr_type;
24551}
24552
22b6cd70
TT
24553/* A helper function for dwarf2_find_containing_comp_unit that returns
24554 the index of the result, and that searches a vector. It will
24555 return a result even if the offset in question does not actually
24556 occur in any CU. This is separate so that it can be unit
24557 tested. */
ae038cb0 24558
22b6cd70
TT
24559static int
24560dwarf2_find_containing_comp_unit
24561 (sect_offset sect_off,
24562 unsigned int offset_in_dwz,
0d305d5c 24563 const std::vector<std::unique_ptr<dwarf2_per_cu_data>> &all_comp_units)
ae038cb0 24564{
ae038cb0
DJ
24565 int low, high;
24566
ae038cb0 24567 low = 0;
22b6cd70 24568 high = all_comp_units.size () - 1;
ae038cb0
DJ
24569 while (high > low)
24570 {
36586728 24571 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24572 int mid = low + (high - low) / 2;
9a619af0 24573
0d305d5c 24574 mid_cu = all_comp_units[mid].get ();
36586728 24575 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24576 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24577 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24578 high = mid;
24579 else
24580 low = mid + 1;
24581 }
24582 gdb_assert (low == high);
22b6cd70
TT
24583 return low;
24584}
24585
24586/* Locate the .debug_info compilation unit from CU's objfile which contains
24587 the DIE at OFFSET. Raises an error on failure. */
24588
24589static struct dwarf2_per_cu_data *
24590dwarf2_find_containing_comp_unit (sect_offset sect_off,
24591 unsigned int offset_in_dwz,
976ca316 24592 dwarf2_per_objfile *per_objfile)
22b6cd70 24593{
976ca316
SM
24594 int low = dwarf2_find_containing_comp_unit
24595 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
0d305d5c
TT
24596 dwarf2_per_cu_data *this_cu
24597 = per_objfile->per_bfd->all_comp_units[low].get ();
22b6cd70 24598
45b8ae0c 24599 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24600 {
36586728 24601 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24602 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24603 "offset %s [in module %s]"),
24604 sect_offset_str (sect_off),
976ca316 24605 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24606
976ca316 24607 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24608 <= sect_off);
0d305d5c 24609 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
ae038cb0
DJ
24610 }
24611 else
24612 {
976ca316 24613 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24614 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24615 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24616 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24617 return this_cu;
24618 }
24619}
24620
22b6cd70
TT
24621#if GDB_SELF_TEST
24622
24623namespace selftests {
24624namespace find_containing_comp_unit {
24625
24626static void
24627run_test ()
24628{
0d305d5c
TT
24629 std::unique_ptr<dwarf2_per_cu_data> one (new dwarf2_per_cu_data);
24630 dwarf2_per_cu_data *one_ptr = one.get ();
24631 std::unique_ptr<dwarf2_per_cu_data> two (new dwarf2_per_cu_data);
24632 dwarf2_per_cu_data *two_ptr = two.get ();
24633 std::unique_ptr<dwarf2_per_cu_data> three (new dwarf2_per_cu_data);
24634 dwarf2_per_cu_data *three_ptr = three.get ();
24635 std::unique_ptr<dwarf2_per_cu_data> four (new dwarf2_per_cu_data);
24636 dwarf2_per_cu_data *four_ptr = four.get ();
24637
24638 one->length = 5;
24639 two->sect_off = sect_offset (one->length);
24640 two->length = 7;
24641
24642 three->length = 5;
24643 three->is_dwz = 1;
24644 four->sect_off = sect_offset (three->length);
24645 four->length = 7;
24646 four->is_dwz = 1;
24647
24648 std::vector<std::unique_ptr<dwarf2_per_cu_data>> units;
24649 units.push_back (std::move (one));
24650 units.push_back (std::move (two));
24651 units.push_back (std::move (three));
24652 units.push_back (std::move (four));
22b6cd70
TT
24653
24654 int result;
24655
24656 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
0d305d5c 24657 SELF_CHECK (units[result].get () == one_ptr);
22b6cd70 24658 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
0d305d5c 24659 SELF_CHECK (units[result].get () == one_ptr);
22b6cd70 24660 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
0d305d5c 24661 SELF_CHECK (units[result].get () == two_ptr);
22b6cd70
TT
24662
24663 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
0d305d5c 24664 SELF_CHECK (units[result].get () == three_ptr);
22b6cd70 24665 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
0d305d5c 24666 SELF_CHECK (units[result].get () == three_ptr);
22b6cd70 24667 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
0d305d5c 24668 SELF_CHECK (units[result].get () == four_ptr);
22b6cd70
TT
24669}
24670
24671}
24672}
24673
24674#endif /* GDB_SELF_TEST */
24675
9e021579 24676/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 24677
9e021579
SM
24678dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24679 dwarf2_per_objfile *per_objfile)
24680 : per_cu (per_cu),
24681 per_objfile (per_objfile),
9068261f
AB
24682 mark (false),
24683 has_loclist (false),
24684 checked_producer (false),
24685 producer_is_gxx_lt_4_6 (false),
24686 producer_is_gcc_lt_4_3 (false),
eb77c9df 24687 producer_is_icc (false),
9068261f 24688 producer_is_icc_lt_14 (false),
c258c396 24689 producer_is_codewarrior (false),
9068261f 24690 processing_has_namespace_info (false)
93311388 24691{
9816fde3
JK
24692}
24693
24694/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24695
24696static void
95554aad
TT
24697prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24698 enum language pretend_language)
9816fde3
JK
24699{
24700 struct attribute *attr;
24701
24702 /* Set the language we're debugging. */
24703 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24704 if (attr != nullptr)
529908cb 24705 set_cu_language (attr->constant_value (0), cu);
9816fde3 24706 else
9cded63f 24707 {
95554aad 24708 cu->language = pretend_language;
9cded63f
TT
24709 cu->language_defn = language_def (cu->language);
24710 }
dee91e82 24711
7d45c7c3 24712 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24713}
24714
7188ed02 24715/* See read.h. */
ae038cb0 24716
7188ed02
SM
24717dwarf2_cu *
24718dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24719{
7188ed02
SM
24720 auto it = m_dwarf2_cus.find (per_cu);
24721 if (it == m_dwarf2_cus.end ())
24722 return nullptr;
ae038cb0 24723
7188ed02
SM
24724 return it->second;
24725}
24726
24727/* See read.h. */
24728
24729void
24730dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24731{
24732 gdb_assert (this->get_cu (per_cu) == nullptr);
24733
24734 m_dwarf2_cus[per_cu] = cu;
24735}
24736
24737/* See read.h. */
24738
24739void
24740dwarf2_per_objfile::age_comp_units ()
24741{
17e593e9
SM
24742 dwarf_read_debug_printf_v ("running");
24743
08ac5771
SM
24744 /* This is not expected to be called in the middle of CU expansion. There is
24745 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24746 loaded in memory. Calling age_comp_units while the queue is in use could
24747 make us free the DIEs for a CU that is in the queue and therefore break
24748 that invariant. */
24749 gdb_assert (!this->per_bfd->queue.has_value ());
24750
7188ed02
SM
24751 /* Start by clearing all marks. */
24752 for (auto pair : m_dwarf2_cus)
24753 pair.second->mark = false;
24754
24755 /* Traverse all CUs, mark them and their dependencies if used recently
24756 enough. */
24757 for (auto pair : m_dwarf2_cus)
ae038cb0 24758 {
7188ed02
SM
24759 dwarf2_cu *cu = pair.second;
24760
24761 cu->last_used++;
24762 if (cu->last_used <= dwarf_max_cache_age)
24763 dwarf2_mark (cu);
ae038cb0
DJ
24764 }
24765
7188ed02
SM
24766 /* Delete all CUs still not marked. */
24767 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 24768 {
7188ed02 24769 dwarf2_cu *cu = it->second;
ae038cb0 24770
7188ed02 24771 if (!cu->mark)
ae038cb0 24772 {
17e593e9
SM
24773 dwarf_read_debug_printf_v ("deleting old CU %s",
24774 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
24775 delete cu;
24776 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
24777 }
24778 else
7188ed02 24779 it++;
ae038cb0
DJ
24780 }
24781}
24782
7188ed02 24783/* See read.h. */
ae038cb0 24784
7188ed02
SM
24785void
24786dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24787{
7188ed02
SM
24788 auto it = m_dwarf2_cus.find (per_cu);
24789 if (it == m_dwarf2_cus.end ())
24790 return;
ae038cb0 24791
7188ed02 24792 delete it->second;
ae038cb0 24793
7188ed02
SM
24794 m_dwarf2_cus.erase (it);
24795}
ae038cb0 24796
7188ed02
SM
24797dwarf2_per_objfile::~dwarf2_per_objfile ()
24798{
24799 remove_all_cus ();
ae038cb0
DJ
24800}
24801
dee91e82
DE
24802/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24803 We store these in a hash table separate from the DIEs, and preserve them
24804 when the DIEs are flushed out of cache.
24805
24806 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24807 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24808 or the type may come from a DWO file. Furthermore, while it's more logical
24809 to use per_cu->section+offset, with Fission the section with the data is in
24810 the DWO file but we don't know that section at the point we need it.
24811 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24812 because we can enter the lookup routine, get_die_type_at_offset, from
24813 outside this file, and thus won't necessarily have PER_CU->cu.
24814 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24815
dee91e82 24816struct dwarf2_per_cu_offset_and_type
1c379e20 24817{
dee91e82 24818 const struct dwarf2_per_cu_data *per_cu;
9c541725 24819 sect_offset sect_off;
1c379e20
DJ
24820 struct type *type;
24821};
24822
dee91e82 24823/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24824
24825static hashval_t
dee91e82 24826per_cu_offset_and_type_hash (const void *item)
1c379e20 24827{
9a3c8263
SM
24828 const struct dwarf2_per_cu_offset_and_type *ofs
24829 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24830
9c541725 24831 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24832}
24833
dee91e82 24834/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24835
24836static int
dee91e82 24837per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24838{
9a3c8263
SM
24839 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24840 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24841 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24842 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24843
dee91e82 24844 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24845 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24846}
24847
24848/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24849 table if necessary. For convenience, return TYPE.
24850
24851 The DIEs reading must have careful ordering to:
85102364 24852 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24853 reading current DIE.
24854 * Not trying to dereference contents of still incompletely read in types
24855 while reading in other DIEs.
24856 * Enable referencing still incompletely read in types just by a pointer to
24857 the type without accessing its fields.
24858
24859 Therefore caller should follow these rules:
24860 * Try to fetch any prerequisite types we may need to build this DIE type
24861 before building the type and calling set_die_type.
e71ec853 24862 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24863 possible before fetching more types to complete the current type.
24864 * Make the type as complete as possible before fetching more types. */
1c379e20 24865
f792889a 24866static struct type *
57567375
TT
24867set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24868 bool skip_data_location)
1c379e20 24869{
976ca316 24870 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 24871 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 24872 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
24873 struct attribute *attr;
24874 struct dynamic_prop prop;
1c379e20 24875
b4ba55a1
JB
24876 /* For Ada types, make sure that the gnat-specific data is always
24877 initialized (if not already set). There are a few types where
24878 we should not be doing so, because the type-specific area is
24879 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24880 where the type-specific area is used to store the floatformat).
24881 But this is not a problem, because the gnat-specific information
24882 is actually not needed for these types. */
24883 if (need_gnat_info (cu)
78134374
SM
24884 && type->code () != TYPE_CODE_FUNC
24885 && type->code () != TYPE_CODE_FLT
24886 && type->code () != TYPE_CODE_METHODPTR
24887 && type->code () != TYPE_CODE_MEMBERPTR
24888 && type->code () != TYPE_CODE_METHOD
09584414 24889 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
24890 && !HAVE_GNAT_AUX_INFO (type))
24891 INIT_GNAT_SPECIFIC (type);
24892
3f2f83dd
KB
24893 /* Read DW_AT_allocated and set in type. */
24894 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 24895 if (attr != NULL)
3f2f83dd 24896 {
293e7e51 24897 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24898 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24899 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 24900 }
3f2f83dd
KB
24901
24902 /* Read DW_AT_associated and set in type. */
24903 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 24904 if (attr != NULL)
3f2f83dd 24905 {
293e7e51 24906 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24907 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24908 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 24909 }
3f2f83dd 24910
3cdcd0ce 24911 /* Read DW_AT_data_location and set in type. */
57567375
TT
24912 if (!skip_data_location)
24913 {
24914 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24915 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24916 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24917 }
3cdcd0ce 24918
976ca316
SM
24919 if (per_objfile->die_type_hash == NULL)
24920 per_objfile->die_type_hash
0335378b
TT
24921 = htab_up (htab_create_alloc (127,
24922 per_cu_offset_and_type_hash,
24923 per_cu_offset_and_type_eq,
24924 NULL, xcalloc, xfree));
1c379e20 24925
dee91e82 24926 ofs.per_cu = cu->per_cu;
9c541725 24927 ofs.sect_off = die->sect_off;
1c379e20 24928 ofs.type = type;
dee91e82 24929 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 24930 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24931 if (*slot)
b98664d3 24932 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24933 sect_offset_str (die->sect_off));
8d749320
SM
24934 *slot = XOBNEW (&objfile->objfile_obstack,
24935 struct dwarf2_per_cu_offset_and_type);
1c379e20 24936 **slot = ofs;
f792889a 24937 return type;
1c379e20
DJ
24938}
24939
9c541725 24940/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24941 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24942
24943static struct type *
9c541725 24944get_die_type_at_offset (sect_offset sect_off,
aa66c379 24945 dwarf2_per_cu_data *per_cu,
976ca316 24946 dwarf2_per_objfile *per_objfile)
1c379e20 24947{
dee91e82 24948 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 24949
976ca316 24950 if (per_objfile->die_type_hash == NULL)
f792889a 24951 return NULL;
1c379e20 24952
dee91e82 24953 ofs.per_cu = per_cu;
9c541725 24954 ofs.sect_off = sect_off;
9a3c8263 24955 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 24956 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24957 if (slot)
24958 return slot->type;
24959 else
24960 return NULL;
24961}
24962
02142a6c 24963/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24964 or return NULL if DIE does not have a saved type. */
24965
24966static struct type *
24967get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24968{
aa66c379 24969 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
24970}
24971
10b3939b
DJ
24972/* Add a dependence relationship from CU to REF_PER_CU. */
24973
24974static void
24975dwarf2_add_dependence (struct dwarf2_cu *cu,
24976 struct dwarf2_per_cu_data *ref_per_cu)
24977{
24978 void **slot;
24979
24980 if (cu->dependencies == NULL)
24981 cu->dependencies
24982 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24983 NULL, &cu->comp_unit_obstack,
24984 hashtab_obstack_allocate,
24985 dummy_obstack_deallocate);
24986
24987 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24988 if (*slot == NULL)
24989 *slot = ref_per_cu;
24990}
1c379e20 24991
f504f079
DE
24992/* Subroutine of dwarf2_mark to pass to htab_traverse.
24993 Set the mark field in every compilation unit in the
7188ed02
SM
24994 cache that we must keep because we are keeping CU.
24995
24996 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 24997
10b3939b
DJ
24998static int
24999dwarf2_mark_helper (void **slot, void *data)
25000{
7188ed02
SM
25001 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25002 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25003 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25004
25005 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25006 reading of the chain. As such dependencies remain valid it is not much
25007 useful to track and undo them during QUIT cleanups. */
7188ed02 25008 if (cu == nullptr)
d07ed419
JK
25009 return 1;
25010
7188ed02 25011 if (cu->mark)
10b3939b 25012 return 1;
10b3939b 25013
7188ed02
SM
25014 cu->mark = true;
25015
25016 if (cu->dependencies != nullptr)
25017 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25018
25019 return 1;
25020}
25021
f504f079
DE
25022/* Set the mark field in CU and in every other compilation unit in the
25023 cache that we must keep because we are keeping CU. */
25024
ae038cb0
DJ
25025static void
25026dwarf2_mark (struct dwarf2_cu *cu)
25027{
25028 if (cu->mark)
25029 return;
7188ed02 25030
9068261f 25031 cu->mark = true;
ae038cb0 25032
7188ed02
SM
25033 if (cu->dependencies != nullptr)
25034 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25035}
25036
72bf9492
DJ
25037/* Trivial hash function for partial_die_info: the hash value of a DIE
25038 is its offset in .debug_info for this objfile. */
25039
25040static hashval_t
25041partial_die_hash (const void *item)
25042{
9a3c8263
SM
25043 const struct partial_die_info *part_die
25044 = (const struct partial_die_info *) item;
9a619af0 25045
9c541725 25046 return to_underlying (part_die->sect_off);
72bf9492
DJ
25047}
25048
25049/* Trivial comparison function for partial_die_info structures: two DIEs
25050 are equal if they have the same offset. */
25051
25052static int
25053partial_die_eq (const void *item_lhs, const void *item_rhs)
25054{
9a3c8263
SM
25055 const struct partial_die_info *part_die_lhs
25056 = (const struct partial_die_info *) item_lhs;
25057 const struct partial_die_info *part_die_rhs
25058 = (const struct partial_die_info *) item_rhs;
9a619af0 25059
9c541725 25060 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25061}
25062
3c3bb058
AB
25063struct cmd_list_element *set_dwarf_cmdlist;
25064struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25065
9291a0cd 25066static void
cd4fb1b2
SM
25067show_check_physname (struct ui_file *file, int from_tty,
25068 struct cmd_list_element *c, const char *value)
9291a0cd 25069{
cd4fb1b2
SM
25070 fprintf_filtered (file,
25071 _("Whether to check \"physname\" is %s.\n"),
25072 value);
9291a0cd
TT
25073}
25074
6c265988 25075void _initialize_dwarf2_read ();
cd4fb1b2 25076void
6c265988 25077_initialize_dwarf2_read ()
9291a0cd 25078{
0743fc83 25079 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25080Set DWARF specific variables.\n\
590042fc 25081Configure DWARF variables such as the cache size."),
0743fc83
TT
25082 &set_dwarf_cmdlist, "maintenance set dwarf ",
25083 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25084
0743fc83 25085 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25086Show DWARF specific variables.\n\
25087Show DWARF variables such as the cache size."),
0743fc83
TT
25088 &show_dwarf_cmdlist, "maintenance show dwarf ",
25089 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25090
cd4fb1b2
SM
25091 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25092 &dwarf_max_cache_age, _("\
25093Set the upper bound on the age of cached DWARF compilation units."), _("\
25094Show the upper bound on the age of cached DWARF compilation units."), _("\
25095A higher limit means that cached compilation units will be stored\n\
25096in memory longer, and more total memory will be used. Zero disables\n\
25097caching, which can slow down startup."),
25098 NULL,
25099 show_dwarf_max_cache_age,
25100 &set_dwarf_cmdlist,
25101 &show_dwarf_cmdlist);
156942c7 25102
cd4fb1b2
SM
25103 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25104Set debugging of the DWARF reader."), _("\
25105Show debugging of the DWARF reader."), _("\
25106When enabled (non-zero), debugging messages are printed during DWARF\n\
25107reading and symtab expansion. A value of 1 (one) provides basic\n\
25108information. A value greater than 1 provides more verbose information."),
25109 NULL,
25110 NULL,
25111 &setdebuglist, &showdebuglist);
9291a0cd 25112
cd4fb1b2
SM
25113 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25114Set debugging of the DWARF DIE reader."), _("\
25115Show debugging of the DWARF DIE reader."), _("\
25116When enabled (non-zero), DIEs are dumped after they are read in.\n\
25117The value is the maximum depth to print."),
25118 NULL,
25119 NULL,
25120 &setdebuglist, &showdebuglist);
9291a0cd 25121
cd4fb1b2
SM
25122 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25123Set debugging of the dwarf line reader."), _("\
25124Show debugging of the dwarf line reader."), _("\
25125When enabled (non-zero), line number entries are dumped as they are read in.\n\
25126A value of 1 (one) provides basic information.\n\
25127A value greater than 1 provides more verbose information."),
25128 NULL,
25129 NULL,
25130 &setdebuglist, &showdebuglist);
437afbb8 25131
cd4fb1b2
SM
25132 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25133Set cross-checking of \"physname\" code against demangler."), _("\
25134Show cross-checking of \"physname\" code against demangler."), _("\
25135When enabled, GDB's internal \"physname\" code is checked against\n\
25136the demangler."),
25137 NULL, show_check_physname,
25138 &setdebuglist, &showdebuglist);
900e11f9 25139
e615022a
DE
25140 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25141 no_class, &use_deprecated_index_sections, _("\
25142Set whether to use deprecated gdb_index sections."), _("\
25143Show whether to use deprecated gdb_index sections."), _("\
25144When enabled, deprecated .gdb_index sections are used anyway.\n\
25145Normally they are ignored either because of a missing feature or\n\
25146performance issue.\n\
25147Warning: This option must be enabled before gdb reads the file."),
25148 NULL,
25149 NULL,
25150 &setlist, &showlist);
25151
f1e6e072
TT
25152 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25153 &dwarf2_locexpr_funcs);
25154 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25155 &dwarf2_loclist_funcs);
25156
25157 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25158 &dwarf2_block_frame_base_locexpr_funcs);
25159 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25160 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25161
25162#if GDB_SELF_TEST
25163 selftests::register_test ("dw2_expand_symtabs_matching",
25164 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25165 selftests::register_test ("dwarf2_find_containing_comp_unit",
25166 selftests::find_containing_comp_unit::run_test);
c62446b1 25167#endif
6502dd73 25168}
This page took 5.678823 seconds and 4 git commands to generate.