gdb/testsuite: rename .py.in files to .py
[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
3db19b2d
TV
687 /* If this CU was inherited by another CU (via specification,
688 abstract_origin, etc), this is the ancestor CU. */
689 dwarf2_cu *ancestor;
690
c24bdb02
KS
691 /* Get the buildsym_compunit for this CU. */
692 buildsym_compunit *get_builder ()
693 {
694 /* If this CU has a builder associated with it, use that. */
695 if (m_builder != nullptr)
696 return m_builder.get ();
697
3db19b2d
TV
698 /* Otherwise, search ancestors for a valid builder. */
699 if (ancestor != nullptr)
700 return ancestor->get_builder ();
701
c24bdb02
KS
702 return nullptr;
703 }
e7c27a73
DJ
704};
705
094b34ac
DE
706/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
707 This includes type_unit_group and quick_file_names. */
708
709struct stmt_list_hash
710{
711 /* The DWO unit this table is from or NULL if there is none. */
712 struct dwo_unit *dwo_unit;
713
714 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 715 sect_offset line_sect_off;
094b34ac
DE
716};
717
5989a64e 718/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
719 an object of this type. This contains elements of type unit groups
720 that can be shared across objfiles. The non-shareable parts are in
721 type_unit_group_unshareable. */
f4dc4d17 722
0d305d5c 723struct type_unit_group : public dwarf2_per_cu_data
f4dc4d17 724{
0186c6a7
DE
725 /* The TUs that share this DW_AT_stmt_list entry.
726 This is added to while parsing type units to build partial symtabs,
727 and is deleted afterwards and not used again. */
0d305d5c 728 std::vector<signatured_type *> *tus = nullptr;
f4dc4d17 729
094b34ac 730 /* The data used to construct the hash key. */
0d305d5c 731 struct stmt_list_hash hash {};
f4dc4d17
DE
732};
733
73869dc2 734/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
735
736struct dwo_sections
737{
738 struct dwarf2_section_info abbrev;
3019eac3
DE
739 struct dwarf2_section_info line;
740 struct dwarf2_section_info loc;
43988095 741 struct dwarf2_section_info loclists;
09262596
DE
742 struct dwarf2_section_info macinfo;
743 struct dwarf2_section_info macro;
d0ce17d8 744 struct dwarf2_section_info rnglists;
3019eac3
DE
745 struct dwarf2_section_info str;
746 struct dwarf2_section_info str_offsets;
80626a55
DE
747 /* In the case of a virtual DWO file, these two are unused. */
748 struct dwarf2_section_info info;
fd5866f6 749 std::vector<dwarf2_section_info> types;
3019eac3
DE
750};
751
c88ee1f0 752/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
753
754struct dwo_unit
755{
756 /* Backlink to the containing struct dwo_file. */
757 struct dwo_file *dwo_file;
758
759 /* The "id" that distinguishes this CU/TU.
760 .debug_info calls this "dwo_id", .debug_types calls this "signature".
761 Since signatures came first, we stick with it for consistency. */
762 ULONGEST signature;
763
764 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 765 struct dwarf2_section_info *section;
3019eac3 766
9c541725
PA
767 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
768 sect_offset sect_off;
3019eac3
DE
769 unsigned int length;
770
771 /* For types, offset in the type's DIE of the type defined by this TU. */
772 cu_offset type_offset_in_tu;
773};
774
73869dc2
DE
775/* include/dwarf2.h defines the DWP section codes.
776 It defines a max value but it doesn't define a min value, which we
777 use for error checking, so provide one. */
778
779enum dwp_v2_section_ids
780{
781 DW_SECT_MIN = 1
782};
783
80626a55 784/* Data for one DWO file.
57d63ce2
DE
785
786 This includes virtual DWO files (a virtual DWO file is a DWO file as it
787 appears in a DWP file). DWP files don't really have DWO files per se -
788 comdat folding of types "loses" the DWO file they came from, and from
789 a high level view DWP files appear to contain a mass of random types.
790 However, to maintain consistency with the non-DWP case we pretend DWP
791 files contain virtual DWO files, and we assign each TU with one virtual
792 DWO file (generally based on the line and abbrev section offsets -
793 a heuristic that seems to work in practice). */
3019eac3
DE
794
795struct dwo_file
796{
51ac9db5
SM
797 dwo_file () = default;
798 DISABLE_COPY_AND_ASSIGN (dwo_file);
799
18a8505e 800 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
801 For virtual DWO files the name is constructed from the section offsets
802 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
803 from related CU+TUs. */
51ac9db5 804 const char *dwo_name = nullptr;
0ac5b59e
DE
805
806 /* The DW_AT_comp_dir attribute. */
51ac9db5 807 const char *comp_dir = nullptr;
3019eac3 808
80626a55
DE
809 /* The bfd, when the file is open. Otherwise this is NULL.
810 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 811 gdb_bfd_ref_ptr dbfd;
3019eac3 812
73869dc2 813 /* The sections that make up this DWO file.
d2854d8d 814 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
73869dc2 815 sections (for lack of a better name). */
51ac9db5 816 struct dwo_sections sections {};
3019eac3 817
33c5cd75
DB
818 /* The CUs in the file.
819 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
820 an extension to handle LLVM's Link Time Optimization output (where
821 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 822 htab_up cus;
3019eac3
DE
823
824 /* Table of TUs in the file.
825 Each element is a struct dwo_unit. */
b0b6a987 826 htab_up tus;
3019eac3
DE
827};
828
80626a55
DE
829/* These sections are what may appear in a DWP file. */
830
831struct dwp_sections
832{
d2854d8d 833 /* These are used by all DWP versions (1, 2 and 5). */
80626a55
DE
834 struct dwarf2_section_info str;
835 struct dwarf2_section_info cu_index;
836 struct dwarf2_section_info tu_index;
73869dc2 837
d2854d8d 838 /* These are only used by DWP version 2 and version 5 files.
73869dc2
DE
839 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
840 sections are referenced by section number, and are not recorded here.
d2854d8d
CT
841 In DWP version 2 or 5 there is at most one copy of all these sections,
842 each section being (effectively) comprised of the concatenation of all of
843 the individual sections that exist in the version 1 format.
73869dc2
DE
844 To keep the code simple we treat each of these concatenated pieces as a
845 section itself (a virtual section?). */
846 struct dwarf2_section_info abbrev;
847 struct dwarf2_section_info info;
848 struct dwarf2_section_info line;
849 struct dwarf2_section_info loc;
d2854d8d 850 struct dwarf2_section_info loclists;
73869dc2
DE
851 struct dwarf2_section_info macinfo;
852 struct dwarf2_section_info macro;
d2854d8d 853 struct dwarf2_section_info rnglists;
73869dc2
DE
854 struct dwarf2_section_info str_offsets;
855 struct dwarf2_section_info types;
80626a55
DE
856};
857
73869dc2
DE
858/* These sections are what may appear in a virtual DWO file in DWP version 1.
859 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 860
73869dc2 861struct virtual_v1_dwo_sections
80626a55
DE
862{
863 struct dwarf2_section_info abbrev;
864 struct dwarf2_section_info line;
865 struct dwarf2_section_info loc;
866 struct dwarf2_section_info macinfo;
867 struct dwarf2_section_info macro;
868 struct dwarf2_section_info str_offsets;
869 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 870 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
871 struct dwarf2_section_info info_or_types;
872};
873
d2854d8d 874/* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
73869dc2
DE
875 In version 2, the sections of the DWO files are concatenated together
876 and stored in one section of that name. Thus each ELF section contains
877 several "virtual" sections. */
878
d2854d8d 879struct virtual_v2_or_v5_dwo_sections
73869dc2
DE
880{
881 bfd_size_type abbrev_offset;
882 bfd_size_type abbrev_size;
883
884 bfd_size_type line_offset;
885 bfd_size_type line_size;
886
887 bfd_size_type loc_offset;
888 bfd_size_type loc_size;
889
d2854d8d
CT
890 bfd_size_type loclists_offset;
891 bfd_size_type loclists_size;
892
73869dc2
DE
893 bfd_size_type macinfo_offset;
894 bfd_size_type macinfo_size;
895
896 bfd_size_type macro_offset;
897 bfd_size_type macro_size;
898
d2854d8d
CT
899 bfd_size_type rnglists_offset;
900 bfd_size_type rnglists_size;
901
73869dc2
DE
902 bfd_size_type str_offsets_offset;
903 bfd_size_type str_offsets_size;
904
905 /* Each DWP hash table entry records one CU or one TU.
906 That is recorded here, and copied to dwo_unit.section. */
907 bfd_size_type info_or_types_offset;
908 bfd_size_type info_or_types_size;
909};
910
80626a55
DE
911/* Contents of DWP hash tables. */
912
913struct dwp_hash_table
914{
73869dc2 915 uint32_t version, nr_columns;
80626a55 916 uint32_t nr_units, nr_slots;
73869dc2
DE
917 const gdb_byte *hash_table, *unit_table;
918 union
919 {
920 struct
921 {
922 const gdb_byte *indices;
923 } v1;
924 struct
925 {
926 /* This is indexed by column number and gives the id of the section
927 in that column. */
928#define MAX_NR_V2_DWO_SECTIONS \
929 (1 /* .debug_info or .debug_types */ \
930 + 1 /* .debug_abbrev */ \
931 + 1 /* .debug_line */ \
932 + 1 /* .debug_loc */ \
933 + 1 /* .debug_str_offsets */ \
934 + 1 /* .debug_macro or .debug_macinfo */)
935 int section_ids[MAX_NR_V2_DWO_SECTIONS];
936 const gdb_byte *offsets;
937 const gdb_byte *sizes;
938 } v2;
d2854d8d
CT
939 struct
940 {
941 /* This is indexed by column number and gives the id of the section
942 in that column. */
943#define MAX_NR_V5_DWO_SECTIONS \
944 (1 /* .debug_info */ \
945 + 1 /* .debug_abbrev */ \
946 + 1 /* .debug_line */ \
947 + 1 /* .debug_loclists */ \
948 + 1 /* .debug_str_offsets */ \
949 + 1 /* .debug_macro */ \
950 + 1 /* .debug_rnglists */)
951 int section_ids[MAX_NR_V5_DWO_SECTIONS];
952 const gdb_byte *offsets;
953 const gdb_byte *sizes;
954 } v5;
73869dc2 955 } section_pool;
80626a55
DE
956};
957
958/* Data for one DWP file. */
959
960struct dwp_file
961{
400174b1
TT
962 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
963 : name (name_),
964 dbfd (std::move (abfd))
965 {
966 }
967
80626a55
DE
968 /* Name of the file. */
969 const char *name;
970
73869dc2 971 /* File format version. */
400174b1 972 int version = 0;
73869dc2 973
93417882 974 /* The bfd. */
400174b1 975 gdb_bfd_ref_ptr dbfd;
80626a55
DE
976
977 /* Section info for this file. */
400174b1 978 struct dwp_sections sections {};
80626a55 979
57d63ce2 980 /* Table of CUs in the file. */
400174b1 981 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
982
983 /* Table of TUs in the file. */
400174b1 984 const struct dwp_hash_table *tus = nullptr;
80626a55 985
19ac8c2e 986 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
987 htab_up loaded_cus;
988 htab_up loaded_tus;
80626a55 989
73869dc2
DE
990 /* Table to map ELF section numbers to their sections.
991 This is only needed for the DWP V1 file format. */
400174b1
TT
992 unsigned int num_sections = 0;
993 asection **elf_sections = nullptr;
80626a55
DE
994};
995
0963b4bd
MS
996/* Struct used to pass misc. parameters to read_die_and_children, et
997 al. which are used for both .debug_info and .debug_types dies.
998 All parameters here are unchanging for the life of the call. This
dee91e82 999 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1000
1001struct die_reader_specs
1002{
a32a8923 1003 /* The bfd of die_section. */
0280fdcc 1004 bfd *abfd;
93311388
DE
1005
1006 /* The CU of the DIE we are parsing. */
1007 struct dwarf2_cu *cu;
1008
80626a55 1009 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1010 struct dwo_file *dwo_file;
1011
dee91e82 1012 /* The section the die comes from.
3019eac3 1013 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1014 struct dwarf2_section_info *die_section;
1015
1016 /* die_section->buffer. */
d521ce57 1017 const gdb_byte *buffer;
f664829e
DE
1018
1019 /* The end of the buffer. */
1020 const gdb_byte *buffer_end;
a2ce51a0 1021
685af9cd
TT
1022 /* The abbreviation table to use when reading the DIEs. */
1023 struct abbrev_table *abbrev_table;
93311388
DE
1024};
1025
c0ab21c2
TT
1026/* A subclass of die_reader_specs that holds storage and has complex
1027 constructor and destructor behavior. */
1028
1029class cutu_reader : public die_reader_specs
1030{
1031public:
1032
ab432490
SM
1033 cutu_reader (dwarf2_per_cu_data *this_cu,
1034 dwarf2_per_objfile *per_objfile,
c0ab21c2 1035 struct abbrev_table *abbrev_table,
2e671100 1036 dwarf2_cu *existing_cu,
c0ab21c2
TT
1037 bool skip_partial);
1038
1039 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 1040 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
1041 struct dwarf2_cu *parent_cu = nullptr,
1042 struct dwo_file *dwo_file = nullptr);
1043
c0ab21c2
TT
1044 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1045
1046 const gdb_byte *info_ptr = nullptr;
1047 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
1048 bool dummy_p = false;
1049
6751ebae
TT
1050 /* Release the new CU, putting it on the chain. This cannot be done
1051 for dummy CUs. */
1052 void keep ();
1053
c0ab21c2 1054private:
9e021579
SM
1055 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1056 dwarf2_per_objfile *per_objfile,
2e671100 1057 dwarf2_cu *existing_cu);
c0ab21c2
TT
1058
1059 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
1060 std::unique_ptr<dwarf2_cu> m_new_cu;
1061
1062 /* The ordinary abbreviation table. */
1063 abbrev_table_up m_abbrev_table_holder;
1064
1065 /* The DWO abbreviation table. */
1066 abbrev_table_up m_dwo_abbrev_table;
1067};
dee91e82 1068
c906108c 1069/* When we construct a partial symbol table entry we only
0963b4bd 1070 need this much information. */
6f06d47b 1071struct partial_die_info : public allocate_on_obstack
c906108c 1072 {
7c32eebb 1073 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
6f06d47b
YQ
1074
1075 /* Disable assign but still keep copy ctor, which is needed
1076 load_partial_dies. */
1077 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1078
52356b79
YQ
1079 /* Adjust the partial die before generating a symbol for it. This
1080 function may set the is_external flag or change the DIE's
1081 name. */
1082 void fixup (struct dwarf2_cu *cu);
1083
48fbe735
YQ
1084 /* Read a minimal amount of information into the minimal die
1085 structure. */
1086 const gdb_byte *read (const struct die_reader_specs *reader,
1087 const struct abbrev_info &abbrev,
1088 const gdb_byte *info_ptr);
1089
7d00ffec
TT
1090 /* Compute the name of this partial DIE. This memoizes the
1091 result, so it is safe to call multiple times. */
1092 const char *name (dwarf2_cu *cu);
1093
72bf9492 1094 /* Offset of this DIE. */
6f06d47b 1095 const sect_offset sect_off;
72bf9492
DJ
1096
1097 /* DWARF-2 tag for this DIE. */
6f06d47b 1098 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1099
72bf9492 1100 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1101 const unsigned int has_children : 1;
1102
72bf9492
DJ
1103 unsigned int is_external : 1;
1104 unsigned int is_declaration : 1;
1105 unsigned int has_type : 1;
1106 unsigned int has_specification : 1;
1107 unsigned int has_pc_info : 1;
481860b3 1108 unsigned int may_be_inlined : 1;
72bf9492 1109
0c1b455e
TT
1110 /* This DIE has been marked DW_AT_main_subprogram. */
1111 unsigned int main_subprogram : 1;
1112
72bf9492
DJ
1113 /* Flag set if the SCOPE field of this structure has been
1114 computed. */
1115 unsigned int scope_set : 1;
1116
fa4028e9
JB
1117 /* Flag set if the DIE has a byte_size attribute. */
1118 unsigned int has_byte_size : 1;
1119
ff908ebf
AW
1120 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1121 unsigned int has_const_value : 1;
1122
98bfdba5
PA
1123 /* Flag set if any of the DIE's children are template arguments. */
1124 unsigned int has_template_arguments : 1;
1125
52356b79 1126 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1127 unsigned int fixup_called : 1;
1128
36586728
TT
1129 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1130 unsigned int is_dwz : 1;
1131
1132 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1133 unsigned int spec_is_dwz : 1;
1134
7d00ffec
TT
1135 unsigned int canonical_name : 1;
1136
72bf9492 1137 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1138 sometimes a default name for unnamed DIEs. */
7d00ffec 1139 const char *raw_name = nullptr;
72bf9492 1140
abc72ce4 1141 /* The linkage name, if present. */
6f06d47b 1142 const char *linkage_name = nullptr;
abc72ce4 1143
72bf9492
DJ
1144 /* The scope to prepend to our children. This is generally
1145 allocated on the comp_unit_obstack, so will disappear
1146 when this compilation unit leaves the cache. */
6f06d47b 1147 const char *scope = nullptr;
72bf9492 1148
95554aad
TT
1149 /* Some data associated with the partial DIE. The tag determines
1150 which field is live. */
1151 union
1152 {
1153 /* The location description associated with this DIE, if any. */
1154 struct dwarf_block *locdesc;
1155 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1156 sect_offset sect_off;
6f06d47b 1157 } d {};
72bf9492
DJ
1158
1159 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1160 CORE_ADDR lowpc = 0;
1161 CORE_ADDR highpc = 0;
72bf9492 1162
93311388 1163 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1164 DW_AT_sibling, if any. */
48fbe735
YQ
1165 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1166 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1167 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1168
1169 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1170 DW_AT_specification (or DW_AT_abstract_origin or
1171 DW_AT_extension). */
6f06d47b 1172 sect_offset spec_offset {};
72bf9492
DJ
1173
1174 /* Pointers to this DIE's parent, first child, and next sibling,
1175 if any. */
6f06d47b
YQ
1176 struct partial_die_info *die_parent = nullptr;
1177 struct partial_die_info *die_child = nullptr;
1178 struct partial_die_info *die_sibling = nullptr;
1179
1180 friend struct partial_die_info *
1181 dwarf2_cu::find_partial_die (sect_offset sect_off);
1182
1183 private:
1184 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1185 partial_die_info (sect_offset sect_off)
1186 : partial_die_info (sect_off, DW_TAG_padding, 0)
1187 {
1188 }
1189
1190 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1191 int has_children_)
1192 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1193 {
1194 is_external = 0;
1195 is_declaration = 0;
1196 has_type = 0;
1197 has_specification = 0;
1198 has_pc_info = 0;
1199 may_be_inlined = 0;
1200 main_subprogram = 0;
1201 scope_set = 0;
1202 has_byte_size = 0;
1203 has_const_value = 0;
1204 has_template_arguments = 0;
1205 fixup_called = 0;
1206 is_dwz = 0;
1207 spec_is_dwz = 0;
7d00ffec 1208 canonical_name = 0;
6f06d47b 1209 }
c906108c
SS
1210 };
1211
c906108c
SS
1212/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1213 but this would require a corresponding change in unpack_field_as_long
1214 and friends. */
1215static int bits_per_byte = 8;
1216
9c6a1327
TT
1217struct variant_part_builder;
1218
1219/* When reading a variant, we track a bit more information about the
1220 field, and store it in an object of this type. */
2ddeaf8a
TT
1221
1222struct variant_field
1223{
9c6a1327
TT
1224 int first_field = -1;
1225 int last_field = -1;
1226
1227 /* A variant can contain other variant parts. */
1228 std::vector<variant_part_builder> variant_parts;
1229
2ddeaf8a
TT
1230 /* If we see a DW_TAG_variant, then this will be set if this is the
1231 default branch. */
9c6a1327
TT
1232 bool default_branch = false;
1233 /* If we see a DW_AT_discr_value, then this will be the discriminant
1234 value. */
1235 ULONGEST discriminant_value = 0;
1236 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1237 data. */
1238 struct dwarf_block *discr_list_data = nullptr;
1239};
1240
1241/* This represents a DW_TAG_variant_part. */
1242
1243struct variant_part_builder
1244{
1245 /* The offset of the discriminant field. */
1246 sect_offset discriminant_offset {};
1247
1248 /* Variants that are direct children of this variant part. */
1249 std::vector<variant_field> variants;
1250
1251 /* True if we're currently reading a variant. */
1252 bool processing_variant = false;
2ddeaf8a
TT
1253};
1254
52059ffd
TT
1255struct nextfield
1256{
be2daae6
TT
1257 int accessibility = 0;
1258 int virtuality = 0;
9c6a1327
TT
1259 /* Variant parts need to find the discriminant, which is a DIE
1260 reference. We track the section offset of each field to make
1261 this link. */
1262 sect_offset offset;
be2daae6 1263 struct field field {};
52059ffd
TT
1264};
1265
1266struct fnfieldlist
1267{
be2daae6
TT
1268 const char *name = nullptr;
1269 std::vector<struct fn_field> fnfields;
52059ffd
TT
1270};
1271
c906108c
SS
1272/* The routines that read and process dies for a C struct or C++ class
1273 pass lists of data member fields and lists of member function fields
1274 in an instance of a field_info structure, as defined below. */
1275struct field_info
2de01bdb
SM
1276{
1277 /* List of data member and baseclasses fields. */
1278 std::vector<struct nextfield> fields;
1279 std::vector<struct nextfield> baseclasses;
1280
1281 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1282 bool non_public_fields = false;
2de01bdb
SM
1283
1284 /* Member function fieldlist array, contains name of possibly overloaded
1285 member function, number of overloaded member functions and a pointer
1286 to the head of the member function field chain. */
1287 std::vector<struct fnfieldlist> fnfieldlists;
1288
1289 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1290 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1291 std::vector<struct decl_field> typedef_field_list;
1292
1293 /* Nested types defined by this class and the number of elements in this
1294 list. */
1295 std::vector<struct decl_field> nested_types_list;
1296
1297 /* If non-null, this is the variant part we are currently
1298 reading. */
1299 variant_part_builder *current_variant_part = nullptr;
1300 /* This holds all the top-level variant parts attached to the type
1301 we're reading. */
1302 std::vector<variant_part_builder> variant_parts;
1303
1304 /* Return the total number of fields (including baseclasses). */
1305 int nfields () const
c5aa993b 1306 {
2de01bdb
SM
1307 return fields.size () + baseclasses.size ();
1308 }
1309};
c906108c 1310
ae038cb0
DJ
1311/* Loaded secondary compilation units are kept in memory until they
1312 have not been referenced for the processing of this many
1313 compilation units. Set this to zero to disable caching. Cache
1314 sizes of up to at least twenty will improve startup time for
1315 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1316static int dwarf_max_cache_age = 5;
920d2a44 1317static void
b4f54984
DE
1318show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1319 struct cmd_list_element *c, const char *value)
920d2a44 1320{
3e43a32a 1321 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1322 "DWARF compilation units is %s.\n"),
920d2a44
AC
1323 value);
1324}
4390d890 1325\f
c906108c
SS
1326/* local function prototypes */
1327
918dd910
JK
1328static void dwarf2_find_base_address (struct die_info *die,
1329 struct dwarf2_cu *cu);
1330
891813be 1331static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1332 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1333 const char *name);
0018ea6f 1334
f1902523
JK
1335static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1336 const gdb_byte *info_ptr,
3e225074 1337 struct die_info *type_unit_die);
f1902523 1338
976ca316 1339static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1340
72bf9492
DJ
1341static void scan_partial_symbols (struct partial_die_info *,
1342 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1343 int, struct dwarf2_cu *);
c906108c 1344
72bf9492
DJ
1345static void add_partial_symbol (struct partial_die_info *,
1346 struct dwarf2_cu *);
63d06c5c 1347
72bf9492
DJ
1348static void add_partial_namespace (struct partial_die_info *pdi,
1349 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1350 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1351
5d7cb8df 1352static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1353 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1354 struct dwarf2_cu *cu);
1355
72bf9492
DJ
1356static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1357 struct dwarf2_cu *cu);
91c24f0a 1358
bc30ff58
JB
1359static void add_partial_subprogram (struct partial_die_info *pdi,
1360 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1361 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1362
d521ce57 1363static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1364
dee91e82 1365static struct partial_die_info *load_partial_dies
d521ce57 1366 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1367
fb816e8b
TV
1368/* A pair of partial_die_info and compilation unit. */
1369struct cu_partial_die_info
1370{
1371 /* The compilation unit of the partial_die_info. */
1372 struct dwarf2_cu *cu;
1373 /* A partial_die_info. */
1374 struct partial_die_info *pdi;
122cf0f2
AB
1375
1376 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1377 : cu (cu),
1378 pdi (pdi)
405feb71 1379 { /* Nothing. */ }
122cf0f2
AB
1380
1381private:
1382 cu_partial_die_info () = delete;
fb816e8b
TV
1383};
1384
122cf0f2
AB
1385static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1386 struct dwarf2_cu *);
72bf9492 1387
d521ce57 1388static const gdb_byte *read_attribute (const struct die_reader_specs *,
4444f407
TT
1389 struct attribute *,
1390 const struct attr_abbrev *,
7a5f294d 1391 const gdb_byte *);
18a8505e
AT
1392
1393static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1394 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1395
1396static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1397
976ca316
SM
1398static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1399 dwarf2_section_info *, sect_offset);
f4dc4d17 1400
ed2dc618 1401static const char *read_indirect_string
976ca316 1402 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1403 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1404
ed2dc618 1405static const char *read_indirect_string_at_offset
976ca316 1406 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1407
d521ce57
TT
1408static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1409 const gdb_byte *,
3019eac3
DE
1410 unsigned int *);
1411
18a8505e
AT
1412static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1413 ULONGEST str_index);
1414
1415static const char *read_stub_str_index (struct dwarf2_cu *cu,
1416 ULONGEST str_index);
3019eac3 1417
e142c38c 1418static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1419
e142c38c
DJ
1420static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1421 struct dwarf2_cu *);
c906108c 1422
7d45c7c3 1423static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
dda83cd7 1424 struct dwarf2_cu *cu);
7d45c7c3 1425
a084a2a6
AT
1426static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1427
05cf31d1 1428static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
dda83cd7 1429 struct dwarf2_cu *cu);
05cf31d1 1430
e142c38c 1431static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1432
e142c38c 1433static struct die_info *die_specification (struct die_info *die,
f2f0e013 1434 struct dwarf2_cu **);
63d06c5c 1435
9c541725 1436static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1437 struct dwarf2_cu *cu);
debd256d 1438
f3f5162e 1439static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1440 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1441 CORE_ADDR, int decode_mapping);
c906108c 1442
804d2729
TT
1443static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1444 const char *);
c906108c 1445
a14ed312 1446static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1447 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1448
ff39bb5e 1449static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1450 struct dwarf2_cu *);
c906108c 1451
ff39bb5e 1452static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1453 struct type *type,
1454 const char *name,
1455 struct obstack *obstack,
12df843f 1456 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1457 const gdb_byte **bytes,
98bfdba5 1458 struct dwarf2_locexpr_baton **baton);
2df3850c 1459
57567375
TT
1460static struct type *read_subrange_index_type (struct die_info *die,
1461 struct dwarf2_cu *cu);
1462
e7c27a73 1463static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1464
b4ba55a1
JB
1465static int need_gnat_info (struct dwarf2_cu *);
1466
3e43a32a
MS
1467static struct type *die_descriptive_type (struct die_info *,
1468 struct dwarf2_cu *);
b4ba55a1
JB
1469
1470static void set_descriptive_type (struct type *, struct die_info *,
1471 struct dwarf2_cu *);
1472
e7c27a73
DJ
1473static struct type *die_containing_type (struct die_info *,
1474 struct dwarf2_cu *);
c906108c 1475
ff39bb5e 1476static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1477 struct dwarf2_cu *);
c906108c 1478
f792889a 1479static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1480
673bfd45
DE
1481static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1482
0d5cff50 1483static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1484
6e70227d 1485static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1486 const char *suffix, int physname,
1487 struct dwarf2_cu *cu);
63d06c5c 1488
e7c27a73 1489static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1490
348e048f
DE
1491static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1492
e7c27a73 1493static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1494
e7c27a73 1495static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1496
96408a79
SA
1497static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1498
71a3c369
TT
1499static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1500
41144253 1501/* Return the .debug_loclists section to use for cu. */
1502static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1503
d0ce17d8
CT
1504/* Return the .debug_rnglists section to use for cu. */
1505static struct dwarf2_section_info *cu_debug_rnglists_section
1506 (struct dwarf2_cu *cu, dwarf_tag tag);
1507
3a2b436a 1508/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1509 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1510enum pc_bounds_kind
1511{
e385593e 1512 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1513 PC_BOUNDS_NOT_PRESENT,
1514
e385593e
JK
1515 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1516 were present but they do not form a valid range of PC addresses. */
1517 PC_BOUNDS_INVALID,
1518
3a2b436a
JK
1519 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1520 PC_BOUNDS_RANGES,
1521
1522 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1523 PC_BOUNDS_HIGH_LOW,
1524};
1525
1526static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1527 CORE_ADDR *, CORE_ADDR *,
1528 struct dwarf2_cu *,
891813be 1529 dwarf2_psymtab *);
c906108c 1530
fae299cd
DC
1531static void get_scope_pc_bounds (struct die_info *,
1532 CORE_ADDR *, CORE_ADDR *,
1533 struct dwarf2_cu *);
1534
801e3a5b 1535static void dwarf2_record_block_ranges (struct die_info *, struct block *,
dda83cd7 1536 CORE_ADDR, struct dwarf2_cu *);
801e3a5b 1537
a14ed312 1538static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1539 struct dwarf2_cu *);
c906108c 1540
a14ed312 1541static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1542 struct type *, struct dwarf2_cu *);
c906108c 1543
a14ed312 1544static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1545 struct die_info *, struct type *,
e7c27a73 1546 struct dwarf2_cu *);
c906108c 1547
a14ed312 1548static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1549 struct type *,
1550 struct dwarf2_cu *);
c906108c 1551
134d01f1 1552static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1553
e7c27a73 1554static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1555
e7c27a73 1556static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1557
5d7cb8df
JK
1558static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1559
804d2729 1560static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1561
27aa8d6a
SW
1562static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1563
74921315
KS
1564static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1565
f55ee35c
JK
1566static struct type *read_module_type (struct die_info *die,
1567 struct dwarf2_cu *cu);
1568
38d518c9 1569static const char *namespace_name (struct die_info *die,
e142c38c 1570 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1571
134d01f1 1572static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1573
7d79de9a
TT
1574static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1575 bool * = nullptr);
c906108c 1576
6e70227d 1577static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1578 struct dwarf2_cu *);
1579
bf6af496 1580static struct die_info *read_die_and_siblings_1
d521ce57 1581 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1582 struct die_info *);
639d11d3 1583
dee91e82 1584static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1585 const gdb_byte *info_ptr,
1586 const gdb_byte **new_info_ptr,
639d11d3
DC
1587 struct die_info *parent);
1588
d521ce57
TT
1589static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1590 struct die_info **, const gdb_byte *,
3e225074 1591 int);
3019eac3 1592
d521ce57 1593static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1594 struct die_info **, const gdb_byte *);
93311388 1595
e7c27a73 1596static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1597
15d034d0 1598static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1599 struct objfile *);
71c25dea 1600
15d034d0 1601static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1602
15d034d0 1603static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1604 struct die_info *die,
1605 struct dwarf2_cu *cu);
1606
ca69b9e6
DE
1607static const char *dwarf2_physname (const char *name, struct die_info *die,
1608 struct dwarf2_cu *cu);
1609
e142c38c 1610static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1611 struct dwarf2_cu **);
9219021c 1612
d97bc12b
DE
1613static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1614
1615static void dump_die_for_error (struct die_info *);
1616
1617static void dump_die_1 (struct ui_file *, int level, int max_level,
1618 struct die_info *);
c906108c 1619
d97bc12b 1620/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1621
51545339 1622static void store_in_ref_table (struct die_info *,
10b3939b 1623 struct dwarf2_cu *);
c906108c 1624
348e048f 1625static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1626 const struct attribute *,
348e048f
DE
1627 struct dwarf2_cu **);
1628
10b3939b 1629static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1630 const struct attribute *,
f2f0e013 1631 struct dwarf2_cu **);
c906108c 1632
348e048f 1633static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1634 const struct attribute *,
348e048f
DE
1635 struct dwarf2_cu **);
1636
ac9ec31b
DE
1637static struct type *get_signatured_type (struct die_info *, ULONGEST,
1638 struct dwarf2_cu *);
1639
1640static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1641 const struct attribute *,
ac9ec31b
DE
1642 struct dwarf2_cu *);
1643
ab432490
SM
1644static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1645 dwarf2_per_objfile *per_objfile);
348e048f 1646
ab432490
SM
1647static void read_signatured_type (signatured_type *sig_type,
1648 dwarf2_per_objfile *per_objfile);
348e048f 1649
63e43d3a
PMR
1650static int attr_to_dynamic_prop (const struct attribute *attr,
1651 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1652 struct dynamic_prop *prop, struct type *type);
63e43d3a 1653
c906108c
SS
1654/* memory allocation interface */
1655
7b5a2f43 1656static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1657
b60c80d6 1658static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1659
43f3e411 1660static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1661
8cf6f0b1
TT
1662static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1663 struct dwarf2_loclist_baton *baton,
ff39bb5e 1664 const struct attribute *attr);
8cf6f0b1 1665
ff39bb5e 1666static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1667 struct symbol *sym,
f1e6e072
TT
1668 struct dwarf2_cu *cu,
1669 int is_block);
4c2df51b 1670
d521ce57
TT
1671static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1672 const gdb_byte *info_ptr,
7c32eebb 1673 const struct abbrev_info *abbrev);
4bb7a0a7 1674
72bf9492
DJ
1675static hashval_t partial_die_hash (const void *item);
1676
1677static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1678
ae038cb0 1679static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1680 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1681 dwarf2_per_objfile *per_objfile);
ae038cb0 1682
9816fde3 1683static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1684 struct die_info *comp_unit_die,
1685 enum language pretend_language);
93311388 1686
f792889a 1687static struct type *set_die_type (struct die_info *, struct type *,
57567375 1688 struct dwarf2_cu *, bool = false);
1c379e20 1689
976ca316 1690static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1691
ab432490
SM
1692static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1693 dwarf2_per_objfile *per_objfile,
4a636814 1694 dwarf2_cu *existing_cu,
ab432490
SM
1695 bool skip_partial,
1696 enum language pretend_language);
10b3939b 1697
8fc0b21d 1698static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1699 enum language pretend_language);
10b3939b 1700
8fc0b21d 1701static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1702 enum language pretend_language);
f4dc4d17 1703
10b3939b
DJ
1704static void dwarf2_add_dependence (struct dwarf2_cu *,
1705 struct dwarf2_per_cu_data *);
1706
ae038cb0
DJ
1707static void dwarf2_mark (struct dwarf2_cu *);
1708
b64f50a1 1709static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1710 dwarf2_per_cu_data *per_cu,
1711 dwarf2_per_objfile *per_objfile);
673bfd45 1712
f792889a 1713static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1714
120ce1b5
SM
1715static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1716 dwarf2_per_objfile *per_objfile,
95554aad
TT
1717 enum language pretend_language);
1718
976ca316 1719static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1720
b303c6f6
AB
1721/* Class, the destructor of which frees all allocated queue entries. This
1722 will only have work to do if an error was thrown while processing the
1723 dwarf. If no error was thrown then the queue entries should have all
1724 been processed, and freed, as we went along. */
1725
1726class dwarf2_queue_guard
1727{
1728public:
39856def
TT
1729 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1730 : m_per_objfile (per_objfile)
1731 {
08ac5771
SM
1732 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1733
1734 m_per_objfile->per_bfd->queue.emplace ();
39856def 1735 }
b303c6f6
AB
1736
1737 /* Free any entries remaining on the queue. There should only be
1738 entries left if we hit an error while processing the dwarf. */
1739 ~dwarf2_queue_guard ()
1740 {
08ac5771
SM
1741 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1742
1743 m_per_objfile->per_bfd->queue.reset ();
39856def 1744 }
b303c6f6 1745
39856def 1746 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1747
39856def
TT
1748private:
1749 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1750};
1751
39856def
TT
1752dwarf2_queue_item::~dwarf2_queue_item ()
1753{
1754 /* Anything still marked queued is likely to be in an
1755 inconsistent state, so discard it. */
1756 if (per_cu->queued)
1757 {
7188ed02 1758 per_objfile->remove_cu (per_cu);
39856def
TT
1759 per_cu->queued = 0;
1760 }
1761}
1762
d721ba37
PA
1763/* The return type of find_file_and_directory. Note, the enclosed
1764 string pointers are only valid while this object is valid. */
1765
1766struct file_and_directory
1767{
1768 /* The filename. This is never NULL. */
1769 const char *name;
1770
1771 /* The compilation directory. NULL if not known. If we needed to
1772 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1773 points directly to the DW_AT_comp_dir string attribute owned by
1774 the obstack that owns the DIE. */
1775 const char *comp_dir;
1776
1777 /* If we needed to build a new string for comp_dir, this is what
1778 owns the storage. */
1779 std::string comp_dir_storage;
1780};
1781
1782static file_and_directory find_file_and_directory (struct die_info *die,
1783 struct dwarf2_cu *cu);
9291a0cd 1784
298e9637 1785static htab_up allocate_signatured_type_table ();
1fd400ff 1786
298e9637 1787static htab_up allocate_dwo_unit_table ();
3019eac3 1788
57d63ce2 1789static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1790 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1791 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1792
976ca316 1793static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1794
3019eac3 1795static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1796 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1797 ULONGEST signature);
3019eac3
DE
1798
1799static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1800 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1801
1b555f17 1802static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1803
263db9a1
TT
1804/* A unique pointer to a dwo_file. */
1805
51ac9db5 1806typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1807
976ca316 1808static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1809
1b80a9fa 1810static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1811
1812static void free_line_header_voidp (void *arg);
4390d890
DE
1813\f
1814/* Various complaints about symbol reading that don't abort the process. */
1815
4390d890
DE
1816static void
1817dwarf2_debug_line_missing_file_complaint (void)
1818{
b98664d3 1819 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1820}
1821
1822static void
1823dwarf2_debug_line_missing_end_sequence_complaint (void)
1824{
b98664d3 1825 complaint (_(".debug_line section has line "
4390d890
DE
1826 "program sequence without an end"));
1827}
1828
1829static void
1830dwarf2_complex_location_expr_complaint (void)
1831{
b98664d3 1832 complaint (_("location expression too complex"));
4390d890
DE
1833}
1834
1835static void
1836dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1837 int arg3)
1838{
b98664d3 1839 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1840 arg1, arg2, arg3);
1841}
1842
4390d890
DE
1843static void
1844dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1845{
b98664d3 1846 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1847 arg1, arg2);
1848}
527f3840
JK
1849
1850/* Hash function for line_header_hash. */
1851
1852static hashval_t
1853line_header_hash (const struct line_header *ofs)
1854{
9c541725 1855 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1856}
1857
1858/* Hash function for htab_create_alloc_ex for line_header_hash. */
1859
1860static hashval_t
1861line_header_hash_voidp (const void *item)
1862{
9a3c8263 1863 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1864
1865 return line_header_hash (ofs);
1866}
1867
1868/* Equality function for line_header_hash. */
1869
1870static int
1871line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1872{
9a3c8263
SM
1873 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1874 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1875
9c541725 1876 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1877 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1878}
1879
4390d890 1880\f
9291a0cd 1881
330cdd98
PA
1882/* See declaration. */
1883
5989a64e
SM
1884dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1885 bool can_copy_)
c3699833
SM
1886 : obfd (obfd),
1887 can_copy (can_copy_)
330cdd98
PA
1888{
1889 if (names == NULL)
1890 names = &dwarf2_elf_names;
1891
330cdd98
PA
1892 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1893 locate_sections (obfd, sec, *names);
1894}
1895
5989a64e 1896dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1897{
0d305d5c 1898 for (auto &per_cu : all_comp_units)
ae640021 1899 per_cu->imported_symtabs_free ();
fc8e7e75 1900
5989a64e 1901 /* Everything else should be on this->obstack. */
330cdd98
PA
1902}
1903
7188ed02 1904/* See read.h. */
330cdd98
PA
1905
1906void
7188ed02 1907dwarf2_per_objfile::remove_all_cus ()
330cdd98 1908{
08ac5771
SM
1909 gdb_assert (!this->per_bfd->queue.has_value ());
1910
7188ed02
SM
1911 for (auto pair : m_dwarf2_cus)
1912 delete pair.second;
330cdd98 1913
7188ed02 1914 m_dwarf2_cus.clear ();
330cdd98
PA
1915}
1916
11ed8cad
TT
1917/* A helper class that calls free_cached_comp_units on
1918 destruction. */
1919
1920class free_cached_comp_units
1921{
1922public:
1923
1924 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1925 : m_per_objfile (per_objfile)
1926 {
1927 }
1928
1929 ~free_cached_comp_units ()
1930 {
7188ed02 1931 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1932 }
1933
1934 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1935
1936private:
1937
1938 dwarf2_per_objfile *m_per_objfile;
1939};
1940
af758d11
SM
1941/* See read.h. */
1942
1943bool
1944dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1945{
1946 gdb_assert (per_cu->index < this->m_symtabs.size ());
1947
1948 return this->m_symtabs[per_cu->index] != nullptr;
1949}
1950
1951/* See read.h. */
1952
1953compunit_symtab *
1954dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1955{
1956 gdb_assert (per_cu->index < this->m_symtabs.size ());
1957
1958 return this->m_symtabs[per_cu->index];
1959}
1960
1961/* See read.h. */
1962
1963void
1964dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1965 compunit_symtab *symtab)
1966{
1967 gdb_assert (per_cu->index < this->m_symtabs.size ());
1968 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1969
1970 this->m_symtabs[per_cu->index] = symtab;
1971}
1972
c906108c 1973/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1974 information and return true if we have enough to do something.
1975 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1976 ELF names are used. CAN_COPY is true for formats where symbol
1977 interposition is possible and so symbol values must follow copy
1978 relocation rules. */
c906108c
SS
1979
1980int
251d32d9 1981dwarf2_has_info (struct objfile *objfile,
dda83cd7 1982 const struct dwarf2_debug_sections *names,
4b610737 1983 bool can_copy)
c906108c 1984{
97cbe998
SDJ
1985 if (objfile->flags & OBJF_READNEVER)
1986 return 0;
1987
976ca316 1988 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1989
976ca316 1990 if (per_objfile == NULL)
5989a64e 1991 {
17ee85fc
TT
1992 dwarf2_per_bfd *per_bfd;
1993
8a91fbdf
SM
1994 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1995 BFD doesn't require relocations.
1996
1997 We don't share with objfiles for which -readnow was requested,
1998 because it would complicate things when loading the same BFD with
1999 -readnow and then without -readnow. */
2000 if (!gdb_bfd_requires_relocations (objfile->obfd)
2001 && (objfile->flags & OBJF_READNOW) == 0)
17ee85fc
TT
2002 {
2003 /* See if one has been created for this BFD yet. */
2004 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
2005
2006 if (per_bfd == nullptr)
2007 {
2008 /* No, create it now. */
2009 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
2010 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
2011 }
2012 }
2013 else
2014 {
2015 /* No sharing possible, create one specifically for this objfile. */
2016 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
2017 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
2018 }
5989a64e 2019
976ca316 2020 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 2021 }
5bfd760d 2022
976ca316
SM
2023 return (!per_objfile->per_bfd->info.is_virtual
2024 && per_objfile->per_bfd->info.s.section != NULL
2025 && !per_objfile->per_bfd->abbrev.is_virtual
2026 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
2027}
2028
330cdd98 2029/* See declaration. */
c906108c 2030
330cdd98 2031void
5989a64e
SM
2032dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
2033 const dwarf2_debug_sections &names)
c906108c 2034{
fd361982 2035 flagword aflag = bfd_section_flags (sectp);
251d32d9 2036
dc7650b8
JK
2037 if ((aflag & SEC_HAS_CONTENTS) == 0)
2038 {
2039 }
950b7495
KS
2040 else if (elf_section_data (sectp)->this_hdr.sh_size
2041 > bfd_get_file_size (abfd))
2042 {
2043 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2044 warning (_("Discarding section %s which has a section size (%s"
2045 ") larger than the file size [in module %s]"),
2046 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2047 bfd_get_filename (abfd));
2048 }
fbedd546 2049 else if (names.info.matches (sectp->name))
c906108c 2050 {
330cdd98 2051 this->info.s.section = sectp;
fd361982 2052 this->info.size = bfd_section_size (sectp);
c906108c 2053 }
fbedd546 2054 else if (names.abbrev.matches (sectp->name))
c906108c 2055 {
330cdd98 2056 this->abbrev.s.section = sectp;
fd361982 2057 this->abbrev.size = bfd_section_size (sectp);
c906108c 2058 }
fbedd546 2059 else if (names.line.matches (sectp->name))
c906108c 2060 {
330cdd98 2061 this->line.s.section = sectp;
fd361982 2062 this->line.size = bfd_section_size (sectp);
c906108c 2063 }
fbedd546 2064 else if (names.loc.matches (sectp->name))
c906108c 2065 {
330cdd98 2066 this->loc.s.section = sectp;
fd361982 2067 this->loc.size = bfd_section_size (sectp);
c906108c 2068 }
fbedd546 2069 else if (names.loclists.matches (sectp->name))
43988095 2070 {
330cdd98 2071 this->loclists.s.section = sectp;
fd361982 2072 this->loclists.size = bfd_section_size (sectp);
43988095 2073 }
fbedd546 2074 else if (names.macinfo.matches (sectp->name))
c906108c 2075 {
330cdd98 2076 this->macinfo.s.section = sectp;
fd361982 2077 this->macinfo.size = bfd_section_size (sectp);
c906108c 2078 }
fbedd546 2079 else if (names.macro.matches (sectp->name))
cf2c3c16 2080 {
330cdd98 2081 this->macro.s.section = sectp;
fd361982 2082 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2083 }
fbedd546 2084 else if (names.str.matches (sectp->name))
c906108c 2085 {
330cdd98 2086 this->str.s.section = sectp;
fd361982 2087 this->str.size = bfd_section_size (sectp);
c906108c 2088 }
fbedd546 2089 else if (names.str_offsets.matches (sectp->name))
18a8505e
AT
2090 {
2091 this->str_offsets.s.section = sectp;
2092 this->str_offsets.size = bfd_section_size (sectp);
2093 }
fbedd546 2094 else if (names.line_str.matches (sectp->name))
43988095 2095 {
330cdd98 2096 this->line_str.s.section = sectp;
fd361982 2097 this->line_str.size = bfd_section_size (sectp);
43988095 2098 }
fbedd546 2099 else if (names.addr.matches (sectp->name))
3019eac3 2100 {
330cdd98 2101 this->addr.s.section = sectp;
fd361982 2102 this->addr.size = bfd_section_size (sectp);
3019eac3 2103 }
fbedd546 2104 else if (names.frame.matches (sectp->name))
b6af0555 2105 {
330cdd98 2106 this->frame.s.section = sectp;
fd361982 2107 this->frame.size = bfd_section_size (sectp);
b6af0555 2108 }
fbedd546 2109 else if (names.eh_frame.matches (sectp->name))
b6af0555 2110 {
330cdd98 2111 this->eh_frame.s.section = sectp;
fd361982 2112 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2113 }
fbedd546 2114 else if (names.ranges.matches (sectp->name))
af34e669 2115 {
330cdd98 2116 this->ranges.s.section = sectp;
fd361982 2117 this->ranges.size = bfd_section_size (sectp);
af34e669 2118 }
fbedd546 2119 else if (names.rnglists.matches (sectp->name))
43988095 2120 {
330cdd98 2121 this->rnglists.s.section = sectp;
fd361982 2122 this->rnglists.size = bfd_section_size (sectp);
43988095 2123 }
fbedd546 2124 else if (names.types.matches (sectp->name))
348e048f 2125 {
8b70b953
TT
2126 struct dwarf2_section_info type_section;
2127
2128 memset (&type_section, 0, sizeof (type_section));
049412e3 2129 type_section.s.section = sectp;
fd361982 2130 type_section.size = bfd_section_size (sectp);
8b70b953 2131
fd5866f6 2132 this->types.push_back (type_section);
348e048f 2133 }
fbedd546 2134 else if (names.gdb_index.matches (sectp->name))
9291a0cd 2135 {
330cdd98 2136 this->gdb_index.s.section = sectp;
fd361982 2137 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2138 }
fbedd546 2139 else if (names.debug_names.matches (sectp->name))
927aa2e7
JK
2140 {
2141 this->debug_names.s.section = sectp;
fd361982 2142 this->debug_names.size = bfd_section_size (sectp);
927aa2e7 2143 }
fbedd546 2144 else if (names.debug_aranges.matches (sectp->name))
927aa2e7
JK
2145 {
2146 this->debug_aranges.s.section = sectp;
fd361982 2147 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2148 }
dce234bc 2149
fd361982
AM
2150 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2151 && bfd_section_vma (sectp) == 0)
330cdd98 2152 this->has_section_at_zero = true;
c906108c
SS
2153}
2154
dce234bc 2155/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2156 SECTION_NAME. */
af34e669 2157
dce234bc 2158void
3017a003 2159dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
2160 enum dwarf2_section_enum sect,
2161 asection **sectp, const gdb_byte **bufp,
2162 bfd_size_type *sizep)
dce234bc 2163{
976ca316 2164 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2165 struct dwarf2_section_info *info;
a3b2a86b
TT
2166
2167 /* We may see an objfile without any DWARF, in which case we just
2168 return nothing. */
976ca316 2169 if (per_objfile == NULL)
a3b2a86b
TT
2170 {
2171 *sectp = NULL;
2172 *bufp = NULL;
2173 *sizep = 0;
2174 return;
2175 }
3017a003
TG
2176 switch (sect)
2177 {
2178 case DWARF2_DEBUG_FRAME:
976ca316 2179 info = &per_objfile->per_bfd->frame;
3017a003
TG
2180 break;
2181 case DWARF2_EH_FRAME:
976ca316 2182 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2183 break;
2184 default:
2185 gdb_assert_not_reached ("unexpected section");
2186 }
dce234bc 2187
96b79293 2188 info->read (objfile);
dce234bc 2189
96b79293 2190 *sectp = info->get_bfd_section ();
dce234bc
PP
2191 *bufp = info->buffer;
2192 *sizep = info->size;
2193}
2194
9291a0cd 2195\f
39298a5d 2196/* DWARF quick_symbol_functions support. */
7b9f3c50
DE
2197
2198/* TUs can share .debug_line entries, and there can be a lot more TUs than
2199 unique line tables, so we maintain a separate table of all .debug_line
2200 derived entries to support the sharing.
2201 All the quick functions need is the list of file names. We discard the
2202 line_header when we're done and don't need to record it here. */
2203struct quick_file_names
2204{
094b34ac
DE
2205 /* The data used to construct the hash key. */
2206 struct stmt_list_hash hash;
7b9f3c50
DE
2207
2208 /* The number of entries in file_names, real_names. */
2209 unsigned int num_file_names;
2210
2211 /* The file names from the line table, after being run through
2212 file_full_name. */
2213 const char **file_names;
2214
2215 /* The file names from the line table after being run through
2216 gdb_realpath. These are computed lazily. */
2217 const char **real_names;
2218};
2219
2220/* When using the index (and thus not using psymtabs), each CU has an
2221 object of this type. This is used to hold information needed by
2222 the various "quick" methods. */
2223struct dwarf2_per_cu_quick_data
2224{
2225 /* The file table. This can be NULL if there was no file table
2226 or it's currently not read in.
5989a64e 2227 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2228 struct quick_file_names *file_names;
2229
7b9f3c50
DE
2230 /* A temporary mark bit used when iterating over all CUs in
2231 expand_symtabs_matching. */
2232 unsigned int mark : 1;
2233
2234 /* True if we've tried to read the file table and found there isn't one.
2235 There will be no point in trying to read it again next time. */
2236 unsigned int no_file_data : 1;
2237};
2238
edc02ceb
TT
2239/* A subclass of psymbol_functions that arranges to read the DWARF
2240 partial symbols when needed. */
2241struct lazy_dwarf_reader : public psymbol_functions
2242{
2243 using psymbol_functions::psymbol_functions;
2244
2245 bool can_lazily_read_symbols () override
2246 {
2247 return true;
2248 }
2249
2250 void read_partial_symbols (struct objfile *objfile) override
2251 {
2252 if (dwarf2_has_info (objfile, nullptr))
2253 dwarf2_build_psymtabs (objfile, this);
2254 }
2255};
2256
2257static quick_symbol_functions_up
2258make_lazy_dwarf_reader ()
2259{
2260 return quick_symbol_functions_up (new lazy_dwarf_reader);
2261}
2262
39298a5d
TT
2263struct dwarf2_base_index_functions : public quick_symbol_functions
2264{
2265 bool has_symbols (struct objfile *objfile) override;
2266
2267 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2268
2269 void forget_cached_source_info (struct objfile *objfile) override;
2270
39298a5d
TT
2271 enum language lookup_global_symbol_language (struct objfile *objfile,
2272 const char *name,
2273 domain_enum domain,
2274 bool *symbol_found_p) override
2275 {
2276 *symbol_found_p = false;
2277 return language_unknown;
2278 }
2279
4829711b 2280 void print_stats (struct objfile *objfile, bool print_bcache) override;
39298a5d
TT
2281
2282 void expand_all_symtabs (struct objfile *objfile) override;
2283
39298a5d
TT
2284 struct compunit_symtab *find_pc_sect_compunit_symtab
2285 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2286 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2287
2288 struct compunit_symtab *find_compunit_symtab_by_address
2289 (struct objfile *objfile, CORE_ADDR address) override
2290 {
2291 return nullptr;
2292 }
2293
2294 void map_symbol_filenames (struct objfile *objfile,
f4655dee
TT
2295 gdb::function_view<symbol_filename_ftype> fun,
2296 bool need_fullname) override;
39298a5d
TT
2297};
2298
2299struct dwarf2_gdb_index : public dwarf2_base_index_functions
2300{
39298a5d
TT
2301 void dump (struct objfile *objfile) override;
2302
0b7b2c2a 2303 void expand_matching_symbols
39298a5d
TT
2304 (struct objfile *,
2305 const lookup_name_info &lookup_name,
2306 domain_enum domain,
2307 int global,
39298a5d
TT
2308 symbol_compare_ftype *ordered_compare) override;
2309
df35e626 2310 bool expand_symtabs_matching
39298a5d
TT
2311 (struct objfile *objfile,
2312 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2313 const lookup_name_info *lookup_name,
2314 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2315 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2316 block_search_flags search_flags,
3bfa51a7 2317 domain_enum domain,
39298a5d
TT
2318 enum search_domain kind) override;
2319};
2320
2321struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2322{
39298a5d
TT
2323 void dump (struct objfile *objfile) override;
2324
0b7b2c2a 2325 void expand_matching_symbols
39298a5d
TT
2326 (struct objfile *,
2327 const lookup_name_info &lookup_name,
2328 domain_enum domain,
2329 int global,
39298a5d
TT
2330 symbol_compare_ftype *ordered_compare) override;
2331
df35e626 2332 bool expand_symtabs_matching
39298a5d
TT
2333 (struct objfile *objfile,
2334 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2335 const lookup_name_info *lookup_name,
2336 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2337 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2338 block_search_flags search_flags,
3bfa51a7 2339 domain_enum domain,
39298a5d
TT
2340 enum search_domain kind) override;
2341};
2342
edc02ceb 2343static quick_symbol_functions_up
39298a5d
TT
2344make_dwarf_gdb_index ()
2345{
2346 return quick_symbol_functions_up (new dwarf2_gdb_index);
2347}
2348
edc02ceb 2349static quick_symbol_functions_up
39298a5d
TT
2350make_dwarf_debug_names ()
2351{
2352 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2353}
2354
094b34ac
DE
2355/* Utility hash function for a stmt_list_hash. */
2356
2357static hashval_t
2358hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2359{
2360 hashval_t v = 0;
2361
2362 if (stmt_list_hash->dwo_unit != NULL)
2363 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2364 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2365 return v;
2366}
2367
2368/* Utility equality function for a stmt_list_hash. */
2369
2370static int
2371eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2372 const struct stmt_list_hash *rhs)
2373{
2374 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2375 return 0;
2376 if (lhs->dwo_unit != NULL
2377 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2378 return 0;
2379
9c541725 2380 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2381}
2382
7b9f3c50
DE
2383/* Hash function for a quick_file_names. */
2384
2385static hashval_t
2386hash_file_name_entry (const void *e)
2387{
9a3c8263
SM
2388 const struct quick_file_names *file_data
2389 = (const struct quick_file_names *) e;
7b9f3c50 2390
094b34ac 2391 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2392}
2393
2394/* Equality function for a quick_file_names. */
2395
2396static int
2397eq_file_name_entry (const void *a, const void *b)
2398{
9a3c8263
SM
2399 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2400 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2401
094b34ac 2402 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2403}
2404
2405/* Delete function for a quick_file_names. */
2406
2407static void
2408delete_file_name_entry (void *e)
2409{
9a3c8263 2410 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2411 int i;
2412
2413 for (i = 0; i < file_data->num_file_names; ++i)
2414 {
2415 xfree ((void*) file_data->file_names[i]);
2416 if (file_data->real_names)
2417 xfree ((void*) file_data->real_names[i]);
2418 }
2419
45940949
TT
2420 /* The space for the struct itself lives on the obstack, so we don't
2421 free it here. */
7b9f3c50
DE
2422}
2423
2424/* Create a quick_file_names hash table. */
2425
5895093f 2426static htab_up
7b9f3c50
DE
2427create_quick_file_names_table (unsigned int nr_initial_entries)
2428{
5895093f
TT
2429 return htab_up (htab_create_alloc (nr_initial_entries,
2430 hash_file_name_entry, eq_file_name_entry,
2431 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2432}
9291a0cd 2433
ab432490
SM
2434/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2435 function is unrelated to symtabs, symtab would have to be created afterwards.
2436 You should call age_cached_comp_units after processing the CU. */
918dd910 2437
1b555f17 2438static dwarf2_cu *
ab432490
SM
2439load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2440 bool skip_partial)
918dd910 2441{
3019eac3 2442 if (per_cu->is_debug_types)
ab432490 2443 load_full_type_unit (per_cu, per_objfile);
918dd910 2444 else
4a636814
SM
2445 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2446 skip_partial, language_minimal);
918dd910 2447
7188ed02
SM
2448 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2449 if (cu == nullptr)
1b555f17 2450 return nullptr; /* Dummy CU. */
2dc860c0 2451
7188ed02 2452 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2453
7188ed02 2454 return cu;
918dd910
JK
2455}
2456
1350c3b4 2457/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2458
9291a0cd 2459static void
97a1449a 2460dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2461 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2462{
f4dc4d17
DE
2463 /* Skip type_unit_groups, reading the type units they contain
2464 is handled elsewhere. */
197400e8 2465 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2466 return;
2467
08ac5771
SM
2468 {
2469 /* The destructor of dwarf2_queue_guard frees any entries left on
2470 the queue. After this point we're guaranteed to leave this function
2471 with the dwarf queue empty. */
2472 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2473
08ac5771
SM
2474 if (!per_objfile->symtab_set_p (per_cu))
2475 {
2476 queue_comp_unit (per_cu, per_objfile, language_minimal);
2477 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2478
2479 /* If we just loaded a CU from a DWO, and we're working with an index
2480 that may badly handle TUs, load all the TUs in that DWO as well.
2481 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2482 if (!per_cu->is_debug_types
2483 && cu != NULL
2484 && cu->dwo_unit != NULL
2485 && per_objfile->per_bfd->index_table != NULL
2486 && per_objfile->per_bfd->index_table->version <= 7
2487 /* DWP files aren't supported yet. */
2488 && get_dwp_file (per_objfile) == NULL)
2489 queue_and_load_all_dwo_tus (cu);
2490 }
9291a0cd 2491
08ac5771
SM
2492 process_queue (per_objfile);
2493 }
9291a0cd
TT
2494
2495 /* Age the cache, releasing compilation units that have not
2496 been used recently. */
976ca316 2497 per_objfile->age_comp_units ();
9291a0cd
TT
2498}
2499
97a1449a
SM
2500/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2501 the per-objfile for which this symtab is instantiated.
2502
2503 Returns the resulting symbol table. */
2fdf6df6 2504
43f3e411 2505static struct compunit_symtab *
97a1449a 2506dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2507 dwarf2_per_objfile *per_objfile,
97a1449a 2508 bool skip_partial)
9291a0cd 2509{
976ca316 2510 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2511
976ca316 2512 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2513 {
976ca316 2514 free_cached_comp_units freer (per_objfile);
c83dd867 2515 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2516 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2517 process_cu_includes (per_objfile);
9291a0cd 2518 }
f194fefb 2519
976ca316 2520 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2521}
2522
d3473f0c
TT
2523/* See read.h. */
2524
0d305d5c 2525std::unique_ptr<dwarf2_per_cu_data>
5989a64e 2526dwarf2_per_bfd::allocate_per_cu ()
d3473f0c 2527{
0d305d5c 2528 std::unique_ptr<dwarf2_per_cu_data> result (new dwarf2_per_cu_data);
1859c670 2529 result->per_bfd = this;
d3473f0c
TT
2530 result->index = m_num_psymtabs++;
2531 return result;
2532}
2533
2534/* See read.h. */
2535
0d305d5c 2536std::unique_ptr<signatured_type>
5989a64e 2537dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c 2538{
0d305d5c
TT
2539 std::unique_ptr<signatured_type> result (new signatured_type);
2540 result->per_bfd = this;
2541 result->index = m_num_psymtabs++;
91eea9cc 2542 tu_stats.nr_tus++;
d3473f0c
TT
2543 return result;
2544}
2545
168c9250 2546/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2547 obstack, and constructed with the specified field values. */
4b514bc8 2548
0d305d5c 2549static std::unique_ptr<dwarf2_per_cu_data>
168c9250
SM
2550create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2551 struct dwarf2_section_info *section,
2552 int is_dwz,
2553 sect_offset sect_off, ULONGEST length)
4b514bc8 2554{
0d305d5c 2555 std::unique_ptr<dwarf2_per_cu_data> the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2556 the_cu->sect_off = sect_off;
2557 the_cu->length = length;
4b514bc8 2558 the_cu->section = section;
168c9250 2559 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2560 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2561 the_cu->is_dwz = is_dwz;
2562 return the_cu;
2563}
2564
2ec9a5e0
TT
2565/* A helper for create_cus_from_index that handles a given list of
2566 CUs. */
2fdf6df6 2567
74a0d9f6 2568static void
168c9250 2569create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2570 const gdb_byte *cu_list, offset_type n_elements,
2571 struct dwarf2_section_info *section,
b76e467d 2572 int is_dwz)
9291a0cd 2573{
12359b5e 2574 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2575 {
74a0d9f6 2576 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2577
2578 sect_offset sect_off
2579 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2580 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2581 cu_list += 2 * 8;
2582
0d305d5c 2583 std::unique_ptr<dwarf2_per_cu_data> per_cu
168c9250
SM
2584 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2585 length);
0d305d5c 2586 per_bfd->all_comp_units.push_back (std::move (per_cu));
9291a0cd 2587 }
9291a0cd
TT
2588}
2589
2ec9a5e0 2590/* Read the CU list from the mapped index, and use it to create all
168c9250 2591 the CU objects for PER_BFD. */
2ec9a5e0 2592
74a0d9f6 2593static void
168c9250 2594create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2595 const gdb_byte *cu_list, offset_type cu_list_elements,
2596 const gdb_byte *dwz_list, offset_type dwz_elements)
2597{
168c9250
SM
2598 gdb_assert (per_bfd->all_comp_units.empty ());
2599 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2600
168c9250
SM
2601 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2602 &per_bfd->info, 0);
2ec9a5e0
TT
2603
2604 if (dwz_elements == 0)
74a0d9f6 2605 return;
2ec9a5e0 2606
168c9250
SM
2607 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2608 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2609 &dwz->info, 1);
2ec9a5e0
TT
2610}
2611
1fd400ff 2612/* Create the signatured type hash table from the index. */
673bfd45 2613
74a0d9f6 2614static void
12359b5e 2615create_signatured_type_table_from_index
168c9250
SM
2616 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2617 const gdb_byte *bytes, offset_type elements)
1fd400ff 2618{
298e9637 2619 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2620
12359b5e 2621 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2622 {
0d305d5c 2623 std::unique_ptr<signatured_type> sig_type;
9c541725 2624 ULONGEST signature;
1fd400ff 2625 void **slot;
9c541725 2626 cu_offset type_offset_in_tu;
1fd400ff 2627
74a0d9f6 2628 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2629 sect_offset sect_off
2630 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2631 type_offset_in_tu
2632 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2633 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2634 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2635 bytes += 3 * 8;
2636
168c9250 2637 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2638 sig_type->signature = signature;
9c541725 2639 sig_type->type_offset_in_tu = type_offset_in_tu;
0d305d5c
TT
2640 sig_type->is_debug_types = 1;
2641 sig_type->section = section;
2642 sig_type->sect_off = sect_off;
2643 sig_type->v.quick
168c9250 2644 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2645 struct dwarf2_per_cu_quick_data);
2646
0d305d5c
TT
2647 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2648 *slot = sig_type.get ();
1fd400ff 2649
91eea9cc 2650 per_bfd->all_comp_units.push_back (std::move (sig_type));
1fd400ff
TT
2651 }
2652
168c9250 2653 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2654}
2655
927aa2e7
JK
2656/* Create the signatured type hash table from .debug_names. */
2657
2658static void
2659create_signatured_type_table_from_debug_names
976ca316 2660 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2661 const mapped_debug_names &map,
2662 struct dwarf2_section_info *section,
2663 struct dwarf2_section_info *abbrev_section)
2664{
976ca316 2665 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2666
96b79293
TT
2667 section->read (objfile);
2668 abbrev_section->read (objfile);
927aa2e7 2669
298e9637 2670 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2671
2672 for (uint32_t i = 0; i < map.tu_count; ++i)
2673 {
0d305d5c 2674 std::unique_ptr<signatured_type> sig_type;
927aa2e7 2675 void **slot;
927aa2e7
JK
2676
2677 sect_offset sect_off
2678 = (sect_offset) (extract_unsigned_integer
2679 (map.tu_table_reordered + i * map.offset_size,
2680 map.offset_size,
2681 map.dwarf5_byte_order));
2682
2683 comp_unit_head cu_header;
976ca316 2684 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2685 abbrev_section,
927aa2e7
JK
2686 section->buffer + to_underlying (sect_off),
2687 rcuh_kind::TYPE);
2688
976ca316 2689 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2690 sig_type->signature = cu_header.signature;
2691 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
0d305d5c
TT
2692 sig_type->is_debug_types = 1;
2693 sig_type->section = section;
2694 sig_type->sect_off = sect_off;
2695 sig_type->v.quick
976ca316 2696 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2697 struct dwarf2_per_cu_quick_data);
2698
0d305d5c
TT
2699 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2700 *slot = sig_type.get ();
927aa2e7 2701
91eea9cc 2702 per_objfile->per_bfd->all_comp_units.push_back (std::move (sig_type));
927aa2e7
JK
2703 }
2704
976ca316 2705 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2706}
2707
9291a0cd 2708/* Read the address map data from the mapped index, and use it to
efd7398e 2709 populate the psymtabs_addrmap. */
2fdf6df6 2710
9291a0cd 2711static void
976ca316 2712create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2713 struct mapped_index *index)
9291a0cd 2714{
976ca316 2715 struct objfile *objfile = per_objfile->objfile;
efd7398e 2716 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
08feed99 2717 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2718 const gdb_byte *iter, *end;
9291a0cd 2719 struct addrmap *mutable_map;
9291a0cd
TT
2720 CORE_ADDR baseaddr;
2721
8268c778
PA
2722 auto_obstack temp_obstack;
2723
9291a0cd
TT
2724 mutable_map = addrmap_create_mutable (&temp_obstack);
2725
f00a2de2
PA
2726 iter = index->address_table.data ();
2727 end = iter + index->address_table.size ();
9291a0cd 2728
b3b3bada 2729 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2730
2731 while (iter < end)
2732 {
2733 ULONGEST hi, lo, cu_index;
2734 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2735 iter += 8;
2736 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2737 iter += 8;
2738 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2739 iter += 4;
f652bce2 2740
24a55014 2741 if (lo > hi)
f652bce2 2742 {
b98664d3 2743 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2744 hex_string (lo), hex_string (hi));
24a55014 2745 continue;
f652bce2 2746 }
24a55014 2747
efd7398e 2748 if (cu_index >= per_bfd->all_comp_units.size ())
f652bce2 2749 {
b98664d3 2750 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2751 (unsigned) cu_index);
24a55014 2752 continue;
f652bce2 2753 }
24a55014 2754
79748972
TT
2755 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2756 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2757 addrmap_set_empty (mutable_map, lo, hi - 1,
efd7398e 2758 per_bfd->get_cu (cu_index));
9291a0cd
TT
2759 }
2760
efd7398e
TT
2761 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2762 &per_bfd->obstack);
9291a0cd
TT
2763}
2764
927aa2e7 2765/* Read the address map data from DWARF-5 .debug_aranges, and use it to
efd7398e 2766 populate the psymtabs_addrmap. */
927aa2e7
JK
2767
2768static void
976ca316 2769create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2770 struct dwarf2_section_info *section)
2771{
976ca316 2772 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2773 bfd *abfd = objfile->obfd;
08feed99 2774 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2775 const CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e 2776 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
2777
2778 auto_obstack temp_obstack;
2779 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2780
2781 std::unordered_map<sect_offset,
2782 dwarf2_per_cu_data *,
2783 gdb::hash_enum<sect_offset>>
2784 debug_info_offset_to_per_cu;
0d305d5c 2785 for (const auto &per_cu : per_bfd->all_comp_units)
927aa2e7 2786 {
927aa2e7 2787 const auto insertpair
0d305d5c
TT
2788 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2789 per_cu.get ());
927aa2e7
JK
2790 if (!insertpair.second)
2791 {
2792 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2793 "debug_info_offset %s, ignoring .debug_aranges."),
2794 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2795 return;
2796 }
2797 }
2798
96b79293 2799 section->read (objfile);
927aa2e7
JK
2800
2801 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2802
2803 const gdb_byte *addr = section->buffer;
2804
2805 while (addr < section->buffer + section->size)
2806 {
2807 const gdb_byte *const entry_addr = addr;
2808 unsigned int bytes_read;
2809
2810 const LONGEST entry_length = read_initial_length (abfd, addr,
2811 &bytes_read);
2812 addr += bytes_read;
2813
2814 const gdb_byte *const entry_end = addr + entry_length;
2815 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2816 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2817 if (addr + entry_length > section->buffer + section->size)
2818 {
47e3f474 2819 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2820 "length %s exceeds section length %s, "
927aa2e7 2821 "ignoring .debug_aranges."),
47e3f474
TV
2822 objfile_name (objfile),
2823 plongest (entry_addr - section->buffer),
927aa2e7
JK
2824 plongest (bytes_read + entry_length),
2825 pulongest (section->size));
2826 return;
2827 }
2828
2829 /* The version number. */
2830 const uint16_t version = read_2_bytes (abfd, addr);
2831 addr += 2;
2832 if (version != 2)
2833 {
47e3f474 2834 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2835 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2836 objfile_name (objfile),
2837 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2838 return;
2839 }
2840
2841 const uint64_t debug_info_offset
2842 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2843 addr += offset_size;
2844 const auto per_cu_it
2845 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2846 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2847 {
47e3f474 2848 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2849 "debug_info_offset %s does not exists, "
2850 "ignoring .debug_aranges."),
47e3f474
TV
2851 objfile_name (objfile),
2852 plongest (entry_addr - section->buffer),
927aa2e7
JK
2853 pulongest (debug_info_offset));
2854 return;
2855 }
2856 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2857
2858 const uint8_t address_size = *addr++;
2859 if (address_size < 1 || address_size > 8)
2860 {
47e3f474 2861 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2862 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2863 objfile_name (objfile),
2864 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2865 return;
2866 }
2867
2868 const uint8_t segment_selector_size = *addr++;
2869 if (segment_selector_size != 0)
2870 {
47e3f474 2871 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2872 "segment_selector_size %u is not supported, "
2873 "ignoring .debug_aranges."),
47e3f474
TV
2874 objfile_name (objfile),
2875 plongest (entry_addr - section->buffer),
927aa2e7
JK
2876 segment_selector_size);
2877 return;
2878 }
2879
2880 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2881 size. It is undocumented by the DWARF standard but GCC does
2882 use it. */
927aa2e7
JK
2883 for (size_t padding = ((-(addr - section->buffer))
2884 & (2 * address_size - 1));
dda83cd7 2885 padding > 0; padding--)
927aa2e7
JK
2886 if (*addr++ != 0)
2887 {
47e3f474 2888 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2889 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2890 objfile_name (objfile),
2891 plongest (entry_addr - section->buffer));
927aa2e7
JK
2892 return;
2893 }
2894
2895 for (;;)
2896 {
2897 if (addr + 2 * address_size > entry_end)
2898 {
47e3f474 2899 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2900 "address list is not properly terminated, "
2901 "ignoring .debug_aranges."),
47e3f474
TV
2902 objfile_name (objfile),
2903 plongest (entry_addr - section->buffer));
927aa2e7
JK
2904 return;
2905 }
2906 ULONGEST start = extract_unsigned_integer (addr, address_size,
2907 dwarf5_byte_order);
2908 addr += address_size;
2909 ULONGEST length = extract_unsigned_integer (addr, address_size,
2910 dwarf5_byte_order);
2911 addr += address_size;
2912 if (start == 0 && length == 0)
2913 break;
efd7398e 2914 if (start == 0 && !per_bfd->has_section_at_zero)
927aa2e7
JK
2915 {
2916 /* Symbol was eliminated due to a COMDAT group. */
2917 continue;
2918 }
2919 ULONGEST end = start + length;
79748972
TT
2920 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2921 - baseaddr);
2922 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2923 - baseaddr);
927aa2e7
JK
2924 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2925 }
2926 }
2927
efd7398e
TT
2928 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2929 &per_bfd->obstack);
927aa2e7
JK
2930}
2931
4485a1c1
SM
2932/* A helper function that reads the .gdb_index from BUFFER and fills
2933 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2934 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2935 ok to use deprecated sections.
2936
2937 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2938 out parameters that are filled in with information about the CU and
2939 TU lists in the section.
2940
4485a1c1 2941 Returns true if all went well, false otherwise. */
2fdf6df6 2942
d33bc52e 2943static bool
3810f182 2944read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2945 bool deprecated_ok,
2946 gdb::array_view<const gdb_byte> buffer,
2947 struct mapped_index *map,
2948 const gdb_byte **cu_list,
2949 offset_type *cu_list_elements,
2950 const gdb_byte **types_list,
2951 offset_type *types_list_elements)
2952{
2953 const gdb_byte *addr = &buffer[0];
42c2c694 2954 offset_view metadata (buffer);
82430852 2955
9291a0cd 2956 /* Version check. */
42c2c694 2957 offset_type version = metadata[0];
987d643c 2958 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2959 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2960 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2961 indices. */
831adc1f 2962 if (version < 4)
481860b3
GB
2963 {
2964 static int warning_printed = 0;
2965 if (!warning_printed)
2966 {
2967 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2968 filename);
481860b3
GB
2969 warning_printed = 1;
2970 }
2971 return 0;
2972 }
2973 /* Index version 4 uses a different hash function than index version
2974 5 and later.
2975
2976 Versions earlier than 6 did not emit psymbols for inlined
2977 functions. Using these files will cause GDB not to be able to
2978 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2979 indices unless the user has done
2980 "set use-deprecated-index-sections on". */
2ec9a5e0 2981 if (version < 6 && !deprecated_ok)
481860b3
GB
2982 {
2983 static int warning_printed = 0;
2984 if (!warning_printed)
2985 {
e615022a
DE
2986 warning (_("\
2987Skipping deprecated .gdb_index section in %s.\n\
2988Do \"set use-deprecated-index-sections on\" before the file is read\n\
2989to use the section anyway."),
2ec9a5e0 2990 filename);
481860b3
GB
2991 warning_printed = 1;
2992 }
2993 return 0;
2994 }
796a7ff8 2995 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2996 of the TU (for symbols coming from TUs),
2997 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2998 Plus gold-generated indices can have duplicate entries for global symbols,
2999 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3000 These are just performance bugs, and we can't distinguish gdb-generated
3001 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3002
481860b3 3003 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3004 longer backward compatible. */
796a7ff8 3005 if (version > 8)
594e8718 3006 return 0;
9291a0cd 3007
559a7a62 3008 map->version = version;
9291a0cd 3009
42c2c694
TT
3010 int i = 1;
3011 *cu_list = addr + metadata[i];
3012 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
1fd400ff
TT
3013 ++i;
3014
42c2c694
TT
3015 *types_list = addr + metadata[i];
3016 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
987d643c 3017 ++i;
1fd400ff 3018
42c2c694
TT
3019 const gdb_byte *address_table = addr + metadata[i];
3020 const gdb_byte *address_table_end = addr + metadata[i + 1];
f00a2de2
PA
3021 map->address_table
3022 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3023 ++i;
3024
42c2c694
TT
3025 const gdb_byte *symbol_table = addr + metadata[i];
3026 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
f00a2de2 3027 map->symbol_table
42c2c694
TT
3028 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
3029 symbol_table_end));
9291a0cd 3030
f00a2de2 3031 ++i;
42c2c694 3032 map->constant_pool = buffer.slice (metadata[i]);
1fd400ff 3033
2ec9a5e0
TT
3034 return 1;
3035}
3036
4485a1c1
SM
3037/* Callback types for dwarf2_read_gdb_index. */
3038
3039typedef gdb::function_view
5989a64e 3040 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3041 get_gdb_index_contents_ftype;
3042typedef gdb::function_view
3043 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3044 get_gdb_index_contents_dwz_ftype;
3045
927aa2e7 3046/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3047 elements of all the CUs and return 1. Otherwise, return 0. */
3048
3049static int
4485a1c1 3050dwarf2_read_gdb_index
976ca316 3051 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3052 get_gdb_index_contents_ftype get_gdb_index_contents,
3053 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3054{
2ec9a5e0
TT
3055 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3056 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3057 struct dwz_file *dwz;
976ca316 3058 struct objfile *objfile = per_objfile->objfile;
f8c41851 3059 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3060
4485a1c1 3061 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3062 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3063
3064 if (main_index_contents.empty ())
3065 return 0;
3066
3063847f 3067 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3068 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3069 use_deprecated_index_sections,
3070 main_index_contents, map.get (), &cu_list,
3071 &cu_list_elements, &types_list,
3072 &types_list_elements))
2ec9a5e0
TT
3073 return 0;
3074
0fefef59 3075 /* Don't use the index if it's empty. */
3063847f 3076 if (map->symbol_table.empty ())
0fefef59
DE
3077 return 0;
3078
2ec9a5e0
TT
3079 /* If there is a .dwz file, read it so we can get its CU list as
3080 well. */
f8c41851 3081 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3082 if (dwz != NULL)
2ec9a5e0 3083 {
2ec9a5e0
TT
3084 struct mapped_index dwz_map;
3085 const gdb_byte *dwz_types_ignore;
3086 offset_type dwz_types_elements_ignore;
3087
4485a1c1
SM
3088 gdb::array_view<const gdb_byte> dwz_index_content
3089 = get_gdb_index_contents_dwz (objfile, dwz);
3090
3091 if (dwz_index_content.empty ())
3092 return 0;
3093
3810f182 3094 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3095 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3096 &dwz_list, &dwz_list_elements,
3097 &dwz_types_ignore,
3098 &dwz_types_elements_ignore))
2ec9a5e0
TT
3099 {
3100 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3101 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3102 return 0;
3103 }
3104 }
3105
f8c41851
SM
3106 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3107 dwz_list_elements);
1fd400ff 3108
8b70b953
TT
3109 if (types_list_elements)
3110 {
8b70b953
TT
3111 /* We can only handle a single .debug_types when we have an
3112 index. */
f8c41851 3113 if (per_bfd->types.size () != 1)
8b70b953
TT
3114 return 0;
3115
f8c41851 3116 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3117
f8c41851 3118 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3119 types_list_elements);
8b70b953 3120 }
9291a0cd 3121
976ca316 3122 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3123
f8c41851
SM
3124 per_bfd->index_table = std::move (map);
3125 per_bfd->using_index = 1;
3126 per_bfd->quick_file_names_table =
3127 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3128
9291a0cd
TT
3129 return 1;
3130}
3131
dee91e82 3132/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3133
dee91e82
DE
3134static void
3135dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 3136 struct die_info *comp_unit_die)
9291a0cd 3137{
dee91e82 3138 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3139 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3140 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3141 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3142 struct attribute *attr;
7b9f3c50
DE
3143 void **slot;
3144 struct quick_file_names *qfn;
9291a0cd 3145
0186c6a7
DE
3146 gdb_assert (! this_cu->is_debug_types);
3147
07261596
TT
3148 /* Our callers never want to match partial units -- instead they
3149 will match the enclosing full CU. */
3150 if (comp_unit_die->tag == DW_TAG_partial_unit)
3151 {
3152 this_cu->v.quick->no_file_data = 1;
3153 return;
3154 }
3155
0186c6a7 3156 lh_cu = this_cu;
7b9f3c50 3157 slot = NULL;
dee91e82 3158
fff8551c 3159 line_header_up lh;
9c541725 3160 sect_offset line_offset {};
fff8551c 3161
dee91e82 3162 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3163 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3164 {
7b9f3c50
DE
3165 struct quick_file_names find_entry;
3166
d4df075e 3167 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3168
3169 /* We may have already read in this line header (TU line header sharing).
3170 If we have we're done. */
094b34ac 3171 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3172 find_entry.hash.line_sect_off = line_offset;
976ca316 3173 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3174 &find_entry, INSERT);
3175 if (*slot != NULL)
3176 {
9a3c8263 3177 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3178 return;
7b9f3c50
DE
3179 }
3180
3019eac3 3181 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3182 }
3183 if (lh == NULL)
3184 {
094b34ac 3185 lh_cu->v.quick->no_file_data = 1;
dee91e82 3186 return;
9291a0cd
TT
3187 }
3188
976ca316 3189 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3190 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3191 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3192 gdb_assert (slot != NULL);
3193 *slot = qfn;
9291a0cd 3194
d721ba37 3195 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3196
aa391654
TT
3197 int offset = 0;
3198 if (strcmp (fnd.name, "<unknown>") != 0)
3199 ++offset;
3200
7ba99d21 3201 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3202 qfn->file_names =
976ca316 3203 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3204 qfn->num_file_names);
aa391654
TT
3205 if (offset != 0)
3206 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3207 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3208 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3209 fnd.comp_dir).release ();
7b9f3c50 3210 qfn->real_names = NULL;
9291a0cd 3211
094b34ac 3212 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3213}
3214
3215/* A helper for the "quick" functions which attempts to read the line
3216 table for THIS_CU. */
3217
3218static struct quick_file_names *
ab432490
SM
3219dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3220 dwarf2_per_objfile *per_objfile)
dee91e82 3221{
0186c6a7
DE
3222 /* This should never be called for TUs. */
3223 gdb_assert (! this_cu->is_debug_types);
3224 /* Nor type unit groups. */
197400e8 3225 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3226
dee91e82
DE
3227 if (this_cu->v.quick->file_names != NULL)
3228 return this_cu->v.quick->file_names;
3229 /* If we know there is no line data, no point in looking again. */
3230 if (this_cu->v.quick->no_file_data)
3231 return NULL;
3232
ab432490 3233 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3234 if (!reader.dummy_p)
eb5dd737 3235 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
3236
3237 if (this_cu->v.quick->no_file_data)
3238 return NULL;
3239 return this_cu->v.quick->file_names;
9291a0cd
TT
3240}
3241
3242/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3243 real path for a given file name from the line table. */
2fdf6df6 3244
9291a0cd 3245static const char *
976ca316 3246dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3247 struct quick_file_names *qfn, int index)
9291a0cd 3248{
7b9f3c50 3249 if (qfn->real_names == NULL)
976ca316 3250 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3251 qfn->num_file_names, const char *);
9291a0cd 3252
7b9f3c50 3253 if (qfn->real_names[index] == NULL)
14278e1f 3254 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3255
7b9f3c50 3256 return qfn->real_names[index];
9291a0cd
TT
3257}
3258
39298a5d
TT
3259struct symtab *
3260dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
9291a0cd 3261{
976ca316 3262 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
0d305d5c
TT
3263 dwarf2_per_cu_data *dwarf_cu
3264 = per_objfile->per_bfd->all_comp_units.back ().get ();
976ca316 3265 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3266
43f3e411
DE
3267 if (cust == NULL)
3268 return NULL;
ed2dc618 3269
43f3e411 3270 return compunit_primary_filetab (cust);
9291a0cd
TT
3271}
3272
7b9f3c50
DE
3273/* Traversal function for dw2_forget_cached_source_info. */
3274
3275static int
3276dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3277{
7b9f3c50 3278 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3279
7b9f3c50 3280 if (file_data->real_names)
9291a0cd 3281 {
7b9f3c50 3282 int i;
9291a0cd 3283
7b9f3c50 3284 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3285 {
7b9f3c50
DE
3286 xfree ((void*) file_data->real_names[i]);
3287 file_data->real_names[i] = NULL;
9291a0cd
TT
3288 }
3289 }
7b9f3c50
DE
3290
3291 return 1;
3292}
3293
39298a5d
TT
3294void
3295dwarf2_base_index_functions::forget_cached_source_info
3296 (struct objfile *objfile)
7b9f3c50 3297{
976ca316 3298 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3299
976ca316 3300 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3301 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3302}
3303
da51c347
DE
3304/* Struct used to manage iterating over all CUs looking for a symbol. */
3305
3306struct dw2_symtab_iterator
9291a0cd 3307{
ed2dc618 3308 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3309 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3310 /* If set, only look for symbols that match that block. Valid values are
3311 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3312 gdb::optional<block_enum> block_index;
da51c347
DE
3313 /* The kind of symbol we're looking for. */
3314 domain_enum domain;
3315 /* The list of CUs from the index entry of the symbol,
3316 or NULL if not found. */
42c2c694 3317 offset_view vec;
da51c347
DE
3318 /* The next element in VEC to look at. */
3319 int next;
3320 /* The number of elements in VEC, or zero if there is no match. */
3321 int length;
8943b874
DE
3322 /* Have we seen a global version of the symbol?
3323 If so we can ignore all further global instances.
3324 This is to work around gold/15646, inefficient gold-generated
3325 indices. */
3326 int global_seen;
da51c347 3327};
9291a0cd 3328
7089bd88 3329/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2fdf6df6 3330
9291a0cd 3331static void
7089bd88
TT
3332dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3333 dwarf2_per_objfile *per_objfile,
3334 gdb::optional<block_enum> block_index,
3335 domain_enum domain, offset_type namei)
da51c347 3336{
976ca316 3337 iter->per_objfile = per_objfile;
da51c347
DE
3338 iter->block_index = block_index;
3339 iter->domain = domain;
3340 iter->next = 0;
8943b874 3341 iter->global_seen = 0;
42c2c694 3342 iter->vec = {};
e5f3ece2 3343 iter->length = 0;
e5f3ece2
TV
3344
3345 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3346 /* index is NULL if OBJF_READNOW. */
3347 if (index == NULL)
3348 return;
3349
3350 gdb_assert (!index->symbol_name_slot_invalid (namei));
42c2c694 3351 offset_type vec_idx = index->symbol_vec_index (namei);
e5f3ece2 3352
42c2c694
TT
3353 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3354 iter->length = iter->vec[0];
da51c347
DE
3355}
3356
3357/* Return the next matching CU or NULL if there are no more. */
3358
3359static struct dwarf2_per_cu_data *
3360dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3361{
976ca316 3362 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3363
da51c347
DE
3364 for ( ; iter->next < iter->length; ++iter->next)
3365 {
42c2c694 3366 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
da51c347 3367 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3368 gdb_index_symbol_kind symbol_kind =
3369 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3370 /* Only check the symbol attributes if they're present.
3371 Indices prior to version 7 don't record them,
3372 and indices >= 7 may elide them for certain symbols
3373 (gold does this). */
3374 int attrs_valid =
976ca316 3375 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3376 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3377
3190f0c6 3378 /* Don't crash on bad data. */
91eea9cc 3379 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3190f0c6 3380 {
b98664d3 3381 complaint (_(".gdb_index entry has bad CU index"
976ca316 3382 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3383 continue;
3384 }
3385
91eea9cc 3386 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3190f0c6 3387
da51c347 3388 /* Skip if already read in. */
976ca316 3389 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3390 continue;
3391
8943b874
DE
3392 /* Check static vs global. */
3393 if (attrs_valid)
3394 {
2b79f376
SM
3395 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3396
3397 if (iter->block_index.has_value ())
3398 {
3399 bool want_static = *iter->block_index == STATIC_BLOCK;
3400
3401 if (is_static != want_static)
3402 continue;
3403 }
3404
8943b874 3405 /* Work around gold/15646. */
f030440d
TV
3406 if (!is_static
3407 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3408 {
3409 if (iter->global_seen)
3410 continue;
3411
3412 iter->global_seen = 1;
3413 }
8943b874 3414 }
da51c347
DE
3415
3416 /* Only check the symbol's kind if it has one. */
3417 if (attrs_valid)
3418 {
3419 switch (iter->domain)
3420 {
3421 case VAR_DOMAIN:
3422 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3423 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3424 /* Some types are also in VAR_DOMAIN. */
3425 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3426 continue;
3427 break;
3428 case STRUCT_DOMAIN:
3429 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3430 continue;
3431 break;
3432 case LABEL_DOMAIN:
3433 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3434 continue;
3435 break;
59c35742
AB
3436 case MODULE_DOMAIN:
3437 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3438 continue;
3439 break;
da51c347
DE
3440 default:
3441 break;
3442 }
3443 }
3444
3445 ++iter->next;
3446 return per_cu;
3447 }
3448
3449 return NULL;
3450}
3451
39298a5d 3452void
4829711b
TT
3453dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3454 bool print_bcache)
9291a0cd 3455{
4829711b
TT
3456 if (print_bcache)
3457 return;
3458
976ca316 3459 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
91eea9cc 3460 int total = per_objfile->per_bfd->all_comp_units.size ();
ed2dc618 3461 int count = 0;
9291a0cd 3462
ed2dc618 3463 for (int i = 0; i < total; ++i)
9291a0cd 3464 {
91eea9cc 3465 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
9291a0cd 3466
976ca316 3467 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3468 ++count;
3469 }
e4a48d9d 3470 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3471 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3472}
3473
779bd270
DE
3474/* This dumps minimal information about the index.
3475 It is called via "mt print objfiles".
3476 One use is to verify .gdb_index has been loaded by the
3477 gdb.dwarf2/gdb-index.exp testcase. */
3478
39298a5d
TT
3479void
3480dwarf2_gdb_index::dump (struct objfile *objfile)
9291a0cd 3481{
976ca316 3482 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3483
976ca316 3484 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3485 printf_filtered (".gdb_index:");
976ca316 3486 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3487 {
3488 printf_filtered (" version %d\n",
976ca316 3489 per_objfile->per_bfd->index_table->version);
779bd270
DE
3490 }
3491 else
3492 printf_filtered (" faked for \"readnow\"\n");
3493 printf_filtered ("\n");
9291a0cd
TT
3494}
3495
39298a5d
TT
3496void
3497dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
9291a0cd 3498{
976ca316 3499 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
91eea9cc 3500 int total_units = per_objfile->per_bfd->all_comp_units.size ();
9291a0cd 3501
ed2dc618 3502 for (int i = 0; i < total_units; ++i)
9291a0cd 3503 {
91eea9cc 3504 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
9291a0cd 3505
58f0c718
TT
3506 /* We don't want to directly expand a partial CU, because if we
3507 read it with the wrong language, then assertion failures can
3508 be triggered later on. See PR symtab/23010. So, tell
3509 dw2_instantiate_symtab to skip partial CUs -- any important
3510 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3511 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3512 }
3513}
3514
df35e626 3515static bool
9a0bacfb
TV
3516dw2_expand_symtabs_matching_symbol
3517 (mapped_index_base &index,
3518 const lookup_name_info &lookup_name_in,
3519 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3520 gdb::function_view<bool (offset_type)> match_callback,
3521 dwarf2_per_objfile *per_objfile);
9a0bacfb 3522
df35e626 3523static bool
9a0bacfb 3524dw2_expand_symtabs_matching_one
97a1449a
SM
3525 (dwarf2_per_cu_data *per_cu,
3526 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3527 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3528 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3529
af82f89d 3530void
0b7b2c2a 3531dwarf2_gdb_index::expand_matching_symbols
199b4314 3532 (struct objfile *objfile,
b054970d 3533 const lookup_name_info &name, domain_enum domain,
199b4314 3534 int global,
199b4314 3535 symbol_compare_ftype *ordered_compare)
9291a0cd 3536{
1aa98955 3537 /* Used for Ada. */
976ca316 3538 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3539
9a0bacfb
TV
3540 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3541
976ca316 3542 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3543 {
976ca316 3544 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3545
9a0bacfb
TV
3546 const char *match_name = name.ada ().lookup_name ().c_str ();
3547 auto matcher = [&] (const char *symname)
3548 {
3549 if (ordered_compare == nullptr)
3550 return true;
3551 return ordered_compare (symname, match_name) == 0;
3552 };
3553
b64f703b 3554 dw2_expand_symtabs_matching_symbol (index, name, matcher,
9a0bacfb
TV
3555 [&] (offset_type namei)
3556 {
3557 struct dw2_symtab_iterator iter;
3558 struct dwarf2_per_cu_data *per_cu;
3559
976ca316 3560 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3561 namei);
9a0bacfb 3562 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3563 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3564 nullptr);
9a0bacfb 3565 return true;
976ca316 3566 }, per_objfile);
9a0bacfb
TV
3567 }
3568 else
3569 {
3570 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3571 proceed assuming all symtabs have been read in. */
3572 }
9291a0cd
TT
3573}
3574
e1ef7d7a
PA
3575/* Starting from a search name, return the string that finds the upper
3576 bound of all strings that start with SEARCH_NAME in a sorted name
3577 list. Returns the empty string to indicate that the upper bound is
3578 the end of the list. */
3579
3580static std::string
3581make_sort_after_prefix_name (const char *search_name)
3582{
3583 /* When looking to complete "func", we find the upper bound of all
3584 symbols that start with "func" by looking for where we'd insert
3585 the closest string that would follow "func" in lexicographical
3586 order. Usually, that's "func"-with-last-character-incremented,
3587 i.e. "fund". Mind non-ASCII characters, though. Usually those
3588 will be UTF-8 multi-byte sequences, but we can't be certain.
3589 Especially mind the 0xff character, which is a valid character in
3590 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3591 rule out compilers allowing it in identifiers. Note that
3592 conveniently, strcmp/strcasecmp are specified to compare
3593 characters interpreted as unsigned char. So what we do is treat
3594 the whole string as a base 256 number composed of a sequence of
3595 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3596 to 0, and carries 1 to the following more-significant position.
3597 If the very first character in SEARCH_NAME ends up incremented
3598 and carries/overflows, then the upper bound is the end of the
3599 list. The string after the empty string is also the empty
3600 string.
3601
3602 Some examples of this operation:
3603
3604 SEARCH_NAME => "+1" RESULT
3605
3606 "abc" => "abd"
3607 "ab\xff" => "ac"
3608 "\xff" "a" "\xff" => "\xff" "b"
3609 "\xff" => ""
3610 "\xff\xff" => ""
3611 "" => ""
3612
3613 Then, with these symbols for example:
3614
3615 func
3616 func1
3617 fund
3618
3619 completing "func" looks for symbols between "func" and
3620 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3621 which finds "func" and "func1", but not "fund".
3622
3623 And with:
3624
3625 funcÿ (Latin1 'ÿ' [0xff])
3626 funcÿ1
3627 fund
3628
3629 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3630 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3631
3632 And with:
3633
3634 ÿÿ (Latin1 'ÿ' [0xff])
3635 ÿÿ1
3636
3637 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3638 the end of the list.
3639 */
3640 std::string after = search_name;
3641 while (!after.empty () && (unsigned char) after.back () == 0xff)
3642 after.pop_back ();
3643 if (!after.empty ())
3644 after.back () = (unsigned char) after.back () + 1;
3645 return after;
3646}
3647
5c58de74 3648/* See declaration. */
61d96d7e 3649
5c58de74
PA
3650std::pair<std::vector<name_component>::const_iterator,
3651 std::vector<name_component>::const_iterator>
44ed8f3e 3652mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3653 (const lookup_name_info &lookup_name_without_params, language lang,
3654 dwarf2_per_objfile *per_objfile) const
3f563c84 3655{
5c58de74
PA
3656 auto *name_cmp
3657 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3658
3b00ef10 3659 const char *lang_name
e0802d59 3660 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3661
3f563c84
PA
3662 /* Comparison function object for lower_bound that matches against a
3663 given symbol name. */
3664 auto lookup_compare_lower = [&] (const name_component &elem,
3665 const char *name)
3666 {
fcf23d5b 3667 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3668 const char *elem_name = elem_qualified + elem.name_offset;
3669 return name_cmp (elem_name, name) < 0;
3670 };
3671
3672 /* Comparison function object for upper_bound that matches against a
3673 given symbol name. */
3674 auto lookup_compare_upper = [&] (const char *name,
3675 const name_component &elem)
3676 {
fcf23d5b 3677 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3678 const char *elem_name = elem_qualified + elem.name_offset;
3679 return name_cmp (name, elem_name) < 0;
3680 };
3681
5c58de74
PA
3682 auto begin = this->name_components.begin ();
3683 auto end = this->name_components.end ();
3f563c84
PA
3684
3685 /* Find the lower bound. */
3686 auto lower = [&] ()
3687 {
3b00ef10 3688 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3689 return begin;
3690 else
3b00ef10 3691 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3692 } ();
3693
3694 /* Find the upper bound. */
3695 auto upper = [&] ()
3696 {
5c58de74 3697 if (lookup_name_without_params.completion_mode ())
3f563c84 3698 {
e1ef7d7a
PA
3699 /* In completion mode, we want UPPER to point past all
3700 symbols names that have the same prefix. I.e., with
3701 these symbols, and completing "func":
3702
3703 function << lower bound
3704 function1
3705 other_function << upper bound
3706
3707 We find the upper bound by looking for the insertion
3708 point of "func"-with-last-character-incremented,
3709 i.e. "fund". */
3b00ef10 3710 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3711 if (after.empty ())
3f563c84 3712 return end;
e6b2f5ef
PA
3713 return std::lower_bound (lower, end, after.c_str (),
3714 lookup_compare_lower);
3f563c84
PA
3715 }
3716 else
3b00ef10 3717 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3718 } ();
3719
5c58de74
PA
3720 return {lower, upper};
3721}
3722
3723/* See declaration. */
3724
3725void
fcf23d5b 3726mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3727{
3728 if (!this->name_components.empty ())
3729 return;
3730
3731 this->name_components_casing = case_sensitivity;
3732 auto *name_cmp
3733 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3734
3735 /* The code below only knows how to break apart components of C++
3736 symbol names (and other languages that use '::' as
3b00ef10 3737 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3738 auto count = this->symbol_name_count ();
3739 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3740 {
44ed8f3e 3741 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3742 continue;
3743
fcf23d5b 3744 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3745
3746 /* Add each name component to the name component table. */
3747 unsigned int previous_len = 0;
3b00ef10
TT
3748
3749 if (strstr (name, "::") != nullptr)
3750 {
3751 for (unsigned int current_len = cp_find_first_component (name);
3752 name[current_len] != '\0';
3753 current_len += cp_find_first_component (name + current_len))
3754 {
3755 gdb_assert (name[current_len] == ':');
3756 this->name_components.push_back ({previous_len, idx});
3757 /* Skip the '::'. */
3758 current_len += 2;
3759 previous_len = current_len;
3760 }
3761 }
3762 else
5c58de74 3763 {
3b00ef10
TT
3764 /* Handle the Ada encoded (aka mangled) form here. */
3765 for (const char *iter = strstr (name, "__");
3766 iter != nullptr;
3767 iter = strstr (iter, "__"))
3768 {
3769 this->name_components.push_back ({previous_len, idx});
3770 iter += 2;
3771 previous_len = iter - name;
3772 }
5c58de74 3773 }
3b00ef10 3774
5c58de74
PA
3775 this->name_components.push_back ({previous_len, idx});
3776 }
3777
3778 /* Sort name_components elements by name. */
3779 auto name_comp_compare = [&] (const name_component &left,
3780 const name_component &right)
3781 {
fcf23d5b
SM
3782 const char *left_qualified
3783 = this->symbol_name_at (left.idx, per_objfile);
3784 const char *right_qualified
3785 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
3786
3787 const char *left_name = left_qualified + left.name_offset;
3788 const char *right_name = right_qualified + right.name_offset;
3789
3790 return name_cmp (left_name, right_name) < 0;
3791 };
3792
3793 std::sort (this->name_components.begin (),
3794 this->name_components.end (),
3795 name_comp_compare);
3796}
3797
3798/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3799 mapped_index_base instead of the containing objfile. This is split
3800 to a separate function in order to be able to unit test the
3801 name_components matching using a mock mapped_index_base. For each
5c58de74 3802 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3803 symbol's index in the mapped_index_base symbol table. */
5c58de74 3804
df35e626 3805static bool
5c58de74 3806dw2_expand_symtabs_matching_symbol
44ed8f3e 3807 (mapped_index_base &index,
5c58de74
PA
3808 const lookup_name_info &lookup_name_in,
3809 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3810 gdb::function_view<bool (offset_type)> match_callback,
3811 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3812{
3813 lookup_name_info lookup_name_without_params
3814 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3815
3816 /* Build the symbol name component sorted vector, if we haven't
3817 yet. */
fcf23d5b 3818 index.build_name_components (per_objfile);
5c58de74 3819
3f563c84
PA
3820 /* The same symbol may appear more than once in the range though.
3821 E.g., if we're looking for symbols that complete "w", and we have
3822 a symbol named "w1::w2", we'll find the two name components for
3823 that same symbol in the range. To be sure we only call the
3824 callback once per symbol, we first collect the symbol name
3825 indexes that matched in a temporary vector and ignore
3826 duplicates. */
3827 std::vector<offset_type> matches;
3f563c84 3828
3b00ef10
TT
3829 struct name_and_matcher
3830 {
3831 symbol_name_matcher_ftype *matcher;
ecc6c606 3832 const char *name;
3b00ef10
TT
3833
3834 bool operator== (const name_and_matcher &other) const
3f563c84 3835 {
ecc6c606 3836 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3837 }
3838 };
3839
3840 /* A vector holding all the different symbol name matchers, for all
3841 languages. */
3842 std::vector<name_and_matcher> matchers;
3843
3844 for (int i = 0; i < nr_languages; i++)
3845 {
3846 enum language lang_e = (enum language) i;
3847
3848 const language_defn *lang = language_def (lang_e);
3849 symbol_name_matcher_ftype *name_matcher
c9debfb9 3850 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 3851
3b00ef10 3852 name_and_matcher key {
dda83cd7 3853 name_matcher,
3b00ef10
TT
3854 lookup_name_without_params.language_lookup_name (lang_e)
3855 };
3856
3857 /* Don't insert the same comparison routine more than once.
3858 Note that we do this linear walk. This is not a problem in
3859 practice because the number of supported languages is
3860 low. */
3861 if (std::find (matchers.begin (), matchers.end (), key)
3862 != matchers.end ())
9291a0cd 3863 continue;
3b00ef10
TT
3864 matchers.push_back (std::move (key));
3865
3866 auto bounds
3867 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 3868 lang_e, per_objfile);
3b00ef10
TT
3869
3870 /* Now for each symbol name in range, check to see if we have a name
3871 match, and if so, call the MATCH_CALLBACK callback. */
3872
3873 for (; bounds.first != bounds.second; ++bounds.first)
3874 {
fcf23d5b
SM
3875 const char *qualified
3876 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
3877
3878 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3879 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3880 continue;
9291a0cd 3881
3b00ef10
TT
3882 matches.push_back (bounds.first->idx);
3883 }
3f563c84
PA
3884 }
3885
3886 std::sort (matches.begin (), matches.end ());
3887
3888 /* Finally call the callback, once per match. */
3889 ULONGEST prev = -1;
df35e626 3890 bool result = true;
3f563c84
PA
3891 for (offset_type idx : matches)
3892 {
3893 if (prev != idx)
3894 {
3b00ef10 3895 if (!match_callback (idx))
df35e626
TT
3896 {
3897 result = false;
3898 break;
3899 }
3f563c84
PA
3900 prev = idx;
3901 }
3902 }
3903
3904 /* Above we use a type wider than idx's for 'prev', since 0 and
3905 (offset_type)-1 are both possible values. */
3906 static_assert (sizeof (prev) > sizeof (offset_type), "");
df35e626
TT
3907
3908 return result;
3f563c84
PA
3909}
3910
c62446b1
PA
3911#if GDB_SELF_TEST
3912
3913namespace selftests { namespace dw2_expand_symtabs_matching {
3914
a3c5fafd
PA
3915/* A mock .gdb_index/.debug_names-like name index table, enough to
3916 exercise dw2_expand_symtabs_matching_symbol, which works with the
3917 mapped_index_base interface. Builds an index from the symbol list
3918 passed as parameter to the constructor. */
3919class mock_mapped_index : public mapped_index_base
c62446b1
PA
3920{
3921public:
a3c5fafd
PA
3922 mock_mapped_index (gdb::array_view<const char *> symbols)
3923 : m_symbol_table (symbols)
c62446b1
PA
3924 {}
3925
a3c5fafd 3926 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 3927
a3c5fafd 3928 /* Return the number of names in the symbol table. */
632e107b 3929 size_t symbol_name_count () const override
c62446b1 3930 {
a3c5fafd 3931 return m_symbol_table.size ();
c62446b1
PA
3932 }
3933
a3c5fafd 3934 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
3935 const char *symbol_name_at
3936 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
3937 {
3938 return m_symbol_table[idx];
3939 }
c62446b1 3940
a3c5fafd
PA
3941private:
3942 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
3943};
3944
3945/* Convenience function that converts a NULL pointer to a "<null>"
3946 string, to pass to print routines. */
3947
3948static const char *
3949string_or_null (const char *str)
3950{
3951 return str != NULL ? str : "<null>";
3952}
3953
3954/* Check if a lookup_name_info built from
3955 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3956 index. EXPECTED_LIST is the list of expected matches, in expected
3957 matching order. If no match expected, then an empty list is
3958 specified. Returns true on success. On failure prints a warning
3959 indicating the file:line that failed, and returns false. */
3960
3961static bool
3962check_match (const char *file, int line,
3963 mock_mapped_index &mock_index,
3964 const char *name, symbol_name_match_type match_type,
3965 bool completion_mode,
fcf23d5b
SM
3966 std::initializer_list<const char *> expected_list,
3967 dwarf2_per_objfile *per_objfile)
c62446b1
PA
3968{
3969 lookup_name_info lookup_name (name, match_type, completion_mode);
3970
3971 bool matched = true;
3972
3973 auto mismatch = [&] (const char *expected_str,
3974 const char *got)
3975 {
3976 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3977 "expected=\"%s\", got=\"%s\"\n"),
3978 file, line,
3979 (match_type == symbol_name_match_type::FULL
3980 ? "FULL" : "WILD"),
3981 name, string_or_null (expected_str), string_or_null (got));
3982 matched = false;
3983 };
3984
3985 auto expected_it = expected_list.begin ();
3986 auto expected_end = expected_list.end ();
3987
a3c5fafd 3988 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
b64f703b 3989 nullptr,
c62446b1
PA
3990 [&] (offset_type idx)
3991 {
fcf23d5b 3992 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
3993 const char *expected_str
3994 = expected_it == expected_end ? NULL : *expected_it++;
3995
3996 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3997 mismatch (expected_str, matched_name);
3b00ef10 3998 return true;
fcf23d5b 3999 }, per_objfile);
c62446b1
PA
4000
4001 const char *expected_str
4002 = expected_it == expected_end ? NULL : *expected_it++;
4003 if (expected_str != NULL)
4004 mismatch (expected_str, NULL);
4005
4006 return matched;
4007}
4008
4009/* The symbols added to the mock mapped_index for testing (in
4010 canonical form). */
4011static const char *test_symbols[] = {
4012 "function",
4013 "std::bar",
4014 "std::zfunction",
4015 "std::zfunction2",
4016 "w1::w2",
4017 "ns::foo<char*>",
4018 "ns::foo<int>",
4019 "ns::foo<long>",
a20714ff
PA
4020 "ns2::tmpl<int>::foo2",
4021 "(anonymous namespace)::A::B::C",
c62446b1 4022
e1ef7d7a
PA
4023 /* These are used to check that the increment-last-char in the
4024 matching algorithm for completion doesn't match "t1_fund" when
4025 completing "t1_func". */
4026 "t1_func",
4027 "t1_func1",
4028 "t1_fund",
4029 "t1_fund1",
4030
4031 /* A UTF-8 name with multi-byte sequences to make sure that
4032 cp-name-parser understands this as a single identifier ("função"
4033 is "function" in PT). */
4034 u8"u8função",
4035
4036 /* \377 (0xff) is Latin1 'ÿ'. */
4037 "yfunc\377",
4038
4039 /* \377 (0xff) is Latin1 'ÿ'. */
4040 "\377",
4041 "\377\377123",
4042
c62446b1
PA
4043 /* A name with all sorts of complications. Starts with "z" to make
4044 it easier for the completion tests below. */
4045#define Z_SYM_NAME \
4046 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4047 "::tuple<(anonymous namespace)::ui*, " \
4048 "std::default_delete<(anonymous namespace)::ui>, void>"
4049
4050 Z_SYM_NAME
4051};
4052
a3c5fafd
PA
4053/* Returns true if the mapped_index_base::find_name_component_bounds
4054 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4055 in completion mode. */
5c58de74
PA
4056
4057static bool
a3c5fafd 4058check_find_bounds_finds (mapped_index_base &index,
5c58de74 4059 const char *search_name,
fcf23d5b
SM
4060 gdb::array_view<const char *> expected_syms,
4061 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4062{
4063 lookup_name_info lookup_name (search_name,
4064 symbol_name_match_type::FULL, true);
4065
3b00ef10 4066 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4067 language_cplus,
4068 per_objfile);
5c58de74
PA
4069
4070 size_t distance = std::distance (bounds.first, bounds.second);
4071 if (distance != expected_syms.size ())
4072 return false;
4073
4074 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4075 {
4076 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4077 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4078 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4079 return false;
4080 }
4081
4082 return true;
4083}
4084
4085/* Test the lower-level mapped_index::find_name_component_bounds
4086 method. */
4087
c62446b1 4088static void
5c58de74
PA
4089test_mapped_index_find_name_component_bounds ()
4090{
4091 mock_mapped_index mock_index (test_symbols);
4092
fcf23d5b 4093 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4094
4095 /* Test the lower-level mapped_index::find_name_component_bounds
4096 method in completion mode. */
4097 {
4098 static const char *expected_syms[] = {
4099 "t1_func",
4100 "t1_func1",
5c58de74
PA
4101 };
4102
fcf23d5b
SM
4103 SELF_CHECK (check_find_bounds_finds
4104 (mock_index, "t1_func", expected_syms,
4105 NULL /* per_objfile */));
5c58de74
PA
4106 }
4107
4108 /* Check that the increment-last-char in the name matching algorithm
4109 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4110 {
4111 static const char *expected_syms1[] = {
4112 "\377",
4113 "\377\377123",
4114 };
fcf23d5b
SM
4115 SELF_CHECK (check_find_bounds_finds
4116 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4117
4118 static const char *expected_syms2[] = {
4119 "\377\377123",
4120 };
fcf23d5b
SM
4121 SELF_CHECK (check_find_bounds_finds
4122 (mock_index, "\377\377", expected_syms2,
4123 NULL /* per_objfile */));
5c58de74
PA
4124 }
4125}
4126
4127/* Test dw2_expand_symtabs_matching_symbol. */
4128
4129static void
4130test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4131{
4132 mock_mapped_index mock_index (test_symbols);
4133
4134 /* We let all tests run until the end even if some fails, for debug
4135 convenience. */
4136 bool any_mismatch = false;
4137
4138 /* Create the expected symbols list (an initializer_list). Needed
4139 because lists have commas, and we need to pass them to CHECK,
4140 which is a macro. */
4141#define EXPECT(...) { __VA_ARGS__ }
4142
4143 /* Wrapper for check_match that passes down the current
4144 __FILE__/__LINE__. */
4145#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4146 any_mismatch |= !check_match (__FILE__, __LINE__, \
4147 mock_index, \
4148 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4149 EXPECTED_LIST, NULL)
c62446b1
PA
4150
4151 /* Identity checks. */
4152 for (const char *sym : test_symbols)
4153 {
4154 /* Should be able to match all existing symbols. */
4155 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4156 EXPECT (sym));
4157
4158 /* Should be able to match all existing symbols with
4159 parameters. */
4160 std::string with_params = std::string (sym) + "(int)";
4161 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4162 EXPECT (sym));
4163
4164 /* Should be able to match all existing symbols with
4165 parameters and qualifiers. */
4166 with_params = std::string (sym) + " ( int ) const";
4167 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4168 EXPECT (sym));
4169
4170 /* This should really find sym, but cp-name-parser.y doesn't
4171 know about lvalue/rvalue qualifiers yet. */
4172 with_params = std::string (sym) + " ( int ) &&";
4173 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4174 {});
4175 }
4176
e1ef7d7a
PA
4177 /* Check that the name matching algorithm for completion doesn't get
4178 confused with Latin1 'ÿ' / 0xff. */
4179 {
4180 static const char str[] = "\377";
4181 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4182 EXPECT ("\377", "\377\377123"));
4183 }
4184
4185 /* Check that the increment-last-char in the matching algorithm for
4186 completion doesn't match "t1_fund" when completing "t1_func". */
4187 {
4188 static const char str[] = "t1_func";
4189 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4190 EXPECT ("t1_func", "t1_func1"));
4191 }
4192
c62446b1
PA
4193 /* Check that completion mode works at each prefix of the expected
4194 symbol name. */
4195 {
4196 static const char str[] = "function(int)";
4197 size_t len = strlen (str);
4198 std::string lookup;
4199
4200 for (size_t i = 1; i < len; i++)
4201 {
4202 lookup.assign (str, i);
4203 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4204 EXPECT ("function"));
4205 }
4206 }
4207
4208 /* While "w" is a prefix of both components, the match function
4209 should still only be called once. */
4210 {
4211 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4212 EXPECT ("w1::w2"));
a20714ff
PA
4213 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4214 EXPECT ("w1::w2"));
c62446b1
PA
4215 }
4216
4217 /* Same, with a "complicated" symbol. */
4218 {
4219 static const char str[] = Z_SYM_NAME;
4220 size_t len = strlen (str);
4221 std::string lookup;
4222
4223 for (size_t i = 1; i < len; i++)
4224 {
4225 lookup.assign (str, i);
4226 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4227 EXPECT (Z_SYM_NAME));
4228 }
4229 }
4230
4231 /* In FULL mode, an incomplete symbol doesn't match. */
4232 {
4233 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4234 {});
4235 }
4236
4237 /* A complete symbol with parameters matches any overload, since the
4238 index has no overload info. */
4239 {
4240 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4241 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4242 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4243 EXPECT ("std::zfunction", "std::zfunction2"));
4244 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4245 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4246 }
4247
4248 /* Check that whitespace is ignored appropriately. A symbol with a
4249 template argument list. */
4250 {
4251 static const char expected[] = "ns::foo<int>";
4252 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4253 EXPECT (expected));
a20714ff
PA
4254 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4255 EXPECT (expected));
c62446b1
PA
4256 }
4257
4258 /* Check that whitespace is ignored appropriately. A symbol with a
4259 template argument list that includes a pointer. */
4260 {
4261 static const char expected[] = "ns::foo<char*>";
4262 /* Try both completion and non-completion modes. */
4263 static const bool completion_mode[2] = {false, true};
4264 for (size_t i = 0; i < 2; i++)
4265 {
4266 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4267 completion_mode[i], EXPECT (expected));
a20714ff
PA
4268 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4269 completion_mode[i], EXPECT (expected));
c62446b1
PA
4270
4271 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4272 completion_mode[i], EXPECT (expected));
a20714ff
PA
4273 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4274 completion_mode[i], EXPECT (expected));
c62446b1
PA
4275 }
4276 }
4277
4278 {
4279 /* Check method qualifiers are ignored. */
4280 static const char expected[] = "ns::foo<char*>";
4281 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4282 symbol_name_match_type::FULL, true, EXPECT (expected));
4283 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4284 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4285 CHECK_MATCH ("foo < char * > ( int ) const",
4286 symbol_name_match_type::WILD, true, EXPECT (expected));
4287 CHECK_MATCH ("foo < char * > ( int ) &&",
4288 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4289 }
4290
4291 /* Test lookup names that don't match anything. */
4292 {
a20714ff
PA
4293 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4294 {});
4295
c62446b1
PA
4296 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4297 {});
4298 }
4299
a20714ff
PA
4300 /* Some wild matching tests, exercising "(anonymous namespace)",
4301 which should not be confused with a parameter list. */
4302 {
4303 static const char *syms[] = {
4304 "A::B::C",
4305 "B::C",
4306 "C",
4307 "A :: B :: C ( int )",
4308 "B :: C ( int )",
4309 "C ( int )",
4310 };
4311
4312 for (const char *s : syms)
4313 {
4314 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4315 EXPECT ("(anonymous namespace)::A::B::C"));
4316 }
4317 }
4318
4319 {
4320 static const char expected[] = "ns2::tmpl<int>::foo2";
4321 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4322 EXPECT (expected));
4323 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4324 EXPECT (expected));
4325 }
4326
c62446b1
PA
4327 SELF_CHECK (!any_mismatch);
4328
4329#undef EXPECT
4330#undef CHECK_MATCH
4331}
4332
5c58de74
PA
4333static void
4334run_test ()
4335{
4336 test_mapped_index_find_name_component_bounds ();
4337 test_dw2_expand_symtabs_matching_symbol ();
4338}
4339
c62446b1
PA
4340}} // namespace selftests::dw2_expand_symtabs_matching
4341
4342#endif /* GDB_SELF_TEST */
4343
4b514bc8
JK
4344/* If FILE_MATCHER is NULL or if PER_CU has
4345 dwarf2_per_cu_quick_data::MARK set (see
4346 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4347 EXPANSION_NOTIFY on it. */
4348
df35e626 4349static bool
4b514bc8 4350dw2_expand_symtabs_matching_one
97a1449a
SM
4351 (dwarf2_per_cu_data *per_cu,
4352 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4353 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4354 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4355{
4356 if (file_matcher == NULL || per_cu->v.quick->mark)
4357 {
af758d11 4358 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4359
97a1449a
SM
4360 compunit_symtab *symtab
4361 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4362 gdb_assert (symtab != nullptr);
4b514bc8 4363
af758d11 4364 if (expansion_notify != NULL && symtab_was_null)
df35e626 4365 return expansion_notify (symtab);
4b514bc8 4366 }
df35e626 4367 return true;
4b514bc8
JK
4368}
4369
3f563c84
PA
4370/* Helper for dw2_expand_matching symtabs. Called on each symbol
4371 matched, to expand corresponding CUs that were marked. IDX is the
4372 index of the symbol name that matched. */
4373
df35e626 4374static bool
3f563c84 4375dw2_expand_marked_cus
976ca316 4376 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4377 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4378 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4379 block_search_flags search_flags,
3f563c84
PA
4380 search_domain kind)
4381{
42c2c694 4382 offset_type vec_len, vec_idx;
3f563c84 4383 bool global_seen = false;
976ca316 4384 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4385
42c2c694
TT
4386 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4387 vec_len = vec[0];
61920122
PA
4388 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4389 {
42c2c694 4390 offset_type cu_index_and_attrs = vec[vec_idx + 1];
61920122
PA
4391 /* This value is only valid for index versions >= 7. */
4392 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4393 gdb_index_symbol_kind symbol_kind =
4394 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4395 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4396 /* Only check the symbol attributes if they're present.
4397 Indices prior to version 7 don't record them,
4398 and indices >= 7 may elide them for certain symbols
4399 (gold does this). */
4400 int attrs_valid =
4401 (index.version >= 7
4402 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4403
4404 /* Work around gold/15646. */
f030440d
TV
4405 if (attrs_valid
4406 && !is_static
4407 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4408 {
f030440d 4409 if (global_seen)
61920122 4410 continue;
f030440d
TV
4411
4412 global_seen = true;
61920122 4413 }
3190f0c6 4414
61920122
PA
4415 /* Only check the symbol's kind if it has one. */
4416 if (attrs_valid)
4417 {
03a8ea51
TT
4418 if (is_static)
4419 {
4420 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4421 continue;
4422 }
4423 else
4424 {
4425 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4426 continue;
4427 }
4428
61920122 4429 switch (kind)
8943b874 4430 {
61920122
PA
4431 case VARIABLES_DOMAIN:
4432 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4433 continue;
4434 break;
4435 case FUNCTIONS_DOMAIN:
4436 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4437 continue;
61920122
PA
4438 break;
4439 case TYPES_DOMAIN:
4440 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4441 continue;
4442 break;
59c35742
AB
4443 case MODULES_DOMAIN:
4444 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4445 continue;
4446 break;
61920122
PA
4447 default:
4448 break;
8943b874 4449 }
61920122 4450 }
8943b874 4451
61920122 4452 /* Don't crash on bad data. */
91eea9cc 4453 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
61920122 4454 {
b98664d3 4455 complaint (_(".gdb_index entry has bad CU index"
976ca316 4456 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4457 continue;
4458 }
4459
91eea9cc 4460 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
df35e626
TT
4461 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4462 expansion_notify))
4463 return false;
61920122 4464 }
df35e626
TT
4465
4466 return true;
61920122
PA
4467}
4468
4b514bc8
JK
4469/* If FILE_MATCHER is non-NULL, set all the
4470 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4471 that match FILE_MATCHER. */
4472
61920122 4473static void
4b514bc8 4474dw_expand_symtabs_matching_file_matcher
976ca316 4475 (dwarf2_per_objfile *per_objfile,
ed2dc618 4476 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4477{
4b514bc8 4478 if (file_matcher == NULL)
61920122
PA
4479 return;
4480
4b514bc8
JK
4481 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4482 htab_eq_pointer,
4483 NULL, xcalloc, xfree));
4484 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4485 htab_eq_pointer,
4486 NULL, xcalloc, xfree));
61920122 4487
4b514bc8
JK
4488 /* The rule is CUs specify all the files, including those used by
4489 any TU, so there's no need to scan TUs here. */
61920122 4490
0d305d5c 4491 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4492 {
927aa2e7
JK
4493 QUIT;
4494
91eea9cc
TT
4495 if (per_cu->is_debug_types)
4496 continue;
927aa2e7
JK
4497 per_cu->v.quick->mark = 0;
4498
4499 /* We only need to look at symtabs not already expanded. */
0d305d5c 4500 if (per_objfile->symtab_set_p (per_cu.get ()))
927aa2e7
JK
4501 continue;
4502
0d305d5c
TT
4503 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4504 per_objfile);
927aa2e7
JK
4505 if (file_data == NULL)
4506 continue;
4507
4508 if (htab_find (visited_not_found.get (), file_data) != NULL)
4509 continue;
4510 else if (htab_find (visited_found.get (), file_data) != NULL)
4511 {
4512 per_cu->v.quick->mark = 1;
4513 continue;
4514 }
4515
b76e467d 4516 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4517 {
4518 const char *this_real_name;
4519
4520 if (file_matcher (file_data->file_names[j], false))
4521 {
4522 per_cu->v.quick->mark = 1;
4523 break;
4524 }
4525
4526 /* Before we invoke realpath, which can get expensive when many
4527 files are involved, do a quick comparison of the basenames. */
4528 if (!basenames_may_differ
4529 && !file_matcher (lbasename (file_data->file_names[j]),
4530 true))
4531 continue;
4532
976ca316 4533 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4534 if (file_matcher (this_real_name, false))
4535 {
4536 per_cu->v.quick->mark = 1;
4537 break;
4538 }
4539 }
4540
b76e467d
SM
4541 void **slot = htab_find_slot (per_cu->v.quick->mark
4542 ? visited_found.get ()
4543 : visited_not_found.get (),
4544 file_data, INSERT);
927aa2e7
JK
4545 *slot = file_data;
4546 }
4547}
4548
df35e626 4549bool
af82f89d
TT
4550dwarf2_gdb_index::expand_symtabs_matching
4551 (struct objfile *objfile,
4552 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4553 const lookup_name_info *lookup_name,
4554 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4555 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4556 block_search_flags search_flags,
3bfa51a7 4557 domain_enum domain,
af82f89d 4558 enum search_domain kind)
927aa2e7 4559{
976ca316 4560 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4561
4562 /* index_table is NULL if OBJF_READNOW. */
976ca316 4563 if (!per_objfile->per_bfd->index_table)
df35e626 4564 return true;
927aa2e7 4565
976ca316 4566 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4567
c1a66c06
TV
4568 if (symbol_matcher == NULL && lookup_name == NULL)
4569 {
0d305d5c 4570 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4571 {
4572 QUIT;
4573
0d305d5c 4574 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
df35e626
TT
4575 file_matcher,
4576 expansion_notify))
4577 return false;
c1a66c06 4578 }
df35e626 4579 return true;
c1a66c06
TV
4580 }
4581
976ca316 4582 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4583
df35e626
TT
4584 bool result
4585 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4586 symbol_matcher,
4587 [&] (offset_type idx)
927aa2e7 4588 {
df35e626 4589 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
03a8ea51 4590 expansion_notify, search_flags, kind))
df35e626 4591 return false;
3b00ef10 4592 return true;
976ca316 4593 }, per_objfile);
df35e626
TT
4594
4595 return result;
927aa2e7
JK
4596}
4597
4598/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4599 symtab. */
4600
4601static struct compunit_symtab *
4602recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4603 CORE_ADDR pc)
4604{
4605 int i;
4606
4607 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4608 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4609 return cust;
4610
4611 if (cust->includes == NULL)
4612 return NULL;
4613
4614 for (i = 0; cust->includes[i]; ++i)
4615 {
4616 struct compunit_symtab *s = cust->includes[i];
4617
4618 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4619 if (s != NULL)
4620 return s;
4621 }
4622
4623 return NULL;
4624}
4625
39298a5d
TT
4626struct compunit_symtab *
4627dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4628 (struct objfile *objfile,
4629 struct bound_minimal_symbol msymbol,
4630 CORE_ADDR pc,
4631 struct obj_section *section,
4632 int warn_if_readin)
927aa2e7
JK
4633{
4634 struct dwarf2_per_cu_data *data;
4635 struct compunit_symtab *result;
4636
efd7398e
TT
4637 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4638 if (per_objfile->per_bfd->index_addrmap == nullptr)
927aa2e7
JK
4639 return NULL;
4640
b3b3bada 4641 CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e
TT
4642 data = ((struct dwarf2_per_cu_data *)
4643 addrmap_find (per_objfile->per_bfd->index_addrmap,
4644 pc - baseaddr));
927aa2e7
JK
4645 if (!data)
4646 return NULL;
4647
af758d11 4648 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4649 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4650 paddress (objfile->arch (), pc));
927aa2e7 4651
97a1449a
SM
4652 result = recursively_find_pc_sect_compunit_symtab
4653 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4654
927aa2e7
JK
4655 gdb_assert (result != NULL);
4656 return result;
4657}
4658
39298a5d 4659void
f4655dee
TT
4660dwarf2_base_index_functions::map_symbol_filenames
4661 (struct objfile *objfile,
4662 gdb::function_view<symbol_filename_ftype> fun,
4663 bool need_fullname)
927aa2e7 4664{
976ca316 4665 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4666
9fc29955
AB
4667 /* Use caches to ensure we only call FUN once for each filename. */
4668 filename_seen_cache filenames_cache;
4669 std::unordered_set<quick_file_names *> qfn_cache;
927aa2e7 4670
9fc29955
AB
4671 /* The rule is CUs specify all the files, including those used by any TU,
4672 so there's no need to scan TUs here. We can ignore file names coming
4673 from already-expanded CUs. It is possible that an expanded CU might
4674 reuse the file names data from a currently unexpanded CU, in this
4675 case we don't want to report the files from the unexpanded CU. */
927aa2e7 4676
0d305d5c 4677 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
9fc29955 4678 {
0d305d5c 4679 if (per_objfile->symtab_set_p (per_cu.get ()))
927aa2e7 4680 {
9fc29955
AB
4681 if (per_cu->v.quick->file_names != nullptr)
4682 qfn_cache.insert (per_cu->v.quick->file_names);
927aa2e7 4683 }
9fc29955 4684 }
927aa2e7 4685
0d305d5c 4686 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
9fc29955
AB
4687 {
4688 /* We only need to look at symtabs not already expanded. */
0d305d5c 4689 if (per_objfile->symtab_set_p (per_cu.get ()))
9fc29955 4690 continue;
927aa2e7 4691
0d305d5c
TT
4692 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4693 per_objfile);
9fc29955
AB
4694 if (file_data == nullptr
4695 || qfn_cache.find (file_data) != qfn_cache.end ())
4696 continue;
927aa2e7 4697
9fc29955
AB
4698 for (int j = 0; j < file_data->num_file_names; ++j)
4699 {
4700 const char *filename = file_data->file_names[j];
4701 filenames_cache.seen (filename);
927aa2e7
JK
4702 }
4703 }
4704
9fc29955 4705 filenames_cache.traverse ([&] (const char *filename)
927aa2e7
JK
4706 {
4707 gdb::unique_xmalloc_ptr<char> this_real_name;
4708
4709 if (need_fullname)
4710 this_real_name = gdb_realpath (filename);
f4655dee 4711 fun (filename, this_real_name.get ());
927aa2e7
JK
4712 });
4713}
4714
39298a5d
TT
4715bool
4716dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
927aa2e7 4717{
fae2120b 4718 return true;
927aa2e7
JK
4719}
4720
927aa2e7
JK
4721/* DWARF-5 debug_names reader. */
4722
4723/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4724static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4725
4726/* A helper function that reads the .debug_names section in SECTION
4727 and fills in MAP. FILENAME is the name of the file containing the
4728 section; it is used for error reporting.
4729
4730 Returns true if all went well, false otherwise. */
4731
4732static bool
4733read_debug_names_from_section (struct objfile *objfile,
4734 const char *filename,
4735 struct dwarf2_section_info *section,
4736 mapped_debug_names &map)
4737{
96b79293 4738 if (section->empty ())
927aa2e7
JK
4739 return false;
4740
4741 /* Older elfutils strip versions could keep the section in the main
4742 executable while splitting it for the separate debug info file. */
96b79293 4743 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4744 return false;
4745
96b79293 4746 section->read (objfile);
927aa2e7 4747
08feed99 4748 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4749
4750 const gdb_byte *addr = section->buffer;
4751
96b79293 4752 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4753
4754 unsigned int bytes_read;
4755 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4756 addr += bytes_read;
4757
4758 map.dwarf5_is_dwarf64 = bytes_read != 4;
4759 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4760 if (bytes_read + length != section->size)
4761 {
4762 /* There may be multiple per-CU indices. */
4763 warning (_("Section .debug_names in %s length %s does not match "
4764 "section length %s, ignoring .debug_names."),
4765 filename, plongest (bytes_read + length),
4766 pulongest (section->size));
4767 return false;
4768 }
4769
4770 /* The version number. */
4771 uint16_t version = read_2_bytes (abfd, addr);
4772 addr += 2;
4773 if (version != 5)
4774 {
4775 warning (_("Section .debug_names in %s has unsupported version %d, "
4776 "ignoring .debug_names."),
4777 filename, version);
4778 return false;
4779 }
4780
4781 /* Padding. */
4782 uint16_t padding = read_2_bytes (abfd, addr);
4783 addr += 2;
4784 if (padding != 0)
4785 {
4786 warning (_("Section .debug_names in %s has unsupported padding %d, "
4787 "ignoring .debug_names."),
4788 filename, padding);
4789 return false;
4790 }
4791
4792 /* comp_unit_count - The number of CUs in the CU list. */
4793 map.cu_count = read_4_bytes (abfd, addr);
4794 addr += 4;
4795
4796 /* local_type_unit_count - The number of TUs in the local TU
4797 list. */
4798 map.tu_count = read_4_bytes (abfd, addr);
4799 addr += 4;
4800
4801 /* foreign_type_unit_count - The number of TUs in the foreign TU
4802 list. */
4803 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4804 addr += 4;
4805 if (foreign_tu_count != 0)
4806 {
4807 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4808 "ignoring .debug_names."),
4809 filename, static_cast<unsigned long> (foreign_tu_count));
4810 return false;
4811 }
4812
4813 /* bucket_count - The number of hash buckets in the hash lookup
4814 table. */
4815 map.bucket_count = read_4_bytes (abfd, addr);
4816 addr += 4;
4817
4818 /* name_count - The number of unique names in the index. */
4819 map.name_count = read_4_bytes (abfd, addr);
4820 addr += 4;
4821
4822 /* abbrev_table_size - The size in bytes of the abbreviations
4823 table. */
4824 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4825 addr += 4;
4826
4827 /* augmentation_string_size - The size in bytes of the augmentation
4828 string. This value is rounded up to a multiple of 4. */
4829 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4830 addr += 4;
4831 map.augmentation_is_gdb = ((augmentation_string_size
4832 == sizeof (dwarf5_augmentation))
4833 && memcmp (addr, dwarf5_augmentation,
4834 sizeof (dwarf5_augmentation)) == 0);
4835 augmentation_string_size += (-augmentation_string_size) & 3;
4836 addr += augmentation_string_size;
4837
4838 /* List of CUs */
4839 map.cu_table_reordered = addr;
4840 addr += map.cu_count * map.offset_size;
4841
4842 /* List of Local TUs */
4843 map.tu_table_reordered = addr;
4844 addr += map.tu_count * map.offset_size;
4845
4846 /* Hash Lookup Table */
4847 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4848 addr += map.bucket_count * 4;
4849 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4850 addr += map.name_count * 4;
4851
4852 /* Name Table */
4853 map.name_table_string_offs_reordered = addr;
4854 addr += map.name_count * map.offset_size;
4855 map.name_table_entry_offs_reordered = addr;
4856 addr += map.name_count * map.offset_size;
4857
4858 const gdb_byte *abbrev_table_start = addr;
4859 for (;;)
4860 {
927aa2e7
JK
4861 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4862 addr += bytes_read;
4863 if (index_num == 0)
4864 break;
4865
4866 const auto insertpair
4867 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4868 if (!insertpair.second)
4869 {
4870 warning (_("Section .debug_names in %s has duplicate index %s, "
4871 "ignoring .debug_names."),
4872 filename, pulongest (index_num));
4873 return false;
4874 }
4875 mapped_debug_names::index_val &indexval = insertpair.first->second;
4876 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4877 addr += bytes_read;
4878
4879 for (;;)
4880 {
4881 mapped_debug_names::index_val::attr attr;
4882 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4883 addr += bytes_read;
4884 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4885 addr += bytes_read;
4886 if (attr.form == DW_FORM_implicit_const)
4887 {
4888 attr.implicit_const = read_signed_leb128 (abfd, addr,
4889 &bytes_read);
4890 addr += bytes_read;
4891 }
4892 if (attr.dw_idx == 0 && attr.form == 0)
4893 break;
4894 indexval.attr_vec.push_back (std::move (attr));
4895 }
4896 }
4897 if (addr != abbrev_table_start + abbrev_table_size)
4898 {
4899 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4900 "of size %s vs. written as %u, ignoring .debug_names."),
4901 filename, plongest (addr - abbrev_table_start),
4902 abbrev_table_size);
927aa2e7
JK
4903 return false;
4904 }
4905 map.entry_pool = addr;
4906
4907 return true;
4908}
4909
4910/* A helper for create_cus_from_debug_names that handles the MAP's CU
4911 list. */
4912
4913static void
168c9250 4914create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4915 const mapped_debug_names &map,
4916 dwarf2_section_info &section,
b76e467d 4917 bool is_dwz)
927aa2e7 4918{
3ee6bb11
TV
4919 if (!map.augmentation_is_gdb)
4920 {
fc9a13fb
TV
4921 for (uint32_t i = 0; i < map.cu_count; ++i)
4922 {
4923 sect_offset sect_off
4924 = (sect_offset) (extract_unsigned_integer
4925 (map.cu_table_reordered + i * map.offset_size,
4926 map.offset_size,
4927 map.dwarf5_byte_order));
4928 /* We don't know the length of the CU, because the CU list in a
4929 .debug_names index can be incomplete, so we can't use the start
4930 of the next CU as end of this CU. We create the CUs here with
4931 length 0, and in cutu_reader::cutu_reader we'll fill in the
4932 actual length. */
0d305d5c 4933 std::unique_ptr<dwarf2_per_cu_data> per_cu
fc9a13fb
TV
4934 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4935 sect_off, 0);
0d305d5c 4936 per_bfd->all_comp_units.push_back (std::move (per_cu));
fc9a13fb 4937 }
d3b54e63 4938 return;
3ee6bb11
TV
4939 }
4940
927aa2e7
JK
4941 sect_offset sect_off_prev;
4942 for (uint32_t i = 0; i <= map.cu_count; ++i)
4943 {
4944 sect_offset sect_off_next;
4945 if (i < map.cu_count)
4946 {
4947 sect_off_next
4948 = (sect_offset) (extract_unsigned_integer
4949 (map.cu_table_reordered + i * map.offset_size,
4950 map.offset_size,
4951 map.dwarf5_byte_order));
4952 }
4953 else
4954 sect_off_next = (sect_offset) section.size;
4955 if (i >= 1)
4956 {
4957 const ULONGEST length = sect_off_next - sect_off_prev;
0d305d5c 4958 std::unique_ptr<dwarf2_per_cu_data> per_cu
168c9250 4959 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 4960 sect_off_prev, length);
0d305d5c 4961 per_bfd->all_comp_units.push_back (std::move (per_cu));
927aa2e7
JK
4962 }
4963 sect_off_prev = sect_off_next;
4964 }
4965}
4966
4967/* Read the CU list from the mapped index, and use it to create all
ed2dc618 4968 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
4969
4970static void
168c9250 4971create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4972 const mapped_debug_names &map,
4973 const mapped_debug_names &dwz_map)
4974{
168c9250
SM
4975 gdb_assert (per_bfd->all_comp_units.empty ());
4976 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 4977
168c9250 4978 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 4979 false /* is_dwz */);
927aa2e7
JK
4980
4981 if (dwz_map.cu_count == 0)
4982 return;
4983
168c9250
SM
4984 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4985 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 4986 true /* is_dwz */);
927aa2e7
JK
4987}
4988
4989/* Read .debug_names. If everything went ok, initialize the "quick"
4990 elements of all the CUs and return true. Otherwise, return false. */
4991
4992static bool
976ca316 4993dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 4994{
fcf23d5b
SM
4995 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4996 mapped_debug_names dwz_map;
976ca316 4997 struct objfile *objfile = per_objfile->objfile;
f8c41851 4998 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
4999
5000 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
bab287cd 5001 &per_bfd->debug_names, *map))
927aa2e7
JK
5002 return false;
5003
5004 /* Don't use the index if it's empty. */
22ca247e 5005 if (map->name_count == 0)
927aa2e7
JK
5006 return false;
5007
5008 /* If there is a .dwz file, read it so we can get its CU list as
5009 well. */
f8c41851 5010 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5011 if (dwz != NULL)
5012 {
5013 if (!read_debug_names_from_section (objfile,
00f93c44 5014 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5015 &dwz->debug_names, dwz_map))
5016 {
5017 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5018 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5019 return false;
5020 }
5021 }
5022
f8c41851 5023 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5024
22ca247e 5025 if (map->tu_count != 0)
927aa2e7
JK
5026 {
5027 /* We can only handle a single .debug_types when we have an
5028 index. */
f8c41851 5029 if (per_bfd->types.size () != 1)
927aa2e7
JK
5030 return false;
5031
f8c41851 5032 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5033
5034 create_signatured_type_table_from_debug_names
f8c41851 5035 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5036 }
5037
f8c41851 5038 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5039
f8c41851
SM
5040 per_bfd->debug_names_table = std::move (map);
5041 per_bfd->using_index = 1;
5042 per_bfd->quick_file_names_table =
bab287cd 5043 create_quick_file_names_table (per_bfd->all_comp_units.size ());
927aa2e7
JK
5044
5045 return true;
5046}
5047
927aa2e7
JK
5048/* Type used to manage iterating over all CUs looking for a symbol for
5049 .debug_names. */
5050
5051class dw2_debug_names_iterator
5052{
5053public:
927aa2e7 5054 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5055 block_search_flags block_index,
2b79f376 5056 domain_enum domain,
fcf23d5b 5057 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5058 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5059 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5060 m_per_objfile (per_objfile)
927aa2e7
JK
5061 {}
5062
5063 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5064 search_domain search, uint32_t namei,
3bfa51a7
TT
5065 dwarf2_per_objfile *per_objfile,
5066 domain_enum domain = UNDEF_DOMAIN)
927aa2e7 5067 : m_map (map),
3bfa51a7 5068 m_domain (domain),
927aa2e7 5069 m_search (search),
fcf23d5b
SM
5070 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5071 m_per_objfile (per_objfile)
927aa2e7
JK
5072 {}
5073
3b00ef10 5074 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5075 block_search_flags block_index, domain_enum domain,
fcf23d5b 5076 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5077 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5078 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5079 m_per_objfile (per_objfile)
3b00ef10
TT
5080 {}
5081
927aa2e7
JK
5082 /* Return the next matching CU or NULL if there are no more. */
5083 dwarf2_per_cu_data *next ();
5084
5085private:
5086 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5087 const char *name,
5088 dwarf2_per_objfile *per_objfile);
927aa2e7 5089 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5090 uint32_t namei,
5091 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5092
5093 /* The internalized form of .debug_names. */
5094 const mapped_debug_names &m_map;
5095
03a8ea51
TT
5096 /* Restrict the search to these blocks. */
5097 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
5098 | SEARCH_STATIC_BLOCK);
927aa2e7
JK
5099
5100 /* The kind of symbol we're looking for. */
5101 const domain_enum m_domain = UNDEF_DOMAIN;
5102 const search_domain m_search = ALL_DOMAIN;
5103
5104 /* The list of CUs from the index entry of the symbol, or NULL if
5105 not found. */
5106 const gdb_byte *m_addr;
fcf23d5b
SM
5107
5108 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5109};
5110
5111const char *
fcf23d5b 5112mapped_debug_names::namei_to_name
976ca316 5113 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5114{
5115 const ULONGEST namei_string_offs
5116 = extract_unsigned_integer ((name_table_string_offs_reordered
5117 + namei * offset_size),
5118 offset_size,
5119 dwarf5_byte_order);
976ca316 5120 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5121}
5122
5123/* Find a slot in .debug_names for the object named NAME. If NAME is
5124 found, return pointer to its pool data. If NAME cannot be found,
5125 return NULL. */
5126
5127const gdb_byte *
5128dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5129 (const mapped_debug_names &map, const char *name,
5130 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5131{
5132 int (*cmp) (const char *, const char *);
5133
54ee4252 5134 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5135 if (current_language->la_language == language_cplus
5136 || current_language->la_language == language_fortran
5137 || current_language->la_language == language_d)
5138 {
5139 /* NAME is already canonical. Drop any qualifiers as
5140 .debug_names does not contain any. */
5141
5142 if (strchr (name, '(') != NULL)
5143 {
54ee4252 5144 without_params = cp_remove_params (name);
927aa2e7 5145 if (without_params != NULL)
54ee4252 5146 name = without_params.get ();
927aa2e7
JK
5147 }
5148 }
5149
5150 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5151
5152 const uint32_t full_hash = dwarf5_djb_hash (name);
5153 uint32_t namei
5154 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5155 (map.bucket_table_reordered
5156 + (full_hash % map.bucket_count)), 4,
5157 map.dwarf5_byte_order);
5158 if (namei == 0)
5159 return NULL;
5160 --namei;
5161 if (namei >= map.name_count)
5162 {
b98664d3 5163 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5164 "[in module %s]"),
5165 namei, map.name_count,
fcf23d5b 5166 objfile_name (per_objfile->objfile));
927aa2e7
JK
5167 return NULL;
5168 }
5169
5170 for (;;)
5171 {
5172 const uint32_t namei_full_hash
5173 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5174 (map.hash_table_reordered + namei), 4,
5175 map.dwarf5_byte_order);
5176 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5177 return NULL;
5178
5179 if (full_hash == namei_full_hash)
5180 {
fcf23d5b 5181 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5182
5183#if 0 /* An expensive sanity check. */
5184 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5185 {
b98664d3 5186 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5187 "[in module %s]"),
5188 namei, objfile_name (dwarf2_per_objfile->objfile));
5189 return NULL;
5190 }
5191#endif
5192
5193 if (cmp (namei_string, name) == 0)
5194 {
5195 const ULONGEST namei_entry_offs
5196 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5197 + namei * map.offset_size),
5198 map.offset_size, map.dwarf5_byte_order);
5199 return map.entry_pool + namei_entry_offs;
5200 }
5201 }
5202
5203 ++namei;
5204 if (namei >= map.name_count)
5205 return NULL;
5206 }
5207}
5208
5209const gdb_byte *
5210dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5211 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5212{
5213 if (namei >= map.name_count)
5214 {
b98664d3 5215 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5216 "[in module %s]"),
5217 namei, map.name_count,
fcf23d5b 5218 objfile_name (per_objfile->objfile));
927aa2e7
JK
5219 return NULL;
5220 }
5221
5222 const ULONGEST namei_entry_offs
5223 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5224 + namei * map.offset_size),
5225 map.offset_size, map.dwarf5_byte_order);
5226 return map.entry_pool + namei_entry_offs;
5227}
5228
5229/* See dw2_debug_names_iterator. */
5230
5231dwarf2_per_cu_data *
5232dw2_debug_names_iterator::next ()
5233{
5234 if (m_addr == NULL)
5235 return NULL;
5236
fcf23d5b
SM
5237 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5238 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5239 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5240
5241 again:
5242
5243 unsigned int bytes_read;
5244 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5245 m_addr += bytes_read;
5246 if (abbrev == 0)
5247 return NULL;
5248
5249 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5250 if (indexval_it == m_map.abbrev_map.cend ())
5251 {
b98664d3 5252 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5253 "[in module %s]"),
ed2dc618 5254 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5255 return NULL;
5256 }
5257 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5258 enum class symbol_linkage {
5259 unknown,
5260 static_,
5261 extern_,
23c13d42 5262 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5263 dwarf2_per_cu_data *per_cu = NULL;
5264 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5265 {
5266 ULONGEST ull;
5267 switch (attr.form)
5268 {
5269 case DW_FORM_implicit_const:
5270 ull = attr.implicit_const;
5271 break;
5272 case DW_FORM_flag_present:
5273 ull = 1;
5274 break;
5275 case DW_FORM_udata:
5276 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5277 m_addr += bytes_read;
5278 break;
6dc55ce9 5279 case DW_FORM_ref4:
5280 ull = read_4_bytes (abfd, m_addr);
5281 m_addr += 4;
5282 break;
5283 case DW_FORM_ref8:
5284 ull = read_8_bytes (abfd, m_addr);
5285 m_addr += 8;
5286 break;
5287 case DW_FORM_ref_sig8:
5288 ull = read_8_bytes (abfd, m_addr);
5289 m_addr += 8;
5290 break;
927aa2e7 5291 default:
b98664d3 5292 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5293 dwarf_form_name (attr.form),
ed2dc618 5294 objfile_name (objfile));
927aa2e7
JK
5295 return NULL;
5296 }
5297 switch (attr.dw_idx)
5298 {
5299 case DW_IDX_compile_unit:
5300 /* Don't crash on bad data. */
bab287cd 5301 if (ull >= per_bfd->all_comp_units.size ())
927aa2e7 5302 {
b98664d3 5303 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5304 " [in module %s]"),
5305 pulongest (ull),
fcf23d5b 5306 objfile_name (objfile));
927aa2e7
JK
5307 continue;
5308 }
91eea9cc 5309 per_cu = per_bfd->get_cu (ull);
927aa2e7 5310 break;
8af5c486
JK
5311 case DW_IDX_type_unit:
5312 /* Don't crash on bad data. */
91eea9cc 5313 if (ull >= per_bfd->tu_stats.nr_tus)
8af5c486 5314 {
b98664d3 5315 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5316 " [in module %s]"),
5317 pulongest (ull),
fcf23d5b 5318 objfile_name (objfile));
8af5c486
JK
5319 continue;
5320 }
91eea9cc 5321 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
8af5c486 5322 break;
6dc55ce9 5323 case DW_IDX_die_offset:
5324 /* In a per-CU index (as opposed to a per-module index), index
5325 entries without CU attribute implicitly refer to the single CU. */
5326 if (per_cu == NULL)
fcf23d5b 5327 per_cu = per_bfd->get_cu (0);
6dc55ce9 5328 break;
927aa2e7
JK
5329 case DW_IDX_GNU_internal:
5330 if (!m_map.augmentation_is_gdb)
5331 break;
23c13d42 5332 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5333 break;
5334 case DW_IDX_GNU_external:
5335 if (!m_map.augmentation_is_gdb)
5336 break;
23c13d42 5337 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5338 break;
5339 }
5340 }
5341
5342 /* Skip if already read in. */
fcf23d5b 5343 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5344 goto again;
5345
5346 /* Check static vs global. */
03a8ea51 5347 if (symbol_linkage_ != symbol_linkage::unknown)
927aa2e7 5348 {
03a8ea51
TT
5349 if (symbol_linkage_ == symbol_linkage::static_)
5350 {
5351 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5352 goto again;
5353 }
5354 else
5355 {
5356 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5357 goto again;
5358 }
927aa2e7
JK
5359 }
5360
5361 /* Match dw2_symtab_iter_next, symbol_kind
5362 and debug_names::psymbol_tag. */
5363 switch (m_domain)
5364 {
5365 case VAR_DOMAIN:
5366 switch (indexval.dwarf_tag)
5367 {
5368 case DW_TAG_variable:
5369 case DW_TAG_subprogram:
5370 /* Some types are also in VAR_DOMAIN. */
5371 case DW_TAG_typedef:
5372 case DW_TAG_structure_type:
5373 break;
5374 default:
5375 goto again;
5376 }
5377 break;
5378 case STRUCT_DOMAIN:
5379 switch (indexval.dwarf_tag)
5380 {
5381 case DW_TAG_typedef:
5382 case DW_TAG_structure_type:
5383 break;
5384 default:
5385 goto again;
5386 }
5387 break;
5388 case LABEL_DOMAIN:
5389 switch (indexval.dwarf_tag)
5390 {
5391 case 0:
5392 case DW_TAG_variable:
5393 break;
5394 default:
5395 goto again;
5396 }
5397 break;
59c35742
AB
5398 case MODULE_DOMAIN:
5399 switch (indexval.dwarf_tag)
5400 {
5401 case DW_TAG_module:
5402 break;
5403 default:
5404 goto again;
5405 }
5406 break;
927aa2e7
JK
5407 default:
5408 break;
5409 }
5410
5411 /* Match dw2_expand_symtabs_matching, symbol_kind and
5412 debug_names::psymbol_tag. */
5413 switch (m_search)
4b514bc8 5414 {
927aa2e7
JK
5415 case VARIABLES_DOMAIN:
5416 switch (indexval.dwarf_tag)
4b514bc8 5417 {
927aa2e7
JK
5418 case DW_TAG_variable:
5419 break;
5420 default:
5421 goto again;
4b514bc8 5422 }
927aa2e7
JK
5423 break;
5424 case FUNCTIONS_DOMAIN:
5425 switch (indexval.dwarf_tag)
4b514bc8 5426 {
927aa2e7
JK
5427 case DW_TAG_subprogram:
5428 break;
5429 default:
5430 goto again;
4b514bc8 5431 }
927aa2e7
JK
5432 break;
5433 case TYPES_DOMAIN:
5434 switch (indexval.dwarf_tag)
5435 {
5436 case DW_TAG_typedef:
5437 case DW_TAG_structure_type:
5438 break;
5439 default:
5440 goto again;
5441 }
5442 break;
59c35742
AB
5443 case MODULES_DOMAIN:
5444 switch (indexval.dwarf_tag)
5445 {
5446 case DW_TAG_module:
5447 break;
5448 default:
5449 goto again;
5450 }
927aa2e7
JK
5451 default:
5452 break;
4b514bc8 5453 }
927aa2e7
JK
5454
5455 return per_cu;
4b514bc8 5456}
61920122 5457
927aa2e7
JK
5458/* This dumps minimal information about .debug_names. It is called
5459 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5460 uses this to verify that .debug_names has been loaded. */
9291a0cd 5461
39298a5d
TT
5462void
5463dwarf2_debug_names_index::dump (struct objfile *objfile)
927aa2e7 5464{
976ca316 5465 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5466
976ca316 5467 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5468 printf_filtered (".debug_names:");
976ca316 5469 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5470 printf_filtered (" exists\n");
5471 else
5472 printf_filtered (" faked for \"readnow\"\n");
5473 printf_filtered ("\n");
9291a0cd
TT
5474}
5475
39298a5d 5476void
0b7b2c2a 5477dwarf2_debug_names_index::expand_matching_symbols
3b00ef10
TT
5478 (struct objfile *objfile,
5479 const lookup_name_info &name, domain_enum domain,
5480 int global,
3b00ef10
TT
5481 symbol_compare_ftype *ordered_compare)
5482{
976ca316 5483 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5484
5485 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5486 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5487 return;
5488
976ca316 5489 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
03a8ea51
TT
5490 const block_search_flags block_flags
5491 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
3b00ef10
TT
5492
5493 const char *match_name = name.ada ().lookup_name ().c_str ();
5494 auto matcher = [&] (const char *symname)
5495 {
5496 if (ordered_compare == nullptr)
5497 return true;
5498 return ordered_compare (symname, match_name) == 0;
5499 };
5500
b64f703b 5501 dw2_expand_symtabs_matching_symbol (map, name, matcher,
3b00ef10
TT
5502 [&] (offset_type namei)
5503 {
5504 /* The name was matched, now expand corresponding CUs that were
5505 marked. */
03a8ea51 5506 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
976ca316 5507 per_objfile);
3b00ef10
TT
5508
5509 struct dwarf2_per_cu_data *per_cu;
5510 while ((per_cu = iter.next ()) != NULL)
976ca316 5511 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5512 nullptr);
3b00ef10 5513 return true;
976ca316 5514 }, per_objfile);
3b00ef10
TT
5515}
5516
df35e626 5517bool
39298a5d 5518dwarf2_debug_names_index::expand_symtabs_matching
927aa2e7
JK
5519 (struct objfile *objfile,
5520 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5521 const lookup_name_info *lookup_name,
927aa2e7
JK
5522 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5523 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 5524 block_search_flags search_flags,
3bfa51a7 5525 domain_enum domain,
927aa2e7
JK
5526 enum search_domain kind)
5527{
976ca316 5528 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5529
927aa2e7 5530 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5531 if (!per_objfile->per_bfd->debug_names_table)
df35e626 5532 return true;
9291a0cd 5533
976ca316 5534 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5535
c1a66c06
TV
5536 if (symbol_matcher == NULL && lookup_name == NULL)
5537 {
0d305d5c 5538 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5539 {
5540 QUIT;
5541
0d305d5c 5542 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
df35e626
TT
5543 file_matcher,
5544 expansion_notify))
5545 return false;
c1a66c06 5546 }
df35e626 5547 return true;
c1a66c06
TV
5548 }
5549
976ca316 5550 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5551
df35e626
TT
5552 bool result
5553 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5554 symbol_matcher,
5555 [&] (offset_type namei)
927aa2e7 5556 {
927aa2e7
JK
5557 /* The name was matched, now expand corresponding CUs that were
5558 marked. */
3bfa51a7 5559 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
bbf2f4df 5560
927aa2e7
JK
5561 struct dwarf2_per_cu_data *per_cu;
5562 while ((per_cu = iter.next ()) != NULL)
df35e626
TT
5563 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5564 file_matcher,
5565 expansion_notify))
5566 return false;
3b00ef10 5567 return true;
976ca316 5568 }, per_objfile);
df35e626
TT
5569
5570 return result;
9291a0cd
TT
5571}
5572
4485a1c1 5573/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5574 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5575
5576template <typename T>
5577static gdb::array_view<const gdb_byte>
5578get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5579{
5580 dwarf2_section_info *section = &section_owner->gdb_index;
5581
96b79293 5582 if (section->empty ())
4485a1c1
SM
5583 return {};
5584
5585 /* Older elfutils strip versions could keep the section in the main
5586 executable while splitting it for the separate debug info file. */
96b79293 5587 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5588 return {};
5589
96b79293 5590 section->read (obj);
4485a1c1 5591
8bebfcda
PA
5592 /* dwarf2_section_info::size is a bfd_size_type, while
5593 gdb::array_view works with size_t. On 32-bit hosts, with
5594 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5595 is 32-bit. So we need an explicit narrowing conversion here.
5596 This is fine, because it's impossible to allocate or mmap an
5597 array/buffer larger than what size_t can represent. */
5598 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5599}
5600
87d6a7aa
SM
5601/* Lookup the index cache for the contents of the index associated to
5602 DWARF2_OBJ. */
5603
5604static gdb::array_view<const gdb_byte>
5989a64e 5605get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5606{
5607 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5608 if (build_id == nullptr)
5609 return {};
5610
5611 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5612 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5613}
5614
5615/* Same as the above, but for DWZ. */
5616
5617static gdb::array_view<const gdb_byte>
5618get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5619{
5620 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5621 if (build_id == nullptr)
5622 return {};
5623
5624 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5625}
5626
edc02ceb 5627/* See dwarf2/public.h. */
9291a0cd 5628
edc02ceb
TT
5629void
5630dwarf2_initialize_objfile (struct objfile *objfile)
9291a0cd 5631{
976ca316
SM
5632 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5633 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5634
850ed749
SM
5635 dwarf_read_debug_printf ("called");
5636
9291a0cd
TT
5637 /* If we're about to read full symbols, don't bother with the
5638 indices. In this case we also don't care if some other debug
5639 format is making psymtabs, because they are all about to be
5640 expanded anyway. */
5641 if ((objfile->flags & OBJF_READNOW))
5642 {
850ed749
SM
5643 dwarf_read_debug_printf ("readnow requested");
5644
17ee85fc
TT
5645 /* When using READNOW, the using_index flag (set below) indicates that
5646 PER_BFD was already initialized, when we loaded some other objfile. */
5647 if (per_bfd->using_index)
5648 {
850ed749 5649 dwarf_read_debug_printf ("using_index already set");
976ca316 5650 per_objfile->resize_symtabs ();
edc02ceb
TT
5651 objfile->qf.push_front (make_dwarf_gdb_index ());
5652 return;
17ee85fc
TT
5653 }
5654
5655 per_bfd->using_index = 1;
976ca316 5656 create_all_comp_units (per_objfile);
17ee85fc
TT
5657 per_bfd->quick_file_names_table
5658 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5659 per_objfile->resize_symtabs ();
9291a0cd 5660
91eea9cc 5661 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
9291a0cd 5662 {
91eea9cc 5663 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
9291a0cd 5664
17ee85fc 5665 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5666 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5667 }
5668
edc02ceb
TT
5669 /* Arrange for gdb to see the "quick" functions. However, these
5670 functions will be no-ops because we will have expanded all
5671 symtabs. */
5672 objfile->qf.push_front (make_dwarf_gdb_index ());
5673 return;
9291a0cd
TT
5674 }
5675
17ee85fc
TT
5676 /* Was a debug names index already read when we processed an objfile sharing
5677 PER_BFD? */
5678 if (per_bfd->debug_names_table != nullptr)
5679 {
850ed749 5680 dwarf_read_debug_printf ("re-using shared debug names table");
976ca316 5681 per_objfile->resize_symtabs ();
edc02ceb
TT
5682 objfile->qf.push_front (make_dwarf_debug_names ());
5683 return;
17ee85fc
TT
5684 }
5685
5686 /* Was a GDB index already read when we processed an objfile sharing
5687 PER_BFD? */
5688 if (per_bfd->index_table != nullptr)
5689 {
850ed749 5690 dwarf_read_debug_printf ("re-using shared index table");
976ca316 5691 per_objfile->resize_symtabs ();
edc02ceb
TT
5692 objfile->qf.push_front (make_dwarf_gdb_index ());
5693 return;
17ee85fc
TT
5694 }
5695
efb763a5
SM
5696 /* There might already be partial symtabs built for this BFD. This happens
5697 when loading the same binary twice with the index-cache enabled. If so,
5698 don't try to read an index. The objfile / per_objfile initialization will
5699 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5700 code path. */
5701 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
5702 {
5703 dwarf_read_debug_printf ("re-using shared partial symtabs");
edc02ceb
TT
5704 objfile->qf.push_front (make_lazy_dwarf_reader ());
5705 return;
850ed749 5706 }
efb763a5 5707
976ca316 5708 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 5709 {
850ed749 5710 dwarf_read_debug_printf ("found debug names");
976ca316 5711 per_objfile->resize_symtabs ();
edc02ceb
TT
5712 objfile->qf.push_front (make_dwarf_debug_names ());
5713 return;
3c0aa29a 5714 }
927aa2e7 5715
976ca316 5716 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 5717 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5718 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 5719 {
850ed749 5720 dwarf_read_debug_printf ("found gdb index from file");
976ca316 5721 per_objfile->resize_symtabs ();
edc02ceb
TT
5722 objfile->qf.push_front (make_dwarf_gdb_index ());
5723 return;
3c0aa29a 5724 }
9291a0cd 5725
87d6a7aa 5726 /* ... otherwise, try to find the index in the index cache. */
976ca316 5727 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
5728 get_gdb_index_contents_from_cache,
5729 get_gdb_index_contents_from_cache_dwz))
5730 {
850ed749 5731 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa 5732 global_index_cache.hit ();
976ca316 5733 per_objfile->resize_symtabs ();
edc02ceb
TT
5734 objfile->qf.push_front (make_dwarf_gdb_index ());
5735 return;
87d6a7aa
SM
5736 }
5737
5738 global_index_cache.miss ();
edc02ceb 5739 objfile->qf.push_front (make_lazy_dwarf_reader ());
9291a0cd
TT
5740}
5741
5742\f
5743
dce234bc
PP
5744/* Build a partial symbol table. */
5745
5746void
eb36a3eb 5747dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
dce234bc 5748{
976ca316
SM
5749 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5750 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
5751
5752 if (per_bfd->partial_symtabs != nullptr)
5753 {
5754 /* Partial symbols were already read, so now we can simply
5755 attach them. */
eb36a3eb
TT
5756 if (psf == nullptr)
5757 {
5758 psf = new psymbol_functions (per_bfd->partial_symtabs);
5759 objfile->qf.emplace_front (psf);
5760 }
5761 else
5762 psf->set_partial_symtabs (per_bfd->partial_symtabs);
976ca316 5763 per_objfile->resize_symtabs ();
17ee85fc
TT
5764 return;
5765 }
c9bf0622 5766
eb36a3eb
TT
5767 if (psf == nullptr)
5768 {
5769 psf = new psymbol_functions;
5770 objfile->qf.emplace_front (psf);
5771 }
5772 const std::shared_ptr<psymtab_storage> &partial_symtabs
5773 = psf->get_partial_symtabs ();
5774
51962708
TT
5775 /* Set the local reference to partial symtabs, so that we don't try
5776 to read them again if reading another objfile with the same BFD.
5777 If we can't in fact share, this won't make a difference anyway as
5778 the dwarf2_per_bfd object won't be shared. */
eb36a3eb 5779 per_bfd->partial_symtabs = partial_symtabs;
51962708 5780
a70b8144 5781 try
c9bf0622
TT
5782 {
5783 /* This isn't really ideal: all the data we allocate on the
5784 objfile's obstack is still uselessly kept around. However,
5785 freeing it seems unsafe. */
eb36a3eb 5786 psymtab_discarder psymtabs (partial_symtabs.get ());
976ca316 5787 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 5788 psymtabs.keep ();
87d6a7aa 5789
976ca316 5790 per_objfile->resize_symtabs ();
af758d11 5791
87d6a7aa 5792 /* (maybe) store an index in the cache. */
976ca316 5793 global_index_cache.store (per_objfile);
c9bf0622 5794 }
230d2906 5795 catch (const gdb_exception_error &except)
492d29ea
PA
5796 {
5797 exception_print (gdb_stderr, except);
5798 }
c906108c 5799}
c906108c 5800
3b80fe9b
DE
5801/* Find the base address of the compilation unit for range lists and
5802 location lists. It will normally be specified by DW_AT_low_pc.
5803 In DWARF-3 draft 4, the base address could be overridden by
5804 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5805 compilation units with discontinuous ranges. */
5806
5807static void
5808dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5809{
5810 struct attribute *attr;
5811
2b24b6e4 5812 cu->base_address.reset ();
3b80fe9b
DE
5813
5814 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5815 if (attr != nullptr)
95f982e5 5816 cu->base_address = attr->as_address ();
3b80fe9b
DE
5817 else
5818 {
5819 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5820 if (attr != nullptr)
95f982e5 5821 cu->base_address = attr->as_address ();
3b80fe9b
DE
5822 }
5823}
5824
36586728
TT
5825/* Helper function that returns the proper abbrev section for
5826 THIS_CU. */
5827
5828static struct dwarf2_section_info *
5829get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5830{
5831 struct dwarf2_section_info *abbrev;
c3699833 5832 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
5833
5834 if (this_cu->is_dwz)
a7308ce0 5835 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 5836 else
c3699833 5837 abbrev = &per_bfd->abbrev;
36586728
TT
5838
5839 return abbrev;
5840}
5841
f4dc4d17
DE
5842/* Fetch the abbreviation table offset from a comp or type unit header. */
5843
5844static sect_offset
976ca316 5845read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 5846 struct dwarf2_section_info *section,
9c541725 5847 sect_offset sect_off)
f4dc4d17 5848{
96b79293 5849 bfd *abfd = section->get_bfd_owner ();
d521ce57 5850 const gdb_byte *info_ptr;
ac298888 5851 unsigned int initial_length_size, offset_size;
43988095 5852 uint16_t version;
f4dc4d17 5853
976ca316 5854 section->read (per_objfile->objfile);
9c541725 5855 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5856 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5857 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5858 info_ptr += initial_length_size;
5859
5860 version = read_2_bytes (abfd, info_ptr);
5861 info_ptr += 2;
5862 if (version >= 5)
5863 {
5864 /* Skip unit type and address size. */
5865 info_ptr += 2;
5866 }
5867
24aa364d 5868 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5869}
5870
b83470bf
TT
5871/* A partial symtab that is used only for include files. */
5872struct dwarf2_include_psymtab : public partial_symtab
5873{
7b249e47
TT
5874 dwarf2_include_psymtab (const char *filename,
5875 psymtab_storage *partial_symtabs,
0072c873
SM
5876 objfile_per_bfd_storage *objfile_per_bfd)
5877 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
b83470bf
TT
5878 {
5879 }
5880
5881 void read_symtab (struct objfile *objfile) override
5882 {
194d088f
TV
5883 /* It's an include file, no symbols to read for it.
5884 Everything is in the includer symtab. */
5885
5886 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5887 expansion of the includer psymtab. We use the dependencies[0] field to
5888 model the includer. But if we go the regular route of calling
5889 expand_psymtab here, and having expand_psymtab call expand_dependencies
5890 to expand the includer, we'll only use expand_psymtab on the includer
5891 (making it a non-toplevel psymtab), while if we expand the includer via
5892 another path, we'll use read_symtab (making it a toplevel psymtab).
5893 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5894 psymtab, and trigger read_symtab on the includer here directly. */
5895 includer ()->read_symtab (objfile);
b83470bf
TT
5896 }
5897
5898 void expand_psymtab (struct objfile *objfile) override
5899 {
194d088f
TV
5900 /* This is not called by read_symtab, and should not be called by any
5901 expand_dependencies. */
5902 gdb_assert (false);
b83470bf
TT
5903 }
5904
5717c425 5905 bool readin_p (struct objfile *objfile) const override
b83470bf 5906 {
5717c425 5907 return includer ()->readin_p (objfile);
b83470bf
TT
5908 }
5909
5717c425 5910 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
5911 {
5912 return nullptr;
5913 }
5914
5915private:
194d088f
TV
5916 partial_symtab *includer () const
5917 {
5918 /* An include psymtab has exactly one dependency: the psymtab that
5919 includes it. */
5920 gdb_assert (this->number_of_dependencies == 1);
5921 return this->dependencies[0];
5922 }
b83470bf
TT
5923};
5924
aaa75496
JB
5925/* Allocate a new partial symtab for file named NAME and mark this new
5926 partial symtab as being an include of PST. */
5927
5928static void
84685904 5929dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
7b249e47
TT
5930 const char *name,
5931 dwarf2_psymtab *pst,
5932 psymtab_storage *partial_symtabs,
0072c873 5933 objfile_per_bfd_storage *objfile_per_bfd)
aaa75496 5934{
7b249e47 5935 dwarf2_include_psymtab *subpst
0072c873 5936 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
aaa75496 5937
fbd9ab74 5938 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 5939 subpst->dirname = pst->dirname;
fbd9ab74 5940
84685904 5941 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5942 subpst->dependencies[0] = pst;
5943 subpst->number_of_dependencies = 1;
aaa75496
JB
5944}
5945
5946/* Read the Line Number Program data and extract the list of files
5947 included by the source file represented by PST. Build an include
d85a05f0 5948 partial symtab for each of these included files. */
aaa75496
JB
5949
5950static void
5951dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5952 struct die_info *die,
891813be 5953 dwarf2_psymtab *pst)
aaa75496 5954{
fff8551c 5955 line_header_up lh;
d85a05f0 5956 struct attribute *attr;
aaa75496 5957
d85a05f0 5958 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
5959 if (attr != nullptr && attr->form_is_unsigned ())
5960 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
5961 if (lh == NULL)
5962 return; /* No linetable, so no includes. */
5963
79748972
TT
5964 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5965 that we pass in the raw text_low here; that is ok because we're
5966 only decoding the line table to make include partial symtabs, and
5967 so the addresses aren't really used. */
4ae976d1 5968 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5969 pst->raw_text_low (), 1);
aaa75496
JB
5970}
5971
348e048f 5972static hashval_t
52dc124a 5973hash_signatured_type (const void *item)
348e048f 5974{
9a3c8263
SM
5975 const struct signatured_type *sig_type
5976 = (const struct signatured_type *) item;
9a619af0 5977
348e048f 5978 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5979 return sig_type->signature;
348e048f
DE
5980}
5981
5982static int
52dc124a 5983eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5984{
9a3c8263
SM
5985 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5986 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5987
348e048f
DE
5988 return lhs->signature == rhs->signature;
5989}
5990
1fd400ff
TT
5991/* Allocate a hash table for signatured types. */
5992
b0b6a987 5993static htab_up
298e9637 5994allocate_signatured_type_table ()
1fd400ff 5995{
b0b6a987
TT
5996 return htab_up (htab_create_alloc (41,
5997 hash_signatured_type,
5998 eq_signatured_type,
5999 NULL, xcalloc, xfree));
1fd400ff
TT
6000}
6001
78d4d2c5 6002/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6003 and fill them into TYPES_HTAB. It will process only type units,
6004 therefore DW_UT_type. */
c88ee1f0 6005
78d4d2c5 6006static void
976ca316 6007create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6008 struct dwo_file *dwo_file,
b0b6a987 6009 dwarf2_section_info *section, htab_up &types_htab,
43988095 6010 rcuh_kind section_kind)
348e048f 6011{
976ca316 6012 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6013 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6014 bfd *abfd;
6015 const gdb_byte *info_ptr, *end_ptr;
348e048f 6016
b8efb248 6017 abbrev_section = &dwo_file->sections.abbrev;
4bdcc0c1 6018
4800761a 6019 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
6020 section->get_name (),
6021 abbrev_section->get_file_name ());
09406207 6022
96b79293 6023 section->read (objfile);
78d4d2c5 6024 info_ptr = section->buffer;
348e048f 6025
78d4d2c5
JK
6026 if (info_ptr == NULL)
6027 return;
348e048f 6028
78d4d2c5
JK
6029 /* We can't set abfd until now because the section may be empty or
6030 not present, in which case the bfd is unknown. */
96b79293 6031 abfd = section->get_bfd_owner ();
348e048f 6032
c0ab21c2
TT
6033 /* We don't use cutu_reader here because we don't need to read
6034 any dies: the signature is in the header. */
3019eac3 6035
78d4d2c5
JK
6036 end_ptr = info_ptr + section->size;
6037 while (info_ptr < end_ptr)
6038 {
0d305d5c 6039 std::unique_ptr<signatured_type> sig_type;
78d4d2c5
JK
6040 struct dwo_unit *dwo_tu;
6041 void **slot;
6042 const gdb_byte *ptr = info_ptr;
6043 struct comp_unit_head header;
6044 unsigned int length;
8b70b953 6045
9c541725 6046 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6047
a49dd8dd
JK
6048 /* Initialize it due to a false compiler warning. */
6049 header.signature = -1;
9c541725 6050 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6051
78d4d2c5
JK
6052 /* We need to read the type's signature in order to build the hash
6053 table, but we don't need anything else just yet. */
348e048f 6054
976ca316 6055 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6056 abbrev_section, ptr, section_kind);
348e048f 6057
4057dfde 6058 length = header.get_length ();
6caca83c 6059
78d4d2c5
JK
6060 /* Skip dummy type units. */
6061 if (ptr >= info_ptr + length
43988095 6062 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6063 || (header.unit_type != DW_UT_type
6064 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6065 {
6066 info_ptr += length;
6067 continue;
6068 }
dee91e82 6069
78d4d2c5 6070 if (types_htab == NULL)
b8efb248 6071 types_htab = allocate_dwo_unit_table ();
8b70b953 6072
b8efb248
TT
6073 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6074 dwo_tu->dwo_file = dwo_file;
6075 dwo_tu->signature = header.signature;
6076 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6077 dwo_tu->section = section;
6078 dwo_tu->sect_off = sect_off;
6079 dwo_tu->length = length;
78d4d2c5 6080
b8efb248 6081 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
78d4d2c5
JK
6082 gdb_assert (slot != NULL);
6083 if (*slot != NULL)
b8efb248
TT
6084 complaint (_("debug type entry at offset %s is duplicate to"
6085 " the entry at offset %s, signature %s"),
6086 sect_offset_str (sect_off),
6087 sect_offset_str (dwo_tu->sect_off),
6088 hex_string (header.signature));
6089 *slot = dwo_tu;
3019eac3 6090
6f738b01
SM
6091 dwarf_read_debug_printf_v (" offset %s, signature %s",
6092 sect_offset_str (sect_off),
6093 hex_string (header.signature));
3019eac3 6094
78d4d2c5
JK
6095 info_ptr += length;
6096 }
6097}
3019eac3 6098
78d4d2c5
JK
6099/* Create the hash table of all entries in the .debug_types
6100 (or .debug_types.dwo) section(s).
b8efb248 6101 DWO_FILE is a pointer to the DWO file object.
b3c8eb43 6102
78d4d2c5 6103 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6104
78d4d2c5 6105 Note: This function processes DWO files only, not DWP files. */
348e048f 6106
78d4d2c5 6107static void
976ca316 6108create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6109 struct dwo_file *dwo_file,
fd5866f6 6110 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6111 htab_up &types_htab)
78d4d2c5 6112{
fd5866f6 6113 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6114 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6115 rcuh_kind::TYPE);
3019eac3
DE
6116}
6117
5989a64e 6118/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6119 If SLOT is non-NULL, it is the entry to use in the hash table.
6120 Otherwise we find one. */
6121
6122static struct signatured_type *
976ca316 6123add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6124{
91eea9cc
TT
6125 if (per_objfile->per_bfd->all_comp_units.size ()
6126 == per_objfile->per_bfd->all_comp_units.capacity ())
976ca316 6127 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6128
0d305d5c
TT
6129 std::unique_ptr<signatured_type> sig_type_holder
6130 = per_objfile->per_bfd->allocate_signatured_type ();
6131 signatured_type *sig_type = sig_type_holder.get ();
b2bdb8cf 6132
976ca316 6133 per_objfile->resize_symtabs ();
af758d11 6134
91eea9cc 6135 per_objfile->per_bfd->all_comp_units.push_back (std::move (sig_type_holder));
6aa5f3a6 6136 sig_type->signature = sig;
0d305d5c 6137 sig_type->is_debug_types = 1;
976ca316 6138 if (per_objfile->per_bfd->using_index)
6aa5f3a6 6139 {
0d305d5c 6140 sig_type->v.quick =
976ca316 6141 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6142 struct dwarf2_per_cu_quick_data);
6143 }
6144
6145 if (slot == NULL)
6146 {
976ca316 6147 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6148 sig_type, INSERT);
6149 }
6150 gdb_assert (*slot == NULL);
6151 *slot = sig_type;
6152 /* The rest of sig_type must be filled in by the caller. */
6153 return sig_type;
6154}
6155
a2ce51a0
DE
6156/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6157 Fill in SIG_ENTRY with DWO_ENTRY. */
6158
6159static void
976ca316 6160fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6161 struct signatured_type *sig_entry,
6162 struct dwo_unit *dwo_entry)
6163{
976ca316 6164 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6165
7ee85ab1 6166 /* Make sure we're not clobbering something we don't expect to. */
0d305d5c
TT
6167 gdb_assert (! sig_entry->queued);
6168 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
1859c670 6169 if (per_bfd->using_index)
6aa5f3a6 6170 {
0d305d5c
TT
6171 gdb_assert (sig_entry->v.quick != NULL);
6172 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6aa5f3a6
DE
6173 }
6174 else
0d305d5c 6175 gdb_assert (sig_entry->v.psymtab == NULL);
a2ce51a0 6176 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6177 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6178 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6179 gdb_assert (sig_entry->dwo_unit == NULL);
6180
0d305d5c
TT
6181 sig_entry->section = dwo_entry->section;
6182 sig_entry->sect_off = dwo_entry->sect_off;
6183 sig_entry->length = dwo_entry->length;
6184 sig_entry->reading_dwo_directly = 1;
6185 sig_entry->per_bfd = per_bfd;
a2ce51a0
DE
6186 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6187 sig_entry->dwo_unit = dwo_entry;
6188}
6189
6190/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6191 If we haven't read the TU yet, create the signatured_type data structure
6192 for a TU to be read in directly from a DWO file, bypassing the stub.
6193 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6194 using .gdb_index, then when reading a CU we want to stay in the DWO file
6195 containing that CU. Otherwise we could end up reading several other DWO
6196 files (due to comdat folding) to process the transitive closure of all the
6197 mentioned TUs, and that can be slow. The current DWO file will have every
6198 type signature that it needs.
a2ce51a0
DE
6199 We only do this for .gdb_index because in the psymtab case we already have
6200 to read all the DWOs to build the type unit groups. */
6201
6202static struct signatured_type *
6203lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6204{
976ca316 6205 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6206 struct dwo_file *dwo_file;
6207 struct dwo_unit find_dwo_entry, *dwo_entry;
6208 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6209 void **slot;
a2ce51a0 6210
976ca316 6211 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6212
6aa5f3a6
DE
6213 /* If TU skeletons have been removed then we may not have read in any
6214 TUs yet. */
976ca316
SM
6215 if (per_objfile->per_bfd->signatured_types == NULL)
6216 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6217
6218 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6219 Use the global signatured_types array to do our own comdat-folding
6220 of types. If this is the first time we're reading this TU, and
6221 the TU has an entry in .gdb_index, replace the recorded data from
6222 .gdb_index with this TU. */
a2ce51a0 6223
a2ce51a0 6224 find_sig_entry.signature = sig;
976ca316 6225 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6226 &find_sig_entry, INSERT);
9a3c8263 6227 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6228
6229 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6230 read. Don't reassign the global entry to point to this DWO if that's
6231 the case. Also note that if the TU is already being read, it may not
6232 have come from a DWO, the program may be a mix of Fission-compiled
6233 code and non-Fission-compiled code. */
6234
6235 /* Have we already tried to read this TU?
6236 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6237 needn't exist in the global table yet). */
0d305d5c 6238 if (sig_entry != NULL && sig_entry->tu_read)
a2ce51a0
DE
6239 return sig_entry;
6240
6aa5f3a6
DE
6241 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6242 dwo_unit of the TU itself. */
6243 dwo_file = cu->dwo_unit->dwo_file;
6244
a2ce51a0
DE
6245 /* Ok, this is the first time we're reading this TU. */
6246 if (dwo_file->tus == NULL)
6247 return NULL;
6248 find_dwo_entry.signature = sig;
b0b6a987
TT
6249 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6250 &find_dwo_entry);
a2ce51a0
DE
6251 if (dwo_entry == NULL)
6252 return NULL;
6253
6aa5f3a6
DE
6254 /* If the global table doesn't have an entry for this TU, add one. */
6255 if (sig_entry == NULL)
976ca316 6256 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6257
976ca316 6258 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
0d305d5c 6259 sig_entry->tu_read = 1;
a2ce51a0
DE
6260 return sig_entry;
6261}
6262
a2ce51a0
DE
6263/* Subroutine of lookup_signatured_type.
6264 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6265 then try the DWP file. If the TU stub (skeleton) has been removed then
6266 it won't be in .gdb_index. */
a2ce51a0
DE
6267
6268static struct signatured_type *
6269lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6270{
976ca316
SM
6271 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6272 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6273 struct dwo_unit *dwo_entry;
6274 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6275 void **slot;
a2ce51a0 6276
976ca316 6277 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6278 gdb_assert (dwp_file != NULL);
6279
6aa5f3a6
DE
6280 /* If TU skeletons have been removed then we may not have read in any
6281 TUs yet. */
976ca316
SM
6282 if (per_objfile->per_bfd->signatured_types == NULL)
6283 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6284
6aa5f3a6 6285 find_sig_entry.signature = sig;
976ca316 6286 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6287 &find_sig_entry, INSERT);
9a3c8263 6288 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6289
6290 /* Have we already tried to read this TU?
6291 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6292 needn't exist in the global table yet). */
6293 if (sig_entry != NULL)
6294 return sig_entry;
6295
a2ce51a0
DE
6296 if (dwp_file->tus == NULL)
6297 return NULL;
976ca316
SM
6298 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6299 1 /* is_debug_types */);
a2ce51a0
DE
6300 if (dwo_entry == NULL)
6301 return NULL;
6302
976ca316
SM
6303 sig_entry = add_type_unit (per_objfile, sig, slot);
6304 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6305
a2ce51a0
DE
6306 return sig_entry;
6307}
6308
380bca97 6309/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6310 Returns NULL if signature SIG is not present in the table.
6311 It is up to the caller to complain about this. */
348e048f
DE
6312
6313static struct signatured_type *
a2ce51a0 6314lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6315{
976ca316 6316 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6317
976ca316 6318 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6319 {
6320 /* We're in a DWO/DWP file, and we're using .gdb_index.
6321 These cases require special processing. */
976ca316 6322 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6323 return lookup_dwo_signatured_type (cu, sig);
6324 else
6325 return lookup_dwp_signatured_type (cu, sig);
6326 }
6327 else
6328 {
6329 struct signatured_type find_entry, *entry;
348e048f 6330
976ca316 6331 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6332 return NULL;
6333 find_entry.signature = sig;
9a3c8263 6334 entry = ((struct signatured_type *)
976ca316 6335 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6336 &find_entry));
a2ce51a0
DE
6337 return entry;
6338 }
348e048f 6339}
18a8505e 6340
42e7ad6c 6341/* Low level DIE reading support. */
348e048f 6342
d85a05f0
DJ
6343/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6344
6345static void
6346init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6347 struct dwarf2_cu *cu,
3019eac3 6348 struct dwarf2_section_info *section,
685af9cd
TT
6349 struct dwo_file *dwo_file,
6350 struct abbrev_table *abbrev_table)
d85a05f0 6351{
fceca515 6352 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6353 reader->abfd = section->get_bfd_owner ();
d85a05f0 6354 reader->cu = cu;
3019eac3 6355 reader->dwo_file = dwo_file;
dee91e82
DE
6356 reader->die_section = section;
6357 reader->buffer = section->buffer;
f664829e 6358 reader->buffer_end = section->buffer + section->size;
685af9cd 6359 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6360}
6361
c0ab21c2 6362/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6363 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6364 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6365 already.
6366
6367 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6368 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6369 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6370 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6371 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6372 STUB_COMP_DIR may be non-NULL.
3e225074 6373 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6374 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6375 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6376 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6377 kept around for at least as long as *RESULT_READER.
6378
b0c7bfa9
DE
6379 The result is non-zero if a valid (non-dummy) DIE was found. */
6380
6381static int
4ab09049 6382read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6383 struct dwo_unit *dwo_unit,
b0c7bfa9 6384 struct die_info *stub_comp_unit_die,
a2ce51a0 6385 const char *stub_comp_dir,
b0c7bfa9 6386 struct die_reader_specs *result_reader,
d521ce57 6387 const gdb_byte **result_info_ptr,
b0c7bfa9 6388 struct die_info **result_comp_unit_die,
685af9cd 6389 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6390{
976ca316 6391 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6392 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6393 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6394 bfd *abfd;
d521ce57 6395 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6396 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6397 int i,num_extra_attrs;
6398 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6399 struct die_info *comp_unit_die;
6400
b0aeadb3
DE
6401 /* At most one of these may be provided. */
6402 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6403
b0c7bfa9
DE
6404 /* These attributes aren't processed until later:
6405 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6406 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6407 referenced later. However, these attributes are found in the stub
6408 which we won't have later. In order to not impose this complication
6409 on the rest of the code, we read them here and copy them to the
6410 DWO CU/TU die. */
b0c7bfa9
DE
6411
6412 stmt_list = NULL;
6413 low_pc = NULL;
6414 high_pc = NULL;
6415 ranges = NULL;
6416 comp_dir = NULL;
6417
6418 if (stub_comp_unit_die != NULL)
6419 {
6420 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6421 DWO file. */
4ab09049 6422 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6423 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6424 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6425 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6426 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6427 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6428
a39fdb41 6429 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6430
2b0c7f41
SM
6431 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6432 We need the value before we can process DW_AT_ranges values from the
6433 DWO. */
6434 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6435
6436 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6437 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6438 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6439 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6440 section. */
6441 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6442 }
a2ce51a0
DE
6443 else if (stub_comp_dir != NULL)
6444 {
6445 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6446 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6447 comp_dir->name = DW_AT_comp_dir;
6448 comp_dir->form = DW_FORM_string;
c6481205 6449 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6450 }
b0c7bfa9
DE
6451
6452 /* Set up for reading the DWO CU/TU. */
6453 cu->dwo_unit = dwo_unit;
685af9cd 6454 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6455 section->read (objfile);
6456 abfd = section->get_bfd_owner ();
9c541725
PA
6457 begin_info_ptr = info_ptr = (section->buffer
6458 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6459 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6460
4ab09049 6461 if (per_cu->is_debug_types)
b0c7bfa9 6462 {
4ab09049 6463 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6464
976ca316
SM
6465 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6466 section, dwo_abbrev_section,
43988095 6467 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6468 /* This is not an assert because it can be caused by bad debug info. */
43988095 6469 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6470 {
6471 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6472 " TU at offset %s [in module %s]"),
a2ce51a0 6473 hex_string (sig_type->signature),
43988095 6474 hex_string (cu->header.signature),
9d8780f0 6475 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6476 bfd_get_filename (abfd));
6477 }
9c541725 6478 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6479 /* For DWOs coming from DWP files, we don't know the CU length
6480 nor the type's offset in the TU until now. */
4057dfde 6481 dwo_unit->length = cu->header.get_length ();
9c541725 6482 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6483
6484 /* Establish the type offset that can be used to lookup the type.
6485 For DWO files, we don't know it until now. */
9c541725
PA
6486 sig_type->type_offset_in_section
6487 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6488 }
6489 else
6490 {
976ca316
SM
6491 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6492 section, dwo_abbrev_section,
43988095 6493 info_ptr, rcuh_kind::COMPILE);
9c541725 6494 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6495 /* For DWOs coming from DWP files, we don't know the CU length
6496 until now. */
4057dfde 6497 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6498 }
6499
606decb2 6500 dwo_abbrev_section->read (objfile);
685af9cd 6501 *result_dwo_abbrev_table
606decb2 6502 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6503 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6504 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6505
6506 /* Read in the die, but leave space to copy over the attributes
6507 from the stub. This has the benefit of simplifying the rest of
6508 the code - all the work to maintain the illusion of a single
6509 DW_TAG_{compile,type}_unit DIE is done here. */
6510 num_extra_attrs = ((stmt_list != NULL)
6511 + (low_pc != NULL)
6512 + (high_pc != NULL)
6513 + (ranges != NULL)
6514 + (comp_dir != NULL));
6515 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6516 num_extra_attrs);
b0c7bfa9
DE
6517
6518 /* Copy over the attributes from the stub to the DIE we just read in. */
6519 comp_unit_die = *result_comp_unit_die;
6520 i = comp_unit_die->num_attrs;
6521 if (stmt_list != NULL)
6522 comp_unit_die->attrs[i++] = *stmt_list;
6523 if (low_pc != NULL)
6524 comp_unit_die->attrs[i++] = *low_pc;
6525 if (high_pc != NULL)
6526 comp_unit_die->attrs[i++] = *high_pc;
6527 if (ranges != NULL)
6528 comp_unit_die->attrs[i++] = *ranges;
6529 if (comp_dir != NULL)
6530 comp_unit_die->attrs[i++] = *comp_dir;
6531 comp_unit_die->num_attrs += num_extra_attrs;
6532
b4f54984 6533 if (dwarf_die_debug)
bf6af496
DE
6534 {
6535 fprintf_unfiltered (gdb_stdlog,
6536 "Read die from %s@0x%x of %s:\n",
96b79293 6537 section->get_name (),
bf6af496
DE
6538 (unsigned) (begin_info_ptr - section->buffer),
6539 bfd_get_filename (abfd));
b4f54984 6540 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6541 }
6542
b0c7bfa9
DE
6543 /* Skip dummy compilation units. */
6544 if (info_ptr >= begin_info_ptr + dwo_unit->length
6545 || peek_abbrev_code (abfd, info_ptr) == 0)
6546 return 0;
6547
6548 *result_info_ptr = info_ptr;
6549 return 1;
6550}
6551
a084a2a6
AT
6552/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6553 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6554 signature is part of the header. */
6555static gdb::optional<ULONGEST>
6556lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6557{
6558 if (cu->header.version >= 5)
6559 return cu->header.signature;
6560 struct attribute *attr;
6561 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 6562 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 6563 return gdb::optional<ULONGEST> ();
529908cb 6564 return attr->as_unsigned ();
a084a2a6
AT
6565}
6566
c0ab21c2 6567/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6568 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6569 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6570
6571static struct dwo_unit *
4ab09049 6572lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6573{
4ab09049 6574 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6575 struct dwo_unit *dwo_unit;
c0ab21c2 6576 const char *comp_dir;
b0c7bfa9 6577
a2ce51a0
DE
6578 gdb_assert (cu != NULL);
6579
b0c7bfa9 6580 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6581 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6582 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6583
4ab09049
SM
6584 if (per_cu->is_debug_types)
6585 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6586 else
6587 {
a084a2a6 6588 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6589
a084a2a6 6590 if (!signature.has_value ())
b0c7bfa9
DE
6591 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6592 " [in module %s]"),
4ab09049
SM
6593 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6594
6595 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6596 }
6597
b0c7bfa9
DE
6598 return dwo_unit;
6599}
6600
c0ab21c2 6601/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6602 See it for a description of the parameters.
fcd3b13d 6603 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6604
c0ab21c2 6605void
9e021579
SM
6606cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6607 dwarf2_per_objfile *per_objfile,
2e671100 6608 dwarf2_cu *existing_cu)
a2ce51a0 6609{
a2ce51a0 6610 struct signatured_type *sig_type;
a2ce51a0
DE
6611
6612 /* Verify we can do the following downcast, and that we have the
6613 data we need. */
6614 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6615 sig_type = (struct signatured_type *) this_cu;
6616 gdb_assert (sig_type->dwo_unit != NULL);
6617
2e671100
SM
6618 dwarf2_cu *cu;
6619
6620 if (existing_cu != nullptr)
6aa5f3a6 6621 {
2e671100
SM
6622 cu = existing_cu;
6623 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6624 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6625 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6626 }
6627 else
6628 {
7188ed02 6629 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6630 in per_objfile yet. */
7188ed02 6631 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6632 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6633 cu = m_new_cu.get ();
6aa5f3a6
DE
6634 }
6635
6636 /* A future optimization, if needed, would be to use an existing
6637 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6638 could share abbrev tables. */
a2ce51a0 6639
2e671100 6640 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6641 NULL /* stub_comp_unit_die */,
6642 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6643 this, &info_ptr,
3e225074 6644 &comp_unit_die,
c0ab21c2 6645 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6646 {
6647 /* Dummy die. */
c0ab21c2 6648 dummy_p = true;
a2ce51a0 6649 }
a2ce51a0
DE
6650}
6651
fd820528 6652/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6653 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6654
f4dc4d17
DE
6655 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6656 Otherwise the table specified in the comp unit header is read in and used.
6657 This is an optimization for when we already have the abbrev table.
6658
2e671100
SM
6659 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6660 allocated. */
aaa75496 6661
ab432490 6662cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6663 dwarf2_per_objfile *per_objfile,
c0ab21c2 6664 struct abbrev_table *abbrev_table,
2e671100 6665 dwarf2_cu *existing_cu,
c0ab21c2
TT
6666 bool skip_partial)
6667 : die_reader_specs {},
6751ebae 6668 m_this_cu (this_cu)
c906108c 6669{
976ca316 6670 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6671 struct dwarf2_section_info *section = this_cu->section;
96b79293 6672 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 6673 const gdb_byte *begin_info_ptr;
dee91e82 6674 struct signatured_type *sig_type = NULL;
4bdcc0c1 6675 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6676 /* Non-zero if CU currently points to a DWO file and we need to
6677 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6678 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6679 int rereading_dwo_cu = 0;
c906108c 6680
b4f54984 6681 if (dwarf_die_debug)
9d8780f0 6682 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6683 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6684 sect_offset_str (this_cu->sect_off));
09406207 6685
a2ce51a0
DE
6686 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6687 file (instead of going through the stub), short-circuit all of this. */
6688 if (this_cu->reading_dwo_directly)
6689 {
6690 /* Narrow down the scope of possibilities to have to understand. */
6691 gdb_assert (this_cu->is_debug_types);
6692 gdb_assert (abbrev_table == NULL);
976ca316 6693 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
6694 return;
6695 }
6696
dee91e82 6697 /* This is cheap if the section is already read in. */
96b79293 6698 section->read (objfile);
dee91e82 6699
9c541725 6700 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6701
6702 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 6703
2e671100
SM
6704 dwarf2_cu *cu;
6705
6706 if (existing_cu != nullptr)
dee91e82 6707 {
2e671100 6708 cu = existing_cu;
42e7ad6c
DE
6709 /* If this CU is from a DWO file we need to start over, we need to
6710 refetch the attributes from the skeleton CU.
6711 This could be optimized by retrieving those attributes from when we
6712 were here the first time: the previous comp_unit_die was stored in
6713 comp_unit_obstack. But there's no data yet that we need this
6714 optimization. */
6715 if (cu->dwo_unit != NULL)
6716 rereading_dwo_cu = 1;
dee91e82
DE
6717 }
6718 else
6719 {
7188ed02 6720 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6721 in per_objfile yet. */
976ca316
SM
6722 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6723 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 6724 cu = m_new_cu.get ();
42e7ad6c 6725 }
dee91e82 6726
b0c7bfa9 6727 /* Get the header. */
9c541725 6728 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6729 {
6730 /* We already have the header, there's no need to read it in again. */
9c541725 6731 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6732 }
6733 else
6734 {
3019eac3 6735 if (this_cu->is_debug_types)
dee91e82 6736 {
976ca316
SM
6737 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6738 section, abbrev_section,
6739 info_ptr, rcuh_kind::TYPE);
dee91e82 6740
42e7ad6c
DE
6741 /* Since per_cu is the first member of struct signatured_type,
6742 we can go from a pointer to one to a pointer to the other. */
6743 sig_type = (struct signatured_type *) this_cu;
43988095 6744 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6745 gdb_assert (sig_type->type_offset_in_tu
6746 == cu->header.type_cu_offset_in_tu);
6747 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6748
42e7ad6c
DE
6749 /* LENGTH has not been set yet for type units if we're
6750 using .gdb_index. */
4057dfde 6751 this_cu->length = cu->header.get_length ();
3019eac3
DE
6752
6753 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6754 sig_type->type_offset_in_section =
6755 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6756
6757 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6758 }
6759 else
6760 {
976ca316
SM
6761 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6762 section, abbrev_section,
43988095
JK
6763 info_ptr,
6764 rcuh_kind::COMPILE);
dee91e82 6765
9c541725 6766 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
6767 if (this_cu->length == 0)
6768 this_cu->length = cu->header.get_length ();
6769 else
6770 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6771 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6772 }
6773 }
10b3939b 6774
6caca83c 6775 /* Skip dummy compilation units. */
dee91e82 6776 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6777 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6778 {
6779 dummy_p = true;
6780 return;
6781 }
6caca83c 6782
433df2d4
DE
6783 /* If we don't have them yet, read the abbrevs for this compilation unit.
6784 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6785 done. */
f4dc4d17 6786 if (abbrev_table != NULL)
685af9cd
TT
6787 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6788 else
f4dc4d17 6789 {
606decb2 6790 abbrev_section->read (objfile);
c0ab21c2 6791 m_abbrev_table_holder
606decb2 6792 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 6793 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6794 }
af703f96 6795
dee91e82 6796 /* Read the top level CU/TU die. */
c0ab21c2 6797 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6798 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6799
58f0c718 6800 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6801 {
6802 dummy_p = true;
6803 return;
6804 }
58f0c718 6805
b0c7bfa9 6806 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6807 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6808 table from the DWO file and pass the ownership over to us. It will be
6809 referenced from READER, so we must make sure to free it after we're done
6810 with READER.
6811
b0c7bfa9
DE
6812 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6813 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6814 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6815 if (dwo_name != nullptr)
3019eac3 6816 {
3019eac3 6817 struct dwo_unit *dwo_unit;
b0c7bfa9 6818 struct die_info *dwo_comp_unit_die;
3019eac3 6819
3e225074 6820 if (comp_unit_die->has_children)
6a506a2d 6821 {
b98664d3 6822 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6823 " has children (offset %s) [in module %s]"),
6824 sect_offset_str (this_cu->sect_off),
6825 bfd_get_filename (abfd));
6a506a2d 6826 }
4ab09049 6827 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 6828 if (dwo_unit != NULL)
3019eac3 6829 {
4ab09049 6830 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 6831 comp_unit_die, NULL,
c0ab21c2 6832 this, &info_ptr,
3e225074 6833 &dwo_comp_unit_die,
c0ab21c2 6834 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6835 {
6836 /* Dummy die. */
c0ab21c2 6837 dummy_p = true;
6a506a2d
DE
6838 return;
6839 }
6840 comp_unit_die = dwo_comp_unit_die;
6841 }
6842 else
6843 {
6844 /* Yikes, we couldn't find the rest of the DIE, we only have
6845 the stub. A complaint has already been logged. There's
6846 not much more we can do except pass on the stub DIE to
6847 die_reader_func. We don't want to throw an error on bad
6848 debug info. */
3019eac3
DE
6849 }
6850 }
c0ab21c2 6851}
3019eac3 6852
6751ebae
TT
6853void
6854cutu_reader::keep ()
c0ab21c2 6855{
b0c7bfa9 6856 /* Done, clean up. */
6751ebae
TT
6857 gdb_assert (!dummy_p);
6858 if (m_new_cu != NULL)
348e048f 6859 {
7188ed02 6860 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 6861 now. */
7188ed02
SM
6862 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6863 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 6864 }
dee91e82
DE
6865}
6866
18a8505e
AT
6867/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6868 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6869 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6870
6871 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6872 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6873
6874 We fill in THIS_CU->length.
6875
dee91e82 6876 THIS_CU->cu is always freed when done.
3019eac3 6877 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6878 to care whether it refers to the "main" CU or the DWO CU.
6879
6880 When parent_cu is passed, it is used to provide a default value for
6881 str_offsets_base and addr_base from the parent. */
dee91e82 6882
ab432490 6883cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6884 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
6885 struct dwarf2_cu *parent_cu,
6886 struct dwo_file *dwo_file)
6887 : die_reader_specs {},
6888 m_this_cu (this_cu)
dee91e82 6889{
976ca316 6890 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6891 struct dwarf2_section_info *section = this_cu->section;
96b79293 6892 bfd *abfd = section->get_bfd_owner ();
33e80786 6893 struct dwarf2_section_info *abbrev_section;
d521ce57 6894 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6895
b4f54984 6896 if (dwarf_die_debug)
9d8780f0 6897 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6898 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6899 sect_offset_str (this_cu->sect_off));
09406207 6900
976ca316 6901 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 6902
33e80786
DE
6903 abbrev_section = (dwo_file != NULL
6904 ? &dwo_file->sections.abbrev
6905 : get_abbrev_section_for_cu (this_cu));
6906
dee91e82 6907 /* This is cheap if the section is already read in. */
96b79293 6908 section->read (objfile);
dee91e82 6909
976ca316 6910 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 6911
9c541725 6912 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
6913 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6914 section, abbrev_section, info_ptr,
43988095
JK
6915 (this_cu->is_debug_types
6916 ? rcuh_kind::TYPE
6917 : rcuh_kind::COMPILE));
dee91e82 6918
18a8505e
AT
6919 if (parent_cu != nullptr)
6920 {
c0ab21c2
TT
6921 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6922 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 6923 }
4057dfde 6924 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
6925
6926 /* Skip dummy compilation units. */
6927 if (info_ptr >= begin_info_ptr + this_cu->length
6928 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6929 {
6930 dummy_p = true;
6931 return;
6932 }
72bf9492 6933
606decb2 6934 abbrev_section->read (objfile);
c0ab21c2 6935 m_abbrev_table_holder
606decb2 6936 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 6937
c0ab21c2
TT
6938 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6939 m_abbrev_table_holder.get ());
3e225074 6940 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
6941}
6942
0018ea6f
DE
6943\f
6944/* Type Unit Groups.
dee91e82 6945
0018ea6f
DE
6946 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6947 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6948 so that all types coming from the same compilation (.o file) are grouped
6949 together. A future step could be to put the types in the same symtab as
6950 the CU the types ultimately came from. */
ff013f42 6951
f4dc4d17
DE
6952static hashval_t
6953hash_type_unit_group (const void *item)
6954{
9a3c8263
SM
6955 const struct type_unit_group *tu_group
6956 = (const struct type_unit_group *) item;
f4dc4d17 6957
094b34ac 6958 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 6959}
348e048f
DE
6960
6961static int
f4dc4d17 6962eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 6963{
9a3c8263
SM
6964 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6965 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 6966
094b34ac 6967 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 6968}
348e048f 6969
f4dc4d17
DE
6970/* Allocate a hash table for type unit groups. */
6971
eaa5fa8b 6972static htab_up
298e9637 6973allocate_type_unit_groups_table ()
f4dc4d17 6974{
eaa5fa8b
TT
6975 return htab_up (htab_create_alloc (3,
6976 hash_type_unit_group,
6977 eq_type_unit_group,
0d305d5c
TT
6978 [] (void *arg)
6979 {
6980 type_unit_group *grp
6981 = (type_unit_group *) arg;
6982 delete grp;
6983 },
6984 xcalloc, xfree));
f4dc4d17 6985}
dee91e82 6986
f4dc4d17
DE
6987/* Type units that don't have DW_AT_stmt_list are grouped into their own
6988 partial symtabs. We combine several TUs per psymtab to not let the size
6989 of any one psymtab grow too big. */
6990#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6991#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 6992
094b34ac 6993/* Helper routine for get_type_unit_group.
f4dc4d17
DE
6994 Create the type_unit_group object used to hold one or more TUs. */
6995
0d305d5c 6996static std::unique_ptr<type_unit_group>
094b34ac 6997create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 6998{
976ca316
SM
6999 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7000 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 7001
0d305d5c
TT
7002 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
7003 tu_group->per_bfd = per_bfd;
f4dc4d17 7004
1859c670 7005 if (per_bfd->using_index)
094b34ac 7006 {
0d305d5c
TT
7007 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7008 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7009 }
7010 else
7011 {
9c541725 7012 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7013 dwarf2_psymtab *pst;
528e1572 7014 std::string name;
094b34ac
DE
7015
7016 /* Give the symtab a useful name for debug purposes. */
7017 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7018 name = string_printf ("<type_units_%d>",
7019 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7020 else
528e1572 7021 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7022
0d305d5c
TT
7023 pst = create_partial_symtab (tu_group.get (), per_objfile,
7024 name.c_str ());
6d94535f 7025 pst->anonymous = true;
094b34ac 7026 }
f4dc4d17 7027
094b34ac 7028 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7029 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7030
7031 return tu_group;
7032}
7033
094b34ac
DE
7034/* Look up the type_unit_group for type unit CU, and create it if necessary.
7035 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7036
7037static struct type_unit_group *
ff39bb5e 7038get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7039{
976ca316
SM
7040 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7041 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7042 struct type_unit_group *tu_group;
7043 void **slot;
7044 unsigned int line_offset;
7045 struct type_unit_group type_unit_group_for_lookup;
7046
976ca316
SM
7047 if (per_objfile->per_bfd->type_unit_groups == NULL)
7048 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7049
7050 /* Do we need to create a new group, or can we use an existing one? */
7051
529908cb 7052 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7053 {
529908cb 7054 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7055 ++tu_stats->nr_symtab_sharers;
7056 }
7057 else
7058 {
7059 /* Ugh, no stmt_list. Rare, but we have to handle it.
7060 We can do various things here like create one group per TU or
7061 spread them over multiple groups to split up the expansion work.
7062 To avoid worst case scenarios (too many groups or too large groups)
7063 we, umm, group them in bunches. */
7064 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7065 | (tu_stats->nr_stmt_less_type_units
7066 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7067 ++tu_stats->nr_stmt_less_type_units;
7068 }
7069
094b34ac 7070 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7071 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7072 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17 7073 &type_unit_group_for_lookup, INSERT);
0d305d5c 7074 if (*slot == nullptr)
f4dc4d17 7075 {
9c541725 7076 sect_offset line_offset_struct = (sect_offset) line_offset;
0d305d5c
TT
7077 std::unique_ptr<type_unit_group> grp
7078 = create_type_unit_group (cu, line_offset_struct);
7079 *slot = grp.release ();
f4dc4d17
DE
7080 ++tu_stats->nr_symtabs;
7081 }
7082
0d305d5c
TT
7083 tu_group = (struct type_unit_group *) *slot;
7084 gdb_assert (tu_group != nullptr);
f4dc4d17
DE
7085 return tu_group;
7086}
0018ea6f
DE
7087\f
7088/* Partial symbol tables. */
7089
7090/* Create a psymtab named NAME and assign it to PER_CU.
7091
7092 The caller must fill in the following details:
7093 dirname, textlow, texthigh. */
7094
891813be 7095static dwarf2_psymtab *
7aa104c4
SM
7096create_partial_symtab (dwarf2_per_cu_data *per_cu,
7097 dwarf2_per_objfile *per_objfile,
7098 const char *name)
0018ea6f 7099{
0072c873
SM
7100 dwarf2_psymtab *pst
7101 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7102 per_objfile->objfile->per_bfd, per_cu);
0018ea6f 7103
6d94535f 7104 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7105
7106 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7107 per_cu->v.psymtab = pst;
7108
7109 return pst;
7110}
7111
c0ab21c2 7112/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7113
7114static void
7115process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7116 const gdb_byte *info_ptr,
0018ea6f 7117 struct die_info *comp_unit_die,
c0ab21c2 7118 enum language pretend_language)
0018ea6f
DE
7119{
7120 struct dwarf2_cu *cu = reader->cu;
7aa104c4 7121 dwarf2_per_objfile *per_objfile = cu->per_objfile;
84685904 7122 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7aa104c4 7123 struct objfile *objfile = per_objfile->objfile;
08feed99 7124 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7125 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7126 CORE_ADDR baseaddr;
7127 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7128 dwarf2_psymtab *pst;
3a2b436a 7129 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7130 const char *filename;
0018ea6f 7131
0018ea6f
DE
7132 gdb_assert (! per_cu->is_debug_types);
7133
c0ab21c2 7134 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7135
0018ea6f 7136 /* Allocate a new partial symbol table structure. */
2e927613
TV
7137 gdb::unique_xmalloc_ptr<char> debug_filename;
7138 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7139 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7140 if (filename == NULL)
0018ea6f 7141 filename = "";
2e927613
TV
7142 else if (strcmp (filename, artificial) == 0)
7143 {
7144 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7145 sect_offset_str (per_cu->sect_off),
7146 (char *) NULL));
2e927613
TV
7147 filename = debug_filename.get ();
7148 }
0018ea6f 7149
7aa104c4 7150 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7151
7152 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7153 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7154
b3b3bada 7155 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7156
7157 dwarf2_find_base_address (comp_unit_die, cu);
7158
7159 /* Possibly set the default values of LOWPC and HIGHPC from
7160 `DW_AT_ranges'. */
3a2b436a
JK
7161 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7162 &best_highpc, cu, pst);
7163 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7164 {
7165 CORE_ADDR low
7166 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7167 - baseaddr);
7168 CORE_ADDR high
7169 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7170 - baseaddr - 1);
7171 /* Store the contiguous range if it is not empty; it can be
7172 empty for CUs with no code. */
84685904 7173 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 7174 low, high, pst);
79748972 7175 }
0018ea6f
DE
7176
7177 /* Check if comp unit has_children.
7178 If so, read the rest of the partial symbols from this comp unit.
7179 If not, there's no more debug_info for this comp unit. */
3e225074 7180 if (comp_unit_die->has_children)
0018ea6f
DE
7181 {
7182 struct partial_die_info *first_die;
7183 CORE_ADDR lowpc, highpc;
7184
7185 lowpc = ((CORE_ADDR) -1);
7186 highpc = ((CORE_ADDR) 0);
7187
7188 first_die = load_partial_dies (reader, info_ptr, 1);
7189
7190 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7191 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7192
7193 /* If we didn't find a lowpc, set it to highpc to avoid
7194 complaints from `maint check'. */
7195 if (lowpc == ((CORE_ADDR) -1))
7196 lowpc = highpc;
7197
7198 /* If the compilation unit didn't have an explicit address range,
7199 then use the information extracted from its child dies. */
e385593e 7200 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7201 {
7202 best_lowpc = lowpc;
7203 best_highpc = highpc;
7204 }
7205 }
4ae976d1 7206 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7207 best_lowpc + baseaddr)
7208 - baseaddr);
4ae976d1 7209 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7210 best_highpc + baseaddr)
7211 - baseaddr);
0018ea6f 7212
ae7754b2 7213 pst->end ();
0018ea6f 7214
ae640021 7215 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7216 {
7217 int i;
ae640021 7218 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7219
7220 /* Fill in 'dependencies' here; we fill in 'users' in a
7221 post-pass. */
7222 pst->number_of_dependencies = len;
a9342b62 7223 pst->dependencies
84685904 7224 = per_bfd->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7225 for (i = 0; i < len; ++i)
7226 {
7227 pst->dependencies[i]
7228 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7229 }
0018ea6f 7230
ae640021 7231 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7232 }
7233
7234 /* Get the list of files included in the current compilation unit,
7235 and build a psymtab for each of them. */
7236 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7237
6f738b01
SM
7238 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7239 ", %d global, %d static syms",
7240 per_cu->is_debug_types ? "type" : "comp",
7241 sect_offset_str (per_cu->sect_off),
7242 paddress (gdbarch, pst->text_low (objfile)),
7243 paddress (gdbarch, pst->text_high (objfile)),
7244 (int) pst->global_psymbols.size (),
7245 (int) pst->static_psymbols.size ());
0018ea6f
DE
7246}
7247
7248/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7249 Process compilation unit THIS_CU for a psymtab. */
7250
7251static void
ab432490
SM
7252process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7253 dwarf2_per_objfile *per_objfile,
135f5437 7254 bool want_partial_unit,
b93601f3 7255 enum language pretend_language)
0018ea6f
DE
7256{
7257 /* If this compilation unit was already read in, free the
7258 cached copy in order to read it in again. This is
7259 necessary because we skipped some symbols when we first
7260 read in the compilation unit (see load_partial_dies).
7261 This problem could be avoided, but the benefit is unclear. */
7188ed02 7262 per_objfile->remove_cu (this_cu);
0018ea6f 7263
2e671100 7264 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7265
58990295
TV
7266 switch (reader.comp_unit_die->tag)
7267 {
7268 case DW_TAG_compile_unit:
7269 this_cu->unit_type = DW_UT_compile;
7270 break;
7271 case DW_TAG_partial_unit:
7272 this_cu->unit_type = DW_UT_partial;
7273 break;
e77b0004
TV
7274 case DW_TAG_type_unit:
7275 this_cu->unit_type = DW_UT_type;
7276 break;
58990295 7277 default:
702cf3f5
AB
7278 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7279 dwarf_tag_name (reader.comp_unit_die->tag),
7280 sect_offset_str (reader.cu->per_cu->sect_off),
7281 objfile_name (per_objfile->objfile));
58990295
TV
7282 }
7283
c0ab21c2 7284 if (reader.dummy_p)
f1902523 7285 {
c0ab21c2 7286 /* Nothing. */
f1902523 7287 }
c0ab21c2 7288 else if (this_cu->is_debug_types)
3e225074
TT
7289 build_type_psymtabs_reader (&reader, reader.info_ptr,
7290 reader.comp_unit_die);
135f5437
TT
7291 else if (want_partial_unit
7292 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7293 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7294 reader.comp_unit_die,
c0ab21c2 7295 pretend_language);
0018ea6f 7296
7188ed02 7297 this_cu->lang = reader.cu->language;
58990295 7298
0018ea6f 7299 /* Age out any secondary CUs. */
7188ed02 7300 per_objfile->age_comp_units ();
0018ea6f 7301}
f4dc4d17
DE
7302
7303/* Reader function for build_type_psymtabs. */
7304
7305static void
7306build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7307 const gdb_byte *info_ptr,
3e225074 7308 struct die_info *type_unit_die)
f4dc4d17 7309{
976ca316 7310 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7311 struct dwarf2_cu *cu = reader->cu;
7312 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7313 struct signatured_type *sig_type;
f4dc4d17
DE
7314 struct type_unit_group *tu_group;
7315 struct attribute *attr;
7316 struct partial_die_info *first_die;
7317 CORE_ADDR lowpc, highpc;
891813be 7318 dwarf2_psymtab *pst;
f4dc4d17 7319
0186c6a7
DE
7320 gdb_assert (per_cu->is_debug_types);
7321 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7322
3e225074 7323 if (! type_unit_die->has_children)
f4dc4d17
DE
7324 return;
7325
052c8bb8 7326 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7327 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7328
df07e2c7 7329 if (tu_group->tus == nullptr)
a8b3b8e9 7330 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7331 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7332
7333 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7334 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7335 pst->anonymous = true;
f4dc4d17
DE
7336
7337 first_die = load_partial_dies (reader, info_ptr, 1);
7338
7339 lowpc = (CORE_ADDR) -1;
7340 highpc = (CORE_ADDR) 0;
7341 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7342
ae7754b2 7343 pst->end ();
f4dc4d17
DE
7344}
7345
73051182
DE
7346/* Struct used to sort TUs by their abbreviation table offset. */
7347
7348struct tu_abbrev_offset
7349{
b2bdb8cf
SM
7350 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7351 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7352 {}
7353
7354 signatured_type *sig_type;
73051182
DE
7355 sect_offset abbrev_offset;
7356};
7357
b8efb248 7358/* Helper routine for build_type_psymtabs, passed to std::sort. */
73051182 7359
484cf504
TT
7360static bool
7361sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7362 const struct tu_abbrev_offset &b)
73051182 7363{
484cf504 7364 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7365}
7366
7367/* Efficiently read all the type units.
73051182
DE
7368
7369 The efficiency is because we sort TUs by the abbrev table they use and
7370 only read each abbrev table once. In one program there are 200K TUs
7371 sharing 8K abbrev tables.
7372
7373 The main purpose of this function is to support building the
5989a64e 7374 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7375 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7376 can collapse the search space by grouping them by stmt_list.
7377 The savings can be significant, in the same program from above the 200K TUs
7378 share 8K stmt_list tables.
7379
7380 FUNC is expected to call get_type_unit_group, which will create the
7381 struct type_unit_group if necessary and add it to
5989a64e 7382 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7383
7384static void
b8efb248 7385build_type_psymtabs (dwarf2_per_objfile *per_objfile)
73051182 7386{
976ca316 7387 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7388 abbrev_table_up abbrev_table;
73051182 7389 sect_offset abbrev_offset;
73051182
DE
7390
7391 /* It's up to the caller to not call us multiple times. */
976ca316 7392 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7393
91eea9cc 7394 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
73051182
DE
7395 return;
7396
7397 /* TUs typically share abbrev tables, and there can be way more TUs than
7398 abbrev tables. Sort by abbrev table to reduce the number of times we
7399 read each abbrev table in.
7400 Alternatives are to punt or to maintain a cache of abbrev tables.
7401 This is simpler and efficient enough for now.
7402
7403 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7404 symtab to use). Typically TUs with the same abbrev offset have the same
7405 stmt_list value too so in practice this should work well.
7406
7407 The basic algorithm here is:
7408
7409 sort TUs by abbrev table
7410 for each TU with same abbrev table:
7411 read abbrev table if first user
7412 read TU top level DIE
7413 [IWBN if DWO skeletons had DW_AT_stmt_list]
7414 call FUNC */
7415
6f738b01 7416 dwarf_read_debug_printf ("Building type unit groups ...");
73051182 7417
91eea9cc 7418 /* Sort in a separate table to maintain the order of all_comp_units
73051182 7419 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7420 std::vector<tu_abbrev_offset> sorted_by_abbrev;
91eea9cc 7421 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
b2bdb8cf 7422
91eea9cc
TT
7423 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7424 {
7425 if (cu->is_debug_types)
7426 {
7427 auto sig_type = static_cast<signatured_type *> (cu.get ());
7428 sorted_by_abbrev.emplace_back
7429 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7430 sig_type->sect_off));
7431 }
7432 }
73051182 7433
484cf504
TT
7434 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7435 sort_tu_by_abbrev_offset);
73051182 7436
9c541725 7437 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7438
b2bdb8cf 7439 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7440 {
73051182
DE
7441 /* Switch to the next abbrev table if necessary. */
7442 if (abbrev_table == NULL
b2bdb8cf 7443 || tu.abbrev_offset != abbrev_offset)
73051182 7444 {
b2bdb8cf 7445 abbrev_offset = tu.abbrev_offset;
606decb2 7446 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7447 abbrev_table =
606decb2 7448 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7449 ++tu_stats->nr_uniq_abbrev_tables;
7450 }
7451
0d305d5c 7452 cutu_reader reader (tu.sig_type, per_objfile,
2e671100 7453 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7454 if (!reader.dummy_p)
7455 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7456 reader.comp_unit_die);
73051182 7457 }
6aa5f3a6 7458}
73051182 7459
6aa5f3a6
DE
7460/* Print collected type unit statistics. */
7461
7462static void
976ca316 7463print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7464{
976ca316 7465 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7466
6f738b01 7467 dwarf_read_debug_printf ("Type unit statistics:");
91eea9cc 7468 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
6f738b01
SM
7469 dwarf_read_debug_printf (" %d uniq abbrev tables",
7470 tu_stats->nr_uniq_abbrev_tables);
7471 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7472 tu_stats->nr_symtabs);
7473 dwarf_read_debug_printf (" %d symtab sharers",
7474 tu_stats->nr_symtab_sharers);
7475 dwarf_read_debug_printf (" %d type units without a stmt_list",
7476 tu_stats->nr_stmt_less_type_units);
7477 dwarf_read_debug_printf (" %d all_type_units reallocs",
7478 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7479}
7480
f4dc4d17
DE
7481/* Traversal function for build_type_psymtabs. */
7482
7483static int
7484build_type_psymtab_dependencies (void **slot, void *info)
7485{
976ca316 7486 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
84685904 7487 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 7488 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
0d305d5c 7489 dwarf2_psymtab *pst = tu_group->v.psymtab;
df07e2c7 7490 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7491 int i;
7492
7493 gdb_assert (len > 0);
0d305d5c 7494 gdb_assert (tu_group->type_unit_group_p ());
f4dc4d17
DE
7495
7496 pst->number_of_dependencies = len;
84685904 7497 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
df07e2c7 7498 for (i = 0; i < len; ++i)
f4dc4d17 7499 {
df07e2c7 7500 struct signatured_type *iter = tu_group->tus->at (i);
0d305d5c
TT
7501 gdb_assert (iter->is_debug_types);
7502 pst->dependencies[i] = iter->v.psymtab;
796a7ff8 7503 iter->type_unit_group = tu_group;
f4dc4d17
DE
7504 }
7505
df07e2c7
AB
7506 delete tu_group->tus;
7507 tu_group->tus = nullptr;
348e048f
DE
7508
7509 return 1;
7510}
7511
6aa5f3a6
DE
7512/* Traversal function for process_skeletonless_type_unit.
7513 Read a TU in a DWO file and build partial symbols for it. */
7514
7515static int
7516process_skeletonless_type_unit (void **slot, void *info)
7517{
7518 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7519 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7520 struct signatured_type find_entry, *entry;
7521
7522 /* If this TU doesn't exist in the global table, add it and read it in. */
7523
976ca316
SM
7524 if (per_objfile->per_bfd->signatured_types == NULL)
7525 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7526
7527 find_entry.signature = dwo_unit->signature;
976ca316 7528 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7529 &find_entry, INSERT);
6aa5f3a6
DE
7530 /* If we've already seen this type there's nothing to do. What's happening
7531 is we're doing our own version of comdat-folding here. */
7532 if (*slot != NULL)
7533 return 1;
7534
b8efb248 7535 /* This does the job that create_all_comp_units would have done for
6aa5f3a6 7536 this TU. */
976ca316
SM
7537 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7538 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7539 *slot = entry;
7540
b8efb248 7541 /* This does the job that build_type_psymtabs would have done. */
0d305d5c 7542 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7543 if (!reader.dummy_p)
7544 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7545 reader.comp_unit_die);
6aa5f3a6
DE
7546
7547 return 1;
7548}
7549
7550/* Traversal function for process_skeletonless_type_units. */
7551
7552static int
7553process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7554{
7555 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7556
7557 if (dwo_file->tus != NULL)
b0b6a987
TT
7558 htab_traverse_noresize (dwo_file->tus.get (),
7559 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7560
7561 return 1;
7562}
7563
7564/* Scan all TUs of DWO files, verifying we've processed them.
7565 This is needed in case a TU was emitted without its skeleton.
7566 Note: This can't be done until we know what all the DWO files are. */
7567
7568static void
976ca316 7569process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7570{
7571 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7572 if (get_dwp_file (per_objfile) == NULL
7573 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7574 {
976ca316 7575 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7576 process_dwo_file_for_skeletonless_type_units,
976ca316 7577 per_objfile);
6aa5f3a6 7578 }
348e048f
DE
7579}
7580
ed2dc618 7581/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7582
7583static void
976ca316 7584set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7585{
0d305d5c 7586 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7587 {
891813be 7588 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7589
36586728
TT
7590 if (pst == NULL)
7591 continue;
7592
b76e467d 7593 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7594 {
7595 /* Set the 'user' field only if it is not already set. */
7596 if (pst->dependencies[j]->user == NULL)
7597 pst->dependencies[j]->user = pst;
7598 }
7599 }
7600}
7601
93311388
DE
7602/* Build the partial symbol table by doing a quick pass through the
7603 .debug_info and .debug_abbrev sections. */
72bf9492 7604
93311388 7605static void
976ca316 7606dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 7607{
976ca316 7608 struct objfile *objfile = per_objfile->objfile;
84685904 7609 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
93311388 7610
6f738b01
SM
7611 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7612 objfile_name (objfile));
45cfd468 7613
76935768 7614 scoped_restore restore_reading_psyms
bab287cd 7615 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
98bfdba5 7616
84685904 7617 per_bfd->info.read (objfile);
91c24f0a 7618
93311388
DE
7619 /* Any cached compilation units will be linked by the per-objfile
7620 read_in_chain. Make sure to free them when we're done. */
976ca316 7621 free_cached_comp_units freer (per_objfile);
72bf9492 7622
976ca316 7623 create_all_comp_units (per_objfile);
b8efb248 7624 build_type_psymtabs (per_objfile);
c906108c 7625
60606b2c
TT
7626 /* Create a temporary address map on a temporary obstack. We later
7627 copy this to the final obstack. */
8268c778 7628 auto_obstack temp_obstack;
791afaa2
TT
7629
7630 scoped_restore save_psymtabs_addrmap
84685904 7631 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
791afaa2 7632 addrmap_create_mutable (&temp_obstack));
72bf9492 7633
0d305d5c 7634 for (const auto &per_cu : per_bfd->all_comp_units)
3d5afab3
TV
7635 {
7636 if (per_cu->v.psymtab != NULL)
7637 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7638 continue;
0d305d5c 7639 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
ab432490 7640 language_minimal);
3d5afab3 7641 }
ff013f42 7642
6aa5f3a6 7643 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 7644 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
7645
7646 /* Now that all TUs have been processed we can fill in the dependencies. */
84685904 7647 if (per_bfd->type_unit_groups != NULL)
6aa5f3a6 7648 {
84685904 7649 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
976ca316 7650 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
7651 }
7652
6f738b01 7653 if (dwarf_read_debug > 0)
976ca316 7654 print_tu_stats (per_objfile);
6aa5f3a6 7655
976ca316 7656 set_partial_user (per_objfile);
95554aad 7657
84685904
TT
7658 per_bfd->partial_symtabs->psymtabs_addrmap
7659 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7660 per_bfd->partial_symtabs->obstack ());
791afaa2
TT
7661 /* At this point we want to keep the address map. */
7662 save_psymtabs_addrmap.release ();
ff013f42 7663
6f738b01
SM
7664 dwarf_read_debug_printf ("Done building psymtabs of %s",
7665 objfile_name (objfile));
ae038cb0
DJ
7666}
7667
dee91e82
DE
7668/* Load the partial DIEs for a secondary CU into memory.
7669 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7670
dee91e82 7671static void
ab432490 7672load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
7673 dwarf2_per_objfile *per_objfile,
7674 dwarf2_cu *existing_cu)
dee91e82 7675{
2e671100 7676 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
7677
7678 if (!reader.dummy_p)
7679 {
7680 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7681 language_minimal);
7682
7683 /* Check if comp unit has_children.
7684 If so, read the rest of the partial symbols from this comp unit.
7685 If not, there's no more debug_info for this comp unit. */
3e225074 7686 if (reader.comp_unit_die->has_children)
c0ab21c2 7687 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7688
7689 reader.keep ();
c0ab21c2 7690 }
ae038cb0
DJ
7691}
7692
ae038cb0 7693static void
976ca316 7694read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 7695 struct dwarf2_section_info *section,
f1902523 7696 struct dwarf2_section_info *abbrev_section,
b8efb248
TT
7697 unsigned int is_dwz,
7698 htab_up &types_htab,
7699 rcuh_kind section_kind)
ae038cb0 7700{
d521ce57 7701 const gdb_byte *info_ptr;
976ca316 7702 struct objfile *objfile = per_objfile->objfile;
be391dca 7703
6f738b01
SM
7704 dwarf_read_debug_printf ("Reading %s for %s",
7705 section->get_name (),
7706 section->get_file_name ());
bf6af496 7707
96b79293 7708 section->read (objfile);
ae038cb0 7709
36586728 7710 info_ptr = section->buffer;
6e70227d 7711
36586728 7712 while (info_ptr < section->buffer + section->size)
ae038cb0 7713 {
0d305d5c 7714 std::unique_ptr<dwarf2_per_cu_data> this_cu;
ae038cb0 7715
9c541725 7716 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7717
f1902523 7718 comp_unit_head cu_header;
976ca316 7719 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 7720 abbrev_section, info_ptr,
b8efb248 7721 section_kind);
ae038cb0
DJ
7722
7723 /* Save the compilation unit for later lookup. */
f1902523 7724 if (cu_header.unit_type != DW_UT_type)
976ca316 7725 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
7726 else
7727 {
b8efb248
TT
7728 if (types_htab == nullptr)
7729 types_htab = allocate_signatured_type_table ();
7730
976ca316 7731 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b8efb248 7732 signatured_type *sig_ptr = sig_type.get ();
f1902523
JK
7733 sig_type->signature = cu_header.signature;
7734 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
0d305d5c 7735 this_cu = std::move (sig_type);
b8efb248
TT
7736
7737 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7738 gdb_assert (slot != nullptr);
7739 if (*slot != nullptr)
7740 complaint (_("debug type entry at offset %s is duplicate to"
7741 " the entry at offset %s, signature %s"),
7742 sect_offset_str (sect_off),
7743 sect_offset_str (sig_ptr->sect_off),
7744 hex_string (sig_ptr->signature));
7745 *slot = sig_ptr;
f1902523
JK
7746 }
7747 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7748 this_cu->sect_off = sect_off;
f1902523 7749 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7750 this_cu->is_dwz = is_dwz;
8a0459fd 7751 this_cu->section = section;
ae038cb0 7752
ae038cb0 7753 info_ptr = info_ptr + this_cu->length;
0d305d5c 7754 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
ae038cb0 7755 }
36586728
TT
7756}
7757
7758/* Create a list of all compilation units in OBJFILE.
7759 This is only done for -readnow and building partial symtabs. */
7760
7761static void
976ca316 7762create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 7763{
b8efb248
TT
7764 htab_up types_htab;
7765
976ca316 7766 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
b8efb248
TT
7767 &per_objfile->per_bfd->abbrev, 0,
7768 types_htab, rcuh_kind::COMPILE);
7769 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7770 read_comp_units_from_section (per_objfile, &section,
7771 &per_objfile->per_bfd->abbrev, 0,
7772 types_htab, rcuh_kind::TYPE);
36586728 7773
976ca316 7774 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 7775 if (dwz != NULL)
b8efb248
TT
7776 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7777 types_htab, rcuh_kind::COMPILE);
7778
7779 per_objfile->per_bfd->signatured_types = std::move (types_htab);
c906108c
SS
7780}
7781
5734ee8b 7782/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7783 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7784 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7785 DW_AT_ranges). See the comments of add_partial_subprogram on how
7786 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7787
72bf9492
DJ
7788static void
7789scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7790 CORE_ADDR *highpc, int set_addrmap,
7791 struct dwarf2_cu *cu)
c906108c 7792{
72bf9492 7793 struct partial_die_info *pdi;
c906108c 7794
91c24f0a
DC
7795 /* Now, march along the PDI's, descending into ones which have
7796 interesting children but skipping the children of the other ones,
7797 until we reach the end of the compilation unit. */
c906108c 7798
72bf9492 7799 pdi = first_die;
91c24f0a 7800
72bf9492
DJ
7801 while (pdi != NULL)
7802 {
52356b79 7803 pdi->fixup (cu);
c906108c 7804
f55ee35c 7805 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7806 children, so we need to look at them. Ditto for anonymous
7807 enums. */
933c6fe4 7808
7d00ffec 7809 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7810 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7811 || pdi->tag == DW_TAG_imported_unit
7812 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7813 {
72bf9492 7814 switch (pdi->tag)
c906108c
SS
7815 {
7816 case DW_TAG_subprogram:
b1dc1806 7817 case DW_TAG_inlined_subroutine:
cdc07690 7818 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
7819 if (cu->language == language_cplus)
7820 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7821 set_addrmap, cu);
c906108c 7822 break;
72929c62 7823 case DW_TAG_constant:
c906108c
SS
7824 case DW_TAG_variable:
7825 case DW_TAG_typedef:
91c24f0a 7826 case DW_TAG_union_type:
317d2668
TV
7827 if (!pdi->is_declaration
7828 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 7829 {
72bf9492 7830 add_partial_symbol (pdi, cu);
63d06c5c
DC
7831 }
7832 break;
c906108c 7833 case DW_TAG_class_type:
680b30c7 7834 case DW_TAG_interface_type:
c906108c 7835 case DW_TAG_structure_type:
72bf9492 7836 if (!pdi->is_declaration)
c906108c 7837 {
72bf9492 7838 add_partial_symbol (pdi, cu);
c906108c 7839 }
b7fee5a3
KS
7840 if ((cu->language == language_rust
7841 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7842 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7843 set_addrmap, cu);
c906108c 7844 break;
91c24f0a 7845 case DW_TAG_enumeration_type:
72bf9492
DJ
7846 if (!pdi->is_declaration)
7847 add_partial_enumeration (pdi, cu);
c906108c
SS
7848 break;
7849 case DW_TAG_base_type:
dda83cd7 7850 case DW_TAG_subrange_type:
c906108c 7851 /* File scope base type definitions are added to the partial
dda83cd7 7852 symbol table. */
72bf9492 7853 add_partial_symbol (pdi, cu);
c906108c 7854 break;
d9fa45fe 7855 case DW_TAG_namespace:
cdc07690 7856 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7857 break;
5d7cb8df 7858 case DW_TAG_module:
59c35742
AB
7859 if (!pdi->is_declaration)
7860 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7861 break;
95554aad
TT
7862 case DW_TAG_imported_unit:
7863 {
7864 struct dwarf2_per_cu_data *per_cu;
7865
f4dc4d17
DE
7866 /* For now we don't handle imported units in type units. */
7867 if (cu->per_cu->is_debug_types)
7868 {
7869 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7870 " supported in type units [in module %s]"),
5e22e966 7871 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
7872 }
7873
e3b94546 7874 per_cu = dwarf2_find_containing_comp_unit
5e22e966 7875 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
7876
7877 /* Go read the partial unit, if needed. */
7878 if (per_cu->v.psymtab == NULL)
ab432490
SM
7879 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7880 cu->language);
95554aad 7881
ae640021 7882 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7883 }
7884 break;
74921315
KS
7885 case DW_TAG_imported_declaration:
7886 add_partial_symbol (pdi, cu);
7887 break;
c906108c
SS
7888 default:
7889 break;
7890 }
7891 }
7892
72bf9492
DJ
7893 /* If the die has a sibling, skip to the sibling. */
7894
7895 pdi = pdi->die_sibling;
7896 }
7897}
7898
7899/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7900
72bf9492 7901 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7902 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7903 Enumerators are an exception; they use the scope of their parent
7904 enumeration type, i.e. the name of the enumeration type is not
7905 prepended to the enumerator.
91c24f0a 7906
72bf9492
DJ
7907 There are two complexities. One is DW_AT_specification; in this
7908 case "parent" means the parent of the target of the specification,
7909 instead of the direct parent of the DIE. The other is compilers
7910 which do not emit DW_TAG_namespace; in this case we try to guess
7911 the fully qualified name of structure types from their members'
7912 linkage names. This must be done using the DIE's children rather
7913 than the children of any DW_AT_specification target. We only need
7914 to do this for structures at the top level, i.e. if the target of
7915 any DW_AT_specification (if any; otherwise the DIE itself) does not
7916 have a parent. */
7917
7918/* Compute the scope prefix associated with PDI's parent, in
7919 compilation unit CU. The result will be allocated on CU's
7920 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7921 field. NULL is returned if no prefix is necessary. */
15d034d0 7922static const char *
72bf9492
DJ
7923partial_die_parent_scope (struct partial_die_info *pdi,
7924 struct dwarf2_cu *cu)
7925{
15d034d0 7926 const char *grandparent_scope;
72bf9492 7927 struct partial_die_info *parent, *real_pdi;
91c24f0a 7928
72bf9492
DJ
7929 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7930 then this means the parent of the specification DIE. */
7931
7932 real_pdi = pdi;
72bf9492 7933 while (real_pdi->has_specification)
fb816e8b 7934 {
122cf0f2
AB
7935 auto res = find_partial_die (real_pdi->spec_offset,
7936 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
7937 real_pdi = res.pdi;
7938 cu = res.cu;
7939 }
72bf9492
DJ
7940
7941 parent = real_pdi->die_parent;
7942 if (parent == NULL)
7943 return NULL;
7944
7945 if (parent->scope_set)
7946 return parent->scope;
7947
52356b79 7948 parent->fixup (cu);
72bf9492 7949
10b3939b 7950 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 7951
acebe513
UW
7952 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7953 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7954 Work around this problem here. */
7955 if (cu->language == language_cplus
6e70227d 7956 && parent->tag == DW_TAG_namespace
7d00ffec 7957 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
7958 && grandparent_scope == NULL)
7959 {
7960 parent->scope = NULL;
7961 parent->scope_set = 1;
7962 return NULL;
7963 }
7964
0a4b0913 7965 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
7966 if (pdi->tag == DW_TAG_enumerator)
7967 /* Enumerators should not get the name of the enumeration as a prefix. */
7968 parent->scope = grandparent_scope;
7969 else if (parent->tag == DW_TAG_namespace
f55ee35c 7970 || parent->tag == DW_TAG_module
72bf9492
DJ
7971 || parent->tag == DW_TAG_structure_type
7972 || parent->tag == DW_TAG_class_type
680b30c7 7973 || parent->tag == DW_TAG_interface_type
ceeb3d5a 7974 || parent->tag == DW_TAG_union_type
0a4b0913
AB
7975 || parent->tag == DW_TAG_enumeration_type
7976 || (cu->language == language_fortran
7977 && parent->tag == DW_TAG_subprogram
7978 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
7979 {
7980 if (grandparent_scope == NULL)
7d00ffec 7981 parent->scope = parent->name (cu);
72bf9492 7982 else
3e43a32a
MS
7983 parent->scope = typename_concat (&cu->comp_unit_obstack,
7984 grandparent_scope,
7d00ffec 7985 parent->name (cu), 0, cu);
72bf9492 7986 }
72bf9492
DJ
7987 else
7988 {
7989 /* FIXME drow/2004-04-01: What should we be doing with
7990 function-local names? For partial symbols, we should probably be
7991 ignoring them. */
fa9c3fa0
TT
7992 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7993 dwarf_tag_name (parent->tag),
7994 sect_offset_str (pdi->sect_off));
72bf9492 7995 parent->scope = grandparent_scope;
c906108c
SS
7996 }
7997
72bf9492
DJ
7998 parent->scope_set = 1;
7999 return parent->scope;
8000}
8001
8002/* Return the fully scoped name associated with PDI, from compilation unit
8003 CU. The result will be allocated with malloc. */
4568ecf9 8004
43816ebc 8005static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8006partial_die_full_name (struct partial_die_info *pdi,
8007 struct dwarf2_cu *cu)
8008{
15d034d0 8009 const char *parent_scope;
72bf9492 8010
98bfdba5
PA
8011 /* If this is a template instantiation, we can not work out the
8012 template arguments from partial DIEs. So, unfortunately, we have
8013 to go through the full DIEs. At least any work we do building
8014 types here will be reused if full symbols are loaded later. */
8015 if (pdi->has_template_arguments)
8016 {
52356b79 8017 pdi->fixup (cu);
98bfdba5 8018
7d00ffec 8019 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8020 {
8021 struct die_info *die;
8022 struct attribute attr;
8023 struct dwarf2_cu *ref_cu = cu;
8024
b64f50a1 8025 /* DW_FORM_ref_addr is using section offset. */
b4069958 8026 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8027 attr.form = DW_FORM_ref_addr;
9c541725 8028 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8029 die = follow_die_ref (NULL, &attr, &ref_cu);
8030
43816ebc 8031 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8032 }
8033 }
8034
72bf9492
DJ
8035 parent_scope = partial_die_parent_scope (pdi, cu);
8036 if (parent_scope == NULL)
8037 return NULL;
8038 else
43816ebc 8039 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8040 pdi->name (cu),
8041 0, cu));
c906108c
SS
8042}
8043
8044static void
72bf9492 8045add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8046{
976ca316
SM
8047 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8048 struct objfile *objfile = per_objfile->objfile;
08feed99 8049 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8050 CORE_ADDR addr = 0;
15d034d0 8051 const char *actual_name = NULL;
e142c38c
DJ
8052 CORE_ADDR baseaddr;
8053
b3b3bada 8054 baseaddr = objfile->text_section_offset ();
c906108c 8055
43816ebc
TT
8056 gdb::unique_xmalloc_ptr<char> built_actual_name
8057 = partial_die_full_name (pdi, cu);
15d034d0 8058 if (built_actual_name != NULL)
43816ebc 8059 actual_name = built_actual_name.get ();
63d06c5c 8060
72bf9492 8061 if (actual_name == NULL)
7d00ffec 8062 actual_name = pdi->name (cu);
72bf9492 8063
76e288d1
TT
8064 partial_symbol psymbol;
8065 memset (&psymbol, 0, sizeof (psymbol));
8066 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 8067 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
8068
8069 /* The code below indicates that the psymbol should be installed by
8070 setting this. */
8071 gdb::optional<psymbol_placement> where;
8072
c906108c
SS
8073 switch (pdi->tag)
8074 {
b1dc1806 8075 case DW_TAG_inlined_subroutine:
c906108c 8076 case DW_TAG_subprogram:
79748972
TT
8077 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8078 - baseaddr);
0a4b0913
AB
8079 if (pdi->is_external
8080 || cu->language == language_ada
8081 || (cu->language == language_fortran
8082 && pdi->die_parent != NULL
8083 && pdi->die_parent->tag == DW_TAG_subprogram))
8084 {
dda83cd7
SM
8085 /* Normally, only "external" DIEs are part of the global scope.
8086 But in Ada and Fortran, we want to be able to access nested
8087 procedures globally. So all Ada and Fortran subprograms are
8088 stored in the global scope. */
76e288d1 8089 where = psymbol_placement::GLOBAL;
c906108c
SS
8090 }
8091 else
76e288d1
TT
8092 where = psymbol_placement::STATIC;
8093
8094 psymbol.domain = VAR_DOMAIN;
8095 psymbol.aclass = LOC_BLOCK;
a52d653e 8096 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 8097 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8098
8099 if (pdi->main_subprogram && actual_name != NULL)
8100 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8101 break;
72929c62 8102 case DW_TAG_constant:
76e288d1
TT
8103 psymbol.domain = VAR_DOMAIN;
8104 psymbol.aclass = LOC_STATIC;
8105 where = (pdi->is_external
8106 ? psymbol_placement::GLOBAL
8107 : psymbol_placement::STATIC);
72929c62 8108 break;
c906108c 8109 case DW_TAG_variable:
95554aad
TT
8110 if (pdi->d.locdesc)
8111 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8112
95554aad 8113 if (pdi->d.locdesc
caac4577 8114 && addr == 0
976ca316 8115 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8116 {
8117 /* A global or static variable may also have been stripped
8118 out by the linker if unused, in which case its address
8119 will be nullified; do not add such variables into partial
8120 symbol table then. */
8121 }
8122 else if (pdi->is_external)
c906108c
SS
8123 {
8124 /* Global Variable.
8125 Don't enter into the minimal symbol tables as there is
8126 a minimal symbol table entry from the ELF symbols already.
8127 Enter into partial symbol table if it has a location
8128 descriptor or a type.
8129 If the location descriptor is missing, new_symbol will create
8130 a LOC_UNRESOLVED symbol, the address of the variable will then
8131 be determined from the minimal symbol table whenever the variable
8132 is referenced.
8133 The address for the partial symbol table entry is not
8134 used by GDB, but it comes in handy for debugging partial symbol
8135 table building. */
8136
95554aad 8137 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8138 {
8139 psymbol.domain = VAR_DOMAIN;
8140 psymbol.aclass = LOC_STATIC;
a52d653e 8141 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8142 psymbol.ginfo.value.address = addr;
8143 where = psymbol_placement::GLOBAL;
8144 }
c906108c
SS
8145 }
8146 else
8147 {
ff908ebf
AW
8148 int has_loc = pdi->d.locdesc != NULL;
8149
8150 /* Static Variable. Skip symbols whose value we cannot know (those
8151 without location descriptors or constant values). */
8152 if (!has_loc && !pdi->has_const_value)
43816ebc 8153 return;
ff908ebf 8154
76e288d1
TT
8155 psymbol.domain = VAR_DOMAIN;
8156 psymbol.aclass = LOC_STATIC;
a52d653e 8157 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8158 if (has_loc)
8159 psymbol.ginfo.value.address = addr;
8160 where = psymbol_placement::STATIC;
c906108c
SS
8161 }
8162 break;
d8f62e84 8163 case DW_TAG_array_type:
c906108c
SS
8164 case DW_TAG_typedef:
8165 case DW_TAG_base_type:
a02abb62 8166 case DW_TAG_subrange_type:
76e288d1
TT
8167 psymbol.domain = VAR_DOMAIN;
8168 psymbol.aclass = LOC_TYPEDEF;
8169 where = psymbol_placement::STATIC;
c906108c 8170 break;
74921315 8171 case DW_TAG_imported_declaration:
72bf9492 8172 case DW_TAG_namespace:
76e288d1
TT
8173 psymbol.domain = VAR_DOMAIN;
8174 psymbol.aclass = LOC_TYPEDEF;
8175 where = psymbol_placement::GLOBAL;
72bf9492 8176 break;
530e8392 8177 case DW_TAG_module:
a5fd13a9 8178 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8179 available without any name. If so, we skip the module as it
8180 doesn't bring any value. */
a5fd13a9 8181 if (actual_name != nullptr)
76e288d1
TT
8182 {
8183 psymbol.domain = MODULE_DOMAIN;
8184 psymbol.aclass = LOC_TYPEDEF;
8185 where = psymbol_placement::GLOBAL;
8186 }
530e8392 8187 break;
c906108c 8188 case DW_TAG_class_type:
680b30c7 8189 case DW_TAG_interface_type:
c906108c
SS
8190 case DW_TAG_structure_type:
8191 case DW_TAG_union_type:
8192 case DW_TAG_enumeration_type:
fa4028e9 8193 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8194 about "Structure, Union, and Class Type Entries": "An incomplete
8195 structure, union or class type is represented by a structure,
8196 union or class entry that does not have a byte size attribute
8197 and that has a DW_AT_declaration attribute." */
fa4028e9 8198 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8199 return;
fa4028e9 8200
63d06c5c
DC
8201 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8202 static vs. global. */
76e288d1
TT
8203 psymbol.domain = STRUCT_DOMAIN;
8204 psymbol.aclass = LOC_TYPEDEF;
8205 where = (cu->language == language_cplus
8206 ? psymbol_placement::GLOBAL
8207 : psymbol_placement::STATIC);
c906108c
SS
8208 break;
8209 case DW_TAG_enumerator:
76e288d1
TT
8210 psymbol.domain = VAR_DOMAIN;
8211 psymbol.aclass = LOC_CONST;
8212 where = (cu->language == language_cplus
8213 ? psymbol_placement::GLOBAL
8214 : psymbol_placement::STATIC);
c906108c
SS
8215 break;
8216 default:
8217 break;
8218 }
76e288d1
TT
8219
8220 if (where.has_value ())
8221 {
f049a313
TT
8222 if (built_actual_name != nullptr)
8223 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8224 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8225 psymbol.ginfo.set_linkage_name (actual_name);
8226 else
8227 {
8228 psymbol.ginfo.set_demangled_name (actual_name,
8229 &objfile->objfile_obstack);
8230 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8231 }
7b249e47
TT
8232 cu->per_cu->v.psymtab->add_psymbol
8233 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8234 objfile);
76e288d1 8235 }
c906108c
SS
8236}
8237
5c4e30ca
DC
8238/* Read a partial die corresponding to a namespace; also, add a symbol
8239 corresponding to that namespace to the symbol table. NAMESPACE is
8240 the name of the enclosing namespace. */
91c24f0a 8241
72bf9492
DJ
8242static void
8243add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8244 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8245 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8246{
72bf9492 8247 /* Add a symbol for the namespace. */
e7c27a73 8248
72bf9492 8249 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8250
8251 /* Now scan partial symbols in that namespace. */
8252
91c24f0a 8253 if (pdi->has_children)
cdc07690 8254 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8255}
8256
5d7cb8df
JK
8257/* Read a partial die corresponding to a Fortran module. */
8258
8259static void
8260add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8261 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8262{
530e8392
KB
8263 /* Add a symbol for the namespace. */
8264
8265 add_partial_symbol (pdi, cu);
8266
f55ee35c 8267 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8268
8269 if (pdi->has_children)
cdc07690 8270 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8271}
8272
b1dc1806
XR
8273/* Read a partial die corresponding to a subprogram or an inlined
8274 subprogram and create a partial symbol for that subprogram.
8275 When the CU language allows it, this routine also defines a partial
8276 symbol for each nested subprogram that this subprogram contains.
8277 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8278 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8279
cdc07690
YQ
8280 PDI may also be a lexical block, in which case we simply search
8281 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8282 Again, this is only performed when the CU language allows this
8283 type of definitions. */
8284
8285static void
8286add_partial_subprogram (struct partial_die_info *pdi,
8287 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8288 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8289{
b1dc1806 8290 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8291 {
8292 if (pdi->has_pc_info)
dda83cd7
SM
8293 {
8294 if (pdi->lowpc < *lowpc)
8295 *lowpc = pdi->lowpc;
8296 if (pdi->highpc > *highpc)
8297 *highpc = pdi->highpc;
cdc07690 8298 if (set_addrmap)
5734ee8b 8299 {
5e22e966 8300 struct objfile *objfile = cu->per_objfile->objfile;
84685904 8301 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 8302 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8303 CORE_ADDR baseaddr;
b926417a
TT
8304 CORE_ADDR this_highpc;
8305 CORE_ADDR this_lowpc;
5734ee8b 8306
b3b3bada 8307 baseaddr = objfile->text_section_offset ();
b926417a
TT
8308 this_lowpc
8309 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8310 pdi->lowpc + baseaddr)
8311 - baseaddr);
8312 this_highpc
8313 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8314 pdi->highpc + baseaddr)
8315 - baseaddr);
84685904 8316 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
b926417a 8317 this_lowpc, this_highpc - 1,
9291a0cd 8318 cu->per_cu->v.psymtab);
5734ee8b 8319 }
dda83cd7 8320 }
481860b3
GB
8321
8322 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8323 {
dda83cd7 8324 if (!pdi->is_declaration)
e8d05480
JB
8325 /* Ignore subprogram DIEs that do not have a name, they are
8326 illegal. Do not emit a complaint at this point, we will
8327 do so when we convert this psymtab into a symtab. */
7d00ffec 8328 if (pdi->name (cu))
e8d05480 8329 add_partial_symbol (pdi, cu);
dda83cd7 8330 }
bc30ff58 8331 }
6e70227d 8332
bc30ff58
JB
8333 if (! pdi->has_children)
8334 return;
8335
0a4b0913 8336 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8337 {
8338 pdi = pdi->die_child;
8339 while (pdi != NULL)
8340 {
52356b79 8341 pdi->fixup (cu);
bc30ff58 8342 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8343 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8344 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8345 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8346 pdi = pdi->die_sibling;
8347 }
8348 }
8349}
8350
91c24f0a
DC
8351/* Read a partial die corresponding to an enumeration type. */
8352
72bf9492
DJ
8353static void
8354add_partial_enumeration (struct partial_die_info *enum_pdi,
8355 struct dwarf2_cu *cu)
91c24f0a 8356{
72bf9492 8357 struct partial_die_info *pdi;
91c24f0a 8358
7d00ffec 8359 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8360 add_partial_symbol (enum_pdi, cu);
8361
8362 pdi = enum_pdi->die_child;
8363 while (pdi)
91c24f0a 8364 {
7d00ffec 8365 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8366 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8367 else
72bf9492
DJ
8368 add_partial_symbol (pdi, cu);
8369 pdi = pdi->die_sibling;
91c24f0a 8370 }
91c24f0a
DC
8371}
8372
6caca83c
CC
8373/* Return the initial uleb128 in the die at INFO_PTR. */
8374
8375static unsigned int
d521ce57 8376peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8377{
8378 unsigned int bytes_read;
8379
8380 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8381}
8382
685af9cd
TT
8383/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8384 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8385
4bb7a0a7
DJ
8386 Return the corresponding abbrev, or NULL if the number is zero (indicating
8387 an empty DIE). In either case *BYTES_READ will be set to the length of
8388 the initial number. */
8389
7c32eebb 8390static const struct abbrev_info *
685af9cd
TT
8391peek_die_abbrev (const die_reader_specs &reader,
8392 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8393{
685af9cd 8394 dwarf2_cu *cu = reader.cu;
0280fdcc 8395 bfd *abfd = reader.abfd;
685af9cd
TT
8396 unsigned int abbrev_number
8397 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8398
8399 if (abbrev_number == 0)
8400 return NULL;
8401
7c32eebb
TT
8402 const abbrev_info *abbrev
8403 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8404 if (!abbrev)
8405 {
422b9917 8406 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8407 " at offset %s [in module %s]"),
422b9917 8408 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8409 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8410 }
8411
8412 return abbrev;
8413}
8414
93311388
DE
8415/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8416 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8417 DIE. Any children of the skipped DIEs will also be skipped. */
8418
d521ce57
TT
8419static const gdb_byte *
8420skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8421{
4bb7a0a7
DJ
8422 while (1)
8423 {
685af9cd 8424 unsigned int bytes_read;
7c32eebb
TT
8425 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8426 &bytes_read);
685af9cd 8427
4bb7a0a7
DJ
8428 if (abbrev == NULL)
8429 return info_ptr + bytes_read;
8430 else
dee91e82 8431 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8432 }
8433}
8434
93311388
DE
8435/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8436 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8437 abbrev corresponding to that skipped uleb128 should be passed in
8438 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8439 children. */
8440
d521ce57
TT
8441static const gdb_byte *
8442skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8443 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8444{
8445 unsigned int bytes_read;
8446 struct attribute attr;
dee91e82
DE
8447 bfd *abfd = reader->abfd;
8448 struct dwarf2_cu *cu = reader->cu;
d521ce57 8449 const gdb_byte *buffer = reader->buffer;
f664829e 8450 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8451 unsigned int form, i;
8452
8453 for (i = 0; i < abbrev->num_attrs; i++)
8454 {
8455 /* The only abbrev we care about is DW_AT_sibling. */
8456 if (abbrev->attrs[i].name == DW_AT_sibling)
8457 {
7a5f294d 8458 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8459 if (attr.form == DW_FORM_ref_addr)
b98664d3 8460 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8461 else
b9502d3f 8462 {
0826b30a 8463 sect_offset off = attr.get_ref_die_offset ();
9c541725 8464 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8465
8466 if (sibling_ptr < info_ptr)
b98664d3 8467 complaint (_("DW_AT_sibling points backwards"));
22869d73 8468 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8469 reader->die_section->overflow_complaint ();
b9502d3f
WN
8470 else
8471 return sibling_ptr;
8472 }
4bb7a0a7
DJ
8473 }
8474
8475 /* If it isn't DW_AT_sibling, skip this attribute. */
8476 form = abbrev->attrs[i].form;
8477 skip_attribute:
8478 switch (form)
8479 {
4bb7a0a7 8480 case DW_FORM_ref_addr:
ae411497
TT
8481 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8482 and later it is offset sized. */
8483 if (cu->header.version == 2)
8484 info_ptr += cu->header.addr_size;
8485 else
8486 info_ptr += cu->header.offset_size;
8487 break;
36586728
TT
8488 case DW_FORM_GNU_ref_alt:
8489 info_ptr += cu->header.offset_size;
8490 break;
ae411497 8491 case DW_FORM_addr:
4bb7a0a7
DJ
8492 info_ptr += cu->header.addr_size;
8493 break;
8494 case DW_FORM_data1:
8495 case DW_FORM_ref1:
8496 case DW_FORM_flag:
8fe0f950 8497 case DW_FORM_strx1:
4bb7a0a7
DJ
8498 info_ptr += 1;
8499 break;
2dc7f7b3 8500 case DW_FORM_flag_present:
43988095 8501 case DW_FORM_implicit_const:
2dc7f7b3 8502 break;
4bb7a0a7
DJ
8503 case DW_FORM_data2:
8504 case DW_FORM_ref2:
8fe0f950 8505 case DW_FORM_strx2:
4bb7a0a7
DJ
8506 info_ptr += 2;
8507 break;
8fe0f950
AT
8508 case DW_FORM_strx3:
8509 info_ptr += 3;
8510 break;
4bb7a0a7
DJ
8511 case DW_FORM_data4:
8512 case DW_FORM_ref4:
8fe0f950 8513 case DW_FORM_strx4:
4bb7a0a7
DJ
8514 info_ptr += 4;
8515 break;
8516 case DW_FORM_data8:
8517 case DW_FORM_ref8:
55f1336d 8518 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8519 info_ptr += 8;
8520 break;
0224619f
JK
8521 case DW_FORM_data16:
8522 info_ptr += 16;
8523 break;
4bb7a0a7 8524 case DW_FORM_string:
9b1c24c8 8525 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8526 info_ptr += bytes_read;
8527 break;
2dc7f7b3 8528 case DW_FORM_sec_offset:
4bb7a0a7 8529 case DW_FORM_strp:
36586728 8530 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8531 info_ptr += cu->header.offset_size;
8532 break;
2dc7f7b3 8533 case DW_FORM_exprloc:
4bb7a0a7
DJ
8534 case DW_FORM_block:
8535 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8536 info_ptr += bytes_read;
8537 break;
8538 case DW_FORM_block1:
8539 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8540 break;
8541 case DW_FORM_block2:
8542 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8543 break;
8544 case DW_FORM_block4:
8545 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8546 break;
336d760d 8547 case DW_FORM_addrx:
cf532bd1 8548 case DW_FORM_strx:
4bb7a0a7
DJ
8549 case DW_FORM_sdata:
8550 case DW_FORM_udata:
8551 case DW_FORM_ref_udata:
3019eac3
DE
8552 case DW_FORM_GNU_addr_index:
8553 case DW_FORM_GNU_str_index:
18a8505e 8554 case DW_FORM_rnglistx:
41144253 8555 case DW_FORM_loclistx:
d521ce57 8556 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8557 break;
8558 case DW_FORM_indirect:
8559 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8560 info_ptr += bytes_read;
8561 /* We need to continue parsing from here, so just go back to
8562 the top. */
8563 goto skip_attribute;
8564
8565 default:
3e43a32a
MS
8566 error (_("Dwarf Error: Cannot handle %s "
8567 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8568 dwarf_form_name (form),
8569 bfd_get_filename (abfd));
8570 }
8571 }
8572
8573 if (abbrev->has_children)
dee91e82 8574 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8575 else
8576 return info_ptr;
8577}
8578
93311388 8579/* Locate ORIG_PDI's sibling.
dee91e82 8580 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8581
d521ce57 8582static const gdb_byte *
dee91e82
DE
8583locate_pdi_sibling (const struct die_reader_specs *reader,
8584 struct partial_die_info *orig_pdi,
d521ce57 8585 const gdb_byte *info_ptr)
91c24f0a
DC
8586{
8587 /* Do we know the sibling already? */
72bf9492 8588
91c24f0a
DC
8589 if (orig_pdi->sibling)
8590 return orig_pdi->sibling;
8591
8592 /* Are there any children to deal with? */
8593
8594 if (!orig_pdi->has_children)
8595 return info_ptr;
8596
4bb7a0a7 8597 /* Skip the children the long way. */
91c24f0a 8598
dee91e82 8599 return skip_children (reader, info_ptr);
91c24f0a
DC
8600}
8601
257e7a09 8602/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8603 not NULL. */
c906108c 8604
891813be
TT
8605void
8606dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8607{
976ca316 8608 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8609
976ca316 8610 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8611
077cbab2
TT
8612 /* If this psymtab is constructed from a debug-only objfile, the
8613 has_section_at_zero flag will not necessarily be correct. We
8614 can get the correct value for this flag by looking at the data
8615 associated with the (presumably stripped) associated objfile. */
8616 if (objfile->separate_debug_objfile_backlink)
c906108c 8617 {
976ca316 8618 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8619 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8620
976ca316
SM
8621 per_objfile->per_bfd->has_section_at_zero
8622 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8623 }
98bfdba5 8624
8566b89b 8625 expand_psymtab (objfile);
95554aad 8626
976ca316 8627 process_cu_includes (per_objfile);
c906108c 8628}
9cdd5dbd
DE
8629\f
8630/* Reading in full CUs. */
c906108c 8631
10b3939b
DJ
8632/* Add PER_CU to the queue. */
8633
8634static void
120ce1b5
SM
8635queue_comp_unit (dwarf2_per_cu_data *per_cu,
8636 dwarf2_per_objfile *per_objfile,
95554aad 8637 enum language pretend_language)
10b3939b 8638{
10b3939b 8639 per_cu->queued = 1;
08ac5771
SM
8640
8641 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8642 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8643}
8644
616c069a
SM
8645/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8646
89e63ee4
DE
8647 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8648 dependency.
69d751e3 8649
616c069a
SM
8650 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8651 DIEs, false otherwise.
8652
8653 Explanation: there is an invariant that if a CU is queued for expansion
8654 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8655 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8656 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8657 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8658 invariant is respected.
8659
8660 The caller is therefore not required to load the CU's DIEs (we return false)
8661 if:
8662
8663 - the CU is already expanded, and therefore does not get enqueued
8664 - the CU gets enqueued for expansion, but its DIEs are already loaded
8665
8666 Note that the caller should not use this function's return value as an
8667 indicator of whether the CU's DIEs are loaded right now, it should check
8668 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
8669
8670static int
89e63ee4 8671maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8672 dwarf2_per_cu_data *per_cu,
8673 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8674 enum language pretend_language)
8675{
8676 /* We may arrive here during partial symbol reading, if we need full
8677 DIEs to process an unusual case (e.g. template arguments). Do
8678 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8679 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 8680 {
7188ed02
SM
8681 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8682
8683 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
8684 return 1;
8685 return 0;
8686 }
8687
8688 /* Mark the dependence relation so that we don't flush PER_CU
8689 too early. */
89e63ee4
DE
8690 if (dependent_cu != NULL)
8691 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8692
8693 /* If it's already on the queue, we have nothing to do. */
8694 if (per_cu->queued)
de53369b
SM
8695 {
8696 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8697 loaded. */
8698 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
8699
8700 /* If the CU is queued for expansion, it should not already be
8701 expanded. */
8702 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8703
8704 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
8705 return 0;
8706 }
0907af0c 8707
616c069a
SM
8708 bool queued = false;
8709 if (!per_objfile->symtab_set_p (per_cu))
8710 {
8711 /* Add it to the queue. */
8712 queue_comp_unit (per_cu, per_objfile, pretend_language);
8713 queued = true;
8714 }
8715
0907af0c
DE
8716 /* If the compilation unit is already loaded, just mark it as
8717 used. */
7188ed02
SM
8718 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8719 if (cu != nullptr)
616c069a 8720 cu->last_used = 0;
0907af0c 8721
616c069a
SM
8722 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8723 and the DIEs are not already loaded. */
8724 return queued && cu == nullptr;
0907af0c
DE
8725}
8726
10b3939b
DJ
8727/* Process the queue. */
8728
8729static void
976ca316 8730process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 8731{
6f738b01
SM
8732 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8733 objfile_name (per_objfile->objfile));
45cfd468 8734
03dd20cc
DJ
8735 /* The queue starts out with one item, but following a DIE reference
8736 may load a new CU, adding it to the end of the queue. */
08ac5771 8737 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 8738 {
08ac5771 8739 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 8740 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 8741
976ca316 8742 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 8743 {
976ca316 8744 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 8745
7188ed02
SM
8746 /* Skip dummy CUs. */
8747 if (cu != nullptr)
73be47f5 8748 {
7188ed02
SM
8749 unsigned int debug_print_threshold;
8750 char buf[100];
8751
8752 if (per_cu->is_debug_types)
8753 {
8754 struct signatured_type *sig_type =
8755 (struct signatured_type *) per_cu;
8756
8757 sprintf (buf, "TU %s at offset %s",
8758 hex_string (sig_type->signature),
8759 sect_offset_str (per_cu->sect_off));
8760 /* There can be 100s of TUs.
8761 Only print them in verbose mode. */
8762 debug_print_threshold = 2;
8763 }
8764 else
8765 {
8766 sprintf (buf, "CU at offset %s",
8767 sect_offset_str (per_cu->sect_off));
8768 debug_print_threshold = 1;
8769 }
247f5c4f 8770
7188ed02 8771 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8772 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 8773
7188ed02
SM
8774 if (per_cu->is_debug_types)
8775 process_full_type_unit (cu, item.pretend_language);
8776 else
8777 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 8778
7188ed02 8779 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8780 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 8781 }
f4dc4d17 8782 }
10b3939b 8783
7188ed02 8784 per_cu->queued = 0;
08ac5771 8785 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
8786 }
8787
6f738b01
SM
8788 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8789 objfile_name (per_objfile->objfile));
10b3939b
DJ
8790}
8791
10b3939b
DJ
8792/* Read in full symbols for PST, and anything it depends on. */
8793
8566b89b
TT
8794void
8795dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8796{
af758d11 8797 gdb_assert (!readin_p (objfile));
95554aad 8798
17ee85fc
TT
8799 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8800 free_cached_comp_units freer (per_objfile);
48993951 8801 expand_dependencies (objfile);
aaa75496 8802
97a1449a 8803 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 8804 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
8805}
8806
af758d11
SM
8807/* See psympriv.h. */
8808
8809bool
8810dwarf2_psymtab::readin_p (struct objfile *objfile) const
8811{
8812 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8813 return per_objfile->symtab_set_p (per_cu_data);
8814}
8815
8816/* See psympriv.h. */
8817
8818compunit_symtab *
8819dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8820{
8821 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8822 return per_objfile->get_symtab (per_cu_data);
8823}
8824
dee91e82
DE
8825/* Trivial hash function for die_info: the hash value of a DIE
8826 is its offset in .debug_info for this objfile. */
10b3939b 8827
dee91e82
DE
8828static hashval_t
8829die_hash (const void *item)
10b3939b 8830{
9a3c8263 8831 const struct die_info *die = (const struct die_info *) item;
6502dd73 8832
9c541725 8833 return to_underlying (die->sect_off);
dee91e82 8834}
63d06c5c 8835
dee91e82
DE
8836/* Trivial comparison function for die_info structures: two DIEs
8837 are equal if they have the same offset. */
98bfdba5 8838
dee91e82
DE
8839static int
8840die_eq (const void *item_lhs, const void *item_rhs)
8841{
9a3c8263
SM
8842 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8843 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8844
9c541725 8845 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8846}
c906108c 8847
4a636814
SM
8848/* Load the DIEs associated with PER_CU into memory.
8849
8850 In some cases, the caller, while reading partial symbols, will need to load
8851 the full symbols for the CU for some reason. It will already have a
8852 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8853 rather than creating a new one. */
c906108c 8854
dee91e82 8855static void
ab432490
SM
8856load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8857 dwarf2_per_objfile *per_objfile,
4a636814 8858 dwarf2_cu *existing_cu,
c0ab21c2
TT
8859 bool skip_partial,
8860 enum language pretend_language)
dee91e82 8861{
c0ab21c2
TT
8862 gdb_assert (! this_cu->is_debug_types);
8863
7188ed02 8864 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
8865 if (reader.dummy_p)
8866 return;
8867
8868 struct dwarf2_cu *cu = reader.cu;
8869 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8870
dee91e82
DE
8871 gdb_assert (cu->die_hash == NULL);
8872 cu->die_hash =
8873 htab_create_alloc_ex (cu->header.length / 12,
8874 die_hash,
8875 die_eq,
8876 NULL,
8877 &cu->comp_unit_obstack,
8878 hashtab_obstack_allocate,
8879 dummy_obstack_deallocate);
e142c38c 8880
3e225074 8881 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8882 reader.comp_unit_die->child
8883 = read_die_and_siblings (&reader, reader.info_ptr,
8884 &info_ptr, reader.comp_unit_die);
8885 cu->dies = reader.comp_unit_die;
dee91e82 8886 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8887
8888 /* We try not to read any attributes in this function, because not
9cdd5dbd 8889 all CUs needed for references have been loaded yet, and symbol
10b3939b 8890 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8891 or we won't be able to build types correctly.
8892 Similarly, if we do not read the producer, we can not apply
8893 producer-specific interpretation. */
c0ab21c2 8894 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8895
8896 reader.keep ();
10b3939b
DJ
8897}
8898
3da10d80
KS
8899/* Add a DIE to the delayed physname list. */
8900
8901static void
8902add_to_method_list (struct type *type, int fnfield_index, int index,
8903 const char *name, struct die_info *die,
8904 struct dwarf2_cu *cu)
8905{
8906 struct delayed_method_info mi;
8907 mi.type = type;
8908 mi.fnfield_index = fnfield_index;
8909 mi.index = index;
8910 mi.name = name;
8911 mi.die = die;
c89b44cd 8912 cu->method_list.push_back (mi);
3da10d80
KS
8913}
8914
3693fdb3
PA
8915/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8916 "const" / "volatile". If so, decrements LEN by the length of the
8917 modifier and return true. Otherwise return false. */
8918
8919template<size_t N>
8920static bool
8921check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8922{
8923 size_t mod_len = sizeof (mod) - 1;
8924 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8925 {
8926 len -= mod_len;
8927 return true;
8928 }
8929 return false;
8930}
8931
3da10d80
KS
8932/* Compute the physnames of any methods on the CU's method list.
8933
8934 The computation of method physnames is delayed in order to avoid the
8935 (bad) condition that one of the method's formal parameters is of an as yet
8936 incomplete type. */
8937
8938static void
8939compute_delayed_physnames (struct dwarf2_cu *cu)
8940{
3693fdb3 8941 /* Only C++ delays computing physnames. */
c89b44cd 8942 if (cu->method_list.empty ())
3693fdb3
PA
8943 return;
8944 gdb_assert (cu->language == language_cplus);
8945
52941706 8946 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8947 {
1d06ead6 8948 const char *physname;
3da10d80 8949 struct fn_fieldlist *fn_flp
c89b44cd
TT
8950 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8951 physname = dwarf2_physname (mi.name, mi.die, cu);
8952 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8953 = physname ? physname : "";
3693fdb3
PA
8954
8955 /* Since there's no tag to indicate whether a method is a
8956 const/volatile overload, extract that information out of the
8957 demangled name. */
8958 if (physname != NULL)
8959 {
8960 size_t len = strlen (physname);
8961
8962 while (1)
8963 {
8964 if (physname[len] == ')') /* shortcut */
8965 break;
8966 else if (check_modifier (physname, len, " const"))
c89b44cd 8967 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 8968 else if (check_modifier (physname, len, " volatile"))
c89b44cd 8969 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
8970 else
8971 break;
8972 }
8973 }
3da10d80 8974 }
c89b44cd
TT
8975
8976 /* The list is no longer needed. */
8977 cu->method_list.clear ();
3da10d80
KS
8978}
8979
a766d390
DE
8980/* Go objects should be embedded in a DW_TAG_module DIE,
8981 and it's not clear if/how imported objects will appear.
8982 To keep Go support simple until that's worked out,
8983 go back through what we've read and create something usable.
8984 We could do this while processing each DIE, and feels kinda cleaner,
8985 but that way is more invasive.
8986 This is to, for example, allow the user to type "p var" or "b main"
8987 without having to specify the package name, and allow lookups
8988 of module.object to work in contexts that use the expression
8989 parser. */
8990
8991static void
8992fixup_go_packaging (struct dwarf2_cu *cu)
8993{
421d1616 8994 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
8995 struct pending *list;
8996 int i;
8997
c24bdb02 8998 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
8999 list != NULL;
9000 list = list->next)
a766d390
DE
9001 {
9002 for (i = 0; i < list->nsyms; ++i)
9003 {
9004 struct symbol *sym = list->symbol[i];
9005
c1b5c1eb 9006 if (sym->language () == language_go
a766d390
DE
9007 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9008 {
421d1616
TT
9009 gdb::unique_xmalloc_ptr<char> this_package_name
9010 (go_symbol_package_name (sym));
a766d390
DE
9011
9012 if (this_package_name == NULL)
9013 continue;
9014 if (package_name == NULL)
421d1616 9015 package_name = std::move (this_package_name);
a766d390
DE
9016 else
9017 {
5e22e966 9018 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9019 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9020 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9021 (symbol_symtab (sym) != NULL
9022 ? symtab_to_filename_for_display
9023 (symbol_symtab (sym))
e3b94546 9024 : objfile_name (objfile)),
421d1616 9025 this_package_name.get (), package_name.get ());
a766d390
DE
9026 }
9027 }
9028 }
9029 }
9030
9031 if (package_name != NULL)
9032 {
5e22e966 9033 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9034 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9035 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9036 saved_package_name);
a766d390
DE
9037 struct symbol *sym;
9038
8c14c3a3 9039 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9040 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9041 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9042 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9043 e.g., "main" finds the "main" module and not C's main(). */
9044 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9045 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9046 SYMBOL_TYPE (sym) = type;
9047
c24bdb02 9048 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9049 }
9050}
9051
c9317f21
TT
9052/* Allocate a fully-qualified name consisting of the two parts on the
9053 obstack. */
9054
9055static const char *
9056rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9057{
9058 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9059}
9060
9c6a1327
TT
9061/* A helper that allocates a variant part to attach to a Rust enum
9062 type. OBSTACK is where the results should be allocated. TYPE is
9063 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9064 discriminant. It must be the index of one of the fields of TYPE,
9065 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9066 DEFAULT_INDEX is the index of the default field; or -1 if there is
9067 no default. RANGES is indexed by "effective" field number (the
9068 field index, but omitting the discriminant and default fields) and
9069 must hold the discriminant values used by the variants. Note that
9070 RANGES must have a lifetime at least as long as OBSTACK -- either
9071 already allocated on it, or static. */
c9317f21 9072
9c6a1327
TT
9073static void
9074alloc_rust_variant (struct obstack *obstack, struct type *type,
9075 int discriminant_index, int default_index,
9076 gdb::array_view<discriminant_range> ranges)
9077{
57d02173
TT
9078 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9079 gdb_assert (discriminant_index == -1
9080 || (discriminant_index >= 0
9081 && discriminant_index < type->num_fields ()));
c9317f21 9082 gdb_assert (default_index == -1
1f704f76 9083 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9084
9c6a1327 9085 /* We have one variant for each non-discriminant field. */
57d02173
TT
9086 int n_variants = type->num_fields ();
9087 if (discriminant_index != -1)
9088 --n_variants;
c9317f21 9089
9c6a1327
TT
9090 variant *variants = new (obstack) variant[n_variants];
9091 int var_idx = 0;
9092 int range_idx = 0;
1f704f76 9093 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9094 {
9095 if (i == discriminant_index)
9096 continue;
c9317f21 9097
9c6a1327
TT
9098 variants[var_idx].first_field = i;
9099 variants[var_idx].last_field = i + 1;
9100
9101 /* The default field does not need a range, but other fields do.
9102 We skipped the discriminant above. */
9103 if (i != default_index)
9104 {
9105 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9106 ++range_idx;
9107 }
c9317f21 9108
9c6a1327
TT
9109 ++var_idx;
9110 }
9111
9112 gdb_assert (range_idx == ranges.size ());
9113 gdb_assert (var_idx == n_variants);
9114
9115 variant_part *part = new (obstack) variant_part;
9116 part->discriminant_index = discriminant_index;
57d02173
TT
9117 /* If there is no discriminant, then whether it is signed is of no
9118 consequence. */
9119 part->is_unsigned
9120 = (discriminant_index == -1
9121 ? false
c6d940a9 9122 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9123 part->variants = gdb::array_view<variant> (variants, n_variants);
9124
9125 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9126 gdb::array_view<variant_part> *prop_value
9127 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9128
9c6a1327 9129 struct dynamic_prop prop;
8c2e4e06 9130 prop.set_variant_parts (prop_value);
9c6a1327 9131
5c54719c 9132 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9133}
9134
9135/* Some versions of rustc emitted enums in an unusual way.
9136
9137 Ordinary enums were emitted as unions. The first element of each
9138 structure in the union was named "RUST$ENUM$DISR". This element
9139 held the discriminant.
9140
9141 These versions of Rust also implemented the "non-zero"
9142 optimization. When the enum had two values, and one is empty and
9143 the other holds a pointer that cannot be zero, the pointer is used
9144 as the discriminant, with a zero value meaning the empty variant.
9145 Here, the union's first member is of the form
9146 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9147 where the fieldnos are the indices of the fields that should be
9148 traversed in order to find the field (which may be several fields deep)
9149 and the variantname is the name of the variant of the case when the
9150 field is zero.
9151
9152 This function recognizes whether TYPE is of one of these forms,
9153 and, if so, smashes it to be a variant type. */
9154
9155static void
9156quirk_rust_enum (struct type *type, struct objfile *objfile)
9157{
78134374 9158 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9159
9160 /* We don't need to deal with empty enums. */
1f704f76 9161 if (type->num_fields () == 0)
c9317f21
TT
9162 return;
9163
9164#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9165 if (type->num_fields () == 1
c9317f21
TT
9166 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9167 {
9168 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9169
9170 /* Decode the field name to find the offset of the
9171 discriminant. */
9172 ULONGEST bit_offset = 0;
940da03e 9173 struct type *field_type = type->field (0).type ();
c9317f21
TT
9174 while (name[0] >= '0' && name[0] <= '9')
9175 {
9176 char *tail;
9177 unsigned long index = strtoul (name, &tail, 10);
9178 name = tail;
9179 if (*name != '$'
1f704f76 9180 || index >= field_type->num_fields ()
c9317f21
TT
9181 || (TYPE_FIELD_LOC_KIND (field_type, index)
9182 != FIELD_LOC_KIND_BITPOS))
9183 {
b98664d3 9184 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9185 "[in module %s]"),
9186 TYPE_FIELD_NAME (type, 0),
9187 objfile_name (objfile));
9188 return;
9189 }
9190 ++name;
9191
9192 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9193 field_type = field_type->field (index).type ();
c9317f21
TT
9194 }
9195
9c6a1327
TT
9196 /* Smash this type to be a structure type. We have to do this
9197 because the type has already been recorded. */
67607e24 9198 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9199 type->set_num_fields (3);
9c6a1327 9200 /* Save the field we care about. */
ceacbf6e 9201 struct field saved_field = type->field (0);
3cabb6b0
SM
9202 type->set_fields
9203 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9204
9c6a1327 9205 /* Put the discriminant at index 0. */
5d14b6e5 9206 type->field (0).set_type (field_type);
9c6a1327
TT
9207 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9208 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9209 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9210
9211 /* The order of fields doesn't really matter, so put the real
9212 field at index 1 and the data-less field at index 2. */
ceacbf6e 9213 type->field (1) = saved_field;
9c6a1327 9214 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9215 = rust_last_path_segment (type->field (1).type ()->name ());
9216 type->field (1).type ()->set_name
7d93a1e0 9217 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9218 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9219
9220 const char *dataless_name
7d93a1e0 9221 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9222 name);
9223 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9224 dataless_name);
5d14b6e5 9225 type->field (2).set_type (dataless_type);
c9317f21
TT
9226 /* NAME points into the original discriminant name, which
9227 already has the correct lifetime. */
9c6a1327 9228 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9229 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9230
9c6a1327
TT
9231 /* Indicate that this is a variant type. */
9232 static discriminant_range ranges[1] = { { 0, 0 } };
9233 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9234 }
77c2dba3
TT
9235 /* A union with a single anonymous field is probably an old-style
9236 univariant enum. */
1f704f76 9237 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9238 {
c9317f21
TT
9239 /* Smash this type to be a structure type. We have to do this
9240 because the type has already been recorded. */
67607e24 9241 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9242
940da03e 9243 struct type *field_type = type->field (0).type ();
c9317f21 9244 const char *variant_name
7d93a1e0 9245 = rust_last_path_segment (field_type->name ());
9c6a1327 9246 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9247 field_type->set_name
9248 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9249 type->name (), variant_name));
57d02173
TT
9250
9251 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9252 }
9253 else
9254 {
9255 struct type *disr_type = nullptr;
1f704f76 9256 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9257 {
940da03e 9258 disr_type = type->field (i).type ();
c9317f21 9259
78134374 9260 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9261 {
9262 /* All fields of a true enum will be structs. */
9263 return;
9264 }
1f704f76 9265 else if (disr_type->num_fields () == 0)
c9317f21
TT
9266 {
9267 /* Could be data-less variant, so keep going. */
a037790e 9268 disr_type = nullptr;
c9317f21
TT
9269 }
9270 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9271 "RUST$ENUM$DISR") != 0)
9272 {
9273 /* Not a Rust enum. */
9274 return;
9275 }
9276 else
9277 {
9278 /* Found one. */
9279 break;
9280 }
9281 }
9282
9283 /* If we got here without a discriminant, then it's probably
9284 just a union. */
9285 if (disr_type == nullptr)
9286 return;
9287
9288 /* Smash this type to be a structure type. We have to do this
9289 because the type has already been recorded. */
67607e24 9290 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9291
9c6a1327 9292 /* Make space for the discriminant field. */
ceacbf6e 9293 struct field *disr_field = &disr_type->field (0);
9c6a1327 9294 field *new_fields
1f704f76 9295 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9296 * sizeof (struct field)));
80fc5e77 9297 memcpy (new_fields + 1, type->fields (),
1f704f76 9298 type->num_fields () * sizeof (struct field));
3cabb6b0 9299 type->set_fields (new_fields);
1f704f76 9300 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9301
9302 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9303 type->field (0) = *disr_field;
9c6a1327
TT
9304 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9305 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9306
9307 /* We need a way to find the correct discriminant given a
9308 variant name. For convenience we build a map here. */
b6cdac4b 9309 struct type *enum_type = disr_field->type ();
c9317f21 9310 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9311 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9312 {
9313 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9314 {
9315 const char *name
9316 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9317 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9318 }
9319 }
9320
1f704f76 9321 int n_fields = type->num_fields ();
9c6a1327
TT
9322 /* We don't need a range entry for the discriminant, but we do
9323 need one for every other field, as there is no default
9324 variant. */
9325 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9326 discriminant_range,
9327 n_fields - 1);
c9317f21
TT
9328 /* Skip the discriminant here. */
9329 for (int i = 1; i < n_fields; ++i)
9330 {
9331 /* Find the final word in the name of this variant's type.
9332 That name can be used to look up the correct
9333 discriminant. */
9334 const char *variant_name
940da03e 9335 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9336
9337 auto iter = discriminant_map.find (variant_name);
9338 if (iter != discriminant_map.end ())
9c6a1327 9339 {
57d02173
TT
9340 ranges[i - 1].low = iter->second;
9341 ranges[i - 1].high = iter->second;
9c6a1327 9342 }
c9317f21 9343
57d02173
TT
9344 /* In Rust, each element should have the size of the
9345 enclosing enum. */
9346 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9347
bedda9ac 9348 /* Remove the discriminant field, if it exists. */
940da03e 9349 struct type *sub_type = type->field (i).type ();
1f704f76 9350 if (sub_type->num_fields () > 0)
bedda9ac 9351 {
5e33d5f4 9352 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9353 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9354 }
9c6a1327 9355 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9356 sub_type->set_name
9357 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9358 type->name (), variant_name));
c9317f21 9359 }
9c6a1327
TT
9360
9361 /* Indicate that this is a variant type. */
a1520ad8 9362 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9363 gdb::array_view<discriminant_range> (ranges,
9364 n_fields - 1));
c9317f21
TT
9365 }
9366}
9367
9368/* Rewrite some Rust unions to be structures with variants parts. */
9369
9370static void
9371rust_union_quirks (struct dwarf2_cu *cu)
9372{
9373 gdb_assert (cu->language == language_rust);
52941706 9374 for (type *type_ : cu->rust_unions)
5e22e966 9375 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9376 /* We don't need this any more. */
9377 cu->rust_unions.clear ();
c9317f21
TT
9378}
9379
8adb8487
TT
9380/* See read.h. */
9381
9382type_unit_group_unshareable *
9383dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9384{
9385 auto iter = this->m_type_units.find (tu_group);
9386 if (iter != this->m_type_units.end ())
9387 return iter->second.get ();
9388
9389 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9390 type_unit_group_unshareable *result = uniq.get ();
9391 this->m_type_units[tu_group] = std::move (uniq);
9392 return result;
9393}
9394
e286671b
TT
9395struct type *
9396dwarf2_per_objfile::get_type_for_signatured_type
9397 (signatured_type *sig_type) const
9398{
9399 auto iter = this->m_type_map.find (sig_type);
9400 if (iter == this->m_type_map.end ())
9401 return nullptr;
9402
9403 return iter->second;
9404}
9405
9406void dwarf2_per_objfile::set_type_for_signatured_type
9407 (signatured_type *sig_type, struct type *type)
9408{
9409 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9410
9411 this->m_type_map[sig_type] = type;
9412}
9413
95554aad
TT
9414/* A helper function for computing the list of all symbol tables
9415 included by PER_CU. */
9416
9417static void
4c39bc03 9418recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9419 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9420 dwarf2_per_cu_data *per_cu,
9421 dwarf2_per_objfile *per_objfile,
43f3e411 9422 struct compunit_symtab *immediate_parent)
95554aad 9423{
af758d11 9424 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9425 if (*slot != NULL)
9426 {
9427 /* This inclusion and its children have been processed. */
9428 return;
9429 }
9430
9431 *slot = per_cu;
af758d11 9432
95554aad 9433 /* Only add a CU if it has a symbol table. */
43182c09 9434 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9435 if (cust != NULL)
ec94af83
DE
9436 {
9437 /* If this is a type unit only add its symbol table if we haven't
9438 seen it yet (type unit per_cu's can share symtabs). */
9439 if (per_cu->is_debug_types)
9440 {
43f3e411 9441 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9442 if (*slot == NULL)
9443 {
43f3e411 9444 *slot = cust;
4c39bc03 9445 result->push_back (cust);
43f3e411
DE
9446 if (cust->user == NULL)
9447 cust->user = immediate_parent;
ec94af83
DE
9448 }
9449 }
9450 else
f9125b6c 9451 {
4c39bc03 9452 result->push_back (cust);
43f3e411
DE
9453 if (cust->user == NULL)
9454 cust->user = immediate_parent;
f9125b6c 9455 }
ec94af83 9456 }
95554aad 9457
ae640021
AB
9458 if (!per_cu->imported_symtabs_empty ())
9459 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9460 {
9461 recursively_compute_inclusions (result, all_children,
43182c09
SM
9462 all_type_symtabs, ptr, per_objfile,
9463 cust);
ae640021 9464 }
95554aad
TT
9465}
9466
43f3e411 9467/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9468 PER_CU. */
9469
9470static void
43182c09
SM
9471compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9472 dwarf2_per_objfile *per_objfile)
95554aad 9473{
f4dc4d17
DE
9474 gdb_assert (! per_cu->is_debug_types);
9475
ae640021 9476 if (!per_cu->imported_symtabs_empty ())
95554aad 9477 {
ae640021 9478 int len;
4c39bc03 9479 std::vector<compunit_symtab *> result_symtabs;
43182c09 9480 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9481
9482 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9483 if (cust == NULL)
95554aad
TT
9484 return;
9485
280a9412
TT
9486 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9487 htab_eq_pointer,
9488 NULL, xcalloc, xfree));
9489 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9490 htab_eq_pointer,
9491 NULL, xcalloc, xfree));
95554aad 9492
ae640021 9493 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9494 {
280a9412
TT
9495 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9496 all_type_symtabs.get (), ptr,
9497 per_objfile, cust);
ec94af83 9498 }
95554aad 9499
ec94af83 9500 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9501 len = result_symtabs.size ();
43f3e411 9502 cust->includes
f6e649dd 9503 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9504 struct compunit_symtab *, len + 1);
4c39bc03
TT
9505 memcpy (cust->includes, result_symtabs.data (),
9506 len * sizeof (compunit_symtab *));
43f3e411 9507 cust->includes[len] = NULL;
95554aad
TT
9508 }
9509}
9510
9511/* Compute the 'includes' field for the symtabs of all the CUs we just
9512 read. */
9513
9514static void
976ca316 9515process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9516{
976ca316 9517 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9518 {
9519 if (! iter->is_debug_types)
976ca316 9520 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9521 }
95554aad 9522
976ca316 9523 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9524}
9525
8fc0b21d 9526/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9527 already been loaded into memory. */
9528
9529static void
8fc0b21d 9530process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9531{
976ca316
SM
9532 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9533 struct objfile *objfile = per_objfile->objfile;
08feed99 9534 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9535 CORE_ADDR lowpc, highpc;
43f3e411 9536 struct compunit_symtab *cust;
10b3939b 9537 CORE_ADDR baseaddr;
4359dff1 9538 struct block *static_block;
3e29f34a 9539 CORE_ADDR addr;
10b3939b 9540
b3b3bada 9541 baseaddr = objfile->text_section_offset ();
10b3939b 9542
c89b44cd
TT
9543 /* Clear the list here in case something was left over. */
9544 cu->method_list.clear ();
10b3939b 9545
95554aad
TT
9546 cu->language = pretend_language;
9547 cu->language_defn = language_def (cu->language);
9548
1c47ec3e
TV
9549 dwarf2_find_base_address (cu->dies, cu);
9550
702cf3f5
AB
9551 /* Before we start reading the top-level DIE, ensure it has a valid tag
9552 type. */
9553 switch (cu->dies->tag)
9554 {
9555 case DW_TAG_compile_unit:
9556 case DW_TAG_partial_unit:
9557 case DW_TAG_type_unit:
9558 break;
9559 default:
9560 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9561 dwarf_tag_name (cu->dies->tag),
9562 sect_offset_str (cu->per_cu->sect_off),
9563 objfile_name (per_objfile->objfile));
9564 }
9565
c906108c 9566 /* Do line number decoding in read_file_scope () */
10b3939b 9567 process_die (cu->dies, cu);
c906108c 9568
a766d390
DE
9569 /* For now fudge the Go package. */
9570 if (cu->language == language_go)
9571 fixup_go_packaging (cu);
9572
5f48f8f3 9573 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9574 should be complete, and it should now be safe to compute all of the
9575 physnames. */
9576 compute_delayed_physnames (cu);
3da10d80 9577
c9317f21
TT
9578 if (cu->language == language_rust)
9579 rust_union_quirks (cu);
9580
fae299cd
DC
9581 /* Some compilers don't define a DW_AT_high_pc attribute for the
9582 compilation unit. If the DW_AT_high_pc is missing, synthesize
9583 it, by scanning the DIE's below the compilation unit. */
10b3939b 9584 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9585
3e29f34a 9586 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9587 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9588
9589 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9590 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9591 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9592 addrmap to help ensure it has an accurate map of pc values belonging to
9593 this comp unit. */
9594 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9595
c24bdb02 9596 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9597 SECT_OFF_TEXT (objfile),
9598 0);
c906108c 9599
43f3e411 9600 if (cust != NULL)
c906108c 9601 {
df15bd07 9602 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9603
8be455d7
JK
9604 /* Set symtab language to language from DW_AT_language. If the
9605 compilation is from a C file generated by language preprocessors, do
9606 not set the language if it was already deduced by start_subfile. */
43f3e411 9607 if (!(cu->language == language_c
40e3ad0e 9608 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9609 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9610
9611 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9612 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9613 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9614 there were bugs in prologue debug info, fixed later in GCC-4.5
9615 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9616
9617 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9618 needed, it would be wrong due to missing DW_AT_producer there.
9619
9620 Still one can confuse GDB by using non-standard GCC compilation
9621 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9622 */
ab260dad 9623 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9624 cust->locations_valid = 1;
e0d00bc7
JK
9625
9626 if (gcc_4_minor >= 5)
43f3e411 9627 cust->epilogue_unwind_valid = 1;
96408a79 9628
43f3e411 9629 cust->call_site_htab = cu->call_site_htab;
c906108c 9630 }
9291a0cd 9631
976ca316 9632 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9633
95554aad 9634 /* Push it for inclusion processing later. */
976ca316 9635 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9636
9637 /* Not needed any more. */
c24bdb02 9638 cu->reset_builder ();
f4dc4d17 9639}
45cfd468 9640
8fc0b21d 9641/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9642 already been loaded into memory. */
9643
9644static void
8fc0b21d 9645process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9646 enum language pretend_language)
9647{
976ca316
SM
9648 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9649 struct objfile *objfile = per_objfile->objfile;
43f3e411 9650 struct compunit_symtab *cust;
0186c6a7
DE
9651 struct signatured_type *sig_type;
9652
8fc0b21d
SM
9653 gdb_assert (cu->per_cu->is_debug_types);
9654 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9655
c89b44cd
TT
9656 /* Clear the list here in case something was left over. */
9657 cu->method_list.clear ();
f4dc4d17 9658
f4dc4d17
DE
9659 cu->language = pretend_language;
9660 cu->language_defn = language_def (cu->language);
9661
9662 /* The symbol tables are set up in read_type_unit_scope. */
9663 process_die (cu->dies, cu);
9664
9665 /* For now fudge the Go package. */
9666 if (cu->language == language_go)
9667 fixup_go_packaging (cu);
9668
5f48f8f3 9669 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9670 should be complete, and it should now be safe to compute all of the
9671 physnames. */
9672 compute_delayed_physnames (cu);
f4dc4d17 9673
c9317f21
TT
9674 if (cu->language == language_rust)
9675 rust_union_quirks (cu);
9676
f4dc4d17
DE
9677 /* TUs share symbol tables.
9678 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9679 of it with end_expandable_symtab. Otherwise, complete the addition of
9680 this TU's symbols to the existing symtab. */
8adb8487 9681 type_unit_group_unshareable *tug_unshare =
976ca316 9682 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9683 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9684 {
c24bdb02
KS
9685 buildsym_compunit *builder = cu->get_builder ();
9686 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9687 tug_unshare->compunit_symtab = cust;
f4dc4d17 9688
43f3e411 9689 if (cust != NULL)
f4dc4d17
DE
9690 {
9691 /* Set symtab language to language from DW_AT_language. If the
9692 compilation is from a C file generated by language preprocessors,
9693 do not set the language if it was already deduced by
9694 start_subfile. */
43f3e411
DE
9695 if (!(cu->language == language_c
9696 && COMPUNIT_FILETABS (cust)->language != language_c))
9697 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9698 }
9699 }
9700 else
9701 {
c24bdb02 9702 cu->get_builder ()->augment_type_symtab ();
8adb8487 9703 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9704 }
9705
976ca316 9706 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9707
9708 /* Not needed any more. */
c24bdb02 9709 cu->reset_builder ();
c906108c
SS
9710}
9711
95554aad
TT
9712/* Process an imported unit DIE. */
9713
9714static void
9715process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9716{
9717 struct attribute *attr;
9718
f4dc4d17
DE
9719 /* For now we don't handle imported units in type units. */
9720 if (cu->per_cu->is_debug_types)
9721 {
9722 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9723 " supported in type units [in module %s]"),
5e22e966 9724 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9725 }
9726
95554aad
TT
9727 attr = dwarf2_attr (die, DW_AT_import, cu);
9728 if (attr != NULL)
9729 {
0826b30a 9730 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9731 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9732 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9733 dwarf2_per_cu_data *per_cu
ab432490 9734 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9735
58990295
TV
9736 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9737 into another compilation unit, at root level. Regard this as a hint,
9738 and ignore it. */
9739 if (die->parent && die->parent->parent == NULL
9740 && per_cu->unit_type == DW_UT_compile
9741 && per_cu->lang == language_cplus)
9742 return;
9743
69d751e3 9744 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 9745 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
9746 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9747 false, cu->language);
95554aad 9748
ae640021 9749 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9750 }
9751}
9752
4c8aa72d
PA
9753/* RAII object that represents a process_die scope: i.e.,
9754 starts/finishes processing a DIE. */
9755class process_die_scope
adde2bff 9756{
4c8aa72d
PA
9757public:
9758 process_die_scope (die_info *die, dwarf2_cu *cu)
9759 : m_die (die), m_cu (cu)
9760 {
9761 /* We should only be processing DIEs not already in process. */
9762 gdb_assert (!m_die->in_process);
9763 m_die->in_process = true;
9764 }
8c3cb9fa 9765
4c8aa72d
PA
9766 ~process_die_scope ()
9767 {
9768 m_die->in_process = false;
9769
9770 /* If we're done processing the DIE for the CU that owns the line
9771 header, we don't need the line header anymore. */
9772 if (m_cu->line_header_die_owner == m_die)
9773 {
9774 delete m_cu->line_header;
9775 m_cu->line_header = NULL;
9776 m_cu->line_header_die_owner = NULL;
9777 }
9778 }
9779
9780private:
9781 die_info *m_die;
9782 dwarf2_cu *m_cu;
9783};
adde2bff 9784
c906108c
SS
9785/* Process a die and its children. */
9786
9787static void
e7c27a73 9788process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9789{
4c8aa72d 9790 process_die_scope scope (die, cu);
adde2bff 9791
c906108c
SS
9792 switch (die->tag)
9793 {
9794 case DW_TAG_padding:
9795 break;
9796 case DW_TAG_compile_unit:
95554aad 9797 case DW_TAG_partial_unit:
e7c27a73 9798 read_file_scope (die, cu);
c906108c 9799 break;
348e048f
DE
9800 case DW_TAG_type_unit:
9801 read_type_unit_scope (die, cu);
9802 break;
c906108c 9803 case DW_TAG_subprogram:
0a4b0913
AB
9804 /* Nested subprograms in Fortran get a prefix. */
9805 if (cu->language == language_fortran
9806 && die->parent != NULL
9807 && die->parent->tag == DW_TAG_subprogram)
9808 cu->processing_has_namespace_info = true;
9809 /* Fall through. */
c906108c 9810 case DW_TAG_inlined_subroutine:
edb3359d 9811 read_func_scope (die, cu);
c906108c
SS
9812 break;
9813 case DW_TAG_lexical_block:
14898363
L
9814 case DW_TAG_try_block:
9815 case DW_TAG_catch_block:
e7c27a73 9816 read_lexical_block_scope (die, cu);
c906108c 9817 break;
216f72a1 9818 case DW_TAG_call_site:
96408a79
SA
9819 case DW_TAG_GNU_call_site:
9820 read_call_site_scope (die, cu);
9821 break;
c906108c 9822 case DW_TAG_class_type:
680b30c7 9823 case DW_TAG_interface_type:
c906108c
SS
9824 case DW_TAG_structure_type:
9825 case DW_TAG_union_type:
134d01f1 9826 process_structure_scope (die, cu);
c906108c
SS
9827 break;
9828 case DW_TAG_enumeration_type:
134d01f1 9829 process_enumeration_scope (die, cu);
c906108c 9830 break;
134d01f1 9831
f792889a
DJ
9832 /* These dies have a type, but processing them does not create
9833 a symbol or recurse to process the children. Therefore we can
9834 read them on-demand through read_type_die. */
c906108c 9835 case DW_TAG_subroutine_type:
72019c9c 9836 case DW_TAG_set_type:
c906108c 9837 case DW_TAG_pointer_type:
c906108c 9838 case DW_TAG_ptr_to_member_type:
c906108c 9839 case DW_TAG_reference_type:
4297a3f0 9840 case DW_TAG_rvalue_reference_type:
c906108c 9841 case DW_TAG_string_type:
c906108c 9842 break;
134d01f1 9843
d8f62e84
TT
9844 case DW_TAG_array_type:
9845 /* We only need to handle this case for Ada -- in other
9846 languages, it's normal for the compiler to emit a typedef
9847 instead. */
9848 if (cu->language != language_ada)
9849 break;
9850 /* FALLTHROUGH */
c906108c 9851 case DW_TAG_base_type:
a02abb62 9852 case DW_TAG_subrange_type:
cb249c71 9853 case DW_TAG_typedef:
134d01f1 9854 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 9855 DW_AT_name. */
f792889a 9856 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9857 break;
c906108c 9858 case DW_TAG_common_block:
e7c27a73 9859 read_common_block (die, cu);
c906108c
SS
9860 break;
9861 case DW_TAG_common_inclusion:
9862 break;
d9fa45fe 9863 case DW_TAG_namespace:
9068261f 9864 cu->processing_has_namespace_info = true;
e7c27a73 9865 read_namespace (die, cu);
d9fa45fe 9866 break;
5d7cb8df 9867 case DW_TAG_module:
9068261f 9868 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9869 read_module (die, cu);
9870 break;
d9fa45fe 9871 case DW_TAG_imported_declaration:
9068261f 9872 cu->processing_has_namespace_info = true;
74921315
KS
9873 if (read_namespace_alias (die, cu))
9874 break;
86a73007
TT
9875 /* The declaration is not a global namespace alias. */
9876 /* Fall through. */
d9fa45fe 9877 case DW_TAG_imported_module:
9068261f 9878 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9879 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9880 || cu->language != language_fortran))
b98664d3 9881 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9882 dwarf_tag_name (die->tag));
9883 read_import_statement (die, cu);
d9fa45fe 9884 break;
95554aad
TT
9885
9886 case DW_TAG_imported_unit:
9887 process_imported_unit_die (die, cu);
9888 break;
9889
71a3c369
TT
9890 case DW_TAG_variable:
9891 read_variable (die, cu);
9892 break;
9893
c906108c 9894 default:
e7c27a73 9895 new_symbol (die, NULL, cu);
c906108c
SS
9896 break;
9897 }
9898}
ca69b9e6
DE
9899\f
9900/* DWARF name computation. */
c906108c 9901
94af9270
KS
9902/* A helper function for dwarf2_compute_name which determines whether DIE
9903 needs to have the name of the scope prepended to the name listed in the
9904 die. */
9905
9906static int
9907die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9908{
1c809c68
TT
9909 struct attribute *attr;
9910
94af9270
KS
9911 switch (die->tag)
9912 {
9913 case DW_TAG_namespace:
9914 case DW_TAG_typedef:
9915 case DW_TAG_class_type:
9916 case DW_TAG_interface_type:
9917 case DW_TAG_structure_type:
9918 case DW_TAG_union_type:
9919 case DW_TAG_enumeration_type:
9920 case DW_TAG_enumerator:
9921 case DW_TAG_subprogram:
08a76f8a 9922 case DW_TAG_inlined_subroutine:
94af9270 9923 case DW_TAG_member:
74921315 9924 case DW_TAG_imported_declaration:
94af9270
KS
9925 return 1;
9926
9927 case DW_TAG_variable:
c2b0a229 9928 case DW_TAG_constant:
94af9270
KS
9929 /* We only need to prefix "globally" visible variables. These include
9930 any variable marked with DW_AT_external or any variable that
9931 lives in a namespace. [Variables in anonymous namespaces
9932 require prefixing, but they are not DW_AT_external.] */
9933
9934 if (dwarf2_attr (die, DW_AT_specification, cu))
9935 {
9936 struct dwarf2_cu *spec_cu = cu;
9a619af0 9937
94af9270
KS
9938 return die_needs_namespace (die_specification (die, &spec_cu),
9939 spec_cu);
9940 }
9941
1c809c68 9942 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9943 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9944 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9945 return 0;
9946 /* A variable in a lexical block of some kind does not need a
9947 namespace, even though in C++ such variables may be external
9948 and have a mangled name. */
9949 if (die->parent->tag == DW_TAG_lexical_block
9950 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9951 || die->parent->tag == DW_TAG_catch_block
9952 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9953 return 0;
9954 return 1;
94af9270
KS
9955
9956 default:
9957 return 0;
9958 }
9959}
9960
73b9be8b
KS
9961/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9962 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9963 defined for the given DIE. */
9964
9965static struct attribute *
9966dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9967{
9968 struct attribute *attr;
9969
9970 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9971 if (attr == NULL)
9972 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9973
9974 return attr;
9975}
9976
9977/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9978 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9979 defined for the given DIE. */
9980
9981static const char *
9982dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9983{
9984 const char *linkage_name;
9985
9986 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9987 if (linkage_name == NULL)
9988 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9989
787de330
TT
9990 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9991 See https://github.com/rust-lang/rust/issues/32925. */
9992 if (cu->language == language_rust && linkage_name != NULL
9993 && strchr (linkage_name, '{') != NULL)
9994 linkage_name = NULL;
9995
73b9be8b
KS
9996 return linkage_name;
9997}
9998
94af9270 9999/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10000 compute the physname for the object, which include a method's:
9c37b5ae 10001 - formal parameters (C++),
a766d390 10002 - receiver type (Go),
a766d390
DE
10003
10004 The term "physname" is a bit confusing.
10005 For C++, for example, it is the demangled name.
10006 For Go, for example, it's the mangled name.
94af9270 10007
af6b7be1
JB
10008 For Ada, return the DIE's linkage name rather than the fully qualified
10009 name. PHYSNAME is ignored..
10010
5989a64e 10011 The result is allocated on the objfile->per_bfd's obstack and
45940949 10012 canonicalized. */
94af9270
KS
10013
10014static const char *
15d034d0
TT
10015dwarf2_compute_name (const char *name,
10016 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10017 int physname)
10018{
5e22e966 10019 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10020
94af9270
KS
10021 if (name == NULL)
10022 name = dwarf2_name (die, cu);
10023
2ee7123e
DE
10024 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10025 but otherwise compute it by typename_concat inside GDB.
10026 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10027 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10028 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10029 will set the demangled name to the result of dwarf2_full_name, and it is
10030 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10031 if (cu->language == language_ada
10032 || (cu->language == language_fortran && physname))
10033 {
10034 /* For Ada unit, we prefer the linkage name over the name, as
10035 the former contains the exported name, which the user expects
10036 to be able to reference. Ideally, we want the user to be able
10037 to reference this entity using either natural or linkage name,
10038 but we haven't started looking at this enhancement yet. */
73b9be8b 10039 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10040
2ee7123e
DE
10041 if (linkage_name != NULL)
10042 return linkage_name;
f55ee35c
JK
10043 }
10044
94af9270
KS
10045 /* These are the only languages we know how to qualify names in. */
10046 if (name != NULL
9c37b5ae 10047 && (cu->language == language_cplus
c44af4eb
TT
10048 || cu->language == language_fortran || cu->language == language_d
10049 || cu->language == language_rust))
94af9270
KS
10050 {
10051 if (die_needs_namespace (die, cu))
10052 {
0d5cff50 10053 const char *prefix;
34a68019 10054 const char *canonical_name = NULL;
94af9270 10055
d7e74731
PA
10056 string_file buf;
10057
94af9270 10058 prefix = determine_prefix (die, cu);
94af9270
KS
10059 if (*prefix != '\0')
10060 {
43816ebc
TT
10061 gdb::unique_xmalloc_ptr<char> prefixed_name
10062 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10063
43816ebc 10064 buf.puts (prefixed_name.get ());
94af9270
KS
10065 }
10066 else
d7e74731 10067 buf.puts (name);
94af9270 10068
98bfdba5
PA
10069 /* Template parameters may be specified in the DIE's DW_AT_name, or
10070 as children with DW_TAG_template_type_param or
10071 DW_TAG_value_type_param. If the latter, add them to the name
10072 here. If the name already has template parameters, then
10073 skip this step; some versions of GCC emit both, and
10074 it is more efficient to use the pre-computed name.
10075
10076 Something to keep in mind about this process: it is very
10077 unlikely, or in some cases downright impossible, to produce
10078 something that will match the mangled name of a function.
10079 If the definition of the function has the same debug info,
10080 we should be able to match up with it anyway. But fallbacks
10081 using the minimal symbol, for instance to find a method
10082 implemented in a stripped copy of libstdc++, will not work.
10083 If we do not have debug info for the definition, we will have to
10084 match them up some other way.
10085
10086 When we do name matching there is a related problem with function
10087 templates; two instantiated function templates are allowed to
10088 differ only by their return types, which we do not add here. */
10089
10090 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10091 {
10092 struct attribute *attr;
10093 struct die_info *child;
10094 int first = 1;
2c75ccb2 10095 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
10096
10097 die->building_fullname = 1;
10098
10099 for (child = die->child; child != NULL; child = child->sibling)
10100 {
10101 struct type *type;
12df843f 10102 LONGEST value;
d521ce57 10103 const gdb_byte *bytes;
98bfdba5
PA
10104 struct dwarf2_locexpr_baton *baton;
10105 struct value *v;
10106
10107 if (child->tag != DW_TAG_template_type_param
10108 && child->tag != DW_TAG_template_value_param)
10109 continue;
10110
10111 if (first)
10112 {
d7e74731 10113 buf.puts ("<");
98bfdba5
PA
10114 first = 0;
10115 }
10116 else
d7e74731 10117 buf.puts (", ");
98bfdba5
PA
10118
10119 attr = dwarf2_attr (child, DW_AT_type, cu);
10120 if (attr == NULL)
10121 {
b98664d3 10122 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10123 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10124 continue;
10125 }
10126 type = die_type (child, cu);
10127
10128 if (child->tag == DW_TAG_template_type_param)
10129 {
2c75ccb2
AB
10130 cplus_lang->print_type (type, "", &buf, -1, 0,
10131 &type_print_raw_options);
98bfdba5
PA
10132 continue;
10133 }
10134
10135 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10136 if (attr == NULL)
10137 {
b98664d3 10138 complaint (_("template parameter missing "
3e43a32a 10139 "DW_AT_const_value"));
d7e74731 10140 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10141 continue;
10142 }
10143
10144 dwarf2_const_value_attr (attr, type, name,
10145 &cu->comp_unit_obstack, cu,
10146 &value, &bytes, &baton);
10147
20ce4123 10148 if (type->has_no_signedness ())
98bfdba5
PA
10149 /* GDB prints characters as NUMBER 'CHAR'. If that's
10150 changed, this can use value_print instead. */
2c75ccb2 10151 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
10152 else
10153 {
10154 struct value_print_options opts;
10155
10156 if (baton != NULL)
10157 v = dwarf2_evaluate_loc_desc (type, NULL,
10158 baton->data,
10159 baton->size,
9f47c707
SM
10160 baton->per_cu,
10161 baton->per_objfile);
98bfdba5
PA
10162 else if (bytes != NULL)
10163 {
10164 v = allocate_value (type);
10165 memcpy (value_contents_writeable (v), bytes,
10166 TYPE_LENGTH (type));
10167 }
10168 else
10169 v = value_from_longest (type, value);
10170
3e43a32a
MS
10171 /* Specify decimal so that we do not depend on
10172 the radix. */
98bfdba5
PA
10173 get_formatted_print_options (&opts, 'd');
10174 opts.raw = 1;
d7e74731 10175 value_print (v, &buf, &opts);
98bfdba5 10176 release_value (v);
98bfdba5
PA
10177 }
10178 }
10179
10180 die->building_fullname = 0;
10181
10182 if (!first)
10183 {
10184 /* Close the argument list, with a space if necessary
10185 (nested templates). */
d7e74731
PA
10186 if (!buf.empty () && buf.string ().back () == '>')
10187 buf.puts (" >");
98bfdba5 10188 else
d7e74731 10189 buf.puts (">");
98bfdba5
PA
10190 }
10191 }
10192
9c37b5ae 10193 /* For C++ methods, append formal parameter type
94af9270 10194 information, if PHYSNAME. */
6e70227d 10195
94af9270 10196 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10197 && cu->language == language_cplus)
94af9270
KS
10198 {
10199 struct type *type = read_type_die (die, cu);
10200
d7e74731 10201 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10202 &type_print_raw_options);
94af9270 10203
9c37b5ae 10204 if (cu->language == language_cplus)
94af9270 10205 {
60430eff
DJ
10206 /* Assume that an artificial first parameter is
10207 "this", but do not crash if it is not. RealView
10208 marks unnamed (and thus unused) parameters as
10209 artificial; there is no way to differentiate
10210 the two cases. */
1f704f76 10211 if (type->num_fields () > 0
94af9270 10212 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10213 && type->field (0).type ()->code () == TYPE_CODE_PTR
10214 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10215 buf.puts (" const");
94af9270
KS
10216 }
10217 }
10218
d7e74731 10219 const std::string &intermediate_name = buf.string ();
94af9270
KS
10220
10221 if (cu->language == language_cplus)
34a68019 10222 canonical_name
322a8516 10223 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10224 objfile);
34a68019
TT
10225
10226 /* If we only computed INTERMEDIATE_NAME, or if
10227 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10228 intern it. */
322a8516 10229 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10230 name = objfile->intern (intermediate_name);
34a68019
TT
10231 else
10232 name = canonical_name;
94af9270
KS
10233 }
10234 }
10235
10236 return name;
10237}
10238
0114d602
DJ
10239/* Return the fully qualified name of DIE, based on its DW_AT_name.
10240 If scope qualifiers are appropriate they will be added. The result
34a68019 10241 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10242 not have a name. NAME may either be from a previous call to
10243 dwarf2_name or NULL.
10244
9c37b5ae 10245 The output string will be canonicalized (if C++). */
0114d602
DJ
10246
10247static const char *
15d034d0 10248dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10249{
94af9270
KS
10250 return dwarf2_compute_name (name, die, cu, 0);
10251}
0114d602 10252
94af9270
KS
10253/* Construct a physname for the given DIE in CU. NAME may either be
10254 from a previous call to dwarf2_name or NULL. The result will be
10255 allocated on the objfile_objstack or NULL if the DIE does not have a
10256 name.
0114d602 10257
9c37b5ae 10258 The output string will be canonicalized (if C++). */
0114d602 10259
94af9270 10260static const char *
15d034d0 10261dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10262{
5e22e966 10263 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10264 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10265 int need_copy = 1;
10266
10267 /* In this case dwarf2_compute_name is just a shortcut not building anything
10268 on its own. */
10269 if (!die_needs_namespace (die, cu))
10270 return dwarf2_compute_name (name, die, cu, 1);
10271
906bb4c5
TT
10272 if (cu->language != language_rust)
10273 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10274
10275 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10276 has computed. */
791afaa2 10277 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10278 if (mangled != NULL)
900e11f9 10279 {
900e11f9 10280
d3355e4d 10281 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10282 {
10283 /* Do nothing (do not demangle the symbol name). */
10284 }
a766d390
DE
10285 else
10286 {
0eb876f5
JB
10287 /* Use DMGL_RET_DROP for C++ template functions to suppress
10288 their return type. It is easier for GDB users to search
10289 for such functions as `name(params)' than `long name(params)'.
10290 In such case the minimal symbol names do not match the full
10291 symbol names but for template functions there is never a need
10292 to look up their definition from their declaration so
10293 the only disadvantage remains the minimal symbol variant
10294 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10295 demangled.reset (gdb_demangle (mangled,
10296 (DMGL_PARAMS | DMGL_ANSI
10297 | DMGL_RET_DROP)));
a766d390 10298 }
900e11f9 10299 if (demangled)
791afaa2 10300 canon = demangled.get ();
900e11f9
JK
10301 else
10302 {
10303 canon = mangled;
10304 need_copy = 0;
10305 }
10306 }
10307
10308 if (canon == NULL || check_physname)
10309 {
10310 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10311
10312 if (canon != NULL && strcmp (physname, canon) != 0)
10313 {
10314 /* It may not mean a bug in GDB. The compiler could also
10315 compute DW_AT_linkage_name incorrectly. But in such case
10316 GDB would need to be bug-to-bug compatible. */
10317
b98664d3 10318 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10319 "(from linkage <%s>) - DIE at %s [in module %s]"),
10320 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10321 objfile_name (objfile));
900e11f9
JK
10322
10323 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10324 is available here - over computed PHYSNAME. It is safer
10325 against both buggy GDB and buggy compilers. */
10326
10327 retval = canon;
10328 }
10329 else
10330 {
10331 retval = physname;
10332 need_copy = 0;
10333 }
10334 }
10335 else
10336 retval = canon;
10337
10338 if (need_copy)
be1e3d3e 10339 retval = objfile->intern (retval);
900e11f9 10340
900e11f9 10341 return retval;
0114d602
DJ
10342}
10343
74921315
KS
10344/* Inspect DIE in CU for a namespace alias. If one exists, record
10345 a new symbol for it.
10346
10347 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10348
10349static int
10350read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10351{
10352 struct attribute *attr;
10353
10354 /* If the die does not have a name, this is not a namespace
10355 alias. */
10356 attr = dwarf2_attr (die, DW_AT_name, cu);
10357 if (attr != NULL)
10358 {
10359 int num;
10360 struct die_info *d = die;
10361 struct dwarf2_cu *imported_cu = cu;
10362
10363 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10364 keep inspecting DIEs until we hit the underlying import. */
10365#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10366 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10367 {
10368 attr = dwarf2_attr (d, DW_AT_import, cu);
10369 if (attr == NULL)
10370 break;
10371
10372 d = follow_die_ref (d, attr, &imported_cu);
10373 if (d->tag != DW_TAG_imported_declaration)
10374 break;
10375 }
10376
10377 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10378 {
b98664d3 10379 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10380 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10381 return 0;
10382 }
10383
10384 if (attr != NULL)
10385 {
10386 struct type *type;
0826b30a 10387 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10388
aa66c379 10389 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10390 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10391 {
10392 /* This declaration is a global namespace alias. Add
10393 a symbol for it whose type is the aliased namespace. */
10394 new_symbol (die, type, cu);
10395 return 1;
10396 }
10397 }
10398 }
10399
10400 return 0;
10401}
10402
22cee43f 10403/* Return the using directives repository (global or local?) to use in the
804d2729 10404 current context for CU.
22cee43f
PMR
10405
10406 For Ada, imported declarations can materialize renamings, which *may* be
10407 global. However it is impossible (for now?) in DWARF to distinguish
10408 "external" imported declarations and "static" ones. As all imported
10409 declarations seem to be static in all other languages, make them all CU-wide
10410 global only in Ada. */
10411
10412static struct using_direct **
804d2729 10413using_directives (struct dwarf2_cu *cu)
22cee43f 10414{
c24bdb02
KS
10415 if (cu->language == language_ada
10416 && cu->get_builder ()->outermost_context_p ())
10417 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10418 else
c24bdb02 10419 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10420}
10421
27aa8d6a
SW
10422/* Read the import statement specified by the given die and record it. */
10423
10424static void
10425read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10426{
5e22e966 10427 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10428 struct attribute *import_attr;
32019081 10429 struct die_info *imported_die, *child_die;
de4affc9 10430 struct dwarf2_cu *imported_cu;
27aa8d6a 10431 const char *imported_name;
794684b6 10432 const char *imported_name_prefix;
13387711
SW
10433 const char *canonical_name;
10434 const char *import_alias;
10435 const char *imported_declaration = NULL;
794684b6 10436 const char *import_prefix;
eb1e02fd 10437 std::vector<const char *> excludes;
13387711 10438
27aa8d6a
SW
10439 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10440 if (import_attr == NULL)
10441 {
b98664d3 10442 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10443 dwarf_tag_name (die->tag));
10444 return;
10445 }
10446
de4affc9
CC
10447 imported_cu = cu;
10448 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10449 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10450 if (imported_name == NULL)
10451 {
10452 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10453
dda83cd7
SM
10454 The import in the following code:
10455 namespace A
10456 {
10457 typedef int B;
10458 }
10459
10460 int main ()
10461 {
10462 using A::B;
10463 B b;
10464 return b;
10465 }
10466
10467 ...
10468 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10469 <52> DW_AT_decl_file : 1
10470 <53> DW_AT_decl_line : 6
10471 <54> DW_AT_import : <0x75>
10472 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10473 <59> DW_AT_name : B
10474 <5b> DW_AT_decl_file : 1
10475 <5c> DW_AT_decl_line : 2
10476 <5d> DW_AT_type : <0x6e>
10477 ...
10478 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10479 <76> DW_AT_byte_size : 4
10480 <77> DW_AT_encoding : 5 (signed)
10481
10482 imports the wrong die ( 0x75 instead of 0x58 ).
10483 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10484 return;
10485 }
10486
82856980
SW
10487 /* Figure out the local name after import. */
10488 import_alias = dwarf2_name (die, cu);
27aa8d6a 10489
794684b6
SW
10490 /* Figure out where the statement is being imported to. */
10491 import_prefix = determine_prefix (die, cu);
10492
10493 /* Figure out what the scope of the imported die is and prepend it
10494 to the name of the imported die. */
de4affc9 10495 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10496
f55ee35c
JK
10497 if (imported_die->tag != DW_TAG_namespace
10498 && imported_die->tag != DW_TAG_module)
794684b6 10499 {
13387711
SW
10500 imported_declaration = imported_name;
10501 canonical_name = imported_name_prefix;
794684b6 10502 }
13387711 10503 else if (strlen (imported_name_prefix) > 0)
12aaed36 10504 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10505 imported_name_prefix,
10506 (cu->language == language_d ? "." : "::"),
10507 imported_name, (char *) NULL);
13387711
SW
10508 else
10509 canonical_name = imported_name;
794684b6 10510
32019081
JK
10511 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10512 for (child_die = die->child; child_die && child_die->tag;
436c571c 10513 child_die = child_die->sibling)
32019081
JK
10514 {
10515 /* DWARF-4: A Fortran use statement with a “rename list” may be
10516 represented by an imported module entry with an import attribute
10517 referring to the module and owned entries corresponding to those
10518 entities that are renamed as part of being imported. */
10519
10520 if (child_die->tag != DW_TAG_imported_declaration)
10521 {
b98664d3 10522 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10523 "- DIE at %s [in module %s]"),
10524 sect_offset_str (child_die->sect_off),
10525 objfile_name (objfile));
32019081
JK
10526 continue;
10527 }
10528
10529 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10530 if (import_attr == NULL)
10531 {
b98664d3 10532 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10533 dwarf_tag_name (child_die->tag));
10534 continue;
10535 }
10536
10537 imported_cu = cu;
10538 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10539 &imported_cu);
10540 imported_name = dwarf2_name (imported_die, imported_cu);
10541 if (imported_name == NULL)
10542 {
b98664d3 10543 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10544 "imported name - DIE at %s [in module %s]"),
10545 sect_offset_str (child_die->sect_off),
10546 objfile_name (objfile));
32019081
JK
10547 continue;
10548 }
10549
eb1e02fd 10550 excludes.push_back (imported_name);
32019081
JK
10551
10552 process_die (child_die, cu);
10553 }
10554
804d2729 10555 add_using_directive (using_directives (cu),
22cee43f
PMR
10556 import_prefix,
10557 canonical_name,
10558 import_alias,
10559 imported_declaration,
10560 excludes,
10561 0,
10562 &objfile->objfile_obstack);
27aa8d6a
SW
10563}
10564
5230b05a
WT
10565/* ICC<14 does not output the required DW_AT_declaration on incomplete
10566 types, but gives them a size of zero. Starting with version 14,
10567 ICC is compatible with GCC. */
10568
9068261f 10569static bool
5230b05a
WT
10570producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10571{
10572 if (!cu->checked_producer)
10573 check_producer (cu);
10574
10575 return cu->producer_is_icc_lt_14;
10576}
10577
eb77c9df
AB
10578/* ICC generates a DW_AT_type for C void functions. This was observed on
10579 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10580 which says that void functions should not have a DW_AT_type. */
10581
10582static bool
10583producer_is_icc (struct dwarf2_cu *cu)
10584{
10585 if (!cu->checked_producer)
10586 check_producer (cu);
10587
10588 return cu->producer_is_icc;
10589}
10590
1b80a9fa
JK
10591/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10592 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10593 this, it was first present in GCC release 4.3.0. */
10594
9068261f 10595static bool
1b80a9fa
JK
10596producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10597{
10598 if (!cu->checked_producer)
10599 check_producer (cu);
10600
10601 return cu->producer_is_gcc_lt_4_3;
10602}
10603
d721ba37
PA
10604static file_and_directory
10605find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10606{
d721ba37
PA
10607 file_and_directory res;
10608
9291a0cd
TT
10609 /* Find the filename. Do not use dwarf2_name here, since the filename
10610 is not a source language identifier. */
d721ba37
PA
10611 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10612 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10613
d721ba37
PA
10614 if (res.comp_dir == NULL
10615 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10616 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10617 {
d721ba37
PA
10618 res.comp_dir_storage = ldirname (res.name);
10619 if (!res.comp_dir_storage.empty ())
10620 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10621 }
d721ba37 10622 if (res.comp_dir != NULL)
9291a0cd
TT
10623 {
10624 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10625 directory, get rid of it. */
d721ba37 10626 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10627
d721ba37
PA
10628 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10629 res.comp_dir = cp + 1;
9291a0cd
TT
10630 }
10631
d721ba37
PA
10632 if (res.name == NULL)
10633 res.name = "<unknown>";
10634
10635 return res;
9291a0cd
TT
10636}
10637
f4dc4d17
DE
10638/* Handle DW_AT_stmt_list for a compilation unit.
10639 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10640 COMP_DIR is the compilation directory. LOWPC is passed to
10641 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10642
10643static void
10644handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10645 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10646{
976ca316 10647 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10648 struct attribute *attr;
527f3840
JK
10649 struct line_header line_header_local;
10650 hashval_t line_header_local_hash;
527f3840
JK
10651 void **slot;
10652 int decode_mapping;
2ab95328 10653
f4dc4d17
DE
10654 gdb_assert (! cu->per_cu->is_debug_types);
10655
2ab95328 10656 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 10657 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
10658 return;
10659
d4df075e 10660 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
10661
10662 /* The line header hash table is only created if needed (it exists to
10663 prevent redundant reading of the line table for partial_units).
10664 If we're given a partial_unit, we'll need it. If we're given a
10665 compile_unit, then use the line header hash table if it's already
10666 created, but don't create one just yet. */
10667
976ca316 10668 if (per_objfile->line_header_hash == NULL
527f3840 10669 && die->tag == DW_TAG_partial_unit)
2ab95328 10670 {
976ca316 10671 per_objfile->line_header_hash
d15acc42
TT
10672 .reset (htab_create_alloc (127, line_header_hash_voidp,
10673 line_header_eq_voidp,
10674 free_line_header_voidp,
10675 xcalloc, xfree));
527f3840 10676 }
2ab95328 10677
9c541725 10678 line_header_local.sect_off = line_offset;
527f3840
JK
10679 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10680 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10681 if (per_objfile->line_header_hash != NULL)
527f3840 10682 {
976ca316 10683 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10684 &line_header_local,
10685 line_header_local_hash, NO_INSERT);
10686
10687 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10688 is not present in *SLOT (since if there is something in *SLOT then
10689 it will be for a partial_unit). */
10690 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10691 {
527f3840 10692 gdb_assert (*slot != NULL);
9a3c8263 10693 cu->line_header = (struct line_header *) *slot;
527f3840 10694 return;
dee91e82 10695 }
2ab95328 10696 }
527f3840
JK
10697
10698 /* dwarf_decode_line_header does not yet provide sufficient information.
10699 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10700 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10701 if (lh == NULL)
527f3840 10702 return;
4c8aa72d
PA
10703
10704 cu->line_header = lh.release ();
10705 cu->line_header_die_owner = die;
527f3840 10706
976ca316 10707 if (per_objfile->line_header_hash == NULL)
527f3840
JK
10708 slot = NULL;
10709 else
10710 {
976ca316 10711 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10712 &line_header_local,
10713 line_header_local_hash, INSERT);
10714 gdb_assert (slot != NULL);
10715 }
10716 if (slot != NULL && *slot == NULL)
10717 {
10718 /* This newly decoded line number information unit will be owned
10719 by line_header_hash hash table. */
10720 *slot = cu->line_header;
4c8aa72d 10721 cu->line_header_die_owner = NULL;
527f3840
JK
10722 }
10723 else
10724 {
10725 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 10726 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
10727 line_header for this CU - it may happen at most once for each line
10728 number information unit. And if we're not using line_header_hash
10729 then this is what we want as well. */
10730 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10731 }
10732 decode_mapping = (die->tag != DW_TAG_partial_unit);
10733 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10734 decode_mapping);
fff8551c 10735
2ab95328
TT
10736}
10737
95554aad 10738/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10739
c906108c 10740static void
e7c27a73 10741read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10742{
976ca316
SM
10743 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10744 struct objfile *objfile = per_objfile->objfile;
08feed99 10745 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10746 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10747 CORE_ADDR highpc = ((CORE_ADDR) 0);
10748 struct attribute *attr;
c906108c 10749 struct die_info *child_die;
e142c38c 10750 CORE_ADDR baseaddr;
6e70227d 10751
380618d6 10752 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10753 baseaddr = objfile->text_section_offset ();
c906108c 10754
fae299cd 10755 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10756
10757 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10758 from finish_block. */
2acceee2 10759 if (lowpc == ((CORE_ADDR) -1))
c906108c 10760 lowpc = highpc;
3e29f34a 10761 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10762
d721ba37 10763 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10764
f4b8a18d
KW
10765 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10766 standardised yet. As a workaround for the language detection we fall
10767 back to the DW_AT_producer string. */
10768 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10769 cu->language = language_opencl;
10770
3019eac3
DE
10771 /* Similar hack for Go. */
10772 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10773 set_cu_language (DW_LANG_Go, cu);
10774
c24bdb02 10775 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10776
10777 /* Decode line number information if present. We do this before
10778 processing child DIEs, so that the line header table is available
10779 for DW_AT_decl_file. */
d721ba37 10780 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10781
10782 /* Process all dies in compilation unit. */
10783 if (die->child != NULL)
10784 {
10785 child_die = die->child;
10786 while (child_die && child_die->tag)
10787 {
10788 process_die (child_die, cu);
436c571c 10789 child_die = child_die->sibling;
3019eac3
DE
10790 }
10791 }
10792
10793 /* Decode macro information, if present. Dwarf 2 macro information
10794 refers to information in the line number info statement program
10795 header, so we can only read it if we've read the header
10796 successfully. */
0af92d60
JK
10797 attr = dwarf2_attr (die, DW_AT_macros, cu);
10798 if (attr == NULL)
10799 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 10800 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
10801 {
10802 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10803 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10804
529908cb 10805 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
10806 }
10807 else
10808 {
10809 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 10810 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 10811 {
529908cb 10812 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 10813
43f3e411 10814 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10815 }
10816 }
3019eac3
DE
10817}
10818
c24bdb02
KS
10819void
10820dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10821{
f4dc4d17
DE
10822 struct type_unit_group *tu_group;
10823 int first_time;
3019eac3 10824 struct attribute *attr;
9c541725 10825 unsigned int i;
0186c6a7 10826 struct signatured_type *sig_type;
3019eac3 10827
f4dc4d17 10828 gdb_assert (per_cu->is_debug_types);
0186c6a7 10829 sig_type = (struct signatured_type *) per_cu;
3019eac3 10830
c24bdb02 10831 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10832
f4dc4d17 10833 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10834 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10835 if (sig_type->type_unit_group == NULL)
c24bdb02 10836 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10837 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10838
10839 /* If we've already processed this stmt_list there's no real need to
10840 do it again, we could fake it and just recreate the part we need
10841 (file name,index -> symtab mapping). If data shows this optimization
10842 is useful we can do it then. */
8adb8487
TT
10843 type_unit_group_unshareable *tug_unshare
10844 = per_objfile->get_type_unit_group_unshareable (tu_group);
10845 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
10846
10847 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10848 debug info. */
fff8551c 10849 line_header_up lh;
d4df075e 10850 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 10851 {
d4df075e 10852 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 10853 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10854 }
10855 if (lh == NULL)
10856 {
10857 if (first_time)
c24bdb02 10858 start_symtab ("", NULL, 0);
f4dc4d17
DE
10859 else
10860 {
8adb8487 10861 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 10862 gdb_assert (m_builder == nullptr);
8adb8487 10863 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10864 m_builder.reset (new struct buildsym_compunit
10865 (COMPUNIT_OBJFILE (cust), "",
10866 COMPUNIT_DIRNAME (cust),
10867 compunit_language (cust),
10868 0, cust));
770479f2 10869 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10870 }
f4dc4d17 10871 return;
3019eac3
DE
10872 }
10873
c24bdb02
KS
10874 line_header = lh.release ();
10875 line_header_die_owner = die;
3019eac3 10876
f4dc4d17
DE
10877 if (first_time)
10878 {
c24bdb02 10879 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10880
1fd60fc0
DE
10881 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10882 still initializing it, and our caller (a few levels up)
10883 process_full_type_unit still needs to know if this is the first
10884 time. */
10885
8adb8487 10886 tug_unshare->symtabs
4ac93832
TT
10887 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10888 struct symtab *, line_header->file_names_size ());
3019eac3 10889
7ba99d21
AT
10890 auto &file_names = line_header->file_names ();
10891 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10892 {
7ba99d21 10893 file_entry &fe = file_names[i];
c24bdb02
KS
10894 dwarf2_start_subfile (this, fe.name,
10895 fe.include_dir (line_header));
10896 buildsym_compunit *b = get_builder ();
10897 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10898 {
4c8aa72d
PA
10899 /* NOTE: start_subfile will recognize when it's been
10900 passed a file it has already seen. So we can't
10901 assume there's a simple mapping from
10902 cu->line_header->file_names to subfiles, plus
10903 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10904 b->get_current_subfile ()->symtab
10905 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10906 }
10907
c24bdb02 10908 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 10909 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
10910 }
10911 }
10912 else
3019eac3 10913 {
c24bdb02 10914 gdb_assert (m_builder == nullptr);
8adb8487 10915 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
10916 m_builder.reset (new struct buildsym_compunit
10917 (COMPUNIT_OBJFILE (cust), "",
10918 COMPUNIT_DIRNAME (cust),
10919 compunit_language (cust),
10920 0, cust));
770479f2 10921 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10922
7ba99d21
AT
10923 auto &file_names = line_header->file_names ();
10924 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10925 {
7ba99d21 10926 file_entry &fe = file_names[i];
8adb8487 10927 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 10928 }
3019eac3
DE
10929 }
10930
f4dc4d17
DE
10931 /* The main symtab is allocated last. Type units don't have DW_AT_name
10932 so they don't have a "real" (so to speak) symtab anyway.
10933 There is later code that will assign the main symtab to all symbols
10934 that don't have one. We need to handle the case of a symbol with a
10935 missing symtab (DW_AT_decl_file) anyway. */
10936}
3019eac3 10937
f4dc4d17
DE
10938/* Process DW_TAG_type_unit.
10939 For TUs we want to skip the first top level sibling if it's not the
10940 actual type being defined by this TU. In this case the first top
10941 level sibling is there to provide context only. */
3019eac3 10942
f4dc4d17
DE
10943static void
10944read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10945{
10946 struct die_info *child_die;
3019eac3 10947
f4dc4d17
DE
10948 prepare_one_comp_unit (cu, die, language_minimal);
10949
10950 /* Initialize (or reinitialize) the machinery for building symtabs.
10951 We do this before processing child DIEs, so that the line header table
10952 is available for DW_AT_decl_file. */
c24bdb02 10953 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10954
10955 if (die->child != NULL)
10956 {
10957 child_die = die->child;
10958 while (child_die && child_die->tag)
10959 {
10960 process_die (child_die, cu);
436c571c 10961 child_die = child_die->sibling;
f4dc4d17
DE
10962 }
10963 }
3019eac3
DE
10964}
10965\f
80626a55
DE
10966/* DWO/DWP files.
10967
10968 http://gcc.gnu.org/wiki/DebugFission
10969 http://gcc.gnu.org/wiki/DebugFissionDWP
10970
10971 To simplify handling of both DWO files ("object" files with the DWARF info)
10972 and DWP files (a file with the DWOs packaged up into one file), we treat
10973 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10974
10975static hashval_t
10976hash_dwo_file (const void *item)
10977{
9a3c8263 10978 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10979 hashval_t hash;
3019eac3 10980
a2ce51a0
DE
10981 hash = htab_hash_string (dwo_file->dwo_name);
10982 if (dwo_file->comp_dir != NULL)
10983 hash += htab_hash_string (dwo_file->comp_dir);
10984 return hash;
3019eac3
DE
10985}
10986
10987static int
10988eq_dwo_file (const void *item_lhs, const void *item_rhs)
10989{
9a3c8263
SM
10990 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10991 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10992
a2ce51a0
DE
10993 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10994 return 0;
10995 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10996 return lhs->comp_dir == rhs->comp_dir;
10997 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10998}
10999
11000/* Allocate a hash table for DWO files. */
11001
51ac9db5 11002static htab_up
298e9637 11003allocate_dwo_file_hash_table ()
3019eac3 11004{
51ac9db5
SM
11005 auto delete_dwo_file = [] (void *item)
11006 {
11007 struct dwo_file *dwo_file = (struct dwo_file *) item;
11008
11009 delete dwo_file;
11010 };
11011
bc68fb19
TT
11012 return htab_up (htab_create_alloc (41,
11013 hash_dwo_file,
11014 eq_dwo_file,
11015 delete_dwo_file,
11016 xcalloc, xfree));
3019eac3
DE
11017}
11018
80626a55
DE
11019/* Lookup DWO file DWO_NAME. */
11020
11021static void **
976ca316 11022lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11023 const char *dwo_name,
11024 const char *comp_dir)
80626a55
DE
11025{
11026 struct dwo_file find_entry;
11027 void **slot;
11028
976ca316
SM
11029 if (per_objfile->per_bfd->dwo_files == NULL)
11030 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11031
0ac5b59e
DE
11032 find_entry.dwo_name = dwo_name;
11033 find_entry.comp_dir = comp_dir;
976ca316 11034 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11035 INSERT);
80626a55
DE
11036
11037 return slot;
11038}
11039
3019eac3
DE
11040static hashval_t
11041hash_dwo_unit (const void *item)
11042{
9a3c8263 11043 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11044
11045 /* This drops the top 32 bits of the id, but is ok for a hash. */
11046 return dwo_unit->signature;
11047}
11048
11049static int
11050eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11051{
9a3c8263
SM
11052 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11053 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11054
11055 /* The signature is assumed to be unique within the DWO file.
11056 So while object file CU dwo_id's always have the value zero,
11057 that's OK, assuming each object file DWO file has only one CU,
11058 and that's the rule for now. */
11059 return lhs->signature == rhs->signature;
11060}
11061
11062/* Allocate a hash table for DWO CUs,TUs.
11063 There is one of these tables for each of CUs,TUs for each DWO file. */
11064
b0b6a987 11065static htab_up
298e9637 11066allocate_dwo_unit_table ()
3019eac3
DE
11067{
11068 /* Start out with a pretty small number.
11069 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11070 return htab_up (htab_create_alloc (3,
11071 hash_dwo_unit,
11072 eq_dwo_unit,
11073 NULL, xcalloc, xfree));
3019eac3
DE
11074}
11075
19c3d4c9 11076/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11077
11078static void
19c3d4c9
DE
11079create_dwo_cu_reader (const struct die_reader_specs *reader,
11080 const gdb_byte *info_ptr,
11081 struct die_info *comp_unit_die,
c0ab21c2
TT
11082 struct dwo_file *dwo_file,
11083 struct dwo_unit *dwo_unit)
3019eac3
DE
11084{
11085 struct dwarf2_cu *cu = reader->cu;
9c541725 11086 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11087 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11088
a084a2a6
AT
11089 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11090 if (!signature.has_value ())
3019eac3 11091 {
b98664d3 11092 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11093 " its dwo_id [in module %s]"),
9d8780f0 11094 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11095 return;
11096 }
11097
3019eac3 11098 dwo_unit->dwo_file = dwo_file;
a084a2a6 11099 dwo_unit->signature = *signature;
8a0459fd 11100 dwo_unit->section = section;
9c541725 11101 dwo_unit->sect_off = sect_off;
3019eac3
DE
11102 dwo_unit->length = cu->per_cu->length;
11103
6f738b01
SM
11104 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11105 sect_offset_str (sect_off),
11106 hex_string (dwo_unit->signature));
3019eac3
DE
11107}
11108
33c5cd75 11109/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11110 Note: This function processes DWO files only, not DWP files. */
3019eac3 11111
33c5cd75 11112static void
976ca316 11113create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11114 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11115 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11116{
976ca316
SM
11117 struct objfile *objfile = per_objfile->objfile;
11118 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11119 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11120
96b79293 11121 section.read (objfile);
33c5cd75 11122 info_ptr = section.buffer;
3019eac3
DE
11123
11124 if (info_ptr == NULL)
33c5cd75 11125 return;
3019eac3 11126
6f738b01
SM
11127 dwarf_read_debug_printf ("Reading %s for %s:",
11128 section.get_name (),
11129 section.get_file_name ());
3019eac3 11130
33c5cd75 11131 end_ptr = info_ptr + section.size;
3019eac3
DE
11132 while (info_ptr < end_ptr)
11133 {
11134 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11135 struct dwo_unit read_unit {};
33c5cd75
DB
11136 struct dwo_unit *dwo_unit;
11137 void **slot;
11138 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11139
1859c670 11140 per_cu.per_bfd = per_bfd;
3019eac3 11141 per_cu.is_debug_types = 0;
33c5cd75
DB
11142 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11143 per_cu.section = &section;
11144
976ca316 11145 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11146 if (!reader.dummy_p)
11147 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11148 &dwo_file, &read_unit);
33c5cd75
DB
11149 info_ptr += per_cu.length;
11150
11151 // If the unit could not be parsed, skip it.
c0ab21c2 11152 if (read_unit.dwo_file == NULL)
33c5cd75 11153 continue;
3019eac3 11154
33c5cd75 11155 if (cus_htab == NULL)
298e9637 11156 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11157
1859c670 11158 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11159 struct dwo_unit);
c0ab21c2 11160 *dwo_unit = read_unit;
b0b6a987 11161 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11162 gdb_assert (slot != NULL);
11163 if (*slot != NULL)
19c3d4c9 11164 {
33c5cd75
DB
11165 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11166 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11167
b98664d3 11168 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11169 " the entry at offset %s, signature %s"),
11170 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11171 hex_string (dwo_unit->signature));
19c3d4c9 11172 }
33c5cd75 11173 *slot = (void *)dwo_unit;
3019eac3 11174 }
3019eac3
DE
11175}
11176
80626a55
DE
11177/* DWP file .debug_{cu,tu}_index section format:
11178 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11179 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11180
11181 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11182 officially standard DWP format was published with DWARF v5 and is called
11183 Version 5. There are no versions 3 or 4.
80626a55 11184
d2415c6c
DE
11185 DWP Version 1:
11186
80626a55
DE
11187 Both index sections have the same format, and serve to map a 64-bit
11188 signature to a set of section numbers. Each section begins with a header,
11189 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11190 indexes, and a pool of 32-bit section numbers. The index sections will be
11191 aligned at 8-byte boundaries in the file.
11192
d2415c6c
DE
11193 The index section header consists of:
11194
11195 V, 32 bit version number
11196 -, 32 bits unused
11197 N, 32 bit number of compilation units or type units in the index
11198 M, 32 bit number of slots in the hash table
80626a55 11199
d2415c6c 11200 Numbers are recorded using the byte order of the application binary.
80626a55 11201
d2415c6c
DE
11202 The hash table begins at offset 16 in the section, and consists of an array
11203 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11204 order of the application binary). Unused slots in the hash table are 0.
11205 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11206
d2415c6c
DE
11207 The parallel table begins immediately after the hash table
11208 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11209 array of 32-bit indexes (using the byte order of the application binary),
11210 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11211 table contains a 32-bit index into the pool of section numbers. For unused
11212 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11213
73869dc2
DE
11214 The pool of section numbers begins immediately following the hash table
11215 (at offset 16 + 12 * M from the beginning of the section). The pool of
11216 section numbers consists of an array of 32-bit words (using the byte order
11217 of the application binary). Each item in the array is indexed starting
11218 from 0. The hash table entry provides the index of the first section
11219 number in the set. Additional section numbers in the set follow, and the
11220 set is terminated by a 0 entry (section number 0 is not used in ELF).
11221
11222 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11223 section must be the first entry in the set, and the .debug_abbrev.dwo must
11224 be the second entry. Other members of the set may follow in any order.
11225
11226 ---
11227
d2854d8d 11228 DWP Versions 2 and 5:
73869dc2 11229
d2854d8d 11230 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11231 and the entries in the index tables are now offsets into these sections.
11232 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11233 section.
11234
11235 Index Section Contents:
11236 Header
11237 Hash Table of Signatures dwp_hash_table.hash_table
11238 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11239 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11240 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11241
11242 The index section header consists of:
11243
11244 V, 32 bit version number
11245 L, 32 bit number of columns in the table of section offsets
11246 N, 32 bit number of compilation units or type units in the index
11247 M, 32 bit number of slots in the hash table
11248
11249 Numbers are recorded using the byte order of the application binary.
11250
11251 The hash table has the same format as version 1.
11252 The parallel table of indices has the same format as version 1,
11253 except that the entries are origin-1 indices into the table of sections
11254 offsets and the table of section sizes.
11255
11256 The table of offsets begins immediately following the parallel table
11257 (at offset 16 + 12 * M from the beginning of the section). The table is
11258 a two-dimensional array of 32-bit words (using the byte order of the
11259 application binary), with L columns and N+1 rows, in row-major order.
11260 Each row in the array is indexed starting from 0. The first row provides
11261 a key to the remaining rows: each column in this row provides an identifier
11262 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11263 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11264
11265 DW_SECT_INFO 1 .debug_info.dwo
11266 DW_SECT_TYPES 2 .debug_types.dwo
11267 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11268 DW_SECT_LINE 4 .debug_line.dwo
11269 DW_SECT_LOC 5 .debug_loc.dwo
11270 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11271 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11272 DW_SECT_MACRO 8 .debug_macro.dwo
11273
d2854d8d
CT
11274 The section identifiers for Version 5 are:
11275
11276 DW_SECT_INFO_V5 1 .debug_info.dwo
11277 DW_SECT_RESERVED_V5 2 --
11278 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11279 DW_SECT_LINE_V5 4 .debug_line.dwo
11280 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11281 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11282 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11283 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11284
73869dc2
DE
11285 The offsets provided by the CU and TU index sections are the base offsets
11286 for the contributions made by each CU or TU to the corresponding section
11287 in the package file. Each CU and TU header contains an abbrev_offset
11288 field, used to find the abbreviations table for that CU or TU within the
11289 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11290 be interpreted as relative to the base offset given in the index section.
11291 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11292 should be interpreted as relative to the base offset for .debug_line.dwo,
11293 and offsets into other debug sections obtained from DWARF attributes should
11294 also be interpreted as relative to the corresponding base offset.
11295
11296 The table of sizes begins immediately following the table of offsets.
11297 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11298 with L columns and N rows, in row-major order. Each row in the array is
11299 indexed starting from 1 (row 0 is shared by the two tables).
11300
11301 ---
11302
11303 Hash table lookup is handled the same in version 1 and 2:
11304
11305 We assume that N and M will not exceed 2^32 - 1.
11306 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11307
d2415c6c
DE
11308 Given a 64-bit compilation unit signature or a type signature S, an entry
11309 in the hash table is located as follows:
80626a55 11310
d2415c6c
DE
11311 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11312 the low-order k bits all set to 1.
80626a55 11313
d2415c6c 11314 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11315
d2415c6c
DE
11316 3) If the hash table entry at index H matches the signature, use that
11317 entry. If the hash table entry at index H is unused (all zeroes),
11318 terminate the search: the signature is not present in the table.
80626a55 11319
d2415c6c 11320 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11321
d2415c6c 11322 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11323 to stop at an unused slot or find the match. */
80626a55
DE
11324
11325/* Create a hash table to map DWO IDs to their CU/TU entry in
11326 .debug_{info,types}.dwo in DWP_FILE.
11327 Returns NULL if there isn't one.
11328 Note: This function processes DWP files only, not DWO files. */
11329
11330static struct dwp_hash_table *
976ca316 11331create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11332 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11333{
976ca316 11334 struct objfile *objfile = per_objfile->objfile;
400174b1 11335 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11336 const gdb_byte *index_ptr, *index_end;
80626a55 11337 struct dwarf2_section_info *index;
73869dc2 11338 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11339 struct dwp_hash_table *htab;
11340
11341 if (is_debug_types)
11342 index = &dwp_file->sections.tu_index;
11343 else
11344 index = &dwp_file->sections.cu_index;
11345
96b79293 11346 if (index->empty ())
80626a55 11347 return NULL;
96b79293 11348 index->read (objfile);
80626a55
DE
11349
11350 index_ptr = index->buffer;
11351 index_end = index_ptr + index->size;
11352
d2854d8d
CT
11353 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11354 For now it's safe to just read 4 bytes (particularly as it's difficult to
11355 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11356 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11357 index_ptr += 4;
d2854d8d 11358 if (version == 2 || version == 5)
73869dc2
DE
11359 nr_columns = read_4_bytes (dbfd, index_ptr);
11360 else
11361 nr_columns = 0;
11362 index_ptr += 4;
80626a55
DE
11363 nr_units = read_4_bytes (dbfd, index_ptr);
11364 index_ptr += 4;
11365 nr_slots = read_4_bytes (dbfd, index_ptr);
11366 index_ptr += 4;
11367
d2854d8d 11368 if (version != 1 && version != 2 && version != 5)
80626a55 11369 {
21aa081e 11370 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11371 " [in module %s]"),
21aa081e 11372 pulongest (version), dwp_file->name);
80626a55
DE
11373 }
11374 if (nr_slots != (nr_slots & -nr_slots))
11375 {
21aa081e 11376 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11377 " is not power of 2 [in module %s]"),
21aa081e 11378 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11379 }
11380
976ca316 11381 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11382 htab->version = version;
11383 htab->nr_columns = nr_columns;
80626a55
DE
11384 htab->nr_units = nr_units;
11385 htab->nr_slots = nr_slots;
11386 htab->hash_table = index_ptr;
11387 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11388
11389 /* Exit early if the table is empty. */
11390 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11391 || (version == 2 && nr_columns == 0)
11392 || (version == 5 && nr_columns == 0))
73869dc2
DE
11393 {
11394 /* All must be zero. */
11395 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11396 || (version == 2 && nr_columns != 0)
11397 || (version == 5 && nr_columns != 0))
73869dc2 11398 {
b98664d3 11399 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11400 " all zero [in modules %s]"),
11401 dwp_file->name);
11402 }
11403 return htab;
11404 }
11405
11406 if (version == 1)
11407 {
11408 htab->section_pool.v1.indices =
11409 htab->unit_table + sizeof (uint32_t) * nr_slots;
11410 /* It's harder to decide whether the section is too small in v1.
11411 V1 is deprecated anyway so we punt. */
11412 }
d2854d8d 11413 else if (version == 2)
73869dc2
DE
11414 {
11415 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11416 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11417 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11418 /* Reverse map for error checking. */
11419 int ids_seen[DW_SECT_MAX + 1];
11420 int i;
11421
11422 if (nr_columns < 2)
11423 {
11424 error (_("Dwarf Error: bad DWP hash table, too few columns"
11425 " in section table [in module %s]"),
11426 dwp_file->name);
11427 }
11428 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11429 {
11430 error (_("Dwarf Error: bad DWP hash table, too many columns"
11431 " in section table [in module %s]"),
11432 dwp_file->name);
11433 }
04fd5eed
GB
11434 memset (ids, 255, sizeof_ids);
11435 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11436 for (i = 0; i < nr_columns; ++i)
11437 {
11438 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11439
11440 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11441 {
11442 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11443 " in section table [in module %s]"),
11444 id, dwp_file->name);
11445 }
11446 if (ids_seen[id] != -1)
11447 {
11448 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11449 " id %d in section table [in module %s]"),
11450 id, dwp_file->name);
11451 }
11452 ids_seen[id] = i;
11453 ids[i] = id;
11454 }
11455 /* Must have exactly one info or types section. */
11456 if (((ids_seen[DW_SECT_INFO] != -1)
11457 + (ids_seen[DW_SECT_TYPES] != -1))
11458 != 1)
11459 {
11460 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11461 " DWO info/types section [in module %s]"),
11462 dwp_file->name);
11463 }
11464 /* Must have an abbrev section. */
11465 if (ids_seen[DW_SECT_ABBREV] == -1)
11466 {
11467 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11468 " section [in module %s]"),
11469 dwp_file->name);
11470 }
11471 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11472 htab->section_pool.v2.sizes =
11473 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11474 * nr_units * nr_columns);
11475 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11476 * nr_units * nr_columns))
11477 > index_end)
11478 {
11479 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11480 " [in module %s]"),
11481 dwp_file->name);
11482 }
11483 }
d2854d8d
CT
11484 else /* version == 5 */
11485 {
11486 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11487 int *ids = htab->section_pool.v5.section_ids;
11488 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11489 /* Reverse map for error checking. */
11490 int ids_seen[DW_SECT_MAX_V5 + 1];
11491
11492 if (nr_columns < 2)
11493 {
11494 error (_("Dwarf Error: bad DWP hash table, too few columns"
11495 " in section table [in module %s]"),
11496 dwp_file->name);
11497 }
11498 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11499 {
11500 error (_("Dwarf Error: bad DWP hash table, too many columns"
11501 " in section table [in module %s]"),
11502 dwp_file->name);
11503 }
11504 memset (ids, 255, sizeof_ids);
11505 memset (ids_seen, 255, sizeof (ids_seen));
11506 for (int i = 0; i < nr_columns; ++i)
11507 {
11508 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11509
11510 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11511 {
11512 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11513 " in section table [in module %s]"),
11514 id, dwp_file->name);
11515 }
11516 if (ids_seen[id] != -1)
11517 {
11518 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11519 " id %d in section table [in module %s]"),
11520 id, dwp_file->name);
11521 }
11522 ids_seen[id] = i;
11523 ids[i] = id;
11524 }
11525 /* Must have seen an info section. */
11526 if (ids_seen[DW_SECT_INFO_V5] == -1)
11527 {
11528 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11529 " DWO info/types section [in module %s]"),
11530 dwp_file->name);
11531 }
11532 /* Must have an abbrev section. */
11533 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11534 {
11535 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11536 " section [in module %s]"),
11537 dwp_file->name);
11538 }
11539 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11540 htab->section_pool.v5.sizes
11541 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11542 * nr_units * nr_columns);
11543 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11544 * nr_units * nr_columns))
11545 > index_end)
11546 {
11547 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11548 " [in module %s]"),
11549 dwp_file->name);
11550 }
11551 }
80626a55
DE
11552
11553 return htab;
11554}
11555
11556/* Update SECTIONS with the data from SECTP.
11557
5bb6e9dd
TT
11558 This function is like the other "locate" section routines, but in
11559 this context the sections to read comes from the DWP V1 hash table,
11560 not the full ELF section table.
80626a55
DE
11561
11562 The result is non-zero for success, or zero if an error was found. */
11563
11564static int
73869dc2
DE
11565locate_v1_virtual_dwo_sections (asection *sectp,
11566 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11567{
11568 const struct dwop_section_names *names = &dwop_section_names;
11569
fbedd546 11570 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
11571 {
11572 /* There can be only one. */
049412e3 11573 if (sections->abbrev.s.section != NULL)
80626a55 11574 return 0;
049412e3 11575 sections->abbrev.s.section = sectp;
fd361982 11576 sections->abbrev.size = bfd_section_size (sectp);
80626a55 11577 }
fbedd546
TT
11578 else if (names->info_dwo.matches (sectp->name)
11579 || names->types_dwo.matches (sectp->name))
80626a55
DE
11580 {
11581 /* There can be only one. */
049412e3 11582 if (sections->info_or_types.s.section != NULL)
80626a55 11583 return 0;
049412e3 11584 sections->info_or_types.s.section = sectp;
fd361982 11585 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 11586 }
fbedd546 11587 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
11588 {
11589 /* There can be only one. */
049412e3 11590 if (sections->line.s.section != NULL)
80626a55 11591 return 0;
049412e3 11592 sections->line.s.section = sectp;
fd361982 11593 sections->line.size = bfd_section_size (sectp);
80626a55 11594 }
fbedd546 11595 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
11596 {
11597 /* There can be only one. */
049412e3 11598 if (sections->loc.s.section != NULL)
80626a55 11599 return 0;
049412e3 11600 sections->loc.s.section = sectp;
fd361982 11601 sections->loc.size = bfd_section_size (sectp);
80626a55 11602 }
fbedd546 11603 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
11604 {
11605 /* There can be only one. */
049412e3 11606 if (sections->macinfo.s.section != NULL)
80626a55 11607 return 0;
049412e3 11608 sections->macinfo.s.section = sectp;
fd361982 11609 sections->macinfo.size = bfd_section_size (sectp);
80626a55 11610 }
fbedd546 11611 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
11612 {
11613 /* There can be only one. */
049412e3 11614 if (sections->macro.s.section != NULL)
80626a55 11615 return 0;
049412e3 11616 sections->macro.s.section = sectp;
fd361982 11617 sections->macro.size = bfd_section_size (sectp);
80626a55 11618 }
fbedd546 11619 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
11620 {
11621 /* There can be only one. */
049412e3 11622 if (sections->str_offsets.s.section != NULL)
80626a55 11623 return 0;
049412e3 11624 sections->str_offsets.s.section = sectp;
fd361982 11625 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11626 }
11627 else
11628 {
11629 /* No other kind of section is valid. */
11630 return 0;
11631 }
11632
11633 return 1;
11634}
11635
73869dc2
DE
11636/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11637 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11638 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11639 This is for DWP version 1 files. */
80626a55
DE
11640
11641static struct dwo_unit *
976ca316 11642create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11643 struct dwp_file *dwp_file,
73869dc2
DE
11644 uint32_t unit_index,
11645 const char *comp_dir,
11646 ULONGEST signature, int is_debug_types)
80626a55 11647{
73869dc2
DE
11648 const struct dwp_hash_table *dwp_htab =
11649 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11650 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11651 const char *kind = is_debug_types ? "TU" : "CU";
11652 struct dwo_file *dwo_file;
11653 struct dwo_unit *dwo_unit;
73869dc2 11654 struct virtual_v1_dwo_sections sections;
80626a55 11655 void **dwo_file_slot;
80626a55
DE
11656 int i;
11657
73869dc2
DE
11658 gdb_assert (dwp_file->version == 1);
11659
6f738b01
SM
11660 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11661 kind, pulongest (unit_index), hex_string (signature),
11662 dwp_file->name);
80626a55 11663
19ac8c2e 11664 /* Fetch the sections of this DWO unit.
80626a55
DE
11665 Put a limit on the number of sections we look for so that bad data
11666 doesn't cause us to loop forever. */
11667
73869dc2 11668#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11669 (1 /* .debug_info or .debug_types */ \
11670 + 1 /* .debug_abbrev */ \
11671 + 1 /* .debug_line */ \
11672 + 1 /* .debug_loc */ \
11673 + 1 /* .debug_str_offsets */ \
19ac8c2e 11674 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11675 + 1 /* trailing zero */)
11676
11677 memset (&sections, 0, sizeof (sections));
80626a55 11678
73869dc2 11679 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11680 {
11681 asection *sectp;
11682 uint32_t section_nr =
11683 read_4_bytes (dbfd,
73869dc2
DE
11684 dwp_htab->section_pool.v1.indices
11685 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11686
11687 if (section_nr == 0)
11688 break;
11689 if (section_nr >= dwp_file->num_sections)
11690 {
11691 error (_("Dwarf Error: bad DWP hash table, section number too large"
11692 " [in module %s]"),
11693 dwp_file->name);
11694 }
11695
11696 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11697 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11698 {
11699 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11700 " [in module %s]"),
11701 dwp_file->name);
11702 }
11703 }
11704
11705 if (i < 2
96b79293
TT
11706 || sections.info_or_types.empty ()
11707 || sections.abbrev.empty ())
80626a55
DE
11708 {
11709 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11710 " [in module %s]"),
11711 dwp_file->name);
11712 }
73869dc2 11713 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11714 {
11715 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11716 " [in module %s]"),
11717 dwp_file->name);
11718 }
11719
11720 /* It's easier for the rest of the code if we fake a struct dwo_file and
11721 have dwo_unit "live" in that. At least for now.
11722
11723 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11724 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11725 file, we can combine them back into a virtual DWO file to save space
11726 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11727 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11728
791afaa2
TT
11729 std::string virtual_dwo_name =
11730 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11731 sections.abbrev.get_id (),
11732 sections.line.get_id (),
11733 sections.loc.get_id (),
11734 sections.str_offsets.get_id ());
80626a55 11735 /* Can we use an existing virtual DWO file? */
976ca316 11736 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11737 comp_dir);
80626a55
DE
11738 /* Create one if necessary. */
11739 if (*dwo_file_slot == NULL)
11740 {
6f738b01
SM
11741 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11742 virtual_dwo_name.c_str ());
11743
51ac9db5 11744 dwo_file = new struct dwo_file;
976ca316 11745 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11746 dwo_file->comp_dir = comp_dir;
80626a55
DE
11747 dwo_file->sections.abbrev = sections.abbrev;
11748 dwo_file->sections.line = sections.line;
11749 dwo_file->sections.loc = sections.loc;
11750 dwo_file->sections.macinfo = sections.macinfo;
11751 dwo_file->sections.macro = sections.macro;
11752 dwo_file->sections.str_offsets = sections.str_offsets;
11753 /* The "str" section is global to the entire DWP file. */
11754 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11755 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11756 there's no need to record it in dwo_file.
11757 Also, we can't simply record type sections in dwo_file because
11758 we record a pointer into the vector in dwo_unit. As we collect more
11759 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11760 for it, invalidating all copies of pointers into the previous
11761 contents. */
80626a55
DE
11762 *dwo_file_slot = dwo_file;
11763 }
11764 else
11765 {
6f738b01
SM
11766 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11767 virtual_dwo_name.c_str ());
11768
9a3c8263 11769 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11770 }
80626a55 11771
976ca316 11772 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11773 dwo_unit->dwo_file = dwo_file;
11774 dwo_unit->signature = signature;
8d749320 11775 dwo_unit->section =
976ca316 11776 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11777 *dwo_unit->section = sections.info_or_types;
57d63ce2 11778 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11779
11780 return dwo_unit;
11781}
11782
d2854d8d
CT
11783/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11784 simplify them. Given a pointer to the containing section SECTION, and
11785 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11786 virtual section of just that piece. */
73869dc2
DE
11787
11788static struct dwarf2_section_info
d2854d8d
CT
11789create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11790 struct dwarf2_section_info *section,
11791 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
11792{
11793 struct dwarf2_section_info result;
11794 asection *sectp;
11795
11796 gdb_assert (section != NULL);
11797 gdb_assert (!section->is_virtual);
11798
11799 memset (&result, 0, sizeof (result));
11800 result.s.containing_section = section;
dc4ccb6f 11801 result.is_virtual = true;
73869dc2
DE
11802
11803 if (size == 0)
11804 return result;
11805
96b79293 11806 sectp = section->get_bfd_section ();
73869dc2
DE
11807
11808 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11809 bounds of the real section. This is a pretty-rare event, so just
11810 flag an error (easier) instead of a warning and trying to cope. */
11811 if (sectp == NULL
fd361982 11812 || offset + size > bfd_section_size (sectp))
73869dc2 11813 {
d2854d8d 11814 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 11815 " in section %s [in module %s]"),
fd361982 11816 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 11817 objfile_name (per_objfile->objfile));
73869dc2
DE
11818 }
11819
11820 result.virtual_offset = offset;
11821 result.size = size;
11822 return result;
11823}
11824
11825/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11826 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11827 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11828 This is for DWP version 2 files. */
11829
11830static struct dwo_unit *
976ca316 11831create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 11832 struct dwp_file *dwp_file,
73869dc2
DE
11833 uint32_t unit_index,
11834 const char *comp_dir,
11835 ULONGEST signature, int is_debug_types)
11836{
73869dc2
DE
11837 const struct dwp_hash_table *dwp_htab =
11838 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11839 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11840 const char *kind = is_debug_types ? "TU" : "CU";
11841 struct dwo_file *dwo_file;
11842 struct dwo_unit *dwo_unit;
d2854d8d 11843 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 11844 void **dwo_file_slot;
73869dc2
DE
11845 int i;
11846
11847 gdb_assert (dwp_file->version == 2);
11848
6f738b01
SM
11849 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11850 kind, pulongest (unit_index), hex_string (signature),
11851 dwp_file->name);
73869dc2
DE
11852
11853 /* Fetch the section offsets of this DWO unit. */
11854
11855 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11856
11857 for (i = 0; i < dwp_htab->nr_columns; ++i)
11858 {
11859 uint32_t offset = read_4_bytes (dbfd,
11860 dwp_htab->section_pool.v2.offsets
11861 + (((unit_index - 1) * dwp_htab->nr_columns
11862 + i)
11863 * sizeof (uint32_t)));
11864 uint32_t size = read_4_bytes (dbfd,
11865 dwp_htab->section_pool.v2.sizes
11866 + (((unit_index - 1) * dwp_htab->nr_columns
11867 + i)
11868 * sizeof (uint32_t)));
11869
11870 switch (dwp_htab->section_pool.v2.section_ids[i])
11871 {
11872 case DW_SECT_INFO:
11873 case DW_SECT_TYPES:
11874 sections.info_or_types_offset = offset;
11875 sections.info_or_types_size = size;
11876 break;
11877 case DW_SECT_ABBREV:
11878 sections.abbrev_offset = offset;
11879 sections.abbrev_size = size;
11880 break;
11881 case DW_SECT_LINE:
11882 sections.line_offset = offset;
11883 sections.line_size = size;
11884 break;
11885 case DW_SECT_LOC:
11886 sections.loc_offset = offset;
11887 sections.loc_size = size;
11888 break;
11889 case DW_SECT_STR_OFFSETS:
11890 sections.str_offsets_offset = offset;
11891 sections.str_offsets_size = size;
11892 break;
11893 case DW_SECT_MACINFO:
11894 sections.macinfo_offset = offset;
11895 sections.macinfo_size = size;
11896 break;
11897 case DW_SECT_MACRO:
11898 sections.macro_offset = offset;
11899 sections.macro_size = size;
11900 break;
11901 }
11902 }
11903
11904 /* It's easier for the rest of the code if we fake a struct dwo_file and
11905 have dwo_unit "live" in that. At least for now.
11906
11907 The DWP file can be made up of a random collection of CUs and TUs.
11908 However, for each CU + set of TUs that came from the same original DWO
11909 file, we can combine them back into a virtual DWO file to save space
11910 (fewer struct dwo_file objects to allocate). Remember that for really
11911 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11912
791afaa2
TT
11913 std::string virtual_dwo_name =
11914 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11915 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11916 (long) (sections.line_size ? sections.line_offset : 0),
11917 (long) (sections.loc_size ? sections.loc_offset : 0),
11918 (long) (sections.str_offsets_size
11919 ? sections.str_offsets_offset : 0));
73869dc2 11920 /* Can we use an existing virtual DWO file? */
976ca316 11921 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11922 comp_dir);
73869dc2
DE
11923 /* Create one if necessary. */
11924 if (*dwo_file_slot == NULL)
11925 {
6f738b01
SM
11926 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11927 virtual_dwo_name.c_str ());
11928
51ac9db5 11929 dwo_file = new struct dwo_file;
976ca316 11930 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
11931 dwo_file->comp_dir = comp_dir;
11932 dwo_file->sections.abbrev =
d2854d8d
CT
11933 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11934 sections.abbrev_offset,
11935 sections.abbrev_size);
73869dc2 11936 dwo_file->sections.line =
d2854d8d
CT
11937 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11938 sections.line_offset,
11939 sections.line_size);
73869dc2 11940 dwo_file->sections.loc =
d2854d8d
CT
11941 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11942 sections.loc_offset, sections.loc_size);
73869dc2 11943 dwo_file->sections.macinfo =
d2854d8d
CT
11944 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11945 sections.macinfo_offset,
11946 sections.macinfo_size);
73869dc2 11947 dwo_file->sections.macro =
d2854d8d
CT
11948 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11949 sections.macro_offset,
11950 sections.macro_size);
73869dc2 11951 dwo_file->sections.str_offsets =
d2854d8d
CT
11952 create_dwp_v2_or_v5_section (per_objfile,
11953 &dwp_file->sections.str_offsets,
11954 sections.str_offsets_offset,
11955 sections.str_offsets_size);
73869dc2
DE
11956 /* The "str" section is global to the entire DWP file. */
11957 dwo_file->sections.str = dwp_file->sections.str;
11958 /* The info or types section is assigned below to dwo_unit,
11959 there's no need to record it in dwo_file.
11960 Also, we can't simply record type sections in dwo_file because
11961 we record a pointer into the vector in dwo_unit. As we collect more
11962 types we'll grow the vector and eventually have to reallocate space
11963 for it, invalidating all copies of pointers into the previous
11964 contents. */
11965 *dwo_file_slot = dwo_file;
11966 }
11967 else
11968 {
6f738b01
SM
11969 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11970 virtual_dwo_name.c_str ());
11971
9a3c8263 11972 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11973 }
73869dc2 11974
976ca316 11975 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
11976 dwo_unit->dwo_file = dwo_file;
11977 dwo_unit->signature = signature;
8d749320 11978 dwo_unit->section =
976ca316 11979 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 11980 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 11981 (per_objfile,
d2854d8d
CT
11982 is_debug_types
11983 ? &dwp_file->sections.types
11984 : &dwp_file->sections.info,
11985 sections.info_or_types_offset,
11986 sections.info_or_types_size);
11987 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11988
11989 return dwo_unit;
11990}
11991
11992/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11993 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11994 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11995 This is for DWP version 5 files. */
11996
11997static struct dwo_unit *
11998create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11999 struct dwp_file *dwp_file,
12000 uint32_t unit_index,
12001 const char *comp_dir,
12002 ULONGEST signature, int is_debug_types)
12003{
12004 const struct dwp_hash_table *dwp_htab
12005 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12006 bfd *dbfd = dwp_file->dbfd.get ();
12007 const char *kind = is_debug_types ? "TU" : "CU";
12008 struct dwo_file *dwo_file;
12009 struct dwo_unit *dwo_unit;
12010 struct virtual_v2_or_v5_dwo_sections sections {};
12011 void **dwo_file_slot;
12012
12013 gdb_assert (dwp_file->version == 5);
12014
6f738b01
SM
12015 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12016 kind, pulongest (unit_index), hex_string (signature),
12017 dwp_file->name);
d2854d8d
CT
12018
12019 /* Fetch the section offsets of this DWO unit. */
12020
12021 /* memset (&sections, 0, sizeof (sections)); */
12022
12023 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12024 {
12025 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12026 dwp_htab->section_pool.v5.offsets
12027 + (((unit_index - 1)
12028 * dwp_htab->nr_columns
12029 + i)
12030 * sizeof (uint32_t)));
d2854d8d 12031 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12032 dwp_htab->section_pool.v5.sizes
12033 + (((unit_index - 1) * dwp_htab->nr_columns
12034 + i)
12035 * sizeof (uint32_t)));
d2854d8d
CT
12036
12037 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12038 {
12039 case DW_SECT_ABBREV_V5:
12040 sections.abbrev_offset = offset;
12041 sections.abbrev_size = size;
12042 break;
12043 case DW_SECT_INFO_V5:
12044 sections.info_or_types_offset = offset;
12045 sections.info_or_types_size = size;
12046 break;
12047 case DW_SECT_LINE_V5:
12048 sections.line_offset = offset;
12049 sections.line_size = size;
12050 break;
12051 case DW_SECT_LOCLISTS_V5:
12052 sections.loclists_offset = offset;
12053 sections.loclists_size = size;
12054 break;
12055 case DW_SECT_MACRO_V5:
12056 sections.macro_offset = offset;
12057 sections.macro_size = size;
12058 break;
12059 case DW_SECT_RNGLISTS_V5:
12060 sections.rnglists_offset = offset;
12061 sections.rnglists_size = size;
12062 break;
12063 case DW_SECT_STR_OFFSETS_V5:
12064 sections.str_offsets_offset = offset;
12065 sections.str_offsets_size = size;
12066 break;
12067 case DW_SECT_RESERVED_V5:
12068 default:
12069 break;
12070 }
d2854d8d
CT
12071 }
12072
12073 /* It's easier for the rest of the code if we fake a struct dwo_file and
12074 have dwo_unit "live" in that. At least for now.
12075
12076 The DWP file can be made up of a random collection of CUs and TUs.
12077 However, for each CU + set of TUs that came from the same original DWO
12078 file, we can combine them back into a virtual DWO file to save space
12079 (fewer struct dwo_file objects to allocate). Remember that for really
12080 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12081
12082 std::string virtual_dwo_name =
12083 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12084 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12085 (long) (sections.line_size ? sections.line_offset : 0),
12086 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12087 (long) (sections.str_offsets_size
12088 ? sections.str_offsets_offset : 0),
12089 (long) (sections.macro_size ? sections.macro_offset : 0),
12090 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12091 /* Can we use an existing virtual DWO file? */
12092 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12093 virtual_dwo_name.c_str (),
12094 comp_dir);
d2854d8d
CT
12095 /* Create one if necessary. */
12096 if (*dwo_file_slot == NULL)
12097 {
6f738b01
SM
12098 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12099 virtual_dwo_name.c_str ());
12100
d2854d8d
CT
12101 dwo_file = new struct dwo_file;
12102 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12103 dwo_file->comp_dir = comp_dir;
12104 dwo_file->sections.abbrev =
dda83cd7
SM
12105 create_dwp_v2_or_v5_section (per_objfile,
12106 &dwp_file->sections.abbrev,
12107 sections.abbrev_offset,
12108 sections.abbrev_size);
d2854d8d 12109 dwo_file->sections.line =
dda83cd7
SM
12110 create_dwp_v2_or_v5_section (per_objfile,
12111 &dwp_file->sections.line,
12112 sections.line_offset, sections.line_size);
d2854d8d 12113 dwo_file->sections.macro =
dda83cd7
SM
12114 create_dwp_v2_or_v5_section (per_objfile,
12115 &dwp_file->sections.macro,
12116 sections.macro_offset,
12117 sections.macro_size);
d2854d8d 12118 dwo_file->sections.loclists =
dda83cd7
SM
12119 create_dwp_v2_or_v5_section (per_objfile,
12120 &dwp_file->sections.loclists,
12121 sections.loclists_offset,
12122 sections.loclists_size);
d2854d8d 12123 dwo_file->sections.rnglists =
dda83cd7
SM
12124 create_dwp_v2_or_v5_section (per_objfile,
12125 &dwp_file->sections.rnglists,
12126 sections.rnglists_offset,
12127 sections.rnglists_size);
d2854d8d 12128 dwo_file->sections.str_offsets =
dda83cd7
SM
12129 create_dwp_v2_or_v5_section (per_objfile,
12130 &dwp_file->sections.str_offsets,
12131 sections.str_offsets_offset,
12132 sections.str_offsets_size);
d2854d8d
CT
12133 /* The "str" section is global to the entire DWP file. */
12134 dwo_file->sections.str = dwp_file->sections.str;
12135 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12136 there's no need to record it in dwo_file.
12137 Also, we can't simply record type sections in dwo_file because
12138 we record a pointer into the vector in dwo_unit. As we collect more
12139 types we'll grow the vector and eventually have to reallocate space
12140 for it, invalidating all copies of pointers into the previous
12141 contents. */
d2854d8d
CT
12142 *dwo_file_slot = dwo_file;
12143 }
12144 else
12145 {
6f738b01
SM
12146 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12147 virtual_dwo_name.c_str ());
12148
d2854d8d
CT
12149 dwo_file = (struct dwo_file *) *dwo_file_slot;
12150 }
12151
12152 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12153 dwo_unit->dwo_file = dwo_file;
12154 dwo_unit->signature = signature;
12155 dwo_unit->section
12156 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12157 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12158 &dwp_file->sections.info,
12159 sections.info_or_types_offset,
12160 sections.info_or_types_size);
73869dc2
DE
12161 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12162
12163 return dwo_unit;
12164}
12165
57d63ce2
DE
12166/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12167 Returns NULL if the signature isn't found. */
80626a55
DE
12168
12169static struct dwo_unit *
976ca316 12170lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12171 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12172 ULONGEST signature, int is_debug_types)
80626a55 12173{
57d63ce2
DE
12174 const struct dwp_hash_table *dwp_htab =
12175 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12176 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12177 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12178 uint32_t hash = signature & mask;
12179 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12180 unsigned int i;
12181 void **slot;
870f88f7 12182 struct dwo_unit find_dwo_cu;
80626a55
DE
12183
12184 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12185 find_dwo_cu.signature = signature;
19ac8c2e 12186 slot = htab_find_slot (is_debug_types
48b490f2
TT
12187 ? dwp_file->loaded_tus.get ()
12188 : dwp_file->loaded_cus.get (),
19ac8c2e 12189 &find_dwo_cu, INSERT);
80626a55
DE
12190
12191 if (*slot != NULL)
9a3c8263 12192 return (struct dwo_unit *) *slot;
80626a55
DE
12193
12194 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12195 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12196 {
12197 ULONGEST signature_in_table;
12198
12199 signature_in_table =
57d63ce2 12200 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12201 if (signature_in_table == signature)
12202 {
57d63ce2
DE
12203 uint32_t unit_index =
12204 read_4_bytes (dbfd,
12205 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12206
73869dc2
DE
12207 if (dwp_file->version == 1)
12208 {
976ca316
SM
12209 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12210 unit_index, comp_dir,
12211 signature, is_debug_types);
73869dc2 12212 }
d2854d8d 12213 else if (dwp_file->version == 2)
73869dc2 12214 {
976ca316
SM
12215 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12216 unit_index, comp_dir,
12217 signature, is_debug_types);
73869dc2 12218 }
d2854d8d
CT
12219 else /* version == 5 */
12220 {
12221 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12222 unit_index, comp_dir,
12223 signature, is_debug_types);
12224 }
9a3c8263 12225 return (struct dwo_unit *) *slot;
80626a55
DE
12226 }
12227 if (signature_in_table == 0)
12228 return NULL;
12229 hash = (hash + hash2) & mask;
12230 }
12231
12232 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12233 " [in module %s]"),
12234 dwp_file->name);
12235}
12236
ab5088bf 12237/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12238 Open the file specified by FILE_NAME and hand it off to BFD for
12239 preliminary analysis. Return a newly initialized bfd *, which
12240 includes a canonicalized copy of FILE_NAME.
80626a55 12241 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12242 SEARCH_CWD is true if the current directory is to be searched.
12243 It will be searched before debug-file-directory.
13aaf454
DE
12244 If successful, the file is added to the bfd include table of the
12245 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12246 If unable to find/open the file, return NULL.
3019eac3
DE
12247 NOTE: This function is derived from symfile_bfd_open. */
12248
192b62ce 12249static gdb_bfd_ref_ptr
976ca316 12250try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12251 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12252{
24b9144d 12253 int desc;
9c02c129
DE
12254 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12255 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12256 to debug_file_directory. */
e0cc99a6 12257 const char *search_path;
9c02c129
DE
12258 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12259
e0cc99a6 12260 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12261 if (search_cwd)
12262 {
12263 if (*debug_file_directory != '\0')
e0cc99a6
TT
12264 {
12265 search_path_holder.reset (concat (".", dirname_separator_string,
12266 debug_file_directory,
12267 (char *) NULL));
12268 search_path = search_path_holder.get ();
12269 }
6ac97d4c 12270 else
e0cc99a6 12271 search_path = ".";
6ac97d4c 12272 }
9c02c129 12273 else
e0cc99a6 12274 search_path = debug_file_directory;
3019eac3 12275
56d467f4
CT
12276 /* Add the path for the executable binary to the list of search paths. */
12277 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12278 search_path_holder.reset (concat (objfile_dir.c_str (),
12279 dirname_separator_string,
12280 search_path, nullptr));
12281 search_path = search_path_holder.get ();
12282
24b9144d 12283 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12284 if (is_dwp)
12285 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12286
12287 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12288 desc = openp (search_path, flags, file_name,
3019eac3
DE
12289 O_RDONLY | O_BINARY, &absolute_name);
12290 if (desc < 0)
12291 return NULL;
12292
e0cc99a6
TT
12293 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12294 gnutarget, desc));
9c02c129
DE
12295 if (sym_bfd == NULL)
12296 return NULL;
192b62ce 12297 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12298
192b62ce
TT
12299 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12300 return NULL;
3019eac3 12301
13aaf454
DE
12302 /* Success. Record the bfd as having been included by the objfile's bfd.
12303 This is important because things like demangled_names_hash lives in the
12304 objfile's per_bfd space and may have references to things like symbol
12305 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12306 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12307
3019eac3
DE
12308 return sym_bfd;
12309}
12310
ab5088bf 12311/* Try to open DWO file FILE_NAME.
3019eac3
DE
12312 COMP_DIR is the DW_AT_comp_dir attribute.
12313 The result is the bfd handle of the file.
12314 If there is a problem finding or opening the file, return NULL.
12315 Upon success, the canonicalized path of the file is stored in the bfd,
12316 same as symfile_bfd_open. */
12317
192b62ce 12318static gdb_bfd_ref_ptr
976ca316 12319open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12320 const char *file_name, const char *comp_dir)
3019eac3 12321{
80626a55 12322 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12323 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12324 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12325
12326 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12327
12328 if (comp_dir != NULL)
12329 {
43816ebc
TT
12330 gdb::unique_xmalloc_ptr<char> path_to_try
12331 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12332
12333 /* NOTE: If comp_dir is a relative path, this will also try the
12334 search path, which seems useful. */
976ca316 12335 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12336 0 /*is_dwp*/,
192b62ce 12337 1 /*search_cwd*/));
3019eac3
DE
12338 if (abfd != NULL)
12339 return abfd;
12340 }
12341
12342 /* That didn't work, try debug-file-directory, which, despite its name,
12343 is a list of paths. */
12344
12345 if (*debug_file_directory == '\0')
12346 return NULL;
12347
976ca316 12348 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12349 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12350}
12351
80626a55
DE
12352/* This function is mapped across the sections and remembers the offset and
12353 size of each of the DWO debugging sections we are interested in. */
12354
12355static void
5bb6e9dd
TT
12356dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12357 dwo_sections *dwo_sections)
80626a55 12358{
80626a55
DE
12359 const struct dwop_section_names *names = &dwop_section_names;
12360
fbedd546 12361 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12362 {
049412e3 12363 dwo_sections->abbrev.s.section = sectp;
fd361982 12364 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12365 }
fbedd546 12366 else if (names->info_dwo.matches (sectp->name))
80626a55 12367 {
049412e3 12368 dwo_sections->info.s.section = sectp;
fd361982 12369 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12370 }
fbedd546 12371 else if (names->line_dwo.matches (sectp->name))
80626a55 12372 {
049412e3 12373 dwo_sections->line.s.section = sectp;
fd361982 12374 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12375 }
fbedd546 12376 else if (names->loc_dwo.matches (sectp->name))
80626a55 12377 {
049412e3 12378 dwo_sections->loc.s.section = sectp;
fd361982 12379 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12380 }
fbedd546 12381 else if (names->loclists_dwo.matches (sectp->name))
41144253 12382 {
12383 dwo_sections->loclists.s.section = sectp;
12384 dwo_sections->loclists.size = bfd_section_size (sectp);
12385 }
fbedd546 12386 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12387 {
049412e3 12388 dwo_sections->macinfo.s.section = sectp;
fd361982 12389 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12390 }
fbedd546 12391 else if (names->macro_dwo.matches (sectp->name))
80626a55 12392 {
049412e3 12393 dwo_sections->macro.s.section = sectp;
fd361982 12394 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12395 }
fbedd546 12396 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12397 {
12398 dwo_sections->rnglists.s.section = sectp;
12399 dwo_sections->rnglists.size = bfd_section_size (sectp);
12400 }
fbedd546 12401 else if (names->str_dwo.matches (sectp->name))
80626a55 12402 {
049412e3 12403 dwo_sections->str.s.section = sectp;
fd361982 12404 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12405 }
fbedd546 12406 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12407 {
049412e3 12408 dwo_sections->str_offsets.s.section = sectp;
fd361982 12409 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12410 }
fbedd546 12411 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12412 {
12413 struct dwarf2_section_info type_section;
12414
12415 memset (&type_section, 0, sizeof (type_section));
049412e3 12416 type_section.s.section = sectp;
fd361982 12417 type_section.size = bfd_section_size (sectp);
fd5866f6 12418 dwo_sections->types.push_back (type_section);
80626a55
DE
12419 }
12420}
12421
ab5088bf 12422/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12423 by PER_CU. This is for the non-DWP case.
80626a55 12424 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12425
12426static struct dwo_file *
4ab09049
SM
12427open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12428 const char *comp_dir)
3019eac3 12429{
976ca316 12430 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12431
976ca316 12432 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12433 if (dbfd == NULL)
12434 {
6f738b01
SM
12435 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12436
80626a55
DE
12437 return NULL;
12438 }
263db9a1 12439
51ac9db5 12440 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12441 dwo_file->dwo_name = dwo_name;
12442 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12443 dwo_file->dbfd = std::move (dbfd);
3019eac3 12444
5bb6e9dd
TT
12445 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12446 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12447 &dwo_file->sections);
3019eac3 12448
976ca316
SM
12449 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12450 dwo_file->cus);
3019eac3 12451
d2854d8d
CT
12452 if (cu->per_cu->dwarf_version < 5)
12453 {
12454 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12455 dwo_file->sections.types, dwo_file->tus);
12456 }
12457 else
12458 {
12459 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12460 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12461 rcuh_kind::COMPILE);
d2854d8d 12462 }
3019eac3 12463
6f738b01 12464 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12465
263db9a1 12466 return dwo_file.release ();
3019eac3
DE
12467}
12468
80626a55 12469/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12470 size of each of the DWP debugging sections common to version 1 and 2 that
12471 we are interested in. */
3019eac3 12472
80626a55 12473static void
73869dc2 12474dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12475 dwp_file *dwp_file)
3019eac3 12476{
80626a55
DE
12477 const struct dwop_section_names *names = &dwop_section_names;
12478 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12479
80626a55 12480 /* Record the ELF section number for later lookup: this is what the
73869dc2 12481 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12482 gdb_assert (elf_section_nr < dwp_file->num_sections);
12483 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12484
80626a55 12485 /* Look for specific sections that we need. */
fbedd546 12486 if (names->str_dwo.matches (sectp->name))
80626a55 12487 {
049412e3 12488 dwp_file->sections.str.s.section = sectp;
fd361982 12489 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12490 }
fbedd546 12491 else if (names->cu_index.matches (sectp->name))
80626a55 12492 {
049412e3 12493 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12494 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12495 }
fbedd546 12496 else if (names->tu_index.matches (sectp->name))
80626a55 12497 {
049412e3 12498 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12499 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12500 }
12501}
3019eac3 12502
73869dc2
DE
12503/* This function is mapped across the sections and remembers the offset and
12504 size of each of the DWP version 2 debugging sections that we are interested
12505 in. This is split into a separate function because we don't know if we
d2854d8d 12506 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12507
12508static void
12509dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12510{
9a3c8263 12511 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12512 const struct dwop_section_names *names = &dwop_section_names;
12513 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12514
12515 /* Record the ELF section number for later lookup: this is what the
12516 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12517 gdb_assert (elf_section_nr < dwp_file->num_sections);
12518 dwp_file->elf_sections[elf_section_nr] = sectp;
12519
12520 /* Look for specific sections that we need. */
fbedd546 12521 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12522 {
049412e3 12523 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12524 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12525 }
fbedd546 12526 else if (names->info_dwo.matches (sectp->name))
73869dc2 12527 {
049412e3 12528 dwp_file->sections.info.s.section = sectp;
fd361982 12529 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12530 }
fbedd546 12531 else if (names->line_dwo.matches (sectp->name))
73869dc2 12532 {
049412e3 12533 dwp_file->sections.line.s.section = sectp;
fd361982 12534 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 12535 }
fbedd546 12536 else if (names->loc_dwo.matches (sectp->name))
73869dc2 12537 {
049412e3 12538 dwp_file->sections.loc.s.section = sectp;
fd361982 12539 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 12540 }
fbedd546 12541 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 12542 {
049412e3 12543 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12544 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 12545 }
fbedd546 12546 else if (names->macro_dwo.matches (sectp->name))
73869dc2 12547 {
049412e3 12548 dwp_file->sections.macro.s.section = sectp;
fd361982 12549 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 12550 }
fbedd546 12551 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 12552 {
049412e3 12553 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12554 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 12555 }
fbedd546 12556 else if (names->types_dwo.matches (sectp->name))
73869dc2 12557 {
049412e3 12558 dwp_file->sections.types.s.section = sectp;
fd361982 12559 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12560 }
12561}
12562
d2854d8d
CT
12563/* This function is mapped across the sections and remembers the offset and
12564 size of each of the DWP version 5 debugging sections that we are interested
12565 in. This is split into a separate function because we don't know if we
12566 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12567
12568static void
12569dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12570{
12571 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12572 const struct dwop_section_names *names = &dwop_section_names;
12573 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12574
12575 /* Record the ELF section number for later lookup: this is what the
12576 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12577 gdb_assert (elf_section_nr < dwp_file->num_sections);
12578 dwp_file->elf_sections[elf_section_nr] = sectp;
12579
12580 /* Look for specific sections that we need. */
fbedd546 12581 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
12582 {
12583 dwp_file->sections.abbrev.s.section = sectp;
12584 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12585 }
fbedd546 12586 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
12587 {
12588 dwp_file->sections.info.s.section = sectp;
12589 dwp_file->sections.info.size = bfd_section_size (sectp);
12590 }
fbedd546 12591 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
12592 {
12593 dwp_file->sections.line.s.section = sectp;
12594 dwp_file->sections.line.size = bfd_section_size (sectp);
12595 }
fbedd546 12596 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
12597 {
12598 dwp_file->sections.loclists.s.section = sectp;
12599 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12600 }
fbedd546 12601 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
12602 {
12603 dwp_file->sections.macro.s.section = sectp;
12604 dwp_file->sections.macro.size = bfd_section_size (sectp);
12605 }
fbedd546 12606 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
12607 {
12608 dwp_file->sections.rnglists.s.section = sectp;
12609 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12610 }
fbedd546 12611 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
12612 {
12613 dwp_file->sections.str_offsets.s.section = sectp;
12614 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12615 }
12616}
12617
80626a55 12618/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12619
80626a55
DE
12620static hashval_t
12621hash_dwp_loaded_cutus (const void *item)
12622{
9a3c8263 12623 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12624
80626a55
DE
12625 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12626 return dwo_unit->signature;
3019eac3
DE
12627}
12628
80626a55 12629/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12630
80626a55
DE
12631static int
12632eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12633{
9a3c8263
SM
12634 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12635 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12636
80626a55
DE
12637 return dua->signature == dub->signature;
12638}
3019eac3 12639
80626a55 12640/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12641
48b490f2 12642static htab_up
298e9637 12643allocate_dwp_loaded_cutus_table ()
80626a55 12644{
48b490f2
TT
12645 return htab_up (htab_create_alloc (3,
12646 hash_dwp_loaded_cutus,
12647 eq_dwp_loaded_cutus,
12648 NULL, xcalloc, xfree));
80626a55 12649}
3019eac3 12650
ab5088bf
DE
12651/* Try to open DWP file FILE_NAME.
12652 The result is the bfd handle of the file.
12653 If there is a problem finding or opening the file, return NULL.
12654 Upon success, the canonicalized path of the file is stored in the bfd,
12655 same as symfile_bfd_open. */
12656
192b62ce 12657static gdb_bfd_ref_ptr
976ca316 12658open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12659{
976ca316 12660 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12661 1 /*is_dwp*/,
192b62ce 12662 1 /*search_cwd*/));
6ac97d4c
DE
12663 if (abfd != NULL)
12664 return abfd;
12665
12666 /* Work around upstream bug 15652.
12667 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12668 [Whether that's a "bug" is debatable, but it is getting in our way.]
12669 We have no real idea where the dwp file is, because gdb's realpath-ing
12670 of the executable's path may have discarded the needed info.
12671 [IWBN if the dwp file name was recorded in the executable, akin to
12672 .gnu_debuglink, but that doesn't exist yet.]
12673 Strip the directory from FILE_NAME and search again. */
12674 if (*debug_file_directory != '\0')
12675 {
12676 /* Don't implicitly search the current directory here.
12677 If the user wants to search "." to handle this case,
12678 it must be added to debug-file-directory. */
976ca316
SM
12679 return try_open_dwop_file (per_objfile, lbasename (file_name),
12680 1 /*is_dwp*/,
6ac97d4c
DE
12681 0 /*search_cwd*/);
12682 }
12683
12684 return NULL;
ab5088bf
DE
12685}
12686
80626a55
DE
12687/* Initialize the use of the DWP file for the current objfile.
12688 By convention the name of the DWP file is ${objfile}.dwp.
12689 The result is NULL if it can't be found. */
a766d390 12690
400174b1 12691static std::unique_ptr<struct dwp_file>
976ca316 12692open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12693{
976ca316 12694 struct objfile *objfile = per_objfile->objfile;
80626a55 12695
82bf32bc
JK
12696 /* Try to find first .dwp for the binary file before any symbolic links
12697 resolving. */
6c447423
DE
12698
12699 /* If the objfile is a debug file, find the name of the real binary
12700 file and get the name of dwp file from there. */
d721ba37 12701 std::string dwp_name;
6c447423
DE
12702 if (objfile->separate_debug_objfile_backlink != NULL)
12703 {
12704 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12705 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12706
d721ba37 12707 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12708 }
12709 else
d721ba37
PA
12710 dwp_name = objfile->original_name;
12711
12712 dwp_name += ".dwp";
80626a55 12713
976ca316 12714 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12715 if (dbfd == NULL
12716 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12717 {
12718 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12719 dwp_name = objfile_name (objfile);
12720 dwp_name += ".dwp";
976ca316 12721 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12722 }
12723
80626a55
DE
12724 if (dbfd == NULL)
12725 {
6f738b01
SM
12726 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12727
400174b1 12728 return std::unique_ptr<dwp_file> ();
3019eac3 12729 }
400174b1
TT
12730
12731 const char *name = bfd_get_filename (dbfd.get ());
12732 std::unique_ptr<struct dwp_file> dwp_file
12733 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12734
0a0f4c01 12735 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12736 dwp_file->elf_sections =
976ca316 12737 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12738 dwp_file->num_sections, asection *);
12739
5bb6e9dd
TT
12740 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12741 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12742 dwp_file.get ());
80626a55 12743
976ca316 12744 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12745
976ca316 12746 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12747
73869dc2 12748 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12749 if (dwp_file->cus && dwp_file->tus
12750 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12751 {
12752 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12753 pretty bizarre. We use pulongest here because that's the established
4d65956b 12754 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12755 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12756 " TU version %s [in DWP file %s]"),
12757 pulongest (dwp_file->cus->version),
d721ba37 12758 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12759 }
08302ed2
DE
12760
12761 if (dwp_file->cus)
12762 dwp_file->version = dwp_file->cus->version;
12763 else if (dwp_file->tus)
12764 dwp_file->version = dwp_file->tus->version;
12765 else
12766 dwp_file->version = 2;
73869dc2 12767
5bb6e9dd
TT
12768 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12769 {
12770 if (dwp_file->version == 2)
12771 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12772 dwp_file.get ());
12773 else
12774 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12775 dwp_file.get ());
12776 }
73869dc2 12777
298e9637
SM
12778 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12779 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12780
6f738b01
SM
12781 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12782 dwarf_read_debug_printf (" %s CUs, %s TUs",
12783 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12784 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12785
12786 return dwp_file;
3019eac3 12787}
c906108c 12788
ab5088bf
DE
12789/* Wrapper around open_and_init_dwp_file, only open it once. */
12790
12791static struct dwp_file *
976ca316 12792get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12793{
976ca316 12794 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12795 {
976ca316
SM
12796 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12797 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12798 }
976ca316 12799 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12800}
12801
80626a55
DE
12802/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12803 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12804 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12805 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12806 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12807
12808 This is called, for example, when wanting to read a variable with a
12809 complex location. Therefore we don't want to do file i/o for every call.
12810 Therefore we don't want to look for a DWO file on every call.
12811 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12812 then we check if we've already seen DWO_NAME, and only THEN do we check
12813 for a DWO file.
12814
1c658ad5 12815 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12816 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12817
3019eac3 12818static struct dwo_unit *
4ab09049 12819lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12820 ULONGEST signature, int is_debug_types)
3019eac3 12821{
976ca316
SM
12822 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12823 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12824 const char *kind = is_debug_types ? "TU" : "CU";
12825 void **dwo_file_slot;
3019eac3 12826 struct dwo_file *dwo_file;
80626a55 12827 struct dwp_file *dwp_file;
cb1df416 12828
6a506a2d
DE
12829 /* First see if there's a DWP file.
12830 If we have a DWP file but didn't find the DWO inside it, don't
12831 look for the original DWO file. It makes gdb behave differently
12832 depending on whether one is debugging in the build tree. */
cf2c3c16 12833
976ca316 12834 dwp_file = get_dwp_file (per_objfile);
80626a55 12835 if (dwp_file != NULL)
cf2c3c16 12836 {
80626a55
DE
12837 const struct dwp_hash_table *dwp_htab =
12838 is_debug_types ? dwp_file->tus : dwp_file->cus;
12839
12840 if (dwp_htab != NULL)
12841 {
12842 struct dwo_unit *dwo_cutu =
976ca316
SM
12843 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12844 is_debug_types);
80626a55
DE
12845
12846 if (dwo_cutu != NULL)
12847 {
6f738b01
SM
12848 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12849 kind, hex_string (signature),
12850 host_address_to_string (dwo_cutu));
12851
80626a55
DE
12852 return dwo_cutu;
12853 }
12854 }
12855 }
6a506a2d 12856 else
80626a55 12857 {
6a506a2d 12858 /* No DWP file, look for the DWO file. */
80626a55 12859
976ca316 12860 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 12861 if (*dwo_file_slot == NULL)
80626a55 12862 {
6a506a2d 12863 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12864 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12865 }
6a506a2d 12866 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12867 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12868
6a506a2d 12869 if (dwo_file != NULL)
19c3d4c9 12870 {
6a506a2d
DE
12871 struct dwo_unit *dwo_cutu = NULL;
12872
12873 if (is_debug_types && dwo_file->tus)
12874 {
12875 struct dwo_unit find_dwo_cutu;
12876
12877 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12878 find_dwo_cutu.signature = signature;
9a3c8263 12879 dwo_cutu
b0b6a987
TT
12880 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12881 &find_dwo_cutu);
6a506a2d 12882 }
33c5cd75 12883 else if (!is_debug_types && dwo_file->cus)
80626a55 12884 {
33c5cd75
DB
12885 struct dwo_unit find_dwo_cutu;
12886
12887 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12888 find_dwo_cutu.signature = signature;
b0b6a987 12889 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12890 &find_dwo_cutu);
6a506a2d
DE
12891 }
12892
12893 if (dwo_cutu != NULL)
12894 {
6f738b01
SM
12895 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12896 kind, dwo_name, hex_string (signature),
12897 host_address_to_string (dwo_cutu));
12898
6a506a2d 12899 return dwo_cutu;
80626a55
DE
12900 }
12901 }
2e276125 12902 }
9cdd5dbd 12903
80626a55
DE
12904 /* We didn't find it. This could mean a dwo_id mismatch, or
12905 someone deleted the DWO/DWP file, or the search path isn't set up
12906 correctly to find the file. */
12907
6f738b01
SM
12908 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12909 kind, dwo_name, hex_string (signature));
3019eac3 12910
6656a72d
DE
12911 /* This is a warning and not a complaint because it can be caused by
12912 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12913 {
12914 /* Print the name of the DWP file if we looked there, helps the user
12915 better diagnose the problem. */
791afaa2 12916 std::string dwp_text;
43942612
DE
12917
12918 if (dwp_file != NULL)
791afaa2
TT
12919 dwp_text = string_printf (" [in DWP file %s]",
12920 lbasename (dwp_file->name));
43942612 12921
9d8780f0 12922 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12923 " [in module %s]"),
4ab09049
SM
12924 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12925 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12926 }
3019eac3 12927 return NULL;
5fb290d7
DJ
12928}
12929
80626a55
DE
12930/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12931 See lookup_dwo_cutu_unit for details. */
12932
12933static struct dwo_unit *
4ab09049 12934lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12935 ULONGEST signature)
12936{
4ab09049
SM
12937 gdb_assert (!cu->per_cu->is_debug_types);
12938
12939 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12940}
12941
12942/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12943 See lookup_dwo_cutu_unit for details. */
12944
12945static struct dwo_unit *
4ab09049 12946lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12947{
4ab09049
SM
12948 gdb_assert (cu->per_cu->is_debug_types);
12949
12950 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12951
12952 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12953}
12954
89e63ee4
DE
12955/* Traversal function for queue_and_load_all_dwo_tus. */
12956
12957static int
12958queue_and_load_dwo_tu (void **slot, void *info)
12959{
12960 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12961 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12962 ULONGEST signature = dwo_unit->signature;
d460f660 12963 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12964
12965 if (sig_type != NULL)
12966 {
89e63ee4
DE
12967 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12968 a real dependency of PER_CU on SIG_TYPE. That is detected later
12969 while processing PER_CU. */
0d305d5c
TT
12970 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12971 cu->language))
12972 load_full_type_unit (sig_type, cu->per_objfile);
12973 cu->per_cu->imported_symtabs_push (sig_type);
89e63ee4
DE
12974 }
12975
12976 return 1;
12977}
12978
1b555f17 12979/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12980 The DWO may have the only definition of the type, though it may not be
12981 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12982 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12983
12984static void
1b555f17 12985queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12986{
12987 struct dwo_unit *dwo_unit;
12988 struct dwo_file *dwo_file;
12989
1b555f17
SM
12990 gdb_assert (cu != nullptr);
12991 gdb_assert (!cu->per_cu->is_debug_types);
12992 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12993
1b555f17 12994 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12995 gdb_assert (dwo_unit != NULL);
12996
12997 dwo_file = dwo_unit->dwo_file;
12998 if (dwo_file->tus != NULL)
1b555f17 12999 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13000}
13001
3019eac3 13002/* Read in various DIEs. */
348e048f 13003
d389af10 13004/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13005 Inherit only the children of the DW_AT_abstract_origin DIE not being
13006 already referenced by DW_AT_abstract_origin from the children of the
13007 current DIE. */
d389af10
JK
13008
13009static void
13010inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13011{
13012 struct die_info *child_die;
791afaa2 13013 sect_offset *offsetp;
d389af10
JK
13014 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13015 struct die_info *origin_die;
13016 /* Iterator of the ORIGIN_DIE children. */
13017 struct die_info *origin_child_die;
d389af10 13018 struct attribute *attr;
cd02d79d
PA
13019 struct dwarf2_cu *origin_cu;
13020 struct pending **origin_previous_list_in_scope;
d389af10
JK
13021
13022 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13023 if (!attr)
13024 return;
13025
cd02d79d
PA
13026 /* Note that following die references may follow to a die in a
13027 different cu. */
13028
13029 origin_cu = cu;
13030 origin_die = follow_die_ref (die, attr, &origin_cu);
13031
13032 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13033 symbols in. */
13034 origin_previous_list_in_scope = origin_cu->list_in_scope;
13035 origin_cu->list_in_scope = cu->list_in_scope;
13036
edb3359d
DJ
13037 if (die->tag != origin_die->tag
13038 && !(die->tag == DW_TAG_inlined_subroutine
13039 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13040 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13041 sect_offset_str (die->sect_off),
13042 sect_offset_str (origin_die->sect_off));
d389af10 13043
34dc0f95
TBA
13044 /* Find if the concrete and abstract trees are structurally the
13045 same. This is a shallow traversal and it is not bullet-proof;
13046 the compiler can trick the debugger into believing that the trees
13047 are isomorphic, whereas they actually are not. However, the
13048 likelyhood of this happening is pretty low, and a full-fledged
13049 check would be an overkill. */
13050 bool are_isomorphic = true;
13051 die_info *concrete_child = die->child;
13052 die_info *abstract_child = origin_die->child;
13053 while (concrete_child != nullptr || abstract_child != nullptr)
13054 {
13055 if (concrete_child == nullptr
13056 || abstract_child == nullptr
13057 || concrete_child->tag != abstract_child->tag)
13058 {
13059 are_isomorphic = false;
13060 break;
13061 }
13062
13063 concrete_child = concrete_child->sibling;
13064 abstract_child = abstract_child->sibling;
13065 }
13066
13067 /* Walk the origin's children in parallel to the concrete children.
13068 This helps match an origin child in case the debug info misses
13069 DW_AT_abstract_origin attributes. Keep in mind that the abstract
13070 origin tree may not have the same tree structure as the concrete
13071 DIE, though. */
13072 die_info *corresponding_abstract_child
13073 = are_isomorphic ? origin_die->child : nullptr;
13074
791afaa2 13075 std::vector<sect_offset> offsets;
d389af10 13076
3ea89b92
PMR
13077 for (child_die = die->child;
13078 child_die && child_die->tag;
436c571c 13079 child_die = child_die->sibling)
3ea89b92
PMR
13080 {
13081 struct die_info *child_origin_die;
13082 struct dwarf2_cu *child_origin_cu;
13083
13084 /* We are trying to process concrete instance entries:
216f72a1 13085 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13086 it's not relevant to our analysis here. i.e. detecting DIEs that are
13087 present in the abstract instance but not referenced in the concrete
13088 one. */
216f72a1 13089 if (child_die->tag == DW_TAG_call_site
dda83cd7 13090 || child_die->tag == DW_TAG_GNU_call_site)
34dc0f95
TBA
13091 {
13092 if (are_isomorphic)
13093 corresponding_abstract_child
13094 = corresponding_abstract_child->sibling;
13095 continue;
13096 }
3ea89b92 13097
c38f313d
DJ
13098 /* For each CHILD_DIE, find the corresponding child of
13099 ORIGIN_DIE. If there is more than one layer of
13100 DW_AT_abstract_origin, follow them all; there shouldn't be,
13101 but GCC versions at least through 4.4 generate this (GCC PR
13102 40573). */
3ea89b92
PMR
13103 child_origin_die = child_die;
13104 child_origin_cu = cu;
c38f313d
DJ
13105 while (1)
13106 {
cd02d79d
PA
13107 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13108 child_origin_cu);
c38f313d
DJ
13109 if (attr == NULL)
13110 break;
cd02d79d
PA
13111 child_origin_die = follow_die_ref (child_origin_die, attr,
13112 &child_origin_cu);
c38f313d
DJ
13113 }
13114
34dc0f95
TBA
13115 /* If missing DW_AT_abstract_origin, try the corresponding child
13116 of the origin. Clang emits such lexical scopes. */
13117 if (child_origin_die == child_die
13118 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
13119 && are_isomorphic
13120 && child_die->tag == DW_TAG_lexical_block)
13121 child_origin_die = corresponding_abstract_child;
13122
d389af10
JK
13123 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13124 counterpart may exist. */
c38f313d 13125 if (child_origin_die != child_die)
d389af10 13126 {
edb3359d
DJ
13127 if (child_die->tag != child_origin_die->tag
13128 && !(child_die->tag == DW_TAG_inlined_subroutine
13129 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13130 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13131 "different tags"),
9d8780f0
SM
13132 sect_offset_str (child_die->sect_off),
13133 sect_offset_str (child_origin_die->sect_off));
c38f313d 13134 if (child_origin_die->parent != origin_die)
b98664d3 13135 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13136 "different parents"),
9d8780f0
SM
13137 sect_offset_str (child_die->sect_off),
13138 sect_offset_str (child_origin_die->sect_off));
c38f313d 13139 else
791afaa2 13140 offsets.push_back (child_origin_die->sect_off);
d389af10 13141 }
34dc0f95
TBA
13142
13143 if (are_isomorphic)
13144 corresponding_abstract_child = corresponding_abstract_child->sibling;
d389af10 13145 }
791afaa2
TT
13146 std::sort (offsets.begin (), offsets.end ());
13147 sect_offset *offsets_end = offsets.data () + offsets.size ();
13148 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13149 if (offsetp[-1] == *offsetp)
b98664d3 13150 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13151 "to DIE %s as their abstract origin"),
13152 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13153
791afaa2 13154 offsetp = offsets.data ();
d389af10
JK
13155 origin_child_die = origin_die->child;
13156 while (origin_child_die && origin_child_die->tag)
13157 {
13158 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13159 while (offsetp < offsets_end
9c541725 13160 && *offsetp < origin_child_die->sect_off)
d389af10 13161 offsetp++;
b64f50a1 13162 if (offsetp >= offsets_end
9c541725 13163 || *offsetp > origin_child_die->sect_off)
d389af10 13164 {
adde2bff
DE
13165 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13166 Check whether we're already processing ORIGIN_CHILD_DIE.
13167 This can happen with mutually referenced abstract_origins.
13168 PR 16581. */
13169 if (!origin_child_die->in_process)
13170 process_die (origin_child_die, origin_cu);
d389af10 13171 }
436c571c 13172 origin_child_die = origin_child_die->sibling;
d389af10 13173 }
cd02d79d 13174 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13175
13176 if (cu != origin_cu)
13177 compute_delayed_physnames (origin_cu);
d389af10
JK
13178}
13179
c906108c 13180static void
e7c27a73 13181read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13182{
5e22e966 13183 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13184 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13185 struct context_stack *newobj;
c906108c
SS
13186 CORE_ADDR lowpc;
13187 CORE_ADDR highpc;
13188 struct die_info *child_die;
edb3359d 13189 struct attribute *attr, *call_line, *call_file;
15d034d0 13190 const char *name;
e142c38c 13191 CORE_ADDR baseaddr;
801e3a5b 13192 struct block *block;
edb3359d 13193 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13194 std::vector<struct symbol *> template_args;
34eaf542 13195 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13196
13197 if (inlined_func)
13198 {
13199 /* If we do not have call site information, we can't show the
13200 caller of this inlined function. That's too confusing, so
13201 only use the scope for local variables. */
13202 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13203 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13204 if (call_line == NULL || call_file == NULL)
13205 {
13206 read_lexical_block_scope (die, cu);
13207 return;
13208 }
13209 }
c906108c 13210
b3b3bada 13211 baseaddr = objfile->text_section_offset ();
e142c38c 13212
94af9270 13213 name = dwarf2_name (die, cu);
c906108c 13214
e8d05480
JB
13215 /* Ignore functions with missing or empty names. These are actually
13216 illegal according to the DWARF standard. */
13217 if (name == NULL)
13218 {
b98664d3 13219 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13220 sect_offset_str (die->sect_off));
e8d05480
JB
13221 return;
13222 }
13223
13224 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13225 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13226 <= PC_BOUNDS_INVALID)
e8d05480 13227 {
ae4d0c03 13228 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13229 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13230 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13231 "for subprogram DIE at %s"),
13232 sect_offset_str (die->sect_off));
e8d05480
JB
13233 return;
13234 }
c906108c 13235
3e29f34a
MR
13236 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13237 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13238
34eaf542
TT
13239 /* If we have any template arguments, then we must allocate a
13240 different sort of symbol. */
436c571c 13241 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13242 {
13243 if (child_die->tag == DW_TAG_template_type_param
13244 || child_die->tag == DW_TAG_template_value_param)
13245 {
8c14c3a3 13246 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13247 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13248 break;
13249 }
13250 }
13251
702cf3f5 13252 gdb_assert (cu->get_builder () != nullptr);
c24bdb02 13253 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13254 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13255 (struct symbol *) templ_func);
4c2df51b 13256
81873cc8 13257 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13258 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13259 cu->language);
13260
4cecd739
DJ
13261 /* If there is a location expression for DW_AT_frame_base, record
13262 it. */
e142c38c 13263 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13264 if (attr != nullptr)
fe978cb0 13265 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13266
63e43d3a
PMR
13267 /* If there is a location for the static link, record it. */
13268 newobj->static_link = NULL;
13269 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13270 if (attr != nullptr)
63e43d3a 13271 {
224c3ddb
SM
13272 newobj->static_link
13273 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13274 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13275 cu->addr_type ());
63e43d3a
PMR
13276 }
13277
c24bdb02 13278 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13279
639d11d3 13280 if (die->child != NULL)
c906108c 13281 {
639d11d3 13282 child_die = die->child;
c906108c
SS
13283 while (child_die && child_die->tag)
13284 {
34eaf542
TT
13285 if (child_die->tag == DW_TAG_template_type_param
13286 || child_die->tag == DW_TAG_template_value_param)
13287 {
13288 struct symbol *arg = new_symbol (child_die, NULL, cu);
13289
f1078f66 13290 if (arg != NULL)
2f4732b0 13291 template_args.push_back (arg);
34eaf542
TT
13292 }
13293 else
13294 process_die (child_die, cu);
436c571c 13295 child_die = child_die->sibling;
c906108c
SS
13296 }
13297 }
13298
d389af10
JK
13299 inherit_abstract_dies (die, cu);
13300
4a811a97
UW
13301 /* If we have a DW_AT_specification, we might need to import using
13302 directives from the context of the specification DIE. See the
13303 comment in determine_prefix. */
13304 if (cu->language == language_cplus
13305 && dwarf2_attr (die, DW_AT_specification, cu))
13306 {
13307 struct dwarf2_cu *spec_cu = cu;
13308 struct die_info *spec_die = die_specification (die, &spec_cu);
13309
13310 while (spec_die)
13311 {
13312 child_die = spec_die->child;
13313 while (child_die && child_die->tag)
13314 {
13315 if (child_die->tag == DW_TAG_imported_module)
13316 process_die (child_die, spec_cu);
436c571c 13317 child_die = child_die->sibling;
4a811a97
UW
13318 }
13319
13320 /* In some cases, GCC generates specification DIEs that
13321 themselves contain DW_AT_specification attributes. */
13322 spec_die = die_specification (spec_die, &spec_cu);
13323 }
13324 }
13325
c24bdb02 13326 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13327 /* Make a block for the local symbols within. */
c24bdb02 13328 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13329 cstk.static_link, lowpc, highpc);
801e3a5b 13330
df8a16a1 13331 /* For C++, set the block's scope. */
45280282
IB
13332 if ((cu->language == language_cplus
13333 || cu->language == language_fortran
c44af4eb
TT
13334 || cu->language == language_d
13335 || cu->language == language_rust)
4d4ec4e5 13336 && cu->processing_has_namespace_info)
195a3f6c
TT
13337 block_set_scope (block, determine_prefix (die, cu),
13338 &objfile->objfile_obstack);
df8a16a1 13339
801e3a5b
JB
13340 /* If we have address ranges, record them. */
13341 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13342
a60f3166 13343 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13344
34eaf542 13345 /* Attach template arguments to function. */
2f4732b0 13346 if (!template_args.empty ())
34eaf542
TT
13347 {
13348 gdb_assert (templ_func != NULL);
13349
2f4732b0 13350 templ_func->n_template_arguments = template_args.size ();
34eaf542 13351 templ_func->template_arguments
dda83cd7 13352 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13353 templ_func->n_template_arguments);
34eaf542 13354 memcpy (templ_func->template_arguments,
2f4732b0 13355 template_args.data (),
34eaf542 13356 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13357
13358 /* Make sure that the symtab is set on the new symbols. Even
13359 though they don't appear in this symtab directly, other parts
13360 of gdb assume that symbols do, and this is reasonably
13361 true. */
8634679f 13362 for (symbol *sym : template_args)
3e1d3d8c 13363 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13364 }
13365
208d8187
JB
13366 /* In C++, we can have functions nested inside functions (e.g., when
13367 a function declares a class that has methods). This means that
13368 when we finish processing a function scope, we may need to go
13369 back to building a containing block's symbol lists. */
c24bdb02
KS
13370 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13371 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13372
921e78cf
JB
13373 /* If we've finished processing a top-level function, subsequent
13374 symbols go in the file symbol list. */
c24bdb02
KS
13375 if (cu->get_builder ()->outermost_context_p ())
13376 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13377}
13378
13379/* Process all the DIES contained within a lexical block scope. Start
13380 a new scope, process the dies, and then close the scope. */
13381
13382static void
e7c27a73 13383read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13384{
5e22e966 13385 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13386 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13387 CORE_ADDR lowpc, highpc;
13388 struct die_info *child_die;
e142c38c
DJ
13389 CORE_ADDR baseaddr;
13390
b3b3bada 13391 baseaddr = objfile->text_section_offset ();
c906108c
SS
13392
13393 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13394 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13395 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13396 be nasty. Might be easier to properly extend generic blocks to
af34e669 13397 describe ranges. */
e385593e
JK
13398 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13399 {
13400 case PC_BOUNDS_NOT_PRESENT:
13401 /* DW_TAG_lexical_block has no attributes, process its children as if
13402 there was no wrapping by that DW_TAG_lexical_block.
13403 GCC does no longer produces such DWARF since GCC r224161. */
13404 for (child_die = die->child;
13405 child_die != NULL && child_die->tag;
436c571c 13406 child_die = child_die->sibling)
4f7bc5ed
TT
13407 {
13408 /* We might already be processing this DIE. This can happen
13409 in an unusual circumstance -- where a subroutine A
13410 appears lexically in another subroutine B, but A actually
13411 inlines B. The recursion is broken here, rather than in
13412 inherit_abstract_dies, because it seems better to simply
13413 drop concrete children here. */
13414 if (!child_die->in_process)
13415 process_die (child_die, cu);
13416 }
e385593e
JK
13417 return;
13418 case PC_BOUNDS_INVALID:
13419 return;
13420 }
3e29f34a
MR
13421 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13422 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13423
c24bdb02 13424 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13425 if (die->child != NULL)
c906108c 13426 {
639d11d3 13427 child_die = die->child;
c906108c
SS
13428 while (child_die && child_die->tag)
13429 {
e7c27a73 13430 process_die (child_die, cu);
436c571c 13431 child_die = child_die->sibling;
c906108c
SS
13432 }
13433 }
3ea89b92 13434 inherit_abstract_dies (die, cu);
c24bdb02 13435 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13436
c24bdb02
KS
13437 if (*cu->get_builder ()->get_local_symbols () != NULL
13438 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13439 {
801e3a5b 13440 struct block *block
dda83cd7 13441 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13442 cstk.start_addr, highpc);
801e3a5b
JB
13443
13444 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13445 do here, means that recording a parent's ranges entails
13446 walking across all its children's ranges as they appear in
13447 the address map, which is quadratic behavior.
13448
13449 It would be nicer to record the parent's ranges before
13450 traversing its children, simply overriding whatever you find
13451 there. But since we don't even decide whether to create a
13452 block until after we've traversed its children, that's hard
13453 to do. */
801e3a5b 13454 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13455 }
c24bdb02
KS
13456 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13457 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13458}
13459
216f72a1 13460/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13461
13462static void
13463read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13464{
5e22e966 13465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13466 struct objfile *objfile = per_objfile->objfile;
08feed99 13467 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13468 CORE_ADDR pc, baseaddr;
13469 struct attribute *attr;
13470 struct call_site *call_site, call_site_local;
13471 void **slot;
13472 int nparams;
13473 struct die_info *child_die;
13474
b3b3bada 13475 baseaddr = objfile->text_section_offset ();
96408a79 13476
216f72a1
JK
13477 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13478 if (attr == NULL)
13479 {
13480 /* This was a pre-DWARF-5 GNU extension alias
13481 for DW_AT_call_return_pc. */
13482 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13483 }
96408a79
SA
13484 if (!attr)
13485 {
b98664d3 13486 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13487 "DIE %s [in module %s]"),
13488 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13489 return;
13490 }
95f982e5 13491 pc = attr->as_address () + baseaddr;
3e29f34a 13492 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13493
13494 if (cu->call_site_htab == NULL)
13495 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13496 NULL, &objfile->objfile_obstack,
13497 hashtab_obstack_allocate, NULL);
13498 call_site_local.pc = pc;
13499 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13500 if (*slot != NULL)
13501 {
b98664d3 13502 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13503 "DIE %s [in module %s]"),
13504 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13505 objfile_name (objfile));
96408a79
SA
13506 return;
13507 }
13508
13509 /* Count parameters at the caller. */
13510
13511 nparams = 0;
13512 for (child_die = die->child; child_die && child_die->tag;
436c571c 13513 child_die = child_die->sibling)
96408a79 13514 {
216f72a1 13515 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13516 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13517 {
b98664d3 13518 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13519 "DW_TAG_call_site child DIE %s [in module %s]"),
13520 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13521 objfile_name (objfile));
96408a79
SA
13522 continue;
13523 }
13524
13525 nparams++;
13526 }
13527
224c3ddb
SM
13528 call_site
13529 = ((struct call_site *)
13530 obstack_alloc (&objfile->objfile_obstack,
13531 sizeof (*call_site)
13532 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13533 *slot = call_site;
13534 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13535 call_site->pc = pc;
13536
216f72a1
JK
13537 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13538 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13539 {
13540 struct die_info *func_die;
13541
13542 /* Skip also over DW_TAG_inlined_subroutine. */
13543 for (func_die = die->parent;
13544 func_die && func_die->tag != DW_TAG_subprogram
13545 && func_die->tag != DW_TAG_subroutine_type;
13546 func_die = func_die->parent);
13547
216f72a1
JK
13548 /* DW_AT_call_all_calls is a superset
13549 of DW_AT_call_all_tail_calls. */
96408a79 13550 if (func_die
dda83cd7
SM
13551 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13552 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13553 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13554 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13555 {
13556 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13557 not complete. But keep CALL_SITE for look ups via call_site_htab,
13558 both the initial caller containing the real return address PC and
13559 the final callee containing the current PC of a chain of tail
13560 calls do not need to have the tail call list complete. But any
13561 function candidate for a virtual tail call frame searched via
13562 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13563 determined unambiguously. */
13564 }
13565 else
13566 {
13567 struct type *func_type = NULL;
13568
13569 if (func_die)
13570 func_type = get_die_type (func_die, cu);
13571 if (func_type != NULL)
13572 {
78134374 13573 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13574
13575 /* Enlist this call site to the function. */
13576 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13577 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13578 }
13579 else
b98664d3 13580 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13581 "DIE %s [in module %s]"),
13582 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13583 }
13584 }
13585
216f72a1
JK
13586 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13587 if (attr == NULL)
13588 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13589 if (attr == NULL)
13590 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13591 if (attr == NULL)
216f72a1
JK
13592 {
13593 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13594 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13595 }
96408a79 13596 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13597 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13598 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13599 else if (attr->form_is_block ())
96408a79
SA
13600 {
13601 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13602 struct dwarf_block *block = attr->as_block ();
96408a79 13603
8d749320 13604 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13605 dlbaton->data = block->data;
13606 dlbaton->size = block->size;
a50264ba 13607 dlbaton->per_objfile = per_objfile;
96408a79
SA
13608 dlbaton->per_cu = cu->per_cu;
13609
13610 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13611 }
cd6c91b4 13612 else if (attr->form_is_ref ())
96408a79 13613 {
96408a79
SA
13614 struct dwarf2_cu *target_cu = cu;
13615 struct die_info *target_die;
13616
ac9ec31b 13617 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13618 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13619 if (die_is_declaration (target_die, target_cu))
13620 {
7d45c7c3 13621 const char *target_physname;
9112db09
JK
13622
13623 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13624 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13625 if (target_physname == NULL)
9112db09 13626 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13627 if (target_physname == NULL)
b98664d3 13628 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13629 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 13630 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13631 else
7d455152 13632 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13633 }
13634 else
13635 {
13636 CORE_ADDR lowpc;
13637
13638 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13639 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13640 <= PC_BOUNDS_INVALID)
b98664d3 13641 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13642 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 13643 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13644 else
3e29f34a
MR
13645 {
13646 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13647 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13648 }
96408a79
SA
13649 }
13650 }
13651 else
b98664d3 13652 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13653 "block nor reference, for DIE %s [in module %s]"),
13654 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13655
13656 call_site->per_cu = cu->per_cu;
9f47c707 13657 call_site->per_objfile = per_objfile;
96408a79
SA
13658
13659 for (child_die = die->child;
13660 child_die && child_die->tag;
436c571c 13661 child_die = child_die->sibling)
96408a79 13662 {
96408a79 13663 struct call_site_parameter *parameter;
1788b2d3 13664 struct attribute *loc, *origin;
96408a79 13665
216f72a1 13666 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13667 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13668 {
13669 /* Already printed the complaint above. */
13670 continue;
13671 }
13672
13673 gdb_assert (call_site->parameter_count < nparams);
13674 parameter = &call_site->parameter[call_site->parameter_count];
13675
1788b2d3
JK
13676 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13677 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13678 register is contained in DW_AT_call_value. */
96408a79 13679
24c5c679 13680 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13681 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13682 if (origin == NULL)
13683 {
13684 /* This was a pre-DWARF-5 GNU extension alias
13685 for DW_AT_call_parameter. */
13686 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13687 }
cd6c91b4 13688 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13689 {
1788b2d3 13690 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13691
0826b30a 13692 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13693 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13694 {
13695 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13696 binding can be done only inside one CU. Such referenced DIE
13697 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13698 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13699 "DW_TAG_call_site child DIE %s [in module %s]"),
13700 sect_offset_str (child_die->sect_off),
9c541725 13701 objfile_name (objfile));
d76b7dbc
JK
13702 continue;
13703 }
9c541725
PA
13704 parameter->u.param_cu_off
13705 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13706 }
4fc6c0d5 13707 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13708 {
b98664d3 13709 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13710 "DW_TAG_call_site child DIE %s [in module %s]"),
13711 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13712 continue;
13713 }
24c5c679 13714 else
96408a79 13715 {
9d2246fc
TT
13716 struct dwarf_block *block = loc->as_block ();
13717
24c5c679 13718 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 13719 (block->data, &block->data[block->size]);
24c5c679
JK
13720 if (parameter->u.dwarf_reg != -1)
13721 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
13722 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13723 &block->data[block->size],
24c5c679
JK
13724 &parameter->u.fb_offset))
13725 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13726 else
13727 {
b98664d3 13728 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13729 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13730 "DW_TAG_call_site child DIE %s "
24c5c679 13731 "[in module %s]"),
9d8780f0 13732 sect_offset_str (child_die->sect_off),
9c541725 13733 objfile_name (objfile));
24c5c679
JK
13734 continue;
13735 }
96408a79
SA
13736 }
13737
216f72a1
JK
13738 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13739 if (attr == NULL)
13740 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13741 if (attr == NULL || !attr->form_is_block ())
96408a79 13742 {
b98664d3 13743 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13744 "DW_TAG_call_site child DIE %s [in module %s]"),
13745 sect_offset_str (child_die->sect_off),
9c541725 13746 objfile_name (objfile));
96408a79
SA
13747 continue;
13748 }
9d2246fc
TT
13749
13750 struct dwarf_block *block = attr->as_block ();
13751 parameter->value = block->data;
13752 parameter->value_size = block->size;
96408a79
SA
13753
13754 /* Parameters are not pre-cleared by memset above. */
13755 parameter->data_value = NULL;
13756 parameter->data_value_size = 0;
13757 call_site->parameter_count++;
13758
216f72a1
JK
13759 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13760 if (attr == NULL)
13761 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13762 if (attr != nullptr)
96408a79 13763 {
4fc6c0d5 13764 if (!attr->form_is_block ())
b98664d3 13765 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13766 "DW_TAG_call_site child DIE %s [in module %s]"),
13767 sect_offset_str (child_die->sect_off),
9c541725 13768 objfile_name (objfile));
96408a79
SA
13769 else
13770 {
9d2246fc
TT
13771 block = attr->as_block ();
13772 parameter->data_value = block->data;
13773 parameter->data_value_size = block->size;
96408a79
SA
13774 }
13775 }
13776 }
13777}
13778
71a3c369
TT
13779/* Helper function for read_variable. If DIE represents a virtual
13780 table, then return the type of the concrete object that is
13781 associated with the virtual table. Otherwise, return NULL. */
13782
13783static struct type *
13784rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13785{
13786 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13787 if (attr == NULL)
13788 return NULL;
13789
13790 /* Find the type DIE. */
13791 struct die_info *type_die = NULL;
13792 struct dwarf2_cu *type_cu = cu;
13793
cd6c91b4 13794 if (attr->form_is_ref ())
71a3c369
TT
13795 type_die = follow_die_ref (die, attr, &type_cu);
13796 if (type_die == NULL)
13797 return NULL;
13798
13799 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13800 return NULL;
13801 return die_containing_type (type_die, type_cu);
13802}
13803
13804/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13805
13806static void
13807read_variable (struct die_info *die, struct dwarf2_cu *cu)
13808{
13809 struct rust_vtable_symbol *storage = NULL;
13810
13811 if (cu->language == language_rust)
13812 {
13813 struct type *containing_type = rust_containing_type (die, cu);
13814
13815 if (containing_type != NULL)
13816 {
5e22e966 13817 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13818
8c14c3a3 13819 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13820 storage->concrete_type = containing_type;
cf724bc9 13821 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13822 }
13823 }
13824
e4a62c65
TV
13825 struct symbol *res = new_symbol (die, NULL, cu, storage);
13826 struct attribute *abstract_origin
13827 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13828 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13829 if (res == NULL && loc && abstract_origin)
13830 {
13831 /* We have a variable without a name, but with a location and an abstract
13832 origin. This may be a concrete instance of an abstract variable
13833 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13834 later. */
13835 struct dwarf2_cu *origin_cu = cu;
13836 struct die_info *origin_die
13837 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13838 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13839 per_objfile->per_bfd->abstract_to_concrete
13840 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13841 }
71a3c369
TT
13842}
13843
43988095
JK
13844/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13845 reading .debug_rnglists.
13846 Callback's type should be:
13847 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13848 Return true if the attributes are present and valid, otherwise,
13849 return false. */
13850
13851template <typename Callback>
13852static bool
13853dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 13854 dwarf_tag tag, Callback &&callback)
43988095 13855{
976ca316
SM
13856 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13857 struct objfile *objfile = per_objfile->objfile;
43988095 13858 bfd *obfd = objfile->obfd;
43988095 13859 /* Base address selection entry. */
2b24b6e4 13860 gdb::optional<CORE_ADDR> base;
43988095 13861 const gdb_byte *buffer;
43988095
JK
13862 CORE_ADDR baseaddr;
13863 bool overflow = false;
d0ce17d8
CT
13864 ULONGEST addr_index;
13865 struct dwarf2_section_info *rnglists_section;
43988095 13866
43988095 13867 base = cu->base_address;
d0ce17d8
CT
13868 rnglists_section = cu_debug_rnglists_section (cu, tag);
13869 rnglists_section->read (objfile);
43988095 13870
d0ce17d8 13871 if (offset >= rnglists_section->size)
43988095 13872 {
b98664d3 13873 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13874 offset);
13875 return false;
13876 }
d0ce17d8 13877 buffer = rnglists_section->buffer + offset;
43988095 13878
b3b3bada 13879 baseaddr = objfile->text_section_offset ();
43988095
JK
13880
13881 while (1)
13882 {
7814882a
JK
13883 /* Initialize it due to a false compiler warning. */
13884 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
13885 const gdb_byte *buf_end = (rnglists_section->buffer
13886 + rnglists_section->size);
43988095
JK
13887 unsigned int bytes_read;
13888
13889 if (buffer == buf_end)
13890 {
13891 overflow = true;
13892 break;
13893 }
13894 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13895 switch (rlet)
13896 {
13897 case DW_RLE_end_of_list:
13898 break;
13899 case DW_RLE_base_address:
13900 if (buffer + cu->header.addr_size > buf_end)
13901 {
13902 overflow = true;
13903 break;
13904 }
c8a7a66f 13905 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13906 buffer += bytes_read;
13907 break;
dda83cd7
SM
13908 case DW_RLE_base_addressx:
13909 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13910 buffer += bytes_read;
13911 base = read_addr_index (cu, addr_index);
13912 break;
43988095
JK
13913 case DW_RLE_start_length:
13914 if (buffer + cu->header.addr_size > buf_end)
13915 {
13916 overflow = true;
13917 break;
13918 }
c8a7a66f
TT
13919 range_beginning = cu->header.read_address (obfd, buffer,
13920 &bytes_read);
43988095
JK
13921 buffer += bytes_read;
13922 range_end = (range_beginning
13923 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13924 buffer += bytes_read;
13925 if (buffer > buf_end)
13926 {
13927 overflow = true;
13928 break;
13929 }
13930 break;
d0ce17d8 13931 case DW_RLE_startx_length:
dda83cd7
SM
13932 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13933 buffer += bytes_read;
13934 range_beginning = read_addr_index (cu, addr_index);
13935 if (buffer > buf_end)
13936 {
13937 overflow = true;
13938 break;
13939 }
13940 range_end = (range_beginning
13941 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13942 buffer += bytes_read;
13943 break;
43988095
JK
13944 case DW_RLE_offset_pair:
13945 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13946 buffer += bytes_read;
13947 if (buffer > buf_end)
13948 {
13949 overflow = true;
13950 break;
13951 }
13952 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13953 buffer += bytes_read;
13954 if (buffer > buf_end)
13955 {
13956 overflow = true;
13957 break;
13958 }
13959 break;
13960 case DW_RLE_start_end:
13961 if (buffer + 2 * cu->header.addr_size > buf_end)
13962 {
13963 overflow = true;
13964 break;
13965 }
c8a7a66f
TT
13966 range_beginning = cu->header.read_address (obfd, buffer,
13967 &bytes_read);
43988095 13968 buffer += bytes_read;
c8a7a66f 13969 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13970 buffer += bytes_read;
13971 break;
d0ce17d8 13972 case DW_RLE_startx_endx:
dda83cd7
SM
13973 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13974 buffer += bytes_read;
13975 range_beginning = read_addr_index (cu, addr_index);
13976 if (buffer > buf_end)
13977 {
13978 overflow = true;
13979 break;
13980 }
13981 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13982 buffer += bytes_read;
13983 range_end = read_addr_index (cu, addr_index);
13984 break;
43988095 13985 default:
b98664d3 13986 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13987 return false;
13988 }
13989 if (rlet == DW_RLE_end_of_list || overflow)
13990 break;
13991 if (rlet == DW_RLE_base_address)
13992 continue;
13993
43988095
JK
13994 if (range_beginning > range_end)
13995 {
13996 /* Inverted range entries are invalid. */
b98664d3 13997 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13998 return false;
13999 }
14000
14001 /* Empty range entries have no effect. */
14002 if (range_beginning == range_end)
14003 continue;
14004
d0ce17d8
CT
14005 /* Only DW_RLE_offset_pair needs the base address added. */
14006 if (rlet == DW_RLE_offset_pair)
14007 {
14008 if (!base.has_value ())
14009 {
14010 /* We have no valid base address for the DW_RLE_offset_pair. */
14011 complaint (_("Invalid .debug_rnglists data (no base address for "
14012 "DW_RLE_offset_pair)"));
14013 return false;
14014 }
14015
14016 range_beginning += *base;
14017 range_end += *base;
14018 }
43988095
JK
14019
14020 /* A not-uncommon case of bad debug info.
14021 Don't pollute the addrmap with bad data. */
14022 if (range_beginning + baseaddr == 0
976ca316 14023 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14024 {
b98664d3 14025 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14026 " [in module %s]"), objfile_name (objfile));
14027 continue;
14028 }
14029
14030 callback (range_beginning, range_end);
14031 }
14032
14033 if (overflow)
14034 {
b98664d3 14035 complaint (_("Offset %d is not terminated "
43988095
JK
14036 "for DW_AT_ranges attribute"),
14037 offset);
14038 return false;
14039 }
14040
14041 return true;
14042}
14043
14044/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14045 Callback's type should be:
14046 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14047 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14048
43988095 14049template <typename Callback>
43039443 14050static int
d0ce17d8 14051dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14052 Callback &&callback)
43039443 14053{
5e22e966
SM
14054 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14055 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14056 struct comp_unit_head *cu_header = &cu->header;
14057 bfd *obfd = objfile->obfd;
14058 unsigned int addr_size = cu_header->addr_size;
14059 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14060 /* Base address selection entry. */
2b24b6e4 14061 gdb::optional<CORE_ADDR> base;
43039443 14062 unsigned int dummy;
d521ce57 14063 const gdb_byte *buffer;
ff013f42 14064 CORE_ADDR baseaddr;
43039443 14065
43988095 14066 if (cu_header->version >= 5)
d0ce17d8 14067 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14068
d00adf39 14069 base = cu->base_address;
43039443 14070
5e22e966
SM
14071 per_objfile->per_bfd->ranges.read (objfile);
14072 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14073 {
b98664d3 14074 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14075 offset);
14076 return 0;
14077 }
5e22e966 14078 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14079
b3b3bada 14080 baseaddr = objfile->text_section_offset ();
ff013f42 14081
43039443
JK
14082 while (1)
14083 {
14084 CORE_ADDR range_beginning, range_end;
14085
c8a7a66f 14086 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14087 buffer += addr_size;
c8a7a66f 14088 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14089 buffer += addr_size;
14090 offset += 2 * addr_size;
14091
14092 /* An end of list marker is a pair of zero addresses. */
14093 if (range_beginning == 0 && range_end == 0)
14094 /* Found the end of list entry. */
14095 break;
14096
14097 /* Each base address selection entry is a pair of 2 values.
14098 The first is the largest possible address, the second is
14099 the base address. Check for a base address here. */
14100 if ((range_beginning & mask) == mask)
14101 {
28d2bfb9
AB
14102 /* If we found the largest possible address, then we already
14103 have the base address in range_end. */
14104 base = range_end;
43039443
JK
14105 continue;
14106 }
14107
2b24b6e4 14108 if (!base.has_value ())
43039443
JK
14109 {
14110 /* We have no valid base address for the ranges
14111 data. */
b98664d3 14112 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14113 return 0;
14114 }
14115
9277c30c
UW
14116 if (range_beginning > range_end)
14117 {
14118 /* Inverted range entries are invalid. */
b98664d3 14119 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14120 return 0;
14121 }
14122
14123 /* Empty range entries have no effect. */
14124 if (range_beginning == range_end)
14125 continue;
14126
2b24b6e4
TT
14127 range_beginning += *base;
14128 range_end += *base;
43039443 14129
01093045
DE
14130 /* A not-uncommon case of bad debug info.
14131 Don't pollute the addrmap with bad data. */
14132 if (range_beginning + baseaddr == 0
5e22e966 14133 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14134 {
b98664d3 14135 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14136 " [in module %s]"), objfile_name (objfile));
01093045
DE
14137 continue;
14138 }
14139
5f46c5a5
JK
14140 callback (range_beginning, range_end);
14141 }
14142
14143 return 1;
14144}
14145
14146/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14147 Return 1 if the attributes are present and valid, otherwise, return 0.
efd7398e 14148 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
5f46c5a5
JK
14149
14150static int
14151dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14152 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14153 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14154{
5e22e966 14155 struct objfile *objfile = cu->per_objfile->objfile;
84685904 14156 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 14157 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14158 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14159 int low_set = 0;
14160 CORE_ADDR low = 0;
14161 CORE_ADDR high = 0;
14162 int retval;
14163
d0ce17d8 14164 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14165 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14166 {
9277c30c 14167 if (ranges_pst != NULL)
3e29f34a
MR
14168 {
14169 CORE_ADDR lowpc;
14170 CORE_ADDR highpc;
14171
79748972
TT
14172 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14173 range_beginning + baseaddr)
14174 - baseaddr);
14175 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14176 range_end + baseaddr)
14177 - baseaddr);
84685904 14178 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 14179 lowpc, highpc - 1, ranges_pst);
3e29f34a 14180 }
ff013f42 14181
43039443
JK
14182 /* FIXME: This is recording everything as a low-high
14183 segment of consecutive addresses. We should have a
14184 data structure for discontiguous block ranges
14185 instead. */
14186 if (! low_set)
14187 {
14188 low = range_beginning;
14189 high = range_end;
14190 low_set = 1;
14191 }
14192 else
14193 {
14194 if (range_beginning < low)
14195 low = range_beginning;
14196 if (range_end > high)
14197 high = range_end;
14198 }
5f46c5a5
JK
14199 });
14200 if (!retval)
14201 return 0;
43039443
JK
14202
14203 if (! low_set)
14204 /* If the first entry is an end-of-list marker, the range
14205 describes an empty scope, i.e. no instructions. */
14206 return 0;
14207
14208 if (low_return)
14209 *low_return = low;
14210 if (high_return)
14211 *high_return = high;
14212 return 1;
14213}
14214
3a2b436a
JK
14215/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14216 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14217 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14218
3a2b436a 14219static enum pc_bounds_kind
af34e669 14220dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14221 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14222 dwarf2_psymtab *pst)
c906108c 14223{
976ca316 14224 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14225 struct attribute *attr;
91da1414 14226 struct attribute *attr_high;
af34e669
DJ
14227 CORE_ADDR low = 0;
14228 CORE_ADDR high = 0;
e385593e 14229 enum pc_bounds_kind ret;
c906108c 14230
91da1414
MW
14231 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14232 if (attr_high)
af34e669 14233 {
e142c38c 14234 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14235 if (attr != nullptr)
dda83cd7 14236 {
95f982e5
TT
14237 low = attr->as_address ();
14238 high = attr_high->as_address ();
cd6c91b4 14239 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14240 high += low;
91da1414 14241 }
af34e669
DJ
14242 else
14243 /* Found high w/o low attribute. */
e385593e 14244 return PC_BOUNDS_INVALID;
af34e669
DJ
14245
14246 /* Found consecutive range of addresses. */
3a2b436a 14247 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14248 }
c906108c 14249 else
af34e669 14250 {
e142c38c 14251 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14252 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14253 {
2b0c7f41
SM
14254 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14255 on DWARF version). */
14256 ULONGEST ranges_offset = attr->as_unsigned ();
14257
14258 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14259 this value. */
14260 if (die->tag != DW_TAG_compile_unit)
14261 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14262
af34e669 14263 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14264 .debug_ranges section. */
d0ce17d8
CT
14265 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14266 die->tag))
e385593e 14267 return PC_BOUNDS_INVALID;
43039443 14268 /* Found discontinuous range of addresses. */
3a2b436a 14269 ret = PC_BOUNDS_RANGES;
af34e669 14270 }
e385593e
JK
14271 else
14272 return PC_BOUNDS_NOT_PRESENT;
af34e669 14273 }
c906108c 14274
48fbe735 14275 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14276 if (high <= low)
e385593e 14277 return PC_BOUNDS_INVALID;
c906108c
SS
14278
14279 /* When using the GNU linker, .gnu.linkonce. sections are used to
14280 eliminate duplicate copies of functions and vtables and such.
14281 The linker will arbitrarily choose one and discard the others.
14282 The AT_*_pc values for such functions refer to local labels in
14283 these sections. If the section from that file was discarded, the
14284 labels are not in the output, so the relocs get a value of 0.
14285 If this is a discarded function, mark the pc bounds as invalid,
14286 so that GDB will ignore it. */
976ca316 14287 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14288 return PC_BOUNDS_INVALID;
c906108c
SS
14289
14290 *lowpc = low;
96408a79
SA
14291 if (highpc)
14292 *highpc = high;
af34e669 14293 return ret;
c906108c
SS
14294}
14295
b084d499
JB
14296/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14297 its low and high PC addresses. Do nothing if these addresses could not
14298 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14299 and HIGHPC to the high address if greater than HIGHPC. */
14300
14301static void
14302dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14303 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14304 struct dwarf2_cu *cu)
b084d499
JB
14305{
14306 CORE_ADDR low, high;
14307 struct die_info *child = die->child;
14308
e385593e 14309 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14310 {
325fac50
PA
14311 *lowpc = std::min (*lowpc, low);
14312 *highpc = std::max (*highpc, high);
b084d499
JB
14313 }
14314
14315 /* If the language does not allow nested subprograms (either inside
14316 subprograms or lexical blocks), we're done. */
14317 if (cu->language != language_ada)
14318 return;
6e70227d 14319
b084d499
JB
14320 /* Check all the children of the given DIE. If it contains nested
14321 subprograms, then check their pc bounds. Likewise, we need to
14322 check lexical blocks as well, as they may also contain subprogram
14323 definitions. */
14324 while (child && child->tag)
14325 {
14326 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14327 || child->tag == DW_TAG_lexical_block)
14328 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14329 child = child->sibling;
b084d499
JB
14330 }
14331}
14332
fae299cd
DC
14333/* Get the low and high pc's represented by the scope DIE, and store
14334 them in *LOWPC and *HIGHPC. If the correct values can't be
14335 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14336
14337static void
14338get_scope_pc_bounds (struct die_info *die,
14339 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14340 struct dwarf2_cu *cu)
14341{
14342 CORE_ADDR best_low = (CORE_ADDR) -1;
14343 CORE_ADDR best_high = (CORE_ADDR) 0;
14344 CORE_ADDR current_low, current_high;
14345
3a2b436a 14346 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14347 >= PC_BOUNDS_RANGES)
fae299cd
DC
14348 {
14349 best_low = current_low;
14350 best_high = current_high;
14351 }
14352 else
14353 {
14354 struct die_info *child = die->child;
14355
14356 while (child && child->tag)
14357 {
14358 switch (child->tag) {
14359 case DW_TAG_subprogram:
dda83cd7 14360 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14361 break;
14362 case DW_TAG_namespace:
f55ee35c 14363 case DW_TAG_module:
fae299cd
DC
14364 /* FIXME: carlton/2004-01-16: Should we do this for
14365 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14366 that current GCC's always emit the DIEs corresponding
14367 to definitions of methods of classes as children of a
14368 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14369 the DIEs giving the declarations, which could be
14370 anywhere). But I don't see any reason why the
14371 standards says that they have to be there. */
14372 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14373
14374 if (current_low != ((CORE_ADDR) -1))
14375 {
325fac50
PA
14376 best_low = std::min (best_low, current_low);
14377 best_high = std::max (best_high, current_high);
fae299cd
DC
14378 }
14379 break;
14380 default:
0963b4bd 14381 /* Ignore. */
fae299cd
DC
14382 break;
14383 }
14384
436c571c 14385 child = child->sibling;
fae299cd
DC
14386 }
14387 }
14388
14389 *lowpc = best_low;
14390 *highpc = best_high;
14391}
14392
801e3a5b
JB
14393/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14394 in DIE. */
380bca97 14395
801e3a5b
JB
14396static void
14397dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14398 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14399{
5e22e966 14400 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14401 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14402 struct attribute *attr;
91da1414 14403 struct attribute *attr_high;
801e3a5b 14404
91da1414
MW
14405 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14406 if (attr_high)
801e3a5b 14407 {
801e3a5b 14408 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14409 if (attr != nullptr)
dda83cd7 14410 {
95f982e5
TT
14411 CORE_ADDR low = attr->as_address ();
14412 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14413
cd6c91b4 14414 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14415 high += low;
9a619af0 14416
3e29f34a
MR
14417 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14418 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14419 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14420 }
801e3a5b
JB
14421 }
14422
14423 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14424 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14425 {
2b0c7f41
SM
14426 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14427 on DWARF version). */
14428 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14429
2b0c7f41
SM
14430 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14431 this value. */
14432 if (die->tag != DW_TAG_compile_unit)
14433 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14434
2d5f09ec 14435 std::vector<blockrange> blockvec;
2b0c7f41 14436 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14437 [&] (CORE_ADDR start, CORE_ADDR end)
14438 {
58fdfd2c
JK
14439 start += baseaddr;
14440 end += baseaddr;
5f46c5a5
JK
14441 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14442 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14443 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14444 blockvec.emplace_back (start, end);
5f46c5a5 14445 });
2d5f09ec
KB
14446
14447 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14448 }
14449}
14450
685b1105
JK
14451/* Check whether the producer field indicates either of GCC < 4.6, or the
14452 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14453
685b1105
JK
14454static void
14455check_producer (struct dwarf2_cu *cu)
60d5a603 14456{
38360086 14457 int major, minor;
60d5a603
JK
14458
14459 if (cu->producer == NULL)
14460 {
14461 /* For unknown compilers expect their behavior is DWARF version
14462 compliant.
14463
14464 GCC started to support .debug_types sections by -gdwarf-4 since
14465 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14466 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14467 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14468 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14469 }
b1ffba5a 14470 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14471 {
38360086
MW
14472 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14473 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14474 }
5230b05a 14475 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14476 {
14477 cu->producer_is_icc = true;
14478 cu->producer_is_icc_lt_14 = major < 14;
14479 }
c258c396
JD
14480 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14481 cu->producer_is_codewarrior = true;
685b1105
JK
14482 else
14483 {
14484 /* For other non-GCC compilers, expect their behavior is DWARF version
14485 compliant. */
60d5a603
JK
14486 }
14487
9068261f 14488 cu->checked_producer = true;
685b1105 14489}
ba919b58 14490
685b1105
JK
14491/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14492 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14493 during 4.6.0 experimental. */
14494
9068261f 14495static bool
685b1105
JK
14496producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14497{
14498 if (!cu->checked_producer)
14499 check_producer (cu);
14500
14501 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14502}
14503
c258c396
JD
14504
14505/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14506 with incorrect is_stmt attributes. */
14507
14508static bool
14509producer_is_codewarrior (struct dwarf2_cu *cu)
14510{
14511 if (!cu->checked_producer)
14512 check_producer (cu);
14513
14514 return cu->producer_is_codewarrior;
14515}
14516
bf23a268
TT
14517/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14518 If that attribute is not available, return the appropriate
14519 default. */
60d5a603
JK
14520
14521static enum dwarf_access_attribute
bf23a268 14522dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14523{
bf23a268
TT
14524 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14525 if (attr != nullptr)
14526 {
14527 LONGEST value = attr->constant_value (-1);
14528 if (value == DW_ACCESS_public
14529 || value == DW_ACCESS_protected
14530 || value == DW_ACCESS_private)
14531 return (dwarf_access_attribute) value;
14532 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14533 plongest (value));
14534 }
14535
60d5a603
JK
14536 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14537 {
14538 /* The default DWARF 2 accessibility for members is public, the default
14539 accessibility for inheritance is private. */
14540
14541 if (die->tag != DW_TAG_inheritance)
14542 return DW_ACCESS_public;
14543 else
14544 return DW_ACCESS_private;
14545 }
14546 else
14547 {
14548 /* DWARF 3+ defines the default accessibility a different way. The same
14549 rules apply now for DW_TAG_inheritance as for the members and it only
14550 depends on the container kind. */
14551
14552 if (die->parent->tag == DW_TAG_class_type)
14553 return DW_ACCESS_private;
14554 else
14555 return DW_ACCESS_public;
14556 }
14557}
14558
74ac6d43
TT
14559/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14560 offset. If the attribute was not found return 0, otherwise return
14561 1. If it was found but could not properly be handled, set *OFFSET
14562 to 0. */
14563
14564static int
14565handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14566 LONGEST *offset)
14567{
14568 struct attribute *attr;
14569
14570 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14571 if (attr != NULL)
14572 {
14573 *offset = 0;
14574
14575 /* Note that we do not check for a section offset first here.
14576 This is because DW_AT_data_member_location is new in DWARF 4,
14577 so if we see it, we can assume that a constant form is really
14578 a constant and not a section offset. */
cd6c91b4 14579 if (attr->form_is_constant ())
0826b30a 14580 *offset = attr->constant_value (0);
cd6c91b4 14581 else if (attr->form_is_section_offset ())
74ac6d43 14582 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14583 else if (attr->form_is_block ())
9d2246fc 14584 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14585 else
14586 dwarf2_complex_location_expr_complaint ();
14587
14588 return 1;
14589 }
14590
14591 return 0;
14592}
14593
7d79de9a
TT
14594/* Look for DW_AT_data_member_location and store the results in FIELD. */
14595
14596static void
14597handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14598 struct field *field)
14599{
14600 struct attribute *attr;
14601
14602 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14603 if (attr != NULL)
14604 {
14605 if (attr->form_is_constant ())
14606 {
14607 LONGEST offset = attr->constant_value (0);
14608 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14609 }
14610 else if (attr->form_is_section_offset ())
14611 dwarf2_complex_location_expr_complaint ();
14612 else if (attr->form_is_block ())
14613 {
14614 bool handled;
9d2246fc 14615 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14616 if (handled)
14617 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14618 else
14619 {
5e22e966
SM
14620 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14621 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14622 struct dwarf2_locexpr_baton *dlbaton
14623 = XOBNEW (&objfile->objfile_obstack,
14624 struct dwarf2_locexpr_baton);
9d2246fc
TT
14625 dlbaton->data = attr->as_block ()->data;
14626 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14627 /* When using this baton, we want to compute the address
14628 of the field, not the value. This is why
14629 is_reference is set to false here. */
14630 dlbaton->is_reference = false;
5e22e966 14631 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14632 dlbaton->per_cu = cu->per_cu;
14633
14634 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14635 }
14636 }
14637 else
14638 dwarf2_complex_location_expr_complaint ();
14639 }
14640}
14641
c906108c
SS
14642/* Add an aggregate field to the field list. */
14643
14644static void
107d2387 14645dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14646 struct dwarf2_cu *cu)
6e70227d 14647{
5e22e966 14648 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14649 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14650 struct nextfield *new_field;
14651 struct attribute *attr;
14652 struct field *fp;
15d034d0 14653 const char *fieldname = "";
c906108c 14654
7d0ccb61
DJ
14655 if (die->tag == DW_TAG_inheritance)
14656 {
be2daae6
TT
14657 fip->baseclasses.emplace_back ();
14658 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14659 }
14660 else
14661 {
be2daae6
TT
14662 fip->fields.emplace_back ();
14663 new_field = &fip->fields.back ();
7d0ccb61 14664 }
be2daae6 14665
9c6a1327
TT
14666 new_field->offset = die->sect_off;
14667
bf23a268 14668 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 14669 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 14670 fip->non_public_fields = true;
60d5a603 14671
e142c38c 14672 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14673 if (attr != nullptr)
23dca5c3 14674 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
14675 else
14676 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14677
14678 fp = &new_field->field;
a9a9bd0f 14679
e142c38c 14680 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14681 {
a9a9bd0f 14682 /* Data member other than a C++ static data member. */
6e70227d 14683
c906108c 14684 /* Get type of field. */
5d14b6e5 14685 fp->set_type (die_type (die, cu));
c906108c 14686
d6a843b5 14687 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14688
c906108c 14689 /* Get bit size of field (zero if none). */
e142c38c 14690 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14691 if (attr != nullptr)
c906108c 14692 {
529908cb 14693 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
14694 }
14695 else
14696 {
14697 FIELD_BITSIZE (*fp) = 0;
14698 }
14699
14700 /* Get bit offset of field. */
7d79de9a 14701 handle_data_member_location (die, cu, fp);
e142c38c 14702 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 14703 if (attr != nullptr && attr->form_is_constant ())
c906108c 14704 {
d5a22e77 14705 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14706 {
14707 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
14708 additional bit offset from the MSB of the containing
14709 anonymous object to the MSB of the field. We don't
14710 have to do anything special since we don't need to
14711 know the size of the anonymous object. */
529908cb 14712 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 14713 + attr->constant_value (0)));
c906108c
SS
14714 }
14715 else
14716 {
14717 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
14718 MSB of the anonymous object, subtract off the number of
14719 bits from the MSB of the field to the MSB of the
14720 object, and then subtract off the number of bits of
14721 the field itself. The result is the bit offset of
14722 the LSB of the field. */
c906108c 14723 int anonymous_size;
7c184d33 14724 int bit_offset = attr->constant_value (0);
c906108c 14725
e142c38c 14726 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 14727 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
14728 {
14729 /* The size of the anonymous object containing
14730 the bit field is explicit, so use the
14731 indicated size (in bytes). */
7c184d33 14732 anonymous_size = attr->constant_value (0);
c906108c
SS
14733 }
14734 else
14735 {
14736 /* The size of the anonymous object containing
14737 the bit field must be inferred from the type
14738 attribute of the data member containing the
14739 bit field. */
5d14b6e5 14740 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14741 }
f41f5e61
PA
14742 SET_FIELD_BITPOS (*fp,
14743 (FIELD_BITPOS (*fp)
14744 + anonymous_size * bits_per_byte
14745 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14746 }
14747 }
da5b30da
AA
14748 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14749 if (attr != NULL)
14750 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14751 + attr->constant_value (0)));
c906108c
SS
14752
14753 /* Get name of field. */
39cbfefa
DJ
14754 fieldname = dwarf2_name (die, cu);
14755 if (fieldname == NULL)
14756 fieldname = "";
d8151005
DJ
14757
14758 /* The name is already allocated along with this objfile, so we don't
14759 need to duplicate it for the type. */
14760 fp->name = fieldname;
c906108c
SS
14761
14762 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 14763 pointer or virtual base class pointer) to private. */
e142c38c 14764 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14765 {
d48cc9dd 14766 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 14767 new_field->accessibility = DW_ACCESS_private;
264fc0e2 14768 fip->non_public_fields = true;
c906108c
SS
14769 }
14770 }
a9a9bd0f 14771 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14772 {
a9a9bd0f
DC
14773 /* C++ static member. */
14774
14775 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14776 is a declaration, but all versions of G++ as of this writing
14777 (so through at least 3.2.1) incorrectly generate
14778 DW_TAG_variable tags. */
6e70227d 14779
ff355380 14780 const char *physname;
c906108c 14781
a9a9bd0f 14782 /* Get name of field. */
39cbfefa
DJ
14783 fieldname = dwarf2_name (die, cu);
14784 if (fieldname == NULL)
c906108c
SS
14785 return;
14786
254e6b9e 14787 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14788 if (attr
14789 /* Only create a symbol if this is an external value.
14790 new_symbol checks this and puts the value in the global symbol
14791 table, which we want. If it is not external, new_symbol
14792 will try to put the value in cu->list_in_scope which is wrong. */
14793 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14794 {
14795 /* A static const member, not much different than an enum as far as
14796 we're concerned, except that we can support more types. */
14797 new_symbol (die, NULL, cu);
14798 }
14799
2df3850c 14800 /* Get physical name. */
ff355380 14801 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14802
d8151005
DJ
14803 /* The name is already allocated along with this objfile, so we don't
14804 need to duplicate it for the type. */
14805 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 14806 fp->set_type (die_type (die, cu));
d8151005 14807 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14808 }
14809 else if (die->tag == DW_TAG_inheritance)
14810 {
74ac6d43 14811 /* C++ base class field. */
7d79de9a 14812 handle_data_member_location (die, cu, fp);
c906108c 14813 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
14814 fp->set_type (die_type (die, cu));
14815 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 14816 }
2ddeaf8a
TT
14817 else
14818 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14819}
14820
883fd55a
KS
14821/* Can the type given by DIE define another type? */
14822
14823static bool
14824type_can_define_types (const struct die_info *die)
14825{
14826 switch (die->tag)
14827 {
14828 case DW_TAG_typedef:
14829 case DW_TAG_class_type:
14830 case DW_TAG_structure_type:
14831 case DW_TAG_union_type:
14832 case DW_TAG_enumeration_type:
14833 return true;
14834
14835 default:
14836 return false;
14837 }
14838}
14839
14840/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14841
14842static void
883fd55a
KS
14843dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14844 struct dwarf2_cu *cu)
6e70227d 14845{
be2daae6
TT
14846 struct decl_field fp;
14847 memset (&fp, 0, sizeof (fp));
98751a41 14848
883fd55a 14849 gdb_assert (type_can_define_types (die));
98751a41 14850
883fd55a 14851 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14852 fp.name = dwarf2_name (die, cu);
14853 fp.type = read_type_die (die, cu);
98751a41 14854
c191a687 14855 /* Save accessibility. */
bf23a268 14856 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
14857 switch (accessibility)
14858 {
14859 case DW_ACCESS_public:
14860 /* The assumed value if neither private nor protected. */
14861 break;
14862 case DW_ACCESS_private:
be2daae6 14863 fp.is_private = 1;
c191a687
KS
14864 break;
14865 case DW_ACCESS_protected:
be2daae6 14866 fp.is_protected = 1;
c191a687 14867 break;
c191a687
KS
14868 }
14869
883fd55a 14870 if (die->tag == DW_TAG_typedef)
be2daae6 14871 fip->typedef_field_list.push_back (fp);
883fd55a 14872 else
be2daae6 14873 fip->nested_types_list.push_back (fp);
98751a41
JK
14874}
14875
9c6a1327
TT
14876/* A convenience typedef that's used when finding the discriminant
14877 field for a variant part. */
1b95cdb7
SM
14878typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14879 offset_map_type;
9c6a1327
TT
14880
14881/* Compute the discriminant range for a given variant. OBSTACK is
14882 where the results will be stored. VARIANT is the variant to
14883 process. IS_UNSIGNED indicates whether the discriminant is signed
14884 or unsigned. */
14885
14886static const gdb::array_view<discriminant_range>
14887convert_variant_range (struct obstack *obstack, const variant_field &variant,
14888 bool is_unsigned)
14889{
14890 std::vector<discriminant_range> ranges;
14891
14892 if (variant.default_branch)
14893 return {};
14894
14895 if (variant.discr_list_data == nullptr)
14896 {
14897 discriminant_range r
14898 = {variant.discriminant_value, variant.discriminant_value};
14899 ranges.push_back (r);
14900 }
14901 else
14902 {
14903 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14904 variant.discr_list_data->size);
14905 while (!data.empty ())
14906 {
14907 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14908 {
14909 complaint (_("invalid discriminant marker: %d"), data[0]);
14910 break;
14911 }
14912 bool is_range = data[0] == DW_DSC_range;
14913 data = data.slice (1);
14914
14915 ULONGEST low, high;
14916 unsigned int bytes_read;
14917
14918 if (data.empty ())
14919 {
14920 complaint (_("DW_AT_discr_list missing low value"));
14921 break;
14922 }
14923 if (is_unsigned)
14924 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14925 else
14926 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14927 &bytes_read);
14928 data = data.slice (bytes_read);
14929
14930 if (is_range)
14931 {
14932 if (data.empty ())
14933 {
14934 complaint (_("DW_AT_discr_list missing high value"));
14935 break;
14936 }
14937 if (is_unsigned)
14938 high = read_unsigned_leb128 (nullptr, data.data (),
14939 &bytes_read);
14940 else
14941 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14942 &bytes_read);
14943 data = data.slice (bytes_read);
14944 }
14945 else
14946 high = low;
14947
14948 ranges.push_back ({ low, high });
14949 }
14950 }
14951
14952 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14953 ranges.size ());
14954 std::copy (ranges.begin (), ranges.end (), result);
14955 return gdb::array_view<discriminant_range> (result, ranges.size ());
14956}
14957
14958static const gdb::array_view<variant_part> create_variant_parts
14959 (struct obstack *obstack,
14960 const offset_map_type &offset_map,
14961 struct field_info *fi,
14962 const std::vector<variant_part_builder> &variant_parts);
14963
14964/* Fill in a "struct variant" for a given variant field. RESULT is
14965 the variant to fill in. OBSTACK is where any needed allocations
14966 will be done. OFFSET_MAP holds the mapping from section offsets to
14967 fields for the type. FI describes the fields of the type we're
14968 processing. FIELD is the variant field we're converting. */
14969
14970static void
14971create_one_variant (variant &result, struct obstack *obstack,
14972 const offset_map_type &offset_map,
14973 struct field_info *fi, const variant_field &field)
14974{
14975 result.discriminants = convert_variant_range (obstack, field, false);
14976 result.first_field = field.first_field + fi->baseclasses.size ();
14977 result.last_field = field.last_field + fi->baseclasses.size ();
14978 result.parts = create_variant_parts (obstack, offset_map, fi,
14979 field.variant_parts);
14980}
14981
14982/* Fill in a "struct variant_part" for a given variant part. RESULT
14983 is the variant part to fill in. OBSTACK is where any needed
14984 allocations will be done. OFFSET_MAP holds the mapping from
14985 section offsets to fields for the type. FI describes the fields of
14986 the type we're processing. BUILDER is the variant part to be
14987 converted. */
14988
14989static void
14990create_one_variant_part (variant_part &result,
14991 struct obstack *obstack,
14992 const offset_map_type &offset_map,
14993 struct field_info *fi,
14994 const variant_part_builder &builder)
14995{
14996 auto iter = offset_map.find (builder.discriminant_offset);
14997 if (iter == offset_map.end ())
14998 {
14999 result.discriminant_index = -1;
15000 /* Doesn't matter. */
15001 result.is_unsigned = false;
15002 }
15003 else
15004 {
15005 result.discriminant_index = iter->second;
15006 result.is_unsigned
c6d940a9 15007 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15008 }
15009
15010 size_t n = builder.variants.size ();
15011 variant *output = new (obstack) variant[n];
15012 for (size_t i = 0; i < n; ++i)
15013 create_one_variant (output[i], obstack, offset_map, fi,
15014 builder.variants[i]);
15015
15016 result.variants = gdb::array_view<variant> (output, n);
15017}
15018
15019/* Create a vector of variant parts that can be attached to a type.
15020 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15021 holds the mapping from section offsets to fields for the type. FI
15022 describes the fields of the type we're processing. VARIANT_PARTS
15023 is the vector to convert. */
15024
15025static const gdb::array_view<variant_part>
15026create_variant_parts (struct obstack *obstack,
15027 const offset_map_type &offset_map,
15028 struct field_info *fi,
15029 const std::vector<variant_part_builder> &variant_parts)
15030{
15031 if (variant_parts.empty ())
15032 return {};
15033
15034 size_t n = variant_parts.size ();
15035 variant_part *result = new (obstack) variant_part[n];
15036 for (size_t i = 0; i < n; ++i)
15037 create_one_variant_part (result[i], obstack, offset_map, fi,
15038 variant_parts[i]);
15039
15040 return gdb::array_view<variant_part> (result, n);
15041}
15042
15043/* Compute the variant part vector for FIP, attaching it to TYPE when
15044 done. */
15045
15046static void
15047add_variant_property (struct field_info *fip, struct type *type,
15048 struct dwarf2_cu *cu)
15049{
15050 /* Map section offsets of fields to their field index. Note the
15051 field index here does not take the number of baseclasses into
15052 account. */
15053 offset_map_type offset_map;
15054 for (int i = 0; i < fip->fields.size (); ++i)
15055 offset_map[fip->fields[i].offset] = i;
15056
5e22e966 15057 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15058 gdb::array_view<variant_part> parts
15059 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15060 fip->variant_parts);
15061
15062 struct dynamic_prop prop;
8c2e4e06
SM
15063 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15064 obstack_copy (&objfile->objfile_obstack, &parts,
15065 sizeof (parts)));
9c6a1327 15066
5c54719c 15067 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15068}
15069
c906108c
SS
15070/* Create the vector of fields, and attach it to the type. */
15071
15072static void
fba45db2 15073dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15074 struct dwarf2_cu *cu)
c906108c 15075{
317f7127 15076 int nfields = fip->nfields ();
c906108c
SS
15077
15078 /* Record the field count, allocate space for the array of fields,
15079 and create blank accessibility bitfields if necessary. */
5e33d5f4 15080 type->set_num_fields (nfields);
3cabb6b0
SM
15081 type->set_fields
15082 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15083
b4ba55a1 15084 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15085 {
15086 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15087
15088 TYPE_FIELD_PRIVATE_BITS (type) =
15089 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15090 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15091
15092 TYPE_FIELD_PROTECTED_BITS (type) =
15093 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15094 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15095
774b6a14
TT
15096 TYPE_FIELD_IGNORE_BITS (type) =
15097 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15098 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15099 }
15100
15101 /* If the type has baseclasses, allocate and clear a bit vector for
15102 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15103 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15104 {
be2daae6 15105 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15106 unsigned char *pointer;
c906108c
SS
15107
15108 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15109 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15110 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15111 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15112 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15113 }
15114
9c6a1327
TT
15115 if (!fip->variant_parts.empty ())
15116 add_variant_property (fip, type, cu);
2ddeaf8a 15117
be2daae6
TT
15118 /* Copy the saved-up fields into the field vector. */
15119 for (int i = 0; i < nfields; ++i)
c906108c 15120 {
be2daae6
TT
15121 struct nextfield &field
15122 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15123 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15124
ceacbf6e 15125 type->field (i) = field.field;
be2daae6 15126 switch (field.accessibility)
c906108c 15127 {
c5aa993b 15128 case DW_ACCESS_private:
b4ba55a1 15129 if (cu->language != language_ada)
be2daae6 15130 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15131 break;
c906108c 15132
c5aa993b 15133 case DW_ACCESS_protected:
b4ba55a1 15134 if (cu->language != language_ada)
be2daae6 15135 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15136 break;
c906108c 15137
c5aa993b
JM
15138 case DW_ACCESS_public:
15139 break;
c906108c 15140
c5aa993b
JM
15141 default:
15142 /* Unknown accessibility. Complain and treat it as public. */
15143 {
b98664d3 15144 complaint (_("unsupported accessibility %d"),
be2daae6 15145 field.accessibility);
c5aa993b
JM
15146 }
15147 break;
c906108c 15148 }
be2daae6 15149 if (i < fip->baseclasses.size ())
c906108c 15150 {
be2daae6 15151 switch (field.virtuality)
c906108c 15152 {
c5aa993b
JM
15153 case DW_VIRTUALITY_virtual:
15154 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15155 if (cu->language == language_ada)
a73c6dcd 15156 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15157 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15158 break;
c906108c
SS
15159 }
15160 }
c906108c
SS
15161 }
15162}
15163
7d27a96d
TT
15164/* Return true if this member function is a constructor, false
15165 otherwise. */
15166
15167static int
15168dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15169{
15170 const char *fieldname;
fe978cb0 15171 const char *type_name;
7d27a96d
TT
15172 int len;
15173
15174 if (die->parent == NULL)
15175 return 0;
15176
15177 if (die->parent->tag != DW_TAG_structure_type
15178 && die->parent->tag != DW_TAG_union_type
15179 && die->parent->tag != DW_TAG_class_type)
15180 return 0;
15181
15182 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15183 type_name = dwarf2_name (die->parent, cu);
15184 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15185 return 0;
15186
15187 len = strlen (fieldname);
fe978cb0
PA
15188 return (strncmp (fieldname, type_name, len) == 0
15189 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15190}
15191
c906108c
SS
15192/* Add a member function to the proper fieldlist. */
15193
15194static void
107d2387 15195dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15196 struct type *type, struct dwarf2_cu *cu)
c906108c 15197{
5e22e966 15198 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15199 struct attribute *attr;
c906108c 15200 int i;
be2daae6 15201 struct fnfieldlist *flp = nullptr;
c906108c 15202 struct fn_field *fnp;
15d034d0 15203 const char *fieldname;
f792889a 15204 struct type *this_type;
c906108c 15205
b4ba55a1 15206 if (cu->language == language_ada)
a73c6dcd 15207 error (_("unexpected member function in Ada type"));
b4ba55a1 15208
2df3850c 15209 /* Get name of member function. */
39cbfefa
DJ
15210 fieldname = dwarf2_name (die, cu);
15211 if (fieldname == NULL)
2df3850c 15212 return;
c906108c 15213
c906108c 15214 /* Look up member function name in fieldlist. */
be2daae6 15215 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15216 {
27bfe10e 15217 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15218 {
15219 flp = &fip->fnfieldlists[i];
15220 break;
15221 }
c906108c
SS
15222 }
15223
be2daae6
TT
15224 /* Create a new fnfieldlist if necessary. */
15225 if (flp == nullptr)
c906108c 15226 {
be2daae6
TT
15227 fip->fnfieldlists.emplace_back ();
15228 flp = &fip->fnfieldlists.back ();
c906108c 15229 flp->name = fieldname;
be2daae6 15230 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15231 }
15232
be2daae6
TT
15233 /* Create a new member function field and add it to the vector of
15234 fnfieldlists. */
15235 flp->fnfields.emplace_back ();
15236 fnp = &flp->fnfields.back ();
3da10d80
KS
15237
15238 /* Delay processing of the physname until later. */
9c37b5ae 15239 if (cu->language == language_cplus)
be2daae6
TT
15240 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15241 die, cu);
3da10d80
KS
15242 else
15243 {
1d06ead6 15244 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15245 fnp->physname = physname ? physname : "";
15246 }
15247
c906108c 15248 fnp->type = alloc_type (objfile);
f792889a 15249 this_type = read_type_die (die, cu);
78134374 15250 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15251 {
1f704f76 15252 int nparams = this_type->num_fields ();
c906108c 15253
f792889a 15254 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15255 of the method itself (TYPE_CODE_METHOD). */
15256 smash_to_method_type (fnp->type, type,
f792889a 15257 TYPE_TARGET_TYPE (this_type),
80fc5e77 15258 this_type->fields (),
1f704f76 15259 this_type->num_fields (),
a409645d 15260 this_type->has_varargs ());
c906108c
SS
15261
15262 /* Handle static member functions.
dda83cd7
SM
15263 Dwarf2 has no clean way to discern C++ static and non-static
15264 member functions. G++ helps GDB by marking the first
15265 parameter for non-static member functions (which is the this
15266 pointer) as artificial. We obtain this information from
15267 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15268 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15269 fnp->voffset = VOFFSET_STATIC;
15270 }
15271 else
b98664d3 15272 complaint (_("member function type missing for '%s'"),
3da10d80 15273 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15274
15275 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15276 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15277 fnp->fcontext = die_containing_type (die, cu);
c906108c 15278
3e43a32a
MS
15279 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15280 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15281
15282 /* Get accessibility. */
bf23a268 15283 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15284 switch (accessibility)
c906108c 15285 {
60d5a603
JK
15286 case DW_ACCESS_private:
15287 fnp->is_private = 1;
15288 break;
15289 case DW_ACCESS_protected:
15290 fnp->is_protected = 1;
15291 break;
c906108c
SS
15292 }
15293
b02dede2 15294 /* Check for artificial methods. */
e142c38c 15295 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15296 if (attr && attr->as_boolean ())
b02dede2
DJ
15297 fnp->is_artificial = 1;
15298
e35000a7
TBA
15299 /* Check for defaulted methods. */
15300 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15301 if (attr != nullptr)
15302 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15303
15304 /* Check for deleted methods. */
15305 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15306 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15307 fnp->is_deleted = 1;
15308
7d27a96d
TT
15309 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15310
0d564a31 15311 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15312 function. For older versions of GCC, this is an offset in the
15313 appropriate virtual table, as specified by DW_AT_containing_type.
15314 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15315 to the object address. */
15316
e142c38c 15317 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15318 if (attr != nullptr)
8e19ed76 15319 {
9d2246fc 15320 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15321 {
9d2246fc
TT
15322 struct dwarf_block *block = attr->as_block ();
15323
15324 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15325 {
15326 /* Old-style GCC. */
9d2246fc 15327 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15328 }
9d2246fc
TT
15329 else if (block->data[0] == DW_OP_deref
15330 || (block->size > 1
15331 && block->data[0] == DW_OP_deref_size
15332 && block->data[1] == cu->header.addr_size))
aec5aa8b 15333 {
9d2246fc 15334 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15335 if ((fnp->voffset % cu->header.addr_size) != 0)
15336 dwarf2_complex_location_expr_complaint ();
15337 else
15338 fnp->voffset /= cu->header.addr_size;
15339 fnp->voffset += 2;
15340 }
15341 else
15342 dwarf2_complex_location_expr_complaint ();
15343
15344 if (!fnp->fcontext)
7e993ebf
KS
15345 {
15346 /* If there is no `this' field and no DW_AT_containing_type,
15347 we cannot actually find a base class context for the
15348 vtable! */
1f704f76 15349 if (this_type->num_fields () == 0
7e993ebf
KS
15350 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15351 {
b98664d3 15352 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15353 "function \"%s\" (offset %s)"),
15354 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15355 }
15356 else
15357 {
15358 fnp->fcontext
940da03e 15359 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15360 }
15361 }
aec5aa8b 15362 }
cd6c91b4 15363 else if (attr->form_is_section_offset ())
dda83cd7 15364 {
4d3c2250 15365 dwarf2_complex_location_expr_complaint ();
dda83cd7 15366 }
8e19ed76 15367 else
dda83cd7 15368 {
4d3c2250
KB
15369 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15370 fieldname);
dda83cd7 15371 }
0d564a31 15372 }
d48cc9dd
DJ
15373 else
15374 {
15375 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15376 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15377 {
15378 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15379 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15380 "but the vtable offset is not specified"),
9d8780f0 15381 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15382 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15383 TYPE_CPLUS_DYNAMIC (type) = 1;
15384 }
15385 }
c906108c
SS
15386}
15387
15388/* Create the vector of member function fields, and attach it to the type. */
15389
15390static void
fba45db2 15391dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15392 struct dwarf2_cu *cu)
c906108c 15393{
b4ba55a1 15394 if (cu->language == language_ada)
a73c6dcd 15395 error (_("unexpected member functions in Ada type"));
b4ba55a1 15396
c906108c
SS
15397 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15398 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15399 TYPE_ALLOC (type,
15400 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15401
be2daae6 15402 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15403 {
be2daae6 15404 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15405 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15406
be2daae6
TT
15407 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15408 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15409 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15410 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15411
15412 for (int k = 0; k < nf.fnfields.size (); ++k)
15413 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15414 }
15415
be2daae6 15416 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15417}
15418
1168df01
JB
15419/* Returns non-zero if NAME is the name of a vtable member in CU's
15420 language, zero otherwise. */
15421static int
15422is_vtable_name (const char *name, struct dwarf2_cu *cu)
15423{
15424 static const char vptr[] = "_vptr";
15425
9c37b5ae
TT
15426 /* Look for the C++ form of the vtable. */
15427 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15428 return 1;
15429
15430 return 0;
15431}
15432
c0dd20ea 15433/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15434 functions, with the ABI-specified layout. If TYPE describes
15435 such a structure, smash it into a member function type.
61049d3b
DJ
15436
15437 GCC shouldn't do this; it should just output pointer to member DIEs.
15438 This is GCC PR debug/28767. */
c0dd20ea 15439
0b92b5bb
TT
15440static void
15441quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15442{
09e2d7c7 15443 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15444
15445 /* Check for a structure with no name and two children. */
1f704f76 15446 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15447 return;
c0dd20ea
DJ
15448
15449 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15450 if (TYPE_FIELD_NAME (type, 0) == NULL
15451 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15452 || TYPE_FIELD_NAME (type, 1) == NULL
15453 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15454 return;
c0dd20ea
DJ
15455
15456 /* Find the type of the method. */
940da03e 15457 pfn_type = type->field (0).type ();
c0dd20ea 15458 if (pfn_type == NULL
78134374
SM
15459 || pfn_type->code () != TYPE_CODE_PTR
15460 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15461 return;
c0dd20ea
DJ
15462
15463 /* Look for the "this" argument. */
15464 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15465 if (pfn_type->num_fields () == 0
940da03e
SM
15466 /* || pfn_type->field (0).type () == NULL */
15467 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15468 return;
c0dd20ea 15469
940da03e 15470 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15471 new_type = alloc_type (objfile);
09e2d7c7 15472 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15473 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15474 pfn_type->has_varargs ());
0b92b5bb 15475 smash_to_methodptr_type (type, new_type);
c0dd20ea 15476}
1168df01 15477
e26624c6
TT
15478/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15479 requires rewriting, then copy it and return the updated copy.
15480 Otherwise return nullptr. */
15481
15482static struct type *
15483rewrite_array_type (struct type *type)
15484{
15485 if (type->code () != TYPE_CODE_ARRAY)
15486 return nullptr;
15487
15488 struct type *index_type = type->index_type ();
15489 range_bounds *current_bounds = index_type->bounds ();
15490
15491 /* Handle multi-dimensional arrays. */
15492 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15493 if (new_target == nullptr)
15494 {
15495 /* Maybe we don't need to rewrite this array. */
15496 if (current_bounds->low.kind () == PROP_CONST
15497 && current_bounds->high.kind () == PROP_CONST)
15498 return nullptr;
15499 }
15500
15501 /* Either the target type was rewritten, or the bounds have to be
15502 updated. Either way we want to copy the type and update
15503 everything. */
15504 struct type *copy = copy_type (type);
15505 int nfields = copy->num_fields ();
15506 field *new_fields
15507 = ((struct field *) TYPE_ZALLOC (copy,
15508 nfields * sizeof (struct field)));
15509 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15510 copy->set_fields (new_fields);
15511 if (new_target != nullptr)
15512 TYPE_TARGET_TYPE (copy) = new_target;
15513
15514 struct type *index_copy = copy_type (index_type);
15515 range_bounds *bounds
15516 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15517 sizeof (range_bounds));
15518 *bounds = *current_bounds;
15519 bounds->low.set_const_val (1);
15520 bounds->high.set_const_val (0);
15521 index_copy->set_bounds (bounds);
15522 copy->set_index_type (index_copy);
15523
15524 return copy;
15525}
15526
57567375
TT
15527/* While some versions of GCC will generate complicated DWARF for an
15528 array (see quirk_ada_thick_pointer), more recent versions were
15529 modified to emit an explicit thick pointer structure. However, in
15530 this case, the array still has DWARF expressions for its ranges,
15531 and these must be ignored. */
15532
15533static void
15534quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15535 struct type *type)
15536{
15537 gdb_assert (cu->language == language_ada);
15538
15539 /* Check for a structure with two children. */
15540 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15541 return;
15542
15543 /* Check for P_ARRAY and P_BOUNDS members. */
15544 if (TYPE_FIELD_NAME (type, 0) == NULL
15545 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15546 || TYPE_FIELD_NAME (type, 1) == NULL
15547 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15548 return;
15549
15550 /* Make sure we're looking at a pointer to an array. */
15551 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15552 return;
57567375 15553
e26624c6
TT
15554 /* The Ada code already knows how to handle these types, so all that
15555 we need to do is turn the bounds into static bounds. However, we
15556 don't want to rewrite existing array or index types in-place,
15557 because those may be referenced in other contexts where this
15558 rewriting is undesirable. */
15559 struct type *new_ary_type
15560 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15561 if (new_ary_type != nullptr)
15562 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15563}
15564
2b4424c3
TT
15565/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15566 appropriate error checking and issuing complaints if there is a
15567 problem. */
15568
15569static ULONGEST
15570get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15571{
15572 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15573
15574 if (attr == nullptr)
15575 return 0;
15576
cd6c91b4 15577 if (!attr->form_is_constant ())
2b4424c3 15578 {
b98664d3 15579 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15580 " - DIE at %s [in module %s]"),
15581 sect_offset_str (die->sect_off),
5e22e966 15582 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15583 return 0;
15584 }
15585
529908cb
TT
15586 LONGEST val = attr->constant_value (0);
15587 if (val < 0)
2b4424c3 15588 {
529908cb
TT
15589 complaint (_("DW_AT_alignment value must not be negative"
15590 " - DIE at %s [in module %s]"),
15591 sect_offset_str (die->sect_off),
15592 objfile_name (cu->per_objfile->objfile));
15593 return 0;
2b4424c3 15594 }
529908cb 15595 ULONGEST align = val;
2b4424c3
TT
15596
15597 if (align == 0)
15598 {
b98664d3 15599 complaint (_("DW_AT_alignment value must not be zero"
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 if ((align & (align - 1)) != 0)
15606 {
b98664d3 15607 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15608 " - DIE at %s [in module %s]"),
15609 sect_offset_str (die->sect_off),
5e22e966 15610 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15611 return 0;
15612 }
15613
15614 return align;
15615}
15616
15617/* If the DIE has a DW_AT_alignment attribute, use its value to set
15618 the alignment for TYPE. */
15619
15620static void
15621maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15622 struct type *type)
15623{
15624 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15625 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15626 " - DIE at %s [in module %s]"),
15627 sect_offset_str (die->sect_off),
5e22e966 15628 objfile_name (cu->per_objfile->objfile));
2b4424c3 15629}
685b1105 15630
e35000a7
TBA
15631/* Check if the given VALUE is a valid enum dwarf_calling_convention
15632 constant for a type, according to DWARF5 spec, Table 5.5. */
15633
15634static bool
15635is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15636{
15637 switch (value)
15638 {
15639 case DW_CC_normal:
15640 case DW_CC_pass_by_reference:
15641 case DW_CC_pass_by_value:
15642 return true;
15643
15644 default:
15645 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15646 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15647 return false;
15648 }
15649}
15650
d0922fcf
TBA
15651/* Check if the given VALUE is a valid enum dwarf_calling_convention
15652 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15653 also according to GNU-specific values (see include/dwarf2.h). */
15654
15655static bool
15656is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15657{
15658 switch (value)
15659 {
15660 case DW_CC_normal:
15661 case DW_CC_program:
15662 case DW_CC_nocall:
15663 return true;
15664
15665 case DW_CC_GNU_renesas_sh:
15666 case DW_CC_GNU_borland_fastcall_i386:
15667 case DW_CC_GDB_IBM_OpenCL:
15668 return true;
15669
15670 default:
15671 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15672 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15673 return false;
15674 }
15675}
15676
c906108c 15677/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15678 (definition) to create a type for the structure or union. Fill in
15679 the type's name and general properties; the members will not be
83655187
DE
15680 processed until process_structure_scope. A symbol table entry for
15681 the type will also not be done until process_structure_scope (assuming
15682 the type has a name).
c906108c 15683
c767944b
DJ
15684 NOTE: we need to call these functions regardless of whether or not the
15685 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15686 structure or union. This gets the type entered into our set of
83655187 15687 user defined types. */
c906108c 15688
f792889a 15689static struct type *
134d01f1 15690read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15691{
5e22e966 15692 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15693 struct type *type;
15694 struct attribute *attr;
15d034d0 15695 const char *name;
c906108c 15696
348e048f
DE
15697 /* If the definition of this type lives in .debug_types, read that type.
15698 Don't follow DW_AT_specification though, that will take us back up
15699 the chain and we want to go down. */
052c8bb8 15700 attr = die->attr (DW_AT_signature);
435d3d88 15701 if (attr != nullptr)
348e048f 15702 {
ac9ec31b 15703 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15704
ac9ec31b 15705 /* The type's CU may not be the same as CU.
02142a6c 15706 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15707 return set_die_type (die, type, cu);
15708 }
15709
c0dd20ea 15710 type = alloc_type (objfile);
c906108c 15711 INIT_CPLUS_SPECIFIC (type);
93311388 15712
39cbfefa
DJ
15713 name = dwarf2_name (die, cu);
15714 if (name != NULL)
c906108c 15715 {
987504bb 15716 if (cu->language == language_cplus
c44af4eb
TT
15717 || cu->language == language_d
15718 || cu->language == language_rust)
63d06c5c 15719 {
15d034d0 15720 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15721
15722 /* dwarf2_full_name might have already finished building the DIE's
15723 type. If so, there is no need to continue. */
15724 if (get_die_type (die, cu) != NULL)
15725 return get_die_type (die, cu);
15726
d0e39ea2 15727 type->set_name (full_name);
63d06c5c
DC
15728 }
15729 else
15730 {
d8151005
DJ
15731 /* The name is already allocated along with this objfile, so
15732 we don't need to duplicate it for the type. */
d0e39ea2 15733 type->set_name (name);
63d06c5c 15734 }
c906108c
SS
15735 }
15736
15737 if (die->tag == DW_TAG_structure_type)
15738 {
67607e24 15739 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15740 }
15741 else if (die->tag == DW_TAG_union_type)
15742 {
67607e24 15743 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15744 }
15745 else
15746 {
67607e24 15747 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15748 }
15749
0cc2414c 15750 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
aa70e35c 15751 type->set_is_declared_class (true);
0cc2414c 15752
e35000a7
TBA
15753 /* Store the calling convention in the type if it's available in
15754 the die. Otherwise the calling convention remains set to
15755 the default value DW_CC_normal. */
15756 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15757 if (attr != nullptr
529908cb 15758 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
15759 {
15760 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15761 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 15762 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
15763 }
15764
e142c38c 15765 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15766 if (attr != nullptr)
c906108c 15767 {
cd6c91b4 15768 if (attr->form_is_constant ())
dda83cd7 15769 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
15770 else
15771 {
f8e89861 15772 struct dynamic_prop prop;
293e7e51 15773 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15774 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 15775 TYPE_LENGTH (type) = 0;
155bfbd3 15776 }
c906108c
SS
15777 }
15778 else
15779 {
15780 TYPE_LENGTH (type) = 0;
15781 }
15782
2b4424c3
TT
15783 maybe_set_alignment (cu, die, type);
15784
5230b05a 15785 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15786 {
5230b05a
WT
15787 /* ICC<14 does not output the required DW_AT_declaration on
15788 incomplete types, but gives them a size of zero. */
b4b73759 15789 type->set_is_stub (true);
685b1105
JK
15790 }
15791 else
9baccff6 15792 type->set_stub_is_supported (true);
685b1105 15793
dc718098 15794 if (die_is_declaration (die, cu))
b4b73759 15795 type->set_is_stub (true);
a6c727b2
DJ
15796 else if (attr == NULL && die->child == NULL
15797 && producer_is_realview (cu->producer))
15798 /* RealView does not output the required DW_AT_declaration
15799 on incomplete types. */
b4b73759 15800 type->set_is_stub (true);
dc718098 15801
c906108c
SS
15802 /* We need to add the type field to the die immediately so we don't
15803 infinitely recurse when dealing with pointers to the structure
0963b4bd 15804 type within the structure itself. */
1c379e20 15805 set_die_type (die, type, cu);
c906108c 15806
7e314c57
JK
15807 /* set_die_type should be already done. */
15808 set_descriptive_type (type, die, cu);
15809
c767944b
DJ
15810 return type;
15811}
15812
9c6a1327
TT
15813static void handle_struct_member_die
15814 (struct die_info *child_die,
15815 struct type *type,
15816 struct field_info *fi,
15817 std::vector<struct symbol *> *template_args,
15818 struct dwarf2_cu *cu);
15819
15820/* A helper for handle_struct_member_die that handles
15821 DW_TAG_variant_part. */
15822
15823static void
15824handle_variant_part (struct die_info *die, struct type *type,
15825 struct field_info *fi,
15826 std::vector<struct symbol *> *template_args,
15827 struct dwarf2_cu *cu)
15828{
15829 variant_part_builder *new_part;
15830 if (fi->current_variant_part == nullptr)
15831 {
15832 fi->variant_parts.emplace_back ();
15833 new_part = &fi->variant_parts.back ();
15834 }
15835 else if (!fi->current_variant_part->processing_variant)
15836 {
15837 complaint (_("nested DW_TAG_variant_part seen "
15838 "- DIE at %s [in module %s]"),
15839 sect_offset_str (die->sect_off),
5e22e966 15840 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15841 return;
15842 }
15843 else
15844 {
15845 variant_field &current = fi->current_variant_part->variants.back ();
15846 current.variant_parts.emplace_back ();
15847 new_part = &current.variant_parts.back ();
15848 }
15849
15850 /* When we recurse, we want callees to add to this new variant
15851 part. */
15852 scoped_restore save_current_variant_part
15853 = make_scoped_restore (&fi->current_variant_part, new_part);
15854
15855 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15856 if (discr == NULL)
15857 {
15858 /* It's a univariant form, an extension we support. */
15859 }
15860 else if (discr->form_is_ref ())
15861 {
15862 struct dwarf2_cu *target_cu = cu;
15863 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15864
15865 new_part->discriminant_offset = target_die->sect_off;
15866 }
15867 else
15868 {
15869 complaint (_("DW_AT_discr does not have DIE reference form"
15870 " - DIE at %s [in module %s]"),
15871 sect_offset_str (die->sect_off),
5e22e966 15872 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15873 }
15874
15875 for (die_info *child_die = die->child;
15876 child_die != NULL;
15877 child_die = child_die->sibling)
15878 handle_struct_member_die (child_die, type, fi, template_args, cu);
15879}
15880
15881/* A helper for handle_struct_member_die that handles
15882 DW_TAG_variant. */
15883
15884static void
15885handle_variant (struct die_info *die, struct type *type,
15886 struct field_info *fi,
15887 std::vector<struct symbol *> *template_args,
15888 struct dwarf2_cu *cu)
15889{
15890 if (fi->current_variant_part == nullptr)
15891 {
15892 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
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 if (fi->current_variant_part->processing_variant)
15899 {
15900 complaint (_("nested DW_TAG_variant seen "
15901 "- DIE at %s [in module %s]"),
15902 sect_offset_str (die->sect_off),
5e22e966 15903 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15904 return;
15905 }
15906
15907 scoped_restore save_processing_variant
15908 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15909 true);
15910
15911 fi->current_variant_part->variants.emplace_back ();
15912 variant_field &variant = fi->current_variant_part->variants.back ();
15913 variant.first_field = fi->fields.size ();
15914
15915 /* In a variant we want to get the discriminant and also add a
15916 field for our sole member child. */
15917 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 15918 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
15919 {
15920 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 15921 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
15922 variant.default_branch = true;
15923 else
9d2246fc 15924 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
15925 }
15926 else
cae21f8e 15927 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
15928
15929 for (die_info *variant_child = die->child;
15930 variant_child != NULL;
15931 variant_child = variant_child->sibling)
15932 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15933
15934 variant.last_field = fi->fields.size ();
15935}
15936
2ddeaf8a
TT
15937/* A helper for process_structure_scope that handles a single member
15938 DIE. */
15939
15940static void
15941handle_struct_member_die (struct die_info *child_die, struct type *type,
15942 struct field_info *fi,
15943 std::vector<struct symbol *> *template_args,
15944 struct dwarf2_cu *cu)
15945{
15946 if (child_die->tag == DW_TAG_member
9c6a1327 15947 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15948 {
15949 /* NOTE: carlton/2002-11-05: A C++ static data member
15950 should be a DW_TAG_member that is a declaration, but
15951 all versions of G++ as of this writing (so through at
15952 least 3.2.1) incorrectly generate DW_TAG_variable
15953 tags for them instead. */
15954 dwarf2_add_field (fi, child_die, cu);
15955 }
15956 else if (child_die->tag == DW_TAG_subprogram)
15957 {
15958 /* Rust doesn't have member functions in the C++ sense.
15959 However, it does emit ordinary functions as children
15960 of a struct DIE. */
15961 if (cu->language == language_rust)
15962 read_func_scope (child_die, cu);
15963 else
15964 {
15965 /* C++ member function. */
15966 dwarf2_add_member_fn (fi, child_die, type, cu);
15967 }
15968 }
15969 else if (child_die->tag == DW_TAG_inheritance)
15970 {
15971 /* C++ base class field. */
15972 dwarf2_add_field (fi, child_die, cu);
15973 }
15974 else if (type_can_define_types (child_die))
15975 dwarf2_add_type_defn (fi, child_die, cu);
15976 else if (child_die->tag == DW_TAG_template_type_param
15977 || child_die->tag == DW_TAG_template_value_param)
15978 {
15979 struct symbol *arg = new_symbol (child_die, NULL, cu);
15980
15981 if (arg != NULL)
15982 template_args->push_back (arg);
15983 }
9c6a1327
TT
15984 else if (child_die->tag == DW_TAG_variant_part)
15985 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15986 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15987 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15988}
15989
c767944b
DJ
15990/* Finish creating a structure or union type, including filling in
15991 its members and creating a symbol for it. */
15992
15993static void
15994process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15995{
5e22e966 15996 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15997 struct die_info *child_die;
c767944b
DJ
15998 struct type *type;
15999
16000 type = get_die_type (die, cu);
16001 if (type == NULL)
16002 type = read_structure_type (die, cu);
16003
3e1d3d8c 16004 bool has_template_parameters = false;
e142c38c 16005 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16006 {
16007 struct field_info fi;
2f4732b0 16008 std::vector<struct symbol *> template_args;
c906108c 16009
639d11d3 16010 child_die = die->child;
c906108c
SS
16011
16012 while (child_die && child_die->tag)
16013 {
2ddeaf8a 16014 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16015 child_die = child_die->sibling;
c906108c
SS
16016 }
16017
34eaf542 16018 /* Attach template arguments to type. */
2f4732b0 16019 if (!template_args.empty ())
34eaf542 16020 {
3e1d3d8c 16021 has_template_parameters = true;
34eaf542 16022 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16023 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16024 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16025 = XOBNEWVEC (&objfile->objfile_obstack,
16026 struct symbol *,
16027 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16028 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16029 template_args.data (),
34eaf542
TT
16030 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16031 * sizeof (struct symbol *)));
34eaf542
TT
16032 }
16033
c906108c 16034 /* Attach fields and member functions to the type. */
317f7127 16035 if (fi.nfields () > 0)
e7c27a73 16036 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16037 if (!fi.fnfieldlists.empty ())
c906108c 16038 {
e7c27a73 16039 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16040
c5aa993b 16041 /* Get the type which refers to the base class (possibly this
c906108c 16042 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16043 class from the DW_AT_containing_type attribute. This use of
16044 DW_AT_containing_type is a GNU extension. */
c906108c 16045
e142c38c 16046 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16047 {
e7c27a73 16048 struct type *t = die_containing_type (die, cu);
c906108c 16049
ae6ae975 16050 set_type_vptr_basetype (type, t);
c906108c
SS
16051 if (type == t)
16052 {
c906108c
SS
16053 int i;
16054
16055 /* Our own class provides vtbl ptr. */
1f704f76 16056 for (i = t->num_fields () - 1;
c906108c
SS
16057 i >= TYPE_N_BASECLASSES (t);
16058 --i)
16059 {
0d5cff50 16060 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16061
dda83cd7 16062 if (is_vtable_name (fieldname, cu))
c906108c 16063 {
ae6ae975 16064 set_type_vptr_fieldno (type, i);
c906108c
SS
16065 break;
16066 }
16067 }
16068
16069 /* Complain if virtual function table field not found. */
16070 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16071 complaint (_("virtual function table pointer "
3e43a32a 16072 "not found when defining class '%s'"),
7d93a1e0 16073 type->name () ? type->name () : "");
c906108c
SS
16074 }
16075 else
16076 {
ae6ae975 16077 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16078 }
16079 }
f6235d4c 16080 else if (cu->producer
61012eef 16081 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16082 {
16083 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16084 of the containing type, but the vtable pointer is
16085 always named __vfp. */
f6235d4c
EZ
16086
16087 int i;
16088
1f704f76 16089 for (i = type->num_fields () - 1;
f6235d4c
EZ
16090 i >= TYPE_N_BASECLASSES (type);
16091 --i)
16092 {
16093 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16094 {
ae6ae975
DE
16095 set_type_vptr_fieldno (type, i);
16096 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16097 break;
16098 }
16099 }
16100 }
c906108c 16101 }
98751a41
JK
16102
16103 /* Copy fi.typedef_field_list linked list elements content into the
16104 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16105 if (!fi.typedef_field_list.empty ())
98751a41 16106 {
be2daae6 16107 int count = fi.typedef_field_list.size ();
98751a41 16108
a0d7a4ff 16109 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16110 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16111 = ((struct decl_field *)
be2daae6
TT
16112 TYPE_ALLOC (type,
16113 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16114 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16115
be2daae6
TT
16116 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16117 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16118 }
c767944b 16119
883fd55a
KS
16120 /* Copy fi.nested_types_list linked list elements content into the
16121 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16122 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16123 {
be2daae6 16124 int count = fi.nested_types_list.size ();
883fd55a
KS
16125
16126 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16127 TYPE_NESTED_TYPES_ARRAY (type)
16128 = ((struct decl_field *)
be2daae6
TT
16129 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16130 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16131
be2daae6
TT
16132 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16133 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16134 }
c906108c 16135 }
63d06c5c 16136
bb5ed363 16137 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16138 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16139 cu->rust_unions.push_back (type);
57567375
TT
16140 else if (cu->language == language_ada)
16141 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16142
90aeadfc
DC
16143 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16144 snapshots) has been known to create a die giving a declaration
16145 for a class that has, as a child, a die giving a definition for a
16146 nested class. So we have to process our children even if the
16147 current die is a declaration. Normally, of course, a declaration
16148 won't have any children at all. */
134d01f1 16149
ca040673
DE
16150 child_die = die->child;
16151
90aeadfc
DC
16152 while (child_die != NULL && child_die->tag)
16153 {
16154 if (child_die->tag == DW_TAG_member
16155 || child_die->tag == DW_TAG_variable
34eaf542
TT
16156 || child_die->tag == DW_TAG_inheritance
16157 || child_die->tag == DW_TAG_template_value_param
16158 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16159 {
90aeadfc 16160 /* Do nothing. */
134d01f1 16161 }
90aeadfc
DC
16162 else
16163 process_die (child_die, cu);
134d01f1 16164
436c571c 16165 child_die = child_die->sibling;
134d01f1
DJ
16166 }
16167
fa4028e9
JB
16168 /* Do not consider external references. According to the DWARF standard,
16169 these DIEs are identified by the fact that they have no byte_size
16170 attribute, and a declaration attribute. */
16171 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16172 || !die_is_declaration (die, cu)
16173 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16174 {
16175 struct symbol *sym = new_symbol (die, type, cu);
16176
16177 if (has_template_parameters)
16178 {
a776957c
TT
16179 struct symtab *symtab;
16180 if (sym != nullptr)
16181 symtab = symbol_symtab (sym);
16182 else if (cu->line_header != nullptr)
16183 {
16184 /* Any related symtab will do. */
16185 symtab
7ba99d21 16186 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16187 }
16188 else
16189 {
16190 symtab = nullptr;
16191 complaint (_("could not find suitable "
16192 "symtab for template parameter"
16193 " - DIE at %s [in module %s]"),
16194 sect_offset_str (die->sect_off),
16195 objfile_name (objfile));
16196 }
16197
16198 if (symtab != nullptr)
16199 {
16200 /* Make sure that the symtab is set on the new symbols.
16201 Even though they don't appear in this symtab directly,
16202 other parts of gdb assume that symbols do, and this is
16203 reasonably true. */
16204 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16205 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16206 }
3e1d3d8c
TT
16207 }
16208 }
134d01f1
DJ
16209}
16210
ed6acedd
TT
16211/* Assuming DIE is an enumeration type, and TYPE is its associated
16212 type, update TYPE using some information only available in DIE's
16213 children. In particular, the fields are computed. */
55426c9d
JB
16214
16215static void
16216update_enumeration_type_from_children (struct die_info *die,
16217 struct type *type,
16218 struct dwarf2_cu *cu)
16219{
60f7655a 16220 struct die_info *child_die;
55426c9d
JB
16221 int unsigned_enum = 1;
16222 int flag_enum = 1;
55426c9d 16223
8268c778 16224 auto_obstack obstack;
ed6acedd 16225 std::vector<struct field> fields;
55426c9d 16226
60f7655a
DE
16227 for (child_die = die->child;
16228 child_die != NULL && child_die->tag;
436c571c 16229 child_die = child_die->sibling)
55426c9d
JB
16230 {
16231 struct attribute *attr;
16232 LONGEST value;
16233 const gdb_byte *bytes;
16234 struct dwarf2_locexpr_baton *baton;
16235 const char *name;
60f7655a 16236
55426c9d
JB
16237 if (child_die->tag != DW_TAG_enumerator)
16238 continue;
16239
16240 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16241 if (attr == NULL)
16242 continue;
16243
16244 name = dwarf2_name (child_die, cu);
16245 if (name == NULL)
16246 name = "<anonymous enumerator>";
16247
16248 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16249 &value, &bytes, &baton);
16250 if (value < 0)
16251 {
16252 unsigned_enum = 0;
16253 flag_enum = 0;
16254 }
55426c9d 16255 else
edd45eb0
SM
16256 {
16257 if (count_one_bits_ll (value) >= 2)
16258 flag_enum = 0;
edd45eb0 16259 }
55426c9d 16260
ed6acedd
TT
16261 fields.emplace_back ();
16262 struct field &field = fields.back ();
16263 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16264 SET_FIELD_ENUMVAL (field, value);
16265 }
16266
16267 if (!fields.empty ())
16268 {
5e33d5f4 16269 type->set_num_fields (fields.size ());
3cabb6b0
SM
16270 type->set_fields
16271 ((struct field *)
16272 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16273 memcpy (type->fields (), fields.data (),
ed6acedd 16274 sizeof (struct field) * fields.size ());
55426c9d
JB
16275 }
16276
16277 if (unsigned_enum)
653223d3
SM
16278 type->set_is_unsigned (true);
16279
55426c9d 16280 if (flag_enum)
9902b327 16281 type->set_is_flag_enum (true);
55426c9d
JB
16282}
16283
134d01f1
DJ
16284/* Given a DW_AT_enumeration_type die, set its type. We do not
16285 complete the type's fields yet, or create any symbols. */
c906108c 16286
f792889a 16287static struct type *
134d01f1 16288read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16289{
5e22e966 16290 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16291 struct type *type;
c906108c 16292 struct attribute *attr;
0114d602 16293 const char *name;
134d01f1 16294
348e048f
DE
16295 /* If the definition of this type lives in .debug_types, read that type.
16296 Don't follow DW_AT_specification though, that will take us back up
16297 the chain and we want to go down. */
052c8bb8 16298 attr = die->attr (DW_AT_signature);
435d3d88 16299 if (attr != nullptr)
348e048f 16300 {
ac9ec31b 16301 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16302
ac9ec31b 16303 /* The type's CU may not be the same as CU.
02142a6c 16304 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16305 return set_die_type (die, type, cu);
16306 }
16307
c906108c
SS
16308 type = alloc_type (objfile);
16309
67607e24 16310 type->set_code (TYPE_CODE_ENUM);
94af9270 16311 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16312 if (name != NULL)
d0e39ea2 16313 type->set_name (name);
c906108c 16314
0626fc76
TT
16315 attr = dwarf2_attr (die, DW_AT_type, cu);
16316 if (attr != NULL)
16317 {
16318 struct type *underlying_type = die_type (die, cu);
16319
16320 TYPE_TARGET_TYPE (type) = underlying_type;
16321 }
16322
e142c38c 16323 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16324 if (attr != nullptr)
c906108c 16325 {
529908cb 16326 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16327 }
16328 else
16329 {
16330 TYPE_LENGTH (type) = 0;
16331 }
16332
2b4424c3
TT
16333 maybe_set_alignment (cu, die, type);
16334
137033e9
JB
16335 /* The enumeration DIE can be incomplete. In Ada, any type can be
16336 declared as private in the package spec, and then defined only
16337 inside the package body. Such types are known as Taft Amendment
16338 Types. When another package uses such a type, an incomplete DIE
16339 may be generated by the compiler. */
02eb380e 16340 if (die_is_declaration (die, cu))
b4b73759 16341 type->set_is_stub (true);
02eb380e 16342
0626fc76
TT
16343 /* If this type has an underlying type that is not a stub, then we
16344 may use its attributes. We always use the "unsigned" attribute
16345 in this situation, because ordinarily we guess whether the type
16346 is unsigned -- but the guess can be wrong and the underlying type
16347 can tell us the reality. However, we defer to a local size
16348 attribute if one exists, because this lets the compiler override
16349 the underlying type if needed. */
e46d3488 16350 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16351 {
9e7c9a03
HD
16352 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16353 underlying_type = check_typedef (underlying_type);
653223d3
SM
16354
16355 type->set_is_unsigned (underlying_type->is_unsigned ());
16356
0626fc76 16357 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16358 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16359
2b4424c3 16360 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16361 && TYPE_RAW_ALIGN (underlying_type) != 0)
16362 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16363 }
16364
aa70e35c 16365 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
3d567982 16366
ed6acedd
TT
16367 set_die_type (die, type, cu);
16368
16369 /* Finish the creation of this type by using the enum's children.
16370 Note that, as usual, this must come after set_die_type to avoid
16371 infinite recursion when trying to compute the names of the
16372 enumerators. */
16373 update_enumeration_type_from_children (die, type, cu);
16374
16375 return type;
134d01f1
DJ
16376}
16377
16378/* Given a pointer to a die which begins an enumeration, process all
16379 the dies that define the members of the enumeration, and create the
16380 symbol for the enumeration type.
16381
16382 NOTE: We reverse the order of the element list. */
16383
16384static void
16385process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16386{
f792889a 16387 struct type *this_type;
134d01f1 16388
f792889a
DJ
16389 this_type = get_die_type (die, cu);
16390 if (this_type == NULL)
16391 this_type = read_enumeration_type (die, cu);
9dc481d3 16392
639d11d3 16393 if (die->child != NULL)
c906108c 16394 {
9dc481d3 16395 struct die_info *child_die;
15d034d0 16396 const char *name;
9dc481d3 16397
639d11d3 16398 child_die = die->child;
c906108c
SS
16399 while (child_die && child_die->tag)
16400 {
16401 if (child_die->tag != DW_TAG_enumerator)
16402 {
e7c27a73 16403 process_die (child_die, cu);
c906108c
SS
16404 }
16405 else
16406 {
39cbfefa
DJ
16407 name = dwarf2_name (child_die, cu);
16408 if (name)
ed6acedd 16409 new_symbol (child_die, this_type, cu);
c906108c
SS
16410 }
16411
436c571c 16412 child_die = child_die->sibling;
c906108c 16413 }
c906108c 16414 }
134d01f1 16415
6c83ed52
TT
16416 /* If we are reading an enum from a .debug_types unit, and the enum
16417 is a declaration, and the enum is not the signatured type in the
16418 unit, then we do not want to add a symbol for it. Adding a
16419 symbol would in some cases obscure the true definition of the
16420 enum, giving users an incomplete type when the definition is
16421 actually available. Note that we do not want to do this for all
16422 enums which are just declarations, because C++0x allows forward
16423 enum declarations. */
3019eac3 16424 if (cu->per_cu->is_debug_types
6c83ed52
TT
16425 && die_is_declaration (die, cu))
16426 {
52dc124a 16427 struct signatured_type *sig_type;
6c83ed52 16428
c0f78cd4 16429 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16430 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16431 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16432 return;
16433 }
16434
f792889a 16435 new_symbol (die, this_type, cu);
c906108c
SS
16436}
16437
57567375
TT
16438/* Helper function for quirk_ada_thick_pointer that examines a bounds
16439 expression for an index type and finds the corresponding field
16440 offset in the hidden "P_BOUNDS" structure. Returns true on success
16441 and updates *FIELD, false if it fails to recognize an
16442 expression. */
16443
16444static bool
16445recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16446 int *bounds_offset, struct field *field,
16447 struct dwarf2_cu *cu)
16448{
16449 struct attribute *attr = dwarf2_attr (die, name, cu);
16450 if (attr == nullptr || !attr->form_is_block ())
16451 return false;
16452
16453 const struct dwarf_block *block = attr->as_block ();
16454 const gdb_byte *start = block->data;
16455 const gdb_byte *end = block->data + block->size;
16456
16457 /* The expression to recognize generally looks like:
16458
16459 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16460 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16461
16462 However, the second "plus_uconst" may be missing:
16463
16464 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16465 DW_OP_deref_size: 4)
16466
16467 This happens when the field is at the start of the structure.
16468
16469 Also, the final deref may not be sized:
16470
16471 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16472 DW_OP_deref)
16473
16474 This happens when the size of the index type happens to be the
16475 same as the architecture's word size. This can occur with or
16476 without the second plus_uconst. */
16477
16478 if (end - start < 2)
16479 return false;
16480 if (*start++ != DW_OP_push_object_address)
16481 return false;
16482 if (*start++ != DW_OP_plus_uconst)
16483 return false;
16484
16485 uint64_t this_bound_off;
16486 start = gdb_read_uleb128 (start, end, &this_bound_off);
16487 if (start == nullptr || (int) this_bound_off != this_bound_off)
16488 return false;
16489 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16490 is consistent among all bounds. */
16491 if (*bounds_offset == -1)
16492 *bounds_offset = this_bound_off;
16493 else if (*bounds_offset != this_bound_off)
16494 return false;
16495
16496 if (start == end || *start++ != DW_OP_deref)
16497 return false;
16498
16499 int offset = 0;
16500 if (start ==end)
16501 return false;
16502 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16503 {
16504 /* This means an offset of 0. */
16505 }
16506 else if (*start++ != DW_OP_plus_uconst)
16507 return false;
16508 else
16509 {
16510 /* The size is the parameter to DW_OP_plus_uconst. */
16511 uint64_t val;
16512 start = gdb_read_uleb128 (start, end, &val);
16513 if (start == nullptr)
16514 return false;
16515 if ((int) val != val)
16516 return false;
16517 offset = val;
16518 }
16519
16520 if (start == end)
16521 return false;
16522
16523 uint64_t size;
16524 if (*start == DW_OP_deref_size)
16525 {
16526 start = gdb_read_uleb128 (start + 1, end, &size);
16527 if (start == nullptr)
16528 return false;
16529 }
16530 else if (*start == DW_OP_deref)
16531 {
16532 size = cu->header.addr_size;
16533 ++start;
16534 }
16535 else
16536 return false;
16537
16538 SET_FIELD_BITPOS (*field, 8 * offset);
16539 if (size != TYPE_LENGTH (field->type ()))
16540 FIELD_BITSIZE (*field) = 8 * size;
16541
16542 return true;
16543}
16544
16545/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16546 some kinds of Ada arrays:
16547
16548 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16549 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16550 <11e0> DW_AT_data_location: 2 byte block: 97 6
16551 (DW_OP_push_object_address; DW_OP_deref)
16552 <11e3> DW_AT_type : <0x1173>
16553 <11e7> DW_AT_sibling : <0x1201>
16554 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16555 <11ec> DW_AT_type : <0x1206>
16556 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16557 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16558 DW_OP_deref_size: 4)
16559 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16560 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16561 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16562
16563 This actually represents a "thick pointer", which is a structure
16564 with two elements: one that is a pointer to the array data, and one
16565 that is a pointer to another structure; this second structure holds
16566 the array bounds.
16567
16568 This returns a new type on success, or nullptr if this didn't
16569 recognize the type. */
16570
16571static struct type *
16572quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16573 struct type *type)
16574{
16575 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16576 /* So far we've only seen this with block form. */
16577 if (attr == nullptr || !attr->form_is_block ())
16578 return nullptr;
16579
16580 /* Note that this will fail if the structure layout is changed by
16581 the compiler. However, we have no good way to recognize some
16582 other layout, because we don't know what expression the compiler
16583 might choose to emit should this happen. */
16584 struct dwarf_block *blk = attr->as_block ();
16585 if (blk->size != 2
16586 || blk->data[0] != DW_OP_push_object_address
16587 || blk->data[1] != DW_OP_deref)
16588 return nullptr;
16589
16590 int bounds_offset = -1;
16591 int max_align = -1;
16592 std::vector<struct field> range_fields;
16593 for (struct die_info *child_die = die->child;
16594 child_die;
16595 child_die = child_die->sibling)
16596 {
16597 if (child_die->tag == DW_TAG_subrange_type)
16598 {
16599 struct type *underlying = read_subrange_index_type (child_die, cu);
16600
16601 int this_align = type_align (underlying);
16602 if (this_align > max_align)
16603 max_align = this_align;
16604
16605 range_fields.emplace_back ();
16606 range_fields.emplace_back ();
16607
16608 struct field &lower = range_fields[range_fields.size () - 2];
16609 struct field &upper = range_fields[range_fields.size () - 1];
16610
16611 lower.set_type (underlying);
16612 FIELD_ARTIFICIAL (lower) = 1;
16613
16614 upper.set_type (underlying);
16615 FIELD_ARTIFICIAL (upper) = 1;
16616
16617 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16618 &bounds_offset, &lower, cu)
16619 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16620 &bounds_offset, &upper, cu))
16621 return nullptr;
16622 }
16623 }
16624
16625 /* This shouldn't really happen, but double-check that we found
16626 where the bounds are stored. */
16627 if (bounds_offset == -1)
16628 return nullptr;
16629
16630 struct objfile *objfile = cu->per_objfile->objfile;
16631 for (int i = 0; i < range_fields.size (); i += 2)
16632 {
16633 char name[20];
16634
16635 /* Set the name of each field in the bounds. */
16636 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16637 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16638 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16639 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16640 }
16641
16642 struct type *bounds = alloc_type (objfile);
16643 bounds->set_code (TYPE_CODE_STRUCT);
16644
16645 bounds->set_num_fields (range_fields.size ());
16646 bounds->set_fields
16647 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16648 * sizeof (struct field))));
16649 memcpy (bounds->fields (), range_fields.data (),
16650 bounds->num_fields () * sizeof (struct field));
16651
16652 int last_fieldno = range_fields.size () - 1;
16653 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16654 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16655 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16656
16657 /* Rewrite the existing array type in place. Specifically, we
16658 remove any dynamic properties we might have read, and we replace
16659 the index types. */
16660 struct type *iter = type;
16661 for (int i = 0; i < range_fields.size (); i += 2)
16662 {
16663 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16664 iter->main_type->dyn_prop_list = nullptr;
16665 iter->set_index_type
16666 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16667 iter = TYPE_TARGET_TYPE (iter);
16668 }
16669
16670 struct type *result = alloc_type (objfile);
16671 result->set_code (TYPE_CODE_STRUCT);
16672
16673 result->set_num_fields (2);
16674 result->set_fields
16675 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16676 * sizeof (struct field))));
16677
16678 /* The names are chosen to coincide with what the compiler does with
16679 -fgnat-encodings=all, which the Ada code in gdb already
16680 understands. */
16681 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16682 result->field (0).set_type (lookup_pointer_type (type));
16683
16684 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16685 result->field (1).set_type (lookup_pointer_type (bounds));
16686 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16687
16688 result->set_name (type->name ());
16689 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16690 + TYPE_LENGTH (result->field (1).type ()));
16691
16692 return result;
16693}
16694
c906108c
SS
16695/* Extract all information from a DW_TAG_array_type DIE and put it in
16696 the DIE's type field. For now, this only handles one dimensional
16697 arrays. */
16698
f792889a 16699static struct type *
e7c27a73 16700read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16701{
5e22e966 16702 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16703 struct die_info *child_die;
7e314c57 16704 struct type *type;
c906108c 16705 struct type *element_type, *range_type, *index_type;
c906108c 16706 struct attribute *attr;
15d034d0 16707 const char *name;
a405673c 16708 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16709 unsigned int bit_stride = 0;
c906108c 16710
e7c27a73 16711 element_type = die_type (die, cu);
c906108c 16712
7e314c57
JK
16713 /* The die_type call above may have already set the type for this DIE. */
16714 type = get_die_type (die, cu);
16715 if (type)
16716 return type;
16717
dc53a7ad
JB
16718 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16719 if (attr != NULL)
a405673c
JB
16720 {
16721 int stride_ok;
293e7e51 16722 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16723
16724 byte_stride_prop
16725 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16726 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16727 prop_type);
a405673c
JB
16728 if (!stride_ok)
16729 {
b98664d3 16730 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16731 " - DIE at %s [in module %s]"),
16732 sect_offset_str (die->sect_off),
5e22e966 16733 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16734 /* Ignore this attribute. We will likely not be able to print
16735 arrays of this type correctly, but there is little we can do
16736 to help if we cannot read the attribute's value. */
16737 byte_stride_prop = NULL;
16738 }
16739 }
dc53a7ad
JB
16740
16741 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16742 if (attr != NULL)
529908cb 16743 bit_stride = attr->constant_value (0);
dc53a7ad 16744
c906108c
SS
16745 /* Irix 6.2 native cc creates array types without children for
16746 arrays with unspecified length. */
639d11d3 16747 if (die->child == NULL)
c906108c 16748 {
46bf5051 16749 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16750 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16751 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16752 byte_stride_prop, bit_stride);
f792889a 16753 return set_die_type (die, type, cu);
c906108c
SS
16754 }
16755
791afaa2 16756 std::vector<struct type *> range_types;
639d11d3 16757 child_die = die->child;
c906108c
SS
16758 while (child_die && child_die->tag)
16759 {
16760 if (child_die->tag == DW_TAG_subrange_type)
16761 {
f792889a 16762 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16763
dda83cd7
SM
16764 if (child_type != NULL)
16765 {
0963b4bd 16766 /* The range type was succesfully read. Save it for the
dda83cd7 16767 array type creation. */
791afaa2 16768 range_types.push_back (child_type);
dda83cd7 16769 }
c906108c 16770 }
436c571c 16771 child_die = child_die->sibling;
c906108c
SS
16772 }
16773
cf2b2075
TV
16774 if (range_types.empty ())
16775 {
16776 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16777 sect_offset_str (die->sect_off),
16778 objfile_name (cu->per_objfile->objfile));
16779 return NULL;
16780 }
16781
c906108c
SS
16782 /* Dwarf2 dimensions are output from left to right, create the
16783 necessary array types in backwards order. */
7ca2d3a3 16784
c906108c 16785 type = element_type;
7ca2d3a3
DL
16786
16787 if (read_array_order (die, cu) == DW_ORD_col_major)
16788 {
16789 int i = 0;
9a619af0 16790
791afaa2 16791 while (i < range_types.size ())
10f6a3ad
TT
16792 {
16793 type = create_array_type_with_stride (NULL, type, range_types[i++],
16794 byte_stride_prop, bit_stride);
16795 bit_stride = 0;
16796 byte_stride_prop = nullptr;
16797 }
7ca2d3a3
DL
16798 }
16799 else
16800 {
791afaa2 16801 size_t ndim = range_types.size ();
7ca2d3a3 16802 while (ndim-- > 0)
10f6a3ad
TT
16803 {
16804 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16805 byte_stride_prop, bit_stride);
16806 bit_stride = 0;
16807 byte_stride_prop = nullptr;
16808 }
7ca2d3a3 16809 }
c906108c 16810
cf2b2075
TV
16811 gdb_assert (type != element_type);
16812
f5f8a009
EZ
16813 /* Understand Dwarf2 support for vector types (like they occur on
16814 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16815 array type. This is not part of the Dwarf2/3 standard yet, but a
16816 custom vendor extension. The main difference between a regular
16817 array and the vector variant is that vectors are passed by value
16818 to functions. */
e142c38c 16819 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16820 if (attr != nullptr)
ea37ba09 16821 make_vector_type (type);
f5f8a009 16822
dbc98a8b
KW
16823 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16824 implementation may choose to implement triple vectors using this
16825 attribute. */
16826 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 16827 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 16828 {
529908cb
TT
16829 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16830 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 16831 else
b98664d3 16832 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16833 "than the total size of elements"));
dbc98a8b
KW
16834 }
16835
39cbfefa
DJ
16836 name = dwarf2_name (die, cu);
16837 if (name)
d0e39ea2 16838 type->set_name (name);
6e70227d 16839
2b4424c3
TT
16840 maybe_set_alignment (cu, die, type);
16841
57567375
TT
16842 struct type *replacement_type = nullptr;
16843 if (cu->language == language_ada)
16844 {
16845 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16846 if (replacement_type != nullptr)
16847 type = replacement_type;
16848 }
16849
0963b4bd 16850 /* Install the type in the die. */
57567375 16851 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
16852
16853 /* set_die_type should be already done. */
b4ba55a1
JB
16854 set_descriptive_type (type, die, cu);
16855
7e314c57 16856 return type;
c906108c
SS
16857}
16858
7ca2d3a3 16859static enum dwarf_array_dim_ordering
6e70227d 16860read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16861{
16862 struct attribute *attr;
16863
16864 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16865
435d3d88 16866 if (attr != nullptr)
1bc397c5
TT
16867 {
16868 LONGEST val = attr->constant_value (-1);
16869 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16870 return (enum dwarf_array_dim_ordering) val;
16871 }
7ca2d3a3 16872
0963b4bd
MS
16873 /* GNU F77 is a special case, as at 08/2004 array type info is the
16874 opposite order to the dwarf2 specification, but data is still
16875 laid out as per normal fortran.
7ca2d3a3 16876
0963b4bd
MS
16877 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16878 version checking. */
7ca2d3a3 16879
905e0470
PM
16880 if (cu->language == language_fortran
16881 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16882 {
16883 return DW_ORD_row_major;
16884 }
16885
3a3440fb 16886 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
16887 {
16888 case array_column_major:
16889 return DW_ORD_col_major;
16890 case array_row_major:
16891 default:
16892 return DW_ORD_row_major;
16893 };
16894}
16895
72019c9c 16896/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16897 the DIE's type field. */
72019c9c 16898
f792889a 16899static struct type *
72019c9c
GM
16900read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16901{
7e314c57
JK
16902 struct type *domain_type, *set_type;
16903 struct attribute *attr;
f792889a 16904
7e314c57
JK
16905 domain_type = die_type (die, cu);
16906
16907 /* The die_type call above may have already set the type for this DIE. */
16908 set_type = get_die_type (die, cu);
16909 if (set_type)
16910 return set_type;
16911
16912 set_type = create_set_type (NULL, domain_type);
16913
16914 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
16915 if (attr != nullptr && attr->form_is_unsigned ())
16916 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 16917
2b4424c3
TT
16918 maybe_set_alignment (cu, die, set_type);
16919
f792889a 16920 return set_die_type (die, set_type, cu);
72019c9c 16921}
7ca2d3a3 16922
0971de02
TT
16923/* A helper for read_common_block that creates a locexpr baton.
16924 SYM is the symbol which we are marking as computed.
16925 COMMON_DIE is the DIE for the common block.
16926 COMMON_LOC is the location expression attribute for the common
16927 block itself.
16928 MEMBER_LOC is the location expression attribute for the particular
16929 member of the common block that we are processing.
16930 CU is the CU from which the above come. */
16931
16932static void
16933mark_common_block_symbol_computed (struct symbol *sym,
16934 struct die_info *common_die,
16935 struct attribute *common_loc,
16936 struct attribute *member_loc,
16937 struct dwarf2_cu *cu)
16938{
5e22e966 16939 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16940 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16941 struct dwarf2_locexpr_baton *baton;
16942 gdb_byte *ptr;
16943 unsigned int cu_off;
08feed99 16944 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16945 LONGEST offset = 0;
16946
16947 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16948 gdb_assert (common_loc->form_is_block ());
16949 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16950 || member_loc->form_is_constant ());
0971de02 16951
8d749320 16952 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16953 baton->per_objfile = per_objfile;
0971de02
TT
16954 baton->per_cu = cu->per_cu;
16955 gdb_assert (baton->per_cu);
16956
16957 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16958
cd6c91b4 16959 if (member_loc->form_is_constant ())
0971de02 16960 {
0826b30a 16961 offset = member_loc->constant_value (0);
0971de02
TT
16962 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16963 }
16964 else
9d2246fc 16965 baton->size += member_loc->as_block ()->size;
0971de02 16966
224c3ddb 16967 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16968 baton->data = ptr;
16969
16970 *ptr++ = DW_OP_call4;
9c541725 16971 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16972 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16973 ptr += 4;
16974
cd6c91b4 16975 if (member_loc->form_is_constant ())
0971de02
TT
16976 {
16977 *ptr++ = DW_OP_addr;
16978 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16979 ptr += cu->header.addr_size;
16980 }
16981 else
16982 {
16983 /* We have to copy the data here, because DW_OP_call4 will only
16984 use a DW_AT_location attribute. */
9d2246fc
TT
16985 struct dwarf_block *block = member_loc->as_block ();
16986 memcpy (ptr, block->data, block->size);
16987 ptr += block->size;
0971de02
TT
16988 }
16989
16990 *ptr++ = DW_OP_plus;
16991 gdb_assert (ptr - baton->data == baton->size);
16992
0971de02 16993 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16994 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16995}
16996
4357ac6c
TT
16997/* Create appropriate locally-scoped variables for all the
16998 DW_TAG_common_block entries. Also create a struct common_block
16999 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 17000 is used to separate the common blocks name namespace from regular
4357ac6c 17001 variable names. */
c906108c
SS
17002
17003static void
e7c27a73 17004read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17005{
0971de02
TT
17006 struct attribute *attr;
17007
17008 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17009 if (attr != nullptr)
0971de02
TT
17010 {
17011 /* Support the .debug_loc offsets. */
4fc6c0d5 17012 if (attr->form_is_block ())
dda83cd7 17013 {
0971de02 17014 /* Ok. */
dda83cd7 17015 }
cd6c91b4 17016 else if (attr->form_is_section_offset ())
dda83cd7 17017 {
0971de02
TT
17018 dwarf2_complex_location_expr_complaint ();
17019 attr = NULL;
dda83cd7 17020 }
0971de02 17021 else
dda83cd7 17022 {
0971de02
TT
17023 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17024 "common block member");
17025 attr = NULL;
dda83cd7 17026 }
0971de02
TT
17027 }
17028
639d11d3 17029 if (die->child != NULL)
c906108c 17030 {
5e22e966 17031 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17032 struct die_info *child_die;
17033 size_t n_entries = 0, size;
17034 struct common_block *common_block;
17035 struct symbol *sym;
74ac6d43 17036
4357ac6c
TT
17037 for (child_die = die->child;
17038 child_die && child_die->tag;
436c571c 17039 child_die = child_die->sibling)
4357ac6c
TT
17040 ++n_entries;
17041
17042 size = (sizeof (struct common_block)
17043 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17044 common_block
17045 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17046 size);
4357ac6c
TT
17047 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17048 common_block->n_entries = 0;
17049
17050 for (child_die = die->child;
17051 child_die && child_die->tag;
436c571c 17052 child_die = child_die->sibling)
4357ac6c
TT
17053 {
17054 /* Create the symbol in the DW_TAG_common_block block in the current
17055 symbol scope. */
e7c27a73 17056 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17057 if (sym != NULL)
17058 {
17059 struct attribute *member_loc;
17060
17061 common_block->contents[common_block->n_entries++] = sym;
17062
17063 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17064 cu);
17065 if (member_loc)
17066 {
17067 /* GDB has handled this for a long time, but it is
17068 not specified by DWARF. It seems to have been
17069 emitted by gfortran at least as recently as:
17070 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17071 complaint (_("Variable in common block has "
0971de02 17072 "DW_AT_data_member_location "
9d8780f0
SM
17073 "- DIE at %s [in module %s]"),
17074 sect_offset_str (child_die->sect_off),
518817b3 17075 objfile_name (objfile));
0971de02 17076
cd6c91b4 17077 if (member_loc->form_is_section_offset ())
0971de02 17078 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17079 else if (member_loc->form_is_constant ()
4fc6c0d5 17080 || member_loc->form_is_block ())
0971de02 17081 {
435d3d88 17082 if (attr != nullptr)
0971de02
TT
17083 mark_common_block_symbol_computed (sym, die, attr,
17084 member_loc, cu);
17085 }
17086 else
17087 dwarf2_complex_location_expr_complaint ();
17088 }
17089 }
c906108c 17090 }
4357ac6c
TT
17091
17092 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17093 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17094 }
17095}
17096
0114d602 17097/* Create a type for a C++ namespace. */
d9fa45fe 17098
0114d602
DJ
17099static struct type *
17100read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17101{
5e22e966 17102 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17103 const char *previous_prefix, *name;
9219021c 17104 int is_anonymous;
0114d602
DJ
17105 struct type *type;
17106
17107 /* For extensions, reuse the type of the original namespace. */
17108 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17109 {
17110 struct die_info *ext_die;
17111 struct dwarf2_cu *ext_cu = cu;
9a619af0 17112
0114d602
DJ
17113 ext_die = dwarf2_extension (die, &ext_cu);
17114 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17115
17116 /* EXT_CU may not be the same as CU.
02142a6c 17117 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17118 return set_die_type (die, type, cu);
17119 }
9219021c 17120
e142c38c 17121 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17122
17123 /* Now build the name of the current namespace. */
17124
0114d602
DJ
17125 previous_prefix = determine_prefix (die, cu);
17126 if (previous_prefix[0] != '\0')
17127 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17128 previous_prefix, name, 0, cu);
0114d602
DJ
17129
17130 /* Create the type. */
19f392bc 17131 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17132
60531b24 17133 return set_die_type (die, type, cu);
0114d602
DJ
17134}
17135
22cee43f 17136/* Read a namespace scope. */
0114d602
DJ
17137
17138static void
17139read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17140{
5e22e966 17141 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17142 int is_anonymous;
9219021c 17143
5c4e30ca
DC
17144 /* Add a symbol associated to this if we haven't seen the namespace
17145 before. Also, add a using directive if it's an anonymous
17146 namespace. */
9219021c 17147
f2f0e013 17148 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17149 {
17150 struct type *type;
17151
0114d602 17152 type = read_type_die (die, cu);
e7c27a73 17153 new_symbol (die, type, cu);
5c4e30ca 17154
e8e80198 17155 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17156 if (is_anonymous)
0114d602
DJ
17157 {
17158 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17159
eb1e02fd 17160 std::vector<const char *> excludes;
804d2729 17161 add_using_directive (using_directives (cu),
7d93a1e0 17162 previous_prefix, type->name (), NULL,
eb1e02fd 17163 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17164 }
5c4e30ca 17165 }
9219021c 17166
639d11d3 17167 if (die->child != NULL)
d9fa45fe 17168 {
639d11d3 17169 struct die_info *child_die = die->child;
6e70227d 17170
d9fa45fe
DC
17171 while (child_die && child_die->tag)
17172 {
e7c27a73 17173 process_die (child_die, cu);
436c571c 17174 child_die = child_die->sibling;
d9fa45fe
DC
17175 }
17176 }
38d518c9
EZ
17177}
17178
f55ee35c
JK
17179/* Read a Fortran module as type. This DIE can be only a declaration used for
17180 imported module. Still we need that type as local Fortran "use ... only"
17181 declaration imports depend on the created type in determine_prefix. */
17182
17183static struct type *
17184read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17185{
5e22e966 17186 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17187 const char *module_name;
f55ee35c
JK
17188 struct type *type;
17189
17190 module_name = dwarf2_name (die, cu);
19f392bc 17191 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17192
f55ee35c
JK
17193 return set_die_type (die, type, cu);
17194}
17195
5d7cb8df
JK
17196/* Read a Fortran module. */
17197
17198static void
17199read_module (struct die_info *die, struct dwarf2_cu *cu)
17200{
17201 struct die_info *child_die = die->child;
530e8392
KB
17202 struct type *type;
17203
17204 type = read_type_die (die, cu);
17205 new_symbol (die, type, cu);
5d7cb8df 17206
5d7cb8df
JK
17207 while (child_die && child_die->tag)
17208 {
17209 process_die (child_die, cu);
436c571c 17210 child_die = child_die->sibling;
5d7cb8df
JK
17211 }
17212}
17213
38d518c9
EZ
17214/* Return the name of the namespace represented by DIE. Set
17215 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17216 namespace. */
17217
17218static const char *
e142c38c 17219namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17220{
17221 struct die_info *current_die;
17222 const char *name = NULL;
17223
17224 /* Loop through the extensions until we find a name. */
17225
17226 for (current_die = die;
17227 current_die != NULL;
f2f0e013 17228 current_die = dwarf2_extension (die, &cu))
38d518c9 17229 {
96553a0c
DE
17230 /* We don't use dwarf2_name here so that we can detect the absence
17231 of a name -> anonymous namespace. */
7d45c7c3 17232 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17233
38d518c9
EZ
17234 if (name != NULL)
17235 break;
17236 }
17237
17238 /* Is it an anonymous namespace? */
17239
17240 *is_anonymous = (name == NULL);
17241 if (*is_anonymous)
2b1dbab0 17242 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17243
17244 return name;
d9fa45fe
DC
17245}
17246
c906108c
SS
17247/* Extract all information from a DW_TAG_pointer_type DIE and add to
17248 the user defined type vector. */
17249
f792889a 17250static struct type *
e7c27a73 17251read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17252{
5e22e966 17253 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17254 struct comp_unit_head *cu_header = &cu->header;
c906108c 17255 struct type *type;
8b2dbe47
KB
17256 struct attribute *attr_byte_size;
17257 struct attribute *attr_address_class;
17258 int byte_size, addr_class;
7e314c57
JK
17259 struct type *target_type;
17260
17261 target_type = die_type (die, cu);
c906108c 17262
7e314c57
JK
17263 /* The die_type call above may have already set the type for this DIE. */
17264 type = get_die_type (die, cu);
17265 if (type)
17266 return type;
17267
17268 type = lookup_pointer_type (target_type);
8b2dbe47 17269
e142c38c 17270 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17271 if (attr_byte_size)
529908cb 17272 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17273 else
8b2dbe47
KB
17274 byte_size = cu_header->addr_size;
17275
e142c38c 17276 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17277 if (attr_address_class)
529908cb 17278 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17279 else
17280 addr_class = DW_ADDR_none;
17281
2b4424c3
TT
17282 ULONGEST alignment = get_alignment (cu, die);
17283
17284 /* If the pointer size, alignment, or address class is different
17285 than the default, create a type variant marked as such and set
17286 the length accordingly. */
17287 if (TYPE_LENGTH (type) != byte_size
17288 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17289 && alignment != TYPE_RAW_ALIGN (type))
17290 || addr_class != DW_ADDR_none)
c906108c 17291 {
5e2b427d 17292 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17293 {
314ad88d
PA
17294 type_instance_flags type_flags
17295 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17296 addr_class);
876cecd0
TT
17297 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17298 == 0);
8b2dbe47
KB
17299 type = make_type_with_address_space (type, type_flags);
17300 }
17301 else if (TYPE_LENGTH (type) != byte_size)
17302 {
b98664d3 17303 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17304 }
2b4424c3
TT
17305 else if (TYPE_RAW_ALIGN (type) != alignment)
17306 {
b98664d3 17307 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17308 " - DIE at %s [in module %s]"),
17309 sect_offset_str (die->sect_off),
5e22e966 17310 objfile_name (cu->per_objfile->objfile));
2b4424c3 17311 }
6e70227d 17312 else
9a619af0
MS
17313 {
17314 /* Should we also complain about unhandled address classes? */
17315 }
c906108c 17316 }
8b2dbe47
KB
17317
17318 TYPE_LENGTH (type) = byte_size;
2b4424c3 17319 set_type_align (type, alignment);
f792889a 17320 return set_die_type (die, type, cu);
c906108c
SS
17321}
17322
17323/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17324 the user defined type vector. */
17325
f792889a 17326static struct type *
e7c27a73 17327read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17328{
17329 struct type *type;
17330 struct type *to_type;
17331 struct type *domain;
17332
e7c27a73
DJ
17333 to_type = die_type (die, cu);
17334 domain = die_containing_type (die, cu);
0d5de010 17335
7e314c57
JK
17336 /* The calls above may have already set the type for this DIE. */
17337 type = get_die_type (die, cu);
17338 if (type)
17339 return type;
17340
78134374 17341 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17342 type = lookup_methodptr_type (to_type);
78134374 17343 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17344 {
5e22e966 17345 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17346
17347 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17348 to_type->fields (), to_type->num_fields (),
a409645d 17349 to_type->has_varargs ());
7078baeb
TT
17350 type = lookup_methodptr_type (new_type);
17351 }
0d5de010
DJ
17352 else
17353 type = lookup_memberptr_type (to_type, domain);
c906108c 17354
f792889a 17355 return set_die_type (die, type, cu);
c906108c
SS
17356}
17357
4297a3f0 17358/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17359 the user defined type vector. */
17360
f792889a 17361static struct type *
4297a3f0 17362read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17363 enum type_code refcode)
c906108c 17364{
e7c27a73 17365 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17366 struct type *type, *target_type;
c906108c
SS
17367 struct attribute *attr;
17368
4297a3f0
AV
17369 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17370
7e314c57
JK
17371 target_type = die_type (die, cu);
17372
17373 /* The die_type call above may have already set the type for this DIE. */
17374 type = get_die_type (die, cu);
17375 if (type)
17376 return type;
17377
4297a3f0 17378 type = lookup_reference_type (target_type, refcode);
e142c38c 17379 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17380 if (attr != nullptr)
c906108c 17381 {
529908cb 17382 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17383 }
17384 else
17385 {
107d2387 17386 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17387 }
2b4424c3 17388 maybe_set_alignment (cu, die, type);
f792889a 17389 return set_die_type (die, type, cu);
c906108c
SS
17390}
17391
cf363f18
MW
17392/* Add the given cv-qualifiers to the element type of the array. GCC
17393 outputs DWARF type qualifiers that apply to an array, not the
17394 element type. But GDB relies on the array element type to carry
17395 the cv-qualifiers. This mimics section 6.7.3 of the C99
17396 specification. */
17397
17398static struct type *
17399add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17400 struct type *base_type, int cnst, int voltl)
17401{
17402 struct type *el_type, *inner_array;
17403
17404 base_type = copy_type (base_type);
17405 inner_array = base_type;
17406
78134374 17407 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17408 {
17409 TYPE_TARGET_TYPE (inner_array) =
17410 copy_type (TYPE_TARGET_TYPE (inner_array));
17411 inner_array = TYPE_TARGET_TYPE (inner_array);
17412 }
17413
17414 el_type = TYPE_TARGET_TYPE (inner_array);
17415 cnst |= TYPE_CONST (el_type);
17416 voltl |= TYPE_VOLATILE (el_type);
17417 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17418
17419 return set_die_type (die, base_type, cu);
17420}
17421
f792889a 17422static struct type *
e7c27a73 17423read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17424{
f792889a 17425 struct type *base_type, *cv_type;
c906108c 17426
e7c27a73 17427 base_type = die_type (die, cu);
7e314c57
JK
17428
17429 /* The die_type call above may have already set the type for this DIE. */
17430 cv_type = get_die_type (die, cu);
17431 if (cv_type)
17432 return cv_type;
17433
2f608a3a
KW
17434 /* In case the const qualifier is applied to an array type, the element type
17435 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17436 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17437 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17438
f792889a
DJ
17439 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17440 return set_die_type (die, cv_type, cu);
c906108c
SS
17441}
17442
f792889a 17443static struct type *
e7c27a73 17444read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17445{
f792889a 17446 struct type *base_type, *cv_type;
c906108c 17447
e7c27a73 17448 base_type = die_type (die, cu);
7e314c57
JK
17449
17450 /* The die_type call above may have already set the type for this DIE. */
17451 cv_type = get_die_type (die, cu);
17452 if (cv_type)
17453 return cv_type;
17454
cf363f18
MW
17455 /* In case the volatile qualifier is applied to an array type, the
17456 element type is so qualified, not the array type (section 6.7.3
17457 of C99). */
78134374 17458 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17459 return add_array_cv_type (die, cu, base_type, 0, 1);
17460
f792889a
DJ
17461 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17462 return set_die_type (die, cv_type, cu);
c906108c
SS
17463}
17464
06d66ee9
TT
17465/* Handle DW_TAG_restrict_type. */
17466
17467static struct type *
17468read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17469{
17470 struct type *base_type, *cv_type;
17471
17472 base_type = die_type (die, cu);
17473
17474 /* The die_type call above may have already set the type for this DIE. */
17475 cv_type = get_die_type (die, cu);
17476 if (cv_type)
17477 return cv_type;
17478
17479 cv_type = make_restrict_type (base_type);
17480 return set_die_type (die, cv_type, cu);
17481}
17482
a2c2acaf
MW
17483/* Handle DW_TAG_atomic_type. */
17484
17485static struct type *
17486read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17487{
17488 struct type *base_type, *cv_type;
17489
17490 base_type = die_type (die, cu);
17491
17492 /* The die_type call above may have already set the type for this DIE. */
17493 cv_type = get_die_type (die, cu);
17494 if (cv_type)
17495 return cv_type;
17496
17497 cv_type = make_atomic_type (base_type);
17498 return set_die_type (die, cv_type, cu);
17499}
17500
c906108c
SS
17501/* Extract all information from a DW_TAG_string_type DIE and add to
17502 the user defined type vector. It isn't really a user defined type,
17503 but it behaves like one, with other DIE's using an AT_user_def_type
17504 attribute to reference it. */
17505
f792889a 17506static struct type *
e7c27a73 17507read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17508{
5e22e966 17509 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17510 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17511 struct type *type, *range_type, *index_type, *char_type;
17512 struct attribute *attr;
216a7e6b
AB
17513 struct dynamic_prop prop;
17514 bool length_is_constant = true;
17515 LONGEST length;
17516
17517 /* There are a couple of places where bit sizes might be made use of
17518 when parsing a DW_TAG_string_type, however, no producer that we know
17519 of make use of these. Handling bit sizes that are a multiple of the
17520 byte size is easy enough, but what about other bit sizes? Lets deal
17521 with that problem when we have to. Warn about these attributes being
17522 unsupported, then parse the type and ignore them like we always
17523 have. */
17524 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17525 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17526 {
17527 static bool warning_printed = false;
17528 if (!warning_printed)
17529 {
17530 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17531 "currently supported on DW_TAG_string_type."));
17532 warning_printed = true;
17533 }
17534 }
c906108c 17535
e142c38c 17536 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17537 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17538 {
17539 /* The string length describes the location at which the length of
17540 the string can be found. The size of the length field can be
17541 specified with one of the attributes below. */
17542 struct type *prop_type;
17543 struct attribute *len
17544 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17545 if (len == nullptr)
17546 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17547 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17548 {
17549 /* Pass 0 as the default as we know this attribute is constant
17550 and the default value will not be returned. */
0826b30a 17551 LONGEST sz = len->constant_value (0);
293e7e51 17552 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17553 }
17554 else
17555 {
17556 /* If the size is not specified then we assume it is the size of
17557 an address on this target. */
293e7e51 17558 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17559 }
17560
17561 /* Convert the attribute into a dynamic property. */
17562 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17563 length = 1;
17564 else
17565 length_is_constant = false;
17566 }
17567 else if (attr != nullptr)
17568 {
17569 /* This DW_AT_string_length just contains the length with no
17570 indirection. There's no need to create a dynamic property in this
17571 case. Pass 0 for the default value as we know it will not be
17572 returned in this case. */
0826b30a 17573 length = attr->constant_value (0);
216a7e6b
AB
17574 }
17575 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17576 {
216a7e6b 17577 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17578 length = attr->constant_value (1);
c906108c
SS
17579 }
17580 else
17581 {
216a7e6b
AB
17582 /* Use 1 as a fallback length if we have nothing else. */
17583 length = 1;
c906108c 17584 }
6ccb9162 17585
46bf5051 17586 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17587 if (length_is_constant)
17588 range_type = create_static_range_type (NULL, index_type, 1, length);
17589 else
17590 {
17591 struct dynamic_prop low_bound;
17592
8c2e4e06 17593 low_bound.set_const_val (1);
216a7e6b
AB
17594 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17595 }
3b7538c0
UW
17596 char_type = language_string_char_type (cu->language_defn, gdbarch);
17597 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17598
f792889a 17599 return set_die_type (die, type, cu);
c906108c
SS
17600}
17601
4d804846
JB
17602/* Assuming that DIE corresponds to a function, returns nonzero
17603 if the function is prototyped. */
17604
17605static int
17606prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17607{
17608 struct attribute *attr;
17609
17610 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17611 if (attr && attr->as_boolean ())
4d804846
JB
17612 return 1;
17613
17614 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17615 is only meaningful for C, but the concept also extends to other
4d804846
JB
17616 languages that allow unprototyped functions (Eg: Objective C).
17617 For all other languages, assume that functions are always
17618 prototyped. */
17619 if (cu->language != language_c
17620 && cu->language != language_objc
17621 && cu->language != language_opencl)
17622 return 1;
17623
17624 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17625 prototyped and unprototyped functions; default to prototyped,
17626 since that is more common in modern code (and RealView warns
17627 about unprototyped functions). */
17628 if (producer_is_realview (cu->producer))
17629 return 1;
17630
17631 return 0;
17632}
17633
c906108c
SS
17634/* Handle DIES due to C code like:
17635
17636 struct foo
c5aa993b
JM
17637 {
17638 int (*funcp)(int a, long l);
17639 int b;
17640 };
c906108c 17641
0963b4bd 17642 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17643
f792889a 17644static struct type *
e7c27a73 17645read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17646{
5e22e966 17647 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17648 struct type *type; /* Type that this function returns. */
17649 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17650 struct attribute *attr;
17651
e7c27a73 17652 type = die_type (die, cu);
7e314c57
JK
17653
17654 /* The die_type call above may have already set the type for this DIE. */
17655 ftype = get_die_type (die, cu);
17656 if (ftype)
17657 return ftype;
17658
0c8b41f1 17659 ftype = lookup_function_type (type);
c906108c 17660
4d804846 17661 if (prototyped_function_p (die, cu))
27e69b7a 17662 ftype->set_is_prototyped (true);
c906108c 17663
c055b101
CV
17664 /* Store the calling convention in the type if it's available in
17665 the subroutine die. Otherwise set the calling convention to
17666 the default value DW_CC_normal. */
17667 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 17668 if (attr != nullptr
529908cb 17669 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 17670 TYPE_CALLING_CONVENTION (ftype)
529908cb 17671 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
17672 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17673 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17674 else
17675 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17676
743649fd
MW
17677 /* Record whether the function returns normally to its caller or not
17678 if the DWARF producer set that information. */
17679 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 17680 if (attr && attr->as_boolean ())
743649fd
MW
17681 TYPE_NO_RETURN (ftype) = 1;
17682
76c10ea2
GM
17683 /* We need to add the subroutine type to the die immediately so
17684 we don't infinitely recurse when dealing with parameters
0963b4bd 17685 declared as the same subroutine type. */
76c10ea2 17686 set_die_type (die, ftype, cu);
6e70227d 17687
639d11d3 17688 if (die->child != NULL)
c906108c 17689 {
bb5ed363 17690 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17691 struct die_info *child_die;
8072405b 17692 int nparams, iparams;
c906108c
SS
17693
17694 /* Count the number of parameters.
dda83cd7
SM
17695 FIXME: GDB currently ignores vararg functions, but knows about
17696 vararg member functions. */
8072405b 17697 nparams = 0;
639d11d3 17698 child_die = die->child;
c906108c
SS
17699 while (child_die && child_die->tag)
17700 {
17701 if (child_die->tag == DW_TAG_formal_parameter)
17702 nparams++;
17703 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
17704 ftype->set_has_varargs (true);
17705
436c571c 17706 child_die = child_die->sibling;
c906108c
SS
17707 }
17708
17709 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17710 ftype->set_num_fields (nparams);
3cabb6b0
SM
17711 ftype->set_fields
17712 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17713
8072405b
JK
17714 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17715 even if we error out during the parameters reading below. */
17716 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17717 ftype->field (iparams).set_type (void_type);
8072405b
JK
17718
17719 iparams = 0;
639d11d3 17720 child_die = die->child;
c906108c
SS
17721 while (child_die && child_die->tag)
17722 {
17723 if (child_die->tag == DW_TAG_formal_parameter)
17724 {
3ce3b1ba
PA
17725 struct type *arg_type;
17726
17727 /* DWARF version 2 has no clean way to discern C++
17728 static and non-static member functions. G++ helps
17729 GDB by marking the first parameter for non-static
17730 member functions (which is the this pointer) as
17731 artificial. We pass this information to
17732 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17733
17734 DWARF version 3 added DW_AT_object_pointer, which GCC
17735 4.5 does not yet generate. */
e142c38c 17736 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17737 if (attr != nullptr)
c45bc3f8 17738 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 17739 else
9c37b5ae 17740 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17741 arg_type = die_type (child_die, cu);
17742
17743 /* RealView does not mark THIS as const, which the testsuite
17744 expects. GCC marks THIS as const in method definitions,
17745 but not in the class specifications (GCC PR 43053). */
17746 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17747 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17748 {
17749 int is_this = 0;
17750 struct dwarf2_cu *arg_cu = cu;
17751 const char *name = dwarf2_name (child_die, cu);
17752
17753 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17754 if (attr != nullptr)
3ce3b1ba
PA
17755 {
17756 /* If the compiler emits this, use it. */
17757 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17758 is_this = 1;
17759 }
17760 else if (name && strcmp (name, "this") == 0)
17761 /* Function definitions will have the argument names. */
17762 is_this = 1;
17763 else if (name == NULL && iparams == 0)
17764 /* Declarations may not have the names, so like
17765 elsewhere in GDB, assume an artificial first
17766 argument is "this". */
17767 is_this = 1;
17768
17769 if (is_this)
17770 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17771 arg_type, 0);
17772 }
17773
5d14b6e5 17774 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17775 iparams++;
17776 }
436c571c 17777 child_die = child_die->sibling;
c906108c
SS
17778 }
17779 }
17780
76c10ea2 17781 return ftype;
c906108c
SS
17782}
17783
f792889a 17784static struct type *
e7c27a73 17785read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17786{
5e22e966 17787 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17788 const char *name = NULL;
3c8e0968 17789 struct type *this_type, *target_type;
c906108c 17790
94af9270 17791 name = dwarf2_full_name (NULL, die, cu);
19f392bc 17792 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 17793 this_type->set_target_is_stub (true);
f792889a 17794 set_die_type (die, this_type, cu);
3c8e0968
DE
17795 target_type = die_type (die, cu);
17796 if (target_type != this_type)
17797 TYPE_TARGET_TYPE (this_type) = target_type;
17798 else
17799 {
17800 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17801 spec and cause infinite loops in GDB. */
b98664d3 17802 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17803 "- DIE at %s [in module %s]"),
17804 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17805 TYPE_TARGET_TYPE (this_type) = NULL;
17806 }
e4003a34
TV
17807 if (name == NULL)
17808 {
17809 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17810 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17811 Handle these by just returning the target type, rather than
17812 constructing an anonymous typedef type and trying to handle this
17813 elsewhere. */
17814 set_die_type (die, target_type, cu);
17815 return target_type;
17816 }
f792889a 17817 return this_type;
c906108c
SS
17818}
17819
a625a8c9
TT
17820/* Helper for get_dwarf2_rational_constant that computes the value of
17821 a given gmp_mpz given an attribute. */
17822
17823static void
17824get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17825{
17826 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17827 location expression that pushes an implicit value. */
17828 if (attr->form == DW_FORM_exprloc)
17829 {
17830 dwarf_block *blk = attr->as_block ();
17831 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17832 {
17833 uint64_t len;
17834 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17835 blk->data + blk->size,
17836 &len);
17837 if (ptr - blk->data + len <= blk->size)
17838 {
17839 mpz_import (value->val, len,
17840 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17841 1, 0, 0, ptr);
17842 return;
17843 }
17844 }
17845
17846 /* On failure set it to 1. */
17847 *value = gdb_mpz (1);
17848 }
17849 else if (attr->form_is_block ())
17850 {
17851 dwarf_block *blk = attr->as_block ();
17852 mpz_import (value->val, blk->size,
17853 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17854 1, 0, 0, blk->data);
17855 }
17856 else
17857 *value = gdb_mpz (attr->constant_value (1));
17858}
17859
09584414
JB
17860/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17861 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17862
17863 If the numerator and/or numerator attribute is missing,
17864 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17865 untouched. */
17866
17867static void
17868get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 17869 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
17870{
17871 struct attribute *num_attr, *denom_attr;
17872
17873 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17874 if (num_attr == nullptr)
17875 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17876 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17877
17878 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17879 if (denom_attr == nullptr)
17880 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17881 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17882
17883 if (num_attr == nullptr || denom_attr == nullptr)
17884 return;
17885
a625a8c9
TT
17886 get_mpz (cu, numerator, num_attr);
17887 get_mpz (cu, denominator, denom_attr);
09584414
JB
17888}
17889
17890/* Same as get_dwarf2_rational_constant, but extracting an unsigned
17891 rational constant, rather than a signed one.
17892
17893 If the rational constant has a negative value, a complaint
17894 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17895
17896static void
17897get_dwarf2_unsigned_rational_constant (struct die_info *die,
17898 struct dwarf2_cu *cu,
5cde1d82
TT
17899 gdb_mpz *numerator,
17900 gdb_mpz *denominator)
09584414 17901{
5cde1d82
TT
17902 gdb_mpz num (1);
17903 gdb_mpz denom (1);
09584414
JB
17904
17905 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 17906 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 17907 {
5cde1d82
TT
17908 mpz_neg (num.val, num.val);
17909 mpz_neg (denom.val, denom.val);
09584414 17910 }
5cde1d82 17911 else if (mpz_sgn (num.val) == -1)
09584414
JB
17912 {
17913 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17914 " in DIE at %s"),
17915 sect_offset_str (die->sect_off));
17916 return;
17917 }
5cde1d82 17918 else if (mpz_sgn (denom.val) == -1)
09584414
JB
17919 {
17920 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17921 " in DIE at %s"),
17922 sect_offset_str (die->sect_off));
17923 return;
17924 }
17925
5cde1d82
TT
17926 *numerator = std::move (num);
17927 *denominator = std::move (denom);
09584414
JB
17928}
17929
bbcdf9ab
TT
17930/* Assuming that ENCODING is a string whose contents starting at the
17931 K'th character is "_nn" where "nn" is a decimal number, scan that
17932 number and set RESULT to the value. K is updated to point to the
17933 character immediately following the number.
17934
17935 If the string does not conform to the format described above, false
17936 is returned, and K may or may not be changed. */
17937
17938static bool
17939ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17940{
17941 /* The next character should be an underscore ('_') followed
17942 by a digit. */
17943 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17944 return false;
17945
17946 /* Skip the underscore. */
17947 k++;
17948 int start = k;
17949
17950 /* Determine the number of digits for our number. */
17951 while (isdigit (encoding[k]))
17952 k++;
17953 if (k == start)
17954 return false;
17955
17956 std::string copy (&encoding[start], k - start);
17957 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17958 return false;
17959
17960 return true;
17961}
17962
17963/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17964 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17965 DENOM, update OFFSET, and return true on success. Return false on
17966 failure. */
17967
17968static bool
17969ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17970 gdb_mpz *num, gdb_mpz *denom)
17971{
17972 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17973 return false;
17974 return ada_get_gnat_encoded_number (encoding, offset, denom);
17975}
17976
09584414 17977/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
17978 of the corresponding TYPE by setting its type-specific data. CU is
17979 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17980 encodings. It is nullptr if the GNAT encoding should be
17981 ignored. */
09584414
JB
17982
17983static void
bbcdf9ab
TT
17984finish_fixed_point_type (struct type *type, const char *suffix,
17985 struct die_info *die, struct dwarf2_cu *cu)
09584414 17986{
09584414
JB
17987 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17988 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17989
bbcdf9ab
TT
17990 /* If GNAT encodings are preferred, don't examine the
17991 attributes. */
17992 struct attribute *attr = nullptr;
17993 if (suffix == nullptr)
17994 {
17995 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17996 if (attr == nullptr)
17997 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17998 if (attr == nullptr)
17999 attr = dwarf2_attr (die, DW_AT_small, cu);
18000 }
09584414 18001
5cde1d82
TT
18002 /* Numerator and denominator of our fixed-point type's scaling factor.
18003 The default is a scaling factor of 1, which we use as a fallback
18004 when we are not able to decode it (problem with the debugging info,
18005 unsupported forms, bug in GDB, etc...). Using that as the default
18006 allows us to at least print the unscaled value, which might still
18007 be useful to a user. */
18008 gdb_mpz scale_num (1);
18009 gdb_mpz scale_denom (1);
18010
09584414
JB
18011 if (attr == nullptr)
18012 {
bbcdf9ab
TT
18013 int offset = 0;
18014 if (suffix != nullptr
18015 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18016 &scale_denom)
18017 /* The number might be encoded as _nn_dd_nn_dd, where the
18018 second ratio is the 'small value. In this situation, we
18019 want the second value. */
18020 && (suffix[offset] != '_'
18021 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18022 &scale_denom)))
18023 {
18024 /* Found it. */
18025 }
18026 else
18027 {
18028 /* Scaling factor not found. Assume a scaling factor of 1,
18029 and hope for the best. At least the user will be able to
18030 see the encoded value. */
18031 scale_num = 1;
18032 scale_denom = 1;
18033 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18034 sect_offset_str (die->sect_off));
18035 }
09584414
JB
18036 }
18037 else if (attr->name == DW_AT_binary_scale)
18038 {
18039 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18040 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18041
5cde1d82 18042 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
18043 }
18044 else if (attr->name == DW_AT_decimal_scale)
18045 {
18046 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18047 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18048
5cde1d82 18049 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
18050 }
18051 else if (attr->name == DW_AT_small)
18052 {
18053 struct die_info *scale_die;
18054 struct dwarf2_cu *scale_cu = cu;
18055
18056 scale_die = follow_die_ref (die, attr, &scale_cu);
18057 if (scale_die->tag == DW_TAG_constant)
18058 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18059 &scale_num, &scale_denom);
18060 else
18061 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18062 " (DIE at %s)"),
18063 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18064 }
18065 else
18066 {
18067 complaint (_("unsupported scale attribute %s for fixed-point type"
18068 " (DIE at %s)"),
18069 dwarf_attr_name (attr->name),
18070 sect_offset_str (die->sect_off));
18071 }
18072
2a12c336 18073 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
18074 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18075 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
18076 mpq_canonicalize (scaling_factor.val);
18077}
18078
bbcdf9ab
TT
18079/* The gnat-encoding suffix for fixed point. */
18080
18081#define GNAT_FIXED_POINT_SUFFIX "___XF_"
18082
18083/* If NAME encodes an Ada fixed-point type, return a pointer to the
18084 "XF" suffix of the name. The text after this is what encodes the
18085 'small and 'delta information. Otherwise, return nullptr. */
18086
18087static const char *
18088gnat_encoded_fixed_point_type_info (const char *name)
18089{
18090 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18091}
18092
9b790ce7
UW
18093/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18094 (which may be different from NAME) to the architecture back-end to allow
18095 it to guess the correct format if necessary. */
18096
18097static struct type *
18098dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18099 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18100{
08feed99 18101 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18102 const struct floatformat **format;
18103 struct type *type;
18104
18105 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18106 if (format)
103a685e 18107 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18108 else
77b7c781 18109 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18110
18111 return type;
18112}
18113
eb77c9df
AB
18114/* Allocate an integer type of size BITS and name NAME. */
18115
18116static struct type *
18117dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18118 int bits, int unsigned_p, const char *name)
18119{
18120 struct type *type;
18121
18122 /* Versions of Intel's C Compiler generate an integer type called "void"
18123 instead of using DW_TAG_unspecified_type. This has been seen on
18124 at least versions 14, 17, and 18. */
35ee2dc2
AB
18125 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18126 && strcmp (name, "void") == 0)
eb77c9df
AB
18127 type = objfile_type (objfile)->builtin_void;
18128 else
18129 type = init_integer_type (objfile, bits, unsigned_p, name);
18130
18131 return type;
18132}
18133
09584414
JB
18134/* Return true if DIE has a DW_AT_small attribute whose value is
18135 a constant rational, where both the numerator and denominator
18136 are equal to zero.
18137
18138 CU is the DIE's Compilation Unit. */
18139
18140static bool
18141has_zero_over_zero_small_attribute (struct die_info *die,
18142 struct dwarf2_cu *cu)
18143{
18144 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18145 if (attr == nullptr)
18146 return false;
18147
18148 struct dwarf2_cu *scale_cu = cu;
18149 struct die_info *scale_die
18150 = follow_die_ref (die, attr, &scale_cu);
18151
18152 if (scale_die->tag != DW_TAG_constant)
18153 return false;
18154
5cde1d82 18155 gdb_mpz num (1), denom (1);
09584414 18156 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 18157 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
18158}
18159
8bdc1658
AB
18160/* Initialise and return a floating point type of size BITS suitable for
18161 use as a component of a complex number. The NAME_HINT is passed through
18162 when initialising the floating point type and is the name of the complex
18163 type.
18164
18165 As DWARF doesn't currently provide an explicit name for the components
18166 of a complex number, but it can be helpful to have these components
18167 named, we try to select a suitable name based on the size of the
18168 component. */
18169static struct type *
18170dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18171 struct objfile *objfile,
103a685e
TT
18172 int bits, const char *name_hint,
18173 enum bfd_endian byte_order)
8bdc1658 18174{
08feed99 18175 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18176 struct type *tt = nullptr;
18177
35add35e
AB
18178 /* Try to find a suitable floating point builtin type of size BITS.
18179 We're going to use the name of this type as the name for the complex
18180 target type that we are about to create. */
1db455a7 18181 switch (cu->language)
8bdc1658 18182 {
1db455a7
AB
18183 case language_fortran:
18184 switch (bits)
18185 {
18186 case 32:
18187 tt = builtin_f_type (gdbarch)->builtin_real;
18188 break;
18189 case 64:
18190 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18191 break;
18192 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18193 case 128:
18194 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18195 break;
18196 }
8bdc1658 18197 break;
1db455a7
AB
18198 default:
18199 switch (bits)
18200 {
18201 case 32:
18202 tt = builtin_type (gdbarch)->builtin_float;
18203 break;
18204 case 64:
18205 tt = builtin_type (gdbarch)->builtin_double;
18206 break;
18207 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18208 case 128:
18209 tt = builtin_type (gdbarch)->builtin_long_double;
18210 break;
18211 }
8bdc1658
AB
18212 break;
18213 }
18214
35add35e
AB
18215 /* If the type we found doesn't match the size we were looking for, then
18216 pretend we didn't find a type at all, the complex target type we
18217 create will then be nameless. */
a12e5744 18218 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18219 tt = nullptr;
18220
7d93a1e0 18221 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18222 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18223}
18224
c906108c
SS
18225/* Find a representation of a given base type and install
18226 it in the TYPE field of the die. */
18227
f792889a 18228static struct type *
e7c27a73 18229read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18230{
5e22e966 18231 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18232 struct type *type;
18233 struct attribute *attr;
19f392bc 18234 int encoding = 0, bits = 0;
15d034d0 18235 const char *name;
34877895 18236 gdbarch *arch;
c906108c 18237
e142c38c 18238 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18239 if (attr != nullptr && attr->form_is_constant ())
18240 encoding = attr->constant_value (0);
e142c38c 18241 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18242 if (attr != nullptr)
529908cb 18243 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18244 name = dwarf2_name (die, cu);
6ccb9162 18245 if (!name)
34877895 18246 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18247
08feed99 18248 arch = objfile->arch ();
103a685e
TT
18249 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18250
34877895 18251 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18252 if (attr != nullptr && attr->form_is_constant ())
103a685e 18253 {
529908cb 18254 int endianity = attr->constant_value (0);
103a685e
TT
18255
18256 switch (endianity)
18257 {
18258 case DW_END_big:
18259 byte_order = BFD_ENDIAN_BIG;
18260 break;
18261 case DW_END_little:
18262 byte_order = BFD_ENDIAN_LITTLE;
18263 break;
18264 default:
18265 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18266 break;
18267 }
18268 }
6ccb9162 18269
09584414
JB
18270 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18271 && cu->language == language_ada
18272 && has_zero_over_zero_small_attribute (die, cu))
18273 {
18274 /* brobecker/2018-02-24: This is a fixed point type for which
18275 the scaling factor is represented as fraction whose value
18276 does not make sense (zero divided by zero), so we should
18277 normally never see these. However, there is a small category
18278 of fixed point types for which GNAT is unable to provide
18279 the scaling factor via the standard DWARF mechanisms, and
18280 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18281 This is likely what this DIE is about. */
09584414
JB
18282 encoding = (encoding == DW_ATE_signed_fixed
18283 ? DW_ATE_signed
18284 : DW_ATE_unsigned);
18285 }
18286
bbcdf9ab
TT
18287 /* With GNAT encodings, fixed-point information will be encoded in
18288 the type name. Note that this can also occur with the above
18289 zero-over-zero case, which is why this is a separate "if" rather
18290 than an "else if". */
18291 const char *gnat_encoding_suffix = nullptr;
18292 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18293 && cu->language == language_ada
18294 && name != nullptr)
18295 {
18296 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18297 if (gnat_encoding_suffix != nullptr)
18298 {
18299 gdb_assert (startswith (gnat_encoding_suffix,
18300 GNAT_FIXED_POINT_SUFFIX));
18301 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18302 name, gnat_encoding_suffix - name);
18303 /* Use -1 here so that SUFFIX points at the "_" after the
18304 "XF". */
18305 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18306
18307 encoding = (encoding == DW_ATE_signed
18308 ? DW_ATE_signed_fixed
18309 : DW_ATE_unsigned_fixed);
18310 }
18311 }
18312
6ccb9162 18313 switch (encoding)
c906108c 18314 {
6ccb9162
UW
18315 case DW_ATE_address:
18316 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18317 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18318 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18319 break;
18320 case DW_ATE_boolean:
19f392bc 18321 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18322 break;
18323 case DW_ATE_complex_float:
103a685e
TT
18324 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18325 byte_order);
78134374 18326 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18327 {
18328 if (name == nullptr)
18329 {
18330 struct obstack *obstack
5e22e966 18331 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18332 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18333 nullptr);
18334 }
18335 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18336 }
18337 else
18338 type = init_complex_type (name, type);
6ccb9162
UW
18339 break;
18340 case DW_ATE_decimal_float:
19f392bc 18341 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18342 break;
18343 case DW_ATE_float:
103a685e 18344 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18345 break;
18346 case DW_ATE_signed:
eb77c9df 18347 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18348 break;
18349 case DW_ATE_unsigned:
3b2b8fea
TT
18350 if (cu->language == language_fortran
18351 && name
61012eef 18352 && startswith (name, "character("))
19f392bc
UW
18353 type = init_character_type (objfile, bits, 1, name);
18354 else
eb77c9df 18355 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18356 break;
18357 case DW_ATE_signed_char:
6e70227d 18358 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18359 || cu->language == language_pascal
18360 || cu->language == language_fortran)
19f392bc
UW
18361 type = init_character_type (objfile, bits, 0, name);
18362 else
eb77c9df 18363 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18364 break;
18365 case DW_ATE_unsigned_char:
868a0084 18366 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18367 || cu->language == language_pascal
c44af4eb
TT
18368 || cu->language == language_fortran
18369 || cu->language == language_rust)
19f392bc
UW
18370 type = init_character_type (objfile, bits, 1, name);
18371 else
eb77c9df 18372 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18373 break;
75079b2b 18374 case DW_ATE_UTF:
53e710ac 18375 {
53e710ac
PA
18376 if (bits == 16)
18377 type = builtin_type (arch)->builtin_char16;
18378 else if (bits == 32)
18379 type = builtin_type (arch)->builtin_char32;
18380 else
18381 {
b98664d3 18382 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18383 bits);
eb77c9df 18384 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18385 }
18386 return set_die_type (die, type, cu);
18387 }
75079b2b 18388 break;
09584414
JB
18389 case DW_ATE_signed_fixed:
18390 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18391 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18392 break;
18393 case DW_ATE_unsigned_fixed:
18394 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18395 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18396 break;
75079b2b 18397
6ccb9162 18398 default:
b98664d3 18399 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18400 dwarf_type_encoding_name (encoding));
77b7c781 18401 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18402 break;
c906108c 18403 }
6ccb9162 18404
0114d602 18405 if (name && strcmp (name, "char") == 0)
15152a54 18406 type->set_has_no_signedness (true);
0114d602 18407
2b4424c3
TT
18408 maybe_set_alignment (cu, die, type);
18409
db558e34 18410 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18411
20a5fcbd
TT
18412 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18413 {
18414 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18415 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18416 {
529908cb 18417 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18418 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18419 /* Only use the attributes if they make sense together. */
18420 if (attr == nullptr
529908cb
TT
18421 || (attr->as_unsigned () + real_bit_size
18422 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18423 {
18424 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18425 = real_bit_size;
18426 if (attr != nullptr)
18427 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18428 = attr->as_unsigned ();
20a5fcbd
TT
18429 }
18430 }
18431 }
18432
f792889a 18433 return set_die_type (die, type, cu);
c906108c
SS
18434}
18435
80180f79
SA
18436/* Parse dwarf attribute if it's a block, reference or constant and put the
18437 resulting value of the attribute into struct bound_prop.
18438 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18439
18440static int
18441attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18442 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18443 struct type *default_type)
80180f79
SA
18444{
18445 struct dwarf2_property_baton *baton;
5e22e966 18446 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18447 struct objfile *objfile = per_objfile->objfile;
18448 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18449
9a49df9d
AB
18450 gdb_assert (default_type != NULL);
18451
80180f79
SA
18452 if (attr == NULL || prop == NULL)
18453 return 0;
18454
4fc6c0d5 18455 if (attr->form_is_block ())
80180f79 18456 {
8d749320 18457 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18458 baton->property_type = default_type;
80180f79 18459 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18460 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18461
18462 struct dwarf_block *block = attr->as_block ();
18463 baton->locexpr.size = block->size;
18464 baton->locexpr.data = block->data;
216a7e6b
AB
18465 switch (attr->name)
18466 {
18467 case DW_AT_string_length:
18468 baton->locexpr.is_reference = true;
18469 break;
18470 default:
18471 baton->locexpr.is_reference = false;
18472 break;
18473 }
8c2e4e06
SM
18474
18475 prop->set_locexpr (baton);
18476 gdb_assert (prop->baton () != NULL);
80180f79 18477 }
cd6c91b4 18478 else if (attr->form_is_ref ())
80180f79
SA
18479 {
18480 struct dwarf2_cu *target_cu = cu;
18481 struct die_info *target_die;
18482 struct attribute *target_attr;
18483
18484 target_die = follow_die_ref (die, attr, &target_cu);
18485 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18486 if (target_attr == NULL)
18487 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18488 target_cu);
80180f79
SA
18489 if (target_attr == NULL)
18490 return 0;
18491
df25ebbd 18492 switch (target_attr->name)
80180f79 18493 {
df25ebbd 18494 case DW_AT_location:
cd6c91b4 18495 if (target_attr->form_is_section_offset ())
df25ebbd 18496 {
8d749320 18497 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18498 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18499 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18500 prop->set_loclist (baton);
18501 gdb_assert (prop->baton () != NULL);
df25ebbd 18502 }
4fc6c0d5 18503 else if (target_attr->form_is_block ())
df25ebbd 18504 {
8d749320 18505 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18506 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18507 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18508 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18509 struct dwarf_block *block = target_attr->as_block ();
18510 baton->locexpr.size = block->size;
18511 baton->locexpr.data = block->data;
9a49df9d 18512 baton->locexpr.is_reference = true;
8c2e4e06
SM
18513 prop->set_locexpr (baton);
18514 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18515 }
18516 else
18517 {
18518 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18519 "dynamic property");
18520 return 0;
18521 }
18522 break;
18523 case DW_AT_data_member_location:
18524 {
18525 LONGEST offset;
18526
18527 if (!handle_data_member_location (target_die, target_cu,
18528 &offset))
18529 return 0;
18530
8d749320 18531 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18532 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18533 target_cu);
df25ebbd
JB
18534 baton->offset_info.offset = offset;
18535 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18536 prop->set_addr_offset (baton);
df25ebbd
JB
18537 break;
18538 }
80180f79
SA
18539 }
18540 }
cd6c91b4 18541 else if (attr->form_is_constant ())
8c2e4e06 18542 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18543 else
18544 {
18545 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18546 dwarf2_name (die, cu));
18547 return 0;
18548 }
18549
18550 return 1;
18551}
18552
09ba997f 18553/* See read.h. */
9a49df9d 18554
09ba997f 18555struct type *
293e7e51 18556dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18557{
9a49df9d
AB
18558 struct type *int_type;
18559
18560 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18561#define TRY_TYPE(F) \
18562 int_type = (unsigned_p \
18563 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18564 : objfile_type (objfile)->builtin_ ## F); \
18565 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18566 return int_type
18567
18568 TRY_TYPE (char);
18569 TRY_TYPE (short);
18570 TRY_TYPE (int);
18571 TRY_TYPE (long);
18572 TRY_TYPE (long_long);
18573
18574#undef TRY_TYPE
18575
18576 gdb_assert_not_reached ("unable to find suitable integer type");
18577}
18578
09ba997f 18579/* See read.h. */
11a8b164 18580
09ba997f 18581struct type *
293e7e51 18582dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18583{
293e7e51
SM
18584 int addr_size = this->per_cu->addr_size ();
18585 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18586}
18587
b86352cf
AB
18588/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18589 present (which is valid) then compute the default type based on the
18590 compilation units address size. */
18591
18592static struct type *
18593read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18594{
18595 struct type *index_type = die_type (die, cu);
18596
18597 /* Dwarf-2 specifications explicitly allows to create subrange types
18598 without specifying a base type.
18599 In that case, the base type must be set to the type of
18600 the lower bound, upper bound or count, in that order, if any of these
18601 three attributes references an object that has a type.
18602 If no base type is found, the Dwarf-2 specifications say that
18603 a signed integer type of size equal to the size of an address should
18604 be used.
18605 For the following C code: `extern char gdb_int [];'
18606 GCC produces an empty range DIE.
18607 FIXME: muller/2010-05-28: Possible references to object for low bound,
18608 high bound or count are not yet handled by this code. */
78134374 18609 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18610 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18611
18612 return index_type;
18613}
18614
a02abb62
JB
18615/* Read the given DW_AT_subrange DIE. */
18616
f792889a 18617static struct type *
a02abb62
JB
18618read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18619{
4c9ad8c2 18620 struct type *base_type, *orig_base_type;
a02abb62
JB
18621 struct type *range_type;
18622 struct attribute *attr;
729efb13 18623 struct dynamic_prop low, high;
4fae6e18 18624 int low_default_is_valid;
c451ebe5 18625 int high_bound_is_count = 0;
15d034d0 18626 const char *name;
d359392f 18627 ULONGEST negative_mask;
e77813c8 18628
b86352cf
AB
18629 orig_base_type = read_subrange_index_type (die, cu);
18630
4c9ad8c2
TT
18631 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18632 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18633 creating the range type, but we use the result of check_typedef
18634 when examining properties of the type. */
18635 base_type = check_typedef (orig_base_type);
a02abb62 18636
7e314c57
JK
18637 /* The die_type call above may have already set the type for this DIE. */
18638 range_type = get_die_type (die, cu);
18639 if (range_type)
18640 return range_type;
18641
8c2e4e06 18642 high.set_const_val (0);
729efb13 18643
4fae6e18
JK
18644 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18645 omitting DW_AT_lower_bound. */
18646 switch (cu->language)
6e70227d 18647 {
4fae6e18
JK
18648 case language_c:
18649 case language_cplus:
8c2e4e06 18650 low.set_const_val (0);
4fae6e18
JK
18651 low_default_is_valid = 1;
18652 break;
18653 case language_fortran:
8c2e4e06 18654 low.set_const_val (1);
4fae6e18
JK
18655 low_default_is_valid = 1;
18656 break;
18657 case language_d:
4fae6e18 18658 case language_objc:
c44af4eb 18659 case language_rust:
8c2e4e06 18660 low.set_const_val (0);
4fae6e18
JK
18661 low_default_is_valid = (cu->header.version >= 4);
18662 break;
18663 case language_ada:
18664 case language_m2:
18665 case language_pascal:
8c2e4e06 18666 low.set_const_val (1);
4fae6e18
JK
18667 low_default_is_valid = (cu->header.version >= 4);
18668 break;
18669 default:
8c2e4e06 18670 low.set_const_val (0);
4fae6e18
JK
18671 low_default_is_valid = 0;
18672 break;
a02abb62
JB
18673 }
18674
e142c38c 18675 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18676 if (attr != nullptr)
9a49df9d 18677 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18678 else if (!low_default_is_valid)
b98664d3 18679 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18680 "- DIE at %s [in module %s]"),
18681 sect_offset_str (die->sect_off),
5e22e966 18682 objfile_name (cu->per_objfile->objfile));
a02abb62 18683
506f5c41
TV
18684 struct attribute *attr_ub, *attr_count;
18685 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18686 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18687 {
506f5c41 18688 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18689 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18690 {
c451ebe5 18691 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18692 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18693 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18694 else
18695 high_bound_is_count = 1;
c2ff108b 18696 }
506f5c41
TV
18697 else
18698 {
18699 if (attr_ub != NULL)
18700 complaint (_("Unresolved DW_AT_upper_bound "
18701 "- DIE at %s [in module %s]"),
18702 sect_offset_str (die->sect_off),
5e22e966 18703 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18704 if (attr_count != NULL)
18705 complaint (_("Unresolved DW_AT_count "
18706 "- DIE at %s [in module %s]"),
18707 sect_offset_str (die->sect_off),
5e22e966 18708 objfile_name (cu->per_objfile->objfile));
506f5c41 18709 }
e77813c8 18710 }
a02abb62 18711
4e962e74
TT
18712 LONGEST bias = 0;
18713 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18714 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18715 bias = bias_attr->constant_value (0);
4e962e74 18716
dbb9c2b1
JB
18717 /* Normally, the DWARF producers are expected to use a signed
18718 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18719 But this is unfortunately not always the case, as witnessed
18720 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18721 is used instead. To work around that ambiguity, we treat
18722 the bounds as signed, and thus sign-extend their values, when
18723 the base type is signed. */
6e70227d 18724 negative_mask =
d359392f 18725 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18726 if (low.kind () == PROP_CONST
c6d940a9 18727 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18728 low.set_const_val (low.const_val () | negative_mask);
18729 if (high.kind () == PROP_CONST
c6d940a9 18730 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18731 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18732
5bbd8269
AB
18733 /* Check for bit and byte strides. */
18734 struct dynamic_prop byte_stride_prop;
18735 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18736 if (attr_byte_stride != nullptr)
18737 {
293e7e51 18738 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18739 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18740 prop_type);
18741 }
18742
18743 struct dynamic_prop bit_stride_prop;
18744 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18745 if (attr_bit_stride != nullptr)
18746 {
18747 /* It only makes sense to have either a bit or byte stride. */
18748 if (attr_byte_stride != nullptr)
18749 {
18750 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18751 "- DIE at %s [in module %s]"),
18752 sect_offset_str (die->sect_off),
5e22e966 18753 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
18754 attr_bit_stride = nullptr;
18755 }
18756 else
18757 {
293e7e51 18758 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18759 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18760 prop_type);
18761 }
18762 }
18763
18764 if (attr_byte_stride != nullptr
18765 || attr_bit_stride != nullptr)
18766 {
18767 bool byte_stride_p = (attr_byte_stride != nullptr);
18768 struct dynamic_prop *stride
18769 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18770
18771 range_type
18772 = create_range_type_with_stride (NULL, orig_base_type, &low,
18773 &high, bias, stride, byte_stride_p);
18774 }
18775 else
18776 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18777
c451ebe5 18778 if (high_bound_is_count)
599088e3 18779 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 18780
c2ff108b
JK
18781 /* Ada expects an empty array on no boundary attributes. */
18782 if (attr == NULL && cu->language != language_ada)
8c2e4e06 18783 range_type->bounds ()->high.set_undefined ();
c2ff108b 18784
39cbfefa
DJ
18785 name = dwarf2_name (die, cu);
18786 if (name)
d0e39ea2 18787 range_type->set_name (name);
6e70227d 18788
e142c38c 18789 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18790 if (attr != nullptr)
529908cb 18791 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 18792
2b4424c3
TT
18793 maybe_set_alignment (cu, die, range_type);
18794
7e314c57
JK
18795 set_die_type (die, range_type, cu);
18796
18797 /* set_die_type should be already done. */
b4ba55a1
JB
18798 set_descriptive_type (range_type, die, cu);
18799
7e314c57 18800 return range_type;
a02abb62 18801}
6e70227d 18802
f792889a 18803static struct type *
81a17f79
JB
18804read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18805{
18806 struct type *type;
81a17f79 18807
5e22e966 18808 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 18809 type->set_name (dwarf2_name (die, cu));
81a17f79 18810
74a2f8ff 18811 /* In Ada, an unspecified type is typically used when the description
85102364 18812 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18813 such a type, we treat it as a stub, and try to resolve it later on,
18814 when needed. */
18815 if (cu->language == language_ada)
b4b73759 18816 type->set_is_stub (true);
74a2f8ff 18817
f792889a 18818 return set_die_type (die, type, cu);
81a17f79 18819}
a02abb62 18820
639d11d3
DC
18821/* Read a single die and all its descendents. Set the die's sibling
18822 field to NULL; set other fields in the die correctly, and set all
18823 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18824 location of the info_ptr after reading all of those dies. PARENT
18825 is the parent of the die in question. */
18826
18827static struct die_info *
dee91e82 18828read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18829 const gdb_byte *info_ptr,
18830 const gdb_byte **new_info_ptr,
dee91e82 18831 struct die_info *parent)
639d11d3
DC
18832{
18833 struct die_info *die;
d521ce57 18834 const gdb_byte *cur_ptr;
639d11d3 18835
3e225074 18836 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18837 if (die == NULL)
18838 {
18839 *new_info_ptr = cur_ptr;
18840 return NULL;
18841 }
93311388 18842 store_in_ref_table (die, reader->cu);
639d11d3 18843
3e225074 18844 if (die->has_children)
bf6af496 18845 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18846 else
18847 {
18848 die->child = NULL;
18849 *new_info_ptr = cur_ptr;
18850 }
18851
18852 die->sibling = NULL;
18853 die->parent = parent;
18854 return die;
18855}
18856
18857/* Read a die, all of its descendents, and all of its siblings; set
18858 all of the fields of all of the dies correctly. Arguments are as
18859 in read_die_and_children. */
18860
18861static struct die_info *
bf6af496 18862read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18863 const gdb_byte *info_ptr,
18864 const gdb_byte **new_info_ptr,
bf6af496 18865 struct die_info *parent)
639d11d3
DC
18866{
18867 struct die_info *first_die, *last_sibling;
d521ce57 18868 const gdb_byte *cur_ptr;
639d11d3 18869
c906108c 18870 cur_ptr = info_ptr;
639d11d3
DC
18871 first_die = last_sibling = NULL;
18872
18873 while (1)
c906108c 18874 {
639d11d3 18875 struct die_info *die
dee91e82 18876 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18877
1d325ec1 18878 if (die == NULL)
c906108c 18879 {
639d11d3
DC
18880 *new_info_ptr = cur_ptr;
18881 return first_die;
c906108c 18882 }
1d325ec1
DJ
18883
18884 if (!first_die)
18885 first_die = die;
c906108c 18886 else
1d325ec1
DJ
18887 last_sibling->sibling = die;
18888
18889 last_sibling = die;
c906108c 18890 }
c906108c
SS
18891}
18892
bf6af496
DE
18893/* Read a die, all of its descendents, and all of its siblings; set
18894 all of the fields of all of the dies correctly. Arguments are as
18895 in read_die_and_children.
18896 This the main entry point for reading a DIE and all its children. */
18897
18898static struct die_info *
18899read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18900 const gdb_byte *info_ptr,
18901 const gdb_byte **new_info_ptr,
bf6af496
DE
18902 struct die_info *parent)
18903{
18904 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18905 new_info_ptr, parent);
18906
b4f54984 18907 if (dwarf_die_debug)
bf6af496
DE
18908 {
18909 fprintf_unfiltered (gdb_stdlog,
18910 "Read die from %s@0x%x of %s:\n",
96b79293 18911 reader->die_section->get_name (),
bf6af496
DE
18912 (unsigned) (info_ptr - reader->die_section->buffer),
18913 bfd_get_filename (reader->abfd));
b4f54984 18914 dump_die (die, dwarf_die_debug);
bf6af496
DE
18915 }
18916
18917 return die;
18918}
18919
3019eac3
DE
18920/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18921 attributes.
18922 The caller is responsible for filling in the extra attributes
18923 and updating (*DIEP)->num_attrs.
18924 Set DIEP to point to a newly allocated die with its information,
3e225074 18925 except for its child, sibling, and parent fields. */
93311388 18926
d521ce57 18927static const gdb_byte *
3019eac3 18928read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18929 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18930 int num_extra_attrs)
93311388 18931{
b64f50a1 18932 unsigned int abbrev_number, bytes_read, i;
7c32eebb 18933 const struct abbrev_info *abbrev;
93311388
DE
18934 struct die_info *die;
18935 struct dwarf2_cu *cu = reader->cu;
18936 bfd *abfd = reader->abfd;
18937
9c541725 18938 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18939 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18940 info_ptr += bytes_read;
18941 if (!abbrev_number)
18942 {
18943 *diep = NULL;
93311388
DE
18944 return info_ptr;
18945 }
18946
685af9cd 18947 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18948 if (!abbrev)
348e048f
DE
18949 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18950 abbrev_number,
18951 bfd_get_filename (abfd));
18952
3019eac3 18953 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18954 die->sect_off = sect_off;
93311388
DE
18955 die->tag = abbrev->tag;
18956 die->abbrev = abbrev_number;
3e225074 18957 die->has_children = abbrev->has_children;
93311388 18958
3019eac3
DE
18959 /* Make the result usable.
18960 The caller needs to update num_attrs after adding the extra
18961 attributes. */
93311388
DE
18962 die->num_attrs = abbrev->num_attrs;
18963
7a5f294d 18964 bool any_need_reprocess = false;
93311388 18965 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 18966 {
7a5f294d
TT
18967 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18968 info_ptr);
18969 if (die->attrs[i].requires_reprocessing_p ())
18970 any_need_reprocess = true;
18a8505e
AT
18971 }
18972
052c8bb8 18973 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
18974 if (attr != nullptr && attr->form_is_unsigned ())
18975 cu->str_offsets_base = attr->as_unsigned ();
93311388 18976
41144253 18977 attr = die->attr (DW_AT_loclists_base);
18978 if (attr != nullptr)
529908cb 18979 cu->loclist_base = attr->as_unsigned ();
41144253 18980
a39fdb41 18981 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18982 if (maybe_addr_base.has_value ())
18983 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
18984
18985 attr = die->attr (DW_AT_rnglists_base);
18986 if (attr != nullptr)
2b0c7f41 18987 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 18988
7a5f294d
TT
18989 if (any_need_reprocess)
18990 {
18991 for (i = 0; i < abbrev->num_attrs; ++i)
18992 {
18993 if (die->attrs[i].requires_reprocessing_p ())
18994 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18995 }
18996 }
93311388 18997 *diep = die;
93311388
DE
18998 return info_ptr;
18999}
19000
3019eac3
DE
19001/* Read a die and all its attributes.
19002 Set DIEP to point to a newly allocated die with its information,
3e225074 19003 except for its child, sibling, and parent fields. */
3019eac3 19004
d521ce57 19005static const gdb_byte *
3019eac3 19006read_full_die (const struct die_reader_specs *reader,
3e225074 19007 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19008{
d521ce57 19009 const gdb_byte *result;
bf6af496 19010
3e225074 19011 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19012
b4f54984 19013 if (dwarf_die_debug)
bf6af496
DE
19014 {
19015 fprintf_unfiltered (gdb_stdlog,
19016 "Read die from %s@0x%x of %s:\n",
96b79293 19017 reader->die_section->get_name (),
bf6af496
DE
19018 (unsigned) (info_ptr - reader->die_section->buffer),
19019 bfd_get_filename (reader->abfd));
b4f54984 19020 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19021 }
19022
19023 return result;
3019eac3 19024}
433df2d4 19025\f
c906108c 19026
72bf9492
DJ
19027/* Returns nonzero if TAG represents a type that we might generate a partial
19028 symbol for. */
19029
19030static int
d8f62e84 19031is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19032{
19033 switch (tag)
19034 {
19035#if 0
19036 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19037 that we don't at present. Note that normally this does not
19038 matter, mainly because C compilers don't give names to these
19039 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19040 case DW_TAG_file_type:
19041 case DW_TAG_ptr_to_member_type:
19042 case DW_TAG_set_type:
19043 case DW_TAG_string_type:
19044 case DW_TAG_subroutine_type:
19045#endif
d8f62e84
TT
19046
19047 /* GNAT may emit an array with a name, but no typedef, so we
19048 need to make a symbol in this case. */
19049 case DW_TAG_array_type:
19050 return lang == language_ada;
19051
72bf9492
DJ
19052 case DW_TAG_base_type:
19053 case DW_TAG_class_type:
680b30c7 19054 case DW_TAG_interface_type:
72bf9492
DJ
19055 case DW_TAG_enumeration_type:
19056 case DW_TAG_structure_type:
19057 case DW_TAG_subrange_type:
19058 case DW_TAG_typedef:
19059 case DW_TAG_union_type:
19060 return 1;
19061 default:
19062 return 0;
19063 }
19064}
19065
19066/* Load all DIEs that are interesting for partial symbols into memory. */
19067
19068static struct partial_die_info *
dee91e82 19069load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19070 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19071{
dee91e82 19072 struct dwarf2_cu *cu = reader->cu;
5e22e966 19073 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19074 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19075 unsigned int bytes_read;
5afb4e99 19076 unsigned int load_all = 0;
72bf9492
DJ
19077 int nesting_level = 1;
19078
19079 parent_die = NULL;
19080 last_die = NULL;
19081
7adf1e79
DE
19082 gdb_assert (cu->per_cu != NULL);
19083 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19084 load_all = 1;
19085
72bf9492
DJ
19086 cu->partial_dies
19087 = htab_create_alloc_ex (cu->header.length / 12,
19088 partial_die_hash,
19089 partial_die_eq,
19090 NULL,
19091 &cu->comp_unit_obstack,
19092 hashtab_obstack_allocate,
19093 dummy_obstack_deallocate);
19094
72bf9492
DJ
19095 while (1)
19096 {
7c32eebb
TT
19097 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19098 &bytes_read);
72bf9492
DJ
19099
19100 /* A NULL abbrev means the end of a series of children. */
19101 if (abbrev == NULL)
19102 {
19103 if (--nesting_level == 0)
cd9983dd
YQ
19104 return first_die;
19105
72bf9492
DJ
19106 info_ptr += bytes_read;
19107 last_die = parent_die;
19108 parent_die = parent_die->die_parent;
19109 continue;
19110 }
19111
98bfdba5
PA
19112 /* Check for template arguments. We never save these; if
19113 they're seen, we just mark the parent, and go on our way. */
19114 if (parent_die != NULL
19115 && cu->language == language_cplus
19116 && (abbrev->tag == DW_TAG_template_type_param
19117 || abbrev->tag == DW_TAG_template_value_param))
19118 {
19119 parent_die->has_template_arguments = 1;
19120
19121 if (!load_all)
19122 {
19123 /* We don't need a partial DIE for the template argument. */
dee91e82 19124 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19125 continue;
19126 }
19127 }
19128
0d99eb77 19129 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19130 Skip their other children. */
19131 if (!load_all
19132 && cu->language == language_cplus
19133 && parent_die != NULL
f9b5d5ea
TV
19134 && parent_die->tag == DW_TAG_subprogram
19135 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19136 {
dee91e82 19137 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19138 continue;
19139 }
19140
5afb4e99
DJ
19141 /* Check whether this DIE is interesting enough to save. Normally
19142 we would not be interested in members here, but there may be
19143 later variables referencing them via DW_AT_specification (for
19144 static members). */
19145 if (!load_all
d8f62e84 19146 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19147 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19148 && abbrev->tag != DW_TAG_enumerator
19149 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19150 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19151 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19152 && abbrev->tag != DW_TAG_variable
5afb4e99 19153 && abbrev->tag != DW_TAG_namespace
f55ee35c 19154 && abbrev->tag != DW_TAG_module
95554aad 19155 && abbrev->tag != DW_TAG_member
74921315
KS
19156 && abbrev->tag != DW_TAG_imported_unit
19157 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19158 {
19159 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19160 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19161 continue;
19162 }
19163
6f06d47b
YQ
19164 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19165 abbrev);
cd9983dd 19166
48fbe735 19167 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19168
19169 /* This two-pass algorithm for processing partial symbols has a
19170 high cost in cache pressure. Thus, handle some simple cases
19171 here which cover the majority of C partial symbols. DIEs
19172 which neither have specification tags in them, nor could have
19173 specification tags elsewhere pointing at them, can simply be
19174 processed and discarded.
19175
19176 This segment is also optional; scan_partial_symbols and
19177 add_partial_symbol will handle these DIEs if we chain
19178 them in normally. When compilers which do not emit large
19179 quantities of duplicate debug information are more common,
19180 this code can probably be removed. */
19181
19182 /* Any complete simple types at the top level (pretty much all
19183 of them, for a language without namespaces), can be processed
19184 directly. */
19185 if (parent_die == NULL
cd9983dd
YQ
19186 && pdi.has_specification == 0
19187 && pdi.is_declaration == 0
19188 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19189 || pdi.tag == DW_TAG_base_type
d8f62e84 19190 || pdi.tag == DW_TAG_array_type
cd9983dd 19191 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19192 {
7d00ffec 19193 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19194 add_partial_symbol (&pdi, cu);
19195
cd9983dd 19196 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19197 continue;
19198 }
19199
d8228535
JK
19200 /* The exception for DW_TAG_typedef with has_children above is
19201 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19202 type_name_or_error will error on such types later.
d8228535
JK
19203
19204 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19205 it could not find the child DIEs referenced later, this is checked
19206 above. In correct DWARF DW_TAG_typedef should have no children. */
19207
cd9983dd 19208 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19209 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19210 "- DIE at %s [in module %s]"),
cd9983dd 19211 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19212
72bf9492
DJ
19213 /* If we're at the second level, and we're an enumerator, and
19214 our parent has no specification (meaning possibly lives in a
19215 namespace elsewhere), then we can add the partial symbol now
19216 instead of queueing it. */
cd9983dd 19217 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19218 && parent_die != NULL
19219 && parent_die->die_parent == NULL
19220 && parent_die->tag == DW_TAG_enumeration_type
19221 && parent_die->has_specification == 0)
19222 {
7d00ffec 19223 if (pdi.raw_name == NULL)
b98664d3 19224 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19225 else if (building_psymtab)
f0fbb768 19226 add_partial_symbol (&pdi, cu);
72bf9492 19227
cd9983dd 19228 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19229 continue;
19230 }
19231
cd9983dd 19232 struct partial_die_info *part_die
6f06d47b 19233 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19234
72bf9492
DJ
19235 /* We'll save this DIE so link it in. */
19236 part_die->die_parent = parent_die;
19237 part_die->die_sibling = NULL;
19238 part_die->die_child = NULL;
19239
19240 if (last_die && last_die == parent_die)
19241 last_die->die_child = part_die;
19242 else if (last_die)
19243 last_die->die_sibling = part_die;
19244
19245 last_die = part_die;
19246
19247 if (first_die == NULL)
19248 first_die = part_die;
19249
19250 /* Maybe add the DIE to the hash table. Not all DIEs that we
19251 find interesting need to be in the hash table, because we
19252 also have the parent/sibling/child chains; only those that we
19253 might refer to by offset later during partial symbol reading.
19254
19255 For now this means things that might have be the target of a
19256 DW_AT_specification, DW_AT_abstract_origin, or
19257 DW_AT_extension. DW_AT_extension will refer only to
19258 namespaces; DW_AT_abstract_origin refers to functions (and
19259 many things under the function DIE, but we do not recurse
19260 into function DIEs during partial symbol reading) and
19261 possibly variables as well; DW_AT_specification refers to
19262 declarations. Declarations ought to have the DW_AT_declaration
19263 flag. It happens that GCC forgets to put it in sometimes, but
19264 only for functions, not for types.
19265
19266 Adding more things than necessary to the hash table is harmless
19267 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19268 wasted time in find_partial_die, when we reread the compilation
19269 unit with load_all_dies set. */
72bf9492 19270
5afb4e99 19271 if (load_all
72929c62 19272 || abbrev->tag == DW_TAG_constant
5afb4e99 19273 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19274 || abbrev->tag == DW_TAG_variable
19275 || abbrev->tag == DW_TAG_namespace
19276 || part_die->is_declaration)
19277 {
19278 void **slot;
19279
19280 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19281 to_underlying (part_die->sect_off),
19282 INSERT);
72bf9492
DJ
19283 *slot = part_die;
19284 }
19285
72bf9492 19286 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19287 we have no reason to follow the children of structures; for other
98bfdba5
PA
19288 languages we have to, so that we can get at method physnames
19289 to infer fully qualified class names, for DW_AT_specification,
19290 and for C++ template arguments. For C++, we also look one level
19291 inside functions to find template arguments (if the name of the
19292 function does not already contain the template arguments).
bc30ff58 19293
0a4b0913
AB
19294 For Ada and Fortran, we need to scan the children of subprograms
19295 and lexical blocks as well because these languages allow the
19296 definition of nested entities that could be interesting for the
19297 debugger, such as nested subprograms for instance. */
72bf9492 19298 if (last_die->has_children
5afb4e99
DJ
19299 && (load_all
19300 || last_die->tag == DW_TAG_namespace
f55ee35c 19301 || last_die->tag == DW_TAG_module
72bf9492 19302 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19303 || (cu->language == language_cplus
19304 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19305 && (last_die->raw_name == NULL
19306 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19307 || (cu->language != language_c
19308 && (last_die->tag == DW_TAG_class_type
680b30c7 19309 || last_die->tag == DW_TAG_interface_type
72bf9492 19310 || last_die->tag == DW_TAG_structure_type
bc30ff58 19311 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19312 || ((cu->language == language_ada
19313 || cu->language == language_fortran)
bc30ff58
JB
19314 && (last_die->tag == DW_TAG_subprogram
19315 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19316 {
19317 nesting_level++;
19318 parent_die = last_die;
19319 continue;
19320 }
19321
19322 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19323 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19324
19325 /* Back to the top, do it again. */
19326 }
19327}
19328
6f06d47b 19329partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19330 const struct abbrev_info *abbrev)
6f06d47b
YQ
19331 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19332{
19333}
19334
7d00ffec
TT
19335/* See class definition. */
19336
19337const char *
19338partial_die_info::name (dwarf2_cu *cu)
19339{
19340 if (!canonical_name && raw_name != nullptr)
19341 {
19342 struct objfile *objfile = cu->per_objfile->objfile;
19343 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19344 canonical_name = 1;
19345 }
19346
19347 return raw_name;
19348}
19349
35cc7ed7
YQ
19350/* Read a minimal amount of information into the minimal die structure.
19351 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19352
48fbe735
YQ
19353const gdb_byte *
19354partial_die_info::read (const struct die_reader_specs *reader,
19355 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19356{
dee91e82 19357 struct dwarf2_cu *cu = reader->cu;
976ca316 19358 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19359 unsigned int i;
c5aa993b 19360 int has_low_pc_attr = 0;
c906108c 19361 int has_high_pc_attr = 0;
91da1414 19362 int high_pc_relative = 0;
c906108c 19363
fd0a254f 19364 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19365 {
e7da7f8f 19366 attribute attr;
7a5f294d 19367 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19368 /* String and address offsets that need to do the reprocessing have
dda83cd7 19369 already been read at this point, so there is no need to wait until
18a8505e 19370 the loop terminates to do the reprocessing. */
7a5f294d 19371 if (attr.requires_reprocessing_p ())
d0ce17d8 19372 read_attribute_reprocess (reader, &attr, tag);
c906108c 19373 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19374 partial symbol table. */
c906108c
SS
19375 switch (attr.name)
19376 {
19377 case DW_AT_name:
48fbe735 19378 switch (tag)
71c25dea
TT
19379 {
19380 case DW_TAG_compile_unit:
95554aad 19381 case DW_TAG_partial_unit:
348e048f 19382 case DW_TAG_type_unit:
71c25dea
TT
19383 /* Compilation units have a DW_AT_name that is a filename, not
19384 a source language identifier. */
19385 case DW_TAG_enumeration_type:
19386 case DW_TAG_enumerator:
19387 /* These tags always have simple identifiers already; no need
19388 to canonicalize them. */
7d00ffec 19389 canonical_name = 1;
2c830f54 19390 raw_name = attr.as_string ();
71c25dea
TT
19391 break;
19392 default:
7d00ffec 19393 canonical_name = 0;
2c830f54 19394 raw_name = attr.as_string ();
71c25dea
TT
19395 break;
19396 }
c906108c 19397 break;
31ef98ae 19398 case DW_AT_linkage_name:
c906108c 19399 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19400 /* Note that both forms of linkage name might appear. We
19401 assume they will be the same, and we only store the last
19402 one we see. */
95f982e5 19403 linkage_name = attr.as_string ();
c906108c
SS
19404 break;
19405 case DW_AT_low_pc:
19406 has_low_pc_attr = 1;
95f982e5 19407 lowpc = attr.as_address ();
c906108c
SS
19408 break;
19409 case DW_AT_high_pc:
19410 has_high_pc_attr = 1;
95f982e5 19411 highpc = attr.as_address ();
cd6c91b4 19412 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19413 high_pc_relative = 1;
c906108c
SS
19414 break;
19415 case DW_AT_location:
dda83cd7
SM
19416 /* Support the .debug_loc offsets. */
19417 if (attr.form_is_block ())
19418 {
9d2246fc 19419 d.locdesc = attr.as_block ();
dda83cd7
SM
19420 }
19421 else if (attr.form_is_section_offset ())
19422 {
4d3c2250 19423 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19424 }
19425 else
19426 {
4d3c2250
KB
19427 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19428 "partial symbol information");
dda83cd7 19429 }
c906108c 19430 break;
c906108c 19431 case DW_AT_external:
c45bc3f8 19432 is_external = attr.as_boolean ();
c906108c
SS
19433 break;
19434 case DW_AT_declaration:
c45bc3f8 19435 is_declaration = attr.as_boolean ();
c906108c
SS
19436 break;
19437 case DW_AT_type:
48fbe735 19438 has_type = 1;
c906108c
SS
19439 break;
19440 case DW_AT_abstract_origin:
19441 case DW_AT_specification:
72bf9492 19442 case DW_AT_extension:
48fbe735 19443 has_specification = 1;
0826b30a 19444 spec_offset = attr.get_ref_die_offset ();
48fbe735 19445 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19446 || cu->per_cu->is_dwz);
c906108c
SS
19447 break;
19448 case DW_AT_sibling:
19449 /* Ignore absolute siblings, they might point outside of
19450 the current compile unit. */
19451 if (attr.form == DW_FORM_ref_addr)
b98664d3 19452 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19453 else
b9502d3f 19454 {
48fbe735 19455 const gdb_byte *buffer = reader->buffer;
0826b30a 19456 sect_offset off = attr.get_ref_die_offset ();
9c541725 19457 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19458
19459 if (sibling_ptr < info_ptr)
b98664d3 19460 complaint (_("DW_AT_sibling points backwards"));
22869d73 19461 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19462 reader->die_section->overflow_complaint ();
b9502d3f 19463 else
48fbe735 19464 sibling = sibling_ptr;
b9502d3f 19465 }
c906108c 19466 break;
dda83cd7
SM
19467 case DW_AT_byte_size:
19468 has_byte_size = 1;
19469 break;
19470 case DW_AT_const_value:
19471 has_const_value = 1;
19472 break;
68511cec
CES
19473 case DW_AT_calling_convention:
19474 /* DWARF doesn't provide a way to identify a program's source-level
19475 entry point. DW_AT_calling_convention attributes are only meant
19476 to describe functions' calling conventions.
19477
19478 However, because it's a necessary piece of information in
0c1b455e
TT
19479 Fortran, and before DWARF 4 DW_CC_program was the only
19480 piece of debugging information whose definition refers to
19481 a 'main program' at all, several compilers marked Fortran
19482 main programs with DW_CC_program --- even when those
19483 functions use the standard calling conventions.
19484
19485 Although DWARF now specifies a way to provide this
19486 information, we support this practice for backward
19487 compatibility. */
529908cb 19488 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19489 && cu->language == language_fortran)
48fbe735 19490 main_subprogram = 1;
68511cec 19491 break;
481860b3 19492 case DW_AT_inline:
529908cb
TT
19493 {
19494 LONGEST value = attr.constant_value (-1);
19495 if (value == DW_INL_inlined
19496 || value == DW_INL_declared_inlined)
19497 may_be_inlined = 1;
19498 }
481860b3 19499 break;
95554aad
TT
19500
19501 case DW_AT_import:
48fbe735 19502 if (tag == DW_TAG_imported_unit)
36586728 19503 {
0826b30a 19504 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19505 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19506 || cu->per_cu->is_dwz);
19507 }
95554aad
TT
19508 break;
19509
0c1b455e 19510 case DW_AT_main_subprogram:
c45bc3f8 19511 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19512 break;
19513
05caa1d2
TT
19514 case DW_AT_ranges:
19515 {
2b0c7f41
SM
19516 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19517 on DWARF version). */
19518 ULONGEST ranges_offset = attr.as_unsigned ();
19519
19520 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19521 this value. */
19522 if (tag != DW_TAG_compile_unit)
19523 ranges_offset += cu->gnu_ranges_base;
19524
05caa1d2 19525 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19526 nullptr, tag))
05caa1d2
TT
19527 has_pc_info = 1;
19528 }
19529 break;
19530
c906108c
SS
19531 default:
19532 break;
19533 }
19534 }
19535
10d06d82
TT
19536 /* For Ada, if both the name and the linkage name appear, we prefer
19537 the latter. This lets "catch exception" work better, regardless
19538 of the order in which the name and linkage name were emitted.
19539 Really, though, this is just a workaround for the fact that gdb
19540 doesn't store both the name and the linkage name. */
19541 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19542 raw_name = linkage_name;
10d06d82 19543
91da1414 19544 if (high_pc_relative)
48fbe735 19545 highpc += lowpc;
91da1414 19546
9373cf26
JK
19547 if (has_low_pc_attr && has_high_pc_attr)
19548 {
19549 /* When using the GNU linker, .gnu.linkonce. sections are used to
19550 eliminate duplicate copies of functions and vtables and such.
19551 The linker will arbitrarily choose one and discard the others.
19552 The AT_*_pc values for such functions refer to local labels in
19553 these sections. If the section from that file was discarded, the
19554 labels are not in the output, so the relocs get a value of 0.
19555 If this is a discarded function, mark the pc bounds as invalid,
19556 so that GDB will ignore it. */
976ca316 19557 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19558 {
976ca316 19559 struct objfile *objfile = per_objfile->objfile;
08feed99 19560 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19561
b98664d3 19562 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19563 "for DIE at %s [in module %s]"),
48fbe735
YQ
19564 paddress (gdbarch, lowpc),
19565 sect_offset_str (sect_off),
9d8780f0 19566 objfile_name (objfile));
9373cf26
JK
19567 }
19568 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19569 else if (lowpc >= highpc)
9373cf26 19570 {
976ca316 19571 struct objfile *objfile = per_objfile->objfile;
08feed99 19572 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19573
b98664d3 19574 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19575 "for DIE at %s [in module %s]"),
48fbe735
YQ
19576 paddress (gdbarch, lowpc),
19577 paddress (gdbarch, highpc),
19578 sect_offset_str (sect_off),
9c541725 19579 objfile_name (objfile));
9373cf26
JK
19580 }
19581 else
48fbe735 19582 has_pc_info = 1;
9373cf26 19583 }
85cbf3d3 19584
c906108c
SS
19585 return info_ptr;
19586}
19587
72bf9492
DJ
19588/* Find a cached partial DIE at OFFSET in CU. */
19589
d590ff25
YQ
19590struct partial_die_info *
19591dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19592{
19593 struct partial_die_info *lookup_die = NULL;
6f06d47b 19594 struct partial_die_info part_die (sect_off);
72bf9492 19595
9a3c8263 19596 lookup_die = ((struct partial_die_info *)
d590ff25 19597 htab_find_with_hash (partial_dies, &part_die,
9c541725 19598 to_underlying (sect_off)));
72bf9492 19599
72bf9492
DJ
19600 return lookup_die;
19601}
19602
348e048f
DE
19603/* Find a partial DIE at OFFSET, which may or may not be in CU,
19604 except in the case of .debug_types DIEs which do not reference
19605 outside their CU (they do however referencing other types via
55f1336d 19606 DW_FORM_ref_sig8). */
72bf9492 19607
122cf0f2 19608static const struct cu_partial_die_info
9c541725 19609find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19610{
976ca316
SM
19611 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19612 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19613 struct partial_die_info *pd = NULL;
72bf9492 19614
36586728 19615 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19616 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19617 {
d590ff25 19618 pd = cu->find_partial_die (sect_off);
5afb4e99 19619 if (pd != NULL)
fb816e8b 19620 return { cu, pd };
0d99eb77
DE
19621 /* We missed recording what we needed.
19622 Load all dies and try again. */
5afb4e99 19623 }
0d99eb77
DE
19624 else
19625 {
19626 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19627 if (cu->per_cu->is_debug_types)
0d99eb77 19628 {
9d8780f0
SM
19629 error (_("Dwarf Error: Type Unit at offset %s contains"
19630 " external reference to offset %s [in module %s].\n"),
19631 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19632 bfd_get_filename (objfile->obfd));
19633 }
7188ed02
SM
19634 dwarf2_per_cu_data *per_cu
19635 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19636 per_objfile);
72bf9492 19637
976ca316 19638 cu = per_objfile->get_cu (per_cu);
7188ed02 19639 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19640 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19641
976ca316 19642 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19643
19644 cu->last_used = 0;
19645 pd = cu->find_partial_die (sect_off);
0d99eb77 19646 }
5afb4e99 19647
dee91e82
DE
19648 /* If we didn't find it, and not all dies have been loaded,
19649 load them all and try again. */
19650
7188ed02 19651 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 19652 {
7188ed02 19653 cu->per_cu->load_all_dies = 1;
fd820528
DE
19654
19655 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19656 THIS_CU->cu may already be in use. So we can't just free it and
19657 replace its DIEs with the ones we read in. Instead, we leave those
19658 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19659 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19660 set. */
976ca316 19661 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19662
7188ed02 19663 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19664 }
19665
19666 if (pd == NULL)
521894aa 19667 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 19668 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19669 return { cu, pd };
72bf9492
DJ
19670}
19671
abc72ce4
DE
19672/* See if we can figure out if the class lives in a namespace. We do
19673 this by looking for a member function; its demangled name will
19674 contain namespace info, if there is any. */
19675
19676static void
19677guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19678 struct dwarf2_cu *cu)
19679{
19680 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19681 what template types look like, because the demangler
19682 frequently doesn't give the same name as the debug info. We
19683 could fix this by only using the demangled name to get the
19684 prefix (but see comment in read_structure_type). */
19685
19686 struct partial_die_info *real_pdi;
19687 struct partial_die_info *child_pdi;
19688
19689 /* If this DIE (this DIE's specification, if any) has a parent, then
19690 we should not do this. We'll prepend the parent's fully qualified
19691 name when we create the partial symbol. */
19692
19693 real_pdi = struct_pdi;
19694 while (real_pdi->has_specification)
fb816e8b 19695 {
122cf0f2
AB
19696 auto res = find_partial_die (real_pdi->spec_offset,
19697 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19698 real_pdi = res.pdi;
19699 cu = res.cu;
19700 }
abc72ce4
DE
19701
19702 if (real_pdi->die_parent != NULL)
19703 return;
19704
19705 for (child_pdi = struct_pdi->die_child;
19706 child_pdi != NULL;
19707 child_pdi = child_pdi->die_sibling)
19708 {
19709 if (child_pdi->tag == DW_TAG_subprogram
19710 && child_pdi->linkage_name != NULL)
19711 {
43816ebc 19712 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19713 (cu->language_defn->class_name_from_physname
19714 (child_pdi->linkage_name));
abc72ce4
DE
19715 if (actual_class_name != NULL)
19716 {
5e22e966 19717 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19718 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19719 struct_pdi->canonical_name = 1;
abc72ce4
DE
19720 }
19721 break;
19722 }
19723 }
19724}
19725
25c11aca
TV
19726/* Return true if a DIE with TAG may have the DW_AT_const_value
19727 attribute. */
19728
19729static bool
19730can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19731{
19732 switch (tag)
19733 {
19734 case DW_TAG_constant:
19735 case DW_TAG_enumerator:
19736 case DW_TAG_formal_parameter:
19737 case DW_TAG_template_value_param:
19738 case DW_TAG_variable:
19739 return true;
19740 }
19741
19742 return false;
19743}
19744
52356b79
YQ
19745void
19746partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19747{
abc72ce4
DE
19748 /* Once we've fixed up a die, there's no point in doing so again.
19749 This also avoids a memory leak if we were to call
19750 guess_partial_die_structure_name multiple times. */
52356b79 19751 if (fixup_called)
abc72ce4
DE
19752 return;
19753
72bf9492
DJ
19754 /* If we found a reference attribute and the DIE has no name, try
19755 to find a name in the referred to DIE. */
19756
7d00ffec 19757 if (raw_name == NULL && has_specification)
72bf9492
DJ
19758 {
19759 struct partial_die_info *spec_die;
72bf9492 19760
122cf0f2 19761 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19762 spec_die = res.pdi;
19763 cu = res.cu;
72bf9492 19764
52356b79 19765 spec_die->fixup (cu);
72bf9492 19766
7d00ffec 19767 if (spec_die->raw_name)
72bf9492 19768 {
7d00ffec
TT
19769 raw_name = spec_die->raw_name;
19770 canonical_name = spec_die->canonical_name;
72bf9492
DJ
19771
19772 /* Copy DW_AT_external attribute if it is set. */
19773 if (spec_die->is_external)
52356b79 19774 is_external = spec_die->is_external;
72bf9492
DJ
19775 }
19776 }
19777
25c11aca
TV
19778 if (!has_const_value && has_specification
19779 && can_have_DW_AT_const_value_p (tag))
19780 {
19781 struct partial_die_info *spec_die;
19782
19783 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19784 spec_die = res.pdi;
19785 cu = res.cu;
19786
19787 spec_die->fixup (cu);
19788
19789 if (spec_die->has_const_value)
19790 {
19791 /* Copy DW_AT_const_value attribute if it is set. */
19792 has_const_value = spec_die->has_const_value;
19793 }
19794 }
19795
72bf9492 19796 /* Set default names for some unnamed DIEs. */
72bf9492 19797
7d00ffec
TT
19798 if (raw_name == NULL && tag == DW_TAG_namespace)
19799 {
19800 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19801 canonical_name = 1;
19802 }
72bf9492 19803
abc72ce4
DE
19804 /* If there is no parent die to provide a namespace, and there are
19805 children, see if we can determine the namespace from their linkage
122d1940 19806 name. */
abc72ce4 19807 if (cu->language == language_cplus
5e22e966 19808 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
19809 && die_parent == NULL
19810 && has_children
19811 && (tag == DW_TAG_class_type
19812 || tag == DW_TAG_structure_type
19813 || tag == DW_TAG_union_type))
19814 guess_partial_die_structure_name (this, cu);
abc72ce4 19815
53832f31
TT
19816 /* GCC might emit a nameless struct or union that has a linkage
19817 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 19818 if (raw_name == NULL
52356b79
YQ
19819 && (tag == DW_TAG_class_type
19820 || tag == DW_TAG_interface_type
19821 || tag == DW_TAG_structure_type
19822 || tag == DW_TAG_union_type)
19823 && linkage_name != NULL)
53832f31 19824 {
43816ebc
TT
19825 gdb::unique_xmalloc_ptr<char> demangled
19826 (gdb_demangle (linkage_name, DMGL_TYPES));
19827 if (demangled != nullptr)
53832f31 19828 {
96408a79
SA
19829 const char *base;
19830
19831 /* Strip any leading namespaces/classes, keep only the base name.
19832 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19833 base = strrchr (demangled.get (), ':');
19834 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19835 base++;
19836 else
43816ebc 19837 base = demangled.get ();
96408a79 19838
5e22e966 19839 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19840 raw_name = objfile->intern (base);
19841 canonical_name = 1;
53832f31
TT
19842 }
19843 }
19844
52356b79 19845 fixup_called = 1;
72bf9492
DJ
19846}
19847
d0ce17d8 19848/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
19849 contents from the given SECTION in the HEADER.
19850
19851 HEADER_OFFSET is the offset of the header in the section. */
41144253 19852static void
d0ce17d8 19853read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
19854 struct dwarf2_section_info *section,
19855 sect_offset header_offset)
41144253 19856{
19857 unsigned int bytes_read;
19858 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
19859 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19860
41144253 19861 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19862 info_ptr += bytes_read;
a1c40103 19863
41144253 19864 header->version = read_2_bytes (abfd, info_ptr);
19865 info_ptr += 2;
a1c40103 19866
41144253 19867 header->addr_size = read_1_byte (abfd, info_ptr);
19868 info_ptr += 1;
a1c40103 19869
41144253 19870 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19871 info_ptr += 1;
a1c40103 19872
41144253 19873 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19874}
19875
19876/* Return the DW_AT_loclists_base value for the CU. */
19877static ULONGEST
19878lookup_loclist_base (struct dwarf2_cu *cu)
19879{
19880 /* For the .dwo unit, the loclist_base points to the first offset following
19881 the header. The header consists of the following entities-
19882 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19883 bit format)
19884 2. version (2 bytes)
19885 3. address size (1 byte)
19886 4. segment selector size (1 byte)
19887 5. offset entry count (4 bytes)
19888 These sizes are derived as per the DWARFv5 standard. */
19889 if (cu->dwo_unit != nullptr)
19890 {
19891 if (cu->header.initial_length_size == 4)
19892 return LOCLIST_HEADER_SIZE32;
19893 return LOCLIST_HEADER_SIZE64;
19894 }
19895 return cu->loclist_base;
19896}
19897
19898/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19899 array of offsets in the .debug_loclists section. */
e57933dc
SM
19900
19901static sect_offset
41144253 19902read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19903{
976ca316
SM
19904 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19905 struct objfile *objfile = per_objfile->objfile;
41144253 19906 bfd *abfd = objfile->obfd;
a1c40103
SM
19907 ULONGEST loclist_header_size =
19908 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19909 : LOCLIST_HEADER_SIZE64);
41144253 19910 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
19911
19912 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19913 ULONGEST start_offset =
19914 loclist_base + loclist_index * cu->header.offset_size;
19915
a1c40103 19916 /* Get loclists section. */
41144253 19917 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19918
a1c40103 19919 /* Read the loclists section content. */
41144253 19920 section->read (objfile);
19921 if (section->buffer == NULL)
a0c1eeba
SM
19922 error (_("DW_FORM_loclistx used without .debug_loclists "
19923 "section [in module %s]"), objfile_name (objfile));
19924
a1c40103
SM
19925 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19926 so if loclist_base is smaller than the header size, we have a problem. */
19927 if (loclist_base < loclist_header_size)
19928 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19929 objfile_name (objfile));
19930
19931 /* Read the header of the loclists contribution. */
d0ce17d8 19932 struct loclists_rnglists_header header;
a1c40103
SM
19933 read_loclists_rnglists_header (&header, section,
19934 (sect_offset) (loclist_base - loclist_header_size));
19935
19936 /* Verify the loclist index is valid. */
41144253 19937 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
19938 error (_("DW_FORM_loclistx pointing outside of "
19939 ".debug_loclists offset array [in module %s]"),
19940 objfile_name (objfile));
19941
05787bad
SM
19942 /* Validate that reading won't go beyond the end of the section. */
19943 if (start_offset + cu->header.offset_size > section->size)
19944 error (_("Reading DW_FORM_loclistx index beyond end of"
19945 ".debug_loclists section [in module %s]"),
19946 objfile_name (objfile));
19947
19948 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 19949
19950 if (cu->header.offset_size == 4)
e57933dc 19951 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 19952 else
e57933dc 19953 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 19954}
19955
d0ce17d8
CT
19956/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19957 array of offsets in the .debug_rnglists section. */
e57933dc
SM
19958
19959static sect_offset
d0ce17d8
CT
19960read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19961 dwarf_tag tag)
19962{
19963 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19964 struct objfile *objfile = dwarf2_per_objfile->objfile;
19965 bfd *abfd = objfile->obfd;
19966 ULONGEST rnglist_header_size =
19967 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19968 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
19969
19970 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19971 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19972 doesn't apply. */
d0ce17d8 19973 ULONGEST rnglist_base =
2b0c7f41 19974 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
19975
19976 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
19977 ULONGEST start_offset =
19978 rnglist_base + rnglist_index * cu->header.offset_size;
19979
19980 /* Get rnglists section. */
19981 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19982
19983 /* Read the rnglists section content. */
19984 section->read (objfile);
19985 if (section->buffer == nullptr)
19986 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19987 "[in module %s]"),
19988 objfile_name (objfile));
19989
a1c40103
SM
19990 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19991 so if rnglist_base is smaller than the header size, we have a problem. */
19992 if (rnglist_base < rnglist_header_size)
19993 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19994 objfile_name (objfile));
19995
19996 /* Read the header of the rnglists contribution. */
d0ce17d8 19997 struct loclists_rnglists_header header;
a1c40103
SM
19998 read_loclists_rnglists_header (&header, section,
19999 (sect_offset) (rnglist_base - rnglist_header_size));
20000
20001 /* Verify the rnglist index is valid. */
d0ce17d8
CT
20002 if (rnglist_index >= header.offset_entry_count)
20003 error (_("DW_FORM_rnglistx index pointing outside of "
20004 ".debug_rnglists offset array [in module %s]"),
20005 objfile_name (objfile));
20006
d0ce17d8 20007 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 20008 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
20009 error (_("Reading DW_FORM_rnglistx index beyond end of"
20010 ".debug_rnglists section [in module %s]"),
20011 objfile_name (objfile));
20012
20013 const gdb_byte *info_ptr = section->buffer + start_offset;
20014
20015 if (cu->header.offset_size == 4)
e57933dc 20016 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 20017 else
e57933dc 20018 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
20019}
20020
18a8505e
AT
20021/* Process the attributes that had to be skipped in the first round. These
20022 attributes are the ones that need str_offsets_base or addr_base attributes.
20023 They could not have been processed in the first round, because at the time
20024 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20025static void
20026read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20027 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20028{
20029 struct dwarf2_cu *cu = reader->cu;
20030 switch (attr->form)
20031 {
20032 case DW_FORM_addrx:
20033 case DW_FORM_GNU_addr_index:
36d378cf
TT
20034 attr->set_address (read_addr_index (cu,
20035 attr->as_unsigned_reprocess ()));
dda83cd7 20036 break;
41144253 20037 case DW_FORM_loclistx:
b1829e1b 20038 {
e57933dc 20039 sect_offset loclists_sect_off
b1829e1b
SM
20040 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20041
e57933dc 20042 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
20043 }
20044 break;
d0ce17d8 20045 case DW_FORM_rnglistx:
b1829e1b 20046 {
e57933dc 20047 sect_offset rnglists_sect_off
b1829e1b
SM
20048 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20049
e57933dc 20050 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 20051 }
dda83cd7 20052 break;
18a8505e
AT
20053 case DW_FORM_strx:
20054 case DW_FORM_strx1:
20055 case DW_FORM_strx2:
20056 case DW_FORM_strx3:
20057 case DW_FORM_strx4:
20058 case DW_FORM_GNU_str_index:
20059 {
fe56917a 20060 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20061 gdb_assert (!attr->canonical_string_p ());
18a8505e 20062 if (reader->dwo_file != NULL)
c6481205
TT
20063 attr->set_string_noncanonical (read_dwo_str_index (reader,
20064 str_index));
18a8505e 20065 else
c6481205
TT
20066 attr->set_string_noncanonical (read_stub_str_index (cu,
20067 str_index));
18a8505e
AT
20068 break;
20069 }
20070 default:
20071 gdb_assert_not_reached (_("Unexpected DWARF form."));
20072 }
20073}
20074
a8329558 20075/* Read an attribute value described by an attribute form. */
c906108c 20076
d521ce57 20077static const gdb_byte *
dee91e82
DE
20078read_attribute_value (const struct die_reader_specs *reader,
20079 struct attribute *attr, unsigned form,
7a5f294d 20080 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20081{
dee91e82 20082 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20083 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20084 struct objfile *objfile = per_objfile->objfile;
dee91e82 20085 bfd *abfd = reader->abfd;
e7c27a73 20086 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20087 unsigned int bytes_read;
20088 struct dwarf_block *blk;
20089
aead7601 20090 attr->form = (enum dwarf_form) form;
a8329558 20091 switch (form)
c906108c 20092 {
c906108c 20093 case DW_FORM_ref_addr:
7c290a04
TT
20094 if (cu_header->version == 2)
20095 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 20096 &bytes_read));
ae411497 20097 else
7c290a04 20098 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20099 &bytes_read));
ae411497
TT
20100 info_ptr += bytes_read;
20101 break;
36586728 20102 case DW_FORM_GNU_ref_alt:
7c290a04 20103 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20104 &bytes_read));
36586728
TT
20105 info_ptr += bytes_read;
20106 break;
ae411497 20107 case DW_FORM_addr:
08feed99
TT
20108 {
20109 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 20110 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
20111 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20112 attr->set_address (addr);
08feed99
TT
20113 info_ptr += bytes_read;
20114 }
c906108c
SS
20115 break;
20116 case DW_FORM_block2:
7b5a2f43 20117 blk = dwarf_alloc_block (cu);
c906108c
SS
20118 blk->size = read_2_bytes (abfd, info_ptr);
20119 info_ptr += 2;
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_block4:
7b5a2f43 20125 blk = dwarf_alloc_block (cu);
c906108c
SS
20126 blk->size = read_4_bytes (abfd, info_ptr);
20127 info_ptr += 4;
20128 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20129 info_ptr += blk->size;
9d2246fc 20130 attr->set_block (blk);
c906108c
SS
20131 break;
20132 case DW_FORM_data2:
414ad644 20133 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20134 info_ptr += 2;
20135 break;
20136 case DW_FORM_data4:
414ad644 20137 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20138 info_ptr += 4;
20139 break;
20140 case DW_FORM_data8:
414ad644 20141 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20142 info_ptr += 8;
20143 break;
0224619f
JK
20144 case DW_FORM_data16:
20145 blk = dwarf_alloc_block (cu);
20146 blk->size = 16;
20147 blk->data = read_n_bytes (abfd, info_ptr, 16);
20148 info_ptr += 16;
9d2246fc 20149 attr->set_block (blk);
0224619f 20150 break;
2dc7f7b3 20151 case DW_FORM_sec_offset:
7c290a04 20152 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20153 &bytes_read));
2dc7f7b3
TT
20154 info_ptr += bytes_read;
20155 break;
41144253 20156 case DW_FORM_loclistx:
20157 {
7a5f294d
TT
20158 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20159 &bytes_read));
20160 info_ptr += bytes_read;
41144253 20161 }
20162 break;
c906108c 20163 case DW_FORM_string:
c6481205
TT
20164 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20165 &bytes_read));
c906108c
SS
20166 info_ptr += bytes_read;
20167 break;
4bdf3d34 20168 case DW_FORM_strp:
36586728
TT
20169 if (!cu->per_cu->is_dwz)
20170 {
c6481205
TT
20171 attr->set_string_noncanonical
20172 (read_indirect_string (per_objfile,
20173 abfd, info_ptr, cu_header,
20174 &bytes_read));
36586728
TT
20175 info_ptr += bytes_read;
20176 break;
20177 }
20178 /* FALLTHROUGH */
43988095
JK
20179 case DW_FORM_line_strp:
20180 if (!cu->per_cu->is_dwz)
20181 {
c6481205
TT
20182 attr->set_string_noncanonical
20183 (per_objfile->read_line_string (info_ptr, cu_header,
20184 &bytes_read));
43988095
JK
20185 info_ptr += bytes_read;
20186 break;
20187 }
20188 /* FALLTHROUGH */
36586728
TT
20189 case DW_FORM_GNU_strp_alt:
20190 {
a7308ce0 20191 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
20192 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20193 &bytes_read);
36586728 20194
c6481205
TT
20195 attr->set_string_noncanonical
20196 (dwz->read_string (objfile, str_offset));
36586728
TT
20197 info_ptr += bytes_read;
20198 }
4bdf3d34 20199 break;
2dc7f7b3 20200 case DW_FORM_exprloc:
c906108c 20201 case DW_FORM_block:
7b5a2f43 20202 blk = dwarf_alloc_block (cu);
c906108c
SS
20203 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20204 info_ptr += bytes_read;
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_block1:
7b5a2f43 20210 blk = dwarf_alloc_block (cu);
c906108c
SS
20211 blk->size = read_1_byte (abfd, info_ptr);
20212 info_ptr += 1;
20213 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20214 info_ptr += blk->size;
9d2246fc 20215 attr->set_block (blk);
c906108c
SS
20216 break;
20217 case DW_FORM_data1:
c906108c 20218 case DW_FORM_flag:
414ad644 20219 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20220 info_ptr += 1;
20221 break;
2dc7f7b3 20222 case DW_FORM_flag_present:
414ad644 20223 attr->set_unsigned (1);
2dc7f7b3 20224 break;
c906108c 20225 case DW_FORM_sdata:
1bc397c5 20226 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20227 info_ptr += bytes_read;
20228 break;
18a8505e 20229 case DW_FORM_rnglistx:
7a5f294d
TT
20230 {
20231 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20232 &bytes_read));
20233 info_ptr += bytes_read;
20234 }
20235 break;
d0ce17d8 20236 case DW_FORM_udata:
414ad644 20237 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20238 info_ptr += bytes_read;
20239 break;
20240 case DW_FORM_ref1:
7c290a04 20241 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20242 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20243 info_ptr += 1;
20244 break;
20245 case DW_FORM_ref2:
7c290a04 20246 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20247 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20248 info_ptr += 2;
20249 break;
20250 case DW_FORM_ref4:
7c290a04 20251 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20252 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20253 info_ptr += 4;
20254 break;
613e1657 20255 case DW_FORM_ref8:
7c290a04 20256 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20257 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20258 info_ptr += 8;
20259 break;
55f1336d 20260 case DW_FORM_ref_sig8:
630ed6b9 20261 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20262 info_ptr += 8;
20263 break;
c906108c 20264 case DW_FORM_ref_udata:
7c290a04 20265 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
20266 + read_unsigned_leb128 (abfd, info_ptr,
20267 &bytes_read)));
c906108c
SS
20268 info_ptr += bytes_read;
20269 break;
c906108c 20270 case DW_FORM_indirect:
a8329558
KW
20271 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20272 info_ptr += bytes_read;
43988095
JK
20273 if (form == DW_FORM_implicit_const)
20274 {
20275 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20276 info_ptr += bytes_read;
20277 }
20278 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20279 info_ptr);
43988095
JK
20280 break;
20281 case DW_FORM_implicit_const:
1bc397c5 20282 attr->set_signed (implicit_const);
a8329558 20283 break;
336d760d 20284 case DW_FORM_addrx:
3019eac3 20285 case DW_FORM_GNU_addr_index:
fe56917a
TT
20286 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20287 &bytes_read));
3019eac3
DE
20288 info_ptr += bytes_read;
20289 break;
cf532bd1 20290 case DW_FORM_strx:
15f18d14
AT
20291 case DW_FORM_strx1:
20292 case DW_FORM_strx2:
20293 case DW_FORM_strx3:
20294 case DW_FORM_strx4:
3019eac3 20295 case DW_FORM_GNU_str_index:
3019eac3 20296 {
15f18d14
AT
20297 ULONGEST str_index;
20298 if (form == DW_FORM_strx1)
20299 {
20300 str_index = read_1_byte (abfd, info_ptr);
20301 info_ptr += 1;
20302 }
20303 else if (form == DW_FORM_strx2)
20304 {
20305 str_index = read_2_bytes (abfd, info_ptr);
20306 info_ptr += 2;
20307 }
20308 else if (form == DW_FORM_strx3)
20309 {
20310 str_index = read_3_bytes (abfd, info_ptr);
20311 info_ptr += 3;
20312 }
20313 else if (form == DW_FORM_strx4)
20314 {
20315 str_index = read_4_bytes (abfd, info_ptr);
20316 info_ptr += 4;
20317 }
20318 else
20319 {
20320 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20321 info_ptr += bytes_read;
20322 }
fe56917a 20323 attr->set_unsigned_reprocess (str_index);
7a5f294d 20324 }
3019eac3 20325 break;
c906108c 20326 default:
8a3fe4f8 20327 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20328 dwarf_form_name (form),
20329 bfd_get_filename (abfd));
c906108c 20330 }
28e94949 20331
36586728 20332 /* Super hack. */
cd6c91b4 20333 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20334 attr->form = DW_FORM_GNU_ref_alt;
20335
28e94949
JB
20336 /* We have seen instances where the compiler tried to emit a byte
20337 size attribute of -1 which ended up being encoded as an unsigned
20338 0xffffffff. Although 0xffffffff is technically a valid size value,
20339 an object of this size seems pretty unlikely so we can relatively
20340 safely treat these cases as if the size attribute was invalid and
20341 treat them as zero by default. */
20342 if (attr->name == DW_AT_byte_size
20343 && form == DW_FORM_data4
529908cb 20344 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20345 {
20346 complaint
dda83cd7
SM
20347 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20348 hex_string (attr->as_unsigned ()));
414ad644 20349 attr->set_unsigned (0);
01c66ae6 20350 }
28e94949 20351
c906108c
SS
20352 return info_ptr;
20353}
20354
a8329558
KW
20355/* Read an attribute described by an abbreviated attribute. */
20356
d521ce57 20357static const gdb_byte *
dee91e82 20358read_attribute (const struct die_reader_specs *reader,
4444f407 20359 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20360 const gdb_byte *info_ptr)
a8329558
KW
20361{
20362 attr->name = abbrev->name;
c6481205 20363 attr->string_is_canonical = 0;
fe56917a 20364 attr->requires_reprocessing = 0;
43988095 20365 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20366 abbrev->implicit_const, info_ptr);
a8329558
KW
20367}
20368
43988095
JK
20369/* Return pointer to string at .debug_str offset STR_OFFSET. */
20370
20371static const char *
976ca316 20372read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20373 LONGEST str_offset)
43988095 20374{
976ca316
SM
20375 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20376 str_offset, "DW_FORM_strp");
c906108c
SS
20377}
20378
43988095
JK
20379/* Return pointer to string at .debug_str offset as read from BUF.
20380 BUF is assumed to be in a compilation unit described by CU_HEADER.
20381 Return *BYTES_READ_PTR count of bytes read from BUF. */
20382
d521ce57 20383static const char *
976ca316 20384read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20385 const gdb_byte *buf,
cf2c3c16
TT
20386 const struct comp_unit_head *cu_header,
20387 unsigned int *bytes_read_ptr)
20388{
8266302d 20389 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20390
976ca316 20391 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20392}
20393
86c0bb4c 20394/* See read.h. */
43988095 20395
86c0bb4c
TT
20396const char *
20397dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20398 const struct comp_unit_head *cu_header,
20399 unsigned int *bytes_read_ptr)
43988095 20400{
86c0bb4c 20401 bfd *abfd = objfile->obfd;
8266302d 20402 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20403
5989a64e 20404 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20405}
20406
3019eac3 20407/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20408 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20409 ADDR_SIZE is the size of addresses from the CU header. */
20410
20411static CORE_ADDR
976ca316
SM
20412read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20413 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20414{
976ca316 20415 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20416 bfd *abfd = objfile->obfd;
20417 const gdb_byte *info_ptr;
18a8505e 20418 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20419
976ca316
SM
20420 per_objfile->per_bfd->addr.read (objfile);
20421 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20422 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20423 objfile_name (objfile));
18a8505e 20424 if (addr_base_or_zero + addr_index * addr_size
976ca316 20425 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20426 error (_("DW_FORM_addr_index pointing outside of "
20427 ".debug_addr section [in module %s]"),
4262abfb 20428 objfile_name (objfile));
976ca316
SM
20429 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20430 + addr_index * addr_size);
3019eac3
DE
20431 if (addr_size == 4)
20432 return bfd_get_32 (abfd, info_ptr);
20433 else
20434 return bfd_get_64 (abfd, info_ptr);
20435}
20436
20437/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20438
20439static CORE_ADDR
20440read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20441{
5e22e966 20442 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20443 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20444}
20445
20446/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20447
20448static CORE_ADDR
d521ce57 20449read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20450 unsigned int *bytes_read)
20451{
5e22e966 20452 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20453 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20454
20455 return read_addr_index (cu, addr_index);
20456}
20457
450a1bfc 20458/* See read.h. */
3019eac3
DE
20459
20460CORE_ADDR
82ca3f51 20461dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20462 dwarf2_per_objfile *per_objfile,
82ca3f51 20463 unsigned int addr_index)
3019eac3 20464{
976ca316 20465 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20466 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20467 int addr_size;
20468
3019eac3
DE
20469 /* We need addr_base and addr_size.
20470 If we don't have PER_CU->cu, we have to get it.
20471 Nasty, but the alternative is storing the needed info in PER_CU,
20472 which at this point doesn't seem justified: it's not clear how frequently
20473 it would get used and it would increase the size of every PER_CU.
20474 Entry points like dwarf2_per_cu_addr_size do a similar thing
20475 so we're not in uncharted territory here.
20476 Alas we need to be a bit more complicated as addr_base is contained
20477 in the DIE.
20478
20479 We don't need to read the entire CU(/TU).
20480 We just need the header and top level die.
a1b64ce1 20481
3019eac3 20482 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20483 For now we skip this optimization. */
3019eac3
DE
20484
20485 if (cu != NULL)
20486 {
20487 addr_base = cu->addr_base;
20488 addr_size = cu->header.addr_size;
20489 }
20490 else
20491 {
976ca316 20492 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20493 addr_base = reader.cu->addr_base;
20494 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20495 }
20496
976ca316 20497 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20498}
20499
18a8505e
AT
20500/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20501 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20502 DWO file. */
3019eac3 20503
d521ce57 20504static const char *
18a8505e
AT
20505read_str_index (struct dwarf2_cu *cu,
20506 struct dwarf2_section_info *str_section,
20507 struct dwarf2_section_info *str_offsets_section,
20508 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20509{
976ca316
SM
20510 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20511 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20512 const char *objf_name = objfile_name (objfile);
3019eac3 20513 bfd *abfd = objfile->obfd;
d521ce57 20514 const gdb_byte *info_ptr;
3019eac3 20515 ULONGEST str_offset;
cf532bd1 20516 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20517
96b79293
TT
20518 str_section->read (objfile);
20519 str_offsets_section->read (objfile);
73869dc2 20520 if (str_section->buffer == NULL)
18a8505e 20521 error (_("%s used without %s section"
9d8780f0 20522 " in CU at offset %s [in module %s]"),
96b79293 20523 form_name, str_section->get_name (),
dda83cd7 20524 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20525 if (str_offsets_section->buffer == NULL)
18a8505e 20526 error (_("%s used without %s section"
9d8780f0 20527 " in CU at offset %s [in module %s]"),
96b79293 20528 form_name, str_section->get_name (),
dda83cd7 20529 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20530 info_ptr = (str_offsets_section->buffer
18a8505e 20531 + str_offsets_base
3019eac3
DE
20532 + str_index * cu->header.offset_size);
20533 if (cu->header.offset_size == 4)
20534 str_offset = bfd_get_32 (abfd, info_ptr);
20535 else
20536 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20537 if (str_offset >= str_section->size)
57d63ce2 20538 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20539 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20540 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20541 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20542}
20543
18a8505e
AT
20544/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20545
20546static const char *
20547read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20548{
20549 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20550 ? reader->cu->header.addr_size : 0;
20551 return read_str_index (reader->cu,
20552 &reader->dwo_file->sections.str,
20553 &reader->dwo_file->sections.str_offsets,
20554 str_offsets_base, str_index);
20555}
20556
20557/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20558
20559static const char *
20560read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20561{
5e22e966 20562 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20563 const char *objf_name = objfile_name (objfile);
20564 static const char form_name[] = "DW_FORM_GNU_str_index";
20565 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20566
20567 if (!cu->str_offsets_base.has_value ())
20568 error (_("%s used in Fission stub without %s"
20569 " in CU at offset 0x%lx [in module %s]"),
20570 form_name, str_offsets_attr_name,
20571 (long) cu->header.offset_size, objf_name);
20572
20573 return read_str_index (cu,
5e22e966
SM
20574 &cu->per_objfile->per_bfd->str,
20575 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20576 *cu->str_offsets_base, str_index);
20577}
20578
3019eac3
DE
20579/* Return the length of an LEB128 number in BUF. */
20580
20581static int
20582leb128_size (const gdb_byte *buf)
20583{
20584 const gdb_byte *begin = buf;
20585 gdb_byte byte;
20586
20587 while (1)
20588 {
20589 byte = *buf++;
20590 if ((byte & 128) == 0)
20591 return buf - begin;
20592 }
20593}
20594
c906108c 20595static void
e142c38c 20596set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20597{
20598 switch (lang)
20599 {
20600 case DW_LANG_C89:
76bee0cc 20601 case DW_LANG_C99:
0cfd832f 20602 case DW_LANG_C11:
c906108c 20603 case DW_LANG_C:
d1be3247 20604 case DW_LANG_UPC:
e142c38c 20605 cu->language = language_c;
c906108c 20606 break;
9c37b5ae 20607 case DW_LANG_Java:
c906108c 20608 case DW_LANG_C_plus_plus:
0cfd832f
MW
20609 case DW_LANG_C_plus_plus_11:
20610 case DW_LANG_C_plus_plus_14:
e142c38c 20611 cu->language = language_cplus;
c906108c 20612 break;
6aecb9c2
JB
20613 case DW_LANG_D:
20614 cu->language = language_d;
20615 break;
c906108c
SS
20616 case DW_LANG_Fortran77:
20617 case DW_LANG_Fortran90:
b21b22e0 20618 case DW_LANG_Fortran95:
f7de9aab
MW
20619 case DW_LANG_Fortran03:
20620 case DW_LANG_Fortran08:
e142c38c 20621 cu->language = language_fortran;
c906108c 20622 break;
a766d390
DE
20623 case DW_LANG_Go:
20624 cu->language = language_go;
20625 break;
c906108c 20626 case DW_LANG_Mips_Assembler:
e142c38c 20627 cu->language = language_asm;
c906108c
SS
20628 break;
20629 case DW_LANG_Ada83:
8aaf0b47 20630 case DW_LANG_Ada95:
bc5f45f8
JB
20631 cu->language = language_ada;
20632 break;
72019c9c
GM
20633 case DW_LANG_Modula2:
20634 cu->language = language_m2;
20635 break;
fe8e67fd
PM
20636 case DW_LANG_Pascal83:
20637 cu->language = language_pascal;
20638 break;
22566fbd
DJ
20639 case DW_LANG_ObjC:
20640 cu->language = language_objc;
20641 break;
c44af4eb
TT
20642 case DW_LANG_Rust:
20643 case DW_LANG_Rust_old:
20644 cu->language = language_rust;
20645 break;
c906108c
SS
20646 case DW_LANG_Cobol74:
20647 case DW_LANG_Cobol85:
c906108c 20648 default:
e142c38c 20649 cu->language = language_minimal;
c906108c
SS
20650 break;
20651 }
e142c38c 20652 cu->language_defn = language_def (cu->language);
c906108c
SS
20653}
20654
20655/* Return the named attribute or NULL if not there. */
20656
20657static struct attribute *
e142c38c 20658dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20659{
a48e046c 20660 for (;;)
c906108c 20661 {
a48e046c
TT
20662 unsigned int i;
20663 struct attribute *spec = NULL;
20664
20665 for (i = 0; i < die->num_attrs; ++i)
20666 {
20667 if (die->attrs[i].name == name)
20668 return &die->attrs[i];
20669 if (die->attrs[i].name == DW_AT_specification
20670 || die->attrs[i].name == DW_AT_abstract_origin)
20671 spec = &die->attrs[i];
20672 }
20673
20674 if (!spec)
20675 break;
c906108c 20676
f2f0e013 20677 die = follow_die_ref (die, spec, &cu);
f2f0e013 20678 }
c5aa993b 20679
c906108c
SS
20680 return NULL;
20681}
20682
7d45c7c3
KB
20683/* Return the string associated with a string-typed attribute, or NULL if it
20684 is either not found or is of an incorrect type. */
20685
20686static const char *
20687dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20688{
20689 struct attribute *attr;
20690 const char *str = NULL;
20691
20692 attr = dwarf2_attr (die, name, cu);
20693
20694 if (attr != NULL)
20695 {
95f982e5 20696 str = attr->as_string ();
e61108c9 20697 if (str == nullptr)
dda83cd7 20698 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20699 "DIE at %s in module %s"),
20700 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20701 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20702 }
20703
20704 return str;
20705}
20706
a084a2a6 20707/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20708 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20709static const char *
20710dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20711{
20712 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20713 if (dwo_name == nullptr)
20714 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20715 return dwo_name;
20716}
20717
05cf31d1
JB
20718/* Return non-zero iff the attribute NAME is defined for the given DIE,
20719 and holds a non-zero value. This function should only be used for
2dc7f7b3 20720 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20721
20722static int
20723dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20724{
20725 struct attribute *attr = dwarf2_attr (die, name, cu);
20726
c45bc3f8 20727 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
20728}
20729
3ca72b44 20730static int
e142c38c 20731die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20732{
05cf31d1
JB
20733 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20734 which value is non-zero. However, we have to be careful with
20735 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20736 (via dwarf2_flag_true_p) follows this attribute. So we may
20737 end up accidently finding a declaration attribute that belongs
20738 to a different DIE referenced by the specification attribute,
20739 even though the given DIE does not have a declaration attribute. */
20740 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20741 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20742}
20743
63d06c5c 20744/* Return the die giving the specification for DIE, if there is
f2f0e013 20745 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20746 containing the return value on output. If there is no
20747 specification, but there is an abstract origin, that is
20748 returned. */
63d06c5c
DC
20749
20750static struct die_info *
f2f0e013 20751die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20752{
f2f0e013
DJ
20753 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20754 *spec_cu);
63d06c5c 20755
edb3359d
DJ
20756 if (spec_attr == NULL)
20757 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20758
63d06c5c
DC
20759 if (spec_attr == NULL)
20760 return NULL;
20761 else
f2f0e013 20762 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20763}
c906108c 20764
527f3840
JK
20765/* Stub for free_line_header to match void * callback types. */
20766
20767static void
20768free_line_header_voidp (void *arg)
20769{
9a3c8263 20770 struct line_header *lh = (struct line_header *) arg;
527f3840 20771
fff8551c 20772 delete lh;
527f3840
JK
20773}
20774
83769d0b 20775/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20776
20777static struct dwarf2_section_info *
20778get_debug_line_section (struct dwarf2_cu *cu)
20779{
20780 struct dwarf2_section_info *section;
976ca316 20781 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
20782
20783 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20784 DWO file. */
20785 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20786 section = &cu->dwo_unit->dwo_file->sections.line;
20787 else if (cu->per_cu->is_dwz)
20788 {
a7308ce0 20789 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
20790
20791 section = &dwz->line;
20792 }
20793 else
976ca316 20794 section = &per_objfile->per_bfd->line;
36586728
TT
20795
20796 return section;
20797}
20798
debd256d 20799/* Read the statement program header starting at OFFSET in
3019eac3 20800 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20801 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20802 Returns NULL if there is a problem reading the header, e.g., if it
20803 has a version we don't understand.
debd256d
JB
20804
20805 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20806 the returned object point into the dwarf line section buffer,
20807 and must not be freed. */
ae2de4f8 20808
fff8551c 20809static line_header_up
9c541725 20810dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20811{
3019eac3 20812 struct dwarf2_section_info *section;
976ca316 20813 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 20814
36586728 20815 section = get_debug_line_section (cu);
976ca316 20816 section->read (per_objfile->objfile);
3019eac3 20817 if (section->buffer == NULL)
debd256d 20818 {
3019eac3 20819 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20820 complaint (_("missing .debug_line.dwo section"));
3019eac3 20821 else
b98664d3 20822 complaint (_("missing .debug_line section"));
debd256d
JB
20823 return 0;
20824 }
20825
0df7ad3a 20826 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 20827 per_objfile, section, &cu->header);
debd256d 20828}
c906108c 20829
c6da4cef 20830/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20831 Return the file name of the psymtab for the given file_entry.
c6da4cef 20832 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20833 If space for the result is malloc'd, *NAME_HOLDER will be set.
20834 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20835
d521ce57 20836static const char *
7ba99d21 20837psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20838 const dwarf2_psymtab *pst,
c89b44cd
TT
20839 const char *comp_dir,
20840 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20841{
d521ce57
TT
20842 const char *include_name = fe.name;
20843 const char *include_name_to_compare = include_name;
72b9f47f 20844 const char *pst_filename;
c6da4cef
DE
20845 int file_is_pst;
20846
8c43009f 20847 const char *dir_name = fe.include_dir (lh);
c6da4cef 20848
c89b44cd 20849 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20850 if (!IS_ABSOLUTE_PATH (include_name)
20851 && (dir_name != NULL || comp_dir != NULL))
20852 {
20853 /* Avoid creating a duplicate psymtab for PST.
20854 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20855 Before we do the comparison, however, we need to account
20856 for DIR_NAME and COMP_DIR.
20857 First prepend dir_name (if non-NULL). If we still don't
20858 have an absolute path prepend comp_dir (if non-NULL).
20859 However, the directory we record in the include-file's
20860 psymtab does not contain COMP_DIR (to match the
20861 corresponding symtab(s)).
20862
20863 Example:
20864
20865 bash$ cd /tmp
20866 bash$ gcc -g ./hello.c
20867 include_name = "hello.c"
20868 dir_name = "."
20869 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20870 DW_AT_name = "./hello.c"
20871
20872 */
c6da4cef
DE
20873
20874 if (dir_name != NULL)
20875 {
c89b44cd
TT
20876 name_holder->reset (concat (dir_name, SLASH_STRING,
20877 include_name, (char *) NULL));
20878 include_name = name_holder->get ();
c6da4cef 20879 include_name_to_compare = include_name;
c6da4cef
DE
20880 }
20881 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20882 {
c89b44cd
TT
20883 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20884 include_name, (char *) NULL));
20885 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20886 }
20887 }
20888
20889 pst_filename = pst->filename;
c89b44cd 20890 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20891 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20892 {
c89b44cd
TT
20893 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20894 pst_filename, (char *) NULL));
20895 pst_filename = copied_name.get ();
c6da4cef
DE
20896 }
20897
1e3fad37 20898 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20899
c6da4cef
DE
20900 if (file_is_pst)
20901 return NULL;
20902 return include_name;
20903}
20904
d9b3de22
DE
20905/* State machine to track the state of the line number program. */
20906
6f77053d 20907class lnp_state_machine
d9b3de22 20908{
6f77053d
PA
20909public:
20910 /* Initialize a machine state for the start of a line number
20911 program. */
804d2729
TT
20912 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20913 bool record_lines_p);
6f77053d 20914
8c43009f
PA
20915 file_entry *current_file ()
20916 {
20917 /* lh->file_names is 0-based, but the file name numbers in the
20918 statement program are 1-based. */
6f77053d
PA
20919 return m_line_header->file_name_at (m_file);
20920 }
20921
20922 /* Record the line in the state machine. END_SEQUENCE is true if
20923 we're processing the end of a sequence. */
20924 void record_line (bool end_sequence);
20925
a8caed5d 20926 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 20927 nop-out rest of the lines in this sequence. */
6f77053d
PA
20928 void check_line_address (struct dwarf2_cu *cu,
20929 const gdb_byte *line_ptr,
7ab6656f 20930 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20931
20932 void handle_set_discriminator (unsigned int discriminator)
20933 {
20934 m_discriminator = discriminator;
20935 m_line_has_non_zero_discriminator |= discriminator != 0;
20936 }
20937
20938 /* Handle DW_LNE_set_address. */
20939 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20940 {
20941 m_op_index = 0;
20942 address += baseaddr;
20943 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20944 }
20945
20946 /* Handle DW_LNS_advance_pc. */
20947 void handle_advance_pc (CORE_ADDR adjust);
20948
20949 /* Handle a special opcode. */
20950 void handle_special_opcode (unsigned char op_code);
20951
20952 /* Handle DW_LNS_advance_line. */
20953 void handle_advance_line (int line_delta)
20954 {
20955 advance_line (line_delta);
20956 }
20957
20958 /* Handle DW_LNS_set_file. */
20959 void handle_set_file (file_name_index file);
20960
20961 /* Handle DW_LNS_negate_stmt. */
20962 void handle_negate_stmt ()
20963 {
20964 m_is_stmt = !m_is_stmt;
20965 }
20966
20967 /* Handle DW_LNS_const_add_pc. */
20968 void handle_const_add_pc ();
20969
20970 /* Handle DW_LNS_fixed_advance_pc. */
20971 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20972 {
20973 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20974 m_op_index = 0;
20975 }
20976
20977 /* Handle DW_LNS_copy. */
20978 void handle_copy ()
20979 {
20980 record_line (false);
20981 m_discriminator = 0;
20982 }
20983
20984 /* Handle DW_LNE_end_sequence. */
20985 void handle_end_sequence ()
20986 {
804d2729 20987 m_currently_recording_lines = true;
6f77053d
PA
20988 }
20989
20990private:
20991 /* Advance the line by LINE_DELTA. */
20992 void advance_line (int line_delta)
20993 {
20994 m_line += line_delta;
20995
20996 if (line_delta != 0)
20997 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20998 }
20999
804d2729
TT
21000 struct dwarf2_cu *m_cu;
21001
6f77053d
PA
21002 gdbarch *m_gdbarch;
21003
21004 /* True if we're recording lines.
21005 Otherwise we're building partial symtabs and are just interested in
21006 finding include files mentioned by the line number program. */
21007 bool m_record_lines_p;
21008
8c43009f 21009 /* The line number header. */
6f77053d 21010 line_header *m_line_header;
8c43009f 21011
6f77053d
PA
21012 /* These are part of the standard DWARF line number state machine,
21013 and initialized according to the DWARF spec. */
d9b3de22 21014
6f77053d 21015 unsigned char m_op_index = 0;
7ba99d21
AT
21016 /* The line table index of the current file. */
21017 file_name_index m_file = 1;
6f77053d
PA
21018 unsigned int m_line = 1;
21019
21020 /* These are initialized in the constructor. */
21021
21022 CORE_ADDR m_address;
21023 bool m_is_stmt;
21024 unsigned int m_discriminator;
d9b3de22
DE
21025
21026 /* Additional bits of state we need to track. */
21027
21028 /* The last file that we called dwarf2_start_subfile for.
21029 This is only used for TLLs. */
6f77053d 21030 unsigned int m_last_file = 0;
d9b3de22 21031 /* The last file a line number was recorded for. */
6f77053d 21032 struct subfile *m_last_subfile = NULL;
d9b3de22 21033
1313c56e
AB
21034 /* The address of the last line entry. */
21035 CORE_ADDR m_last_address;
21036
21037 /* Set to true when a previous line at the same address (using
21038 m_last_address) had m_is_stmt true. This is reset to false when a
21039 line entry at a new address (m_address different to m_last_address) is
21040 processed. */
21041 bool m_stmt_at_address = false;
21042
804d2729
TT
21043 /* When true, record the lines we decode. */
21044 bool m_currently_recording_lines = false;
d9b3de22
DE
21045
21046 /* The last line number that was recorded, used to coalesce
21047 consecutive entries for the same line. This can happen, for
21048 example, when discriminators are present. PR 17276. */
6f77053d
PA
21049 unsigned int m_last_line = 0;
21050 bool m_line_has_non_zero_discriminator = false;
8c43009f 21051};
d9b3de22 21052
6f77053d
PA
21053void
21054lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21055{
21056 CORE_ADDR addr_adj = (((m_op_index + adjust)
21057 / m_line_header->maximum_ops_per_instruction)
21058 * m_line_header->minimum_instruction_length);
21059 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21060 m_op_index = ((m_op_index + adjust)
21061 % m_line_header->maximum_ops_per_instruction);
21062}
d9b3de22 21063
6f77053d
PA
21064void
21065lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21066{
6f77053d 21067 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21068 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21069 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21070 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21071 / m_line_header->maximum_ops_per_instruction)
21072 * m_line_header->minimum_instruction_length);
21073 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21074 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21075 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21076
258bf0ee 21077 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21078 advance_line (line_delta);
21079 record_line (false);
21080 m_discriminator = 0;
21081}
d9b3de22 21082
6f77053d
PA
21083void
21084lnp_state_machine::handle_set_file (file_name_index file)
21085{
21086 m_file = file;
21087
21088 const file_entry *fe = current_file ();
21089 if (fe == NULL)
21090 dwarf2_debug_line_missing_file_complaint ();
21091 else if (m_record_lines_p)
21092 {
21093 const char *dir = fe->include_dir (m_line_header);
21094
c24bdb02 21095 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21096 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21097 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21098 }
21099}
21100
21101void
21102lnp_state_machine::handle_const_add_pc ()
21103{
21104 CORE_ADDR adjust
21105 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21106
21107 CORE_ADDR addr_adj
21108 = (((m_op_index + adjust)
21109 / m_line_header->maximum_ops_per_instruction)
21110 * m_line_header->minimum_instruction_length);
21111
21112 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21113 m_op_index = ((m_op_index + adjust)
21114 % m_line_header->maximum_ops_per_instruction);
21115}
d9b3de22 21116
a05a36a5
DE
21117/* Return non-zero if we should add LINE to the line number table.
21118 LINE is the line to add, LAST_LINE is the last line that was added,
21119 LAST_SUBFILE is the subfile for LAST_LINE.
21120 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21121 had a non-zero discriminator.
21122
21123 We have to be careful in the presence of discriminators.
21124 E.g., for this line:
21125
21126 for (i = 0; i < 100000; i++);
21127
21128 clang can emit four line number entries for that one line,
21129 each with a different discriminator.
21130 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21131
21132 However, we want gdb to coalesce all four entries into one.
21133 Otherwise the user could stepi into the middle of the line and
21134 gdb would get confused about whether the pc really was in the
21135 middle of the line.
21136
21137 Things are further complicated by the fact that two consecutive
21138 line number entries for the same line is a heuristic used by gcc
21139 to denote the end of the prologue. So we can't just discard duplicate
21140 entries, we have to be selective about it. The heuristic we use is
21141 that we only collapse consecutive entries for the same line if at least
21142 one of those entries has a non-zero discriminator. PR 17276.
21143
21144 Note: Addresses in the line number state machine can never go backwards
21145 within one sequence, thus this coalescing is ok. */
21146
21147static int
804d2729
TT
21148dwarf_record_line_p (struct dwarf2_cu *cu,
21149 unsigned int line, unsigned int last_line,
a05a36a5
DE
21150 int line_has_non_zero_discriminator,
21151 struct subfile *last_subfile)
21152{
c24bdb02 21153 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21154 return 1;
21155 if (line != last_line)
21156 return 1;
21157 /* Same line for the same file that we've seen already.
21158 As a last check, for pr 17276, only record the line if the line
21159 has never had a non-zero discriminator. */
21160 if (!line_has_non_zero_discriminator)
21161 return 1;
21162 return 0;
21163}
21164
804d2729
TT
21165/* Use the CU's builder to record line number LINE beginning at
21166 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21167
21168static void
d9b3de22 21169dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21170 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21171 struct dwarf2_cu *cu)
252a6764
DE
21172{
21173 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21174
27e0867f
DE
21175 if (dwarf_line_debug)
21176 {
21177 fprintf_unfiltered (gdb_stdlog,
21178 "Recording line %u, file %s, address %s\n",
21179 line, lbasename (subfile->name),
21180 paddress (gdbarch, address));
21181 }
21182
804d2729 21183 if (cu != nullptr)
8c95582d 21184 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21185}
21186
21187/* Subroutine of dwarf_decode_lines_1 to simplify it.
21188 Mark the end of a set of line number records.
d9b3de22 21189 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21190 If SUBFILE is NULL the request is ignored. */
21191
21192static void
21193dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21194 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21195{
27e0867f
DE
21196 if (subfile == NULL)
21197 return;
21198
21199 if (dwarf_line_debug)
21200 {
21201 fprintf_unfiltered (gdb_stdlog,
21202 "Finishing current line, file %s, address %s\n",
21203 lbasename (subfile->name),
21204 paddress (gdbarch, address));
21205 }
21206
8c95582d 21207 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21208}
21209
6f77053d
PA
21210void
21211lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21212{
d9b3de22
DE
21213 if (dwarf_line_debug)
21214 {
21215 fprintf_unfiltered (gdb_stdlog,
21216 "Processing actual line %u: file %u,"
94a72be7 21217 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21218 m_line, m_file,
6f77053d 21219 paddress (m_gdbarch, m_address),
94a72be7
AB
21220 m_is_stmt, m_discriminator,
21221 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21222 }
21223
6f77053d 21224 file_entry *fe = current_file ();
8c43009f
PA
21225
21226 if (fe == NULL)
d9b3de22
DE
21227 dwarf2_debug_line_missing_file_complaint ();
21228 /* For now we ignore lines not starting on an instruction boundary.
21229 But not when processing end_sequence for compatibility with the
21230 previous version of the code. */
6f77053d 21231 else if (m_op_index == 0 || end_sequence)
d9b3de22 21232 {
8c43009f 21233 fe->included_p = 1;
8c95582d 21234 if (m_record_lines_p)
d9b3de22 21235 {
1313c56e
AB
21236 /* When we switch files we insert an end maker in the first file,
21237 switch to the second file and add a new line entry. The
21238 problem is that the end marker inserted in the first file will
21239 discard any previous line entries at the same address. If the
21240 line entries in the first file are marked as is-stmt, while
21241 the new line in the second file is non-stmt, then this means
21242 the end marker will discard is-stmt lines so we can have a
21243 non-stmt line. This means that there are less addresses at
21244 which the user can insert a breakpoint.
21245
21246 To improve this we track the last address in m_last_address,
21247 and whether we have seen an is-stmt at this address. Then
21248 when switching files, if we have seen a stmt at the current
21249 address, and we are switching to create a non-stmt line, then
21250 discard the new line. */
21251 bool file_changed
21252 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21253 bool ignore_this_line
dda83cd7
SM
21254 = ((file_changed && !end_sequence && m_last_address == m_address
21255 && !m_is_stmt && m_stmt_at_address)
21256 || (!end_sequence && m_line == 0));
1313c56e
AB
21257
21258 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21259 {
804d2729
TT
21260 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21261 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21262 }
21263
1313c56e 21264 if (!end_sequence && !ignore_this_line)
d9b3de22 21265 {
8c95582d
AB
21266 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21267
804d2729 21268 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21269 m_line_has_non_zero_discriminator,
21270 m_last_subfile))
d9b3de22 21271 {
c24bdb02 21272 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21273 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21274 builder->get_current_subfile (),
8c95582d 21275 m_line, m_address, is_stmt,
804d2729 21276 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21277 }
c24bdb02 21278 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21279 m_last_line = m_line;
d9b3de22
DE
21280 }
21281 }
21282 }
1313c56e
AB
21283
21284 /* Track whether we have seen any m_is_stmt true at m_address in case we
21285 have multiple line table entries all at m_address. */
21286 if (m_last_address != m_address)
21287 {
21288 m_stmt_at_address = false;
21289 m_last_address = m_address;
21290 }
21291 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21292}
21293
804d2729
TT
21294lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21295 line_header *lh, bool record_lines_p)
d9b3de22 21296{
804d2729 21297 m_cu = cu;
6f77053d
PA
21298 m_gdbarch = arch;
21299 m_record_lines_p = record_lines_p;
21300 m_line_header = lh;
d9b3de22 21301
804d2729 21302 m_currently_recording_lines = true;
d9b3de22 21303
d9b3de22
DE
21304 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21305 was a line entry for it so that the backend has a chance to adjust it
21306 and also record it in case it needs it. This is currently used by MIPS
21307 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21308 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21309 m_is_stmt = lh->default_is_stmt;
21310 m_discriminator = 0;
1313c56e
AB
21311
21312 m_last_address = m_address;
21313 m_stmt_at_address = false;
252a6764
DE
21314}
21315
6f77053d
PA
21316void
21317lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21318 const gdb_byte *line_ptr,
7ab6656f 21319 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21320{
a8caed5d
FS
21321 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21322 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21323 located at 0x0. In this case, additionally check that if
21324 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21325
a8caed5d
FS
21326 if ((address == 0 && address < unrelocated_lowpc)
21327 || address == (CORE_ADDR) -1)
924c2928
DE
21328 {
21329 /* This line table is for a function which has been
21330 GCd by the linker. Ignore it. PR gdb/12528 */
21331
5e22e966 21332 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21333 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21334
b98664d3 21335 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21336 line_offset, objfile_name (objfile));
804d2729
TT
21337 m_currently_recording_lines = false;
21338 /* Note: m_currently_recording_lines is left as false until we see
21339 DW_LNE_end_sequence. */
924c2928
DE
21340 }
21341}
21342
f3f5162e 21343/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21344 Process the line number information in LH.
21345 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21346 program in order to set included_p for every referenced header. */
debd256d 21347
c906108c 21348static void
43f3e411
DE
21349dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21350 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21351{
d521ce57
TT
21352 const gdb_byte *line_ptr, *extended_end;
21353 const gdb_byte *line_end;
a8c50c1f 21354 unsigned int bytes_read, extended_len;
699ca60a 21355 unsigned char op_code, extended_op;
e142c38c 21356 CORE_ADDR baseaddr;
5e22e966 21357 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21358 bfd *abfd = objfile->obfd;
08feed99 21359 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21360 /* True if we're recording line info (as opposed to building partial
21361 symtabs and just interested in finding include files mentioned by
21362 the line number program). */
21363 bool record_lines_p = !decode_for_pst_p;
e142c38c 21364
b3b3bada 21365 baseaddr = objfile->text_section_offset ();
c906108c 21366
debd256d
JB
21367 line_ptr = lh->statement_program_start;
21368 line_end = lh->statement_program_end;
c906108c
SS
21369
21370 /* Read the statement sequences until there's nothing left. */
21371 while (line_ptr < line_end)
21372 {
6f77053d
PA
21373 /* The DWARF line number program state machine. Reset the state
21374 machine at the start of each sequence. */
804d2729 21375 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21376 bool end_sequence = false;
d9b3de22 21377
8c43009f 21378 if (record_lines_p)
c906108c 21379 {
8c43009f
PA
21380 /* Start a subfile for the current file of the state
21381 machine. */
21382 const file_entry *fe = state_machine.current_file ();
21383
21384 if (fe != NULL)
804d2729 21385 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21386 }
21387
a738430d 21388 /* Decode the table. */
d9b3de22 21389 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21390 {
21391 op_code = read_1_byte (abfd, line_ptr);
21392 line_ptr += 1;
9aa1fe7e 21393
debd256d 21394 if (op_code >= lh->opcode_base)
6e70227d 21395 {
8e07a239 21396 /* Special opcode. */
6f77053d 21397 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21398 }
21399 else switch (op_code)
c906108c
SS
21400 {
21401 case DW_LNS_extended_op:
3e43a32a
MS
21402 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21403 &bytes_read);
473b7be6 21404 line_ptr += bytes_read;
a8c50c1f 21405 extended_end = line_ptr + extended_len;
c906108c
SS
21406 extended_op = read_1_byte (abfd, line_ptr);
21407 line_ptr += 1;
8f34b746
TV
21408 if (DW_LNE_lo_user <= extended_op
21409 && extended_op <= DW_LNE_hi_user)
21410 {
21411 /* Vendor extension, ignore. */
21412 line_ptr = extended_end;
21413 break;
21414 }
c906108c
SS
21415 switch (extended_op)
21416 {
21417 case DW_LNE_end_sequence:
6f77053d
PA
21418 state_machine.handle_end_sequence ();
21419 end_sequence = true;
c906108c
SS
21420 break;
21421 case DW_LNE_set_address:
d9b3de22
DE
21422 {
21423 CORE_ADDR address
c8a7a66f 21424 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21425 line_ptr += bytes_read;
6f77053d
PA
21426
21427 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21428 lowpc - baseaddr, address);
6f77053d 21429 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21430 }
c906108c
SS
21431 break;
21432 case DW_LNE_define_file:
dda83cd7
SM
21433 {
21434 const char *cur_file;
ecfb656c
PA
21435 unsigned int mod_time, length;
21436 dir_index dindex;
6e70227d 21437
dda83cd7 21438 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21439 &bytes_read);
dda83cd7
SM
21440 line_ptr += bytes_read;
21441 dindex = (dir_index)
21442 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21443 line_ptr += bytes_read;
21444 mod_time =
21445 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21446 line_ptr += bytes_read;
21447 length =
21448 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21449 line_ptr += bytes_read;
21450 lh->add_file_name (cur_file, dindex, mod_time, length);
21451 }
c906108c 21452 break;
d0c6ba3d 21453 case DW_LNE_set_discriminator:
6f77053d
PA
21454 {
21455 /* The discriminator is not interesting to the
21456 debugger; just ignore it. We still need to
21457 check its value though:
21458 if there are consecutive entries for the same
21459 (non-prologue) line we want to coalesce them.
21460 PR 17276. */
21461 unsigned int discr
21462 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21463 line_ptr += bytes_read;
21464
21465 state_machine.handle_set_discriminator (discr);
21466 }
d0c6ba3d 21467 break;
c906108c 21468 default:
b98664d3 21469 complaint (_("mangled .debug_line section"));
debd256d 21470 return;
c906108c 21471 }
a8c50c1f
DJ
21472 /* Make sure that we parsed the extended op correctly. If e.g.
21473 we expected a different address size than the producer used,
21474 we may have read the wrong number of bytes. */
21475 if (line_ptr != extended_end)
21476 {
b98664d3 21477 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21478 return;
21479 }
c906108c
SS
21480 break;
21481 case DW_LNS_copy:
6f77053d 21482 state_machine.handle_copy ();
c906108c
SS
21483 break;
21484 case DW_LNS_advance_pc:
2dc7f7b3
TT
21485 {
21486 CORE_ADDR adjust
21487 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21488 line_ptr += bytes_read;
6f77053d
PA
21489
21490 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21491 }
c906108c
SS
21492 break;
21493 case DW_LNS_advance_line:
a05a36a5
DE
21494 {
21495 int line_delta
21496 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21497 line_ptr += bytes_read;
6f77053d
PA
21498
21499 state_machine.handle_advance_line (line_delta);
a05a36a5 21500 }
c906108c
SS
21501 break;
21502 case DW_LNS_set_file:
d9b3de22 21503 {
6f77053d 21504 file_name_index file
ecfb656c
PA
21505 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21506 &bytes_read);
d9b3de22 21507 line_ptr += bytes_read;
8c43009f 21508
6f77053d 21509 state_machine.handle_set_file (file);
d9b3de22 21510 }
c906108c
SS
21511 break;
21512 case DW_LNS_set_column:
0ad93d4f 21513 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21514 line_ptr += bytes_read;
21515 break;
21516 case DW_LNS_negate_stmt:
6f77053d 21517 state_machine.handle_negate_stmt ();
c906108c
SS
21518 break;
21519 case DW_LNS_set_basic_block:
c906108c 21520 break;
c2c6d25f
JM
21521 /* Add to the address register of the state machine the
21522 address increment value corresponding to special opcode
a738430d
MK
21523 255. I.e., this value is scaled by the minimum
21524 instruction length since special opcode 255 would have
b021a221 21525 scaled the increment. */
c906108c 21526 case DW_LNS_const_add_pc:
6f77053d 21527 state_machine.handle_const_add_pc ();
c906108c
SS
21528 break;
21529 case DW_LNS_fixed_advance_pc:
3e29f34a 21530 {
6f77053d 21531 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21532 line_ptr += 2;
6f77053d
PA
21533
21534 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21535 }
c906108c 21536 break;
9aa1fe7e 21537 default:
a738430d
MK
21538 {
21539 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21540 int i;
a738430d 21541
debd256d 21542 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21543 {
21544 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21545 line_ptr += bytes_read;
21546 }
21547 }
c906108c
SS
21548 }
21549 }
d9b3de22
DE
21550
21551 if (!end_sequence)
21552 dwarf2_debug_line_missing_end_sequence_complaint ();
21553
21554 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21555 in which case we still finish recording the last line). */
6f77053d 21556 state_machine.record_line (true);
c906108c 21557 }
f3f5162e
DE
21558}
21559
21560/* Decode the Line Number Program (LNP) for the given line_header
21561 structure and CU. The actual information extracted and the type
21562 of structures created from the LNP depends on the value of PST.
21563
21564 1. If PST is NULL, then this procedure uses the data from the program
21565 to create all necessary symbol tables, and their linetables.
21566
21567 2. If PST is not NULL, this procedure reads the program to determine
21568 the list of files included by the unit represented by PST, and
21569 builds all the associated partial symbol tables.
21570
21571 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21572 It is used for relative paths in the line table.
21573 NOTE: When processing partial symtabs (pst != NULL),
21574 comp_dir == pst->dirname.
21575
21576 NOTE: It is important that psymtabs have the same file name (via strcmp)
21577 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21578 symtab we don't use it in the name of the psymtabs we create.
21579 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21580 A good testcase for this is mb-inline.exp.
21581
527f3840
JK
21582 LOWPC is the lowest address in CU (or 0 if not known).
21583
21584 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21585 for its PC<->lines mapping information. Otherwise only the filename
21586 table is read in. */
f3f5162e
DE
21587
21588static void
21589dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21590 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21591 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21592{
5e22e966 21593 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21594 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21595
527f3840
JK
21596 if (decode_mapping)
21597 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21598
21599 if (decode_for_pst_p)
21600 {
aaa75496 21601 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21602 create the psymtab of each included file. */
7ba99d21 21603 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21604 if (file_entry.included_p == 1)
21605 {
c89b44cd 21606 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21607 const char *include_name =
7ba99d21
AT
21608 psymtab_include_file_name (lh, file_entry, pst,
21609 comp_dir, &name_holder);
c6da4cef 21610 if (include_name != NULL)
7b249e47
TT
21611 dwarf2_create_include_psymtab
21612 (cu->per_objfile->per_bfd, include_name, pst,
21613 cu->per_objfile->per_bfd->partial_symtabs.get (),
0072c873 21614 objfile->per_bfd);
dda83cd7 21615 }
aaa75496 21616 }
cb1df416
DJ
21617 else
21618 {
21619 /* Make sure a symtab is created for every file, even files
21620 which contain only variables (i.e. no code with associated
21621 line numbers). */
c24bdb02
KS
21622 buildsym_compunit *builder = cu->get_builder ();
21623 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21624
7ba99d21 21625 for (auto &fe : lh->file_names ())
cb1df416 21626 {
804d2729 21627 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21628 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21629 {
c24bdb02 21630 builder->get_current_subfile ()->symtab
804d2729 21631 = allocate_symtab (cust,
c24bdb02 21632 builder->get_current_subfile ()->name);
43f3e411 21633 }
c24bdb02 21634 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21635 }
21636 }
c906108c
SS
21637}
21638
21639/* Start a subfile for DWARF. FILENAME is the name of the file and
21640 DIRNAME the name of the source directory which contains FILENAME
4d663531 21641 or NULL if not known.
c906108c
SS
21642 This routine tries to keep line numbers from identical absolute and
21643 relative file names in a common subfile.
21644
21645 Using the `list' example from the GDB testsuite, which resides in
21646 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21647 of /srcdir/list0.c yields the following debugging information for list0.c:
21648
c5aa993b 21649 DW_AT_name: /srcdir/list0.c
4d663531 21650 DW_AT_comp_dir: /compdir
357e46e7 21651 files.files[0].name: list0.h
c5aa993b 21652 files.files[0].dir: /srcdir
357e46e7 21653 files.files[1].name: list0.c
c5aa993b 21654 files.files[1].dir: /srcdir
c906108c
SS
21655
21656 The line number information for list0.c has to end up in a single
4f1520fb
FR
21657 subfile, so that `break /srcdir/list0.c:1' works as expected.
21658 start_subfile will ensure that this happens provided that we pass the
21659 concatenation of files.files[1].dir and files.files[1].name as the
21660 subfile's name. */
c906108c
SS
21661
21662static void
804d2729
TT
21663dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21664 const char *dirname)
c906108c 21665{
43816ebc 21666 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21667
4d663531 21668 /* In order not to lose the line information directory,
4f1520fb
FR
21669 we concatenate it to the filename when it makes sense.
21670 Note that the Dwarf3 standard says (speaking of filenames in line
21671 information): ``The directory index is ignored for file names
21672 that represent full path names''. Thus ignoring dirname in the
21673 `else' branch below isn't an issue. */
c906108c 21674
d5166ae1 21675 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21676 {
43816ebc
TT
21677 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21678 filename = copy.get ();
d521ce57 21679 }
c906108c 21680
c24bdb02 21681 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21682}
21683
804d2729
TT
21684/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21685 buildsym_compunit constructor. */
f4dc4d17 21686
c24bdb02
KS
21687struct compunit_symtab *
21688dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21689 CORE_ADDR low_pc)
f4dc4d17 21690{
c24bdb02 21691 gdb_assert (m_builder == nullptr);
43f3e411 21692
c24bdb02 21693 m_builder.reset (new struct buildsym_compunit
f6e649dd 21694 (this->per_objfile->objfile,
c24bdb02 21695 name, comp_dir, language, low_pc));
93b8bea4 21696
c24bdb02 21697 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21698
c24bdb02
KS
21699 get_builder ()->record_debugformat ("DWARF 2");
21700 get_builder ()->record_producer (producer);
f4dc4d17 21701
c24bdb02 21702 processing_has_namespace_info = false;
43f3e411 21703
c24bdb02 21704 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21705}
21706
4c2df51b
DJ
21707static void
21708var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21709 struct dwarf2_cu *cu)
4c2df51b 21710{
5e22e966 21711 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21712 struct comp_unit_head *cu_header = &cu->header;
21713
4c2df51b
DJ
21714 /* NOTE drow/2003-01-30: There used to be a comment and some special
21715 code here to turn a symbol with DW_AT_external and a
21716 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21717 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21718 with some versions of binutils) where shared libraries could have
21719 relocations against symbols in their debug information - the
21720 minimal symbol would have the right address, but the debug info
21721 would not. It's no longer necessary, because we will explicitly
21722 apply relocations when we read in the debug information now. */
21723
21724 /* A DW_AT_location attribute with no contents indicates that a
21725 variable has been optimized away. */
9d2246fc 21726 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21727 {
f1e6e072 21728 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21729 return;
21730 }
21731
21732 /* Handle one degenerate form of location expression specially, to
21733 preserve GDB's previous behavior when section offsets are
336d760d
AT
21734 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21735 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21736
9d2246fc
TT
21737 if (attr->form_is_block ())
21738 {
21739 struct dwarf_block *block = attr->as_block ();
21740
21741 if ((block->data[0] == DW_OP_addr
21742 && block->size == 1 + cu_header->addr_size)
21743 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 21744 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
21745 && (block->size
21746 == 1 + leb128_size (&block->data[1]))))
21747 {
21748 unsigned int dummy;
21749
21750 if (block->data[0] == DW_OP_addr)
21751 SET_SYMBOL_VALUE_ADDRESS
21752 (sym, cu->header.read_address (objfile->obfd,
21753 block->data + 1,
38583298 21754 &dummy));
9d2246fc
TT
21755 else
21756 SET_SYMBOL_VALUE_ADDRESS
21757 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21758 &dummy));
21759 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21760 fixup_symbol_section (sym, objfile);
21761 SET_SYMBOL_VALUE_ADDRESS
21762 (sym,
21763 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 21764 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
21765 return;
21766 }
4c2df51b
DJ
21767 }
21768
21769 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21770 expression evaluator, and use LOC_COMPUTED only when necessary
21771 (i.e. when the value of a register or memory location is
21772 referenced, or a thread-local block, etc.). Then again, it might
21773 not be worthwhile. I'm assuming that it isn't unless performance
21774 or memory numbers show me otherwise. */
21775
f1e6e072 21776 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21777
f1e6e072 21778 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21779 cu->has_loclist = true;
4c2df51b
DJ
21780}
21781
c906108c
SS
21782/* Given a pointer to a DWARF information entry, figure out if we need
21783 to make a symbol table entry for it, and if so, create a new entry
21784 and return a pointer to it.
21785 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21786 used the passed type.
21787 If SPACE is not NULL, use it to hold the new symbol. If it is
21788 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21789
21790static struct symbol *
5e2db402
TT
21791new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21792 struct symbol *space)
c906108c 21793{
976ca316
SM
21794 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21795 struct objfile *objfile = per_objfile->objfile;
08feed99 21796 struct gdbarch *gdbarch = objfile->arch ();
c906108c 21797 struct symbol *sym = NULL;
15d034d0 21798 const char *name;
c906108c
SS
21799 struct attribute *attr = NULL;
21800 struct attribute *attr2 = NULL;
e142c38c 21801 CORE_ADDR baseaddr;
e37fd15a
SW
21802 struct pending **list_to_add = NULL;
21803
edb3359d 21804 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21805
b3b3bada 21806 baseaddr = objfile->text_section_offset ();
c906108c 21807
94af9270 21808 name = dwarf2_name (die, cu);
c906108c
SS
21809 if (name)
21810 {
34eaf542 21811 int suppress_add = 0;
94af9270 21812
34eaf542
TT
21813 if (space)
21814 sym = space;
21815 else
8c14c3a3 21816 sym = new (&objfile->objfile_obstack) symbol;
c906108c 21817 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21818
21819 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21820 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
21821 /* Fortran does not have mangling standard and the mangling does differ
21822 between gfortran, iFort etc. */
bcfe6157
TT
21823 const char *physname
21824 = (cu->language == language_fortran
21825 ? dwarf2_full_name (name, die, cu)
21826 : dwarf2_physname (name, die, cu));
21827 const char *linkagename = dw2_linkage_name (die, cu);
21828
21829 if (linkagename == nullptr || cu->language == language_ada)
21830 sym->set_linkage_name (physname);
21831 else
21832 {
21833 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21834 sym->set_linkage_name (linkagename);
21835 }
f55ee35c 21836
c906108c 21837 /* Default assumptions.
dda83cd7 21838 Use the passed type or decode it from the die. */
176620f1 21839 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21840 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21841 if (type != NULL)
21842 SYMBOL_TYPE (sym) = type;
21843 else
e7c27a73 21844 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21845 attr = dwarf2_attr (die,
21846 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21847 cu);
435d3d88 21848 if (attr != nullptr)
529908cb 21849 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 21850
edb3359d
DJ
21851 attr = dwarf2_attr (die,
21852 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21853 cu);
aa659cfa 21854 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 21855 {
529908cb 21856 file_name_index file_index
aa659cfa 21857 = (file_name_index) attr->as_nonnegative ();
8c43009f 21858 struct file_entry *fe;
9a619af0 21859
ecfb656c
PA
21860 if (cu->line_header != NULL)
21861 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21862 else
21863 fe = NULL;
21864
21865 if (fe == NULL)
b98664d3 21866 complaint (_("file index out of range"));
8c43009f
PA
21867 else
21868 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21869 }
21870
c906108c
SS
21871 switch (die->tag)
21872 {
21873 case DW_TAG_label:
e142c38c 21874 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21875 if (attr != nullptr)
3e29f34a
MR
21876 {
21877 CORE_ADDR addr;
21878
95f982e5 21879 addr = attr->as_address ();
3e29f34a 21880 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21881 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 21882 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 21883 }
8f5c6526
TV
21884 else
21885 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
21886 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21887 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 21888 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21889 break;
21890 case DW_TAG_subprogram:
21891 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21892 finish_block. */
f1e6e072 21893 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21894 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 21895 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
21896 || cu->language == language_ada
21897 || cu->language == language_fortran)
c906108c 21898 {
dda83cd7
SM
21899 /* Subprograms marked external are stored as a global symbol.
21900 Ada and Fortran subprograms, whether marked external or
21901 not, are always stored as a global symbol, because we want
21902 to be able to access them globally. For instance, we want
21903 to be able to break on a nested subprogram without having
21904 to specify the context. */
c24bdb02 21905 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21906 }
21907 else
21908 {
e37fd15a 21909 list_to_add = cu->list_in_scope;
c906108c
SS
21910 }
21911 break;
edb3359d
DJ
21912 case DW_TAG_inlined_subroutine:
21913 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21914 finish_block. */
f1e6e072 21915 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21916 SYMBOL_INLINED (sym) = 1;
481860b3 21917 list_to_add = cu->list_in_scope;
edb3359d 21918 break;
34eaf542
TT
21919 case DW_TAG_template_value_param:
21920 suppress_add = 1;
21921 /* Fall through. */
72929c62 21922 case DW_TAG_constant:
c906108c 21923 case DW_TAG_variable:
254e6b9e 21924 case DW_TAG_member:
0963b4bd
MS
21925 /* Compilation with minimal debug info may result in
21926 variables with missing type entries. Change the
21927 misleading `void' type to something sensible. */
78134374 21928 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 21929 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21930
e142c38c 21931 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21932 /* In the case of DW_TAG_member, we should only be called for
21933 static const members. */
21934 if (die->tag == DW_TAG_member)
21935 {
3863f96c
DE
21936 /* dwarf2_add_field uses die_is_declaration,
21937 so we do the same. */
254e6b9e
DE
21938 gdb_assert (die_is_declaration (die, cu));
21939 gdb_assert (attr);
21940 }
435d3d88 21941 if (attr != nullptr)
c906108c 21942 {
e7c27a73 21943 dwarf2_const_value (attr, sym, cu);
e142c38c 21944 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21945 if (!suppress_add)
34eaf542 21946 {
c45bc3f8 21947 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 21948 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21949 else
e37fd15a 21950 list_to_add = cu->list_in_scope;
34eaf542 21951 }
c906108c
SS
21952 break;
21953 }
e142c38c 21954 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21955 if (attr != nullptr)
c906108c 21956 {
e7c27a73 21957 var_decode_location (attr, sym, cu);
e142c38c 21958 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21959
21960 /* Fortran explicitly imports any global symbols to the local
21961 scope by DW_TAG_common_block. */
21962 if (cu->language == language_fortran && die->parent
21963 && die->parent->tag == DW_TAG_common_block)
21964 attr2 = NULL;
21965
caac4577
JG
21966 if (SYMBOL_CLASS (sym) == LOC_STATIC
21967 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 21968 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
21969 {
21970 /* When a static variable is eliminated by the linker,
21971 the corresponding debug information is not stripped
21972 out, but the variable address is set to null;
21973 do not add such variables into symbol table. */
21974 }
c45bc3f8 21975 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 21976 {
4b610737
TT
21977 if (SYMBOL_CLASS (sym) == LOC_STATIC
21978 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 21979 && per_objfile->per_bfd->can_copy)
4b610737
TT
21980 {
21981 /* A global static variable might be subject to
21982 copy relocation. We first check for a local
21983 minsym, though, because maybe the symbol was
21984 marked hidden, in which case this would not
21985 apply. */
21986 bound_minimal_symbol found
21987 = (lookup_minimal_symbol_linkage
987012b8 21988 (sym->linkage_name (), objfile));
4b610737
TT
21989 if (found.minsym != nullptr)
21990 sym->maybe_copied = 1;
21991 }
f55ee35c 21992
1c809c68
TT
21993 /* A variable with DW_AT_external is never static,
21994 but it may be block-scoped. */
804d2729 21995 list_to_add
c24bdb02
KS
21996 = ((cu->list_in_scope
21997 == cu->get_builder ()->get_file_symbols ())
21998 ? cu->get_builder ()->get_global_symbols ()
804d2729 21999 : cu->list_in_scope);
1c809c68 22000 }
c906108c 22001 else
e37fd15a 22002 list_to_add = cu->list_in_scope;
c906108c
SS
22003 }
22004 else
22005 {
22006 /* We do not know the address of this symbol.
dda83cd7
SM
22007 If it is an external symbol and we have type information
22008 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22009 The address of the variable will then be determined from
22010 the minimal symbol table whenever the variable is
22011 referenced. */
e142c38c 22012 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22013
22014 /* Fortran explicitly imports any global symbols to the local
22015 scope by DW_TAG_common_block. */
22016 if (cu->language == language_fortran && die->parent
22017 && die->parent->tag == DW_TAG_common_block)
22018 {
22019 /* SYMBOL_CLASS doesn't matter here because
22020 read_common_block is going to reset it. */
22021 if (!suppress_add)
22022 list_to_add = cu->list_in_scope;
22023 }
c45bc3f8 22024 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22025 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22026 {
0fe7935b
DJ
22027 /* A variable with DW_AT_external is never static, but it
22028 may be block-scoped. */
804d2729 22029 list_to_add
c24bdb02
KS
22030 = ((cu->list_in_scope
22031 == cu->get_builder ()->get_file_symbols ())
22032 ? cu->get_builder ()->get_global_symbols ()
804d2729 22033 : cu->list_in_scope);
0fe7935b 22034
f1e6e072 22035 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22036 }
442ddf59
JK
22037 else if (!die_is_declaration (die, cu))
22038 {
22039 /* Use the default LOC_OPTIMIZED_OUT class. */
22040 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22041 if (!suppress_add)
22042 list_to_add = cu->list_in_scope;
442ddf59 22043 }
c906108c
SS
22044 }
22045 break;
22046 case DW_TAG_formal_parameter:
a60f3166 22047 {
3db19b2d
TV
22048 /* If we are inside a function, mark this as an argument. If
22049 not, we might be looking at an argument to an inlined function
22050 when we do not have enough information to show inlined frames;
22051 pretend it's a local variable in that case so that the user can
22052 still see it. */
22053 struct context_stack *curr
22054 = cu->get_builder ()->get_current_context_stack ();
22055 if (curr != nullptr && curr->name != nullptr)
22056 SYMBOL_IS_ARGUMENT (sym) = 1;
a60f3166 22057 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22058 if (attr != nullptr)
a60f3166
TT
22059 {
22060 var_decode_location (attr, sym, cu);
22061 }
22062 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22063 if (attr != nullptr)
a60f3166
TT
22064 {
22065 dwarf2_const_value (attr, sym, cu);
22066 }
f346a30d 22067
a60f3166
TT
22068 list_to_add = cu->list_in_scope;
22069 }
c906108c
SS
22070 break;
22071 case DW_TAG_unspecified_parameters:
22072 /* From varargs functions; gdb doesn't seem to have any
22073 interest in this information, so just ignore it for now.
22074 (FIXME?) */
22075 break;
34eaf542
TT
22076 case DW_TAG_template_type_param:
22077 suppress_add = 1;
22078 /* Fall through. */
c906108c 22079 case DW_TAG_class_type:
680b30c7 22080 case DW_TAG_interface_type:
c906108c
SS
22081 case DW_TAG_structure_type:
22082 case DW_TAG_union_type:
72019c9c 22083 case DW_TAG_set_type:
c906108c 22084 case DW_TAG_enumeration_type:
f1e6e072 22085 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22086 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22087
63d06c5c 22088 {
9c37b5ae 22089 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22090 really ever be static objects: otherwise, if you try
22091 to, say, break of a class's method and you're in a file
22092 which doesn't mention that class, it won't work unless
22093 the check for all static symbols in lookup_symbol_aux
22094 saves you. See the OtherFileClass tests in
22095 gdb.c++/namespace.exp. */
22096
e37fd15a 22097 if (!suppress_add)
34eaf542 22098 {
c24bdb02 22099 buildsym_compunit *builder = cu->get_builder ();
804d2729 22100 list_to_add
c24bdb02 22101 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22102 && cu->language == language_cplus
c24bdb02 22103 ? builder->get_global_symbols ()
804d2729 22104 : cu->list_in_scope);
63d06c5c 22105
64382290 22106 /* The semantics of C++ state that "struct foo {
9c37b5ae 22107 ... }" also defines a typedef for "foo". */
64382290 22108 if (cu->language == language_cplus
45280282 22109 || cu->language == language_ada
c44af4eb
TT
22110 || cu->language == language_d
22111 || cu->language == language_rust)
64382290
TT
22112 {
22113 /* The symbol's name is already allocated along
22114 with this objfile, so we don't need to
22115 duplicate it for the type. */
7d93a1e0 22116 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22117 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22118 }
63d06c5c
DC
22119 }
22120 }
c906108c
SS
22121 break;
22122 case DW_TAG_typedef:
f1e6e072 22123 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22124 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22125 list_to_add = cu->list_in_scope;
63d06c5c 22126 break;
d8f62e84 22127 case DW_TAG_array_type:
c906108c 22128 case DW_TAG_base_type:
dda83cd7 22129 case DW_TAG_subrange_type:
f1e6e072 22130 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22131 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22132 list_to_add = cu->list_in_scope;
c906108c
SS
22133 break;
22134 case DW_TAG_enumerator:
e142c38c 22135 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22136 if (attr != nullptr)
c906108c 22137 {
e7c27a73 22138 dwarf2_const_value (attr, sym, cu);
c906108c 22139 }
63d06c5c
DC
22140 {
22141 /* NOTE: carlton/2003-11-10: See comment above in the
22142 DW_TAG_class_type, etc. block. */
22143
804d2729 22144 list_to_add
c24bdb02 22145 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22146 && cu->language == language_cplus
c24bdb02 22147 ? cu->get_builder ()->get_global_symbols ()
804d2729 22148 : cu->list_in_scope);
63d06c5c 22149 }
c906108c 22150 break;
74921315 22151 case DW_TAG_imported_declaration:
5c4e30ca 22152 case DW_TAG_namespace:
f1e6e072 22153 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22154 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22155 break;
530e8392
KB
22156 case DW_TAG_module:
22157 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22158 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22159 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22160 break;
4357ac6c 22161 case DW_TAG_common_block:
f1e6e072 22162 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22163 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22164 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22165 break;
c906108c
SS
22166 default:
22167 /* Not a tag we recognize. Hopefully we aren't processing
22168 trash data, but since we must specifically ignore things
22169 we don't recognize, there is nothing else we should do at
0963b4bd 22170 this point. */
b98664d3 22171 complaint (_("unsupported tag: '%s'"),
4d3c2250 22172 dwarf_tag_name (die->tag));
c906108c
SS
22173 break;
22174 }
df8a16a1 22175
e37fd15a
SW
22176 if (suppress_add)
22177 {
22178 sym->hash_next = objfile->template_symbols;
22179 objfile->template_symbols = sym;
22180 list_to_add = NULL;
22181 }
22182
22183 if (list_to_add != NULL)
d3cb6808 22184 add_symbol_to_list (sym, list_to_add);
e37fd15a 22185
df8a16a1
DJ
22186 /* For the benefit of old versions of GCC, check for anonymous
22187 namespaces based on the demangled name. */
4d4ec4e5 22188 if (!cu->processing_has_namespace_info
94af9270 22189 && cu->language == language_cplus)
c24bdb02 22190 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22191 }
22192 return (sym);
22193}
22194
98bfdba5
PA
22195/* Given an attr with a DW_FORM_dataN value in host byte order,
22196 zero-extend it as appropriate for the symbol's type. The DWARF
22197 standard (v4) is not entirely clear about the meaning of using
22198 DW_FORM_dataN for a constant with a signed type, where the type is
22199 wider than the data. The conclusion of a discussion on the DWARF
22200 list was that this is unspecified. We choose to always zero-extend
22201 because that is the interpretation long in use by GCC. */
c906108c 22202
98bfdba5 22203static gdb_byte *
ff39bb5e 22204dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22205 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22206{
5e22e966 22207 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22208 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22209 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22210 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22211
22212 if (bits < sizeof (*value) * 8)
22213 {
22214 l &= ((LONGEST) 1 << bits) - 1;
22215 *value = l;
22216 }
22217 else if (bits == sizeof (*value) * 8)
22218 *value = l;
22219 else
22220 {
224c3ddb 22221 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22222 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22223 return bytes;
22224 }
22225
22226 return NULL;
22227}
22228
22229/* Read a constant value from an attribute. Either set *VALUE, or if
22230 the value does not fit in *VALUE, set *BYTES - either already
22231 allocated on the objfile obstack, or newly allocated on OBSTACK,
22232 or, set *BATON, if we translated the constant to a location
22233 expression. */
22234
22235static void
ff39bb5e 22236dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22237 const char *name, struct obstack *obstack,
22238 struct dwarf2_cu *cu,
d521ce57 22239 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22240 struct dwarf2_locexpr_baton **baton)
22241{
5e22e966 22242 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22243 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22244 struct comp_unit_head *cu_header = &cu->header;
c906108c 22245 struct dwarf_block *blk;
98bfdba5
PA
22246 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22247 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22248
22249 *value = 0;
22250 *bytes = NULL;
22251 *baton = NULL;
c906108c
SS
22252
22253 switch (attr->form)
22254 {
22255 case DW_FORM_addr:
336d760d 22256 case DW_FORM_addrx:
3019eac3 22257 case DW_FORM_GNU_addr_index:
ac56253d 22258 {
ac56253d
TT
22259 gdb_byte *data;
22260
98bfdba5
PA
22261 if (TYPE_LENGTH (type) != cu_header->addr_size)
22262 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22263 cu_header->addr_size,
98bfdba5 22264 TYPE_LENGTH (type));
ac56253d
TT
22265 /* Symbols of this form are reasonably rare, so we just
22266 piggyback on the existing location code rather than writing
22267 a new implementation of symbol_computed_ops. */
8d749320 22268 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22269 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22270 (*baton)->per_cu = cu->per_cu;
22271 gdb_assert ((*baton)->per_cu);
ac56253d 22272
98bfdba5 22273 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22274 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22275 (*baton)->data = data;
ac56253d
TT
22276
22277 data[0] = DW_OP_addr;
22278 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22279 byte_order, attr->as_address ());
ac56253d 22280 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22281 }
c906108c 22282 break;
4ac36638 22283 case DW_FORM_string:
93b5768b 22284 case DW_FORM_strp:
cf532bd1 22285 case DW_FORM_strx:
3019eac3 22286 case DW_FORM_GNU_str_index:
36586728 22287 case DW_FORM_GNU_strp_alt:
c6481205 22288 /* The string is already allocated on the objfile obstack, point
98bfdba5 22289 directly to it. */
2c830f54 22290 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22291 break;
c906108c
SS
22292 case DW_FORM_block1:
22293 case DW_FORM_block2:
22294 case DW_FORM_block4:
22295 case DW_FORM_block:
2dc7f7b3 22296 case DW_FORM_exprloc:
0224619f 22297 case DW_FORM_data16:
9d2246fc 22298 blk = attr->as_block ();
98bfdba5
PA
22299 if (TYPE_LENGTH (type) != blk->size)
22300 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22301 TYPE_LENGTH (type));
22302 *bytes = blk->data;
c906108c 22303 break;
2df3850c
JM
22304
22305 /* The DW_AT_const_value attributes are supposed to carry the
22306 symbol's value "represented as it would be on the target
22307 architecture." By the time we get here, it's already been
22308 converted to host endianness, so we just need to sign- or
22309 zero-extend it as appropriate. */
22310 case DW_FORM_data1:
3aef2284 22311 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22312 break;
c906108c 22313 case DW_FORM_data2:
3aef2284 22314 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22315 break;
c906108c 22316 case DW_FORM_data4:
3aef2284 22317 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22318 break;
c906108c 22319 case DW_FORM_data8:
3aef2284 22320 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22321 break;
22322
c906108c 22323 case DW_FORM_sdata:
663c44ac 22324 case DW_FORM_implicit_const:
1bc397c5 22325 *value = attr->as_signed ();
2df3850c
JM
22326 break;
22327
c906108c 22328 case DW_FORM_udata:
529908cb 22329 *value = attr->as_unsigned ();
c906108c 22330 break;
2df3850c 22331
c906108c 22332 default:
b98664d3 22333 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22334 dwarf_form_name (attr->form));
98bfdba5 22335 *value = 0;
c906108c
SS
22336 break;
22337 }
22338}
22339
2df3850c 22340
98bfdba5
PA
22341/* Copy constant value from an attribute to a symbol. */
22342
2df3850c 22343static void
ff39bb5e 22344dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22345 struct dwarf2_cu *cu)
2df3850c 22346{
5e22e966 22347 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22348 LONGEST value;
d521ce57 22349 const gdb_byte *bytes;
98bfdba5 22350 struct dwarf2_locexpr_baton *baton;
2df3850c 22351
98bfdba5 22352 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22353 sym->print_name (),
98bfdba5
PA
22354 &objfile->objfile_obstack, cu,
22355 &value, &bytes, &baton);
2df3850c 22356
98bfdba5
PA
22357 if (baton != NULL)
22358 {
98bfdba5 22359 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22360 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22361 }
22362 else if (bytes != NULL)
22363 {
22364 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22365 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22366 }
22367 else
22368 {
22369 SYMBOL_VALUE (sym) = value;
f1e6e072 22370 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22371 }
2df3850c
JM
22372}
22373
c906108c
SS
22374/* Return the type of the die in question using its DW_AT_type attribute. */
22375
22376static struct type *
e7c27a73 22377die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22378{
c906108c 22379 struct attribute *type_attr;
c906108c 22380
e142c38c 22381 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22382 if (!type_attr)
22383 {
5e22e966 22384 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22385 /* A missing DW_AT_type represents a void type. */
518817b3 22386 return objfile_type (objfile)->builtin_void;
c906108c 22387 }
348e048f 22388
673bfd45 22389 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22390}
22391
b4ba55a1
JB
22392/* True iff CU's producer generates GNAT Ada auxiliary information
22393 that allows to find parallel types through that information instead
22394 of having to do expensive parallel lookups by type name. */
22395
22396static int
22397need_gnat_info (struct dwarf2_cu *cu)
22398{
de4cb04a
JB
22399 /* Assume that the Ada compiler was GNAT, which always produces
22400 the auxiliary information. */
22401 return (cu->language == language_ada);
b4ba55a1
JB
22402}
22403
b4ba55a1
JB
22404/* Return the auxiliary type of the die in question using its
22405 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22406 attribute is not present. */
22407
22408static struct type *
22409die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22410{
b4ba55a1 22411 struct attribute *type_attr;
b4ba55a1
JB
22412
22413 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22414 if (!type_attr)
22415 return NULL;
22416
673bfd45 22417 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22418}
22419
22420/* If DIE has a descriptive_type attribute, then set the TYPE's
22421 descriptive type accordingly. */
22422
22423static void
22424set_descriptive_type (struct type *type, struct die_info *die,
22425 struct dwarf2_cu *cu)
22426{
22427 struct type *descriptive_type = die_descriptive_type (die, cu);
22428
22429 if (descriptive_type)
22430 {
22431 ALLOCATE_GNAT_AUX_TYPE (type);
22432 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22433 }
22434}
22435
c906108c
SS
22436/* Return the containing type of the die in question using its
22437 DW_AT_containing_type attribute. */
22438
22439static struct type *
e7c27a73 22440die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22441{
c906108c 22442 struct attribute *type_attr;
5e22e966 22443 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22444
e142c38c 22445 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22446 if (!type_attr)
22447 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22448 "[in module %s]"), objfile_name (objfile));
33ac96f0 22449
673bfd45 22450 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22451}
22452
ac9ec31b
DE
22453/* Return an error marker type to use for the ill formed type in DIE/CU. */
22454
22455static struct type *
22456build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22457{
976ca316
SM
22458 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22459 struct objfile *objfile = per_objfile->objfile;
528e1572 22460 char *saved;
ac9ec31b 22461
528e1572
SM
22462 std::string message
22463 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22464 objfile_name (objfile),
22465 sect_offset_str (cu->header.sect_off),
22466 sect_offset_str (die->sect_off));
efba19b0 22467 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22468
19f392bc 22469 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22470}
22471
673bfd45 22472/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22473 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22474 DW_AT_containing_type.
673bfd45
DE
22475 If there is no type substitute an error marker. */
22476
c906108c 22477static struct type *
ff39bb5e 22478lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22479 struct dwarf2_cu *cu)
c906108c 22480{
976ca316
SM
22481 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22482 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22483 struct type *this_type;
22484
ac9ec31b
DE
22485 gdb_assert (attr->name == DW_AT_type
22486 || attr->name == DW_AT_GNAT_descriptive_type
22487 || attr->name == DW_AT_containing_type);
22488
673bfd45
DE
22489 /* First see if we have it cached. */
22490
36586728
TT
22491 if (attr->form == DW_FORM_GNU_ref_alt)
22492 {
22493 struct dwarf2_per_cu_data *per_cu;
0826b30a 22494 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22495
976ca316
SM
22496 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22497 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22498 }
cd6c91b4 22499 else if (attr->form_is_ref ())
673bfd45 22500 {
0826b30a 22501 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22502
976ca316 22503 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22504 }
55f1336d 22505 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22506 {
630ed6b9 22507 ULONGEST signature = attr->as_signature ();
673bfd45 22508
ac9ec31b 22509 return get_signatured_type (die, signature, cu);
673bfd45
DE
22510 }
22511 else
22512 {
b98664d3 22513 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22514 " at %s [in module %s]"),
22515 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22516 objfile_name (objfile));
ac9ec31b 22517 return build_error_marker_type (cu, die);
673bfd45
DE
22518 }
22519
22520 /* If not cached we need to read it in. */
22521
22522 if (this_type == NULL)
22523 {
ac9ec31b 22524 struct die_info *type_die = NULL;
673bfd45
DE
22525 struct dwarf2_cu *type_cu = cu;
22526
cd6c91b4 22527 if (attr->form_is_ref ())
ac9ec31b
DE
22528 type_die = follow_die_ref (die, attr, &type_cu);
22529 if (type_die == NULL)
22530 return build_error_marker_type (cu, die);
22531 /* If we find the type now, it's probably because the type came
3019eac3
DE
22532 from an inter-CU reference and the type's CU got expanded before
22533 ours. */
ac9ec31b 22534 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22535 }
22536
22537 /* If we still don't have a type use an error marker. */
22538
22539 if (this_type == NULL)
ac9ec31b 22540 return build_error_marker_type (cu, die);
673bfd45 22541
f792889a 22542 return this_type;
c906108c
SS
22543}
22544
673bfd45
DE
22545/* Return the type in DIE, CU.
22546 Returns NULL for invalid types.
22547
02142a6c 22548 This first does a lookup in die_type_hash,
673bfd45
DE
22549 and only reads the die in if necessary.
22550
22551 NOTE: This can be called when reading in partial or full symbols. */
22552
f792889a 22553static struct type *
e7c27a73 22554read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22555{
f792889a
DJ
22556 struct type *this_type;
22557
22558 this_type = get_die_type (die, cu);
22559 if (this_type)
22560 return this_type;
22561
673bfd45
DE
22562 return read_type_die_1 (die, cu);
22563}
22564
22565/* Read the type in DIE, CU.
22566 Returns NULL for invalid types. */
22567
22568static struct type *
22569read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22570{
22571 struct type *this_type = NULL;
22572
c906108c
SS
22573 switch (die->tag)
22574 {
22575 case DW_TAG_class_type:
680b30c7 22576 case DW_TAG_interface_type:
c906108c
SS
22577 case DW_TAG_structure_type:
22578 case DW_TAG_union_type:
f792889a 22579 this_type = read_structure_type (die, cu);
c906108c
SS
22580 break;
22581 case DW_TAG_enumeration_type:
f792889a 22582 this_type = read_enumeration_type (die, cu);
c906108c
SS
22583 break;
22584 case DW_TAG_subprogram:
22585 case DW_TAG_subroutine_type:
edb3359d 22586 case DW_TAG_inlined_subroutine:
f792889a 22587 this_type = read_subroutine_type (die, cu);
c906108c
SS
22588 break;
22589 case DW_TAG_array_type:
f792889a 22590 this_type = read_array_type (die, cu);
c906108c 22591 break;
72019c9c 22592 case DW_TAG_set_type:
f792889a 22593 this_type = read_set_type (die, cu);
72019c9c 22594 break;
c906108c 22595 case DW_TAG_pointer_type:
f792889a 22596 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22597 break;
22598 case DW_TAG_ptr_to_member_type:
f792889a 22599 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22600 break;
22601 case DW_TAG_reference_type:
4297a3f0
AV
22602 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22603 break;
22604 case DW_TAG_rvalue_reference_type:
22605 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22606 break;
22607 case DW_TAG_const_type:
f792889a 22608 this_type = read_tag_const_type (die, cu);
c906108c
SS
22609 break;
22610 case DW_TAG_volatile_type:
f792889a 22611 this_type = read_tag_volatile_type (die, cu);
c906108c 22612 break;
06d66ee9
TT
22613 case DW_TAG_restrict_type:
22614 this_type = read_tag_restrict_type (die, cu);
22615 break;
c906108c 22616 case DW_TAG_string_type:
f792889a 22617 this_type = read_tag_string_type (die, cu);
c906108c
SS
22618 break;
22619 case DW_TAG_typedef:
f792889a 22620 this_type = read_typedef (die, cu);
c906108c 22621 break;
a02abb62 22622 case DW_TAG_subrange_type:
f792889a 22623 this_type = read_subrange_type (die, cu);
a02abb62 22624 break;
c906108c 22625 case DW_TAG_base_type:
f792889a 22626 this_type = read_base_type (die, cu);
c906108c 22627 break;
81a17f79 22628 case DW_TAG_unspecified_type:
f792889a 22629 this_type = read_unspecified_type (die, cu);
81a17f79 22630 break;
0114d602
DJ
22631 case DW_TAG_namespace:
22632 this_type = read_namespace_type (die, cu);
22633 break;
f55ee35c
JK
22634 case DW_TAG_module:
22635 this_type = read_module_type (die, cu);
22636 break;
a2c2acaf
MW
22637 case DW_TAG_atomic_type:
22638 this_type = read_tag_atomic_type (die, cu);
22639 break;
c906108c 22640 default:
b98664d3 22641 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22642 dwarf_tag_name (die->tag));
c906108c
SS
22643 break;
22644 }
63d06c5c 22645
f792889a 22646 return this_type;
63d06c5c
DC
22647}
22648
abc72ce4
DE
22649/* See if we can figure out if the class lives in a namespace. We do
22650 this by looking for a member function; its demangled name will
22651 contain namespace info, if there is any.
22652 Return the computed name or NULL.
22653 Space for the result is allocated on the objfile's obstack.
22654 This is the full-die version of guess_partial_die_structure_name.
22655 In this case we know DIE has no useful parent. */
22656
43816ebc 22657static const char *
abc72ce4
DE
22658guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22659{
22660 struct die_info *spec_die;
22661 struct dwarf2_cu *spec_cu;
22662 struct die_info *child;
5e22e966 22663 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22664
22665 spec_cu = cu;
22666 spec_die = die_specification (die, &spec_cu);
22667 if (spec_die != NULL)
22668 {
22669 die = spec_die;
22670 cu = spec_cu;
22671 }
22672
22673 for (child = die->child;
22674 child != NULL;
22675 child = child->sibling)
22676 {
22677 if (child->tag == DW_TAG_subprogram)
22678 {
73b9be8b 22679 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22680
7d45c7c3 22681 if (linkage_name != NULL)
abc72ce4 22682 {
43816ebc 22683 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22684 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22685 const char *name = NULL;
abc72ce4
DE
22686
22687 if (actual_name != NULL)
22688 {
15d034d0 22689 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22690
22691 if (die_name != NULL
43816ebc 22692 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22693 {
22694 /* Strip off the class name from the full name.
22695 We want the prefix. */
22696 int die_name_len = strlen (die_name);
43816ebc
TT
22697 int actual_name_len = strlen (actual_name.get ());
22698 const char *ptr = actual_name.get ();
abc72ce4
DE
22699
22700 /* Test for '::' as a sanity check. */
22701 if (actual_name_len > die_name_len + 2
43816ebc 22702 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22703 name = obstack_strndup (
e3b94546 22704 &objfile->per_bfd->storage_obstack,
43816ebc 22705 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22706 }
22707 }
abc72ce4
DE
22708 return name;
22709 }
22710 }
22711 }
22712
22713 return NULL;
22714}
22715
96408a79
SA
22716/* GCC might emit a nameless typedef that has a linkage name. Determine the
22717 prefix part in such case. See
22718 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22719
a121b7c1 22720static const char *
96408a79
SA
22721anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22722{
22723 struct attribute *attr;
e6a959d6 22724 const char *base;
96408a79
SA
22725
22726 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22727 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22728 return NULL;
22729
7d45c7c3 22730 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22731 return NULL;
22732
73b9be8b 22733 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22734 const char *attr_name = attr->as_string ();
22735 if (attr == NULL || attr_name == NULL)
96408a79
SA
22736 return NULL;
22737
22738 /* dwarf2_name had to be already called. */
3b64bf15 22739 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22740
22741 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22742 base = strrchr (attr_name, ':');
22743 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22744 return "";
22745
5e22e966 22746 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22747 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22748 attr_name,
22749 &base[-1] - attr_name);
96408a79
SA
22750}
22751
fdde2d81 22752/* Return the name of the namespace/class that DIE is defined within,
0114d602 22753 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22754
0114d602
DJ
22755 For example, if we're within the method foo() in the following
22756 code:
22757
22758 namespace N {
22759 class C {
22760 void foo () {
22761 }
22762 };
22763 }
22764
22765 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22766
0d5cff50 22767static const char *
e142c38c 22768determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22769{
976ca316 22770 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
22771 struct die_info *parent, *spec_die;
22772 struct dwarf2_cu *spec_cu;
22773 struct type *parent_type;
a121b7c1 22774 const char *retval;
63d06c5c 22775
9c37b5ae 22776 if (cu->language != language_cplus
c44af4eb
TT
22777 && cu->language != language_fortran && cu->language != language_d
22778 && cu->language != language_rust)
0114d602
DJ
22779 return "";
22780
96408a79
SA
22781 retval = anonymous_struct_prefix (die, cu);
22782 if (retval)
22783 return retval;
22784
0114d602
DJ
22785 /* We have to be careful in the presence of DW_AT_specification.
22786 For example, with GCC 3.4, given the code
22787
22788 namespace N {
22789 void foo() {
22790 // Definition of N::foo.
22791 }
22792 }
22793
22794 then we'll have a tree of DIEs like this:
22795
22796 1: DW_TAG_compile_unit
22797 2: DW_TAG_namespace // N
22798 3: DW_TAG_subprogram // declaration of N::foo
22799 4: DW_TAG_subprogram // definition of N::foo
22800 DW_AT_specification // refers to die #3
22801
22802 Thus, when processing die #4, we have to pretend that we're in
22803 the context of its DW_AT_specification, namely the contex of die
22804 #3. */
22805 spec_cu = cu;
22806 spec_die = die_specification (die, &spec_cu);
22807 if (spec_die == NULL)
22808 parent = die->parent;
22809 else
63d06c5c 22810 {
0114d602
DJ
22811 parent = spec_die->parent;
22812 cu = spec_cu;
63d06c5c 22813 }
0114d602
DJ
22814
22815 if (parent == NULL)
22816 return "";
98bfdba5
PA
22817 else if (parent->building_fullname)
22818 {
22819 const char *name;
22820 const char *parent_name;
22821
22822 /* It has been seen on RealView 2.2 built binaries,
22823 DW_TAG_template_type_param types actually _defined_ as
22824 children of the parent class:
22825
22826 enum E {};
22827 template class <class Enum> Class{};
22828 Class<enum E> class_e;
22829
dda83cd7
SM
22830 1: DW_TAG_class_type (Class)
22831 2: DW_TAG_enumeration_type (E)
22832 3: DW_TAG_enumerator (enum1:0)
22833 3: DW_TAG_enumerator (enum2:1)
22834 ...
22835 2: DW_TAG_template_type_param
22836 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
22837
22838 Besides being broken debug info, it can put GDB into an
22839 infinite loop. Consider:
22840
22841 When we're building the full name for Class<E>, we'll start
22842 at Class, and go look over its template type parameters,
22843 finding E. We'll then try to build the full name of E, and
22844 reach here. We're now trying to build the full name of E,
22845 and look over the parent DIE for containing scope. In the
22846 broken case, if we followed the parent DIE of E, we'd again
22847 find Class, and once again go look at its template type
22848 arguments, etc., etc. Simply don't consider such parent die
22849 as source-level parent of this die (it can't be, the language
22850 doesn't allow it), and break the loop here. */
22851 name = dwarf2_name (die, cu);
22852 parent_name = dwarf2_name (parent, cu);
b98664d3 22853 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22854 name ? name : "<unknown>",
22855 parent_name ? parent_name : "<unknown>");
22856 return "";
22857 }
63d06c5c 22858 else
0114d602
DJ
22859 switch (parent->tag)
22860 {
63d06c5c 22861 case DW_TAG_namespace:
0114d602 22862 parent_type = read_type_die (parent, cu);
acebe513
UW
22863 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22864 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22865 Work around this problem here. */
22866 if (cu->language == language_cplus
7d93a1e0 22867 && strcmp (parent_type->name (), "::") == 0)
acebe513 22868 return "";
0114d602 22869 /* We give a name to even anonymous namespaces. */
7d93a1e0 22870 return parent_type->name ();
63d06c5c 22871 case DW_TAG_class_type:
680b30c7 22872 case DW_TAG_interface_type:
63d06c5c 22873 case DW_TAG_structure_type:
0114d602 22874 case DW_TAG_union_type:
f55ee35c 22875 case DW_TAG_module:
0114d602 22876 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
22877 if (parent_type->name () != NULL)
22878 return parent_type->name ();
0114d602
DJ
22879 else
22880 /* An anonymous structure is only allowed non-static data
22881 members; no typedefs, no member functions, et cetera.
22882 So it does not need a prefix. */
22883 return "";
abc72ce4 22884 case DW_TAG_compile_unit:
95554aad 22885 case DW_TAG_partial_unit:
abc72ce4
DE
22886 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22887 if (cu->language == language_cplus
976ca316 22888 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
22889 && die->child != NULL
22890 && (die->tag == DW_TAG_class_type
22891 || die->tag == DW_TAG_structure_type
22892 || die->tag == DW_TAG_union_type))
22893 {
43816ebc 22894 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22895 if (name != NULL)
22896 return name;
22897 }
22898 return "";
0a4b0913
AB
22899 case DW_TAG_subprogram:
22900 /* Nested subroutines in Fortran get a prefix with the name
22901 of the parent's subroutine. */
22902 if (cu->language == language_fortran)
22903 {
22904 if ((die->tag == DW_TAG_subprogram)
22905 && (dwarf2_name (parent, cu) != NULL))
22906 return dwarf2_name (parent, cu);
22907 }
22908 return determine_prefix (parent, cu);
3d567982
TT
22909 case DW_TAG_enumeration_type:
22910 parent_type = read_type_die (parent, cu);
3bc440a2 22911 if (parent_type->is_declared_class ())
3d567982 22912 {
7d93a1e0
SM
22913 if (parent_type->name () != NULL)
22914 return parent_type->name ();
3d567982
TT
22915 return "";
22916 }
22917 /* Fall through. */
63d06c5c 22918 default:
8176b9b8 22919 return determine_prefix (parent, cu);
63d06c5c 22920 }
63d06c5c
DC
22921}
22922
3e43a32a
MS
22923/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22924 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22925 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22926 an obconcat, otherwise allocate storage for the result. The CU argument is
22927 used to determine the language and hence, the appropriate separator. */
987504bb 22928
f55ee35c 22929#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22930
22931static char *
f55ee35c 22932typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 22933 int physname, struct dwarf2_cu *cu)
63d06c5c 22934{
f55ee35c 22935 const char *lead = "";
5c315b68 22936 const char *sep;
63d06c5c 22937
3e43a32a
MS
22938 if (suffix == NULL || suffix[0] == '\0'
22939 || prefix == NULL || prefix[0] == '\0')
987504bb 22940 sep = "";
45280282
IB
22941 else if (cu->language == language_d)
22942 {
22943 /* For D, the 'main' function could be defined in any module, but it
22944 should never be prefixed. */
22945 if (strcmp (suffix, "D main") == 0)
22946 {
22947 prefix = "";
22948 sep = "";
22949 }
22950 else
22951 sep = ".";
22952 }
f55ee35c
JK
22953 else if (cu->language == language_fortran && physname)
22954 {
22955 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22956 DW_AT_MIPS_linkage_name is preferred and used instead. */
22957
22958 lead = "__";
22959 sep = "_MOD_";
22960 }
987504bb
JJ
22961 else
22962 sep = "::";
63d06c5c 22963
6dd47d34
DE
22964 if (prefix == NULL)
22965 prefix = "";
22966 if (suffix == NULL)
22967 suffix = "";
22968
987504bb
JJ
22969 if (obs == NULL)
22970 {
3e43a32a 22971 char *retval
224c3ddb
SM
22972 = ((char *)
22973 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22974
f55ee35c
JK
22975 strcpy (retval, lead);
22976 strcat (retval, prefix);
6dd47d34
DE
22977 strcat (retval, sep);
22978 strcat (retval, suffix);
63d06c5c
DC
22979 return retval;
22980 }
987504bb
JJ
22981 else
22982 {
22983 /* We have an obstack. */
f55ee35c 22984 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22985 }
63d06c5c
DC
22986}
22987
71c25dea
TT
22988/* Get name of a die, return NULL if not found. */
22989
15d034d0
TT
22990static const char *
22991dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 22992 struct objfile *objfile)
71c25dea
TT
22993{
22994 if (name && cu->language == language_cplus)
22995 {
596dc4ad
TT
22996 gdb::unique_xmalloc_ptr<char> canon_name
22997 = cp_canonicalize_string (name);
71c25dea 22998
596dc4ad
TT
22999 if (canon_name != nullptr)
23000 name = objfile->intern (canon_name.get ());
71c25dea
TT
23001 }
23002
23003 return name;
c906108c
SS
23004}
23005
96553a0c
DE
23006/* Get name of a die, return NULL if not found.
23007 Anonymous namespaces are converted to their magic string. */
9219021c 23008
15d034d0 23009static const char *
e142c38c 23010dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23011{
23012 struct attribute *attr;
5e22e966 23013 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 23014
e142c38c 23015 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23016 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23017 if (attr_name == nullptr
96553a0c 23018 && die->tag != DW_TAG_namespace
53832f31
TT
23019 && die->tag != DW_TAG_class_type
23020 && die->tag != DW_TAG_interface_type
23021 && die->tag != DW_TAG_structure_type
23022 && die->tag != DW_TAG_union_type)
71c25dea
TT
23023 return NULL;
23024
23025 switch (die->tag)
23026 {
23027 case DW_TAG_compile_unit:
95554aad 23028 case DW_TAG_partial_unit:
71c25dea
TT
23029 /* Compilation units have a DW_AT_name that is a filename, not
23030 a source language identifier. */
23031 case DW_TAG_enumeration_type:
23032 case DW_TAG_enumerator:
23033 /* These tags always have simple identifiers already; no need
23034 to canonicalize them. */
2c830f54 23035 return attr_name;
907af001 23036
96553a0c 23037 case DW_TAG_namespace:
2c830f54
TT
23038 if (attr_name != nullptr)
23039 return attr_name;
96553a0c
DE
23040 return CP_ANONYMOUS_NAMESPACE_STR;
23041
907af001
UW
23042 case DW_TAG_class_type:
23043 case DW_TAG_interface_type:
23044 case DW_TAG_structure_type:
23045 case DW_TAG_union_type:
23046 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23047 structures or unions. These were of the form "._%d" in GCC 4.1,
23048 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23049 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23050 if (attr_name != nullptr
23051 && (startswith (attr_name, "._")
23052 || startswith (attr_name, "<anonymous")))
907af001 23053 return NULL;
53832f31
TT
23054
23055 /* GCC might emit a nameless typedef that has a linkage name. See
23056 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23057 if (!attr || attr_name == NULL)
53832f31 23058 {
73b9be8b 23059 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23060 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23061 if (attr == NULL || attr_name == NULL)
53832f31
TT
23062 return NULL;
23063
2c830f54 23064 /* Avoid demangling attr_name the second time on a second
df5c6c50 23065 call for the same DIE. */
3b64bf15 23066 if (!attr->canonical_string_p ())
53832f31 23067 {
43816ebc 23068 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23069 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23070 if (demangled == nullptr)
23071 return nullptr;
43816ebc 23072
c6481205 23073 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23074 attr_name = attr->as_string ();
53832f31 23075 }
67430cd0 23076
2c830f54
TT
23077 /* Strip any leading namespaces/classes, keep only the
23078 base name. DW_AT_name for named DIEs does not
23079 contain the prefixes. */
23080 const char *base = strrchr (attr_name, ':');
23081 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23082 return &base[1];
23083 else
2c830f54 23084 return attr_name;
53832f31 23085 }
907af001
UW
23086 break;
23087
71c25dea 23088 default:
907af001
UW
23089 break;
23090 }
23091
3b64bf15 23092 if (!attr->canonical_string_p ())
c6481205
TT
23093 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23094 objfile));
2c830f54 23095 return attr->as_string ();
9219021c
DC
23096}
23097
23098/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23099 is none. *EXT_CU is the CU containing DIE on input, and the CU
23100 containing the return value on output. */
9219021c
DC
23101
23102static struct die_info *
f2f0e013 23103dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23104{
23105 struct attribute *attr;
9219021c 23106
f2f0e013 23107 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23108 if (attr == NULL)
23109 return NULL;
23110
f2f0e013 23111 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23112}
23113
f9aca02d 23114static void
d97bc12b 23115dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23116{
23117 unsigned int i;
23118
d97bc12b 23119 print_spaces (indent, f);
9d8780f0 23120 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23121 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23122 sect_offset_str (die->sect_off));
d97bc12b
DE
23123
23124 if (die->parent != NULL)
23125 {
23126 print_spaces (indent, f);
9d8780f0
SM
23127 fprintf_unfiltered (f, " parent at offset: %s\n",
23128 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23129 }
23130
23131 print_spaces (indent, f);
23132 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23133 dwarf_bool_name (die->child != NULL));
c906108c 23134
d97bc12b
DE
23135 print_spaces (indent, f);
23136 fprintf_unfiltered (f, " attributes:\n");
23137
c906108c
SS
23138 for (i = 0; i < die->num_attrs; ++i)
23139 {
d97bc12b
DE
23140 print_spaces (indent, f);
23141 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23142 dwarf_attr_name (die->attrs[i].name),
23143 dwarf_form_name (die->attrs[i].form));
d97bc12b 23144
c906108c
SS
23145 switch (die->attrs[i].form)
23146 {
c906108c 23147 case DW_FORM_addr:
336d760d 23148 case DW_FORM_addrx:
3019eac3 23149 case DW_FORM_GNU_addr_index:
d97bc12b 23150 fprintf_unfiltered (f, "address: ");
36d378cf 23151 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23152 break;
23153 case DW_FORM_block2:
23154 case DW_FORM_block4:
23155 case DW_FORM_block:
23156 case DW_FORM_block1:
56eb65bd 23157 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23158 pulongest (die->attrs[i].as_block ()->size));
c906108c 23159 break;
2dc7f7b3 23160 case DW_FORM_exprloc:
56eb65bd 23161 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23162 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23163 break;
0224619f
JK
23164 case DW_FORM_data16:
23165 fprintf_unfiltered (f, "constant of 16 bytes");
23166 break;
4568ecf9
DE
23167 case DW_FORM_ref_addr:
23168 fprintf_unfiltered (f, "ref address: ");
529908cb 23169 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23170 break;
36586728
TT
23171 case DW_FORM_GNU_ref_alt:
23172 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23173 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23174 break;
10b3939b
DJ
23175 case DW_FORM_ref1:
23176 case DW_FORM_ref2:
23177 case DW_FORM_ref4:
4568ecf9
DE
23178 case DW_FORM_ref8:
23179 case DW_FORM_ref_udata:
d97bc12b 23180 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23181 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23182 break;
c906108c
SS
23183 case DW_FORM_data1:
23184 case DW_FORM_data2:
23185 case DW_FORM_data4:
ce5d95e1 23186 case DW_FORM_data8:
c906108c 23187 case DW_FORM_udata:
43bbcdc2 23188 fprintf_unfiltered (f, "constant: %s",
529908cb 23189 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23190 break;
2dc7f7b3
TT
23191 case DW_FORM_sec_offset:
23192 fprintf_unfiltered (f, "section offset: %s",
529908cb 23193 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23194 break;
55f1336d 23195 case DW_FORM_ref_sig8:
ac9ec31b 23196 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23197 hex_string (die->attrs[i].as_signature ()));
348e048f 23198 break;
c906108c 23199 case DW_FORM_string:
4bdf3d34 23200 case DW_FORM_strp:
43988095 23201 case DW_FORM_line_strp:
cf532bd1 23202 case DW_FORM_strx:
3019eac3 23203 case DW_FORM_GNU_str_index:
36586728 23204 case DW_FORM_GNU_strp_alt:
8285870a 23205 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23206 die->attrs[i].as_string ()
23207 ? die->attrs[i].as_string () : "",
23208 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23209 break;
23210 case DW_FORM_flag:
c45bc3f8 23211 if (die->attrs[i].as_boolean ())
d97bc12b 23212 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23213 else
d97bc12b 23214 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23215 break;
2dc7f7b3
TT
23216 case DW_FORM_flag_present:
23217 fprintf_unfiltered (f, "flag: TRUE");
23218 break;
a8329558 23219 case DW_FORM_indirect:
0963b4bd
MS
23220 /* The reader will have reduced the indirect form to
23221 the "base form" so this form should not occur. */
5f48f8f3 23222 fprintf_unfiltered (f,
3e43a32a 23223 "unexpected attribute form: DW_FORM_indirect");
a8329558 23224 break;
1bc397c5 23225 case DW_FORM_sdata:
663c44ac
JK
23226 case DW_FORM_implicit_const:
23227 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23228 plongest (die->attrs[i].as_signed ()));
663c44ac 23229 break;
c906108c 23230 default:
d97bc12b 23231 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23232 die->attrs[i].form);
d97bc12b 23233 break;
c906108c 23234 }
d97bc12b 23235 fprintf_unfiltered (f, "\n");
c906108c
SS
23236 }
23237}
23238
f9aca02d 23239static void
d97bc12b 23240dump_die_for_error (struct die_info *die)
c906108c 23241{
d97bc12b
DE
23242 dump_die_shallow (gdb_stderr, 0, die);
23243}
23244
23245static void
23246dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23247{
23248 int indent = level * 4;
23249
23250 gdb_assert (die != NULL);
23251
23252 if (level >= max_level)
23253 return;
23254
23255 dump_die_shallow (f, indent, die);
23256
23257 if (die->child != NULL)
c906108c 23258 {
d97bc12b
DE
23259 print_spaces (indent, f);
23260 fprintf_unfiltered (f, " Children:");
23261 if (level + 1 < max_level)
23262 {
23263 fprintf_unfiltered (f, "\n");
23264 dump_die_1 (f, level + 1, max_level, die->child);
23265 }
23266 else
23267 {
3e43a32a
MS
23268 fprintf_unfiltered (f,
23269 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23270 }
23271 }
23272
23273 if (die->sibling != NULL && level > 0)
23274 {
23275 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23276 }
23277}
23278
d97bc12b
DE
23279/* This is called from the pdie macro in gdbinit.in.
23280 It's not static so gcc will keep a copy callable from gdb. */
23281
23282void
23283dump_die (struct die_info *die, int max_level)
23284{
23285 dump_die_1 (gdb_stdlog, 0, max_level, die);
23286}
23287
f9aca02d 23288static void
51545339 23289store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23290{
51545339 23291 void **slot;
c906108c 23292
9c541725
PA
23293 slot = htab_find_slot_with_hash (cu->die_hash, die,
23294 to_underlying (die->sect_off),
b64f50a1 23295 INSERT);
51545339
DJ
23296
23297 *slot = die;
c906108c
SS
23298}
23299
348e048f
DE
23300/* Follow reference or signature attribute ATTR of SRC_DIE.
23301 On entry *REF_CU is the CU of SRC_DIE.
23302 On exit *REF_CU is the CU of the result. */
23303
23304static struct die_info *
ff39bb5e 23305follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23306 struct dwarf2_cu **ref_cu)
23307{
23308 struct die_info *die;
23309
cd6c91b4 23310 if (attr->form_is_ref ())
348e048f 23311 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23312 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23313 die = follow_die_sig (src_die, attr, ref_cu);
23314 else
23315 {
23316 dump_die_for_error (src_die);
23317 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23318 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23319 }
23320
23321 return die;
03dd20cc
DJ
23322}
23323
5c631832 23324/* Follow reference OFFSET.
673bfd45
DE
23325 On entry *REF_CU is the CU of the source die referencing OFFSET.
23326 On exit *REF_CU is the CU of the result.
23327 Returns NULL if OFFSET is invalid. */
f504f079 23328
f9aca02d 23329static struct die_info *
9c541725 23330follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23331 struct dwarf2_cu **ref_cu)
c906108c 23332{
10b3939b 23333 struct die_info temp_die;
f2f0e013 23334 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23335 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23336
348e048f
DE
23337 gdb_assert (cu->per_cu != NULL);
23338
98bfdba5
PA
23339 target_cu = cu;
23340
17e593e9
SM
23341 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23342 "source CU contains target offset: %d",
23343 sect_offset_str (cu->per_cu->sect_off),
23344 sect_offset_str (sect_off),
23345 cu->header.offset_in_cu_p (sect_off));
23346
3019eac3 23347 if (cu->per_cu->is_debug_types)
348e048f
DE
23348 {
23349 /* .debug_types CUs cannot reference anything outside their CU.
23350 If they need to, they have to reference a signatured type via
55f1336d 23351 DW_FORM_ref_sig8. */
4057dfde 23352 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23353 return NULL;
348e048f 23354 }
36586728 23355 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23356 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23357 {
23358 struct dwarf2_per_cu_data *per_cu;
9a619af0 23359
9c541725 23360 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23361 per_objfile);
03dd20cc 23362
17e593e9
SM
23363 dwarf_read_debug_printf_v ("target CU offset: %s, "
23364 "target CU DIEs loaded: %d",
23365 sect_offset_str (per_cu->sect_off),
23366 per_objfile->get_cu (per_cu) != nullptr);
23367
616c069a
SM
23368 /* If necessary, add it to the queue and load its DIEs.
23369
23370 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23371 it doesn't mean they are currently loaded. Since we require them
23372 to be loaded, we must check for ourselves. */
23373 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23374 || per_objfile->get_cu (per_cu) == nullptr)
4a636814
SM
23375 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23376 false, cu->language);
03dd20cc 23377
976ca316 23378 target_cu = per_objfile->get_cu (per_cu);
616c069a 23379 gdb_assert (target_cu != nullptr);
10b3939b 23380 }
98bfdba5
PA
23381 else if (cu->dies == NULL)
23382 {
23383 /* We're loading full DIEs during partial symbol reading. */
976ca316 23384 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23385 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23386 language_minimal);
98bfdba5 23387 }
c906108c 23388
f2f0e013 23389 *ref_cu = target_cu;
9c541725 23390 temp_die.sect_off = sect_off;
c24bdb02 23391
3db19b2d
TV
23392 if (target_cu != cu)
23393 target_cu->ancestor = cu;
23394
9a3c8263 23395 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23396 &temp_die,
23397 to_underlying (sect_off));
5c631832 23398}
10b3939b 23399
5c631832
JK
23400/* Follow reference attribute ATTR of SRC_DIE.
23401 On entry *REF_CU is the CU of SRC_DIE.
23402 On exit *REF_CU is the CU of the result. */
23403
23404static struct die_info *
ff39bb5e 23405follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23406 struct dwarf2_cu **ref_cu)
23407{
0826b30a 23408 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23409 struct dwarf2_cu *cu = *ref_cu;
23410 struct die_info *die;
23411
9c541725 23412 die = follow_die_offset (sect_off,
36586728
TT
23413 (attr->form == DW_FORM_GNU_ref_alt
23414 || cu->per_cu->is_dwz),
23415 ref_cu);
5c631832 23416 if (!die)
9d8780f0
SM
23417 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23418 "at %s [in module %s]"),
23419 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23420 objfile_name (cu->per_objfile->objfile));
348e048f 23421
5c631832
JK
23422 return die;
23423}
23424
d4c9a4f8 23425/* See read.h. */
5c631832
JK
23426
23427struct dwarf2_locexpr_baton
9c541725 23428dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23429 dwarf2_per_cu_data *per_cu,
976ca316 23430 dwarf2_per_objfile *per_objfile,
041d9819
SM
23431 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23432 bool resolve_abstract_p)
5c631832 23433{
5c631832
JK
23434 struct die_info *die;
23435 struct attribute *attr;
23436 struct dwarf2_locexpr_baton retval;
976ca316 23437 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23438
976ca316 23439 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23440 if (cu == nullptr)
976ca316 23441 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23442
23443 if (cu == nullptr)
cc12ce38
DE
23444 {
23445 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23446 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23447 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23448 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23449 }
918dd910 23450
9c541725 23451 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23452 if (!die)
9d8780f0
SM
23453 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23454 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23455
23456 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23457 if (!attr && resolve_abstract_p
976ca316
SM
23458 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23459 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23460 {
041d9819 23461 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23462 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23463 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23464
3360b6e7 23465 for (const auto &cand_off
976ca316 23466 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23467 {
3360b6e7
TV
23468 struct dwarf2_cu *cand_cu = cu;
23469 struct die_info *cand
23470 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23471 if (!cand
23472 || !cand->parent
e4a62c65
TV
23473 || cand->parent->tag != DW_TAG_subprogram)
23474 continue;
23475
23476 CORE_ADDR pc_low, pc_high;
23477 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23478 if (pc_low == ((CORE_ADDR) -1))
23479 continue;
23480 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23481 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23482 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23483 continue;
23484
23485 die = cand;
23486 attr = dwarf2_attr (die, DW_AT_location, cu);
23487 break;
23488 }
23489 }
23490
5c631832
JK
23491 if (!attr)
23492 {
e103e986
JK
23493 /* DWARF: "If there is no such attribute, then there is no effect.".
23494 DATA is ignored if SIZE is 0. */
5c631832 23495
e103e986 23496 retval.data = NULL;
5c631832
JK
23497 retval.size = 0;
23498 }
cd6c91b4 23499 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23500 {
23501 struct dwarf2_loclist_baton loclist_baton;
041d9819 23502 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23503 size_t size;
23504
23505 fill_in_loclist_baton (cu, &loclist_baton, attr);
23506
23507 retval.data = dwarf2_find_location_expression (&loclist_baton,
23508 &size, pc);
23509 retval.size = size;
23510 }
5c631832
JK
23511 else
23512 {
4fc6c0d5 23513 if (!attr->form_is_block ())
9d8780f0 23514 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23515 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23516 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23517
9d2246fc
TT
23518 struct dwarf_block *block = attr->as_block ();
23519 retval.data = block->data;
23520 retval.size = block->size;
5c631832 23521 }
976ca316 23522 retval.per_objfile = per_objfile;
5c631832 23523 retval.per_cu = cu->per_cu;
918dd910 23524
976ca316 23525 per_objfile->age_comp_units ();
918dd910 23526
5c631832 23527 return retval;
348e048f
DE
23528}
23529
d4c9a4f8 23530/* See read.h. */
8b9737bf
TT
23531
23532struct dwarf2_locexpr_baton
23533dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23534 dwarf2_per_cu_data *per_cu,
14095eb3 23535 dwarf2_per_objfile *per_objfile,
041d9819 23536 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23537{
9c541725 23538 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23539
14095eb3 23540 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23541 get_frame_pc);
8b9737bf
TT
23542}
23543
b6807d98
TT
23544/* Write a constant of a given type as target-ordered bytes into
23545 OBSTACK. */
23546
23547static const gdb_byte *
23548write_constant_as_bytes (struct obstack *obstack,
23549 enum bfd_endian byte_order,
23550 struct type *type,
23551 ULONGEST value,
23552 LONGEST *len)
23553{
23554 gdb_byte *result;
23555
23556 *len = TYPE_LENGTH (type);
224c3ddb 23557 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23558 store_unsigned_integer (result, *len, byte_order, value);
23559
23560 return result;
23561}
23562
d4c9a4f8 23563/* See read.h. */
b6807d98
TT
23564
23565const gdb_byte *
9c541725 23566dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23567 dwarf2_per_cu_data *per_cu,
14095eb3 23568 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23569 obstack *obstack,
b6807d98
TT
23570 LONGEST *len)
23571{
b6807d98
TT
23572 struct die_info *die;
23573 struct attribute *attr;
23574 const gdb_byte *result = NULL;
23575 struct type *type;
23576 LONGEST value;
23577 enum bfd_endian byte_order;
14095eb3 23578 struct objfile *objfile = per_objfile->objfile;
b6807d98 23579
7188ed02 23580 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23581 if (cu == nullptr)
23582 cu = load_cu (per_cu, per_objfile, false);
23583
23584 if (cu == nullptr)
cc12ce38
DE
23585 {
23586 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23587 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23588 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23589 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23590 }
b6807d98 23591
9c541725 23592 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23593 if (!die)
9d8780f0
SM
23594 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23595 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23596
23597 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23598 if (attr == NULL)
23599 return NULL;
23600
e3b94546 23601 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23602 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23603
23604 switch (attr->form)
23605 {
23606 case DW_FORM_addr:
336d760d 23607 case DW_FORM_addrx:
b6807d98
TT
23608 case DW_FORM_GNU_addr_index:
23609 {
23610 gdb_byte *tem;
23611
23612 *len = cu->header.addr_size;
224c3ddb 23613 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23614 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23615 result = tem;
23616 }
23617 break;
23618 case DW_FORM_string:
23619 case DW_FORM_strp:
cf532bd1 23620 case DW_FORM_strx:
b6807d98
TT
23621 case DW_FORM_GNU_str_index:
23622 case DW_FORM_GNU_strp_alt:
c6481205 23623 /* The string is already allocated on the objfile obstack, point
b6807d98 23624 directly to it. */
2c830f54
TT
23625 {
23626 const char *attr_name = attr->as_string ();
23627 result = (const gdb_byte *) attr_name;
23628 *len = strlen (attr_name);
23629 }
b6807d98
TT
23630 break;
23631 case DW_FORM_block1:
23632 case DW_FORM_block2:
23633 case DW_FORM_block4:
23634 case DW_FORM_block:
23635 case DW_FORM_exprloc:
0224619f 23636 case DW_FORM_data16:
9d2246fc
TT
23637 {
23638 struct dwarf_block *block = attr->as_block ();
23639 result = block->data;
23640 *len = block->size;
23641 }
b6807d98
TT
23642 break;
23643
23644 /* The DW_AT_const_value attributes are supposed to carry the
23645 symbol's value "represented as it would be on the target
23646 architecture." By the time we get here, it's already been
23647 converted to host endianness, so we just need to sign- or
23648 zero-extend it as appropriate. */
23649 case DW_FORM_data1:
23650 type = die_type (die, cu);
23651 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23652 if (result == NULL)
23653 result = write_constant_as_bytes (obstack, byte_order,
23654 type, value, len);
23655 break;
23656 case DW_FORM_data2:
23657 type = die_type (die, cu);
23658 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23659 if (result == NULL)
23660 result = write_constant_as_bytes (obstack, byte_order,
23661 type, value, len);
23662 break;
23663 case DW_FORM_data4:
23664 type = die_type (die, cu);
23665 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23666 if (result == NULL)
23667 result = write_constant_as_bytes (obstack, byte_order,
23668 type, value, len);
23669 break;
23670 case DW_FORM_data8:
23671 type = die_type (die, cu);
23672 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23673 if (result == NULL)
23674 result = write_constant_as_bytes (obstack, byte_order,
23675 type, value, len);
23676 break;
23677
23678 case DW_FORM_sdata:
663c44ac 23679 case DW_FORM_implicit_const:
b6807d98
TT
23680 type = die_type (die, cu);
23681 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23682 type, attr->as_signed (), len);
b6807d98
TT
23683 break;
23684
23685 case DW_FORM_udata:
23686 type = die_type (die, cu);
23687 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23688 type, attr->as_unsigned (), len);
b6807d98
TT
23689 break;
23690
23691 default:
b98664d3 23692 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23693 dwarf_form_name (attr->form));
23694 break;
23695 }
23696
23697 return result;
23698}
23699
d4c9a4f8 23700/* See read.h. */
7942e96e
AA
23701
23702struct type *
9c541725 23703dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
23704 dwarf2_per_cu_data *per_cu,
23705 dwarf2_per_objfile *per_objfile)
7942e96e 23706{
7942e96e
AA
23707 struct die_info *die;
23708
7188ed02 23709 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23710 if (cu == nullptr)
23711 cu = load_cu (per_cu, per_objfile, false);
23712
23713 if (cu == nullptr)
23714 return nullptr;
7942e96e 23715
9c541725 23716 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23717 if (!die)
23718 return NULL;
23719
23720 return die_type (die, cu);
23721}
23722
8cb5117c 23723/* See read.h. */
8a9b8146
TT
23724
23725struct type *
b64f50a1 23726dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23727 dwarf2_per_cu_data *per_cu,
23728 dwarf2_per_objfile *per_objfile)
8a9b8146 23729{
9c541725 23730 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23731 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23732}
23733
ac9ec31b 23734/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23735 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23736 On exit *REF_CU is the CU of the result.
23737 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23738
23739static struct die_info *
ac9ec31b
DE
23740follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23741 struct dwarf2_cu **ref_cu)
348e048f 23742{
348e048f 23743 struct die_info temp_die;
3db19b2d 23744 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 23745 struct die_info *die;
976ca316 23746 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23747
348e048f 23748
ac9ec31b
DE
23749 /* While it might be nice to assert sig_type->type == NULL here,
23750 we can get here for DW_AT_imported_declaration where we need
23751 the DIE not the type. */
348e048f 23752
616c069a 23753 /* If necessary, add it to the queue and load its DIEs.
348e048f 23754
616c069a
SM
23755 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23756 it doesn't mean they are currently loaded. Since we require them
23757 to be loaded, we must check for ourselves. */
0d305d5c 23758 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
616c069a 23759 language_minimal)
0d305d5c 23760 || per_objfile->get_cu (sig_type) == nullptr)
976ca316 23761 read_signatured_type (sig_type, per_objfile);
348e048f 23762
0d305d5c 23763 sig_cu = per_objfile->get_cu (sig_type);
69d751e3 23764 gdb_assert (sig_cu != NULL);
9c541725
PA
23765 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23766 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23767 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23768 to_underlying (temp_die.sect_off));
348e048f
DE
23769 if (die)
23770 {
796a7ff8
DE
23771 /* For .gdb_index version 7 keep track of included TUs.
23772 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
23773 if (per_objfile->per_bfd->index_table != NULL
23774 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 23775 {
ae640021 23776 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23777 }
23778
348e048f 23779 *ref_cu = sig_cu;
3db19b2d
TV
23780 if (sig_cu != cu)
23781 sig_cu->ancestor = cu;
c24bdb02 23782
348e048f
DE
23783 return die;
23784 }
23785
ac9ec31b
DE
23786 return NULL;
23787}
23788
23789/* Follow signatured type referenced by ATTR in SRC_DIE.
23790 On entry *REF_CU is the CU of SRC_DIE.
23791 On exit *REF_CU is the CU of the result.
23792 The result is the DIE of the type.
23793 If the referenced type cannot be found an error is thrown. */
23794
23795static struct die_info *
ff39bb5e 23796follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23797 struct dwarf2_cu **ref_cu)
23798{
630ed6b9 23799 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
23800 struct signatured_type *sig_type;
23801 struct die_info *die;
23802
23803 gdb_assert (attr->form == DW_FORM_ref_sig8);
23804
a2ce51a0 23805 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23806 /* sig_type will be NULL if the signatured type is missing from
23807 the debug info. */
23808 if (sig_type == NULL)
23809 {
23810 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
23811 " from DIE at %s [in module %s]"),
23812 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23813 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23814 }
23815
23816 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23817 if (die == NULL)
23818 {
23819 dump_die_for_error (src_die);
23820 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23821 " from DIE at %s [in module %s]"),
23822 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23823 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23824 }
23825
23826 return die;
23827}
23828
23829/* Get the type specified by SIGNATURE referenced in DIE/CU,
23830 reading in and processing the type unit if necessary. */
23831
23832static struct type *
23833get_signatured_type (struct die_info *die, ULONGEST signature,
23834 struct dwarf2_cu *cu)
23835{
976ca316 23836 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
23837 struct signatured_type *sig_type;
23838 struct dwarf2_cu *type_cu;
23839 struct die_info *type_die;
23840 struct type *type;
23841
a2ce51a0 23842 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23843 /* sig_type will be NULL if the signatured type is missing from
23844 the debug info. */
23845 if (sig_type == NULL)
23846 {
b98664d3 23847 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23848 " from DIE at %s [in module %s]"),
23849 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23850 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23851 return build_error_marker_type (cu, die);
23852 }
23853
23854 /* If we already know the type we're done. */
976ca316 23855 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
23856 if (type != nullptr)
23857 return type;
ac9ec31b
DE
23858
23859 type_cu = cu;
23860 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23861 if (type_die != NULL)
23862 {
23863 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23864 is created. This is important, for example, because for c++ classes
23865 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23866 type = read_type_die (type_die, type_cu);
23867 if (type == NULL)
23868 {
b98664d3 23869 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23870 " referenced from DIE at %s [in module %s]"),
23871 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23872 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23873 type = build_error_marker_type (cu, die);
23874 }
23875 }
23876 else
23877 {
b98664d3 23878 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23879 " from DIE at %s [in module %s]"),
23880 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23881 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23882 type = build_error_marker_type (cu, die);
23883 }
e286671b 23884
976ca316 23885 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
23886
23887 return type;
23888}
23889
23890/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23891 reading in and processing the type unit if necessary. */
23892
23893static struct type *
ff39bb5e 23894get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23895 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23896{
23897 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 23898 if (attr->form_is_ref ())
ac9ec31b
DE
23899 {
23900 struct dwarf2_cu *type_cu = cu;
23901 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23902
23903 return read_type_die (type_die, type_cu);
23904 }
23905 else if (attr->form == DW_FORM_ref_sig8)
23906 {
630ed6b9 23907 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
23908 }
23909 else
23910 {
976ca316 23911 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23912
b98664d3 23913 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23914 " at %s [in module %s]"),
23915 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 23916 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23917 return build_error_marker_type (cu, die);
23918 }
348e048f
DE
23919}
23920
e5fe5e75 23921/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23922
23923static void
ab432490
SM
23924load_full_type_unit (dwarf2_per_cu_data *per_cu,
23925 dwarf2_per_objfile *per_objfile)
348e048f 23926{
52dc124a 23927 struct signatured_type *sig_type;
348e048f 23928
f4dc4d17 23929 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 23930 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 23931
6721b2ec
DE
23932 /* We have the per_cu, but we need the signatured_type.
23933 Fortunately this is an easy translation. */
23934 gdb_assert (per_cu->is_debug_types);
23935 sig_type = (struct signatured_type *) per_cu;
348e048f 23936
7188ed02 23937 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 23938
ab432490 23939 read_signatured_type (sig_type, per_objfile);
348e048f 23940
7188ed02 23941 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
23942}
23943
3019eac3
DE
23944/* Read in a signatured type and build its CU and DIEs.
23945 If the type is a stub for the real type in a DWO file,
23946 read in the real type from the DWO file as well. */
dee91e82
DE
23947
23948static void
ab432490
SM
23949read_signatured_type (signatured_type *sig_type,
23950 dwarf2_per_objfile *per_objfile)
dee91e82 23951{
0d305d5c
TT
23952 gdb_assert (sig_type->is_debug_types);
23953 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
348e048f 23954
0d305d5c 23955 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
23956
23957 if (!reader.dummy_p)
23958 {
23959 struct dwarf2_cu *cu = reader.cu;
23960 const gdb_byte *info_ptr = reader.info_ptr;
23961
23962 gdb_assert (cu->die_hash == NULL);
23963 cu->die_hash =
23964 htab_create_alloc_ex (cu->header.length / 12,
23965 die_hash,
23966 die_eq,
23967 NULL,
23968 &cu->comp_unit_obstack,
23969 hashtab_obstack_allocate,
23970 dummy_obstack_deallocate);
23971
3e225074 23972 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
23973 reader.comp_unit_die->child
23974 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23975 reader.comp_unit_die);
23976 cu->dies = reader.comp_unit_die;
23977 /* comp_unit_die is not stored in die_hash, no need. */
23978
23979 /* We try not to read any attributes in this function, because
23980 not all CUs needed for references have been loaded yet, and
23981 symbol table processing isn't initialized. But we have to
23982 set the CU language, or we won't be able to build types
23983 correctly. Similarly, if we do not read the producer, we can
23984 not apply producer-specific interpretation. */
23985 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
23986
23987 reader.keep ();
c0ab21c2
TT
23988 }
23989
0d305d5c 23990 sig_type->tu_read = 1;
c906108c
SS
23991}
23992
c906108c
SS
23993/* Decode simple location descriptions.
23994 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
23995 the location and return the value. If COMPUTED is non-null, it is
23996 set to true to indicate that decoding was successful, and false
23997 otherwise. If COMPUTED is null, then this function may emit a
23998 complaint. */
c906108c
SS
23999
24000static CORE_ADDR
7d79de9a 24001decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 24002{
5e22e966 24003 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
24004 size_t i;
24005 size_t size = blk->size;
d521ce57 24006 const gdb_byte *data = blk->data;
21ae7a4d
JK
24007 CORE_ADDR stack[64];
24008 int stacki;
24009 unsigned int bytes_read, unsnd;
24010 gdb_byte op;
c906108c 24011
7d79de9a
TT
24012 if (computed != nullptr)
24013 *computed = false;
24014
21ae7a4d
JK
24015 i = 0;
24016 stacki = 0;
24017 stack[stacki] = 0;
24018 stack[++stacki] = 0;
24019
24020 while (i < size)
24021 {
24022 op = data[i++];
24023 switch (op)
24024 {
24025 case DW_OP_lit0:
24026 case DW_OP_lit1:
24027 case DW_OP_lit2:
24028 case DW_OP_lit3:
24029 case DW_OP_lit4:
24030 case DW_OP_lit5:
24031 case DW_OP_lit6:
24032 case DW_OP_lit7:
24033 case DW_OP_lit8:
24034 case DW_OP_lit9:
24035 case DW_OP_lit10:
24036 case DW_OP_lit11:
24037 case DW_OP_lit12:
24038 case DW_OP_lit13:
24039 case DW_OP_lit14:
24040 case DW_OP_lit15:
24041 case DW_OP_lit16:
24042 case DW_OP_lit17:
24043 case DW_OP_lit18:
24044 case DW_OP_lit19:
24045 case DW_OP_lit20:
24046 case DW_OP_lit21:
24047 case DW_OP_lit22:
24048 case DW_OP_lit23:
24049 case DW_OP_lit24:
24050 case DW_OP_lit25:
24051 case DW_OP_lit26:
24052 case DW_OP_lit27:
24053 case DW_OP_lit28:
24054 case DW_OP_lit29:
24055 case DW_OP_lit30:
24056 case DW_OP_lit31:
24057 stack[++stacki] = op - DW_OP_lit0;
24058 break;
f1bea926 24059
21ae7a4d
JK
24060 case DW_OP_reg0:
24061 case DW_OP_reg1:
24062 case DW_OP_reg2:
24063 case DW_OP_reg3:
24064 case DW_OP_reg4:
24065 case DW_OP_reg5:
24066 case DW_OP_reg6:
24067 case DW_OP_reg7:
24068 case DW_OP_reg8:
24069 case DW_OP_reg9:
24070 case DW_OP_reg10:
24071 case DW_OP_reg11:
24072 case DW_OP_reg12:
24073 case DW_OP_reg13:
24074 case DW_OP_reg14:
24075 case DW_OP_reg15:
24076 case DW_OP_reg16:
24077 case DW_OP_reg17:
24078 case DW_OP_reg18:
24079 case DW_OP_reg19:
24080 case DW_OP_reg20:
24081 case DW_OP_reg21:
24082 case DW_OP_reg22:
24083 case DW_OP_reg23:
24084 case DW_OP_reg24:
24085 case DW_OP_reg25:
24086 case DW_OP_reg26:
24087 case DW_OP_reg27:
24088 case DW_OP_reg28:
24089 case DW_OP_reg29:
24090 case DW_OP_reg30:
24091 case DW_OP_reg31:
24092 stack[++stacki] = op - DW_OP_reg0;
24093 if (i < size)
7d79de9a
TT
24094 {
24095 if (computed == nullptr)
24096 dwarf2_complex_location_expr_complaint ();
24097 else
24098 return 0;
24099 }
21ae7a4d 24100 break;
c906108c 24101
21ae7a4d
JK
24102 case DW_OP_regx:
24103 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24104 i += bytes_read;
24105 stack[++stacki] = unsnd;
24106 if (i < size)
7d79de9a
TT
24107 {
24108 if (computed == nullptr)
24109 dwarf2_complex_location_expr_complaint ();
24110 else
24111 return 0;
24112 }
21ae7a4d 24113 break;
c906108c 24114
21ae7a4d 24115 case DW_OP_addr:
c8a7a66f
TT
24116 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24117 &bytes_read);
21ae7a4d
JK
24118 i += bytes_read;
24119 break;
d53d4ac5 24120
21ae7a4d
JK
24121 case DW_OP_const1u:
24122 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24123 i += 1;
24124 break;
24125
24126 case DW_OP_const1s:
24127 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24128 i += 1;
24129 break;
24130
24131 case DW_OP_const2u:
24132 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24133 i += 2;
24134 break;
24135
24136 case DW_OP_const2s:
24137 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24138 i += 2;
24139 break;
d53d4ac5 24140
21ae7a4d
JK
24141 case DW_OP_const4u:
24142 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24143 i += 4;
24144 break;
24145
24146 case DW_OP_const4s:
24147 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24148 i += 4;
24149 break;
24150
585861ea
JK
24151 case DW_OP_const8u:
24152 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24153 i += 8;
24154 break;
24155
21ae7a4d
JK
24156 case DW_OP_constu:
24157 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24158 &bytes_read);
24159 i += bytes_read;
24160 break;
24161
24162 case DW_OP_consts:
24163 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24164 i += bytes_read;
24165 break;
24166
24167 case DW_OP_dup:
24168 stack[stacki + 1] = stack[stacki];
24169 stacki++;
24170 break;
24171
24172 case DW_OP_plus:
24173 stack[stacki - 1] += stack[stacki];
24174 stacki--;
24175 break;
24176
24177 case DW_OP_plus_uconst:
24178 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24179 &bytes_read);
24180 i += bytes_read;
24181 break;
24182
24183 case DW_OP_minus:
24184 stack[stacki - 1] -= stack[stacki];
24185 stacki--;
24186 break;
24187
24188 case DW_OP_deref:
24189 /* If we're not the last op, then we definitely can't encode
24190 this using GDB's address_class enum. This is valid for partial
24191 global symbols, although the variable's address will be bogus
24192 in the psymtab. */
24193 if (i < size)
7d79de9a
TT
24194 {
24195 if (computed == nullptr)
24196 dwarf2_complex_location_expr_complaint ();
24197 else
24198 return 0;
24199 }
21ae7a4d
JK
24200 break;
24201
dda83cd7 24202 case DW_OP_GNU_push_tls_address:
4aa4e28b 24203 case DW_OP_form_tls_address:
21ae7a4d
JK
24204 /* The top of the stack has the offset from the beginning
24205 of the thread control block at which the variable is located. */
24206 /* Nothing should follow this operator, so the top of stack would
24207 be returned. */
24208 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24209 address will be bogus in the psymtab. Make it always at least
24210 non-zero to not look as a variable garbage collected by linker
24211 which have DW_OP_addr 0. */
21ae7a4d 24212 if (i < size)
7d79de9a
TT
24213 {
24214 if (computed == nullptr)
24215 dwarf2_complex_location_expr_complaint ();
24216 else
24217 return 0;
24218 }
585861ea 24219 stack[stacki]++;
dda83cd7 24220 break;
21ae7a4d
JK
24221
24222 case DW_OP_GNU_uninit:
7d79de9a
TT
24223 if (computed != nullptr)
24224 return 0;
21ae7a4d
JK
24225 break;
24226
336d760d 24227 case DW_OP_addrx:
3019eac3 24228 case DW_OP_GNU_addr_index:
49f6c839 24229 case DW_OP_GNU_const_index:
3019eac3
DE
24230 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24231 &bytes_read);
24232 i += bytes_read;
24233 break;
24234
21ae7a4d 24235 default:
7d79de9a
TT
24236 if (computed == nullptr)
24237 {
24238 const char *name = get_DW_OP_name (op);
21ae7a4d 24239
7d79de9a
TT
24240 if (name)
24241 complaint (_("unsupported stack op: '%s'"),
24242 name);
24243 else
24244 complaint (_("unsupported stack op: '%02x'"),
24245 op);
24246 }
21ae7a4d
JK
24247
24248 return (stack[stacki]);
d53d4ac5 24249 }
3c6e0cb3 24250
21ae7a4d 24251 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24252 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24253 if (stacki >= ARRAY_SIZE (stack) - 1)
24254 {
7d79de9a
TT
24255 if (computed == nullptr)
24256 complaint (_("location description stack overflow"));
21ae7a4d
JK
24257 return 0;
24258 }
24259
24260 if (stacki <= 0)
24261 {
7d79de9a
TT
24262 if (computed == nullptr)
24263 complaint (_("location description stack underflow"));
21ae7a4d
JK
24264 return 0;
24265 }
24266 }
7d79de9a
TT
24267
24268 if (computed != nullptr)
24269 *computed = true;
21ae7a4d 24270 return (stack[stacki]);
c906108c
SS
24271}
24272
24273/* memory allocation interface */
24274
c906108c 24275static struct dwarf_block *
7b5a2f43 24276dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24277{
8d749320 24278 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24279}
24280
c906108c 24281static struct die_info *
b60c80d6 24282dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24283{
24284 struct die_info *die;
b60c80d6
DJ
24285 size_t size = sizeof (struct die_info);
24286
24287 if (num_attrs > 1)
24288 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24289
b60c80d6 24290 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24291 memset (die, 0, sizeof (struct die_info));
24292 return (die);
24293}
2e276125
JB
24294
24295\f
a036ba48 24296
c90ec28a 24297/* Macro support. */
cf2c3c16 24298
9eac9650
TT
24299/* An overload of dwarf_decode_macros that finds the correct section
24300 and ensures it is read in before calling the other overload. */
24301
24302static void
24303dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24304 int section_is_gnu)
24305{
976ca316
SM
24306 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24307 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24308 const struct line_header *lh = cu->line_header;
9eac9650
TT
24309 unsigned int offset_size = cu->header.offset_size;
24310 struct dwarf2_section_info *section;
24311 const char *section_name;
24312
24313 if (cu->dwo_unit != nullptr)
24314 {
24315 if (section_is_gnu)
24316 {
24317 section = &cu->dwo_unit->dwo_file->sections.macro;
24318 section_name = ".debug_macro.dwo";
24319 }
24320 else
24321 {
24322 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24323 section_name = ".debug_macinfo.dwo";
24324 }
24325 }
24326 else
24327 {
24328 if (section_is_gnu)
24329 {
976ca316 24330 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24331 section_name = ".debug_macro";
24332 }
24333 else
24334 {
976ca316 24335 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24336 section_name = ".debug_macinfo";
24337 }
24338 }
24339
24340 section->read (objfile);
24341 if (section->buffer == nullptr)
24342 {
24343 complaint (_("missing %s section"), section_name);
24344 return;
24345 }
24346
24347 buildsym_compunit *builder = cu->get_builder ();
24348
048fde1e 24349 struct dwarf2_section_info *str_offsets_section;
24350 struct dwarf2_section_info *str_section;
24351 ULONGEST str_offsets_base;
24352
24353 if (cu->dwo_unit != nullptr)
24354 {
24355 str_offsets_section = &cu->dwo_unit->dwo_file
24356 ->sections.str_offsets;
24357 str_section = &cu->dwo_unit->dwo_file->sections.str;
24358 str_offsets_base = cu->header.addr_size;
24359 }
24360 else
24361 {
24362 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24363 str_section = &per_objfile->per_bfd->str;
24364 str_offsets_base = *cu->str_offsets_base;
24365 }
24366
976ca316 24367 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24368 offset_size, offset, str_section, str_offsets_section,
24369 str_offsets_base, section_is_gnu);
9eac9650
TT
24370}
24371
3019eac3
DE
24372/* Return the .debug_loc section to use for CU.
24373 For DWO files use .debug_loc.dwo. */
24374
24375static struct dwarf2_section_info *
24376cu_debug_loc_section (struct dwarf2_cu *cu)
24377{
976ca316 24378 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24379
3019eac3 24380 if (cu->dwo_unit)
43988095
JK
24381 {
24382 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24383
43988095
JK
24384 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24385 }
976ca316
SM
24386 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24387 : &per_objfile->per_bfd->loc);
3019eac3
DE
24388}
24389
d0ce17d8
CT
24390/* Return the .debug_rnglists section to use for CU. */
24391static struct dwarf2_section_info *
24392cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24393{
24394 if (cu->header.version < 5)
24395 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24396 cu->header.version);
24397 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24398
24399 /* Make sure we read the .debug_rnglists section from the file that
24400 contains the DW_AT_ranges attribute we are reading. Normally that
24401 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24402 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24403 program. */
24404 if (cu->dwo_unit != nullptr
24405 && tag != DW_TAG_compile_unit
24406 && tag != DW_TAG_skeleton_unit)
24407 {
24408 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24409
24410 if (sections->rnglists.size > 0)
24411 return &sections->rnglists;
24412 else
24413 error (_(".debug_rnglists section is missing from .dwo file."));
24414 }
24415 return &dwarf2_per_objfile->per_bfd->rnglists;
24416}
24417
8cf6f0b1
TT
24418/* A helper function that fills in a dwarf2_loclist_baton. */
24419
24420static void
24421fill_in_loclist_baton (struct dwarf2_cu *cu,
24422 struct dwarf2_loclist_baton *baton,
ff39bb5e 24423 const struct attribute *attr)
8cf6f0b1 24424{
976ca316 24425 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24426 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24427
976ca316 24428 section->read (per_objfile->objfile);
8cf6f0b1 24429
976ca316 24430 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24431 baton->per_cu = cu->per_cu;
24432 gdb_assert (baton->per_cu);
24433 /* We don't know how long the location list is, but make sure we
24434 don't run off the edge of the section. */
d4df075e
TT
24435 baton->size = section->size - attr->as_unsigned ();
24436 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24437 if (cu->base_address.has_value ())
24438 baton->base_address = *cu->base_address;
24439 else
24440 baton->base_address = 0;
f664829e 24441 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24442}
24443
4c2df51b 24444static void
ff39bb5e 24445dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24446 struct dwarf2_cu *cu, int is_block)
4c2df51b 24447{
976ca316
SM
24448 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24449 struct objfile *objfile = per_objfile->objfile;
3019eac3 24450 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24451
cd6c91b4 24452 if (attr->form_is_section_offset ()
3019eac3 24453 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24454 the section. If so, fall through to the complaint in the
24455 other branch. */
d4df075e 24456 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24457 {
0d53c4c4 24458 struct dwarf2_loclist_baton *baton;
4c2df51b 24459
8d749320 24460 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24461
8cf6f0b1 24462 fill_in_loclist_baton (cu, baton, attr);
be391dca 24463
2b24b6e4 24464 if (!cu->base_address.has_value ())
b98664d3 24465 complaint (_("Location list used without "
3e43a32a 24466 "specifying the CU base address."));
4c2df51b 24467
f1e6e072
TT
24468 SYMBOL_ACLASS_INDEX (sym) = (is_block
24469 ? dwarf2_loclist_block_index
24470 : dwarf2_loclist_index);
0d53c4c4
DJ
24471 SYMBOL_LOCATION_BATON (sym) = baton;
24472 }
24473 else
24474 {
24475 struct dwarf2_locexpr_baton *baton;
24476
8d749320 24477 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24478 baton->per_objfile = per_objfile;
ae0d2f24
UW
24479 baton->per_cu = cu->per_cu;
24480 gdb_assert (baton->per_cu);
0d53c4c4 24481
4fc6c0d5 24482 if (attr->form_is_block ())
0d53c4c4
DJ
24483 {
24484 /* Note that we're just copying the block's data pointer
24485 here, not the actual data. We're still pointing into the
6502dd73
DJ
24486 info_buffer for SYM's objfile; right now we never release
24487 that buffer, but when we do clean up properly this may
24488 need to change. */
9d2246fc
TT
24489 struct dwarf_block *block = attr->as_block ();
24490 baton->size = block->size;
24491 baton->data = block->data;
0d53c4c4
DJ
24492 }
24493 else
24494 {
24495 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24496 sym->natural_name ());
0d53c4c4 24497 baton->size = 0;
0d53c4c4 24498 }
6e70227d 24499
f1e6e072
TT
24500 SYMBOL_ACLASS_INDEX (sym) = (is_block
24501 ? dwarf2_locexpr_block_index
24502 : dwarf2_locexpr_index);
0d53c4c4
DJ
24503 SYMBOL_LOCATION_BATON (sym) = baton;
24504 }
4c2df51b 24505}
6502dd73 24506
2e6a9f79 24507/* See read.h. */
96408a79 24508
2e6a9f79
SM
24509const comp_unit_head *
24510dwarf2_per_cu_data::get_header () const
96408a79 24511{
2e6a9f79
SM
24512 if (!m_header_read_in)
24513 {
24514 const gdb_byte *info_ptr
24515 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24516
2e6a9f79 24517 memset (&m_header, 0, sizeof (m_header));
96408a79 24518
2e6a9f79
SM
24519 read_comp_unit_head (&m_header, info_ptr, this->section,
24520 rcuh_kind::COMPILE);
a9f172c6
TT
24521
24522 m_header_read_in = true;
2e6a9f79 24523 }
96408a79 24524
2e6a9f79 24525 return &m_header;
96408a79
SA
24526}
24527
09ba997f 24528/* See read.h. */
ae0d2f24 24529
98714339 24530int
09ba997f 24531dwarf2_per_cu_data::addr_size () const
ae0d2f24 24532{
2e6a9f79 24533 return this->get_header ()->addr_size;
ae0d2f24
UW
24534}
24535
09ba997f 24536/* See read.h. */
9eae7c52
TT
24537
24538int
09ba997f 24539dwarf2_per_cu_data::offset_size () const
9eae7c52 24540{
2e6a9f79 24541 return this->get_header ()->offset_size;
96408a79
SA
24542}
24543
09ba997f 24544/* See read.h. */
96408a79
SA
24545
24546int
09ba997f 24547dwarf2_per_cu_data::ref_addr_size () const
96408a79 24548{
2e6a9f79 24549 const comp_unit_head *header = this->get_header ();
96408a79 24550
2e6a9f79
SM
24551 if (header->version == 2)
24552 return header->addr_size;
96408a79 24553 else
2e6a9f79 24554 return header->offset_size;
181cebd4
JK
24555}
24556
09ba997f 24557/* See read.h. */
9aa1f1e3 24558
09ba997f 24559struct type *
293e7e51 24560dwarf2_cu::addr_type () const
9a49df9d 24561{
293e7e51 24562 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24563 struct type *void_type = objfile_type (objfile)->builtin_void;
24564 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24565 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24566
24567 if (TYPE_LENGTH (addr_type) == addr_size)
24568 return addr_type;
24569
c6d940a9 24570 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24571 return addr_type;
24572}
24573
22b6cd70
TT
24574/* A helper function for dwarf2_find_containing_comp_unit that returns
24575 the index of the result, and that searches a vector. It will
24576 return a result even if the offset in question does not actually
24577 occur in any CU. This is separate so that it can be unit
24578 tested. */
ae038cb0 24579
22b6cd70
TT
24580static int
24581dwarf2_find_containing_comp_unit
24582 (sect_offset sect_off,
24583 unsigned int offset_in_dwz,
0d305d5c 24584 const std::vector<std::unique_ptr<dwarf2_per_cu_data>> &all_comp_units)
ae038cb0 24585{
ae038cb0
DJ
24586 int low, high;
24587
ae038cb0 24588 low = 0;
22b6cd70 24589 high = all_comp_units.size () - 1;
ae038cb0
DJ
24590 while (high > low)
24591 {
36586728 24592 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24593 int mid = low + (high - low) / 2;
9a619af0 24594
0d305d5c 24595 mid_cu = all_comp_units[mid].get ();
36586728 24596 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24597 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24598 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24599 high = mid;
24600 else
24601 low = mid + 1;
24602 }
24603 gdb_assert (low == high);
22b6cd70
TT
24604 return low;
24605}
24606
24607/* Locate the .debug_info compilation unit from CU's objfile which contains
24608 the DIE at OFFSET. Raises an error on failure. */
24609
24610static struct dwarf2_per_cu_data *
24611dwarf2_find_containing_comp_unit (sect_offset sect_off,
24612 unsigned int offset_in_dwz,
976ca316 24613 dwarf2_per_objfile *per_objfile)
22b6cd70 24614{
976ca316
SM
24615 int low = dwarf2_find_containing_comp_unit
24616 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
0d305d5c
TT
24617 dwarf2_per_cu_data *this_cu
24618 = per_objfile->per_bfd->all_comp_units[low].get ();
22b6cd70 24619
45b8ae0c 24620 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24621 {
36586728 24622 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24623 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24624 "offset %s [in module %s]"),
24625 sect_offset_str (sect_off),
976ca316 24626 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24627
976ca316 24628 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24629 <= sect_off);
0d305d5c 24630 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
ae038cb0
DJ
24631 }
24632 else
24633 {
976ca316 24634 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24635 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24636 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24637 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24638 return this_cu;
24639 }
24640}
24641
22b6cd70
TT
24642#if GDB_SELF_TEST
24643
24644namespace selftests {
24645namespace find_containing_comp_unit {
24646
24647static void
24648run_test ()
24649{
0d305d5c
TT
24650 std::unique_ptr<dwarf2_per_cu_data> one (new dwarf2_per_cu_data);
24651 dwarf2_per_cu_data *one_ptr = one.get ();
24652 std::unique_ptr<dwarf2_per_cu_data> two (new dwarf2_per_cu_data);
24653 dwarf2_per_cu_data *two_ptr = two.get ();
24654 std::unique_ptr<dwarf2_per_cu_data> three (new dwarf2_per_cu_data);
24655 dwarf2_per_cu_data *three_ptr = three.get ();
24656 std::unique_ptr<dwarf2_per_cu_data> four (new dwarf2_per_cu_data);
24657 dwarf2_per_cu_data *four_ptr = four.get ();
24658
24659 one->length = 5;
24660 two->sect_off = sect_offset (one->length);
24661 two->length = 7;
24662
24663 three->length = 5;
24664 three->is_dwz = 1;
24665 four->sect_off = sect_offset (three->length);
24666 four->length = 7;
24667 four->is_dwz = 1;
24668
24669 std::vector<std::unique_ptr<dwarf2_per_cu_data>> units;
24670 units.push_back (std::move (one));
24671 units.push_back (std::move (two));
24672 units.push_back (std::move (three));
24673 units.push_back (std::move (four));
22b6cd70
TT
24674
24675 int result;
24676
24677 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
0d305d5c 24678 SELF_CHECK (units[result].get () == one_ptr);
22b6cd70 24679 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
0d305d5c 24680 SELF_CHECK (units[result].get () == one_ptr);
22b6cd70 24681 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
0d305d5c 24682 SELF_CHECK (units[result].get () == two_ptr);
22b6cd70
TT
24683
24684 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
0d305d5c 24685 SELF_CHECK (units[result].get () == three_ptr);
22b6cd70 24686 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
0d305d5c 24687 SELF_CHECK (units[result].get () == three_ptr);
22b6cd70 24688 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
0d305d5c 24689 SELF_CHECK (units[result].get () == four_ptr);
22b6cd70
TT
24690}
24691
24692}
24693}
24694
24695#endif /* GDB_SELF_TEST */
24696
9e021579 24697/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 24698
9e021579
SM
24699dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24700 dwarf2_per_objfile *per_objfile)
24701 : per_cu (per_cu),
24702 per_objfile (per_objfile),
9068261f
AB
24703 mark (false),
24704 has_loclist (false),
24705 checked_producer (false),
24706 producer_is_gxx_lt_4_6 (false),
24707 producer_is_gcc_lt_4_3 (false),
eb77c9df 24708 producer_is_icc (false),
9068261f 24709 producer_is_icc_lt_14 (false),
c258c396 24710 producer_is_codewarrior (false),
9068261f 24711 processing_has_namespace_info (false)
93311388 24712{
9816fde3
JK
24713}
24714
24715/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24716
24717static void
95554aad
TT
24718prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24719 enum language pretend_language)
9816fde3
JK
24720{
24721 struct attribute *attr;
24722
24723 /* Set the language we're debugging. */
24724 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24725 if (attr != nullptr)
529908cb 24726 set_cu_language (attr->constant_value (0), cu);
9816fde3 24727 else
9cded63f 24728 {
95554aad 24729 cu->language = pretend_language;
9cded63f
TT
24730 cu->language_defn = language_def (cu->language);
24731 }
dee91e82 24732
7d45c7c3 24733 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24734}
24735
7188ed02 24736/* See read.h. */
ae038cb0 24737
7188ed02
SM
24738dwarf2_cu *
24739dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24740{
7188ed02
SM
24741 auto it = m_dwarf2_cus.find (per_cu);
24742 if (it == m_dwarf2_cus.end ())
24743 return nullptr;
ae038cb0 24744
7188ed02
SM
24745 return it->second;
24746}
24747
24748/* See read.h. */
24749
24750void
24751dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24752{
24753 gdb_assert (this->get_cu (per_cu) == nullptr);
24754
24755 m_dwarf2_cus[per_cu] = cu;
24756}
24757
24758/* See read.h. */
24759
24760void
24761dwarf2_per_objfile::age_comp_units ()
24762{
17e593e9
SM
24763 dwarf_read_debug_printf_v ("running");
24764
08ac5771
SM
24765 /* This is not expected to be called in the middle of CU expansion. There is
24766 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24767 loaded in memory. Calling age_comp_units while the queue is in use could
24768 make us free the DIEs for a CU that is in the queue and therefore break
24769 that invariant. */
24770 gdb_assert (!this->per_bfd->queue.has_value ());
24771
7188ed02
SM
24772 /* Start by clearing all marks. */
24773 for (auto pair : m_dwarf2_cus)
24774 pair.second->mark = false;
24775
24776 /* Traverse all CUs, mark them and their dependencies if used recently
24777 enough. */
24778 for (auto pair : m_dwarf2_cus)
ae038cb0 24779 {
7188ed02
SM
24780 dwarf2_cu *cu = pair.second;
24781
24782 cu->last_used++;
24783 if (cu->last_used <= dwarf_max_cache_age)
24784 dwarf2_mark (cu);
ae038cb0
DJ
24785 }
24786
7188ed02
SM
24787 /* Delete all CUs still not marked. */
24788 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 24789 {
7188ed02 24790 dwarf2_cu *cu = it->second;
ae038cb0 24791
7188ed02 24792 if (!cu->mark)
ae038cb0 24793 {
17e593e9
SM
24794 dwarf_read_debug_printf_v ("deleting old CU %s",
24795 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
24796 delete cu;
24797 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
24798 }
24799 else
7188ed02 24800 it++;
ae038cb0
DJ
24801 }
24802}
24803
7188ed02 24804/* See read.h. */
ae038cb0 24805
7188ed02
SM
24806void
24807dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24808{
7188ed02
SM
24809 auto it = m_dwarf2_cus.find (per_cu);
24810 if (it == m_dwarf2_cus.end ())
24811 return;
ae038cb0 24812
7188ed02 24813 delete it->second;
ae038cb0 24814
7188ed02
SM
24815 m_dwarf2_cus.erase (it);
24816}
ae038cb0 24817
7188ed02
SM
24818dwarf2_per_objfile::~dwarf2_per_objfile ()
24819{
24820 remove_all_cus ();
ae038cb0
DJ
24821}
24822
dee91e82
DE
24823/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24824 We store these in a hash table separate from the DIEs, and preserve them
24825 when the DIEs are flushed out of cache.
24826
24827 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24828 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24829 or the type may come from a DWO file. Furthermore, while it's more logical
24830 to use per_cu->section+offset, with Fission the section with the data is in
24831 the DWO file but we don't know that section at the point we need it.
24832 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24833 because we can enter the lookup routine, get_die_type_at_offset, from
24834 outside this file, and thus won't necessarily have PER_CU->cu.
24835 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24836
dee91e82 24837struct dwarf2_per_cu_offset_and_type
1c379e20 24838{
dee91e82 24839 const struct dwarf2_per_cu_data *per_cu;
9c541725 24840 sect_offset sect_off;
1c379e20
DJ
24841 struct type *type;
24842};
24843
dee91e82 24844/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24845
24846static hashval_t
dee91e82 24847per_cu_offset_and_type_hash (const void *item)
1c379e20 24848{
9a3c8263
SM
24849 const struct dwarf2_per_cu_offset_and_type *ofs
24850 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24851
9c541725 24852 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24853}
24854
dee91e82 24855/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24856
24857static int
dee91e82 24858per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24859{
9a3c8263
SM
24860 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24861 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24862 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24863 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24864
dee91e82 24865 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24866 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24867}
24868
24869/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24870 table if necessary. For convenience, return TYPE.
24871
24872 The DIEs reading must have careful ordering to:
85102364 24873 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24874 reading current DIE.
24875 * Not trying to dereference contents of still incompletely read in types
24876 while reading in other DIEs.
24877 * Enable referencing still incompletely read in types just by a pointer to
24878 the type without accessing its fields.
24879
24880 Therefore caller should follow these rules:
24881 * Try to fetch any prerequisite types we may need to build this DIE type
24882 before building the type and calling set_die_type.
e71ec853 24883 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24884 possible before fetching more types to complete the current type.
24885 * Make the type as complete as possible before fetching more types. */
1c379e20 24886
f792889a 24887static struct type *
57567375
TT
24888set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24889 bool skip_data_location)
1c379e20 24890{
976ca316 24891 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 24892 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 24893 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
24894 struct attribute *attr;
24895 struct dynamic_prop prop;
1c379e20 24896
b4ba55a1
JB
24897 /* For Ada types, make sure that the gnat-specific data is always
24898 initialized (if not already set). There are a few types where
24899 we should not be doing so, because the type-specific area is
24900 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24901 where the type-specific area is used to store the floatformat).
24902 But this is not a problem, because the gnat-specific information
24903 is actually not needed for these types. */
24904 if (need_gnat_info (cu)
78134374
SM
24905 && type->code () != TYPE_CODE_FUNC
24906 && type->code () != TYPE_CODE_FLT
24907 && type->code () != TYPE_CODE_METHODPTR
24908 && type->code () != TYPE_CODE_MEMBERPTR
24909 && type->code () != TYPE_CODE_METHOD
09584414 24910 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
24911 && !HAVE_GNAT_AUX_INFO (type))
24912 INIT_GNAT_SPECIFIC (type);
24913
3f2f83dd
KB
24914 /* Read DW_AT_allocated and set in type. */
24915 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 24916 if (attr != NULL)
3f2f83dd 24917 {
293e7e51 24918 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24919 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24920 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 24921 }
3f2f83dd
KB
24922
24923 /* Read DW_AT_associated and set in type. */
24924 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 24925 if (attr != NULL)
3f2f83dd 24926 {
293e7e51 24927 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24928 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 24929 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 24930 }
3f2f83dd 24931
3cdcd0ce 24932 /* Read DW_AT_data_location and set in type. */
57567375
TT
24933 if (!skip_data_location)
24934 {
24935 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24936 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24937 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24938 }
3cdcd0ce 24939
976ca316
SM
24940 if (per_objfile->die_type_hash == NULL)
24941 per_objfile->die_type_hash
0335378b
TT
24942 = htab_up (htab_create_alloc (127,
24943 per_cu_offset_and_type_hash,
24944 per_cu_offset_and_type_eq,
24945 NULL, xcalloc, xfree));
1c379e20 24946
dee91e82 24947 ofs.per_cu = cu->per_cu;
9c541725 24948 ofs.sect_off = die->sect_off;
1c379e20 24949 ofs.type = type;
dee91e82 24950 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 24951 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24952 if (*slot)
b98664d3 24953 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24954 sect_offset_str (die->sect_off));
8d749320
SM
24955 *slot = XOBNEW (&objfile->objfile_obstack,
24956 struct dwarf2_per_cu_offset_and_type);
1c379e20 24957 **slot = ofs;
f792889a 24958 return type;
1c379e20
DJ
24959}
24960
9c541725 24961/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24962 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24963
24964static struct type *
9c541725 24965get_die_type_at_offset (sect_offset sect_off,
aa66c379 24966 dwarf2_per_cu_data *per_cu,
976ca316 24967 dwarf2_per_objfile *per_objfile)
1c379e20 24968{
dee91e82 24969 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 24970
976ca316 24971 if (per_objfile->die_type_hash == NULL)
f792889a 24972 return NULL;
1c379e20 24973
dee91e82 24974 ofs.per_cu = per_cu;
9c541725 24975 ofs.sect_off = sect_off;
9a3c8263 24976 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 24977 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24978 if (slot)
24979 return slot->type;
24980 else
24981 return NULL;
24982}
24983
02142a6c 24984/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24985 or return NULL if DIE does not have a saved type. */
24986
24987static struct type *
24988get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24989{
aa66c379 24990 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
24991}
24992
10b3939b
DJ
24993/* Add a dependence relationship from CU to REF_PER_CU. */
24994
24995static void
24996dwarf2_add_dependence (struct dwarf2_cu *cu,
24997 struct dwarf2_per_cu_data *ref_per_cu)
24998{
24999 void **slot;
25000
25001 if (cu->dependencies == NULL)
25002 cu->dependencies
25003 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25004 NULL, &cu->comp_unit_obstack,
25005 hashtab_obstack_allocate,
25006 dummy_obstack_deallocate);
25007
25008 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25009 if (*slot == NULL)
25010 *slot = ref_per_cu;
25011}
1c379e20 25012
f504f079
DE
25013/* Subroutine of dwarf2_mark to pass to htab_traverse.
25014 Set the mark field in every compilation unit in the
7188ed02
SM
25015 cache that we must keep because we are keeping CU.
25016
25017 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 25018
10b3939b
DJ
25019static int
25020dwarf2_mark_helper (void **slot, void *data)
25021{
7188ed02
SM
25022 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25023 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25024 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25025
25026 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25027 reading of the chain. As such dependencies remain valid it is not much
25028 useful to track and undo them during QUIT cleanups. */
7188ed02 25029 if (cu == nullptr)
d07ed419
JK
25030 return 1;
25031
7188ed02 25032 if (cu->mark)
10b3939b 25033 return 1;
10b3939b 25034
7188ed02
SM
25035 cu->mark = true;
25036
25037 if (cu->dependencies != nullptr)
25038 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25039
25040 return 1;
25041}
25042
f504f079
DE
25043/* Set the mark field in CU and in every other compilation unit in the
25044 cache that we must keep because we are keeping CU. */
25045
ae038cb0
DJ
25046static void
25047dwarf2_mark (struct dwarf2_cu *cu)
25048{
25049 if (cu->mark)
25050 return;
7188ed02 25051
9068261f 25052 cu->mark = true;
ae038cb0 25053
7188ed02
SM
25054 if (cu->dependencies != nullptr)
25055 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25056}
25057
72bf9492
DJ
25058/* Trivial hash function for partial_die_info: the hash value of a DIE
25059 is its offset in .debug_info for this objfile. */
25060
25061static hashval_t
25062partial_die_hash (const void *item)
25063{
9a3c8263
SM
25064 const struct partial_die_info *part_die
25065 = (const struct partial_die_info *) item;
9a619af0 25066
9c541725 25067 return to_underlying (part_die->sect_off);
72bf9492
DJ
25068}
25069
25070/* Trivial comparison function for partial_die_info structures: two DIEs
25071 are equal if they have the same offset. */
25072
25073static int
25074partial_die_eq (const void *item_lhs, const void *item_rhs)
25075{
9a3c8263
SM
25076 const struct partial_die_info *part_die_lhs
25077 = (const struct partial_die_info *) item_lhs;
25078 const struct partial_die_info *part_die_rhs
25079 = (const struct partial_die_info *) item_rhs;
9a619af0 25080
9c541725 25081 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25082}
25083
3c3bb058
AB
25084struct cmd_list_element *set_dwarf_cmdlist;
25085struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25086
9291a0cd 25087static void
cd4fb1b2
SM
25088show_check_physname (struct ui_file *file, int from_tty,
25089 struct cmd_list_element *c, const char *value)
9291a0cd 25090{
cd4fb1b2
SM
25091 fprintf_filtered (file,
25092 _("Whether to check \"physname\" is %s.\n"),
25093 value);
9291a0cd
TT
25094}
25095
6c265988 25096void _initialize_dwarf2_read ();
cd4fb1b2 25097void
6c265988 25098_initialize_dwarf2_read ()
9291a0cd 25099{
0743fc83 25100 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25101Set DWARF specific variables.\n\
590042fc 25102Configure DWARF variables such as the cache size."),
2f822da5 25103 &set_dwarf_cmdlist,
0743fc83 25104 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25105
0743fc83 25106 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25107Show DWARF specific variables.\n\
25108Show DWARF variables such as the cache size."),
2f822da5 25109 &show_dwarf_cmdlist,
0743fc83 25110 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25111
cd4fb1b2
SM
25112 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25113 &dwarf_max_cache_age, _("\
25114Set the upper bound on the age of cached DWARF compilation units."), _("\
25115Show the upper bound on the age of cached DWARF compilation units."), _("\
25116A higher limit means that cached compilation units will be stored\n\
25117in memory longer, and more total memory will be used. Zero disables\n\
25118caching, which can slow down startup."),
25119 NULL,
25120 show_dwarf_max_cache_age,
25121 &set_dwarf_cmdlist,
25122 &show_dwarf_cmdlist);
156942c7 25123
cd4fb1b2
SM
25124 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25125Set debugging of the DWARF reader."), _("\
25126Show debugging of the DWARF reader."), _("\
25127When enabled (non-zero), debugging messages are printed during DWARF\n\
25128reading and symtab expansion. A value of 1 (one) provides basic\n\
25129information. A value greater than 1 provides more verbose information."),
25130 NULL,
25131 NULL,
25132 &setdebuglist, &showdebuglist);
9291a0cd 25133
cd4fb1b2
SM
25134 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25135Set debugging of the DWARF DIE reader."), _("\
25136Show debugging of the DWARF DIE reader."), _("\
25137When enabled (non-zero), DIEs are dumped after they are read in.\n\
25138The value is the maximum depth to print."),
25139 NULL,
25140 NULL,
25141 &setdebuglist, &showdebuglist);
9291a0cd 25142
cd4fb1b2
SM
25143 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25144Set debugging of the dwarf line reader."), _("\
25145Show debugging of the dwarf line reader."), _("\
25146When enabled (non-zero), line number entries are dumped as they are read in.\n\
25147A value of 1 (one) provides basic information.\n\
25148A value greater than 1 provides more verbose information."),
25149 NULL,
25150 NULL,
25151 &setdebuglist, &showdebuglist);
437afbb8 25152
cd4fb1b2
SM
25153 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25154Set cross-checking of \"physname\" code against demangler."), _("\
25155Show cross-checking of \"physname\" code against demangler."), _("\
25156When enabled, GDB's internal \"physname\" code is checked against\n\
25157the demangler."),
25158 NULL, show_check_physname,
25159 &setdebuglist, &showdebuglist);
900e11f9 25160
e615022a
DE
25161 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25162 no_class, &use_deprecated_index_sections, _("\
25163Set whether to use deprecated gdb_index sections."), _("\
25164Show whether to use deprecated gdb_index sections."), _("\
25165When enabled, deprecated .gdb_index sections are used anyway.\n\
25166Normally they are ignored either because of a missing feature or\n\
25167performance issue.\n\
25168Warning: This option must be enabled before gdb reads the file."),
25169 NULL,
25170 NULL,
25171 &setlist, &showlist);
25172
f1e6e072
TT
25173 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25174 &dwarf2_locexpr_funcs);
25175 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25176 &dwarf2_loclist_funcs);
25177
25178 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25179 &dwarf2_block_frame_base_locexpr_funcs);
25180 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25181 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25182
25183#if GDB_SELF_TEST
25184 selftests::register_test ("dw2_expand_symtabs_matching",
25185 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25186 selftests::register_test ("dwarf2_find_containing_comp_unit",
25187 selftests::find_containing_comp_unit::run_test);
c62446b1 25188#endif
6502dd73 25189}
This page took 5.911086 seconds and 4 git commands to generate.