x86: TLS desc call relocs target zero-size fields
[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"
437afbb8 91
73be47f5
DE
92/* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
b4f54984
DE
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95static unsigned int dwarf_read_debug = 0;
45cfd468 96
6f738b01
SM
97/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
98
99#define dwarf_read_debug_printf(fmt, ...) \
74b773fc
SM
100 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
101 ##__VA_ARGS__)
6f738b01
SM
102
103/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
104
105#define dwarf_read_debug_printf_v(fmt, ...) \
74b773fc
SM
106 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
107 ##__VA_ARGS__)
6f738b01 108
d97bc12b 109/* When non-zero, dump DIEs after they are read in. */
b4f54984 110static unsigned int dwarf_die_debug = 0;
d97bc12b 111
27e0867f 112/* When non-zero, dump line number entries as they are read in. */
8fdd972c 113unsigned int dwarf_line_debug = 0;
27e0867f 114
491144b5
CB
115/* When true, cross-check physname against demangler. */
116static bool check_physname = false;
900e11f9 117
491144b5
CB
118/* When true, do not reject deprecated .gdb_index sections. */
119static bool use_deprecated_index_sections = false;
481860b3 120
17ee85fc
TT
121/* This is used to store the data that is always per objfile. */
122static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
123
124/* These are used to store the dwarf2_per_bfd objects.
125
126 objfiles having the same BFD, which doesn't require relocations, are going to
127 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
128
129 Other objfiles are not going to share a dwarf2_per_bfd with any other
130 objfiles, so they'll have their own version kept in the _objfile_data_key
131 version. */
132static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
133static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 134
f1e6e072
TT
135/* The "aclass" indices for various kinds of computed DWARF symbols. */
136
137static int dwarf2_locexpr_index;
138static int dwarf2_loclist_index;
139static int dwarf2_locexpr_block_index;
140static int dwarf2_loclist_block_index;
141
41144253 142/* Size of .debug_loclists section header for 32-bit DWARF format. */
143#define LOCLIST_HEADER_SIZE32 12
144
145/* Size of .debug_loclists section header for 64-bit DWARF format. */
146#define LOCLIST_HEADER_SIZE64 20
147
d0ce17d8
CT
148/* Size of .debug_rnglists section header for 32-bit DWARF format. */
149#define RNGLIST_HEADER_SIZE32 12
150
151/* Size of .debug_rnglists section header for 64-bit DWARF format. */
152#define RNGLIST_HEADER_SIZE64 20
153
3f563c84
PA
154/* An index into a (C++) symbol name component in a symbol name as
155 recorded in the mapped_index's symbol table. For each C++ symbol
156 in the symbol table, we record one entry for the start of each
157 component in the symbol in a table of name components, and then
158 sort the table, in order to be able to binary search symbol names,
159 ignoring leading namespaces, both completion and regular look up.
160 For example, for symbol "A::B::C", we'll have an entry that points
161 to "A::B::C", another that points to "B::C", and another for "C".
162 Note that function symbols in GDB index have no parameter
163 information, just the function/method names. You can convert a
164 name_component to a "const char *" using the
165 'mapped_index::symbol_name_at(offset_type)' method. */
166
167struct name_component
168{
169 /* Offset in the symbol name where the component starts. Stored as
170 a (32-bit) offset instead of a pointer to save memory and improve
171 locality on 64-bit architectures. */
172 offset_type name_offset;
173
174 /* The symbol's index in the symbol and constant pool tables of a
175 mapped_index. */
176 offset_type idx;
177};
178
44ed8f3e
PA
179/* Base class containing bits shared by both .gdb_index and
180 .debug_name indexes. */
181
182struct mapped_index_base
183{
22ca247e
TT
184 mapped_index_base () = default;
185 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
186
44ed8f3e
PA
187 /* The name_component table (a sorted vector). See name_component's
188 description above. */
189 std::vector<name_component> name_components;
190
191 /* How NAME_COMPONENTS is sorted. */
192 enum case_sensitivity name_components_casing;
193
194 /* Return the number of names in the symbol table. */
195 virtual size_t symbol_name_count () const = 0;
196
197 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
198 virtual const char *symbol_name_at
199 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
200
201 /* Return whether the name at IDX in the symbol table should be
202 ignored. */
203 virtual bool symbol_name_slot_invalid (offset_type idx) const
204 {
205 return false;
206 }
207
208 /* Build the symbol name component sorted vector, if we haven't
209 yet. */
fcf23d5b 210 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
211
212 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
213 possible matches for LN_NO_PARAMS in the name component
214 vector. */
215 std::pair<std::vector<name_component>::const_iterator,
216 std::vector<name_component>::const_iterator>
3b00ef10 217 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
218 enum language lang,
219 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
220
221 /* Prevent deleting/destroying via a base class pointer. */
222protected:
223 ~mapped_index_base() = default;
224};
225
42c2c694
TT
226/* This is a view into the index that converts from bytes to an
227 offset_type, and allows indexing. Unaligned bytes are specifically
228 allowed here, and handled via unpacking. */
229
230class offset_view
231{
232public:
233 offset_view () = default;
234
235 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
236 : m_bytes (bytes)
237 {
238 }
239
240 /* Extract the INDEXth offset_type from the array. */
241 offset_type operator[] (size_t index) const
242 {
243 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
244 return (offset_type) extract_unsigned_integer (bytes,
245 sizeof (offset_type),
246 BFD_ENDIAN_LITTLE);
247 }
248
249 /* Return the number of offset_types in this array. */
250 size_t size () const
251 {
252 return m_bytes.size () / sizeof (offset_type);
253 }
254
255 /* Return true if this view is empty. */
256 bool empty () const
257 {
258 return m_bytes.empty ();
259 }
260
261private:
262 /* The underlying bytes. */
263 gdb::array_view<const gdb_byte> m_bytes;
264};
265
9291a0cd
TT
266/* A description of the mapped index. The file format is described in
267 a comment by the code that writes the index. */
fc898b42 268struct mapped_index final : public mapped_index_base
9291a0cd 269{
559a7a62 270 /* Index data format version. */
3063847f 271 int version = 0;
559a7a62 272
f00a2de2
PA
273 /* The address table data. */
274 gdb::array_view<const gdb_byte> address_table;
b11b1f88 275
3876f04e 276 /* The symbol table, implemented as a hash table. */
42c2c694 277 offset_view symbol_table;
b11b1f88 278
9291a0cd 279 /* A pointer to the constant pool. */
42c2c694
TT
280 gdb::array_view<const gdb_byte> constant_pool;
281
282 /* Return the index into the constant pool of the name of the IDXth
283 symbol in the symbol table. */
284 offset_type symbol_name_index (offset_type idx) const
285 {
286 return symbol_table[2 * idx];
287 }
288
289 /* Return the index into the constant pool of the CU vector of the
290 IDXth symbol in the symbol table. */
291 offset_type symbol_vec_index (offset_type idx) const
292 {
293 return symbol_table[2 * idx + 1];
294 }
3f563c84 295
44ed8f3e
PA
296 bool symbol_name_slot_invalid (offset_type idx) const override
297 {
42c2c694
TT
298 return (symbol_name_index (idx) == 0
299 && symbol_vec_index (idx) == 0);
44ed8f3e 300 }
5c58de74 301
3f563c84
PA
302 /* Convenience method to get at the name of the symbol at IDX in the
303 symbol table. */
fcf23d5b
SM
304 const char *symbol_name_at
305 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
42c2c694
TT
306 {
307 return (const char *) (this->constant_pool.data ()
308 + symbol_name_index (idx));
309 }
5c58de74 310
44ed8f3e 311 size_t symbol_name_count () const override
42c2c694 312 { return this->symbol_table.size () / 2; }
9291a0cd
TT
313};
314
927aa2e7
JK
315/* A description of the mapped .debug_names.
316 Uninitialized map has CU_COUNT 0. */
fc898b42 317struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
318{
319 bfd_endian dwarf5_byte_order;
320 bool dwarf5_is_dwarf64;
321 bool augmentation_is_gdb;
322 uint8_t offset_size;
323 uint32_t cu_count = 0;
324 uint32_t tu_count, bucket_count, name_count;
325 const gdb_byte *cu_table_reordered, *tu_table_reordered;
326 const uint32_t *bucket_table_reordered, *hash_table_reordered;
327 const gdb_byte *name_table_string_offs_reordered;
328 const gdb_byte *name_table_entry_offs_reordered;
329 const gdb_byte *entry_pool;
330
331 struct index_val
332 {
333 ULONGEST dwarf_tag;
334 struct attr
335 {
336 /* Attribute name DW_IDX_*. */
337 ULONGEST dw_idx;
338
339 /* Attribute form DW_FORM_*. */
340 ULONGEST form;
341
342 /* Value if FORM is DW_FORM_implicit_const. */
343 LONGEST implicit_const;
344 };
345 std::vector<attr> attr_vec;
346 };
347
348 std::unordered_map<ULONGEST, index_val> abbrev_map;
349
fcf23d5b
SM
350 const char *namei_to_name
351 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
352
353 /* Implementation of the mapped_index_base virtual interface, for
354 the name_components cache. */
355
fcf23d5b
SM
356 const char *symbol_name_at
357 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
358 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
359
360 size_t symbol_name_count () const override
361 { return this->name_count; }
927aa2e7
JK
362};
363
cd4fb1b2 364/* See dwarf2read.h. */
ed2dc618 365
cd4fb1b2 366dwarf2_per_objfile *
ed2dc618
SM
367get_dwarf2_per_objfile (struct objfile *objfile)
368{
5bfd760d 369 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 370}
c906108c 371
251d32d9 372/* Default names of the debugging sections. */
c906108c 373
233a11ab
CS
374/* Note that if the debugging section has been compressed, it might
375 have a name like .zdebug_info. */
376
9938d15a 377const struct dwarf2_debug_sections dwarf2_elf_names =
9cdd5dbd 378{
251d32d9
TG
379 { ".debug_info", ".zdebug_info" },
380 { ".debug_abbrev", ".zdebug_abbrev" },
381 { ".debug_line", ".zdebug_line" },
382 { ".debug_loc", ".zdebug_loc" },
43988095 383 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 384 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 385 { ".debug_macro", ".zdebug_macro" },
251d32d9 386 { ".debug_str", ".zdebug_str" },
18a8505e 387 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 388 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 389 { ".debug_ranges", ".zdebug_ranges" },
43988095 390 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 391 { ".debug_types", ".zdebug_types" },
3019eac3 392 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
393 { ".debug_frame", ".zdebug_frame" },
394 { ".eh_frame", NULL },
24d3216f 395 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
396 { ".debug_names", ".zdebug_names" },
397 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 398 23
251d32d9 399};
c906108c 400
80626a55 401/* List of DWO/DWP sections. */
3019eac3 402
80626a55 403static const struct dwop_section_names
3019eac3
DE
404{
405 struct dwarf2_section_names abbrev_dwo;
406 struct dwarf2_section_names info_dwo;
407 struct dwarf2_section_names line_dwo;
408 struct dwarf2_section_names loc_dwo;
43988095 409 struct dwarf2_section_names loclists_dwo;
09262596
DE
410 struct dwarf2_section_names macinfo_dwo;
411 struct dwarf2_section_names macro_dwo;
d0ce17d8 412 struct dwarf2_section_names rnglists_dwo;
3019eac3
DE
413 struct dwarf2_section_names str_dwo;
414 struct dwarf2_section_names str_offsets_dwo;
415 struct dwarf2_section_names types_dwo;
80626a55
DE
416 struct dwarf2_section_names cu_index;
417 struct dwarf2_section_names tu_index;
3019eac3 418}
80626a55 419dwop_section_names =
3019eac3
DE
420{
421 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
422 { ".debug_info.dwo", ".zdebug_info.dwo" },
423 { ".debug_line.dwo", ".zdebug_line.dwo" },
424 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 425 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
426 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
427 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
d0ce17d8 428 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
3019eac3
DE
429 { ".debug_str.dwo", ".zdebug_str.dwo" },
430 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
431 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
432 { ".debug_cu_index", ".zdebug_cu_index" },
433 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
434};
435
c906108c
SS
436/* local data types */
437
d0ce17d8
CT
438/* The location list and range list sections (.debug_loclists & .debug_rnglists)
439 begin with a header, which contains the following information. */
440struct loclists_rnglists_header
41144253 441{
442 /* A 4-byte or 12-byte length containing the length of the
443 set of entries for this compilation unit, not including the
444 length field itself. */
445 unsigned int length;
446
447 /* A 2-byte version identifier. */
448 short version;
449
450 /* A 1-byte unsigned integer containing the size in bytes of an address on
451 the target system. */
452 unsigned char addr_size;
453
454 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
455 on the target system. */
456 unsigned char segment_collector_size;
457
458 /* A 4-byte count of the number of offsets that follow the header. */
459 unsigned int offset_entry_count;
460};
461
3da10d80
KS
462/* Type used for delaying computation of method physnames.
463 See comments for compute_delayed_physnames. */
464struct delayed_method_info
465{
466 /* The type to which the method is attached, i.e., its parent class. */
467 struct type *type;
468
469 /* The index of the method in the type's function fieldlists. */
470 int fnfield_index;
471
472 /* The index of the method in the fieldlist. */
473 int index;
474
475 /* The name of the DIE. */
476 const char *name;
477
478 /* The DIE associated with this method. */
479 struct die_info *die;
480};
481
e7c27a73
DJ
482/* Internal state when decoding a particular compilation unit. */
483struct dwarf2_cu
484{
9e021579
SM
485 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
486 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
487
488 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
489
c24bdb02
KS
490 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
491 Create the set of symtabs used by this TU, or if this TU is sharing
492 symtabs with another TU and the symtabs have already been created
493 then restore those symtabs in the line header.
494 We don't need the pc/line-number mapping for type units. */
495 void setup_type_unit_groups (struct die_info *die);
496
497 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
498 buildsym_compunit constructor. */
499 struct compunit_symtab *start_symtab (const char *name,
500 const char *comp_dir,
501 CORE_ADDR low_pc);
502
503 /* Reset the builder. */
504 void reset_builder () { m_builder.reset (); }
505
293e7e51
SM
506 /* Return a type that is a generic pointer type, the size of which
507 matches the address size given in the compilation unit header for
508 this CU. */
509 struct type *addr_type () const;
510
511 /* Find an integer type the same size as the address size given in
512 the compilation unit header for this CU. UNSIGNED_P controls if
513 the integer is unsigned or not. */
514 struct type *addr_sized_int_type (bool unsigned_p) const;
515
d00adf39 516 /* The header of the compilation unit. */
fcd3b13d 517 struct comp_unit_head header {};
e142c38c 518
d00adf39 519 /* Base address of this compilation unit. */
2b24b6e4 520 gdb::optional<CORE_ADDR> base_address;
d00adf39 521
e142c38c 522 /* The language we are debugging. */
fcd3b13d
SM
523 enum language language = language_unknown;
524 const struct language_defn *language_defn = nullptr;
e142c38c 525
fcd3b13d 526 const char *producer = nullptr;
b0f35d58 527
c24bdb02 528private:
804d2729
TT
529 /* The symtab builder for this CU. This is only non-NULL when full
530 symbols are being read. */
c24bdb02 531 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 532
c24bdb02 533public:
e142c38c
DJ
534 /* The generic symbol table building routines have separate lists for
535 file scope symbols and all all other scopes (local scopes). So
536 we need to select the right one to pass to add_symbol_to_list().
537 We do it by keeping a pointer to the correct list in list_in_scope.
538
539 FIXME: The original dwarf code just treated the file scope as the
540 first local scope, and all other local scopes as nested local
541 scopes, and worked fine. Check to see if we really need to
542 distinguish these in buildsym.c. */
fcd3b13d 543 struct pending **list_in_scope = nullptr;
e142c38c 544
b64f50a1
JK
545 /* Hash table holding all the loaded partial DIEs
546 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 547 htab_t partial_dies = nullptr;
72bf9492
DJ
548
549 /* Storage for things with the same lifetime as this read-in compilation
550 unit, including partial DIEs. */
fcd3b13d 551 auto_obstack comp_unit_obstack;
72bf9492 552
69d751e3 553 /* Backlink to our per_cu entry. */
ae038cb0
DJ
554 struct dwarf2_per_cu_data *per_cu;
555
9e021579 556 /* The dwarf2_per_objfile that owns this. */
976ca316 557 dwarf2_per_objfile *per_objfile;
9e021579 558
ae038cb0 559 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 560 int last_used = 0;
ae038cb0 561
b64f50a1
JK
562 /* A hash table of DIE cu_offset for following references with
563 die_info->offset.sect_off as hash. */
fcd3b13d 564 htab_t die_hash = nullptr;
10b3939b
DJ
565
566 /* Full DIEs if read in. */
fcd3b13d 567 struct die_info *dies = nullptr;
10b3939b
DJ
568
569 /* A set of pointers to dwarf2_per_cu_data objects for compilation
570 units referenced by this one. Only set during full symbol processing;
571 partial symbol tables do not have dependencies. */
fcd3b13d 572 htab_t dependencies = nullptr;
10b3939b 573
cb1df416 574 /* Header data from the line table, during full symbol processing. */
fcd3b13d 575 struct line_header *line_header = nullptr;
4c8aa72d 576 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 577 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
578 this is the DW_TAG_compile_unit die for this CU. We'll hold on
579 to the line header as long as this DIE is being processed. See
580 process_die_scope. */
fcd3b13d 581 die_info *line_header_die_owner = nullptr;
cb1df416 582
3da10d80
KS
583 /* A list of methods which need to have physnames computed
584 after all type information has been read. */
c89b44cd 585 std::vector<delayed_method_info> method_list;
3da10d80 586
96408a79 587 /* To be copied to symtab->call_site_htab. */
fcd3b13d 588 htab_t call_site_htab = nullptr;
96408a79 589
034e5797
DE
590 /* Non-NULL if this CU came from a DWO file.
591 There is an invariant here that is important to remember:
592 Except for attributes copied from the top level DIE in the "main"
593 (or "stub") file in preparation for reading the DWO file
18a8505e 594 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
595 Either there isn't a DWO file (in which case this is NULL and the point
596 is moot), or there is and either we're not going to read it (in which
597 case this is NULL) or there is and we are reading it (in which case this
598 is non-NULL). */
fcd3b13d 599 struct dwo_unit *dwo_unit = nullptr;
3019eac3 600
18a8505e 601 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 602 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 603 gdb::optional<ULONGEST> addr_base;
3019eac3 604
2b0c7f41
SM
605 /* The DW_AT_GNU_ranges_base attribute, if present.
606
607 This is only relevant in the context of pre-DWARF 5 split units. In this
608 context, there is a .debug_ranges section in the linked executable,
609 containing all the ranges data for all the compilation units. Each
610 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
611 indicates the base of its contribution to that section. The DW_AT_ranges
612 attributes in the split-unit are of the form DW_FORM_sec_offset and point
613 into the .debug_ranges section of the linked file. However, they are not
614 "true" DW_FORM_sec_offset, because they are relative to the base of their
615 compilation unit's contribution, rather than relative to the beginning of
616 the section. The DW_AT_GNU_ranges_base value must be added to it to make
617 it relative to the beginning of the section.
618
619 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
620 case, so this value can be added without needing to know whether we are in
621 this case or not.
622
623 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
624 skeleton/stub, it must not have the base added, as it already points to the
625 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
626 have a DW_AT_ranges attribute, we can use the
627
628 die->tag != DW_AT_compile_unit
629
630 to determine whether the base should be added or not. */
631 ULONGEST gnu_ranges_base = 0;
632
633 /* The DW_AT_rnglists_base attribute, if present.
634
635 This is used when processing attributes of form DW_FORM_rnglistx in
636 non-split units. Attributes of this form found in a split unit don't
637 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
638 section. */
639 ULONGEST rnglists_base = 0;
2e3cf129 640
41144253 641 /* The DW_AT_loclists_base attribute if present. */
642 ULONGEST loclist_base = 0;
643
c9317f21
TT
644 /* When reading debug info generated by older versions of rustc, we
645 have to rewrite some union types to be struct types with a
646 variant part. This rewriting must be done after the CU is fully
647 read in, because otherwise at the point of rewriting some struct
648 type might not have been fully processed. So, we keep a list of
649 all such types here and process them after expansion. */
650 std::vector<struct type *> rust_unions;
651
18a8505e
AT
652 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
653 files, the value is implicitly zero. For DWARF 5 version DWO files, the
654 value is often implicit and is the size of the header of
655 .debug_str_offsets section (8 or 4, depending on the address size). */
656 gdb::optional<ULONGEST> str_offsets_base;
657
ae038cb0 658 /* Mark used when releasing cached dies. */
9068261f 659 bool mark : 1;
ae038cb0 660
8be455d7
JK
661 /* This CU references .debug_loc. See the symtab->locations_valid field.
662 This test is imperfect as there may exist optimized debug code not using
663 any location list and still facing inlining issues if handled as
664 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 665 bool has_loclist : 1;
ba919b58 666
9068261f 667 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
668 if all the producer_is_* fields are valid. This information is cached
669 because profiling CU expansion showed excessive time spent in
670 producer_is_gxx_lt_4_6. */
9068261f
AB
671 bool checked_producer : 1;
672 bool producer_is_gxx_lt_4_6 : 1;
673 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 674 bool producer_is_icc : 1;
9068261f 675 bool producer_is_icc_lt_14 : 1;
c258c396 676 bool producer_is_codewarrior : 1;
4d4ec4e5 677
9068261f 678 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
679 debugging info for C++ namespaces. GCC 3.3.x did not produce
680 this information, but later versions do. */
681
9068261f 682 bool processing_has_namespace_info : 1;
d590ff25
YQ
683
684 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
685
686 /* If this CU was inherited by another CU (via specification,
687 abstract_origin, etc), this is the ancestor CU. */
688 dwarf2_cu *ancestor;
689
690 /* Get the buildsym_compunit for this CU. */
691 buildsym_compunit *get_builder ()
692 {
693 /* If this CU has a builder associated with it, use that. */
694 if (m_builder != nullptr)
695 return m_builder.get ();
696
697 /* Otherwise, search ancestors for a valid builder. */
698 if (ancestor != nullptr)
699 return ancestor->get_builder ();
700
701 return nullptr;
702 }
e7c27a73
DJ
703};
704
094b34ac
DE
705/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
706 This includes type_unit_group and quick_file_names. */
707
708struct stmt_list_hash
709{
710 /* The DWO unit this table is from or NULL if there is none. */
711 struct dwo_unit *dwo_unit;
712
713 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 714 sect_offset line_sect_off;
094b34ac
DE
715};
716
5989a64e 717/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
718 an object of this type. This contains elements of type unit groups
719 that can be shared across objfiles. The non-shareable parts are in
720 type_unit_group_unshareable. */
f4dc4d17
DE
721
722struct type_unit_group
723{
0186c6a7 724 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
725 To simplify things we create an artificial CU that "includes" all the
726 type units using this stmt_list so that the rest of the code still has
197400e8 727 a "per_cu" handle on the symtab. */
094b34ac
DE
728 struct dwarf2_per_cu_data per_cu;
729
0186c6a7
DE
730 /* The TUs that share this DW_AT_stmt_list entry.
731 This is added to while parsing type units to build partial symtabs,
732 and is deleted afterwards and not used again. */
a8b3b8e9 733 std::vector<signatured_type *> *tus;
f4dc4d17 734
094b34ac
DE
735 /* The data used to construct the hash key. */
736 struct stmt_list_hash hash;
f4dc4d17
DE
737};
738
73869dc2 739/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
740
741struct dwo_sections
742{
743 struct dwarf2_section_info abbrev;
3019eac3
DE
744 struct dwarf2_section_info line;
745 struct dwarf2_section_info loc;
43988095 746 struct dwarf2_section_info loclists;
09262596
DE
747 struct dwarf2_section_info macinfo;
748 struct dwarf2_section_info macro;
d0ce17d8 749 struct dwarf2_section_info rnglists;
3019eac3
DE
750 struct dwarf2_section_info str;
751 struct dwarf2_section_info str_offsets;
80626a55
DE
752 /* In the case of a virtual DWO file, these two are unused. */
753 struct dwarf2_section_info info;
fd5866f6 754 std::vector<dwarf2_section_info> types;
3019eac3
DE
755};
756
c88ee1f0 757/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
758
759struct dwo_unit
760{
761 /* Backlink to the containing struct dwo_file. */
762 struct dwo_file *dwo_file;
763
764 /* The "id" that distinguishes this CU/TU.
765 .debug_info calls this "dwo_id", .debug_types calls this "signature".
766 Since signatures came first, we stick with it for consistency. */
767 ULONGEST signature;
768
769 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 770 struct dwarf2_section_info *section;
3019eac3 771
9c541725
PA
772 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
773 sect_offset sect_off;
3019eac3
DE
774 unsigned int length;
775
776 /* For types, offset in the type's DIE of the type defined by this TU. */
777 cu_offset type_offset_in_tu;
778};
779
73869dc2
DE
780/* include/dwarf2.h defines the DWP section codes.
781 It defines a max value but it doesn't define a min value, which we
782 use for error checking, so provide one. */
783
784enum dwp_v2_section_ids
785{
786 DW_SECT_MIN = 1
787};
788
80626a55 789/* Data for one DWO file.
57d63ce2
DE
790
791 This includes virtual DWO files (a virtual DWO file is a DWO file as it
792 appears in a DWP file). DWP files don't really have DWO files per se -
793 comdat folding of types "loses" the DWO file they came from, and from
794 a high level view DWP files appear to contain a mass of random types.
795 However, to maintain consistency with the non-DWP case we pretend DWP
796 files contain virtual DWO files, and we assign each TU with one virtual
797 DWO file (generally based on the line and abbrev section offsets -
798 a heuristic that seems to work in practice). */
3019eac3
DE
799
800struct dwo_file
801{
51ac9db5
SM
802 dwo_file () = default;
803 DISABLE_COPY_AND_ASSIGN (dwo_file);
804
18a8505e 805 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
806 For virtual DWO files the name is constructed from the section offsets
807 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
808 from related CU+TUs. */
51ac9db5 809 const char *dwo_name = nullptr;
0ac5b59e
DE
810
811 /* The DW_AT_comp_dir attribute. */
51ac9db5 812 const char *comp_dir = nullptr;
3019eac3 813
80626a55
DE
814 /* The bfd, when the file is open. Otherwise this is NULL.
815 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 816 gdb_bfd_ref_ptr dbfd;
3019eac3 817
73869dc2 818 /* The sections that make up this DWO file.
d2854d8d 819 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
73869dc2 820 sections (for lack of a better name). */
51ac9db5 821 struct dwo_sections sections {};
3019eac3 822
33c5cd75
DB
823 /* The CUs in the file.
824 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
825 an extension to handle LLVM's Link Time Optimization output (where
826 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 827 htab_up cus;
3019eac3
DE
828
829 /* Table of TUs in the file.
830 Each element is a struct dwo_unit. */
b0b6a987 831 htab_up tus;
3019eac3
DE
832};
833
80626a55
DE
834/* These sections are what may appear in a DWP file. */
835
836struct dwp_sections
837{
d2854d8d 838 /* These are used by all DWP versions (1, 2 and 5). */
80626a55
DE
839 struct dwarf2_section_info str;
840 struct dwarf2_section_info cu_index;
841 struct dwarf2_section_info tu_index;
73869dc2 842
d2854d8d 843 /* These are only used by DWP version 2 and version 5 files.
73869dc2
DE
844 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
845 sections are referenced by section number, and are not recorded here.
d2854d8d
CT
846 In DWP version 2 or 5 there is at most one copy of all these sections,
847 each section being (effectively) comprised of the concatenation of all of
848 the individual sections that exist in the version 1 format.
73869dc2
DE
849 To keep the code simple we treat each of these concatenated pieces as a
850 section itself (a virtual section?). */
851 struct dwarf2_section_info abbrev;
852 struct dwarf2_section_info info;
853 struct dwarf2_section_info line;
854 struct dwarf2_section_info loc;
d2854d8d 855 struct dwarf2_section_info loclists;
73869dc2
DE
856 struct dwarf2_section_info macinfo;
857 struct dwarf2_section_info macro;
d2854d8d 858 struct dwarf2_section_info rnglists;
73869dc2
DE
859 struct dwarf2_section_info str_offsets;
860 struct dwarf2_section_info types;
80626a55
DE
861};
862
73869dc2
DE
863/* These sections are what may appear in a virtual DWO file in DWP version 1.
864 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 865
73869dc2 866struct virtual_v1_dwo_sections
80626a55
DE
867{
868 struct dwarf2_section_info abbrev;
869 struct dwarf2_section_info line;
870 struct dwarf2_section_info loc;
871 struct dwarf2_section_info macinfo;
872 struct dwarf2_section_info macro;
873 struct dwarf2_section_info str_offsets;
874 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 875 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
876 struct dwarf2_section_info info_or_types;
877};
878
d2854d8d 879/* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
73869dc2
DE
880 In version 2, the sections of the DWO files are concatenated together
881 and stored in one section of that name. Thus each ELF section contains
882 several "virtual" sections. */
883
d2854d8d 884struct virtual_v2_or_v5_dwo_sections
73869dc2
DE
885{
886 bfd_size_type abbrev_offset;
887 bfd_size_type abbrev_size;
888
889 bfd_size_type line_offset;
890 bfd_size_type line_size;
891
892 bfd_size_type loc_offset;
893 bfd_size_type loc_size;
894
d2854d8d
CT
895 bfd_size_type loclists_offset;
896 bfd_size_type loclists_size;
897
73869dc2
DE
898 bfd_size_type macinfo_offset;
899 bfd_size_type macinfo_size;
900
901 bfd_size_type macro_offset;
902 bfd_size_type macro_size;
903
d2854d8d
CT
904 bfd_size_type rnglists_offset;
905 bfd_size_type rnglists_size;
906
73869dc2
DE
907 bfd_size_type str_offsets_offset;
908 bfd_size_type str_offsets_size;
909
910 /* Each DWP hash table entry records one CU or one TU.
911 That is recorded here, and copied to dwo_unit.section. */
912 bfd_size_type info_or_types_offset;
913 bfd_size_type info_or_types_size;
914};
915
80626a55
DE
916/* Contents of DWP hash tables. */
917
918struct dwp_hash_table
919{
73869dc2 920 uint32_t version, nr_columns;
80626a55 921 uint32_t nr_units, nr_slots;
73869dc2
DE
922 const gdb_byte *hash_table, *unit_table;
923 union
924 {
925 struct
926 {
927 const gdb_byte *indices;
928 } v1;
929 struct
930 {
931 /* This is indexed by column number and gives the id of the section
932 in that column. */
933#define MAX_NR_V2_DWO_SECTIONS \
934 (1 /* .debug_info or .debug_types */ \
935 + 1 /* .debug_abbrev */ \
936 + 1 /* .debug_line */ \
937 + 1 /* .debug_loc */ \
938 + 1 /* .debug_str_offsets */ \
939 + 1 /* .debug_macro or .debug_macinfo */)
940 int section_ids[MAX_NR_V2_DWO_SECTIONS];
941 const gdb_byte *offsets;
942 const gdb_byte *sizes;
943 } v2;
d2854d8d
CT
944 struct
945 {
946 /* This is indexed by column number and gives the id of the section
947 in that column. */
948#define MAX_NR_V5_DWO_SECTIONS \
949 (1 /* .debug_info */ \
950 + 1 /* .debug_abbrev */ \
951 + 1 /* .debug_line */ \
952 + 1 /* .debug_loclists */ \
953 + 1 /* .debug_str_offsets */ \
954 + 1 /* .debug_macro */ \
955 + 1 /* .debug_rnglists */)
956 int section_ids[MAX_NR_V5_DWO_SECTIONS];
957 const gdb_byte *offsets;
958 const gdb_byte *sizes;
959 } v5;
73869dc2 960 } section_pool;
80626a55
DE
961};
962
963/* Data for one DWP file. */
964
965struct dwp_file
966{
400174b1
TT
967 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
968 : name (name_),
969 dbfd (std::move (abfd))
970 {
971 }
972
80626a55
DE
973 /* Name of the file. */
974 const char *name;
975
73869dc2 976 /* File format version. */
400174b1 977 int version = 0;
73869dc2 978
93417882 979 /* The bfd. */
400174b1 980 gdb_bfd_ref_ptr dbfd;
80626a55
DE
981
982 /* Section info for this file. */
400174b1 983 struct dwp_sections sections {};
80626a55 984
57d63ce2 985 /* Table of CUs in the file. */
400174b1 986 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
987
988 /* Table of TUs in the file. */
400174b1 989 const struct dwp_hash_table *tus = nullptr;
80626a55 990
19ac8c2e 991 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
992 htab_up loaded_cus;
993 htab_up loaded_tus;
80626a55 994
73869dc2
DE
995 /* Table to map ELF section numbers to their sections.
996 This is only needed for the DWP V1 file format. */
400174b1
TT
997 unsigned int num_sections = 0;
998 asection **elf_sections = nullptr;
80626a55
DE
999};
1000
0963b4bd
MS
1001/* Struct used to pass misc. parameters to read_die_and_children, et
1002 al. which are used for both .debug_info and .debug_types dies.
1003 All parameters here are unchanging for the life of the call. This
dee91e82 1004 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
1005
1006struct die_reader_specs
1007{
a32a8923 1008 /* The bfd of die_section. */
0280fdcc 1009 bfd *abfd;
93311388
DE
1010
1011 /* The CU of the DIE we are parsing. */
1012 struct dwarf2_cu *cu;
1013
80626a55 1014 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
1015 struct dwo_file *dwo_file;
1016
dee91e82 1017 /* The section the die comes from.
3019eac3 1018 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
1019 struct dwarf2_section_info *die_section;
1020
1021 /* die_section->buffer. */
d521ce57 1022 const gdb_byte *buffer;
f664829e
DE
1023
1024 /* The end of the buffer. */
1025 const gdb_byte *buffer_end;
a2ce51a0 1026
685af9cd
TT
1027 /* The abbreviation table to use when reading the DIEs. */
1028 struct abbrev_table *abbrev_table;
93311388
DE
1029};
1030
c0ab21c2
TT
1031/* A subclass of die_reader_specs that holds storage and has complex
1032 constructor and destructor behavior. */
1033
1034class cutu_reader : public die_reader_specs
1035{
1036public:
1037
ab432490
SM
1038 cutu_reader (dwarf2_per_cu_data *this_cu,
1039 dwarf2_per_objfile *per_objfile,
c0ab21c2 1040 struct abbrev_table *abbrev_table,
2e671100 1041 dwarf2_cu *existing_cu,
c0ab21c2
TT
1042 bool skip_partial);
1043
1044 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 1045 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
1046 struct dwarf2_cu *parent_cu = nullptr,
1047 struct dwo_file *dwo_file = nullptr);
1048
c0ab21c2
TT
1049 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1050
1051 const gdb_byte *info_ptr = nullptr;
1052 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
1053 bool dummy_p = false;
1054
6751ebae
TT
1055 /* Release the new CU, putting it on the chain. This cannot be done
1056 for dummy CUs. */
1057 void keep ();
1058
c0ab21c2 1059private:
9e021579
SM
1060 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1061 dwarf2_per_objfile *per_objfile,
2e671100 1062 dwarf2_cu *existing_cu);
c0ab21c2
TT
1063
1064 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
1065 std::unique_ptr<dwarf2_cu> m_new_cu;
1066
1067 /* The ordinary abbreviation table. */
1068 abbrev_table_up m_abbrev_table_holder;
1069
1070 /* The DWO abbreviation table. */
1071 abbrev_table_up m_dwo_abbrev_table;
1072};
dee91e82 1073
c906108c 1074/* When we construct a partial symbol table entry we only
0963b4bd 1075 need this much information. */
6f06d47b 1076struct partial_die_info : public allocate_on_obstack
c906108c 1077 {
7c32eebb 1078 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
6f06d47b
YQ
1079
1080 /* Disable assign but still keep copy ctor, which is needed
1081 load_partial_dies. */
1082 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1083
52356b79
YQ
1084 /* Adjust the partial die before generating a symbol for it. This
1085 function may set the is_external flag or change the DIE's
1086 name. */
1087 void fixup (struct dwarf2_cu *cu);
1088
48fbe735
YQ
1089 /* Read a minimal amount of information into the minimal die
1090 structure. */
1091 const gdb_byte *read (const struct die_reader_specs *reader,
1092 const struct abbrev_info &abbrev,
1093 const gdb_byte *info_ptr);
1094
7d00ffec
TT
1095 /* Compute the name of this partial DIE. This memoizes the
1096 result, so it is safe to call multiple times. */
1097 const char *name (dwarf2_cu *cu);
1098
72bf9492 1099 /* Offset of this DIE. */
6f06d47b 1100 const sect_offset sect_off;
72bf9492
DJ
1101
1102 /* DWARF-2 tag for this DIE. */
6f06d47b 1103 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1104
72bf9492 1105 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1106 const unsigned int has_children : 1;
1107
72bf9492
DJ
1108 unsigned int is_external : 1;
1109 unsigned int is_declaration : 1;
1110 unsigned int has_type : 1;
1111 unsigned int has_specification : 1;
1112 unsigned int has_pc_info : 1;
481860b3 1113 unsigned int may_be_inlined : 1;
72bf9492 1114
0c1b455e
TT
1115 /* This DIE has been marked DW_AT_main_subprogram. */
1116 unsigned int main_subprogram : 1;
1117
72bf9492
DJ
1118 /* Flag set if the SCOPE field of this structure has been
1119 computed. */
1120 unsigned int scope_set : 1;
1121
fa4028e9
JB
1122 /* Flag set if the DIE has a byte_size attribute. */
1123 unsigned int has_byte_size : 1;
1124
ff908ebf
AW
1125 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1126 unsigned int has_const_value : 1;
1127
98bfdba5
PA
1128 /* Flag set if any of the DIE's children are template arguments. */
1129 unsigned int has_template_arguments : 1;
1130
52356b79 1131 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1132 unsigned int fixup_called : 1;
1133
36586728
TT
1134 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1135 unsigned int is_dwz : 1;
1136
1137 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1138 unsigned int spec_is_dwz : 1;
1139
7d00ffec
TT
1140 unsigned int canonical_name : 1;
1141
72bf9492 1142 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1143 sometimes a default name for unnamed DIEs. */
7d00ffec 1144 const char *raw_name = nullptr;
72bf9492 1145
abc72ce4 1146 /* The linkage name, if present. */
6f06d47b 1147 const char *linkage_name = nullptr;
abc72ce4 1148
72bf9492
DJ
1149 /* The scope to prepend to our children. This is generally
1150 allocated on the comp_unit_obstack, so will disappear
1151 when this compilation unit leaves the cache. */
6f06d47b 1152 const char *scope = nullptr;
72bf9492 1153
95554aad
TT
1154 /* Some data associated with the partial DIE. The tag determines
1155 which field is live. */
1156 union
1157 {
1158 /* The location description associated with this DIE, if any. */
1159 struct dwarf_block *locdesc;
1160 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1161 sect_offset sect_off;
6f06d47b 1162 } d {};
72bf9492
DJ
1163
1164 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1165 CORE_ADDR lowpc = 0;
1166 CORE_ADDR highpc = 0;
72bf9492 1167
93311388 1168 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1169 DW_AT_sibling, if any. */
48fbe735
YQ
1170 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1171 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1172 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1173
1174 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1175 DW_AT_specification (or DW_AT_abstract_origin or
1176 DW_AT_extension). */
6f06d47b 1177 sect_offset spec_offset {};
72bf9492
DJ
1178
1179 /* Pointers to this DIE's parent, first child, and next sibling,
1180 if any. */
6f06d47b
YQ
1181 struct partial_die_info *die_parent = nullptr;
1182 struct partial_die_info *die_child = nullptr;
1183 struct partial_die_info *die_sibling = nullptr;
1184
1185 friend struct partial_die_info *
1186 dwarf2_cu::find_partial_die (sect_offset sect_off);
1187
1188 private:
1189 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1190 partial_die_info (sect_offset sect_off)
1191 : partial_die_info (sect_off, DW_TAG_padding, 0)
1192 {
1193 }
1194
1195 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1196 int has_children_)
1197 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1198 {
1199 is_external = 0;
1200 is_declaration = 0;
1201 has_type = 0;
1202 has_specification = 0;
1203 has_pc_info = 0;
1204 may_be_inlined = 0;
1205 main_subprogram = 0;
1206 scope_set = 0;
1207 has_byte_size = 0;
1208 has_const_value = 0;
1209 has_template_arguments = 0;
1210 fixup_called = 0;
1211 is_dwz = 0;
1212 spec_is_dwz = 0;
7d00ffec 1213 canonical_name = 0;
6f06d47b 1214 }
c906108c
SS
1215 };
1216
c906108c
SS
1217/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1218 but this would require a corresponding change in unpack_field_as_long
1219 and friends. */
1220static int bits_per_byte = 8;
1221
9c6a1327
TT
1222struct variant_part_builder;
1223
1224/* When reading a variant, we track a bit more information about the
1225 field, and store it in an object of this type. */
2ddeaf8a
TT
1226
1227struct variant_field
1228{
9c6a1327
TT
1229 int first_field = -1;
1230 int last_field = -1;
1231
1232 /* A variant can contain other variant parts. */
1233 std::vector<variant_part_builder> variant_parts;
1234
2ddeaf8a
TT
1235 /* If we see a DW_TAG_variant, then this will be set if this is the
1236 default branch. */
9c6a1327
TT
1237 bool default_branch = false;
1238 /* If we see a DW_AT_discr_value, then this will be the discriminant
1239 value. */
1240 ULONGEST discriminant_value = 0;
1241 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1242 data. */
1243 struct dwarf_block *discr_list_data = nullptr;
1244};
1245
1246/* This represents a DW_TAG_variant_part. */
1247
1248struct variant_part_builder
1249{
1250 /* The offset of the discriminant field. */
1251 sect_offset discriminant_offset {};
1252
1253 /* Variants that are direct children of this variant part. */
1254 std::vector<variant_field> variants;
1255
1256 /* True if we're currently reading a variant. */
1257 bool processing_variant = false;
2ddeaf8a
TT
1258};
1259
52059ffd
TT
1260struct nextfield
1261{
be2daae6
TT
1262 int accessibility = 0;
1263 int virtuality = 0;
9c6a1327
TT
1264 /* Variant parts need to find the discriminant, which is a DIE
1265 reference. We track the section offset of each field to make
1266 this link. */
1267 sect_offset offset;
be2daae6 1268 struct field field {};
52059ffd
TT
1269};
1270
1271struct fnfieldlist
1272{
be2daae6
TT
1273 const char *name = nullptr;
1274 std::vector<struct fn_field> fnfields;
52059ffd
TT
1275};
1276
c906108c
SS
1277/* The routines that read and process dies for a C struct or C++ class
1278 pass lists of data member fields and lists of member function fields
1279 in an instance of a field_info structure, as defined below. */
1280struct field_info
2de01bdb
SM
1281{
1282 /* List of data member and baseclasses fields. */
1283 std::vector<struct nextfield> fields;
1284 std::vector<struct nextfield> baseclasses;
1285
1286 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1287 bool non_public_fields = false;
2de01bdb
SM
1288
1289 /* Member function fieldlist array, contains name of possibly overloaded
1290 member function, number of overloaded member functions and a pointer
1291 to the head of the member function field chain. */
1292 std::vector<struct fnfieldlist> fnfieldlists;
1293
1294 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1295 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1296 std::vector<struct decl_field> typedef_field_list;
1297
1298 /* Nested types defined by this class and the number of elements in this
1299 list. */
1300 std::vector<struct decl_field> nested_types_list;
1301
1302 /* If non-null, this is the variant part we are currently
1303 reading. */
1304 variant_part_builder *current_variant_part = nullptr;
1305 /* This holds all the top-level variant parts attached to the type
1306 we're reading. */
1307 std::vector<variant_part_builder> variant_parts;
1308
1309 /* Return the total number of fields (including baseclasses). */
1310 int nfields () const
c5aa993b 1311 {
2de01bdb
SM
1312 return fields.size () + baseclasses.size ();
1313 }
1314};
c906108c 1315
ae038cb0
DJ
1316/* Loaded secondary compilation units are kept in memory until they
1317 have not been referenced for the processing of this many
1318 compilation units. Set this to zero to disable caching. Cache
1319 sizes of up to at least twenty will improve startup time for
1320 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1321static int dwarf_max_cache_age = 5;
920d2a44 1322static void
b4f54984
DE
1323show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1324 struct cmd_list_element *c, const char *value)
920d2a44 1325{
3e43a32a 1326 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1327 "DWARF compilation units is %s.\n"),
920d2a44
AC
1328 value);
1329}
4390d890 1330\f
c906108c
SS
1331/* local function prototypes */
1332
918dd910
JK
1333static void dwarf2_find_base_address (struct die_info *die,
1334 struct dwarf2_cu *cu);
1335
891813be 1336static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1337 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1338 const char *name);
0018ea6f 1339
f1902523
JK
1340static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1341 const gdb_byte *info_ptr,
3e225074 1342 struct die_info *type_unit_die);
f1902523 1343
976ca316 1344static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1345
72bf9492
DJ
1346static void scan_partial_symbols (struct partial_die_info *,
1347 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1348 int, struct dwarf2_cu *);
c906108c 1349
72bf9492
DJ
1350static void add_partial_symbol (struct partial_die_info *,
1351 struct dwarf2_cu *);
63d06c5c 1352
72bf9492
DJ
1353static void add_partial_namespace (struct partial_die_info *pdi,
1354 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1355 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1356
5d7cb8df 1357static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1358 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1359 struct dwarf2_cu *cu);
1360
72bf9492
DJ
1361static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1362 struct dwarf2_cu *cu);
91c24f0a 1363
bc30ff58
JB
1364static void add_partial_subprogram (struct partial_die_info *pdi,
1365 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1366 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1367
d521ce57 1368static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1369
dee91e82 1370static struct partial_die_info *load_partial_dies
d521ce57 1371 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1372
fb816e8b
TV
1373/* A pair of partial_die_info and compilation unit. */
1374struct cu_partial_die_info
1375{
1376 /* The compilation unit of the partial_die_info. */
1377 struct dwarf2_cu *cu;
1378 /* A partial_die_info. */
1379 struct partial_die_info *pdi;
122cf0f2
AB
1380
1381 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1382 : cu (cu),
1383 pdi (pdi)
405feb71 1384 { /* Nothing. */ }
122cf0f2
AB
1385
1386private:
1387 cu_partial_die_info () = delete;
fb816e8b
TV
1388};
1389
122cf0f2
AB
1390static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1391 struct dwarf2_cu *);
72bf9492 1392
d521ce57 1393static const gdb_byte *read_attribute (const struct die_reader_specs *,
4444f407
TT
1394 struct attribute *,
1395 const struct attr_abbrev *,
7a5f294d 1396 const gdb_byte *);
18a8505e
AT
1397
1398static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1399 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1400
1401static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1402
976ca316
SM
1403static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1404 dwarf2_section_info *, sect_offset);
f4dc4d17 1405
ed2dc618 1406static const char *read_indirect_string
976ca316 1407 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1408 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1409
ed2dc618 1410static const char *read_indirect_string_at_offset
976ca316 1411 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1412
d521ce57
TT
1413static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1414 const gdb_byte *,
3019eac3
DE
1415 unsigned int *);
1416
18a8505e
AT
1417static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1418 ULONGEST str_index);
1419
1420static const char *read_stub_str_index (struct dwarf2_cu *cu,
1421 ULONGEST str_index);
3019eac3 1422
e142c38c 1423static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1424
e142c38c
DJ
1425static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1426 struct dwarf2_cu *);
c906108c 1427
7d45c7c3 1428static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
dda83cd7 1429 struct dwarf2_cu *cu);
7d45c7c3 1430
a084a2a6
AT
1431static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1432
05cf31d1 1433static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
dda83cd7 1434 struct dwarf2_cu *cu);
05cf31d1 1435
e142c38c 1436static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1437
e142c38c 1438static struct die_info *die_specification (struct die_info *die,
f2f0e013 1439 struct dwarf2_cu **);
63d06c5c 1440
9c541725 1441static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1442 struct dwarf2_cu *cu);
debd256d 1443
f3f5162e 1444static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1445 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1446 CORE_ADDR, int decode_mapping);
c906108c 1447
804d2729
TT
1448static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1449 const char *);
c906108c 1450
a14ed312 1451static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1452 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1453
ff39bb5e 1454static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1455 struct dwarf2_cu *);
c906108c 1456
ff39bb5e 1457static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1458 struct type *type,
1459 const char *name,
1460 struct obstack *obstack,
12df843f 1461 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1462 const gdb_byte **bytes,
98bfdba5 1463 struct dwarf2_locexpr_baton **baton);
2df3850c 1464
57567375
TT
1465static struct type *read_subrange_index_type (struct die_info *die,
1466 struct dwarf2_cu *cu);
1467
e7c27a73 1468static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1469
b4ba55a1
JB
1470static int need_gnat_info (struct dwarf2_cu *);
1471
3e43a32a
MS
1472static struct type *die_descriptive_type (struct die_info *,
1473 struct dwarf2_cu *);
b4ba55a1
JB
1474
1475static void set_descriptive_type (struct type *, struct die_info *,
1476 struct dwarf2_cu *);
1477
e7c27a73
DJ
1478static struct type *die_containing_type (struct die_info *,
1479 struct dwarf2_cu *);
c906108c 1480
ff39bb5e 1481static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1482 struct dwarf2_cu *);
c906108c 1483
f792889a 1484static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1485
673bfd45
DE
1486static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1487
0d5cff50 1488static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1489
6e70227d 1490static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1491 const char *suffix, int physname,
1492 struct dwarf2_cu *cu);
63d06c5c 1493
e7c27a73 1494static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1495
348e048f
DE
1496static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1497
e7c27a73 1498static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1499
e7c27a73 1500static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1501
96408a79
SA
1502static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1503
71a3c369
TT
1504static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1505
41144253 1506/* Return the .debug_loclists section to use for cu. */
1507static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1508
d0ce17d8
CT
1509/* Return the .debug_rnglists section to use for cu. */
1510static struct dwarf2_section_info *cu_debug_rnglists_section
1511 (struct dwarf2_cu *cu, dwarf_tag tag);
1512
3a2b436a 1513/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1514 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1515enum pc_bounds_kind
1516{
e385593e 1517 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1518 PC_BOUNDS_NOT_PRESENT,
1519
e385593e
JK
1520 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1521 were present but they do not form a valid range of PC addresses. */
1522 PC_BOUNDS_INVALID,
1523
3a2b436a
JK
1524 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1525 PC_BOUNDS_RANGES,
1526
1527 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1528 PC_BOUNDS_HIGH_LOW,
1529};
1530
1531static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1532 CORE_ADDR *, CORE_ADDR *,
1533 struct dwarf2_cu *,
891813be 1534 dwarf2_psymtab *);
c906108c 1535
fae299cd
DC
1536static void get_scope_pc_bounds (struct die_info *,
1537 CORE_ADDR *, CORE_ADDR *,
1538 struct dwarf2_cu *);
1539
801e3a5b 1540static void dwarf2_record_block_ranges (struct die_info *, struct block *,
dda83cd7 1541 CORE_ADDR, struct dwarf2_cu *);
801e3a5b 1542
a14ed312 1543static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1544 struct dwarf2_cu *);
c906108c 1545
a14ed312 1546static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1547 struct type *, struct dwarf2_cu *);
c906108c 1548
a14ed312 1549static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1550 struct die_info *, struct type *,
e7c27a73 1551 struct dwarf2_cu *);
c906108c 1552
a14ed312 1553static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1554 struct type *,
1555 struct dwarf2_cu *);
c906108c 1556
134d01f1 1557static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1558
e7c27a73 1559static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1560
e7c27a73 1561static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1562
5d7cb8df
JK
1563static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1564
804d2729 1565static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1566
27aa8d6a
SW
1567static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1568
74921315
KS
1569static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1570
f55ee35c
JK
1571static struct type *read_module_type (struct die_info *die,
1572 struct dwarf2_cu *cu);
1573
38d518c9 1574static const char *namespace_name (struct die_info *die,
e142c38c 1575 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1576
134d01f1 1577static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1578
7d79de9a
TT
1579static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1580 bool * = nullptr);
c906108c 1581
6e70227d 1582static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1583 struct dwarf2_cu *);
1584
bf6af496 1585static struct die_info *read_die_and_siblings_1
d521ce57 1586 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1587 struct die_info *);
639d11d3 1588
dee91e82 1589static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1590 const gdb_byte *info_ptr,
1591 const gdb_byte **new_info_ptr,
639d11d3
DC
1592 struct die_info *parent);
1593
d521ce57
TT
1594static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1595 struct die_info **, const gdb_byte *,
3e225074 1596 int);
3019eac3 1597
d521ce57 1598static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1599 struct die_info **, const gdb_byte *);
93311388 1600
e7c27a73 1601static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1602
15d034d0 1603static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1604 struct objfile *);
71c25dea 1605
15d034d0 1606static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1607
15d034d0 1608static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1609 struct die_info *die,
1610 struct dwarf2_cu *cu);
1611
ca69b9e6
DE
1612static const char *dwarf2_physname (const char *name, struct die_info *die,
1613 struct dwarf2_cu *cu);
1614
e142c38c 1615static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1616 struct dwarf2_cu **);
9219021c 1617
d97bc12b
DE
1618static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1619
1620static void dump_die_for_error (struct die_info *);
1621
1622static void dump_die_1 (struct ui_file *, int level, int max_level,
1623 struct die_info *);
c906108c 1624
d97bc12b 1625/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1626
51545339 1627static void store_in_ref_table (struct die_info *,
10b3939b 1628 struct dwarf2_cu *);
c906108c 1629
348e048f 1630static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1631 const struct attribute *,
348e048f
DE
1632 struct dwarf2_cu **);
1633
10b3939b 1634static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1635 const struct attribute *,
f2f0e013 1636 struct dwarf2_cu **);
c906108c 1637
348e048f 1638static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1639 const struct attribute *,
348e048f
DE
1640 struct dwarf2_cu **);
1641
ac9ec31b
DE
1642static struct type *get_signatured_type (struct die_info *, ULONGEST,
1643 struct dwarf2_cu *);
1644
1645static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1646 const struct attribute *,
ac9ec31b
DE
1647 struct dwarf2_cu *);
1648
ab432490
SM
1649static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1650 dwarf2_per_objfile *per_objfile);
348e048f 1651
ab432490
SM
1652static void read_signatured_type (signatured_type *sig_type,
1653 dwarf2_per_objfile *per_objfile);
348e048f 1654
63e43d3a
PMR
1655static int attr_to_dynamic_prop (const struct attribute *attr,
1656 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1657 struct dynamic_prop *prop, struct type *type);
63e43d3a 1658
c906108c
SS
1659/* memory allocation interface */
1660
7b5a2f43 1661static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1662
b60c80d6 1663static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1664
43f3e411 1665static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1666
8cf6f0b1
TT
1667static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1668 struct dwarf2_loclist_baton *baton,
ff39bb5e 1669 const struct attribute *attr);
8cf6f0b1 1670
ff39bb5e 1671static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1672 struct symbol *sym,
f1e6e072
TT
1673 struct dwarf2_cu *cu,
1674 int is_block);
4c2df51b 1675
d521ce57
TT
1676static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1677 const gdb_byte *info_ptr,
7c32eebb 1678 const struct abbrev_info *abbrev);
4bb7a0a7 1679
72bf9492
DJ
1680static hashval_t partial_die_hash (const void *item);
1681
1682static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1683
ae038cb0 1684static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1685 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1686 dwarf2_per_objfile *per_objfile);
ae038cb0 1687
9816fde3 1688static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1689 struct die_info *comp_unit_die,
1690 enum language pretend_language);
93311388 1691
f792889a 1692static struct type *set_die_type (struct die_info *, struct type *,
57567375 1693 struct dwarf2_cu *, bool = false);
1c379e20 1694
976ca316 1695static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1696
976ca316 1697static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1fd400ff 1698
ab432490
SM
1699static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1700 dwarf2_per_objfile *per_objfile,
4a636814 1701 dwarf2_cu *existing_cu,
ab432490
SM
1702 bool skip_partial,
1703 enum language pretend_language);
10b3939b 1704
8fc0b21d 1705static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1706 enum language pretend_language);
10b3939b 1707
8fc0b21d 1708static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1709 enum language pretend_language);
f4dc4d17 1710
10b3939b
DJ
1711static void dwarf2_add_dependence (struct dwarf2_cu *,
1712 struct dwarf2_per_cu_data *);
1713
ae038cb0
DJ
1714static void dwarf2_mark (struct dwarf2_cu *);
1715
b64f50a1 1716static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1717 dwarf2_per_cu_data *per_cu,
1718 dwarf2_per_objfile *per_objfile);
673bfd45 1719
f792889a 1720static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1721
120ce1b5
SM
1722static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1723 dwarf2_per_objfile *per_objfile,
95554aad
TT
1724 enum language pretend_language);
1725
976ca316 1726static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1727
b303c6f6
AB
1728/* Class, the destructor of which frees all allocated queue entries. This
1729 will only have work to do if an error was thrown while processing the
1730 dwarf. If no error was thrown then the queue entries should have all
1731 been processed, and freed, as we went along. */
1732
1733class dwarf2_queue_guard
1734{
1735public:
39856def
TT
1736 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1737 : m_per_objfile (per_objfile)
1738 {
08ac5771
SM
1739 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1740
1741 m_per_objfile->per_bfd->queue.emplace ();
39856def 1742 }
b303c6f6
AB
1743
1744 /* Free any entries remaining on the queue. There should only be
1745 entries left if we hit an error while processing the dwarf. */
1746 ~dwarf2_queue_guard ()
1747 {
08ac5771
SM
1748 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1749
1750 m_per_objfile->per_bfd->queue.reset ();
39856def 1751 }
b303c6f6 1752
39856def 1753 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1754
39856def
TT
1755private:
1756 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1757};
1758
39856def
TT
1759dwarf2_queue_item::~dwarf2_queue_item ()
1760{
1761 /* Anything still marked queued is likely to be in an
1762 inconsistent state, so discard it. */
1763 if (per_cu->queued)
1764 {
7188ed02 1765 per_objfile->remove_cu (per_cu);
39856def
TT
1766 per_cu->queued = 0;
1767 }
1768}
1769
d721ba37
PA
1770/* The return type of find_file_and_directory. Note, the enclosed
1771 string pointers are only valid while this object is valid. */
1772
1773struct file_and_directory
1774{
1775 /* The filename. This is never NULL. */
1776 const char *name;
1777
1778 /* The compilation directory. NULL if not known. If we needed to
1779 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1780 points directly to the DW_AT_comp_dir string attribute owned by
1781 the obstack that owns the DIE. */
1782 const char *comp_dir;
1783
1784 /* If we needed to build a new string for comp_dir, this is what
1785 owns the storage. */
1786 std::string comp_dir_storage;
1787};
1788
1789static file_and_directory find_file_and_directory (struct die_info *die,
1790 struct dwarf2_cu *cu);
9291a0cd 1791
298e9637 1792static htab_up allocate_signatured_type_table ();
1fd400ff 1793
298e9637 1794static htab_up allocate_dwo_unit_table ();
3019eac3 1795
57d63ce2 1796static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1797 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1798 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1799
976ca316 1800static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1801
3019eac3 1802static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1803 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1804 ULONGEST signature);
3019eac3
DE
1805
1806static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1807 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1808
1b555f17 1809static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1810
263db9a1
TT
1811/* A unique pointer to a dwo_file. */
1812
51ac9db5 1813typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1814
976ca316 1815static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1816
1b80a9fa 1817static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1818
1819static void free_line_header_voidp (void *arg);
4390d890
DE
1820\f
1821/* Various complaints about symbol reading that don't abort the process. */
1822
4390d890
DE
1823static void
1824dwarf2_debug_line_missing_file_complaint (void)
1825{
b98664d3 1826 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1827}
1828
1829static void
1830dwarf2_debug_line_missing_end_sequence_complaint (void)
1831{
b98664d3 1832 complaint (_(".debug_line section has line "
4390d890
DE
1833 "program sequence without an end"));
1834}
1835
1836static void
1837dwarf2_complex_location_expr_complaint (void)
1838{
b98664d3 1839 complaint (_("location expression too complex"));
4390d890
DE
1840}
1841
1842static void
1843dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1844 int arg3)
1845{
b98664d3 1846 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1847 arg1, arg2, arg3);
1848}
1849
4390d890
DE
1850static void
1851dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1852{
b98664d3 1853 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1854 arg1, arg2);
1855}
527f3840
JK
1856
1857/* Hash function for line_header_hash. */
1858
1859static hashval_t
1860line_header_hash (const struct line_header *ofs)
1861{
9c541725 1862 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1863}
1864
1865/* Hash function for htab_create_alloc_ex for line_header_hash. */
1866
1867static hashval_t
1868line_header_hash_voidp (const void *item)
1869{
9a3c8263 1870 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1871
1872 return line_header_hash (ofs);
1873}
1874
1875/* Equality function for line_header_hash. */
1876
1877static int
1878line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1879{
9a3c8263
SM
1880 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1881 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1882
9c541725 1883 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1884 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1885}
1886
4390d890 1887\f
9291a0cd 1888
330cdd98
PA
1889/* See declaration. */
1890
5989a64e
SM
1891dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1892 bool can_copy_)
c3699833
SM
1893 : obfd (obfd),
1894 can_copy (can_copy_)
330cdd98
PA
1895{
1896 if (names == NULL)
1897 names = &dwarf2_elf_names;
1898
330cdd98
PA
1899 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1900 locate_sections (obfd, sec, *names);
1901}
1902
5989a64e 1903dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1904{
b76e467d 1905 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1906 per_cu->imported_symtabs_free ();
fc8e7e75 1907
b2bdb8cf 1908 for (signatured_type *sig_type : all_type_units)
ae640021 1909 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1910
5989a64e 1911 /* Everything else should be on this->obstack. */
330cdd98
PA
1912}
1913
7188ed02 1914/* See read.h. */
330cdd98
PA
1915
1916void
7188ed02 1917dwarf2_per_objfile::remove_all_cus ()
330cdd98 1918{
08ac5771
SM
1919 gdb_assert (!this->per_bfd->queue.has_value ());
1920
7188ed02
SM
1921 for (auto pair : m_dwarf2_cus)
1922 delete pair.second;
330cdd98 1923
7188ed02 1924 m_dwarf2_cus.clear ();
330cdd98
PA
1925}
1926
11ed8cad
TT
1927/* A helper class that calls free_cached_comp_units on
1928 destruction. */
1929
1930class free_cached_comp_units
1931{
1932public:
1933
1934 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1935 : m_per_objfile (per_objfile)
1936 {
1937 }
1938
1939 ~free_cached_comp_units ()
1940 {
7188ed02 1941 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1942 }
1943
1944 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1945
1946private:
1947
1948 dwarf2_per_objfile *m_per_objfile;
1949};
1950
af758d11
SM
1951/* See read.h. */
1952
1953bool
1954dwarf2_per_objfile::symtab_set_p (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] != nullptr;
1959}
1960
1961/* See read.h. */
1962
1963compunit_symtab *
1964dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1965{
1966 gdb_assert (per_cu->index < this->m_symtabs.size ());
1967
1968 return this->m_symtabs[per_cu->index];
1969}
1970
1971/* See read.h. */
1972
1973void
1974dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1975 compunit_symtab *symtab)
1976{
1977 gdb_assert (per_cu->index < this->m_symtabs.size ());
1978 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1979
1980 this->m_symtabs[per_cu->index] = symtab;
1981}
1982
c906108c 1983/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1984 information and return true if we have enough to do something.
1985 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1986 ELF names are used. CAN_COPY is true for formats where symbol
1987 interposition is possible and so symbol values must follow copy
1988 relocation rules. */
c906108c
SS
1989
1990int
251d32d9 1991dwarf2_has_info (struct objfile *objfile,
dda83cd7 1992 const struct dwarf2_debug_sections *names,
4b610737 1993 bool can_copy)
c906108c 1994{
97cbe998
SDJ
1995 if (objfile->flags & OBJF_READNEVER)
1996 return 0;
1997
976ca316 1998 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1999
976ca316 2000 if (per_objfile == NULL)
5989a64e 2001 {
17ee85fc
TT
2002 dwarf2_per_bfd *per_bfd;
2003
8a91fbdf
SM
2004 /* We can share a "dwarf2_per_bfd" with other objfiles if the
2005 BFD doesn't require relocations.
2006
2007 We don't share with objfiles for which -readnow was requested,
2008 because it would complicate things when loading the same BFD with
2009 -readnow and then without -readnow. */
2010 if (!gdb_bfd_requires_relocations (objfile->obfd)
2011 && (objfile->flags & OBJF_READNOW) == 0)
17ee85fc
TT
2012 {
2013 /* See if one has been created for this BFD yet. */
2014 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
2015
2016 if (per_bfd == nullptr)
2017 {
2018 /* No, create it now. */
2019 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
2020 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
2021 }
2022 }
2023 else
2024 {
2025 /* No sharing possible, create one specifically for this objfile. */
2026 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
2027 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
2028 }
5989a64e 2029
976ca316 2030 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 2031 }
5bfd760d 2032
976ca316
SM
2033 return (!per_objfile->per_bfd->info.is_virtual
2034 && per_objfile->per_bfd->info.s.section != NULL
2035 && !per_objfile->per_bfd->abbrev.is_virtual
2036 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
2037}
2038
330cdd98 2039/* See declaration. */
c906108c 2040
330cdd98 2041void
5989a64e
SM
2042dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
2043 const dwarf2_debug_sections &names)
c906108c 2044{
fd361982 2045 flagword aflag = bfd_section_flags (sectp);
251d32d9 2046
dc7650b8
JK
2047 if ((aflag & SEC_HAS_CONTENTS) == 0)
2048 {
2049 }
950b7495
KS
2050 else if (elf_section_data (sectp)->this_hdr.sh_size
2051 > bfd_get_file_size (abfd))
2052 {
2053 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2054 warning (_("Discarding section %s which has a section size (%s"
2055 ") larger than the file size [in module %s]"),
2056 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2057 bfd_get_filename (abfd));
2058 }
fbedd546 2059 else if (names.info.matches (sectp->name))
c906108c 2060 {
330cdd98 2061 this->info.s.section = sectp;
fd361982 2062 this->info.size = bfd_section_size (sectp);
c906108c 2063 }
fbedd546 2064 else if (names.abbrev.matches (sectp->name))
c906108c 2065 {
330cdd98 2066 this->abbrev.s.section = sectp;
fd361982 2067 this->abbrev.size = bfd_section_size (sectp);
c906108c 2068 }
fbedd546 2069 else if (names.line.matches (sectp->name))
c906108c 2070 {
330cdd98 2071 this->line.s.section = sectp;
fd361982 2072 this->line.size = bfd_section_size (sectp);
c906108c 2073 }
fbedd546 2074 else if (names.loc.matches (sectp->name))
c906108c 2075 {
330cdd98 2076 this->loc.s.section = sectp;
fd361982 2077 this->loc.size = bfd_section_size (sectp);
c906108c 2078 }
fbedd546 2079 else if (names.loclists.matches (sectp->name))
43988095 2080 {
330cdd98 2081 this->loclists.s.section = sectp;
fd361982 2082 this->loclists.size = bfd_section_size (sectp);
43988095 2083 }
fbedd546 2084 else if (names.macinfo.matches (sectp->name))
c906108c 2085 {
330cdd98 2086 this->macinfo.s.section = sectp;
fd361982 2087 this->macinfo.size = bfd_section_size (sectp);
c906108c 2088 }
fbedd546 2089 else if (names.macro.matches (sectp->name))
cf2c3c16 2090 {
330cdd98 2091 this->macro.s.section = sectp;
fd361982 2092 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2093 }
fbedd546 2094 else if (names.str.matches (sectp->name))
c906108c 2095 {
330cdd98 2096 this->str.s.section = sectp;
fd361982 2097 this->str.size = bfd_section_size (sectp);
c906108c 2098 }
fbedd546 2099 else if (names.str_offsets.matches (sectp->name))
18a8505e
AT
2100 {
2101 this->str_offsets.s.section = sectp;
2102 this->str_offsets.size = bfd_section_size (sectp);
2103 }
fbedd546 2104 else if (names.line_str.matches (sectp->name))
43988095 2105 {
330cdd98 2106 this->line_str.s.section = sectp;
fd361982 2107 this->line_str.size = bfd_section_size (sectp);
43988095 2108 }
fbedd546 2109 else if (names.addr.matches (sectp->name))
3019eac3 2110 {
330cdd98 2111 this->addr.s.section = sectp;
fd361982 2112 this->addr.size = bfd_section_size (sectp);
3019eac3 2113 }
fbedd546 2114 else if (names.frame.matches (sectp->name))
b6af0555 2115 {
330cdd98 2116 this->frame.s.section = sectp;
fd361982 2117 this->frame.size = bfd_section_size (sectp);
b6af0555 2118 }
fbedd546 2119 else if (names.eh_frame.matches (sectp->name))
b6af0555 2120 {
330cdd98 2121 this->eh_frame.s.section = sectp;
fd361982 2122 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2123 }
fbedd546 2124 else if (names.ranges.matches (sectp->name))
af34e669 2125 {
330cdd98 2126 this->ranges.s.section = sectp;
fd361982 2127 this->ranges.size = bfd_section_size (sectp);
af34e669 2128 }
fbedd546 2129 else if (names.rnglists.matches (sectp->name))
43988095 2130 {
330cdd98 2131 this->rnglists.s.section = sectp;
fd361982 2132 this->rnglists.size = bfd_section_size (sectp);
43988095 2133 }
fbedd546 2134 else if (names.types.matches (sectp->name))
348e048f 2135 {
8b70b953
TT
2136 struct dwarf2_section_info type_section;
2137
2138 memset (&type_section, 0, sizeof (type_section));
049412e3 2139 type_section.s.section = sectp;
fd361982 2140 type_section.size = bfd_section_size (sectp);
8b70b953 2141
fd5866f6 2142 this->types.push_back (type_section);
348e048f 2143 }
fbedd546 2144 else if (names.gdb_index.matches (sectp->name))
9291a0cd 2145 {
330cdd98 2146 this->gdb_index.s.section = sectp;
fd361982 2147 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2148 }
fbedd546 2149 else if (names.debug_names.matches (sectp->name))
927aa2e7
JK
2150 {
2151 this->debug_names.s.section = sectp;
fd361982 2152 this->debug_names.size = bfd_section_size (sectp);
927aa2e7 2153 }
fbedd546 2154 else if (names.debug_aranges.matches (sectp->name))
927aa2e7
JK
2155 {
2156 this->debug_aranges.s.section = sectp;
fd361982 2157 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2158 }
dce234bc 2159
fd361982
AM
2160 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2161 && bfd_section_vma (sectp) == 0)
330cdd98 2162 this->has_section_at_zero = true;
c906108c
SS
2163}
2164
dce234bc 2165/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2166 SECTION_NAME. */
af34e669 2167
dce234bc 2168void
3017a003 2169dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
2170 enum dwarf2_section_enum sect,
2171 asection **sectp, const gdb_byte **bufp,
2172 bfd_size_type *sizep)
dce234bc 2173{
976ca316 2174 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2175 struct dwarf2_section_info *info;
a3b2a86b
TT
2176
2177 /* We may see an objfile without any DWARF, in which case we just
2178 return nothing. */
976ca316 2179 if (per_objfile == NULL)
a3b2a86b
TT
2180 {
2181 *sectp = NULL;
2182 *bufp = NULL;
2183 *sizep = 0;
2184 return;
2185 }
3017a003
TG
2186 switch (sect)
2187 {
2188 case DWARF2_DEBUG_FRAME:
976ca316 2189 info = &per_objfile->per_bfd->frame;
3017a003
TG
2190 break;
2191 case DWARF2_EH_FRAME:
976ca316 2192 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2193 break;
2194 default:
2195 gdb_assert_not_reached ("unexpected section");
2196 }
dce234bc 2197
96b79293 2198 info->read (objfile);
dce234bc 2199
96b79293 2200 *sectp = info->get_bfd_section ();
dce234bc
PP
2201 *bufp = info->buffer;
2202 *sizep = info->size;
2203}
2204
9291a0cd 2205\f
39298a5d 2206/* DWARF quick_symbol_functions support. */
7b9f3c50
DE
2207
2208/* TUs can share .debug_line entries, and there can be a lot more TUs than
2209 unique line tables, so we maintain a separate table of all .debug_line
2210 derived entries to support the sharing.
2211 All the quick functions need is the list of file names. We discard the
2212 line_header when we're done and don't need to record it here. */
2213struct quick_file_names
2214{
094b34ac
DE
2215 /* The data used to construct the hash key. */
2216 struct stmt_list_hash hash;
7b9f3c50
DE
2217
2218 /* The number of entries in file_names, real_names. */
2219 unsigned int num_file_names;
2220
2221 /* The file names from the line table, after being run through
2222 file_full_name. */
2223 const char **file_names;
2224
2225 /* The file names from the line table after being run through
2226 gdb_realpath. These are computed lazily. */
2227 const char **real_names;
2228};
2229
2230/* When using the index (and thus not using psymtabs), each CU has an
2231 object of this type. This is used to hold information needed by
2232 the various "quick" methods. */
2233struct dwarf2_per_cu_quick_data
2234{
2235 /* The file table. This can be NULL if there was no file table
2236 or it's currently not read in.
5989a64e 2237 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2238 struct quick_file_names *file_names;
2239
7b9f3c50
DE
2240 /* A temporary mark bit used when iterating over all CUs in
2241 expand_symtabs_matching. */
2242 unsigned int mark : 1;
2243
2244 /* True if we've tried to read the file table and found there isn't one.
2245 There will be no point in trying to read it again next time. */
2246 unsigned int no_file_data : 1;
2247};
2248
edc02ceb
TT
2249/* A subclass of psymbol_functions that arranges to read the DWARF
2250 partial symbols when needed. */
2251struct lazy_dwarf_reader : public psymbol_functions
2252{
2253 using psymbol_functions::psymbol_functions;
2254
2255 bool can_lazily_read_symbols () override
2256 {
2257 return true;
2258 }
2259
2260 void read_partial_symbols (struct objfile *objfile) override
2261 {
2262 if (dwarf2_has_info (objfile, nullptr))
2263 dwarf2_build_psymtabs (objfile, this);
2264 }
2265};
2266
2267static quick_symbol_functions_up
2268make_lazy_dwarf_reader ()
2269{
2270 return quick_symbol_functions_up (new lazy_dwarf_reader);
2271}
2272
39298a5d
TT
2273struct dwarf2_base_index_functions : public quick_symbol_functions
2274{
2275 bool has_symbols (struct objfile *objfile) override;
2276
2277 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2278
2279 void forget_cached_source_info (struct objfile *objfile) override;
2280
39298a5d
TT
2281 enum language lookup_global_symbol_language (struct objfile *objfile,
2282 const char *name,
2283 domain_enum domain,
2284 bool *symbol_found_p) override
2285 {
2286 *symbol_found_p = false;
2287 return language_unknown;
2288 }
2289
4829711b 2290 void print_stats (struct objfile *objfile, bool print_bcache) override;
39298a5d
TT
2291
2292 void expand_all_symtabs (struct objfile *objfile) override;
2293
39298a5d
TT
2294 struct compunit_symtab *find_pc_sect_compunit_symtab
2295 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2296 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2297
2298 struct compunit_symtab *find_compunit_symtab_by_address
2299 (struct objfile *objfile, CORE_ADDR address) override
2300 {
2301 return nullptr;
2302 }
2303
2304 void map_symbol_filenames (struct objfile *objfile,
f4655dee
TT
2305 gdb::function_view<symbol_filename_ftype> fun,
2306 bool need_fullname) override;
39298a5d
TT
2307};
2308
2309struct dwarf2_gdb_index : public dwarf2_base_index_functions
2310{
39298a5d
TT
2311 void dump (struct objfile *objfile) override;
2312
0b7b2c2a 2313 void expand_matching_symbols
39298a5d
TT
2314 (struct objfile *,
2315 const lookup_name_info &lookup_name,
2316 domain_enum domain,
2317 int global,
39298a5d
TT
2318 symbol_compare_ftype *ordered_compare) override;
2319
df35e626 2320 bool expand_symtabs_matching
39298a5d
TT
2321 (struct objfile *objfile,
2322 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2323 const lookup_name_info *lookup_name,
2324 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2325 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2326 block_search_flags search_flags,
3bfa51a7 2327 domain_enum domain,
39298a5d
TT
2328 enum search_domain kind) override;
2329};
2330
2331struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2332{
39298a5d
TT
2333 void dump (struct objfile *objfile) override;
2334
0b7b2c2a 2335 void expand_matching_symbols
39298a5d
TT
2336 (struct objfile *,
2337 const lookup_name_info &lookup_name,
2338 domain_enum domain,
2339 int global,
39298a5d
TT
2340 symbol_compare_ftype *ordered_compare) override;
2341
df35e626 2342 bool expand_symtabs_matching
39298a5d
TT
2343 (struct objfile *objfile,
2344 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2345 const lookup_name_info *lookup_name,
2346 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2347 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 2348 block_search_flags search_flags,
3bfa51a7 2349 domain_enum domain,
39298a5d
TT
2350 enum search_domain kind) override;
2351};
2352
edc02ceb 2353static quick_symbol_functions_up
39298a5d
TT
2354make_dwarf_gdb_index ()
2355{
2356 return quick_symbol_functions_up (new dwarf2_gdb_index);
2357}
2358
edc02ceb 2359static quick_symbol_functions_up
39298a5d
TT
2360make_dwarf_debug_names ()
2361{
2362 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2363}
2364
094b34ac
DE
2365/* Utility hash function for a stmt_list_hash. */
2366
2367static hashval_t
2368hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2369{
2370 hashval_t v = 0;
2371
2372 if (stmt_list_hash->dwo_unit != NULL)
2373 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2374 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2375 return v;
2376}
2377
2378/* Utility equality function for a stmt_list_hash. */
2379
2380static int
2381eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2382 const struct stmt_list_hash *rhs)
2383{
2384 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2385 return 0;
2386 if (lhs->dwo_unit != NULL
2387 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2388 return 0;
2389
9c541725 2390 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2391}
2392
7b9f3c50
DE
2393/* Hash function for a quick_file_names. */
2394
2395static hashval_t
2396hash_file_name_entry (const void *e)
2397{
9a3c8263
SM
2398 const struct quick_file_names *file_data
2399 = (const struct quick_file_names *) e;
7b9f3c50 2400
094b34ac 2401 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2402}
2403
2404/* Equality function for a quick_file_names. */
2405
2406static int
2407eq_file_name_entry (const void *a, const void *b)
2408{
9a3c8263
SM
2409 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2410 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2411
094b34ac 2412 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2413}
2414
2415/* Delete function for a quick_file_names. */
2416
2417static void
2418delete_file_name_entry (void *e)
2419{
9a3c8263 2420 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2421 int i;
2422
2423 for (i = 0; i < file_data->num_file_names; ++i)
2424 {
2425 xfree ((void*) file_data->file_names[i]);
2426 if (file_data->real_names)
2427 xfree ((void*) file_data->real_names[i]);
2428 }
2429
45940949
TT
2430 /* The space for the struct itself lives on the obstack, so we don't
2431 free it here. */
7b9f3c50
DE
2432}
2433
2434/* Create a quick_file_names hash table. */
2435
5895093f 2436static htab_up
7b9f3c50
DE
2437create_quick_file_names_table (unsigned int nr_initial_entries)
2438{
5895093f
TT
2439 return htab_up (htab_create_alloc (nr_initial_entries,
2440 hash_file_name_entry, eq_file_name_entry,
2441 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2442}
9291a0cd 2443
ab432490
SM
2444/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2445 function is unrelated to symtabs, symtab would have to be created afterwards.
2446 You should call age_cached_comp_units after processing the CU. */
918dd910 2447
1b555f17 2448static dwarf2_cu *
ab432490
SM
2449load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2450 bool skip_partial)
918dd910 2451{
3019eac3 2452 if (per_cu->is_debug_types)
ab432490 2453 load_full_type_unit (per_cu, per_objfile);
918dd910 2454 else
4a636814
SM
2455 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2456 skip_partial, language_minimal);
918dd910 2457
7188ed02
SM
2458 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2459 if (cu == nullptr)
1b555f17 2460 return nullptr; /* Dummy CU. */
2dc860c0 2461
7188ed02 2462 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2463
7188ed02 2464 return cu;
918dd910
JK
2465}
2466
1350c3b4 2467/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2468
9291a0cd 2469static void
97a1449a 2470dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2471 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2472{
f4dc4d17
DE
2473 /* Skip type_unit_groups, reading the type units they contain
2474 is handled elsewhere. */
197400e8 2475 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2476 return;
2477
08ac5771
SM
2478 {
2479 /* The destructor of dwarf2_queue_guard frees any entries left on
2480 the queue. After this point we're guaranteed to leave this function
2481 with the dwarf queue empty. */
2482 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2483
08ac5771
SM
2484 if (!per_objfile->symtab_set_p (per_cu))
2485 {
2486 queue_comp_unit (per_cu, per_objfile, language_minimal);
2487 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2488
2489 /* If we just loaded a CU from a DWO, and we're working with an index
2490 that may badly handle TUs, load all the TUs in that DWO as well.
2491 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2492 if (!per_cu->is_debug_types
2493 && cu != NULL
2494 && cu->dwo_unit != NULL
2495 && per_objfile->per_bfd->index_table != NULL
2496 && per_objfile->per_bfd->index_table->version <= 7
2497 /* DWP files aren't supported yet. */
2498 && get_dwp_file (per_objfile) == NULL)
2499 queue_and_load_all_dwo_tus (cu);
2500 }
9291a0cd 2501
08ac5771
SM
2502 process_queue (per_objfile);
2503 }
9291a0cd
TT
2504
2505 /* Age the cache, releasing compilation units that have not
2506 been used recently. */
976ca316 2507 per_objfile->age_comp_units ();
9291a0cd
TT
2508}
2509
97a1449a
SM
2510/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2511 the per-objfile for which this symtab is instantiated.
2512
2513 Returns the resulting symbol table. */
2fdf6df6 2514
43f3e411 2515static struct compunit_symtab *
97a1449a 2516dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2517 dwarf2_per_objfile *per_objfile,
97a1449a 2518 bool skip_partial)
9291a0cd 2519{
976ca316 2520 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2521
976ca316 2522 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2523 {
976ca316 2524 free_cached_comp_units freer (per_objfile);
c83dd867 2525 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2526 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2527 process_cu_includes (per_objfile);
9291a0cd 2528 }
f194fefb 2529
976ca316 2530 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2531}
2532
ff4c9fec 2533/* See declaration. */
f4dc4d17 2534
ff4c9fec 2535dwarf2_per_cu_data *
5989a64e 2536dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2537{
b76e467d 2538 if (index >= this->all_comp_units.size ())
ff4c9fec 2539 {
b76e467d 2540 index -= this->all_comp_units.size ();
b2bdb8cf 2541 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2542 return &this->all_type_units[index]->per_cu;
2543 }
f4dc4d17 2544
ff4c9fec
SM
2545 return this->all_comp_units[index];
2546}
f4dc4d17 2547
ff4c9fec 2548/* See declaration. */
2fdf6df6 2549
ff4c9fec 2550dwarf2_per_cu_data *
5989a64e 2551dwarf2_per_bfd::get_cu (int index)
1fd400ff 2552{
b76e467d 2553 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2554
ff4c9fec 2555 return this->all_comp_units[index];
f4dc4d17
DE
2556}
2557
ff4c9fec 2558/* See declaration. */
f4dc4d17 2559
ff4c9fec 2560signatured_type *
5989a64e 2561dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2562{
b2bdb8cf 2563 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2564
ff4c9fec 2565 return this->all_type_units[index];
1fd400ff
TT
2566}
2567
d3473f0c
TT
2568/* See read.h. */
2569
2570dwarf2_per_cu_data *
5989a64e 2571dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2572{
2573 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2574 result->per_bfd = this;
d3473f0c
TT
2575 result->index = m_num_psymtabs++;
2576 return result;
2577}
2578
2579/* See read.h. */
2580
2581signatured_type *
5989a64e 2582dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2583{
2584 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2585 result->per_cu.per_bfd = this;
d3473f0c
TT
2586 result->per_cu.index = m_num_psymtabs++;
2587 return result;
2588}
2589
168c9250 2590/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2591 obstack, and constructed with the specified field values. */
4b514bc8
JK
2592
2593static dwarf2_per_cu_data *
168c9250
SM
2594create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2595 struct dwarf2_section_info *section,
2596 int is_dwz,
2597 sect_offset sect_off, ULONGEST length)
4b514bc8 2598{
168c9250 2599 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2600 the_cu->sect_off = sect_off;
2601 the_cu->length = length;
4b514bc8 2602 the_cu->section = section;
168c9250 2603 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2604 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2605 the_cu->is_dwz = is_dwz;
2606 return the_cu;
2607}
2608
2ec9a5e0
TT
2609/* A helper for create_cus_from_index that handles a given list of
2610 CUs. */
2fdf6df6 2611
74a0d9f6 2612static void
168c9250 2613create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2614 const gdb_byte *cu_list, offset_type n_elements,
2615 struct dwarf2_section_info *section,
b76e467d 2616 int is_dwz)
9291a0cd 2617{
12359b5e 2618 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2619 {
74a0d9f6 2620 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2621
2622 sect_offset sect_off
2623 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2624 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2625 cu_list += 2 * 8;
2626
b76e467d 2627 dwarf2_per_cu_data *per_cu
168c9250
SM
2628 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2629 length);
2630 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2631 }
9291a0cd
TT
2632}
2633
2ec9a5e0 2634/* Read the CU list from the mapped index, and use it to create all
168c9250 2635 the CU objects for PER_BFD. */
2ec9a5e0 2636
74a0d9f6 2637static void
168c9250 2638create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2639 const gdb_byte *cu_list, offset_type cu_list_elements,
2640 const gdb_byte *dwz_list, offset_type dwz_elements)
2641{
168c9250
SM
2642 gdb_assert (per_bfd->all_comp_units.empty ());
2643 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2644
168c9250
SM
2645 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2646 &per_bfd->info, 0);
2ec9a5e0
TT
2647
2648 if (dwz_elements == 0)
74a0d9f6 2649 return;
2ec9a5e0 2650
168c9250
SM
2651 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2652 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2653 &dwz->info, 1);
2ec9a5e0
TT
2654}
2655
1fd400ff 2656/* Create the signatured type hash table from the index. */
673bfd45 2657
74a0d9f6 2658static void
12359b5e 2659create_signatured_type_table_from_index
168c9250
SM
2660 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2661 const gdb_byte *bytes, offset_type elements)
1fd400ff 2662{
168c9250
SM
2663 gdb_assert (per_bfd->all_type_units.empty ());
2664 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2665
298e9637 2666 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2667
12359b5e 2668 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2669 {
52dc124a 2670 struct signatured_type *sig_type;
9c541725 2671 ULONGEST signature;
1fd400ff 2672 void **slot;
9c541725 2673 cu_offset type_offset_in_tu;
1fd400ff 2674
74a0d9f6 2675 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2676 sect_offset sect_off
2677 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2678 type_offset_in_tu
2679 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2680 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2681 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2682 bytes += 3 * 8;
2683
168c9250 2684 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2685 sig_type->signature = signature;
9c541725 2686 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2687 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2688 sig_type->per_cu.section = section;
9c541725 2689 sig_type->per_cu.sect_off = sect_off;
52dc124a 2690 sig_type->per_cu.v.quick
168c9250 2691 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2692 struct dwarf2_per_cu_quick_data);
2693
b0b6a987 2694 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2695 *slot = sig_type;
1fd400ff 2696
168c9250 2697 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2698 }
2699
168c9250 2700 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2701}
2702
927aa2e7
JK
2703/* Create the signatured type hash table from .debug_names. */
2704
2705static void
2706create_signatured_type_table_from_debug_names
976ca316 2707 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2708 const mapped_debug_names &map,
2709 struct dwarf2_section_info *section,
2710 struct dwarf2_section_info *abbrev_section)
2711{
976ca316 2712 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2713
96b79293
TT
2714 section->read (objfile);
2715 abbrev_section->read (objfile);
927aa2e7 2716
976ca316
SM
2717 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2718 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2719
298e9637 2720 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2721
2722 for (uint32_t i = 0; i < map.tu_count; ++i)
2723 {
2724 struct signatured_type *sig_type;
927aa2e7 2725 void **slot;
927aa2e7
JK
2726
2727 sect_offset sect_off
2728 = (sect_offset) (extract_unsigned_integer
2729 (map.tu_table_reordered + i * map.offset_size,
2730 map.offset_size,
2731 map.dwarf5_byte_order));
2732
2733 comp_unit_head cu_header;
976ca316 2734 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2735 abbrev_section,
927aa2e7
JK
2736 section->buffer + to_underlying (sect_off),
2737 rcuh_kind::TYPE);
2738
976ca316 2739 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2740 sig_type->signature = cu_header.signature;
2741 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2742 sig_type->per_cu.is_debug_types = 1;
2743 sig_type->per_cu.section = section;
2744 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2745 sig_type->per_cu.v.quick
976ca316 2746 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2747 struct dwarf2_per_cu_quick_data);
2748
b0b6a987 2749 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2750 *slot = sig_type;
2751
976ca316 2752 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2753 }
2754
976ca316 2755 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2756}
2757
9291a0cd 2758/* Read the address map data from the mapped index, and use it to
efd7398e 2759 populate the psymtabs_addrmap. */
2fdf6df6 2760
9291a0cd 2761static void
976ca316 2762create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2763 struct mapped_index *index)
9291a0cd 2764{
976ca316 2765 struct objfile *objfile = per_objfile->objfile;
efd7398e 2766 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
08feed99 2767 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2768 const gdb_byte *iter, *end;
9291a0cd 2769 struct addrmap *mutable_map;
9291a0cd
TT
2770 CORE_ADDR baseaddr;
2771
8268c778
PA
2772 auto_obstack temp_obstack;
2773
9291a0cd
TT
2774 mutable_map = addrmap_create_mutable (&temp_obstack);
2775
f00a2de2
PA
2776 iter = index->address_table.data ();
2777 end = iter + index->address_table.size ();
9291a0cd 2778
b3b3bada 2779 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2780
2781 while (iter < end)
2782 {
2783 ULONGEST hi, lo, cu_index;
2784 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2785 iter += 8;
2786 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2787 iter += 8;
2788 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2789 iter += 4;
f652bce2 2790
24a55014 2791 if (lo > hi)
f652bce2 2792 {
b98664d3 2793 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2794 hex_string (lo), hex_string (hi));
24a55014 2795 continue;
f652bce2 2796 }
24a55014 2797
efd7398e 2798 if (cu_index >= per_bfd->all_comp_units.size ())
f652bce2 2799 {
b98664d3 2800 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2801 (unsigned) cu_index);
24a55014 2802 continue;
f652bce2 2803 }
24a55014 2804
79748972
TT
2805 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2806 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2807 addrmap_set_empty (mutable_map, lo, hi - 1,
efd7398e 2808 per_bfd->get_cu (cu_index));
9291a0cd
TT
2809 }
2810
efd7398e
TT
2811 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2812 &per_bfd->obstack);
9291a0cd
TT
2813}
2814
927aa2e7 2815/* Read the address map data from DWARF-5 .debug_aranges, and use it to
efd7398e 2816 populate the psymtabs_addrmap. */
927aa2e7
JK
2817
2818static void
976ca316 2819create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2820 struct dwarf2_section_info *section)
2821{
976ca316 2822 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2823 bfd *abfd = objfile->obfd;
08feed99 2824 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2825 const CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e 2826 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
2827
2828 auto_obstack temp_obstack;
2829 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2830
2831 std::unordered_map<sect_offset,
2832 dwarf2_per_cu_data *,
2833 gdb::hash_enum<sect_offset>>
2834 debug_info_offset_to_per_cu;
bab287cd 2835 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
927aa2e7 2836 {
927aa2e7
JK
2837 const auto insertpair
2838 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2839 if (!insertpair.second)
2840 {
2841 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2842 "debug_info_offset %s, ignoring .debug_aranges."),
2843 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2844 return;
2845 }
2846 }
2847
96b79293 2848 section->read (objfile);
927aa2e7
JK
2849
2850 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2851
2852 const gdb_byte *addr = section->buffer;
2853
2854 while (addr < section->buffer + section->size)
2855 {
2856 const gdb_byte *const entry_addr = addr;
2857 unsigned int bytes_read;
2858
2859 const LONGEST entry_length = read_initial_length (abfd, addr,
2860 &bytes_read);
2861 addr += bytes_read;
2862
2863 const gdb_byte *const entry_end = addr + entry_length;
2864 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2865 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2866 if (addr + entry_length > section->buffer + section->size)
2867 {
47e3f474 2868 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2869 "length %s exceeds section length %s, "
927aa2e7 2870 "ignoring .debug_aranges."),
47e3f474
TV
2871 objfile_name (objfile),
2872 plongest (entry_addr - section->buffer),
927aa2e7
JK
2873 plongest (bytes_read + entry_length),
2874 pulongest (section->size));
2875 return;
2876 }
2877
2878 /* The version number. */
2879 const uint16_t version = read_2_bytes (abfd, addr);
2880 addr += 2;
2881 if (version != 2)
2882 {
47e3f474 2883 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2884 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2885 objfile_name (objfile),
2886 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2887 return;
2888 }
2889
2890 const uint64_t debug_info_offset
2891 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2892 addr += offset_size;
2893 const auto per_cu_it
2894 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2895 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2896 {
47e3f474 2897 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2898 "debug_info_offset %s does not exists, "
2899 "ignoring .debug_aranges."),
47e3f474
TV
2900 objfile_name (objfile),
2901 plongest (entry_addr - section->buffer),
927aa2e7
JK
2902 pulongest (debug_info_offset));
2903 return;
2904 }
2905 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2906
2907 const uint8_t address_size = *addr++;
2908 if (address_size < 1 || address_size > 8)
2909 {
47e3f474 2910 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2911 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2912 objfile_name (objfile),
2913 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2914 return;
2915 }
2916
2917 const uint8_t segment_selector_size = *addr++;
2918 if (segment_selector_size != 0)
2919 {
47e3f474 2920 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2921 "segment_selector_size %u is not supported, "
2922 "ignoring .debug_aranges."),
47e3f474
TV
2923 objfile_name (objfile),
2924 plongest (entry_addr - section->buffer),
927aa2e7
JK
2925 segment_selector_size);
2926 return;
2927 }
2928
2929 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2930 size. It is undocumented by the DWARF standard but GCC does
2931 use it. */
927aa2e7
JK
2932 for (size_t padding = ((-(addr - section->buffer))
2933 & (2 * address_size - 1));
dda83cd7 2934 padding > 0; padding--)
927aa2e7
JK
2935 if (*addr++ != 0)
2936 {
47e3f474 2937 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2938 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2939 objfile_name (objfile),
2940 plongest (entry_addr - section->buffer));
927aa2e7
JK
2941 return;
2942 }
2943
2944 for (;;)
2945 {
2946 if (addr + 2 * address_size > entry_end)
2947 {
47e3f474 2948 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2949 "address list is not properly terminated, "
2950 "ignoring .debug_aranges."),
47e3f474
TV
2951 objfile_name (objfile),
2952 plongest (entry_addr - section->buffer));
927aa2e7
JK
2953 return;
2954 }
2955 ULONGEST start = extract_unsigned_integer (addr, address_size,
2956 dwarf5_byte_order);
2957 addr += address_size;
2958 ULONGEST length = extract_unsigned_integer (addr, address_size,
2959 dwarf5_byte_order);
2960 addr += address_size;
2961 if (start == 0 && length == 0)
2962 break;
efd7398e 2963 if (start == 0 && !per_bfd->has_section_at_zero)
927aa2e7
JK
2964 {
2965 /* Symbol was eliminated due to a COMDAT group. */
2966 continue;
2967 }
2968 ULONGEST end = start + length;
79748972
TT
2969 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2970 - baseaddr);
2971 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2972 - baseaddr);
927aa2e7
JK
2973 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2974 }
2975 }
2976
efd7398e
TT
2977 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2978 &per_bfd->obstack);
927aa2e7
JK
2979}
2980
4485a1c1
SM
2981/* A helper function that reads the .gdb_index from BUFFER and fills
2982 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2983 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2984 ok to use deprecated sections.
2985
2986 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2987 out parameters that are filled in with information about the CU and
2988 TU lists in the section.
2989
4485a1c1 2990 Returns true if all went well, false otherwise. */
2fdf6df6 2991
d33bc52e 2992static bool
3810f182 2993read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2994 bool deprecated_ok,
2995 gdb::array_view<const gdb_byte> buffer,
2996 struct mapped_index *map,
2997 const gdb_byte **cu_list,
2998 offset_type *cu_list_elements,
2999 const gdb_byte **types_list,
3000 offset_type *types_list_elements)
3001{
3002 const gdb_byte *addr = &buffer[0];
42c2c694 3003 offset_view metadata (buffer);
82430852 3004
9291a0cd 3005 /* Version check. */
42c2c694 3006 offset_type version = metadata[0];
987d643c 3007 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3008 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3009 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3010 indices. */
831adc1f 3011 if (version < 4)
481860b3
GB
3012 {
3013 static int warning_printed = 0;
3014 if (!warning_printed)
3015 {
3016 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3017 filename);
481860b3
GB
3018 warning_printed = 1;
3019 }
3020 return 0;
3021 }
3022 /* Index version 4 uses a different hash function than index version
3023 5 and later.
3024
3025 Versions earlier than 6 did not emit psymbols for inlined
3026 functions. Using these files will cause GDB not to be able to
3027 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3028 indices unless the user has done
3029 "set use-deprecated-index-sections on". */
2ec9a5e0 3030 if (version < 6 && !deprecated_ok)
481860b3
GB
3031 {
3032 static int warning_printed = 0;
3033 if (!warning_printed)
3034 {
e615022a
DE
3035 warning (_("\
3036Skipping deprecated .gdb_index section in %s.\n\
3037Do \"set use-deprecated-index-sections on\" before the file is read\n\
3038to use the section anyway."),
2ec9a5e0 3039 filename);
481860b3
GB
3040 warning_printed = 1;
3041 }
3042 return 0;
3043 }
796a7ff8 3044 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3045 of the TU (for symbols coming from TUs),
3046 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3047 Plus gold-generated indices can have duplicate entries for global symbols,
3048 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3049 These are just performance bugs, and we can't distinguish gdb-generated
3050 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3051
481860b3 3052 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3053 longer backward compatible. */
796a7ff8 3054 if (version > 8)
594e8718 3055 return 0;
9291a0cd 3056
559a7a62 3057 map->version = version;
9291a0cd 3058
42c2c694
TT
3059 int i = 1;
3060 *cu_list = addr + metadata[i];
3061 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
1fd400ff
TT
3062 ++i;
3063
42c2c694
TT
3064 *types_list = addr + metadata[i];
3065 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
987d643c 3066 ++i;
1fd400ff 3067
42c2c694
TT
3068 const gdb_byte *address_table = addr + metadata[i];
3069 const gdb_byte *address_table_end = addr + metadata[i + 1];
f00a2de2
PA
3070 map->address_table
3071 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3072 ++i;
3073
42c2c694
TT
3074 const gdb_byte *symbol_table = addr + metadata[i];
3075 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
f00a2de2 3076 map->symbol_table
42c2c694
TT
3077 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
3078 symbol_table_end));
9291a0cd 3079
f00a2de2 3080 ++i;
42c2c694 3081 map->constant_pool = buffer.slice (metadata[i]);
1fd400ff 3082
2ec9a5e0
TT
3083 return 1;
3084}
3085
4485a1c1
SM
3086/* Callback types for dwarf2_read_gdb_index. */
3087
3088typedef gdb::function_view
5989a64e 3089 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3090 get_gdb_index_contents_ftype;
3091typedef gdb::function_view
3092 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3093 get_gdb_index_contents_dwz_ftype;
3094
927aa2e7 3095/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3096 elements of all the CUs and return 1. Otherwise, return 0. */
3097
3098static int
4485a1c1 3099dwarf2_read_gdb_index
976ca316 3100 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3101 get_gdb_index_contents_ftype get_gdb_index_contents,
3102 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3103{
2ec9a5e0
TT
3104 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3105 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3106 struct dwz_file *dwz;
976ca316 3107 struct objfile *objfile = per_objfile->objfile;
f8c41851 3108 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3109
4485a1c1 3110 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3111 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3112
3113 if (main_index_contents.empty ())
3114 return 0;
3115
3063847f 3116 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3117 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3118 use_deprecated_index_sections,
3119 main_index_contents, map.get (), &cu_list,
3120 &cu_list_elements, &types_list,
3121 &types_list_elements))
2ec9a5e0
TT
3122 return 0;
3123
0fefef59 3124 /* Don't use the index if it's empty. */
3063847f 3125 if (map->symbol_table.empty ())
0fefef59
DE
3126 return 0;
3127
2ec9a5e0
TT
3128 /* If there is a .dwz file, read it so we can get its CU list as
3129 well. */
f8c41851 3130 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3131 if (dwz != NULL)
2ec9a5e0 3132 {
2ec9a5e0
TT
3133 struct mapped_index dwz_map;
3134 const gdb_byte *dwz_types_ignore;
3135 offset_type dwz_types_elements_ignore;
3136
4485a1c1
SM
3137 gdb::array_view<const gdb_byte> dwz_index_content
3138 = get_gdb_index_contents_dwz (objfile, dwz);
3139
3140 if (dwz_index_content.empty ())
3141 return 0;
3142
3810f182 3143 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3144 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3145 &dwz_list, &dwz_list_elements,
3146 &dwz_types_ignore,
3147 &dwz_types_elements_ignore))
2ec9a5e0
TT
3148 {
3149 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3150 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3151 return 0;
3152 }
3153 }
3154
f8c41851
SM
3155 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3156 dwz_list_elements);
1fd400ff 3157
8b70b953
TT
3158 if (types_list_elements)
3159 {
8b70b953
TT
3160 /* We can only handle a single .debug_types when we have an
3161 index. */
f8c41851 3162 if (per_bfd->types.size () != 1)
8b70b953
TT
3163 return 0;
3164
f8c41851 3165 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3166
f8c41851 3167 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3168 types_list_elements);
8b70b953 3169 }
9291a0cd 3170
976ca316 3171 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3172
f8c41851
SM
3173 per_bfd->index_table = std::move (map);
3174 per_bfd->using_index = 1;
3175 per_bfd->quick_file_names_table =
3176 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3177
9291a0cd
TT
3178 return 1;
3179}
3180
dee91e82 3181/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3182
dee91e82
DE
3183static void
3184dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 3185 struct die_info *comp_unit_die)
9291a0cd 3186{
dee91e82 3187 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3188 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3189 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3190 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3191 struct attribute *attr;
7b9f3c50
DE
3192 void **slot;
3193 struct quick_file_names *qfn;
9291a0cd 3194
0186c6a7
DE
3195 gdb_assert (! this_cu->is_debug_types);
3196
07261596
TT
3197 /* Our callers never want to match partial units -- instead they
3198 will match the enclosing full CU. */
3199 if (comp_unit_die->tag == DW_TAG_partial_unit)
3200 {
3201 this_cu->v.quick->no_file_data = 1;
3202 return;
3203 }
3204
0186c6a7 3205 lh_cu = this_cu;
7b9f3c50 3206 slot = NULL;
dee91e82 3207
fff8551c 3208 line_header_up lh;
9c541725 3209 sect_offset line_offset {};
fff8551c 3210
dee91e82 3211 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3212 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3213 {
7b9f3c50
DE
3214 struct quick_file_names find_entry;
3215
d4df075e 3216 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3217
3218 /* We may have already read in this line header (TU line header sharing).
3219 If we have we're done. */
094b34ac 3220 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3221 find_entry.hash.line_sect_off = line_offset;
976ca316 3222 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3223 &find_entry, INSERT);
3224 if (*slot != NULL)
3225 {
9a3c8263 3226 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3227 return;
7b9f3c50
DE
3228 }
3229
3019eac3 3230 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3231 }
3232 if (lh == NULL)
3233 {
094b34ac 3234 lh_cu->v.quick->no_file_data = 1;
dee91e82 3235 return;
9291a0cd
TT
3236 }
3237
976ca316 3238 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3239 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3240 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3241 gdb_assert (slot != NULL);
3242 *slot = qfn;
9291a0cd 3243
d721ba37 3244 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3245
aa391654
TT
3246 int offset = 0;
3247 if (strcmp (fnd.name, "<unknown>") != 0)
3248 ++offset;
3249
7ba99d21 3250 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3251 qfn->file_names =
976ca316 3252 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3253 qfn->num_file_names);
aa391654
TT
3254 if (offset != 0)
3255 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3256 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3257 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3258 fnd.comp_dir).release ();
7b9f3c50 3259 qfn->real_names = NULL;
9291a0cd 3260
094b34ac 3261 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3262}
3263
3264/* A helper for the "quick" functions which attempts to read the line
3265 table for THIS_CU. */
3266
3267static struct quick_file_names *
ab432490
SM
3268dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3269 dwarf2_per_objfile *per_objfile)
dee91e82 3270{
0186c6a7
DE
3271 /* This should never be called for TUs. */
3272 gdb_assert (! this_cu->is_debug_types);
3273 /* Nor type unit groups. */
197400e8 3274 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3275
dee91e82
DE
3276 if (this_cu->v.quick->file_names != NULL)
3277 return this_cu->v.quick->file_names;
3278 /* If we know there is no line data, no point in looking again. */
3279 if (this_cu->v.quick->no_file_data)
3280 return NULL;
3281
ab432490 3282 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3283 if (!reader.dummy_p)
eb5dd737 3284 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
3285
3286 if (this_cu->v.quick->no_file_data)
3287 return NULL;
3288 return this_cu->v.quick->file_names;
9291a0cd
TT
3289}
3290
3291/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3292 real path for a given file name from the line table. */
2fdf6df6 3293
9291a0cd 3294static const char *
976ca316 3295dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3296 struct quick_file_names *qfn, int index)
9291a0cd 3297{
7b9f3c50 3298 if (qfn->real_names == NULL)
976ca316 3299 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3300 qfn->num_file_names, const char *);
9291a0cd 3301
7b9f3c50 3302 if (qfn->real_names[index] == NULL)
14278e1f 3303 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3304
7b9f3c50 3305 return qfn->real_names[index];
9291a0cd
TT
3306}
3307
39298a5d
TT
3308struct symtab *
3309dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
9291a0cd 3310{
976ca316
SM
3311 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3312 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3313 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3314
43f3e411
DE
3315 if (cust == NULL)
3316 return NULL;
ed2dc618 3317
43f3e411 3318 return compunit_primary_filetab (cust);
9291a0cd
TT
3319}
3320
7b9f3c50
DE
3321/* Traversal function for dw2_forget_cached_source_info. */
3322
3323static int
3324dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3325{
7b9f3c50 3326 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3327
7b9f3c50 3328 if (file_data->real_names)
9291a0cd 3329 {
7b9f3c50 3330 int i;
9291a0cd 3331
7b9f3c50 3332 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3333 {
7b9f3c50
DE
3334 xfree ((void*) file_data->real_names[i]);
3335 file_data->real_names[i] = NULL;
9291a0cd
TT
3336 }
3337 }
7b9f3c50
DE
3338
3339 return 1;
3340}
3341
39298a5d
TT
3342void
3343dwarf2_base_index_functions::forget_cached_source_info
3344 (struct objfile *objfile)
7b9f3c50 3345{
976ca316 3346 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3347
976ca316 3348 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3349 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3350}
3351
da51c347
DE
3352/* Struct used to manage iterating over all CUs looking for a symbol. */
3353
3354struct dw2_symtab_iterator
9291a0cd 3355{
ed2dc618 3356 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3357 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3358 /* If set, only look for symbols that match that block. Valid values are
3359 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3360 gdb::optional<block_enum> block_index;
da51c347
DE
3361 /* The kind of symbol we're looking for. */
3362 domain_enum domain;
3363 /* The list of CUs from the index entry of the symbol,
3364 or NULL if not found. */
42c2c694 3365 offset_view vec;
da51c347
DE
3366 /* The next element in VEC to look at. */
3367 int next;
3368 /* The number of elements in VEC, or zero if there is no match. */
3369 int length;
8943b874
DE
3370 /* Have we seen a global version of the symbol?
3371 If so we can ignore all further global instances.
3372 This is to work around gold/15646, inefficient gold-generated
3373 indices. */
3374 int global_seen;
da51c347 3375};
9291a0cd 3376
7089bd88 3377/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2fdf6df6 3378
9291a0cd 3379static void
7089bd88
TT
3380dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3381 dwarf2_per_objfile *per_objfile,
3382 gdb::optional<block_enum> block_index,
3383 domain_enum domain, offset_type namei)
da51c347 3384{
976ca316 3385 iter->per_objfile = per_objfile;
da51c347
DE
3386 iter->block_index = block_index;
3387 iter->domain = domain;
3388 iter->next = 0;
8943b874 3389 iter->global_seen = 0;
42c2c694 3390 iter->vec = {};
e5f3ece2 3391 iter->length = 0;
e5f3ece2
TV
3392
3393 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3394 /* index is NULL if OBJF_READNOW. */
3395 if (index == NULL)
3396 return;
3397
3398 gdb_assert (!index->symbol_name_slot_invalid (namei));
42c2c694 3399 offset_type vec_idx = index->symbol_vec_index (namei);
e5f3ece2 3400
42c2c694
TT
3401 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3402 iter->length = iter->vec[0];
da51c347
DE
3403}
3404
3405/* Return the next matching CU or NULL if there are no more. */
3406
3407static struct dwarf2_per_cu_data *
3408dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3409{
976ca316 3410 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3411
da51c347
DE
3412 for ( ; iter->next < iter->length; ++iter->next)
3413 {
42c2c694 3414 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
da51c347 3415 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3416 gdb_index_symbol_kind symbol_kind =
3417 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3418 /* Only check the symbol attributes if they're present.
3419 Indices prior to version 7 don't record them,
3420 and indices >= 7 may elide them for certain symbols
3421 (gold does this). */
3422 int attrs_valid =
976ca316 3423 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3424 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3425
3190f0c6 3426 /* Don't crash on bad data. */
976ca316
SM
3427 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3428 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3429 {
b98664d3 3430 complaint (_(".gdb_index entry has bad CU index"
976ca316 3431 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3432 continue;
3433 }
3434
976ca316 3435 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3436
da51c347 3437 /* Skip if already read in. */
976ca316 3438 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3439 continue;
3440
8943b874
DE
3441 /* Check static vs global. */
3442 if (attrs_valid)
3443 {
2b79f376
SM
3444 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3445
3446 if (iter->block_index.has_value ())
3447 {
3448 bool want_static = *iter->block_index == STATIC_BLOCK;
3449
3450 if (is_static != want_static)
3451 continue;
3452 }
3453
8943b874 3454 /* Work around gold/15646. */
f030440d
TV
3455 if (!is_static
3456 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3457 {
3458 if (iter->global_seen)
3459 continue;
3460
3461 iter->global_seen = 1;
3462 }
8943b874 3463 }
da51c347
DE
3464
3465 /* Only check the symbol's kind if it has one. */
3466 if (attrs_valid)
3467 {
3468 switch (iter->domain)
3469 {
3470 case VAR_DOMAIN:
3471 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3472 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3473 /* Some types are also in VAR_DOMAIN. */
3474 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3475 continue;
3476 break;
3477 case STRUCT_DOMAIN:
3478 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3479 continue;
3480 break;
3481 case LABEL_DOMAIN:
3482 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3483 continue;
3484 break;
59c35742
AB
3485 case MODULE_DOMAIN:
3486 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3487 continue;
3488 break;
da51c347
DE
3489 default:
3490 break;
3491 }
3492 }
3493
3494 ++iter->next;
3495 return per_cu;
3496 }
3497
3498 return NULL;
3499}
3500
39298a5d 3501void
4829711b
TT
3502dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3503 bool print_bcache)
9291a0cd 3504{
4829711b
TT
3505 if (print_bcache)
3506 return;
3507
976ca316
SM
3508 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3509 int total = (per_objfile->per_bfd->all_comp_units.size ()
3510 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3511 int count = 0;
9291a0cd 3512
ed2dc618 3513 for (int i = 0; i < total; ++i)
9291a0cd 3514 {
976ca316 3515 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3516
976ca316 3517 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3518 ++count;
3519 }
e4a48d9d 3520 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3521 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3522}
3523
779bd270
DE
3524/* This dumps minimal information about the index.
3525 It is called via "mt print objfiles".
3526 One use is to verify .gdb_index has been loaded by the
3527 gdb.dwarf2/gdb-index.exp testcase. */
3528
39298a5d
TT
3529void
3530dwarf2_gdb_index::dump (struct objfile *objfile)
9291a0cd 3531{
976ca316 3532 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3533
976ca316 3534 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3535 printf_filtered (".gdb_index:");
976ca316 3536 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3537 {
3538 printf_filtered (" version %d\n",
976ca316 3539 per_objfile->per_bfd->index_table->version);
779bd270
DE
3540 }
3541 else
3542 printf_filtered (" faked for \"readnow\"\n");
3543 printf_filtered ("\n");
9291a0cd
TT
3544}
3545
39298a5d
TT
3546void
3547dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
9291a0cd 3548{
976ca316
SM
3549 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3550 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3551 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3552
ed2dc618 3553 for (int i = 0; i < total_units; ++i)
9291a0cd 3554 {
976ca316 3555 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3556
58f0c718
TT
3557 /* We don't want to directly expand a partial CU, because if we
3558 read it with the wrong language, then assertion failures can
3559 be triggered later on. See PR symtab/23010. So, tell
3560 dw2_instantiate_symtab to skip partial CUs -- any important
3561 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3562 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3563 }
3564}
3565
df35e626 3566static bool
9a0bacfb
TV
3567dw2_expand_symtabs_matching_symbol
3568 (mapped_index_base &index,
3569 const lookup_name_info &lookup_name_in,
3570 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3571 gdb::function_view<bool (offset_type)> match_callback,
3572 dwarf2_per_objfile *per_objfile);
9a0bacfb 3573
df35e626 3574static bool
9a0bacfb 3575dw2_expand_symtabs_matching_one
97a1449a
SM
3576 (dwarf2_per_cu_data *per_cu,
3577 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3578 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3579 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3580
af82f89d 3581void
0b7b2c2a 3582dwarf2_gdb_index::expand_matching_symbols
199b4314 3583 (struct objfile *objfile,
b054970d 3584 const lookup_name_info &name, domain_enum domain,
199b4314 3585 int global,
199b4314 3586 symbol_compare_ftype *ordered_compare)
9291a0cd 3587{
1aa98955 3588 /* Used for Ada. */
976ca316 3589 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3590
9a0bacfb
TV
3591 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3592
976ca316 3593 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3594 {
976ca316 3595 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3596
9a0bacfb
TV
3597 const char *match_name = name.ada ().lookup_name ().c_str ();
3598 auto matcher = [&] (const char *symname)
3599 {
3600 if (ordered_compare == nullptr)
3601 return true;
3602 return ordered_compare (symname, match_name) == 0;
3603 };
3604
b64f703b 3605 dw2_expand_symtabs_matching_symbol (index, name, matcher,
9a0bacfb
TV
3606 [&] (offset_type namei)
3607 {
3608 struct dw2_symtab_iterator iter;
3609 struct dwarf2_per_cu_data *per_cu;
3610
976ca316 3611 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3612 namei);
9a0bacfb 3613 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3614 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3615 nullptr);
9a0bacfb 3616 return true;
976ca316 3617 }, per_objfile);
9a0bacfb
TV
3618 }
3619 else
3620 {
3621 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3622 proceed assuming all symtabs have been read in. */
3623 }
9291a0cd
TT
3624}
3625
e1ef7d7a
PA
3626/* Starting from a search name, return the string that finds the upper
3627 bound of all strings that start with SEARCH_NAME in a sorted name
3628 list. Returns the empty string to indicate that the upper bound is
3629 the end of the list. */
3630
3631static std::string
3632make_sort_after_prefix_name (const char *search_name)
3633{
3634 /* When looking to complete "func", we find the upper bound of all
3635 symbols that start with "func" by looking for where we'd insert
3636 the closest string that would follow "func" in lexicographical
3637 order. Usually, that's "func"-with-last-character-incremented,
3638 i.e. "fund". Mind non-ASCII characters, though. Usually those
3639 will be UTF-8 multi-byte sequences, but we can't be certain.
3640 Especially mind the 0xff character, which is a valid character in
3641 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3642 rule out compilers allowing it in identifiers. Note that
3643 conveniently, strcmp/strcasecmp are specified to compare
3644 characters interpreted as unsigned char. So what we do is treat
3645 the whole string as a base 256 number composed of a sequence of
3646 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3647 to 0, and carries 1 to the following more-significant position.
3648 If the very first character in SEARCH_NAME ends up incremented
3649 and carries/overflows, then the upper bound is the end of the
3650 list. The string after the empty string is also the empty
3651 string.
3652
3653 Some examples of this operation:
3654
3655 SEARCH_NAME => "+1" RESULT
3656
3657 "abc" => "abd"
3658 "ab\xff" => "ac"
3659 "\xff" "a" "\xff" => "\xff" "b"
3660 "\xff" => ""
3661 "\xff\xff" => ""
3662 "" => ""
3663
3664 Then, with these symbols for example:
3665
3666 func
3667 func1
3668 fund
3669
3670 completing "func" looks for symbols between "func" and
3671 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3672 which finds "func" and "func1", but not "fund".
3673
3674 And with:
3675
3676 funcÿ (Latin1 'ÿ' [0xff])
3677 funcÿ1
3678 fund
3679
3680 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3681 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3682
3683 And with:
3684
3685 ÿÿ (Latin1 'ÿ' [0xff])
3686 ÿÿ1
3687
3688 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3689 the end of the list.
3690 */
3691 std::string after = search_name;
3692 while (!after.empty () && (unsigned char) after.back () == 0xff)
3693 after.pop_back ();
3694 if (!after.empty ())
3695 after.back () = (unsigned char) after.back () + 1;
3696 return after;
3697}
3698
5c58de74 3699/* See declaration. */
61d96d7e 3700
5c58de74
PA
3701std::pair<std::vector<name_component>::const_iterator,
3702 std::vector<name_component>::const_iterator>
44ed8f3e 3703mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3704 (const lookup_name_info &lookup_name_without_params, language lang,
3705 dwarf2_per_objfile *per_objfile) const
3f563c84 3706{
5c58de74
PA
3707 auto *name_cmp
3708 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3709
3b00ef10 3710 const char *lang_name
e0802d59 3711 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3712
3f563c84
PA
3713 /* Comparison function object for lower_bound that matches against a
3714 given symbol name. */
3715 auto lookup_compare_lower = [&] (const name_component &elem,
3716 const char *name)
3717 {
fcf23d5b 3718 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3719 const char *elem_name = elem_qualified + elem.name_offset;
3720 return name_cmp (elem_name, name) < 0;
3721 };
3722
3723 /* Comparison function object for upper_bound that matches against a
3724 given symbol name. */
3725 auto lookup_compare_upper = [&] (const char *name,
3726 const name_component &elem)
3727 {
fcf23d5b 3728 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3729 const char *elem_name = elem_qualified + elem.name_offset;
3730 return name_cmp (name, elem_name) < 0;
3731 };
3732
5c58de74
PA
3733 auto begin = this->name_components.begin ();
3734 auto end = this->name_components.end ();
3f563c84
PA
3735
3736 /* Find the lower bound. */
3737 auto lower = [&] ()
3738 {
3b00ef10 3739 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3740 return begin;
3741 else
3b00ef10 3742 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3743 } ();
3744
3745 /* Find the upper bound. */
3746 auto upper = [&] ()
3747 {
5c58de74 3748 if (lookup_name_without_params.completion_mode ())
3f563c84 3749 {
e1ef7d7a
PA
3750 /* In completion mode, we want UPPER to point past all
3751 symbols names that have the same prefix. I.e., with
3752 these symbols, and completing "func":
3753
3754 function << lower bound
3755 function1
3756 other_function << upper bound
3757
3758 We find the upper bound by looking for the insertion
3759 point of "func"-with-last-character-incremented,
3760 i.e. "fund". */
3b00ef10 3761 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3762 if (after.empty ())
3f563c84 3763 return end;
e6b2f5ef
PA
3764 return std::lower_bound (lower, end, after.c_str (),
3765 lookup_compare_lower);
3f563c84
PA
3766 }
3767 else
3b00ef10 3768 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3769 } ();
3770
5c58de74
PA
3771 return {lower, upper};
3772}
3773
3774/* See declaration. */
3775
3776void
fcf23d5b 3777mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3778{
3779 if (!this->name_components.empty ())
3780 return;
3781
3782 this->name_components_casing = case_sensitivity;
3783 auto *name_cmp
3784 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3785
3786 /* The code below only knows how to break apart components of C++
3787 symbol names (and other languages that use '::' as
3b00ef10 3788 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3789 auto count = this->symbol_name_count ();
3790 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3791 {
44ed8f3e 3792 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3793 continue;
3794
fcf23d5b 3795 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3796
3797 /* Add each name component to the name component table. */
3798 unsigned int previous_len = 0;
3b00ef10
TT
3799
3800 if (strstr (name, "::") != nullptr)
3801 {
3802 for (unsigned int current_len = cp_find_first_component (name);
3803 name[current_len] != '\0';
3804 current_len += cp_find_first_component (name + current_len))
3805 {
3806 gdb_assert (name[current_len] == ':');
3807 this->name_components.push_back ({previous_len, idx});
3808 /* Skip the '::'. */
3809 current_len += 2;
3810 previous_len = current_len;
3811 }
3812 }
3813 else
5c58de74 3814 {
3b00ef10
TT
3815 /* Handle the Ada encoded (aka mangled) form here. */
3816 for (const char *iter = strstr (name, "__");
3817 iter != nullptr;
3818 iter = strstr (iter, "__"))
3819 {
3820 this->name_components.push_back ({previous_len, idx});
3821 iter += 2;
3822 previous_len = iter - name;
3823 }
5c58de74 3824 }
3b00ef10 3825
5c58de74
PA
3826 this->name_components.push_back ({previous_len, idx});
3827 }
3828
3829 /* Sort name_components elements by name. */
3830 auto name_comp_compare = [&] (const name_component &left,
3831 const name_component &right)
3832 {
fcf23d5b
SM
3833 const char *left_qualified
3834 = this->symbol_name_at (left.idx, per_objfile);
3835 const char *right_qualified
3836 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
3837
3838 const char *left_name = left_qualified + left.name_offset;
3839 const char *right_name = right_qualified + right.name_offset;
3840
3841 return name_cmp (left_name, right_name) < 0;
3842 };
3843
3844 std::sort (this->name_components.begin (),
3845 this->name_components.end (),
3846 name_comp_compare);
3847}
3848
3849/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3850 mapped_index_base instead of the containing objfile. This is split
3851 to a separate function in order to be able to unit test the
3852 name_components matching using a mock mapped_index_base. For each
5c58de74 3853 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3854 symbol's index in the mapped_index_base symbol table. */
5c58de74 3855
df35e626 3856static bool
5c58de74 3857dw2_expand_symtabs_matching_symbol
44ed8f3e 3858 (mapped_index_base &index,
5c58de74
PA
3859 const lookup_name_info &lookup_name_in,
3860 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
fcf23d5b
SM
3861 gdb::function_view<bool (offset_type)> match_callback,
3862 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3863{
3864 lookup_name_info lookup_name_without_params
3865 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3866
3867 /* Build the symbol name component sorted vector, if we haven't
3868 yet. */
fcf23d5b 3869 index.build_name_components (per_objfile);
5c58de74 3870
3f563c84
PA
3871 /* The same symbol may appear more than once in the range though.
3872 E.g., if we're looking for symbols that complete "w", and we have
3873 a symbol named "w1::w2", we'll find the two name components for
3874 that same symbol in the range. To be sure we only call the
3875 callback once per symbol, we first collect the symbol name
3876 indexes that matched in a temporary vector and ignore
3877 duplicates. */
3878 std::vector<offset_type> matches;
3f563c84 3879
3b00ef10
TT
3880 struct name_and_matcher
3881 {
3882 symbol_name_matcher_ftype *matcher;
ecc6c606 3883 const char *name;
3b00ef10
TT
3884
3885 bool operator== (const name_and_matcher &other) const
3f563c84 3886 {
ecc6c606 3887 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3888 }
3889 };
3890
3891 /* A vector holding all the different symbol name matchers, for all
3892 languages. */
3893 std::vector<name_and_matcher> matchers;
3894
3895 for (int i = 0; i < nr_languages; i++)
3896 {
3897 enum language lang_e = (enum language) i;
3898
3899 const language_defn *lang = language_def (lang_e);
3900 symbol_name_matcher_ftype *name_matcher
c9debfb9 3901 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 3902
3b00ef10 3903 name_and_matcher key {
dda83cd7 3904 name_matcher,
3b00ef10
TT
3905 lookup_name_without_params.language_lookup_name (lang_e)
3906 };
3907
3908 /* Don't insert the same comparison routine more than once.
3909 Note that we do this linear walk. This is not a problem in
3910 practice because the number of supported languages is
3911 low. */
3912 if (std::find (matchers.begin (), matchers.end (), key)
3913 != matchers.end ())
9291a0cd 3914 continue;
3b00ef10
TT
3915 matchers.push_back (std::move (key));
3916
3917 auto bounds
3918 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 3919 lang_e, per_objfile);
3b00ef10
TT
3920
3921 /* Now for each symbol name in range, check to see if we have a name
3922 match, and if so, call the MATCH_CALLBACK callback. */
3923
3924 for (; bounds.first != bounds.second; ++bounds.first)
3925 {
fcf23d5b
SM
3926 const char *qualified
3927 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
3928
3929 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3930 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3931 continue;
9291a0cd 3932
3b00ef10
TT
3933 matches.push_back (bounds.first->idx);
3934 }
3f563c84
PA
3935 }
3936
3937 std::sort (matches.begin (), matches.end ());
3938
3939 /* Finally call the callback, once per match. */
3940 ULONGEST prev = -1;
df35e626 3941 bool result = true;
3f563c84
PA
3942 for (offset_type idx : matches)
3943 {
3944 if (prev != idx)
3945 {
3b00ef10 3946 if (!match_callback (idx))
df35e626
TT
3947 {
3948 result = false;
3949 break;
3950 }
3f563c84
PA
3951 prev = idx;
3952 }
3953 }
3954
3955 /* Above we use a type wider than idx's for 'prev', since 0 and
3956 (offset_type)-1 are both possible values. */
3957 static_assert (sizeof (prev) > sizeof (offset_type), "");
df35e626
TT
3958
3959 return result;
3f563c84
PA
3960}
3961
c62446b1
PA
3962#if GDB_SELF_TEST
3963
3964namespace selftests { namespace dw2_expand_symtabs_matching {
3965
a3c5fafd
PA
3966/* A mock .gdb_index/.debug_names-like name index table, enough to
3967 exercise dw2_expand_symtabs_matching_symbol, which works with the
3968 mapped_index_base interface. Builds an index from the symbol list
3969 passed as parameter to the constructor. */
3970class mock_mapped_index : public mapped_index_base
c62446b1
PA
3971{
3972public:
a3c5fafd
PA
3973 mock_mapped_index (gdb::array_view<const char *> symbols)
3974 : m_symbol_table (symbols)
c62446b1
PA
3975 {}
3976
a3c5fafd 3977 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 3978
a3c5fafd 3979 /* Return the number of names in the symbol table. */
632e107b 3980 size_t symbol_name_count () const override
c62446b1 3981 {
a3c5fafd 3982 return m_symbol_table.size ();
c62446b1
PA
3983 }
3984
a3c5fafd 3985 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
3986 const char *symbol_name_at
3987 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
3988 {
3989 return m_symbol_table[idx];
3990 }
c62446b1 3991
a3c5fafd
PA
3992private:
3993 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
3994};
3995
3996/* Convenience function that converts a NULL pointer to a "<null>"
3997 string, to pass to print routines. */
3998
3999static const char *
4000string_or_null (const char *str)
4001{
4002 return str != NULL ? str : "<null>";
4003}
4004
4005/* Check if a lookup_name_info built from
4006 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4007 index. EXPECTED_LIST is the list of expected matches, in expected
4008 matching order. If no match expected, then an empty list is
4009 specified. Returns true on success. On failure prints a warning
4010 indicating the file:line that failed, and returns false. */
4011
4012static bool
4013check_match (const char *file, int line,
4014 mock_mapped_index &mock_index,
4015 const char *name, symbol_name_match_type match_type,
4016 bool completion_mode,
fcf23d5b
SM
4017 std::initializer_list<const char *> expected_list,
4018 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4019{
4020 lookup_name_info lookup_name (name, match_type, completion_mode);
4021
4022 bool matched = true;
4023
4024 auto mismatch = [&] (const char *expected_str,
4025 const char *got)
4026 {
4027 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4028 "expected=\"%s\", got=\"%s\"\n"),
4029 file, line,
4030 (match_type == symbol_name_match_type::FULL
4031 ? "FULL" : "WILD"),
4032 name, string_or_null (expected_str), string_or_null (got));
4033 matched = false;
4034 };
4035
4036 auto expected_it = expected_list.begin ();
4037 auto expected_end = expected_list.end ();
4038
a3c5fafd 4039 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
b64f703b 4040 nullptr,
c62446b1
PA
4041 [&] (offset_type idx)
4042 {
fcf23d5b 4043 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4044 const char *expected_str
4045 = expected_it == expected_end ? NULL : *expected_it++;
4046
4047 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4048 mismatch (expected_str, matched_name);
3b00ef10 4049 return true;
fcf23d5b 4050 }, per_objfile);
c62446b1
PA
4051
4052 const char *expected_str
4053 = expected_it == expected_end ? NULL : *expected_it++;
4054 if (expected_str != NULL)
4055 mismatch (expected_str, NULL);
4056
4057 return matched;
4058}
4059
4060/* The symbols added to the mock mapped_index for testing (in
4061 canonical form). */
4062static const char *test_symbols[] = {
4063 "function",
4064 "std::bar",
4065 "std::zfunction",
4066 "std::zfunction2",
4067 "w1::w2",
4068 "ns::foo<char*>",
4069 "ns::foo<int>",
4070 "ns::foo<long>",
a20714ff
PA
4071 "ns2::tmpl<int>::foo2",
4072 "(anonymous namespace)::A::B::C",
c62446b1 4073
e1ef7d7a
PA
4074 /* These are used to check that the increment-last-char in the
4075 matching algorithm for completion doesn't match "t1_fund" when
4076 completing "t1_func". */
4077 "t1_func",
4078 "t1_func1",
4079 "t1_fund",
4080 "t1_fund1",
4081
4082 /* A UTF-8 name with multi-byte sequences to make sure that
4083 cp-name-parser understands this as a single identifier ("função"
4084 is "function" in PT). */
4085 u8"u8função",
4086
4087 /* \377 (0xff) is Latin1 'ÿ'. */
4088 "yfunc\377",
4089
4090 /* \377 (0xff) is Latin1 'ÿ'. */
4091 "\377",
4092 "\377\377123",
4093
c62446b1
PA
4094 /* A name with all sorts of complications. Starts with "z" to make
4095 it easier for the completion tests below. */
4096#define Z_SYM_NAME \
4097 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4098 "::tuple<(anonymous namespace)::ui*, " \
4099 "std::default_delete<(anonymous namespace)::ui>, void>"
4100
4101 Z_SYM_NAME
4102};
4103
a3c5fafd
PA
4104/* Returns true if the mapped_index_base::find_name_component_bounds
4105 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4106 in completion mode. */
5c58de74
PA
4107
4108static bool
a3c5fafd 4109check_find_bounds_finds (mapped_index_base &index,
5c58de74 4110 const char *search_name,
fcf23d5b
SM
4111 gdb::array_view<const char *> expected_syms,
4112 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4113{
4114 lookup_name_info lookup_name (search_name,
4115 symbol_name_match_type::FULL, true);
4116
3b00ef10 4117 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4118 language_cplus,
4119 per_objfile);
5c58de74
PA
4120
4121 size_t distance = std::distance (bounds.first, bounds.second);
4122 if (distance != expected_syms.size ())
4123 return false;
4124
4125 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4126 {
4127 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4128 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4129 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4130 return false;
4131 }
4132
4133 return true;
4134}
4135
4136/* Test the lower-level mapped_index::find_name_component_bounds
4137 method. */
4138
c62446b1 4139static void
5c58de74
PA
4140test_mapped_index_find_name_component_bounds ()
4141{
4142 mock_mapped_index mock_index (test_symbols);
4143
fcf23d5b 4144 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4145
4146 /* Test the lower-level mapped_index::find_name_component_bounds
4147 method in completion mode. */
4148 {
4149 static const char *expected_syms[] = {
4150 "t1_func",
4151 "t1_func1",
5c58de74
PA
4152 };
4153
fcf23d5b
SM
4154 SELF_CHECK (check_find_bounds_finds
4155 (mock_index, "t1_func", expected_syms,
4156 NULL /* per_objfile */));
5c58de74
PA
4157 }
4158
4159 /* Check that the increment-last-char in the name matching algorithm
4160 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4161 {
4162 static const char *expected_syms1[] = {
4163 "\377",
4164 "\377\377123",
4165 };
fcf23d5b
SM
4166 SELF_CHECK (check_find_bounds_finds
4167 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4168
4169 static const char *expected_syms2[] = {
4170 "\377\377123",
4171 };
fcf23d5b
SM
4172 SELF_CHECK (check_find_bounds_finds
4173 (mock_index, "\377\377", expected_syms2,
4174 NULL /* per_objfile */));
5c58de74
PA
4175 }
4176}
4177
4178/* Test dw2_expand_symtabs_matching_symbol. */
4179
4180static void
4181test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4182{
4183 mock_mapped_index mock_index (test_symbols);
4184
4185 /* We let all tests run until the end even if some fails, for debug
4186 convenience. */
4187 bool any_mismatch = false;
4188
4189 /* Create the expected symbols list (an initializer_list). Needed
4190 because lists have commas, and we need to pass them to CHECK,
4191 which is a macro. */
4192#define EXPECT(...) { __VA_ARGS__ }
4193
4194 /* Wrapper for check_match that passes down the current
4195 __FILE__/__LINE__. */
4196#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4197 any_mismatch |= !check_match (__FILE__, __LINE__, \
4198 mock_index, \
4199 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4200 EXPECTED_LIST, NULL)
c62446b1
PA
4201
4202 /* Identity checks. */
4203 for (const char *sym : test_symbols)
4204 {
4205 /* Should be able to match all existing symbols. */
4206 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4207 EXPECT (sym));
4208
4209 /* Should be able to match all existing symbols with
4210 parameters. */
4211 std::string with_params = std::string (sym) + "(int)";
4212 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4213 EXPECT (sym));
4214
4215 /* Should be able to match all existing symbols with
4216 parameters and qualifiers. */
4217 with_params = std::string (sym) + " ( int ) const";
4218 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4219 EXPECT (sym));
4220
4221 /* This should really find sym, but cp-name-parser.y doesn't
4222 know about lvalue/rvalue qualifiers yet. */
4223 with_params = std::string (sym) + " ( int ) &&";
4224 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4225 {});
4226 }
4227
e1ef7d7a
PA
4228 /* Check that the name matching algorithm for completion doesn't get
4229 confused with Latin1 'ÿ' / 0xff. */
4230 {
4231 static const char str[] = "\377";
4232 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4233 EXPECT ("\377", "\377\377123"));
4234 }
4235
4236 /* Check that the increment-last-char in the matching algorithm for
4237 completion doesn't match "t1_fund" when completing "t1_func". */
4238 {
4239 static const char str[] = "t1_func";
4240 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4241 EXPECT ("t1_func", "t1_func1"));
4242 }
4243
c62446b1
PA
4244 /* Check that completion mode works at each prefix of the expected
4245 symbol name. */
4246 {
4247 static const char str[] = "function(int)";
4248 size_t len = strlen (str);
4249 std::string lookup;
4250
4251 for (size_t i = 1; i < len; i++)
4252 {
4253 lookup.assign (str, i);
4254 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4255 EXPECT ("function"));
4256 }
4257 }
4258
4259 /* While "w" is a prefix of both components, the match function
4260 should still only be called once. */
4261 {
4262 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4263 EXPECT ("w1::w2"));
a20714ff
PA
4264 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4265 EXPECT ("w1::w2"));
c62446b1
PA
4266 }
4267
4268 /* Same, with a "complicated" symbol. */
4269 {
4270 static const char str[] = Z_SYM_NAME;
4271 size_t len = strlen (str);
4272 std::string lookup;
4273
4274 for (size_t i = 1; i < len; i++)
4275 {
4276 lookup.assign (str, i);
4277 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4278 EXPECT (Z_SYM_NAME));
4279 }
4280 }
4281
4282 /* In FULL mode, an incomplete symbol doesn't match. */
4283 {
4284 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4285 {});
4286 }
4287
4288 /* A complete symbol with parameters matches any overload, since the
4289 index has no overload info. */
4290 {
4291 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4292 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4293 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4294 EXPECT ("std::zfunction", "std::zfunction2"));
4295 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4296 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4297 }
4298
4299 /* Check that whitespace is ignored appropriately. A symbol with a
4300 template argument list. */
4301 {
4302 static const char expected[] = "ns::foo<int>";
4303 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4304 EXPECT (expected));
a20714ff
PA
4305 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4306 EXPECT (expected));
c62446b1
PA
4307 }
4308
4309 /* Check that whitespace is ignored appropriately. A symbol with a
4310 template argument list that includes a pointer. */
4311 {
4312 static const char expected[] = "ns::foo<char*>";
4313 /* Try both completion and non-completion modes. */
4314 static const bool completion_mode[2] = {false, true};
4315 for (size_t i = 0; i < 2; i++)
4316 {
4317 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4318 completion_mode[i], EXPECT (expected));
a20714ff
PA
4319 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4320 completion_mode[i], EXPECT (expected));
c62446b1
PA
4321
4322 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4323 completion_mode[i], EXPECT (expected));
a20714ff
PA
4324 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4325 completion_mode[i], EXPECT (expected));
c62446b1
PA
4326 }
4327 }
4328
4329 {
4330 /* Check method qualifiers are ignored. */
4331 static const char expected[] = "ns::foo<char*>";
4332 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4333 symbol_name_match_type::FULL, true, EXPECT (expected));
4334 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4335 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4336 CHECK_MATCH ("foo < char * > ( int ) const",
4337 symbol_name_match_type::WILD, true, EXPECT (expected));
4338 CHECK_MATCH ("foo < char * > ( int ) &&",
4339 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4340 }
4341
4342 /* Test lookup names that don't match anything. */
4343 {
a20714ff
PA
4344 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4345 {});
4346
c62446b1
PA
4347 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4348 {});
4349 }
4350
a20714ff
PA
4351 /* Some wild matching tests, exercising "(anonymous namespace)",
4352 which should not be confused with a parameter list. */
4353 {
4354 static const char *syms[] = {
4355 "A::B::C",
4356 "B::C",
4357 "C",
4358 "A :: B :: C ( int )",
4359 "B :: C ( int )",
4360 "C ( int )",
4361 };
4362
4363 for (const char *s : syms)
4364 {
4365 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4366 EXPECT ("(anonymous namespace)::A::B::C"));
4367 }
4368 }
4369
4370 {
4371 static const char expected[] = "ns2::tmpl<int>::foo2";
4372 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4373 EXPECT (expected));
4374 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4375 EXPECT (expected));
4376 }
4377
c62446b1
PA
4378 SELF_CHECK (!any_mismatch);
4379
4380#undef EXPECT
4381#undef CHECK_MATCH
4382}
4383
5c58de74
PA
4384static void
4385run_test ()
4386{
4387 test_mapped_index_find_name_component_bounds ();
4388 test_dw2_expand_symtabs_matching_symbol ();
4389}
4390
c62446b1
PA
4391}} // namespace selftests::dw2_expand_symtabs_matching
4392
4393#endif /* GDB_SELF_TEST */
4394
4b514bc8
JK
4395/* If FILE_MATCHER is NULL or if PER_CU has
4396 dwarf2_per_cu_quick_data::MARK set (see
4397 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4398 EXPANSION_NOTIFY on it. */
4399
df35e626 4400static bool
4b514bc8 4401dw2_expand_symtabs_matching_one
97a1449a
SM
4402 (dwarf2_per_cu_data *per_cu,
4403 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4404 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4405 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4406{
4407 if (file_matcher == NULL || per_cu->v.quick->mark)
4408 {
af758d11 4409 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4410
97a1449a
SM
4411 compunit_symtab *symtab
4412 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4413 gdb_assert (symtab != nullptr);
4b514bc8 4414
af758d11 4415 if (expansion_notify != NULL && symtab_was_null)
df35e626 4416 return expansion_notify (symtab);
4b514bc8 4417 }
df35e626 4418 return true;
4b514bc8
JK
4419}
4420
3f563c84
PA
4421/* Helper for dw2_expand_matching symtabs. Called on each symbol
4422 matched, to expand corresponding CUs that were marked. IDX is the
4423 index of the symbol name that matched. */
4424
df35e626 4425static bool
3f563c84 4426dw2_expand_marked_cus
976ca316 4427 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4428 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4429 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4430 block_search_flags search_flags,
3f563c84
PA
4431 search_domain kind)
4432{
42c2c694 4433 offset_type vec_len, vec_idx;
3f563c84 4434 bool global_seen = false;
976ca316 4435 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4436
42c2c694
TT
4437 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4438 vec_len = vec[0];
61920122
PA
4439 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4440 {
42c2c694 4441 offset_type cu_index_and_attrs = vec[vec_idx + 1];
61920122
PA
4442 /* This value is only valid for index versions >= 7. */
4443 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4444 gdb_index_symbol_kind symbol_kind =
4445 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4446 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4447 /* Only check the symbol attributes if they're present.
4448 Indices prior to version 7 don't record them,
4449 and indices >= 7 may elide them for certain symbols
4450 (gold does this). */
4451 int attrs_valid =
4452 (index.version >= 7
4453 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4454
4455 /* Work around gold/15646. */
f030440d
TV
4456 if (attrs_valid
4457 && !is_static
4458 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4459 {
f030440d 4460 if (global_seen)
61920122 4461 continue;
f030440d
TV
4462
4463 global_seen = true;
61920122 4464 }
3190f0c6 4465
61920122
PA
4466 /* Only check the symbol's kind if it has one. */
4467 if (attrs_valid)
4468 {
03a8ea51
TT
4469 if (is_static)
4470 {
4471 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4472 continue;
4473 }
4474 else
4475 {
4476 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4477 continue;
4478 }
4479
61920122 4480 switch (kind)
8943b874 4481 {
61920122
PA
4482 case VARIABLES_DOMAIN:
4483 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4484 continue;
4485 break;
4486 case FUNCTIONS_DOMAIN:
4487 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4488 continue;
61920122
PA
4489 break;
4490 case TYPES_DOMAIN:
4491 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4492 continue;
4493 break;
59c35742
AB
4494 case MODULES_DOMAIN:
4495 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4496 continue;
4497 break;
61920122
PA
4498 default:
4499 break;
8943b874 4500 }
61920122 4501 }
8943b874 4502
61920122 4503 /* Don't crash on bad data. */
976ca316
SM
4504 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4505 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4506 {
b98664d3 4507 complaint (_(".gdb_index entry has bad CU index"
976ca316 4508 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4509 continue;
4510 }
4511
976ca316 4512 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
df35e626
TT
4513 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4514 expansion_notify))
4515 return false;
61920122 4516 }
df35e626
TT
4517
4518 return true;
61920122
PA
4519}
4520
4b514bc8
JK
4521/* If FILE_MATCHER is non-NULL, set all the
4522 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4523 that match FILE_MATCHER. */
4524
61920122 4525static void
4b514bc8 4526dw_expand_symtabs_matching_file_matcher
976ca316 4527 (dwarf2_per_objfile *per_objfile,
ed2dc618 4528 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4529{
4b514bc8 4530 if (file_matcher == NULL)
61920122
PA
4531 return;
4532
4b514bc8
JK
4533 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4534 htab_eq_pointer,
4535 NULL, xcalloc, xfree));
4536 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4537 htab_eq_pointer,
4538 NULL, xcalloc, xfree));
61920122 4539
4b514bc8
JK
4540 /* The rule is CUs specify all the files, including those used by
4541 any TU, so there's no need to scan TUs here. */
61920122 4542
976ca316 4543 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4544 {
927aa2e7
JK
4545 QUIT;
4546
4547 per_cu->v.quick->mark = 0;
4548
4549 /* We only need to look at symtabs not already expanded. */
976ca316 4550 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4551 continue;
4552
976ca316 4553 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4554 if (file_data == NULL)
4555 continue;
4556
4557 if (htab_find (visited_not_found.get (), file_data) != NULL)
4558 continue;
4559 else if (htab_find (visited_found.get (), file_data) != NULL)
4560 {
4561 per_cu->v.quick->mark = 1;
4562 continue;
4563 }
4564
b76e467d 4565 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4566 {
4567 const char *this_real_name;
4568
4569 if (file_matcher (file_data->file_names[j], false))
4570 {
4571 per_cu->v.quick->mark = 1;
4572 break;
4573 }
4574
4575 /* Before we invoke realpath, which can get expensive when many
4576 files are involved, do a quick comparison of the basenames. */
4577 if (!basenames_may_differ
4578 && !file_matcher (lbasename (file_data->file_names[j]),
4579 true))
4580 continue;
4581
976ca316 4582 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4583 if (file_matcher (this_real_name, false))
4584 {
4585 per_cu->v.quick->mark = 1;
4586 break;
4587 }
4588 }
4589
b76e467d
SM
4590 void **slot = htab_find_slot (per_cu->v.quick->mark
4591 ? visited_found.get ()
4592 : visited_not_found.get (),
4593 file_data, INSERT);
927aa2e7
JK
4594 *slot = file_data;
4595 }
4596}
4597
df35e626 4598bool
af82f89d
TT
4599dwarf2_gdb_index::expand_symtabs_matching
4600 (struct objfile *objfile,
4601 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4602 const lookup_name_info *lookup_name,
4603 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4604 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 4605 block_search_flags search_flags,
3bfa51a7 4606 domain_enum domain,
af82f89d 4607 enum search_domain kind)
927aa2e7 4608{
976ca316 4609 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4610
4611 /* index_table is NULL if OBJF_READNOW. */
976ca316 4612 if (!per_objfile->per_bfd->index_table)
df35e626 4613 return true;
927aa2e7 4614
976ca316 4615 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4616
c1a66c06
TV
4617 if (symbol_matcher == NULL && lookup_name == NULL)
4618 {
976ca316 4619 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4620 {
4621 QUIT;
4622
df35e626
TT
4623 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4624 file_matcher,
4625 expansion_notify))
4626 return false;
c1a66c06 4627 }
df35e626 4628 return true;
c1a66c06
TV
4629 }
4630
976ca316 4631 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4632
df35e626
TT
4633 bool result
4634 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4635 symbol_matcher,
4636 [&] (offset_type idx)
927aa2e7 4637 {
df35e626 4638 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
03a8ea51 4639 expansion_notify, search_flags, kind))
df35e626 4640 return false;
3b00ef10 4641 return true;
976ca316 4642 }, per_objfile);
df35e626
TT
4643
4644 return result;
927aa2e7
JK
4645}
4646
4647/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4648 symtab. */
4649
4650static struct compunit_symtab *
4651recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4652 CORE_ADDR pc)
4653{
4654 int i;
4655
4656 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4657 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4658 return cust;
4659
4660 if (cust->includes == NULL)
4661 return NULL;
4662
4663 for (i = 0; cust->includes[i]; ++i)
4664 {
4665 struct compunit_symtab *s = cust->includes[i];
4666
4667 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4668 if (s != NULL)
4669 return s;
4670 }
4671
4672 return NULL;
4673}
4674
39298a5d
TT
4675struct compunit_symtab *
4676dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4677 (struct objfile *objfile,
4678 struct bound_minimal_symbol msymbol,
4679 CORE_ADDR pc,
4680 struct obj_section *section,
4681 int warn_if_readin)
927aa2e7
JK
4682{
4683 struct dwarf2_per_cu_data *data;
4684 struct compunit_symtab *result;
4685
efd7398e
TT
4686 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4687 if (per_objfile->per_bfd->index_addrmap == nullptr)
927aa2e7
JK
4688 return NULL;
4689
b3b3bada 4690 CORE_ADDR baseaddr = objfile->text_section_offset ();
efd7398e
TT
4691 data = ((struct dwarf2_per_cu_data *)
4692 addrmap_find (per_objfile->per_bfd->index_addrmap,
4693 pc - baseaddr));
927aa2e7
JK
4694 if (!data)
4695 return NULL;
4696
af758d11 4697 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4698 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4699 paddress (objfile->arch (), pc));
927aa2e7 4700
97a1449a
SM
4701 result = recursively_find_pc_sect_compunit_symtab
4702 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4703
927aa2e7
JK
4704 gdb_assert (result != NULL);
4705 return result;
4706}
4707
39298a5d 4708void
f4655dee
TT
4709dwarf2_base_index_functions::map_symbol_filenames
4710 (struct objfile *objfile,
4711 gdb::function_view<symbol_filename_ftype> fun,
4712 bool need_fullname)
927aa2e7 4713{
976ca316 4714 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4715
976ca316 4716 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4717 {
976ca316 4718 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4719
4720 htab_up visited (htab_create_alloc (10,
4721 htab_hash_pointer, htab_eq_pointer,
4722 NULL, xcalloc, xfree));
4723
4724 /* The rule is CUs specify all the files, including those used
4725 by any TU, so there's no need to scan TUs here. We can
4726 ignore file names coming from already-expanded CUs. */
4727
976ca316 4728 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4729 {
976ca316 4730 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4731 {
4732 void **slot = htab_find_slot (visited.get (),
4733 per_cu->v.quick->file_names,
4734 INSERT);
4735
4736 *slot = per_cu->v.quick->file_names;
4737 }
4738 }
4739
976ca316 4740 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4741 {
927aa2e7 4742 /* We only need to look at symtabs not already expanded. */
976ca316 4743 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4744 continue;
4745
ab432490 4746 quick_file_names *file_data
976ca316 4747 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4748 if (file_data == NULL)
4749 continue;
4750
b76e467d 4751 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4752 if (*slot)
4753 {
4754 /* Already visited. */
4755 continue;
4756 }
4757 *slot = file_data;
4758
4759 for (int j = 0; j < file_data->num_file_names; ++j)
4760 {
4761 const char *filename = file_data->file_names[j];
976ca316 4762 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4763 }
4764 }
4765 }
4766
976ca316 4767 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4768 {
4769 gdb::unique_xmalloc_ptr<char> this_real_name;
4770
4771 if (need_fullname)
4772 this_real_name = gdb_realpath (filename);
f4655dee 4773 fun (filename, this_real_name.get ());
927aa2e7
JK
4774 });
4775}
4776
39298a5d
TT
4777bool
4778dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
927aa2e7 4779{
fae2120b 4780 return true;
927aa2e7
JK
4781}
4782
927aa2e7
JK
4783/* DWARF-5 debug_names reader. */
4784
4785/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4786static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4787
4788/* A helper function that reads the .debug_names section in SECTION
4789 and fills in MAP. FILENAME is the name of the file containing the
4790 section; it is used for error reporting.
4791
4792 Returns true if all went well, false otherwise. */
4793
4794static bool
4795read_debug_names_from_section (struct objfile *objfile,
4796 const char *filename,
4797 struct dwarf2_section_info *section,
4798 mapped_debug_names &map)
4799{
96b79293 4800 if (section->empty ())
927aa2e7
JK
4801 return false;
4802
4803 /* Older elfutils strip versions could keep the section in the main
4804 executable while splitting it for the separate debug info file. */
96b79293 4805 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4806 return false;
4807
96b79293 4808 section->read (objfile);
927aa2e7 4809
08feed99 4810 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4811
4812 const gdb_byte *addr = section->buffer;
4813
96b79293 4814 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4815
4816 unsigned int bytes_read;
4817 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4818 addr += bytes_read;
4819
4820 map.dwarf5_is_dwarf64 = bytes_read != 4;
4821 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4822 if (bytes_read + length != section->size)
4823 {
4824 /* There may be multiple per-CU indices. */
4825 warning (_("Section .debug_names in %s length %s does not match "
4826 "section length %s, ignoring .debug_names."),
4827 filename, plongest (bytes_read + length),
4828 pulongest (section->size));
4829 return false;
4830 }
4831
4832 /* The version number. */
4833 uint16_t version = read_2_bytes (abfd, addr);
4834 addr += 2;
4835 if (version != 5)
4836 {
4837 warning (_("Section .debug_names in %s has unsupported version %d, "
4838 "ignoring .debug_names."),
4839 filename, version);
4840 return false;
4841 }
4842
4843 /* Padding. */
4844 uint16_t padding = read_2_bytes (abfd, addr);
4845 addr += 2;
4846 if (padding != 0)
4847 {
4848 warning (_("Section .debug_names in %s has unsupported padding %d, "
4849 "ignoring .debug_names."),
4850 filename, padding);
4851 return false;
4852 }
4853
4854 /* comp_unit_count - The number of CUs in the CU list. */
4855 map.cu_count = read_4_bytes (abfd, addr);
4856 addr += 4;
4857
4858 /* local_type_unit_count - The number of TUs in the local TU
4859 list. */
4860 map.tu_count = read_4_bytes (abfd, addr);
4861 addr += 4;
4862
4863 /* foreign_type_unit_count - The number of TUs in the foreign TU
4864 list. */
4865 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4866 addr += 4;
4867 if (foreign_tu_count != 0)
4868 {
4869 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4870 "ignoring .debug_names."),
4871 filename, static_cast<unsigned long> (foreign_tu_count));
4872 return false;
4873 }
4874
4875 /* bucket_count - The number of hash buckets in the hash lookup
4876 table. */
4877 map.bucket_count = read_4_bytes (abfd, addr);
4878 addr += 4;
4879
4880 /* name_count - The number of unique names in the index. */
4881 map.name_count = read_4_bytes (abfd, addr);
4882 addr += 4;
4883
4884 /* abbrev_table_size - The size in bytes of the abbreviations
4885 table. */
4886 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4887 addr += 4;
4888
4889 /* augmentation_string_size - The size in bytes of the augmentation
4890 string. This value is rounded up to a multiple of 4. */
4891 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4892 addr += 4;
4893 map.augmentation_is_gdb = ((augmentation_string_size
4894 == sizeof (dwarf5_augmentation))
4895 && memcmp (addr, dwarf5_augmentation,
4896 sizeof (dwarf5_augmentation)) == 0);
4897 augmentation_string_size += (-augmentation_string_size) & 3;
4898 addr += augmentation_string_size;
4899
4900 /* List of CUs */
4901 map.cu_table_reordered = addr;
4902 addr += map.cu_count * map.offset_size;
4903
4904 /* List of Local TUs */
4905 map.tu_table_reordered = addr;
4906 addr += map.tu_count * map.offset_size;
4907
4908 /* Hash Lookup Table */
4909 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4910 addr += map.bucket_count * 4;
4911 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4912 addr += map.name_count * 4;
4913
4914 /* Name Table */
4915 map.name_table_string_offs_reordered = addr;
4916 addr += map.name_count * map.offset_size;
4917 map.name_table_entry_offs_reordered = addr;
4918 addr += map.name_count * map.offset_size;
4919
4920 const gdb_byte *abbrev_table_start = addr;
4921 for (;;)
4922 {
927aa2e7
JK
4923 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4924 addr += bytes_read;
4925 if (index_num == 0)
4926 break;
4927
4928 const auto insertpair
4929 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4930 if (!insertpair.second)
4931 {
4932 warning (_("Section .debug_names in %s has duplicate index %s, "
4933 "ignoring .debug_names."),
4934 filename, pulongest (index_num));
4935 return false;
4936 }
4937 mapped_debug_names::index_val &indexval = insertpair.first->second;
4938 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4939 addr += bytes_read;
4940
4941 for (;;)
4942 {
4943 mapped_debug_names::index_val::attr attr;
4944 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4945 addr += bytes_read;
4946 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4947 addr += bytes_read;
4948 if (attr.form == DW_FORM_implicit_const)
4949 {
4950 attr.implicit_const = read_signed_leb128 (abfd, addr,
4951 &bytes_read);
4952 addr += bytes_read;
4953 }
4954 if (attr.dw_idx == 0 && attr.form == 0)
4955 break;
4956 indexval.attr_vec.push_back (std::move (attr));
4957 }
4958 }
4959 if (addr != abbrev_table_start + abbrev_table_size)
4960 {
4961 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4962 "of size %s vs. written as %u, ignoring .debug_names."),
4963 filename, plongest (addr - abbrev_table_start),
4964 abbrev_table_size);
927aa2e7
JK
4965 return false;
4966 }
4967 map.entry_pool = addr;
4968
4969 return true;
4970}
4971
4972/* A helper for create_cus_from_debug_names that handles the MAP's CU
4973 list. */
4974
4975static void
168c9250 4976create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
4977 const mapped_debug_names &map,
4978 dwarf2_section_info &section,
b76e467d 4979 bool is_dwz)
927aa2e7 4980{
3ee6bb11
TV
4981 if (!map.augmentation_is_gdb)
4982 {
fc9a13fb
TV
4983 for (uint32_t i = 0; i < map.cu_count; ++i)
4984 {
4985 sect_offset sect_off
4986 = (sect_offset) (extract_unsigned_integer
4987 (map.cu_table_reordered + i * map.offset_size,
4988 map.offset_size,
4989 map.dwarf5_byte_order));
4990 /* We don't know the length of the CU, because the CU list in a
4991 .debug_names index can be incomplete, so we can't use the start
4992 of the next CU as end of this CU. We create the CUs here with
4993 length 0, and in cutu_reader::cutu_reader we'll fill in the
4994 actual length. */
4995 dwarf2_per_cu_data *per_cu
4996 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4997 sect_off, 0);
4998 per_bfd->all_comp_units.push_back (per_cu);
4999 }
d3b54e63 5000 return;
3ee6bb11
TV
5001 }
5002
927aa2e7
JK
5003 sect_offset sect_off_prev;
5004 for (uint32_t i = 0; i <= map.cu_count; ++i)
5005 {
5006 sect_offset sect_off_next;
5007 if (i < map.cu_count)
5008 {
5009 sect_off_next
5010 = (sect_offset) (extract_unsigned_integer
5011 (map.cu_table_reordered + i * map.offset_size,
5012 map.offset_size,
5013 map.dwarf5_byte_order));
5014 }
5015 else
5016 sect_off_next = (sect_offset) section.size;
5017 if (i >= 1)
5018 {
5019 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5020 dwarf2_per_cu_data *per_cu
168c9250 5021 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5022 sect_off_prev, length);
168c9250 5023 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5024 }
5025 sect_off_prev = sect_off_next;
5026 }
5027}
5028
5029/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5030 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5031
5032static void
168c9250 5033create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5034 const mapped_debug_names &map,
5035 const mapped_debug_names &dwz_map)
5036{
168c9250
SM
5037 gdb_assert (per_bfd->all_comp_units.empty ());
5038 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5039
168c9250 5040 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5041 false /* is_dwz */);
927aa2e7
JK
5042
5043 if (dwz_map.cu_count == 0)
5044 return;
5045
168c9250
SM
5046 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5047 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5048 true /* is_dwz */);
927aa2e7
JK
5049}
5050
5051/* Read .debug_names. If everything went ok, initialize the "quick"
5052 elements of all the CUs and return true. Otherwise, return false. */
5053
5054static bool
976ca316 5055dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5056{
fcf23d5b
SM
5057 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5058 mapped_debug_names dwz_map;
976ca316 5059 struct objfile *objfile = per_objfile->objfile;
f8c41851 5060 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5061
5062 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
bab287cd 5063 &per_bfd->debug_names, *map))
927aa2e7
JK
5064 return false;
5065
5066 /* Don't use the index if it's empty. */
22ca247e 5067 if (map->name_count == 0)
927aa2e7
JK
5068 return false;
5069
5070 /* If there is a .dwz file, read it so we can get its CU list as
5071 well. */
f8c41851 5072 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5073 if (dwz != NULL)
5074 {
5075 if (!read_debug_names_from_section (objfile,
00f93c44 5076 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5077 &dwz->debug_names, dwz_map))
5078 {
5079 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5080 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5081 return false;
5082 }
5083 }
5084
f8c41851 5085 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5086
22ca247e 5087 if (map->tu_count != 0)
927aa2e7
JK
5088 {
5089 /* We can only handle a single .debug_types when we have an
5090 index. */
f8c41851 5091 if (per_bfd->types.size () != 1)
927aa2e7
JK
5092 return false;
5093
f8c41851 5094 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5095
5096 create_signatured_type_table_from_debug_names
f8c41851 5097 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5098 }
5099
f8c41851 5100 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5101
f8c41851
SM
5102 per_bfd->debug_names_table = std::move (map);
5103 per_bfd->using_index = 1;
5104 per_bfd->quick_file_names_table =
bab287cd 5105 create_quick_file_names_table (per_bfd->all_comp_units.size ());
927aa2e7
JK
5106
5107 return true;
5108}
5109
927aa2e7
JK
5110/* Type used to manage iterating over all CUs looking for a symbol for
5111 .debug_names. */
5112
5113class dw2_debug_names_iterator
5114{
5115public:
927aa2e7 5116 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5117 block_search_flags block_index,
2b79f376 5118 domain_enum domain,
fcf23d5b 5119 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5120 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5121 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5122 m_per_objfile (per_objfile)
927aa2e7
JK
5123 {}
5124
5125 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5126 search_domain search, uint32_t namei,
3bfa51a7
TT
5127 dwarf2_per_objfile *per_objfile,
5128 domain_enum domain = UNDEF_DOMAIN)
927aa2e7 5129 : m_map (map),
3bfa51a7 5130 m_domain (domain),
927aa2e7 5131 m_search (search),
fcf23d5b
SM
5132 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5133 m_per_objfile (per_objfile)
927aa2e7
JK
5134 {}
5135
3b00ef10 5136 dw2_debug_names_iterator (const mapped_debug_names &map,
03a8ea51 5137 block_search_flags block_index, domain_enum domain,
fcf23d5b 5138 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5139 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5140 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5141 m_per_objfile (per_objfile)
3b00ef10
TT
5142 {}
5143
927aa2e7
JK
5144 /* Return the next matching CU or NULL if there are no more. */
5145 dwarf2_per_cu_data *next ();
5146
5147private:
5148 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5149 const char *name,
5150 dwarf2_per_objfile *per_objfile);
927aa2e7 5151 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5152 uint32_t namei,
5153 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5154
5155 /* The internalized form of .debug_names. */
5156 const mapped_debug_names &m_map;
5157
03a8ea51
TT
5158 /* Restrict the search to these blocks. */
5159 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
5160 | SEARCH_STATIC_BLOCK);
927aa2e7
JK
5161
5162 /* The kind of symbol we're looking for. */
5163 const domain_enum m_domain = UNDEF_DOMAIN;
5164 const search_domain m_search = ALL_DOMAIN;
5165
5166 /* The list of CUs from the index entry of the symbol, or NULL if
5167 not found. */
5168 const gdb_byte *m_addr;
fcf23d5b
SM
5169
5170 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5171};
5172
5173const char *
fcf23d5b 5174mapped_debug_names::namei_to_name
976ca316 5175 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5176{
5177 const ULONGEST namei_string_offs
5178 = extract_unsigned_integer ((name_table_string_offs_reordered
5179 + namei * offset_size),
5180 offset_size,
5181 dwarf5_byte_order);
976ca316 5182 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5183}
5184
5185/* Find a slot in .debug_names for the object named NAME. If NAME is
5186 found, return pointer to its pool data. If NAME cannot be found,
5187 return NULL. */
5188
5189const gdb_byte *
5190dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5191 (const mapped_debug_names &map, const char *name,
5192 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5193{
5194 int (*cmp) (const char *, const char *);
5195
54ee4252 5196 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5197 if (current_language->la_language == language_cplus
5198 || current_language->la_language == language_fortran
5199 || current_language->la_language == language_d)
5200 {
5201 /* NAME is already canonical. Drop any qualifiers as
5202 .debug_names does not contain any. */
5203
5204 if (strchr (name, '(') != NULL)
5205 {
54ee4252 5206 without_params = cp_remove_params (name);
927aa2e7 5207 if (without_params != NULL)
54ee4252 5208 name = without_params.get ();
927aa2e7
JK
5209 }
5210 }
5211
5212 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5213
5214 const uint32_t full_hash = dwarf5_djb_hash (name);
5215 uint32_t namei
5216 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5217 (map.bucket_table_reordered
5218 + (full_hash % map.bucket_count)), 4,
5219 map.dwarf5_byte_order);
5220 if (namei == 0)
5221 return NULL;
5222 --namei;
5223 if (namei >= map.name_count)
5224 {
b98664d3 5225 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5226 "[in module %s]"),
5227 namei, map.name_count,
fcf23d5b 5228 objfile_name (per_objfile->objfile));
927aa2e7
JK
5229 return NULL;
5230 }
5231
5232 for (;;)
5233 {
5234 const uint32_t namei_full_hash
5235 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5236 (map.hash_table_reordered + namei), 4,
5237 map.dwarf5_byte_order);
5238 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5239 return NULL;
5240
5241 if (full_hash == namei_full_hash)
5242 {
fcf23d5b 5243 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5244
5245#if 0 /* An expensive sanity check. */
5246 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5247 {
b98664d3 5248 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5249 "[in module %s]"),
5250 namei, objfile_name (dwarf2_per_objfile->objfile));
5251 return NULL;
5252 }
5253#endif
5254
5255 if (cmp (namei_string, name) == 0)
5256 {
5257 const ULONGEST namei_entry_offs
5258 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5259 + namei * map.offset_size),
5260 map.offset_size, map.dwarf5_byte_order);
5261 return map.entry_pool + namei_entry_offs;
5262 }
5263 }
5264
5265 ++namei;
5266 if (namei >= map.name_count)
5267 return NULL;
5268 }
5269}
5270
5271const gdb_byte *
5272dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5273 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5274{
5275 if (namei >= map.name_count)
5276 {
b98664d3 5277 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5278 "[in module %s]"),
5279 namei, map.name_count,
fcf23d5b 5280 objfile_name (per_objfile->objfile));
927aa2e7
JK
5281 return NULL;
5282 }
5283
5284 const ULONGEST namei_entry_offs
5285 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5286 + namei * map.offset_size),
5287 map.offset_size, map.dwarf5_byte_order);
5288 return map.entry_pool + namei_entry_offs;
5289}
5290
5291/* See dw2_debug_names_iterator. */
5292
5293dwarf2_per_cu_data *
5294dw2_debug_names_iterator::next ()
5295{
5296 if (m_addr == NULL)
5297 return NULL;
5298
fcf23d5b
SM
5299 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5300 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5301 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5302
5303 again:
5304
5305 unsigned int bytes_read;
5306 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5307 m_addr += bytes_read;
5308 if (abbrev == 0)
5309 return NULL;
5310
5311 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5312 if (indexval_it == m_map.abbrev_map.cend ())
5313 {
b98664d3 5314 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5315 "[in module %s]"),
ed2dc618 5316 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5317 return NULL;
5318 }
5319 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5320 enum class symbol_linkage {
5321 unknown,
5322 static_,
5323 extern_,
23c13d42 5324 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5325 dwarf2_per_cu_data *per_cu = NULL;
5326 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5327 {
5328 ULONGEST ull;
5329 switch (attr.form)
5330 {
5331 case DW_FORM_implicit_const:
5332 ull = attr.implicit_const;
5333 break;
5334 case DW_FORM_flag_present:
5335 ull = 1;
5336 break;
5337 case DW_FORM_udata:
5338 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5339 m_addr += bytes_read;
5340 break;
6dc55ce9 5341 case DW_FORM_ref4:
5342 ull = read_4_bytes (abfd, m_addr);
5343 m_addr += 4;
5344 break;
5345 case DW_FORM_ref8:
5346 ull = read_8_bytes (abfd, m_addr);
5347 m_addr += 8;
5348 break;
5349 case DW_FORM_ref_sig8:
5350 ull = read_8_bytes (abfd, m_addr);
5351 m_addr += 8;
5352 break;
927aa2e7 5353 default:
b98664d3 5354 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5355 dwarf_form_name (attr.form),
ed2dc618 5356 objfile_name (objfile));
927aa2e7
JK
5357 return NULL;
5358 }
5359 switch (attr.dw_idx)
5360 {
5361 case DW_IDX_compile_unit:
5362 /* Don't crash on bad data. */
bab287cd 5363 if (ull >= per_bfd->all_comp_units.size ())
927aa2e7 5364 {
b98664d3 5365 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5366 " [in module %s]"),
5367 pulongest (ull),
fcf23d5b 5368 objfile_name (objfile));
927aa2e7
JK
5369 continue;
5370 }
fcf23d5b 5371 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5372 break;
8af5c486
JK
5373 case DW_IDX_type_unit:
5374 /* Don't crash on bad data. */
fcf23d5b 5375 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5376 {
b98664d3 5377 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5378 " [in module %s]"),
5379 pulongest (ull),
fcf23d5b 5380 objfile_name (objfile));
8af5c486
JK
5381 continue;
5382 }
fcf23d5b 5383 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5384 break;
6dc55ce9 5385 case DW_IDX_die_offset:
5386 /* In a per-CU index (as opposed to a per-module index), index
5387 entries without CU attribute implicitly refer to the single CU. */
5388 if (per_cu == NULL)
fcf23d5b 5389 per_cu = per_bfd->get_cu (0);
6dc55ce9 5390 break;
927aa2e7
JK
5391 case DW_IDX_GNU_internal:
5392 if (!m_map.augmentation_is_gdb)
5393 break;
23c13d42 5394 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5395 break;
5396 case DW_IDX_GNU_external:
5397 if (!m_map.augmentation_is_gdb)
5398 break;
23c13d42 5399 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5400 break;
5401 }
5402 }
5403
5404 /* Skip if already read in. */
fcf23d5b 5405 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5406 goto again;
5407
5408 /* Check static vs global. */
03a8ea51 5409 if (symbol_linkage_ != symbol_linkage::unknown)
927aa2e7 5410 {
03a8ea51
TT
5411 if (symbol_linkage_ == symbol_linkage::static_)
5412 {
5413 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5414 goto again;
5415 }
5416 else
5417 {
5418 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5419 goto again;
5420 }
927aa2e7
JK
5421 }
5422
5423 /* Match dw2_symtab_iter_next, symbol_kind
5424 and debug_names::psymbol_tag. */
5425 switch (m_domain)
5426 {
5427 case VAR_DOMAIN:
5428 switch (indexval.dwarf_tag)
5429 {
5430 case DW_TAG_variable:
5431 case DW_TAG_subprogram:
5432 /* Some types are also in VAR_DOMAIN. */
5433 case DW_TAG_typedef:
5434 case DW_TAG_structure_type:
5435 break;
5436 default:
5437 goto again;
5438 }
5439 break;
5440 case STRUCT_DOMAIN:
5441 switch (indexval.dwarf_tag)
5442 {
5443 case DW_TAG_typedef:
5444 case DW_TAG_structure_type:
5445 break;
5446 default:
5447 goto again;
5448 }
5449 break;
5450 case LABEL_DOMAIN:
5451 switch (indexval.dwarf_tag)
5452 {
5453 case 0:
5454 case DW_TAG_variable:
5455 break;
5456 default:
5457 goto again;
5458 }
5459 break;
59c35742
AB
5460 case MODULE_DOMAIN:
5461 switch (indexval.dwarf_tag)
5462 {
5463 case DW_TAG_module:
5464 break;
5465 default:
5466 goto again;
5467 }
5468 break;
927aa2e7
JK
5469 default:
5470 break;
5471 }
5472
5473 /* Match dw2_expand_symtabs_matching, symbol_kind and
5474 debug_names::psymbol_tag. */
5475 switch (m_search)
4b514bc8 5476 {
927aa2e7
JK
5477 case VARIABLES_DOMAIN:
5478 switch (indexval.dwarf_tag)
4b514bc8 5479 {
927aa2e7
JK
5480 case DW_TAG_variable:
5481 break;
5482 default:
5483 goto again;
4b514bc8 5484 }
927aa2e7
JK
5485 break;
5486 case FUNCTIONS_DOMAIN:
5487 switch (indexval.dwarf_tag)
4b514bc8 5488 {
927aa2e7
JK
5489 case DW_TAG_subprogram:
5490 break;
5491 default:
5492 goto again;
4b514bc8 5493 }
927aa2e7
JK
5494 break;
5495 case TYPES_DOMAIN:
5496 switch (indexval.dwarf_tag)
5497 {
5498 case DW_TAG_typedef:
5499 case DW_TAG_structure_type:
5500 break;
5501 default:
5502 goto again;
5503 }
5504 break;
59c35742
AB
5505 case MODULES_DOMAIN:
5506 switch (indexval.dwarf_tag)
5507 {
5508 case DW_TAG_module:
5509 break;
5510 default:
5511 goto again;
5512 }
927aa2e7
JK
5513 default:
5514 break;
4b514bc8 5515 }
927aa2e7
JK
5516
5517 return per_cu;
4b514bc8 5518}
61920122 5519
927aa2e7
JK
5520/* This dumps minimal information about .debug_names. It is called
5521 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5522 uses this to verify that .debug_names has been loaded. */
9291a0cd 5523
39298a5d
TT
5524void
5525dwarf2_debug_names_index::dump (struct objfile *objfile)
927aa2e7 5526{
976ca316 5527 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5528
976ca316 5529 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5530 printf_filtered (".debug_names:");
976ca316 5531 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5532 printf_filtered (" exists\n");
5533 else
5534 printf_filtered (" faked for \"readnow\"\n");
5535 printf_filtered ("\n");
9291a0cd
TT
5536}
5537
39298a5d 5538void
0b7b2c2a 5539dwarf2_debug_names_index::expand_matching_symbols
3b00ef10
TT
5540 (struct objfile *objfile,
5541 const lookup_name_info &name, domain_enum domain,
5542 int global,
3b00ef10
TT
5543 symbol_compare_ftype *ordered_compare)
5544{
976ca316 5545 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5546
5547 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5548 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5549 return;
5550
976ca316 5551 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
03a8ea51
TT
5552 const block_search_flags block_flags
5553 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
3b00ef10
TT
5554
5555 const char *match_name = name.ada ().lookup_name ().c_str ();
5556 auto matcher = [&] (const char *symname)
5557 {
5558 if (ordered_compare == nullptr)
5559 return true;
5560 return ordered_compare (symname, match_name) == 0;
5561 };
5562
b64f703b 5563 dw2_expand_symtabs_matching_symbol (map, name, matcher,
3b00ef10
TT
5564 [&] (offset_type namei)
5565 {
5566 /* The name was matched, now expand corresponding CUs that were
5567 marked. */
03a8ea51 5568 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
976ca316 5569 per_objfile);
3b00ef10
TT
5570
5571 struct dwarf2_per_cu_data *per_cu;
5572 while ((per_cu = iter.next ()) != NULL)
976ca316 5573 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5574 nullptr);
3b00ef10 5575 return true;
976ca316 5576 }, per_objfile);
3b00ef10
TT
5577}
5578
df35e626 5579bool
39298a5d 5580dwarf2_debug_names_index::expand_symtabs_matching
927aa2e7
JK
5581 (struct objfile *objfile,
5582 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5583 const lookup_name_info *lookup_name,
927aa2e7
JK
5584 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5585 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
03a8ea51 5586 block_search_flags search_flags,
3bfa51a7 5587 domain_enum domain,
927aa2e7
JK
5588 enum search_domain kind)
5589{
976ca316 5590 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5591
927aa2e7 5592 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5593 if (!per_objfile->per_bfd->debug_names_table)
df35e626 5594 return true;
9291a0cd 5595
976ca316 5596 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5597
c1a66c06
TV
5598 if (symbol_matcher == NULL && lookup_name == NULL)
5599 {
976ca316 5600 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5601 {
5602 QUIT;
5603
df35e626
TT
5604 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5605 file_matcher,
5606 expansion_notify))
5607 return false;
c1a66c06 5608 }
df35e626 5609 return true;
c1a66c06
TV
5610 }
5611
976ca316 5612 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5613
df35e626
TT
5614 bool result
5615 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5616 symbol_matcher,
5617 [&] (offset_type namei)
927aa2e7 5618 {
927aa2e7
JK
5619 /* The name was matched, now expand corresponding CUs that were
5620 marked. */
3bfa51a7 5621 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
bbf2f4df 5622
927aa2e7
JK
5623 struct dwarf2_per_cu_data *per_cu;
5624 while ((per_cu = iter.next ()) != NULL)
df35e626
TT
5625 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5626 file_matcher,
5627 expansion_notify))
5628 return false;
3b00ef10 5629 return true;
976ca316 5630 }, per_objfile);
df35e626
TT
5631
5632 return result;
9291a0cd
TT
5633}
5634
4485a1c1 5635/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5636 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5637
5638template <typename T>
5639static gdb::array_view<const gdb_byte>
5640get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5641{
5642 dwarf2_section_info *section = &section_owner->gdb_index;
5643
96b79293 5644 if (section->empty ())
4485a1c1
SM
5645 return {};
5646
5647 /* Older elfutils strip versions could keep the section in the main
5648 executable while splitting it for the separate debug info file. */
96b79293 5649 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5650 return {};
5651
96b79293 5652 section->read (obj);
4485a1c1 5653
8bebfcda
PA
5654 /* dwarf2_section_info::size is a bfd_size_type, while
5655 gdb::array_view works with size_t. On 32-bit hosts, with
5656 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5657 is 32-bit. So we need an explicit narrowing conversion here.
5658 This is fine, because it's impossible to allocate or mmap an
5659 array/buffer larger than what size_t can represent. */
5660 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5661}
5662
87d6a7aa
SM
5663/* Lookup the index cache for the contents of the index associated to
5664 DWARF2_OBJ. */
5665
5666static gdb::array_view<const gdb_byte>
5989a64e 5667get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5668{
5669 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5670 if (build_id == nullptr)
5671 return {};
5672
5673 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5674 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5675}
5676
5677/* Same as the above, but for DWZ. */
5678
5679static gdb::array_view<const gdb_byte>
5680get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5681{
5682 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5683 if (build_id == nullptr)
5684 return {};
5685
5686 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5687}
5688
edc02ceb 5689/* See dwarf2/public.h. */
9291a0cd 5690
edc02ceb
TT
5691void
5692dwarf2_initialize_objfile (struct objfile *objfile)
9291a0cd 5693{
976ca316
SM
5694 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5695 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5696
850ed749
SM
5697 dwarf_read_debug_printf ("called");
5698
9291a0cd
TT
5699 /* If we're about to read full symbols, don't bother with the
5700 indices. In this case we also don't care if some other debug
5701 format is making psymtabs, because they are all about to be
5702 expanded anyway. */
5703 if ((objfile->flags & OBJF_READNOW))
5704 {
850ed749
SM
5705 dwarf_read_debug_printf ("readnow requested");
5706
17ee85fc
TT
5707 /* When using READNOW, the using_index flag (set below) indicates that
5708 PER_BFD was already initialized, when we loaded some other objfile. */
5709 if (per_bfd->using_index)
5710 {
850ed749 5711 dwarf_read_debug_printf ("using_index already set");
976ca316 5712 per_objfile->resize_symtabs ();
edc02ceb
TT
5713 objfile->qf.push_front (make_dwarf_gdb_index ());
5714 return;
17ee85fc
TT
5715 }
5716
5717 per_bfd->using_index = 1;
976ca316
SM
5718 create_all_comp_units (per_objfile);
5719 create_all_type_units (per_objfile);
17ee85fc
TT
5720 per_bfd->quick_file_names_table
5721 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5722 per_objfile->resize_symtabs ();
9291a0cd 5723
17ee85fc
TT
5724 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5725 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 5726 {
17ee85fc 5727 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 5728
17ee85fc 5729 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5730 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5731 }
5732
edc02ceb
TT
5733 /* Arrange for gdb to see the "quick" functions. However, these
5734 functions will be no-ops because we will have expanded all
5735 symtabs. */
5736 objfile->qf.push_front (make_dwarf_gdb_index ());
5737 return;
9291a0cd
TT
5738 }
5739
17ee85fc
TT
5740 /* Was a debug names index already read when we processed an objfile sharing
5741 PER_BFD? */
5742 if (per_bfd->debug_names_table != nullptr)
5743 {
850ed749 5744 dwarf_read_debug_printf ("re-using shared debug names table");
976ca316 5745 per_objfile->resize_symtabs ();
edc02ceb
TT
5746 objfile->qf.push_front (make_dwarf_debug_names ());
5747 return;
17ee85fc
TT
5748 }
5749
5750 /* Was a GDB index already read when we processed an objfile sharing
5751 PER_BFD? */
5752 if (per_bfd->index_table != nullptr)
5753 {
850ed749 5754 dwarf_read_debug_printf ("re-using shared index table");
976ca316 5755 per_objfile->resize_symtabs ();
edc02ceb
TT
5756 objfile->qf.push_front (make_dwarf_gdb_index ());
5757 return;
17ee85fc
TT
5758 }
5759
efb763a5
SM
5760 /* There might already be partial symtabs built for this BFD. This happens
5761 when loading the same binary twice with the index-cache enabled. If so,
5762 don't try to read an index. The objfile / per_objfile initialization will
5763 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5764 code path. */
5765 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
5766 {
5767 dwarf_read_debug_printf ("re-using shared partial symtabs");
edc02ceb
TT
5768 objfile->qf.push_front (make_lazy_dwarf_reader ());
5769 return;
850ed749 5770 }
efb763a5 5771
976ca316 5772 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 5773 {
850ed749 5774 dwarf_read_debug_printf ("found debug names");
976ca316 5775 per_objfile->resize_symtabs ();
edc02ceb
TT
5776 objfile->qf.push_front (make_dwarf_debug_names ());
5777 return;
3c0aa29a 5778 }
927aa2e7 5779
976ca316 5780 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 5781 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5782 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 5783 {
850ed749 5784 dwarf_read_debug_printf ("found gdb index from file");
976ca316 5785 per_objfile->resize_symtabs ();
edc02ceb
TT
5786 objfile->qf.push_front (make_dwarf_gdb_index ());
5787 return;
3c0aa29a 5788 }
9291a0cd 5789
87d6a7aa 5790 /* ... otherwise, try to find the index in the index cache. */
976ca316 5791 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
5792 get_gdb_index_contents_from_cache,
5793 get_gdb_index_contents_from_cache_dwz))
5794 {
850ed749 5795 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa 5796 global_index_cache.hit ();
976ca316 5797 per_objfile->resize_symtabs ();
edc02ceb
TT
5798 objfile->qf.push_front (make_dwarf_gdb_index ());
5799 return;
87d6a7aa
SM
5800 }
5801
5802 global_index_cache.miss ();
edc02ceb 5803 objfile->qf.push_front (make_lazy_dwarf_reader ());
9291a0cd
TT
5804}
5805
5806\f
5807
dce234bc
PP
5808/* Build a partial symbol table. */
5809
5810void
eb36a3eb 5811dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
dce234bc 5812{
976ca316
SM
5813 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5814 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
5815
5816 if (per_bfd->partial_symtabs != nullptr)
5817 {
5818 /* Partial symbols were already read, so now we can simply
5819 attach them. */
eb36a3eb
TT
5820 if (psf == nullptr)
5821 {
5822 psf = new psymbol_functions (per_bfd->partial_symtabs);
5823 objfile->qf.emplace_front (psf);
5824 }
5825 else
5826 psf->set_partial_symtabs (per_bfd->partial_symtabs);
976ca316 5827 per_objfile->resize_symtabs ();
17ee85fc
TT
5828 return;
5829 }
c9bf0622 5830
eb36a3eb
TT
5831 if (psf == nullptr)
5832 {
5833 psf = new psymbol_functions;
5834 objfile->qf.emplace_front (psf);
5835 }
5836 const std::shared_ptr<psymtab_storage> &partial_symtabs
5837 = psf->get_partial_symtabs ();
5838
51962708
TT
5839 /* Set the local reference to partial symtabs, so that we don't try
5840 to read them again if reading another objfile with the same BFD.
5841 If we can't in fact share, this won't make a difference anyway as
5842 the dwarf2_per_bfd object won't be shared. */
eb36a3eb 5843 per_bfd->partial_symtabs = partial_symtabs;
51962708 5844
a70b8144 5845 try
c9bf0622
TT
5846 {
5847 /* This isn't really ideal: all the data we allocate on the
5848 objfile's obstack is still uselessly kept around. However,
5849 freeing it seems unsafe. */
eb36a3eb 5850 psymtab_discarder psymtabs (partial_symtabs.get ());
976ca316 5851 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 5852 psymtabs.keep ();
87d6a7aa 5853
976ca316 5854 per_objfile->resize_symtabs ();
af758d11 5855
87d6a7aa 5856 /* (maybe) store an index in the cache. */
976ca316 5857 global_index_cache.store (per_objfile);
c9bf0622 5858 }
230d2906 5859 catch (const gdb_exception_error &except)
492d29ea
PA
5860 {
5861 exception_print (gdb_stderr, except);
5862 }
c906108c 5863}
c906108c 5864
3b80fe9b
DE
5865/* Find the base address of the compilation unit for range lists and
5866 location lists. It will normally be specified by DW_AT_low_pc.
5867 In DWARF-3 draft 4, the base address could be overridden by
5868 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5869 compilation units with discontinuous ranges. */
5870
5871static void
5872dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5873{
5874 struct attribute *attr;
5875
2b24b6e4 5876 cu->base_address.reset ();
3b80fe9b
DE
5877
5878 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5879 if (attr != nullptr)
95f982e5 5880 cu->base_address = attr->as_address ();
3b80fe9b
DE
5881 else
5882 {
5883 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5884 if (attr != nullptr)
95f982e5 5885 cu->base_address = attr->as_address ();
3b80fe9b
DE
5886 }
5887}
5888
36586728
TT
5889/* Helper function that returns the proper abbrev section for
5890 THIS_CU. */
5891
5892static struct dwarf2_section_info *
5893get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5894{
5895 struct dwarf2_section_info *abbrev;
c3699833 5896 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
5897
5898 if (this_cu->is_dwz)
a7308ce0 5899 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 5900 else
c3699833 5901 abbrev = &per_bfd->abbrev;
36586728
TT
5902
5903 return abbrev;
5904}
5905
f4dc4d17
DE
5906/* Fetch the abbreviation table offset from a comp or type unit header. */
5907
5908static sect_offset
976ca316 5909read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 5910 struct dwarf2_section_info *section,
9c541725 5911 sect_offset sect_off)
f4dc4d17 5912{
96b79293 5913 bfd *abfd = section->get_bfd_owner ();
d521ce57 5914 const gdb_byte *info_ptr;
ac298888 5915 unsigned int initial_length_size, offset_size;
43988095 5916 uint16_t version;
f4dc4d17 5917
976ca316 5918 section->read (per_objfile->objfile);
9c541725 5919 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5920 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5921 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5922 info_ptr += initial_length_size;
5923
5924 version = read_2_bytes (abfd, info_ptr);
5925 info_ptr += 2;
5926 if (version >= 5)
5927 {
5928 /* Skip unit type and address size. */
5929 info_ptr += 2;
5930 }
5931
24aa364d 5932 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5933}
5934
b83470bf
TT
5935/* A partial symtab that is used only for include files. */
5936struct dwarf2_include_psymtab : public partial_symtab
5937{
7b249e47
TT
5938 dwarf2_include_psymtab (const char *filename,
5939 psymtab_storage *partial_symtabs,
0072c873
SM
5940 objfile_per_bfd_storage *objfile_per_bfd)
5941 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
b83470bf
TT
5942 {
5943 }
5944
5945 void read_symtab (struct objfile *objfile) override
5946 {
194d088f
TV
5947 /* It's an include file, no symbols to read for it.
5948 Everything is in the includer symtab. */
5949
5950 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5951 expansion of the includer psymtab. We use the dependencies[0] field to
5952 model the includer. But if we go the regular route of calling
5953 expand_psymtab here, and having expand_psymtab call expand_dependencies
5954 to expand the includer, we'll only use expand_psymtab on the includer
5955 (making it a non-toplevel psymtab), while if we expand the includer via
5956 another path, we'll use read_symtab (making it a toplevel psymtab).
5957 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5958 psymtab, and trigger read_symtab on the includer here directly. */
5959 includer ()->read_symtab (objfile);
b83470bf
TT
5960 }
5961
5962 void expand_psymtab (struct objfile *objfile) override
5963 {
194d088f
TV
5964 /* This is not called by read_symtab, and should not be called by any
5965 expand_dependencies. */
5966 gdb_assert (false);
b83470bf
TT
5967 }
5968
5717c425 5969 bool readin_p (struct objfile *objfile) const override
b83470bf 5970 {
5717c425 5971 return includer ()->readin_p (objfile);
b83470bf
TT
5972 }
5973
5717c425 5974 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
5975 {
5976 return nullptr;
5977 }
5978
5979private:
194d088f
TV
5980 partial_symtab *includer () const
5981 {
5982 /* An include psymtab has exactly one dependency: the psymtab that
5983 includes it. */
5984 gdb_assert (this->number_of_dependencies == 1);
5985 return this->dependencies[0];
5986 }
b83470bf
TT
5987};
5988
aaa75496
JB
5989/* Allocate a new partial symtab for file named NAME and mark this new
5990 partial symtab as being an include of PST. */
5991
5992static void
84685904 5993dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
7b249e47
TT
5994 const char *name,
5995 dwarf2_psymtab *pst,
5996 psymtab_storage *partial_symtabs,
0072c873 5997 objfile_per_bfd_storage *objfile_per_bfd)
aaa75496 5998{
7b249e47 5999 dwarf2_include_psymtab *subpst
0072c873 6000 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
aaa75496 6001
fbd9ab74 6002 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6003 subpst->dirname = pst->dirname;
fbd9ab74 6004
84685904 6005 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6006 subpst->dependencies[0] = pst;
6007 subpst->number_of_dependencies = 1;
aaa75496
JB
6008}
6009
6010/* Read the Line Number Program data and extract the list of files
6011 included by the source file represented by PST. Build an include
d85a05f0 6012 partial symtab for each of these included files. */
aaa75496
JB
6013
6014static void
6015dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6016 struct die_info *die,
891813be 6017 dwarf2_psymtab *pst)
aaa75496 6018{
fff8551c 6019 line_header_up lh;
d85a05f0 6020 struct attribute *attr;
aaa75496 6021
d85a05f0 6022 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
6023 if (attr != nullptr && attr->form_is_unsigned ())
6024 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
6025 if (lh == NULL)
6026 return; /* No linetable, so no includes. */
6027
79748972
TT
6028 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6029 that we pass in the raw text_low here; that is ok because we're
6030 only decoding the line table to make include partial symtabs, and
6031 so the addresses aren't really used. */
4ae976d1 6032 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6033 pst->raw_text_low (), 1);
aaa75496
JB
6034}
6035
348e048f 6036static hashval_t
52dc124a 6037hash_signatured_type (const void *item)
348e048f 6038{
9a3c8263
SM
6039 const struct signatured_type *sig_type
6040 = (const struct signatured_type *) item;
9a619af0 6041
348e048f 6042 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6043 return sig_type->signature;
348e048f
DE
6044}
6045
6046static int
52dc124a 6047eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6048{
9a3c8263
SM
6049 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6050 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6051
348e048f
DE
6052 return lhs->signature == rhs->signature;
6053}
6054
1fd400ff
TT
6055/* Allocate a hash table for signatured types. */
6056
b0b6a987 6057static htab_up
298e9637 6058allocate_signatured_type_table ()
1fd400ff 6059{
b0b6a987
TT
6060 return htab_up (htab_create_alloc (41,
6061 hash_signatured_type,
6062 eq_signatured_type,
6063 NULL, xcalloc, xfree));
1fd400ff
TT
6064}
6065
d467dd73 6066/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6067
6068static int
d467dd73 6069add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6070{
9a3c8263 6071 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6072 std::vector<signatured_type *> *all_type_units
6073 = (std::vector<signatured_type *> *) datum;
1fd400ff 6074
b2bdb8cf 6075 all_type_units->push_back (sigt);
1fd400ff
TT
6076
6077 return 1;
6078}
6079
78d4d2c5 6080/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6081 and fill them into TYPES_HTAB. It will process only type units,
6082 therefore DW_UT_type. */
c88ee1f0 6083
78d4d2c5 6084static void
976ca316 6085create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6086 struct dwo_file *dwo_file,
b0b6a987 6087 dwarf2_section_info *section, htab_up &types_htab,
43988095 6088 rcuh_kind section_kind)
348e048f 6089{
976ca316 6090 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6091 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6092 bfd *abfd;
6093 const gdb_byte *info_ptr, *end_ptr;
348e048f 6094
4bdcc0c1
DE
6095 abbrev_section = (dwo_file != NULL
6096 ? &dwo_file->sections.abbrev
976ca316 6097 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6098
4800761a 6099 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
6100 section->get_name (),
6101 abbrev_section->get_file_name ());
09406207 6102
96b79293 6103 section->read (objfile);
78d4d2c5 6104 info_ptr = section->buffer;
348e048f 6105
78d4d2c5
JK
6106 if (info_ptr == NULL)
6107 return;
348e048f 6108
78d4d2c5
JK
6109 /* We can't set abfd until now because the section may be empty or
6110 not present, in which case the bfd is unknown. */
96b79293 6111 abfd = section->get_bfd_owner ();
348e048f 6112
c0ab21c2
TT
6113 /* We don't use cutu_reader here because we don't need to read
6114 any dies: the signature is in the header. */
3019eac3 6115
78d4d2c5
JK
6116 end_ptr = info_ptr + section->size;
6117 while (info_ptr < end_ptr)
6118 {
78d4d2c5
JK
6119 struct signatured_type *sig_type;
6120 struct dwo_unit *dwo_tu;
6121 void **slot;
6122 const gdb_byte *ptr = info_ptr;
6123 struct comp_unit_head header;
6124 unsigned int length;
8b70b953 6125
9c541725 6126 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6127
a49dd8dd
JK
6128 /* Initialize it due to a false compiler warning. */
6129 header.signature = -1;
9c541725 6130 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6131
78d4d2c5
JK
6132 /* We need to read the type's signature in order to build the hash
6133 table, but we don't need anything else just yet. */
348e048f 6134
976ca316 6135 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6136 abbrev_section, ptr, section_kind);
348e048f 6137
4057dfde 6138 length = header.get_length ();
6caca83c 6139
78d4d2c5
JK
6140 /* Skip dummy type units. */
6141 if (ptr >= info_ptr + length
43988095 6142 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6143 || (header.unit_type != DW_UT_type
6144 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6145 {
6146 info_ptr += length;
6147 continue;
6148 }
dee91e82 6149
78d4d2c5
JK
6150 if (types_htab == NULL)
6151 {
6152 if (dwo_file)
298e9637 6153 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6154 else
298e9637 6155 types_htab = allocate_signatured_type_table ();
78d4d2c5 6156 }
8b70b953 6157
78d4d2c5
JK
6158 if (dwo_file)
6159 {
6160 sig_type = NULL;
976ca316 6161 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6162 dwo_tu->dwo_file = dwo_file;
43988095 6163 dwo_tu->signature = header.signature;
9c541725 6164 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6165 dwo_tu->section = section;
9c541725 6166 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6167 dwo_tu->length = length;
6168 }
6169 else
6170 {
6171 /* N.B.: type_offset is not usable if this type uses a DWO file.
6172 The real type_offset is in the DWO file. */
6173 dwo_tu = NULL;
976ca316 6174 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6175 sig_type->signature = header.signature;
9c541725 6176 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6177 sig_type->per_cu.is_debug_types = 1;
6178 sig_type->per_cu.section = section;
9c541725 6179 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6180 sig_type->per_cu.length = length;
6181 }
6182
b0b6a987 6183 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6184 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6185 INSERT);
6186 gdb_assert (slot != NULL);
6187 if (*slot != NULL)
6188 {
9c541725 6189 sect_offset dup_sect_off;
0349ea22 6190
3019eac3
DE
6191 if (dwo_file)
6192 {
78d4d2c5
JK
6193 const struct dwo_unit *dup_tu
6194 = (const struct dwo_unit *) *slot;
6195
9c541725 6196 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6197 }
6198 else
6199 {
78d4d2c5
JK
6200 const struct signatured_type *dup_tu
6201 = (const struct signatured_type *) *slot;
6202
9c541725 6203 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6204 }
8b70b953 6205
b98664d3 6206 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6207 " the entry at offset %s, signature %s"),
6208 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6209 hex_string (header.signature));
78d4d2c5
JK
6210 }
6211 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6212
6f738b01
SM
6213 dwarf_read_debug_printf_v (" offset %s, signature %s",
6214 sect_offset_str (sect_off),
6215 hex_string (header.signature));
3019eac3 6216
78d4d2c5
JK
6217 info_ptr += length;
6218 }
6219}
3019eac3 6220
78d4d2c5
JK
6221/* Create the hash table of all entries in the .debug_types
6222 (or .debug_types.dwo) section(s).
6223 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6224 otherwise it is NULL.
b3c8eb43 6225
78d4d2c5 6226 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6227
78d4d2c5 6228 Note: This function processes DWO files only, not DWP files. */
348e048f 6229
78d4d2c5 6230static void
976ca316 6231create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6232 struct dwo_file *dwo_file,
fd5866f6 6233 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6234 htab_up &types_htab)
78d4d2c5 6235{
fd5866f6 6236 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6237 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6238 rcuh_kind::TYPE);
3019eac3
DE
6239}
6240
6241/* Create the hash table of all entries in the .debug_types section,
6242 and initialize all_type_units.
6243 The result is zero if there is an error (e.g. missing .debug_types section),
6244 otherwise non-zero. */
6245
6246static int
976ca316 6247create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6248{
b0b6a987 6249 htab_up types_htab;
3019eac3 6250
976ca316
SM
6251 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6252 types_htab, rcuh_kind::COMPILE);
6253 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6254 types_htab);
3019eac3
DE
6255 if (types_htab == NULL)
6256 {
976ca316 6257 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6258 return 0;
6259 }
6260
976ca316 6261 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6262
976ca316
SM
6263 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6264 per_objfile->per_bfd->all_type_units.reserve
6265 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6266
976ca316 6267 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6268 add_signatured_type_cu_to_table,
976ca316 6269 &per_objfile->per_bfd->all_type_units);
1fd400ff 6270
348e048f
DE
6271 return 1;
6272}
6273
5989a64e 6274/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6275 If SLOT is non-NULL, it is the entry to use in the hash table.
6276 Otherwise we find one. */
6277
6278static struct signatured_type *
976ca316 6279add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6280{
976ca316
SM
6281 if (per_objfile->per_bfd->all_type_units.size ()
6282 == per_objfile->per_bfd->all_type_units.capacity ())
6283 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6284
976ca316 6285 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6286
976ca316 6287 per_objfile->resize_symtabs ();
af758d11 6288
976ca316 6289 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6290 sig_type->signature = sig;
6291 sig_type->per_cu.is_debug_types = 1;
976ca316 6292 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6293 {
6294 sig_type->per_cu.v.quick =
976ca316 6295 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6296 struct dwarf2_per_cu_quick_data);
6297 }
6298
6299 if (slot == NULL)
6300 {
976ca316 6301 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6302 sig_type, INSERT);
6303 }
6304 gdb_assert (*slot == NULL);
6305 *slot = sig_type;
6306 /* The rest of sig_type must be filled in by the caller. */
6307 return sig_type;
6308}
6309
a2ce51a0
DE
6310/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6311 Fill in SIG_ENTRY with DWO_ENTRY. */
6312
6313static void
976ca316 6314fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6315 struct signatured_type *sig_entry,
6316 struct dwo_unit *dwo_entry)
6317{
976ca316 6318 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6319
7ee85ab1 6320 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6321 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6322 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6323 if (per_bfd->using_index)
6aa5f3a6
DE
6324 {
6325 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6326 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6327 }
6328 else
6329 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6330 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6331 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6332 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6333 gdb_assert (sig_entry->dwo_unit == NULL);
6334
6335 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6336 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6337 sig_entry->per_cu.length = dwo_entry->length;
6338 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6339 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6340 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6341 sig_entry->dwo_unit = dwo_entry;
6342}
6343
6344/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6345 If we haven't read the TU yet, create the signatured_type data structure
6346 for a TU to be read in directly from a DWO file, bypassing the stub.
6347 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6348 using .gdb_index, then when reading a CU we want to stay in the DWO file
6349 containing that CU. Otherwise we could end up reading several other DWO
6350 files (due to comdat folding) to process the transitive closure of all the
6351 mentioned TUs, and that can be slow. The current DWO file will have every
6352 type signature that it needs.
a2ce51a0
DE
6353 We only do this for .gdb_index because in the psymtab case we already have
6354 to read all the DWOs to build the type unit groups. */
6355
6356static struct signatured_type *
6357lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6358{
976ca316 6359 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6360 struct dwo_file *dwo_file;
6361 struct dwo_unit find_dwo_entry, *dwo_entry;
6362 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6363 void **slot;
a2ce51a0 6364
976ca316 6365 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6366
6aa5f3a6
DE
6367 /* If TU skeletons have been removed then we may not have read in any
6368 TUs yet. */
976ca316
SM
6369 if (per_objfile->per_bfd->signatured_types == NULL)
6370 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6371
6372 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6373 Use the global signatured_types array to do our own comdat-folding
6374 of types. If this is the first time we're reading this TU, and
6375 the TU has an entry in .gdb_index, replace the recorded data from
6376 .gdb_index with this TU. */
a2ce51a0 6377
a2ce51a0 6378 find_sig_entry.signature = sig;
976ca316 6379 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6380 &find_sig_entry, INSERT);
9a3c8263 6381 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6382
6383 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6384 read. Don't reassign the global entry to point to this DWO if that's
6385 the case. Also note that if the TU is already being read, it may not
6386 have come from a DWO, the program may be a mix of Fission-compiled
6387 code and non-Fission-compiled code. */
6388
6389 /* Have we already tried to read this TU?
6390 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6391 needn't exist in the global table yet). */
6392 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6393 return sig_entry;
6394
6aa5f3a6
DE
6395 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6396 dwo_unit of the TU itself. */
6397 dwo_file = cu->dwo_unit->dwo_file;
6398
a2ce51a0
DE
6399 /* Ok, this is the first time we're reading this TU. */
6400 if (dwo_file->tus == NULL)
6401 return NULL;
6402 find_dwo_entry.signature = sig;
b0b6a987
TT
6403 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6404 &find_dwo_entry);
a2ce51a0
DE
6405 if (dwo_entry == NULL)
6406 return NULL;
6407
6aa5f3a6
DE
6408 /* If the global table doesn't have an entry for this TU, add one. */
6409 if (sig_entry == NULL)
976ca316 6410 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6411
976ca316 6412 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6413 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6414 return sig_entry;
6415}
6416
a2ce51a0
DE
6417/* Subroutine of lookup_signatured_type.
6418 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6419 then try the DWP file. If the TU stub (skeleton) has been removed then
6420 it won't be in .gdb_index. */
a2ce51a0
DE
6421
6422static struct signatured_type *
6423lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6424{
976ca316
SM
6425 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6426 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6427 struct dwo_unit *dwo_entry;
6428 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6429 void **slot;
a2ce51a0 6430
976ca316 6431 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6432 gdb_assert (dwp_file != NULL);
6433
6aa5f3a6
DE
6434 /* If TU skeletons have been removed then we may not have read in any
6435 TUs yet. */
976ca316
SM
6436 if (per_objfile->per_bfd->signatured_types == NULL)
6437 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6438
6aa5f3a6 6439 find_sig_entry.signature = sig;
976ca316 6440 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6441 &find_sig_entry, INSERT);
9a3c8263 6442 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6443
6444 /* Have we already tried to read this TU?
6445 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6446 needn't exist in the global table yet). */
6447 if (sig_entry != NULL)
6448 return sig_entry;
6449
a2ce51a0
DE
6450 if (dwp_file->tus == NULL)
6451 return NULL;
976ca316
SM
6452 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6453 1 /* is_debug_types */);
a2ce51a0
DE
6454 if (dwo_entry == NULL)
6455 return NULL;
6456
976ca316
SM
6457 sig_entry = add_type_unit (per_objfile, sig, slot);
6458 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6459
a2ce51a0
DE
6460 return sig_entry;
6461}
6462
380bca97 6463/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6464 Returns NULL if signature SIG is not present in the table.
6465 It is up to the caller to complain about this. */
348e048f
DE
6466
6467static struct signatured_type *
a2ce51a0 6468lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6469{
976ca316 6470 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6471
976ca316 6472 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6473 {
6474 /* We're in a DWO/DWP file, and we're using .gdb_index.
6475 These cases require special processing. */
976ca316 6476 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6477 return lookup_dwo_signatured_type (cu, sig);
6478 else
6479 return lookup_dwp_signatured_type (cu, sig);
6480 }
6481 else
6482 {
6483 struct signatured_type find_entry, *entry;
348e048f 6484
976ca316 6485 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6486 return NULL;
6487 find_entry.signature = sig;
9a3c8263 6488 entry = ((struct signatured_type *)
976ca316 6489 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6490 &find_entry));
a2ce51a0
DE
6491 return entry;
6492 }
348e048f 6493}
18a8505e 6494
42e7ad6c 6495/* Low level DIE reading support. */
348e048f 6496
d85a05f0
DJ
6497/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6498
6499static void
6500init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6501 struct dwarf2_cu *cu,
3019eac3 6502 struct dwarf2_section_info *section,
685af9cd
TT
6503 struct dwo_file *dwo_file,
6504 struct abbrev_table *abbrev_table)
d85a05f0 6505{
fceca515 6506 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6507 reader->abfd = section->get_bfd_owner ();
d85a05f0 6508 reader->cu = cu;
3019eac3 6509 reader->dwo_file = dwo_file;
dee91e82
DE
6510 reader->die_section = section;
6511 reader->buffer = section->buffer;
f664829e 6512 reader->buffer_end = section->buffer + section->size;
685af9cd 6513 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6514}
6515
c0ab21c2 6516/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6517 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6518 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6519 already.
6520
6521 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6522 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6523 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6524 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6525 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6526 STUB_COMP_DIR may be non-NULL.
3e225074 6527 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6528 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6529 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6530 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6531 kept around for at least as long as *RESULT_READER.
6532
b0c7bfa9
DE
6533 The result is non-zero if a valid (non-dummy) DIE was found. */
6534
6535static int
4ab09049 6536read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6537 struct dwo_unit *dwo_unit,
b0c7bfa9 6538 struct die_info *stub_comp_unit_die,
a2ce51a0 6539 const char *stub_comp_dir,
b0c7bfa9 6540 struct die_reader_specs *result_reader,
d521ce57 6541 const gdb_byte **result_info_ptr,
b0c7bfa9 6542 struct die_info **result_comp_unit_die,
685af9cd 6543 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6544{
976ca316 6545 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6546 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6547 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6548 bfd *abfd;
d521ce57 6549 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6550 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6551 int i,num_extra_attrs;
6552 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6553 struct die_info *comp_unit_die;
6554
b0aeadb3
DE
6555 /* At most one of these may be provided. */
6556 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6557
b0c7bfa9
DE
6558 /* These attributes aren't processed until later:
6559 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6560 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6561 referenced later. However, these attributes are found in the stub
6562 which we won't have later. In order to not impose this complication
6563 on the rest of the code, we read them here and copy them to the
6564 DWO CU/TU die. */
b0c7bfa9
DE
6565
6566 stmt_list = NULL;
6567 low_pc = NULL;
6568 high_pc = NULL;
6569 ranges = NULL;
6570 comp_dir = NULL;
6571
6572 if (stub_comp_unit_die != NULL)
6573 {
6574 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6575 DWO file. */
4ab09049 6576 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6577 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6578 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6579 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6580 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6581 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6582
a39fdb41 6583 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6584
2b0c7f41
SM
6585 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6586 We need the value before we can process DW_AT_ranges values from the
6587 DWO. */
6588 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6589
6590 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6591 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6592 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6593 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6594 section. */
6595 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6596 }
a2ce51a0
DE
6597 else if (stub_comp_dir != NULL)
6598 {
6599 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6600 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6601 comp_dir->name = DW_AT_comp_dir;
6602 comp_dir->form = DW_FORM_string;
c6481205 6603 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6604 }
b0c7bfa9
DE
6605
6606 /* Set up for reading the DWO CU/TU. */
6607 cu->dwo_unit = dwo_unit;
685af9cd 6608 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6609 section->read (objfile);
6610 abfd = section->get_bfd_owner ();
9c541725
PA
6611 begin_info_ptr = info_ptr = (section->buffer
6612 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6613 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6614
4ab09049 6615 if (per_cu->is_debug_types)
b0c7bfa9 6616 {
4ab09049 6617 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6618
976ca316
SM
6619 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6620 section, dwo_abbrev_section,
43988095 6621 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6622 /* This is not an assert because it can be caused by bad debug info. */
43988095 6623 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6624 {
6625 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6626 " TU at offset %s [in module %s]"),
a2ce51a0 6627 hex_string (sig_type->signature),
43988095 6628 hex_string (cu->header.signature),
9d8780f0 6629 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6630 bfd_get_filename (abfd));
6631 }
9c541725 6632 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6633 /* For DWOs coming from DWP files, we don't know the CU length
6634 nor the type's offset in the TU until now. */
4057dfde 6635 dwo_unit->length = cu->header.get_length ();
9c541725 6636 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6637
6638 /* Establish the type offset that can be used to lookup the type.
6639 For DWO files, we don't know it until now. */
9c541725
PA
6640 sig_type->type_offset_in_section
6641 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6642 }
6643 else
6644 {
976ca316
SM
6645 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6646 section, dwo_abbrev_section,
43988095 6647 info_ptr, rcuh_kind::COMPILE);
9c541725 6648 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6649 /* For DWOs coming from DWP files, we don't know the CU length
6650 until now. */
4057dfde 6651 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6652 }
6653
606decb2 6654 dwo_abbrev_section->read (objfile);
685af9cd 6655 *result_dwo_abbrev_table
606decb2 6656 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6657 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6658 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6659
6660 /* Read in the die, but leave space to copy over the attributes
6661 from the stub. This has the benefit of simplifying the rest of
6662 the code - all the work to maintain the illusion of a single
6663 DW_TAG_{compile,type}_unit DIE is done here. */
6664 num_extra_attrs = ((stmt_list != NULL)
6665 + (low_pc != NULL)
6666 + (high_pc != NULL)
6667 + (ranges != NULL)
6668 + (comp_dir != NULL));
6669 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6670 num_extra_attrs);
b0c7bfa9
DE
6671
6672 /* Copy over the attributes from the stub to the DIE we just read in. */
6673 comp_unit_die = *result_comp_unit_die;
6674 i = comp_unit_die->num_attrs;
6675 if (stmt_list != NULL)
6676 comp_unit_die->attrs[i++] = *stmt_list;
6677 if (low_pc != NULL)
6678 comp_unit_die->attrs[i++] = *low_pc;
6679 if (high_pc != NULL)
6680 comp_unit_die->attrs[i++] = *high_pc;
6681 if (ranges != NULL)
6682 comp_unit_die->attrs[i++] = *ranges;
6683 if (comp_dir != NULL)
6684 comp_unit_die->attrs[i++] = *comp_dir;
6685 comp_unit_die->num_attrs += num_extra_attrs;
6686
b4f54984 6687 if (dwarf_die_debug)
bf6af496
DE
6688 {
6689 fprintf_unfiltered (gdb_stdlog,
6690 "Read die from %s@0x%x of %s:\n",
96b79293 6691 section->get_name (),
bf6af496
DE
6692 (unsigned) (begin_info_ptr - section->buffer),
6693 bfd_get_filename (abfd));
b4f54984 6694 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6695 }
6696
b0c7bfa9
DE
6697 /* Skip dummy compilation units. */
6698 if (info_ptr >= begin_info_ptr + dwo_unit->length
6699 || peek_abbrev_code (abfd, info_ptr) == 0)
6700 return 0;
6701
6702 *result_info_ptr = info_ptr;
6703 return 1;
6704}
6705
a084a2a6
AT
6706/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6707 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6708 signature is part of the header. */
6709static gdb::optional<ULONGEST>
6710lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6711{
6712 if (cu->header.version >= 5)
6713 return cu->header.signature;
6714 struct attribute *attr;
6715 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 6716 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 6717 return gdb::optional<ULONGEST> ();
529908cb 6718 return attr->as_unsigned ();
a084a2a6
AT
6719}
6720
c0ab21c2 6721/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6722 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6723 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6724
6725static struct dwo_unit *
4ab09049 6726lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6727{
4ab09049 6728 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6729 struct dwo_unit *dwo_unit;
c0ab21c2 6730 const char *comp_dir;
b0c7bfa9 6731
a2ce51a0
DE
6732 gdb_assert (cu != NULL);
6733
b0c7bfa9 6734 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6735 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6736 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6737
4ab09049
SM
6738 if (per_cu->is_debug_types)
6739 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6740 else
6741 {
a084a2a6 6742 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6743
a084a2a6 6744 if (!signature.has_value ())
b0c7bfa9
DE
6745 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6746 " [in module %s]"),
4ab09049
SM
6747 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6748
6749 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6750 }
6751
b0c7bfa9
DE
6752 return dwo_unit;
6753}
6754
c0ab21c2 6755/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6756 See it for a description of the parameters.
fcd3b13d 6757 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6758
c0ab21c2 6759void
9e021579
SM
6760cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6761 dwarf2_per_objfile *per_objfile,
2e671100 6762 dwarf2_cu *existing_cu)
a2ce51a0 6763{
a2ce51a0 6764 struct signatured_type *sig_type;
a2ce51a0
DE
6765
6766 /* Verify we can do the following downcast, and that we have the
6767 data we need. */
6768 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6769 sig_type = (struct signatured_type *) this_cu;
6770 gdb_assert (sig_type->dwo_unit != NULL);
6771
2e671100
SM
6772 dwarf2_cu *cu;
6773
6774 if (existing_cu != nullptr)
6aa5f3a6 6775 {
2e671100
SM
6776 cu = existing_cu;
6777 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6778 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6779 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6780 }
6781 else
6782 {
7188ed02 6783 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6784 in per_objfile yet. */
7188ed02 6785 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6786 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6787 cu = m_new_cu.get ();
6aa5f3a6
DE
6788 }
6789
6790 /* A future optimization, if needed, would be to use an existing
6791 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6792 could share abbrev tables. */
a2ce51a0 6793
2e671100 6794 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6795 NULL /* stub_comp_unit_die */,
6796 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6797 this, &info_ptr,
3e225074 6798 &comp_unit_die,
c0ab21c2 6799 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6800 {
6801 /* Dummy die. */
c0ab21c2 6802 dummy_p = true;
a2ce51a0 6803 }
a2ce51a0
DE
6804}
6805
fd820528 6806/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6807 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6808
f4dc4d17
DE
6809 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6810 Otherwise the table specified in the comp unit header is read in and used.
6811 This is an optimization for when we already have the abbrev table.
6812
2e671100
SM
6813 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6814 allocated. */
aaa75496 6815
ab432490 6816cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 6817 dwarf2_per_objfile *per_objfile,
c0ab21c2 6818 struct abbrev_table *abbrev_table,
2e671100 6819 dwarf2_cu *existing_cu,
c0ab21c2
TT
6820 bool skip_partial)
6821 : die_reader_specs {},
6751ebae 6822 m_this_cu (this_cu)
c906108c 6823{
976ca316 6824 struct objfile *objfile = per_objfile->objfile;
8a0459fd 6825 struct dwarf2_section_info *section = this_cu->section;
96b79293 6826 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 6827 const gdb_byte *begin_info_ptr;
dee91e82 6828 struct signatured_type *sig_type = NULL;
4bdcc0c1 6829 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6830 /* Non-zero if CU currently points to a DWO file and we need to
6831 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6832 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6833 int rereading_dwo_cu = 0;
c906108c 6834
b4f54984 6835 if (dwarf_die_debug)
9d8780f0 6836 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6837 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6838 sect_offset_str (this_cu->sect_off));
09406207 6839
a2ce51a0
DE
6840 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6841 file (instead of going through the stub), short-circuit all of this. */
6842 if (this_cu->reading_dwo_directly)
6843 {
6844 /* Narrow down the scope of possibilities to have to understand. */
6845 gdb_assert (this_cu->is_debug_types);
6846 gdb_assert (abbrev_table == NULL);
976ca316 6847 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
6848 return;
6849 }
6850
dee91e82 6851 /* This is cheap if the section is already read in. */
96b79293 6852 section->read (objfile);
dee91e82 6853
9c541725 6854 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6855
6856 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 6857
2e671100
SM
6858 dwarf2_cu *cu;
6859
6860 if (existing_cu != nullptr)
dee91e82 6861 {
2e671100 6862 cu = existing_cu;
42e7ad6c
DE
6863 /* If this CU is from a DWO file we need to start over, we need to
6864 refetch the attributes from the skeleton CU.
6865 This could be optimized by retrieving those attributes from when we
6866 were here the first time: the previous comp_unit_die was stored in
6867 comp_unit_obstack. But there's no data yet that we need this
6868 optimization. */
6869 if (cu->dwo_unit != NULL)
6870 rereading_dwo_cu = 1;
dee91e82
DE
6871 }
6872 else
6873 {
7188ed02 6874 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6875 in per_objfile yet. */
976ca316
SM
6876 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6877 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 6878 cu = m_new_cu.get ();
42e7ad6c 6879 }
dee91e82 6880
b0c7bfa9 6881 /* Get the header. */
9c541725 6882 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6883 {
6884 /* We already have the header, there's no need to read it in again. */
9c541725 6885 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6886 }
6887 else
6888 {
3019eac3 6889 if (this_cu->is_debug_types)
dee91e82 6890 {
976ca316
SM
6891 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6892 section, abbrev_section,
6893 info_ptr, rcuh_kind::TYPE);
dee91e82 6894
42e7ad6c
DE
6895 /* Since per_cu is the first member of struct signatured_type,
6896 we can go from a pointer to one to a pointer to the other. */
6897 sig_type = (struct signatured_type *) this_cu;
43988095 6898 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6899 gdb_assert (sig_type->type_offset_in_tu
6900 == cu->header.type_cu_offset_in_tu);
6901 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6902
42e7ad6c
DE
6903 /* LENGTH has not been set yet for type units if we're
6904 using .gdb_index. */
4057dfde 6905 this_cu->length = cu->header.get_length ();
3019eac3
DE
6906
6907 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6908 sig_type->type_offset_in_section =
6909 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6910
6911 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6912 }
6913 else
6914 {
976ca316
SM
6915 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6916 section, abbrev_section,
43988095
JK
6917 info_ptr,
6918 rcuh_kind::COMPILE);
dee91e82 6919
9c541725 6920 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
6921 if (this_cu->length == 0)
6922 this_cu->length = cu->header.get_length ();
6923 else
6924 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6925 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6926 }
6927 }
10b3939b 6928
6caca83c 6929 /* Skip dummy compilation units. */
dee91e82 6930 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6931 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6932 {
6933 dummy_p = true;
6934 return;
6935 }
6caca83c 6936
433df2d4
DE
6937 /* If we don't have them yet, read the abbrevs for this compilation unit.
6938 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6939 done. */
f4dc4d17 6940 if (abbrev_table != NULL)
685af9cd
TT
6941 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6942 else
f4dc4d17 6943 {
606decb2 6944 abbrev_section->read (objfile);
c0ab21c2 6945 m_abbrev_table_holder
606decb2 6946 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 6947 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6948 }
af703f96 6949
dee91e82 6950 /* Read the top level CU/TU die. */
c0ab21c2 6951 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6952 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6953
58f0c718 6954 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6955 {
6956 dummy_p = true;
6957 return;
6958 }
58f0c718 6959
b0c7bfa9 6960 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6961 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6962 table from the DWO file and pass the ownership over to us. It will be
6963 referenced from READER, so we must make sure to free it after we're done
6964 with READER.
6965
b0c7bfa9
DE
6966 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6967 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6968 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6969 if (dwo_name != nullptr)
3019eac3 6970 {
3019eac3 6971 struct dwo_unit *dwo_unit;
b0c7bfa9 6972 struct die_info *dwo_comp_unit_die;
3019eac3 6973
3e225074 6974 if (comp_unit_die->has_children)
6a506a2d 6975 {
b98664d3 6976 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6977 " has children (offset %s) [in module %s]"),
6978 sect_offset_str (this_cu->sect_off),
6979 bfd_get_filename (abfd));
6a506a2d 6980 }
4ab09049 6981 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 6982 if (dwo_unit != NULL)
3019eac3 6983 {
4ab09049 6984 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 6985 comp_unit_die, NULL,
c0ab21c2 6986 this, &info_ptr,
3e225074 6987 &dwo_comp_unit_die,
c0ab21c2 6988 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6989 {
6990 /* Dummy die. */
c0ab21c2 6991 dummy_p = true;
6a506a2d
DE
6992 return;
6993 }
6994 comp_unit_die = dwo_comp_unit_die;
6995 }
6996 else
6997 {
6998 /* Yikes, we couldn't find the rest of the DIE, we only have
6999 the stub. A complaint has already been logged. There's
7000 not much more we can do except pass on the stub DIE to
7001 die_reader_func. We don't want to throw an error on bad
7002 debug info. */
3019eac3
DE
7003 }
7004 }
c0ab21c2 7005}
3019eac3 7006
6751ebae
TT
7007void
7008cutu_reader::keep ()
c0ab21c2 7009{
b0c7bfa9 7010 /* Done, clean up. */
6751ebae
TT
7011 gdb_assert (!dummy_p);
7012 if (m_new_cu != NULL)
348e048f 7013 {
7188ed02 7014 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 7015 now. */
7188ed02
SM
7016 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7017 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7018 }
dee91e82
DE
7019}
7020
18a8505e
AT
7021/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7022 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7023 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7024
7025 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7026 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7027
7028 We fill in THIS_CU->length.
7029
dee91e82 7030 THIS_CU->cu is always freed when done.
3019eac3 7031 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7032 to care whether it refers to the "main" CU or the DWO CU.
7033
7034 When parent_cu is passed, it is used to provide a default value for
7035 str_offsets_base and addr_base from the parent. */
dee91e82 7036
ab432490 7037cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7038 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7039 struct dwarf2_cu *parent_cu,
7040 struct dwo_file *dwo_file)
7041 : die_reader_specs {},
7042 m_this_cu (this_cu)
dee91e82 7043{
976ca316 7044 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7045 struct dwarf2_section_info *section = this_cu->section;
96b79293 7046 bfd *abfd = section->get_bfd_owner ();
33e80786 7047 struct dwarf2_section_info *abbrev_section;
d521ce57 7048 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7049
b4f54984 7050 if (dwarf_die_debug)
9d8780f0 7051 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7052 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7053 sect_offset_str (this_cu->sect_off));
09406207 7054
976ca316 7055 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7056
33e80786
DE
7057 abbrev_section = (dwo_file != NULL
7058 ? &dwo_file->sections.abbrev
7059 : get_abbrev_section_for_cu (this_cu));
7060
dee91e82 7061 /* This is cheap if the section is already read in. */
96b79293 7062 section->read (objfile);
dee91e82 7063
976ca316 7064 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7065
9c541725 7066 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7067 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7068 section, abbrev_section, info_ptr,
43988095
JK
7069 (this_cu->is_debug_types
7070 ? rcuh_kind::TYPE
7071 : rcuh_kind::COMPILE));
dee91e82 7072
18a8505e
AT
7073 if (parent_cu != nullptr)
7074 {
c0ab21c2
TT
7075 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7076 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7077 }
4057dfde 7078 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7079
7080 /* Skip dummy compilation units. */
7081 if (info_ptr >= begin_info_ptr + this_cu->length
7082 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7083 {
7084 dummy_p = true;
7085 return;
7086 }
72bf9492 7087
606decb2 7088 abbrev_section->read (objfile);
c0ab21c2 7089 m_abbrev_table_holder
606decb2 7090 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 7091
c0ab21c2
TT
7092 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7093 m_abbrev_table_holder.get ());
3e225074 7094 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7095}
7096
0018ea6f
DE
7097\f
7098/* Type Unit Groups.
dee91e82 7099
0018ea6f
DE
7100 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7101 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7102 so that all types coming from the same compilation (.o file) are grouped
7103 together. A future step could be to put the types in the same symtab as
7104 the CU the types ultimately came from. */
ff013f42 7105
f4dc4d17
DE
7106static hashval_t
7107hash_type_unit_group (const void *item)
7108{
9a3c8263
SM
7109 const struct type_unit_group *tu_group
7110 = (const struct type_unit_group *) item;
f4dc4d17 7111
094b34ac 7112 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7113}
348e048f
DE
7114
7115static int
f4dc4d17 7116eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7117{
9a3c8263
SM
7118 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7119 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7120
094b34ac 7121 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7122}
348e048f 7123
f4dc4d17
DE
7124/* Allocate a hash table for type unit groups. */
7125
eaa5fa8b 7126static htab_up
298e9637 7127allocate_type_unit_groups_table ()
f4dc4d17 7128{
eaa5fa8b
TT
7129 return htab_up (htab_create_alloc (3,
7130 hash_type_unit_group,
7131 eq_type_unit_group,
7132 NULL, xcalloc, xfree));
f4dc4d17 7133}
dee91e82 7134
f4dc4d17
DE
7135/* Type units that don't have DW_AT_stmt_list are grouped into their own
7136 partial symtabs. We combine several TUs per psymtab to not let the size
7137 of any one psymtab grow too big. */
7138#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7139#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7140
094b34ac 7141/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7142 Create the type_unit_group object used to hold one or more TUs. */
7143
7144static struct type_unit_group *
094b34ac 7145create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7146{
976ca316
SM
7147 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7148 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7149 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7150 struct type_unit_group *tu_group;
f4dc4d17 7151
bab287cd 7152 tu_group = OBSTACK_ZALLOC (&per_bfd->obstack, type_unit_group);
094b34ac 7153 per_cu = &tu_group->per_cu;
1859c670 7154 per_cu->per_bfd = per_bfd;
f4dc4d17 7155
1859c670 7156 if (per_bfd->using_index)
094b34ac 7157 {
1859c670 7158 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7159 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7160 }
7161 else
7162 {
9c541725 7163 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7164 dwarf2_psymtab *pst;
528e1572 7165 std::string name;
094b34ac
DE
7166
7167 /* Give the symtab a useful name for debug purposes. */
7168 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7169 name = string_printf ("<type_units_%d>",
7170 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7171 else
528e1572 7172 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7173
976ca316 7174 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7175 pst->anonymous = true;
094b34ac 7176 }
f4dc4d17 7177
094b34ac 7178 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7179 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7180
7181 return tu_group;
7182}
7183
094b34ac
DE
7184/* Look up the type_unit_group for type unit CU, and create it if necessary.
7185 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7186
7187static struct type_unit_group *
ff39bb5e 7188get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7189{
976ca316
SM
7190 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7191 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7192 struct type_unit_group *tu_group;
7193 void **slot;
7194 unsigned int line_offset;
7195 struct type_unit_group type_unit_group_for_lookup;
7196
976ca316
SM
7197 if (per_objfile->per_bfd->type_unit_groups == NULL)
7198 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7199
7200 /* Do we need to create a new group, or can we use an existing one? */
7201
529908cb 7202 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7203 {
529908cb 7204 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7205 ++tu_stats->nr_symtab_sharers;
7206 }
7207 else
7208 {
7209 /* Ugh, no stmt_list. Rare, but we have to handle it.
7210 We can do various things here like create one group per TU or
7211 spread them over multiple groups to split up the expansion work.
7212 To avoid worst case scenarios (too many groups or too large groups)
7213 we, umm, group them in bunches. */
7214 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7215 | (tu_stats->nr_stmt_less_type_units
7216 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7217 ++tu_stats->nr_stmt_less_type_units;
7218 }
7219
094b34ac 7220 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7221 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7222 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7223 &type_unit_group_for_lookup, INSERT);
7224 if (*slot != NULL)
7225 {
9a3c8263 7226 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7227 gdb_assert (tu_group != NULL);
7228 }
7229 else
7230 {
9c541725 7231 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7232 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7233 *slot = tu_group;
7234 ++tu_stats->nr_symtabs;
7235 }
7236
7237 return tu_group;
7238}
0018ea6f
DE
7239\f
7240/* Partial symbol tables. */
7241
7242/* Create a psymtab named NAME and assign it to PER_CU.
7243
7244 The caller must fill in the following details:
7245 dirname, textlow, texthigh. */
7246
891813be 7247static dwarf2_psymtab *
7aa104c4
SM
7248create_partial_symtab (dwarf2_per_cu_data *per_cu,
7249 dwarf2_per_objfile *per_objfile,
7250 const char *name)
0018ea6f 7251{
0072c873
SM
7252 dwarf2_psymtab *pst
7253 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7254 per_objfile->objfile->per_bfd, per_cu);
0018ea6f 7255
6d94535f 7256 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7257
7258 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7259 per_cu->v.psymtab = pst;
7260
7261 return pst;
7262}
7263
c0ab21c2 7264/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7265
7266static void
7267process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7268 const gdb_byte *info_ptr,
0018ea6f 7269 struct die_info *comp_unit_die,
c0ab21c2 7270 enum language pretend_language)
0018ea6f
DE
7271{
7272 struct dwarf2_cu *cu = reader->cu;
7aa104c4 7273 dwarf2_per_objfile *per_objfile = cu->per_objfile;
84685904 7274 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7aa104c4 7275 struct objfile *objfile = per_objfile->objfile;
08feed99 7276 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7277 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7278 CORE_ADDR baseaddr;
7279 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7280 dwarf2_psymtab *pst;
3a2b436a 7281 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7282 const char *filename;
0018ea6f 7283
0018ea6f
DE
7284 gdb_assert (! per_cu->is_debug_types);
7285
c0ab21c2 7286 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7287
0018ea6f 7288 /* Allocate a new partial symbol table structure. */
2e927613
TV
7289 gdb::unique_xmalloc_ptr<char> debug_filename;
7290 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7291 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7292 if (filename == NULL)
0018ea6f 7293 filename = "";
2e927613
TV
7294 else if (strcmp (filename, artificial) == 0)
7295 {
7296 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7297 sect_offset_str (per_cu->sect_off),
7298 (char *) NULL));
2e927613
TV
7299 filename = debug_filename.get ();
7300 }
0018ea6f 7301
7aa104c4 7302 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7303
7304 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7305 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7306
b3b3bada 7307 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7308
7309 dwarf2_find_base_address (comp_unit_die, cu);
7310
7311 /* Possibly set the default values of LOWPC and HIGHPC from
7312 `DW_AT_ranges'. */
3a2b436a
JK
7313 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7314 &best_highpc, cu, pst);
7315 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7316 {
7317 CORE_ADDR low
7318 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7319 - baseaddr);
7320 CORE_ADDR high
7321 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7322 - baseaddr - 1);
7323 /* Store the contiguous range if it is not empty; it can be
7324 empty for CUs with no code. */
84685904 7325 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 7326 low, high, pst);
79748972 7327 }
0018ea6f
DE
7328
7329 /* Check if comp unit has_children.
7330 If so, read the rest of the partial symbols from this comp unit.
7331 If not, there's no more debug_info for this comp unit. */
3e225074 7332 if (comp_unit_die->has_children)
0018ea6f
DE
7333 {
7334 struct partial_die_info *first_die;
7335 CORE_ADDR lowpc, highpc;
7336
7337 lowpc = ((CORE_ADDR) -1);
7338 highpc = ((CORE_ADDR) 0);
7339
7340 first_die = load_partial_dies (reader, info_ptr, 1);
7341
7342 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7343 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7344
7345 /* If we didn't find a lowpc, set it to highpc to avoid
7346 complaints from `maint check'. */
7347 if (lowpc == ((CORE_ADDR) -1))
7348 lowpc = highpc;
7349
7350 /* If the compilation unit didn't have an explicit address range,
7351 then use the information extracted from its child dies. */
e385593e 7352 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7353 {
7354 best_lowpc = lowpc;
7355 best_highpc = highpc;
7356 }
7357 }
4ae976d1 7358 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7359 best_lowpc + baseaddr)
7360 - baseaddr);
4ae976d1 7361 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7362 best_highpc + baseaddr)
7363 - baseaddr);
0018ea6f 7364
ae7754b2 7365 pst->end ();
0018ea6f 7366
ae640021 7367 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7368 {
7369 int i;
ae640021 7370 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7371
7372 /* Fill in 'dependencies' here; we fill in 'users' in a
7373 post-pass. */
7374 pst->number_of_dependencies = len;
a9342b62 7375 pst->dependencies
84685904 7376 = per_bfd->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7377 for (i = 0; i < len; ++i)
7378 {
7379 pst->dependencies[i]
7380 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7381 }
0018ea6f 7382
ae640021 7383 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7384 }
7385
7386 /* Get the list of files included in the current compilation unit,
7387 and build a psymtab for each of them. */
7388 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7389
6f738b01
SM
7390 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7391 ", %d global, %d static syms",
7392 per_cu->is_debug_types ? "type" : "comp",
7393 sect_offset_str (per_cu->sect_off),
7394 paddress (gdbarch, pst->text_low (objfile)),
7395 paddress (gdbarch, pst->text_high (objfile)),
7396 (int) pst->global_psymbols.size (),
7397 (int) pst->static_psymbols.size ());
0018ea6f
DE
7398}
7399
7400/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7401 Process compilation unit THIS_CU for a psymtab. */
7402
7403static void
ab432490
SM
7404process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7405 dwarf2_per_objfile *per_objfile,
135f5437 7406 bool want_partial_unit,
b93601f3 7407 enum language pretend_language)
0018ea6f
DE
7408{
7409 /* If this compilation unit was already read in, free the
7410 cached copy in order to read it in again. This is
7411 necessary because we skipped some symbols when we first
7412 read in the compilation unit (see load_partial_dies).
7413 This problem could be avoided, but the benefit is unclear. */
7188ed02 7414 per_objfile->remove_cu (this_cu);
0018ea6f 7415
2e671100 7416 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7417
58990295
TV
7418 switch (reader.comp_unit_die->tag)
7419 {
7420 case DW_TAG_compile_unit:
7421 this_cu->unit_type = DW_UT_compile;
7422 break;
7423 case DW_TAG_partial_unit:
7424 this_cu->unit_type = DW_UT_partial;
7425 break;
e77b0004
TV
7426 case DW_TAG_type_unit:
7427 this_cu->unit_type = DW_UT_type;
7428 break;
58990295 7429 default:
702cf3f5
AB
7430 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7431 dwarf_tag_name (reader.comp_unit_die->tag),
7432 sect_offset_str (reader.cu->per_cu->sect_off),
7433 objfile_name (per_objfile->objfile));
58990295
TV
7434 }
7435
c0ab21c2 7436 if (reader.dummy_p)
f1902523 7437 {
c0ab21c2 7438 /* Nothing. */
f1902523 7439 }
c0ab21c2 7440 else if (this_cu->is_debug_types)
3e225074
TT
7441 build_type_psymtabs_reader (&reader, reader.info_ptr,
7442 reader.comp_unit_die);
135f5437
TT
7443 else if (want_partial_unit
7444 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7445 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7446 reader.comp_unit_die,
c0ab21c2 7447 pretend_language);
0018ea6f 7448
7188ed02 7449 this_cu->lang = reader.cu->language;
58990295 7450
0018ea6f 7451 /* Age out any secondary CUs. */
7188ed02 7452 per_objfile->age_comp_units ();
0018ea6f 7453}
f4dc4d17
DE
7454
7455/* Reader function for build_type_psymtabs. */
7456
7457static void
7458build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7459 const gdb_byte *info_ptr,
3e225074 7460 struct die_info *type_unit_die)
f4dc4d17 7461{
976ca316 7462 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7463 struct dwarf2_cu *cu = reader->cu;
7464 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7465 struct signatured_type *sig_type;
f4dc4d17
DE
7466 struct type_unit_group *tu_group;
7467 struct attribute *attr;
7468 struct partial_die_info *first_die;
7469 CORE_ADDR lowpc, highpc;
891813be 7470 dwarf2_psymtab *pst;
f4dc4d17 7471
0186c6a7
DE
7472 gdb_assert (per_cu->is_debug_types);
7473 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7474
3e225074 7475 if (! type_unit_die->has_children)
f4dc4d17
DE
7476 return;
7477
052c8bb8 7478 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7479 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7480
df07e2c7 7481 if (tu_group->tus == nullptr)
a8b3b8e9 7482 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7483 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7484
7485 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7486 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7487 pst->anonymous = true;
f4dc4d17
DE
7488
7489 first_die = load_partial_dies (reader, info_ptr, 1);
7490
7491 lowpc = (CORE_ADDR) -1;
7492 highpc = (CORE_ADDR) 0;
7493 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7494
ae7754b2 7495 pst->end ();
f4dc4d17
DE
7496}
7497
73051182
DE
7498/* Struct used to sort TUs by their abbreviation table offset. */
7499
7500struct tu_abbrev_offset
7501{
b2bdb8cf
SM
7502 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7503 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7504 {}
7505
7506 signatured_type *sig_type;
73051182
DE
7507 sect_offset abbrev_offset;
7508};
7509
484cf504 7510/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7511
484cf504
TT
7512static bool
7513sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7514 const struct tu_abbrev_offset &b)
73051182 7515{
484cf504 7516 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7517}
7518
7519/* Efficiently read all the type units.
7520 This does the bulk of the work for build_type_psymtabs.
7521
7522 The efficiency is because we sort TUs by the abbrev table they use and
7523 only read each abbrev table once. In one program there are 200K TUs
7524 sharing 8K abbrev tables.
7525
7526 The main purpose of this function is to support building the
5989a64e 7527 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7528 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7529 can collapse the search space by grouping them by stmt_list.
7530 The savings can be significant, in the same program from above the 200K TUs
7531 share 8K stmt_list tables.
7532
7533 FUNC is expected to call get_type_unit_group, which will create the
7534 struct type_unit_group if necessary and add it to
5989a64e 7535 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7536
7537static void
976ca316 7538build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7539{
976ca316 7540 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7541 abbrev_table_up abbrev_table;
73051182 7542 sect_offset abbrev_offset;
73051182
DE
7543
7544 /* It's up to the caller to not call us multiple times. */
976ca316 7545 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7546
976ca316 7547 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7548 return;
7549
7550 /* TUs typically share abbrev tables, and there can be way more TUs than
7551 abbrev tables. Sort by abbrev table to reduce the number of times we
7552 read each abbrev table in.
7553 Alternatives are to punt or to maintain a cache of abbrev tables.
7554 This is simpler and efficient enough for now.
7555
7556 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7557 symtab to use). Typically TUs with the same abbrev offset have the same
7558 stmt_list value too so in practice this should work well.
7559
7560 The basic algorithm here is:
7561
7562 sort TUs by abbrev table
7563 for each TU with same abbrev table:
7564 read abbrev table if first user
7565 read TU top level DIE
7566 [IWBN if DWO skeletons had DW_AT_stmt_list]
7567 call FUNC */
7568
6f738b01 7569 dwarf_read_debug_printf ("Building type unit groups ...");
73051182
DE
7570
7571 /* Sort in a separate table to maintain the order of all_type_units
7572 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7573 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7574 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7575
976ca316 7576 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7577 sorted_by_abbrev.emplace_back
976ca316 7578 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7579 sig_type->per_cu.sect_off));
73051182 7580
484cf504
TT
7581 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7582 sort_tu_by_abbrev_offset);
73051182 7583
9c541725 7584 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7585
b2bdb8cf 7586 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7587 {
73051182
DE
7588 /* Switch to the next abbrev table if necessary. */
7589 if (abbrev_table == NULL
b2bdb8cf 7590 || tu.abbrev_offset != abbrev_offset)
73051182 7591 {
b2bdb8cf 7592 abbrev_offset = tu.abbrev_offset;
606decb2 7593 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7594 abbrev_table =
606decb2 7595 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7596 ++tu_stats->nr_uniq_abbrev_tables;
7597 }
7598
976ca316 7599 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7600 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7601 if (!reader.dummy_p)
7602 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7603 reader.comp_unit_die);
73051182 7604 }
6aa5f3a6 7605}
73051182 7606
6aa5f3a6
DE
7607/* Print collected type unit statistics. */
7608
7609static void
976ca316 7610print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7611{
976ca316 7612 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7613
6f738b01
SM
7614 dwarf_read_debug_printf ("Type unit statistics:");
7615 dwarf_read_debug_printf (" %zu TUs",
7616 per_objfile->per_bfd->all_type_units.size ());
7617 dwarf_read_debug_printf (" %d uniq abbrev tables",
7618 tu_stats->nr_uniq_abbrev_tables);
7619 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7620 tu_stats->nr_symtabs);
7621 dwarf_read_debug_printf (" %d symtab sharers",
7622 tu_stats->nr_symtab_sharers);
7623 dwarf_read_debug_printf (" %d type units without a stmt_list",
7624 tu_stats->nr_stmt_less_type_units);
7625 dwarf_read_debug_printf (" %d all_type_units reallocs",
7626 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7627}
7628
f4dc4d17
DE
7629/* Traversal function for build_type_psymtabs. */
7630
7631static int
7632build_type_psymtab_dependencies (void **slot, void *info)
7633{
976ca316 7634 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
84685904 7635 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
f4dc4d17 7636 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7637 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7638 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7639 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7640 int i;
7641
7642 gdb_assert (len > 0);
197400e8 7643 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7644
7645 pst->number_of_dependencies = len;
84685904 7646 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
df07e2c7 7647 for (i = 0; i < len; ++i)
f4dc4d17 7648 {
df07e2c7 7649 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7650 gdb_assert (iter->per_cu.is_debug_types);
7651 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7652 iter->type_unit_group = tu_group;
f4dc4d17
DE
7653 }
7654
df07e2c7
AB
7655 delete tu_group->tus;
7656 tu_group->tus = nullptr;
348e048f
DE
7657
7658 return 1;
7659}
7660
7661/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7662 Build partial symbol tables for the .debug_types comp-units. */
7663
7664static void
976ca316 7665build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7666{
976ca316 7667 if (! create_all_type_units (per_objfile))
348e048f
DE
7668 return;
7669
976ca316 7670 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7671}
f4dc4d17 7672
6aa5f3a6
DE
7673/* Traversal function for process_skeletonless_type_unit.
7674 Read a TU in a DWO file and build partial symbols for it. */
7675
7676static int
7677process_skeletonless_type_unit (void **slot, void *info)
7678{
7679 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7680 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7681 struct signatured_type find_entry, *entry;
7682
7683 /* If this TU doesn't exist in the global table, add it and read it in. */
7684
976ca316
SM
7685 if (per_objfile->per_bfd->signatured_types == NULL)
7686 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7687
7688 find_entry.signature = dwo_unit->signature;
976ca316 7689 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7690 &find_entry, INSERT);
6aa5f3a6
DE
7691 /* If we've already seen this type there's nothing to do. What's happening
7692 is we're doing our own version of comdat-folding here. */
7693 if (*slot != NULL)
7694 return 1;
7695
7696 /* This does the job that create_all_type_units would have done for
7697 this TU. */
976ca316
SM
7698 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7699 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7700 *slot = entry;
7701
7702 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7703 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7704 if (!reader.dummy_p)
7705 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7706 reader.comp_unit_die);
6aa5f3a6
DE
7707
7708 return 1;
7709}
7710
7711/* Traversal function for process_skeletonless_type_units. */
7712
7713static int
7714process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7715{
7716 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7717
7718 if (dwo_file->tus != NULL)
b0b6a987
TT
7719 htab_traverse_noresize (dwo_file->tus.get (),
7720 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7721
7722 return 1;
7723}
7724
7725/* Scan all TUs of DWO files, verifying we've processed them.
7726 This is needed in case a TU was emitted without its skeleton.
7727 Note: This can't be done until we know what all the DWO files are. */
7728
7729static void
976ca316 7730process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7731{
7732 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7733 if (get_dwp_file (per_objfile) == NULL
7734 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7735 {
976ca316 7736 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7737 process_dwo_file_for_skeletonless_type_units,
976ca316 7738 per_objfile);
6aa5f3a6 7739 }
348e048f
DE
7740}
7741
ed2dc618 7742/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7743
7744static void
976ca316 7745set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7746{
976ca316 7747 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7748 {
891813be 7749 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7750
36586728
TT
7751 if (pst == NULL)
7752 continue;
7753
b76e467d 7754 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7755 {
7756 /* Set the 'user' field only if it is not already set. */
7757 if (pst->dependencies[j]->user == NULL)
7758 pst->dependencies[j]->user = pst;
7759 }
7760 }
7761}
7762
93311388
DE
7763/* Build the partial symbol table by doing a quick pass through the
7764 .debug_info and .debug_abbrev sections. */
72bf9492 7765
93311388 7766static void
976ca316 7767dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 7768{
976ca316 7769 struct objfile *objfile = per_objfile->objfile;
84685904 7770 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
93311388 7771
6f738b01
SM
7772 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7773 objfile_name (objfile));
45cfd468 7774
76935768 7775 scoped_restore restore_reading_psyms
bab287cd 7776 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
98bfdba5 7777
84685904 7778 per_bfd->info.read (objfile);
91c24f0a 7779
93311388
DE
7780 /* Any cached compilation units will be linked by the per-objfile
7781 read_in_chain. Make sure to free them when we're done. */
976ca316 7782 free_cached_comp_units freer (per_objfile);
72bf9492 7783
976ca316 7784 build_type_psymtabs (per_objfile);
348e048f 7785
976ca316 7786 create_all_comp_units (per_objfile);
c906108c 7787
60606b2c
TT
7788 /* Create a temporary address map on a temporary obstack. We later
7789 copy this to the final obstack. */
8268c778 7790 auto_obstack temp_obstack;
791afaa2
TT
7791
7792 scoped_restore save_psymtabs_addrmap
84685904 7793 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
791afaa2 7794 addrmap_create_mutable (&temp_obstack));
72bf9492 7795
84685904 7796 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
3d5afab3
TV
7797 {
7798 if (per_cu->v.psymtab != NULL)
7799 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7800 continue;
976ca316 7801 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 7802 language_minimal);
3d5afab3 7803 }
ff013f42 7804
6aa5f3a6 7805 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 7806 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
7807
7808 /* Now that all TUs have been processed we can fill in the dependencies. */
84685904 7809 if (per_bfd->type_unit_groups != NULL)
6aa5f3a6 7810 {
84685904 7811 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
976ca316 7812 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
7813 }
7814
6f738b01 7815 if (dwarf_read_debug > 0)
976ca316 7816 print_tu_stats (per_objfile);
6aa5f3a6 7817
976ca316 7818 set_partial_user (per_objfile);
95554aad 7819
84685904
TT
7820 per_bfd->partial_symtabs->psymtabs_addrmap
7821 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7822 per_bfd->partial_symtabs->obstack ());
791afaa2
TT
7823 /* At this point we want to keep the address map. */
7824 save_psymtabs_addrmap.release ();
ff013f42 7825
6f738b01
SM
7826 dwarf_read_debug_printf ("Done building psymtabs of %s",
7827 objfile_name (objfile));
ae038cb0
DJ
7828}
7829
dee91e82
DE
7830/* Load the partial DIEs for a secondary CU into memory.
7831 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7832
dee91e82 7833static void
ab432490 7834load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
7835 dwarf2_per_objfile *per_objfile,
7836 dwarf2_cu *existing_cu)
dee91e82 7837{
2e671100 7838 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
7839
7840 if (!reader.dummy_p)
7841 {
7842 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7843 language_minimal);
7844
7845 /* Check if comp unit has_children.
7846 If so, read the rest of the partial symbols from this comp unit.
7847 If not, there's no more debug_info for this comp unit. */
3e225074 7848 if (reader.comp_unit_die->has_children)
c0ab21c2 7849 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7850
7851 reader.keep ();
c0ab21c2 7852 }
ae038cb0
DJ
7853}
7854
ae038cb0 7855static void
976ca316 7856read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 7857 struct dwarf2_section_info *section,
f1902523 7858 struct dwarf2_section_info *abbrev_section,
b76e467d 7859 unsigned int is_dwz)
ae038cb0 7860{
d521ce57 7861 const gdb_byte *info_ptr;
976ca316 7862 struct objfile *objfile = per_objfile->objfile;
be391dca 7863
6f738b01
SM
7864 dwarf_read_debug_printf ("Reading %s for %s",
7865 section->get_name (),
7866 section->get_file_name ());
bf6af496 7867
96b79293 7868 section->read (objfile);
ae038cb0 7869
36586728 7870 info_ptr = section->buffer;
6e70227d 7871
36586728 7872 while (info_ptr < section->buffer + section->size)
ae038cb0 7873 {
ae038cb0 7874 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7875
9c541725 7876 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7877
f1902523 7878 comp_unit_head cu_header;
976ca316 7879 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
7880 abbrev_section, info_ptr,
7881 rcuh_kind::COMPILE);
ae038cb0
DJ
7882
7883 /* Save the compilation unit for later lookup. */
f1902523 7884 if (cu_header.unit_type != DW_UT_type)
976ca316 7885 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
7886 else
7887 {
976ca316 7888 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
7889 sig_type->signature = cu_header.signature;
7890 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7891 this_cu = &sig_type->per_cu;
7892 }
7893 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7894 this_cu->sect_off = sect_off;
f1902523 7895 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7896 this_cu->is_dwz = is_dwz;
8a0459fd 7897 this_cu->section = section;
ae038cb0 7898
976ca316 7899 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7900
7901 info_ptr = info_ptr + this_cu->length;
7902 }
36586728
TT
7903}
7904
7905/* Create a list of all compilation units in OBJFILE.
7906 This is only done for -readnow and building partial symtabs. */
7907
7908static void
976ca316 7909create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 7910{
976ca316
SM
7911 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
7912 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7913 &per_objfile->per_bfd->abbrev, 0);
36586728 7914
976ca316 7915 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 7916 if (dwz != NULL)
976ca316 7917 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
7918}
7919
5734ee8b 7920/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7921 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7922 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7923 DW_AT_ranges). See the comments of add_partial_subprogram on how
7924 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7925
72bf9492
DJ
7926static void
7927scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7928 CORE_ADDR *highpc, int set_addrmap,
7929 struct dwarf2_cu *cu)
c906108c 7930{
72bf9492 7931 struct partial_die_info *pdi;
c906108c 7932
91c24f0a
DC
7933 /* Now, march along the PDI's, descending into ones which have
7934 interesting children but skipping the children of the other ones,
7935 until we reach the end of the compilation unit. */
c906108c 7936
72bf9492 7937 pdi = first_die;
91c24f0a 7938
72bf9492
DJ
7939 while (pdi != NULL)
7940 {
52356b79 7941 pdi->fixup (cu);
c906108c 7942
f55ee35c 7943 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7944 children, so we need to look at them. Ditto for anonymous
7945 enums. */
933c6fe4 7946
7d00ffec 7947 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7948 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7949 || pdi->tag == DW_TAG_imported_unit
7950 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7951 {
72bf9492 7952 switch (pdi->tag)
c906108c
SS
7953 {
7954 case DW_TAG_subprogram:
b1dc1806 7955 case DW_TAG_inlined_subroutine:
cdc07690 7956 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
7957 if (cu->language == language_cplus)
7958 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7959 set_addrmap, cu);
c906108c 7960 break;
72929c62 7961 case DW_TAG_constant:
c906108c
SS
7962 case DW_TAG_variable:
7963 case DW_TAG_typedef:
91c24f0a 7964 case DW_TAG_union_type:
317d2668
TV
7965 if (!pdi->is_declaration
7966 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 7967 {
72bf9492 7968 add_partial_symbol (pdi, cu);
63d06c5c
DC
7969 }
7970 break;
c906108c 7971 case DW_TAG_class_type:
680b30c7 7972 case DW_TAG_interface_type:
c906108c 7973 case DW_TAG_structure_type:
72bf9492 7974 if (!pdi->is_declaration)
c906108c 7975 {
72bf9492 7976 add_partial_symbol (pdi, cu);
c906108c 7977 }
b7fee5a3
KS
7978 if ((cu->language == language_rust
7979 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7980 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7981 set_addrmap, cu);
c906108c 7982 break;
91c24f0a 7983 case DW_TAG_enumeration_type:
72bf9492
DJ
7984 if (!pdi->is_declaration)
7985 add_partial_enumeration (pdi, cu);
c906108c
SS
7986 break;
7987 case DW_TAG_base_type:
dda83cd7 7988 case DW_TAG_subrange_type:
c906108c 7989 /* File scope base type definitions are added to the partial
dda83cd7 7990 symbol table. */
72bf9492 7991 add_partial_symbol (pdi, cu);
c906108c 7992 break;
d9fa45fe 7993 case DW_TAG_namespace:
cdc07690 7994 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7995 break;
5d7cb8df 7996 case DW_TAG_module:
59c35742
AB
7997 if (!pdi->is_declaration)
7998 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7999 break;
95554aad
TT
8000 case DW_TAG_imported_unit:
8001 {
8002 struct dwarf2_per_cu_data *per_cu;
8003
f4dc4d17
DE
8004 /* For now we don't handle imported units in type units. */
8005 if (cu->per_cu->is_debug_types)
8006 {
8007 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8008 " supported in type units [in module %s]"),
5e22e966 8009 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8010 }
8011
e3b94546 8012 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8013 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8014
8015 /* Go read the partial unit, if needed. */
8016 if (per_cu->v.psymtab == NULL)
ab432490
SM
8017 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8018 cu->language);
95554aad 8019
ae640021 8020 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8021 }
8022 break;
74921315
KS
8023 case DW_TAG_imported_declaration:
8024 add_partial_symbol (pdi, cu);
8025 break;
c906108c
SS
8026 default:
8027 break;
8028 }
8029 }
8030
72bf9492
DJ
8031 /* If the die has a sibling, skip to the sibling. */
8032
8033 pdi = pdi->die_sibling;
8034 }
8035}
8036
8037/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8038
72bf9492 8039 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8040 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8041 Enumerators are an exception; they use the scope of their parent
8042 enumeration type, i.e. the name of the enumeration type is not
8043 prepended to the enumerator.
91c24f0a 8044
72bf9492
DJ
8045 There are two complexities. One is DW_AT_specification; in this
8046 case "parent" means the parent of the target of the specification,
8047 instead of the direct parent of the DIE. The other is compilers
8048 which do not emit DW_TAG_namespace; in this case we try to guess
8049 the fully qualified name of structure types from their members'
8050 linkage names. This must be done using the DIE's children rather
8051 than the children of any DW_AT_specification target. We only need
8052 to do this for structures at the top level, i.e. if the target of
8053 any DW_AT_specification (if any; otherwise the DIE itself) does not
8054 have a parent. */
8055
8056/* Compute the scope prefix associated with PDI's parent, in
8057 compilation unit CU. The result will be allocated on CU's
8058 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8059 field. NULL is returned if no prefix is necessary. */
15d034d0 8060static const char *
72bf9492
DJ
8061partial_die_parent_scope (struct partial_die_info *pdi,
8062 struct dwarf2_cu *cu)
8063{
15d034d0 8064 const char *grandparent_scope;
72bf9492 8065 struct partial_die_info *parent, *real_pdi;
91c24f0a 8066
72bf9492
DJ
8067 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8068 then this means the parent of the specification DIE. */
8069
8070 real_pdi = pdi;
72bf9492 8071 while (real_pdi->has_specification)
fb816e8b 8072 {
122cf0f2
AB
8073 auto res = find_partial_die (real_pdi->spec_offset,
8074 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8075 real_pdi = res.pdi;
8076 cu = res.cu;
8077 }
72bf9492
DJ
8078
8079 parent = real_pdi->die_parent;
8080 if (parent == NULL)
8081 return NULL;
8082
8083 if (parent->scope_set)
8084 return parent->scope;
8085
52356b79 8086 parent->fixup (cu);
72bf9492 8087
10b3939b 8088 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8089
acebe513
UW
8090 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8091 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8092 Work around this problem here. */
8093 if (cu->language == language_cplus
6e70227d 8094 && parent->tag == DW_TAG_namespace
7d00ffec 8095 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8096 && grandparent_scope == NULL)
8097 {
8098 parent->scope = NULL;
8099 parent->scope_set = 1;
8100 return NULL;
8101 }
8102
0a4b0913 8103 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8104 if (pdi->tag == DW_TAG_enumerator)
8105 /* Enumerators should not get the name of the enumeration as a prefix. */
8106 parent->scope = grandparent_scope;
8107 else if (parent->tag == DW_TAG_namespace
f55ee35c 8108 || parent->tag == DW_TAG_module
72bf9492
DJ
8109 || parent->tag == DW_TAG_structure_type
8110 || parent->tag == DW_TAG_class_type
680b30c7 8111 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8112 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8113 || parent->tag == DW_TAG_enumeration_type
8114 || (cu->language == language_fortran
8115 && parent->tag == DW_TAG_subprogram
8116 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8117 {
8118 if (grandparent_scope == NULL)
7d00ffec 8119 parent->scope = parent->name (cu);
72bf9492 8120 else
3e43a32a
MS
8121 parent->scope = typename_concat (&cu->comp_unit_obstack,
8122 grandparent_scope,
7d00ffec 8123 parent->name (cu), 0, cu);
72bf9492 8124 }
72bf9492
DJ
8125 else
8126 {
8127 /* FIXME drow/2004-04-01: What should we be doing with
8128 function-local names? For partial symbols, we should probably be
8129 ignoring them. */
fa9c3fa0
TT
8130 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8131 dwarf_tag_name (parent->tag),
8132 sect_offset_str (pdi->sect_off));
72bf9492 8133 parent->scope = grandparent_scope;
c906108c
SS
8134 }
8135
72bf9492
DJ
8136 parent->scope_set = 1;
8137 return parent->scope;
8138}
8139
8140/* Return the fully scoped name associated with PDI, from compilation unit
8141 CU. The result will be allocated with malloc. */
4568ecf9 8142
43816ebc 8143static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8144partial_die_full_name (struct partial_die_info *pdi,
8145 struct dwarf2_cu *cu)
8146{
15d034d0 8147 const char *parent_scope;
72bf9492 8148
98bfdba5
PA
8149 /* If this is a template instantiation, we can not work out the
8150 template arguments from partial DIEs. So, unfortunately, we have
8151 to go through the full DIEs. At least any work we do building
8152 types here will be reused if full symbols are loaded later. */
8153 if (pdi->has_template_arguments)
8154 {
52356b79 8155 pdi->fixup (cu);
98bfdba5 8156
7d00ffec 8157 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8158 {
8159 struct die_info *die;
8160 struct attribute attr;
8161 struct dwarf2_cu *ref_cu = cu;
8162
b64f50a1 8163 /* DW_FORM_ref_addr is using section offset. */
b4069958 8164 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8165 attr.form = DW_FORM_ref_addr;
9c541725 8166 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8167 die = follow_die_ref (NULL, &attr, &ref_cu);
8168
43816ebc 8169 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8170 }
8171 }
8172
72bf9492
DJ
8173 parent_scope = partial_die_parent_scope (pdi, cu);
8174 if (parent_scope == NULL)
8175 return NULL;
8176 else
43816ebc 8177 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8178 pdi->name (cu),
8179 0, cu));
c906108c
SS
8180}
8181
8182static void
72bf9492 8183add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8184{
976ca316
SM
8185 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8186 struct objfile *objfile = per_objfile->objfile;
08feed99 8187 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8188 CORE_ADDR addr = 0;
15d034d0 8189 const char *actual_name = NULL;
e142c38c
DJ
8190 CORE_ADDR baseaddr;
8191
b3b3bada 8192 baseaddr = objfile->text_section_offset ();
c906108c 8193
43816ebc
TT
8194 gdb::unique_xmalloc_ptr<char> built_actual_name
8195 = partial_die_full_name (pdi, cu);
15d034d0 8196 if (built_actual_name != NULL)
43816ebc 8197 actual_name = built_actual_name.get ();
63d06c5c 8198
72bf9492 8199 if (actual_name == NULL)
7d00ffec 8200 actual_name = pdi->name (cu);
72bf9492 8201
76e288d1
TT
8202 partial_symbol psymbol;
8203 memset (&psymbol, 0, sizeof (psymbol));
8204 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 8205 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
8206
8207 /* The code below indicates that the psymbol should be installed by
8208 setting this. */
8209 gdb::optional<psymbol_placement> where;
8210
c906108c
SS
8211 switch (pdi->tag)
8212 {
b1dc1806 8213 case DW_TAG_inlined_subroutine:
c906108c 8214 case DW_TAG_subprogram:
79748972
TT
8215 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8216 - baseaddr);
0a4b0913
AB
8217 if (pdi->is_external
8218 || cu->language == language_ada
8219 || (cu->language == language_fortran
8220 && pdi->die_parent != NULL
8221 && pdi->die_parent->tag == DW_TAG_subprogram))
8222 {
dda83cd7
SM
8223 /* Normally, only "external" DIEs are part of the global scope.
8224 But in Ada and Fortran, we want to be able to access nested
8225 procedures globally. So all Ada and Fortran subprograms are
8226 stored in the global scope. */
76e288d1 8227 where = psymbol_placement::GLOBAL;
c906108c
SS
8228 }
8229 else
76e288d1
TT
8230 where = psymbol_placement::STATIC;
8231
8232 psymbol.domain = VAR_DOMAIN;
8233 psymbol.aclass = LOC_BLOCK;
a52d653e 8234 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 8235 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8236
8237 if (pdi->main_subprogram && actual_name != NULL)
8238 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8239 break;
72929c62 8240 case DW_TAG_constant:
76e288d1
TT
8241 psymbol.domain = VAR_DOMAIN;
8242 psymbol.aclass = LOC_STATIC;
8243 where = (pdi->is_external
8244 ? psymbol_placement::GLOBAL
8245 : psymbol_placement::STATIC);
72929c62 8246 break;
c906108c 8247 case DW_TAG_variable:
95554aad
TT
8248 if (pdi->d.locdesc)
8249 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8250
95554aad 8251 if (pdi->d.locdesc
caac4577 8252 && addr == 0
976ca316 8253 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8254 {
8255 /* A global or static variable may also have been stripped
8256 out by the linker if unused, in which case its address
8257 will be nullified; do not add such variables into partial
8258 symbol table then. */
8259 }
8260 else if (pdi->is_external)
c906108c
SS
8261 {
8262 /* Global Variable.
8263 Don't enter into the minimal symbol tables as there is
8264 a minimal symbol table entry from the ELF symbols already.
8265 Enter into partial symbol table if it has a location
8266 descriptor or a type.
8267 If the location descriptor is missing, new_symbol will create
8268 a LOC_UNRESOLVED symbol, the address of the variable will then
8269 be determined from the minimal symbol table whenever the variable
8270 is referenced.
8271 The address for the partial symbol table entry is not
8272 used by GDB, but it comes in handy for debugging partial symbol
8273 table building. */
8274
95554aad 8275 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8276 {
8277 psymbol.domain = VAR_DOMAIN;
8278 psymbol.aclass = LOC_STATIC;
a52d653e 8279 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8280 psymbol.ginfo.value.address = addr;
8281 where = psymbol_placement::GLOBAL;
8282 }
c906108c
SS
8283 }
8284 else
8285 {
ff908ebf
AW
8286 int has_loc = pdi->d.locdesc != NULL;
8287
8288 /* Static Variable. Skip symbols whose value we cannot know (those
8289 without location descriptors or constant values). */
8290 if (!has_loc && !pdi->has_const_value)
43816ebc 8291 return;
ff908ebf 8292
76e288d1
TT
8293 psymbol.domain = VAR_DOMAIN;
8294 psymbol.aclass = LOC_STATIC;
a52d653e 8295 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8296 if (has_loc)
8297 psymbol.ginfo.value.address = addr;
8298 where = psymbol_placement::STATIC;
c906108c
SS
8299 }
8300 break;
d8f62e84 8301 case DW_TAG_array_type:
c906108c
SS
8302 case DW_TAG_typedef:
8303 case DW_TAG_base_type:
a02abb62 8304 case DW_TAG_subrange_type:
76e288d1
TT
8305 psymbol.domain = VAR_DOMAIN;
8306 psymbol.aclass = LOC_TYPEDEF;
8307 where = psymbol_placement::STATIC;
c906108c 8308 break;
74921315 8309 case DW_TAG_imported_declaration:
72bf9492 8310 case DW_TAG_namespace:
76e288d1
TT
8311 psymbol.domain = VAR_DOMAIN;
8312 psymbol.aclass = LOC_TYPEDEF;
8313 where = psymbol_placement::GLOBAL;
72bf9492 8314 break;
530e8392 8315 case DW_TAG_module:
a5fd13a9 8316 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8317 available without any name. If so, we skip the module as it
8318 doesn't bring any value. */
a5fd13a9 8319 if (actual_name != nullptr)
76e288d1
TT
8320 {
8321 psymbol.domain = MODULE_DOMAIN;
8322 psymbol.aclass = LOC_TYPEDEF;
8323 where = psymbol_placement::GLOBAL;
8324 }
530e8392 8325 break;
c906108c 8326 case DW_TAG_class_type:
680b30c7 8327 case DW_TAG_interface_type:
c906108c
SS
8328 case DW_TAG_structure_type:
8329 case DW_TAG_union_type:
8330 case DW_TAG_enumeration_type:
fa4028e9 8331 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8332 about "Structure, Union, and Class Type Entries": "An incomplete
8333 structure, union or class type is represented by a structure,
8334 union or class entry that does not have a byte size attribute
8335 and that has a DW_AT_declaration attribute." */
fa4028e9 8336 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8337 return;
fa4028e9 8338
63d06c5c
DC
8339 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8340 static vs. global. */
76e288d1
TT
8341 psymbol.domain = STRUCT_DOMAIN;
8342 psymbol.aclass = LOC_TYPEDEF;
8343 where = (cu->language == language_cplus
8344 ? psymbol_placement::GLOBAL
8345 : psymbol_placement::STATIC);
c906108c
SS
8346 break;
8347 case DW_TAG_enumerator:
76e288d1
TT
8348 psymbol.domain = VAR_DOMAIN;
8349 psymbol.aclass = LOC_CONST;
8350 where = (cu->language == language_cplus
8351 ? psymbol_placement::GLOBAL
8352 : psymbol_placement::STATIC);
c906108c
SS
8353 break;
8354 default:
8355 break;
8356 }
76e288d1
TT
8357
8358 if (where.has_value ())
8359 {
f049a313
TT
8360 if (built_actual_name != nullptr)
8361 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8362 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8363 psymbol.ginfo.set_linkage_name (actual_name);
8364 else
8365 {
8366 psymbol.ginfo.set_demangled_name (actual_name,
8367 &objfile->objfile_obstack);
8368 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8369 }
7b249e47
TT
8370 cu->per_cu->v.psymtab->add_psymbol
8371 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8372 objfile);
76e288d1 8373 }
c906108c
SS
8374}
8375
5c4e30ca
DC
8376/* Read a partial die corresponding to a namespace; also, add a symbol
8377 corresponding to that namespace to the symbol table. NAMESPACE is
8378 the name of the enclosing namespace. */
91c24f0a 8379
72bf9492
DJ
8380static void
8381add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8382 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8383 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8384{
72bf9492 8385 /* Add a symbol for the namespace. */
e7c27a73 8386
72bf9492 8387 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8388
8389 /* Now scan partial symbols in that namespace. */
8390
91c24f0a 8391 if (pdi->has_children)
cdc07690 8392 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8393}
8394
5d7cb8df
JK
8395/* Read a partial die corresponding to a Fortran module. */
8396
8397static void
8398add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8399 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8400{
530e8392
KB
8401 /* Add a symbol for the namespace. */
8402
8403 add_partial_symbol (pdi, cu);
8404
f55ee35c 8405 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8406
8407 if (pdi->has_children)
cdc07690 8408 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8409}
8410
b1dc1806
XR
8411/* Read a partial die corresponding to a subprogram or an inlined
8412 subprogram and create a partial symbol for that subprogram.
8413 When the CU language allows it, this routine also defines a partial
8414 symbol for each nested subprogram that this subprogram contains.
8415 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8416 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8417
cdc07690
YQ
8418 PDI may also be a lexical block, in which case we simply search
8419 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8420 Again, this is only performed when the CU language allows this
8421 type of definitions. */
8422
8423static void
8424add_partial_subprogram (struct partial_die_info *pdi,
8425 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8426 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8427{
b1dc1806 8428 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8429 {
8430 if (pdi->has_pc_info)
dda83cd7
SM
8431 {
8432 if (pdi->lowpc < *lowpc)
8433 *lowpc = pdi->lowpc;
8434 if (pdi->highpc > *highpc)
8435 *highpc = pdi->highpc;
cdc07690 8436 if (set_addrmap)
5734ee8b 8437 {
5e22e966 8438 struct objfile *objfile = cu->per_objfile->objfile;
84685904 8439 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 8440 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8441 CORE_ADDR baseaddr;
b926417a
TT
8442 CORE_ADDR this_highpc;
8443 CORE_ADDR this_lowpc;
5734ee8b 8444
b3b3bada 8445 baseaddr = objfile->text_section_offset ();
b926417a
TT
8446 this_lowpc
8447 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8448 pdi->lowpc + baseaddr)
8449 - baseaddr);
8450 this_highpc
8451 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8452 pdi->highpc + baseaddr)
8453 - baseaddr);
84685904 8454 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
b926417a 8455 this_lowpc, this_highpc - 1,
9291a0cd 8456 cu->per_cu->v.psymtab);
5734ee8b 8457 }
dda83cd7 8458 }
481860b3
GB
8459
8460 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8461 {
dda83cd7 8462 if (!pdi->is_declaration)
e8d05480
JB
8463 /* Ignore subprogram DIEs that do not have a name, they are
8464 illegal. Do not emit a complaint at this point, we will
8465 do so when we convert this psymtab into a symtab. */
7d00ffec 8466 if (pdi->name (cu))
e8d05480 8467 add_partial_symbol (pdi, cu);
dda83cd7 8468 }
bc30ff58 8469 }
6e70227d 8470
bc30ff58
JB
8471 if (! pdi->has_children)
8472 return;
8473
0a4b0913 8474 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8475 {
8476 pdi = pdi->die_child;
8477 while (pdi != NULL)
8478 {
52356b79 8479 pdi->fixup (cu);
bc30ff58 8480 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8481 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8482 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8483 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8484 pdi = pdi->die_sibling;
8485 }
8486 }
8487}
8488
91c24f0a
DC
8489/* Read a partial die corresponding to an enumeration type. */
8490
72bf9492
DJ
8491static void
8492add_partial_enumeration (struct partial_die_info *enum_pdi,
8493 struct dwarf2_cu *cu)
91c24f0a 8494{
72bf9492 8495 struct partial_die_info *pdi;
91c24f0a 8496
7d00ffec 8497 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8498 add_partial_symbol (enum_pdi, cu);
8499
8500 pdi = enum_pdi->die_child;
8501 while (pdi)
91c24f0a 8502 {
7d00ffec 8503 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8504 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8505 else
72bf9492
DJ
8506 add_partial_symbol (pdi, cu);
8507 pdi = pdi->die_sibling;
91c24f0a 8508 }
91c24f0a
DC
8509}
8510
6caca83c
CC
8511/* Return the initial uleb128 in the die at INFO_PTR. */
8512
8513static unsigned int
d521ce57 8514peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8515{
8516 unsigned int bytes_read;
8517
8518 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8519}
8520
685af9cd
TT
8521/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8522 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8523
4bb7a0a7
DJ
8524 Return the corresponding abbrev, or NULL if the number is zero (indicating
8525 an empty DIE). In either case *BYTES_READ will be set to the length of
8526 the initial number. */
8527
7c32eebb 8528static const struct abbrev_info *
685af9cd
TT
8529peek_die_abbrev (const die_reader_specs &reader,
8530 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8531{
685af9cd 8532 dwarf2_cu *cu = reader.cu;
0280fdcc 8533 bfd *abfd = reader.abfd;
685af9cd
TT
8534 unsigned int abbrev_number
8535 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8536
8537 if (abbrev_number == 0)
8538 return NULL;
8539
7c32eebb
TT
8540 const abbrev_info *abbrev
8541 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8542 if (!abbrev)
8543 {
422b9917 8544 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8545 " at offset %s [in module %s]"),
422b9917 8546 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8547 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8548 }
8549
8550 return abbrev;
8551}
8552
93311388
DE
8553/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8554 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8555 DIE. Any children of the skipped DIEs will also be skipped. */
8556
d521ce57
TT
8557static const gdb_byte *
8558skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8559{
4bb7a0a7
DJ
8560 while (1)
8561 {
685af9cd 8562 unsigned int bytes_read;
7c32eebb
TT
8563 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8564 &bytes_read);
685af9cd 8565
4bb7a0a7
DJ
8566 if (abbrev == NULL)
8567 return info_ptr + bytes_read;
8568 else
dee91e82 8569 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8570 }
8571}
8572
93311388
DE
8573/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8574 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8575 abbrev corresponding to that skipped uleb128 should be passed in
8576 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8577 children. */
8578
d521ce57
TT
8579static const gdb_byte *
8580skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8581 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8582{
8583 unsigned int bytes_read;
8584 struct attribute attr;
dee91e82
DE
8585 bfd *abfd = reader->abfd;
8586 struct dwarf2_cu *cu = reader->cu;
d521ce57 8587 const gdb_byte *buffer = reader->buffer;
f664829e 8588 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8589 unsigned int form, i;
8590
8591 for (i = 0; i < abbrev->num_attrs; i++)
8592 {
8593 /* The only abbrev we care about is DW_AT_sibling. */
8594 if (abbrev->attrs[i].name == DW_AT_sibling)
8595 {
7a5f294d 8596 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8597 if (attr.form == DW_FORM_ref_addr)
b98664d3 8598 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8599 else
b9502d3f 8600 {
0826b30a 8601 sect_offset off = attr.get_ref_die_offset ();
9c541725 8602 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8603
8604 if (sibling_ptr < info_ptr)
b98664d3 8605 complaint (_("DW_AT_sibling points backwards"));
22869d73 8606 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8607 reader->die_section->overflow_complaint ();
b9502d3f
WN
8608 else
8609 return sibling_ptr;
8610 }
4bb7a0a7
DJ
8611 }
8612
8613 /* If it isn't DW_AT_sibling, skip this attribute. */
8614 form = abbrev->attrs[i].form;
8615 skip_attribute:
8616 switch (form)
8617 {
4bb7a0a7 8618 case DW_FORM_ref_addr:
ae411497
TT
8619 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8620 and later it is offset sized. */
8621 if (cu->header.version == 2)
8622 info_ptr += cu->header.addr_size;
8623 else
8624 info_ptr += cu->header.offset_size;
8625 break;
36586728
TT
8626 case DW_FORM_GNU_ref_alt:
8627 info_ptr += cu->header.offset_size;
8628 break;
ae411497 8629 case DW_FORM_addr:
4bb7a0a7
DJ
8630 info_ptr += cu->header.addr_size;
8631 break;
8632 case DW_FORM_data1:
8633 case DW_FORM_ref1:
8634 case DW_FORM_flag:
8fe0f950 8635 case DW_FORM_strx1:
4bb7a0a7
DJ
8636 info_ptr += 1;
8637 break;
2dc7f7b3 8638 case DW_FORM_flag_present:
43988095 8639 case DW_FORM_implicit_const:
2dc7f7b3 8640 break;
4bb7a0a7
DJ
8641 case DW_FORM_data2:
8642 case DW_FORM_ref2:
8fe0f950 8643 case DW_FORM_strx2:
4bb7a0a7
DJ
8644 info_ptr += 2;
8645 break;
8fe0f950
AT
8646 case DW_FORM_strx3:
8647 info_ptr += 3;
8648 break;
4bb7a0a7
DJ
8649 case DW_FORM_data4:
8650 case DW_FORM_ref4:
8fe0f950 8651 case DW_FORM_strx4:
4bb7a0a7
DJ
8652 info_ptr += 4;
8653 break;
8654 case DW_FORM_data8:
8655 case DW_FORM_ref8:
55f1336d 8656 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8657 info_ptr += 8;
8658 break;
0224619f
JK
8659 case DW_FORM_data16:
8660 info_ptr += 16;
8661 break;
4bb7a0a7 8662 case DW_FORM_string:
9b1c24c8 8663 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8664 info_ptr += bytes_read;
8665 break;
2dc7f7b3 8666 case DW_FORM_sec_offset:
4bb7a0a7 8667 case DW_FORM_strp:
36586728 8668 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8669 info_ptr += cu->header.offset_size;
8670 break;
2dc7f7b3 8671 case DW_FORM_exprloc:
4bb7a0a7
DJ
8672 case DW_FORM_block:
8673 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8674 info_ptr += bytes_read;
8675 break;
8676 case DW_FORM_block1:
8677 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8678 break;
8679 case DW_FORM_block2:
8680 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8681 break;
8682 case DW_FORM_block4:
8683 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8684 break;
336d760d 8685 case DW_FORM_addrx:
cf532bd1 8686 case DW_FORM_strx:
4bb7a0a7
DJ
8687 case DW_FORM_sdata:
8688 case DW_FORM_udata:
8689 case DW_FORM_ref_udata:
3019eac3
DE
8690 case DW_FORM_GNU_addr_index:
8691 case DW_FORM_GNU_str_index:
18a8505e 8692 case DW_FORM_rnglistx:
41144253 8693 case DW_FORM_loclistx:
d521ce57 8694 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8695 break;
8696 case DW_FORM_indirect:
8697 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8698 info_ptr += bytes_read;
8699 /* We need to continue parsing from here, so just go back to
8700 the top. */
8701 goto skip_attribute;
8702
8703 default:
3e43a32a
MS
8704 error (_("Dwarf Error: Cannot handle %s "
8705 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8706 dwarf_form_name (form),
8707 bfd_get_filename (abfd));
8708 }
8709 }
8710
8711 if (abbrev->has_children)
dee91e82 8712 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8713 else
8714 return info_ptr;
8715}
8716
93311388 8717/* Locate ORIG_PDI's sibling.
dee91e82 8718 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8719
d521ce57 8720static const gdb_byte *
dee91e82
DE
8721locate_pdi_sibling (const struct die_reader_specs *reader,
8722 struct partial_die_info *orig_pdi,
d521ce57 8723 const gdb_byte *info_ptr)
91c24f0a
DC
8724{
8725 /* Do we know the sibling already? */
72bf9492 8726
91c24f0a
DC
8727 if (orig_pdi->sibling)
8728 return orig_pdi->sibling;
8729
8730 /* Are there any children to deal with? */
8731
8732 if (!orig_pdi->has_children)
8733 return info_ptr;
8734
4bb7a0a7 8735 /* Skip the children the long way. */
91c24f0a 8736
dee91e82 8737 return skip_children (reader, info_ptr);
91c24f0a
DC
8738}
8739
257e7a09 8740/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8741 not NULL. */
c906108c 8742
891813be
TT
8743void
8744dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8745{
976ca316 8746 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8747
976ca316 8748 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8749
077cbab2
TT
8750 /* If this psymtab is constructed from a debug-only objfile, the
8751 has_section_at_zero flag will not necessarily be correct. We
8752 can get the correct value for this flag by looking at the data
8753 associated with the (presumably stripped) associated objfile. */
8754 if (objfile->separate_debug_objfile_backlink)
c906108c 8755 {
976ca316 8756 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8757 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8758
976ca316
SM
8759 per_objfile->per_bfd->has_section_at_zero
8760 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8761 }
98bfdba5 8762
8566b89b 8763 expand_psymtab (objfile);
95554aad 8764
976ca316 8765 process_cu_includes (per_objfile);
c906108c 8766}
9cdd5dbd
DE
8767\f
8768/* Reading in full CUs. */
c906108c 8769
10b3939b
DJ
8770/* Add PER_CU to the queue. */
8771
8772static void
120ce1b5
SM
8773queue_comp_unit (dwarf2_per_cu_data *per_cu,
8774 dwarf2_per_objfile *per_objfile,
95554aad 8775 enum language pretend_language)
10b3939b 8776{
10b3939b 8777 per_cu->queued = 1;
08ac5771
SM
8778
8779 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8780 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8781}
8782
616c069a
SM
8783/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8784
89e63ee4
DE
8785 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8786 dependency.
69d751e3 8787
616c069a
SM
8788 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8789 DIEs, false otherwise.
8790
8791 Explanation: there is an invariant that if a CU is queued for expansion
8792 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8793 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8794 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8795 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8796 invariant is respected.
8797
8798 The caller is therefore not required to load the CU's DIEs (we return false)
8799 if:
8800
8801 - the CU is already expanded, and therefore does not get enqueued
8802 - the CU gets enqueued for expansion, but its DIEs are already loaded
8803
8804 Note that the caller should not use this function's return value as an
8805 indicator of whether the CU's DIEs are loaded right now, it should check
8806 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
8807
8808static int
89e63ee4 8809maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8810 dwarf2_per_cu_data *per_cu,
8811 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8812 enum language pretend_language)
8813{
8814 /* We may arrive here during partial symbol reading, if we need full
8815 DIEs to process an unusual case (e.g. template arguments). Do
8816 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8817 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 8818 {
7188ed02
SM
8819 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8820
8821 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
8822 return 1;
8823 return 0;
8824 }
8825
8826 /* Mark the dependence relation so that we don't flush PER_CU
8827 too early. */
89e63ee4
DE
8828 if (dependent_cu != NULL)
8829 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8830
8831 /* If it's already on the queue, we have nothing to do. */
8832 if (per_cu->queued)
de53369b
SM
8833 {
8834 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8835 loaded. */
8836 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
8837
8838 /* If the CU is queued for expansion, it should not already be
8839 expanded. */
8840 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8841
8842 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
8843 return 0;
8844 }
0907af0c 8845
616c069a
SM
8846 bool queued = false;
8847 if (!per_objfile->symtab_set_p (per_cu))
8848 {
8849 /* Add it to the queue. */
8850 queue_comp_unit (per_cu, per_objfile, pretend_language);
8851 queued = true;
8852 }
8853
0907af0c
DE
8854 /* If the compilation unit is already loaded, just mark it as
8855 used. */
7188ed02
SM
8856 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8857 if (cu != nullptr)
616c069a 8858 cu->last_used = 0;
0907af0c 8859
616c069a
SM
8860 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8861 and the DIEs are not already loaded. */
8862 return queued && cu == nullptr;
0907af0c
DE
8863}
8864
10b3939b
DJ
8865/* Process the queue. */
8866
8867static void
976ca316 8868process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 8869{
6f738b01
SM
8870 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8871 objfile_name (per_objfile->objfile));
45cfd468 8872
03dd20cc
DJ
8873 /* The queue starts out with one item, but following a DIE reference
8874 may load a new CU, adding it to the end of the queue. */
08ac5771 8875 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 8876 {
08ac5771 8877 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 8878 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 8879
976ca316 8880 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 8881 {
976ca316 8882 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 8883
7188ed02
SM
8884 /* Skip dummy CUs. */
8885 if (cu != nullptr)
73be47f5 8886 {
7188ed02
SM
8887 unsigned int debug_print_threshold;
8888 char buf[100];
8889
8890 if (per_cu->is_debug_types)
8891 {
8892 struct signatured_type *sig_type =
8893 (struct signatured_type *) per_cu;
8894
8895 sprintf (buf, "TU %s at offset %s",
8896 hex_string (sig_type->signature),
8897 sect_offset_str (per_cu->sect_off));
8898 /* There can be 100s of TUs.
8899 Only print them in verbose mode. */
8900 debug_print_threshold = 2;
8901 }
8902 else
8903 {
8904 sprintf (buf, "CU at offset %s",
8905 sect_offset_str (per_cu->sect_off));
8906 debug_print_threshold = 1;
8907 }
247f5c4f 8908
7188ed02 8909 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8910 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 8911
7188ed02
SM
8912 if (per_cu->is_debug_types)
8913 process_full_type_unit (cu, item.pretend_language);
8914 else
8915 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 8916
7188ed02 8917 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 8918 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 8919 }
f4dc4d17 8920 }
10b3939b 8921
7188ed02 8922 per_cu->queued = 0;
08ac5771 8923 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
8924 }
8925
6f738b01
SM
8926 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8927 objfile_name (per_objfile->objfile));
10b3939b
DJ
8928}
8929
10b3939b
DJ
8930/* Read in full symbols for PST, and anything it depends on. */
8931
8566b89b
TT
8932void
8933dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8934{
af758d11 8935 gdb_assert (!readin_p (objfile));
95554aad 8936
17ee85fc
TT
8937 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8938 free_cached_comp_units freer (per_objfile);
48993951 8939 expand_dependencies (objfile);
aaa75496 8940
97a1449a 8941 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 8942 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
8943}
8944
af758d11
SM
8945/* See psympriv.h. */
8946
8947bool
8948dwarf2_psymtab::readin_p (struct objfile *objfile) const
8949{
8950 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8951 return per_objfile->symtab_set_p (per_cu_data);
8952}
8953
8954/* See psympriv.h. */
8955
8956compunit_symtab *
8957dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8958{
8959 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8960 return per_objfile->get_symtab (per_cu_data);
8961}
8962
dee91e82
DE
8963/* Trivial hash function for die_info: the hash value of a DIE
8964 is its offset in .debug_info for this objfile. */
10b3939b 8965
dee91e82
DE
8966static hashval_t
8967die_hash (const void *item)
10b3939b 8968{
9a3c8263 8969 const struct die_info *die = (const struct die_info *) item;
6502dd73 8970
9c541725 8971 return to_underlying (die->sect_off);
dee91e82 8972}
63d06c5c 8973
dee91e82
DE
8974/* Trivial comparison function for die_info structures: two DIEs
8975 are equal if they have the same offset. */
98bfdba5 8976
dee91e82
DE
8977static int
8978die_eq (const void *item_lhs, const void *item_rhs)
8979{
9a3c8263
SM
8980 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8981 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8982
9c541725 8983 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8984}
c906108c 8985
4a636814
SM
8986/* Load the DIEs associated with PER_CU into memory.
8987
8988 In some cases, the caller, while reading partial symbols, will need to load
8989 the full symbols for the CU for some reason. It will already have a
8990 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8991 rather than creating a new one. */
c906108c 8992
dee91e82 8993static void
ab432490
SM
8994load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8995 dwarf2_per_objfile *per_objfile,
4a636814 8996 dwarf2_cu *existing_cu,
c0ab21c2
TT
8997 bool skip_partial,
8998 enum language pretend_language)
dee91e82 8999{
c0ab21c2
TT
9000 gdb_assert (! this_cu->is_debug_types);
9001
7188ed02 9002 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9003 if (reader.dummy_p)
9004 return;
9005
9006 struct dwarf2_cu *cu = reader.cu;
9007 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9008
dee91e82
DE
9009 gdb_assert (cu->die_hash == NULL);
9010 cu->die_hash =
9011 htab_create_alloc_ex (cu->header.length / 12,
9012 die_hash,
9013 die_eq,
9014 NULL,
9015 &cu->comp_unit_obstack,
9016 hashtab_obstack_allocate,
9017 dummy_obstack_deallocate);
e142c38c 9018
3e225074 9019 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9020 reader.comp_unit_die->child
9021 = read_die_and_siblings (&reader, reader.info_ptr,
9022 &info_ptr, reader.comp_unit_die);
9023 cu->dies = reader.comp_unit_die;
dee91e82 9024 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9025
9026 /* We try not to read any attributes in this function, because not
9cdd5dbd 9027 all CUs needed for references have been loaded yet, and symbol
10b3939b 9028 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9029 or we won't be able to build types correctly.
9030 Similarly, if we do not read the producer, we can not apply
9031 producer-specific interpretation. */
c0ab21c2 9032 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9033
9034 reader.keep ();
10b3939b
DJ
9035}
9036
3da10d80
KS
9037/* Add a DIE to the delayed physname list. */
9038
9039static void
9040add_to_method_list (struct type *type, int fnfield_index, int index,
9041 const char *name, struct die_info *die,
9042 struct dwarf2_cu *cu)
9043{
9044 struct delayed_method_info mi;
9045 mi.type = type;
9046 mi.fnfield_index = fnfield_index;
9047 mi.index = index;
9048 mi.name = name;
9049 mi.die = die;
c89b44cd 9050 cu->method_list.push_back (mi);
3da10d80
KS
9051}
9052
3693fdb3
PA
9053/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9054 "const" / "volatile". If so, decrements LEN by the length of the
9055 modifier and return true. Otherwise return false. */
9056
9057template<size_t N>
9058static bool
9059check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9060{
9061 size_t mod_len = sizeof (mod) - 1;
9062 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9063 {
9064 len -= mod_len;
9065 return true;
9066 }
9067 return false;
9068}
9069
3da10d80
KS
9070/* Compute the physnames of any methods on the CU's method list.
9071
9072 The computation of method physnames is delayed in order to avoid the
9073 (bad) condition that one of the method's formal parameters is of an as yet
9074 incomplete type. */
9075
9076static void
9077compute_delayed_physnames (struct dwarf2_cu *cu)
9078{
3693fdb3 9079 /* Only C++ delays computing physnames. */
c89b44cd 9080 if (cu->method_list.empty ())
3693fdb3
PA
9081 return;
9082 gdb_assert (cu->language == language_cplus);
9083
52941706 9084 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9085 {
1d06ead6 9086 const char *physname;
3da10d80 9087 struct fn_fieldlist *fn_flp
c89b44cd
TT
9088 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9089 physname = dwarf2_physname (mi.name, mi.die, cu);
9090 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9091 = physname ? physname : "";
3693fdb3
PA
9092
9093 /* Since there's no tag to indicate whether a method is a
9094 const/volatile overload, extract that information out of the
9095 demangled name. */
9096 if (physname != NULL)
9097 {
9098 size_t len = strlen (physname);
9099
9100 while (1)
9101 {
9102 if (physname[len] == ')') /* shortcut */
9103 break;
9104 else if (check_modifier (physname, len, " const"))
c89b44cd 9105 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9106 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9107 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9108 else
9109 break;
9110 }
9111 }
3da10d80 9112 }
c89b44cd
TT
9113
9114 /* The list is no longer needed. */
9115 cu->method_list.clear ();
3da10d80
KS
9116}
9117
a766d390
DE
9118/* Go objects should be embedded in a DW_TAG_module DIE,
9119 and it's not clear if/how imported objects will appear.
9120 To keep Go support simple until that's worked out,
9121 go back through what we've read and create something usable.
9122 We could do this while processing each DIE, and feels kinda cleaner,
9123 but that way is more invasive.
9124 This is to, for example, allow the user to type "p var" or "b main"
9125 without having to specify the package name, and allow lookups
9126 of module.object to work in contexts that use the expression
9127 parser. */
9128
9129static void
9130fixup_go_packaging (struct dwarf2_cu *cu)
9131{
421d1616 9132 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9133 struct pending *list;
9134 int i;
9135
c24bdb02 9136 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9137 list != NULL;
9138 list = list->next)
a766d390
DE
9139 {
9140 for (i = 0; i < list->nsyms; ++i)
9141 {
9142 struct symbol *sym = list->symbol[i];
9143
c1b5c1eb 9144 if (sym->language () == language_go
a766d390
DE
9145 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9146 {
421d1616
TT
9147 gdb::unique_xmalloc_ptr<char> this_package_name
9148 (go_symbol_package_name (sym));
a766d390
DE
9149
9150 if (this_package_name == NULL)
9151 continue;
9152 if (package_name == NULL)
421d1616 9153 package_name = std::move (this_package_name);
a766d390
DE
9154 else
9155 {
5e22e966 9156 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9157 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9158 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9159 (symbol_symtab (sym) != NULL
9160 ? symtab_to_filename_for_display
9161 (symbol_symtab (sym))
e3b94546 9162 : objfile_name (objfile)),
421d1616 9163 this_package_name.get (), package_name.get ());
a766d390
DE
9164 }
9165 }
9166 }
9167 }
9168
9169 if (package_name != NULL)
9170 {
5e22e966 9171 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9172 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9173 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9174 saved_package_name);
a766d390
DE
9175 struct symbol *sym;
9176
8c14c3a3 9177 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9178 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9179 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9180 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9181 e.g., "main" finds the "main" module and not C's main(). */
9182 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9183 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9184 SYMBOL_TYPE (sym) = type;
9185
c24bdb02 9186 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9187 }
9188}
9189
c9317f21
TT
9190/* Allocate a fully-qualified name consisting of the two parts on the
9191 obstack. */
9192
9193static const char *
9194rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9195{
9196 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9197}
9198
9c6a1327
TT
9199/* A helper that allocates a variant part to attach to a Rust enum
9200 type. OBSTACK is where the results should be allocated. TYPE is
9201 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9202 discriminant. It must be the index of one of the fields of TYPE,
9203 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9204 DEFAULT_INDEX is the index of the default field; or -1 if there is
9205 no default. RANGES is indexed by "effective" field number (the
9206 field index, but omitting the discriminant and default fields) and
9207 must hold the discriminant values used by the variants. Note that
9208 RANGES must have a lifetime at least as long as OBSTACK -- either
9209 already allocated on it, or static. */
c9317f21 9210
9c6a1327
TT
9211static void
9212alloc_rust_variant (struct obstack *obstack, struct type *type,
9213 int discriminant_index, int default_index,
9214 gdb::array_view<discriminant_range> ranges)
9215{
57d02173
TT
9216 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9217 gdb_assert (discriminant_index == -1
9218 || (discriminant_index >= 0
9219 && discriminant_index < type->num_fields ()));
c9317f21 9220 gdb_assert (default_index == -1
1f704f76 9221 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9222
9c6a1327 9223 /* We have one variant for each non-discriminant field. */
57d02173
TT
9224 int n_variants = type->num_fields ();
9225 if (discriminant_index != -1)
9226 --n_variants;
c9317f21 9227
9c6a1327
TT
9228 variant *variants = new (obstack) variant[n_variants];
9229 int var_idx = 0;
9230 int range_idx = 0;
1f704f76 9231 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9232 {
9233 if (i == discriminant_index)
9234 continue;
c9317f21 9235
9c6a1327
TT
9236 variants[var_idx].first_field = i;
9237 variants[var_idx].last_field = i + 1;
9238
9239 /* The default field does not need a range, but other fields do.
9240 We skipped the discriminant above. */
9241 if (i != default_index)
9242 {
9243 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9244 ++range_idx;
9245 }
c9317f21 9246
9c6a1327
TT
9247 ++var_idx;
9248 }
9249
9250 gdb_assert (range_idx == ranges.size ());
9251 gdb_assert (var_idx == n_variants);
9252
9253 variant_part *part = new (obstack) variant_part;
9254 part->discriminant_index = discriminant_index;
57d02173
TT
9255 /* If there is no discriminant, then whether it is signed is of no
9256 consequence. */
9257 part->is_unsigned
9258 = (discriminant_index == -1
9259 ? false
c6d940a9 9260 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9261 part->variants = gdb::array_view<variant> (variants, n_variants);
9262
9263 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9264 gdb::array_view<variant_part> *prop_value
9265 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9266
9c6a1327 9267 struct dynamic_prop prop;
8c2e4e06 9268 prop.set_variant_parts (prop_value);
9c6a1327 9269
5c54719c 9270 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9271}
9272
9273/* Some versions of rustc emitted enums in an unusual way.
9274
9275 Ordinary enums were emitted as unions. The first element of each
9276 structure in the union was named "RUST$ENUM$DISR". This element
9277 held the discriminant.
9278
9279 These versions of Rust also implemented the "non-zero"
9280 optimization. When the enum had two values, and one is empty and
9281 the other holds a pointer that cannot be zero, the pointer is used
9282 as the discriminant, with a zero value meaning the empty variant.
9283 Here, the union's first member is of the form
9284 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9285 where the fieldnos are the indices of the fields that should be
9286 traversed in order to find the field (which may be several fields deep)
9287 and the variantname is the name of the variant of the case when the
9288 field is zero.
9289
9290 This function recognizes whether TYPE is of one of these forms,
9291 and, if so, smashes it to be a variant type. */
9292
9293static void
9294quirk_rust_enum (struct type *type, struct objfile *objfile)
9295{
78134374 9296 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9297
9298 /* We don't need to deal with empty enums. */
1f704f76 9299 if (type->num_fields () == 0)
c9317f21
TT
9300 return;
9301
9302#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9303 if (type->num_fields () == 1
c9317f21
TT
9304 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9305 {
9306 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9307
9308 /* Decode the field name to find the offset of the
9309 discriminant. */
9310 ULONGEST bit_offset = 0;
940da03e 9311 struct type *field_type = type->field (0).type ();
c9317f21
TT
9312 while (name[0] >= '0' && name[0] <= '9')
9313 {
9314 char *tail;
9315 unsigned long index = strtoul (name, &tail, 10);
9316 name = tail;
9317 if (*name != '$'
1f704f76 9318 || index >= field_type->num_fields ()
c9317f21
TT
9319 || (TYPE_FIELD_LOC_KIND (field_type, index)
9320 != FIELD_LOC_KIND_BITPOS))
9321 {
b98664d3 9322 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9323 "[in module %s]"),
9324 TYPE_FIELD_NAME (type, 0),
9325 objfile_name (objfile));
9326 return;
9327 }
9328 ++name;
9329
9330 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9331 field_type = field_type->field (index).type ();
c9317f21
TT
9332 }
9333
9c6a1327
TT
9334 /* Smash this type to be a structure type. We have to do this
9335 because the type has already been recorded. */
67607e24 9336 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9337 type->set_num_fields (3);
9c6a1327 9338 /* Save the field we care about. */
ceacbf6e 9339 struct field saved_field = type->field (0);
3cabb6b0
SM
9340 type->set_fields
9341 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9342
9c6a1327 9343 /* Put the discriminant at index 0. */
5d14b6e5 9344 type->field (0).set_type (field_type);
9c6a1327
TT
9345 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9346 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9347 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9348
9349 /* The order of fields doesn't really matter, so put the real
9350 field at index 1 and the data-less field at index 2. */
ceacbf6e 9351 type->field (1) = saved_field;
9c6a1327 9352 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9353 = rust_last_path_segment (type->field (1).type ()->name ());
9354 type->field (1).type ()->set_name
7d93a1e0 9355 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9356 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9357
9358 const char *dataless_name
7d93a1e0 9359 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9360 name);
9361 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9362 dataless_name);
5d14b6e5 9363 type->field (2).set_type (dataless_type);
c9317f21
TT
9364 /* NAME points into the original discriminant name, which
9365 already has the correct lifetime. */
9c6a1327 9366 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9367 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9368
9c6a1327
TT
9369 /* Indicate that this is a variant type. */
9370 static discriminant_range ranges[1] = { { 0, 0 } };
9371 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9372 }
77c2dba3
TT
9373 /* A union with a single anonymous field is probably an old-style
9374 univariant enum. */
1f704f76 9375 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9376 {
c9317f21
TT
9377 /* Smash this type to be a structure type. We have to do this
9378 because the type has already been recorded. */
67607e24 9379 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9380
940da03e 9381 struct type *field_type = type->field (0).type ();
c9317f21 9382 const char *variant_name
7d93a1e0 9383 = rust_last_path_segment (field_type->name ());
9c6a1327 9384 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9385 field_type->set_name
9386 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9387 type->name (), variant_name));
57d02173
TT
9388
9389 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9390 }
9391 else
9392 {
9393 struct type *disr_type = nullptr;
1f704f76 9394 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9395 {
940da03e 9396 disr_type = type->field (i).type ();
c9317f21 9397
78134374 9398 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9399 {
9400 /* All fields of a true enum will be structs. */
9401 return;
9402 }
1f704f76 9403 else if (disr_type->num_fields () == 0)
c9317f21
TT
9404 {
9405 /* Could be data-less variant, so keep going. */
a037790e 9406 disr_type = nullptr;
c9317f21
TT
9407 }
9408 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9409 "RUST$ENUM$DISR") != 0)
9410 {
9411 /* Not a Rust enum. */
9412 return;
9413 }
9414 else
9415 {
9416 /* Found one. */
9417 break;
9418 }
9419 }
9420
9421 /* If we got here without a discriminant, then it's probably
9422 just a union. */
9423 if (disr_type == nullptr)
9424 return;
9425
9426 /* Smash this type to be a structure type. We have to do this
9427 because the type has already been recorded. */
67607e24 9428 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9429
9c6a1327 9430 /* Make space for the discriminant field. */
ceacbf6e 9431 struct field *disr_field = &disr_type->field (0);
9c6a1327 9432 field *new_fields
1f704f76 9433 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9434 * sizeof (struct field)));
80fc5e77 9435 memcpy (new_fields + 1, type->fields (),
1f704f76 9436 type->num_fields () * sizeof (struct field));
3cabb6b0 9437 type->set_fields (new_fields);
1f704f76 9438 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9439
9440 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9441 type->field (0) = *disr_field;
9c6a1327
TT
9442 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9443 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9444
9445 /* We need a way to find the correct discriminant given a
9446 variant name. For convenience we build a map here. */
b6cdac4b 9447 struct type *enum_type = disr_field->type ();
c9317f21 9448 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9449 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9450 {
9451 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9452 {
9453 const char *name
9454 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9455 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9456 }
9457 }
9458
1f704f76 9459 int n_fields = type->num_fields ();
9c6a1327
TT
9460 /* We don't need a range entry for the discriminant, but we do
9461 need one for every other field, as there is no default
9462 variant. */
9463 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9464 discriminant_range,
9465 n_fields - 1);
c9317f21
TT
9466 /* Skip the discriminant here. */
9467 for (int i = 1; i < n_fields; ++i)
9468 {
9469 /* Find the final word in the name of this variant's type.
9470 That name can be used to look up the correct
9471 discriminant. */
9472 const char *variant_name
940da03e 9473 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9474
9475 auto iter = discriminant_map.find (variant_name);
9476 if (iter != discriminant_map.end ())
9c6a1327 9477 {
57d02173
TT
9478 ranges[i - 1].low = iter->second;
9479 ranges[i - 1].high = iter->second;
9c6a1327 9480 }
c9317f21 9481
57d02173
TT
9482 /* In Rust, each element should have the size of the
9483 enclosing enum. */
9484 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9485
bedda9ac 9486 /* Remove the discriminant field, if it exists. */
940da03e 9487 struct type *sub_type = type->field (i).type ();
1f704f76 9488 if (sub_type->num_fields () > 0)
bedda9ac 9489 {
5e33d5f4 9490 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9491 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9492 }
9c6a1327 9493 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9494 sub_type->set_name
9495 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9496 type->name (), variant_name));
c9317f21 9497 }
9c6a1327
TT
9498
9499 /* Indicate that this is a variant type. */
a1520ad8 9500 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9501 gdb::array_view<discriminant_range> (ranges,
9502 n_fields - 1));
c9317f21
TT
9503 }
9504}
9505
9506/* Rewrite some Rust unions to be structures with variants parts. */
9507
9508static void
9509rust_union_quirks (struct dwarf2_cu *cu)
9510{
9511 gdb_assert (cu->language == language_rust);
52941706 9512 for (type *type_ : cu->rust_unions)
5e22e966 9513 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9514 /* We don't need this any more. */
9515 cu->rust_unions.clear ();
c9317f21
TT
9516}
9517
8adb8487
TT
9518/* See read.h. */
9519
9520type_unit_group_unshareable *
9521dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9522{
9523 auto iter = this->m_type_units.find (tu_group);
9524 if (iter != this->m_type_units.end ())
9525 return iter->second.get ();
9526
9527 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9528 type_unit_group_unshareable *result = uniq.get ();
9529 this->m_type_units[tu_group] = std::move (uniq);
9530 return result;
9531}
9532
e286671b
TT
9533struct type *
9534dwarf2_per_objfile::get_type_for_signatured_type
9535 (signatured_type *sig_type) const
9536{
9537 auto iter = this->m_type_map.find (sig_type);
9538 if (iter == this->m_type_map.end ())
9539 return nullptr;
9540
9541 return iter->second;
9542}
9543
9544void dwarf2_per_objfile::set_type_for_signatured_type
9545 (signatured_type *sig_type, struct type *type)
9546{
9547 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9548
9549 this->m_type_map[sig_type] = type;
9550}
9551
95554aad
TT
9552/* A helper function for computing the list of all symbol tables
9553 included by PER_CU. */
9554
9555static void
4c39bc03 9556recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9557 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9558 dwarf2_per_cu_data *per_cu,
9559 dwarf2_per_objfile *per_objfile,
43f3e411 9560 struct compunit_symtab *immediate_parent)
95554aad 9561{
af758d11 9562 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9563 if (*slot != NULL)
9564 {
9565 /* This inclusion and its children have been processed. */
9566 return;
9567 }
9568
9569 *slot = per_cu;
af758d11 9570
95554aad 9571 /* Only add a CU if it has a symbol table. */
43182c09 9572 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9573 if (cust != NULL)
ec94af83
DE
9574 {
9575 /* If this is a type unit only add its symbol table if we haven't
9576 seen it yet (type unit per_cu's can share symtabs). */
9577 if (per_cu->is_debug_types)
9578 {
43f3e411 9579 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9580 if (*slot == NULL)
9581 {
43f3e411 9582 *slot = cust;
4c39bc03 9583 result->push_back (cust);
43f3e411
DE
9584 if (cust->user == NULL)
9585 cust->user = immediate_parent;
ec94af83
DE
9586 }
9587 }
9588 else
f9125b6c 9589 {
4c39bc03 9590 result->push_back (cust);
43f3e411
DE
9591 if (cust->user == NULL)
9592 cust->user = immediate_parent;
f9125b6c 9593 }
ec94af83 9594 }
95554aad 9595
ae640021
AB
9596 if (!per_cu->imported_symtabs_empty ())
9597 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9598 {
9599 recursively_compute_inclusions (result, all_children,
43182c09
SM
9600 all_type_symtabs, ptr, per_objfile,
9601 cust);
ae640021 9602 }
95554aad
TT
9603}
9604
43f3e411 9605/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9606 PER_CU. */
9607
9608static void
43182c09
SM
9609compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9610 dwarf2_per_objfile *per_objfile)
95554aad 9611{
f4dc4d17
DE
9612 gdb_assert (! per_cu->is_debug_types);
9613
ae640021 9614 if (!per_cu->imported_symtabs_empty ())
95554aad 9615 {
ae640021 9616 int len;
4c39bc03 9617 std::vector<compunit_symtab *> result_symtabs;
43182c09 9618 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9619
9620 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9621 if (cust == NULL)
95554aad
TT
9622 return;
9623
280a9412
TT
9624 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9625 htab_eq_pointer,
9626 NULL, xcalloc, xfree));
9627 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9628 htab_eq_pointer,
9629 NULL, xcalloc, xfree));
95554aad 9630
ae640021 9631 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9632 {
280a9412
TT
9633 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9634 all_type_symtabs.get (), ptr,
9635 per_objfile, cust);
ec94af83 9636 }
95554aad 9637
ec94af83 9638 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9639 len = result_symtabs.size ();
43f3e411 9640 cust->includes
f6e649dd 9641 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9642 struct compunit_symtab *, len + 1);
4c39bc03
TT
9643 memcpy (cust->includes, result_symtabs.data (),
9644 len * sizeof (compunit_symtab *));
43f3e411 9645 cust->includes[len] = NULL;
95554aad
TT
9646 }
9647}
9648
9649/* Compute the 'includes' field for the symtabs of all the CUs we just
9650 read. */
9651
9652static void
976ca316 9653process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9654{
976ca316 9655 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9656 {
9657 if (! iter->is_debug_types)
976ca316 9658 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9659 }
95554aad 9660
976ca316 9661 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9662}
9663
8fc0b21d 9664/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9665 already been loaded into memory. */
9666
9667static void
8fc0b21d 9668process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9669{
976ca316
SM
9670 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9671 struct objfile *objfile = per_objfile->objfile;
08feed99 9672 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9673 CORE_ADDR lowpc, highpc;
43f3e411 9674 struct compunit_symtab *cust;
10b3939b 9675 CORE_ADDR baseaddr;
4359dff1 9676 struct block *static_block;
3e29f34a 9677 CORE_ADDR addr;
10b3939b 9678
b3b3bada 9679 baseaddr = objfile->text_section_offset ();
10b3939b 9680
c89b44cd
TT
9681 /* Clear the list here in case something was left over. */
9682 cu->method_list.clear ();
10b3939b 9683
95554aad
TT
9684 cu->language = pretend_language;
9685 cu->language_defn = language_def (cu->language);
9686
1c47ec3e
TV
9687 dwarf2_find_base_address (cu->dies, cu);
9688
702cf3f5
AB
9689 /* Before we start reading the top-level DIE, ensure it has a valid tag
9690 type. */
9691 switch (cu->dies->tag)
9692 {
9693 case DW_TAG_compile_unit:
9694 case DW_TAG_partial_unit:
9695 case DW_TAG_type_unit:
9696 break;
9697 default:
9698 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9699 dwarf_tag_name (cu->dies->tag),
9700 sect_offset_str (cu->per_cu->sect_off),
9701 objfile_name (per_objfile->objfile));
9702 }
9703
c906108c 9704 /* Do line number decoding in read_file_scope () */
10b3939b 9705 process_die (cu->dies, cu);
c906108c 9706
a766d390
DE
9707 /* For now fudge the Go package. */
9708 if (cu->language == language_go)
9709 fixup_go_packaging (cu);
9710
5f48f8f3 9711 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9712 should be complete, and it should now be safe to compute all of the
9713 physnames. */
9714 compute_delayed_physnames (cu);
3da10d80 9715
c9317f21
TT
9716 if (cu->language == language_rust)
9717 rust_union_quirks (cu);
9718
fae299cd
DC
9719 /* Some compilers don't define a DW_AT_high_pc attribute for the
9720 compilation unit. If the DW_AT_high_pc is missing, synthesize
9721 it, by scanning the DIE's below the compilation unit. */
10b3939b 9722 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9723
3e29f34a 9724 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9725 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9726
9727 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9728 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9729 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9730 addrmap to help ensure it has an accurate map of pc values belonging to
9731 this comp unit. */
9732 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9733
c24bdb02 9734 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9735 SECT_OFF_TEXT (objfile),
9736 0);
c906108c 9737
43f3e411 9738 if (cust != NULL)
c906108c 9739 {
df15bd07 9740 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9741
8be455d7
JK
9742 /* Set symtab language to language from DW_AT_language. If the
9743 compilation is from a C file generated by language preprocessors, do
9744 not set the language if it was already deduced by start_subfile. */
43f3e411 9745 if (!(cu->language == language_c
40e3ad0e 9746 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9747 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9748
9749 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9750 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9751 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9752 there were bugs in prologue debug info, fixed later in GCC-4.5
9753 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9754
9755 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9756 needed, it would be wrong due to missing DW_AT_producer there.
9757
9758 Still one can confuse GDB by using non-standard GCC compilation
9759 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9760 */
ab260dad 9761 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9762 cust->locations_valid = 1;
e0d00bc7
JK
9763
9764 if (gcc_4_minor >= 5)
43f3e411 9765 cust->epilogue_unwind_valid = 1;
96408a79 9766
43f3e411 9767 cust->call_site_htab = cu->call_site_htab;
c906108c 9768 }
9291a0cd 9769
976ca316 9770 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9771
95554aad 9772 /* Push it for inclusion processing later. */
976ca316 9773 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9774
9775 /* Not needed any more. */
c24bdb02 9776 cu->reset_builder ();
f4dc4d17 9777}
45cfd468 9778
8fc0b21d 9779/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9780 already been loaded into memory. */
9781
9782static void
8fc0b21d 9783process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9784 enum language pretend_language)
9785{
976ca316
SM
9786 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9787 struct objfile *objfile = per_objfile->objfile;
43f3e411 9788 struct compunit_symtab *cust;
0186c6a7
DE
9789 struct signatured_type *sig_type;
9790
8fc0b21d
SM
9791 gdb_assert (cu->per_cu->is_debug_types);
9792 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9793
c89b44cd
TT
9794 /* Clear the list here in case something was left over. */
9795 cu->method_list.clear ();
f4dc4d17 9796
f4dc4d17
DE
9797 cu->language = pretend_language;
9798 cu->language_defn = language_def (cu->language);
9799
9800 /* The symbol tables are set up in read_type_unit_scope. */
9801 process_die (cu->dies, cu);
9802
9803 /* For now fudge the Go package. */
9804 if (cu->language == language_go)
9805 fixup_go_packaging (cu);
9806
5f48f8f3 9807 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9808 should be complete, and it should now be safe to compute all of the
9809 physnames. */
9810 compute_delayed_physnames (cu);
f4dc4d17 9811
c9317f21
TT
9812 if (cu->language == language_rust)
9813 rust_union_quirks (cu);
9814
f4dc4d17
DE
9815 /* TUs share symbol tables.
9816 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9817 of it with end_expandable_symtab. Otherwise, complete the addition of
9818 this TU's symbols to the existing symtab. */
8adb8487 9819 type_unit_group_unshareable *tug_unshare =
976ca316 9820 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9821 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9822 {
c24bdb02
KS
9823 buildsym_compunit *builder = cu->get_builder ();
9824 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9825 tug_unshare->compunit_symtab = cust;
f4dc4d17 9826
43f3e411 9827 if (cust != NULL)
f4dc4d17
DE
9828 {
9829 /* Set symtab language to language from DW_AT_language. If the
9830 compilation is from a C file generated by language preprocessors,
9831 do not set the language if it was already deduced by
9832 start_subfile. */
43f3e411
DE
9833 if (!(cu->language == language_c
9834 && COMPUNIT_FILETABS (cust)->language != language_c))
9835 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9836 }
9837 }
9838 else
9839 {
c24bdb02 9840 cu->get_builder ()->augment_type_symtab ();
8adb8487 9841 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9842 }
9843
976ca316 9844 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9845
9846 /* Not needed any more. */
c24bdb02 9847 cu->reset_builder ();
c906108c
SS
9848}
9849
95554aad
TT
9850/* Process an imported unit DIE. */
9851
9852static void
9853process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9854{
9855 struct attribute *attr;
9856
f4dc4d17
DE
9857 /* For now we don't handle imported units in type units. */
9858 if (cu->per_cu->is_debug_types)
9859 {
9860 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9861 " supported in type units [in module %s]"),
5e22e966 9862 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9863 }
9864
95554aad
TT
9865 attr = dwarf2_attr (die, DW_AT_import, cu);
9866 if (attr != NULL)
9867 {
0826b30a 9868 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9869 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9870 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9871 dwarf2_per_cu_data *per_cu
ab432490 9872 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9873
58990295
TV
9874 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9875 into another compilation unit, at root level. Regard this as a hint,
9876 and ignore it. */
9877 if (die->parent && die->parent->parent == NULL
9878 && per_cu->unit_type == DW_UT_compile
9879 && per_cu->lang == language_cplus)
9880 return;
9881
69d751e3 9882 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 9883 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
9884 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9885 false, cu->language);
95554aad 9886
ae640021 9887 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9888 }
9889}
9890
4c8aa72d
PA
9891/* RAII object that represents a process_die scope: i.e.,
9892 starts/finishes processing a DIE. */
9893class process_die_scope
adde2bff 9894{
4c8aa72d
PA
9895public:
9896 process_die_scope (die_info *die, dwarf2_cu *cu)
9897 : m_die (die), m_cu (cu)
9898 {
9899 /* We should only be processing DIEs not already in process. */
9900 gdb_assert (!m_die->in_process);
9901 m_die->in_process = true;
9902 }
8c3cb9fa 9903
4c8aa72d
PA
9904 ~process_die_scope ()
9905 {
9906 m_die->in_process = false;
9907
9908 /* If we're done processing the DIE for the CU that owns the line
9909 header, we don't need the line header anymore. */
9910 if (m_cu->line_header_die_owner == m_die)
9911 {
9912 delete m_cu->line_header;
9913 m_cu->line_header = NULL;
9914 m_cu->line_header_die_owner = NULL;
9915 }
9916 }
9917
9918private:
9919 die_info *m_die;
9920 dwarf2_cu *m_cu;
9921};
adde2bff 9922
c906108c
SS
9923/* Process a die and its children. */
9924
9925static void
e7c27a73 9926process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9927{
4c8aa72d 9928 process_die_scope scope (die, cu);
adde2bff 9929
c906108c
SS
9930 switch (die->tag)
9931 {
9932 case DW_TAG_padding:
9933 break;
9934 case DW_TAG_compile_unit:
95554aad 9935 case DW_TAG_partial_unit:
e7c27a73 9936 read_file_scope (die, cu);
c906108c 9937 break;
348e048f
DE
9938 case DW_TAG_type_unit:
9939 read_type_unit_scope (die, cu);
9940 break;
c906108c 9941 case DW_TAG_subprogram:
0a4b0913
AB
9942 /* Nested subprograms in Fortran get a prefix. */
9943 if (cu->language == language_fortran
9944 && die->parent != NULL
9945 && die->parent->tag == DW_TAG_subprogram)
9946 cu->processing_has_namespace_info = true;
9947 /* Fall through. */
c906108c 9948 case DW_TAG_inlined_subroutine:
edb3359d 9949 read_func_scope (die, cu);
c906108c
SS
9950 break;
9951 case DW_TAG_lexical_block:
14898363
L
9952 case DW_TAG_try_block:
9953 case DW_TAG_catch_block:
e7c27a73 9954 read_lexical_block_scope (die, cu);
c906108c 9955 break;
216f72a1 9956 case DW_TAG_call_site:
96408a79
SA
9957 case DW_TAG_GNU_call_site:
9958 read_call_site_scope (die, cu);
9959 break;
c906108c 9960 case DW_TAG_class_type:
680b30c7 9961 case DW_TAG_interface_type:
c906108c
SS
9962 case DW_TAG_structure_type:
9963 case DW_TAG_union_type:
134d01f1 9964 process_structure_scope (die, cu);
c906108c
SS
9965 break;
9966 case DW_TAG_enumeration_type:
134d01f1 9967 process_enumeration_scope (die, cu);
c906108c 9968 break;
134d01f1 9969
f792889a
DJ
9970 /* These dies have a type, but processing them does not create
9971 a symbol or recurse to process the children. Therefore we can
9972 read them on-demand through read_type_die. */
c906108c 9973 case DW_TAG_subroutine_type:
72019c9c 9974 case DW_TAG_set_type:
c906108c 9975 case DW_TAG_pointer_type:
c906108c 9976 case DW_TAG_ptr_to_member_type:
c906108c 9977 case DW_TAG_reference_type:
4297a3f0 9978 case DW_TAG_rvalue_reference_type:
c906108c 9979 case DW_TAG_string_type:
c906108c 9980 break;
134d01f1 9981
d8f62e84
TT
9982 case DW_TAG_array_type:
9983 /* We only need to handle this case for Ada -- in other
9984 languages, it's normal for the compiler to emit a typedef
9985 instead. */
9986 if (cu->language != language_ada)
9987 break;
9988 /* FALLTHROUGH */
c906108c 9989 case DW_TAG_base_type:
a02abb62 9990 case DW_TAG_subrange_type:
cb249c71 9991 case DW_TAG_typedef:
134d01f1 9992 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 9993 DW_AT_name. */
f792889a 9994 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9995 break;
c906108c 9996 case DW_TAG_common_block:
e7c27a73 9997 read_common_block (die, cu);
c906108c
SS
9998 break;
9999 case DW_TAG_common_inclusion:
10000 break;
d9fa45fe 10001 case DW_TAG_namespace:
9068261f 10002 cu->processing_has_namespace_info = true;
e7c27a73 10003 read_namespace (die, cu);
d9fa45fe 10004 break;
5d7cb8df 10005 case DW_TAG_module:
9068261f 10006 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10007 read_module (die, cu);
10008 break;
d9fa45fe 10009 case DW_TAG_imported_declaration:
9068261f 10010 cu->processing_has_namespace_info = true;
74921315
KS
10011 if (read_namespace_alias (die, cu))
10012 break;
86a73007
TT
10013 /* The declaration is not a global namespace alias. */
10014 /* Fall through. */
d9fa45fe 10015 case DW_TAG_imported_module:
9068261f 10016 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10017 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10018 || cu->language != language_fortran))
b98664d3 10019 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10020 dwarf_tag_name (die->tag));
10021 read_import_statement (die, cu);
d9fa45fe 10022 break;
95554aad
TT
10023
10024 case DW_TAG_imported_unit:
10025 process_imported_unit_die (die, cu);
10026 break;
10027
71a3c369
TT
10028 case DW_TAG_variable:
10029 read_variable (die, cu);
10030 break;
10031
c906108c 10032 default:
e7c27a73 10033 new_symbol (die, NULL, cu);
c906108c
SS
10034 break;
10035 }
10036}
ca69b9e6
DE
10037\f
10038/* DWARF name computation. */
c906108c 10039
94af9270
KS
10040/* A helper function for dwarf2_compute_name which determines whether DIE
10041 needs to have the name of the scope prepended to the name listed in the
10042 die. */
10043
10044static int
10045die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10046{
1c809c68
TT
10047 struct attribute *attr;
10048
94af9270
KS
10049 switch (die->tag)
10050 {
10051 case DW_TAG_namespace:
10052 case DW_TAG_typedef:
10053 case DW_TAG_class_type:
10054 case DW_TAG_interface_type:
10055 case DW_TAG_structure_type:
10056 case DW_TAG_union_type:
10057 case DW_TAG_enumeration_type:
10058 case DW_TAG_enumerator:
10059 case DW_TAG_subprogram:
08a76f8a 10060 case DW_TAG_inlined_subroutine:
94af9270 10061 case DW_TAG_member:
74921315 10062 case DW_TAG_imported_declaration:
94af9270
KS
10063 return 1;
10064
10065 case DW_TAG_variable:
c2b0a229 10066 case DW_TAG_constant:
94af9270
KS
10067 /* We only need to prefix "globally" visible variables. These include
10068 any variable marked with DW_AT_external or any variable that
10069 lives in a namespace. [Variables in anonymous namespaces
10070 require prefixing, but they are not DW_AT_external.] */
10071
10072 if (dwarf2_attr (die, DW_AT_specification, cu))
10073 {
10074 struct dwarf2_cu *spec_cu = cu;
9a619af0 10075
94af9270
KS
10076 return die_needs_namespace (die_specification (die, &spec_cu),
10077 spec_cu);
10078 }
10079
1c809c68 10080 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10081 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10082 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10083 return 0;
10084 /* A variable in a lexical block of some kind does not need a
10085 namespace, even though in C++ such variables may be external
10086 and have a mangled name. */
10087 if (die->parent->tag == DW_TAG_lexical_block
10088 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10089 || die->parent->tag == DW_TAG_catch_block
10090 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10091 return 0;
10092 return 1;
94af9270
KS
10093
10094 default:
10095 return 0;
10096 }
10097}
10098
73b9be8b
KS
10099/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10100 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10101 defined for the given DIE. */
10102
10103static struct attribute *
10104dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10105{
10106 struct attribute *attr;
10107
10108 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10109 if (attr == NULL)
10110 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10111
10112 return attr;
10113}
10114
10115/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10116 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10117 defined for the given DIE. */
10118
10119static const char *
10120dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10121{
10122 const char *linkage_name;
10123
10124 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10125 if (linkage_name == NULL)
10126 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10127
787de330
TT
10128 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10129 See https://github.com/rust-lang/rust/issues/32925. */
10130 if (cu->language == language_rust && linkage_name != NULL
10131 && strchr (linkage_name, '{') != NULL)
10132 linkage_name = NULL;
10133
73b9be8b
KS
10134 return linkage_name;
10135}
10136
94af9270 10137/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10138 compute the physname for the object, which include a method's:
9c37b5ae 10139 - formal parameters (C++),
a766d390 10140 - receiver type (Go),
a766d390
DE
10141
10142 The term "physname" is a bit confusing.
10143 For C++, for example, it is the demangled name.
10144 For Go, for example, it's the mangled name.
94af9270 10145
af6b7be1
JB
10146 For Ada, return the DIE's linkage name rather than the fully qualified
10147 name. PHYSNAME is ignored..
10148
5989a64e 10149 The result is allocated on the objfile->per_bfd's obstack and
45940949 10150 canonicalized. */
94af9270
KS
10151
10152static const char *
15d034d0
TT
10153dwarf2_compute_name (const char *name,
10154 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10155 int physname)
10156{
5e22e966 10157 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10158
94af9270
KS
10159 if (name == NULL)
10160 name = dwarf2_name (die, cu);
10161
2ee7123e
DE
10162 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10163 but otherwise compute it by typename_concat inside GDB.
10164 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10165 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10166 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10167 will set the demangled name to the result of dwarf2_full_name, and it is
10168 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10169 if (cu->language == language_ada
10170 || (cu->language == language_fortran && physname))
10171 {
10172 /* For Ada unit, we prefer the linkage name over the name, as
10173 the former contains the exported name, which the user expects
10174 to be able to reference. Ideally, we want the user to be able
10175 to reference this entity using either natural or linkage name,
10176 but we haven't started looking at this enhancement yet. */
73b9be8b 10177 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10178
2ee7123e
DE
10179 if (linkage_name != NULL)
10180 return linkage_name;
f55ee35c
JK
10181 }
10182
94af9270
KS
10183 /* These are the only languages we know how to qualify names in. */
10184 if (name != NULL
9c37b5ae 10185 && (cu->language == language_cplus
c44af4eb
TT
10186 || cu->language == language_fortran || cu->language == language_d
10187 || cu->language == language_rust))
94af9270
KS
10188 {
10189 if (die_needs_namespace (die, cu))
10190 {
0d5cff50 10191 const char *prefix;
34a68019 10192 const char *canonical_name = NULL;
94af9270 10193
d7e74731
PA
10194 string_file buf;
10195
94af9270 10196 prefix = determine_prefix (die, cu);
94af9270
KS
10197 if (*prefix != '\0')
10198 {
43816ebc
TT
10199 gdb::unique_xmalloc_ptr<char> prefixed_name
10200 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10201
43816ebc 10202 buf.puts (prefixed_name.get ());
94af9270
KS
10203 }
10204 else
d7e74731 10205 buf.puts (name);
94af9270 10206
98bfdba5
PA
10207 /* Template parameters may be specified in the DIE's DW_AT_name, or
10208 as children with DW_TAG_template_type_param or
10209 DW_TAG_value_type_param. If the latter, add them to the name
10210 here. If the name already has template parameters, then
10211 skip this step; some versions of GCC emit both, and
10212 it is more efficient to use the pre-computed name.
10213
10214 Something to keep in mind about this process: it is very
10215 unlikely, or in some cases downright impossible, to produce
10216 something that will match the mangled name of a function.
10217 If the definition of the function has the same debug info,
10218 we should be able to match up with it anyway. But fallbacks
10219 using the minimal symbol, for instance to find a method
10220 implemented in a stripped copy of libstdc++, will not work.
10221 If we do not have debug info for the definition, we will have to
10222 match them up some other way.
10223
10224 When we do name matching there is a related problem with function
10225 templates; two instantiated function templates are allowed to
10226 differ only by their return types, which we do not add here. */
10227
10228 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10229 {
10230 struct attribute *attr;
10231 struct die_info *child;
10232 int first = 1;
2c75ccb2 10233 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
10234
10235 die->building_fullname = 1;
10236
10237 for (child = die->child; child != NULL; child = child->sibling)
10238 {
10239 struct type *type;
12df843f 10240 LONGEST value;
d521ce57 10241 const gdb_byte *bytes;
98bfdba5
PA
10242 struct dwarf2_locexpr_baton *baton;
10243 struct value *v;
10244
10245 if (child->tag != DW_TAG_template_type_param
10246 && child->tag != DW_TAG_template_value_param)
10247 continue;
10248
10249 if (first)
10250 {
d7e74731 10251 buf.puts ("<");
98bfdba5
PA
10252 first = 0;
10253 }
10254 else
d7e74731 10255 buf.puts (", ");
98bfdba5
PA
10256
10257 attr = dwarf2_attr (child, DW_AT_type, cu);
10258 if (attr == NULL)
10259 {
b98664d3 10260 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10261 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10262 continue;
10263 }
10264 type = die_type (child, cu);
10265
10266 if (child->tag == DW_TAG_template_type_param)
10267 {
2c75ccb2
AB
10268 cplus_lang->print_type (type, "", &buf, -1, 0,
10269 &type_print_raw_options);
98bfdba5
PA
10270 continue;
10271 }
10272
10273 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10274 if (attr == NULL)
10275 {
b98664d3 10276 complaint (_("template parameter missing "
3e43a32a 10277 "DW_AT_const_value"));
d7e74731 10278 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10279 continue;
10280 }
10281
10282 dwarf2_const_value_attr (attr, type, name,
10283 &cu->comp_unit_obstack, cu,
10284 &value, &bytes, &baton);
10285
20ce4123 10286 if (type->has_no_signedness ())
98bfdba5
PA
10287 /* GDB prints characters as NUMBER 'CHAR'. If that's
10288 changed, this can use value_print instead. */
2c75ccb2 10289 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
10290 else
10291 {
10292 struct value_print_options opts;
10293
10294 if (baton != NULL)
10295 v = dwarf2_evaluate_loc_desc (type, NULL,
10296 baton->data,
10297 baton->size,
9f47c707
SM
10298 baton->per_cu,
10299 baton->per_objfile);
98bfdba5
PA
10300 else if (bytes != NULL)
10301 {
10302 v = allocate_value (type);
10303 memcpy (value_contents_writeable (v), bytes,
10304 TYPE_LENGTH (type));
10305 }
10306 else
10307 v = value_from_longest (type, value);
10308
3e43a32a
MS
10309 /* Specify decimal so that we do not depend on
10310 the radix. */
98bfdba5
PA
10311 get_formatted_print_options (&opts, 'd');
10312 opts.raw = 1;
d7e74731 10313 value_print (v, &buf, &opts);
98bfdba5 10314 release_value (v);
98bfdba5
PA
10315 }
10316 }
10317
10318 die->building_fullname = 0;
10319
10320 if (!first)
10321 {
10322 /* Close the argument list, with a space if necessary
10323 (nested templates). */
d7e74731
PA
10324 if (!buf.empty () && buf.string ().back () == '>')
10325 buf.puts (" >");
98bfdba5 10326 else
d7e74731 10327 buf.puts (">");
98bfdba5
PA
10328 }
10329 }
10330
9c37b5ae 10331 /* For C++ methods, append formal parameter type
94af9270 10332 information, if PHYSNAME. */
6e70227d 10333
94af9270 10334 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10335 && cu->language == language_cplus)
94af9270
KS
10336 {
10337 struct type *type = read_type_die (die, cu);
10338
d7e74731 10339 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10340 &type_print_raw_options);
94af9270 10341
9c37b5ae 10342 if (cu->language == language_cplus)
94af9270 10343 {
60430eff
DJ
10344 /* Assume that an artificial first parameter is
10345 "this", but do not crash if it is not. RealView
10346 marks unnamed (and thus unused) parameters as
10347 artificial; there is no way to differentiate
10348 the two cases. */
1f704f76 10349 if (type->num_fields () > 0
94af9270 10350 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10351 && type->field (0).type ()->code () == TYPE_CODE_PTR
10352 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10353 buf.puts (" const");
94af9270
KS
10354 }
10355 }
10356
d7e74731 10357 const std::string &intermediate_name = buf.string ();
94af9270
KS
10358
10359 if (cu->language == language_cplus)
34a68019 10360 canonical_name
322a8516 10361 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10362 objfile);
34a68019
TT
10363
10364 /* If we only computed INTERMEDIATE_NAME, or if
10365 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10366 intern it. */
322a8516 10367 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10368 name = objfile->intern (intermediate_name);
34a68019
TT
10369 else
10370 name = canonical_name;
94af9270
KS
10371 }
10372 }
10373
10374 return name;
10375}
10376
0114d602
DJ
10377/* Return the fully qualified name of DIE, based on its DW_AT_name.
10378 If scope qualifiers are appropriate they will be added. The result
34a68019 10379 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10380 not have a name. NAME may either be from a previous call to
10381 dwarf2_name or NULL.
10382
9c37b5ae 10383 The output string will be canonicalized (if C++). */
0114d602
DJ
10384
10385static const char *
15d034d0 10386dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10387{
94af9270
KS
10388 return dwarf2_compute_name (name, die, cu, 0);
10389}
0114d602 10390
94af9270
KS
10391/* Construct a physname for the given DIE in CU. NAME may either be
10392 from a previous call to dwarf2_name or NULL. The result will be
10393 allocated on the objfile_objstack or NULL if the DIE does not have a
10394 name.
0114d602 10395
9c37b5ae 10396 The output string will be canonicalized (if C++). */
0114d602 10397
94af9270 10398static const char *
15d034d0 10399dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10400{
5e22e966 10401 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10402 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10403 int need_copy = 1;
10404
10405 /* In this case dwarf2_compute_name is just a shortcut not building anything
10406 on its own. */
10407 if (!die_needs_namespace (die, cu))
10408 return dwarf2_compute_name (name, die, cu, 1);
10409
906bb4c5
TT
10410 if (cu->language != language_rust)
10411 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10412
10413 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10414 has computed. */
791afaa2 10415 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10416 if (mangled != NULL)
900e11f9 10417 {
900e11f9 10418
d3355e4d 10419 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10420 {
10421 /* Do nothing (do not demangle the symbol name). */
10422 }
a766d390
DE
10423 else
10424 {
0eb876f5
JB
10425 /* Use DMGL_RET_DROP for C++ template functions to suppress
10426 their return type. It is easier for GDB users to search
10427 for such functions as `name(params)' than `long name(params)'.
10428 In such case the minimal symbol names do not match the full
10429 symbol names but for template functions there is never a need
10430 to look up their definition from their declaration so
10431 the only disadvantage remains the minimal symbol variant
10432 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10433 demangled.reset (gdb_demangle (mangled,
10434 (DMGL_PARAMS | DMGL_ANSI
10435 | DMGL_RET_DROP)));
a766d390 10436 }
900e11f9 10437 if (demangled)
791afaa2 10438 canon = demangled.get ();
900e11f9
JK
10439 else
10440 {
10441 canon = mangled;
10442 need_copy = 0;
10443 }
10444 }
10445
10446 if (canon == NULL || check_physname)
10447 {
10448 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10449
10450 if (canon != NULL && strcmp (physname, canon) != 0)
10451 {
10452 /* It may not mean a bug in GDB. The compiler could also
10453 compute DW_AT_linkage_name incorrectly. But in such case
10454 GDB would need to be bug-to-bug compatible. */
10455
b98664d3 10456 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10457 "(from linkage <%s>) - DIE at %s [in module %s]"),
10458 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10459 objfile_name (objfile));
900e11f9
JK
10460
10461 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10462 is available here - over computed PHYSNAME. It is safer
10463 against both buggy GDB and buggy compilers. */
10464
10465 retval = canon;
10466 }
10467 else
10468 {
10469 retval = physname;
10470 need_copy = 0;
10471 }
10472 }
10473 else
10474 retval = canon;
10475
10476 if (need_copy)
be1e3d3e 10477 retval = objfile->intern (retval);
900e11f9 10478
900e11f9 10479 return retval;
0114d602
DJ
10480}
10481
74921315
KS
10482/* Inspect DIE in CU for a namespace alias. If one exists, record
10483 a new symbol for it.
10484
10485 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10486
10487static int
10488read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10489{
10490 struct attribute *attr;
10491
10492 /* If the die does not have a name, this is not a namespace
10493 alias. */
10494 attr = dwarf2_attr (die, DW_AT_name, cu);
10495 if (attr != NULL)
10496 {
10497 int num;
10498 struct die_info *d = die;
10499 struct dwarf2_cu *imported_cu = cu;
10500
10501 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10502 keep inspecting DIEs until we hit the underlying import. */
10503#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10504 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10505 {
10506 attr = dwarf2_attr (d, DW_AT_import, cu);
10507 if (attr == NULL)
10508 break;
10509
10510 d = follow_die_ref (d, attr, &imported_cu);
10511 if (d->tag != DW_TAG_imported_declaration)
10512 break;
10513 }
10514
10515 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10516 {
b98664d3 10517 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10518 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10519 return 0;
10520 }
10521
10522 if (attr != NULL)
10523 {
10524 struct type *type;
0826b30a 10525 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10526
aa66c379 10527 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10528 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10529 {
10530 /* This declaration is a global namespace alias. Add
10531 a symbol for it whose type is the aliased namespace. */
10532 new_symbol (die, type, cu);
10533 return 1;
10534 }
10535 }
10536 }
10537
10538 return 0;
10539}
10540
22cee43f 10541/* Return the using directives repository (global or local?) to use in the
804d2729 10542 current context for CU.
22cee43f
PMR
10543
10544 For Ada, imported declarations can materialize renamings, which *may* be
10545 global. However it is impossible (for now?) in DWARF to distinguish
10546 "external" imported declarations and "static" ones. As all imported
10547 declarations seem to be static in all other languages, make them all CU-wide
10548 global only in Ada. */
10549
10550static struct using_direct **
804d2729 10551using_directives (struct dwarf2_cu *cu)
22cee43f 10552{
c24bdb02
KS
10553 if (cu->language == language_ada
10554 && cu->get_builder ()->outermost_context_p ())
10555 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10556 else
c24bdb02 10557 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10558}
10559
27aa8d6a
SW
10560/* Read the import statement specified by the given die and record it. */
10561
10562static void
10563read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10564{
5e22e966 10565 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10566 struct attribute *import_attr;
32019081 10567 struct die_info *imported_die, *child_die;
de4affc9 10568 struct dwarf2_cu *imported_cu;
27aa8d6a 10569 const char *imported_name;
794684b6 10570 const char *imported_name_prefix;
13387711
SW
10571 const char *canonical_name;
10572 const char *import_alias;
10573 const char *imported_declaration = NULL;
794684b6 10574 const char *import_prefix;
eb1e02fd 10575 std::vector<const char *> excludes;
13387711 10576
27aa8d6a
SW
10577 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10578 if (import_attr == NULL)
10579 {
b98664d3 10580 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10581 dwarf_tag_name (die->tag));
10582 return;
10583 }
10584
de4affc9
CC
10585 imported_cu = cu;
10586 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10587 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10588 if (imported_name == NULL)
10589 {
10590 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10591
dda83cd7
SM
10592 The import in the following code:
10593 namespace A
10594 {
10595 typedef int B;
10596 }
10597
10598 int main ()
10599 {
10600 using A::B;
10601 B b;
10602 return b;
10603 }
10604
10605 ...
10606 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10607 <52> DW_AT_decl_file : 1
10608 <53> DW_AT_decl_line : 6
10609 <54> DW_AT_import : <0x75>
10610 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10611 <59> DW_AT_name : B
10612 <5b> DW_AT_decl_file : 1
10613 <5c> DW_AT_decl_line : 2
10614 <5d> DW_AT_type : <0x6e>
10615 ...
10616 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10617 <76> DW_AT_byte_size : 4
10618 <77> DW_AT_encoding : 5 (signed)
10619
10620 imports the wrong die ( 0x75 instead of 0x58 ).
10621 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10622 return;
10623 }
10624
82856980
SW
10625 /* Figure out the local name after import. */
10626 import_alias = dwarf2_name (die, cu);
27aa8d6a 10627
794684b6
SW
10628 /* Figure out where the statement is being imported to. */
10629 import_prefix = determine_prefix (die, cu);
10630
10631 /* Figure out what the scope of the imported die is and prepend it
10632 to the name of the imported die. */
de4affc9 10633 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10634
f55ee35c
JK
10635 if (imported_die->tag != DW_TAG_namespace
10636 && imported_die->tag != DW_TAG_module)
794684b6 10637 {
13387711
SW
10638 imported_declaration = imported_name;
10639 canonical_name = imported_name_prefix;
794684b6 10640 }
13387711 10641 else if (strlen (imported_name_prefix) > 0)
12aaed36 10642 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10643 imported_name_prefix,
10644 (cu->language == language_d ? "." : "::"),
10645 imported_name, (char *) NULL);
13387711
SW
10646 else
10647 canonical_name = imported_name;
794684b6 10648
32019081
JK
10649 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10650 for (child_die = die->child; child_die && child_die->tag;
436c571c 10651 child_die = child_die->sibling)
32019081
JK
10652 {
10653 /* DWARF-4: A Fortran use statement with a “rename list” may be
10654 represented by an imported module entry with an import attribute
10655 referring to the module and owned entries corresponding to those
10656 entities that are renamed as part of being imported. */
10657
10658 if (child_die->tag != DW_TAG_imported_declaration)
10659 {
b98664d3 10660 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10661 "- DIE at %s [in module %s]"),
10662 sect_offset_str (child_die->sect_off),
10663 objfile_name (objfile));
32019081
JK
10664 continue;
10665 }
10666
10667 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10668 if (import_attr == NULL)
10669 {
b98664d3 10670 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10671 dwarf_tag_name (child_die->tag));
10672 continue;
10673 }
10674
10675 imported_cu = cu;
10676 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10677 &imported_cu);
10678 imported_name = dwarf2_name (imported_die, imported_cu);
10679 if (imported_name == NULL)
10680 {
b98664d3 10681 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10682 "imported name - DIE at %s [in module %s]"),
10683 sect_offset_str (child_die->sect_off),
10684 objfile_name (objfile));
32019081
JK
10685 continue;
10686 }
10687
eb1e02fd 10688 excludes.push_back (imported_name);
32019081
JK
10689
10690 process_die (child_die, cu);
10691 }
10692
804d2729 10693 add_using_directive (using_directives (cu),
22cee43f
PMR
10694 import_prefix,
10695 canonical_name,
10696 import_alias,
10697 imported_declaration,
10698 excludes,
10699 0,
10700 &objfile->objfile_obstack);
27aa8d6a
SW
10701}
10702
5230b05a
WT
10703/* ICC<14 does not output the required DW_AT_declaration on incomplete
10704 types, but gives them a size of zero. Starting with version 14,
10705 ICC is compatible with GCC. */
10706
9068261f 10707static bool
5230b05a
WT
10708producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10709{
10710 if (!cu->checked_producer)
10711 check_producer (cu);
10712
10713 return cu->producer_is_icc_lt_14;
10714}
10715
eb77c9df
AB
10716/* ICC generates a DW_AT_type for C void functions. This was observed on
10717 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10718 which says that void functions should not have a DW_AT_type. */
10719
10720static bool
10721producer_is_icc (struct dwarf2_cu *cu)
10722{
10723 if (!cu->checked_producer)
10724 check_producer (cu);
10725
10726 return cu->producer_is_icc;
10727}
10728
1b80a9fa
JK
10729/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10730 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10731 this, it was first present in GCC release 4.3.0. */
10732
9068261f 10733static bool
1b80a9fa
JK
10734producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10735{
10736 if (!cu->checked_producer)
10737 check_producer (cu);
10738
10739 return cu->producer_is_gcc_lt_4_3;
10740}
10741
d721ba37
PA
10742static file_and_directory
10743find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10744{
d721ba37
PA
10745 file_and_directory res;
10746
9291a0cd
TT
10747 /* Find the filename. Do not use dwarf2_name here, since the filename
10748 is not a source language identifier. */
d721ba37
PA
10749 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10750 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10751
d721ba37
PA
10752 if (res.comp_dir == NULL
10753 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10754 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10755 {
d721ba37
PA
10756 res.comp_dir_storage = ldirname (res.name);
10757 if (!res.comp_dir_storage.empty ())
10758 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10759 }
d721ba37 10760 if (res.comp_dir != NULL)
9291a0cd
TT
10761 {
10762 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10763 directory, get rid of it. */
d721ba37 10764 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10765
d721ba37
PA
10766 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10767 res.comp_dir = cp + 1;
9291a0cd
TT
10768 }
10769
d721ba37
PA
10770 if (res.name == NULL)
10771 res.name = "<unknown>";
10772
10773 return res;
9291a0cd
TT
10774}
10775
f4dc4d17
DE
10776/* Handle DW_AT_stmt_list for a compilation unit.
10777 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10778 COMP_DIR is the compilation directory. LOWPC is passed to
10779 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10780
10781static void
10782handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10783 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10784{
976ca316 10785 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10786 struct attribute *attr;
527f3840
JK
10787 struct line_header line_header_local;
10788 hashval_t line_header_local_hash;
527f3840
JK
10789 void **slot;
10790 int decode_mapping;
2ab95328 10791
f4dc4d17
DE
10792 gdb_assert (! cu->per_cu->is_debug_types);
10793
2ab95328 10794 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 10795 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
10796 return;
10797
d4df075e 10798 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
10799
10800 /* The line header hash table is only created if needed (it exists to
10801 prevent redundant reading of the line table for partial_units).
10802 If we're given a partial_unit, we'll need it. If we're given a
10803 compile_unit, then use the line header hash table if it's already
10804 created, but don't create one just yet. */
10805
976ca316 10806 if (per_objfile->line_header_hash == NULL
527f3840 10807 && die->tag == DW_TAG_partial_unit)
2ab95328 10808 {
976ca316 10809 per_objfile->line_header_hash
d15acc42
TT
10810 .reset (htab_create_alloc (127, line_header_hash_voidp,
10811 line_header_eq_voidp,
10812 free_line_header_voidp,
10813 xcalloc, xfree));
527f3840 10814 }
2ab95328 10815
9c541725 10816 line_header_local.sect_off = line_offset;
527f3840
JK
10817 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10818 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10819 if (per_objfile->line_header_hash != NULL)
527f3840 10820 {
976ca316 10821 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10822 &line_header_local,
10823 line_header_local_hash, NO_INSERT);
10824
10825 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10826 is not present in *SLOT (since if there is something in *SLOT then
10827 it will be for a partial_unit). */
10828 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10829 {
527f3840 10830 gdb_assert (*slot != NULL);
9a3c8263 10831 cu->line_header = (struct line_header *) *slot;
527f3840 10832 return;
dee91e82 10833 }
2ab95328 10834 }
527f3840
JK
10835
10836 /* dwarf_decode_line_header does not yet provide sufficient information.
10837 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10838 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10839 if (lh == NULL)
527f3840 10840 return;
4c8aa72d
PA
10841
10842 cu->line_header = lh.release ();
10843 cu->line_header_die_owner = die;
527f3840 10844
976ca316 10845 if (per_objfile->line_header_hash == NULL)
527f3840
JK
10846 slot = NULL;
10847 else
10848 {
976ca316 10849 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10850 &line_header_local,
10851 line_header_local_hash, INSERT);
10852 gdb_assert (slot != NULL);
10853 }
10854 if (slot != NULL && *slot == NULL)
10855 {
10856 /* This newly decoded line number information unit will be owned
10857 by line_header_hash hash table. */
10858 *slot = cu->line_header;
4c8aa72d 10859 cu->line_header_die_owner = NULL;
527f3840
JK
10860 }
10861 else
10862 {
10863 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 10864 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
10865 line_header for this CU - it may happen at most once for each line
10866 number information unit. And if we're not using line_header_hash
10867 then this is what we want as well. */
10868 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10869 }
10870 decode_mapping = (die->tag != DW_TAG_partial_unit);
10871 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10872 decode_mapping);
fff8551c 10873
2ab95328
TT
10874}
10875
95554aad 10876/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10877
c906108c 10878static void
e7c27a73 10879read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10880{
976ca316
SM
10881 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10882 struct objfile *objfile = per_objfile->objfile;
08feed99 10883 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10884 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10885 CORE_ADDR highpc = ((CORE_ADDR) 0);
10886 struct attribute *attr;
c906108c 10887 struct die_info *child_die;
e142c38c 10888 CORE_ADDR baseaddr;
6e70227d 10889
380618d6 10890 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10891 baseaddr = objfile->text_section_offset ();
c906108c 10892
fae299cd 10893 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10894
10895 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10896 from finish_block. */
2acceee2 10897 if (lowpc == ((CORE_ADDR) -1))
c906108c 10898 lowpc = highpc;
3e29f34a 10899 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10900
d721ba37 10901 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10902
f4b8a18d
KW
10903 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10904 standardised yet. As a workaround for the language detection we fall
10905 back to the DW_AT_producer string. */
10906 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10907 cu->language = language_opencl;
10908
3019eac3
DE
10909 /* Similar hack for Go. */
10910 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10911 set_cu_language (DW_LANG_Go, cu);
10912
c24bdb02 10913 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10914
10915 /* Decode line number information if present. We do this before
10916 processing child DIEs, so that the line header table is available
10917 for DW_AT_decl_file. */
d721ba37 10918 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10919
10920 /* Process all dies in compilation unit. */
10921 if (die->child != NULL)
10922 {
10923 child_die = die->child;
10924 while (child_die && child_die->tag)
10925 {
10926 process_die (child_die, cu);
436c571c 10927 child_die = child_die->sibling;
3019eac3
DE
10928 }
10929 }
10930
10931 /* Decode macro information, if present. Dwarf 2 macro information
10932 refers to information in the line number info statement program
10933 header, so we can only read it if we've read the header
10934 successfully. */
0af92d60
JK
10935 attr = dwarf2_attr (die, DW_AT_macros, cu);
10936 if (attr == NULL)
10937 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 10938 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
10939 {
10940 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10941 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10942
529908cb 10943 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
10944 }
10945 else
10946 {
10947 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 10948 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 10949 {
529908cb 10950 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 10951
43f3e411 10952 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10953 }
10954 }
3019eac3
DE
10955}
10956
c24bdb02
KS
10957void
10958dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10959{
f4dc4d17
DE
10960 struct type_unit_group *tu_group;
10961 int first_time;
3019eac3 10962 struct attribute *attr;
9c541725 10963 unsigned int i;
0186c6a7 10964 struct signatured_type *sig_type;
3019eac3 10965
f4dc4d17 10966 gdb_assert (per_cu->is_debug_types);
0186c6a7 10967 sig_type = (struct signatured_type *) per_cu;
3019eac3 10968
c24bdb02 10969 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10970
f4dc4d17 10971 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10972 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10973 if (sig_type->type_unit_group == NULL)
c24bdb02 10974 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10975 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10976
10977 /* If we've already processed this stmt_list there's no real need to
10978 do it again, we could fake it and just recreate the part we need
10979 (file name,index -> symtab mapping). If data shows this optimization
10980 is useful we can do it then. */
8adb8487
TT
10981 type_unit_group_unshareable *tug_unshare
10982 = per_objfile->get_type_unit_group_unshareable (tu_group);
10983 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
10984
10985 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10986 debug info. */
fff8551c 10987 line_header_up lh;
d4df075e 10988 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 10989 {
d4df075e 10990 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 10991 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10992 }
10993 if (lh == NULL)
10994 {
10995 if (first_time)
c24bdb02 10996 start_symtab ("", NULL, 0);
f4dc4d17
DE
10997 else
10998 {
8adb8487 10999 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11000 gdb_assert (m_builder == nullptr);
8adb8487 11001 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11002 m_builder.reset (new struct buildsym_compunit
11003 (COMPUNIT_OBJFILE (cust), "",
11004 COMPUNIT_DIRNAME (cust),
11005 compunit_language (cust),
11006 0, cust));
770479f2 11007 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11008 }
f4dc4d17 11009 return;
3019eac3
DE
11010 }
11011
c24bdb02
KS
11012 line_header = lh.release ();
11013 line_header_die_owner = die;
3019eac3 11014
f4dc4d17
DE
11015 if (first_time)
11016 {
c24bdb02 11017 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11018
1fd60fc0
DE
11019 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11020 still initializing it, and our caller (a few levels up)
11021 process_full_type_unit still needs to know if this is the first
11022 time. */
11023
8adb8487 11024 tug_unshare->symtabs
4ac93832
TT
11025 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11026 struct symtab *, line_header->file_names_size ());
3019eac3 11027
7ba99d21
AT
11028 auto &file_names = line_header->file_names ();
11029 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11030 {
7ba99d21 11031 file_entry &fe = file_names[i];
c24bdb02
KS
11032 dwarf2_start_subfile (this, fe.name,
11033 fe.include_dir (line_header));
11034 buildsym_compunit *b = get_builder ();
11035 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11036 {
4c8aa72d
PA
11037 /* NOTE: start_subfile will recognize when it's been
11038 passed a file it has already seen. So we can't
11039 assume there's a simple mapping from
11040 cu->line_header->file_names to subfiles, plus
11041 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11042 b->get_current_subfile ()->symtab
11043 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11044 }
11045
c24bdb02 11046 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11047 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11048 }
11049 }
11050 else
3019eac3 11051 {
c24bdb02 11052 gdb_assert (m_builder == nullptr);
8adb8487 11053 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11054 m_builder.reset (new struct buildsym_compunit
11055 (COMPUNIT_OBJFILE (cust), "",
11056 COMPUNIT_DIRNAME (cust),
11057 compunit_language (cust),
11058 0, cust));
770479f2 11059 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11060
7ba99d21
AT
11061 auto &file_names = line_header->file_names ();
11062 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11063 {
7ba99d21 11064 file_entry &fe = file_names[i];
8adb8487 11065 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11066 }
3019eac3
DE
11067 }
11068
f4dc4d17
DE
11069 /* The main symtab is allocated last. Type units don't have DW_AT_name
11070 so they don't have a "real" (so to speak) symtab anyway.
11071 There is later code that will assign the main symtab to all symbols
11072 that don't have one. We need to handle the case of a symbol with a
11073 missing symtab (DW_AT_decl_file) anyway. */
11074}
3019eac3 11075
f4dc4d17
DE
11076/* Process DW_TAG_type_unit.
11077 For TUs we want to skip the first top level sibling if it's not the
11078 actual type being defined by this TU. In this case the first top
11079 level sibling is there to provide context only. */
3019eac3 11080
f4dc4d17
DE
11081static void
11082read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11083{
11084 struct die_info *child_die;
3019eac3 11085
f4dc4d17
DE
11086 prepare_one_comp_unit (cu, die, language_minimal);
11087
11088 /* Initialize (or reinitialize) the machinery for building symtabs.
11089 We do this before processing child DIEs, so that the line header table
11090 is available for DW_AT_decl_file. */
c24bdb02 11091 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11092
11093 if (die->child != NULL)
11094 {
11095 child_die = die->child;
11096 while (child_die && child_die->tag)
11097 {
11098 process_die (child_die, cu);
436c571c 11099 child_die = child_die->sibling;
f4dc4d17
DE
11100 }
11101 }
3019eac3
DE
11102}
11103\f
80626a55
DE
11104/* DWO/DWP files.
11105
11106 http://gcc.gnu.org/wiki/DebugFission
11107 http://gcc.gnu.org/wiki/DebugFissionDWP
11108
11109 To simplify handling of both DWO files ("object" files with the DWARF info)
11110 and DWP files (a file with the DWOs packaged up into one file), we treat
11111 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11112
11113static hashval_t
11114hash_dwo_file (const void *item)
11115{
9a3c8263 11116 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11117 hashval_t hash;
3019eac3 11118
a2ce51a0
DE
11119 hash = htab_hash_string (dwo_file->dwo_name);
11120 if (dwo_file->comp_dir != NULL)
11121 hash += htab_hash_string (dwo_file->comp_dir);
11122 return hash;
3019eac3
DE
11123}
11124
11125static int
11126eq_dwo_file (const void *item_lhs, const void *item_rhs)
11127{
9a3c8263
SM
11128 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11129 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11130
a2ce51a0
DE
11131 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11132 return 0;
11133 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11134 return lhs->comp_dir == rhs->comp_dir;
11135 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11136}
11137
11138/* Allocate a hash table for DWO files. */
11139
51ac9db5 11140static htab_up
298e9637 11141allocate_dwo_file_hash_table ()
3019eac3 11142{
51ac9db5
SM
11143 auto delete_dwo_file = [] (void *item)
11144 {
11145 struct dwo_file *dwo_file = (struct dwo_file *) item;
11146
11147 delete dwo_file;
11148 };
11149
bc68fb19
TT
11150 return htab_up (htab_create_alloc (41,
11151 hash_dwo_file,
11152 eq_dwo_file,
11153 delete_dwo_file,
11154 xcalloc, xfree));
3019eac3
DE
11155}
11156
80626a55
DE
11157/* Lookup DWO file DWO_NAME. */
11158
11159static void **
976ca316 11160lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11161 const char *dwo_name,
11162 const char *comp_dir)
80626a55
DE
11163{
11164 struct dwo_file find_entry;
11165 void **slot;
11166
976ca316
SM
11167 if (per_objfile->per_bfd->dwo_files == NULL)
11168 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11169
0ac5b59e
DE
11170 find_entry.dwo_name = dwo_name;
11171 find_entry.comp_dir = comp_dir;
976ca316 11172 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11173 INSERT);
80626a55
DE
11174
11175 return slot;
11176}
11177
3019eac3
DE
11178static hashval_t
11179hash_dwo_unit (const void *item)
11180{
9a3c8263 11181 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11182
11183 /* This drops the top 32 bits of the id, but is ok for a hash. */
11184 return dwo_unit->signature;
11185}
11186
11187static int
11188eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11189{
9a3c8263
SM
11190 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11191 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11192
11193 /* The signature is assumed to be unique within the DWO file.
11194 So while object file CU dwo_id's always have the value zero,
11195 that's OK, assuming each object file DWO file has only one CU,
11196 and that's the rule for now. */
11197 return lhs->signature == rhs->signature;
11198}
11199
11200/* Allocate a hash table for DWO CUs,TUs.
11201 There is one of these tables for each of CUs,TUs for each DWO file. */
11202
b0b6a987 11203static htab_up
298e9637 11204allocate_dwo_unit_table ()
3019eac3
DE
11205{
11206 /* Start out with a pretty small number.
11207 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11208 return htab_up (htab_create_alloc (3,
11209 hash_dwo_unit,
11210 eq_dwo_unit,
11211 NULL, xcalloc, xfree));
3019eac3
DE
11212}
11213
19c3d4c9 11214/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11215
11216static void
19c3d4c9
DE
11217create_dwo_cu_reader (const struct die_reader_specs *reader,
11218 const gdb_byte *info_ptr,
11219 struct die_info *comp_unit_die,
c0ab21c2
TT
11220 struct dwo_file *dwo_file,
11221 struct dwo_unit *dwo_unit)
3019eac3
DE
11222{
11223 struct dwarf2_cu *cu = reader->cu;
9c541725 11224 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11225 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11226
a084a2a6
AT
11227 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11228 if (!signature.has_value ())
3019eac3 11229 {
b98664d3 11230 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11231 " its dwo_id [in module %s]"),
9d8780f0 11232 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11233 return;
11234 }
11235
3019eac3 11236 dwo_unit->dwo_file = dwo_file;
a084a2a6 11237 dwo_unit->signature = *signature;
8a0459fd 11238 dwo_unit->section = section;
9c541725 11239 dwo_unit->sect_off = sect_off;
3019eac3
DE
11240 dwo_unit->length = cu->per_cu->length;
11241
6f738b01
SM
11242 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11243 sect_offset_str (sect_off),
11244 hex_string (dwo_unit->signature));
3019eac3
DE
11245}
11246
33c5cd75 11247/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11248 Note: This function processes DWO files only, not DWP files. */
3019eac3 11249
33c5cd75 11250static void
976ca316 11251create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11252 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11253 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11254{
976ca316
SM
11255 struct objfile *objfile = per_objfile->objfile;
11256 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11257 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11258
96b79293 11259 section.read (objfile);
33c5cd75 11260 info_ptr = section.buffer;
3019eac3
DE
11261
11262 if (info_ptr == NULL)
33c5cd75 11263 return;
3019eac3 11264
6f738b01
SM
11265 dwarf_read_debug_printf ("Reading %s for %s:",
11266 section.get_name (),
11267 section.get_file_name ());
3019eac3 11268
33c5cd75 11269 end_ptr = info_ptr + section.size;
3019eac3
DE
11270 while (info_ptr < end_ptr)
11271 {
11272 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11273 struct dwo_unit read_unit {};
33c5cd75
DB
11274 struct dwo_unit *dwo_unit;
11275 void **slot;
11276 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11277
11278 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11279 per_cu.per_bfd = per_bfd;
3019eac3 11280 per_cu.is_debug_types = 0;
33c5cd75
DB
11281 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11282 per_cu.section = &section;
11283
976ca316 11284 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11285 if (!reader.dummy_p)
11286 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11287 &dwo_file, &read_unit);
33c5cd75
DB
11288 info_ptr += per_cu.length;
11289
11290 // If the unit could not be parsed, skip it.
c0ab21c2 11291 if (read_unit.dwo_file == NULL)
33c5cd75 11292 continue;
3019eac3 11293
33c5cd75 11294 if (cus_htab == NULL)
298e9637 11295 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11296
1859c670 11297 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11298 struct dwo_unit);
c0ab21c2 11299 *dwo_unit = read_unit;
b0b6a987 11300 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11301 gdb_assert (slot != NULL);
11302 if (*slot != NULL)
19c3d4c9 11303 {
33c5cd75
DB
11304 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11305 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11306
b98664d3 11307 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11308 " the entry at offset %s, signature %s"),
11309 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11310 hex_string (dwo_unit->signature));
19c3d4c9 11311 }
33c5cd75 11312 *slot = (void *)dwo_unit;
3019eac3 11313 }
3019eac3
DE
11314}
11315
80626a55
DE
11316/* DWP file .debug_{cu,tu}_index section format:
11317 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11318 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11319
11320 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11321 officially standard DWP format was published with DWARF v5 and is called
11322 Version 5. There are no versions 3 or 4.
80626a55 11323
d2415c6c
DE
11324 DWP Version 1:
11325
80626a55
DE
11326 Both index sections have the same format, and serve to map a 64-bit
11327 signature to a set of section numbers. Each section begins with a header,
11328 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11329 indexes, and a pool of 32-bit section numbers. The index sections will be
11330 aligned at 8-byte boundaries in the file.
11331
d2415c6c
DE
11332 The index section header consists of:
11333
11334 V, 32 bit version number
11335 -, 32 bits unused
11336 N, 32 bit number of compilation units or type units in the index
11337 M, 32 bit number of slots in the hash table
80626a55 11338
d2415c6c 11339 Numbers are recorded using the byte order of the application binary.
80626a55 11340
d2415c6c
DE
11341 The hash table begins at offset 16 in the section, and consists of an array
11342 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11343 order of the application binary). Unused slots in the hash table are 0.
11344 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11345
d2415c6c
DE
11346 The parallel table begins immediately after the hash table
11347 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11348 array of 32-bit indexes (using the byte order of the application binary),
11349 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11350 table contains a 32-bit index into the pool of section numbers. For unused
11351 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11352
73869dc2
DE
11353 The pool of section numbers begins immediately following the hash table
11354 (at offset 16 + 12 * M from the beginning of the section). The pool of
11355 section numbers consists of an array of 32-bit words (using the byte order
11356 of the application binary). Each item in the array is indexed starting
11357 from 0. The hash table entry provides the index of the first section
11358 number in the set. Additional section numbers in the set follow, and the
11359 set is terminated by a 0 entry (section number 0 is not used in ELF).
11360
11361 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11362 section must be the first entry in the set, and the .debug_abbrev.dwo must
11363 be the second entry. Other members of the set may follow in any order.
11364
11365 ---
11366
d2854d8d 11367 DWP Versions 2 and 5:
73869dc2 11368
d2854d8d 11369 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11370 and the entries in the index tables are now offsets into these sections.
11371 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11372 section.
11373
11374 Index Section Contents:
11375 Header
11376 Hash Table of Signatures dwp_hash_table.hash_table
11377 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11378 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11379 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11380
11381 The index section header consists of:
11382
11383 V, 32 bit version number
11384 L, 32 bit number of columns in the table of section offsets
11385 N, 32 bit number of compilation units or type units in the index
11386 M, 32 bit number of slots in the hash table
11387
11388 Numbers are recorded using the byte order of the application binary.
11389
11390 The hash table has the same format as version 1.
11391 The parallel table of indices has the same format as version 1,
11392 except that the entries are origin-1 indices into the table of sections
11393 offsets and the table of section sizes.
11394
11395 The table of offsets begins immediately following the parallel table
11396 (at offset 16 + 12 * M from the beginning of the section). The table is
11397 a two-dimensional array of 32-bit words (using the byte order of the
11398 application binary), with L columns and N+1 rows, in row-major order.
11399 Each row in the array is indexed starting from 0. The first row provides
11400 a key to the remaining rows: each column in this row provides an identifier
11401 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11402 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11403
11404 DW_SECT_INFO 1 .debug_info.dwo
11405 DW_SECT_TYPES 2 .debug_types.dwo
11406 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11407 DW_SECT_LINE 4 .debug_line.dwo
11408 DW_SECT_LOC 5 .debug_loc.dwo
11409 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11410 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11411 DW_SECT_MACRO 8 .debug_macro.dwo
11412
d2854d8d
CT
11413 The section identifiers for Version 5 are:
11414
11415 DW_SECT_INFO_V5 1 .debug_info.dwo
11416 DW_SECT_RESERVED_V5 2 --
11417 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11418 DW_SECT_LINE_V5 4 .debug_line.dwo
11419 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11420 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11421 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11422 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11423
73869dc2
DE
11424 The offsets provided by the CU and TU index sections are the base offsets
11425 for the contributions made by each CU or TU to the corresponding section
11426 in the package file. Each CU and TU header contains an abbrev_offset
11427 field, used to find the abbreviations table for that CU or TU within the
11428 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11429 be interpreted as relative to the base offset given in the index section.
11430 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11431 should be interpreted as relative to the base offset for .debug_line.dwo,
11432 and offsets into other debug sections obtained from DWARF attributes should
11433 also be interpreted as relative to the corresponding base offset.
11434
11435 The table of sizes begins immediately following the table of offsets.
11436 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11437 with L columns and N rows, in row-major order. Each row in the array is
11438 indexed starting from 1 (row 0 is shared by the two tables).
11439
11440 ---
11441
11442 Hash table lookup is handled the same in version 1 and 2:
11443
11444 We assume that N and M will not exceed 2^32 - 1.
11445 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11446
d2415c6c
DE
11447 Given a 64-bit compilation unit signature or a type signature S, an entry
11448 in the hash table is located as follows:
80626a55 11449
d2415c6c
DE
11450 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11451 the low-order k bits all set to 1.
80626a55 11452
d2415c6c 11453 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11454
d2415c6c
DE
11455 3) If the hash table entry at index H matches the signature, use that
11456 entry. If the hash table entry at index H is unused (all zeroes),
11457 terminate the search: the signature is not present in the table.
80626a55 11458
d2415c6c 11459 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11460
d2415c6c 11461 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11462 to stop at an unused slot or find the match. */
80626a55
DE
11463
11464/* Create a hash table to map DWO IDs to their CU/TU entry in
11465 .debug_{info,types}.dwo in DWP_FILE.
11466 Returns NULL if there isn't one.
11467 Note: This function processes DWP files only, not DWO files. */
11468
11469static struct dwp_hash_table *
976ca316 11470create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11471 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11472{
976ca316 11473 struct objfile *objfile = per_objfile->objfile;
400174b1 11474 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11475 const gdb_byte *index_ptr, *index_end;
80626a55 11476 struct dwarf2_section_info *index;
73869dc2 11477 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11478 struct dwp_hash_table *htab;
11479
11480 if (is_debug_types)
11481 index = &dwp_file->sections.tu_index;
11482 else
11483 index = &dwp_file->sections.cu_index;
11484
96b79293 11485 if (index->empty ())
80626a55 11486 return NULL;
96b79293 11487 index->read (objfile);
80626a55
DE
11488
11489 index_ptr = index->buffer;
11490 index_end = index_ptr + index->size;
11491
d2854d8d
CT
11492 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11493 For now it's safe to just read 4 bytes (particularly as it's difficult to
11494 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11495 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11496 index_ptr += 4;
d2854d8d 11497 if (version == 2 || version == 5)
73869dc2
DE
11498 nr_columns = read_4_bytes (dbfd, index_ptr);
11499 else
11500 nr_columns = 0;
11501 index_ptr += 4;
80626a55
DE
11502 nr_units = read_4_bytes (dbfd, index_ptr);
11503 index_ptr += 4;
11504 nr_slots = read_4_bytes (dbfd, index_ptr);
11505 index_ptr += 4;
11506
d2854d8d 11507 if (version != 1 && version != 2 && version != 5)
80626a55 11508 {
21aa081e 11509 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11510 " [in module %s]"),
21aa081e 11511 pulongest (version), dwp_file->name);
80626a55
DE
11512 }
11513 if (nr_slots != (nr_slots & -nr_slots))
11514 {
21aa081e 11515 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11516 " is not power of 2 [in module %s]"),
21aa081e 11517 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11518 }
11519
976ca316 11520 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11521 htab->version = version;
11522 htab->nr_columns = nr_columns;
80626a55
DE
11523 htab->nr_units = nr_units;
11524 htab->nr_slots = nr_slots;
11525 htab->hash_table = index_ptr;
11526 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11527
11528 /* Exit early if the table is empty. */
11529 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11530 || (version == 2 && nr_columns == 0)
11531 || (version == 5 && nr_columns == 0))
73869dc2
DE
11532 {
11533 /* All must be zero. */
11534 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11535 || (version == 2 && nr_columns != 0)
11536 || (version == 5 && nr_columns != 0))
73869dc2 11537 {
b98664d3 11538 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11539 " all zero [in modules %s]"),
11540 dwp_file->name);
11541 }
11542 return htab;
11543 }
11544
11545 if (version == 1)
11546 {
11547 htab->section_pool.v1.indices =
11548 htab->unit_table + sizeof (uint32_t) * nr_slots;
11549 /* It's harder to decide whether the section is too small in v1.
11550 V1 is deprecated anyway so we punt. */
11551 }
d2854d8d 11552 else if (version == 2)
73869dc2
DE
11553 {
11554 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11555 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11556 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11557 /* Reverse map for error checking. */
11558 int ids_seen[DW_SECT_MAX + 1];
11559 int i;
11560
11561 if (nr_columns < 2)
11562 {
11563 error (_("Dwarf Error: bad DWP hash table, too few columns"
11564 " in section table [in module %s]"),
11565 dwp_file->name);
11566 }
11567 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11568 {
11569 error (_("Dwarf Error: bad DWP hash table, too many columns"
11570 " in section table [in module %s]"),
11571 dwp_file->name);
11572 }
04fd5eed
GB
11573 memset (ids, 255, sizeof_ids);
11574 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11575 for (i = 0; i < nr_columns; ++i)
11576 {
11577 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11578
11579 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11580 {
11581 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11582 " in section table [in module %s]"),
11583 id, dwp_file->name);
11584 }
11585 if (ids_seen[id] != -1)
11586 {
11587 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11588 " id %d in section table [in module %s]"),
11589 id, dwp_file->name);
11590 }
11591 ids_seen[id] = i;
11592 ids[i] = id;
11593 }
11594 /* Must have exactly one info or types section. */
11595 if (((ids_seen[DW_SECT_INFO] != -1)
11596 + (ids_seen[DW_SECT_TYPES] != -1))
11597 != 1)
11598 {
11599 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11600 " DWO info/types section [in module %s]"),
11601 dwp_file->name);
11602 }
11603 /* Must have an abbrev section. */
11604 if (ids_seen[DW_SECT_ABBREV] == -1)
11605 {
11606 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11607 " section [in module %s]"),
11608 dwp_file->name);
11609 }
11610 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11611 htab->section_pool.v2.sizes =
11612 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11613 * nr_units * nr_columns);
11614 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11615 * nr_units * nr_columns))
11616 > index_end)
11617 {
11618 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11619 " [in module %s]"),
11620 dwp_file->name);
11621 }
11622 }
d2854d8d
CT
11623 else /* version == 5 */
11624 {
11625 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11626 int *ids = htab->section_pool.v5.section_ids;
11627 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11628 /* Reverse map for error checking. */
11629 int ids_seen[DW_SECT_MAX_V5 + 1];
11630
11631 if (nr_columns < 2)
11632 {
11633 error (_("Dwarf Error: bad DWP hash table, too few columns"
11634 " in section table [in module %s]"),
11635 dwp_file->name);
11636 }
11637 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11638 {
11639 error (_("Dwarf Error: bad DWP hash table, too many columns"
11640 " in section table [in module %s]"),
11641 dwp_file->name);
11642 }
11643 memset (ids, 255, sizeof_ids);
11644 memset (ids_seen, 255, sizeof (ids_seen));
11645 for (int i = 0; i < nr_columns; ++i)
11646 {
11647 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11648
11649 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11650 {
11651 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11652 " in section table [in module %s]"),
11653 id, dwp_file->name);
11654 }
11655 if (ids_seen[id] != -1)
11656 {
11657 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11658 " id %d in section table [in module %s]"),
11659 id, dwp_file->name);
11660 }
11661 ids_seen[id] = i;
11662 ids[i] = id;
11663 }
11664 /* Must have seen an info section. */
11665 if (ids_seen[DW_SECT_INFO_V5] == -1)
11666 {
11667 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11668 " DWO info/types section [in module %s]"),
11669 dwp_file->name);
11670 }
11671 /* Must have an abbrev section. */
11672 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11673 {
11674 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11675 " section [in module %s]"),
11676 dwp_file->name);
11677 }
11678 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11679 htab->section_pool.v5.sizes
11680 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11681 * nr_units * nr_columns);
11682 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11683 * nr_units * nr_columns))
11684 > index_end)
11685 {
11686 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11687 " [in module %s]"),
11688 dwp_file->name);
11689 }
11690 }
80626a55
DE
11691
11692 return htab;
11693}
11694
11695/* Update SECTIONS with the data from SECTP.
11696
5bb6e9dd
TT
11697 This function is like the other "locate" section routines, but in
11698 this context the sections to read comes from the DWP V1 hash table,
11699 not the full ELF section table.
80626a55
DE
11700
11701 The result is non-zero for success, or zero if an error was found. */
11702
11703static int
73869dc2
DE
11704locate_v1_virtual_dwo_sections (asection *sectp,
11705 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11706{
11707 const struct dwop_section_names *names = &dwop_section_names;
11708
fbedd546 11709 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
11710 {
11711 /* There can be only one. */
049412e3 11712 if (sections->abbrev.s.section != NULL)
80626a55 11713 return 0;
049412e3 11714 sections->abbrev.s.section = sectp;
fd361982 11715 sections->abbrev.size = bfd_section_size (sectp);
80626a55 11716 }
fbedd546
TT
11717 else if (names->info_dwo.matches (sectp->name)
11718 || names->types_dwo.matches (sectp->name))
80626a55
DE
11719 {
11720 /* There can be only one. */
049412e3 11721 if (sections->info_or_types.s.section != NULL)
80626a55 11722 return 0;
049412e3 11723 sections->info_or_types.s.section = sectp;
fd361982 11724 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 11725 }
fbedd546 11726 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
11727 {
11728 /* There can be only one. */
049412e3 11729 if (sections->line.s.section != NULL)
80626a55 11730 return 0;
049412e3 11731 sections->line.s.section = sectp;
fd361982 11732 sections->line.size = bfd_section_size (sectp);
80626a55 11733 }
fbedd546 11734 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
11735 {
11736 /* There can be only one. */
049412e3 11737 if (sections->loc.s.section != NULL)
80626a55 11738 return 0;
049412e3 11739 sections->loc.s.section = sectp;
fd361982 11740 sections->loc.size = bfd_section_size (sectp);
80626a55 11741 }
fbedd546 11742 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
11743 {
11744 /* There can be only one. */
049412e3 11745 if (sections->macinfo.s.section != NULL)
80626a55 11746 return 0;
049412e3 11747 sections->macinfo.s.section = sectp;
fd361982 11748 sections->macinfo.size = bfd_section_size (sectp);
80626a55 11749 }
fbedd546 11750 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
11751 {
11752 /* There can be only one. */
049412e3 11753 if (sections->macro.s.section != NULL)
80626a55 11754 return 0;
049412e3 11755 sections->macro.s.section = sectp;
fd361982 11756 sections->macro.size = bfd_section_size (sectp);
80626a55 11757 }
fbedd546 11758 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
11759 {
11760 /* There can be only one. */
049412e3 11761 if (sections->str_offsets.s.section != NULL)
80626a55 11762 return 0;
049412e3 11763 sections->str_offsets.s.section = sectp;
fd361982 11764 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11765 }
11766 else
11767 {
11768 /* No other kind of section is valid. */
11769 return 0;
11770 }
11771
11772 return 1;
11773}
11774
73869dc2
DE
11775/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11776 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11777 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11778 This is for DWP version 1 files. */
80626a55
DE
11779
11780static struct dwo_unit *
976ca316 11781create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11782 struct dwp_file *dwp_file,
73869dc2
DE
11783 uint32_t unit_index,
11784 const char *comp_dir,
11785 ULONGEST signature, int is_debug_types)
80626a55 11786{
73869dc2
DE
11787 const struct dwp_hash_table *dwp_htab =
11788 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11789 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11790 const char *kind = is_debug_types ? "TU" : "CU";
11791 struct dwo_file *dwo_file;
11792 struct dwo_unit *dwo_unit;
73869dc2 11793 struct virtual_v1_dwo_sections sections;
80626a55 11794 void **dwo_file_slot;
80626a55
DE
11795 int i;
11796
73869dc2
DE
11797 gdb_assert (dwp_file->version == 1);
11798
6f738b01
SM
11799 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11800 kind, pulongest (unit_index), hex_string (signature),
11801 dwp_file->name);
80626a55 11802
19ac8c2e 11803 /* Fetch the sections of this DWO unit.
80626a55
DE
11804 Put a limit on the number of sections we look for so that bad data
11805 doesn't cause us to loop forever. */
11806
73869dc2 11807#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11808 (1 /* .debug_info or .debug_types */ \
11809 + 1 /* .debug_abbrev */ \
11810 + 1 /* .debug_line */ \
11811 + 1 /* .debug_loc */ \
11812 + 1 /* .debug_str_offsets */ \
19ac8c2e 11813 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11814 + 1 /* trailing zero */)
11815
11816 memset (&sections, 0, sizeof (sections));
80626a55 11817
73869dc2 11818 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11819 {
11820 asection *sectp;
11821 uint32_t section_nr =
11822 read_4_bytes (dbfd,
73869dc2
DE
11823 dwp_htab->section_pool.v1.indices
11824 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11825
11826 if (section_nr == 0)
11827 break;
11828 if (section_nr >= dwp_file->num_sections)
11829 {
11830 error (_("Dwarf Error: bad DWP hash table, section number too large"
11831 " [in module %s]"),
11832 dwp_file->name);
11833 }
11834
11835 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11836 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11837 {
11838 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11839 " [in module %s]"),
11840 dwp_file->name);
11841 }
11842 }
11843
11844 if (i < 2
96b79293
TT
11845 || sections.info_or_types.empty ()
11846 || sections.abbrev.empty ())
80626a55
DE
11847 {
11848 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11849 " [in module %s]"),
11850 dwp_file->name);
11851 }
73869dc2 11852 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11853 {
11854 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11855 " [in module %s]"),
11856 dwp_file->name);
11857 }
11858
11859 /* It's easier for the rest of the code if we fake a struct dwo_file and
11860 have dwo_unit "live" in that. At least for now.
11861
11862 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11863 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11864 file, we can combine them back into a virtual DWO file to save space
11865 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11866 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11867
791afaa2
TT
11868 std::string virtual_dwo_name =
11869 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11870 sections.abbrev.get_id (),
11871 sections.line.get_id (),
11872 sections.loc.get_id (),
11873 sections.str_offsets.get_id ());
80626a55 11874 /* Can we use an existing virtual DWO file? */
976ca316 11875 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 11876 comp_dir);
80626a55
DE
11877 /* Create one if necessary. */
11878 if (*dwo_file_slot == NULL)
11879 {
6f738b01
SM
11880 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11881 virtual_dwo_name.c_str ());
11882
51ac9db5 11883 dwo_file = new struct dwo_file;
976ca316 11884 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11885 dwo_file->comp_dir = comp_dir;
80626a55
DE
11886 dwo_file->sections.abbrev = sections.abbrev;
11887 dwo_file->sections.line = sections.line;
11888 dwo_file->sections.loc = sections.loc;
11889 dwo_file->sections.macinfo = sections.macinfo;
11890 dwo_file->sections.macro = sections.macro;
11891 dwo_file->sections.str_offsets = sections.str_offsets;
11892 /* The "str" section is global to the entire DWP file. */
11893 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11894 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11895 there's no need to record it in dwo_file.
11896 Also, we can't simply record type sections in dwo_file because
11897 we record a pointer into the vector in dwo_unit. As we collect more
11898 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11899 for it, invalidating all copies of pointers into the previous
11900 contents. */
80626a55
DE
11901 *dwo_file_slot = dwo_file;
11902 }
11903 else
11904 {
6f738b01
SM
11905 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11906 virtual_dwo_name.c_str ());
11907
9a3c8263 11908 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11909 }
80626a55 11910
976ca316 11911 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11912 dwo_unit->dwo_file = dwo_file;
11913 dwo_unit->signature = signature;
8d749320 11914 dwo_unit->section =
976ca316 11915 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11916 *dwo_unit->section = sections.info_or_types;
57d63ce2 11917 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11918
11919 return dwo_unit;
11920}
11921
d2854d8d
CT
11922/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11923 simplify them. Given a pointer to the containing section SECTION, and
11924 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11925 virtual section of just that piece. */
73869dc2
DE
11926
11927static struct dwarf2_section_info
d2854d8d
CT
11928create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11929 struct dwarf2_section_info *section,
11930 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
11931{
11932 struct dwarf2_section_info result;
11933 asection *sectp;
11934
11935 gdb_assert (section != NULL);
11936 gdb_assert (!section->is_virtual);
11937
11938 memset (&result, 0, sizeof (result));
11939 result.s.containing_section = section;
dc4ccb6f 11940 result.is_virtual = true;
73869dc2
DE
11941
11942 if (size == 0)
11943 return result;
11944
96b79293 11945 sectp = section->get_bfd_section ();
73869dc2
DE
11946
11947 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11948 bounds of the real section. This is a pretty-rare event, so just
11949 flag an error (easier) instead of a warning and trying to cope. */
11950 if (sectp == NULL
fd361982 11951 || offset + size > bfd_section_size (sectp))
73869dc2 11952 {
d2854d8d 11953 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 11954 " in section %s [in module %s]"),
fd361982 11955 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 11956 objfile_name (per_objfile->objfile));
73869dc2
DE
11957 }
11958
11959 result.virtual_offset = offset;
11960 result.size = size;
11961 return result;
11962}
11963
11964/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11965 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11966 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11967 This is for DWP version 2 files. */
11968
11969static struct dwo_unit *
976ca316 11970create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 11971 struct dwp_file *dwp_file,
73869dc2
DE
11972 uint32_t unit_index,
11973 const char *comp_dir,
11974 ULONGEST signature, int is_debug_types)
11975{
73869dc2
DE
11976 const struct dwp_hash_table *dwp_htab =
11977 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11978 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11979 const char *kind = is_debug_types ? "TU" : "CU";
11980 struct dwo_file *dwo_file;
11981 struct dwo_unit *dwo_unit;
d2854d8d 11982 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 11983 void **dwo_file_slot;
73869dc2
DE
11984 int i;
11985
11986 gdb_assert (dwp_file->version == 2);
11987
6f738b01
SM
11988 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11989 kind, pulongest (unit_index), hex_string (signature),
11990 dwp_file->name);
73869dc2
DE
11991
11992 /* Fetch the section offsets of this DWO unit. */
11993
11994 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11995
11996 for (i = 0; i < dwp_htab->nr_columns; ++i)
11997 {
11998 uint32_t offset = read_4_bytes (dbfd,
11999 dwp_htab->section_pool.v2.offsets
12000 + (((unit_index - 1) * dwp_htab->nr_columns
12001 + i)
12002 * sizeof (uint32_t)));
12003 uint32_t size = read_4_bytes (dbfd,
12004 dwp_htab->section_pool.v2.sizes
12005 + (((unit_index - 1) * dwp_htab->nr_columns
12006 + i)
12007 * sizeof (uint32_t)));
12008
12009 switch (dwp_htab->section_pool.v2.section_ids[i])
12010 {
12011 case DW_SECT_INFO:
12012 case DW_SECT_TYPES:
12013 sections.info_or_types_offset = offset;
12014 sections.info_or_types_size = size;
12015 break;
12016 case DW_SECT_ABBREV:
12017 sections.abbrev_offset = offset;
12018 sections.abbrev_size = size;
12019 break;
12020 case DW_SECT_LINE:
12021 sections.line_offset = offset;
12022 sections.line_size = size;
12023 break;
12024 case DW_SECT_LOC:
12025 sections.loc_offset = offset;
12026 sections.loc_size = size;
12027 break;
12028 case DW_SECT_STR_OFFSETS:
12029 sections.str_offsets_offset = offset;
12030 sections.str_offsets_size = size;
12031 break;
12032 case DW_SECT_MACINFO:
12033 sections.macinfo_offset = offset;
12034 sections.macinfo_size = size;
12035 break;
12036 case DW_SECT_MACRO:
12037 sections.macro_offset = offset;
12038 sections.macro_size = size;
12039 break;
12040 }
12041 }
12042
12043 /* It's easier for the rest of the code if we fake a struct dwo_file and
12044 have dwo_unit "live" in that. At least for now.
12045
12046 The DWP file can be made up of a random collection of CUs and TUs.
12047 However, for each CU + set of TUs that came from the same original DWO
12048 file, we can combine them back into a virtual DWO file to save space
12049 (fewer struct dwo_file objects to allocate). Remember that for really
12050 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12051
791afaa2
TT
12052 std::string virtual_dwo_name =
12053 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12054 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12055 (long) (sections.line_size ? sections.line_offset : 0),
12056 (long) (sections.loc_size ? sections.loc_offset : 0),
12057 (long) (sections.str_offsets_size
12058 ? sections.str_offsets_offset : 0));
73869dc2 12059 /* Can we use an existing virtual DWO file? */
976ca316 12060 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12061 comp_dir);
73869dc2
DE
12062 /* Create one if necessary. */
12063 if (*dwo_file_slot == NULL)
12064 {
6f738b01
SM
12065 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12066 virtual_dwo_name.c_str ());
12067
51ac9db5 12068 dwo_file = new struct dwo_file;
976ca316 12069 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12070 dwo_file->comp_dir = comp_dir;
12071 dwo_file->sections.abbrev =
d2854d8d
CT
12072 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12073 sections.abbrev_offset,
12074 sections.abbrev_size);
73869dc2 12075 dwo_file->sections.line =
d2854d8d
CT
12076 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12077 sections.line_offset,
12078 sections.line_size);
73869dc2 12079 dwo_file->sections.loc =
d2854d8d
CT
12080 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12081 sections.loc_offset, sections.loc_size);
73869dc2 12082 dwo_file->sections.macinfo =
d2854d8d
CT
12083 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12084 sections.macinfo_offset,
12085 sections.macinfo_size);
73869dc2 12086 dwo_file->sections.macro =
d2854d8d
CT
12087 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12088 sections.macro_offset,
12089 sections.macro_size);
73869dc2 12090 dwo_file->sections.str_offsets =
d2854d8d
CT
12091 create_dwp_v2_or_v5_section (per_objfile,
12092 &dwp_file->sections.str_offsets,
12093 sections.str_offsets_offset,
12094 sections.str_offsets_size);
73869dc2
DE
12095 /* The "str" section is global to the entire DWP file. */
12096 dwo_file->sections.str = dwp_file->sections.str;
12097 /* The info or types section is assigned below to dwo_unit,
12098 there's no need to record it in dwo_file.
12099 Also, we can't simply record type sections in dwo_file because
12100 we record a pointer into the vector in dwo_unit. As we collect more
12101 types we'll grow the vector and eventually have to reallocate space
12102 for it, invalidating all copies of pointers into the previous
12103 contents. */
12104 *dwo_file_slot = dwo_file;
12105 }
12106 else
12107 {
6f738b01
SM
12108 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12109 virtual_dwo_name.c_str ());
12110
9a3c8263 12111 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12112 }
73869dc2 12113
976ca316 12114 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12115 dwo_unit->dwo_file = dwo_file;
12116 dwo_unit->signature = signature;
8d749320 12117 dwo_unit->section =
976ca316 12118 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 12119 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 12120 (per_objfile,
d2854d8d
CT
12121 is_debug_types
12122 ? &dwp_file->sections.types
12123 : &dwp_file->sections.info,
12124 sections.info_or_types_offset,
12125 sections.info_or_types_size);
12126 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12127
12128 return dwo_unit;
12129}
12130
12131/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12132 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12133 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12134 This is for DWP version 5 files. */
12135
12136static struct dwo_unit *
12137create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12138 struct dwp_file *dwp_file,
12139 uint32_t unit_index,
12140 const char *comp_dir,
12141 ULONGEST signature, int is_debug_types)
12142{
12143 const struct dwp_hash_table *dwp_htab
12144 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12145 bfd *dbfd = dwp_file->dbfd.get ();
12146 const char *kind = is_debug_types ? "TU" : "CU";
12147 struct dwo_file *dwo_file;
12148 struct dwo_unit *dwo_unit;
12149 struct virtual_v2_or_v5_dwo_sections sections {};
12150 void **dwo_file_slot;
12151
12152 gdb_assert (dwp_file->version == 5);
12153
6f738b01
SM
12154 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12155 kind, pulongest (unit_index), hex_string (signature),
12156 dwp_file->name);
d2854d8d
CT
12157
12158 /* Fetch the section offsets of this DWO unit. */
12159
12160 /* memset (&sections, 0, sizeof (sections)); */
12161
12162 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12163 {
12164 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12165 dwp_htab->section_pool.v5.offsets
12166 + (((unit_index - 1)
12167 * dwp_htab->nr_columns
12168 + i)
12169 * sizeof (uint32_t)));
d2854d8d 12170 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12171 dwp_htab->section_pool.v5.sizes
12172 + (((unit_index - 1) * dwp_htab->nr_columns
12173 + i)
12174 * sizeof (uint32_t)));
d2854d8d
CT
12175
12176 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12177 {
12178 case DW_SECT_ABBREV_V5:
12179 sections.abbrev_offset = offset;
12180 sections.abbrev_size = size;
12181 break;
12182 case DW_SECT_INFO_V5:
12183 sections.info_or_types_offset = offset;
12184 sections.info_or_types_size = size;
12185 break;
12186 case DW_SECT_LINE_V5:
12187 sections.line_offset = offset;
12188 sections.line_size = size;
12189 break;
12190 case DW_SECT_LOCLISTS_V5:
12191 sections.loclists_offset = offset;
12192 sections.loclists_size = size;
12193 break;
12194 case DW_SECT_MACRO_V5:
12195 sections.macro_offset = offset;
12196 sections.macro_size = size;
12197 break;
12198 case DW_SECT_RNGLISTS_V5:
12199 sections.rnglists_offset = offset;
12200 sections.rnglists_size = size;
12201 break;
12202 case DW_SECT_STR_OFFSETS_V5:
12203 sections.str_offsets_offset = offset;
12204 sections.str_offsets_size = size;
12205 break;
12206 case DW_SECT_RESERVED_V5:
12207 default:
12208 break;
12209 }
d2854d8d
CT
12210 }
12211
12212 /* It's easier for the rest of the code if we fake a struct dwo_file and
12213 have dwo_unit "live" in that. At least for now.
12214
12215 The DWP file can be made up of a random collection of CUs and TUs.
12216 However, for each CU + set of TUs that came from the same original DWO
12217 file, we can combine them back into a virtual DWO file to save space
12218 (fewer struct dwo_file objects to allocate). Remember that for really
12219 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12220
12221 std::string virtual_dwo_name =
12222 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12223 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12224 (long) (sections.line_size ? sections.line_offset : 0),
12225 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12226 (long) (sections.str_offsets_size
12227 ? sections.str_offsets_offset : 0),
12228 (long) (sections.macro_size ? sections.macro_offset : 0),
12229 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12230 /* Can we use an existing virtual DWO file? */
12231 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12232 virtual_dwo_name.c_str (),
12233 comp_dir);
d2854d8d
CT
12234 /* Create one if necessary. */
12235 if (*dwo_file_slot == NULL)
12236 {
6f738b01
SM
12237 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12238 virtual_dwo_name.c_str ());
12239
d2854d8d
CT
12240 dwo_file = new struct dwo_file;
12241 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12242 dwo_file->comp_dir = comp_dir;
12243 dwo_file->sections.abbrev =
dda83cd7
SM
12244 create_dwp_v2_or_v5_section (per_objfile,
12245 &dwp_file->sections.abbrev,
12246 sections.abbrev_offset,
12247 sections.abbrev_size);
d2854d8d 12248 dwo_file->sections.line =
dda83cd7
SM
12249 create_dwp_v2_or_v5_section (per_objfile,
12250 &dwp_file->sections.line,
12251 sections.line_offset, sections.line_size);
d2854d8d 12252 dwo_file->sections.macro =
dda83cd7
SM
12253 create_dwp_v2_or_v5_section (per_objfile,
12254 &dwp_file->sections.macro,
12255 sections.macro_offset,
12256 sections.macro_size);
d2854d8d 12257 dwo_file->sections.loclists =
dda83cd7
SM
12258 create_dwp_v2_or_v5_section (per_objfile,
12259 &dwp_file->sections.loclists,
12260 sections.loclists_offset,
12261 sections.loclists_size);
d2854d8d 12262 dwo_file->sections.rnglists =
dda83cd7
SM
12263 create_dwp_v2_or_v5_section (per_objfile,
12264 &dwp_file->sections.rnglists,
12265 sections.rnglists_offset,
12266 sections.rnglists_size);
d2854d8d 12267 dwo_file->sections.str_offsets =
dda83cd7
SM
12268 create_dwp_v2_or_v5_section (per_objfile,
12269 &dwp_file->sections.str_offsets,
12270 sections.str_offsets_offset,
12271 sections.str_offsets_size);
d2854d8d
CT
12272 /* The "str" section is global to the entire DWP file. */
12273 dwo_file->sections.str = dwp_file->sections.str;
12274 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12275 there's no need to record it in dwo_file.
12276 Also, we can't simply record type sections in dwo_file because
12277 we record a pointer into the vector in dwo_unit. As we collect more
12278 types we'll grow the vector and eventually have to reallocate space
12279 for it, invalidating all copies of pointers into the previous
12280 contents. */
d2854d8d
CT
12281 *dwo_file_slot = dwo_file;
12282 }
12283 else
12284 {
6f738b01
SM
12285 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12286 virtual_dwo_name.c_str ());
12287
d2854d8d
CT
12288 dwo_file = (struct dwo_file *) *dwo_file_slot;
12289 }
12290
12291 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12292 dwo_unit->dwo_file = dwo_file;
12293 dwo_unit->signature = signature;
12294 dwo_unit->section
12295 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12296 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12297 &dwp_file->sections.info,
12298 sections.info_or_types_offset,
12299 sections.info_or_types_size);
73869dc2
DE
12300 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12301
12302 return dwo_unit;
12303}
12304
57d63ce2
DE
12305/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12306 Returns NULL if the signature isn't found. */
80626a55
DE
12307
12308static struct dwo_unit *
976ca316 12309lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12310 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12311 ULONGEST signature, int is_debug_types)
80626a55 12312{
57d63ce2
DE
12313 const struct dwp_hash_table *dwp_htab =
12314 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12315 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12316 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12317 uint32_t hash = signature & mask;
12318 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12319 unsigned int i;
12320 void **slot;
870f88f7 12321 struct dwo_unit find_dwo_cu;
80626a55
DE
12322
12323 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12324 find_dwo_cu.signature = signature;
19ac8c2e 12325 slot = htab_find_slot (is_debug_types
48b490f2
TT
12326 ? dwp_file->loaded_tus.get ()
12327 : dwp_file->loaded_cus.get (),
19ac8c2e 12328 &find_dwo_cu, INSERT);
80626a55
DE
12329
12330 if (*slot != NULL)
9a3c8263 12331 return (struct dwo_unit *) *slot;
80626a55
DE
12332
12333 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12334 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12335 {
12336 ULONGEST signature_in_table;
12337
12338 signature_in_table =
57d63ce2 12339 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12340 if (signature_in_table == signature)
12341 {
57d63ce2
DE
12342 uint32_t unit_index =
12343 read_4_bytes (dbfd,
12344 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12345
73869dc2
DE
12346 if (dwp_file->version == 1)
12347 {
976ca316
SM
12348 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12349 unit_index, comp_dir,
12350 signature, is_debug_types);
73869dc2 12351 }
d2854d8d 12352 else if (dwp_file->version == 2)
73869dc2 12353 {
976ca316
SM
12354 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12355 unit_index, comp_dir,
12356 signature, is_debug_types);
73869dc2 12357 }
d2854d8d
CT
12358 else /* version == 5 */
12359 {
12360 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12361 unit_index, comp_dir,
12362 signature, is_debug_types);
12363 }
9a3c8263 12364 return (struct dwo_unit *) *slot;
80626a55
DE
12365 }
12366 if (signature_in_table == 0)
12367 return NULL;
12368 hash = (hash + hash2) & mask;
12369 }
12370
12371 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12372 " [in module %s]"),
12373 dwp_file->name);
12374}
12375
ab5088bf 12376/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12377 Open the file specified by FILE_NAME and hand it off to BFD for
12378 preliminary analysis. Return a newly initialized bfd *, which
12379 includes a canonicalized copy of FILE_NAME.
80626a55 12380 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12381 SEARCH_CWD is true if the current directory is to be searched.
12382 It will be searched before debug-file-directory.
13aaf454
DE
12383 If successful, the file is added to the bfd include table of the
12384 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12385 If unable to find/open the file, return NULL.
3019eac3
DE
12386 NOTE: This function is derived from symfile_bfd_open. */
12387
192b62ce 12388static gdb_bfd_ref_ptr
976ca316 12389try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12390 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12391{
24b9144d 12392 int desc;
9c02c129
DE
12393 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12394 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12395 to debug_file_directory. */
e0cc99a6 12396 const char *search_path;
9c02c129
DE
12397 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12398
e0cc99a6 12399 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12400 if (search_cwd)
12401 {
12402 if (*debug_file_directory != '\0')
e0cc99a6
TT
12403 {
12404 search_path_holder.reset (concat (".", dirname_separator_string,
12405 debug_file_directory,
12406 (char *) NULL));
12407 search_path = search_path_holder.get ();
12408 }
6ac97d4c 12409 else
e0cc99a6 12410 search_path = ".";
6ac97d4c 12411 }
9c02c129 12412 else
e0cc99a6 12413 search_path = debug_file_directory;
3019eac3 12414
56d467f4
CT
12415 /* Add the path for the executable binary to the list of search paths. */
12416 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12417 search_path_holder.reset (concat (objfile_dir.c_str (),
12418 dirname_separator_string,
12419 search_path, nullptr));
12420 search_path = search_path_holder.get ();
12421
24b9144d 12422 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12423 if (is_dwp)
12424 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12425
12426 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12427 desc = openp (search_path, flags, file_name,
3019eac3
DE
12428 O_RDONLY | O_BINARY, &absolute_name);
12429 if (desc < 0)
12430 return NULL;
12431
e0cc99a6
TT
12432 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12433 gnutarget, desc));
9c02c129
DE
12434 if (sym_bfd == NULL)
12435 return NULL;
192b62ce 12436 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12437
192b62ce
TT
12438 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12439 return NULL;
3019eac3 12440
13aaf454
DE
12441 /* Success. Record the bfd as having been included by the objfile's bfd.
12442 This is important because things like demangled_names_hash lives in the
12443 objfile's per_bfd space and may have references to things like symbol
12444 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12445 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12446
3019eac3
DE
12447 return sym_bfd;
12448}
12449
ab5088bf 12450/* Try to open DWO file FILE_NAME.
3019eac3
DE
12451 COMP_DIR is the DW_AT_comp_dir attribute.
12452 The result is the bfd handle of the file.
12453 If there is a problem finding or opening the file, return NULL.
12454 Upon success, the canonicalized path of the file is stored in the bfd,
12455 same as symfile_bfd_open. */
12456
192b62ce 12457static gdb_bfd_ref_ptr
976ca316 12458open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12459 const char *file_name, const char *comp_dir)
3019eac3 12460{
80626a55 12461 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12462 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12463 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12464
12465 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12466
12467 if (comp_dir != NULL)
12468 {
43816ebc
TT
12469 gdb::unique_xmalloc_ptr<char> path_to_try
12470 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12471
12472 /* NOTE: If comp_dir is a relative path, this will also try the
12473 search path, which seems useful. */
976ca316 12474 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12475 0 /*is_dwp*/,
192b62ce 12476 1 /*search_cwd*/));
3019eac3
DE
12477 if (abfd != NULL)
12478 return abfd;
12479 }
12480
12481 /* That didn't work, try debug-file-directory, which, despite its name,
12482 is a list of paths. */
12483
12484 if (*debug_file_directory == '\0')
12485 return NULL;
12486
976ca316 12487 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12488 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12489}
12490
80626a55
DE
12491/* This function is mapped across the sections and remembers the offset and
12492 size of each of the DWO debugging sections we are interested in. */
12493
12494static void
5bb6e9dd
TT
12495dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12496 dwo_sections *dwo_sections)
80626a55 12497{
80626a55
DE
12498 const struct dwop_section_names *names = &dwop_section_names;
12499
fbedd546 12500 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12501 {
049412e3 12502 dwo_sections->abbrev.s.section = sectp;
fd361982 12503 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12504 }
fbedd546 12505 else if (names->info_dwo.matches (sectp->name))
80626a55 12506 {
049412e3 12507 dwo_sections->info.s.section = sectp;
fd361982 12508 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12509 }
fbedd546 12510 else if (names->line_dwo.matches (sectp->name))
80626a55 12511 {
049412e3 12512 dwo_sections->line.s.section = sectp;
fd361982 12513 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12514 }
fbedd546 12515 else if (names->loc_dwo.matches (sectp->name))
80626a55 12516 {
049412e3 12517 dwo_sections->loc.s.section = sectp;
fd361982 12518 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12519 }
fbedd546 12520 else if (names->loclists_dwo.matches (sectp->name))
41144253 12521 {
12522 dwo_sections->loclists.s.section = sectp;
12523 dwo_sections->loclists.size = bfd_section_size (sectp);
12524 }
fbedd546 12525 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12526 {
049412e3 12527 dwo_sections->macinfo.s.section = sectp;
fd361982 12528 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12529 }
fbedd546 12530 else if (names->macro_dwo.matches (sectp->name))
80626a55 12531 {
049412e3 12532 dwo_sections->macro.s.section = sectp;
fd361982 12533 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12534 }
fbedd546 12535 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12536 {
12537 dwo_sections->rnglists.s.section = sectp;
12538 dwo_sections->rnglists.size = bfd_section_size (sectp);
12539 }
fbedd546 12540 else if (names->str_dwo.matches (sectp->name))
80626a55 12541 {
049412e3 12542 dwo_sections->str.s.section = sectp;
fd361982 12543 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12544 }
fbedd546 12545 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12546 {
049412e3 12547 dwo_sections->str_offsets.s.section = sectp;
fd361982 12548 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12549 }
fbedd546 12550 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12551 {
12552 struct dwarf2_section_info type_section;
12553
12554 memset (&type_section, 0, sizeof (type_section));
049412e3 12555 type_section.s.section = sectp;
fd361982 12556 type_section.size = bfd_section_size (sectp);
fd5866f6 12557 dwo_sections->types.push_back (type_section);
80626a55
DE
12558 }
12559}
12560
ab5088bf 12561/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12562 by PER_CU. This is for the non-DWP case.
80626a55 12563 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12564
12565static struct dwo_file *
4ab09049
SM
12566open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12567 const char *comp_dir)
3019eac3 12568{
976ca316 12569 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12570
976ca316 12571 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12572 if (dbfd == NULL)
12573 {
6f738b01
SM
12574 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12575
80626a55
DE
12576 return NULL;
12577 }
263db9a1 12578
51ac9db5 12579 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12580 dwo_file->dwo_name = dwo_name;
12581 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12582 dwo_file->dbfd = std::move (dbfd);
3019eac3 12583
5bb6e9dd
TT
12584 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12585 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12586 &dwo_file->sections);
3019eac3 12587
976ca316
SM
12588 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12589 dwo_file->cus);
3019eac3 12590
d2854d8d
CT
12591 if (cu->per_cu->dwarf_version < 5)
12592 {
12593 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12594 dwo_file->sections.types, dwo_file->tus);
12595 }
12596 else
12597 {
12598 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12599 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12600 rcuh_kind::COMPILE);
d2854d8d 12601 }
3019eac3 12602
6f738b01 12603 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12604
263db9a1 12605 return dwo_file.release ();
3019eac3
DE
12606}
12607
80626a55 12608/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12609 size of each of the DWP debugging sections common to version 1 and 2 that
12610 we are interested in. */
3019eac3 12611
80626a55 12612static void
73869dc2 12613dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12614 dwp_file *dwp_file)
3019eac3 12615{
80626a55
DE
12616 const struct dwop_section_names *names = &dwop_section_names;
12617 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12618
80626a55 12619 /* Record the ELF section number for later lookup: this is what the
73869dc2 12620 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12621 gdb_assert (elf_section_nr < dwp_file->num_sections);
12622 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12623
80626a55 12624 /* Look for specific sections that we need. */
fbedd546 12625 if (names->str_dwo.matches (sectp->name))
80626a55 12626 {
049412e3 12627 dwp_file->sections.str.s.section = sectp;
fd361982 12628 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12629 }
fbedd546 12630 else if (names->cu_index.matches (sectp->name))
80626a55 12631 {
049412e3 12632 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12633 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12634 }
fbedd546 12635 else if (names->tu_index.matches (sectp->name))
80626a55 12636 {
049412e3 12637 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12638 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12639 }
12640}
3019eac3 12641
73869dc2
DE
12642/* This function is mapped across the sections and remembers the offset and
12643 size of each of the DWP version 2 debugging sections that we are interested
12644 in. This is split into a separate function because we don't know if we
d2854d8d 12645 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12646
12647static void
12648dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12649{
9a3c8263 12650 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12651 const struct dwop_section_names *names = &dwop_section_names;
12652 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12653
12654 /* Record the ELF section number for later lookup: this is what the
12655 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12656 gdb_assert (elf_section_nr < dwp_file->num_sections);
12657 dwp_file->elf_sections[elf_section_nr] = sectp;
12658
12659 /* Look for specific sections that we need. */
fbedd546 12660 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12661 {
049412e3 12662 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12663 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12664 }
fbedd546 12665 else if (names->info_dwo.matches (sectp->name))
73869dc2 12666 {
049412e3 12667 dwp_file->sections.info.s.section = sectp;
fd361982 12668 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12669 }
fbedd546 12670 else if (names->line_dwo.matches (sectp->name))
73869dc2 12671 {
049412e3 12672 dwp_file->sections.line.s.section = sectp;
fd361982 12673 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 12674 }
fbedd546 12675 else if (names->loc_dwo.matches (sectp->name))
73869dc2 12676 {
049412e3 12677 dwp_file->sections.loc.s.section = sectp;
fd361982 12678 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 12679 }
fbedd546 12680 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 12681 {
049412e3 12682 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12683 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 12684 }
fbedd546 12685 else if (names->macro_dwo.matches (sectp->name))
73869dc2 12686 {
049412e3 12687 dwp_file->sections.macro.s.section = sectp;
fd361982 12688 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 12689 }
fbedd546 12690 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 12691 {
049412e3 12692 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12693 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 12694 }
fbedd546 12695 else if (names->types_dwo.matches (sectp->name))
73869dc2 12696 {
049412e3 12697 dwp_file->sections.types.s.section = sectp;
fd361982 12698 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12699 }
12700}
12701
d2854d8d
CT
12702/* This function is mapped across the sections and remembers the offset and
12703 size of each of the DWP version 5 debugging sections that we are interested
12704 in. This is split into a separate function because we don't know if we
12705 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12706
12707static void
12708dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12709{
12710 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12711 const struct dwop_section_names *names = &dwop_section_names;
12712 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12713
12714 /* Record the ELF section number for later lookup: this is what the
12715 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12716 gdb_assert (elf_section_nr < dwp_file->num_sections);
12717 dwp_file->elf_sections[elf_section_nr] = sectp;
12718
12719 /* Look for specific sections that we need. */
fbedd546 12720 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
12721 {
12722 dwp_file->sections.abbrev.s.section = sectp;
12723 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12724 }
fbedd546 12725 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
12726 {
12727 dwp_file->sections.info.s.section = sectp;
12728 dwp_file->sections.info.size = bfd_section_size (sectp);
12729 }
fbedd546 12730 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
12731 {
12732 dwp_file->sections.line.s.section = sectp;
12733 dwp_file->sections.line.size = bfd_section_size (sectp);
12734 }
fbedd546 12735 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
12736 {
12737 dwp_file->sections.loclists.s.section = sectp;
12738 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12739 }
fbedd546 12740 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
12741 {
12742 dwp_file->sections.macro.s.section = sectp;
12743 dwp_file->sections.macro.size = bfd_section_size (sectp);
12744 }
fbedd546 12745 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
12746 {
12747 dwp_file->sections.rnglists.s.section = sectp;
12748 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12749 }
fbedd546 12750 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
12751 {
12752 dwp_file->sections.str_offsets.s.section = sectp;
12753 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12754 }
12755}
12756
80626a55 12757/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12758
80626a55
DE
12759static hashval_t
12760hash_dwp_loaded_cutus (const void *item)
12761{
9a3c8263 12762 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12763
80626a55
DE
12764 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12765 return dwo_unit->signature;
3019eac3
DE
12766}
12767
80626a55 12768/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12769
80626a55
DE
12770static int
12771eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12772{
9a3c8263
SM
12773 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12774 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12775
80626a55
DE
12776 return dua->signature == dub->signature;
12777}
3019eac3 12778
80626a55 12779/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12780
48b490f2 12781static htab_up
298e9637 12782allocate_dwp_loaded_cutus_table ()
80626a55 12783{
48b490f2
TT
12784 return htab_up (htab_create_alloc (3,
12785 hash_dwp_loaded_cutus,
12786 eq_dwp_loaded_cutus,
12787 NULL, xcalloc, xfree));
80626a55 12788}
3019eac3 12789
ab5088bf
DE
12790/* Try to open DWP file FILE_NAME.
12791 The result is the bfd handle of the file.
12792 If there is a problem finding or opening the file, return NULL.
12793 Upon success, the canonicalized path of the file is stored in the bfd,
12794 same as symfile_bfd_open. */
12795
192b62ce 12796static gdb_bfd_ref_ptr
976ca316 12797open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12798{
976ca316 12799 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12800 1 /*is_dwp*/,
192b62ce 12801 1 /*search_cwd*/));
6ac97d4c
DE
12802 if (abfd != NULL)
12803 return abfd;
12804
12805 /* Work around upstream bug 15652.
12806 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12807 [Whether that's a "bug" is debatable, but it is getting in our way.]
12808 We have no real idea where the dwp file is, because gdb's realpath-ing
12809 of the executable's path may have discarded the needed info.
12810 [IWBN if the dwp file name was recorded in the executable, akin to
12811 .gnu_debuglink, but that doesn't exist yet.]
12812 Strip the directory from FILE_NAME and search again. */
12813 if (*debug_file_directory != '\0')
12814 {
12815 /* Don't implicitly search the current directory here.
12816 If the user wants to search "." to handle this case,
12817 it must be added to debug-file-directory. */
976ca316
SM
12818 return try_open_dwop_file (per_objfile, lbasename (file_name),
12819 1 /*is_dwp*/,
6ac97d4c
DE
12820 0 /*search_cwd*/);
12821 }
12822
12823 return NULL;
ab5088bf
DE
12824}
12825
80626a55
DE
12826/* Initialize the use of the DWP file for the current objfile.
12827 By convention the name of the DWP file is ${objfile}.dwp.
12828 The result is NULL if it can't be found. */
a766d390 12829
400174b1 12830static std::unique_ptr<struct dwp_file>
976ca316 12831open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12832{
976ca316 12833 struct objfile *objfile = per_objfile->objfile;
80626a55 12834
82bf32bc
JK
12835 /* Try to find first .dwp for the binary file before any symbolic links
12836 resolving. */
6c447423
DE
12837
12838 /* If the objfile is a debug file, find the name of the real binary
12839 file and get the name of dwp file from there. */
d721ba37 12840 std::string dwp_name;
6c447423
DE
12841 if (objfile->separate_debug_objfile_backlink != NULL)
12842 {
12843 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12844 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12845
d721ba37 12846 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12847 }
12848 else
d721ba37
PA
12849 dwp_name = objfile->original_name;
12850
12851 dwp_name += ".dwp";
80626a55 12852
976ca316 12853 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12854 if (dbfd == NULL
12855 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12856 {
12857 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12858 dwp_name = objfile_name (objfile);
12859 dwp_name += ".dwp";
976ca316 12860 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
12861 }
12862
80626a55
DE
12863 if (dbfd == NULL)
12864 {
6f738b01
SM
12865 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12866
400174b1 12867 return std::unique_ptr<dwp_file> ();
3019eac3 12868 }
400174b1
TT
12869
12870 const char *name = bfd_get_filename (dbfd.get ());
12871 std::unique_ptr<struct dwp_file> dwp_file
12872 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12873
0a0f4c01 12874 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12875 dwp_file->elf_sections =
976ca316 12876 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
12877 dwp_file->num_sections, asection *);
12878
5bb6e9dd
TT
12879 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12880 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12881 dwp_file.get ());
80626a55 12882
976ca316 12883 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 12884
976ca316 12885 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 12886
73869dc2 12887 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12888 if (dwp_file->cus && dwp_file->tus
12889 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12890 {
12891 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12892 pretty bizarre. We use pulongest here because that's the established
4d65956b 12893 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12894 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12895 " TU version %s [in DWP file %s]"),
12896 pulongest (dwp_file->cus->version),
d721ba37 12897 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12898 }
08302ed2
DE
12899
12900 if (dwp_file->cus)
12901 dwp_file->version = dwp_file->cus->version;
12902 else if (dwp_file->tus)
12903 dwp_file->version = dwp_file->tus->version;
12904 else
12905 dwp_file->version = 2;
73869dc2 12906
5bb6e9dd
TT
12907 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12908 {
12909 if (dwp_file->version == 2)
12910 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12911 dwp_file.get ());
12912 else
12913 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12914 dwp_file.get ());
12915 }
73869dc2 12916
298e9637
SM
12917 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12918 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12919
6f738b01
SM
12920 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12921 dwarf_read_debug_printf (" %s CUs, %s TUs",
12922 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12923 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12924
12925 return dwp_file;
3019eac3 12926}
c906108c 12927
ab5088bf
DE
12928/* Wrapper around open_and_init_dwp_file, only open it once. */
12929
12930static struct dwp_file *
976ca316 12931get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 12932{
976ca316 12933 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 12934 {
976ca316
SM
12935 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12936 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12937 }
976ca316 12938 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12939}
12940
80626a55
DE
12941/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12942 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12943 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12944 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12945 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12946
12947 This is called, for example, when wanting to read a variable with a
12948 complex location. Therefore we don't want to do file i/o for every call.
12949 Therefore we don't want to look for a DWO file on every call.
12950 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12951 then we check if we've already seen DWO_NAME, and only THEN do we check
12952 for a DWO file.
12953
1c658ad5 12954 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12955 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12956
3019eac3 12957static struct dwo_unit *
4ab09049 12958lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12959 ULONGEST signature, int is_debug_types)
3019eac3 12960{
976ca316
SM
12961 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12962 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
12963 const char *kind = is_debug_types ? "TU" : "CU";
12964 void **dwo_file_slot;
3019eac3 12965 struct dwo_file *dwo_file;
80626a55 12966 struct dwp_file *dwp_file;
cb1df416 12967
6a506a2d
DE
12968 /* First see if there's a DWP file.
12969 If we have a DWP file but didn't find the DWO inside it, don't
12970 look for the original DWO file. It makes gdb behave differently
12971 depending on whether one is debugging in the build tree. */
cf2c3c16 12972
976ca316 12973 dwp_file = get_dwp_file (per_objfile);
80626a55 12974 if (dwp_file != NULL)
cf2c3c16 12975 {
80626a55
DE
12976 const struct dwp_hash_table *dwp_htab =
12977 is_debug_types ? dwp_file->tus : dwp_file->cus;
12978
12979 if (dwp_htab != NULL)
12980 {
12981 struct dwo_unit *dwo_cutu =
976ca316
SM
12982 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12983 is_debug_types);
80626a55
DE
12984
12985 if (dwo_cutu != NULL)
12986 {
6f738b01
SM
12987 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12988 kind, hex_string (signature),
12989 host_address_to_string (dwo_cutu));
12990
80626a55
DE
12991 return dwo_cutu;
12992 }
12993 }
12994 }
6a506a2d 12995 else
80626a55 12996 {
6a506a2d 12997 /* No DWP file, look for the DWO file. */
80626a55 12998
976ca316 12999 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 13000 if (*dwo_file_slot == NULL)
80626a55 13001 {
6a506a2d 13002 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 13003 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 13004 }
6a506a2d 13005 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13006 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13007
6a506a2d 13008 if (dwo_file != NULL)
19c3d4c9 13009 {
6a506a2d
DE
13010 struct dwo_unit *dwo_cutu = NULL;
13011
13012 if (is_debug_types && dwo_file->tus)
13013 {
13014 struct dwo_unit find_dwo_cutu;
13015
13016 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13017 find_dwo_cutu.signature = signature;
9a3c8263 13018 dwo_cutu
b0b6a987
TT
13019 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13020 &find_dwo_cutu);
6a506a2d 13021 }
33c5cd75 13022 else if (!is_debug_types && dwo_file->cus)
80626a55 13023 {
33c5cd75
DB
13024 struct dwo_unit find_dwo_cutu;
13025
13026 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13027 find_dwo_cutu.signature = signature;
b0b6a987 13028 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 13029 &find_dwo_cutu);
6a506a2d
DE
13030 }
13031
13032 if (dwo_cutu != NULL)
13033 {
6f738b01
SM
13034 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13035 kind, dwo_name, hex_string (signature),
13036 host_address_to_string (dwo_cutu));
13037
6a506a2d 13038 return dwo_cutu;
80626a55
DE
13039 }
13040 }
2e276125 13041 }
9cdd5dbd 13042
80626a55
DE
13043 /* We didn't find it. This could mean a dwo_id mismatch, or
13044 someone deleted the DWO/DWP file, or the search path isn't set up
13045 correctly to find the file. */
13046
6f738b01
SM
13047 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13048 kind, dwo_name, hex_string (signature));
3019eac3 13049
6656a72d
DE
13050 /* This is a warning and not a complaint because it can be caused by
13051 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13052 {
13053 /* Print the name of the DWP file if we looked there, helps the user
13054 better diagnose the problem. */
791afaa2 13055 std::string dwp_text;
43942612
DE
13056
13057 if (dwp_file != NULL)
791afaa2
TT
13058 dwp_text = string_printf (" [in DWP file %s]",
13059 lbasename (dwp_file->name));
43942612 13060
9d8780f0 13061 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13062 " [in module %s]"),
4ab09049
SM
13063 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13064 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13065 }
3019eac3 13066 return NULL;
5fb290d7
DJ
13067}
13068
80626a55
DE
13069/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13070 See lookup_dwo_cutu_unit for details. */
13071
13072static struct dwo_unit *
4ab09049 13073lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13074 ULONGEST signature)
13075{
4ab09049
SM
13076 gdb_assert (!cu->per_cu->is_debug_types);
13077
13078 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13079}
13080
13081/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13082 See lookup_dwo_cutu_unit for details. */
13083
13084static struct dwo_unit *
4ab09049 13085lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13086{
4ab09049
SM
13087 gdb_assert (cu->per_cu->is_debug_types);
13088
13089 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13090
13091 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13092}
13093
89e63ee4
DE
13094/* Traversal function for queue_and_load_all_dwo_tus. */
13095
13096static int
13097queue_and_load_dwo_tu (void **slot, void *info)
13098{
13099 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13100 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13101 ULONGEST signature = dwo_unit->signature;
d460f660 13102 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13103
13104 if (sig_type != NULL)
13105 {
13106 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13107
13108 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13109 a real dependency of PER_CU on SIG_TYPE. That is detected later
13110 while processing PER_CU. */
120ce1b5 13111 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13112 load_full_type_unit (sig_cu, cu->per_objfile);
13113 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13114 }
13115
13116 return 1;
13117}
13118
1b555f17 13119/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13120 The DWO may have the only definition of the type, though it may not be
13121 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13122 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13123
13124static void
1b555f17 13125queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13126{
13127 struct dwo_unit *dwo_unit;
13128 struct dwo_file *dwo_file;
13129
1b555f17
SM
13130 gdb_assert (cu != nullptr);
13131 gdb_assert (!cu->per_cu->is_debug_types);
13132 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13133
1b555f17 13134 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13135 gdb_assert (dwo_unit != NULL);
13136
13137 dwo_file = dwo_unit->dwo_file;
13138 if (dwo_file->tus != NULL)
1b555f17 13139 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13140}
13141
3019eac3 13142/* Read in various DIEs. */
348e048f 13143
d389af10 13144/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13145 Inherit only the children of the DW_AT_abstract_origin DIE not being
13146 already referenced by DW_AT_abstract_origin from the children of the
13147 current DIE. */
d389af10
JK
13148
13149static void
13150inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13151{
13152 struct die_info *child_die;
791afaa2 13153 sect_offset *offsetp;
d389af10
JK
13154 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13155 struct die_info *origin_die;
13156 /* Iterator of the ORIGIN_DIE children. */
13157 struct die_info *origin_child_die;
d389af10 13158 struct attribute *attr;
cd02d79d
PA
13159 struct dwarf2_cu *origin_cu;
13160 struct pending **origin_previous_list_in_scope;
d389af10
JK
13161
13162 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13163 if (!attr)
13164 return;
13165
cd02d79d
PA
13166 /* Note that following die references may follow to a die in a
13167 different cu. */
13168
13169 origin_cu = cu;
13170 origin_die = follow_die_ref (die, attr, &origin_cu);
13171
13172 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13173 symbols in. */
13174 origin_previous_list_in_scope = origin_cu->list_in_scope;
13175 origin_cu->list_in_scope = cu->list_in_scope;
13176
edb3359d
DJ
13177 if (die->tag != origin_die->tag
13178 && !(die->tag == DW_TAG_inlined_subroutine
13179 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13180 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13181 sect_offset_str (die->sect_off),
13182 sect_offset_str (origin_die->sect_off));
d389af10 13183
34dc0f95
TBA
13184 /* Find if the concrete and abstract trees are structurally the
13185 same. This is a shallow traversal and it is not bullet-proof;
13186 the compiler can trick the debugger into believing that the trees
13187 are isomorphic, whereas they actually are not. However, the
13188 likelyhood of this happening is pretty low, and a full-fledged
13189 check would be an overkill. */
13190 bool are_isomorphic = true;
13191 die_info *concrete_child = die->child;
13192 die_info *abstract_child = origin_die->child;
13193 while (concrete_child != nullptr || abstract_child != nullptr)
13194 {
13195 if (concrete_child == nullptr
13196 || abstract_child == nullptr
13197 || concrete_child->tag != abstract_child->tag)
13198 {
13199 are_isomorphic = false;
13200 break;
13201 }
13202
13203 concrete_child = concrete_child->sibling;
13204 abstract_child = abstract_child->sibling;
13205 }
13206
13207 /* Walk the origin's children in parallel to the concrete children.
13208 This helps match an origin child in case the debug info misses
13209 DW_AT_abstract_origin attributes. Keep in mind that the abstract
13210 origin tree may not have the same tree structure as the concrete
13211 DIE, though. */
13212 die_info *corresponding_abstract_child
13213 = are_isomorphic ? origin_die->child : nullptr;
13214
791afaa2 13215 std::vector<sect_offset> offsets;
d389af10 13216
3ea89b92
PMR
13217 for (child_die = die->child;
13218 child_die && child_die->tag;
436c571c 13219 child_die = child_die->sibling)
3ea89b92
PMR
13220 {
13221 struct die_info *child_origin_die;
13222 struct dwarf2_cu *child_origin_cu;
13223
13224 /* We are trying to process concrete instance entries:
216f72a1 13225 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13226 it's not relevant to our analysis here. i.e. detecting DIEs that are
13227 present in the abstract instance but not referenced in the concrete
13228 one. */
216f72a1 13229 if (child_die->tag == DW_TAG_call_site
dda83cd7 13230 || child_die->tag == DW_TAG_GNU_call_site)
34dc0f95
TBA
13231 {
13232 if (are_isomorphic)
13233 corresponding_abstract_child
13234 = corresponding_abstract_child->sibling;
13235 continue;
13236 }
3ea89b92 13237
c38f313d
DJ
13238 /* For each CHILD_DIE, find the corresponding child of
13239 ORIGIN_DIE. If there is more than one layer of
13240 DW_AT_abstract_origin, follow them all; there shouldn't be,
13241 but GCC versions at least through 4.4 generate this (GCC PR
13242 40573). */
3ea89b92
PMR
13243 child_origin_die = child_die;
13244 child_origin_cu = cu;
c38f313d
DJ
13245 while (1)
13246 {
cd02d79d
PA
13247 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13248 child_origin_cu);
c38f313d
DJ
13249 if (attr == NULL)
13250 break;
cd02d79d
PA
13251 child_origin_die = follow_die_ref (child_origin_die, attr,
13252 &child_origin_cu);
c38f313d
DJ
13253 }
13254
34dc0f95
TBA
13255 /* If missing DW_AT_abstract_origin, try the corresponding child
13256 of the origin. Clang emits such lexical scopes. */
13257 if (child_origin_die == child_die
13258 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
13259 && are_isomorphic
13260 && child_die->tag == DW_TAG_lexical_block)
13261 child_origin_die = corresponding_abstract_child;
13262
d389af10
JK
13263 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13264 counterpart may exist. */
c38f313d 13265 if (child_origin_die != child_die)
d389af10 13266 {
edb3359d
DJ
13267 if (child_die->tag != child_origin_die->tag
13268 && !(child_die->tag == DW_TAG_inlined_subroutine
13269 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13270 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13271 "different tags"),
9d8780f0
SM
13272 sect_offset_str (child_die->sect_off),
13273 sect_offset_str (child_origin_die->sect_off));
c38f313d 13274 if (child_origin_die->parent != origin_die)
b98664d3 13275 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13276 "different parents"),
9d8780f0
SM
13277 sect_offset_str (child_die->sect_off),
13278 sect_offset_str (child_origin_die->sect_off));
c38f313d 13279 else
791afaa2 13280 offsets.push_back (child_origin_die->sect_off);
d389af10 13281 }
34dc0f95
TBA
13282
13283 if (are_isomorphic)
13284 corresponding_abstract_child = corresponding_abstract_child->sibling;
d389af10 13285 }
791afaa2
TT
13286 std::sort (offsets.begin (), offsets.end ());
13287 sect_offset *offsets_end = offsets.data () + offsets.size ();
13288 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13289 if (offsetp[-1] == *offsetp)
b98664d3 13290 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13291 "to DIE %s as their abstract origin"),
13292 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13293
791afaa2 13294 offsetp = offsets.data ();
d389af10
JK
13295 origin_child_die = origin_die->child;
13296 while (origin_child_die && origin_child_die->tag)
13297 {
13298 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13299 while (offsetp < offsets_end
9c541725 13300 && *offsetp < origin_child_die->sect_off)
d389af10 13301 offsetp++;
b64f50a1 13302 if (offsetp >= offsets_end
9c541725 13303 || *offsetp > origin_child_die->sect_off)
d389af10 13304 {
adde2bff
DE
13305 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13306 Check whether we're already processing ORIGIN_CHILD_DIE.
13307 This can happen with mutually referenced abstract_origins.
13308 PR 16581. */
13309 if (!origin_child_die->in_process)
13310 process_die (origin_child_die, origin_cu);
d389af10 13311 }
436c571c 13312 origin_child_die = origin_child_die->sibling;
d389af10 13313 }
cd02d79d 13314 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13315
13316 if (cu != origin_cu)
13317 compute_delayed_physnames (origin_cu);
d389af10
JK
13318}
13319
c906108c 13320static void
e7c27a73 13321read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13322{
5e22e966 13323 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13324 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13325 struct context_stack *newobj;
c906108c
SS
13326 CORE_ADDR lowpc;
13327 CORE_ADDR highpc;
13328 struct die_info *child_die;
edb3359d 13329 struct attribute *attr, *call_line, *call_file;
15d034d0 13330 const char *name;
e142c38c 13331 CORE_ADDR baseaddr;
801e3a5b 13332 struct block *block;
edb3359d 13333 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13334 std::vector<struct symbol *> template_args;
34eaf542 13335 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13336
13337 if (inlined_func)
13338 {
13339 /* If we do not have call site information, we can't show the
13340 caller of this inlined function. That's too confusing, so
13341 only use the scope for local variables. */
13342 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13343 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13344 if (call_line == NULL || call_file == NULL)
13345 {
13346 read_lexical_block_scope (die, cu);
13347 return;
13348 }
13349 }
c906108c 13350
b3b3bada 13351 baseaddr = objfile->text_section_offset ();
e142c38c 13352
94af9270 13353 name = dwarf2_name (die, cu);
c906108c 13354
e8d05480
JB
13355 /* Ignore functions with missing or empty names. These are actually
13356 illegal according to the DWARF standard. */
13357 if (name == NULL)
13358 {
b98664d3 13359 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13360 sect_offset_str (die->sect_off));
e8d05480
JB
13361 return;
13362 }
13363
13364 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13365 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13366 <= PC_BOUNDS_INVALID)
e8d05480 13367 {
ae4d0c03 13368 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13369 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13370 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13371 "for subprogram DIE at %s"),
13372 sect_offset_str (die->sect_off));
e8d05480
JB
13373 return;
13374 }
c906108c 13375
3e29f34a
MR
13376 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13377 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13378
34eaf542
TT
13379 /* If we have any template arguments, then we must allocate a
13380 different sort of symbol. */
436c571c 13381 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13382 {
13383 if (child_die->tag == DW_TAG_template_type_param
13384 || child_die->tag == DW_TAG_template_value_param)
13385 {
8c14c3a3 13386 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13387 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13388 break;
13389 }
13390 }
13391
702cf3f5 13392 gdb_assert (cu->get_builder () != nullptr);
c24bdb02 13393 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13394 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13395 (struct symbol *) templ_func);
4c2df51b 13396
81873cc8 13397 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13398 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13399 cu->language);
13400
4cecd739
DJ
13401 /* If there is a location expression for DW_AT_frame_base, record
13402 it. */
e142c38c 13403 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13404 if (attr != nullptr)
fe978cb0 13405 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13406
63e43d3a
PMR
13407 /* If there is a location for the static link, record it. */
13408 newobj->static_link = NULL;
13409 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13410 if (attr != nullptr)
63e43d3a 13411 {
224c3ddb
SM
13412 newobj->static_link
13413 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13414 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13415 cu->addr_type ());
63e43d3a
PMR
13416 }
13417
c24bdb02 13418 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13419
639d11d3 13420 if (die->child != NULL)
c906108c 13421 {
639d11d3 13422 child_die = die->child;
c906108c
SS
13423 while (child_die && child_die->tag)
13424 {
34eaf542
TT
13425 if (child_die->tag == DW_TAG_template_type_param
13426 || child_die->tag == DW_TAG_template_value_param)
13427 {
13428 struct symbol *arg = new_symbol (child_die, NULL, cu);
13429
f1078f66 13430 if (arg != NULL)
2f4732b0 13431 template_args.push_back (arg);
34eaf542
TT
13432 }
13433 else
13434 process_die (child_die, cu);
436c571c 13435 child_die = child_die->sibling;
c906108c
SS
13436 }
13437 }
13438
d389af10
JK
13439 inherit_abstract_dies (die, cu);
13440
4a811a97
UW
13441 /* If we have a DW_AT_specification, we might need to import using
13442 directives from the context of the specification DIE. See the
13443 comment in determine_prefix. */
13444 if (cu->language == language_cplus
13445 && dwarf2_attr (die, DW_AT_specification, cu))
13446 {
13447 struct dwarf2_cu *spec_cu = cu;
13448 struct die_info *spec_die = die_specification (die, &spec_cu);
13449
13450 while (spec_die)
13451 {
13452 child_die = spec_die->child;
13453 while (child_die && child_die->tag)
13454 {
13455 if (child_die->tag == DW_TAG_imported_module)
13456 process_die (child_die, spec_cu);
436c571c 13457 child_die = child_die->sibling;
4a811a97
UW
13458 }
13459
13460 /* In some cases, GCC generates specification DIEs that
13461 themselves contain DW_AT_specification attributes. */
13462 spec_die = die_specification (spec_die, &spec_cu);
13463 }
13464 }
13465
c24bdb02 13466 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13467 /* Make a block for the local symbols within. */
c24bdb02 13468 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13469 cstk.static_link, lowpc, highpc);
801e3a5b 13470
df8a16a1 13471 /* For C++, set the block's scope. */
45280282
IB
13472 if ((cu->language == language_cplus
13473 || cu->language == language_fortran
c44af4eb
TT
13474 || cu->language == language_d
13475 || cu->language == language_rust)
4d4ec4e5 13476 && cu->processing_has_namespace_info)
195a3f6c
TT
13477 block_set_scope (block, determine_prefix (die, cu),
13478 &objfile->objfile_obstack);
df8a16a1 13479
801e3a5b
JB
13480 /* If we have address ranges, record them. */
13481 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13482
a60f3166 13483 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13484
34eaf542 13485 /* Attach template arguments to function. */
2f4732b0 13486 if (!template_args.empty ())
34eaf542
TT
13487 {
13488 gdb_assert (templ_func != NULL);
13489
2f4732b0 13490 templ_func->n_template_arguments = template_args.size ();
34eaf542 13491 templ_func->template_arguments
dda83cd7 13492 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13493 templ_func->n_template_arguments);
34eaf542 13494 memcpy (templ_func->template_arguments,
2f4732b0 13495 template_args.data (),
34eaf542 13496 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13497
13498 /* Make sure that the symtab is set on the new symbols. Even
13499 though they don't appear in this symtab directly, other parts
13500 of gdb assume that symbols do, and this is reasonably
13501 true. */
8634679f 13502 for (symbol *sym : template_args)
3e1d3d8c 13503 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13504 }
13505
208d8187
JB
13506 /* In C++, we can have functions nested inside functions (e.g., when
13507 a function declares a class that has methods). This means that
13508 when we finish processing a function scope, we may need to go
13509 back to building a containing block's symbol lists. */
c24bdb02
KS
13510 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13511 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13512
921e78cf
JB
13513 /* If we've finished processing a top-level function, subsequent
13514 symbols go in the file symbol list. */
c24bdb02
KS
13515 if (cu->get_builder ()->outermost_context_p ())
13516 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13517}
13518
13519/* Process all the DIES contained within a lexical block scope. Start
13520 a new scope, process the dies, and then close the scope. */
13521
13522static void
e7c27a73 13523read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13524{
5e22e966 13525 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13526 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13527 CORE_ADDR lowpc, highpc;
13528 struct die_info *child_die;
e142c38c
DJ
13529 CORE_ADDR baseaddr;
13530
b3b3bada 13531 baseaddr = objfile->text_section_offset ();
c906108c
SS
13532
13533 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13534 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13535 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13536 be nasty. Might be easier to properly extend generic blocks to
af34e669 13537 describe ranges. */
e385593e
JK
13538 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13539 {
13540 case PC_BOUNDS_NOT_PRESENT:
13541 /* DW_TAG_lexical_block has no attributes, process its children as if
13542 there was no wrapping by that DW_TAG_lexical_block.
13543 GCC does no longer produces such DWARF since GCC r224161. */
13544 for (child_die = die->child;
13545 child_die != NULL && child_die->tag;
436c571c 13546 child_die = child_die->sibling)
4f7bc5ed
TT
13547 {
13548 /* We might already be processing this DIE. This can happen
13549 in an unusual circumstance -- where a subroutine A
13550 appears lexically in another subroutine B, but A actually
13551 inlines B. The recursion is broken here, rather than in
13552 inherit_abstract_dies, because it seems better to simply
13553 drop concrete children here. */
13554 if (!child_die->in_process)
13555 process_die (child_die, cu);
13556 }
e385593e
JK
13557 return;
13558 case PC_BOUNDS_INVALID:
13559 return;
13560 }
3e29f34a
MR
13561 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13562 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13563
c24bdb02 13564 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13565 if (die->child != NULL)
c906108c 13566 {
639d11d3 13567 child_die = die->child;
c906108c
SS
13568 while (child_die && child_die->tag)
13569 {
e7c27a73 13570 process_die (child_die, cu);
436c571c 13571 child_die = child_die->sibling;
c906108c
SS
13572 }
13573 }
3ea89b92 13574 inherit_abstract_dies (die, cu);
c24bdb02 13575 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13576
c24bdb02
KS
13577 if (*cu->get_builder ()->get_local_symbols () != NULL
13578 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13579 {
801e3a5b 13580 struct block *block
dda83cd7 13581 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13582 cstk.start_addr, highpc);
801e3a5b
JB
13583
13584 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13585 do here, means that recording a parent's ranges entails
13586 walking across all its children's ranges as they appear in
13587 the address map, which is quadratic behavior.
13588
13589 It would be nicer to record the parent's ranges before
13590 traversing its children, simply overriding whatever you find
13591 there. But since we don't even decide whether to create a
13592 block until after we've traversed its children, that's hard
13593 to do. */
801e3a5b 13594 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13595 }
c24bdb02
KS
13596 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13597 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13598}
13599
216f72a1 13600/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13601
13602static void
13603read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13604{
5e22e966 13605 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13606 struct objfile *objfile = per_objfile->objfile;
08feed99 13607 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13608 CORE_ADDR pc, baseaddr;
13609 struct attribute *attr;
13610 struct call_site *call_site, call_site_local;
13611 void **slot;
13612 int nparams;
13613 struct die_info *child_die;
13614
b3b3bada 13615 baseaddr = objfile->text_section_offset ();
96408a79 13616
216f72a1
JK
13617 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13618 if (attr == NULL)
13619 {
13620 /* This was a pre-DWARF-5 GNU extension alias
13621 for DW_AT_call_return_pc. */
13622 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13623 }
96408a79
SA
13624 if (!attr)
13625 {
b98664d3 13626 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13627 "DIE %s [in module %s]"),
13628 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13629 return;
13630 }
95f982e5 13631 pc = attr->as_address () + baseaddr;
3e29f34a 13632 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13633
13634 if (cu->call_site_htab == NULL)
13635 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13636 NULL, &objfile->objfile_obstack,
13637 hashtab_obstack_allocate, NULL);
13638 call_site_local.pc = pc;
13639 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13640 if (*slot != NULL)
13641 {
b98664d3 13642 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13643 "DIE %s [in module %s]"),
13644 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13645 objfile_name (objfile));
96408a79
SA
13646 return;
13647 }
13648
13649 /* Count parameters at the caller. */
13650
13651 nparams = 0;
13652 for (child_die = die->child; child_die && child_die->tag;
436c571c 13653 child_die = child_die->sibling)
96408a79 13654 {
216f72a1 13655 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13656 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13657 {
b98664d3 13658 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13659 "DW_TAG_call_site child DIE %s [in module %s]"),
13660 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13661 objfile_name (objfile));
96408a79
SA
13662 continue;
13663 }
13664
13665 nparams++;
13666 }
13667
224c3ddb
SM
13668 call_site
13669 = ((struct call_site *)
13670 obstack_alloc (&objfile->objfile_obstack,
13671 sizeof (*call_site)
13672 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13673 *slot = call_site;
13674 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13675 call_site->pc = pc;
13676
216f72a1
JK
13677 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13678 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13679 {
13680 struct die_info *func_die;
13681
13682 /* Skip also over DW_TAG_inlined_subroutine. */
13683 for (func_die = die->parent;
13684 func_die && func_die->tag != DW_TAG_subprogram
13685 && func_die->tag != DW_TAG_subroutine_type;
13686 func_die = func_die->parent);
13687
216f72a1
JK
13688 /* DW_AT_call_all_calls is a superset
13689 of DW_AT_call_all_tail_calls. */
96408a79 13690 if (func_die
dda83cd7
SM
13691 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13692 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13693 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13694 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13695 {
13696 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13697 not complete. But keep CALL_SITE for look ups via call_site_htab,
13698 both the initial caller containing the real return address PC and
13699 the final callee containing the current PC of a chain of tail
13700 calls do not need to have the tail call list complete. But any
13701 function candidate for a virtual tail call frame searched via
13702 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13703 determined unambiguously. */
13704 }
13705 else
13706 {
13707 struct type *func_type = NULL;
13708
13709 if (func_die)
13710 func_type = get_die_type (func_die, cu);
13711 if (func_type != NULL)
13712 {
78134374 13713 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13714
13715 /* Enlist this call site to the function. */
13716 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13717 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13718 }
13719 else
b98664d3 13720 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13721 "DIE %s [in module %s]"),
13722 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13723 }
13724 }
13725
216f72a1
JK
13726 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13727 if (attr == NULL)
13728 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13729 if (attr == NULL)
13730 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13731 if (attr == NULL)
216f72a1
JK
13732 {
13733 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13734 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13735 }
96408a79 13736 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13737 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13738 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13739 else if (attr->form_is_block ())
96408a79
SA
13740 {
13741 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13742 struct dwarf_block *block = attr->as_block ();
96408a79 13743
8d749320 13744 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13745 dlbaton->data = block->data;
13746 dlbaton->size = block->size;
a50264ba 13747 dlbaton->per_objfile = per_objfile;
96408a79
SA
13748 dlbaton->per_cu = cu->per_cu;
13749
13750 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13751 }
cd6c91b4 13752 else if (attr->form_is_ref ())
96408a79 13753 {
96408a79
SA
13754 struct dwarf2_cu *target_cu = cu;
13755 struct die_info *target_die;
13756
ac9ec31b 13757 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13758 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13759 if (die_is_declaration (target_die, target_cu))
13760 {
7d45c7c3 13761 const char *target_physname;
9112db09
JK
13762
13763 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13764 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13765 if (target_physname == NULL)
9112db09 13766 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13767 if (target_physname == NULL)
b98664d3 13768 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13769 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 13770 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13771 else
7d455152 13772 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13773 }
13774 else
13775 {
13776 CORE_ADDR lowpc;
13777
13778 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13779 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13780 <= PC_BOUNDS_INVALID)
b98664d3 13781 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13782 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 13783 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13784 else
3e29f34a
MR
13785 {
13786 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13787 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13788 }
96408a79
SA
13789 }
13790 }
13791 else
b98664d3 13792 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13793 "block nor reference, for DIE %s [in module %s]"),
13794 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13795
13796 call_site->per_cu = cu->per_cu;
9f47c707 13797 call_site->per_objfile = per_objfile;
96408a79
SA
13798
13799 for (child_die = die->child;
13800 child_die && child_die->tag;
436c571c 13801 child_die = child_die->sibling)
96408a79 13802 {
96408a79 13803 struct call_site_parameter *parameter;
1788b2d3 13804 struct attribute *loc, *origin;
96408a79 13805
216f72a1 13806 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13807 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13808 {
13809 /* Already printed the complaint above. */
13810 continue;
13811 }
13812
13813 gdb_assert (call_site->parameter_count < nparams);
13814 parameter = &call_site->parameter[call_site->parameter_count];
13815
1788b2d3
JK
13816 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13817 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13818 register is contained in DW_AT_call_value. */
96408a79 13819
24c5c679 13820 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13821 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13822 if (origin == NULL)
13823 {
13824 /* This was a pre-DWARF-5 GNU extension alias
13825 for DW_AT_call_parameter. */
13826 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13827 }
cd6c91b4 13828 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13829 {
1788b2d3 13830 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13831
0826b30a 13832 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13833 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13834 {
13835 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13836 binding can be done only inside one CU. Such referenced DIE
13837 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13838 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13839 "DW_TAG_call_site child DIE %s [in module %s]"),
13840 sect_offset_str (child_die->sect_off),
9c541725 13841 objfile_name (objfile));
d76b7dbc
JK
13842 continue;
13843 }
9c541725
PA
13844 parameter->u.param_cu_off
13845 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13846 }
4fc6c0d5 13847 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13848 {
b98664d3 13849 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13850 "DW_TAG_call_site child DIE %s [in module %s]"),
13851 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13852 continue;
13853 }
24c5c679 13854 else
96408a79 13855 {
9d2246fc
TT
13856 struct dwarf_block *block = loc->as_block ();
13857
24c5c679 13858 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 13859 (block->data, &block->data[block->size]);
24c5c679
JK
13860 if (parameter->u.dwarf_reg != -1)
13861 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
13862 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13863 &block->data[block->size],
24c5c679
JK
13864 &parameter->u.fb_offset))
13865 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13866 else
13867 {
b98664d3 13868 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13869 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13870 "DW_TAG_call_site child DIE %s "
24c5c679 13871 "[in module %s]"),
9d8780f0 13872 sect_offset_str (child_die->sect_off),
9c541725 13873 objfile_name (objfile));
24c5c679
JK
13874 continue;
13875 }
96408a79
SA
13876 }
13877
216f72a1
JK
13878 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13879 if (attr == NULL)
13880 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13881 if (attr == NULL || !attr->form_is_block ())
96408a79 13882 {
b98664d3 13883 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13884 "DW_TAG_call_site child DIE %s [in module %s]"),
13885 sect_offset_str (child_die->sect_off),
9c541725 13886 objfile_name (objfile));
96408a79
SA
13887 continue;
13888 }
9d2246fc
TT
13889
13890 struct dwarf_block *block = attr->as_block ();
13891 parameter->value = block->data;
13892 parameter->value_size = block->size;
96408a79
SA
13893
13894 /* Parameters are not pre-cleared by memset above. */
13895 parameter->data_value = NULL;
13896 parameter->data_value_size = 0;
13897 call_site->parameter_count++;
13898
216f72a1
JK
13899 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13900 if (attr == NULL)
13901 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13902 if (attr != nullptr)
96408a79 13903 {
4fc6c0d5 13904 if (!attr->form_is_block ())
b98664d3 13905 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13906 "DW_TAG_call_site child DIE %s [in module %s]"),
13907 sect_offset_str (child_die->sect_off),
9c541725 13908 objfile_name (objfile));
96408a79
SA
13909 else
13910 {
9d2246fc
TT
13911 block = attr->as_block ();
13912 parameter->data_value = block->data;
13913 parameter->data_value_size = block->size;
96408a79
SA
13914 }
13915 }
13916 }
13917}
13918
71a3c369
TT
13919/* Helper function for read_variable. If DIE represents a virtual
13920 table, then return the type of the concrete object that is
13921 associated with the virtual table. Otherwise, return NULL. */
13922
13923static struct type *
13924rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13925{
13926 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13927 if (attr == NULL)
13928 return NULL;
13929
13930 /* Find the type DIE. */
13931 struct die_info *type_die = NULL;
13932 struct dwarf2_cu *type_cu = cu;
13933
cd6c91b4 13934 if (attr->form_is_ref ())
71a3c369
TT
13935 type_die = follow_die_ref (die, attr, &type_cu);
13936 if (type_die == NULL)
13937 return NULL;
13938
13939 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13940 return NULL;
13941 return die_containing_type (type_die, type_cu);
13942}
13943
13944/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13945
13946static void
13947read_variable (struct die_info *die, struct dwarf2_cu *cu)
13948{
13949 struct rust_vtable_symbol *storage = NULL;
13950
13951 if (cu->language == language_rust)
13952 {
13953 struct type *containing_type = rust_containing_type (die, cu);
13954
13955 if (containing_type != NULL)
13956 {
5e22e966 13957 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13958
8c14c3a3 13959 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13960 storage->concrete_type = containing_type;
cf724bc9 13961 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13962 }
13963 }
13964
e4a62c65
TV
13965 struct symbol *res = new_symbol (die, NULL, cu, storage);
13966 struct attribute *abstract_origin
13967 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13968 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13969 if (res == NULL && loc && abstract_origin)
13970 {
13971 /* We have a variable without a name, but with a location and an abstract
13972 origin. This may be a concrete instance of an abstract variable
13973 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13974 later. */
13975 struct dwarf2_cu *origin_cu = cu;
13976 struct die_info *origin_die
13977 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13978 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13979 per_objfile->per_bfd->abstract_to_concrete
13980 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13981 }
71a3c369
TT
13982}
13983
43988095
JK
13984/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13985 reading .debug_rnglists.
13986 Callback's type should be:
13987 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13988 Return true if the attributes are present and valid, otherwise,
13989 return false. */
13990
13991template <typename Callback>
13992static bool
13993dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 13994 dwarf_tag tag, Callback &&callback)
43988095 13995{
976ca316
SM
13996 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13997 struct objfile *objfile = per_objfile->objfile;
43988095 13998 bfd *obfd = objfile->obfd;
43988095 13999 /* Base address selection entry. */
2b24b6e4 14000 gdb::optional<CORE_ADDR> base;
43988095 14001 const gdb_byte *buffer;
43988095
JK
14002 CORE_ADDR baseaddr;
14003 bool overflow = false;
d0ce17d8
CT
14004 ULONGEST addr_index;
14005 struct dwarf2_section_info *rnglists_section;
43988095 14006
43988095 14007 base = cu->base_address;
d0ce17d8
CT
14008 rnglists_section = cu_debug_rnglists_section (cu, tag);
14009 rnglists_section->read (objfile);
43988095 14010
d0ce17d8 14011 if (offset >= rnglists_section->size)
43988095 14012 {
b98664d3 14013 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14014 offset);
14015 return false;
14016 }
d0ce17d8 14017 buffer = rnglists_section->buffer + offset;
43988095 14018
b3b3bada 14019 baseaddr = objfile->text_section_offset ();
43988095
JK
14020
14021 while (1)
14022 {
7814882a
JK
14023 /* Initialize it due to a false compiler warning. */
14024 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
14025 const gdb_byte *buf_end = (rnglists_section->buffer
14026 + rnglists_section->size);
43988095
JK
14027 unsigned int bytes_read;
14028
14029 if (buffer == buf_end)
14030 {
14031 overflow = true;
14032 break;
14033 }
14034 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14035 switch (rlet)
14036 {
14037 case DW_RLE_end_of_list:
14038 break;
14039 case DW_RLE_base_address:
14040 if (buffer + cu->header.addr_size > buf_end)
14041 {
14042 overflow = true;
14043 break;
14044 }
c8a7a66f 14045 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14046 buffer += bytes_read;
14047 break;
dda83cd7
SM
14048 case DW_RLE_base_addressx:
14049 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14050 buffer += bytes_read;
14051 base = read_addr_index (cu, addr_index);
14052 break;
43988095
JK
14053 case DW_RLE_start_length:
14054 if (buffer + cu->header.addr_size > buf_end)
14055 {
14056 overflow = true;
14057 break;
14058 }
c8a7a66f
TT
14059 range_beginning = cu->header.read_address (obfd, buffer,
14060 &bytes_read);
43988095
JK
14061 buffer += bytes_read;
14062 range_end = (range_beginning
14063 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14064 buffer += bytes_read;
14065 if (buffer > buf_end)
14066 {
14067 overflow = true;
14068 break;
14069 }
14070 break;
d0ce17d8 14071 case DW_RLE_startx_length:
dda83cd7
SM
14072 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14073 buffer += bytes_read;
14074 range_beginning = read_addr_index (cu, addr_index);
14075 if (buffer > buf_end)
14076 {
14077 overflow = true;
14078 break;
14079 }
14080 range_end = (range_beginning
14081 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14082 buffer += bytes_read;
14083 break;
43988095
JK
14084 case DW_RLE_offset_pair:
14085 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14086 buffer += bytes_read;
14087 if (buffer > buf_end)
14088 {
14089 overflow = true;
14090 break;
14091 }
14092 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14093 buffer += bytes_read;
14094 if (buffer > buf_end)
14095 {
14096 overflow = true;
14097 break;
14098 }
14099 break;
14100 case DW_RLE_start_end:
14101 if (buffer + 2 * cu->header.addr_size > buf_end)
14102 {
14103 overflow = true;
14104 break;
14105 }
c8a7a66f
TT
14106 range_beginning = cu->header.read_address (obfd, buffer,
14107 &bytes_read);
43988095 14108 buffer += bytes_read;
c8a7a66f 14109 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14110 buffer += bytes_read;
14111 break;
d0ce17d8 14112 case DW_RLE_startx_endx:
dda83cd7
SM
14113 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14114 buffer += bytes_read;
14115 range_beginning = read_addr_index (cu, addr_index);
14116 if (buffer > buf_end)
14117 {
14118 overflow = true;
14119 break;
14120 }
14121 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14122 buffer += bytes_read;
14123 range_end = read_addr_index (cu, addr_index);
14124 break;
43988095 14125 default:
b98664d3 14126 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14127 return false;
14128 }
14129 if (rlet == DW_RLE_end_of_list || overflow)
14130 break;
14131 if (rlet == DW_RLE_base_address)
14132 continue;
14133
43988095
JK
14134 if (range_beginning > range_end)
14135 {
14136 /* Inverted range entries are invalid. */
b98664d3 14137 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14138 return false;
14139 }
14140
14141 /* Empty range entries have no effect. */
14142 if (range_beginning == range_end)
14143 continue;
14144
d0ce17d8
CT
14145 /* Only DW_RLE_offset_pair needs the base address added. */
14146 if (rlet == DW_RLE_offset_pair)
14147 {
14148 if (!base.has_value ())
14149 {
14150 /* We have no valid base address for the DW_RLE_offset_pair. */
14151 complaint (_("Invalid .debug_rnglists data (no base address for "
14152 "DW_RLE_offset_pair)"));
14153 return false;
14154 }
14155
14156 range_beginning += *base;
14157 range_end += *base;
14158 }
43988095
JK
14159
14160 /* A not-uncommon case of bad debug info.
14161 Don't pollute the addrmap with bad data. */
14162 if (range_beginning + baseaddr == 0
976ca316 14163 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14164 {
b98664d3 14165 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14166 " [in module %s]"), objfile_name (objfile));
14167 continue;
14168 }
14169
14170 callback (range_beginning, range_end);
14171 }
14172
14173 if (overflow)
14174 {
b98664d3 14175 complaint (_("Offset %d is not terminated "
43988095
JK
14176 "for DW_AT_ranges attribute"),
14177 offset);
14178 return false;
14179 }
14180
14181 return true;
14182}
14183
14184/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14185 Callback's type should be:
14186 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14187 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14188
43988095 14189template <typename Callback>
43039443 14190static int
d0ce17d8 14191dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14192 Callback &&callback)
43039443 14193{
5e22e966
SM
14194 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14195 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14196 struct comp_unit_head *cu_header = &cu->header;
14197 bfd *obfd = objfile->obfd;
14198 unsigned int addr_size = cu_header->addr_size;
14199 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14200 /* Base address selection entry. */
2b24b6e4 14201 gdb::optional<CORE_ADDR> base;
43039443 14202 unsigned int dummy;
d521ce57 14203 const gdb_byte *buffer;
ff013f42 14204 CORE_ADDR baseaddr;
43039443 14205
43988095 14206 if (cu_header->version >= 5)
d0ce17d8 14207 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14208
d00adf39 14209 base = cu->base_address;
43039443 14210
5e22e966
SM
14211 per_objfile->per_bfd->ranges.read (objfile);
14212 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14213 {
b98664d3 14214 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14215 offset);
14216 return 0;
14217 }
5e22e966 14218 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14219
b3b3bada 14220 baseaddr = objfile->text_section_offset ();
ff013f42 14221
43039443
JK
14222 while (1)
14223 {
14224 CORE_ADDR range_beginning, range_end;
14225
c8a7a66f 14226 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14227 buffer += addr_size;
c8a7a66f 14228 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14229 buffer += addr_size;
14230 offset += 2 * addr_size;
14231
14232 /* An end of list marker is a pair of zero addresses. */
14233 if (range_beginning == 0 && range_end == 0)
14234 /* Found the end of list entry. */
14235 break;
14236
14237 /* Each base address selection entry is a pair of 2 values.
14238 The first is the largest possible address, the second is
14239 the base address. Check for a base address here. */
14240 if ((range_beginning & mask) == mask)
14241 {
28d2bfb9
AB
14242 /* If we found the largest possible address, then we already
14243 have the base address in range_end. */
14244 base = range_end;
43039443
JK
14245 continue;
14246 }
14247
2b24b6e4 14248 if (!base.has_value ())
43039443
JK
14249 {
14250 /* We have no valid base address for the ranges
14251 data. */
b98664d3 14252 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14253 return 0;
14254 }
14255
9277c30c
UW
14256 if (range_beginning > range_end)
14257 {
14258 /* Inverted range entries are invalid. */
b98664d3 14259 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14260 return 0;
14261 }
14262
14263 /* Empty range entries have no effect. */
14264 if (range_beginning == range_end)
14265 continue;
14266
2b24b6e4
TT
14267 range_beginning += *base;
14268 range_end += *base;
43039443 14269
01093045
DE
14270 /* A not-uncommon case of bad debug info.
14271 Don't pollute the addrmap with bad data. */
14272 if (range_beginning + baseaddr == 0
5e22e966 14273 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14274 {
b98664d3 14275 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14276 " [in module %s]"), objfile_name (objfile));
01093045
DE
14277 continue;
14278 }
14279
5f46c5a5
JK
14280 callback (range_beginning, range_end);
14281 }
14282
14283 return 1;
14284}
14285
14286/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14287 Return 1 if the attributes are present and valid, otherwise, return 0.
efd7398e 14288 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
5f46c5a5
JK
14289
14290static int
14291dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14292 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14293 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14294{
5e22e966 14295 struct objfile *objfile = cu->per_objfile->objfile;
84685904 14296 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
08feed99 14297 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14298 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14299 int low_set = 0;
14300 CORE_ADDR low = 0;
14301 CORE_ADDR high = 0;
14302 int retval;
14303
d0ce17d8 14304 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14305 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14306 {
9277c30c 14307 if (ranges_pst != NULL)
3e29f34a
MR
14308 {
14309 CORE_ADDR lowpc;
14310 CORE_ADDR highpc;
14311
79748972
TT
14312 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14313 range_beginning + baseaddr)
14314 - baseaddr);
14315 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14316 range_end + baseaddr)
14317 - baseaddr);
84685904 14318 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
d320c2b5 14319 lowpc, highpc - 1, ranges_pst);
3e29f34a 14320 }
ff013f42 14321
43039443
JK
14322 /* FIXME: This is recording everything as a low-high
14323 segment of consecutive addresses. We should have a
14324 data structure for discontiguous block ranges
14325 instead. */
14326 if (! low_set)
14327 {
14328 low = range_beginning;
14329 high = range_end;
14330 low_set = 1;
14331 }
14332 else
14333 {
14334 if (range_beginning < low)
14335 low = range_beginning;
14336 if (range_end > high)
14337 high = range_end;
14338 }
5f46c5a5
JK
14339 });
14340 if (!retval)
14341 return 0;
43039443
JK
14342
14343 if (! low_set)
14344 /* If the first entry is an end-of-list marker, the range
14345 describes an empty scope, i.e. no instructions. */
14346 return 0;
14347
14348 if (low_return)
14349 *low_return = low;
14350 if (high_return)
14351 *high_return = high;
14352 return 1;
14353}
14354
3a2b436a
JK
14355/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14356 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14357 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14358
3a2b436a 14359static enum pc_bounds_kind
af34e669 14360dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14361 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14362 dwarf2_psymtab *pst)
c906108c 14363{
976ca316 14364 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14365 struct attribute *attr;
91da1414 14366 struct attribute *attr_high;
af34e669
DJ
14367 CORE_ADDR low = 0;
14368 CORE_ADDR high = 0;
e385593e 14369 enum pc_bounds_kind ret;
c906108c 14370
91da1414
MW
14371 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14372 if (attr_high)
af34e669 14373 {
e142c38c 14374 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14375 if (attr != nullptr)
dda83cd7 14376 {
95f982e5
TT
14377 low = attr->as_address ();
14378 high = attr_high->as_address ();
cd6c91b4 14379 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14380 high += low;
91da1414 14381 }
af34e669
DJ
14382 else
14383 /* Found high w/o low attribute. */
e385593e 14384 return PC_BOUNDS_INVALID;
af34e669
DJ
14385
14386 /* Found consecutive range of addresses. */
3a2b436a 14387 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14388 }
c906108c 14389 else
af34e669 14390 {
e142c38c 14391 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14392 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14393 {
2b0c7f41
SM
14394 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14395 on DWARF version). */
14396 ULONGEST ranges_offset = attr->as_unsigned ();
14397
14398 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14399 this value. */
14400 if (die->tag != DW_TAG_compile_unit)
14401 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14402
af34e669 14403 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14404 .debug_ranges section. */
d0ce17d8
CT
14405 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14406 die->tag))
e385593e 14407 return PC_BOUNDS_INVALID;
43039443 14408 /* Found discontinuous range of addresses. */
3a2b436a 14409 ret = PC_BOUNDS_RANGES;
af34e669 14410 }
e385593e
JK
14411 else
14412 return PC_BOUNDS_NOT_PRESENT;
af34e669 14413 }
c906108c 14414
48fbe735 14415 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14416 if (high <= low)
e385593e 14417 return PC_BOUNDS_INVALID;
c906108c
SS
14418
14419 /* When using the GNU linker, .gnu.linkonce. sections are used to
14420 eliminate duplicate copies of functions and vtables and such.
14421 The linker will arbitrarily choose one and discard the others.
14422 The AT_*_pc values for such functions refer to local labels in
14423 these sections. If the section from that file was discarded, the
14424 labels are not in the output, so the relocs get a value of 0.
14425 If this is a discarded function, mark the pc bounds as invalid,
14426 so that GDB will ignore it. */
976ca316 14427 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14428 return PC_BOUNDS_INVALID;
c906108c
SS
14429
14430 *lowpc = low;
96408a79
SA
14431 if (highpc)
14432 *highpc = high;
af34e669 14433 return ret;
c906108c
SS
14434}
14435
b084d499
JB
14436/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14437 its low and high PC addresses. Do nothing if these addresses could not
14438 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14439 and HIGHPC to the high address if greater than HIGHPC. */
14440
14441static void
14442dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14443 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14444 struct dwarf2_cu *cu)
b084d499
JB
14445{
14446 CORE_ADDR low, high;
14447 struct die_info *child = die->child;
14448
e385593e 14449 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14450 {
325fac50
PA
14451 *lowpc = std::min (*lowpc, low);
14452 *highpc = std::max (*highpc, high);
b084d499
JB
14453 }
14454
14455 /* If the language does not allow nested subprograms (either inside
14456 subprograms or lexical blocks), we're done. */
14457 if (cu->language != language_ada)
14458 return;
6e70227d 14459
b084d499
JB
14460 /* Check all the children of the given DIE. If it contains nested
14461 subprograms, then check their pc bounds. Likewise, we need to
14462 check lexical blocks as well, as they may also contain subprogram
14463 definitions. */
14464 while (child && child->tag)
14465 {
14466 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14467 || child->tag == DW_TAG_lexical_block)
14468 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14469 child = child->sibling;
b084d499
JB
14470 }
14471}
14472
fae299cd
DC
14473/* Get the low and high pc's represented by the scope DIE, and store
14474 them in *LOWPC and *HIGHPC. If the correct values can't be
14475 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14476
14477static void
14478get_scope_pc_bounds (struct die_info *die,
14479 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14480 struct dwarf2_cu *cu)
14481{
14482 CORE_ADDR best_low = (CORE_ADDR) -1;
14483 CORE_ADDR best_high = (CORE_ADDR) 0;
14484 CORE_ADDR current_low, current_high;
14485
3a2b436a 14486 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14487 >= PC_BOUNDS_RANGES)
fae299cd
DC
14488 {
14489 best_low = current_low;
14490 best_high = current_high;
14491 }
14492 else
14493 {
14494 struct die_info *child = die->child;
14495
14496 while (child && child->tag)
14497 {
14498 switch (child->tag) {
14499 case DW_TAG_subprogram:
dda83cd7 14500 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14501 break;
14502 case DW_TAG_namespace:
f55ee35c 14503 case DW_TAG_module:
fae299cd
DC
14504 /* FIXME: carlton/2004-01-16: Should we do this for
14505 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14506 that current GCC's always emit the DIEs corresponding
14507 to definitions of methods of classes as children of a
14508 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14509 the DIEs giving the declarations, which could be
14510 anywhere). But I don't see any reason why the
14511 standards says that they have to be there. */
14512 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14513
14514 if (current_low != ((CORE_ADDR) -1))
14515 {
325fac50
PA
14516 best_low = std::min (best_low, current_low);
14517 best_high = std::max (best_high, current_high);
fae299cd
DC
14518 }
14519 break;
14520 default:
0963b4bd 14521 /* Ignore. */
fae299cd
DC
14522 break;
14523 }
14524
436c571c 14525 child = child->sibling;
fae299cd
DC
14526 }
14527 }
14528
14529 *lowpc = best_low;
14530 *highpc = best_high;
14531}
14532
801e3a5b
JB
14533/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14534 in DIE. */
380bca97 14535
801e3a5b
JB
14536static void
14537dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14538 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14539{
5e22e966 14540 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14541 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14542 struct attribute *attr;
91da1414 14543 struct attribute *attr_high;
801e3a5b 14544
91da1414
MW
14545 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14546 if (attr_high)
801e3a5b 14547 {
801e3a5b 14548 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14549 if (attr != nullptr)
dda83cd7 14550 {
95f982e5
TT
14551 CORE_ADDR low = attr->as_address ();
14552 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14553
cd6c91b4 14554 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14555 high += low;
9a619af0 14556
3e29f34a
MR
14557 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14558 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14559 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14560 }
801e3a5b
JB
14561 }
14562
14563 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14564 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14565 {
2b0c7f41
SM
14566 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14567 on DWARF version). */
14568 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14569
2b0c7f41
SM
14570 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14571 this value. */
14572 if (die->tag != DW_TAG_compile_unit)
14573 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14574
2d5f09ec 14575 std::vector<blockrange> blockvec;
2b0c7f41 14576 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14577 [&] (CORE_ADDR start, CORE_ADDR end)
14578 {
58fdfd2c
JK
14579 start += baseaddr;
14580 end += baseaddr;
5f46c5a5
JK
14581 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14582 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14583 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14584 blockvec.emplace_back (start, end);
5f46c5a5 14585 });
2d5f09ec
KB
14586
14587 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14588 }
14589}
14590
685b1105
JK
14591/* Check whether the producer field indicates either of GCC < 4.6, or the
14592 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14593
685b1105
JK
14594static void
14595check_producer (struct dwarf2_cu *cu)
60d5a603 14596{
38360086 14597 int major, minor;
60d5a603
JK
14598
14599 if (cu->producer == NULL)
14600 {
14601 /* For unknown compilers expect their behavior is DWARF version
14602 compliant.
14603
14604 GCC started to support .debug_types sections by -gdwarf-4 since
14605 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14606 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14607 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14608 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14609 }
b1ffba5a 14610 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14611 {
38360086
MW
14612 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14613 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14614 }
5230b05a 14615 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14616 {
14617 cu->producer_is_icc = true;
14618 cu->producer_is_icc_lt_14 = major < 14;
14619 }
c258c396
JD
14620 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14621 cu->producer_is_codewarrior = true;
685b1105
JK
14622 else
14623 {
14624 /* For other non-GCC compilers, expect their behavior is DWARF version
14625 compliant. */
60d5a603
JK
14626 }
14627
9068261f 14628 cu->checked_producer = true;
685b1105 14629}
ba919b58 14630
685b1105
JK
14631/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14632 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14633 during 4.6.0 experimental. */
14634
9068261f 14635static bool
685b1105
JK
14636producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14637{
14638 if (!cu->checked_producer)
14639 check_producer (cu);
14640
14641 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14642}
14643
c258c396
JD
14644
14645/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14646 with incorrect is_stmt attributes. */
14647
14648static bool
14649producer_is_codewarrior (struct dwarf2_cu *cu)
14650{
14651 if (!cu->checked_producer)
14652 check_producer (cu);
14653
14654 return cu->producer_is_codewarrior;
14655}
14656
bf23a268
TT
14657/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14658 If that attribute is not available, return the appropriate
14659 default. */
60d5a603
JK
14660
14661static enum dwarf_access_attribute
bf23a268 14662dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14663{
bf23a268
TT
14664 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14665 if (attr != nullptr)
14666 {
14667 LONGEST value = attr->constant_value (-1);
14668 if (value == DW_ACCESS_public
14669 || value == DW_ACCESS_protected
14670 || value == DW_ACCESS_private)
14671 return (dwarf_access_attribute) value;
14672 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14673 plongest (value));
14674 }
14675
60d5a603
JK
14676 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14677 {
14678 /* The default DWARF 2 accessibility for members is public, the default
14679 accessibility for inheritance is private. */
14680
14681 if (die->tag != DW_TAG_inheritance)
14682 return DW_ACCESS_public;
14683 else
14684 return DW_ACCESS_private;
14685 }
14686 else
14687 {
14688 /* DWARF 3+ defines the default accessibility a different way. The same
14689 rules apply now for DW_TAG_inheritance as for the members and it only
14690 depends on the container kind. */
14691
14692 if (die->parent->tag == DW_TAG_class_type)
14693 return DW_ACCESS_private;
14694 else
14695 return DW_ACCESS_public;
14696 }
14697}
14698
74ac6d43
TT
14699/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14700 offset. If the attribute was not found return 0, otherwise return
14701 1. If it was found but could not properly be handled, set *OFFSET
14702 to 0. */
14703
14704static int
14705handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14706 LONGEST *offset)
14707{
14708 struct attribute *attr;
14709
14710 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14711 if (attr != NULL)
14712 {
14713 *offset = 0;
14714
14715 /* Note that we do not check for a section offset first here.
14716 This is because DW_AT_data_member_location is new in DWARF 4,
14717 so if we see it, we can assume that a constant form is really
14718 a constant and not a section offset. */
cd6c91b4 14719 if (attr->form_is_constant ())
0826b30a 14720 *offset = attr->constant_value (0);
cd6c91b4 14721 else if (attr->form_is_section_offset ())
74ac6d43 14722 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14723 else if (attr->form_is_block ())
9d2246fc 14724 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14725 else
14726 dwarf2_complex_location_expr_complaint ();
14727
14728 return 1;
14729 }
14730
14731 return 0;
14732}
14733
7d79de9a
TT
14734/* Look for DW_AT_data_member_location and store the results in FIELD. */
14735
14736static void
14737handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14738 struct field *field)
14739{
14740 struct attribute *attr;
14741
14742 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14743 if (attr != NULL)
14744 {
14745 if (attr->form_is_constant ())
14746 {
14747 LONGEST offset = attr->constant_value (0);
14748 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14749 }
14750 else if (attr->form_is_section_offset ())
14751 dwarf2_complex_location_expr_complaint ();
14752 else if (attr->form_is_block ())
14753 {
14754 bool handled;
9d2246fc 14755 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14756 if (handled)
14757 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14758 else
14759 {
5e22e966
SM
14760 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14761 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14762 struct dwarf2_locexpr_baton *dlbaton
14763 = XOBNEW (&objfile->objfile_obstack,
14764 struct dwarf2_locexpr_baton);
9d2246fc
TT
14765 dlbaton->data = attr->as_block ()->data;
14766 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14767 /* When using this baton, we want to compute the address
14768 of the field, not the value. This is why
14769 is_reference is set to false here. */
14770 dlbaton->is_reference = false;
5e22e966 14771 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14772 dlbaton->per_cu = cu->per_cu;
14773
14774 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14775 }
14776 }
14777 else
14778 dwarf2_complex_location_expr_complaint ();
14779 }
14780}
14781
c906108c
SS
14782/* Add an aggregate field to the field list. */
14783
14784static void
107d2387 14785dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14786 struct dwarf2_cu *cu)
6e70227d 14787{
5e22e966 14788 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14789 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14790 struct nextfield *new_field;
14791 struct attribute *attr;
14792 struct field *fp;
15d034d0 14793 const char *fieldname = "";
c906108c 14794
7d0ccb61
DJ
14795 if (die->tag == DW_TAG_inheritance)
14796 {
be2daae6
TT
14797 fip->baseclasses.emplace_back ();
14798 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14799 }
14800 else
14801 {
be2daae6
TT
14802 fip->fields.emplace_back ();
14803 new_field = &fip->fields.back ();
7d0ccb61 14804 }
be2daae6 14805
9c6a1327
TT
14806 new_field->offset = die->sect_off;
14807
bf23a268 14808 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 14809 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 14810 fip->non_public_fields = true;
60d5a603 14811
e142c38c 14812 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14813 if (attr != nullptr)
23dca5c3 14814 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
14815 else
14816 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14817
14818 fp = &new_field->field;
a9a9bd0f 14819
e142c38c 14820 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14821 {
a9a9bd0f 14822 /* Data member other than a C++ static data member. */
6e70227d 14823
c906108c 14824 /* Get type of field. */
5d14b6e5 14825 fp->set_type (die_type (die, cu));
c906108c 14826
d6a843b5 14827 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14828
c906108c 14829 /* Get bit size of field (zero if none). */
e142c38c 14830 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14831 if (attr != nullptr)
c906108c 14832 {
529908cb 14833 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
14834 }
14835 else
14836 {
14837 FIELD_BITSIZE (*fp) = 0;
14838 }
14839
14840 /* Get bit offset of field. */
7d79de9a 14841 handle_data_member_location (die, cu, fp);
e142c38c 14842 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 14843 if (attr != nullptr && attr->form_is_constant ())
c906108c 14844 {
d5a22e77 14845 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14846 {
14847 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
14848 additional bit offset from the MSB of the containing
14849 anonymous object to the MSB of the field. We don't
14850 have to do anything special since we don't need to
14851 know the size of the anonymous object. */
529908cb 14852 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 14853 + attr->constant_value (0)));
c906108c
SS
14854 }
14855 else
14856 {
14857 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
14858 MSB of the anonymous object, subtract off the number of
14859 bits from the MSB of the field to the MSB of the
14860 object, and then subtract off the number of bits of
14861 the field itself. The result is the bit offset of
14862 the LSB of the field. */
c906108c 14863 int anonymous_size;
7c184d33 14864 int bit_offset = attr->constant_value (0);
c906108c 14865
e142c38c 14866 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 14867 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
14868 {
14869 /* The size of the anonymous object containing
14870 the bit field is explicit, so use the
14871 indicated size (in bytes). */
7c184d33 14872 anonymous_size = attr->constant_value (0);
c906108c
SS
14873 }
14874 else
14875 {
14876 /* The size of the anonymous object containing
14877 the bit field must be inferred from the type
14878 attribute of the data member containing the
14879 bit field. */
5d14b6e5 14880 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14881 }
f41f5e61
PA
14882 SET_FIELD_BITPOS (*fp,
14883 (FIELD_BITPOS (*fp)
14884 + anonymous_size * bits_per_byte
14885 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14886 }
14887 }
da5b30da
AA
14888 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14889 if (attr != NULL)
14890 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14891 + attr->constant_value (0)));
c906108c
SS
14892
14893 /* Get name of field. */
39cbfefa
DJ
14894 fieldname = dwarf2_name (die, cu);
14895 if (fieldname == NULL)
14896 fieldname = "";
d8151005
DJ
14897
14898 /* The name is already allocated along with this objfile, so we don't
14899 need to duplicate it for the type. */
14900 fp->name = fieldname;
c906108c
SS
14901
14902 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 14903 pointer or virtual base class pointer) to private. */
e142c38c 14904 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14905 {
d48cc9dd 14906 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 14907 new_field->accessibility = DW_ACCESS_private;
264fc0e2 14908 fip->non_public_fields = true;
c906108c
SS
14909 }
14910 }
a9a9bd0f 14911 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14912 {
a9a9bd0f
DC
14913 /* C++ static member. */
14914
14915 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14916 is a declaration, but all versions of G++ as of this writing
14917 (so through at least 3.2.1) incorrectly generate
14918 DW_TAG_variable tags. */
6e70227d 14919
ff355380 14920 const char *physname;
c906108c 14921
a9a9bd0f 14922 /* Get name of field. */
39cbfefa
DJ
14923 fieldname = dwarf2_name (die, cu);
14924 if (fieldname == NULL)
c906108c
SS
14925 return;
14926
254e6b9e 14927 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14928 if (attr
14929 /* Only create a symbol if this is an external value.
14930 new_symbol checks this and puts the value in the global symbol
14931 table, which we want. If it is not external, new_symbol
14932 will try to put the value in cu->list_in_scope which is wrong. */
14933 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14934 {
14935 /* A static const member, not much different than an enum as far as
14936 we're concerned, except that we can support more types. */
14937 new_symbol (die, NULL, cu);
14938 }
14939
2df3850c 14940 /* Get physical name. */
ff355380 14941 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14942
d8151005
DJ
14943 /* The name is already allocated along with this objfile, so we don't
14944 need to duplicate it for the type. */
14945 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 14946 fp->set_type (die_type (die, cu));
d8151005 14947 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14948 }
14949 else if (die->tag == DW_TAG_inheritance)
14950 {
74ac6d43 14951 /* C++ base class field. */
7d79de9a 14952 handle_data_member_location (die, cu, fp);
c906108c 14953 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
14954 fp->set_type (die_type (die, cu));
14955 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 14956 }
2ddeaf8a
TT
14957 else
14958 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14959}
14960
883fd55a
KS
14961/* Can the type given by DIE define another type? */
14962
14963static bool
14964type_can_define_types (const struct die_info *die)
14965{
14966 switch (die->tag)
14967 {
14968 case DW_TAG_typedef:
14969 case DW_TAG_class_type:
14970 case DW_TAG_structure_type:
14971 case DW_TAG_union_type:
14972 case DW_TAG_enumeration_type:
14973 return true;
14974
14975 default:
14976 return false;
14977 }
14978}
14979
14980/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14981
14982static void
883fd55a
KS
14983dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14984 struct dwarf2_cu *cu)
6e70227d 14985{
be2daae6
TT
14986 struct decl_field fp;
14987 memset (&fp, 0, sizeof (fp));
98751a41 14988
883fd55a 14989 gdb_assert (type_can_define_types (die));
98751a41 14990
883fd55a 14991 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14992 fp.name = dwarf2_name (die, cu);
14993 fp.type = read_type_die (die, cu);
98751a41 14994
c191a687 14995 /* Save accessibility. */
bf23a268 14996 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
14997 switch (accessibility)
14998 {
14999 case DW_ACCESS_public:
15000 /* The assumed value if neither private nor protected. */
15001 break;
15002 case DW_ACCESS_private:
be2daae6 15003 fp.is_private = 1;
c191a687
KS
15004 break;
15005 case DW_ACCESS_protected:
be2daae6 15006 fp.is_protected = 1;
c191a687 15007 break;
c191a687
KS
15008 }
15009
883fd55a 15010 if (die->tag == DW_TAG_typedef)
be2daae6 15011 fip->typedef_field_list.push_back (fp);
883fd55a 15012 else
be2daae6 15013 fip->nested_types_list.push_back (fp);
98751a41
JK
15014}
15015
9c6a1327
TT
15016/* A convenience typedef that's used when finding the discriminant
15017 field for a variant part. */
1b95cdb7
SM
15018typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15019 offset_map_type;
9c6a1327
TT
15020
15021/* Compute the discriminant range for a given variant. OBSTACK is
15022 where the results will be stored. VARIANT is the variant to
15023 process. IS_UNSIGNED indicates whether the discriminant is signed
15024 or unsigned. */
15025
15026static const gdb::array_view<discriminant_range>
15027convert_variant_range (struct obstack *obstack, const variant_field &variant,
15028 bool is_unsigned)
15029{
15030 std::vector<discriminant_range> ranges;
15031
15032 if (variant.default_branch)
15033 return {};
15034
15035 if (variant.discr_list_data == nullptr)
15036 {
15037 discriminant_range r
15038 = {variant.discriminant_value, variant.discriminant_value};
15039 ranges.push_back (r);
15040 }
15041 else
15042 {
15043 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15044 variant.discr_list_data->size);
15045 while (!data.empty ())
15046 {
15047 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15048 {
15049 complaint (_("invalid discriminant marker: %d"), data[0]);
15050 break;
15051 }
15052 bool is_range = data[0] == DW_DSC_range;
15053 data = data.slice (1);
15054
15055 ULONGEST low, high;
15056 unsigned int bytes_read;
15057
15058 if (data.empty ())
15059 {
15060 complaint (_("DW_AT_discr_list missing low value"));
15061 break;
15062 }
15063 if (is_unsigned)
15064 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15065 else
15066 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15067 &bytes_read);
15068 data = data.slice (bytes_read);
15069
15070 if (is_range)
15071 {
15072 if (data.empty ())
15073 {
15074 complaint (_("DW_AT_discr_list missing high value"));
15075 break;
15076 }
15077 if (is_unsigned)
15078 high = read_unsigned_leb128 (nullptr, data.data (),
15079 &bytes_read);
15080 else
15081 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15082 &bytes_read);
15083 data = data.slice (bytes_read);
15084 }
15085 else
15086 high = low;
15087
15088 ranges.push_back ({ low, high });
15089 }
15090 }
15091
15092 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15093 ranges.size ());
15094 std::copy (ranges.begin (), ranges.end (), result);
15095 return gdb::array_view<discriminant_range> (result, ranges.size ());
15096}
15097
15098static const gdb::array_view<variant_part> create_variant_parts
15099 (struct obstack *obstack,
15100 const offset_map_type &offset_map,
15101 struct field_info *fi,
15102 const std::vector<variant_part_builder> &variant_parts);
15103
15104/* Fill in a "struct variant" for a given variant field. RESULT is
15105 the variant to fill in. OBSTACK is where any needed allocations
15106 will be done. OFFSET_MAP holds the mapping from section offsets to
15107 fields for the type. FI describes the fields of the type we're
15108 processing. FIELD is the variant field we're converting. */
15109
15110static void
15111create_one_variant (variant &result, struct obstack *obstack,
15112 const offset_map_type &offset_map,
15113 struct field_info *fi, const variant_field &field)
15114{
15115 result.discriminants = convert_variant_range (obstack, field, false);
15116 result.first_field = field.first_field + fi->baseclasses.size ();
15117 result.last_field = field.last_field + fi->baseclasses.size ();
15118 result.parts = create_variant_parts (obstack, offset_map, fi,
15119 field.variant_parts);
15120}
15121
15122/* Fill in a "struct variant_part" for a given variant part. RESULT
15123 is the variant part to fill in. OBSTACK is where any needed
15124 allocations will be done. OFFSET_MAP holds the mapping from
15125 section offsets to fields for the type. FI describes the fields of
15126 the type we're processing. BUILDER is the variant part to be
15127 converted. */
15128
15129static void
15130create_one_variant_part (variant_part &result,
15131 struct obstack *obstack,
15132 const offset_map_type &offset_map,
15133 struct field_info *fi,
15134 const variant_part_builder &builder)
15135{
15136 auto iter = offset_map.find (builder.discriminant_offset);
15137 if (iter == offset_map.end ())
15138 {
15139 result.discriminant_index = -1;
15140 /* Doesn't matter. */
15141 result.is_unsigned = false;
15142 }
15143 else
15144 {
15145 result.discriminant_index = iter->second;
15146 result.is_unsigned
c6d940a9 15147 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15148 }
15149
15150 size_t n = builder.variants.size ();
15151 variant *output = new (obstack) variant[n];
15152 for (size_t i = 0; i < n; ++i)
15153 create_one_variant (output[i], obstack, offset_map, fi,
15154 builder.variants[i]);
15155
15156 result.variants = gdb::array_view<variant> (output, n);
15157}
15158
15159/* Create a vector of variant parts that can be attached to a type.
15160 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15161 holds the mapping from section offsets to fields for the type. FI
15162 describes the fields of the type we're processing. VARIANT_PARTS
15163 is the vector to convert. */
15164
15165static const gdb::array_view<variant_part>
15166create_variant_parts (struct obstack *obstack,
15167 const offset_map_type &offset_map,
15168 struct field_info *fi,
15169 const std::vector<variant_part_builder> &variant_parts)
15170{
15171 if (variant_parts.empty ())
15172 return {};
15173
15174 size_t n = variant_parts.size ();
15175 variant_part *result = new (obstack) variant_part[n];
15176 for (size_t i = 0; i < n; ++i)
15177 create_one_variant_part (result[i], obstack, offset_map, fi,
15178 variant_parts[i]);
15179
15180 return gdb::array_view<variant_part> (result, n);
15181}
15182
15183/* Compute the variant part vector for FIP, attaching it to TYPE when
15184 done. */
15185
15186static void
15187add_variant_property (struct field_info *fip, struct type *type,
15188 struct dwarf2_cu *cu)
15189{
15190 /* Map section offsets of fields to their field index. Note the
15191 field index here does not take the number of baseclasses into
15192 account. */
15193 offset_map_type offset_map;
15194 for (int i = 0; i < fip->fields.size (); ++i)
15195 offset_map[fip->fields[i].offset] = i;
15196
5e22e966 15197 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15198 gdb::array_view<variant_part> parts
15199 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15200 fip->variant_parts);
15201
15202 struct dynamic_prop prop;
8c2e4e06
SM
15203 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15204 obstack_copy (&objfile->objfile_obstack, &parts,
15205 sizeof (parts)));
9c6a1327 15206
5c54719c 15207 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15208}
15209
c906108c
SS
15210/* Create the vector of fields, and attach it to the type. */
15211
15212static void
fba45db2 15213dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15214 struct dwarf2_cu *cu)
c906108c 15215{
317f7127 15216 int nfields = fip->nfields ();
c906108c
SS
15217
15218 /* Record the field count, allocate space for the array of fields,
15219 and create blank accessibility bitfields if necessary. */
5e33d5f4 15220 type->set_num_fields (nfields);
3cabb6b0
SM
15221 type->set_fields
15222 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15223
b4ba55a1 15224 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15225 {
15226 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15227
15228 TYPE_FIELD_PRIVATE_BITS (type) =
15229 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15230 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15231
15232 TYPE_FIELD_PROTECTED_BITS (type) =
15233 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15234 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15235
774b6a14
TT
15236 TYPE_FIELD_IGNORE_BITS (type) =
15237 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15238 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15239 }
15240
15241 /* If the type has baseclasses, allocate and clear a bit vector for
15242 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15243 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15244 {
be2daae6 15245 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15246 unsigned char *pointer;
c906108c
SS
15247
15248 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15249 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15250 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15251 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15252 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15253 }
15254
9c6a1327
TT
15255 if (!fip->variant_parts.empty ())
15256 add_variant_property (fip, type, cu);
2ddeaf8a 15257
be2daae6
TT
15258 /* Copy the saved-up fields into the field vector. */
15259 for (int i = 0; i < nfields; ++i)
c906108c 15260 {
be2daae6
TT
15261 struct nextfield &field
15262 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15263 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15264
ceacbf6e 15265 type->field (i) = field.field;
be2daae6 15266 switch (field.accessibility)
c906108c 15267 {
c5aa993b 15268 case DW_ACCESS_private:
b4ba55a1 15269 if (cu->language != language_ada)
be2daae6 15270 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15271 break;
c906108c 15272
c5aa993b 15273 case DW_ACCESS_protected:
b4ba55a1 15274 if (cu->language != language_ada)
be2daae6 15275 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15276 break;
c906108c 15277
c5aa993b
JM
15278 case DW_ACCESS_public:
15279 break;
c906108c 15280
c5aa993b
JM
15281 default:
15282 /* Unknown accessibility. Complain and treat it as public. */
15283 {
b98664d3 15284 complaint (_("unsupported accessibility %d"),
be2daae6 15285 field.accessibility);
c5aa993b
JM
15286 }
15287 break;
c906108c 15288 }
be2daae6 15289 if (i < fip->baseclasses.size ())
c906108c 15290 {
be2daae6 15291 switch (field.virtuality)
c906108c 15292 {
c5aa993b
JM
15293 case DW_VIRTUALITY_virtual:
15294 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15295 if (cu->language == language_ada)
a73c6dcd 15296 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15297 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15298 break;
c906108c
SS
15299 }
15300 }
c906108c
SS
15301 }
15302}
15303
7d27a96d
TT
15304/* Return true if this member function is a constructor, false
15305 otherwise. */
15306
15307static int
15308dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15309{
15310 const char *fieldname;
fe978cb0 15311 const char *type_name;
7d27a96d
TT
15312 int len;
15313
15314 if (die->parent == NULL)
15315 return 0;
15316
15317 if (die->parent->tag != DW_TAG_structure_type
15318 && die->parent->tag != DW_TAG_union_type
15319 && die->parent->tag != DW_TAG_class_type)
15320 return 0;
15321
15322 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15323 type_name = dwarf2_name (die->parent, cu);
15324 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15325 return 0;
15326
15327 len = strlen (fieldname);
fe978cb0
PA
15328 return (strncmp (fieldname, type_name, len) == 0
15329 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15330}
15331
c906108c
SS
15332/* Add a member function to the proper fieldlist. */
15333
15334static void
107d2387 15335dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15336 struct type *type, struct dwarf2_cu *cu)
c906108c 15337{
5e22e966 15338 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15339 struct attribute *attr;
c906108c 15340 int i;
be2daae6 15341 struct fnfieldlist *flp = nullptr;
c906108c 15342 struct fn_field *fnp;
15d034d0 15343 const char *fieldname;
f792889a 15344 struct type *this_type;
c906108c 15345
b4ba55a1 15346 if (cu->language == language_ada)
a73c6dcd 15347 error (_("unexpected member function in Ada type"));
b4ba55a1 15348
2df3850c 15349 /* Get name of member function. */
39cbfefa
DJ
15350 fieldname = dwarf2_name (die, cu);
15351 if (fieldname == NULL)
2df3850c 15352 return;
c906108c 15353
c906108c 15354 /* Look up member function name in fieldlist. */
be2daae6 15355 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15356 {
27bfe10e 15357 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15358 {
15359 flp = &fip->fnfieldlists[i];
15360 break;
15361 }
c906108c
SS
15362 }
15363
be2daae6
TT
15364 /* Create a new fnfieldlist if necessary. */
15365 if (flp == nullptr)
c906108c 15366 {
be2daae6
TT
15367 fip->fnfieldlists.emplace_back ();
15368 flp = &fip->fnfieldlists.back ();
c906108c 15369 flp->name = fieldname;
be2daae6 15370 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15371 }
15372
be2daae6
TT
15373 /* Create a new member function field and add it to the vector of
15374 fnfieldlists. */
15375 flp->fnfields.emplace_back ();
15376 fnp = &flp->fnfields.back ();
3da10d80
KS
15377
15378 /* Delay processing of the physname until later. */
9c37b5ae 15379 if (cu->language == language_cplus)
be2daae6
TT
15380 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15381 die, cu);
3da10d80
KS
15382 else
15383 {
1d06ead6 15384 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15385 fnp->physname = physname ? physname : "";
15386 }
15387
c906108c 15388 fnp->type = alloc_type (objfile);
f792889a 15389 this_type = read_type_die (die, cu);
78134374 15390 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15391 {
1f704f76 15392 int nparams = this_type->num_fields ();
c906108c 15393
f792889a 15394 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15395 of the method itself (TYPE_CODE_METHOD). */
15396 smash_to_method_type (fnp->type, type,
f792889a 15397 TYPE_TARGET_TYPE (this_type),
80fc5e77 15398 this_type->fields (),
1f704f76 15399 this_type->num_fields (),
a409645d 15400 this_type->has_varargs ());
c906108c
SS
15401
15402 /* Handle static member functions.
dda83cd7
SM
15403 Dwarf2 has no clean way to discern C++ static and non-static
15404 member functions. G++ helps GDB by marking the first
15405 parameter for non-static member functions (which is the this
15406 pointer) as artificial. We obtain this information from
15407 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15408 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15409 fnp->voffset = VOFFSET_STATIC;
15410 }
15411 else
b98664d3 15412 complaint (_("member function type missing for '%s'"),
3da10d80 15413 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15414
15415 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15416 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15417 fnp->fcontext = die_containing_type (die, cu);
c906108c 15418
3e43a32a
MS
15419 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15420 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15421
15422 /* Get accessibility. */
bf23a268 15423 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15424 switch (accessibility)
c906108c 15425 {
60d5a603
JK
15426 case DW_ACCESS_private:
15427 fnp->is_private = 1;
15428 break;
15429 case DW_ACCESS_protected:
15430 fnp->is_protected = 1;
15431 break;
c906108c
SS
15432 }
15433
b02dede2 15434 /* Check for artificial methods. */
e142c38c 15435 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15436 if (attr && attr->as_boolean ())
b02dede2
DJ
15437 fnp->is_artificial = 1;
15438
e35000a7
TBA
15439 /* Check for defaulted methods. */
15440 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15441 if (attr != nullptr)
15442 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15443
15444 /* Check for deleted methods. */
15445 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15446 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15447 fnp->is_deleted = 1;
15448
7d27a96d
TT
15449 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15450
0d564a31 15451 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15452 function. For older versions of GCC, this is an offset in the
15453 appropriate virtual table, as specified by DW_AT_containing_type.
15454 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15455 to the object address. */
15456
e142c38c 15457 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15458 if (attr != nullptr)
8e19ed76 15459 {
9d2246fc 15460 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15461 {
9d2246fc
TT
15462 struct dwarf_block *block = attr->as_block ();
15463
15464 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15465 {
15466 /* Old-style GCC. */
9d2246fc 15467 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15468 }
9d2246fc
TT
15469 else if (block->data[0] == DW_OP_deref
15470 || (block->size > 1
15471 && block->data[0] == DW_OP_deref_size
15472 && block->data[1] == cu->header.addr_size))
aec5aa8b 15473 {
9d2246fc 15474 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15475 if ((fnp->voffset % cu->header.addr_size) != 0)
15476 dwarf2_complex_location_expr_complaint ();
15477 else
15478 fnp->voffset /= cu->header.addr_size;
15479 fnp->voffset += 2;
15480 }
15481 else
15482 dwarf2_complex_location_expr_complaint ();
15483
15484 if (!fnp->fcontext)
7e993ebf
KS
15485 {
15486 /* If there is no `this' field and no DW_AT_containing_type,
15487 we cannot actually find a base class context for the
15488 vtable! */
1f704f76 15489 if (this_type->num_fields () == 0
7e993ebf
KS
15490 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15491 {
b98664d3 15492 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15493 "function \"%s\" (offset %s)"),
15494 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15495 }
15496 else
15497 {
15498 fnp->fcontext
940da03e 15499 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15500 }
15501 }
aec5aa8b 15502 }
cd6c91b4 15503 else if (attr->form_is_section_offset ())
dda83cd7 15504 {
4d3c2250 15505 dwarf2_complex_location_expr_complaint ();
dda83cd7 15506 }
8e19ed76 15507 else
dda83cd7 15508 {
4d3c2250
KB
15509 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15510 fieldname);
dda83cd7 15511 }
0d564a31 15512 }
d48cc9dd
DJ
15513 else
15514 {
15515 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15516 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15517 {
15518 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15519 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15520 "but the vtable offset is not specified"),
9d8780f0 15521 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15522 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15523 TYPE_CPLUS_DYNAMIC (type) = 1;
15524 }
15525 }
c906108c
SS
15526}
15527
15528/* Create the vector of member function fields, and attach it to the type. */
15529
15530static void
fba45db2 15531dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15532 struct dwarf2_cu *cu)
c906108c 15533{
b4ba55a1 15534 if (cu->language == language_ada)
a73c6dcd 15535 error (_("unexpected member functions in Ada type"));
b4ba55a1 15536
c906108c
SS
15537 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15538 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15539 TYPE_ALLOC (type,
15540 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15541
be2daae6 15542 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15543 {
be2daae6 15544 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15545 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15546
be2daae6
TT
15547 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15548 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15549 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15550 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15551
15552 for (int k = 0; k < nf.fnfields.size (); ++k)
15553 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15554 }
15555
be2daae6 15556 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15557}
15558
1168df01
JB
15559/* Returns non-zero if NAME is the name of a vtable member in CU's
15560 language, zero otherwise. */
15561static int
15562is_vtable_name (const char *name, struct dwarf2_cu *cu)
15563{
15564 static const char vptr[] = "_vptr";
15565
9c37b5ae
TT
15566 /* Look for the C++ form of the vtable. */
15567 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15568 return 1;
15569
15570 return 0;
15571}
15572
c0dd20ea 15573/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15574 functions, with the ABI-specified layout. If TYPE describes
15575 such a structure, smash it into a member function type.
61049d3b
DJ
15576
15577 GCC shouldn't do this; it should just output pointer to member DIEs.
15578 This is GCC PR debug/28767. */
c0dd20ea 15579
0b92b5bb
TT
15580static void
15581quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15582{
09e2d7c7 15583 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15584
15585 /* Check for a structure with no name and two children. */
1f704f76 15586 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15587 return;
c0dd20ea
DJ
15588
15589 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15590 if (TYPE_FIELD_NAME (type, 0) == NULL
15591 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15592 || TYPE_FIELD_NAME (type, 1) == NULL
15593 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15594 return;
c0dd20ea
DJ
15595
15596 /* Find the type of the method. */
940da03e 15597 pfn_type = type->field (0).type ();
c0dd20ea 15598 if (pfn_type == NULL
78134374
SM
15599 || pfn_type->code () != TYPE_CODE_PTR
15600 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15601 return;
c0dd20ea
DJ
15602
15603 /* Look for the "this" argument. */
15604 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15605 if (pfn_type->num_fields () == 0
940da03e
SM
15606 /* || pfn_type->field (0).type () == NULL */
15607 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15608 return;
c0dd20ea 15609
940da03e 15610 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15611 new_type = alloc_type (objfile);
09e2d7c7 15612 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15613 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15614 pfn_type->has_varargs ());
0b92b5bb 15615 smash_to_methodptr_type (type, new_type);
c0dd20ea 15616}
1168df01 15617
e26624c6
TT
15618/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15619 requires rewriting, then copy it and return the updated copy.
15620 Otherwise return nullptr. */
15621
15622static struct type *
15623rewrite_array_type (struct type *type)
15624{
15625 if (type->code () != TYPE_CODE_ARRAY)
15626 return nullptr;
15627
15628 struct type *index_type = type->index_type ();
15629 range_bounds *current_bounds = index_type->bounds ();
15630
15631 /* Handle multi-dimensional arrays. */
15632 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15633 if (new_target == nullptr)
15634 {
15635 /* Maybe we don't need to rewrite this array. */
15636 if (current_bounds->low.kind () == PROP_CONST
15637 && current_bounds->high.kind () == PROP_CONST)
15638 return nullptr;
15639 }
15640
15641 /* Either the target type was rewritten, or the bounds have to be
15642 updated. Either way we want to copy the type and update
15643 everything. */
15644 struct type *copy = copy_type (type);
15645 int nfields = copy->num_fields ();
15646 field *new_fields
15647 = ((struct field *) TYPE_ZALLOC (copy,
15648 nfields * sizeof (struct field)));
15649 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15650 copy->set_fields (new_fields);
15651 if (new_target != nullptr)
15652 TYPE_TARGET_TYPE (copy) = new_target;
15653
15654 struct type *index_copy = copy_type (index_type);
15655 range_bounds *bounds
15656 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15657 sizeof (range_bounds));
15658 *bounds = *current_bounds;
15659 bounds->low.set_const_val (1);
15660 bounds->high.set_const_val (0);
15661 index_copy->set_bounds (bounds);
15662 copy->set_index_type (index_copy);
15663
15664 return copy;
15665}
15666
57567375
TT
15667/* While some versions of GCC will generate complicated DWARF for an
15668 array (see quirk_ada_thick_pointer), more recent versions were
15669 modified to emit an explicit thick pointer structure. However, in
15670 this case, the array still has DWARF expressions for its ranges,
15671 and these must be ignored. */
15672
15673static void
15674quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15675 struct type *type)
15676{
15677 gdb_assert (cu->language == language_ada);
15678
15679 /* Check for a structure with two children. */
15680 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15681 return;
15682
15683 /* Check for P_ARRAY and P_BOUNDS members. */
15684 if (TYPE_FIELD_NAME (type, 0) == NULL
15685 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15686 || TYPE_FIELD_NAME (type, 1) == NULL
15687 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15688 return;
15689
15690 /* Make sure we're looking at a pointer to an array. */
15691 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15692 return;
57567375 15693
e26624c6
TT
15694 /* The Ada code already knows how to handle these types, so all that
15695 we need to do is turn the bounds into static bounds. However, we
15696 don't want to rewrite existing array or index types in-place,
15697 because those may be referenced in other contexts where this
15698 rewriting is undesirable. */
15699 struct type *new_ary_type
15700 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15701 if (new_ary_type != nullptr)
15702 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15703}
15704
2b4424c3
TT
15705/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15706 appropriate error checking and issuing complaints if there is a
15707 problem. */
15708
15709static ULONGEST
15710get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15711{
15712 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15713
15714 if (attr == nullptr)
15715 return 0;
15716
cd6c91b4 15717 if (!attr->form_is_constant ())
2b4424c3 15718 {
b98664d3 15719 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15720 " - DIE at %s [in module %s]"),
15721 sect_offset_str (die->sect_off),
5e22e966 15722 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15723 return 0;
15724 }
15725
529908cb
TT
15726 LONGEST val = attr->constant_value (0);
15727 if (val < 0)
2b4424c3 15728 {
529908cb
TT
15729 complaint (_("DW_AT_alignment value must not be negative"
15730 " - DIE at %s [in module %s]"),
15731 sect_offset_str (die->sect_off),
15732 objfile_name (cu->per_objfile->objfile));
15733 return 0;
2b4424c3 15734 }
529908cb 15735 ULONGEST align = val;
2b4424c3
TT
15736
15737 if (align == 0)
15738 {
b98664d3 15739 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15740 " - DIE at %s [in module %s]"),
15741 sect_offset_str (die->sect_off),
5e22e966 15742 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15743 return 0;
15744 }
15745 if ((align & (align - 1)) != 0)
15746 {
b98664d3 15747 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15748 " - DIE at %s [in module %s]"),
15749 sect_offset_str (die->sect_off),
5e22e966 15750 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15751 return 0;
15752 }
15753
15754 return align;
15755}
15756
15757/* If the DIE has a DW_AT_alignment attribute, use its value to set
15758 the alignment for TYPE. */
15759
15760static void
15761maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15762 struct type *type)
15763{
15764 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15765 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15766 " - DIE at %s [in module %s]"),
15767 sect_offset_str (die->sect_off),
5e22e966 15768 objfile_name (cu->per_objfile->objfile));
2b4424c3 15769}
685b1105 15770
e35000a7
TBA
15771/* Check if the given VALUE is a valid enum dwarf_calling_convention
15772 constant for a type, according to DWARF5 spec, Table 5.5. */
15773
15774static bool
15775is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15776{
15777 switch (value)
15778 {
15779 case DW_CC_normal:
15780 case DW_CC_pass_by_reference:
15781 case DW_CC_pass_by_value:
15782 return true;
15783
15784 default:
15785 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15786 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15787 return false;
15788 }
15789}
15790
d0922fcf
TBA
15791/* Check if the given VALUE is a valid enum dwarf_calling_convention
15792 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15793 also according to GNU-specific values (see include/dwarf2.h). */
15794
15795static bool
15796is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15797{
15798 switch (value)
15799 {
15800 case DW_CC_normal:
15801 case DW_CC_program:
15802 case DW_CC_nocall:
15803 return true;
15804
15805 case DW_CC_GNU_renesas_sh:
15806 case DW_CC_GNU_borland_fastcall_i386:
15807 case DW_CC_GDB_IBM_OpenCL:
15808 return true;
15809
15810 default:
15811 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15812 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15813 return false;
15814 }
15815}
15816
c906108c 15817/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15818 (definition) to create a type for the structure or union. Fill in
15819 the type's name and general properties; the members will not be
83655187
DE
15820 processed until process_structure_scope. A symbol table entry for
15821 the type will also not be done until process_structure_scope (assuming
15822 the type has a name).
c906108c 15823
c767944b
DJ
15824 NOTE: we need to call these functions regardless of whether or not the
15825 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15826 structure or union. This gets the type entered into our set of
83655187 15827 user defined types. */
c906108c 15828
f792889a 15829static struct type *
134d01f1 15830read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15831{
5e22e966 15832 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15833 struct type *type;
15834 struct attribute *attr;
15d034d0 15835 const char *name;
c906108c 15836
348e048f
DE
15837 /* If the definition of this type lives in .debug_types, read that type.
15838 Don't follow DW_AT_specification though, that will take us back up
15839 the chain and we want to go down. */
052c8bb8 15840 attr = die->attr (DW_AT_signature);
435d3d88 15841 if (attr != nullptr)
348e048f 15842 {
ac9ec31b 15843 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15844
ac9ec31b 15845 /* The type's CU may not be the same as CU.
02142a6c 15846 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15847 return set_die_type (die, type, cu);
15848 }
15849
c0dd20ea 15850 type = alloc_type (objfile);
c906108c 15851 INIT_CPLUS_SPECIFIC (type);
93311388 15852
39cbfefa
DJ
15853 name = dwarf2_name (die, cu);
15854 if (name != NULL)
c906108c 15855 {
987504bb 15856 if (cu->language == language_cplus
c44af4eb
TT
15857 || cu->language == language_d
15858 || cu->language == language_rust)
63d06c5c 15859 {
15d034d0 15860 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15861
15862 /* dwarf2_full_name might have already finished building the DIE's
15863 type. If so, there is no need to continue. */
15864 if (get_die_type (die, cu) != NULL)
15865 return get_die_type (die, cu);
15866
d0e39ea2 15867 type->set_name (full_name);
63d06c5c
DC
15868 }
15869 else
15870 {
d8151005
DJ
15871 /* The name is already allocated along with this objfile, so
15872 we don't need to duplicate it for the type. */
d0e39ea2 15873 type->set_name (name);
63d06c5c 15874 }
c906108c
SS
15875 }
15876
15877 if (die->tag == DW_TAG_structure_type)
15878 {
67607e24 15879 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15880 }
15881 else if (die->tag == DW_TAG_union_type)
15882 {
67607e24 15883 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15884 }
15885 else
15886 {
67607e24 15887 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15888 }
15889
0cc2414c 15890 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
aa70e35c 15891 type->set_is_declared_class (true);
0cc2414c 15892
e35000a7
TBA
15893 /* Store the calling convention in the type if it's available in
15894 the die. Otherwise the calling convention remains set to
15895 the default value DW_CC_normal. */
15896 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15897 if (attr != nullptr
529908cb 15898 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
15899 {
15900 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15901 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 15902 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
15903 }
15904
e142c38c 15905 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15906 if (attr != nullptr)
c906108c 15907 {
cd6c91b4 15908 if (attr->form_is_constant ())
dda83cd7 15909 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
15910 else
15911 {
f8e89861 15912 struct dynamic_prop prop;
293e7e51 15913 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15914 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 15915 TYPE_LENGTH (type) = 0;
155bfbd3 15916 }
c906108c
SS
15917 }
15918 else
15919 {
15920 TYPE_LENGTH (type) = 0;
15921 }
15922
2b4424c3
TT
15923 maybe_set_alignment (cu, die, type);
15924
5230b05a 15925 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15926 {
5230b05a
WT
15927 /* ICC<14 does not output the required DW_AT_declaration on
15928 incomplete types, but gives them a size of zero. */
b4b73759 15929 type->set_is_stub (true);
685b1105
JK
15930 }
15931 else
9baccff6 15932 type->set_stub_is_supported (true);
685b1105 15933
dc718098 15934 if (die_is_declaration (die, cu))
b4b73759 15935 type->set_is_stub (true);
a6c727b2
DJ
15936 else if (attr == NULL && die->child == NULL
15937 && producer_is_realview (cu->producer))
15938 /* RealView does not output the required DW_AT_declaration
15939 on incomplete types. */
b4b73759 15940 type->set_is_stub (true);
dc718098 15941
c906108c
SS
15942 /* We need to add the type field to the die immediately so we don't
15943 infinitely recurse when dealing with pointers to the structure
0963b4bd 15944 type within the structure itself. */
1c379e20 15945 set_die_type (die, type, cu);
c906108c 15946
7e314c57
JK
15947 /* set_die_type should be already done. */
15948 set_descriptive_type (type, die, cu);
15949
c767944b
DJ
15950 return type;
15951}
15952
9c6a1327
TT
15953static void handle_struct_member_die
15954 (struct die_info *child_die,
15955 struct type *type,
15956 struct field_info *fi,
15957 std::vector<struct symbol *> *template_args,
15958 struct dwarf2_cu *cu);
15959
15960/* A helper for handle_struct_member_die that handles
15961 DW_TAG_variant_part. */
15962
15963static void
15964handle_variant_part (struct die_info *die, struct type *type,
15965 struct field_info *fi,
15966 std::vector<struct symbol *> *template_args,
15967 struct dwarf2_cu *cu)
15968{
15969 variant_part_builder *new_part;
15970 if (fi->current_variant_part == nullptr)
15971 {
15972 fi->variant_parts.emplace_back ();
15973 new_part = &fi->variant_parts.back ();
15974 }
15975 else if (!fi->current_variant_part->processing_variant)
15976 {
15977 complaint (_("nested DW_TAG_variant_part seen "
15978 "- DIE at %s [in module %s]"),
15979 sect_offset_str (die->sect_off),
5e22e966 15980 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15981 return;
15982 }
15983 else
15984 {
15985 variant_field &current = fi->current_variant_part->variants.back ();
15986 current.variant_parts.emplace_back ();
15987 new_part = &current.variant_parts.back ();
15988 }
15989
15990 /* When we recurse, we want callees to add to this new variant
15991 part. */
15992 scoped_restore save_current_variant_part
15993 = make_scoped_restore (&fi->current_variant_part, new_part);
15994
15995 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15996 if (discr == NULL)
15997 {
15998 /* It's a univariant form, an extension we support. */
15999 }
16000 else if (discr->form_is_ref ())
16001 {
16002 struct dwarf2_cu *target_cu = cu;
16003 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16004
16005 new_part->discriminant_offset = target_die->sect_off;
16006 }
16007 else
16008 {
16009 complaint (_("DW_AT_discr does not have DIE reference form"
16010 " - DIE at %s [in module %s]"),
16011 sect_offset_str (die->sect_off),
5e22e966 16012 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16013 }
16014
16015 for (die_info *child_die = die->child;
16016 child_die != NULL;
16017 child_die = child_die->sibling)
16018 handle_struct_member_die (child_die, type, fi, template_args, cu);
16019}
16020
16021/* A helper for handle_struct_member_die that handles
16022 DW_TAG_variant. */
16023
16024static void
16025handle_variant (struct die_info *die, struct type *type,
16026 struct field_info *fi,
16027 std::vector<struct symbol *> *template_args,
16028 struct dwarf2_cu *cu)
16029{
16030 if (fi->current_variant_part == nullptr)
16031 {
16032 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16033 "- DIE at %s [in module %s]"),
16034 sect_offset_str (die->sect_off),
5e22e966 16035 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16036 return;
16037 }
16038 if (fi->current_variant_part->processing_variant)
16039 {
16040 complaint (_("nested DW_TAG_variant seen "
16041 "- DIE at %s [in module %s]"),
16042 sect_offset_str (die->sect_off),
5e22e966 16043 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16044 return;
16045 }
16046
16047 scoped_restore save_processing_variant
16048 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16049 true);
16050
16051 fi->current_variant_part->variants.emplace_back ();
16052 variant_field &variant = fi->current_variant_part->variants.back ();
16053 variant.first_field = fi->fields.size ();
16054
16055 /* In a variant we want to get the discriminant and also add a
16056 field for our sole member child. */
16057 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 16058 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
16059 {
16060 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16061 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16062 variant.default_branch = true;
16063 else
9d2246fc 16064 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16065 }
16066 else
cae21f8e 16067 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
16068
16069 for (die_info *variant_child = die->child;
16070 variant_child != NULL;
16071 variant_child = variant_child->sibling)
16072 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16073
16074 variant.last_field = fi->fields.size ();
16075}
16076
2ddeaf8a
TT
16077/* A helper for process_structure_scope that handles a single member
16078 DIE. */
16079
16080static void
16081handle_struct_member_die (struct die_info *child_die, struct type *type,
16082 struct field_info *fi,
16083 std::vector<struct symbol *> *template_args,
16084 struct dwarf2_cu *cu)
16085{
16086 if (child_die->tag == DW_TAG_member
9c6a1327 16087 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16088 {
16089 /* NOTE: carlton/2002-11-05: A C++ static data member
16090 should be a DW_TAG_member that is a declaration, but
16091 all versions of G++ as of this writing (so through at
16092 least 3.2.1) incorrectly generate DW_TAG_variable
16093 tags for them instead. */
16094 dwarf2_add_field (fi, child_die, cu);
16095 }
16096 else if (child_die->tag == DW_TAG_subprogram)
16097 {
16098 /* Rust doesn't have member functions in the C++ sense.
16099 However, it does emit ordinary functions as children
16100 of a struct DIE. */
16101 if (cu->language == language_rust)
16102 read_func_scope (child_die, cu);
16103 else
16104 {
16105 /* C++ member function. */
16106 dwarf2_add_member_fn (fi, child_die, type, cu);
16107 }
16108 }
16109 else if (child_die->tag == DW_TAG_inheritance)
16110 {
16111 /* C++ base class field. */
16112 dwarf2_add_field (fi, child_die, cu);
16113 }
16114 else if (type_can_define_types (child_die))
16115 dwarf2_add_type_defn (fi, child_die, cu);
16116 else if (child_die->tag == DW_TAG_template_type_param
16117 || child_die->tag == DW_TAG_template_value_param)
16118 {
16119 struct symbol *arg = new_symbol (child_die, NULL, cu);
16120
16121 if (arg != NULL)
16122 template_args->push_back (arg);
16123 }
9c6a1327
TT
16124 else if (child_die->tag == DW_TAG_variant_part)
16125 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16126 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16127 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16128}
16129
c767944b
DJ
16130/* Finish creating a structure or union type, including filling in
16131 its members and creating a symbol for it. */
16132
16133static void
16134process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16135{
5e22e966 16136 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16137 struct die_info *child_die;
c767944b
DJ
16138 struct type *type;
16139
16140 type = get_die_type (die, cu);
16141 if (type == NULL)
16142 type = read_structure_type (die, cu);
16143
3e1d3d8c 16144 bool has_template_parameters = false;
e142c38c 16145 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16146 {
16147 struct field_info fi;
2f4732b0 16148 std::vector<struct symbol *> template_args;
c906108c 16149
639d11d3 16150 child_die = die->child;
c906108c
SS
16151
16152 while (child_die && child_die->tag)
16153 {
2ddeaf8a 16154 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16155 child_die = child_die->sibling;
c906108c
SS
16156 }
16157
34eaf542 16158 /* Attach template arguments to type. */
2f4732b0 16159 if (!template_args.empty ())
34eaf542 16160 {
3e1d3d8c 16161 has_template_parameters = true;
34eaf542 16162 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16163 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16164 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16165 = XOBNEWVEC (&objfile->objfile_obstack,
16166 struct symbol *,
16167 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16168 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16169 template_args.data (),
34eaf542
TT
16170 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16171 * sizeof (struct symbol *)));
34eaf542
TT
16172 }
16173
c906108c 16174 /* Attach fields and member functions to the type. */
317f7127 16175 if (fi.nfields () > 0)
e7c27a73 16176 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16177 if (!fi.fnfieldlists.empty ())
c906108c 16178 {
e7c27a73 16179 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16180
c5aa993b 16181 /* Get the type which refers to the base class (possibly this
c906108c 16182 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16183 class from the DW_AT_containing_type attribute. This use of
16184 DW_AT_containing_type is a GNU extension. */
c906108c 16185
e142c38c 16186 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16187 {
e7c27a73 16188 struct type *t = die_containing_type (die, cu);
c906108c 16189
ae6ae975 16190 set_type_vptr_basetype (type, t);
c906108c
SS
16191 if (type == t)
16192 {
c906108c
SS
16193 int i;
16194
16195 /* Our own class provides vtbl ptr. */
1f704f76 16196 for (i = t->num_fields () - 1;
c906108c
SS
16197 i >= TYPE_N_BASECLASSES (t);
16198 --i)
16199 {
0d5cff50 16200 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16201
dda83cd7 16202 if (is_vtable_name (fieldname, cu))
c906108c 16203 {
ae6ae975 16204 set_type_vptr_fieldno (type, i);
c906108c
SS
16205 break;
16206 }
16207 }
16208
16209 /* Complain if virtual function table field not found. */
16210 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16211 complaint (_("virtual function table pointer "
3e43a32a 16212 "not found when defining class '%s'"),
7d93a1e0 16213 type->name () ? type->name () : "");
c906108c
SS
16214 }
16215 else
16216 {
ae6ae975 16217 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16218 }
16219 }
f6235d4c 16220 else if (cu->producer
61012eef 16221 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16222 {
16223 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16224 of the containing type, but the vtable pointer is
16225 always named __vfp. */
f6235d4c
EZ
16226
16227 int i;
16228
1f704f76 16229 for (i = type->num_fields () - 1;
f6235d4c
EZ
16230 i >= TYPE_N_BASECLASSES (type);
16231 --i)
16232 {
16233 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16234 {
ae6ae975
DE
16235 set_type_vptr_fieldno (type, i);
16236 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16237 break;
16238 }
16239 }
16240 }
c906108c 16241 }
98751a41
JK
16242
16243 /* Copy fi.typedef_field_list linked list elements content into the
16244 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16245 if (!fi.typedef_field_list.empty ())
98751a41 16246 {
be2daae6 16247 int count = fi.typedef_field_list.size ();
98751a41 16248
a0d7a4ff 16249 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16250 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16251 = ((struct decl_field *)
be2daae6
TT
16252 TYPE_ALLOC (type,
16253 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16254 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16255
be2daae6
TT
16256 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16257 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16258 }
c767944b 16259
883fd55a
KS
16260 /* Copy fi.nested_types_list linked list elements content into the
16261 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16262 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16263 {
be2daae6 16264 int count = fi.nested_types_list.size ();
883fd55a
KS
16265
16266 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16267 TYPE_NESTED_TYPES_ARRAY (type)
16268 = ((struct decl_field *)
be2daae6
TT
16269 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16270 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16271
be2daae6
TT
16272 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16273 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16274 }
c906108c 16275 }
63d06c5c 16276
bb5ed363 16277 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16278 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16279 cu->rust_unions.push_back (type);
57567375
TT
16280 else if (cu->language == language_ada)
16281 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16282
90aeadfc
DC
16283 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16284 snapshots) has been known to create a die giving a declaration
16285 for a class that has, as a child, a die giving a definition for a
16286 nested class. So we have to process our children even if the
16287 current die is a declaration. Normally, of course, a declaration
16288 won't have any children at all. */
134d01f1 16289
ca040673
DE
16290 child_die = die->child;
16291
90aeadfc
DC
16292 while (child_die != NULL && child_die->tag)
16293 {
16294 if (child_die->tag == DW_TAG_member
16295 || child_die->tag == DW_TAG_variable
34eaf542
TT
16296 || child_die->tag == DW_TAG_inheritance
16297 || child_die->tag == DW_TAG_template_value_param
16298 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16299 {
90aeadfc 16300 /* Do nothing. */
134d01f1 16301 }
90aeadfc
DC
16302 else
16303 process_die (child_die, cu);
134d01f1 16304
436c571c 16305 child_die = child_die->sibling;
134d01f1
DJ
16306 }
16307
fa4028e9
JB
16308 /* Do not consider external references. According to the DWARF standard,
16309 these DIEs are identified by the fact that they have no byte_size
16310 attribute, and a declaration attribute. */
16311 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16312 || !die_is_declaration (die, cu)
16313 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16314 {
16315 struct symbol *sym = new_symbol (die, type, cu);
16316
16317 if (has_template_parameters)
16318 {
a776957c
TT
16319 struct symtab *symtab;
16320 if (sym != nullptr)
16321 symtab = symbol_symtab (sym);
16322 else if (cu->line_header != nullptr)
16323 {
16324 /* Any related symtab will do. */
16325 symtab
7ba99d21 16326 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16327 }
16328 else
16329 {
16330 symtab = nullptr;
16331 complaint (_("could not find suitable "
16332 "symtab for template parameter"
16333 " - DIE at %s [in module %s]"),
16334 sect_offset_str (die->sect_off),
16335 objfile_name (objfile));
16336 }
16337
16338 if (symtab != nullptr)
16339 {
16340 /* Make sure that the symtab is set on the new symbols.
16341 Even though they don't appear in this symtab directly,
16342 other parts of gdb assume that symbols do, and this is
16343 reasonably true. */
16344 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16345 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16346 }
3e1d3d8c
TT
16347 }
16348 }
134d01f1
DJ
16349}
16350
ed6acedd
TT
16351/* Assuming DIE is an enumeration type, and TYPE is its associated
16352 type, update TYPE using some information only available in DIE's
16353 children. In particular, the fields are computed. */
55426c9d
JB
16354
16355static void
16356update_enumeration_type_from_children (struct die_info *die,
16357 struct type *type,
16358 struct dwarf2_cu *cu)
16359{
60f7655a 16360 struct die_info *child_die;
55426c9d
JB
16361 int unsigned_enum = 1;
16362 int flag_enum = 1;
55426c9d 16363
8268c778 16364 auto_obstack obstack;
ed6acedd 16365 std::vector<struct field> fields;
55426c9d 16366
60f7655a
DE
16367 for (child_die = die->child;
16368 child_die != NULL && child_die->tag;
436c571c 16369 child_die = child_die->sibling)
55426c9d
JB
16370 {
16371 struct attribute *attr;
16372 LONGEST value;
16373 const gdb_byte *bytes;
16374 struct dwarf2_locexpr_baton *baton;
16375 const char *name;
60f7655a 16376
55426c9d
JB
16377 if (child_die->tag != DW_TAG_enumerator)
16378 continue;
16379
16380 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16381 if (attr == NULL)
16382 continue;
16383
16384 name = dwarf2_name (child_die, cu);
16385 if (name == NULL)
16386 name = "<anonymous enumerator>";
16387
16388 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16389 &value, &bytes, &baton);
16390 if (value < 0)
16391 {
16392 unsigned_enum = 0;
16393 flag_enum = 0;
16394 }
55426c9d 16395 else
edd45eb0
SM
16396 {
16397 if (count_one_bits_ll (value) >= 2)
16398 flag_enum = 0;
edd45eb0 16399 }
55426c9d 16400
ed6acedd
TT
16401 fields.emplace_back ();
16402 struct field &field = fields.back ();
16403 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16404 SET_FIELD_ENUMVAL (field, value);
16405 }
16406
16407 if (!fields.empty ())
16408 {
5e33d5f4 16409 type->set_num_fields (fields.size ());
3cabb6b0
SM
16410 type->set_fields
16411 ((struct field *)
16412 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16413 memcpy (type->fields (), fields.data (),
ed6acedd 16414 sizeof (struct field) * fields.size ());
55426c9d
JB
16415 }
16416
16417 if (unsigned_enum)
653223d3
SM
16418 type->set_is_unsigned (true);
16419
55426c9d 16420 if (flag_enum)
9902b327 16421 type->set_is_flag_enum (true);
55426c9d
JB
16422}
16423
134d01f1
DJ
16424/* Given a DW_AT_enumeration_type die, set its type. We do not
16425 complete the type's fields yet, or create any symbols. */
c906108c 16426
f792889a 16427static struct type *
134d01f1 16428read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16429{
5e22e966 16430 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16431 struct type *type;
c906108c 16432 struct attribute *attr;
0114d602 16433 const char *name;
134d01f1 16434
348e048f
DE
16435 /* If the definition of this type lives in .debug_types, read that type.
16436 Don't follow DW_AT_specification though, that will take us back up
16437 the chain and we want to go down. */
052c8bb8 16438 attr = die->attr (DW_AT_signature);
435d3d88 16439 if (attr != nullptr)
348e048f 16440 {
ac9ec31b 16441 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16442
ac9ec31b 16443 /* The type's CU may not be the same as CU.
02142a6c 16444 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16445 return set_die_type (die, type, cu);
16446 }
16447
c906108c
SS
16448 type = alloc_type (objfile);
16449
67607e24 16450 type->set_code (TYPE_CODE_ENUM);
94af9270 16451 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16452 if (name != NULL)
d0e39ea2 16453 type->set_name (name);
c906108c 16454
0626fc76
TT
16455 attr = dwarf2_attr (die, DW_AT_type, cu);
16456 if (attr != NULL)
16457 {
16458 struct type *underlying_type = die_type (die, cu);
16459
16460 TYPE_TARGET_TYPE (type) = underlying_type;
16461 }
16462
e142c38c 16463 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16464 if (attr != nullptr)
c906108c 16465 {
529908cb 16466 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16467 }
16468 else
16469 {
16470 TYPE_LENGTH (type) = 0;
16471 }
16472
2b4424c3
TT
16473 maybe_set_alignment (cu, die, type);
16474
137033e9
JB
16475 /* The enumeration DIE can be incomplete. In Ada, any type can be
16476 declared as private in the package spec, and then defined only
16477 inside the package body. Such types are known as Taft Amendment
16478 Types. When another package uses such a type, an incomplete DIE
16479 may be generated by the compiler. */
02eb380e 16480 if (die_is_declaration (die, cu))
b4b73759 16481 type->set_is_stub (true);
02eb380e 16482
0626fc76
TT
16483 /* If this type has an underlying type that is not a stub, then we
16484 may use its attributes. We always use the "unsigned" attribute
16485 in this situation, because ordinarily we guess whether the type
16486 is unsigned -- but the guess can be wrong and the underlying type
16487 can tell us the reality. However, we defer to a local size
16488 attribute if one exists, because this lets the compiler override
16489 the underlying type if needed. */
e46d3488 16490 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16491 {
9e7c9a03
HD
16492 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16493 underlying_type = check_typedef (underlying_type);
653223d3
SM
16494
16495 type->set_is_unsigned (underlying_type->is_unsigned ());
16496
0626fc76 16497 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16498 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16499
2b4424c3 16500 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16501 && TYPE_RAW_ALIGN (underlying_type) != 0)
16502 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16503 }
16504
aa70e35c 16505 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
3d567982 16506
ed6acedd
TT
16507 set_die_type (die, type, cu);
16508
16509 /* Finish the creation of this type by using the enum's children.
16510 Note that, as usual, this must come after set_die_type to avoid
16511 infinite recursion when trying to compute the names of the
16512 enumerators. */
16513 update_enumeration_type_from_children (die, type, cu);
16514
16515 return type;
134d01f1
DJ
16516}
16517
16518/* Given a pointer to a die which begins an enumeration, process all
16519 the dies that define the members of the enumeration, and create the
16520 symbol for the enumeration type.
16521
16522 NOTE: We reverse the order of the element list. */
16523
16524static void
16525process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16526{
f792889a 16527 struct type *this_type;
134d01f1 16528
f792889a
DJ
16529 this_type = get_die_type (die, cu);
16530 if (this_type == NULL)
16531 this_type = read_enumeration_type (die, cu);
9dc481d3 16532
639d11d3 16533 if (die->child != NULL)
c906108c 16534 {
9dc481d3 16535 struct die_info *child_die;
15d034d0 16536 const char *name;
9dc481d3 16537
639d11d3 16538 child_die = die->child;
c906108c
SS
16539 while (child_die && child_die->tag)
16540 {
16541 if (child_die->tag != DW_TAG_enumerator)
16542 {
e7c27a73 16543 process_die (child_die, cu);
c906108c
SS
16544 }
16545 else
16546 {
39cbfefa
DJ
16547 name = dwarf2_name (child_die, cu);
16548 if (name)
ed6acedd 16549 new_symbol (child_die, this_type, cu);
c906108c
SS
16550 }
16551
436c571c 16552 child_die = child_die->sibling;
c906108c 16553 }
c906108c 16554 }
134d01f1 16555
6c83ed52
TT
16556 /* If we are reading an enum from a .debug_types unit, and the enum
16557 is a declaration, and the enum is not the signatured type in the
16558 unit, then we do not want to add a symbol for it. Adding a
16559 symbol would in some cases obscure the true definition of the
16560 enum, giving users an incomplete type when the definition is
16561 actually available. Note that we do not want to do this for all
16562 enums which are just declarations, because C++0x allows forward
16563 enum declarations. */
3019eac3 16564 if (cu->per_cu->is_debug_types
6c83ed52
TT
16565 && die_is_declaration (die, cu))
16566 {
52dc124a 16567 struct signatured_type *sig_type;
6c83ed52 16568
c0f78cd4 16569 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16570 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16571 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16572 return;
16573 }
16574
f792889a 16575 new_symbol (die, this_type, cu);
c906108c
SS
16576}
16577
57567375
TT
16578/* Helper function for quirk_ada_thick_pointer that examines a bounds
16579 expression for an index type and finds the corresponding field
16580 offset in the hidden "P_BOUNDS" structure. Returns true on success
16581 and updates *FIELD, false if it fails to recognize an
16582 expression. */
16583
16584static bool
16585recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16586 int *bounds_offset, struct field *field,
16587 struct dwarf2_cu *cu)
16588{
16589 struct attribute *attr = dwarf2_attr (die, name, cu);
16590 if (attr == nullptr || !attr->form_is_block ())
16591 return false;
16592
16593 const struct dwarf_block *block = attr->as_block ();
16594 const gdb_byte *start = block->data;
16595 const gdb_byte *end = block->data + block->size;
16596
16597 /* The expression to recognize generally looks like:
16598
16599 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16600 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16601
16602 However, the second "plus_uconst" may be missing:
16603
16604 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16605 DW_OP_deref_size: 4)
16606
16607 This happens when the field is at the start of the structure.
16608
16609 Also, the final deref may not be sized:
16610
16611 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16612 DW_OP_deref)
16613
16614 This happens when the size of the index type happens to be the
16615 same as the architecture's word size. This can occur with or
16616 without the second plus_uconst. */
16617
16618 if (end - start < 2)
16619 return false;
16620 if (*start++ != DW_OP_push_object_address)
16621 return false;
16622 if (*start++ != DW_OP_plus_uconst)
16623 return false;
16624
16625 uint64_t this_bound_off;
16626 start = gdb_read_uleb128 (start, end, &this_bound_off);
16627 if (start == nullptr || (int) this_bound_off != this_bound_off)
16628 return false;
16629 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16630 is consistent among all bounds. */
16631 if (*bounds_offset == -1)
16632 *bounds_offset = this_bound_off;
16633 else if (*bounds_offset != this_bound_off)
16634 return false;
16635
16636 if (start == end || *start++ != DW_OP_deref)
16637 return false;
16638
16639 int offset = 0;
16640 if (start ==end)
16641 return false;
16642 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16643 {
16644 /* This means an offset of 0. */
16645 }
16646 else if (*start++ != DW_OP_plus_uconst)
16647 return false;
16648 else
16649 {
16650 /* The size is the parameter to DW_OP_plus_uconst. */
16651 uint64_t val;
16652 start = gdb_read_uleb128 (start, end, &val);
16653 if (start == nullptr)
16654 return false;
16655 if ((int) val != val)
16656 return false;
16657 offset = val;
16658 }
16659
16660 if (start == end)
16661 return false;
16662
16663 uint64_t size;
16664 if (*start == DW_OP_deref_size)
16665 {
16666 start = gdb_read_uleb128 (start + 1, end, &size);
16667 if (start == nullptr)
16668 return false;
16669 }
16670 else if (*start == DW_OP_deref)
16671 {
16672 size = cu->header.addr_size;
16673 ++start;
16674 }
16675 else
16676 return false;
16677
16678 SET_FIELD_BITPOS (*field, 8 * offset);
16679 if (size != TYPE_LENGTH (field->type ()))
16680 FIELD_BITSIZE (*field) = 8 * size;
16681
16682 return true;
16683}
16684
16685/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16686 some kinds of Ada arrays:
16687
16688 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16689 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16690 <11e0> DW_AT_data_location: 2 byte block: 97 6
16691 (DW_OP_push_object_address; DW_OP_deref)
16692 <11e3> DW_AT_type : <0x1173>
16693 <11e7> DW_AT_sibling : <0x1201>
16694 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16695 <11ec> DW_AT_type : <0x1206>
16696 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16697 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16698 DW_OP_deref_size: 4)
16699 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16700 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16701 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16702
16703 This actually represents a "thick pointer", which is a structure
16704 with two elements: one that is a pointer to the array data, and one
16705 that is a pointer to another structure; this second structure holds
16706 the array bounds.
16707
16708 This returns a new type on success, or nullptr if this didn't
16709 recognize the type. */
16710
16711static struct type *
16712quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16713 struct type *type)
16714{
16715 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16716 /* So far we've only seen this with block form. */
16717 if (attr == nullptr || !attr->form_is_block ())
16718 return nullptr;
16719
16720 /* Note that this will fail if the structure layout is changed by
16721 the compiler. However, we have no good way to recognize some
16722 other layout, because we don't know what expression the compiler
16723 might choose to emit should this happen. */
16724 struct dwarf_block *blk = attr->as_block ();
16725 if (blk->size != 2
16726 || blk->data[0] != DW_OP_push_object_address
16727 || blk->data[1] != DW_OP_deref)
16728 return nullptr;
16729
16730 int bounds_offset = -1;
16731 int max_align = -1;
16732 std::vector<struct field> range_fields;
16733 for (struct die_info *child_die = die->child;
16734 child_die;
16735 child_die = child_die->sibling)
16736 {
16737 if (child_die->tag == DW_TAG_subrange_type)
16738 {
16739 struct type *underlying = read_subrange_index_type (child_die, cu);
16740
16741 int this_align = type_align (underlying);
16742 if (this_align > max_align)
16743 max_align = this_align;
16744
16745 range_fields.emplace_back ();
16746 range_fields.emplace_back ();
16747
16748 struct field &lower = range_fields[range_fields.size () - 2];
16749 struct field &upper = range_fields[range_fields.size () - 1];
16750
16751 lower.set_type (underlying);
16752 FIELD_ARTIFICIAL (lower) = 1;
16753
16754 upper.set_type (underlying);
16755 FIELD_ARTIFICIAL (upper) = 1;
16756
16757 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16758 &bounds_offset, &lower, cu)
16759 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16760 &bounds_offset, &upper, cu))
16761 return nullptr;
16762 }
16763 }
16764
16765 /* This shouldn't really happen, but double-check that we found
16766 where the bounds are stored. */
16767 if (bounds_offset == -1)
16768 return nullptr;
16769
16770 struct objfile *objfile = cu->per_objfile->objfile;
16771 for (int i = 0; i < range_fields.size (); i += 2)
16772 {
16773 char name[20];
16774
16775 /* Set the name of each field in the bounds. */
16776 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16777 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16778 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16779 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16780 }
16781
16782 struct type *bounds = alloc_type (objfile);
16783 bounds->set_code (TYPE_CODE_STRUCT);
16784
16785 bounds->set_num_fields (range_fields.size ());
16786 bounds->set_fields
16787 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16788 * sizeof (struct field))));
16789 memcpy (bounds->fields (), range_fields.data (),
16790 bounds->num_fields () * sizeof (struct field));
16791
16792 int last_fieldno = range_fields.size () - 1;
16793 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16794 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16795 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16796
16797 /* Rewrite the existing array type in place. Specifically, we
16798 remove any dynamic properties we might have read, and we replace
16799 the index types. */
16800 struct type *iter = type;
16801 for (int i = 0; i < range_fields.size (); i += 2)
16802 {
16803 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16804 iter->main_type->dyn_prop_list = nullptr;
16805 iter->set_index_type
16806 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16807 iter = TYPE_TARGET_TYPE (iter);
16808 }
16809
16810 struct type *result = alloc_type (objfile);
16811 result->set_code (TYPE_CODE_STRUCT);
16812
16813 result->set_num_fields (2);
16814 result->set_fields
16815 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16816 * sizeof (struct field))));
16817
16818 /* The names are chosen to coincide with what the compiler does with
16819 -fgnat-encodings=all, which the Ada code in gdb already
16820 understands. */
16821 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16822 result->field (0).set_type (lookup_pointer_type (type));
16823
16824 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16825 result->field (1).set_type (lookup_pointer_type (bounds));
16826 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16827
16828 result->set_name (type->name ());
16829 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16830 + TYPE_LENGTH (result->field (1).type ()));
16831
16832 return result;
16833}
16834
c906108c
SS
16835/* Extract all information from a DW_TAG_array_type DIE and put it in
16836 the DIE's type field. For now, this only handles one dimensional
16837 arrays. */
16838
f792889a 16839static struct type *
e7c27a73 16840read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16841{
5e22e966 16842 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16843 struct die_info *child_die;
7e314c57 16844 struct type *type;
c906108c 16845 struct type *element_type, *range_type, *index_type;
c906108c 16846 struct attribute *attr;
15d034d0 16847 const char *name;
a405673c 16848 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16849 unsigned int bit_stride = 0;
c906108c 16850
e7c27a73 16851 element_type = die_type (die, cu);
c906108c 16852
7e314c57
JK
16853 /* The die_type call above may have already set the type for this DIE. */
16854 type = get_die_type (die, cu);
16855 if (type)
16856 return type;
16857
dc53a7ad
JB
16858 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16859 if (attr != NULL)
a405673c
JB
16860 {
16861 int stride_ok;
293e7e51 16862 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16863
16864 byte_stride_prop
16865 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16866 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16867 prop_type);
a405673c
JB
16868 if (!stride_ok)
16869 {
b98664d3 16870 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16871 " - DIE at %s [in module %s]"),
16872 sect_offset_str (die->sect_off),
5e22e966 16873 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16874 /* Ignore this attribute. We will likely not be able to print
16875 arrays of this type correctly, but there is little we can do
16876 to help if we cannot read the attribute's value. */
16877 byte_stride_prop = NULL;
16878 }
16879 }
dc53a7ad
JB
16880
16881 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16882 if (attr != NULL)
529908cb 16883 bit_stride = attr->constant_value (0);
dc53a7ad 16884
c906108c
SS
16885 /* Irix 6.2 native cc creates array types without children for
16886 arrays with unspecified length. */
639d11d3 16887 if (die->child == NULL)
c906108c 16888 {
46bf5051 16889 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16890 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16891 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16892 byte_stride_prop, bit_stride);
f792889a 16893 return set_die_type (die, type, cu);
c906108c
SS
16894 }
16895
791afaa2 16896 std::vector<struct type *> range_types;
639d11d3 16897 child_die = die->child;
c906108c
SS
16898 while (child_die && child_die->tag)
16899 {
16900 if (child_die->tag == DW_TAG_subrange_type)
16901 {
f792889a 16902 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16903
dda83cd7
SM
16904 if (child_type != NULL)
16905 {
0963b4bd 16906 /* The range type was succesfully read. Save it for the
dda83cd7 16907 array type creation. */
791afaa2 16908 range_types.push_back (child_type);
dda83cd7 16909 }
c906108c 16910 }
436c571c 16911 child_die = child_die->sibling;
c906108c
SS
16912 }
16913
cf2b2075
TV
16914 if (range_types.empty ())
16915 {
16916 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16917 sect_offset_str (die->sect_off),
16918 objfile_name (cu->per_objfile->objfile));
16919 return NULL;
16920 }
16921
c906108c
SS
16922 /* Dwarf2 dimensions are output from left to right, create the
16923 necessary array types in backwards order. */
7ca2d3a3 16924
c906108c 16925 type = element_type;
7ca2d3a3
DL
16926
16927 if (read_array_order (die, cu) == DW_ORD_col_major)
16928 {
16929 int i = 0;
9a619af0 16930
791afaa2 16931 while (i < range_types.size ())
10f6a3ad
TT
16932 {
16933 type = create_array_type_with_stride (NULL, type, range_types[i++],
16934 byte_stride_prop, bit_stride);
16935 bit_stride = 0;
16936 byte_stride_prop = nullptr;
16937 }
7ca2d3a3
DL
16938 }
16939 else
16940 {
791afaa2 16941 size_t ndim = range_types.size ();
7ca2d3a3 16942 while (ndim-- > 0)
10f6a3ad
TT
16943 {
16944 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16945 byte_stride_prop, bit_stride);
16946 bit_stride = 0;
16947 byte_stride_prop = nullptr;
16948 }
7ca2d3a3 16949 }
c906108c 16950
cf2b2075
TV
16951 gdb_assert (type != element_type);
16952
f5f8a009
EZ
16953 /* Understand Dwarf2 support for vector types (like they occur on
16954 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16955 array type. This is not part of the Dwarf2/3 standard yet, but a
16956 custom vendor extension. The main difference between a regular
16957 array and the vector variant is that vectors are passed by value
16958 to functions. */
e142c38c 16959 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16960 if (attr != nullptr)
ea37ba09 16961 make_vector_type (type);
f5f8a009 16962
dbc98a8b
KW
16963 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16964 implementation may choose to implement triple vectors using this
16965 attribute. */
16966 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 16967 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 16968 {
529908cb
TT
16969 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16970 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 16971 else
b98664d3 16972 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16973 "than the total size of elements"));
dbc98a8b
KW
16974 }
16975
39cbfefa
DJ
16976 name = dwarf2_name (die, cu);
16977 if (name)
d0e39ea2 16978 type->set_name (name);
6e70227d 16979
2b4424c3
TT
16980 maybe_set_alignment (cu, die, type);
16981
57567375
TT
16982 struct type *replacement_type = nullptr;
16983 if (cu->language == language_ada)
16984 {
16985 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16986 if (replacement_type != nullptr)
16987 type = replacement_type;
16988 }
16989
0963b4bd 16990 /* Install the type in the die. */
57567375 16991 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
16992
16993 /* set_die_type should be already done. */
b4ba55a1
JB
16994 set_descriptive_type (type, die, cu);
16995
7e314c57 16996 return type;
c906108c
SS
16997}
16998
7ca2d3a3 16999static enum dwarf_array_dim_ordering
6e70227d 17000read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
17001{
17002 struct attribute *attr;
17003
17004 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17005
435d3d88 17006 if (attr != nullptr)
1bc397c5
TT
17007 {
17008 LONGEST val = attr->constant_value (-1);
17009 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17010 return (enum dwarf_array_dim_ordering) val;
17011 }
7ca2d3a3 17012
0963b4bd
MS
17013 /* GNU F77 is a special case, as at 08/2004 array type info is the
17014 opposite order to the dwarf2 specification, but data is still
17015 laid out as per normal fortran.
7ca2d3a3 17016
0963b4bd
MS
17017 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17018 version checking. */
7ca2d3a3 17019
905e0470
PM
17020 if (cu->language == language_fortran
17021 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
17022 {
17023 return DW_ORD_row_major;
17024 }
17025
3a3440fb 17026 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
17027 {
17028 case array_column_major:
17029 return DW_ORD_col_major;
17030 case array_row_major:
17031 default:
17032 return DW_ORD_row_major;
17033 };
17034}
17035
72019c9c 17036/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 17037 the DIE's type field. */
72019c9c 17038
f792889a 17039static struct type *
72019c9c
GM
17040read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17041{
7e314c57
JK
17042 struct type *domain_type, *set_type;
17043 struct attribute *attr;
f792889a 17044
7e314c57
JK
17045 domain_type = die_type (die, cu);
17046
17047 /* The die_type call above may have already set the type for this DIE. */
17048 set_type = get_die_type (die, cu);
17049 if (set_type)
17050 return set_type;
17051
17052 set_type = create_set_type (NULL, domain_type);
17053
17054 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
17055 if (attr != nullptr && attr->form_is_unsigned ())
17056 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 17057
2b4424c3
TT
17058 maybe_set_alignment (cu, die, set_type);
17059
f792889a 17060 return set_die_type (die, set_type, cu);
72019c9c 17061}
7ca2d3a3 17062
0971de02
TT
17063/* A helper for read_common_block that creates a locexpr baton.
17064 SYM is the symbol which we are marking as computed.
17065 COMMON_DIE is the DIE for the common block.
17066 COMMON_LOC is the location expression attribute for the common
17067 block itself.
17068 MEMBER_LOC is the location expression attribute for the particular
17069 member of the common block that we are processing.
17070 CU is the CU from which the above come. */
17071
17072static void
17073mark_common_block_symbol_computed (struct symbol *sym,
17074 struct die_info *common_die,
17075 struct attribute *common_loc,
17076 struct attribute *member_loc,
17077 struct dwarf2_cu *cu)
17078{
5e22e966 17079 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 17080 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
17081 struct dwarf2_locexpr_baton *baton;
17082 gdb_byte *ptr;
17083 unsigned int cu_off;
08feed99 17084 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
17085 LONGEST offset = 0;
17086
17087 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
17088 gdb_assert (common_loc->form_is_block ());
17089 gdb_assert (member_loc->form_is_block ()
cd6c91b4 17090 || member_loc->form_is_constant ());
0971de02 17091
8d749320 17092 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 17093 baton->per_objfile = per_objfile;
0971de02
TT
17094 baton->per_cu = cu->per_cu;
17095 gdb_assert (baton->per_cu);
17096
17097 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17098
cd6c91b4 17099 if (member_loc->form_is_constant ())
0971de02 17100 {
0826b30a 17101 offset = member_loc->constant_value (0);
0971de02
TT
17102 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17103 }
17104 else
9d2246fc 17105 baton->size += member_loc->as_block ()->size;
0971de02 17106
224c3ddb 17107 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17108 baton->data = ptr;
17109
17110 *ptr++ = DW_OP_call4;
9c541725 17111 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17112 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17113 ptr += 4;
17114
cd6c91b4 17115 if (member_loc->form_is_constant ())
0971de02
TT
17116 {
17117 *ptr++ = DW_OP_addr;
17118 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17119 ptr += cu->header.addr_size;
17120 }
17121 else
17122 {
17123 /* We have to copy the data here, because DW_OP_call4 will only
17124 use a DW_AT_location attribute. */
9d2246fc
TT
17125 struct dwarf_block *block = member_loc->as_block ();
17126 memcpy (ptr, block->data, block->size);
17127 ptr += block->size;
0971de02
TT
17128 }
17129
17130 *ptr++ = DW_OP_plus;
17131 gdb_assert (ptr - baton->data == baton->size);
17132
0971de02 17133 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17134 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17135}
17136
4357ac6c
TT
17137/* Create appropriate locally-scoped variables for all the
17138 DW_TAG_common_block entries. Also create a struct common_block
17139 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 17140 is used to separate the common blocks name namespace from regular
4357ac6c 17141 variable names. */
c906108c
SS
17142
17143static void
e7c27a73 17144read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17145{
0971de02
TT
17146 struct attribute *attr;
17147
17148 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17149 if (attr != nullptr)
0971de02
TT
17150 {
17151 /* Support the .debug_loc offsets. */
4fc6c0d5 17152 if (attr->form_is_block ())
dda83cd7 17153 {
0971de02 17154 /* Ok. */
dda83cd7 17155 }
cd6c91b4 17156 else if (attr->form_is_section_offset ())
dda83cd7 17157 {
0971de02
TT
17158 dwarf2_complex_location_expr_complaint ();
17159 attr = NULL;
dda83cd7 17160 }
0971de02 17161 else
dda83cd7 17162 {
0971de02
TT
17163 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17164 "common block member");
17165 attr = NULL;
dda83cd7 17166 }
0971de02
TT
17167 }
17168
639d11d3 17169 if (die->child != NULL)
c906108c 17170 {
5e22e966 17171 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17172 struct die_info *child_die;
17173 size_t n_entries = 0, size;
17174 struct common_block *common_block;
17175 struct symbol *sym;
74ac6d43 17176
4357ac6c
TT
17177 for (child_die = die->child;
17178 child_die && child_die->tag;
436c571c 17179 child_die = child_die->sibling)
4357ac6c
TT
17180 ++n_entries;
17181
17182 size = (sizeof (struct common_block)
17183 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17184 common_block
17185 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17186 size);
4357ac6c
TT
17187 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17188 common_block->n_entries = 0;
17189
17190 for (child_die = die->child;
17191 child_die && child_die->tag;
436c571c 17192 child_die = child_die->sibling)
4357ac6c
TT
17193 {
17194 /* Create the symbol in the DW_TAG_common_block block in the current
17195 symbol scope. */
e7c27a73 17196 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17197 if (sym != NULL)
17198 {
17199 struct attribute *member_loc;
17200
17201 common_block->contents[common_block->n_entries++] = sym;
17202
17203 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17204 cu);
17205 if (member_loc)
17206 {
17207 /* GDB has handled this for a long time, but it is
17208 not specified by DWARF. It seems to have been
17209 emitted by gfortran at least as recently as:
17210 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17211 complaint (_("Variable in common block has "
0971de02 17212 "DW_AT_data_member_location "
9d8780f0
SM
17213 "- DIE at %s [in module %s]"),
17214 sect_offset_str (child_die->sect_off),
518817b3 17215 objfile_name (objfile));
0971de02 17216
cd6c91b4 17217 if (member_loc->form_is_section_offset ())
0971de02 17218 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17219 else if (member_loc->form_is_constant ()
4fc6c0d5 17220 || member_loc->form_is_block ())
0971de02 17221 {
435d3d88 17222 if (attr != nullptr)
0971de02
TT
17223 mark_common_block_symbol_computed (sym, die, attr,
17224 member_loc, cu);
17225 }
17226 else
17227 dwarf2_complex_location_expr_complaint ();
17228 }
17229 }
c906108c 17230 }
4357ac6c
TT
17231
17232 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17233 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17234 }
17235}
17236
0114d602 17237/* Create a type for a C++ namespace. */
d9fa45fe 17238
0114d602
DJ
17239static struct type *
17240read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17241{
5e22e966 17242 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17243 const char *previous_prefix, *name;
9219021c 17244 int is_anonymous;
0114d602
DJ
17245 struct type *type;
17246
17247 /* For extensions, reuse the type of the original namespace. */
17248 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17249 {
17250 struct die_info *ext_die;
17251 struct dwarf2_cu *ext_cu = cu;
9a619af0 17252
0114d602
DJ
17253 ext_die = dwarf2_extension (die, &ext_cu);
17254 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17255
17256 /* EXT_CU may not be the same as CU.
02142a6c 17257 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17258 return set_die_type (die, type, cu);
17259 }
9219021c 17260
e142c38c 17261 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17262
17263 /* Now build the name of the current namespace. */
17264
0114d602
DJ
17265 previous_prefix = determine_prefix (die, cu);
17266 if (previous_prefix[0] != '\0')
17267 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17268 previous_prefix, name, 0, cu);
0114d602
DJ
17269
17270 /* Create the type. */
19f392bc 17271 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17272
60531b24 17273 return set_die_type (die, type, cu);
0114d602
DJ
17274}
17275
22cee43f 17276/* Read a namespace scope. */
0114d602
DJ
17277
17278static void
17279read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17280{
5e22e966 17281 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17282 int is_anonymous;
9219021c 17283
5c4e30ca
DC
17284 /* Add a symbol associated to this if we haven't seen the namespace
17285 before. Also, add a using directive if it's an anonymous
17286 namespace. */
9219021c 17287
f2f0e013 17288 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17289 {
17290 struct type *type;
17291
0114d602 17292 type = read_type_die (die, cu);
e7c27a73 17293 new_symbol (die, type, cu);
5c4e30ca 17294
e8e80198 17295 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17296 if (is_anonymous)
0114d602
DJ
17297 {
17298 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17299
eb1e02fd 17300 std::vector<const char *> excludes;
804d2729 17301 add_using_directive (using_directives (cu),
7d93a1e0 17302 previous_prefix, type->name (), NULL,
eb1e02fd 17303 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17304 }
5c4e30ca 17305 }
9219021c 17306
639d11d3 17307 if (die->child != NULL)
d9fa45fe 17308 {
639d11d3 17309 struct die_info *child_die = die->child;
6e70227d 17310
d9fa45fe
DC
17311 while (child_die && child_die->tag)
17312 {
e7c27a73 17313 process_die (child_die, cu);
436c571c 17314 child_die = child_die->sibling;
d9fa45fe
DC
17315 }
17316 }
38d518c9
EZ
17317}
17318
f55ee35c
JK
17319/* Read a Fortran module as type. This DIE can be only a declaration used for
17320 imported module. Still we need that type as local Fortran "use ... only"
17321 declaration imports depend on the created type in determine_prefix. */
17322
17323static struct type *
17324read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17325{
5e22e966 17326 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17327 const char *module_name;
f55ee35c
JK
17328 struct type *type;
17329
17330 module_name = dwarf2_name (die, cu);
19f392bc 17331 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17332
f55ee35c
JK
17333 return set_die_type (die, type, cu);
17334}
17335
5d7cb8df
JK
17336/* Read a Fortran module. */
17337
17338static void
17339read_module (struct die_info *die, struct dwarf2_cu *cu)
17340{
17341 struct die_info *child_die = die->child;
530e8392
KB
17342 struct type *type;
17343
17344 type = read_type_die (die, cu);
17345 new_symbol (die, type, cu);
5d7cb8df 17346
5d7cb8df
JK
17347 while (child_die && child_die->tag)
17348 {
17349 process_die (child_die, cu);
436c571c 17350 child_die = child_die->sibling;
5d7cb8df
JK
17351 }
17352}
17353
38d518c9
EZ
17354/* Return the name of the namespace represented by DIE. Set
17355 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17356 namespace. */
17357
17358static const char *
e142c38c 17359namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17360{
17361 struct die_info *current_die;
17362 const char *name = NULL;
17363
17364 /* Loop through the extensions until we find a name. */
17365
17366 for (current_die = die;
17367 current_die != NULL;
f2f0e013 17368 current_die = dwarf2_extension (die, &cu))
38d518c9 17369 {
96553a0c
DE
17370 /* We don't use dwarf2_name here so that we can detect the absence
17371 of a name -> anonymous namespace. */
7d45c7c3 17372 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17373
38d518c9
EZ
17374 if (name != NULL)
17375 break;
17376 }
17377
17378 /* Is it an anonymous namespace? */
17379
17380 *is_anonymous = (name == NULL);
17381 if (*is_anonymous)
2b1dbab0 17382 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17383
17384 return name;
d9fa45fe
DC
17385}
17386
c906108c
SS
17387/* Extract all information from a DW_TAG_pointer_type DIE and add to
17388 the user defined type vector. */
17389
f792889a 17390static struct type *
e7c27a73 17391read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17392{
5e22e966 17393 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17394 struct comp_unit_head *cu_header = &cu->header;
c906108c 17395 struct type *type;
8b2dbe47
KB
17396 struct attribute *attr_byte_size;
17397 struct attribute *attr_address_class;
17398 int byte_size, addr_class;
7e314c57
JK
17399 struct type *target_type;
17400
17401 target_type = die_type (die, cu);
c906108c 17402
7e314c57
JK
17403 /* The die_type call above may have already set the type for this DIE. */
17404 type = get_die_type (die, cu);
17405 if (type)
17406 return type;
17407
17408 type = lookup_pointer_type (target_type);
8b2dbe47 17409
e142c38c 17410 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17411 if (attr_byte_size)
529908cb 17412 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17413 else
8b2dbe47
KB
17414 byte_size = cu_header->addr_size;
17415
e142c38c 17416 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17417 if (attr_address_class)
529908cb 17418 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17419 else
17420 addr_class = DW_ADDR_none;
17421
2b4424c3
TT
17422 ULONGEST alignment = get_alignment (cu, die);
17423
17424 /* If the pointer size, alignment, or address class is different
17425 than the default, create a type variant marked as such and set
17426 the length accordingly. */
17427 if (TYPE_LENGTH (type) != byte_size
17428 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17429 && alignment != TYPE_RAW_ALIGN (type))
17430 || addr_class != DW_ADDR_none)
c906108c 17431 {
5e2b427d 17432 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17433 {
314ad88d
PA
17434 type_instance_flags type_flags
17435 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17436 addr_class);
876cecd0
TT
17437 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17438 == 0);
8b2dbe47
KB
17439 type = make_type_with_address_space (type, type_flags);
17440 }
17441 else if (TYPE_LENGTH (type) != byte_size)
17442 {
b98664d3 17443 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17444 }
2b4424c3
TT
17445 else if (TYPE_RAW_ALIGN (type) != alignment)
17446 {
b98664d3 17447 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17448 " - DIE at %s [in module %s]"),
17449 sect_offset_str (die->sect_off),
5e22e966 17450 objfile_name (cu->per_objfile->objfile));
2b4424c3 17451 }
6e70227d 17452 else
9a619af0
MS
17453 {
17454 /* Should we also complain about unhandled address classes? */
17455 }
c906108c 17456 }
8b2dbe47
KB
17457
17458 TYPE_LENGTH (type) = byte_size;
2b4424c3 17459 set_type_align (type, alignment);
f792889a 17460 return set_die_type (die, type, cu);
c906108c
SS
17461}
17462
17463/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17464 the user defined type vector. */
17465
f792889a 17466static struct type *
e7c27a73 17467read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17468{
17469 struct type *type;
17470 struct type *to_type;
17471 struct type *domain;
17472
e7c27a73
DJ
17473 to_type = die_type (die, cu);
17474 domain = die_containing_type (die, cu);
0d5de010 17475
7e314c57
JK
17476 /* The calls above may have already set the type for this DIE. */
17477 type = get_die_type (die, cu);
17478 if (type)
17479 return type;
17480
78134374 17481 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17482 type = lookup_methodptr_type (to_type);
78134374 17483 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17484 {
5e22e966 17485 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17486
17487 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17488 to_type->fields (), to_type->num_fields (),
a409645d 17489 to_type->has_varargs ());
7078baeb
TT
17490 type = lookup_methodptr_type (new_type);
17491 }
0d5de010
DJ
17492 else
17493 type = lookup_memberptr_type (to_type, domain);
c906108c 17494
f792889a 17495 return set_die_type (die, type, cu);
c906108c
SS
17496}
17497
4297a3f0 17498/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17499 the user defined type vector. */
17500
f792889a 17501static struct type *
4297a3f0 17502read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17503 enum type_code refcode)
c906108c 17504{
e7c27a73 17505 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17506 struct type *type, *target_type;
c906108c
SS
17507 struct attribute *attr;
17508
4297a3f0
AV
17509 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17510
7e314c57
JK
17511 target_type = die_type (die, cu);
17512
17513 /* The die_type call above may have already set the type for this DIE. */
17514 type = get_die_type (die, cu);
17515 if (type)
17516 return type;
17517
4297a3f0 17518 type = lookup_reference_type (target_type, refcode);
e142c38c 17519 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17520 if (attr != nullptr)
c906108c 17521 {
529908cb 17522 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17523 }
17524 else
17525 {
107d2387 17526 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17527 }
2b4424c3 17528 maybe_set_alignment (cu, die, type);
f792889a 17529 return set_die_type (die, type, cu);
c906108c
SS
17530}
17531
cf363f18
MW
17532/* Add the given cv-qualifiers to the element type of the array. GCC
17533 outputs DWARF type qualifiers that apply to an array, not the
17534 element type. But GDB relies on the array element type to carry
17535 the cv-qualifiers. This mimics section 6.7.3 of the C99
17536 specification. */
17537
17538static struct type *
17539add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17540 struct type *base_type, int cnst, int voltl)
17541{
17542 struct type *el_type, *inner_array;
17543
17544 base_type = copy_type (base_type);
17545 inner_array = base_type;
17546
78134374 17547 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17548 {
17549 TYPE_TARGET_TYPE (inner_array) =
17550 copy_type (TYPE_TARGET_TYPE (inner_array));
17551 inner_array = TYPE_TARGET_TYPE (inner_array);
17552 }
17553
17554 el_type = TYPE_TARGET_TYPE (inner_array);
17555 cnst |= TYPE_CONST (el_type);
17556 voltl |= TYPE_VOLATILE (el_type);
17557 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17558
17559 return set_die_type (die, base_type, cu);
17560}
17561
f792889a 17562static struct type *
e7c27a73 17563read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17564{
f792889a 17565 struct type *base_type, *cv_type;
c906108c 17566
e7c27a73 17567 base_type = die_type (die, cu);
7e314c57
JK
17568
17569 /* The die_type call above may have already set the type for this DIE. */
17570 cv_type = get_die_type (die, cu);
17571 if (cv_type)
17572 return cv_type;
17573
2f608a3a
KW
17574 /* In case the const qualifier is applied to an array type, the element type
17575 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17576 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17577 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17578
f792889a
DJ
17579 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17580 return set_die_type (die, cv_type, cu);
c906108c
SS
17581}
17582
f792889a 17583static struct type *
e7c27a73 17584read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17585{
f792889a 17586 struct type *base_type, *cv_type;
c906108c 17587
e7c27a73 17588 base_type = die_type (die, cu);
7e314c57
JK
17589
17590 /* The die_type call above may have already set the type for this DIE. */
17591 cv_type = get_die_type (die, cu);
17592 if (cv_type)
17593 return cv_type;
17594
cf363f18
MW
17595 /* In case the volatile qualifier is applied to an array type, the
17596 element type is so qualified, not the array type (section 6.7.3
17597 of C99). */
78134374 17598 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17599 return add_array_cv_type (die, cu, base_type, 0, 1);
17600
f792889a
DJ
17601 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17602 return set_die_type (die, cv_type, cu);
c906108c
SS
17603}
17604
06d66ee9
TT
17605/* Handle DW_TAG_restrict_type. */
17606
17607static struct type *
17608read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17609{
17610 struct type *base_type, *cv_type;
17611
17612 base_type = die_type (die, cu);
17613
17614 /* The die_type call above may have already set the type for this DIE. */
17615 cv_type = get_die_type (die, cu);
17616 if (cv_type)
17617 return cv_type;
17618
17619 cv_type = make_restrict_type (base_type);
17620 return set_die_type (die, cv_type, cu);
17621}
17622
a2c2acaf
MW
17623/* Handle DW_TAG_atomic_type. */
17624
17625static struct type *
17626read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17627{
17628 struct type *base_type, *cv_type;
17629
17630 base_type = die_type (die, cu);
17631
17632 /* The die_type call above may have already set the type for this DIE. */
17633 cv_type = get_die_type (die, cu);
17634 if (cv_type)
17635 return cv_type;
17636
17637 cv_type = make_atomic_type (base_type);
17638 return set_die_type (die, cv_type, cu);
17639}
17640
c906108c
SS
17641/* Extract all information from a DW_TAG_string_type DIE and add to
17642 the user defined type vector. It isn't really a user defined type,
17643 but it behaves like one, with other DIE's using an AT_user_def_type
17644 attribute to reference it. */
17645
f792889a 17646static struct type *
e7c27a73 17647read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17648{
5e22e966 17649 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17650 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17651 struct type *type, *range_type, *index_type, *char_type;
17652 struct attribute *attr;
216a7e6b
AB
17653 struct dynamic_prop prop;
17654 bool length_is_constant = true;
17655 LONGEST length;
17656
17657 /* There are a couple of places where bit sizes might be made use of
17658 when parsing a DW_TAG_string_type, however, no producer that we know
17659 of make use of these. Handling bit sizes that are a multiple of the
17660 byte size is easy enough, but what about other bit sizes? Lets deal
17661 with that problem when we have to. Warn about these attributes being
17662 unsupported, then parse the type and ignore them like we always
17663 have. */
17664 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17665 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17666 {
17667 static bool warning_printed = false;
17668 if (!warning_printed)
17669 {
17670 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17671 "currently supported on DW_TAG_string_type."));
17672 warning_printed = true;
17673 }
17674 }
c906108c 17675
e142c38c 17676 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17677 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17678 {
17679 /* The string length describes the location at which the length of
17680 the string can be found. The size of the length field can be
17681 specified with one of the attributes below. */
17682 struct type *prop_type;
17683 struct attribute *len
17684 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17685 if (len == nullptr)
17686 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17687 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17688 {
17689 /* Pass 0 as the default as we know this attribute is constant
17690 and the default value will not be returned. */
0826b30a 17691 LONGEST sz = len->constant_value (0);
293e7e51 17692 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17693 }
17694 else
17695 {
17696 /* If the size is not specified then we assume it is the size of
17697 an address on this target. */
293e7e51 17698 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17699 }
17700
17701 /* Convert the attribute into a dynamic property. */
17702 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17703 length = 1;
17704 else
17705 length_is_constant = false;
17706 }
17707 else if (attr != nullptr)
17708 {
17709 /* This DW_AT_string_length just contains the length with no
17710 indirection. There's no need to create a dynamic property in this
17711 case. Pass 0 for the default value as we know it will not be
17712 returned in this case. */
0826b30a 17713 length = attr->constant_value (0);
216a7e6b
AB
17714 }
17715 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17716 {
216a7e6b 17717 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17718 length = attr->constant_value (1);
c906108c
SS
17719 }
17720 else
17721 {
216a7e6b
AB
17722 /* Use 1 as a fallback length if we have nothing else. */
17723 length = 1;
c906108c 17724 }
6ccb9162 17725
46bf5051 17726 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17727 if (length_is_constant)
17728 range_type = create_static_range_type (NULL, index_type, 1, length);
17729 else
17730 {
17731 struct dynamic_prop low_bound;
17732
8c2e4e06 17733 low_bound.set_const_val (1);
216a7e6b
AB
17734 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17735 }
3b7538c0
UW
17736 char_type = language_string_char_type (cu->language_defn, gdbarch);
17737 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17738
f792889a 17739 return set_die_type (die, type, cu);
c906108c
SS
17740}
17741
4d804846
JB
17742/* Assuming that DIE corresponds to a function, returns nonzero
17743 if the function is prototyped. */
17744
17745static int
17746prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17747{
17748 struct attribute *attr;
17749
17750 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17751 if (attr && attr->as_boolean ())
4d804846
JB
17752 return 1;
17753
17754 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17755 is only meaningful for C, but the concept also extends to other
4d804846
JB
17756 languages that allow unprototyped functions (Eg: Objective C).
17757 For all other languages, assume that functions are always
17758 prototyped. */
17759 if (cu->language != language_c
17760 && cu->language != language_objc
17761 && cu->language != language_opencl)
17762 return 1;
17763
17764 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17765 prototyped and unprototyped functions; default to prototyped,
17766 since that is more common in modern code (and RealView warns
17767 about unprototyped functions). */
17768 if (producer_is_realview (cu->producer))
17769 return 1;
17770
17771 return 0;
17772}
17773
c906108c
SS
17774/* Handle DIES due to C code like:
17775
17776 struct foo
c5aa993b
JM
17777 {
17778 int (*funcp)(int a, long l);
17779 int b;
17780 };
c906108c 17781
0963b4bd 17782 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17783
f792889a 17784static struct type *
e7c27a73 17785read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17786{
5e22e966 17787 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17788 struct type *type; /* Type that this function returns. */
17789 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17790 struct attribute *attr;
17791
e7c27a73 17792 type = die_type (die, cu);
7e314c57
JK
17793
17794 /* The die_type call above may have already set the type for this DIE. */
17795 ftype = get_die_type (die, cu);
17796 if (ftype)
17797 return ftype;
17798
0c8b41f1 17799 ftype = lookup_function_type (type);
c906108c 17800
4d804846 17801 if (prototyped_function_p (die, cu))
27e69b7a 17802 ftype->set_is_prototyped (true);
c906108c 17803
c055b101
CV
17804 /* Store the calling convention in the type if it's available in
17805 the subroutine die. Otherwise set the calling convention to
17806 the default value DW_CC_normal. */
17807 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 17808 if (attr != nullptr
529908cb 17809 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 17810 TYPE_CALLING_CONVENTION (ftype)
529908cb 17811 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
17812 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17813 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17814 else
17815 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17816
743649fd
MW
17817 /* Record whether the function returns normally to its caller or not
17818 if the DWARF producer set that information. */
17819 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 17820 if (attr && attr->as_boolean ())
743649fd
MW
17821 TYPE_NO_RETURN (ftype) = 1;
17822
76c10ea2
GM
17823 /* We need to add the subroutine type to the die immediately so
17824 we don't infinitely recurse when dealing with parameters
0963b4bd 17825 declared as the same subroutine type. */
76c10ea2 17826 set_die_type (die, ftype, cu);
6e70227d 17827
639d11d3 17828 if (die->child != NULL)
c906108c 17829 {
bb5ed363 17830 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17831 struct die_info *child_die;
8072405b 17832 int nparams, iparams;
c906108c
SS
17833
17834 /* Count the number of parameters.
dda83cd7
SM
17835 FIXME: GDB currently ignores vararg functions, but knows about
17836 vararg member functions. */
8072405b 17837 nparams = 0;
639d11d3 17838 child_die = die->child;
c906108c
SS
17839 while (child_die && child_die->tag)
17840 {
17841 if (child_die->tag == DW_TAG_formal_parameter)
17842 nparams++;
17843 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
17844 ftype->set_has_varargs (true);
17845
436c571c 17846 child_die = child_die->sibling;
c906108c
SS
17847 }
17848
17849 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17850 ftype->set_num_fields (nparams);
3cabb6b0
SM
17851 ftype->set_fields
17852 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17853
8072405b
JK
17854 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17855 even if we error out during the parameters reading below. */
17856 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17857 ftype->field (iparams).set_type (void_type);
8072405b
JK
17858
17859 iparams = 0;
639d11d3 17860 child_die = die->child;
c906108c
SS
17861 while (child_die && child_die->tag)
17862 {
17863 if (child_die->tag == DW_TAG_formal_parameter)
17864 {
3ce3b1ba
PA
17865 struct type *arg_type;
17866
17867 /* DWARF version 2 has no clean way to discern C++
17868 static and non-static member functions. G++ helps
17869 GDB by marking the first parameter for non-static
17870 member functions (which is the this pointer) as
17871 artificial. We pass this information to
17872 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17873
17874 DWARF version 3 added DW_AT_object_pointer, which GCC
17875 4.5 does not yet generate. */
e142c38c 17876 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17877 if (attr != nullptr)
c45bc3f8 17878 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 17879 else
9c37b5ae 17880 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17881 arg_type = die_type (child_die, cu);
17882
17883 /* RealView does not mark THIS as const, which the testsuite
17884 expects. GCC marks THIS as const in method definitions,
17885 but not in the class specifications (GCC PR 43053). */
17886 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17887 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17888 {
17889 int is_this = 0;
17890 struct dwarf2_cu *arg_cu = cu;
17891 const char *name = dwarf2_name (child_die, cu);
17892
17893 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17894 if (attr != nullptr)
3ce3b1ba
PA
17895 {
17896 /* If the compiler emits this, use it. */
17897 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17898 is_this = 1;
17899 }
17900 else if (name && strcmp (name, "this") == 0)
17901 /* Function definitions will have the argument names. */
17902 is_this = 1;
17903 else if (name == NULL && iparams == 0)
17904 /* Declarations may not have the names, so like
17905 elsewhere in GDB, assume an artificial first
17906 argument is "this". */
17907 is_this = 1;
17908
17909 if (is_this)
17910 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17911 arg_type, 0);
17912 }
17913
5d14b6e5 17914 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17915 iparams++;
17916 }
436c571c 17917 child_die = child_die->sibling;
c906108c
SS
17918 }
17919 }
17920
76c10ea2 17921 return ftype;
c906108c
SS
17922}
17923
f792889a 17924static struct type *
e7c27a73 17925read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17926{
5e22e966 17927 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17928 const char *name = NULL;
3c8e0968 17929 struct type *this_type, *target_type;
c906108c 17930
94af9270 17931 name = dwarf2_full_name (NULL, die, cu);
19f392bc 17932 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 17933 this_type->set_target_is_stub (true);
f792889a 17934 set_die_type (die, this_type, cu);
3c8e0968
DE
17935 target_type = die_type (die, cu);
17936 if (target_type != this_type)
17937 TYPE_TARGET_TYPE (this_type) = target_type;
17938 else
17939 {
17940 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17941 spec and cause infinite loops in GDB. */
b98664d3 17942 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17943 "- DIE at %s [in module %s]"),
17944 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17945 TYPE_TARGET_TYPE (this_type) = NULL;
17946 }
e4003a34
TV
17947 if (name == NULL)
17948 {
17949 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17950 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17951 Handle these by just returning the target type, rather than
17952 constructing an anonymous typedef type and trying to handle this
17953 elsewhere. */
17954 set_die_type (die, target_type, cu);
17955 return target_type;
17956 }
f792889a 17957 return this_type;
c906108c
SS
17958}
17959
a625a8c9
TT
17960/* Helper for get_dwarf2_rational_constant that computes the value of
17961 a given gmp_mpz given an attribute. */
17962
17963static void
17964get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17965{
17966 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17967 location expression that pushes an implicit value. */
17968 if (attr->form == DW_FORM_exprloc)
17969 {
17970 dwarf_block *blk = attr->as_block ();
17971 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17972 {
17973 uint64_t len;
17974 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17975 blk->data + blk->size,
17976 &len);
17977 if (ptr - blk->data + len <= blk->size)
17978 {
17979 mpz_import (value->val, len,
17980 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17981 1, 0, 0, ptr);
17982 return;
17983 }
17984 }
17985
17986 /* On failure set it to 1. */
17987 *value = gdb_mpz (1);
17988 }
17989 else if (attr->form_is_block ())
17990 {
17991 dwarf_block *blk = attr->as_block ();
17992 mpz_import (value->val, blk->size,
17993 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17994 1, 0, 0, blk->data);
17995 }
17996 else
17997 *value = gdb_mpz (attr->constant_value (1));
17998}
17999
09584414
JB
18000/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18001 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18002
18003 If the numerator and/or numerator attribute is missing,
18004 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18005 untouched. */
18006
18007static void
18008get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 18009 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
18010{
18011 struct attribute *num_attr, *denom_attr;
18012
18013 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18014 if (num_attr == nullptr)
18015 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18016 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18017
18018 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18019 if (denom_attr == nullptr)
18020 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18021 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18022
18023 if (num_attr == nullptr || denom_attr == nullptr)
18024 return;
18025
a625a8c9
TT
18026 get_mpz (cu, numerator, num_attr);
18027 get_mpz (cu, denominator, denom_attr);
09584414
JB
18028}
18029
18030/* Same as get_dwarf2_rational_constant, but extracting an unsigned
18031 rational constant, rather than a signed one.
18032
18033 If the rational constant has a negative value, a complaint
18034 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18035
18036static void
18037get_dwarf2_unsigned_rational_constant (struct die_info *die,
18038 struct dwarf2_cu *cu,
5cde1d82
TT
18039 gdb_mpz *numerator,
18040 gdb_mpz *denominator)
09584414 18041{
5cde1d82
TT
18042 gdb_mpz num (1);
18043 gdb_mpz denom (1);
09584414
JB
18044
18045 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 18046 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 18047 {
5cde1d82
TT
18048 mpz_neg (num.val, num.val);
18049 mpz_neg (denom.val, denom.val);
09584414 18050 }
5cde1d82 18051 else if (mpz_sgn (num.val) == -1)
09584414
JB
18052 {
18053 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18054 " in DIE at %s"),
18055 sect_offset_str (die->sect_off));
18056 return;
18057 }
5cde1d82 18058 else if (mpz_sgn (denom.val) == -1)
09584414
JB
18059 {
18060 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18061 " in DIE at %s"),
18062 sect_offset_str (die->sect_off));
18063 return;
18064 }
18065
5cde1d82
TT
18066 *numerator = std::move (num);
18067 *denominator = std::move (denom);
09584414
JB
18068}
18069
bbcdf9ab
TT
18070/* Assuming that ENCODING is a string whose contents starting at the
18071 K'th character is "_nn" where "nn" is a decimal number, scan that
18072 number and set RESULT to the value. K is updated to point to the
18073 character immediately following the number.
18074
18075 If the string does not conform to the format described above, false
18076 is returned, and K may or may not be changed. */
18077
18078static bool
18079ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18080{
18081 /* The next character should be an underscore ('_') followed
18082 by a digit. */
18083 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18084 return false;
18085
18086 /* Skip the underscore. */
18087 k++;
18088 int start = k;
18089
18090 /* Determine the number of digits for our number. */
18091 while (isdigit (encoding[k]))
18092 k++;
18093 if (k == start)
18094 return false;
18095
18096 std::string copy (&encoding[start], k - start);
18097 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18098 return false;
18099
18100 return true;
18101}
18102
18103/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18104 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18105 DENOM, update OFFSET, and return true on success. Return false on
18106 failure. */
18107
18108static bool
18109ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18110 gdb_mpz *num, gdb_mpz *denom)
18111{
18112 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18113 return false;
18114 return ada_get_gnat_encoded_number (encoding, offset, denom);
18115}
18116
09584414 18117/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
18118 of the corresponding TYPE by setting its type-specific data. CU is
18119 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18120 encodings. It is nullptr if the GNAT encoding should be
18121 ignored. */
09584414
JB
18122
18123static void
bbcdf9ab
TT
18124finish_fixed_point_type (struct type *type, const char *suffix,
18125 struct die_info *die, struct dwarf2_cu *cu)
09584414 18126{
09584414
JB
18127 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18128 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18129
bbcdf9ab
TT
18130 /* If GNAT encodings are preferred, don't examine the
18131 attributes. */
18132 struct attribute *attr = nullptr;
18133 if (suffix == nullptr)
18134 {
18135 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18136 if (attr == nullptr)
18137 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18138 if (attr == nullptr)
18139 attr = dwarf2_attr (die, DW_AT_small, cu);
18140 }
09584414 18141
5cde1d82
TT
18142 /* Numerator and denominator of our fixed-point type's scaling factor.
18143 The default is a scaling factor of 1, which we use as a fallback
18144 when we are not able to decode it (problem with the debugging info,
18145 unsupported forms, bug in GDB, etc...). Using that as the default
18146 allows us to at least print the unscaled value, which might still
18147 be useful to a user. */
18148 gdb_mpz scale_num (1);
18149 gdb_mpz scale_denom (1);
18150
09584414
JB
18151 if (attr == nullptr)
18152 {
bbcdf9ab
TT
18153 int offset = 0;
18154 if (suffix != nullptr
18155 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18156 &scale_denom)
18157 /* The number might be encoded as _nn_dd_nn_dd, where the
18158 second ratio is the 'small value. In this situation, we
18159 want the second value. */
18160 && (suffix[offset] != '_'
18161 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18162 &scale_denom)))
18163 {
18164 /* Found it. */
18165 }
18166 else
18167 {
18168 /* Scaling factor not found. Assume a scaling factor of 1,
18169 and hope for the best. At least the user will be able to
18170 see the encoded value. */
18171 scale_num = 1;
18172 scale_denom = 1;
18173 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18174 sect_offset_str (die->sect_off));
18175 }
09584414
JB
18176 }
18177 else if (attr->name == DW_AT_binary_scale)
18178 {
18179 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18180 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18181
5cde1d82 18182 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
18183 }
18184 else if (attr->name == DW_AT_decimal_scale)
18185 {
18186 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18187 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18188
5cde1d82 18189 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
18190 }
18191 else if (attr->name == DW_AT_small)
18192 {
18193 struct die_info *scale_die;
18194 struct dwarf2_cu *scale_cu = cu;
18195
18196 scale_die = follow_die_ref (die, attr, &scale_cu);
18197 if (scale_die->tag == DW_TAG_constant)
18198 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18199 &scale_num, &scale_denom);
18200 else
18201 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18202 " (DIE at %s)"),
18203 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18204 }
18205 else
18206 {
18207 complaint (_("unsupported scale attribute %s for fixed-point type"
18208 " (DIE at %s)"),
18209 dwarf_attr_name (attr->name),
18210 sect_offset_str (die->sect_off));
18211 }
18212
2a12c336 18213 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
18214 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18215 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
18216 mpq_canonicalize (scaling_factor.val);
18217}
18218
bbcdf9ab
TT
18219/* The gnat-encoding suffix for fixed point. */
18220
18221#define GNAT_FIXED_POINT_SUFFIX "___XF_"
18222
18223/* If NAME encodes an Ada fixed-point type, return a pointer to the
18224 "XF" suffix of the name. The text after this is what encodes the
18225 'small and 'delta information. Otherwise, return nullptr. */
18226
18227static const char *
18228gnat_encoded_fixed_point_type_info (const char *name)
18229{
18230 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18231}
18232
9b790ce7
UW
18233/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18234 (which may be different from NAME) to the architecture back-end to allow
18235 it to guess the correct format if necessary. */
18236
18237static struct type *
18238dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18239 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18240{
08feed99 18241 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18242 const struct floatformat **format;
18243 struct type *type;
18244
18245 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18246 if (format)
103a685e 18247 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18248 else
77b7c781 18249 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18250
18251 return type;
18252}
18253
eb77c9df
AB
18254/* Allocate an integer type of size BITS and name NAME. */
18255
18256static struct type *
18257dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18258 int bits, int unsigned_p, const char *name)
18259{
18260 struct type *type;
18261
18262 /* Versions of Intel's C Compiler generate an integer type called "void"
18263 instead of using DW_TAG_unspecified_type. This has been seen on
18264 at least versions 14, 17, and 18. */
35ee2dc2
AB
18265 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18266 && strcmp (name, "void") == 0)
eb77c9df
AB
18267 type = objfile_type (objfile)->builtin_void;
18268 else
18269 type = init_integer_type (objfile, bits, unsigned_p, name);
18270
18271 return type;
18272}
18273
09584414
JB
18274/* Return true if DIE has a DW_AT_small attribute whose value is
18275 a constant rational, where both the numerator and denominator
18276 are equal to zero.
18277
18278 CU is the DIE's Compilation Unit. */
18279
18280static bool
18281has_zero_over_zero_small_attribute (struct die_info *die,
18282 struct dwarf2_cu *cu)
18283{
18284 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18285 if (attr == nullptr)
18286 return false;
18287
18288 struct dwarf2_cu *scale_cu = cu;
18289 struct die_info *scale_die
18290 = follow_die_ref (die, attr, &scale_cu);
18291
18292 if (scale_die->tag != DW_TAG_constant)
18293 return false;
18294
5cde1d82 18295 gdb_mpz num (1), denom (1);
09584414 18296 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 18297 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
18298}
18299
8bdc1658
AB
18300/* Initialise and return a floating point type of size BITS suitable for
18301 use as a component of a complex number. The NAME_HINT is passed through
18302 when initialising the floating point type and is the name of the complex
18303 type.
18304
18305 As DWARF doesn't currently provide an explicit name for the components
18306 of a complex number, but it can be helpful to have these components
18307 named, we try to select a suitable name based on the size of the
18308 component. */
18309static struct type *
18310dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18311 struct objfile *objfile,
103a685e
TT
18312 int bits, const char *name_hint,
18313 enum bfd_endian byte_order)
8bdc1658 18314{
08feed99 18315 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18316 struct type *tt = nullptr;
18317
35add35e
AB
18318 /* Try to find a suitable floating point builtin type of size BITS.
18319 We're going to use the name of this type as the name for the complex
18320 target type that we are about to create. */
1db455a7 18321 switch (cu->language)
8bdc1658 18322 {
1db455a7
AB
18323 case language_fortran:
18324 switch (bits)
18325 {
18326 case 32:
18327 tt = builtin_f_type (gdbarch)->builtin_real;
18328 break;
18329 case 64:
18330 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18331 break;
18332 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18333 case 128:
18334 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18335 break;
18336 }
8bdc1658 18337 break;
1db455a7
AB
18338 default:
18339 switch (bits)
18340 {
18341 case 32:
18342 tt = builtin_type (gdbarch)->builtin_float;
18343 break;
18344 case 64:
18345 tt = builtin_type (gdbarch)->builtin_double;
18346 break;
18347 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18348 case 128:
18349 tt = builtin_type (gdbarch)->builtin_long_double;
18350 break;
18351 }
8bdc1658
AB
18352 break;
18353 }
18354
35add35e
AB
18355 /* If the type we found doesn't match the size we were looking for, then
18356 pretend we didn't find a type at all, the complex target type we
18357 create will then be nameless. */
a12e5744 18358 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18359 tt = nullptr;
18360
7d93a1e0 18361 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18362 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18363}
18364
c906108c
SS
18365/* Find a representation of a given base type and install
18366 it in the TYPE field of the die. */
18367
f792889a 18368static struct type *
e7c27a73 18369read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18370{
5e22e966 18371 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18372 struct type *type;
18373 struct attribute *attr;
19f392bc 18374 int encoding = 0, bits = 0;
15d034d0 18375 const char *name;
34877895 18376 gdbarch *arch;
c906108c 18377
e142c38c 18378 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18379 if (attr != nullptr && attr->form_is_constant ())
18380 encoding = attr->constant_value (0);
e142c38c 18381 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18382 if (attr != nullptr)
529908cb 18383 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18384 name = dwarf2_name (die, cu);
6ccb9162 18385 if (!name)
34877895 18386 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18387
08feed99 18388 arch = objfile->arch ();
103a685e
TT
18389 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18390
34877895 18391 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18392 if (attr != nullptr && attr->form_is_constant ())
103a685e 18393 {
529908cb 18394 int endianity = attr->constant_value (0);
103a685e
TT
18395
18396 switch (endianity)
18397 {
18398 case DW_END_big:
18399 byte_order = BFD_ENDIAN_BIG;
18400 break;
18401 case DW_END_little:
18402 byte_order = BFD_ENDIAN_LITTLE;
18403 break;
18404 default:
18405 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18406 break;
18407 }
18408 }
6ccb9162 18409
09584414
JB
18410 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18411 && cu->language == language_ada
18412 && has_zero_over_zero_small_attribute (die, cu))
18413 {
18414 /* brobecker/2018-02-24: This is a fixed point type for which
18415 the scaling factor is represented as fraction whose value
18416 does not make sense (zero divided by zero), so we should
18417 normally never see these. However, there is a small category
18418 of fixed point types for which GNAT is unable to provide
18419 the scaling factor via the standard DWARF mechanisms, and
18420 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18421 This is likely what this DIE is about. */
09584414
JB
18422 encoding = (encoding == DW_ATE_signed_fixed
18423 ? DW_ATE_signed
18424 : DW_ATE_unsigned);
18425 }
18426
bbcdf9ab
TT
18427 /* With GNAT encodings, fixed-point information will be encoded in
18428 the type name. Note that this can also occur with the above
18429 zero-over-zero case, which is why this is a separate "if" rather
18430 than an "else if". */
18431 const char *gnat_encoding_suffix = nullptr;
18432 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18433 && cu->language == language_ada
18434 && name != nullptr)
18435 {
18436 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18437 if (gnat_encoding_suffix != nullptr)
18438 {
18439 gdb_assert (startswith (gnat_encoding_suffix,
18440 GNAT_FIXED_POINT_SUFFIX));
18441 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18442 name, gnat_encoding_suffix - name);
18443 /* Use -1 here so that SUFFIX points at the "_" after the
18444 "XF". */
18445 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18446
18447 encoding = (encoding == DW_ATE_signed
18448 ? DW_ATE_signed_fixed
18449 : DW_ATE_unsigned_fixed);
18450 }
18451 }
18452
6ccb9162 18453 switch (encoding)
c906108c 18454 {
6ccb9162
UW
18455 case DW_ATE_address:
18456 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18457 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18458 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18459 break;
18460 case DW_ATE_boolean:
19f392bc 18461 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18462 break;
18463 case DW_ATE_complex_float:
103a685e
TT
18464 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18465 byte_order);
78134374 18466 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18467 {
18468 if (name == nullptr)
18469 {
18470 struct obstack *obstack
5e22e966 18471 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18472 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18473 nullptr);
18474 }
18475 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18476 }
18477 else
18478 type = init_complex_type (name, type);
6ccb9162
UW
18479 break;
18480 case DW_ATE_decimal_float:
19f392bc 18481 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18482 break;
18483 case DW_ATE_float:
103a685e 18484 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18485 break;
18486 case DW_ATE_signed:
eb77c9df 18487 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18488 break;
18489 case DW_ATE_unsigned:
3b2b8fea
TT
18490 if (cu->language == language_fortran
18491 && name
61012eef 18492 && startswith (name, "character("))
19f392bc
UW
18493 type = init_character_type (objfile, bits, 1, name);
18494 else
eb77c9df 18495 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18496 break;
18497 case DW_ATE_signed_char:
6e70227d 18498 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18499 || cu->language == language_pascal
18500 || cu->language == language_fortran)
19f392bc
UW
18501 type = init_character_type (objfile, bits, 0, name);
18502 else
eb77c9df 18503 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18504 break;
18505 case DW_ATE_unsigned_char:
868a0084 18506 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18507 || cu->language == language_pascal
c44af4eb
TT
18508 || cu->language == language_fortran
18509 || cu->language == language_rust)
19f392bc
UW
18510 type = init_character_type (objfile, bits, 1, name);
18511 else
eb77c9df 18512 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18513 break;
75079b2b 18514 case DW_ATE_UTF:
53e710ac 18515 {
53e710ac
PA
18516 if (bits == 16)
18517 type = builtin_type (arch)->builtin_char16;
18518 else if (bits == 32)
18519 type = builtin_type (arch)->builtin_char32;
18520 else
18521 {
b98664d3 18522 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18523 bits);
eb77c9df 18524 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18525 }
18526 return set_die_type (die, type, cu);
18527 }
75079b2b 18528 break;
09584414
JB
18529 case DW_ATE_signed_fixed:
18530 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18531 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18532 break;
18533 case DW_ATE_unsigned_fixed:
18534 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18535 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18536 break;
75079b2b 18537
6ccb9162 18538 default:
b98664d3 18539 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18540 dwarf_type_encoding_name (encoding));
77b7c781 18541 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18542 break;
c906108c 18543 }
6ccb9162 18544
0114d602 18545 if (name && strcmp (name, "char") == 0)
15152a54 18546 type->set_has_no_signedness (true);
0114d602 18547
2b4424c3
TT
18548 maybe_set_alignment (cu, die, type);
18549
db558e34 18550 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18551
20a5fcbd
TT
18552 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18553 {
18554 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18555 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18556 {
529908cb 18557 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18558 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18559 /* Only use the attributes if they make sense together. */
18560 if (attr == nullptr
529908cb
TT
18561 || (attr->as_unsigned () + real_bit_size
18562 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18563 {
18564 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18565 = real_bit_size;
18566 if (attr != nullptr)
18567 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18568 = attr->as_unsigned ();
20a5fcbd
TT
18569 }
18570 }
18571 }
18572
f792889a 18573 return set_die_type (die, type, cu);
c906108c
SS
18574}
18575
80180f79
SA
18576/* Parse dwarf attribute if it's a block, reference or constant and put the
18577 resulting value of the attribute into struct bound_prop.
18578 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18579
18580static int
18581attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18582 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18583 struct type *default_type)
80180f79
SA
18584{
18585 struct dwarf2_property_baton *baton;
5e22e966 18586 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18587 struct objfile *objfile = per_objfile->objfile;
18588 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18589
9a49df9d
AB
18590 gdb_assert (default_type != NULL);
18591
80180f79
SA
18592 if (attr == NULL || prop == NULL)
18593 return 0;
18594
4fc6c0d5 18595 if (attr->form_is_block ())
80180f79 18596 {
8d749320 18597 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18598 baton->property_type = default_type;
80180f79 18599 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18600 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18601
18602 struct dwarf_block *block = attr->as_block ();
18603 baton->locexpr.size = block->size;
18604 baton->locexpr.data = block->data;
216a7e6b
AB
18605 switch (attr->name)
18606 {
18607 case DW_AT_string_length:
18608 baton->locexpr.is_reference = true;
18609 break;
18610 default:
18611 baton->locexpr.is_reference = false;
18612 break;
18613 }
8c2e4e06
SM
18614
18615 prop->set_locexpr (baton);
18616 gdb_assert (prop->baton () != NULL);
80180f79 18617 }
cd6c91b4 18618 else if (attr->form_is_ref ())
80180f79
SA
18619 {
18620 struct dwarf2_cu *target_cu = cu;
18621 struct die_info *target_die;
18622 struct attribute *target_attr;
18623
18624 target_die = follow_die_ref (die, attr, &target_cu);
18625 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18626 if (target_attr == NULL)
18627 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18628 target_cu);
80180f79
SA
18629 if (target_attr == NULL)
18630 return 0;
18631
df25ebbd 18632 switch (target_attr->name)
80180f79 18633 {
df25ebbd 18634 case DW_AT_location:
cd6c91b4 18635 if (target_attr->form_is_section_offset ())
df25ebbd 18636 {
8d749320 18637 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18638 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18639 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18640 prop->set_loclist (baton);
18641 gdb_assert (prop->baton () != NULL);
df25ebbd 18642 }
4fc6c0d5 18643 else if (target_attr->form_is_block ())
df25ebbd 18644 {
8d749320 18645 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18646 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18647 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18648 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18649 struct dwarf_block *block = target_attr->as_block ();
18650 baton->locexpr.size = block->size;
18651 baton->locexpr.data = block->data;
9a49df9d 18652 baton->locexpr.is_reference = true;
8c2e4e06
SM
18653 prop->set_locexpr (baton);
18654 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18655 }
18656 else
18657 {
18658 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18659 "dynamic property");
18660 return 0;
18661 }
18662 break;
18663 case DW_AT_data_member_location:
18664 {
18665 LONGEST offset;
18666
18667 if (!handle_data_member_location (target_die, target_cu,
18668 &offset))
18669 return 0;
18670
8d749320 18671 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18672 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18673 target_cu);
df25ebbd
JB
18674 baton->offset_info.offset = offset;
18675 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18676 prop->set_addr_offset (baton);
df25ebbd
JB
18677 break;
18678 }
80180f79
SA
18679 }
18680 }
cd6c91b4 18681 else if (attr->form_is_constant ())
8c2e4e06 18682 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18683 else
18684 {
18685 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18686 dwarf2_name (die, cu));
18687 return 0;
18688 }
18689
18690 return 1;
18691}
18692
09ba997f 18693/* See read.h. */
9a49df9d 18694
09ba997f 18695struct type *
293e7e51 18696dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18697{
9a49df9d
AB
18698 struct type *int_type;
18699
18700 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18701#define TRY_TYPE(F) \
18702 int_type = (unsigned_p \
18703 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18704 : objfile_type (objfile)->builtin_ ## F); \
18705 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18706 return int_type
18707
18708 TRY_TYPE (char);
18709 TRY_TYPE (short);
18710 TRY_TYPE (int);
18711 TRY_TYPE (long);
18712 TRY_TYPE (long_long);
18713
18714#undef TRY_TYPE
18715
18716 gdb_assert_not_reached ("unable to find suitable integer type");
18717}
18718
09ba997f 18719/* See read.h. */
11a8b164 18720
09ba997f 18721struct type *
293e7e51 18722dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18723{
293e7e51
SM
18724 int addr_size = this->per_cu->addr_size ();
18725 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18726}
18727
b86352cf
AB
18728/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18729 present (which is valid) then compute the default type based on the
18730 compilation units address size. */
18731
18732static struct type *
18733read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18734{
18735 struct type *index_type = die_type (die, cu);
18736
18737 /* Dwarf-2 specifications explicitly allows to create subrange types
18738 without specifying a base type.
18739 In that case, the base type must be set to the type of
18740 the lower bound, upper bound or count, in that order, if any of these
18741 three attributes references an object that has a type.
18742 If no base type is found, the Dwarf-2 specifications say that
18743 a signed integer type of size equal to the size of an address should
18744 be used.
18745 For the following C code: `extern char gdb_int [];'
18746 GCC produces an empty range DIE.
18747 FIXME: muller/2010-05-28: Possible references to object for low bound,
18748 high bound or count are not yet handled by this code. */
78134374 18749 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18750 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18751
18752 return index_type;
18753}
18754
a02abb62
JB
18755/* Read the given DW_AT_subrange DIE. */
18756
f792889a 18757static struct type *
a02abb62
JB
18758read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18759{
4c9ad8c2 18760 struct type *base_type, *orig_base_type;
a02abb62
JB
18761 struct type *range_type;
18762 struct attribute *attr;
729efb13 18763 struct dynamic_prop low, high;
4fae6e18 18764 int low_default_is_valid;
c451ebe5 18765 int high_bound_is_count = 0;
15d034d0 18766 const char *name;
d359392f 18767 ULONGEST negative_mask;
e77813c8 18768
b86352cf
AB
18769 orig_base_type = read_subrange_index_type (die, cu);
18770
4c9ad8c2
TT
18771 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18772 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18773 creating the range type, but we use the result of check_typedef
18774 when examining properties of the type. */
18775 base_type = check_typedef (orig_base_type);
a02abb62 18776
7e314c57
JK
18777 /* The die_type call above may have already set the type for this DIE. */
18778 range_type = get_die_type (die, cu);
18779 if (range_type)
18780 return range_type;
18781
8c2e4e06 18782 high.set_const_val (0);
729efb13 18783
4fae6e18
JK
18784 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18785 omitting DW_AT_lower_bound. */
18786 switch (cu->language)
6e70227d 18787 {
4fae6e18
JK
18788 case language_c:
18789 case language_cplus:
8c2e4e06 18790 low.set_const_val (0);
4fae6e18
JK
18791 low_default_is_valid = 1;
18792 break;
18793 case language_fortran:
8c2e4e06 18794 low.set_const_val (1);
4fae6e18
JK
18795 low_default_is_valid = 1;
18796 break;
18797 case language_d:
4fae6e18 18798 case language_objc:
c44af4eb 18799 case language_rust:
8c2e4e06 18800 low.set_const_val (0);
4fae6e18
JK
18801 low_default_is_valid = (cu->header.version >= 4);
18802 break;
18803 case language_ada:
18804 case language_m2:
18805 case language_pascal:
8c2e4e06 18806 low.set_const_val (1);
4fae6e18
JK
18807 low_default_is_valid = (cu->header.version >= 4);
18808 break;
18809 default:
8c2e4e06 18810 low.set_const_val (0);
4fae6e18
JK
18811 low_default_is_valid = 0;
18812 break;
a02abb62
JB
18813 }
18814
e142c38c 18815 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18816 if (attr != nullptr)
9a49df9d 18817 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18818 else if (!low_default_is_valid)
b98664d3 18819 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18820 "- DIE at %s [in module %s]"),
18821 sect_offset_str (die->sect_off),
5e22e966 18822 objfile_name (cu->per_objfile->objfile));
a02abb62 18823
506f5c41
TV
18824 struct attribute *attr_ub, *attr_count;
18825 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18826 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18827 {
506f5c41 18828 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18829 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18830 {
c451ebe5 18831 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18832 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18833 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18834 else
18835 high_bound_is_count = 1;
c2ff108b 18836 }
506f5c41
TV
18837 else
18838 {
18839 if (attr_ub != NULL)
18840 complaint (_("Unresolved DW_AT_upper_bound "
18841 "- DIE at %s [in module %s]"),
18842 sect_offset_str (die->sect_off),
5e22e966 18843 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18844 if (attr_count != NULL)
18845 complaint (_("Unresolved DW_AT_count "
18846 "- DIE at %s [in module %s]"),
18847 sect_offset_str (die->sect_off),
5e22e966 18848 objfile_name (cu->per_objfile->objfile));
506f5c41 18849 }
e77813c8 18850 }
a02abb62 18851
4e962e74
TT
18852 LONGEST bias = 0;
18853 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18854 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18855 bias = bias_attr->constant_value (0);
4e962e74 18856
dbb9c2b1
JB
18857 /* Normally, the DWARF producers are expected to use a signed
18858 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18859 But this is unfortunately not always the case, as witnessed
18860 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18861 is used instead. To work around that ambiguity, we treat
18862 the bounds as signed, and thus sign-extend their values, when
18863 the base type is signed. */
6e70227d 18864 negative_mask =
d359392f 18865 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18866 if (low.kind () == PROP_CONST
c6d940a9 18867 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18868 low.set_const_val (low.const_val () | negative_mask);
18869 if (high.kind () == PROP_CONST
c6d940a9 18870 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18871 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18872
5bbd8269
AB
18873 /* Check for bit and byte strides. */
18874 struct dynamic_prop byte_stride_prop;
18875 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18876 if (attr_byte_stride != nullptr)
18877 {
293e7e51 18878 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18879 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18880 prop_type);
18881 }
18882
18883 struct dynamic_prop bit_stride_prop;
18884 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18885 if (attr_bit_stride != nullptr)
18886 {
18887 /* It only makes sense to have either a bit or byte stride. */
18888 if (attr_byte_stride != nullptr)
18889 {
18890 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18891 "- DIE at %s [in module %s]"),
18892 sect_offset_str (die->sect_off),
5e22e966 18893 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
18894 attr_bit_stride = nullptr;
18895 }
18896 else
18897 {
293e7e51 18898 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18899 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18900 prop_type);
18901 }
18902 }
18903
18904 if (attr_byte_stride != nullptr
18905 || attr_bit_stride != nullptr)
18906 {
18907 bool byte_stride_p = (attr_byte_stride != nullptr);
18908 struct dynamic_prop *stride
18909 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18910
18911 range_type
18912 = create_range_type_with_stride (NULL, orig_base_type, &low,
18913 &high, bias, stride, byte_stride_p);
18914 }
18915 else
18916 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18917
c451ebe5 18918 if (high_bound_is_count)
599088e3 18919 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 18920
c2ff108b
JK
18921 /* Ada expects an empty array on no boundary attributes. */
18922 if (attr == NULL && cu->language != language_ada)
8c2e4e06 18923 range_type->bounds ()->high.set_undefined ();
c2ff108b 18924
39cbfefa
DJ
18925 name = dwarf2_name (die, cu);
18926 if (name)
d0e39ea2 18927 range_type->set_name (name);
6e70227d 18928
e142c38c 18929 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18930 if (attr != nullptr)
529908cb 18931 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 18932
2b4424c3
TT
18933 maybe_set_alignment (cu, die, range_type);
18934
7e314c57
JK
18935 set_die_type (die, range_type, cu);
18936
18937 /* set_die_type should be already done. */
b4ba55a1
JB
18938 set_descriptive_type (range_type, die, cu);
18939
7e314c57 18940 return range_type;
a02abb62 18941}
6e70227d 18942
f792889a 18943static struct type *
81a17f79
JB
18944read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18945{
18946 struct type *type;
81a17f79 18947
5e22e966 18948 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 18949 type->set_name (dwarf2_name (die, cu));
81a17f79 18950
74a2f8ff 18951 /* In Ada, an unspecified type is typically used when the description
85102364 18952 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18953 such a type, we treat it as a stub, and try to resolve it later on,
18954 when needed. */
18955 if (cu->language == language_ada)
b4b73759 18956 type->set_is_stub (true);
74a2f8ff 18957
f792889a 18958 return set_die_type (die, type, cu);
81a17f79 18959}
a02abb62 18960
639d11d3
DC
18961/* Read a single die and all its descendents. Set the die's sibling
18962 field to NULL; set other fields in the die correctly, and set all
18963 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18964 location of the info_ptr after reading all of those dies. PARENT
18965 is the parent of the die in question. */
18966
18967static struct die_info *
dee91e82 18968read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18969 const gdb_byte *info_ptr,
18970 const gdb_byte **new_info_ptr,
dee91e82 18971 struct die_info *parent)
639d11d3
DC
18972{
18973 struct die_info *die;
d521ce57 18974 const gdb_byte *cur_ptr;
639d11d3 18975
3e225074 18976 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18977 if (die == NULL)
18978 {
18979 *new_info_ptr = cur_ptr;
18980 return NULL;
18981 }
93311388 18982 store_in_ref_table (die, reader->cu);
639d11d3 18983
3e225074 18984 if (die->has_children)
bf6af496 18985 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18986 else
18987 {
18988 die->child = NULL;
18989 *new_info_ptr = cur_ptr;
18990 }
18991
18992 die->sibling = NULL;
18993 die->parent = parent;
18994 return die;
18995}
18996
18997/* Read a die, all of its descendents, and all of its siblings; set
18998 all of the fields of all of the dies correctly. Arguments are as
18999 in read_die_and_children. */
19000
19001static struct die_info *
bf6af496 19002read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
19003 const gdb_byte *info_ptr,
19004 const gdb_byte **new_info_ptr,
bf6af496 19005 struct die_info *parent)
639d11d3
DC
19006{
19007 struct die_info *first_die, *last_sibling;
d521ce57 19008 const gdb_byte *cur_ptr;
639d11d3 19009
c906108c 19010 cur_ptr = info_ptr;
639d11d3
DC
19011 first_die = last_sibling = NULL;
19012
19013 while (1)
c906108c 19014 {
639d11d3 19015 struct die_info *die
dee91e82 19016 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 19017
1d325ec1 19018 if (die == NULL)
c906108c 19019 {
639d11d3
DC
19020 *new_info_ptr = cur_ptr;
19021 return first_die;
c906108c 19022 }
1d325ec1
DJ
19023
19024 if (!first_die)
19025 first_die = die;
c906108c 19026 else
1d325ec1
DJ
19027 last_sibling->sibling = die;
19028
19029 last_sibling = die;
c906108c 19030 }
c906108c
SS
19031}
19032
bf6af496
DE
19033/* Read a die, all of its descendents, and all of its siblings; set
19034 all of the fields of all of the dies correctly. Arguments are as
19035 in read_die_and_children.
19036 This the main entry point for reading a DIE and all its children. */
19037
19038static struct die_info *
19039read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
19040 const gdb_byte *info_ptr,
19041 const gdb_byte **new_info_ptr,
bf6af496
DE
19042 struct die_info *parent)
19043{
19044 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19045 new_info_ptr, parent);
19046
b4f54984 19047 if (dwarf_die_debug)
bf6af496
DE
19048 {
19049 fprintf_unfiltered (gdb_stdlog,
19050 "Read die from %s@0x%x of %s:\n",
96b79293 19051 reader->die_section->get_name (),
bf6af496
DE
19052 (unsigned) (info_ptr - reader->die_section->buffer),
19053 bfd_get_filename (reader->abfd));
b4f54984 19054 dump_die (die, dwarf_die_debug);
bf6af496
DE
19055 }
19056
19057 return die;
19058}
19059
3019eac3
DE
19060/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19061 attributes.
19062 The caller is responsible for filling in the extra attributes
19063 and updating (*DIEP)->num_attrs.
19064 Set DIEP to point to a newly allocated die with its information,
3e225074 19065 except for its child, sibling, and parent fields. */
93311388 19066
d521ce57 19067static const gdb_byte *
3019eac3 19068read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 19069 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 19070 int num_extra_attrs)
93311388 19071{
b64f50a1 19072 unsigned int abbrev_number, bytes_read, i;
7c32eebb 19073 const struct abbrev_info *abbrev;
93311388
DE
19074 struct die_info *die;
19075 struct dwarf2_cu *cu = reader->cu;
19076 bfd *abfd = reader->abfd;
19077
9c541725 19078 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
19079 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19080 info_ptr += bytes_read;
19081 if (!abbrev_number)
19082 {
19083 *diep = NULL;
93311388
DE
19084 return info_ptr;
19085 }
19086
685af9cd 19087 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 19088 if (!abbrev)
348e048f
DE
19089 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19090 abbrev_number,
19091 bfd_get_filename (abfd));
19092
3019eac3 19093 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 19094 die->sect_off = sect_off;
93311388
DE
19095 die->tag = abbrev->tag;
19096 die->abbrev = abbrev_number;
3e225074 19097 die->has_children = abbrev->has_children;
93311388 19098
3019eac3
DE
19099 /* Make the result usable.
19100 The caller needs to update num_attrs after adding the extra
19101 attributes. */
93311388
DE
19102 die->num_attrs = abbrev->num_attrs;
19103
7a5f294d 19104 bool any_need_reprocess = false;
93311388 19105 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 19106 {
7a5f294d
TT
19107 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19108 info_ptr);
19109 if (die->attrs[i].requires_reprocessing_p ())
19110 any_need_reprocess = true;
18a8505e
AT
19111 }
19112
052c8bb8 19113 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
19114 if (attr != nullptr && attr->form_is_unsigned ())
19115 cu->str_offsets_base = attr->as_unsigned ();
93311388 19116
41144253 19117 attr = die->attr (DW_AT_loclists_base);
19118 if (attr != nullptr)
529908cb 19119 cu->loclist_base = attr->as_unsigned ();
41144253 19120
a39fdb41 19121 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
19122 if (maybe_addr_base.has_value ())
19123 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
19124
19125 attr = die->attr (DW_AT_rnglists_base);
19126 if (attr != nullptr)
2b0c7f41 19127 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 19128
7a5f294d
TT
19129 if (any_need_reprocess)
19130 {
19131 for (i = 0; i < abbrev->num_attrs; ++i)
19132 {
19133 if (die->attrs[i].requires_reprocessing_p ())
19134 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19135 }
19136 }
93311388 19137 *diep = die;
93311388
DE
19138 return info_ptr;
19139}
19140
3019eac3
DE
19141/* Read a die and all its attributes.
19142 Set DIEP to point to a newly allocated die with its information,
3e225074 19143 except for its child, sibling, and parent fields. */
3019eac3 19144
d521ce57 19145static const gdb_byte *
3019eac3 19146read_full_die (const struct die_reader_specs *reader,
3e225074 19147 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19148{
d521ce57 19149 const gdb_byte *result;
bf6af496 19150
3e225074 19151 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19152
b4f54984 19153 if (dwarf_die_debug)
bf6af496
DE
19154 {
19155 fprintf_unfiltered (gdb_stdlog,
19156 "Read die from %s@0x%x of %s:\n",
96b79293 19157 reader->die_section->get_name (),
bf6af496
DE
19158 (unsigned) (info_ptr - reader->die_section->buffer),
19159 bfd_get_filename (reader->abfd));
b4f54984 19160 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19161 }
19162
19163 return result;
3019eac3 19164}
433df2d4 19165\f
c906108c 19166
72bf9492
DJ
19167/* Returns nonzero if TAG represents a type that we might generate a partial
19168 symbol for. */
19169
19170static int
d8f62e84 19171is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19172{
19173 switch (tag)
19174 {
19175#if 0
19176 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19177 that we don't at present. Note that normally this does not
19178 matter, mainly because C compilers don't give names to these
19179 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19180 case DW_TAG_file_type:
19181 case DW_TAG_ptr_to_member_type:
19182 case DW_TAG_set_type:
19183 case DW_TAG_string_type:
19184 case DW_TAG_subroutine_type:
19185#endif
d8f62e84
TT
19186
19187 /* GNAT may emit an array with a name, but no typedef, so we
19188 need to make a symbol in this case. */
19189 case DW_TAG_array_type:
19190 return lang == language_ada;
19191
72bf9492
DJ
19192 case DW_TAG_base_type:
19193 case DW_TAG_class_type:
680b30c7 19194 case DW_TAG_interface_type:
72bf9492
DJ
19195 case DW_TAG_enumeration_type:
19196 case DW_TAG_structure_type:
19197 case DW_TAG_subrange_type:
19198 case DW_TAG_typedef:
19199 case DW_TAG_union_type:
19200 return 1;
19201 default:
19202 return 0;
19203 }
19204}
19205
19206/* Load all DIEs that are interesting for partial symbols into memory. */
19207
19208static struct partial_die_info *
dee91e82 19209load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19210 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19211{
dee91e82 19212 struct dwarf2_cu *cu = reader->cu;
5e22e966 19213 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19214 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19215 unsigned int bytes_read;
5afb4e99 19216 unsigned int load_all = 0;
72bf9492
DJ
19217 int nesting_level = 1;
19218
19219 parent_die = NULL;
19220 last_die = NULL;
19221
7adf1e79
DE
19222 gdb_assert (cu->per_cu != NULL);
19223 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19224 load_all = 1;
19225
72bf9492
DJ
19226 cu->partial_dies
19227 = htab_create_alloc_ex (cu->header.length / 12,
19228 partial_die_hash,
19229 partial_die_eq,
19230 NULL,
19231 &cu->comp_unit_obstack,
19232 hashtab_obstack_allocate,
19233 dummy_obstack_deallocate);
19234
72bf9492
DJ
19235 while (1)
19236 {
7c32eebb
TT
19237 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19238 &bytes_read);
72bf9492
DJ
19239
19240 /* A NULL abbrev means the end of a series of children. */
19241 if (abbrev == NULL)
19242 {
19243 if (--nesting_level == 0)
cd9983dd
YQ
19244 return first_die;
19245
72bf9492
DJ
19246 info_ptr += bytes_read;
19247 last_die = parent_die;
19248 parent_die = parent_die->die_parent;
19249 continue;
19250 }
19251
98bfdba5
PA
19252 /* Check for template arguments. We never save these; if
19253 they're seen, we just mark the parent, and go on our way. */
19254 if (parent_die != NULL
19255 && cu->language == language_cplus
19256 && (abbrev->tag == DW_TAG_template_type_param
19257 || abbrev->tag == DW_TAG_template_value_param))
19258 {
19259 parent_die->has_template_arguments = 1;
19260
19261 if (!load_all)
19262 {
19263 /* We don't need a partial DIE for the template argument. */
dee91e82 19264 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19265 continue;
19266 }
19267 }
19268
0d99eb77 19269 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19270 Skip their other children. */
19271 if (!load_all
19272 && cu->language == language_cplus
19273 && parent_die != NULL
f9b5d5ea
TV
19274 && parent_die->tag == DW_TAG_subprogram
19275 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19276 {
dee91e82 19277 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19278 continue;
19279 }
19280
5afb4e99
DJ
19281 /* Check whether this DIE is interesting enough to save. Normally
19282 we would not be interested in members here, but there may be
19283 later variables referencing them via DW_AT_specification (for
19284 static members). */
19285 if (!load_all
d8f62e84 19286 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19287 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19288 && abbrev->tag != DW_TAG_enumerator
19289 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19290 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19291 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19292 && abbrev->tag != DW_TAG_variable
5afb4e99 19293 && abbrev->tag != DW_TAG_namespace
f55ee35c 19294 && abbrev->tag != DW_TAG_module
95554aad 19295 && abbrev->tag != DW_TAG_member
74921315
KS
19296 && abbrev->tag != DW_TAG_imported_unit
19297 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19298 {
19299 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19300 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19301 continue;
19302 }
19303
6f06d47b
YQ
19304 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19305 abbrev);
cd9983dd 19306
48fbe735 19307 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19308
19309 /* This two-pass algorithm for processing partial symbols has a
19310 high cost in cache pressure. Thus, handle some simple cases
19311 here which cover the majority of C partial symbols. DIEs
19312 which neither have specification tags in them, nor could have
19313 specification tags elsewhere pointing at them, can simply be
19314 processed and discarded.
19315
19316 This segment is also optional; scan_partial_symbols and
19317 add_partial_symbol will handle these DIEs if we chain
19318 them in normally. When compilers which do not emit large
19319 quantities of duplicate debug information are more common,
19320 this code can probably be removed. */
19321
19322 /* Any complete simple types at the top level (pretty much all
19323 of them, for a language without namespaces), can be processed
19324 directly. */
19325 if (parent_die == NULL
cd9983dd
YQ
19326 && pdi.has_specification == 0
19327 && pdi.is_declaration == 0
19328 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19329 || pdi.tag == DW_TAG_base_type
d8f62e84 19330 || pdi.tag == DW_TAG_array_type
cd9983dd 19331 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19332 {
7d00ffec 19333 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19334 add_partial_symbol (&pdi, cu);
19335
cd9983dd 19336 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19337 continue;
19338 }
19339
d8228535
JK
19340 /* The exception for DW_TAG_typedef with has_children above is
19341 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19342 type_name_or_error will error on such types later.
d8228535
JK
19343
19344 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19345 it could not find the child DIEs referenced later, this is checked
19346 above. In correct DWARF DW_TAG_typedef should have no children. */
19347
cd9983dd 19348 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19349 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19350 "- DIE at %s [in module %s]"),
cd9983dd 19351 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19352
72bf9492
DJ
19353 /* If we're at the second level, and we're an enumerator, and
19354 our parent has no specification (meaning possibly lives in a
19355 namespace elsewhere), then we can add the partial symbol now
19356 instead of queueing it. */
cd9983dd 19357 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19358 && parent_die != NULL
19359 && parent_die->die_parent == NULL
19360 && parent_die->tag == DW_TAG_enumeration_type
19361 && parent_die->has_specification == 0)
19362 {
7d00ffec 19363 if (pdi.raw_name == NULL)
b98664d3 19364 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19365 else if (building_psymtab)
f0fbb768 19366 add_partial_symbol (&pdi, cu);
72bf9492 19367
cd9983dd 19368 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19369 continue;
19370 }
19371
cd9983dd 19372 struct partial_die_info *part_die
6f06d47b 19373 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19374
72bf9492
DJ
19375 /* We'll save this DIE so link it in. */
19376 part_die->die_parent = parent_die;
19377 part_die->die_sibling = NULL;
19378 part_die->die_child = NULL;
19379
19380 if (last_die && last_die == parent_die)
19381 last_die->die_child = part_die;
19382 else if (last_die)
19383 last_die->die_sibling = part_die;
19384
19385 last_die = part_die;
19386
19387 if (first_die == NULL)
19388 first_die = part_die;
19389
19390 /* Maybe add the DIE to the hash table. Not all DIEs that we
19391 find interesting need to be in the hash table, because we
19392 also have the parent/sibling/child chains; only those that we
19393 might refer to by offset later during partial symbol reading.
19394
19395 For now this means things that might have be the target of a
19396 DW_AT_specification, DW_AT_abstract_origin, or
19397 DW_AT_extension. DW_AT_extension will refer only to
19398 namespaces; DW_AT_abstract_origin refers to functions (and
19399 many things under the function DIE, but we do not recurse
19400 into function DIEs during partial symbol reading) and
19401 possibly variables as well; DW_AT_specification refers to
19402 declarations. Declarations ought to have the DW_AT_declaration
19403 flag. It happens that GCC forgets to put it in sometimes, but
19404 only for functions, not for types.
19405
19406 Adding more things than necessary to the hash table is harmless
19407 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19408 wasted time in find_partial_die, when we reread the compilation
19409 unit with load_all_dies set. */
72bf9492 19410
5afb4e99 19411 if (load_all
72929c62 19412 || abbrev->tag == DW_TAG_constant
5afb4e99 19413 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19414 || abbrev->tag == DW_TAG_variable
19415 || abbrev->tag == DW_TAG_namespace
19416 || part_die->is_declaration)
19417 {
19418 void **slot;
19419
19420 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19421 to_underlying (part_die->sect_off),
19422 INSERT);
72bf9492
DJ
19423 *slot = part_die;
19424 }
19425
72bf9492 19426 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19427 we have no reason to follow the children of structures; for other
98bfdba5
PA
19428 languages we have to, so that we can get at method physnames
19429 to infer fully qualified class names, for DW_AT_specification,
19430 and for C++ template arguments. For C++, we also look one level
19431 inside functions to find template arguments (if the name of the
19432 function does not already contain the template arguments).
bc30ff58 19433
0a4b0913
AB
19434 For Ada and Fortran, we need to scan the children of subprograms
19435 and lexical blocks as well because these languages allow the
19436 definition of nested entities that could be interesting for the
19437 debugger, such as nested subprograms for instance. */
72bf9492 19438 if (last_die->has_children
5afb4e99
DJ
19439 && (load_all
19440 || last_die->tag == DW_TAG_namespace
f55ee35c 19441 || last_die->tag == DW_TAG_module
72bf9492 19442 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19443 || (cu->language == language_cplus
19444 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19445 && (last_die->raw_name == NULL
19446 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19447 || (cu->language != language_c
19448 && (last_die->tag == DW_TAG_class_type
680b30c7 19449 || last_die->tag == DW_TAG_interface_type
72bf9492 19450 || last_die->tag == DW_TAG_structure_type
bc30ff58 19451 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19452 || ((cu->language == language_ada
19453 || cu->language == language_fortran)
bc30ff58
JB
19454 && (last_die->tag == DW_TAG_subprogram
19455 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19456 {
19457 nesting_level++;
19458 parent_die = last_die;
19459 continue;
19460 }
19461
19462 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19463 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19464
19465 /* Back to the top, do it again. */
19466 }
19467}
19468
6f06d47b 19469partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19470 const struct abbrev_info *abbrev)
6f06d47b
YQ
19471 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19472{
19473}
19474
7d00ffec
TT
19475/* See class definition. */
19476
19477const char *
19478partial_die_info::name (dwarf2_cu *cu)
19479{
19480 if (!canonical_name && raw_name != nullptr)
19481 {
19482 struct objfile *objfile = cu->per_objfile->objfile;
19483 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19484 canonical_name = 1;
19485 }
19486
19487 return raw_name;
19488}
19489
35cc7ed7
YQ
19490/* Read a minimal amount of information into the minimal die structure.
19491 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19492
48fbe735
YQ
19493const gdb_byte *
19494partial_die_info::read (const struct die_reader_specs *reader,
19495 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19496{
dee91e82 19497 struct dwarf2_cu *cu = reader->cu;
976ca316 19498 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19499 unsigned int i;
c5aa993b 19500 int has_low_pc_attr = 0;
c906108c 19501 int has_high_pc_attr = 0;
91da1414 19502 int high_pc_relative = 0;
c906108c 19503
fd0a254f 19504 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19505 {
e7da7f8f 19506 attribute attr;
7a5f294d 19507 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19508 /* String and address offsets that need to do the reprocessing have
dda83cd7 19509 already been read at this point, so there is no need to wait until
18a8505e 19510 the loop terminates to do the reprocessing. */
7a5f294d 19511 if (attr.requires_reprocessing_p ())
d0ce17d8 19512 read_attribute_reprocess (reader, &attr, tag);
c906108c 19513 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19514 partial symbol table. */
c906108c
SS
19515 switch (attr.name)
19516 {
19517 case DW_AT_name:
48fbe735 19518 switch (tag)
71c25dea
TT
19519 {
19520 case DW_TAG_compile_unit:
95554aad 19521 case DW_TAG_partial_unit:
348e048f 19522 case DW_TAG_type_unit:
71c25dea
TT
19523 /* Compilation units have a DW_AT_name that is a filename, not
19524 a source language identifier. */
19525 case DW_TAG_enumeration_type:
19526 case DW_TAG_enumerator:
19527 /* These tags always have simple identifiers already; no need
19528 to canonicalize them. */
7d00ffec 19529 canonical_name = 1;
2c830f54 19530 raw_name = attr.as_string ();
71c25dea
TT
19531 break;
19532 default:
7d00ffec 19533 canonical_name = 0;
2c830f54 19534 raw_name = attr.as_string ();
71c25dea
TT
19535 break;
19536 }
c906108c 19537 break;
31ef98ae 19538 case DW_AT_linkage_name:
c906108c 19539 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19540 /* Note that both forms of linkage name might appear. We
19541 assume they will be the same, and we only store the last
19542 one we see. */
95f982e5 19543 linkage_name = attr.as_string ();
c906108c
SS
19544 break;
19545 case DW_AT_low_pc:
19546 has_low_pc_attr = 1;
95f982e5 19547 lowpc = attr.as_address ();
c906108c
SS
19548 break;
19549 case DW_AT_high_pc:
19550 has_high_pc_attr = 1;
95f982e5 19551 highpc = attr.as_address ();
cd6c91b4 19552 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19553 high_pc_relative = 1;
c906108c
SS
19554 break;
19555 case DW_AT_location:
dda83cd7
SM
19556 /* Support the .debug_loc offsets. */
19557 if (attr.form_is_block ())
19558 {
9d2246fc 19559 d.locdesc = attr.as_block ();
dda83cd7
SM
19560 }
19561 else if (attr.form_is_section_offset ())
19562 {
4d3c2250 19563 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19564 }
19565 else
19566 {
4d3c2250
KB
19567 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19568 "partial symbol information");
dda83cd7 19569 }
c906108c 19570 break;
c906108c 19571 case DW_AT_external:
c45bc3f8 19572 is_external = attr.as_boolean ();
c906108c
SS
19573 break;
19574 case DW_AT_declaration:
c45bc3f8 19575 is_declaration = attr.as_boolean ();
c906108c
SS
19576 break;
19577 case DW_AT_type:
48fbe735 19578 has_type = 1;
c906108c
SS
19579 break;
19580 case DW_AT_abstract_origin:
19581 case DW_AT_specification:
72bf9492 19582 case DW_AT_extension:
48fbe735 19583 has_specification = 1;
0826b30a 19584 spec_offset = attr.get_ref_die_offset ();
48fbe735 19585 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19586 || cu->per_cu->is_dwz);
c906108c
SS
19587 break;
19588 case DW_AT_sibling:
19589 /* Ignore absolute siblings, they might point outside of
19590 the current compile unit. */
19591 if (attr.form == DW_FORM_ref_addr)
b98664d3 19592 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19593 else
b9502d3f 19594 {
48fbe735 19595 const gdb_byte *buffer = reader->buffer;
0826b30a 19596 sect_offset off = attr.get_ref_die_offset ();
9c541725 19597 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19598
19599 if (sibling_ptr < info_ptr)
b98664d3 19600 complaint (_("DW_AT_sibling points backwards"));
22869d73 19601 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19602 reader->die_section->overflow_complaint ();
b9502d3f 19603 else
48fbe735 19604 sibling = sibling_ptr;
b9502d3f 19605 }
c906108c 19606 break;
dda83cd7
SM
19607 case DW_AT_byte_size:
19608 has_byte_size = 1;
19609 break;
19610 case DW_AT_const_value:
19611 has_const_value = 1;
19612 break;
68511cec
CES
19613 case DW_AT_calling_convention:
19614 /* DWARF doesn't provide a way to identify a program's source-level
19615 entry point. DW_AT_calling_convention attributes are only meant
19616 to describe functions' calling conventions.
19617
19618 However, because it's a necessary piece of information in
0c1b455e
TT
19619 Fortran, and before DWARF 4 DW_CC_program was the only
19620 piece of debugging information whose definition refers to
19621 a 'main program' at all, several compilers marked Fortran
19622 main programs with DW_CC_program --- even when those
19623 functions use the standard calling conventions.
19624
19625 Although DWARF now specifies a way to provide this
19626 information, we support this practice for backward
19627 compatibility. */
529908cb 19628 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19629 && cu->language == language_fortran)
48fbe735 19630 main_subprogram = 1;
68511cec 19631 break;
481860b3 19632 case DW_AT_inline:
529908cb
TT
19633 {
19634 LONGEST value = attr.constant_value (-1);
19635 if (value == DW_INL_inlined
19636 || value == DW_INL_declared_inlined)
19637 may_be_inlined = 1;
19638 }
481860b3 19639 break;
95554aad
TT
19640
19641 case DW_AT_import:
48fbe735 19642 if (tag == DW_TAG_imported_unit)
36586728 19643 {
0826b30a 19644 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19645 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19646 || cu->per_cu->is_dwz);
19647 }
95554aad
TT
19648 break;
19649
0c1b455e 19650 case DW_AT_main_subprogram:
c45bc3f8 19651 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19652 break;
19653
05caa1d2
TT
19654 case DW_AT_ranges:
19655 {
2b0c7f41
SM
19656 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19657 on DWARF version). */
19658 ULONGEST ranges_offset = attr.as_unsigned ();
19659
19660 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19661 this value. */
19662 if (tag != DW_TAG_compile_unit)
19663 ranges_offset += cu->gnu_ranges_base;
19664
05caa1d2 19665 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19666 nullptr, tag))
05caa1d2
TT
19667 has_pc_info = 1;
19668 }
19669 break;
19670
c906108c
SS
19671 default:
19672 break;
19673 }
19674 }
19675
10d06d82
TT
19676 /* For Ada, if both the name and the linkage name appear, we prefer
19677 the latter. This lets "catch exception" work better, regardless
19678 of the order in which the name and linkage name were emitted.
19679 Really, though, this is just a workaround for the fact that gdb
19680 doesn't store both the name and the linkage name. */
19681 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19682 raw_name = linkage_name;
10d06d82 19683
91da1414 19684 if (high_pc_relative)
48fbe735 19685 highpc += lowpc;
91da1414 19686
9373cf26
JK
19687 if (has_low_pc_attr && has_high_pc_attr)
19688 {
19689 /* When using the GNU linker, .gnu.linkonce. sections are used to
19690 eliminate duplicate copies of functions and vtables and such.
19691 The linker will arbitrarily choose one and discard the others.
19692 The AT_*_pc values for such functions refer to local labels in
19693 these sections. If the section from that file was discarded, the
19694 labels are not in the output, so the relocs get a value of 0.
19695 If this is a discarded function, mark the pc bounds as invalid,
19696 so that GDB will ignore it. */
976ca316 19697 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19698 {
976ca316 19699 struct objfile *objfile = per_objfile->objfile;
08feed99 19700 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19701
b98664d3 19702 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19703 "for DIE at %s [in module %s]"),
48fbe735
YQ
19704 paddress (gdbarch, lowpc),
19705 sect_offset_str (sect_off),
9d8780f0 19706 objfile_name (objfile));
9373cf26
JK
19707 }
19708 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19709 else if (lowpc >= highpc)
9373cf26 19710 {
976ca316 19711 struct objfile *objfile = per_objfile->objfile;
08feed99 19712 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19713
b98664d3 19714 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19715 "for DIE at %s [in module %s]"),
48fbe735
YQ
19716 paddress (gdbarch, lowpc),
19717 paddress (gdbarch, highpc),
19718 sect_offset_str (sect_off),
9c541725 19719 objfile_name (objfile));
9373cf26
JK
19720 }
19721 else
48fbe735 19722 has_pc_info = 1;
9373cf26 19723 }
85cbf3d3 19724
c906108c
SS
19725 return info_ptr;
19726}
19727
72bf9492
DJ
19728/* Find a cached partial DIE at OFFSET in CU. */
19729
d590ff25
YQ
19730struct partial_die_info *
19731dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19732{
19733 struct partial_die_info *lookup_die = NULL;
6f06d47b 19734 struct partial_die_info part_die (sect_off);
72bf9492 19735
9a3c8263 19736 lookup_die = ((struct partial_die_info *)
d590ff25 19737 htab_find_with_hash (partial_dies, &part_die,
9c541725 19738 to_underlying (sect_off)));
72bf9492 19739
72bf9492
DJ
19740 return lookup_die;
19741}
19742
348e048f
DE
19743/* Find a partial DIE at OFFSET, which may or may not be in CU,
19744 except in the case of .debug_types DIEs which do not reference
19745 outside their CU (they do however referencing other types via
55f1336d 19746 DW_FORM_ref_sig8). */
72bf9492 19747
122cf0f2 19748static const struct cu_partial_die_info
9c541725 19749find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19750{
976ca316
SM
19751 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19752 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19753 struct partial_die_info *pd = NULL;
72bf9492 19754
36586728 19755 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19756 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19757 {
d590ff25 19758 pd = cu->find_partial_die (sect_off);
5afb4e99 19759 if (pd != NULL)
fb816e8b 19760 return { cu, pd };
0d99eb77
DE
19761 /* We missed recording what we needed.
19762 Load all dies and try again. */
5afb4e99 19763 }
0d99eb77
DE
19764 else
19765 {
19766 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19767 if (cu->per_cu->is_debug_types)
0d99eb77 19768 {
9d8780f0
SM
19769 error (_("Dwarf Error: Type Unit at offset %s contains"
19770 " external reference to offset %s [in module %s].\n"),
19771 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19772 bfd_get_filename (objfile->obfd));
19773 }
7188ed02
SM
19774 dwarf2_per_cu_data *per_cu
19775 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19776 per_objfile);
72bf9492 19777
976ca316 19778 cu = per_objfile->get_cu (per_cu);
7188ed02 19779 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19780 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19781
976ca316 19782 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19783
19784 cu->last_used = 0;
19785 pd = cu->find_partial_die (sect_off);
0d99eb77 19786 }
5afb4e99 19787
dee91e82
DE
19788 /* If we didn't find it, and not all dies have been loaded,
19789 load them all and try again. */
19790
7188ed02 19791 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 19792 {
7188ed02 19793 cu->per_cu->load_all_dies = 1;
fd820528
DE
19794
19795 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19796 THIS_CU->cu may already be in use. So we can't just free it and
19797 replace its DIEs with the ones we read in. Instead, we leave those
19798 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19799 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19800 set. */
976ca316 19801 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19802
7188ed02 19803 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19804 }
19805
19806 if (pd == NULL)
521894aa 19807 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 19808 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19809 return { cu, pd };
72bf9492
DJ
19810}
19811
abc72ce4
DE
19812/* See if we can figure out if the class lives in a namespace. We do
19813 this by looking for a member function; its demangled name will
19814 contain namespace info, if there is any. */
19815
19816static void
19817guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19818 struct dwarf2_cu *cu)
19819{
19820 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19821 what template types look like, because the demangler
19822 frequently doesn't give the same name as the debug info. We
19823 could fix this by only using the demangled name to get the
19824 prefix (but see comment in read_structure_type). */
19825
19826 struct partial_die_info *real_pdi;
19827 struct partial_die_info *child_pdi;
19828
19829 /* If this DIE (this DIE's specification, if any) has a parent, then
19830 we should not do this. We'll prepend the parent's fully qualified
19831 name when we create the partial symbol. */
19832
19833 real_pdi = struct_pdi;
19834 while (real_pdi->has_specification)
fb816e8b 19835 {
122cf0f2
AB
19836 auto res = find_partial_die (real_pdi->spec_offset,
19837 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19838 real_pdi = res.pdi;
19839 cu = res.cu;
19840 }
abc72ce4
DE
19841
19842 if (real_pdi->die_parent != NULL)
19843 return;
19844
19845 for (child_pdi = struct_pdi->die_child;
19846 child_pdi != NULL;
19847 child_pdi = child_pdi->die_sibling)
19848 {
19849 if (child_pdi->tag == DW_TAG_subprogram
19850 && child_pdi->linkage_name != NULL)
19851 {
43816ebc 19852 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19853 (cu->language_defn->class_name_from_physname
19854 (child_pdi->linkage_name));
abc72ce4
DE
19855 if (actual_class_name != NULL)
19856 {
5e22e966 19857 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19858 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19859 struct_pdi->canonical_name = 1;
abc72ce4
DE
19860 }
19861 break;
19862 }
19863 }
19864}
19865
25c11aca
TV
19866/* Return true if a DIE with TAG may have the DW_AT_const_value
19867 attribute. */
19868
19869static bool
19870can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19871{
19872 switch (tag)
19873 {
19874 case DW_TAG_constant:
19875 case DW_TAG_enumerator:
19876 case DW_TAG_formal_parameter:
19877 case DW_TAG_template_value_param:
19878 case DW_TAG_variable:
19879 return true;
19880 }
19881
19882 return false;
19883}
19884
52356b79
YQ
19885void
19886partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19887{
abc72ce4
DE
19888 /* Once we've fixed up a die, there's no point in doing so again.
19889 This also avoids a memory leak if we were to call
19890 guess_partial_die_structure_name multiple times. */
52356b79 19891 if (fixup_called)
abc72ce4
DE
19892 return;
19893
72bf9492
DJ
19894 /* If we found a reference attribute and the DIE has no name, try
19895 to find a name in the referred to DIE. */
19896
7d00ffec 19897 if (raw_name == NULL && has_specification)
72bf9492
DJ
19898 {
19899 struct partial_die_info *spec_die;
72bf9492 19900
122cf0f2 19901 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19902 spec_die = res.pdi;
19903 cu = res.cu;
72bf9492 19904
52356b79 19905 spec_die->fixup (cu);
72bf9492 19906
7d00ffec 19907 if (spec_die->raw_name)
72bf9492 19908 {
7d00ffec
TT
19909 raw_name = spec_die->raw_name;
19910 canonical_name = spec_die->canonical_name;
72bf9492
DJ
19911
19912 /* Copy DW_AT_external attribute if it is set. */
19913 if (spec_die->is_external)
52356b79 19914 is_external = spec_die->is_external;
72bf9492
DJ
19915 }
19916 }
19917
25c11aca
TV
19918 if (!has_const_value && has_specification
19919 && can_have_DW_AT_const_value_p (tag))
19920 {
19921 struct partial_die_info *spec_die;
19922
19923 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19924 spec_die = res.pdi;
19925 cu = res.cu;
19926
19927 spec_die->fixup (cu);
19928
19929 if (spec_die->has_const_value)
19930 {
19931 /* Copy DW_AT_const_value attribute if it is set. */
19932 has_const_value = spec_die->has_const_value;
19933 }
19934 }
19935
72bf9492 19936 /* Set default names for some unnamed DIEs. */
72bf9492 19937
7d00ffec
TT
19938 if (raw_name == NULL && tag == DW_TAG_namespace)
19939 {
19940 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19941 canonical_name = 1;
19942 }
72bf9492 19943
abc72ce4
DE
19944 /* If there is no parent die to provide a namespace, and there are
19945 children, see if we can determine the namespace from their linkage
122d1940 19946 name. */
abc72ce4 19947 if (cu->language == language_cplus
5e22e966 19948 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
19949 && die_parent == NULL
19950 && has_children
19951 && (tag == DW_TAG_class_type
19952 || tag == DW_TAG_structure_type
19953 || tag == DW_TAG_union_type))
19954 guess_partial_die_structure_name (this, cu);
abc72ce4 19955
53832f31
TT
19956 /* GCC might emit a nameless struct or union that has a linkage
19957 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 19958 if (raw_name == NULL
52356b79
YQ
19959 && (tag == DW_TAG_class_type
19960 || tag == DW_TAG_interface_type
19961 || tag == DW_TAG_structure_type
19962 || tag == DW_TAG_union_type)
19963 && linkage_name != NULL)
53832f31 19964 {
43816ebc
TT
19965 gdb::unique_xmalloc_ptr<char> demangled
19966 (gdb_demangle (linkage_name, DMGL_TYPES));
19967 if (demangled != nullptr)
53832f31 19968 {
96408a79
SA
19969 const char *base;
19970
19971 /* Strip any leading namespaces/classes, keep only the base name.
19972 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19973 base = strrchr (demangled.get (), ':');
19974 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19975 base++;
19976 else
43816ebc 19977 base = demangled.get ();
96408a79 19978
5e22e966 19979 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19980 raw_name = objfile->intern (base);
19981 canonical_name = 1;
53832f31
TT
19982 }
19983 }
19984
52356b79 19985 fixup_called = 1;
72bf9492
DJ
19986}
19987
d0ce17d8 19988/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
19989 contents from the given SECTION in the HEADER.
19990
19991 HEADER_OFFSET is the offset of the header in the section. */
41144253 19992static void
d0ce17d8 19993read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
19994 struct dwarf2_section_info *section,
19995 sect_offset header_offset)
41144253 19996{
19997 unsigned int bytes_read;
19998 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
19999 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20000
41144253 20001 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20002 info_ptr += bytes_read;
a1c40103 20003
41144253 20004 header->version = read_2_bytes (abfd, info_ptr);
20005 info_ptr += 2;
a1c40103 20006
41144253 20007 header->addr_size = read_1_byte (abfd, info_ptr);
20008 info_ptr += 1;
a1c40103 20009
41144253 20010 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20011 info_ptr += 1;
a1c40103 20012
41144253 20013 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20014}
20015
20016/* Return the DW_AT_loclists_base value for the CU. */
20017static ULONGEST
20018lookup_loclist_base (struct dwarf2_cu *cu)
20019{
20020 /* For the .dwo unit, the loclist_base points to the first offset following
20021 the header. The header consists of the following entities-
20022 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20023 bit format)
20024 2. version (2 bytes)
20025 3. address size (1 byte)
20026 4. segment selector size (1 byte)
20027 5. offset entry count (4 bytes)
20028 These sizes are derived as per the DWARFv5 standard. */
20029 if (cu->dwo_unit != nullptr)
20030 {
20031 if (cu->header.initial_length_size == 4)
20032 return LOCLIST_HEADER_SIZE32;
20033 return LOCLIST_HEADER_SIZE64;
20034 }
20035 return cu->loclist_base;
20036}
20037
20038/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20039 array of offsets in the .debug_loclists section. */
e57933dc
SM
20040
20041static sect_offset
41144253 20042read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20043{
976ca316
SM
20044 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20045 struct objfile *objfile = per_objfile->objfile;
41144253 20046 bfd *abfd = objfile->obfd;
a1c40103
SM
20047 ULONGEST loclist_header_size =
20048 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20049 : LOCLIST_HEADER_SIZE64);
41144253 20050 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
20051
20052 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20053 ULONGEST start_offset =
20054 loclist_base + loclist_index * cu->header.offset_size;
20055
a1c40103 20056 /* Get loclists section. */
41144253 20057 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20058
a1c40103 20059 /* Read the loclists section content. */
41144253 20060 section->read (objfile);
20061 if (section->buffer == NULL)
a0c1eeba
SM
20062 error (_("DW_FORM_loclistx used without .debug_loclists "
20063 "section [in module %s]"), objfile_name (objfile));
20064
a1c40103
SM
20065 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20066 so if loclist_base is smaller than the header size, we have a problem. */
20067 if (loclist_base < loclist_header_size)
20068 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20069 objfile_name (objfile));
20070
20071 /* Read the header of the loclists contribution. */
d0ce17d8 20072 struct loclists_rnglists_header header;
a1c40103
SM
20073 read_loclists_rnglists_header (&header, section,
20074 (sect_offset) (loclist_base - loclist_header_size));
20075
20076 /* Verify the loclist index is valid. */
41144253 20077 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
20078 error (_("DW_FORM_loclistx pointing outside of "
20079 ".debug_loclists offset array [in module %s]"),
20080 objfile_name (objfile));
20081
05787bad
SM
20082 /* Validate that reading won't go beyond the end of the section. */
20083 if (start_offset + cu->header.offset_size > section->size)
20084 error (_("Reading DW_FORM_loclistx index beyond end of"
20085 ".debug_loclists section [in module %s]"),
20086 objfile_name (objfile));
20087
20088 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 20089
20090 if (cu->header.offset_size == 4)
e57933dc 20091 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 20092 else
e57933dc 20093 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 20094}
20095
d0ce17d8
CT
20096/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20097 array of offsets in the .debug_rnglists section. */
e57933dc
SM
20098
20099static sect_offset
d0ce17d8
CT
20100read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20101 dwarf_tag tag)
20102{
20103 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20104 struct objfile *objfile = dwarf2_per_objfile->objfile;
20105 bfd *abfd = objfile->obfd;
20106 ULONGEST rnglist_header_size =
20107 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20108 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
20109
20110 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20111 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20112 doesn't apply. */
d0ce17d8 20113 ULONGEST rnglist_base =
2b0c7f41 20114 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
20115
20116 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
20117 ULONGEST start_offset =
20118 rnglist_base + rnglist_index * cu->header.offset_size;
20119
20120 /* Get rnglists section. */
20121 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20122
20123 /* Read the rnglists section content. */
20124 section->read (objfile);
20125 if (section->buffer == nullptr)
20126 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20127 "[in module %s]"),
20128 objfile_name (objfile));
20129
a1c40103
SM
20130 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20131 so if rnglist_base is smaller than the header size, we have a problem. */
20132 if (rnglist_base < rnglist_header_size)
20133 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20134 objfile_name (objfile));
20135
20136 /* Read the header of the rnglists contribution. */
d0ce17d8 20137 struct loclists_rnglists_header header;
a1c40103
SM
20138 read_loclists_rnglists_header (&header, section,
20139 (sect_offset) (rnglist_base - rnglist_header_size));
20140
20141 /* Verify the rnglist index is valid. */
d0ce17d8
CT
20142 if (rnglist_index >= header.offset_entry_count)
20143 error (_("DW_FORM_rnglistx index pointing outside of "
20144 ".debug_rnglists offset array [in module %s]"),
20145 objfile_name (objfile));
20146
d0ce17d8 20147 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 20148 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
20149 error (_("Reading DW_FORM_rnglistx index beyond end of"
20150 ".debug_rnglists section [in module %s]"),
20151 objfile_name (objfile));
20152
20153 const gdb_byte *info_ptr = section->buffer + start_offset;
20154
20155 if (cu->header.offset_size == 4)
e57933dc 20156 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 20157 else
e57933dc 20158 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
20159}
20160
18a8505e
AT
20161/* Process the attributes that had to be skipped in the first round. These
20162 attributes are the ones that need str_offsets_base or addr_base attributes.
20163 They could not have been processed in the first round, because at the time
20164 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20165static void
20166read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20167 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20168{
20169 struct dwarf2_cu *cu = reader->cu;
20170 switch (attr->form)
20171 {
20172 case DW_FORM_addrx:
20173 case DW_FORM_GNU_addr_index:
36d378cf
TT
20174 attr->set_address (read_addr_index (cu,
20175 attr->as_unsigned_reprocess ()));
dda83cd7 20176 break;
41144253 20177 case DW_FORM_loclistx:
b1829e1b 20178 {
e57933dc 20179 sect_offset loclists_sect_off
b1829e1b
SM
20180 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20181
e57933dc 20182 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
20183 }
20184 break;
d0ce17d8 20185 case DW_FORM_rnglistx:
b1829e1b 20186 {
e57933dc 20187 sect_offset rnglists_sect_off
b1829e1b
SM
20188 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20189
e57933dc 20190 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 20191 }
dda83cd7 20192 break;
18a8505e
AT
20193 case DW_FORM_strx:
20194 case DW_FORM_strx1:
20195 case DW_FORM_strx2:
20196 case DW_FORM_strx3:
20197 case DW_FORM_strx4:
20198 case DW_FORM_GNU_str_index:
20199 {
fe56917a 20200 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20201 gdb_assert (!attr->canonical_string_p ());
18a8505e 20202 if (reader->dwo_file != NULL)
c6481205
TT
20203 attr->set_string_noncanonical (read_dwo_str_index (reader,
20204 str_index));
18a8505e 20205 else
c6481205
TT
20206 attr->set_string_noncanonical (read_stub_str_index (cu,
20207 str_index));
18a8505e
AT
20208 break;
20209 }
20210 default:
20211 gdb_assert_not_reached (_("Unexpected DWARF form."));
20212 }
20213}
20214
a8329558 20215/* Read an attribute value described by an attribute form. */
c906108c 20216
d521ce57 20217static const gdb_byte *
dee91e82
DE
20218read_attribute_value (const struct die_reader_specs *reader,
20219 struct attribute *attr, unsigned form,
7a5f294d 20220 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20221{
dee91e82 20222 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20223 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20224 struct objfile *objfile = per_objfile->objfile;
dee91e82 20225 bfd *abfd = reader->abfd;
e7c27a73 20226 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20227 unsigned int bytes_read;
20228 struct dwarf_block *blk;
20229
aead7601 20230 attr->form = (enum dwarf_form) form;
a8329558 20231 switch (form)
c906108c 20232 {
c906108c 20233 case DW_FORM_ref_addr:
7c290a04
TT
20234 if (cu_header->version == 2)
20235 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 20236 &bytes_read));
ae411497 20237 else
7c290a04 20238 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20239 &bytes_read));
ae411497
TT
20240 info_ptr += bytes_read;
20241 break;
36586728 20242 case DW_FORM_GNU_ref_alt:
7c290a04 20243 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20244 &bytes_read));
36586728
TT
20245 info_ptr += bytes_read;
20246 break;
ae411497 20247 case DW_FORM_addr:
08feed99
TT
20248 {
20249 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 20250 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
20251 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20252 attr->set_address (addr);
08feed99
TT
20253 info_ptr += bytes_read;
20254 }
c906108c
SS
20255 break;
20256 case DW_FORM_block2:
7b5a2f43 20257 blk = dwarf_alloc_block (cu);
c906108c
SS
20258 blk->size = read_2_bytes (abfd, info_ptr);
20259 info_ptr += 2;
20260 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20261 info_ptr += blk->size;
9d2246fc 20262 attr->set_block (blk);
c906108c
SS
20263 break;
20264 case DW_FORM_block4:
7b5a2f43 20265 blk = dwarf_alloc_block (cu);
c906108c
SS
20266 blk->size = read_4_bytes (abfd, info_ptr);
20267 info_ptr += 4;
20268 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20269 info_ptr += blk->size;
9d2246fc 20270 attr->set_block (blk);
c906108c
SS
20271 break;
20272 case DW_FORM_data2:
414ad644 20273 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20274 info_ptr += 2;
20275 break;
20276 case DW_FORM_data4:
414ad644 20277 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20278 info_ptr += 4;
20279 break;
20280 case DW_FORM_data8:
414ad644 20281 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20282 info_ptr += 8;
20283 break;
0224619f
JK
20284 case DW_FORM_data16:
20285 blk = dwarf_alloc_block (cu);
20286 blk->size = 16;
20287 blk->data = read_n_bytes (abfd, info_ptr, 16);
20288 info_ptr += 16;
9d2246fc 20289 attr->set_block (blk);
0224619f 20290 break;
2dc7f7b3 20291 case DW_FORM_sec_offset:
7c290a04 20292 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20293 &bytes_read));
2dc7f7b3
TT
20294 info_ptr += bytes_read;
20295 break;
41144253 20296 case DW_FORM_loclistx:
20297 {
7a5f294d
TT
20298 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20299 &bytes_read));
20300 info_ptr += bytes_read;
41144253 20301 }
20302 break;
c906108c 20303 case DW_FORM_string:
c6481205
TT
20304 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20305 &bytes_read));
c906108c
SS
20306 info_ptr += bytes_read;
20307 break;
4bdf3d34 20308 case DW_FORM_strp:
36586728
TT
20309 if (!cu->per_cu->is_dwz)
20310 {
c6481205
TT
20311 attr->set_string_noncanonical
20312 (read_indirect_string (per_objfile,
20313 abfd, info_ptr, cu_header,
20314 &bytes_read));
36586728
TT
20315 info_ptr += bytes_read;
20316 break;
20317 }
20318 /* FALLTHROUGH */
43988095
JK
20319 case DW_FORM_line_strp:
20320 if (!cu->per_cu->is_dwz)
20321 {
c6481205
TT
20322 attr->set_string_noncanonical
20323 (per_objfile->read_line_string (info_ptr, cu_header,
20324 &bytes_read));
43988095
JK
20325 info_ptr += bytes_read;
20326 break;
20327 }
20328 /* FALLTHROUGH */
36586728
TT
20329 case DW_FORM_GNU_strp_alt:
20330 {
a7308ce0 20331 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
20332 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20333 &bytes_read);
36586728 20334
c6481205
TT
20335 attr->set_string_noncanonical
20336 (dwz->read_string (objfile, str_offset));
36586728
TT
20337 info_ptr += bytes_read;
20338 }
4bdf3d34 20339 break;
2dc7f7b3 20340 case DW_FORM_exprloc:
c906108c 20341 case DW_FORM_block:
7b5a2f43 20342 blk = dwarf_alloc_block (cu);
c906108c
SS
20343 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20344 info_ptr += bytes_read;
20345 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20346 info_ptr += blk->size;
9d2246fc 20347 attr->set_block (blk);
c906108c
SS
20348 break;
20349 case DW_FORM_block1:
7b5a2f43 20350 blk = dwarf_alloc_block (cu);
c906108c
SS
20351 blk->size = read_1_byte (abfd, info_ptr);
20352 info_ptr += 1;
20353 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20354 info_ptr += blk->size;
9d2246fc 20355 attr->set_block (blk);
c906108c
SS
20356 break;
20357 case DW_FORM_data1:
c906108c 20358 case DW_FORM_flag:
414ad644 20359 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20360 info_ptr += 1;
20361 break;
2dc7f7b3 20362 case DW_FORM_flag_present:
414ad644 20363 attr->set_unsigned (1);
2dc7f7b3 20364 break;
c906108c 20365 case DW_FORM_sdata:
1bc397c5 20366 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20367 info_ptr += bytes_read;
20368 break;
18a8505e 20369 case DW_FORM_rnglistx:
7a5f294d
TT
20370 {
20371 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20372 &bytes_read));
20373 info_ptr += bytes_read;
20374 }
20375 break;
d0ce17d8 20376 case DW_FORM_udata:
414ad644 20377 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20378 info_ptr += bytes_read;
20379 break;
20380 case DW_FORM_ref1:
7c290a04 20381 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20382 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20383 info_ptr += 1;
20384 break;
20385 case DW_FORM_ref2:
7c290a04 20386 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20387 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20388 info_ptr += 2;
20389 break;
20390 case DW_FORM_ref4:
7c290a04 20391 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20392 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20393 info_ptr += 4;
20394 break;
613e1657 20395 case DW_FORM_ref8:
7c290a04 20396 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20397 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20398 info_ptr += 8;
20399 break;
55f1336d 20400 case DW_FORM_ref_sig8:
630ed6b9 20401 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20402 info_ptr += 8;
20403 break;
c906108c 20404 case DW_FORM_ref_udata:
7c290a04 20405 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
20406 + read_unsigned_leb128 (abfd, info_ptr,
20407 &bytes_read)));
c906108c
SS
20408 info_ptr += bytes_read;
20409 break;
c906108c 20410 case DW_FORM_indirect:
a8329558
KW
20411 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20412 info_ptr += bytes_read;
43988095
JK
20413 if (form == DW_FORM_implicit_const)
20414 {
20415 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20416 info_ptr += bytes_read;
20417 }
20418 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20419 info_ptr);
43988095
JK
20420 break;
20421 case DW_FORM_implicit_const:
1bc397c5 20422 attr->set_signed (implicit_const);
a8329558 20423 break;
336d760d 20424 case DW_FORM_addrx:
3019eac3 20425 case DW_FORM_GNU_addr_index:
fe56917a
TT
20426 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20427 &bytes_read));
3019eac3
DE
20428 info_ptr += bytes_read;
20429 break;
cf532bd1 20430 case DW_FORM_strx:
15f18d14
AT
20431 case DW_FORM_strx1:
20432 case DW_FORM_strx2:
20433 case DW_FORM_strx3:
20434 case DW_FORM_strx4:
3019eac3 20435 case DW_FORM_GNU_str_index:
3019eac3 20436 {
15f18d14
AT
20437 ULONGEST str_index;
20438 if (form == DW_FORM_strx1)
20439 {
20440 str_index = read_1_byte (abfd, info_ptr);
20441 info_ptr += 1;
20442 }
20443 else if (form == DW_FORM_strx2)
20444 {
20445 str_index = read_2_bytes (abfd, info_ptr);
20446 info_ptr += 2;
20447 }
20448 else if (form == DW_FORM_strx3)
20449 {
20450 str_index = read_3_bytes (abfd, info_ptr);
20451 info_ptr += 3;
20452 }
20453 else if (form == DW_FORM_strx4)
20454 {
20455 str_index = read_4_bytes (abfd, info_ptr);
20456 info_ptr += 4;
20457 }
20458 else
20459 {
20460 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20461 info_ptr += bytes_read;
20462 }
fe56917a 20463 attr->set_unsigned_reprocess (str_index);
7a5f294d 20464 }
3019eac3 20465 break;
c906108c 20466 default:
8a3fe4f8 20467 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20468 dwarf_form_name (form),
20469 bfd_get_filename (abfd));
c906108c 20470 }
28e94949 20471
36586728 20472 /* Super hack. */
cd6c91b4 20473 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20474 attr->form = DW_FORM_GNU_ref_alt;
20475
28e94949
JB
20476 /* We have seen instances where the compiler tried to emit a byte
20477 size attribute of -1 which ended up being encoded as an unsigned
20478 0xffffffff. Although 0xffffffff is technically a valid size value,
20479 an object of this size seems pretty unlikely so we can relatively
20480 safely treat these cases as if the size attribute was invalid and
20481 treat them as zero by default. */
20482 if (attr->name == DW_AT_byte_size
20483 && form == DW_FORM_data4
529908cb 20484 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20485 {
20486 complaint
dda83cd7
SM
20487 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20488 hex_string (attr->as_unsigned ()));
414ad644 20489 attr->set_unsigned (0);
01c66ae6 20490 }
28e94949 20491
c906108c
SS
20492 return info_ptr;
20493}
20494
a8329558
KW
20495/* Read an attribute described by an abbreviated attribute. */
20496
d521ce57 20497static const gdb_byte *
dee91e82 20498read_attribute (const struct die_reader_specs *reader,
4444f407 20499 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20500 const gdb_byte *info_ptr)
a8329558
KW
20501{
20502 attr->name = abbrev->name;
c6481205 20503 attr->string_is_canonical = 0;
fe56917a 20504 attr->requires_reprocessing = 0;
43988095 20505 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20506 abbrev->implicit_const, info_ptr);
a8329558
KW
20507}
20508
43988095
JK
20509/* Return pointer to string at .debug_str offset STR_OFFSET. */
20510
20511static const char *
976ca316 20512read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20513 LONGEST str_offset)
43988095 20514{
976ca316
SM
20515 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20516 str_offset, "DW_FORM_strp");
c906108c
SS
20517}
20518
43988095
JK
20519/* Return pointer to string at .debug_str offset as read from BUF.
20520 BUF is assumed to be in a compilation unit described by CU_HEADER.
20521 Return *BYTES_READ_PTR count of bytes read from BUF. */
20522
d521ce57 20523static const char *
976ca316 20524read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20525 const gdb_byte *buf,
cf2c3c16
TT
20526 const struct comp_unit_head *cu_header,
20527 unsigned int *bytes_read_ptr)
20528{
8266302d 20529 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20530
976ca316 20531 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20532}
20533
86c0bb4c 20534/* See read.h. */
43988095 20535
86c0bb4c
TT
20536const char *
20537dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20538 const struct comp_unit_head *cu_header,
20539 unsigned int *bytes_read_ptr)
43988095 20540{
86c0bb4c 20541 bfd *abfd = objfile->obfd;
8266302d 20542 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20543
5989a64e 20544 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20545}
20546
3019eac3 20547/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20548 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20549 ADDR_SIZE is the size of addresses from the CU header. */
20550
20551static CORE_ADDR
976ca316
SM
20552read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20553 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20554{
976ca316 20555 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20556 bfd *abfd = objfile->obfd;
20557 const gdb_byte *info_ptr;
18a8505e 20558 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20559
976ca316
SM
20560 per_objfile->per_bfd->addr.read (objfile);
20561 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20562 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20563 objfile_name (objfile));
18a8505e 20564 if (addr_base_or_zero + addr_index * addr_size
976ca316 20565 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20566 error (_("DW_FORM_addr_index pointing outside of "
20567 ".debug_addr section [in module %s]"),
4262abfb 20568 objfile_name (objfile));
976ca316
SM
20569 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20570 + addr_index * addr_size);
3019eac3
DE
20571 if (addr_size == 4)
20572 return bfd_get_32 (abfd, info_ptr);
20573 else
20574 return bfd_get_64 (abfd, info_ptr);
20575}
20576
20577/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20578
20579static CORE_ADDR
20580read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20581{
5e22e966 20582 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20583 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20584}
20585
20586/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20587
20588static CORE_ADDR
d521ce57 20589read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20590 unsigned int *bytes_read)
20591{
5e22e966 20592 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20593 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20594
20595 return read_addr_index (cu, addr_index);
20596}
20597
450a1bfc 20598/* See read.h. */
3019eac3
DE
20599
20600CORE_ADDR
82ca3f51 20601dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20602 dwarf2_per_objfile *per_objfile,
82ca3f51 20603 unsigned int addr_index)
3019eac3 20604{
976ca316 20605 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20606 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20607 int addr_size;
20608
3019eac3
DE
20609 /* We need addr_base and addr_size.
20610 If we don't have PER_CU->cu, we have to get it.
20611 Nasty, but the alternative is storing the needed info in PER_CU,
20612 which at this point doesn't seem justified: it's not clear how frequently
20613 it would get used and it would increase the size of every PER_CU.
20614 Entry points like dwarf2_per_cu_addr_size do a similar thing
20615 so we're not in uncharted territory here.
20616 Alas we need to be a bit more complicated as addr_base is contained
20617 in the DIE.
20618
20619 We don't need to read the entire CU(/TU).
20620 We just need the header and top level die.
a1b64ce1 20621
3019eac3 20622 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20623 For now we skip this optimization. */
3019eac3
DE
20624
20625 if (cu != NULL)
20626 {
20627 addr_base = cu->addr_base;
20628 addr_size = cu->header.addr_size;
20629 }
20630 else
20631 {
976ca316 20632 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20633 addr_base = reader.cu->addr_base;
20634 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20635 }
20636
976ca316 20637 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20638}
20639
18a8505e
AT
20640/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20641 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20642 DWO file. */
3019eac3 20643
d521ce57 20644static const char *
18a8505e
AT
20645read_str_index (struct dwarf2_cu *cu,
20646 struct dwarf2_section_info *str_section,
20647 struct dwarf2_section_info *str_offsets_section,
20648 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20649{
976ca316
SM
20650 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20651 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20652 const char *objf_name = objfile_name (objfile);
3019eac3 20653 bfd *abfd = objfile->obfd;
d521ce57 20654 const gdb_byte *info_ptr;
3019eac3 20655 ULONGEST str_offset;
cf532bd1 20656 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20657
96b79293
TT
20658 str_section->read (objfile);
20659 str_offsets_section->read (objfile);
73869dc2 20660 if (str_section->buffer == NULL)
18a8505e 20661 error (_("%s used without %s section"
9d8780f0 20662 " in CU at offset %s [in module %s]"),
96b79293 20663 form_name, str_section->get_name (),
dda83cd7 20664 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20665 if (str_offsets_section->buffer == NULL)
18a8505e 20666 error (_("%s used without %s section"
9d8780f0 20667 " in CU at offset %s [in module %s]"),
96b79293 20668 form_name, str_section->get_name (),
dda83cd7 20669 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20670 info_ptr = (str_offsets_section->buffer
18a8505e 20671 + str_offsets_base
3019eac3
DE
20672 + str_index * cu->header.offset_size);
20673 if (cu->header.offset_size == 4)
20674 str_offset = bfd_get_32 (abfd, info_ptr);
20675 else
20676 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20677 if (str_offset >= str_section->size)
57d63ce2 20678 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20679 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20680 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20681 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20682}
20683
18a8505e
AT
20684/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20685
20686static const char *
20687read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20688{
20689 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20690 ? reader->cu->header.addr_size : 0;
20691 return read_str_index (reader->cu,
20692 &reader->dwo_file->sections.str,
20693 &reader->dwo_file->sections.str_offsets,
20694 str_offsets_base, str_index);
20695}
20696
20697/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20698
20699static const char *
20700read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20701{
5e22e966 20702 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20703 const char *objf_name = objfile_name (objfile);
20704 static const char form_name[] = "DW_FORM_GNU_str_index";
20705 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20706
20707 if (!cu->str_offsets_base.has_value ())
20708 error (_("%s used in Fission stub without %s"
20709 " in CU at offset 0x%lx [in module %s]"),
20710 form_name, str_offsets_attr_name,
20711 (long) cu->header.offset_size, objf_name);
20712
20713 return read_str_index (cu,
5e22e966
SM
20714 &cu->per_objfile->per_bfd->str,
20715 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20716 *cu->str_offsets_base, str_index);
20717}
20718
3019eac3
DE
20719/* Return the length of an LEB128 number in BUF. */
20720
20721static int
20722leb128_size (const gdb_byte *buf)
20723{
20724 const gdb_byte *begin = buf;
20725 gdb_byte byte;
20726
20727 while (1)
20728 {
20729 byte = *buf++;
20730 if ((byte & 128) == 0)
20731 return buf - begin;
20732 }
20733}
20734
c906108c 20735static void
e142c38c 20736set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20737{
20738 switch (lang)
20739 {
20740 case DW_LANG_C89:
76bee0cc 20741 case DW_LANG_C99:
0cfd832f 20742 case DW_LANG_C11:
c906108c 20743 case DW_LANG_C:
d1be3247 20744 case DW_LANG_UPC:
e142c38c 20745 cu->language = language_c;
c906108c 20746 break;
9c37b5ae 20747 case DW_LANG_Java:
c906108c 20748 case DW_LANG_C_plus_plus:
0cfd832f
MW
20749 case DW_LANG_C_plus_plus_11:
20750 case DW_LANG_C_plus_plus_14:
e142c38c 20751 cu->language = language_cplus;
c906108c 20752 break;
6aecb9c2
JB
20753 case DW_LANG_D:
20754 cu->language = language_d;
20755 break;
c906108c
SS
20756 case DW_LANG_Fortran77:
20757 case DW_LANG_Fortran90:
b21b22e0 20758 case DW_LANG_Fortran95:
f7de9aab
MW
20759 case DW_LANG_Fortran03:
20760 case DW_LANG_Fortran08:
e142c38c 20761 cu->language = language_fortran;
c906108c 20762 break;
a766d390
DE
20763 case DW_LANG_Go:
20764 cu->language = language_go;
20765 break;
c906108c 20766 case DW_LANG_Mips_Assembler:
e142c38c 20767 cu->language = language_asm;
c906108c
SS
20768 break;
20769 case DW_LANG_Ada83:
8aaf0b47 20770 case DW_LANG_Ada95:
bc5f45f8
JB
20771 cu->language = language_ada;
20772 break;
72019c9c
GM
20773 case DW_LANG_Modula2:
20774 cu->language = language_m2;
20775 break;
fe8e67fd
PM
20776 case DW_LANG_Pascal83:
20777 cu->language = language_pascal;
20778 break;
22566fbd
DJ
20779 case DW_LANG_ObjC:
20780 cu->language = language_objc;
20781 break;
c44af4eb
TT
20782 case DW_LANG_Rust:
20783 case DW_LANG_Rust_old:
20784 cu->language = language_rust;
20785 break;
c906108c
SS
20786 case DW_LANG_Cobol74:
20787 case DW_LANG_Cobol85:
c906108c 20788 default:
e142c38c 20789 cu->language = language_minimal;
c906108c
SS
20790 break;
20791 }
e142c38c 20792 cu->language_defn = language_def (cu->language);
c906108c
SS
20793}
20794
20795/* Return the named attribute or NULL if not there. */
20796
20797static struct attribute *
e142c38c 20798dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20799{
a48e046c 20800 for (;;)
c906108c 20801 {
a48e046c
TT
20802 unsigned int i;
20803 struct attribute *spec = NULL;
20804
20805 for (i = 0; i < die->num_attrs; ++i)
20806 {
20807 if (die->attrs[i].name == name)
20808 return &die->attrs[i];
20809 if (die->attrs[i].name == DW_AT_specification
20810 || die->attrs[i].name == DW_AT_abstract_origin)
20811 spec = &die->attrs[i];
20812 }
20813
20814 if (!spec)
20815 break;
c906108c 20816
f2f0e013 20817 die = follow_die_ref (die, spec, &cu);
f2f0e013 20818 }
c5aa993b 20819
c906108c
SS
20820 return NULL;
20821}
20822
7d45c7c3
KB
20823/* Return the string associated with a string-typed attribute, or NULL if it
20824 is either not found or is of an incorrect type. */
20825
20826static const char *
20827dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20828{
20829 struct attribute *attr;
20830 const char *str = NULL;
20831
20832 attr = dwarf2_attr (die, name, cu);
20833
20834 if (attr != NULL)
20835 {
95f982e5 20836 str = attr->as_string ();
e61108c9 20837 if (str == nullptr)
dda83cd7 20838 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20839 "DIE at %s in module %s"),
20840 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20841 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20842 }
20843
20844 return str;
20845}
20846
a084a2a6 20847/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20848 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20849static const char *
20850dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20851{
20852 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20853 if (dwo_name == nullptr)
20854 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20855 return dwo_name;
20856}
20857
05cf31d1
JB
20858/* Return non-zero iff the attribute NAME is defined for the given DIE,
20859 and holds a non-zero value. This function should only be used for
2dc7f7b3 20860 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20861
20862static int
20863dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20864{
20865 struct attribute *attr = dwarf2_attr (die, name, cu);
20866
c45bc3f8 20867 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
20868}
20869
3ca72b44 20870static int
e142c38c 20871die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20872{
05cf31d1
JB
20873 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20874 which value is non-zero. However, we have to be careful with
20875 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20876 (via dwarf2_flag_true_p) follows this attribute. So we may
20877 end up accidently finding a declaration attribute that belongs
20878 to a different DIE referenced by the specification attribute,
20879 even though the given DIE does not have a declaration attribute. */
20880 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20881 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20882}
20883
63d06c5c 20884/* Return the die giving the specification for DIE, if there is
f2f0e013 20885 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20886 containing the return value on output. If there is no
20887 specification, but there is an abstract origin, that is
20888 returned. */
63d06c5c
DC
20889
20890static struct die_info *
f2f0e013 20891die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20892{
f2f0e013
DJ
20893 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20894 *spec_cu);
63d06c5c 20895
edb3359d
DJ
20896 if (spec_attr == NULL)
20897 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20898
63d06c5c
DC
20899 if (spec_attr == NULL)
20900 return NULL;
20901 else
f2f0e013 20902 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20903}
c906108c 20904
527f3840
JK
20905/* Stub for free_line_header to match void * callback types. */
20906
20907static void
20908free_line_header_voidp (void *arg)
20909{
9a3c8263 20910 struct line_header *lh = (struct line_header *) arg;
527f3840 20911
fff8551c 20912 delete lh;
527f3840
JK
20913}
20914
83769d0b 20915/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20916
20917static struct dwarf2_section_info *
20918get_debug_line_section (struct dwarf2_cu *cu)
20919{
20920 struct dwarf2_section_info *section;
976ca316 20921 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
20922
20923 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20924 DWO file. */
20925 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20926 section = &cu->dwo_unit->dwo_file->sections.line;
20927 else if (cu->per_cu->is_dwz)
20928 {
a7308ce0 20929 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
20930
20931 section = &dwz->line;
20932 }
20933 else
976ca316 20934 section = &per_objfile->per_bfd->line;
36586728
TT
20935
20936 return section;
20937}
20938
debd256d 20939/* Read the statement program header starting at OFFSET in
3019eac3 20940 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20941 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20942 Returns NULL if there is a problem reading the header, e.g., if it
20943 has a version we don't understand.
debd256d
JB
20944
20945 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20946 the returned object point into the dwarf line section buffer,
20947 and must not be freed. */
ae2de4f8 20948
fff8551c 20949static line_header_up
9c541725 20950dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20951{
3019eac3 20952 struct dwarf2_section_info *section;
976ca316 20953 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 20954
36586728 20955 section = get_debug_line_section (cu);
976ca316 20956 section->read (per_objfile->objfile);
3019eac3 20957 if (section->buffer == NULL)
debd256d 20958 {
3019eac3 20959 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20960 complaint (_("missing .debug_line.dwo section"));
3019eac3 20961 else
b98664d3 20962 complaint (_("missing .debug_line section"));
debd256d
JB
20963 return 0;
20964 }
20965
0df7ad3a 20966 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 20967 per_objfile, section, &cu->header);
debd256d 20968}
c906108c 20969
c6da4cef 20970/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20971 Return the file name of the psymtab for the given file_entry.
c6da4cef 20972 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20973 If space for the result is malloc'd, *NAME_HOLDER will be set.
20974 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20975
d521ce57 20976static const char *
7ba99d21 20977psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20978 const dwarf2_psymtab *pst,
c89b44cd
TT
20979 const char *comp_dir,
20980 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20981{
d521ce57
TT
20982 const char *include_name = fe.name;
20983 const char *include_name_to_compare = include_name;
72b9f47f 20984 const char *pst_filename;
c6da4cef
DE
20985 int file_is_pst;
20986
8c43009f 20987 const char *dir_name = fe.include_dir (lh);
c6da4cef 20988
c89b44cd 20989 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20990 if (!IS_ABSOLUTE_PATH (include_name)
20991 && (dir_name != NULL || comp_dir != NULL))
20992 {
20993 /* Avoid creating a duplicate psymtab for PST.
20994 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20995 Before we do the comparison, however, we need to account
20996 for DIR_NAME and COMP_DIR.
20997 First prepend dir_name (if non-NULL). If we still don't
20998 have an absolute path prepend comp_dir (if non-NULL).
20999 However, the directory we record in the include-file's
21000 psymtab does not contain COMP_DIR (to match the
21001 corresponding symtab(s)).
21002
21003 Example:
21004
21005 bash$ cd /tmp
21006 bash$ gcc -g ./hello.c
21007 include_name = "hello.c"
21008 dir_name = "."
21009 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
21010 DW_AT_name = "./hello.c"
21011
21012 */
c6da4cef
DE
21013
21014 if (dir_name != NULL)
21015 {
c89b44cd
TT
21016 name_holder->reset (concat (dir_name, SLASH_STRING,
21017 include_name, (char *) NULL));
21018 include_name = name_holder->get ();
c6da4cef 21019 include_name_to_compare = include_name;
c6da4cef
DE
21020 }
21021 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21022 {
c89b44cd
TT
21023 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21024 include_name, (char *) NULL));
21025 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
21026 }
21027 }
21028
21029 pst_filename = pst->filename;
c89b44cd 21030 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
21031 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21032 {
c89b44cd
TT
21033 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21034 pst_filename, (char *) NULL));
21035 pst_filename = copied_name.get ();
c6da4cef
DE
21036 }
21037
1e3fad37 21038 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 21039
c6da4cef
DE
21040 if (file_is_pst)
21041 return NULL;
21042 return include_name;
21043}
21044
d9b3de22
DE
21045/* State machine to track the state of the line number program. */
21046
6f77053d 21047class lnp_state_machine
d9b3de22 21048{
6f77053d
PA
21049public:
21050 /* Initialize a machine state for the start of a line number
21051 program. */
804d2729
TT
21052 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21053 bool record_lines_p);
6f77053d 21054
8c43009f
PA
21055 file_entry *current_file ()
21056 {
21057 /* lh->file_names is 0-based, but the file name numbers in the
21058 statement program are 1-based. */
6f77053d
PA
21059 return m_line_header->file_name_at (m_file);
21060 }
21061
21062 /* Record the line in the state machine. END_SEQUENCE is true if
21063 we're processing the end of a sequence. */
21064 void record_line (bool end_sequence);
21065
a8caed5d 21066 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 21067 nop-out rest of the lines in this sequence. */
6f77053d
PA
21068 void check_line_address (struct dwarf2_cu *cu,
21069 const gdb_byte *line_ptr,
7ab6656f 21070 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21071
21072 void handle_set_discriminator (unsigned int discriminator)
21073 {
21074 m_discriminator = discriminator;
21075 m_line_has_non_zero_discriminator |= discriminator != 0;
21076 }
21077
21078 /* Handle DW_LNE_set_address. */
21079 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21080 {
21081 m_op_index = 0;
21082 address += baseaddr;
21083 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21084 }
21085
21086 /* Handle DW_LNS_advance_pc. */
21087 void handle_advance_pc (CORE_ADDR adjust);
21088
21089 /* Handle a special opcode. */
21090 void handle_special_opcode (unsigned char op_code);
21091
21092 /* Handle DW_LNS_advance_line. */
21093 void handle_advance_line (int line_delta)
21094 {
21095 advance_line (line_delta);
21096 }
21097
21098 /* Handle DW_LNS_set_file. */
21099 void handle_set_file (file_name_index file);
21100
21101 /* Handle DW_LNS_negate_stmt. */
21102 void handle_negate_stmt ()
21103 {
21104 m_is_stmt = !m_is_stmt;
21105 }
21106
21107 /* Handle DW_LNS_const_add_pc. */
21108 void handle_const_add_pc ();
21109
21110 /* Handle DW_LNS_fixed_advance_pc. */
21111 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21112 {
21113 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21114 m_op_index = 0;
21115 }
21116
21117 /* Handle DW_LNS_copy. */
21118 void handle_copy ()
21119 {
21120 record_line (false);
21121 m_discriminator = 0;
21122 }
21123
21124 /* Handle DW_LNE_end_sequence. */
21125 void handle_end_sequence ()
21126 {
804d2729 21127 m_currently_recording_lines = true;
6f77053d
PA
21128 }
21129
21130private:
21131 /* Advance the line by LINE_DELTA. */
21132 void advance_line (int line_delta)
21133 {
21134 m_line += line_delta;
21135
21136 if (line_delta != 0)
21137 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21138 }
21139
804d2729
TT
21140 struct dwarf2_cu *m_cu;
21141
6f77053d
PA
21142 gdbarch *m_gdbarch;
21143
21144 /* True if we're recording lines.
21145 Otherwise we're building partial symtabs and are just interested in
21146 finding include files mentioned by the line number program. */
21147 bool m_record_lines_p;
21148
8c43009f 21149 /* The line number header. */
6f77053d 21150 line_header *m_line_header;
8c43009f 21151
6f77053d
PA
21152 /* These are part of the standard DWARF line number state machine,
21153 and initialized according to the DWARF spec. */
d9b3de22 21154
6f77053d 21155 unsigned char m_op_index = 0;
7ba99d21
AT
21156 /* The line table index of the current file. */
21157 file_name_index m_file = 1;
6f77053d
PA
21158 unsigned int m_line = 1;
21159
21160 /* These are initialized in the constructor. */
21161
21162 CORE_ADDR m_address;
21163 bool m_is_stmt;
21164 unsigned int m_discriminator;
d9b3de22
DE
21165
21166 /* Additional bits of state we need to track. */
21167
21168 /* The last file that we called dwarf2_start_subfile for.
21169 This is only used for TLLs. */
6f77053d 21170 unsigned int m_last_file = 0;
d9b3de22 21171 /* The last file a line number was recorded for. */
6f77053d 21172 struct subfile *m_last_subfile = NULL;
d9b3de22 21173
1313c56e
AB
21174 /* The address of the last line entry. */
21175 CORE_ADDR m_last_address;
21176
21177 /* Set to true when a previous line at the same address (using
21178 m_last_address) had m_is_stmt true. This is reset to false when a
21179 line entry at a new address (m_address different to m_last_address) is
21180 processed. */
21181 bool m_stmt_at_address = false;
21182
804d2729
TT
21183 /* When true, record the lines we decode. */
21184 bool m_currently_recording_lines = false;
d9b3de22
DE
21185
21186 /* The last line number that was recorded, used to coalesce
21187 consecutive entries for the same line. This can happen, for
21188 example, when discriminators are present. PR 17276. */
6f77053d
PA
21189 unsigned int m_last_line = 0;
21190 bool m_line_has_non_zero_discriminator = false;
8c43009f 21191};
d9b3de22 21192
6f77053d
PA
21193void
21194lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21195{
21196 CORE_ADDR addr_adj = (((m_op_index + adjust)
21197 / m_line_header->maximum_ops_per_instruction)
21198 * m_line_header->minimum_instruction_length);
21199 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21200 m_op_index = ((m_op_index + adjust)
21201 % m_line_header->maximum_ops_per_instruction);
21202}
d9b3de22 21203
6f77053d
PA
21204void
21205lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21206{
6f77053d 21207 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21208 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21209 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21210 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21211 / m_line_header->maximum_ops_per_instruction)
21212 * m_line_header->minimum_instruction_length);
21213 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21214 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21215 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21216
258bf0ee 21217 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21218 advance_line (line_delta);
21219 record_line (false);
21220 m_discriminator = 0;
21221}
d9b3de22 21222
6f77053d
PA
21223void
21224lnp_state_machine::handle_set_file (file_name_index file)
21225{
21226 m_file = file;
21227
21228 const file_entry *fe = current_file ();
21229 if (fe == NULL)
21230 dwarf2_debug_line_missing_file_complaint ();
21231 else if (m_record_lines_p)
21232 {
21233 const char *dir = fe->include_dir (m_line_header);
21234
c24bdb02 21235 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21236 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21237 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21238 }
21239}
21240
21241void
21242lnp_state_machine::handle_const_add_pc ()
21243{
21244 CORE_ADDR adjust
21245 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21246
21247 CORE_ADDR addr_adj
21248 = (((m_op_index + adjust)
21249 / m_line_header->maximum_ops_per_instruction)
21250 * m_line_header->minimum_instruction_length);
21251
21252 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21253 m_op_index = ((m_op_index + adjust)
21254 % m_line_header->maximum_ops_per_instruction);
21255}
d9b3de22 21256
a05a36a5
DE
21257/* Return non-zero if we should add LINE to the line number table.
21258 LINE is the line to add, LAST_LINE is the last line that was added,
21259 LAST_SUBFILE is the subfile for LAST_LINE.
21260 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21261 had a non-zero discriminator.
21262
21263 We have to be careful in the presence of discriminators.
21264 E.g., for this line:
21265
21266 for (i = 0; i < 100000; i++);
21267
21268 clang can emit four line number entries for that one line,
21269 each with a different discriminator.
21270 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21271
21272 However, we want gdb to coalesce all four entries into one.
21273 Otherwise the user could stepi into the middle of the line and
21274 gdb would get confused about whether the pc really was in the
21275 middle of the line.
21276
21277 Things are further complicated by the fact that two consecutive
21278 line number entries for the same line is a heuristic used by gcc
21279 to denote the end of the prologue. So we can't just discard duplicate
21280 entries, we have to be selective about it. The heuristic we use is
21281 that we only collapse consecutive entries for the same line if at least
21282 one of those entries has a non-zero discriminator. PR 17276.
21283
21284 Note: Addresses in the line number state machine can never go backwards
21285 within one sequence, thus this coalescing is ok. */
21286
21287static int
804d2729
TT
21288dwarf_record_line_p (struct dwarf2_cu *cu,
21289 unsigned int line, unsigned int last_line,
a05a36a5
DE
21290 int line_has_non_zero_discriminator,
21291 struct subfile *last_subfile)
21292{
c24bdb02 21293 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21294 return 1;
21295 if (line != last_line)
21296 return 1;
21297 /* Same line for the same file that we've seen already.
21298 As a last check, for pr 17276, only record the line if the line
21299 has never had a non-zero discriminator. */
21300 if (!line_has_non_zero_discriminator)
21301 return 1;
21302 return 0;
21303}
21304
804d2729
TT
21305/* Use the CU's builder to record line number LINE beginning at
21306 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21307
21308static void
d9b3de22 21309dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21310 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21311 struct dwarf2_cu *cu)
252a6764
DE
21312{
21313 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21314
27e0867f
DE
21315 if (dwarf_line_debug)
21316 {
21317 fprintf_unfiltered (gdb_stdlog,
21318 "Recording line %u, file %s, address %s\n",
21319 line, lbasename (subfile->name),
21320 paddress (gdbarch, address));
21321 }
21322
804d2729 21323 if (cu != nullptr)
8c95582d 21324 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21325}
21326
21327/* Subroutine of dwarf_decode_lines_1 to simplify it.
21328 Mark the end of a set of line number records.
d9b3de22 21329 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21330 If SUBFILE is NULL the request is ignored. */
21331
21332static void
21333dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21334 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21335{
27e0867f
DE
21336 if (subfile == NULL)
21337 return;
21338
21339 if (dwarf_line_debug)
21340 {
21341 fprintf_unfiltered (gdb_stdlog,
21342 "Finishing current line, file %s, address %s\n",
21343 lbasename (subfile->name),
21344 paddress (gdbarch, address));
21345 }
21346
8c95582d 21347 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21348}
21349
6f77053d
PA
21350void
21351lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21352{
d9b3de22
DE
21353 if (dwarf_line_debug)
21354 {
21355 fprintf_unfiltered (gdb_stdlog,
21356 "Processing actual line %u: file %u,"
94a72be7 21357 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21358 m_line, m_file,
6f77053d 21359 paddress (m_gdbarch, m_address),
94a72be7
AB
21360 m_is_stmt, m_discriminator,
21361 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21362 }
21363
6f77053d 21364 file_entry *fe = current_file ();
8c43009f
PA
21365
21366 if (fe == NULL)
d9b3de22
DE
21367 dwarf2_debug_line_missing_file_complaint ();
21368 /* For now we ignore lines not starting on an instruction boundary.
21369 But not when processing end_sequence for compatibility with the
21370 previous version of the code. */
6f77053d 21371 else if (m_op_index == 0 || end_sequence)
d9b3de22 21372 {
8c43009f 21373 fe->included_p = 1;
8c95582d 21374 if (m_record_lines_p)
d9b3de22 21375 {
1313c56e
AB
21376 /* When we switch files we insert an end maker in the first file,
21377 switch to the second file and add a new line entry. The
21378 problem is that the end marker inserted in the first file will
21379 discard any previous line entries at the same address. If the
21380 line entries in the first file are marked as is-stmt, while
21381 the new line in the second file is non-stmt, then this means
21382 the end marker will discard is-stmt lines so we can have a
21383 non-stmt line. This means that there are less addresses at
21384 which the user can insert a breakpoint.
21385
21386 To improve this we track the last address in m_last_address,
21387 and whether we have seen an is-stmt at this address. Then
21388 when switching files, if we have seen a stmt at the current
21389 address, and we are switching to create a non-stmt line, then
21390 discard the new line. */
21391 bool file_changed
21392 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21393 bool ignore_this_line
dda83cd7
SM
21394 = ((file_changed && !end_sequence && m_last_address == m_address
21395 && !m_is_stmt && m_stmt_at_address)
21396 || (!end_sequence && m_line == 0));
1313c56e
AB
21397
21398 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21399 {
804d2729
TT
21400 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21401 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21402 }
21403
1313c56e 21404 if (!end_sequence && !ignore_this_line)
d9b3de22 21405 {
8c95582d
AB
21406 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21407
804d2729 21408 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21409 m_line_has_non_zero_discriminator,
21410 m_last_subfile))
d9b3de22 21411 {
c24bdb02 21412 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21413 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21414 builder->get_current_subfile (),
8c95582d 21415 m_line, m_address, is_stmt,
804d2729 21416 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21417 }
c24bdb02 21418 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21419 m_last_line = m_line;
d9b3de22
DE
21420 }
21421 }
21422 }
1313c56e
AB
21423
21424 /* Track whether we have seen any m_is_stmt true at m_address in case we
21425 have multiple line table entries all at m_address. */
21426 if (m_last_address != m_address)
21427 {
21428 m_stmt_at_address = false;
21429 m_last_address = m_address;
21430 }
21431 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21432}
21433
804d2729
TT
21434lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21435 line_header *lh, bool record_lines_p)
d9b3de22 21436{
804d2729 21437 m_cu = cu;
6f77053d
PA
21438 m_gdbarch = arch;
21439 m_record_lines_p = record_lines_p;
21440 m_line_header = lh;
d9b3de22 21441
804d2729 21442 m_currently_recording_lines = true;
d9b3de22 21443
d9b3de22
DE
21444 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21445 was a line entry for it so that the backend has a chance to adjust it
21446 and also record it in case it needs it. This is currently used by MIPS
21447 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21448 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21449 m_is_stmt = lh->default_is_stmt;
21450 m_discriminator = 0;
1313c56e
AB
21451
21452 m_last_address = m_address;
21453 m_stmt_at_address = false;
252a6764
DE
21454}
21455
6f77053d
PA
21456void
21457lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21458 const gdb_byte *line_ptr,
7ab6656f 21459 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21460{
a8caed5d
FS
21461 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21462 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21463 located at 0x0. In this case, additionally check that if
21464 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21465
a8caed5d
FS
21466 if ((address == 0 && address < unrelocated_lowpc)
21467 || address == (CORE_ADDR) -1)
924c2928
DE
21468 {
21469 /* This line table is for a function which has been
21470 GCd by the linker. Ignore it. PR gdb/12528 */
21471
5e22e966 21472 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21473 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21474
b98664d3 21475 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21476 line_offset, objfile_name (objfile));
804d2729
TT
21477 m_currently_recording_lines = false;
21478 /* Note: m_currently_recording_lines is left as false until we see
21479 DW_LNE_end_sequence. */
924c2928
DE
21480 }
21481}
21482
f3f5162e 21483/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21484 Process the line number information in LH.
21485 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21486 program in order to set included_p for every referenced header. */
debd256d 21487
c906108c 21488static void
43f3e411
DE
21489dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21490 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21491{
d521ce57
TT
21492 const gdb_byte *line_ptr, *extended_end;
21493 const gdb_byte *line_end;
a8c50c1f 21494 unsigned int bytes_read, extended_len;
699ca60a 21495 unsigned char op_code, extended_op;
e142c38c 21496 CORE_ADDR baseaddr;
5e22e966 21497 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21498 bfd *abfd = objfile->obfd;
08feed99 21499 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21500 /* True if we're recording line info (as opposed to building partial
21501 symtabs and just interested in finding include files mentioned by
21502 the line number program). */
21503 bool record_lines_p = !decode_for_pst_p;
e142c38c 21504
b3b3bada 21505 baseaddr = objfile->text_section_offset ();
c906108c 21506
debd256d
JB
21507 line_ptr = lh->statement_program_start;
21508 line_end = lh->statement_program_end;
c906108c
SS
21509
21510 /* Read the statement sequences until there's nothing left. */
21511 while (line_ptr < line_end)
21512 {
6f77053d
PA
21513 /* The DWARF line number program state machine. Reset the state
21514 machine at the start of each sequence. */
804d2729 21515 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21516 bool end_sequence = false;
d9b3de22 21517
8c43009f 21518 if (record_lines_p)
c906108c 21519 {
8c43009f
PA
21520 /* Start a subfile for the current file of the state
21521 machine. */
21522 const file_entry *fe = state_machine.current_file ();
21523
21524 if (fe != NULL)
804d2729 21525 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21526 }
21527
a738430d 21528 /* Decode the table. */
d9b3de22 21529 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21530 {
21531 op_code = read_1_byte (abfd, line_ptr);
21532 line_ptr += 1;
9aa1fe7e 21533
debd256d 21534 if (op_code >= lh->opcode_base)
6e70227d 21535 {
8e07a239 21536 /* Special opcode. */
6f77053d 21537 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21538 }
21539 else switch (op_code)
c906108c
SS
21540 {
21541 case DW_LNS_extended_op:
3e43a32a
MS
21542 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21543 &bytes_read);
473b7be6 21544 line_ptr += bytes_read;
a8c50c1f 21545 extended_end = line_ptr + extended_len;
c906108c
SS
21546 extended_op = read_1_byte (abfd, line_ptr);
21547 line_ptr += 1;
8f34b746
TV
21548 if (DW_LNE_lo_user <= extended_op
21549 && extended_op <= DW_LNE_hi_user)
21550 {
21551 /* Vendor extension, ignore. */
21552 line_ptr = extended_end;
21553 break;
21554 }
c906108c
SS
21555 switch (extended_op)
21556 {
21557 case DW_LNE_end_sequence:
6f77053d
PA
21558 state_machine.handle_end_sequence ();
21559 end_sequence = true;
c906108c
SS
21560 break;
21561 case DW_LNE_set_address:
d9b3de22
DE
21562 {
21563 CORE_ADDR address
c8a7a66f 21564 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21565 line_ptr += bytes_read;
6f77053d
PA
21566
21567 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21568 lowpc - baseaddr, address);
6f77053d 21569 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21570 }
c906108c
SS
21571 break;
21572 case DW_LNE_define_file:
dda83cd7
SM
21573 {
21574 const char *cur_file;
ecfb656c
PA
21575 unsigned int mod_time, length;
21576 dir_index dindex;
6e70227d 21577
dda83cd7 21578 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21579 &bytes_read);
dda83cd7
SM
21580 line_ptr += bytes_read;
21581 dindex = (dir_index)
21582 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21583 line_ptr += bytes_read;
21584 mod_time =
21585 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21586 line_ptr += bytes_read;
21587 length =
21588 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21589 line_ptr += bytes_read;
21590 lh->add_file_name (cur_file, dindex, mod_time, length);
21591 }
c906108c 21592 break;
d0c6ba3d 21593 case DW_LNE_set_discriminator:
6f77053d
PA
21594 {
21595 /* The discriminator is not interesting to the
21596 debugger; just ignore it. We still need to
21597 check its value though:
21598 if there are consecutive entries for the same
21599 (non-prologue) line we want to coalesce them.
21600 PR 17276. */
21601 unsigned int discr
21602 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21603 line_ptr += bytes_read;
21604
21605 state_machine.handle_set_discriminator (discr);
21606 }
d0c6ba3d 21607 break;
c906108c 21608 default:
b98664d3 21609 complaint (_("mangled .debug_line section"));
debd256d 21610 return;
c906108c 21611 }
a8c50c1f
DJ
21612 /* Make sure that we parsed the extended op correctly. If e.g.
21613 we expected a different address size than the producer used,
21614 we may have read the wrong number of bytes. */
21615 if (line_ptr != extended_end)
21616 {
b98664d3 21617 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21618 return;
21619 }
c906108c
SS
21620 break;
21621 case DW_LNS_copy:
6f77053d 21622 state_machine.handle_copy ();
c906108c
SS
21623 break;
21624 case DW_LNS_advance_pc:
2dc7f7b3
TT
21625 {
21626 CORE_ADDR adjust
21627 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21628 line_ptr += bytes_read;
6f77053d
PA
21629
21630 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21631 }
c906108c
SS
21632 break;
21633 case DW_LNS_advance_line:
a05a36a5
DE
21634 {
21635 int line_delta
21636 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21637 line_ptr += bytes_read;
6f77053d
PA
21638
21639 state_machine.handle_advance_line (line_delta);
a05a36a5 21640 }
c906108c
SS
21641 break;
21642 case DW_LNS_set_file:
d9b3de22 21643 {
6f77053d 21644 file_name_index file
ecfb656c
PA
21645 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21646 &bytes_read);
d9b3de22 21647 line_ptr += bytes_read;
8c43009f 21648
6f77053d 21649 state_machine.handle_set_file (file);
d9b3de22 21650 }
c906108c
SS
21651 break;
21652 case DW_LNS_set_column:
0ad93d4f 21653 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21654 line_ptr += bytes_read;
21655 break;
21656 case DW_LNS_negate_stmt:
6f77053d 21657 state_machine.handle_negate_stmt ();
c906108c
SS
21658 break;
21659 case DW_LNS_set_basic_block:
c906108c 21660 break;
c2c6d25f
JM
21661 /* Add to the address register of the state machine the
21662 address increment value corresponding to special opcode
a738430d
MK
21663 255. I.e., this value is scaled by the minimum
21664 instruction length since special opcode 255 would have
b021a221 21665 scaled the increment. */
c906108c 21666 case DW_LNS_const_add_pc:
6f77053d 21667 state_machine.handle_const_add_pc ();
c906108c
SS
21668 break;
21669 case DW_LNS_fixed_advance_pc:
3e29f34a 21670 {
6f77053d 21671 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21672 line_ptr += 2;
6f77053d
PA
21673
21674 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21675 }
c906108c 21676 break;
9aa1fe7e 21677 default:
a738430d
MK
21678 {
21679 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21680 int i;
a738430d 21681
debd256d 21682 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21683 {
21684 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21685 line_ptr += bytes_read;
21686 }
21687 }
c906108c
SS
21688 }
21689 }
d9b3de22
DE
21690
21691 if (!end_sequence)
21692 dwarf2_debug_line_missing_end_sequence_complaint ();
21693
21694 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21695 in which case we still finish recording the last line). */
6f77053d 21696 state_machine.record_line (true);
c906108c 21697 }
f3f5162e
DE
21698}
21699
21700/* Decode the Line Number Program (LNP) for the given line_header
21701 structure and CU. The actual information extracted and the type
21702 of structures created from the LNP depends on the value of PST.
21703
21704 1. If PST is NULL, then this procedure uses the data from the program
21705 to create all necessary symbol tables, and their linetables.
21706
21707 2. If PST is not NULL, this procedure reads the program to determine
21708 the list of files included by the unit represented by PST, and
21709 builds all the associated partial symbol tables.
21710
21711 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21712 It is used for relative paths in the line table.
21713 NOTE: When processing partial symtabs (pst != NULL),
21714 comp_dir == pst->dirname.
21715
21716 NOTE: It is important that psymtabs have the same file name (via strcmp)
21717 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21718 symtab we don't use it in the name of the psymtabs we create.
21719 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21720 A good testcase for this is mb-inline.exp.
21721
527f3840
JK
21722 LOWPC is the lowest address in CU (or 0 if not known).
21723
21724 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21725 for its PC<->lines mapping information. Otherwise only the filename
21726 table is read in. */
f3f5162e
DE
21727
21728static void
21729dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21730 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21731 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21732{
5e22e966 21733 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21734 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21735
527f3840
JK
21736 if (decode_mapping)
21737 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21738
21739 if (decode_for_pst_p)
21740 {
aaa75496 21741 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21742 create the psymtab of each included file. */
7ba99d21 21743 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21744 if (file_entry.included_p == 1)
21745 {
c89b44cd 21746 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21747 const char *include_name =
7ba99d21
AT
21748 psymtab_include_file_name (lh, file_entry, pst,
21749 comp_dir, &name_holder);
c6da4cef 21750 if (include_name != NULL)
7b249e47
TT
21751 dwarf2_create_include_psymtab
21752 (cu->per_objfile->per_bfd, include_name, pst,
21753 cu->per_objfile->per_bfd->partial_symtabs.get (),
0072c873 21754 objfile->per_bfd);
dda83cd7 21755 }
aaa75496 21756 }
cb1df416
DJ
21757 else
21758 {
21759 /* Make sure a symtab is created for every file, even files
21760 which contain only variables (i.e. no code with associated
21761 line numbers). */
c24bdb02
KS
21762 buildsym_compunit *builder = cu->get_builder ();
21763 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21764
7ba99d21 21765 for (auto &fe : lh->file_names ())
cb1df416 21766 {
804d2729 21767 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21768 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21769 {
c24bdb02 21770 builder->get_current_subfile ()->symtab
804d2729 21771 = allocate_symtab (cust,
c24bdb02 21772 builder->get_current_subfile ()->name);
43f3e411 21773 }
c24bdb02 21774 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21775 }
21776 }
c906108c
SS
21777}
21778
21779/* Start a subfile for DWARF. FILENAME is the name of the file and
21780 DIRNAME the name of the source directory which contains FILENAME
4d663531 21781 or NULL if not known.
c906108c
SS
21782 This routine tries to keep line numbers from identical absolute and
21783 relative file names in a common subfile.
21784
21785 Using the `list' example from the GDB testsuite, which resides in
21786 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21787 of /srcdir/list0.c yields the following debugging information for list0.c:
21788
c5aa993b 21789 DW_AT_name: /srcdir/list0.c
4d663531 21790 DW_AT_comp_dir: /compdir
357e46e7 21791 files.files[0].name: list0.h
c5aa993b 21792 files.files[0].dir: /srcdir
357e46e7 21793 files.files[1].name: list0.c
c5aa993b 21794 files.files[1].dir: /srcdir
c906108c
SS
21795
21796 The line number information for list0.c has to end up in a single
4f1520fb
FR
21797 subfile, so that `break /srcdir/list0.c:1' works as expected.
21798 start_subfile will ensure that this happens provided that we pass the
21799 concatenation of files.files[1].dir and files.files[1].name as the
21800 subfile's name. */
c906108c
SS
21801
21802static void
804d2729
TT
21803dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21804 const char *dirname)
c906108c 21805{
43816ebc 21806 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21807
4d663531 21808 /* In order not to lose the line information directory,
4f1520fb
FR
21809 we concatenate it to the filename when it makes sense.
21810 Note that the Dwarf3 standard says (speaking of filenames in line
21811 information): ``The directory index is ignored for file names
21812 that represent full path names''. Thus ignoring dirname in the
21813 `else' branch below isn't an issue. */
c906108c 21814
d5166ae1 21815 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21816 {
43816ebc
TT
21817 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21818 filename = copy.get ();
d521ce57 21819 }
c906108c 21820
c24bdb02 21821 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21822}
21823
804d2729
TT
21824/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21825 buildsym_compunit constructor. */
f4dc4d17 21826
c24bdb02
KS
21827struct compunit_symtab *
21828dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21829 CORE_ADDR low_pc)
f4dc4d17 21830{
c24bdb02 21831 gdb_assert (m_builder == nullptr);
43f3e411 21832
c24bdb02 21833 m_builder.reset (new struct buildsym_compunit
f6e649dd 21834 (this->per_objfile->objfile,
c24bdb02 21835 name, comp_dir, language, low_pc));
93b8bea4 21836
c24bdb02 21837 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21838
c24bdb02
KS
21839 get_builder ()->record_debugformat ("DWARF 2");
21840 get_builder ()->record_producer (producer);
f4dc4d17 21841
c24bdb02 21842 processing_has_namespace_info = false;
43f3e411 21843
c24bdb02 21844 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21845}
21846
4c2df51b
DJ
21847static void
21848var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21849 struct dwarf2_cu *cu)
4c2df51b 21850{
5e22e966 21851 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21852 struct comp_unit_head *cu_header = &cu->header;
21853
4c2df51b
DJ
21854 /* NOTE drow/2003-01-30: There used to be a comment and some special
21855 code here to turn a symbol with DW_AT_external and a
21856 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21857 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21858 with some versions of binutils) where shared libraries could have
21859 relocations against symbols in their debug information - the
21860 minimal symbol would have the right address, but the debug info
21861 would not. It's no longer necessary, because we will explicitly
21862 apply relocations when we read in the debug information now. */
21863
21864 /* A DW_AT_location attribute with no contents indicates that a
21865 variable has been optimized away. */
9d2246fc 21866 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21867 {
f1e6e072 21868 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21869 return;
21870 }
21871
21872 /* Handle one degenerate form of location expression specially, to
21873 preserve GDB's previous behavior when section offsets are
336d760d
AT
21874 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21875 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21876
9d2246fc
TT
21877 if (attr->form_is_block ())
21878 {
21879 struct dwarf_block *block = attr->as_block ();
21880
21881 if ((block->data[0] == DW_OP_addr
21882 && block->size == 1 + cu_header->addr_size)
21883 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 21884 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
21885 && (block->size
21886 == 1 + leb128_size (&block->data[1]))))
21887 {
21888 unsigned int dummy;
21889
21890 if (block->data[0] == DW_OP_addr)
21891 SET_SYMBOL_VALUE_ADDRESS
21892 (sym, cu->header.read_address (objfile->obfd,
21893 block->data + 1,
38583298 21894 &dummy));
9d2246fc
TT
21895 else
21896 SET_SYMBOL_VALUE_ADDRESS
21897 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21898 &dummy));
21899 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21900 fixup_symbol_section (sym, objfile);
21901 SET_SYMBOL_VALUE_ADDRESS
21902 (sym,
21903 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 21904 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
21905 return;
21906 }
4c2df51b
DJ
21907 }
21908
21909 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21910 expression evaluator, and use LOC_COMPUTED only when necessary
21911 (i.e. when the value of a register or memory location is
21912 referenced, or a thread-local block, etc.). Then again, it might
21913 not be worthwhile. I'm assuming that it isn't unless performance
21914 or memory numbers show me otherwise. */
21915
f1e6e072 21916 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21917
f1e6e072 21918 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21919 cu->has_loclist = true;
4c2df51b
DJ
21920}
21921
c906108c
SS
21922/* Given a pointer to a DWARF information entry, figure out if we need
21923 to make a symbol table entry for it, and if so, create a new entry
21924 and return a pointer to it.
21925 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21926 used the passed type.
21927 If SPACE is not NULL, use it to hold the new symbol. If it is
21928 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21929
21930static struct symbol *
5e2db402
TT
21931new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21932 struct symbol *space)
c906108c 21933{
976ca316
SM
21934 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21935 struct objfile *objfile = per_objfile->objfile;
08feed99 21936 struct gdbarch *gdbarch = objfile->arch ();
c906108c 21937 struct symbol *sym = NULL;
15d034d0 21938 const char *name;
c906108c
SS
21939 struct attribute *attr = NULL;
21940 struct attribute *attr2 = NULL;
e142c38c 21941 CORE_ADDR baseaddr;
e37fd15a
SW
21942 struct pending **list_to_add = NULL;
21943
edb3359d 21944 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21945
b3b3bada 21946 baseaddr = objfile->text_section_offset ();
c906108c 21947
94af9270 21948 name = dwarf2_name (die, cu);
c906108c
SS
21949 if (name)
21950 {
34eaf542 21951 int suppress_add = 0;
94af9270 21952
34eaf542
TT
21953 if (space)
21954 sym = space;
21955 else
8c14c3a3 21956 sym = new (&objfile->objfile_obstack) symbol;
c906108c 21957 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21958
21959 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21960 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
21961 /* Fortran does not have mangling standard and the mangling does differ
21962 between gfortran, iFort etc. */
bcfe6157
TT
21963 const char *physname
21964 = (cu->language == language_fortran
21965 ? dwarf2_full_name (name, die, cu)
21966 : dwarf2_physname (name, die, cu));
21967 const char *linkagename = dw2_linkage_name (die, cu);
21968
21969 if (linkagename == nullptr || cu->language == language_ada)
21970 sym->set_linkage_name (physname);
21971 else
21972 {
21973 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21974 sym->set_linkage_name (linkagename);
21975 }
f55ee35c 21976
c906108c 21977 /* Default assumptions.
dda83cd7 21978 Use the passed type or decode it from the die. */
176620f1 21979 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21980 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21981 if (type != NULL)
21982 SYMBOL_TYPE (sym) = type;
21983 else
e7c27a73 21984 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21985 attr = dwarf2_attr (die,
21986 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21987 cu);
435d3d88 21988 if (attr != nullptr)
529908cb 21989 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 21990
edb3359d
DJ
21991 attr = dwarf2_attr (die,
21992 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21993 cu);
aa659cfa 21994 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 21995 {
529908cb 21996 file_name_index file_index
aa659cfa 21997 = (file_name_index) attr->as_nonnegative ();
8c43009f 21998 struct file_entry *fe;
9a619af0 21999
ecfb656c
PA
22000 if (cu->line_header != NULL)
22001 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
22002 else
22003 fe = NULL;
22004
22005 if (fe == NULL)
b98664d3 22006 complaint (_("file index out of range"));
8c43009f
PA
22007 else
22008 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
22009 }
22010
c906108c
SS
22011 switch (die->tag)
22012 {
22013 case DW_TAG_label:
e142c38c 22014 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 22015 if (attr != nullptr)
3e29f34a
MR
22016 {
22017 CORE_ADDR addr;
22018
95f982e5 22019 addr = attr->as_address ();
3e29f34a 22020 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 22021 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 22022 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 22023 }
8f5c6526
TV
22024 else
22025 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
22026 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22027 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 22028 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
22029 break;
22030 case DW_TAG_subprogram:
22031 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22032 finish_block. */
f1e6e072 22033 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 22034 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 22035 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
22036 || cu->language == language_ada
22037 || cu->language == language_fortran)
c906108c 22038 {
dda83cd7
SM
22039 /* Subprograms marked external are stored as a global symbol.
22040 Ada and Fortran subprograms, whether marked external or
22041 not, are always stored as a global symbol, because we want
22042 to be able to access them globally. For instance, we want
22043 to be able to break on a nested subprogram without having
22044 to specify the context. */
c24bdb02 22045 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
22046 }
22047 else
22048 {
e37fd15a 22049 list_to_add = cu->list_in_scope;
c906108c
SS
22050 }
22051 break;
edb3359d
DJ
22052 case DW_TAG_inlined_subroutine:
22053 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22054 finish_block. */
f1e6e072 22055 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 22056 SYMBOL_INLINED (sym) = 1;
481860b3 22057 list_to_add = cu->list_in_scope;
edb3359d 22058 break;
34eaf542
TT
22059 case DW_TAG_template_value_param:
22060 suppress_add = 1;
22061 /* Fall through. */
72929c62 22062 case DW_TAG_constant:
c906108c 22063 case DW_TAG_variable:
254e6b9e 22064 case DW_TAG_member:
0963b4bd
MS
22065 /* Compilation with minimal debug info may result in
22066 variables with missing type entries. Change the
22067 misleading `void' type to something sensible. */
78134374 22068 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 22069 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 22070
e142c38c 22071 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
22072 /* In the case of DW_TAG_member, we should only be called for
22073 static const members. */
22074 if (die->tag == DW_TAG_member)
22075 {
3863f96c
DE
22076 /* dwarf2_add_field uses die_is_declaration,
22077 so we do the same. */
254e6b9e
DE
22078 gdb_assert (die_is_declaration (die, cu));
22079 gdb_assert (attr);
22080 }
435d3d88 22081 if (attr != nullptr)
c906108c 22082 {
e7c27a73 22083 dwarf2_const_value (attr, sym, cu);
e142c38c 22084 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 22085 if (!suppress_add)
34eaf542 22086 {
c45bc3f8 22087 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 22088 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 22089 else
e37fd15a 22090 list_to_add = cu->list_in_scope;
34eaf542 22091 }
c906108c
SS
22092 break;
22093 }
e142c38c 22094 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22095 if (attr != nullptr)
c906108c 22096 {
e7c27a73 22097 var_decode_location (attr, sym, cu);
e142c38c 22098 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
22099
22100 /* Fortran explicitly imports any global symbols to the local
22101 scope by DW_TAG_common_block. */
22102 if (cu->language == language_fortran && die->parent
22103 && die->parent->tag == DW_TAG_common_block)
22104 attr2 = NULL;
22105
caac4577
JG
22106 if (SYMBOL_CLASS (sym) == LOC_STATIC
22107 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 22108 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
22109 {
22110 /* When a static variable is eliminated by the linker,
22111 the corresponding debug information is not stripped
22112 out, but the variable address is set to null;
22113 do not add such variables into symbol table. */
22114 }
c45bc3f8 22115 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 22116 {
4b610737
TT
22117 if (SYMBOL_CLASS (sym) == LOC_STATIC
22118 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 22119 && per_objfile->per_bfd->can_copy)
4b610737
TT
22120 {
22121 /* A global static variable might be subject to
22122 copy relocation. We first check for a local
22123 minsym, though, because maybe the symbol was
22124 marked hidden, in which case this would not
22125 apply. */
22126 bound_minimal_symbol found
22127 = (lookup_minimal_symbol_linkage
987012b8 22128 (sym->linkage_name (), objfile));
4b610737
TT
22129 if (found.minsym != nullptr)
22130 sym->maybe_copied = 1;
22131 }
f55ee35c 22132
1c809c68
TT
22133 /* A variable with DW_AT_external is never static,
22134 but it may be block-scoped. */
804d2729 22135 list_to_add
c24bdb02
KS
22136 = ((cu->list_in_scope
22137 == cu->get_builder ()->get_file_symbols ())
22138 ? cu->get_builder ()->get_global_symbols ()
804d2729 22139 : cu->list_in_scope);
1c809c68 22140 }
c906108c 22141 else
e37fd15a 22142 list_to_add = cu->list_in_scope;
c906108c
SS
22143 }
22144 else
22145 {
22146 /* We do not know the address of this symbol.
dda83cd7
SM
22147 If it is an external symbol and we have type information
22148 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22149 The address of the variable will then be determined from
22150 the minimal symbol table whenever the variable is
22151 referenced. */
e142c38c 22152 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22153
22154 /* Fortran explicitly imports any global symbols to the local
22155 scope by DW_TAG_common_block. */
22156 if (cu->language == language_fortran && die->parent
22157 && die->parent->tag == DW_TAG_common_block)
22158 {
22159 /* SYMBOL_CLASS doesn't matter here because
22160 read_common_block is going to reset it. */
22161 if (!suppress_add)
22162 list_to_add = cu->list_in_scope;
22163 }
c45bc3f8 22164 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22165 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22166 {
0fe7935b
DJ
22167 /* A variable with DW_AT_external is never static, but it
22168 may be block-scoped. */
804d2729 22169 list_to_add
c24bdb02
KS
22170 = ((cu->list_in_scope
22171 == cu->get_builder ()->get_file_symbols ())
22172 ? cu->get_builder ()->get_global_symbols ()
804d2729 22173 : cu->list_in_scope);
0fe7935b 22174
f1e6e072 22175 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22176 }
442ddf59
JK
22177 else if (!die_is_declaration (die, cu))
22178 {
22179 /* Use the default LOC_OPTIMIZED_OUT class. */
22180 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22181 if (!suppress_add)
22182 list_to_add = cu->list_in_scope;
442ddf59 22183 }
c906108c
SS
22184 }
22185 break;
22186 case DW_TAG_formal_parameter:
a60f3166
TT
22187 {
22188 /* If we are inside a function, mark this as an argument. If
22189 not, we might be looking at an argument to an inlined function
22190 when we do not have enough information to show inlined frames;
22191 pretend it's a local variable in that case so that the user can
22192 still see it. */
804d2729 22193 struct context_stack *curr
c24bdb02 22194 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22195 if (curr != nullptr && curr->name != nullptr)
22196 SYMBOL_IS_ARGUMENT (sym) = 1;
22197 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22198 if (attr != nullptr)
a60f3166
TT
22199 {
22200 var_decode_location (attr, sym, cu);
22201 }
22202 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22203 if (attr != nullptr)
a60f3166
TT
22204 {
22205 dwarf2_const_value (attr, sym, cu);
22206 }
f346a30d 22207
a60f3166
TT
22208 list_to_add = cu->list_in_scope;
22209 }
c906108c
SS
22210 break;
22211 case DW_TAG_unspecified_parameters:
22212 /* From varargs functions; gdb doesn't seem to have any
22213 interest in this information, so just ignore it for now.
22214 (FIXME?) */
22215 break;
34eaf542
TT
22216 case DW_TAG_template_type_param:
22217 suppress_add = 1;
22218 /* Fall through. */
c906108c 22219 case DW_TAG_class_type:
680b30c7 22220 case DW_TAG_interface_type:
c906108c
SS
22221 case DW_TAG_structure_type:
22222 case DW_TAG_union_type:
72019c9c 22223 case DW_TAG_set_type:
c906108c 22224 case DW_TAG_enumeration_type:
f1e6e072 22225 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22226 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22227
63d06c5c 22228 {
9c37b5ae 22229 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22230 really ever be static objects: otherwise, if you try
22231 to, say, break of a class's method and you're in a file
22232 which doesn't mention that class, it won't work unless
22233 the check for all static symbols in lookup_symbol_aux
22234 saves you. See the OtherFileClass tests in
22235 gdb.c++/namespace.exp. */
22236
e37fd15a 22237 if (!suppress_add)
34eaf542 22238 {
c24bdb02 22239 buildsym_compunit *builder = cu->get_builder ();
804d2729 22240 list_to_add
c24bdb02 22241 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22242 && cu->language == language_cplus
c24bdb02 22243 ? builder->get_global_symbols ()
804d2729 22244 : cu->list_in_scope);
63d06c5c 22245
64382290 22246 /* The semantics of C++ state that "struct foo {
9c37b5ae 22247 ... }" also defines a typedef for "foo". */
64382290 22248 if (cu->language == language_cplus
45280282 22249 || cu->language == language_ada
c44af4eb
TT
22250 || cu->language == language_d
22251 || cu->language == language_rust)
64382290
TT
22252 {
22253 /* The symbol's name is already allocated along
22254 with this objfile, so we don't need to
22255 duplicate it for the type. */
7d93a1e0 22256 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22257 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22258 }
63d06c5c
DC
22259 }
22260 }
c906108c
SS
22261 break;
22262 case DW_TAG_typedef:
f1e6e072 22263 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22264 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22265 list_to_add = cu->list_in_scope;
63d06c5c 22266 break;
d8f62e84 22267 case DW_TAG_array_type:
c906108c 22268 case DW_TAG_base_type:
dda83cd7 22269 case DW_TAG_subrange_type:
f1e6e072 22270 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22271 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22272 list_to_add = cu->list_in_scope;
c906108c
SS
22273 break;
22274 case DW_TAG_enumerator:
e142c38c 22275 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22276 if (attr != nullptr)
c906108c 22277 {
e7c27a73 22278 dwarf2_const_value (attr, sym, cu);
c906108c 22279 }
63d06c5c
DC
22280 {
22281 /* NOTE: carlton/2003-11-10: See comment above in the
22282 DW_TAG_class_type, etc. block. */
22283
804d2729 22284 list_to_add
c24bdb02 22285 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22286 && cu->language == language_cplus
c24bdb02 22287 ? cu->get_builder ()->get_global_symbols ()
804d2729 22288 : cu->list_in_scope);
63d06c5c 22289 }
c906108c 22290 break;
74921315 22291 case DW_TAG_imported_declaration:
5c4e30ca 22292 case DW_TAG_namespace:
f1e6e072 22293 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22294 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22295 break;
530e8392
KB
22296 case DW_TAG_module:
22297 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22298 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22299 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22300 break;
4357ac6c 22301 case DW_TAG_common_block:
f1e6e072 22302 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22303 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22304 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22305 break;
c906108c
SS
22306 default:
22307 /* Not a tag we recognize. Hopefully we aren't processing
22308 trash data, but since we must specifically ignore things
22309 we don't recognize, there is nothing else we should do at
0963b4bd 22310 this point. */
b98664d3 22311 complaint (_("unsupported tag: '%s'"),
4d3c2250 22312 dwarf_tag_name (die->tag));
c906108c
SS
22313 break;
22314 }
df8a16a1 22315
e37fd15a
SW
22316 if (suppress_add)
22317 {
22318 sym->hash_next = objfile->template_symbols;
22319 objfile->template_symbols = sym;
22320 list_to_add = NULL;
22321 }
22322
22323 if (list_to_add != NULL)
d3cb6808 22324 add_symbol_to_list (sym, list_to_add);
e37fd15a 22325
df8a16a1
DJ
22326 /* For the benefit of old versions of GCC, check for anonymous
22327 namespaces based on the demangled name. */
4d4ec4e5 22328 if (!cu->processing_has_namespace_info
94af9270 22329 && cu->language == language_cplus)
c24bdb02 22330 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22331 }
22332 return (sym);
22333}
22334
98bfdba5
PA
22335/* Given an attr with a DW_FORM_dataN value in host byte order,
22336 zero-extend it as appropriate for the symbol's type. The DWARF
22337 standard (v4) is not entirely clear about the meaning of using
22338 DW_FORM_dataN for a constant with a signed type, where the type is
22339 wider than the data. The conclusion of a discussion on the DWARF
22340 list was that this is unspecified. We choose to always zero-extend
22341 because that is the interpretation long in use by GCC. */
c906108c 22342
98bfdba5 22343static gdb_byte *
ff39bb5e 22344dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22345 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22346{
5e22e966 22347 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22348 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22349 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22350 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22351
22352 if (bits < sizeof (*value) * 8)
22353 {
22354 l &= ((LONGEST) 1 << bits) - 1;
22355 *value = l;
22356 }
22357 else if (bits == sizeof (*value) * 8)
22358 *value = l;
22359 else
22360 {
224c3ddb 22361 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22362 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22363 return bytes;
22364 }
22365
22366 return NULL;
22367}
22368
22369/* Read a constant value from an attribute. Either set *VALUE, or if
22370 the value does not fit in *VALUE, set *BYTES - either already
22371 allocated on the objfile obstack, or newly allocated on OBSTACK,
22372 or, set *BATON, if we translated the constant to a location
22373 expression. */
22374
22375static void
ff39bb5e 22376dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22377 const char *name, struct obstack *obstack,
22378 struct dwarf2_cu *cu,
d521ce57 22379 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22380 struct dwarf2_locexpr_baton **baton)
22381{
5e22e966 22382 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22383 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22384 struct comp_unit_head *cu_header = &cu->header;
c906108c 22385 struct dwarf_block *blk;
98bfdba5
PA
22386 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22387 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22388
22389 *value = 0;
22390 *bytes = NULL;
22391 *baton = NULL;
c906108c
SS
22392
22393 switch (attr->form)
22394 {
22395 case DW_FORM_addr:
336d760d 22396 case DW_FORM_addrx:
3019eac3 22397 case DW_FORM_GNU_addr_index:
ac56253d 22398 {
ac56253d
TT
22399 gdb_byte *data;
22400
98bfdba5
PA
22401 if (TYPE_LENGTH (type) != cu_header->addr_size)
22402 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22403 cu_header->addr_size,
98bfdba5 22404 TYPE_LENGTH (type));
ac56253d
TT
22405 /* Symbols of this form are reasonably rare, so we just
22406 piggyback on the existing location code rather than writing
22407 a new implementation of symbol_computed_ops. */
8d749320 22408 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22409 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22410 (*baton)->per_cu = cu->per_cu;
22411 gdb_assert ((*baton)->per_cu);
ac56253d 22412
98bfdba5 22413 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22414 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22415 (*baton)->data = data;
ac56253d
TT
22416
22417 data[0] = DW_OP_addr;
22418 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22419 byte_order, attr->as_address ());
ac56253d 22420 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22421 }
c906108c 22422 break;
4ac36638 22423 case DW_FORM_string:
93b5768b 22424 case DW_FORM_strp:
cf532bd1 22425 case DW_FORM_strx:
3019eac3 22426 case DW_FORM_GNU_str_index:
36586728 22427 case DW_FORM_GNU_strp_alt:
c6481205 22428 /* The string is already allocated on the objfile obstack, point
98bfdba5 22429 directly to it. */
2c830f54 22430 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22431 break;
c906108c
SS
22432 case DW_FORM_block1:
22433 case DW_FORM_block2:
22434 case DW_FORM_block4:
22435 case DW_FORM_block:
2dc7f7b3 22436 case DW_FORM_exprloc:
0224619f 22437 case DW_FORM_data16:
9d2246fc 22438 blk = attr->as_block ();
98bfdba5
PA
22439 if (TYPE_LENGTH (type) != blk->size)
22440 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22441 TYPE_LENGTH (type));
22442 *bytes = blk->data;
c906108c 22443 break;
2df3850c
JM
22444
22445 /* The DW_AT_const_value attributes are supposed to carry the
22446 symbol's value "represented as it would be on the target
22447 architecture." By the time we get here, it's already been
22448 converted to host endianness, so we just need to sign- or
22449 zero-extend it as appropriate. */
22450 case DW_FORM_data1:
3aef2284 22451 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22452 break;
c906108c 22453 case DW_FORM_data2:
3aef2284 22454 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22455 break;
c906108c 22456 case DW_FORM_data4:
3aef2284 22457 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22458 break;
c906108c 22459 case DW_FORM_data8:
3aef2284 22460 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22461 break;
22462
c906108c 22463 case DW_FORM_sdata:
663c44ac 22464 case DW_FORM_implicit_const:
1bc397c5 22465 *value = attr->as_signed ();
2df3850c
JM
22466 break;
22467
c906108c 22468 case DW_FORM_udata:
529908cb 22469 *value = attr->as_unsigned ();
c906108c 22470 break;
2df3850c 22471
c906108c 22472 default:
b98664d3 22473 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22474 dwarf_form_name (attr->form));
98bfdba5 22475 *value = 0;
c906108c
SS
22476 break;
22477 }
22478}
22479
2df3850c 22480
98bfdba5
PA
22481/* Copy constant value from an attribute to a symbol. */
22482
2df3850c 22483static void
ff39bb5e 22484dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22485 struct dwarf2_cu *cu)
2df3850c 22486{
5e22e966 22487 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22488 LONGEST value;
d521ce57 22489 const gdb_byte *bytes;
98bfdba5 22490 struct dwarf2_locexpr_baton *baton;
2df3850c 22491
98bfdba5 22492 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22493 sym->print_name (),
98bfdba5
PA
22494 &objfile->objfile_obstack, cu,
22495 &value, &bytes, &baton);
2df3850c 22496
98bfdba5
PA
22497 if (baton != NULL)
22498 {
98bfdba5 22499 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22500 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22501 }
22502 else if (bytes != NULL)
22503 {
22504 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22505 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22506 }
22507 else
22508 {
22509 SYMBOL_VALUE (sym) = value;
f1e6e072 22510 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22511 }
2df3850c
JM
22512}
22513
c906108c
SS
22514/* Return the type of the die in question using its DW_AT_type attribute. */
22515
22516static struct type *
e7c27a73 22517die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22518{
c906108c 22519 struct attribute *type_attr;
c906108c 22520
e142c38c 22521 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22522 if (!type_attr)
22523 {
5e22e966 22524 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22525 /* A missing DW_AT_type represents a void type. */
518817b3 22526 return objfile_type (objfile)->builtin_void;
c906108c 22527 }
348e048f 22528
673bfd45 22529 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22530}
22531
b4ba55a1
JB
22532/* True iff CU's producer generates GNAT Ada auxiliary information
22533 that allows to find parallel types through that information instead
22534 of having to do expensive parallel lookups by type name. */
22535
22536static int
22537need_gnat_info (struct dwarf2_cu *cu)
22538{
de4cb04a
JB
22539 /* Assume that the Ada compiler was GNAT, which always produces
22540 the auxiliary information. */
22541 return (cu->language == language_ada);
b4ba55a1
JB
22542}
22543
b4ba55a1
JB
22544/* Return the auxiliary type of the die in question using its
22545 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22546 attribute is not present. */
22547
22548static struct type *
22549die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22550{
b4ba55a1 22551 struct attribute *type_attr;
b4ba55a1
JB
22552
22553 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22554 if (!type_attr)
22555 return NULL;
22556
673bfd45 22557 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22558}
22559
22560/* If DIE has a descriptive_type attribute, then set the TYPE's
22561 descriptive type accordingly. */
22562
22563static void
22564set_descriptive_type (struct type *type, struct die_info *die,
22565 struct dwarf2_cu *cu)
22566{
22567 struct type *descriptive_type = die_descriptive_type (die, cu);
22568
22569 if (descriptive_type)
22570 {
22571 ALLOCATE_GNAT_AUX_TYPE (type);
22572 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22573 }
22574}
22575
c906108c
SS
22576/* Return the containing type of the die in question using its
22577 DW_AT_containing_type attribute. */
22578
22579static struct type *
e7c27a73 22580die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22581{
c906108c 22582 struct attribute *type_attr;
5e22e966 22583 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22584
e142c38c 22585 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22586 if (!type_attr)
22587 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22588 "[in module %s]"), objfile_name (objfile));
33ac96f0 22589
673bfd45 22590 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22591}
22592
ac9ec31b
DE
22593/* Return an error marker type to use for the ill formed type in DIE/CU. */
22594
22595static struct type *
22596build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22597{
976ca316
SM
22598 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22599 struct objfile *objfile = per_objfile->objfile;
528e1572 22600 char *saved;
ac9ec31b 22601
528e1572
SM
22602 std::string message
22603 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22604 objfile_name (objfile),
22605 sect_offset_str (cu->header.sect_off),
22606 sect_offset_str (die->sect_off));
efba19b0 22607 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22608
19f392bc 22609 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22610}
22611
673bfd45 22612/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22613 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22614 DW_AT_containing_type.
673bfd45
DE
22615 If there is no type substitute an error marker. */
22616
c906108c 22617static struct type *
ff39bb5e 22618lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22619 struct dwarf2_cu *cu)
c906108c 22620{
976ca316
SM
22621 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22622 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22623 struct type *this_type;
22624
ac9ec31b
DE
22625 gdb_assert (attr->name == DW_AT_type
22626 || attr->name == DW_AT_GNAT_descriptive_type
22627 || attr->name == DW_AT_containing_type);
22628
673bfd45
DE
22629 /* First see if we have it cached. */
22630
36586728
TT
22631 if (attr->form == DW_FORM_GNU_ref_alt)
22632 {
22633 struct dwarf2_per_cu_data *per_cu;
0826b30a 22634 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22635
976ca316
SM
22636 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22637 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22638 }
cd6c91b4 22639 else if (attr->form_is_ref ())
673bfd45 22640 {
0826b30a 22641 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22642
976ca316 22643 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22644 }
55f1336d 22645 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22646 {
630ed6b9 22647 ULONGEST signature = attr->as_signature ();
673bfd45 22648
ac9ec31b 22649 return get_signatured_type (die, signature, cu);
673bfd45
DE
22650 }
22651 else
22652 {
b98664d3 22653 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22654 " at %s [in module %s]"),
22655 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22656 objfile_name (objfile));
ac9ec31b 22657 return build_error_marker_type (cu, die);
673bfd45
DE
22658 }
22659
22660 /* If not cached we need to read it in. */
22661
22662 if (this_type == NULL)
22663 {
ac9ec31b 22664 struct die_info *type_die = NULL;
673bfd45
DE
22665 struct dwarf2_cu *type_cu = cu;
22666
cd6c91b4 22667 if (attr->form_is_ref ())
ac9ec31b
DE
22668 type_die = follow_die_ref (die, attr, &type_cu);
22669 if (type_die == NULL)
22670 return build_error_marker_type (cu, die);
22671 /* If we find the type now, it's probably because the type came
3019eac3
DE
22672 from an inter-CU reference and the type's CU got expanded before
22673 ours. */
ac9ec31b 22674 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22675 }
22676
22677 /* If we still don't have a type use an error marker. */
22678
22679 if (this_type == NULL)
ac9ec31b 22680 return build_error_marker_type (cu, die);
673bfd45 22681
f792889a 22682 return this_type;
c906108c
SS
22683}
22684
673bfd45
DE
22685/* Return the type in DIE, CU.
22686 Returns NULL for invalid types.
22687
02142a6c 22688 This first does a lookup in die_type_hash,
673bfd45
DE
22689 and only reads the die in if necessary.
22690
22691 NOTE: This can be called when reading in partial or full symbols. */
22692
f792889a 22693static struct type *
e7c27a73 22694read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22695{
f792889a
DJ
22696 struct type *this_type;
22697
22698 this_type = get_die_type (die, cu);
22699 if (this_type)
22700 return this_type;
22701
673bfd45
DE
22702 return read_type_die_1 (die, cu);
22703}
22704
22705/* Read the type in DIE, CU.
22706 Returns NULL for invalid types. */
22707
22708static struct type *
22709read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22710{
22711 struct type *this_type = NULL;
22712
c906108c
SS
22713 switch (die->tag)
22714 {
22715 case DW_TAG_class_type:
680b30c7 22716 case DW_TAG_interface_type:
c906108c
SS
22717 case DW_TAG_structure_type:
22718 case DW_TAG_union_type:
f792889a 22719 this_type = read_structure_type (die, cu);
c906108c
SS
22720 break;
22721 case DW_TAG_enumeration_type:
f792889a 22722 this_type = read_enumeration_type (die, cu);
c906108c
SS
22723 break;
22724 case DW_TAG_subprogram:
22725 case DW_TAG_subroutine_type:
edb3359d 22726 case DW_TAG_inlined_subroutine:
f792889a 22727 this_type = read_subroutine_type (die, cu);
c906108c
SS
22728 break;
22729 case DW_TAG_array_type:
f792889a 22730 this_type = read_array_type (die, cu);
c906108c 22731 break;
72019c9c 22732 case DW_TAG_set_type:
f792889a 22733 this_type = read_set_type (die, cu);
72019c9c 22734 break;
c906108c 22735 case DW_TAG_pointer_type:
f792889a 22736 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22737 break;
22738 case DW_TAG_ptr_to_member_type:
f792889a 22739 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22740 break;
22741 case DW_TAG_reference_type:
4297a3f0
AV
22742 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22743 break;
22744 case DW_TAG_rvalue_reference_type:
22745 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22746 break;
22747 case DW_TAG_const_type:
f792889a 22748 this_type = read_tag_const_type (die, cu);
c906108c
SS
22749 break;
22750 case DW_TAG_volatile_type:
f792889a 22751 this_type = read_tag_volatile_type (die, cu);
c906108c 22752 break;
06d66ee9
TT
22753 case DW_TAG_restrict_type:
22754 this_type = read_tag_restrict_type (die, cu);
22755 break;
c906108c 22756 case DW_TAG_string_type:
f792889a 22757 this_type = read_tag_string_type (die, cu);
c906108c
SS
22758 break;
22759 case DW_TAG_typedef:
f792889a 22760 this_type = read_typedef (die, cu);
c906108c 22761 break;
a02abb62 22762 case DW_TAG_subrange_type:
f792889a 22763 this_type = read_subrange_type (die, cu);
a02abb62 22764 break;
c906108c 22765 case DW_TAG_base_type:
f792889a 22766 this_type = read_base_type (die, cu);
c906108c 22767 break;
81a17f79 22768 case DW_TAG_unspecified_type:
f792889a 22769 this_type = read_unspecified_type (die, cu);
81a17f79 22770 break;
0114d602
DJ
22771 case DW_TAG_namespace:
22772 this_type = read_namespace_type (die, cu);
22773 break;
f55ee35c
JK
22774 case DW_TAG_module:
22775 this_type = read_module_type (die, cu);
22776 break;
a2c2acaf
MW
22777 case DW_TAG_atomic_type:
22778 this_type = read_tag_atomic_type (die, cu);
22779 break;
c906108c 22780 default:
b98664d3 22781 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22782 dwarf_tag_name (die->tag));
c906108c
SS
22783 break;
22784 }
63d06c5c 22785
f792889a 22786 return this_type;
63d06c5c
DC
22787}
22788
abc72ce4
DE
22789/* See if we can figure out if the class lives in a namespace. We do
22790 this by looking for a member function; its demangled name will
22791 contain namespace info, if there is any.
22792 Return the computed name or NULL.
22793 Space for the result is allocated on the objfile's obstack.
22794 This is the full-die version of guess_partial_die_structure_name.
22795 In this case we know DIE has no useful parent. */
22796
43816ebc 22797static const char *
abc72ce4
DE
22798guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22799{
22800 struct die_info *spec_die;
22801 struct dwarf2_cu *spec_cu;
22802 struct die_info *child;
5e22e966 22803 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22804
22805 spec_cu = cu;
22806 spec_die = die_specification (die, &spec_cu);
22807 if (spec_die != NULL)
22808 {
22809 die = spec_die;
22810 cu = spec_cu;
22811 }
22812
22813 for (child = die->child;
22814 child != NULL;
22815 child = child->sibling)
22816 {
22817 if (child->tag == DW_TAG_subprogram)
22818 {
73b9be8b 22819 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22820
7d45c7c3 22821 if (linkage_name != NULL)
abc72ce4 22822 {
43816ebc 22823 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22824 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22825 const char *name = NULL;
abc72ce4
DE
22826
22827 if (actual_name != NULL)
22828 {
15d034d0 22829 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22830
22831 if (die_name != NULL
43816ebc 22832 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22833 {
22834 /* Strip off the class name from the full name.
22835 We want the prefix. */
22836 int die_name_len = strlen (die_name);
43816ebc
TT
22837 int actual_name_len = strlen (actual_name.get ());
22838 const char *ptr = actual_name.get ();
abc72ce4
DE
22839
22840 /* Test for '::' as a sanity check. */
22841 if (actual_name_len > die_name_len + 2
43816ebc 22842 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22843 name = obstack_strndup (
e3b94546 22844 &objfile->per_bfd->storage_obstack,
43816ebc 22845 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22846 }
22847 }
abc72ce4
DE
22848 return name;
22849 }
22850 }
22851 }
22852
22853 return NULL;
22854}
22855
96408a79
SA
22856/* GCC might emit a nameless typedef that has a linkage name. Determine the
22857 prefix part in such case. See
22858 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22859
a121b7c1 22860static const char *
96408a79
SA
22861anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22862{
22863 struct attribute *attr;
e6a959d6 22864 const char *base;
96408a79
SA
22865
22866 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22867 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22868 return NULL;
22869
7d45c7c3 22870 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22871 return NULL;
22872
73b9be8b 22873 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22874 const char *attr_name = attr->as_string ();
22875 if (attr == NULL || attr_name == NULL)
96408a79
SA
22876 return NULL;
22877
22878 /* dwarf2_name had to be already called. */
3b64bf15 22879 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22880
22881 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22882 base = strrchr (attr_name, ':');
22883 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22884 return "";
22885
5e22e966 22886 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22887 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22888 attr_name,
22889 &base[-1] - attr_name);
96408a79
SA
22890}
22891
fdde2d81 22892/* Return the name of the namespace/class that DIE is defined within,
0114d602 22893 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22894
0114d602
DJ
22895 For example, if we're within the method foo() in the following
22896 code:
22897
22898 namespace N {
22899 class C {
22900 void foo () {
22901 }
22902 };
22903 }
22904
22905 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22906
0d5cff50 22907static const char *
e142c38c 22908determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22909{
976ca316 22910 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
22911 struct die_info *parent, *spec_die;
22912 struct dwarf2_cu *spec_cu;
22913 struct type *parent_type;
a121b7c1 22914 const char *retval;
63d06c5c 22915
9c37b5ae 22916 if (cu->language != language_cplus
c44af4eb
TT
22917 && cu->language != language_fortran && cu->language != language_d
22918 && cu->language != language_rust)
0114d602
DJ
22919 return "";
22920
96408a79
SA
22921 retval = anonymous_struct_prefix (die, cu);
22922 if (retval)
22923 return retval;
22924
0114d602
DJ
22925 /* We have to be careful in the presence of DW_AT_specification.
22926 For example, with GCC 3.4, given the code
22927
22928 namespace N {
22929 void foo() {
22930 // Definition of N::foo.
22931 }
22932 }
22933
22934 then we'll have a tree of DIEs like this:
22935
22936 1: DW_TAG_compile_unit
22937 2: DW_TAG_namespace // N
22938 3: DW_TAG_subprogram // declaration of N::foo
22939 4: DW_TAG_subprogram // definition of N::foo
22940 DW_AT_specification // refers to die #3
22941
22942 Thus, when processing die #4, we have to pretend that we're in
22943 the context of its DW_AT_specification, namely the contex of die
22944 #3. */
22945 spec_cu = cu;
22946 spec_die = die_specification (die, &spec_cu);
22947 if (spec_die == NULL)
22948 parent = die->parent;
22949 else
63d06c5c 22950 {
0114d602
DJ
22951 parent = spec_die->parent;
22952 cu = spec_cu;
63d06c5c 22953 }
0114d602
DJ
22954
22955 if (parent == NULL)
22956 return "";
98bfdba5
PA
22957 else if (parent->building_fullname)
22958 {
22959 const char *name;
22960 const char *parent_name;
22961
22962 /* It has been seen on RealView 2.2 built binaries,
22963 DW_TAG_template_type_param types actually _defined_ as
22964 children of the parent class:
22965
22966 enum E {};
22967 template class <class Enum> Class{};
22968 Class<enum E> class_e;
22969
dda83cd7
SM
22970 1: DW_TAG_class_type (Class)
22971 2: DW_TAG_enumeration_type (E)
22972 3: DW_TAG_enumerator (enum1:0)
22973 3: DW_TAG_enumerator (enum2:1)
22974 ...
22975 2: DW_TAG_template_type_param
22976 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
22977
22978 Besides being broken debug info, it can put GDB into an
22979 infinite loop. Consider:
22980
22981 When we're building the full name for Class<E>, we'll start
22982 at Class, and go look over its template type parameters,
22983 finding E. We'll then try to build the full name of E, and
22984 reach here. We're now trying to build the full name of E,
22985 and look over the parent DIE for containing scope. In the
22986 broken case, if we followed the parent DIE of E, we'd again
22987 find Class, and once again go look at its template type
22988 arguments, etc., etc. Simply don't consider such parent die
22989 as source-level parent of this die (it can't be, the language
22990 doesn't allow it), and break the loop here. */
22991 name = dwarf2_name (die, cu);
22992 parent_name = dwarf2_name (parent, cu);
b98664d3 22993 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22994 name ? name : "<unknown>",
22995 parent_name ? parent_name : "<unknown>");
22996 return "";
22997 }
63d06c5c 22998 else
0114d602
DJ
22999 switch (parent->tag)
23000 {
63d06c5c 23001 case DW_TAG_namespace:
0114d602 23002 parent_type = read_type_die (parent, cu);
acebe513
UW
23003 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23004 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23005 Work around this problem here. */
23006 if (cu->language == language_cplus
7d93a1e0 23007 && strcmp (parent_type->name (), "::") == 0)
acebe513 23008 return "";
0114d602 23009 /* We give a name to even anonymous namespaces. */
7d93a1e0 23010 return parent_type->name ();
63d06c5c 23011 case DW_TAG_class_type:
680b30c7 23012 case DW_TAG_interface_type:
63d06c5c 23013 case DW_TAG_structure_type:
0114d602 23014 case DW_TAG_union_type:
f55ee35c 23015 case DW_TAG_module:
0114d602 23016 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
23017 if (parent_type->name () != NULL)
23018 return parent_type->name ();
0114d602
DJ
23019 else
23020 /* An anonymous structure is only allowed non-static data
23021 members; no typedefs, no member functions, et cetera.
23022 So it does not need a prefix. */
23023 return "";
abc72ce4 23024 case DW_TAG_compile_unit:
95554aad 23025 case DW_TAG_partial_unit:
abc72ce4
DE
23026 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23027 if (cu->language == language_cplus
976ca316 23028 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
23029 && die->child != NULL
23030 && (die->tag == DW_TAG_class_type
23031 || die->tag == DW_TAG_structure_type
23032 || die->tag == DW_TAG_union_type))
23033 {
43816ebc 23034 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
23035 if (name != NULL)
23036 return name;
23037 }
23038 return "";
0a4b0913
AB
23039 case DW_TAG_subprogram:
23040 /* Nested subroutines in Fortran get a prefix with the name
23041 of the parent's subroutine. */
23042 if (cu->language == language_fortran)
23043 {
23044 if ((die->tag == DW_TAG_subprogram)
23045 && (dwarf2_name (parent, cu) != NULL))
23046 return dwarf2_name (parent, cu);
23047 }
23048 return determine_prefix (parent, cu);
3d567982
TT
23049 case DW_TAG_enumeration_type:
23050 parent_type = read_type_die (parent, cu);
3bc440a2 23051 if (parent_type->is_declared_class ())
3d567982 23052 {
7d93a1e0
SM
23053 if (parent_type->name () != NULL)
23054 return parent_type->name ();
3d567982
TT
23055 return "";
23056 }
23057 /* Fall through. */
63d06c5c 23058 default:
8176b9b8 23059 return determine_prefix (parent, cu);
63d06c5c 23060 }
63d06c5c
DC
23061}
23062
3e43a32a
MS
23063/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23064 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23065 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23066 an obconcat, otherwise allocate storage for the result. The CU argument is
23067 used to determine the language and hence, the appropriate separator. */
987504bb 23068
f55ee35c 23069#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
23070
23071static char *
f55ee35c 23072typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 23073 int physname, struct dwarf2_cu *cu)
63d06c5c 23074{
f55ee35c 23075 const char *lead = "";
5c315b68 23076 const char *sep;
63d06c5c 23077
3e43a32a
MS
23078 if (suffix == NULL || suffix[0] == '\0'
23079 || prefix == NULL || prefix[0] == '\0')
987504bb 23080 sep = "";
45280282
IB
23081 else if (cu->language == language_d)
23082 {
23083 /* For D, the 'main' function could be defined in any module, but it
23084 should never be prefixed. */
23085 if (strcmp (suffix, "D main") == 0)
23086 {
23087 prefix = "";
23088 sep = "";
23089 }
23090 else
23091 sep = ".";
23092 }
f55ee35c
JK
23093 else if (cu->language == language_fortran && physname)
23094 {
23095 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23096 DW_AT_MIPS_linkage_name is preferred and used instead. */
23097
23098 lead = "__";
23099 sep = "_MOD_";
23100 }
987504bb
JJ
23101 else
23102 sep = "::";
63d06c5c 23103
6dd47d34
DE
23104 if (prefix == NULL)
23105 prefix = "";
23106 if (suffix == NULL)
23107 suffix = "";
23108
987504bb
JJ
23109 if (obs == NULL)
23110 {
3e43a32a 23111 char *retval
224c3ddb
SM
23112 = ((char *)
23113 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 23114
f55ee35c
JK
23115 strcpy (retval, lead);
23116 strcat (retval, prefix);
6dd47d34
DE
23117 strcat (retval, sep);
23118 strcat (retval, suffix);
63d06c5c
DC
23119 return retval;
23120 }
987504bb
JJ
23121 else
23122 {
23123 /* We have an obstack. */
f55ee35c 23124 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23125 }
63d06c5c
DC
23126}
23127
71c25dea
TT
23128/* Get name of a die, return NULL if not found. */
23129
15d034d0
TT
23130static const char *
23131dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 23132 struct objfile *objfile)
71c25dea
TT
23133{
23134 if (name && cu->language == language_cplus)
23135 {
596dc4ad
TT
23136 gdb::unique_xmalloc_ptr<char> canon_name
23137 = cp_canonicalize_string (name);
71c25dea 23138
596dc4ad
TT
23139 if (canon_name != nullptr)
23140 name = objfile->intern (canon_name.get ());
71c25dea
TT
23141 }
23142
23143 return name;
c906108c
SS
23144}
23145
96553a0c
DE
23146/* Get name of a die, return NULL if not found.
23147 Anonymous namespaces are converted to their magic string. */
9219021c 23148
15d034d0 23149static const char *
e142c38c 23150dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23151{
23152 struct attribute *attr;
5e22e966 23153 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 23154
e142c38c 23155 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23156 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23157 if (attr_name == nullptr
96553a0c 23158 && die->tag != DW_TAG_namespace
53832f31
TT
23159 && die->tag != DW_TAG_class_type
23160 && die->tag != DW_TAG_interface_type
23161 && die->tag != DW_TAG_structure_type
23162 && die->tag != DW_TAG_union_type)
71c25dea
TT
23163 return NULL;
23164
23165 switch (die->tag)
23166 {
23167 case DW_TAG_compile_unit:
95554aad 23168 case DW_TAG_partial_unit:
71c25dea
TT
23169 /* Compilation units have a DW_AT_name that is a filename, not
23170 a source language identifier. */
23171 case DW_TAG_enumeration_type:
23172 case DW_TAG_enumerator:
23173 /* These tags always have simple identifiers already; no need
23174 to canonicalize them. */
2c830f54 23175 return attr_name;
907af001 23176
96553a0c 23177 case DW_TAG_namespace:
2c830f54
TT
23178 if (attr_name != nullptr)
23179 return attr_name;
96553a0c
DE
23180 return CP_ANONYMOUS_NAMESPACE_STR;
23181
907af001
UW
23182 case DW_TAG_class_type:
23183 case DW_TAG_interface_type:
23184 case DW_TAG_structure_type:
23185 case DW_TAG_union_type:
23186 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23187 structures or unions. These were of the form "._%d" in GCC 4.1,
23188 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23189 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23190 if (attr_name != nullptr
23191 && (startswith (attr_name, "._")
23192 || startswith (attr_name, "<anonymous")))
907af001 23193 return NULL;
53832f31
TT
23194
23195 /* GCC might emit a nameless typedef that has a linkage name. See
23196 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23197 if (!attr || attr_name == NULL)
53832f31 23198 {
73b9be8b 23199 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23200 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23201 if (attr == NULL || attr_name == NULL)
53832f31
TT
23202 return NULL;
23203
2c830f54 23204 /* Avoid demangling attr_name the second time on a second
df5c6c50 23205 call for the same DIE. */
3b64bf15 23206 if (!attr->canonical_string_p ())
53832f31 23207 {
43816ebc 23208 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23209 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23210 if (demangled == nullptr)
23211 return nullptr;
43816ebc 23212
c6481205 23213 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23214 attr_name = attr->as_string ();
53832f31 23215 }
67430cd0 23216
2c830f54
TT
23217 /* Strip any leading namespaces/classes, keep only the
23218 base name. DW_AT_name for named DIEs does not
23219 contain the prefixes. */
23220 const char *base = strrchr (attr_name, ':');
23221 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23222 return &base[1];
23223 else
2c830f54 23224 return attr_name;
53832f31 23225 }
907af001
UW
23226 break;
23227
71c25dea 23228 default:
907af001
UW
23229 break;
23230 }
23231
3b64bf15 23232 if (!attr->canonical_string_p ())
c6481205
TT
23233 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23234 objfile));
2c830f54 23235 return attr->as_string ();
9219021c
DC
23236}
23237
23238/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23239 is none. *EXT_CU is the CU containing DIE on input, and the CU
23240 containing the return value on output. */
9219021c
DC
23241
23242static struct die_info *
f2f0e013 23243dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23244{
23245 struct attribute *attr;
9219021c 23246
f2f0e013 23247 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23248 if (attr == NULL)
23249 return NULL;
23250
f2f0e013 23251 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23252}
23253
f9aca02d 23254static void
d97bc12b 23255dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23256{
23257 unsigned int i;
23258
d97bc12b 23259 print_spaces (indent, f);
9d8780f0 23260 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23261 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23262 sect_offset_str (die->sect_off));
d97bc12b
DE
23263
23264 if (die->parent != NULL)
23265 {
23266 print_spaces (indent, f);
9d8780f0
SM
23267 fprintf_unfiltered (f, " parent at offset: %s\n",
23268 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23269 }
23270
23271 print_spaces (indent, f);
23272 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23273 dwarf_bool_name (die->child != NULL));
c906108c 23274
d97bc12b
DE
23275 print_spaces (indent, f);
23276 fprintf_unfiltered (f, " attributes:\n");
23277
c906108c
SS
23278 for (i = 0; i < die->num_attrs; ++i)
23279 {
d97bc12b
DE
23280 print_spaces (indent, f);
23281 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23282 dwarf_attr_name (die->attrs[i].name),
23283 dwarf_form_name (die->attrs[i].form));
d97bc12b 23284
c906108c
SS
23285 switch (die->attrs[i].form)
23286 {
c906108c 23287 case DW_FORM_addr:
336d760d 23288 case DW_FORM_addrx:
3019eac3 23289 case DW_FORM_GNU_addr_index:
d97bc12b 23290 fprintf_unfiltered (f, "address: ");
36d378cf 23291 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23292 break;
23293 case DW_FORM_block2:
23294 case DW_FORM_block4:
23295 case DW_FORM_block:
23296 case DW_FORM_block1:
56eb65bd 23297 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23298 pulongest (die->attrs[i].as_block ()->size));
c906108c 23299 break;
2dc7f7b3 23300 case DW_FORM_exprloc:
56eb65bd 23301 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23302 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23303 break;
0224619f
JK
23304 case DW_FORM_data16:
23305 fprintf_unfiltered (f, "constant of 16 bytes");
23306 break;
4568ecf9
DE
23307 case DW_FORM_ref_addr:
23308 fprintf_unfiltered (f, "ref address: ");
529908cb 23309 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23310 break;
36586728
TT
23311 case DW_FORM_GNU_ref_alt:
23312 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23313 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23314 break;
10b3939b
DJ
23315 case DW_FORM_ref1:
23316 case DW_FORM_ref2:
23317 case DW_FORM_ref4:
4568ecf9
DE
23318 case DW_FORM_ref8:
23319 case DW_FORM_ref_udata:
d97bc12b 23320 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23321 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23322 break;
c906108c
SS
23323 case DW_FORM_data1:
23324 case DW_FORM_data2:
23325 case DW_FORM_data4:
ce5d95e1 23326 case DW_FORM_data8:
c906108c 23327 case DW_FORM_udata:
43bbcdc2 23328 fprintf_unfiltered (f, "constant: %s",
529908cb 23329 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23330 break;
2dc7f7b3
TT
23331 case DW_FORM_sec_offset:
23332 fprintf_unfiltered (f, "section offset: %s",
529908cb 23333 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23334 break;
55f1336d 23335 case DW_FORM_ref_sig8:
ac9ec31b 23336 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23337 hex_string (die->attrs[i].as_signature ()));
348e048f 23338 break;
c906108c 23339 case DW_FORM_string:
4bdf3d34 23340 case DW_FORM_strp:
43988095 23341 case DW_FORM_line_strp:
cf532bd1 23342 case DW_FORM_strx:
3019eac3 23343 case DW_FORM_GNU_str_index:
36586728 23344 case DW_FORM_GNU_strp_alt:
8285870a 23345 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23346 die->attrs[i].as_string ()
23347 ? die->attrs[i].as_string () : "",
23348 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23349 break;
23350 case DW_FORM_flag:
c45bc3f8 23351 if (die->attrs[i].as_boolean ())
d97bc12b 23352 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23353 else
d97bc12b 23354 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23355 break;
2dc7f7b3
TT
23356 case DW_FORM_flag_present:
23357 fprintf_unfiltered (f, "flag: TRUE");
23358 break;
a8329558 23359 case DW_FORM_indirect:
0963b4bd
MS
23360 /* The reader will have reduced the indirect form to
23361 the "base form" so this form should not occur. */
5f48f8f3 23362 fprintf_unfiltered (f,
3e43a32a 23363 "unexpected attribute form: DW_FORM_indirect");
a8329558 23364 break;
1bc397c5 23365 case DW_FORM_sdata:
663c44ac
JK
23366 case DW_FORM_implicit_const:
23367 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23368 plongest (die->attrs[i].as_signed ()));
663c44ac 23369 break;
c906108c 23370 default:
d97bc12b 23371 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23372 die->attrs[i].form);
d97bc12b 23373 break;
c906108c 23374 }
d97bc12b 23375 fprintf_unfiltered (f, "\n");
c906108c
SS
23376 }
23377}
23378
f9aca02d 23379static void
d97bc12b 23380dump_die_for_error (struct die_info *die)
c906108c 23381{
d97bc12b
DE
23382 dump_die_shallow (gdb_stderr, 0, die);
23383}
23384
23385static void
23386dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23387{
23388 int indent = level * 4;
23389
23390 gdb_assert (die != NULL);
23391
23392 if (level >= max_level)
23393 return;
23394
23395 dump_die_shallow (f, indent, die);
23396
23397 if (die->child != NULL)
c906108c 23398 {
d97bc12b
DE
23399 print_spaces (indent, f);
23400 fprintf_unfiltered (f, " Children:");
23401 if (level + 1 < max_level)
23402 {
23403 fprintf_unfiltered (f, "\n");
23404 dump_die_1 (f, level + 1, max_level, die->child);
23405 }
23406 else
23407 {
3e43a32a
MS
23408 fprintf_unfiltered (f,
23409 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23410 }
23411 }
23412
23413 if (die->sibling != NULL && level > 0)
23414 {
23415 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23416 }
23417}
23418
d97bc12b
DE
23419/* This is called from the pdie macro in gdbinit.in.
23420 It's not static so gcc will keep a copy callable from gdb. */
23421
23422void
23423dump_die (struct die_info *die, int max_level)
23424{
23425 dump_die_1 (gdb_stdlog, 0, max_level, die);
23426}
23427
f9aca02d 23428static void
51545339 23429store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23430{
51545339 23431 void **slot;
c906108c 23432
9c541725
PA
23433 slot = htab_find_slot_with_hash (cu->die_hash, die,
23434 to_underlying (die->sect_off),
b64f50a1 23435 INSERT);
51545339
DJ
23436
23437 *slot = die;
c906108c
SS
23438}
23439
348e048f
DE
23440/* Follow reference or signature attribute ATTR of SRC_DIE.
23441 On entry *REF_CU is the CU of SRC_DIE.
23442 On exit *REF_CU is the CU of the result. */
23443
23444static struct die_info *
ff39bb5e 23445follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23446 struct dwarf2_cu **ref_cu)
23447{
23448 struct die_info *die;
23449
cd6c91b4 23450 if (attr->form_is_ref ())
348e048f 23451 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23452 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23453 die = follow_die_sig (src_die, attr, ref_cu);
23454 else
23455 {
23456 dump_die_for_error (src_die);
23457 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23458 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23459 }
23460
23461 return die;
03dd20cc
DJ
23462}
23463
5c631832 23464/* Follow reference OFFSET.
673bfd45
DE
23465 On entry *REF_CU is the CU of the source die referencing OFFSET.
23466 On exit *REF_CU is the CU of the result.
23467 Returns NULL if OFFSET is invalid. */
f504f079 23468
f9aca02d 23469static struct die_info *
9c541725 23470follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23471 struct dwarf2_cu **ref_cu)
c906108c 23472{
10b3939b 23473 struct die_info temp_die;
f2f0e013 23474 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23475 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23476
348e048f
DE
23477 gdb_assert (cu->per_cu != NULL);
23478
98bfdba5
PA
23479 target_cu = cu;
23480
17e593e9
SM
23481 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23482 "source CU contains target offset: %d",
23483 sect_offset_str (cu->per_cu->sect_off),
23484 sect_offset_str (sect_off),
23485 cu->header.offset_in_cu_p (sect_off));
23486
3019eac3 23487 if (cu->per_cu->is_debug_types)
348e048f
DE
23488 {
23489 /* .debug_types CUs cannot reference anything outside their CU.
23490 If they need to, they have to reference a signatured type via
55f1336d 23491 DW_FORM_ref_sig8. */
4057dfde 23492 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23493 return NULL;
348e048f 23494 }
36586728 23495 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23496 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23497 {
23498 struct dwarf2_per_cu_data *per_cu;
9a619af0 23499
9c541725 23500 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23501 per_objfile);
03dd20cc 23502
17e593e9
SM
23503 dwarf_read_debug_printf_v ("target CU offset: %s, "
23504 "target CU DIEs loaded: %d",
23505 sect_offset_str (per_cu->sect_off),
23506 per_objfile->get_cu (per_cu) != nullptr);
23507
616c069a
SM
23508 /* If necessary, add it to the queue and load its DIEs.
23509
23510 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23511 it doesn't mean they are currently loaded. Since we require them
23512 to be loaded, we must check for ourselves. */
23513 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23514 || per_objfile->get_cu (per_cu) == nullptr)
4a636814
SM
23515 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23516 false, cu->language);
03dd20cc 23517
976ca316 23518 target_cu = per_objfile->get_cu (per_cu);
616c069a 23519 gdb_assert (target_cu != nullptr);
10b3939b 23520 }
98bfdba5
PA
23521 else if (cu->dies == NULL)
23522 {
23523 /* We're loading full DIEs during partial symbol reading. */
976ca316 23524 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23525 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23526 language_minimal);
98bfdba5 23527 }
c906108c 23528
f2f0e013 23529 *ref_cu = target_cu;
9c541725 23530 temp_die.sect_off = sect_off;
c24bdb02
KS
23531
23532 if (target_cu != cu)
23533 target_cu->ancestor = cu;
23534
9a3c8263 23535 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23536 &temp_die,
23537 to_underlying (sect_off));
5c631832 23538}
10b3939b 23539
5c631832
JK
23540/* Follow reference attribute ATTR of SRC_DIE.
23541 On entry *REF_CU is the CU of SRC_DIE.
23542 On exit *REF_CU is the CU of the result. */
23543
23544static struct die_info *
ff39bb5e 23545follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23546 struct dwarf2_cu **ref_cu)
23547{
0826b30a 23548 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23549 struct dwarf2_cu *cu = *ref_cu;
23550 struct die_info *die;
23551
9c541725 23552 die = follow_die_offset (sect_off,
36586728
TT
23553 (attr->form == DW_FORM_GNU_ref_alt
23554 || cu->per_cu->is_dwz),
23555 ref_cu);
5c631832 23556 if (!die)
9d8780f0
SM
23557 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23558 "at %s [in module %s]"),
23559 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23560 objfile_name (cu->per_objfile->objfile));
348e048f 23561
5c631832
JK
23562 return die;
23563}
23564
d4c9a4f8 23565/* See read.h. */
5c631832
JK
23566
23567struct dwarf2_locexpr_baton
9c541725 23568dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23569 dwarf2_per_cu_data *per_cu,
976ca316 23570 dwarf2_per_objfile *per_objfile,
041d9819
SM
23571 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23572 bool resolve_abstract_p)
5c631832 23573{
5c631832
JK
23574 struct die_info *die;
23575 struct attribute *attr;
23576 struct dwarf2_locexpr_baton retval;
976ca316 23577 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23578
976ca316 23579 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23580 if (cu == nullptr)
976ca316 23581 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23582
23583 if (cu == nullptr)
cc12ce38
DE
23584 {
23585 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23586 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23587 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23588 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23589 }
918dd910 23590
9c541725 23591 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23592 if (!die)
9d8780f0
SM
23593 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23594 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23595
23596 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23597 if (!attr && resolve_abstract_p
976ca316
SM
23598 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23599 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23600 {
041d9819 23601 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23602 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23603 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23604
3360b6e7 23605 for (const auto &cand_off
976ca316 23606 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23607 {
3360b6e7
TV
23608 struct dwarf2_cu *cand_cu = cu;
23609 struct die_info *cand
23610 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23611 if (!cand
23612 || !cand->parent
e4a62c65
TV
23613 || cand->parent->tag != DW_TAG_subprogram)
23614 continue;
23615
23616 CORE_ADDR pc_low, pc_high;
23617 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23618 if (pc_low == ((CORE_ADDR) -1))
23619 continue;
23620 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23621 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23622 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23623 continue;
23624
23625 die = cand;
23626 attr = dwarf2_attr (die, DW_AT_location, cu);
23627 break;
23628 }
23629 }
23630
5c631832
JK
23631 if (!attr)
23632 {
e103e986
JK
23633 /* DWARF: "If there is no such attribute, then there is no effect.".
23634 DATA is ignored if SIZE is 0. */
5c631832 23635
e103e986 23636 retval.data = NULL;
5c631832
JK
23637 retval.size = 0;
23638 }
cd6c91b4 23639 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23640 {
23641 struct dwarf2_loclist_baton loclist_baton;
041d9819 23642 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23643 size_t size;
23644
23645 fill_in_loclist_baton (cu, &loclist_baton, attr);
23646
23647 retval.data = dwarf2_find_location_expression (&loclist_baton,
23648 &size, pc);
23649 retval.size = size;
23650 }
5c631832
JK
23651 else
23652 {
4fc6c0d5 23653 if (!attr->form_is_block ())
9d8780f0 23654 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23655 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23656 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23657
9d2246fc
TT
23658 struct dwarf_block *block = attr->as_block ();
23659 retval.data = block->data;
23660 retval.size = block->size;
5c631832 23661 }
976ca316 23662 retval.per_objfile = per_objfile;
5c631832 23663 retval.per_cu = cu->per_cu;
918dd910 23664
976ca316 23665 per_objfile->age_comp_units ();
918dd910 23666
5c631832 23667 return retval;
348e048f
DE
23668}
23669
d4c9a4f8 23670/* See read.h. */
8b9737bf
TT
23671
23672struct dwarf2_locexpr_baton
23673dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23674 dwarf2_per_cu_data *per_cu,
14095eb3 23675 dwarf2_per_objfile *per_objfile,
041d9819 23676 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23677{
9c541725 23678 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23679
14095eb3 23680 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23681 get_frame_pc);
8b9737bf
TT
23682}
23683
b6807d98
TT
23684/* Write a constant of a given type as target-ordered bytes into
23685 OBSTACK. */
23686
23687static const gdb_byte *
23688write_constant_as_bytes (struct obstack *obstack,
23689 enum bfd_endian byte_order,
23690 struct type *type,
23691 ULONGEST value,
23692 LONGEST *len)
23693{
23694 gdb_byte *result;
23695
23696 *len = TYPE_LENGTH (type);
224c3ddb 23697 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23698 store_unsigned_integer (result, *len, byte_order, value);
23699
23700 return result;
23701}
23702
d4c9a4f8 23703/* See read.h. */
b6807d98
TT
23704
23705const gdb_byte *
9c541725 23706dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23707 dwarf2_per_cu_data *per_cu,
14095eb3 23708 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23709 obstack *obstack,
b6807d98
TT
23710 LONGEST *len)
23711{
b6807d98
TT
23712 struct die_info *die;
23713 struct attribute *attr;
23714 const gdb_byte *result = NULL;
23715 struct type *type;
23716 LONGEST value;
23717 enum bfd_endian byte_order;
14095eb3 23718 struct objfile *objfile = per_objfile->objfile;
b6807d98 23719
7188ed02 23720 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23721 if (cu == nullptr)
23722 cu = load_cu (per_cu, per_objfile, false);
23723
23724 if (cu == nullptr)
cc12ce38
DE
23725 {
23726 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23727 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23728 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23729 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23730 }
b6807d98 23731
9c541725 23732 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23733 if (!die)
9d8780f0
SM
23734 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23735 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23736
23737 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23738 if (attr == NULL)
23739 return NULL;
23740
e3b94546 23741 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23742 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23743
23744 switch (attr->form)
23745 {
23746 case DW_FORM_addr:
336d760d 23747 case DW_FORM_addrx:
b6807d98
TT
23748 case DW_FORM_GNU_addr_index:
23749 {
23750 gdb_byte *tem;
23751
23752 *len = cu->header.addr_size;
224c3ddb 23753 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23754 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23755 result = tem;
23756 }
23757 break;
23758 case DW_FORM_string:
23759 case DW_FORM_strp:
cf532bd1 23760 case DW_FORM_strx:
b6807d98
TT
23761 case DW_FORM_GNU_str_index:
23762 case DW_FORM_GNU_strp_alt:
c6481205 23763 /* The string is already allocated on the objfile obstack, point
b6807d98 23764 directly to it. */
2c830f54
TT
23765 {
23766 const char *attr_name = attr->as_string ();
23767 result = (const gdb_byte *) attr_name;
23768 *len = strlen (attr_name);
23769 }
b6807d98
TT
23770 break;
23771 case DW_FORM_block1:
23772 case DW_FORM_block2:
23773 case DW_FORM_block4:
23774 case DW_FORM_block:
23775 case DW_FORM_exprloc:
0224619f 23776 case DW_FORM_data16:
9d2246fc
TT
23777 {
23778 struct dwarf_block *block = attr->as_block ();
23779 result = block->data;
23780 *len = block->size;
23781 }
b6807d98
TT
23782 break;
23783
23784 /* The DW_AT_const_value attributes are supposed to carry the
23785 symbol's value "represented as it would be on the target
23786 architecture." By the time we get here, it's already been
23787 converted to host endianness, so we just need to sign- or
23788 zero-extend it as appropriate. */
23789 case DW_FORM_data1:
23790 type = die_type (die, cu);
23791 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23792 if (result == NULL)
23793 result = write_constant_as_bytes (obstack, byte_order,
23794 type, value, len);
23795 break;
23796 case DW_FORM_data2:
23797 type = die_type (die, cu);
23798 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23799 if (result == NULL)
23800 result = write_constant_as_bytes (obstack, byte_order,
23801 type, value, len);
23802 break;
23803 case DW_FORM_data4:
23804 type = die_type (die, cu);
23805 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23806 if (result == NULL)
23807 result = write_constant_as_bytes (obstack, byte_order,
23808 type, value, len);
23809 break;
23810 case DW_FORM_data8:
23811 type = die_type (die, cu);
23812 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23813 if (result == NULL)
23814 result = write_constant_as_bytes (obstack, byte_order,
23815 type, value, len);
23816 break;
23817
23818 case DW_FORM_sdata:
663c44ac 23819 case DW_FORM_implicit_const:
b6807d98
TT
23820 type = die_type (die, cu);
23821 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23822 type, attr->as_signed (), len);
b6807d98
TT
23823 break;
23824
23825 case DW_FORM_udata:
23826 type = die_type (die, cu);
23827 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23828 type, attr->as_unsigned (), len);
b6807d98
TT
23829 break;
23830
23831 default:
b98664d3 23832 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23833 dwarf_form_name (attr->form));
23834 break;
23835 }
23836
23837 return result;
23838}
23839
d4c9a4f8 23840/* See read.h. */
7942e96e
AA
23841
23842struct type *
9c541725 23843dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
23844 dwarf2_per_cu_data *per_cu,
23845 dwarf2_per_objfile *per_objfile)
7942e96e 23846{
7942e96e
AA
23847 struct die_info *die;
23848
7188ed02 23849 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23850 if (cu == nullptr)
23851 cu = load_cu (per_cu, per_objfile, false);
23852
23853 if (cu == nullptr)
23854 return nullptr;
7942e96e 23855
9c541725 23856 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23857 if (!die)
23858 return NULL;
23859
23860 return die_type (die, cu);
23861}
23862
8cb5117c 23863/* See read.h. */
8a9b8146
TT
23864
23865struct type *
b64f50a1 23866dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23867 dwarf2_per_cu_data *per_cu,
23868 dwarf2_per_objfile *per_objfile)
8a9b8146 23869{
9c541725 23870 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23871 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23872}
23873
ac9ec31b 23874/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23875 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23876 On exit *REF_CU is the CU of the result.
23877 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23878
23879static struct die_info *
ac9ec31b
DE
23880follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23881 struct dwarf2_cu **ref_cu)
348e048f 23882{
348e048f 23883 struct die_info temp_die;
c24bdb02 23884 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 23885 struct die_info *die;
976ca316 23886 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23887
348e048f 23888
ac9ec31b
DE
23889 /* While it might be nice to assert sig_type->type == NULL here,
23890 we can get here for DW_AT_imported_declaration where we need
23891 the DIE not the type. */
348e048f 23892
616c069a 23893 /* If necessary, add it to the queue and load its DIEs.
348e048f 23894
616c069a
SM
23895 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23896 it doesn't mean they are currently loaded. Since we require them
23897 to be loaded, we must check for ourselves. */
976ca316 23898 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
616c069a
SM
23899 language_minimal)
23900 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
976ca316 23901 read_signatured_type (sig_type, per_objfile);
348e048f 23902
976ca316 23903 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 23904 gdb_assert (sig_cu != NULL);
9c541725
PA
23905 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23906 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23907 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23908 to_underlying (temp_die.sect_off));
348e048f
DE
23909 if (die)
23910 {
796a7ff8
DE
23911 /* For .gdb_index version 7 keep track of included TUs.
23912 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
23913 if (per_objfile->per_bfd->index_table != NULL
23914 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 23915 {
ae640021 23916 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23917 }
23918
348e048f 23919 *ref_cu = sig_cu;
c24bdb02
KS
23920 if (sig_cu != cu)
23921 sig_cu->ancestor = cu;
23922
348e048f
DE
23923 return die;
23924 }
23925
ac9ec31b
DE
23926 return NULL;
23927}
23928
23929/* Follow signatured type referenced by ATTR in SRC_DIE.
23930 On entry *REF_CU is the CU of SRC_DIE.
23931 On exit *REF_CU is the CU of the result.
23932 The result is the DIE of the type.
23933 If the referenced type cannot be found an error is thrown. */
23934
23935static struct die_info *
ff39bb5e 23936follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23937 struct dwarf2_cu **ref_cu)
23938{
630ed6b9 23939 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
23940 struct signatured_type *sig_type;
23941 struct die_info *die;
23942
23943 gdb_assert (attr->form == DW_FORM_ref_sig8);
23944
a2ce51a0 23945 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23946 /* sig_type will be NULL if the signatured type is missing from
23947 the debug info. */
23948 if (sig_type == NULL)
23949 {
23950 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
23951 " from DIE at %s [in module %s]"),
23952 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23953 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23954 }
23955
23956 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23957 if (die == NULL)
23958 {
23959 dump_die_for_error (src_die);
23960 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23961 " from DIE at %s [in module %s]"),
23962 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23963 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23964 }
23965
23966 return die;
23967}
23968
23969/* Get the type specified by SIGNATURE referenced in DIE/CU,
23970 reading in and processing the type unit if necessary. */
23971
23972static struct type *
23973get_signatured_type (struct die_info *die, ULONGEST signature,
23974 struct dwarf2_cu *cu)
23975{
976ca316 23976 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
23977 struct signatured_type *sig_type;
23978 struct dwarf2_cu *type_cu;
23979 struct die_info *type_die;
23980 struct type *type;
23981
a2ce51a0 23982 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23983 /* sig_type will be NULL if the signatured type is missing from
23984 the debug info. */
23985 if (sig_type == NULL)
23986 {
b98664d3 23987 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23988 " from DIE at %s [in module %s]"),
23989 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23990 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23991 return build_error_marker_type (cu, die);
23992 }
23993
23994 /* If we already know the type we're done. */
976ca316 23995 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
23996 if (type != nullptr)
23997 return type;
ac9ec31b
DE
23998
23999 type_cu = cu;
24000 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24001 if (type_die != NULL)
24002 {
24003 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24004 is created. This is important, for example, because for c++ classes
24005 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24006 type = read_type_die (type_die, type_cu);
24007 if (type == NULL)
24008 {
b98664d3 24009 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24010 " referenced from DIE at %s [in module %s]"),
24011 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24012 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24013 type = build_error_marker_type (cu, die);
24014 }
24015 }
24016 else
24017 {
b98664d3 24018 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24019 " from DIE at %s [in module %s]"),
24020 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24021 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24022 type = build_error_marker_type (cu, die);
24023 }
e286671b 24024
976ca316 24025 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
24026
24027 return type;
24028}
24029
24030/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24031 reading in and processing the type unit if necessary. */
24032
24033static struct type *
ff39bb5e 24034get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24035 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24036{
24037 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 24038 if (attr->form_is_ref ())
ac9ec31b
DE
24039 {
24040 struct dwarf2_cu *type_cu = cu;
24041 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24042
24043 return read_type_die (type_die, type_cu);
24044 }
24045 else if (attr->form == DW_FORM_ref_sig8)
24046 {
630ed6b9 24047 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
24048 }
24049 else
24050 {
976ca316 24051 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24052
b98664d3 24053 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24054 " at %s [in module %s]"),
24055 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 24056 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24057 return build_error_marker_type (cu, die);
24058 }
348e048f
DE
24059}
24060
e5fe5e75 24061/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24062
24063static void
ab432490
SM
24064load_full_type_unit (dwarf2_per_cu_data *per_cu,
24065 dwarf2_per_objfile *per_objfile)
348e048f 24066{
52dc124a 24067 struct signatured_type *sig_type;
348e048f 24068
f4dc4d17 24069 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 24070 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 24071
6721b2ec
DE
24072 /* We have the per_cu, but we need the signatured_type.
24073 Fortunately this is an easy translation. */
24074 gdb_assert (per_cu->is_debug_types);
24075 sig_type = (struct signatured_type *) per_cu;
348e048f 24076
7188ed02 24077 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24078
ab432490 24079 read_signatured_type (sig_type, per_objfile);
348e048f 24080
7188ed02 24081 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
24082}
24083
3019eac3
DE
24084/* Read in a signatured type and build its CU and DIEs.
24085 If the type is a stub for the real type in a DWO file,
24086 read in the real type from the DWO file as well. */
dee91e82
DE
24087
24088static void
ab432490
SM
24089read_signatured_type (signatured_type *sig_type,
24090 dwarf2_per_objfile *per_objfile)
dee91e82
DE
24091{
24092 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24093
3019eac3 24094 gdb_assert (per_cu->is_debug_types);
7188ed02 24095 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24096
2e671100 24097 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
24098
24099 if (!reader.dummy_p)
24100 {
24101 struct dwarf2_cu *cu = reader.cu;
24102 const gdb_byte *info_ptr = reader.info_ptr;
24103
24104 gdb_assert (cu->die_hash == NULL);
24105 cu->die_hash =
24106 htab_create_alloc_ex (cu->header.length / 12,
24107 die_hash,
24108 die_eq,
24109 NULL,
24110 &cu->comp_unit_obstack,
24111 hashtab_obstack_allocate,
24112 dummy_obstack_deallocate);
24113
3e225074 24114 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
24115 reader.comp_unit_die->child
24116 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24117 reader.comp_unit_die);
24118 cu->dies = reader.comp_unit_die;
24119 /* comp_unit_die is not stored in die_hash, no need. */
24120
24121 /* We try not to read any attributes in this function, because
24122 not all CUs needed for references have been loaded yet, and
24123 symbol table processing isn't initialized. But we have to
24124 set the CU language, or we won't be able to build types
24125 correctly. Similarly, if we do not read the producer, we can
24126 not apply producer-specific interpretation. */
24127 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
24128
24129 reader.keep ();
c0ab21c2
TT
24130 }
24131
7ee85ab1 24132 sig_type->per_cu.tu_read = 1;
c906108c
SS
24133}
24134
c906108c
SS
24135/* Decode simple location descriptions.
24136 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
24137 the location and return the value. If COMPUTED is non-null, it is
24138 set to true to indicate that decoding was successful, and false
24139 otherwise. If COMPUTED is null, then this function may emit a
24140 complaint. */
c906108c
SS
24141
24142static CORE_ADDR
7d79de9a 24143decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 24144{
5e22e966 24145 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
24146 size_t i;
24147 size_t size = blk->size;
d521ce57 24148 const gdb_byte *data = blk->data;
21ae7a4d
JK
24149 CORE_ADDR stack[64];
24150 int stacki;
24151 unsigned int bytes_read, unsnd;
24152 gdb_byte op;
c906108c 24153
7d79de9a
TT
24154 if (computed != nullptr)
24155 *computed = false;
24156
21ae7a4d
JK
24157 i = 0;
24158 stacki = 0;
24159 stack[stacki] = 0;
24160 stack[++stacki] = 0;
24161
24162 while (i < size)
24163 {
24164 op = data[i++];
24165 switch (op)
24166 {
24167 case DW_OP_lit0:
24168 case DW_OP_lit1:
24169 case DW_OP_lit2:
24170 case DW_OP_lit3:
24171 case DW_OP_lit4:
24172 case DW_OP_lit5:
24173 case DW_OP_lit6:
24174 case DW_OP_lit7:
24175 case DW_OP_lit8:
24176 case DW_OP_lit9:
24177 case DW_OP_lit10:
24178 case DW_OP_lit11:
24179 case DW_OP_lit12:
24180 case DW_OP_lit13:
24181 case DW_OP_lit14:
24182 case DW_OP_lit15:
24183 case DW_OP_lit16:
24184 case DW_OP_lit17:
24185 case DW_OP_lit18:
24186 case DW_OP_lit19:
24187 case DW_OP_lit20:
24188 case DW_OP_lit21:
24189 case DW_OP_lit22:
24190 case DW_OP_lit23:
24191 case DW_OP_lit24:
24192 case DW_OP_lit25:
24193 case DW_OP_lit26:
24194 case DW_OP_lit27:
24195 case DW_OP_lit28:
24196 case DW_OP_lit29:
24197 case DW_OP_lit30:
24198 case DW_OP_lit31:
24199 stack[++stacki] = op - DW_OP_lit0;
24200 break;
f1bea926 24201
21ae7a4d
JK
24202 case DW_OP_reg0:
24203 case DW_OP_reg1:
24204 case DW_OP_reg2:
24205 case DW_OP_reg3:
24206 case DW_OP_reg4:
24207 case DW_OP_reg5:
24208 case DW_OP_reg6:
24209 case DW_OP_reg7:
24210 case DW_OP_reg8:
24211 case DW_OP_reg9:
24212 case DW_OP_reg10:
24213 case DW_OP_reg11:
24214 case DW_OP_reg12:
24215 case DW_OP_reg13:
24216 case DW_OP_reg14:
24217 case DW_OP_reg15:
24218 case DW_OP_reg16:
24219 case DW_OP_reg17:
24220 case DW_OP_reg18:
24221 case DW_OP_reg19:
24222 case DW_OP_reg20:
24223 case DW_OP_reg21:
24224 case DW_OP_reg22:
24225 case DW_OP_reg23:
24226 case DW_OP_reg24:
24227 case DW_OP_reg25:
24228 case DW_OP_reg26:
24229 case DW_OP_reg27:
24230 case DW_OP_reg28:
24231 case DW_OP_reg29:
24232 case DW_OP_reg30:
24233 case DW_OP_reg31:
24234 stack[++stacki] = op - DW_OP_reg0;
24235 if (i < size)
7d79de9a
TT
24236 {
24237 if (computed == nullptr)
24238 dwarf2_complex_location_expr_complaint ();
24239 else
24240 return 0;
24241 }
21ae7a4d 24242 break;
c906108c 24243
21ae7a4d
JK
24244 case DW_OP_regx:
24245 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24246 i += bytes_read;
24247 stack[++stacki] = unsnd;
24248 if (i < size)
7d79de9a
TT
24249 {
24250 if (computed == nullptr)
24251 dwarf2_complex_location_expr_complaint ();
24252 else
24253 return 0;
24254 }
21ae7a4d 24255 break;
c906108c 24256
21ae7a4d 24257 case DW_OP_addr:
c8a7a66f
TT
24258 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24259 &bytes_read);
21ae7a4d
JK
24260 i += bytes_read;
24261 break;
d53d4ac5 24262
21ae7a4d
JK
24263 case DW_OP_const1u:
24264 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24265 i += 1;
24266 break;
24267
24268 case DW_OP_const1s:
24269 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24270 i += 1;
24271 break;
24272
24273 case DW_OP_const2u:
24274 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24275 i += 2;
24276 break;
24277
24278 case DW_OP_const2s:
24279 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24280 i += 2;
24281 break;
d53d4ac5 24282
21ae7a4d
JK
24283 case DW_OP_const4u:
24284 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24285 i += 4;
24286 break;
24287
24288 case DW_OP_const4s:
24289 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24290 i += 4;
24291 break;
24292
585861ea
JK
24293 case DW_OP_const8u:
24294 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24295 i += 8;
24296 break;
24297
21ae7a4d
JK
24298 case DW_OP_constu:
24299 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24300 &bytes_read);
24301 i += bytes_read;
24302 break;
24303
24304 case DW_OP_consts:
24305 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24306 i += bytes_read;
24307 break;
24308
24309 case DW_OP_dup:
24310 stack[stacki + 1] = stack[stacki];
24311 stacki++;
24312 break;
24313
24314 case DW_OP_plus:
24315 stack[stacki - 1] += stack[stacki];
24316 stacki--;
24317 break;
24318
24319 case DW_OP_plus_uconst:
24320 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24321 &bytes_read);
24322 i += bytes_read;
24323 break;
24324
24325 case DW_OP_minus:
24326 stack[stacki - 1] -= stack[stacki];
24327 stacki--;
24328 break;
24329
24330 case DW_OP_deref:
24331 /* If we're not the last op, then we definitely can't encode
24332 this using GDB's address_class enum. This is valid for partial
24333 global symbols, although the variable's address will be bogus
24334 in the psymtab. */
24335 if (i < size)
7d79de9a
TT
24336 {
24337 if (computed == nullptr)
24338 dwarf2_complex_location_expr_complaint ();
24339 else
24340 return 0;
24341 }
21ae7a4d
JK
24342 break;
24343
dda83cd7 24344 case DW_OP_GNU_push_tls_address:
4aa4e28b 24345 case DW_OP_form_tls_address:
21ae7a4d
JK
24346 /* The top of the stack has the offset from the beginning
24347 of the thread control block at which the variable is located. */
24348 /* Nothing should follow this operator, so the top of stack would
24349 be returned. */
24350 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24351 address will be bogus in the psymtab. Make it always at least
24352 non-zero to not look as a variable garbage collected by linker
24353 which have DW_OP_addr 0. */
21ae7a4d 24354 if (i < size)
7d79de9a
TT
24355 {
24356 if (computed == nullptr)
24357 dwarf2_complex_location_expr_complaint ();
24358 else
24359 return 0;
24360 }
585861ea 24361 stack[stacki]++;
dda83cd7 24362 break;
21ae7a4d
JK
24363
24364 case DW_OP_GNU_uninit:
7d79de9a
TT
24365 if (computed != nullptr)
24366 return 0;
21ae7a4d
JK
24367 break;
24368
336d760d 24369 case DW_OP_addrx:
3019eac3 24370 case DW_OP_GNU_addr_index:
49f6c839 24371 case DW_OP_GNU_const_index:
3019eac3
DE
24372 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24373 &bytes_read);
24374 i += bytes_read;
24375 break;
24376
21ae7a4d 24377 default:
7d79de9a
TT
24378 if (computed == nullptr)
24379 {
24380 const char *name = get_DW_OP_name (op);
21ae7a4d 24381
7d79de9a
TT
24382 if (name)
24383 complaint (_("unsupported stack op: '%s'"),
24384 name);
24385 else
24386 complaint (_("unsupported stack op: '%02x'"),
24387 op);
24388 }
21ae7a4d
JK
24389
24390 return (stack[stacki]);
d53d4ac5 24391 }
3c6e0cb3 24392
21ae7a4d 24393 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24394 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24395 if (stacki >= ARRAY_SIZE (stack) - 1)
24396 {
7d79de9a
TT
24397 if (computed == nullptr)
24398 complaint (_("location description stack overflow"));
21ae7a4d
JK
24399 return 0;
24400 }
24401
24402 if (stacki <= 0)
24403 {
7d79de9a
TT
24404 if (computed == nullptr)
24405 complaint (_("location description stack underflow"));
21ae7a4d
JK
24406 return 0;
24407 }
24408 }
7d79de9a
TT
24409
24410 if (computed != nullptr)
24411 *computed = true;
21ae7a4d 24412 return (stack[stacki]);
c906108c
SS
24413}
24414
24415/* memory allocation interface */
24416
c906108c 24417static struct dwarf_block *
7b5a2f43 24418dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24419{
8d749320 24420 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24421}
24422
c906108c 24423static struct die_info *
b60c80d6 24424dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24425{
24426 struct die_info *die;
b60c80d6
DJ
24427 size_t size = sizeof (struct die_info);
24428
24429 if (num_attrs > 1)
24430 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24431
b60c80d6 24432 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24433 memset (die, 0, sizeof (struct die_info));
24434 return (die);
24435}
2e276125
JB
24436
24437\f
a036ba48 24438
c90ec28a 24439/* Macro support. */
cf2c3c16 24440
9eac9650
TT
24441/* An overload of dwarf_decode_macros that finds the correct section
24442 and ensures it is read in before calling the other overload. */
24443
24444static void
24445dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24446 int section_is_gnu)
24447{
976ca316
SM
24448 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24449 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24450 const struct line_header *lh = cu->line_header;
9eac9650
TT
24451 unsigned int offset_size = cu->header.offset_size;
24452 struct dwarf2_section_info *section;
24453 const char *section_name;
24454
24455 if (cu->dwo_unit != nullptr)
24456 {
24457 if (section_is_gnu)
24458 {
24459 section = &cu->dwo_unit->dwo_file->sections.macro;
24460 section_name = ".debug_macro.dwo";
24461 }
24462 else
24463 {
24464 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24465 section_name = ".debug_macinfo.dwo";
24466 }
24467 }
24468 else
24469 {
24470 if (section_is_gnu)
24471 {
976ca316 24472 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24473 section_name = ".debug_macro";
24474 }
24475 else
24476 {
976ca316 24477 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24478 section_name = ".debug_macinfo";
24479 }
24480 }
24481
24482 section->read (objfile);
24483 if (section->buffer == nullptr)
24484 {
24485 complaint (_("missing %s section"), section_name);
24486 return;
24487 }
24488
24489 buildsym_compunit *builder = cu->get_builder ();
24490
048fde1e 24491 struct dwarf2_section_info *str_offsets_section;
24492 struct dwarf2_section_info *str_section;
24493 ULONGEST str_offsets_base;
24494
24495 if (cu->dwo_unit != nullptr)
24496 {
24497 str_offsets_section = &cu->dwo_unit->dwo_file
24498 ->sections.str_offsets;
24499 str_section = &cu->dwo_unit->dwo_file->sections.str;
24500 str_offsets_base = cu->header.addr_size;
24501 }
24502 else
24503 {
24504 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24505 str_section = &per_objfile->per_bfd->str;
24506 str_offsets_base = *cu->str_offsets_base;
24507 }
24508
976ca316 24509 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24510 offset_size, offset, str_section, str_offsets_section,
24511 str_offsets_base, section_is_gnu);
9eac9650
TT
24512}
24513
3019eac3
DE
24514/* Return the .debug_loc section to use for CU.
24515 For DWO files use .debug_loc.dwo. */
24516
24517static struct dwarf2_section_info *
24518cu_debug_loc_section (struct dwarf2_cu *cu)
24519{
976ca316 24520 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24521
3019eac3 24522 if (cu->dwo_unit)
43988095
JK
24523 {
24524 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24525
43988095
JK
24526 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24527 }
976ca316
SM
24528 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24529 : &per_objfile->per_bfd->loc);
3019eac3
DE
24530}
24531
d0ce17d8
CT
24532/* Return the .debug_rnglists section to use for CU. */
24533static struct dwarf2_section_info *
24534cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24535{
24536 if (cu->header.version < 5)
24537 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24538 cu->header.version);
24539 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24540
24541 /* Make sure we read the .debug_rnglists section from the file that
24542 contains the DW_AT_ranges attribute we are reading. Normally that
24543 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24544 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24545 program. */
24546 if (cu->dwo_unit != nullptr
24547 && tag != DW_TAG_compile_unit
24548 && tag != DW_TAG_skeleton_unit)
24549 {
24550 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24551
24552 if (sections->rnglists.size > 0)
24553 return &sections->rnglists;
24554 else
24555 error (_(".debug_rnglists section is missing from .dwo file."));
24556 }
24557 return &dwarf2_per_objfile->per_bfd->rnglists;
24558}
24559
8cf6f0b1
TT
24560/* A helper function that fills in a dwarf2_loclist_baton. */
24561
24562static void
24563fill_in_loclist_baton (struct dwarf2_cu *cu,
24564 struct dwarf2_loclist_baton *baton,
ff39bb5e 24565 const struct attribute *attr)
8cf6f0b1 24566{
976ca316 24567 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24568 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24569
976ca316 24570 section->read (per_objfile->objfile);
8cf6f0b1 24571
976ca316 24572 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24573 baton->per_cu = cu->per_cu;
24574 gdb_assert (baton->per_cu);
24575 /* We don't know how long the location list is, but make sure we
24576 don't run off the edge of the section. */
d4df075e
TT
24577 baton->size = section->size - attr->as_unsigned ();
24578 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24579 if (cu->base_address.has_value ())
24580 baton->base_address = *cu->base_address;
24581 else
24582 baton->base_address = 0;
f664829e 24583 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24584}
24585
4c2df51b 24586static void
ff39bb5e 24587dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24588 struct dwarf2_cu *cu, int is_block)
4c2df51b 24589{
976ca316
SM
24590 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24591 struct objfile *objfile = per_objfile->objfile;
3019eac3 24592 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24593
cd6c91b4 24594 if (attr->form_is_section_offset ()
3019eac3 24595 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24596 the section. If so, fall through to the complaint in the
24597 other branch. */
d4df075e 24598 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24599 {
0d53c4c4 24600 struct dwarf2_loclist_baton *baton;
4c2df51b 24601
8d749320 24602 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24603
8cf6f0b1 24604 fill_in_loclist_baton (cu, baton, attr);
be391dca 24605
2b24b6e4 24606 if (!cu->base_address.has_value ())
b98664d3 24607 complaint (_("Location list used without "
3e43a32a 24608 "specifying the CU base address."));
4c2df51b 24609
f1e6e072
TT
24610 SYMBOL_ACLASS_INDEX (sym) = (is_block
24611 ? dwarf2_loclist_block_index
24612 : dwarf2_loclist_index);
0d53c4c4
DJ
24613 SYMBOL_LOCATION_BATON (sym) = baton;
24614 }
24615 else
24616 {
24617 struct dwarf2_locexpr_baton *baton;
24618
8d749320 24619 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24620 baton->per_objfile = per_objfile;
ae0d2f24
UW
24621 baton->per_cu = cu->per_cu;
24622 gdb_assert (baton->per_cu);
0d53c4c4 24623
4fc6c0d5 24624 if (attr->form_is_block ())
0d53c4c4
DJ
24625 {
24626 /* Note that we're just copying the block's data pointer
24627 here, not the actual data. We're still pointing into the
6502dd73
DJ
24628 info_buffer for SYM's objfile; right now we never release
24629 that buffer, but when we do clean up properly this may
24630 need to change. */
9d2246fc
TT
24631 struct dwarf_block *block = attr->as_block ();
24632 baton->size = block->size;
24633 baton->data = block->data;
0d53c4c4
DJ
24634 }
24635 else
24636 {
24637 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24638 sym->natural_name ());
0d53c4c4 24639 baton->size = 0;
0d53c4c4 24640 }
6e70227d 24641
f1e6e072
TT
24642 SYMBOL_ACLASS_INDEX (sym) = (is_block
24643 ? dwarf2_locexpr_block_index
24644 : dwarf2_locexpr_index);
0d53c4c4
DJ
24645 SYMBOL_LOCATION_BATON (sym) = baton;
24646 }
4c2df51b 24647}
6502dd73 24648
2e6a9f79 24649/* See read.h. */
96408a79 24650
2e6a9f79
SM
24651const comp_unit_head *
24652dwarf2_per_cu_data::get_header () const
96408a79 24653{
2e6a9f79
SM
24654 if (!m_header_read_in)
24655 {
24656 const gdb_byte *info_ptr
24657 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24658
2e6a9f79 24659 memset (&m_header, 0, sizeof (m_header));
96408a79 24660
2e6a9f79
SM
24661 read_comp_unit_head (&m_header, info_ptr, this->section,
24662 rcuh_kind::COMPILE);
a9f172c6
TT
24663
24664 m_header_read_in = true;
2e6a9f79 24665 }
96408a79 24666
2e6a9f79 24667 return &m_header;
96408a79
SA
24668}
24669
09ba997f 24670/* See read.h. */
ae0d2f24 24671
98714339 24672int
09ba997f 24673dwarf2_per_cu_data::addr_size () const
ae0d2f24 24674{
2e6a9f79 24675 return this->get_header ()->addr_size;
ae0d2f24
UW
24676}
24677
09ba997f 24678/* See read.h. */
9eae7c52
TT
24679
24680int
09ba997f 24681dwarf2_per_cu_data::offset_size () const
9eae7c52 24682{
2e6a9f79 24683 return this->get_header ()->offset_size;
96408a79
SA
24684}
24685
09ba997f 24686/* See read.h. */
96408a79
SA
24687
24688int
09ba997f 24689dwarf2_per_cu_data::ref_addr_size () const
96408a79 24690{
2e6a9f79 24691 const comp_unit_head *header = this->get_header ();
96408a79 24692
2e6a9f79
SM
24693 if (header->version == 2)
24694 return header->addr_size;
96408a79 24695 else
2e6a9f79 24696 return header->offset_size;
181cebd4
JK
24697}
24698
09ba997f 24699/* See read.h. */
9aa1f1e3 24700
09ba997f 24701struct type *
293e7e51 24702dwarf2_cu::addr_type () const
9a49df9d 24703{
293e7e51 24704 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24705 struct type *void_type = objfile_type (objfile)->builtin_void;
24706 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24707 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24708
24709 if (TYPE_LENGTH (addr_type) == addr_size)
24710 return addr_type;
24711
c6d940a9 24712 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24713 return addr_type;
24714}
24715
22b6cd70
TT
24716/* A helper function for dwarf2_find_containing_comp_unit that returns
24717 the index of the result, and that searches a vector. It will
24718 return a result even if the offset in question does not actually
24719 occur in any CU. This is separate so that it can be unit
24720 tested. */
ae038cb0 24721
22b6cd70
TT
24722static int
24723dwarf2_find_containing_comp_unit
24724 (sect_offset sect_off,
24725 unsigned int offset_in_dwz,
24726 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24727{
ae038cb0
DJ
24728 int low, high;
24729
ae038cb0 24730 low = 0;
22b6cd70 24731 high = all_comp_units.size () - 1;
ae038cb0
DJ
24732 while (high > low)
24733 {
36586728 24734 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24735 int mid = low + (high - low) / 2;
9a619af0 24736
22b6cd70 24737 mid_cu = all_comp_units[mid];
36586728 24738 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24739 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24740 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24741 high = mid;
24742 else
24743 low = mid + 1;
24744 }
24745 gdb_assert (low == high);
22b6cd70
TT
24746 return low;
24747}
24748
24749/* Locate the .debug_info compilation unit from CU's objfile which contains
24750 the DIE at OFFSET. Raises an error on failure. */
24751
24752static struct dwarf2_per_cu_data *
24753dwarf2_find_containing_comp_unit (sect_offset sect_off,
24754 unsigned int offset_in_dwz,
976ca316 24755 dwarf2_per_objfile *per_objfile)
22b6cd70 24756{
976ca316
SM
24757 int low = dwarf2_find_containing_comp_unit
24758 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24759 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 24760
45b8ae0c 24761 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24762 {
36586728 24763 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24764 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24765 "offset %s [in module %s]"),
24766 sect_offset_str (sect_off),
976ca316 24767 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24768
976ca316 24769 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24770 <= sect_off);
976ca316 24771 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
24772 }
24773 else
24774 {
976ca316 24775 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24776 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24777 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24778 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24779 return this_cu;
24780 }
24781}
24782
22b6cd70
TT
24783#if GDB_SELF_TEST
24784
24785namespace selftests {
24786namespace find_containing_comp_unit {
24787
24788static void
24789run_test ()
24790{
24791 struct dwarf2_per_cu_data one {};
24792 struct dwarf2_per_cu_data two {};
24793 struct dwarf2_per_cu_data three {};
24794 struct dwarf2_per_cu_data four {};
24795
24796 one.length = 5;
24797 two.sect_off = sect_offset (one.length);
24798 two.length = 7;
24799
24800 three.length = 5;
24801 three.is_dwz = 1;
24802 four.sect_off = sect_offset (three.length);
24803 four.length = 7;
24804 four.is_dwz = 1;
24805
24806 std::vector<dwarf2_per_cu_data *> units;
24807 units.push_back (&one);
24808 units.push_back (&two);
24809 units.push_back (&three);
24810 units.push_back (&four);
24811
24812 int result;
24813
24814 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24815 SELF_CHECK (units[result] == &one);
24816 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24817 SELF_CHECK (units[result] == &one);
24818 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24819 SELF_CHECK (units[result] == &two);
24820
24821 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24822 SELF_CHECK (units[result] == &three);
24823 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24824 SELF_CHECK (units[result] == &three);
24825 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24826 SELF_CHECK (units[result] == &four);
24827}
24828
24829}
24830}
24831
24832#endif /* GDB_SELF_TEST */
24833
9e021579 24834/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 24835
9e021579
SM
24836dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24837 dwarf2_per_objfile *per_objfile)
24838 : per_cu (per_cu),
24839 per_objfile (per_objfile),
9068261f
AB
24840 mark (false),
24841 has_loclist (false),
24842 checked_producer (false),
24843 producer_is_gxx_lt_4_6 (false),
24844 producer_is_gcc_lt_4_3 (false),
eb77c9df 24845 producer_is_icc (false),
9068261f 24846 producer_is_icc_lt_14 (false),
c258c396 24847 producer_is_codewarrior (false),
9068261f 24848 processing_has_namespace_info (false)
93311388 24849{
9816fde3
JK
24850}
24851
24852/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24853
24854static void
95554aad
TT
24855prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24856 enum language pretend_language)
9816fde3
JK
24857{
24858 struct attribute *attr;
24859
24860 /* Set the language we're debugging. */
24861 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24862 if (attr != nullptr)
529908cb 24863 set_cu_language (attr->constant_value (0), cu);
9816fde3 24864 else
9cded63f 24865 {
95554aad 24866 cu->language = pretend_language;
9cded63f
TT
24867 cu->language_defn = language_def (cu->language);
24868 }
dee91e82 24869
7d45c7c3 24870 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24871}
24872
7188ed02 24873/* See read.h. */
ae038cb0 24874
7188ed02
SM
24875dwarf2_cu *
24876dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24877{
7188ed02
SM
24878 auto it = m_dwarf2_cus.find (per_cu);
24879 if (it == m_dwarf2_cus.end ())
24880 return nullptr;
ae038cb0 24881
7188ed02
SM
24882 return it->second;
24883}
24884
24885/* See read.h. */
24886
24887void
24888dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24889{
24890 gdb_assert (this->get_cu (per_cu) == nullptr);
24891
24892 m_dwarf2_cus[per_cu] = cu;
24893}
24894
24895/* See read.h. */
24896
24897void
24898dwarf2_per_objfile::age_comp_units ()
24899{
17e593e9
SM
24900 dwarf_read_debug_printf_v ("running");
24901
08ac5771
SM
24902 /* This is not expected to be called in the middle of CU expansion. There is
24903 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24904 loaded in memory. Calling age_comp_units while the queue is in use could
24905 make us free the DIEs for a CU that is in the queue and therefore break
24906 that invariant. */
24907 gdb_assert (!this->per_bfd->queue.has_value ());
24908
7188ed02
SM
24909 /* Start by clearing all marks. */
24910 for (auto pair : m_dwarf2_cus)
24911 pair.second->mark = false;
24912
24913 /* Traverse all CUs, mark them and their dependencies if used recently
24914 enough. */
24915 for (auto pair : m_dwarf2_cus)
ae038cb0 24916 {
7188ed02
SM
24917 dwarf2_cu *cu = pair.second;
24918
24919 cu->last_used++;
24920 if (cu->last_used <= dwarf_max_cache_age)
24921 dwarf2_mark (cu);
ae038cb0
DJ
24922 }
24923
7188ed02
SM
24924 /* Delete all CUs still not marked. */
24925 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 24926 {
7188ed02 24927 dwarf2_cu *cu = it->second;
ae038cb0 24928
7188ed02 24929 if (!cu->mark)
ae038cb0 24930 {
17e593e9
SM
24931 dwarf_read_debug_printf_v ("deleting old CU %s",
24932 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
24933 delete cu;
24934 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
24935 }
24936 else
7188ed02 24937 it++;
ae038cb0
DJ
24938 }
24939}
24940
7188ed02 24941/* See read.h. */
ae038cb0 24942
7188ed02
SM
24943void
24944dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24945{
7188ed02
SM
24946 auto it = m_dwarf2_cus.find (per_cu);
24947 if (it == m_dwarf2_cus.end ())
24948 return;
ae038cb0 24949
7188ed02 24950 delete it->second;
ae038cb0 24951
7188ed02
SM
24952 m_dwarf2_cus.erase (it);
24953}
ae038cb0 24954
7188ed02
SM
24955dwarf2_per_objfile::~dwarf2_per_objfile ()
24956{
24957 remove_all_cus ();
ae038cb0
DJ
24958}
24959
dee91e82
DE
24960/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24961 We store these in a hash table separate from the DIEs, and preserve them
24962 when the DIEs are flushed out of cache.
24963
24964 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24965 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24966 or the type may come from a DWO file. Furthermore, while it's more logical
24967 to use per_cu->section+offset, with Fission the section with the data is in
24968 the DWO file but we don't know that section at the point we need it.
24969 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24970 because we can enter the lookup routine, get_die_type_at_offset, from
24971 outside this file, and thus won't necessarily have PER_CU->cu.
24972 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24973
dee91e82 24974struct dwarf2_per_cu_offset_and_type
1c379e20 24975{
dee91e82 24976 const struct dwarf2_per_cu_data *per_cu;
9c541725 24977 sect_offset sect_off;
1c379e20
DJ
24978 struct type *type;
24979};
24980
dee91e82 24981/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24982
24983static hashval_t
dee91e82 24984per_cu_offset_and_type_hash (const void *item)
1c379e20 24985{
9a3c8263
SM
24986 const struct dwarf2_per_cu_offset_and_type *ofs
24987 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24988
9c541725 24989 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24990}
24991
dee91e82 24992/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24993
24994static int
dee91e82 24995per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24996{
9a3c8263
SM
24997 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24998 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24999 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25000 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25001
dee91e82 25002 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25003 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25004}
25005
25006/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25007 table if necessary. For convenience, return TYPE.
25008
25009 The DIEs reading must have careful ordering to:
85102364 25010 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25011 reading current DIE.
25012 * Not trying to dereference contents of still incompletely read in types
25013 while reading in other DIEs.
25014 * Enable referencing still incompletely read in types just by a pointer to
25015 the type without accessing its fields.
25016
25017 Therefore caller should follow these rules:
25018 * Try to fetch any prerequisite types we may need to build this DIE type
25019 before building the type and calling set_die_type.
e71ec853 25020 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25021 possible before fetching more types to complete the current type.
25022 * Make the type as complete as possible before fetching more types. */
1c379e20 25023
f792889a 25024static struct type *
57567375
TT
25025set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25026 bool skip_data_location)
1c379e20 25027{
976ca316 25028 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 25029 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 25030 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
25031 struct attribute *attr;
25032 struct dynamic_prop prop;
1c379e20 25033
b4ba55a1
JB
25034 /* For Ada types, make sure that the gnat-specific data is always
25035 initialized (if not already set). There are a few types where
25036 we should not be doing so, because the type-specific area is
25037 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25038 where the type-specific area is used to store the floatformat).
25039 But this is not a problem, because the gnat-specific information
25040 is actually not needed for these types. */
25041 if (need_gnat_info (cu)
78134374
SM
25042 && type->code () != TYPE_CODE_FUNC
25043 && type->code () != TYPE_CODE_FLT
25044 && type->code () != TYPE_CODE_METHODPTR
25045 && type->code () != TYPE_CODE_MEMBERPTR
25046 && type->code () != TYPE_CODE_METHOD
09584414 25047 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
25048 && !HAVE_GNAT_AUX_INFO (type))
25049 INIT_GNAT_SPECIFIC (type);
25050
3f2f83dd
KB
25051 /* Read DW_AT_allocated and set in type. */
25052 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 25053 if (attr != NULL)
3f2f83dd 25054 {
293e7e51 25055 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25056 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25057 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 25058 }
3f2f83dd
KB
25059
25060 /* Read DW_AT_associated and set in type. */
25061 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 25062 if (attr != NULL)
3f2f83dd 25063 {
293e7e51 25064 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25065 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25066 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 25067 }
3f2f83dd 25068
3cdcd0ce 25069 /* Read DW_AT_data_location and set in type. */
57567375
TT
25070 if (!skip_data_location)
25071 {
25072 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25073 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25074 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25075 }
3cdcd0ce 25076
976ca316
SM
25077 if (per_objfile->die_type_hash == NULL)
25078 per_objfile->die_type_hash
0335378b
TT
25079 = htab_up (htab_create_alloc (127,
25080 per_cu_offset_and_type_hash,
25081 per_cu_offset_and_type_eq,
25082 NULL, xcalloc, xfree));
1c379e20 25083
dee91e82 25084 ofs.per_cu = cu->per_cu;
9c541725 25085 ofs.sect_off = die->sect_off;
1c379e20 25086 ofs.type = type;
dee91e82 25087 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 25088 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 25089 if (*slot)
b98664d3 25090 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25091 sect_offset_str (die->sect_off));
8d749320
SM
25092 *slot = XOBNEW (&objfile->objfile_obstack,
25093 struct dwarf2_per_cu_offset_and_type);
1c379e20 25094 **slot = ofs;
f792889a 25095 return type;
1c379e20
DJ
25096}
25097
9c541725 25098/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25099 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25100
25101static struct type *
9c541725 25102get_die_type_at_offset (sect_offset sect_off,
aa66c379 25103 dwarf2_per_cu_data *per_cu,
976ca316 25104 dwarf2_per_objfile *per_objfile)
1c379e20 25105{
dee91e82 25106 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25107
976ca316 25108 if (per_objfile->die_type_hash == NULL)
f792889a 25109 return NULL;
1c379e20 25110
dee91e82 25111 ofs.per_cu = per_cu;
9c541725 25112 ofs.sect_off = sect_off;
9a3c8263 25113 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 25114 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
25115 if (slot)
25116 return slot->type;
25117 else
25118 return NULL;
25119}
25120
02142a6c 25121/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25122 or return NULL if DIE does not have a saved type. */
25123
25124static struct type *
25125get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25126{
aa66c379 25127 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
25128}
25129
10b3939b
DJ
25130/* Add a dependence relationship from CU to REF_PER_CU. */
25131
25132static void
25133dwarf2_add_dependence (struct dwarf2_cu *cu,
25134 struct dwarf2_per_cu_data *ref_per_cu)
25135{
25136 void **slot;
25137
25138 if (cu->dependencies == NULL)
25139 cu->dependencies
25140 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25141 NULL, &cu->comp_unit_obstack,
25142 hashtab_obstack_allocate,
25143 dummy_obstack_deallocate);
25144
25145 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25146 if (*slot == NULL)
25147 *slot = ref_per_cu;
25148}
1c379e20 25149
f504f079
DE
25150/* Subroutine of dwarf2_mark to pass to htab_traverse.
25151 Set the mark field in every compilation unit in the
7188ed02
SM
25152 cache that we must keep because we are keeping CU.
25153
25154 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 25155
10b3939b
DJ
25156static int
25157dwarf2_mark_helper (void **slot, void *data)
25158{
7188ed02
SM
25159 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25160 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25161 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25162
25163 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25164 reading of the chain. As such dependencies remain valid it is not much
25165 useful to track and undo them during QUIT cleanups. */
7188ed02 25166 if (cu == nullptr)
d07ed419
JK
25167 return 1;
25168
7188ed02 25169 if (cu->mark)
10b3939b 25170 return 1;
10b3939b 25171
7188ed02
SM
25172 cu->mark = true;
25173
25174 if (cu->dependencies != nullptr)
25175 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25176
25177 return 1;
25178}
25179
f504f079
DE
25180/* Set the mark field in CU and in every other compilation unit in the
25181 cache that we must keep because we are keeping CU. */
25182
ae038cb0
DJ
25183static void
25184dwarf2_mark (struct dwarf2_cu *cu)
25185{
25186 if (cu->mark)
25187 return;
7188ed02 25188
9068261f 25189 cu->mark = true;
ae038cb0 25190
7188ed02
SM
25191 if (cu->dependencies != nullptr)
25192 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25193}
25194
72bf9492
DJ
25195/* Trivial hash function for partial_die_info: the hash value of a DIE
25196 is its offset in .debug_info for this objfile. */
25197
25198static hashval_t
25199partial_die_hash (const void *item)
25200{
9a3c8263
SM
25201 const struct partial_die_info *part_die
25202 = (const struct partial_die_info *) item;
9a619af0 25203
9c541725 25204 return to_underlying (part_die->sect_off);
72bf9492
DJ
25205}
25206
25207/* Trivial comparison function for partial_die_info structures: two DIEs
25208 are equal if they have the same offset. */
25209
25210static int
25211partial_die_eq (const void *item_lhs, const void *item_rhs)
25212{
9a3c8263
SM
25213 const struct partial_die_info *part_die_lhs
25214 = (const struct partial_die_info *) item_lhs;
25215 const struct partial_die_info *part_die_rhs
25216 = (const struct partial_die_info *) item_rhs;
9a619af0 25217
9c541725 25218 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25219}
25220
3c3bb058
AB
25221struct cmd_list_element *set_dwarf_cmdlist;
25222struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25223
9291a0cd 25224static void
cd4fb1b2
SM
25225show_check_physname (struct ui_file *file, int from_tty,
25226 struct cmd_list_element *c, const char *value)
9291a0cd 25227{
cd4fb1b2
SM
25228 fprintf_filtered (file,
25229 _("Whether to check \"physname\" is %s.\n"),
25230 value);
9291a0cd
TT
25231}
25232
6c265988 25233void _initialize_dwarf2_read ();
cd4fb1b2 25234void
6c265988 25235_initialize_dwarf2_read ()
9291a0cd 25236{
0743fc83 25237 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25238Set DWARF specific variables.\n\
590042fc 25239Configure DWARF variables such as the cache size."),
0743fc83
TT
25240 &set_dwarf_cmdlist, "maintenance set dwarf ",
25241 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25242
0743fc83 25243 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25244Show DWARF specific variables.\n\
25245Show DWARF variables such as the cache size."),
0743fc83
TT
25246 &show_dwarf_cmdlist, "maintenance show dwarf ",
25247 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25248
cd4fb1b2
SM
25249 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25250 &dwarf_max_cache_age, _("\
25251Set the upper bound on the age of cached DWARF compilation units."), _("\
25252Show the upper bound on the age of cached DWARF compilation units."), _("\
25253A higher limit means that cached compilation units will be stored\n\
25254in memory longer, and more total memory will be used. Zero disables\n\
25255caching, which can slow down startup."),
25256 NULL,
25257 show_dwarf_max_cache_age,
25258 &set_dwarf_cmdlist,
25259 &show_dwarf_cmdlist);
156942c7 25260
cd4fb1b2
SM
25261 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25262Set debugging of the DWARF reader."), _("\
25263Show debugging of the DWARF reader."), _("\
25264When enabled (non-zero), debugging messages are printed during DWARF\n\
25265reading and symtab expansion. A value of 1 (one) provides basic\n\
25266information. A value greater than 1 provides more verbose information."),
25267 NULL,
25268 NULL,
25269 &setdebuglist, &showdebuglist);
9291a0cd 25270
cd4fb1b2
SM
25271 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25272Set debugging of the DWARF DIE reader."), _("\
25273Show debugging of the DWARF DIE reader."), _("\
25274When enabled (non-zero), DIEs are dumped after they are read in.\n\
25275The value is the maximum depth to print."),
25276 NULL,
25277 NULL,
25278 &setdebuglist, &showdebuglist);
9291a0cd 25279
cd4fb1b2
SM
25280 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25281Set debugging of the dwarf line reader."), _("\
25282Show debugging of the dwarf line reader."), _("\
25283When enabled (non-zero), line number entries are dumped as they are read in.\n\
25284A value of 1 (one) provides basic information.\n\
25285A value greater than 1 provides more verbose information."),
25286 NULL,
25287 NULL,
25288 &setdebuglist, &showdebuglist);
437afbb8 25289
cd4fb1b2
SM
25290 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25291Set cross-checking of \"physname\" code against demangler."), _("\
25292Show cross-checking of \"physname\" code against demangler."), _("\
25293When enabled, GDB's internal \"physname\" code is checked against\n\
25294the demangler."),
25295 NULL, show_check_physname,
25296 &setdebuglist, &showdebuglist);
900e11f9 25297
e615022a
DE
25298 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25299 no_class, &use_deprecated_index_sections, _("\
25300Set whether to use deprecated gdb_index sections."), _("\
25301Show whether to use deprecated gdb_index sections."), _("\
25302When enabled, deprecated .gdb_index sections are used anyway.\n\
25303Normally they are ignored either because of a missing feature or\n\
25304performance issue.\n\
25305Warning: This option must be enabled before gdb reads the file."),
25306 NULL,
25307 NULL,
25308 &setlist, &showlist);
25309
f1e6e072
TT
25310 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25311 &dwarf2_locexpr_funcs);
25312 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25313 &dwarf2_loclist_funcs);
25314
25315 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25316 &dwarf2_block_frame_base_locexpr_funcs);
25317 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25318 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25319
25320#if GDB_SELF_TEST
25321 selftests::register_test ("dw2_expand_symtabs_matching",
25322 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25323 selftests::register_test ("dwarf2_find_containing_comp_unit",
25324 selftests::find_containing_comp_unit::run_test);
c62446b1 25325#endif
6502dd73 25326}
This page took 5.782508 seconds and 4 git commands to generate.